blob: c9834bf53648b6a9acdfdaa891a7076f2582b59f [file] [log] [blame]
Tom Stellardf8794352012-12-19 22:10:31 +00001//===-- SIAnnotateControlFlow.cpp - ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// Annotates the control flow with hardware specific intrinsics.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPU.h"
Tom Stellardf8794352012-12-19 22:10:31 +000016#include "llvm/ADT/DepthFirstIterator.h"
Tom Stellardbc4497b2016-02-12 23:45:29 +000017#include "llvm/Analysis/DivergenceAnalysis.h"
Tom Stellard0f29de72015-02-05 15:32:15 +000018#include "llvm/Analysis/LoopInfo.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000019#include "llvm/IR/Constants.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000020#include "llvm/IR/Dominators.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000021#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000023#include "llvm/Pass.h"
24#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Tom Stellardf8794352012-12-19 22:10:31 +000025#include "llvm/Transforms/Utils/SSAUpdater.h"
26
27using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "si-annotate-control-flow"
30
Tom Stellardf8794352012-12-19 22:10:31 +000031namespace {
32
33// Complex types used in this pass
34typedef std::pair<BasicBlock *, Value *> StackEntry;
35typedef SmallVector<StackEntry, 16> StackVector;
36
Tom Stellardf8794352012-12-19 22:10:31 +000037class SIAnnotateControlFlow : public FunctionPass {
Tom Stellardbc4497b2016-02-12 23:45:29 +000038 DivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000039
Tom Stellardf8794352012-12-19 22:10:31 +000040 Type *Boolean;
41 Type *Void;
42 Type *Int64;
43 Type *ReturnStruct;
44
45 ConstantInt *BoolTrue;
46 ConstantInt *BoolFalse;
47 UndefValue *BoolUndef;
48 Constant *Int64Zero;
49
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000050 Function *If;
51 Function *Else;
52 Function *Break;
53 Function *IfBreak;
54 Function *ElseBreak;
55 Function *Loop;
56 Function *EndCf;
Tom Stellardf8794352012-12-19 22:10:31 +000057
58 DominatorTree *DT;
59 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000060
Tom Stellard0f29de72015-02-05 15:32:15 +000061 LoopInfo *LI;
62
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000063 bool isUniform(BranchInst *T);
64
Tom Stellardf8794352012-12-19 22:10:31 +000065 bool isTopOfStack(BasicBlock *BB);
66
67 Value *popSaved();
68
69 void push(BasicBlock *BB, Value *Saved);
70
71 bool isElse(PHINode *Phi);
72
73 void eraseIfUnused(PHINode *Phi);
74
75 void openIf(BranchInst *Term);
76
77 void insertElse(BranchInst *Term);
78
Changpeng Fange07f1aa2016-02-12 17:11:04 +000079 Value *handleLoopCondition(Value *Cond, PHINode *Broken,
80 llvm::Loop *L, BranchInst *Term);
Tom Stellardf8794352012-12-19 22:10:31 +000081
82 void handleLoop(BranchInst *Term);
83
84 void closeControlFlow(BasicBlock *BB);
85
86public:
Tom Stellard77a17772016-01-20 15:48:27 +000087 static char ID;
88
Tom Stellardf8794352012-12-19 22:10:31 +000089 SIAnnotateControlFlow():
90 FunctionPass(ID) { }
91
Craig Topper5656db42014-04-29 07:57:24 +000092 bool doInitialization(Module &M) override;
Tom Stellardf8794352012-12-19 22:10:31 +000093
Craig Topper5656db42014-04-29 07:57:24 +000094 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +000095
Mehdi Amini117296c2016-10-01 02:56:57 +000096 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +000097
Craig Topper5656db42014-04-29 07:57:24 +000098 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +000099 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000100 AU.addRequired<DominatorTreeWrapperPass>();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000101 AU.addRequired<DivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000102 AU.addPreserved<DominatorTreeWrapperPass>();
Tom Stellardf8794352012-12-19 22:10:31 +0000103 FunctionPass::getAnalysisUsage(AU);
104 }
105
106};
107
108} // end anonymous namespace
109
Tom Stellard77a17772016-01-20 15:48:27 +0000110INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
111 "Annotate SI Control Flow", false, false)
Matt Arsenault31a58c62017-03-02 23:50:51 +0000112INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Tom Stellardbc4497b2016-02-12 23:45:29 +0000113INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
Tom Stellard77a17772016-01-20 15:48:27 +0000114INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
115 "Annotate SI Control Flow", false, false)
116
Tom Stellardf8794352012-12-19 22:10:31 +0000117char SIAnnotateControlFlow::ID = 0;
118
119/// \brief Initialize all the types and constants used in the pass
120bool SIAnnotateControlFlow::doInitialization(Module &M) {
Tom Stellardf8794352012-12-19 22:10:31 +0000121 LLVMContext &Context = M.getContext();
122
123 Void = Type::getVoidTy(Context);
124 Boolean = Type::getInt1Ty(Context);
125 Int64 = Type::getInt64Ty(Context);
Craig Topper062a2ba2014-04-25 05:30:21 +0000126 ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000127
128 BoolTrue = ConstantInt::getTrue(Context);
129 BoolFalse = ConstantInt::getFalse(Context);
130 BoolUndef = UndefValue::get(Boolean);
131 Int64Zero = ConstantInt::get(Int64, 0);
132
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000133 If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if);
134 Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else);
135 Break = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_break);
136 IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break);
137 ElseBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else_break);
138 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop);
139 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf);
Tom Stellardf8794352012-12-19 22:10:31 +0000140 return false;
141}
142
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000143/// \brief Is the branch condition uniform or did the StructurizeCFG pass
144/// consider it as such?
145bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
146 return DA->isUniform(T->getCondition()) ||
147 T->getMetadata("structurizecfg.uniform") != nullptr;
148}
149
Tom Stellardf8794352012-12-19 22:10:31 +0000150/// \brief Is BB the last block saved on the stack ?
151bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
Michel Danzerae0a4032013-02-14 08:00:33 +0000152 return !Stack.empty() && Stack.back().first == BB;
Tom Stellardf8794352012-12-19 22:10:31 +0000153}
154
155/// \brief Pop the last saved value from the control flow stack
156Value *SIAnnotateControlFlow::popSaved() {
157 return Stack.pop_back_val().second;
158}
159
160/// \brief Push a BB and saved value to the control flow stack
161void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
162 Stack.push_back(std::make_pair(BB, Saved));
163}
164
165/// \brief Can the condition represented by this PHI node treated like
166/// an "Else" block?
167bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
Tom Stellardf8794352012-12-19 22:10:31 +0000168 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
169 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
170 if (Phi->getIncomingBlock(i) == IDom) {
171
172 if (Phi->getIncomingValue(i) != BoolTrue)
173 return false;
174
175 } else {
176 if (Phi->getIncomingValue(i) != BoolFalse)
177 return false;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000178
Tom Stellardf8794352012-12-19 22:10:31 +0000179 }
180 }
181 return true;
182}
183
184// \brief Erase "Phi" if it is not used any more
185void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
186 if (!Phi->hasNUsesOrMore(1))
187 Phi->eraseFromParent();
188}
189
190/// \brief Open a new "If" block
191void SIAnnotateControlFlow::openIf(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000192 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000193 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000194
Tom Stellardf8794352012-12-19 22:10:31 +0000195 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
196 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
197 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
198}
199
200/// \brief Close the last "If" block and open a new "Else" block
201void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000202 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000203 return;
204 }
Tom Stellardf8794352012-12-19 22:10:31 +0000205 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
206 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
207 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
208}
209
210/// \brief Recursively handle the condition leading to a loop
Tom Stellardd4a19502015-04-14 14:36:45 +0000211Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken,
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000212 llvm::Loop *L, BranchInst *Term) {
Tom Stellard0b7feb12015-05-01 03:44:08 +0000213
214 // Only search through PHI nodes which are inside the loop. If we try this
215 // with PHI nodes that are outside of the loop, we end up inserting new PHI
216 // nodes outside of the loop which depend on values defined inside the loop.
217 // This will break the module with
218 // 'Instruction does not dominate all users!' errors.
219 PHINode *Phi = nullptr;
220 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
221
Tom Stellardde16a2e2014-06-20 17:06:02 +0000222 BasicBlock *Parent = Phi->getParent();
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000223 PHINode *NewPhi = PHINode::Create(Int64, 0, "loop.phi", &Parent->front());
Tom Stellardde16a2e2014-06-20 17:06:02 +0000224 Value *Ret = NewPhi;
Tom Stellardf8794352012-12-19 22:10:31 +0000225
Alp Tokerf907b892013-12-05 05:44:44 +0000226 // Handle all non-constant incoming values first
Tom Stellardf8794352012-12-19 22:10:31 +0000227 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
228 Value *Incoming = Phi->getIncomingValue(i);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000229 BasicBlock *From = Phi->getIncomingBlock(i);
230 if (isa<ConstantInt>(Incoming)) {
231 NewPhi->addIncoming(Broken, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000232 continue;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000233 }
Tom Stellardf8794352012-12-19 22:10:31 +0000234
235 Phi->setIncomingValue(i, BoolFalse);
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000236 Value *PhiArg = handleLoopCondition(Incoming, Broken, L, Term);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000237 NewPhi->addIncoming(PhiArg, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000238 }
239
Tom Stellardf8794352012-12-19 22:10:31 +0000240 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
241
242 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
243
244 Value *Incoming = Phi->getIncomingValue(i);
245 if (Incoming != BoolTrue)
246 continue;
247
248 BasicBlock *From = Phi->getIncomingBlock(i);
249 if (From == IDom) {
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000250 // We're in the following situation:
251 // IDom/From
252 // | \
253 // | If-block
254 // | /
255 // Parent
256 // where we want to break out of the loop if the If-block is not taken.
257 // Due to the depth-first traversal, there should be an end.cf
258 // intrinsic in Parent, and we insert an else.break before it.
259 //
260 // Note that the end.cf need not be the first non-phi instruction
261 // of parent, particularly when we're dealing with a multi-level
262 // break, but it should occur within a group of intrinsic calls
263 // at the beginning of the block.
Tom Stellardf8794352012-12-19 22:10:31 +0000264 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000265 while (OldEnd && OldEnd->getCalledFunction() != EndCf)
266 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode());
Tom Stellardf8794352012-12-19 22:10:31 +0000267 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
Tom Stellardde16a2e2014-06-20 17:06:02 +0000268 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
269 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
Tom Stellardf8794352012-12-19 22:10:31 +0000270 continue;
271 }
272 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000273
Tom Stellardf8794352012-12-19 22:10:31 +0000274 TerminatorInst *Insert = From->getTerminator();
Tom Stellardde16a2e2014-06-20 17:06:02 +0000275 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
276 NewPhi->setIncomingValue(i, PhiArg);
Tom Stellardf8794352012-12-19 22:10:31 +0000277 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000278
Tom Stellardf8794352012-12-19 22:10:31 +0000279 eraseIfUnused(Phi);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000280 return Ret;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000281 }
Tom Stellardf8794352012-12-19 22:10:31 +0000282
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000283 if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000284 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000285 Instruction *Insert;
286 if (L->contains(Inst)) {
287 Insert = Parent->getTerminator();
288 } else {
289 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
290 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000291
Tom Stellardde16a2e2014-06-20 17:06:02 +0000292 Value *Args[] = { Cond, Broken };
293 return CallInst::Create(IfBreak, Args, "", Insert);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000294 }
Tom Stellardf8794352012-12-19 22:10:31 +0000295
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000296 // Insert IfBreak in the loop header TERM for constant COND other than true.
297 if (isa<Constant>(Cond)) {
298 Instruction *Insert = Cond == BoolTrue ?
299 Term : L->getHeader()->getTerminator();
300
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000301 Value *Args[] = { Cond, Broken };
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000302 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000303 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000304
305 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000306}
307
308/// \brief Handle a back edge (loop)
309void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000310 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000311 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000312
Tom Stellardd4a19502015-04-14 14:36:45 +0000313 BasicBlock *BB = Term->getParent();
314 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000315 if (!L)
316 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000317
Tom Stellardf8794352012-12-19 22:10:31 +0000318 BasicBlock *Target = Term->getSuccessor(1);
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000319 PHINode *Broken = PHINode::Create(Int64, 0, "phi.broken", &Target->front());
Tom Stellardf8794352012-12-19 22:10:31 +0000320
Tom Stellardf8794352012-12-19 22:10:31 +0000321 Value *Cond = Term->getCondition();
322 Term->setCondition(BoolTrue);
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000323 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
Tom Stellardf8794352012-12-19 22:10:31 +0000324
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000325 for (BasicBlock *Pred : predecessors(Target))
326 Broken->addIncoming(Pred == BB ? Arg : Int64Zero, Pred);
Tom Stellardf8794352012-12-19 22:10:31 +0000327
328 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
329 push(Term->getSuccessor(0), Arg);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000330}
331
332/// \brief Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000333void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000334 llvm::Loop *L = LI->getLoopFor(BB);
335
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000336 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000337
Tom Stellard0f29de72015-02-05 15:32:15 +0000338 if (L && L->getHeader() == BB) {
339 // We can't insert an EndCF call into a loop header, because it will
340 // get executed on every iteration of the loop, when it should be
341 // executed only once before the loop.
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000342 SmallVector <BasicBlock *, 8> Latches;
Tom Stellard0f29de72015-02-05 15:32:15 +0000343 L->getLoopLatches(Latches);
344
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000345 SmallVector<BasicBlock *, 2> Preds;
346 for (BasicBlock *Pred : predecessors(BB)) {
347 if (!is_contained(Latches, Pred))
348 Preds.push_back(Pred);
Tom Stellard0f29de72015-02-05 15:32:15 +0000349 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000350
Chandler Carruth96ada252015-07-22 09:52:54 +0000351 BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000352 }
353
Tom Stellardbc4497b2016-02-12 23:45:29 +0000354 Value *Exec = popSaved();
Changpeng Fang6b49fa42017-03-07 23:29:36 +0000355 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
356 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
357 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
Tom Stellardf8794352012-12-19 22:10:31 +0000358}
359
360/// \brief Annotate the control flow with intrinsics so the backend can
361/// recognize if/then/else and loops.
362bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Chandler Carruth73523022014-01-13 13:07:17 +0000363 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000364 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000365 DA = &getAnalysis<DivergenceAnalysis>();
Tom Stellardf8794352012-12-19 22:10:31 +0000366
367 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
368 E = df_end(&F.getEntryBlock()); I != E; ++I) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000369 BasicBlock *BB = *I;
370 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
Tom Stellardf8794352012-12-19 22:10:31 +0000371
372 if (!Term || Term->isUnconditional()) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000373 if (isTopOfStack(BB))
374 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000375
Tom Stellardf8794352012-12-19 22:10:31 +0000376 continue;
377 }
378
379 if (I.nodeVisited(Term->getSuccessor(1))) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000380 if (isTopOfStack(BB))
381 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000382
Tom Stellardf8794352012-12-19 22:10:31 +0000383 handleLoop(Term);
384 continue;
385 }
386
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000387 if (isTopOfStack(BB)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000388 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000389 if (Phi && Phi->getParent() == BB && isElse(Phi)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000390 insertElse(Term);
391 eraseIfUnused(Phi);
392 continue;
393 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000394
395 closeControlFlow(BB);
Tom Stellardf8794352012-12-19 22:10:31 +0000396 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000397
Tom Stellardf8794352012-12-19 22:10:31 +0000398 openIf(Term);
399 }
400
401 assert(Stack.empty());
402 return true;
403}
404
405/// \brief Create the annotation pass
406FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000407 return new SIAnnotateControlFlow();
408}