#include "llvm/Transforms/Utils/SSAUpdater.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
#include <cassert>
#include <utility>
using namespace llvm;
#define DEBUG_TYPE "ssaupdater"
using AvailableValsTy = DenseMap<BasicBlock *, Value *>;
static AvailableValsTy &getAvailableVals(void *AV) {
return *static_cast<AvailableValsTy*>(AV);
}
SSAUpdater::SSAUpdater(SmallVectorImpl<PHINode *> *NewPHI)
: InsertedPHIs(NewPHI) {}
SSAUpdater::~SSAUpdater() {
delete static_cast<AvailableValsTy*>(AV);
}
void SSAUpdater::Initialize(Type *Ty, StringRef Name) {
if (!AV)
AV = new AvailableValsTy();
else
getAvailableVals(AV).clear();
ProtoType = Ty;
ProtoName = std::string(Name);
}
bool SSAUpdater::HasValueForBlock(BasicBlock *BB) const {
return getAvailableVals(AV).count(BB);
}
Value *SSAUpdater::FindValueForBlock(BasicBlock *BB) const {
return getAvailableVals(AV).lookup(BB);
}
void SSAUpdater::AddAvailableValue(BasicBlock *BB, Value *V) {
assert(ProtoType && "Need to initialize SSAUpdater");
assert(ProtoType == V->getType() &&
"All rewritten values must have the same type");
getAvailableVals(AV)[BB] = V;
}
static bool IsEquivalentPHI(PHINode *PHI,
SmallDenseMap<BasicBlock *, Value *, 8> &ValueMapping) {
unsigned PHINumValues = PHI->getNumIncomingValues();
if (PHINumValues != ValueMapping.size())
return false;
for (unsigned i = 0, e = PHINumValues; i != e; ++i)
if (ValueMapping[PHI->getIncomingBlock(i)] !=
PHI->getIncomingValue(i)) {
return false;
}
return true;
}
Value *SSAUpdater::GetValueAtEndOfBlock(BasicBlock *BB) {
Value *Res = GetValueAtEndOfBlockInternal(BB);
return Res;
}
Value *SSAUpdater::GetValueInMiddleOfBlock(BasicBlock *BB) {
if (!HasValueForBlock(BB))
return GetValueAtEndOfBlock(BB);
SmallVector<std::pair<BasicBlock *, Value *>, 8> PredValues;
Value *SingularValue = nullptr;
if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) {
for (unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) {
BasicBlock *PredBB = SomePhi->getIncomingBlock(i);
Value *PredVal = GetValueAtEndOfBlock(PredBB);
PredValues.push_back(std::make_pair(PredBB, PredVal));
if (i == 0)
SingularValue = PredVal;
else if (PredVal != SingularValue)
SingularValue = nullptr;
}
} else {
bool isFirstPred = true;
for (BasicBlock *PredBB : predecessors(BB)) {
Value *PredVal = GetValueAtEndOfBlock(PredBB);
PredValues.push_back(std::make_pair(PredBB, PredVal));
if (isFirstPred) {
SingularValue = PredVal;
isFirstPred = false;
} else if (PredVal != SingularValue)
SingularValue = nullptr;
}
}
if (PredValues.empty())
return UndefValue::get(ProtoType);
if (SingularValue)
return SingularValue;
if (isa<PHINode>(BB->begin())) {
SmallDenseMap<BasicBlock *, Value *, 8> ValueMapping(PredValues.begin(),
PredValues.end());
for (PHINode &SomePHI : BB->phis()) {
if (IsEquivalentPHI(&SomePHI, ValueMapping))
return &SomePHI;
}
}
PHINode *InsertedPHI = PHINode::Create(ProtoType, PredValues.size(),
ProtoName, &BB->front());
for (const auto &PredValue : PredValues)
InsertedPHI->addIncoming(PredValue.second, PredValue.first);
if (Value *V =
simplifyInstruction(InsertedPHI, BB->getModule()->getDataLayout())) {
InsertedPHI->eraseFromParent();
return V;
}
DebugLoc DL;
if (const Instruction *I = BB->getFirstNonPHI())
DL = I->getDebugLoc();
InsertedPHI->setDebugLoc(DL);
if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
return InsertedPHI;
}
void SSAUpdater::RewriteUse(Use &U) {
Instruction *User = cast<Instruction>(U.getUser());
Value *V;
if (PHINode *UserPN = dyn_cast<PHINode>(User))
V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U));
else
V = GetValueInMiddleOfBlock(User->getParent());
U.set(V);
}
void SSAUpdater::RewriteUseAfterInsertions(Use &U) {
Instruction *User = cast<Instruction>(U.getUser());
Value *V;
if (PHINode *UserPN = dyn_cast<PHINode>(User))
V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U));
else
V = GetValueAtEndOfBlock(User->getParent());
U.set(V);
}
namespace llvm {
template<>
class SSAUpdaterTraits<SSAUpdater> {
public:
using BlkT = BasicBlock;
using ValT = Value *;
using PhiT = PHINode;
using BlkSucc_iterator = succ_iterator;
static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return succ_begin(BB); }
static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return succ_end(BB); }
class PHI_iterator {
private:
PHINode *PHI;
unsigned idx;
public:
explicit PHI_iterator(PHINode *P) : PHI(P), idx(0) {}
PHI_iterator(PHINode *P, bool) : PHI(P), idx(PHI->getNumIncomingValues()) {}
PHI_iterator &operator++() { ++idx; return *this; }
bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
Value *getIncomingValue() { return PHI->getIncomingValue(idx); }
BasicBlock *getIncomingBlock() { return PHI->getIncomingBlock(idx); }
};
static PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
static PHI_iterator PHI_end(PhiT *PHI) {
return PHI_iterator(PHI, true);
}
static void FindPredecessorBlocks(BasicBlock *BB,
SmallVectorImpl<BasicBlock *> *Preds) {
if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin()))
append_range(*Preds, SomePhi->blocks());
else
append_range(*Preds, predecessors(BB));
}
static Value *GetUndefVal(BasicBlock *BB, SSAUpdater *Updater) {
return UndefValue::get(Updater->ProtoType);
}
static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds,
SSAUpdater *Updater) {
PHINode *PHI = PHINode::Create(Updater->ProtoType, NumPreds,
Updater->ProtoName, &BB->front());
return PHI;
}
static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred) {
PHI->addIncoming(Val, Pred);
}
static PHINode *ValueIsPHI(Value *Val, SSAUpdater *Updater) {
return dyn_cast<PHINode>(Val);
}
static PHINode *ValueIsNewPHI(Value *Val, SSAUpdater *Updater) {
PHINode *PHI = ValueIsPHI(Val, Updater);
if (PHI && PHI->getNumIncomingValues() == 0)
return PHI;
return nullptr;
}
static Value *GetPHIValue(PHINode *PHI) {
return PHI;
}
};
}
Value *SSAUpdater::GetValueAtEndOfBlockInternal(BasicBlock *BB) {
AvailableValsTy &AvailableVals = getAvailableVals(AV);
if (Value *V = AvailableVals[BB])
return V;
SSAUpdaterImpl<SSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
return Impl.GetValue(BB);
}
LoadAndStorePromoter::
LoadAndStorePromoter(ArrayRef<const Instruction *> Insts,
SSAUpdater &S, StringRef BaseName) : SSA(S) {
if (Insts.empty()) return;
const Value *SomeVal;
if (const LoadInst *LI = dyn_cast<LoadInst>(Insts[0]))
SomeVal = LI;
else
SomeVal = cast<StoreInst>(Insts[0])->getOperand(0);
if (BaseName.empty())
BaseName = SomeVal->getName();
SSA.Initialize(SomeVal->getType(), BaseName);
}
void LoadAndStorePromoter::run(const SmallVectorImpl<Instruction *> &Insts) {
DenseMap<BasicBlock *, TinyPtrVector<Instruction *>> UsesByBlock;
for (Instruction *User : Insts)
UsesByBlock[User->getParent()].push_back(User);
SmallVector<LoadInst *, 32> LiveInLoads;
DenseMap<Value *, Value *> ReplacedLoads;
for (Instruction *User : Insts) {
BasicBlock *BB = User->getParent();
TinyPtrVector<Instruction *> &BlockUses = UsesByBlock[BB];
if (BlockUses.empty()) continue;
if (BlockUses.size() == 1) {
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
updateDebugInfo(SI);
SSA.AddAvailableValue(BB, SI->getOperand(0));
} else
LiveInLoads.push_back(cast<LoadInst>(User));
BlockUses.clear();
continue;
}
bool HasStore = false;
for (Instruction *I : BlockUses) {
if (isa<StoreInst>(I)) {
HasStore = true;
break;
}
}
if (!HasStore) {
for (Instruction *I : BlockUses)
LiveInLoads.push_back(cast<LoadInst>(I));
BlockUses.clear();
continue;
}
Value *StoredValue = nullptr;
for (Instruction &I : *BB) {
if (LoadInst *L = dyn_cast<LoadInst>(&I)) {
if (!isInstInList(L, Insts)) continue;
if (StoredValue) {
replaceLoadWithValue(L, StoredValue);
L->replaceAllUsesWith(StoredValue);
ReplacedLoads[L] = StoredValue;
} else {
LiveInLoads.push_back(L);
}
continue;
}
if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
if (!isInstInList(SI, Insts)) continue;
updateDebugInfo(SI);
StoredValue = SI->getOperand(0);
}
}
assert(StoredValue && "Already checked that there is a store in block");
SSA.AddAvailableValue(BB, StoredValue);
BlockUses.clear();
}
for (LoadInst *ALoad : LiveInLoads) {
Value *NewVal = SSA.GetValueInMiddleOfBlock(ALoad->getParent());
replaceLoadWithValue(ALoad, NewVal);
if (NewVal == ALoad) NewVal = UndefValue::get(NewVal->getType());
ALoad->replaceAllUsesWith(NewVal);
ReplacedLoads[ALoad] = NewVal;
}
doExtraRewritesBeforeFinalDeletion();
for (Instruction *User : Insts) {
if (!shouldDelete(User))
continue;
if (!User->use_empty()) {
Value *NewVal = ReplacedLoads[User];
assert(NewVal && "not a replaced load?");
DenseMap<Value*, Value*>::iterator RLI = ReplacedLoads.find(NewVal);
while (RLI != ReplacedLoads.end()) {
NewVal = RLI->second;
RLI = ReplacedLoads.find(NewVal);
}
replaceLoadWithValue(cast<LoadInst>(User), NewVal);
User->replaceAllUsesWith(NewVal);
}
instructionDeleted(User);
User->eraseFromParent();
}
}
bool
LoadAndStorePromoter::isInstInList(Instruction *I,
const SmallVectorImpl<Instruction *> &Insts)
const {
return is_contained(Insts, I);
}