blob: b764ca7d7061797d46dbef987303caa43ded7883 [file] [log] [blame]
Eugene Zelenko59e12822017-08-08 00:47:13 +00001//===- SIAnnotateControlFlow.cpp ------------------------------------------===//
Tom Stellardf8794352012-12-19 22:10:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellardf8794352012-12-19 22:10:31 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Annotates the control flow with hardware specific intrinsics.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AMDGPU.h"
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +000015#include "AMDGPUSubtarget.h"
Tom Stellardf8794352012-12-19 22:10:31 +000016#include "llvm/ADT/DepthFirstIterator.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
Nicolai Haehnle35617ed2018-08-30 14:21:36 +000019#include "llvm/Analysis/LegacyDivergenceAnalysis.h"
Tom Stellard0f29de72015-02-05 15:32:15 +000020#include "llvm/Analysis/LoopInfo.h"
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +000021#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000022#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/CFG.h"
24#include "llvm/IR/Constant.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000025#include "llvm/IR/Constants.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000026#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000027#include "llvm/IR/Dominators.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000028#include "llvm/IR/Function.h"
29#include "llvm/IR/Instruction.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000030#include "llvm/IR/Instructions.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000031#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000033#include "llvm/IR/Type.h"
34#include "llvm/IR/ValueHandle.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000035#include "llvm/Pass.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000036#include "llvm/Support/Casting.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/raw_ostream.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000040#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Stanislav Mekhanoshin79b28282019-05-13 18:05:10 +000041#include "llvm/Transforms/Utils/Local.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000042#include <cassert>
43#include <utility>
Tom Stellardf8794352012-12-19 22:10:31 +000044
45using namespace llvm;
46
Chandler Carruth84e68b22014-04-22 02:41:26 +000047#define DEBUG_TYPE "si-annotate-control-flow"
48
Tom Stellardf8794352012-12-19 22:10:31 +000049namespace {
50
51// Complex types used in this pass
Eugene Zelenko59e12822017-08-08 00:47:13 +000052using StackEntry = std::pair<BasicBlock *, Value *>;
53using StackVector = SmallVector<StackEntry, 16>;
Tom Stellardf8794352012-12-19 22:10:31 +000054
Tom Stellardf8794352012-12-19 22:10:31 +000055class SIAnnotateControlFlow : public FunctionPass {
Nicolai Haehnle35617ed2018-08-30 14:21:36 +000056 LegacyDivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000057
Tom Stellardf8794352012-12-19 22:10:31 +000058 Type *Boolean;
59 Type *Void;
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +000060 Type *IntMask;
Tom Stellardf8794352012-12-19 22:10:31 +000061 Type *ReturnStruct;
62
63 ConstantInt *BoolTrue;
64 ConstantInt *BoolFalse;
65 UndefValue *BoolUndef;
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +000066 Constant *IntMaskZero;
Tom Stellardf8794352012-12-19 22:10:31 +000067
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000068 Function *If;
69 Function *Else;
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000070 Function *IfBreak;
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000071 Function *Loop;
72 Function *EndCf;
Tom Stellardf8794352012-12-19 22:10:31 +000073
74 DominatorTree *DT;
75 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000076
Tom Stellard0f29de72015-02-05 15:32:15 +000077 LoopInfo *LI;
78
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +000079 void initialize(Module &M, const GCNSubtarget &ST);
80
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000081 bool isUniform(BranchInst *T);
82
Tom Stellardf8794352012-12-19 22:10:31 +000083 bool isTopOfStack(BasicBlock *BB);
84
85 Value *popSaved();
86
87 void push(BasicBlock *BB, Value *Saved);
88
89 bool isElse(PHINode *Phi);
90
91 void eraseIfUnused(PHINode *Phi);
92
93 void openIf(BranchInst *Term);
94
95 void insertElse(BranchInst *Term);
96
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000097 Value *
98 handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
Nicolai Haehnle28212cc2018-10-31 13:26:48 +000099 BranchInst *Term);
Tom Stellardf8794352012-12-19 22:10:31 +0000100
101 void handleLoop(BranchInst *Term);
102
103 void closeControlFlow(BasicBlock *BB);
104
105public:
Tom Stellard77a17772016-01-20 15:48:27 +0000106 static char ID;
107
Eugene Zelenko59e12822017-08-08 00:47:13 +0000108 SIAnnotateControlFlow() : FunctionPass(ID) {}
Tom Stellardf8794352012-12-19 22:10:31 +0000109
Craig Topper5656db42014-04-29 07:57:24 +0000110 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000111
Mehdi Amini117296c2016-10-01 02:56:57 +0000112 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +0000113
Craig Topper5656db42014-04-29 07:57:24 +0000114 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +0000115 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000116 AU.addRequired<DominatorTreeWrapperPass>();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000117 AU.addRequired<LegacyDivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000118 AU.addPreserved<DominatorTreeWrapperPass>();
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000119 AU.addRequired<TargetPassConfig>();
Tom Stellardf8794352012-12-19 22:10:31 +0000120 FunctionPass::getAnalysisUsage(AU);
121 }
Tom Stellardf8794352012-12-19 22:10:31 +0000122};
123
124} // end anonymous namespace
125
Tom Stellard77a17772016-01-20 15:48:27 +0000126INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
127 "Annotate SI Control Flow", false, false)
Matt Arsenault31a58c62017-03-02 23:50:51 +0000128INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000129INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000130INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
Tom Stellard77a17772016-01-20 15:48:27 +0000131INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
132 "Annotate SI Control Flow", false, false)
133
Tom Stellardf8794352012-12-19 22:10:31 +0000134char SIAnnotateControlFlow::ID = 0;
135
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000136/// Initialize all the types and constants used in the pass
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000137void SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) {
Tom Stellardf8794352012-12-19 22:10:31 +0000138 LLVMContext &Context = M.getContext();
139
140 Void = Type::getVoidTy(Context);
141 Boolean = Type::getInt1Ty(Context);
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000142 IntMask = ST.isWave32() ? Type::getInt32Ty(Context)
143 : Type::getInt64Ty(Context);
144 ReturnStruct = StructType::get(Boolean, IntMask);
Tom Stellardf8794352012-12-19 22:10:31 +0000145
146 BoolTrue = ConstantInt::getTrue(Context);
147 BoolFalse = ConstantInt::getFalse(Context);
148 BoolUndef = UndefValue::get(Boolean);
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000149 IntMaskZero = ConstantInt::get(IntMask, 0);
Tom Stellardf8794352012-12-19 22:10:31 +0000150
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000151 If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if, { IntMask });
152 Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else,
153 { IntMask, IntMask });
154 IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break,
155 { IntMask, IntMask });
156 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask });
157 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask });
Tom Stellardf8794352012-12-19 22:10:31 +0000158}
159
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000160/// Is the branch condition uniform or did the StructurizeCFG pass
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000161/// consider it as such?
162bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
Rhys Perryf77e2e82019-01-07 15:52:28 +0000163 return DA->isUniform(T) ||
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000164 T->getMetadata("structurizecfg.uniform") != nullptr;
165}
166
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000167/// Is BB the last block saved on the stack ?
Tom Stellardf8794352012-12-19 22:10:31 +0000168bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
Michel Danzerae0a4032013-02-14 08:00:33 +0000169 return !Stack.empty() && Stack.back().first == BB;
Tom Stellardf8794352012-12-19 22:10:31 +0000170}
171
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000172/// Pop the last saved value from the control flow stack
Tom Stellardf8794352012-12-19 22:10:31 +0000173Value *SIAnnotateControlFlow::popSaved() {
174 return Stack.pop_back_val().second;
175}
176
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000177/// Push a BB and saved value to the control flow stack
Tom Stellardf8794352012-12-19 22:10:31 +0000178void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
179 Stack.push_back(std::make_pair(BB, Saved));
180}
181
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000182/// Can the condition represented by this PHI node treated like
Tom Stellardf8794352012-12-19 22:10:31 +0000183/// an "Else" block?
184bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
Tom Stellardf8794352012-12-19 22:10:31 +0000185 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
186 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
187 if (Phi->getIncomingBlock(i) == IDom) {
188
189 if (Phi->getIncomingValue(i) != BoolTrue)
190 return false;
191
192 } else {
193 if (Phi->getIncomingValue(i) != BoolFalse)
194 return false;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000195
Tom Stellardf8794352012-12-19 22:10:31 +0000196 }
197 }
198 return true;
199}
200
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000201// Erase "Phi" if it is not used any more
Tom Stellardf8794352012-12-19 22:10:31 +0000202void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
Eugene Zelenko59e12822017-08-08 00:47:13 +0000203 if (RecursivelyDeleteDeadPHINode(Phi)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000204 LLVM_DEBUG(dbgs() << "Erased unused condition phi\n");
Matt Arsenault0607a442017-03-24 20:57:10 +0000205 }
Tom Stellardf8794352012-12-19 22:10:31 +0000206}
207
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000208/// Open a new "If" block
Tom Stellardf8794352012-12-19 22:10:31 +0000209void SIAnnotateControlFlow::openIf(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000210 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000211 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000212
Tom Stellardf8794352012-12-19 22:10:31 +0000213 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
214 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
215 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
216}
217
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000218/// Close the last "If" block and open a new "Else" block
Tom Stellardf8794352012-12-19 22:10:31 +0000219void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000220 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000221 return;
222 }
Tom Stellardf8794352012-12-19 22:10:31 +0000223 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
224 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
225 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
226}
227
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000228/// Recursively handle the condition leading to a loop
Matt Arsenault0607a442017-03-24 20:57:10 +0000229Value *SIAnnotateControlFlow::handleLoopCondition(
Nicolai Haehnle28212cc2018-10-31 13:26:48 +0000230 Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000231 if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000232 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000233 Instruction *Insert;
234 if (L->contains(Inst)) {
235 Insert = Parent->getTerminator();
236 } else {
237 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
238 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000239
Tom Stellardde16a2e2014-06-20 17:06:02 +0000240 Value *Args[] = { Cond, Broken };
241 return CallInst::Create(IfBreak, Args, "", Insert);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000242 }
Tom Stellardf8794352012-12-19 22:10:31 +0000243
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000244 // Insert IfBreak in the loop header TERM for constant COND other than true.
245 if (isa<Constant>(Cond)) {
246 Instruction *Insert = Cond == BoolTrue ?
247 Term : L->getHeader()->getTerminator();
248
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000249 Value *Args[] = { Cond, Broken };
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000250 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000251 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000252
253 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000254}
255
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000256/// Handle a back edge (loop)
Tom Stellardf8794352012-12-19 22:10:31 +0000257void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000258 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000259 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000260
Tom Stellardd4a19502015-04-14 14:36:45 +0000261 BasicBlock *BB = Term->getParent();
262 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000263 if (!L)
264 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000265
Tom Stellardf8794352012-12-19 22:10:31 +0000266 BasicBlock *Target = Term->getSuccessor(1);
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000267 PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken", &Target->front());
Tom Stellardf8794352012-12-19 22:10:31 +0000268
Tom Stellardf8794352012-12-19 22:10:31 +0000269 Value *Cond = Term->getCondition();
270 Term->setCondition(BoolTrue);
Nicolai Haehnle28212cc2018-10-31 13:26:48 +0000271 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
Tom Stellardf8794352012-12-19 22:10:31 +0000272
Changpeng Fang989ec592019-03-15 21:02:48 +0000273 for (BasicBlock *Pred : predecessors(Target)) {
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000274 Value *PHIValue = IntMaskZero;
Changpeng Fang989ec592019-03-15 21:02:48 +0000275 if (Pred == BB) // Remember the value of the previous iteration.
276 PHIValue = Arg;
277 // If the backedge from Pred to Target could be executed before the exit
278 // of the loop at BB, it should not reset or change "Broken", which keeps
279 // track of the number of threads exited the loop at BB.
280 else if (L->contains(Pred) && DT->dominates(Pred, BB))
281 PHIValue = Broken;
282 Broken->addIncoming(PHIValue, Pred);
283 }
Tom Stellardf8794352012-12-19 22:10:31 +0000284
285 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
Matt Arsenault0607a442017-03-24 20:57:10 +0000286
Tom Stellardf8794352012-12-19 22:10:31 +0000287 push(Term->getSuccessor(0), Arg);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000288}
289
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000290/// Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000291void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000292 llvm::Loop *L = LI->getLoopFor(BB);
293
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000294 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000295
Tom Stellard0f29de72015-02-05 15:32:15 +0000296 if (L && L->getHeader() == BB) {
297 // We can't insert an EndCF call into a loop header, because it will
298 // get executed on every iteration of the loop, when it should be
299 // executed only once before the loop.
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000300 SmallVector <BasicBlock *, 8> Latches;
Tom Stellard0f29de72015-02-05 15:32:15 +0000301 L->getLoopLatches(Latches);
302
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000303 SmallVector<BasicBlock *, 2> Preds;
304 for (BasicBlock *Pred : predecessors(BB)) {
305 if (!is_contained(Latches, Pred))
306 Preds.push_back(Pred);
Tom Stellard0f29de72015-02-05 15:32:15 +0000307 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000308
Alina Sbirleaab6f84f72018-08-21 23:32:03 +0000309 BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
310 false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000311 }
312
Tom Stellardbc4497b2016-02-12 23:45:29 +0000313 Value *Exec = popSaved();
Changpeng Fang6b49fa42017-03-07 23:29:36 +0000314 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
315 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
316 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
Tom Stellardf8794352012-12-19 22:10:31 +0000317}
318
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000319/// Annotate the control flow with intrinsics so the backend can
Tom Stellardf8794352012-12-19 22:10:31 +0000320/// recognize if/then/else and loops.
321bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Chandler Carruth73523022014-01-13 13:07:17 +0000322 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000323 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000324 DA = &getAnalysis<LegacyDivergenceAnalysis>();
Stanislav Mekhanoshin68a2fef2019-06-13 23:47:36 +0000325 TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
326 const TargetMachine &TM = TPC.getTM<TargetMachine>();
327
328 initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
Tom Stellardf8794352012-12-19 22:10:31 +0000329
330 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
331 E = df_end(&F.getEntryBlock()); I != E; ++I) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000332 BasicBlock *BB = *I;
333 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
Tom Stellardf8794352012-12-19 22:10:31 +0000334
335 if (!Term || Term->isUnconditional()) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000336 if (isTopOfStack(BB))
337 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000338
Tom Stellardf8794352012-12-19 22:10:31 +0000339 continue;
340 }
341
342 if (I.nodeVisited(Term->getSuccessor(1))) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000343 if (isTopOfStack(BB))
344 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000345
Tom Stellardf8794352012-12-19 22:10:31 +0000346 handleLoop(Term);
347 continue;
348 }
349
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000350 if (isTopOfStack(BB)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000351 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000352 if (Phi && Phi->getParent() == BB && isElse(Phi)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000353 insertElse(Term);
354 eraseIfUnused(Phi);
355 continue;
356 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000357
358 closeControlFlow(BB);
Tom Stellardf8794352012-12-19 22:10:31 +0000359 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000360
Tom Stellardf8794352012-12-19 22:10:31 +0000361 openIf(Term);
362 }
363
Matt Arsenault1491ca82018-01-17 16:30:01 +0000364 if (!Stack.empty()) {
365 // CFG was probably not structured.
366 report_fatal_error("failed to annotate CFG");
367 }
368
Tom Stellardf8794352012-12-19 22:10:31 +0000369 return true;
370}
371
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000372/// Create the annotation pass
Tom Stellardf8794352012-12-19 22:10:31 +0000373FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000374 return new SIAnnotateControlFlow();
375}