1 //===- SIAnnotateControlFlow.cpp ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// Annotates the control flow with hardware specific intrinsics.
12 //===----------------------------------------------------------------------===//
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"
48 #define DEBUG_TYPE "si-annotate-control-flow"
52 // Complex types used in this pass
53 using StackEntry = std::pair<BasicBlock *, Value *>;
54 using StackVector = SmallVector<StackEntry, 16>;
56 class SIAnnotateControlFlow : public FunctionPass {
57 LegacyDivergenceAnalysis *DA;
64 ConstantInt *BoolTrue;
65 ConstantInt *BoolFalse;
66 UndefValue *BoolUndef;
67 Constant *IntMaskZero;
80 void initialize(Module &M, const GCNSubtarget &ST);
82 bool isUniform(BranchInst *T);
84 bool isTopOfStack(BasicBlock *BB);
88 void push(BasicBlock *BB, Value *Saved);
90 bool isElse(PHINode *Phi);
92 void eraseIfUnused(PHINode *Phi);
94 void openIf(BranchInst *Term);
96 void insertElse(BranchInst *Term);
99 handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
102 void handleLoop(BranchInst *Term);
104 void closeControlFlow(BasicBlock *BB);
109 SIAnnotateControlFlow() : FunctionPass(ID) {}
111 bool runOnFunction(Function &F) override;
113 StringRef getPassName() const override { return "SI annotate control flow"; }
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);
125 } // end anonymous namespace
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)
135 char SIAnnotateControlFlow::ID = 0;
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();
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);
147 BoolTrue = ConstantInt::getTrue(Context);
148 BoolFalse = ConstantInt::getFalse(Context);
149 BoolUndef = UndefValue::get(Boolean);
150 IntMaskZero = ConstantInt::get(IntMask, 0);
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,
157 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask });
158 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask });
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;
168 /// Is BB the last block saved on the stack ?
169 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
170 return !Stack.empty() && Stack.back().first == BB;
173 /// Pop the last saved value from the control flow stack
174 Value *SIAnnotateControlFlow::popSaved() {
175 return Stack.pop_back_val().second;
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));
183 /// Can the condition represented by this PHI node treated like
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) {
190 if (Phi->getIncomingValue(i) != BoolTrue)
194 if (Phi->getIncomingValue(i) != BoolFalse)
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");
209 /// Open a new "If" block
210 void SIAnnotateControlFlow::openIf(BranchInst *Term) {
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));
219 /// Close the last "If" block and open a new "Else" block
220 void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
221 if (isUniform(Term)) {
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));
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();
235 if (L->contains(Inst)) {
236 Insert = Parent->getTerminator();
238 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
241 Value *Args[] = { Cond, Broken };
242 return CallInst::Create(IfBreak, Args, "", Insert);
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();
250 Value *Args[] = { Cond, Broken };
251 return CallInst::Create(IfBreak, Args, "", Insert);
254 llvm_unreachable("Unhandled loop condition!");
257 /// Handle a back edge (loop)
258 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
262 BasicBlock *BB = Term->getParent();
263 llvm::Loop *L = LI->getLoopFor(BB);
267 BasicBlock *Target = Term->getSuccessor(1);
268 PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken", &Target->front());
270 Value *Cond = Term->getCondition();
271 Term->setCondition(BoolTrue);
272 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
274 for (BasicBlock *Pred : predecessors(Target)) {
275 Value *PHIValue = IntMaskZero;
276 if (Pred == BB) // Remember the value of the previous iteration.
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))
283 Broken->addIncoming(PHIValue, Pred);
286 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
288 push(Term->getSuccessor(0), Arg);
291 /// Close the last opened control flow
292 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
293 llvm::Loop *L = LI->getLoopFor(BB);
295 assert(Stack.back().first == BB);
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);
304 SmallVector<BasicBlock *, 2> Preds;
305 for (BasicBlock *Pred : predecessors(BB)) {
306 if (!is_contained(Latches, Pred))
307 Preds.push_back(Pred);
310 BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
314 Value *Exec = popSaved();
315 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
316 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
317 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
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>();
329 initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
331 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
332 E = df_end(&F.getEntryBlock()); I != E; ++I) {
334 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
336 if (!Term || Term->isUnconditional()) {
337 if (isTopOfStack(BB))
338 closeControlFlow(BB);
343 if (I.nodeVisited(Term->getSuccessor(1))) {
344 if (isTopOfStack(BB))
345 closeControlFlow(BB);
351 if (isTopOfStack(BB)) {
352 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
353 if (Phi && Phi->getParent() == BB && isElse(Phi)) {
359 closeControlFlow(BB);
365 if (!Stack.empty()) {
366 // CFG was probably not structured.
367 report_fatal_error("failed to annotate CFG");
373 /// Create the annotation pass
374 FunctionPass *llvm::createSIAnnotateControlFlowPass() {
375 return new SIAnnotateControlFlow();