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