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

source code of llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp