1 //===- SIAnnotateControlFlow.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Annotates the control flow with hardware specific intrinsics.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AMDGPU.h"
15 #include "AMDGPUSubtarget.h"
16 #include "llvm/ADT/DepthFirstIterator.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/CodeGen/TargetPassConfig.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
42 #include "llvm/Transforms/Utils/Local.h"
43 #include <cassert>
44 #include <utility>
45 
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "si-annotate-control-flow"
49 
50 namespace {
51 
52 // Complex types used in this pass
53 using StackEntry = std::pair<BasicBlock *, Value *>;
54 using StackVector = SmallVector<StackEntry, 16>;
55 
56 class SIAnnotateControlFlow : public FunctionPass {
57   LegacyDivergenceAnalysis *DA;
58 
59   Type *Boolean;
60   Type *Void;
61   Type *IntMask;
62   Type *ReturnStruct;
63 
64   ConstantInt *BoolTrue;
65   ConstantInt *BoolFalse;
66   UndefValue *BoolUndef;
67   Constant *IntMaskZero;
68 
69   Function *If;
70   Function *Else;
71   Function *IfBreak;
72   Function *Loop;
73   Function *EndCf;
74 
75   DominatorTree *DT;
76   StackVector Stack;
77 
78   LoopInfo *LI;
79 
80   void initialize(Module &M, const GCNSubtarget &ST);
81 
82   bool isUniform(BranchInst *T);
83 
84   bool isTopOfStack(BasicBlock *BB);
85 
86   Value *popSaved();
87 
88   void push(BasicBlock *BB, Value *Saved);
89 
90   bool isElse(PHINode *Phi);
91 
92   void eraseIfUnused(PHINode *Phi);
93 
94   void openIf(BranchInst *Term);
95 
96   void insertElse(BranchInst *Term);
97 
98   Value *
99   handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
100                       BranchInst *Term);
101 
102   void handleLoop(BranchInst *Term);
103 
104   void closeControlFlow(BasicBlock *BB);
105 
106 public:
107   static char ID;
108 
109   SIAnnotateControlFlow() : FunctionPass(ID) {}
110 
111   bool runOnFunction(Function &F) override;
112 
113   StringRef getPassName() const override { return "SI annotate control flow"; }
114 
115   void getAnalysisUsage(AnalysisUsage &AU) const override {
116     AU.addRequired<LoopInfoWrapperPass>();
117     AU.addRequired<DominatorTreeWrapperPass>();
118     AU.addRequired<LegacyDivergenceAnalysis>();
119     AU.addPreserved<DominatorTreeWrapperPass>();
120     AU.addRequired<TargetPassConfig>();
121     FunctionPass::getAnalysisUsage(AU);
122   }
123 };
124 
125 } // end anonymous namespace
126 
127 INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
128                       "Annotate SI Control Flow", false, false)
129 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
130 INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
131 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
132 INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
133                     "Annotate SI Control Flow", false, false)
134 
135 char SIAnnotateControlFlow::ID = 0;
136 
137 /// Initialize all the types and constants used in the pass
138 void SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) {
139   LLVMContext &Context = M.getContext();
140 
141   Void = Type::getVoidTy(Context);
142   Boolean = Type::getInt1Ty(Context);
143   IntMask = ST.isWave32() ? Type::getInt32Ty(Context)
144                            : Type::getInt64Ty(Context);
145   ReturnStruct = StructType::get(Boolean, IntMask);
146 
147   BoolTrue = ConstantInt::getTrue(Context);
148   BoolFalse = ConstantInt::getFalse(Context);
149   BoolUndef = UndefValue::get(Boolean);
150   IntMaskZero = ConstantInt::get(IntMask, 0);
151 
152   If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if, { IntMask });
153   Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else,
154                                    { IntMask, IntMask });
155   IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break,
156                                       { IntMask, IntMask });
157   Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask });
158   EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask });
159 }
160 
161 /// Is the branch condition uniform or did the StructurizeCFG pass
162 /// consider it as such?
163 bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
164   return DA->isUniform(T) ||
165          T->getMetadata("structurizecfg.uniform") != nullptr;
166 }
167 
168 /// Is BB the last block saved on the stack ?
169 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
170   return !Stack.empty() && Stack.back().first == BB;
171 }
172 
173 /// Pop the last saved value from the control flow stack
174 Value *SIAnnotateControlFlow::popSaved() {
175   return Stack.pop_back_val().second;
176 }
177 
178 /// Push a BB and saved value to the control flow stack
179 void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
180   Stack.push_back(std::make_pair(BB, Saved));
181 }
182 
183 /// Can the condition represented by this PHI node treated like
184 /// an "Else" block?
185 bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
186   BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
187   for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
188     if (Phi->getIncomingBlock(i) == IDom) {
189 
190       if (Phi->getIncomingValue(i) != BoolTrue)
191         return false;
192 
193     } else {
194       if (Phi->getIncomingValue(i) != BoolFalse)
195         return false;
196 
197     }
198   }
199   return true;
200 }
201 
202 // Erase "Phi" if it is not used any more
203 void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
204   if (RecursivelyDeleteDeadPHINode(Phi)) {
205     LLVM_DEBUG(dbgs() << "Erased unused condition phi\n");
206   }
207 }
208 
209 /// Open a new "If" block
210 void SIAnnotateControlFlow::openIf(BranchInst *Term) {
211   if (isUniform(Term))
212     return;
213 
214   Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
215   Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
216   push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
217 }
218 
219 /// Close the last "If" block and open a new "Else" block
220 void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
221   if (isUniform(Term)) {
222     return;
223   }
224   Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
225   Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
226   push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
227 }
228 
229 /// Recursively handle the condition leading to a loop
230 Value *SIAnnotateControlFlow::handleLoopCondition(
231     Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) {
232   if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
233     BasicBlock *Parent = Inst->getParent();
234     Instruction *Insert;
235     if (L->contains(Inst)) {
236       Insert = Parent->getTerminator();
237     } else {
238       Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
239     }
240 
241     Value *Args[] = { Cond, Broken };
242     return CallInst::Create(IfBreak, Args, "", Insert);
243   }
244 
245   // Insert IfBreak in the loop header TERM for constant COND other than true.
246   if (isa<Constant>(Cond)) {
247     Instruction *Insert = Cond == BoolTrue ?
248       Term : L->getHeader()->getTerminator();
249 
250     Value *Args[] = { Cond, Broken };
251     return CallInst::Create(IfBreak, Args, "", Insert);
252   }
253 
254   llvm_unreachable("Unhandled loop condition!");
255 }
256 
257 /// Handle a back edge (loop)
258 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
259   if (isUniform(Term))
260     return;
261 
262   BasicBlock *BB = Term->getParent();
263   llvm::Loop *L = LI->getLoopFor(BB);
264   if (!L)
265     return;
266 
267   BasicBlock *Target = Term->getSuccessor(1);
268   PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken", &Target->front());
269 
270   Value *Cond = Term->getCondition();
271   Term->setCondition(BoolTrue);
272   Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
273 
274   for (BasicBlock *Pred : predecessors(Target)) {
275     Value *PHIValue = IntMaskZero;
276     if (Pred == BB) // Remember the value of the previous iteration.
277       PHIValue = Arg;
278     // If the backedge from Pred to Target could be executed before the exit
279     // of the loop at BB, it should not reset or change "Broken", which keeps
280     // track of the number of threads exited the loop at BB.
281     else if (L->contains(Pred) && DT->dominates(Pred, BB))
282       PHIValue = Broken;
283     Broken->addIncoming(PHIValue, Pred);
284   }
285 
286   Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
287 
288   push(Term->getSuccessor(0), Arg);
289 }
290 
291 /// Close the last opened control flow
292 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
293   llvm::Loop *L = LI->getLoopFor(BB);
294 
295   assert(Stack.back().first == BB);
296 
297   if (L && L->getHeader() == BB) {
298     // We can't insert an EndCF call into a loop header, because it will
299     // get executed on every iteration of the loop, when it should be
300     // executed only once before the loop.
301     SmallVector <BasicBlock *, 8> Latches;
302     L->getLoopLatches(Latches);
303 
304     SmallVector<BasicBlock *, 2> Preds;
305     for (BasicBlock *Pred : predecessors(BB)) {
306       if (!is_contained(Latches, Pred))
307         Preds.push_back(Pred);
308     }
309 
310     BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
311                                 false);
312   }
313 
314   Value *Exec = popSaved();
315   Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
316   if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
317     CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
318 }
319 
320 /// Annotate the control flow with intrinsics so the backend can
321 /// recognize if/then/else and loops.
322 bool SIAnnotateControlFlow::runOnFunction(Function &F) {
323   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
324   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
325   DA = &getAnalysis<LegacyDivergenceAnalysis>();
326   TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
327   const TargetMachine &TM = TPC.getTM<TargetMachine>();
328 
329   initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
330 
331   for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
332        E = df_end(&F.getEntryBlock()); I != E; ++I) {
333     BasicBlock *BB = *I;
334     BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
335 
336     if (!Term || Term->isUnconditional()) {
337       if (isTopOfStack(BB))
338         closeControlFlow(BB);
339 
340       continue;
341     }
342 
343     if (I.nodeVisited(Term->getSuccessor(1))) {
344       if (isTopOfStack(BB))
345         closeControlFlow(BB);
346 
347       handleLoop(Term);
348       continue;
349     }
350 
351     if (isTopOfStack(BB)) {
352       PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
353       if (Phi && Phi->getParent() == BB && isElse(Phi)) {
354         insertElse(Term);
355         eraseIfUnused(Phi);
356         continue;
357       }
358 
359       closeControlFlow(BB);
360     }
361 
362     openIf(Term);
363   }
364 
365   if (!Stack.empty()) {
366     // CFG was probably not structured.
367     report_fatal_error("failed to annotate CFG");
368   }
369 
370   return true;
371 }
372 
373 /// Create the annotation pass
374 FunctionPass *llvm::createSIAnnotateControlFlowPass() {
375   return new SIAnnotateControlFlow();
376 }
377