1 //===-- SIAnnotateControlFlow.cpp - ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Annotates the control flow with hardware specific intrinsics.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/DepthFirstIterator.h"
17 #include "llvm/Analysis/DivergenceAnalysis.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
25 #include "llvm/Transforms/Utils/SSAUpdater.h"
29 #define DEBUG_TYPE "si-annotate-control-flow"
33 // Complex types used in this pass
34 typedef std::pair<BasicBlock *, Value *> StackEntry;
35 typedef SmallVector<StackEntry, 16> StackVector;
37 // Intrinsic names the control flow is annotated with
38 static const char *const IfIntrinsic = "llvm.amdgcn.if";
39 static const char *const ElseIntrinsic = "llvm.amdgcn.else";
40 static const char *const BreakIntrinsic = "llvm.amdgcn.break";
41 static const char *const IfBreakIntrinsic = "llvm.amdgcn.if.break";
42 static const char *const ElseBreakIntrinsic = "llvm.amdgcn.else.break";
43 static const char *const LoopIntrinsic = "llvm.amdgcn.loop";
44 static const char *const EndCfIntrinsic = "llvm.amdgcn.end.cf";
46 class SIAnnotateControlFlow : public FunctionPass {
47 DivergenceAnalysis *DA;
54 ConstantInt *BoolTrue;
55 ConstantInt *BoolFalse;
56 UndefValue *BoolUndef;
72 bool isUniform(BranchInst *T);
74 bool isTopOfStack(BasicBlock *BB);
78 void push(BasicBlock *BB, Value *Saved);
80 bool isElse(PHINode *Phi);
82 void eraseIfUnused(PHINode *Phi);
84 void openIf(BranchInst *Term);
86 void insertElse(BranchInst *Term);
88 Value *handleLoopCondition(Value *Cond, PHINode *Broken,
89 llvm::Loop *L, BranchInst *Term);
91 void handleLoop(BranchInst *Term);
93 void closeControlFlow(BasicBlock *BB);
98 SIAnnotateControlFlow():
101 bool doInitialization(Module &M) override;
103 bool runOnFunction(Function &F) override;
105 StringRef getPassName() const override { return "SI annotate control flow"; }
107 void getAnalysisUsage(AnalysisUsage &AU) const override {
108 AU.addRequired<LoopInfoWrapperPass>();
109 AU.addRequired<DominatorTreeWrapperPass>();
110 AU.addRequired<DivergenceAnalysis>();
111 AU.addPreserved<DominatorTreeWrapperPass>();
112 FunctionPass::getAnalysisUsage(AU);
117 } // end anonymous namespace
119 INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
120 "Annotate SI Control Flow", false, false)
121 INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
122 INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
123 "Annotate SI Control Flow", false, false)
125 char SIAnnotateControlFlow::ID = 0;
127 /// \brief Initialize all the types and constants used in the pass
128 bool SIAnnotateControlFlow::doInitialization(Module &M) {
129 LLVMContext &Context = M.getContext();
131 Void = Type::getVoidTy(Context);
132 Boolean = Type::getInt1Ty(Context);
133 Int64 = Type::getInt64Ty(Context);
134 ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr);
136 BoolTrue = ConstantInt::getTrue(Context);
137 BoolFalse = ConstantInt::getFalse(Context);
138 BoolUndef = UndefValue::get(Boolean);
139 Int64Zero = ConstantInt::get(Int64, 0);
141 If = M.getOrInsertFunction(
142 IfIntrinsic, ReturnStruct, Boolean, (Type *)nullptr);
144 Else = M.getOrInsertFunction(
145 ElseIntrinsic, ReturnStruct, Int64, (Type *)nullptr);
147 Break = M.getOrInsertFunction(
148 BreakIntrinsic, Int64, Int64, (Type *)nullptr);
149 cast<Function>(Break)->setDoesNotAccessMemory();
151 IfBreak = M.getOrInsertFunction(
152 IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)nullptr);
153 cast<Function>(IfBreak)->setDoesNotAccessMemory();;
155 ElseBreak = M.getOrInsertFunction(
156 ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)nullptr);
157 cast<Function>(ElseBreak)->setDoesNotAccessMemory();
159 Loop = M.getOrInsertFunction(
160 LoopIntrinsic, Boolean, Int64, (Type *)nullptr);
162 EndCf = M.getOrInsertFunction(
163 EndCfIntrinsic, Void, Int64, (Type *)nullptr);
168 /// \brief Is the branch condition uniform or did the StructurizeCFG pass
169 /// consider it as such?
170 bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
171 return DA->isUniform(T->getCondition()) ||
172 T->getMetadata("structurizecfg.uniform") != nullptr;
175 /// \brief Is BB the last block saved on the stack ?
176 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
177 return !Stack.empty() && Stack.back().first == BB;
180 /// \brief Pop the last saved value from the control flow stack
181 Value *SIAnnotateControlFlow::popSaved() {
182 return Stack.pop_back_val().second;
185 /// \brief Push a BB and saved value to the control flow stack
186 void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
187 Stack.push_back(std::make_pair(BB, Saved));
190 /// \brief Can the condition represented by this PHI node treated like
192 bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
193 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
194 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
195 if (Phi->getIncomingBlock(i) == IDom) {
197 if (Phi->getIncomingValue(i) != BoolTrue)
201 if (Phi->getIncomingValue(i) != BoolFalse)
209 // \brief Erase "Phi" if it is not used any more
210 void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
211 if (!Phi->hasNUsesOrMore(1))
212 Phi->eraseFromParent();
215 /// \brief Open a new "If" block
216 void SIAnnotateControlFlow::openIf(BranchInst *Term) {
217 if (isUniform(Term)) {
220 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
221 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
222 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
225 /// \brief Close the last "If" block and open a new "Else" block
226 void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
227 if (isUniform(Term)) {
230 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
231 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
232 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
235 /// \brief Recursively handle the condition leading to a loop
236 Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken,
237 llvm::Loop *L, BranchInst *Term) {
239 // Only search through PHI nodes which are inside the loop. If we try this
240 // with PHI nodes that are outside of the loop, we end up inserting new PHI
241 // nodes outside of the loop which depend on values defined inside the loop.
242 // This will break the module with
243 // 'Instruction does not dominate all users!' errors.
244 PHINode *Phi = nullptr;
245 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
247 BasicBlock *Parent = Phi->getParent();
248 PHINode *NewPhi = PHINode::Create(Int64, 0, "", &Parent->front());
251 // Handle all non-constant incoming values first
252 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
253 Value *Incoming = Phi->getIncomingValue(i);
254 BasicBlock *From = Phi->getIncomingBlock(i);
255 if (isa<ConstantInt>(Incoming)) {
256 NewPhi->addIncoming(Broken, From);
260 Phi->setIncomingValue(i, BoolFalse);
261 Value *PhiArg = handleLoopCondition(Incoming, Broken, L, Term);
262 NewPhi->addIncoming(PhiArg, From);
265 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
267 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
269 Value *Incoming = Phi->getIncomingValue(i);
270 if (Incoming != BoolTrue)
273 BasicBlock *From = Phi->getIncomingBlock(i);
275 // We're in the following situation:
281 // where we want to break out of the loop if the If-block is not taken.
282 // Due to the depth-first traversal, there should be an end.cf
283 // intrinsic in Parent, and we insert an else.break before it.
285 // Note that the end.cf need not be the first non-phi instruction
286 // of parent, particularly when we're dealing with a multi-level
287 // break, but it should occur within a group of intrinsic calls
288 // at the beginning of the block.
289 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
290 while (OldEnd && OldEnd->getCalledFunction() != EndCf)
291 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode());
292 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
293 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
294 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
298 TerminatorInst *Insert = From->getTerminator();
299 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
300 NewPhi->setIncomingValue(i, PhiArg);
305 } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
306 BasicBlock *Parent = Inst->getParent();
308 if (L->contains(Inst)) {
309 Insert = Parent->getTerminator();
311 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
313 Value *Args[] = { Cond, Broken };
314 return CallInst::Create(IfBreak, Args, "", Insert);
316 // Insert IfBreak before TERM for constant COND.
317 } else if (isa<ConstantInt>(Cond)) {
318 Value *Args[] = { Cond, Broken };
319 return CallInst::Create(IfBreak, Args, "", Term);
322 llvm_unreachable("Unhandled loop condition!");
327 /// \brief Handle a back edge (loop)
328 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
329 if (isUniform(Term)) {
333 BasicBlock *BB = Term->getParent();
334 llvm::Loop *L = LI->getLoopFor(BB);
337 BasicBlock *Target = Term->getSuccessor(1);
338 PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front());
340 Value *Cond = Term->getCondition();
341 Term->setCondition(BoolTrue);
342 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
344 for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target);
347 Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI);
350 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
351 push(Term->getSuccessor(0), Arg);
352 }/// \brief Close the last opened control flow
353 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
354 llvm::Loop *L = LI->getLoopFor(BB);
356 assert(Stack.back().first == BB);
358 if (L && L->getHeader() == BB) {
359 // We can't insert an EndCF call into a loop header, because it will
360 // get executed on every iteration of the loop, when it should be
361 // executed only once before the loop.
362 SmallVector <BasicBlock*, 8> Latches;
363 L->getLoopLatches(Latches);
365 std::vector<BasicBlock*> Preds;
366 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
367 if (!is_contained(Latches, *PI))
368 Preds.push_back(*PI);
370 BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false);
373 Value *Exec = popSaved();
374 if (!isa<UndefValue>(Exec))
375 CallInst::Create(EndCf, Exec, "", &*BB->getFirstInsertionPt());
378 /// \brief Annotate the control flow with intrinsics so the backend can
379 /// recognize if/then/else and loops.
380 bool SIAnnotateControlFlow::runOnFunction(Function &F) {
382 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
383 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
384 DA = &getAnalysis<DivergenceAnalysis>();
386 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
387 E = df_end(&F.getEntryBlock()); I != E; ++I) {
389 BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator());
391 if (!Term || Term->isUnconditional()) {
392 if (isTopOfStack(*I))
393 closeControlFlow(*I);
398 if (I.nodeVisited(Term->getSuccessor(1))) {
399 if (isTopOfStack(*I))
400 closeControlFlow(*I);
406 if (isTopOfStack(*I)) {
407 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
408 if (Phi && Phi->getParent() == *I && isElse(Phi)) {
413 closeControlFlow(*I);
418 assert(Stack.empty());
422 /// \brief Create the annotation pass
423 FunctionPass *llvm::createSIAnnotateControlFlowPass() {
424 return new SIAnnotateControlFlow();