blob: d70f52e0f295279bf248287cf19d699955b80ffd [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
37// Intrinsic names the control flow is annotated with
Matt Arsenault7898b902016-01-22 18:42:55 +000038static const char *const IfIntrinsic = "llvm.amdgcn.if";
39static const char *const ElseIntrinsic = "llvm.amdgcn.else";
Matt Arsenault48d70cb2016-07-09 17:18:39 +000040static const char *const BreakIntrinsic = "llvm.amdgcn.break";
Matt Arsenault7898b902016-01-22 18:42:55 +000041static const char *const IfBreakIntrinsic = "llvm.amdgcn.if.break";
42static const char *const ElseBreakIntrinsic = "llvm.amdgcn.else.break";
43static const char *const LoopIntrinsic = "llvm.amdgcn.loop";
44static const char *const EndCfIntrinsic = "llvm.amdgcn.end.cf";
Tom Stellardf8794352012-12-19 22:10:31 +000045
46class SIAnnotateControlFlow : public FunctionPass {
Tom Stellardbc4497b2016-02-12 23:45:29 +000047 DivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000048
Tom Stellardf8794352012-12-19 22:10:31 +000049 Type *Boolean;
50 Type *Void;
51 Type *Int64;
52 Type *ReturnStruct;
53
54 ConstantInt *BoolTrue;
55 ConstantInt *BoolFalse;
56 UndefValue *BoolUndef;
57 Constant *Int64Zero;
58
59 Constant *If;
60 Constant *Else;
61 Constant *Break;
62 Constant *IfBreak;
63 Constant *ElseBreak;
64 Constant *Loop;
65 Constant *EndCf;
66
67 DominatorTree *DT;
68 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000069
Tom Stellard0f29de72015-02-05 15:32:15 +000070 LoopInfo *LI;
71
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000072 bool isUniform(BranchInst *T);
73
Tom Stellardf8794352012-12-19 22:10:31 +000074 bool isTopOfStack(BasicBlock *BB);
75
76 Value *popSaved();
77
78 void push(BasicBlock *BB, Value *Saved);
79
80 bool isElse(PHINode *Phi);
81
82 void eraseIfUnused(PHINode *Phi);
83
84 void openIf(BranchInst *Term);
85
86 void insertElse(BranchInst *Term);
87
Changpeng Fange07f1aa2016-02-12 17:11:04 +000088 Value *handleLoopCondition(Value *Cond, PHINode *Broken,
89 llvm::Loop *L, BranchInst *Term);
Tom Stellardf8794352012-12-19 22:10:31 +000090
91 void handleLoop(BranchInst *Term);
92
93 void closeControlFlow(BasicBlock *BB);
94
95public:
Tom Stellard77a17772016-01-20 15:48:27 +000096 static char ID;
97
Tom Stellardf8794352012-12-19 22:10:31 +000098 SIAnnotateControlFlow():
99 FunctionPass(ID) { }
100
Craig Topper5656db42014-04-29 07:57:24 +0000101 bool doInitialization(Module &M) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000102
Craig Topper5656db42014-04-29 07:57:24 +0000103 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000104
Mehdi Amini117296c2016-10-01 02:56:57 +0000105 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +0000106
Craig Topper5656db42014-04-29 07:57:24 +0000107 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +0000108 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000109 AU.addRequired<DominatorTreeWrapperPass>();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000110 AU.addRequired<DivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000111 AU.addPreserved<DominatorTreeWrapperPass>();
Tom Stellardf8794352012-12-19 22:10:31 +0000112 FunctionPass::getAnalysisUsage(AU);
113 }
114
115};
116
117} // end anonymous namespace
118
Tom Stellard77a17772016-01-20 15:48:27 +0000119INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
120 "Annotate SI Control Flow", false, false)
Tom Stellardbc4497b2016-02-12 23:45:29 +0000121INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
Tom Stellard77a17772016-01-20 15:48:27 +0000122INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
123 "Annotate SI Control Flow", false, false)
124
Tom Stellardf8794352012-12-19 22:10:31 +0000125char SIAnnotateControlFlow::ID = 0;
126
127/// \brief Initialize all the types and constants used in the pass
128bool SIAnnotateControlFlow::doInitialization(Module &M) {
Tom Stellardf8794352012-12-19 22:10:31 +0000129 LLVMContext &Context = M.getContext();
130
131 Void = Type::getVoidTy(Context);
132 Boolean = Type::getInt1Ty(Context);
133 Int64 = Type::getInt64Ty(Context);
Craig Topper062a2ba2014-04-25 05:30:21 +0000134 ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000135
136 BoolTrue = ConstantInt::getTrue(Context);
137 BoolFalse = ConstantInt::getFalse(Context);
138 BoolUndef = UndefValue::get(Boolean);
139 Int64Zero = ConstantInt::get(Int64, 0);
140
141 If = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000142 IfIntrinsic, ReturnStruct, Boolean, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000143
144 Else = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000145 ElseIntrinsic, ReturnStruct, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000146
Matt Arsenault48d70cb2016-07-09 17:18:39 +0000147 Break = M.getOrInsertFunction(
148 BreakIntrinsic, Int64, Int64, (Type *)nullptr);
Matt Arsenault6408c912016-09-16 22:11:18 +0000149 cast<Function>(Break)->setDoesNotAccessMemory();
Matt Arsenault48d70cb2016-07-09 17:18:39 +0000150
Tom Stellardf8794352012-12-19 22:10:31 +0000151 IfBreak = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)nullptr);
Matt Arsenault6408c912016-09-16 22:11:18 +0000153 cast<Function>(IfBreak)->setDoesNotAccessMemory();;
Tom Stellardf8794352012-12-19 22:10:31 +0000154
155 ElseBreak = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000156 ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)nullptr);
Matt Arsenault6408c912016-09-16 22:11:18 +0000157 cast<Function>(ElseBreak)->setDoesNotAccessMemory();
Tom Stellardf8794352012-12-19 22:10:31 +0000158
159 Loop = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000160 LoopIntrinsic, Boolean, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000161
162 EndCf = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000163 EndCfIntrinsic, Void, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000164
165 return false;
166}
167
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000168/// \brief Is the branch condition uniform or did the StructurizeCFG pass
169/// consider it as such?
170bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
171 return DA->isUniform(T->getCondition()) ||
172 T->getMetadata("structurizecfg.uniform") != nullptr;
173}
174
Tom Stellardf8794352012-12-19 22:10:31 +0000175/// \brief Is BB the last block saved on the stack ?
176bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
Michel Danzerae0a4032013-02-14 08:00:33 +0000177 return !Stack.empty() && Stack.back().first == BB;
Tom Stellardf8794352012-12-19 22:10:31 +0000178}
179
180/// \brief Pop the last saved value from the control flow stack
181Value *SIAnnotateControlFlow::popSaved() {
182 return Stack.pop_back_val().second;
183}
184
185/// \brief Push a BB and saved value to the control flow stack
186void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
187 Stack.push_back(std::make_pair(BB, Saved));
188}
189
190/// \brief Can the condition represented by this PHI node treated like
191/// an "Else" block?
192bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
Tom Stellardf8794352012-12-19 22:10:31 +0000193 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
194 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
195 if (Phi->getIncomingBlock(i) == IDom) {
196
197 if (Phi->getIncomingValue(i) != BoolTrue)
198 return false;
199
200 } else {
201 if (Phi->getIncomingValue(i) != BoolFalse)
202 return false;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000203
Tom Stellardf8794352012-12-19 22:10:31 +0000204 }
205 }
206 return true;
207}
208
209// \brief Erase "Phi" if it is not used any more
210void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
211 if (!Phi->hasNUsesOrMore(1))
212 Phi->eraseFromParent();
213}
214
215/// \brief Open a new "If" block
216void SIAnnotateControlFlow::openIf(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000217 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000218 return;
219 }
Tom Stellardf8794352012-12-19 22:10:31 +0000220 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
221 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
222 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
223}
224
225/// \brief Close the last "If" block and open a new "Else" block
226void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000227 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000228 return;
229 }
Tom Stellardf8794352012-12-19 22:10:31 +0000230 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
231 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
232 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
233}
234
235/// \brief Recursively handle the condition leading to a loop
Tom Stellardd4a19502015-04-14 14:36:45 +0000236Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken,
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000237 llvm::Loop *L, BranchInst *Term) {
Tom Stellard0b7feb12015-05-01 03:44:08 +0000238
239 // Only search through PHI nodes which are inside the loop. If we try this
240 // with PHI nodes that are outside of the loop, we end up inserting new PHI
241 // nodes outside of the loop which depend on values defined inside the loop.
242 // This will break the module with
243 // 'Instruction does not dominate all users!' errors.
244 PHINode *Phi = nullptr;
245 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
246
Tom Stellardde16a2e2014-06-20 17:06:02 +0000247 BasicBlock *Parent = Phi->getParent();
248 PHINode *NewPhi = PHINode::Create(Int64, 0, "", &Parent->front());
249 Value *Ret = NewPhi;
Tom Stellardf8794352012-12-19 22:10:31 +0000250
Alp Tokerf907b892013-12-05 05:44:44 +0000251 // Handle all non-constant incoming values first
Tom Stellardf8794352012-12-19 22:10:31 +0000252 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
253 Value *Incoming = Phi->getIncomingValue(i);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000254 BasicBlock *From = Phi->getIncomingBlock(i);
255 if (isa<ConstantInt>(Incoming)) {
256 NewPhi->addIncoming(Broken, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000257 continue;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000258 }
Tom Stellardf8794352012-12-19 22:10:31 +0000259
260 Phi->setIncomingValue(i, BoolFalse);
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000261 Value *PhiArg = handleLoopCondition(Incoming, Broken, L, Term);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000262 NewPhi->addIncoming(PhiArg, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000263 }
264
Tom Stellardf8794352012-12-19 22:10:31 +0000265 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
266
267 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
268
269 Value *Incoming = Phi->getIncomingValue(i);
270 if (Incoming != BoolTrue)
271 continue;
272
273 BasicBlock *From = Phi->getIncomingBlock(i);
274 if (From == IDom) {
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000275 // We're in the following situation:
276 // IDom/From
277 // | \
278 // | If-block
279 // | /
280 // Parent
281 // where we want to break out of the loop if the If-block is not taken.
282 // Due to the depth-first traversal, there should be an end.cf
283 // intrinsic in Parent, and we insert an else.break before it.
284 //
285 // Note that the end.cf need not be the first non-phi instruction
286 // of parent, particularly when we're dealing with a multi-level
287 // break, but it should occur within a group of intrinsic calls
288 // at the beginning of the block.
Tom Stellardf8794352012-12-19 22:10:31 +0000289 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000290 while (OldEnd && OldEnd->getCalledFunction() != EndCf)
291 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode());
Tom Stellardf8794352012-12-19 22:10:31 +0000292 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
Tom Stellardde16a2e2014-06-20 17:06:02 +0000293 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
294 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
Tom Stellardf8794352012-12-19 22:10:31 +0000295 continue;
296 }
297 }
Tom Stellardf8794352012-12-19 22:10:31 +0000298 TerminatorInst *Insert = From->getTerminator();
Tom Stellardde16a2e2014-06-20 17:06:02 +0000299 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
300 NewPhi->setIncomingValue(i, PhiArg);
Tom Stellardf8794352012-12-19 22:10:31 +0000301 }
302 eraseIfUnused(Phi);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000303 return Ret;
Tom Stellardf8794352012-12-19 22:10:31 +0000304
305 } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
306 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000307 Instruction *Insert;
308 if (L->contains(Inst)) {
309 Insert = Parent->getTerminator();
310 } else {
311 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
312 }
Tom Stellardde16a2e2014-06-20 17:06:02 +0000313 Value *Args[] = { Cond, Broken };
314 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000315
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000316 // Insert IfBreak before TERM for constant COND.
317 } else if (isa<ConstantInt>(Cond)) {
318 Value *Args[] = { Cond, Broken };
319 return CallInst::Create(IfBreak, Args, "", Term);
320
Tom Stellardf8794352012-12-19 22:10:31 +0000321 } else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000322 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000323 }
Matt Arsenault37fefd62016-06-10 02:18:02 +0000324 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000325}
326
327/// \brief Handle a back edge (loop)
328void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000329 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000330 return;
331 }
332
Tom Stellardd4a19502015-04-14 14:36:45 +0000333 BasicBlock *BB = Term->getParent();
334 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000335 if (!L)
336 return;
Tom Stellardf8794352012-12-19 22:10:31 +0000337 BasicBlock *Target = Term->getSuccessor(1);
338 PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front());
339
Tom Stellardf8794352012-12-19 22:10:31 +0000340 Value *Cond = Term->getCondition();
341 Term->setCondition(BoolTrue);
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000342 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
Tom Stellardf8794352012-12-19 22:10:31 +0000343
Tom Stellardf8794352012-12-19 22:10:31 +0000344 for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target);
345 PI != PE; ++PI) {
346
347 Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI);
348 }
349
350 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
351 push(Term->getSuccessor(0), Arg);
Tom Stellard0f29de72015-02-05 15:32:15 +0000352}/// \brief Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000353void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000354 llvm::Loop *L = LI->getLoopFor(BB);
355
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000356 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000357
Tom Stellard0f29de72015-02-05 15:32:15 +0000358 if (L && L->getHeader() == BB) {
359 // We can't insert an EndCF call into a loop header, because it will
360 // get executed on every iteration of the loop, when it should be
361 // executed only once before the loop.
362 SmallVector <BasicBlock*, 8> Latches;
363 L->getLoopLatches(Latches);
364
365 std::vector<BasicBlock*> Preds;
366 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000367 if (!is_contained(Latches, *PI))
Tom Stellard0f29de72015-02-05 15:32:15 +0000368 Preds.push_back(*PI);
369 }
Chandler Carruth96ada252015-07-22 09:52:54 +0000370 BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000371 }
372
Tom Stellardbc4497b2016-02-12 23:45:29 +0000373 Value *Exec = popSaved();
374 if (!isa<UndefValue>(Exec))
375 CallInst::Create(EndCf, Exec, "", &*BB->getFirstInsertionPt());
Tom Stellardf8794352012-12-19 22:10:31 +0000376}
377
378/// \brief Annotate the control flow with intrinsics so the backend can
379/// recognize if/then/else and loops.
380bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000381
Chandler Carruth73523022014-01-13 13:07:17 +0000382 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000383 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000384 DA = &getAnalysis<DivergenceAnalysis>();
Tom Stellardf8794352012-12-19 22:10:31 +0000385
386 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
387 E = df_end(&F.getEntryBlock()); I != E; ++I) {
388
389 BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator());
390
391 if (!Term || Term->isUnconditional()) {
392 if (isTopOfStack(*I))
393 closeControlFlow(*I);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000394
Tom Stellardf8794352012-12-19 22:10:31 +0000395 continue;
396 }
397
398 if (I.nodeVisited(Term->getSuccessor(1))) {
399 if (isTopOfStack(*I))
400 closeControlFlow(*I);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000401
Tom Stellardf8794352012-12-19 22:10:31 +0000402 handleLoop(Term);
403 continue;
404 }
405
406 if (isTopOfStack(*I)) {
407 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
408 if (Phi && Phi->getParent() == *I && isElse(Phi)) {
409 insertElse(Term);
410 eraseIfUnused(Phi);
411 continue;
412 }
413 closeControlFlow(*I);
414 }
415 openIf(Term);
416 }
417
418 assert(Stack.empty());
419 return true;
420}
421
422/// \brief Create the annotation pass
423FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000424 return new SIAnnotateControlFlow();
425}