#include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
#include "llvm/Analysis/IteratedDominanceFrontier.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/Value.h"
using namespace llvm;
#define DEBUG_TYPE "ssaupdaterbulk"
static BasicBlock *getUserBB(Use *U) {
auto *User = cast<Instruction>(U->getUser());
if (auto *UserPN = dyn_cast<PHINode>(User))
return UserPN->getIncomingBlock(*U);
else
return User->getParent();
}
unsigned SSAUpdaterBulk::AddVariable(StringRef Name, Type *Ty) {
unsigned Var = Rewrites.size();
LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": initialized with Ty = "
<< *Ty << ", Name = " << Name << "\n");
RewriteInfo RI(Name, Ty);
Rewrites.push_back(RI);
return Var;
}
void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) {
assert(Var < Rewrites.size() && "Variable not found!");
LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var
<< ": added new available value" << *V << " in "
<< BB->getName() << "\n");
Rewrites[Var].Defines[BB] = V;
}
void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
assert(Var < Rewrites.size() && "Variable not found!");
LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added a use" << *U->get()
<< " in " << getUserBB(U)->getName() << "\n");
Rewrites[Var].Uses.push_back(U);
}
Value *SSAUpdaterBulk::computeValueAt(BasicBlock *BB, RewriteInfo &R,
DominatorTree *DT) {
if (!R.Defines.count(BB)) {
if (DT->isReachableFromEntry(BB) && PredCache.get(BB).size()) {
BasicBlock *IDom = DT->getNode(BB)->getIDom()->getBlock();
Value *V = computeValueAt(IDom, R, DT);
R.Defines[BB] = V;
} else
R.Defines[BB] = UndefValue::get(R.Ty);
}
return R.Defines[BB];
}
static void
ComputeLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &UsingBlocks,
const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
SmallPtrSetImpl<BasicBlock *> &LiveInBlocks,
PredIteratorCache &PredCache) {
SmallVector<BasicBlock *, 64> LiveInBlockWorklist(UsingBlocks.begin(),
UsingBlocks.end());
while (!LiveInBlockWorklist.empty()) {
BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
if (!LiveInBlocks.insert(BB).second)
continue;
for (BasicBlock *P : PredCache.get(BB)) {
if (DefBlocks.count(P))
continue;
LiveInBlockWorklist.push_back(P);
}
}
}
void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
SmallVectorImpl<PHINode *> *InsertedPHIs) {
for (auto &R : Rewrites) {
ForwardIDFCalculator IDF(*DT);
LLVM_DEBUG(dbgs() << "SSAUpdater: rewriting " << R.Uses.size()
<< " use(s)\n");
SmallPtrSet<BasicBlock *, 2> DefBlocks;
for (auto &Def : R.Defines)
DefBlocks.insert(Def.first);
IDF.setDefiningBlocks(DefBlocks);
SmallPtrSet<BasicBlock *, 2> UsingBlocks;
for (Use *U : R.Uses)
UsingBlocks.insert(getUserBB(U));
SmallVector<BasicBlock *, 32> IDFBlocks;
SmallPtrSet<BasicBlock *, 32> LiveInBlocks;
ComputeLiveInBlocks(UsingBlocks, DefBlocks, LiveInBlocks, PredCache);
IDF.resetLiveInBlocks();
IDF.setLiveInBlocks(LiveInBlocks);
IDF.calculate(IDFBlocks);
SmallVector<PHINode *, 4> InsertedPHIsForVar;
for (auto *FrontierBB : IDFBlocks) {
IRBuilder<> B(FrontierBB, FrontierBB->begin());
PHINode *PN = B.CreatePHI(R.Ty, 0, R.Name);
R.Defines[FrontierBB] = PN;
InsertedPHIsForVar.push_back(PN);
if (InsertedPHIs)
InsertedPHIs->push_back(PN);
}
for (auto *PN : InsertedPHIsForVar) {
BasicBlock *PBB = PN->getParent();
for (BasicBlock *Pred : PredCache.get(PBB))
PN->addIncoming(computeValueAt(Pred, R, DT), Pred);
}
SmallPtrSet<Use *, 4> ProcessedUses;
for (Use *U : R.Uses) {
if (!ProcessedUses.insert(U).second)
continue;
Value *V = computeValueAt(getUserBB(U), R, DT);
Value *OldVal = U->get();
assert(OldVal && "Invalid use!");
if (OldVal != V && OldVal->hasValueHandle())
ValueHandleBase::ValueIsRAUWd(OldVal, V);
LLVM_DEBUG(dbgs() << "SSAUpdater: replacing " << *OldVal << " with " << *V
<< "\n");
U->set(V);
}
}
}