blob: b7e62075244b8147e79b0d760500c06527d86000 [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"
Matt Arsenault0607a442017-03-24 20:57:10 +000025#include "llvm/Transforms/Utils/Local.h"
Tom Stellardf8794352012-12-19 22:10:31 +000026#include "llvm/Transforms/Utils/SSAUpdater.h"
27
28using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "si-annotate-control-flow"
31
Tom Stellardf8794352012-12-19 22:10:31 +000032namespace {
33
34// Complex types used in this pass
35typedef std::pair<BasicBlock *, Value *> StackEntry;
36typedef SmallVector<StackEntry, 16> StackVector;
37
Tom Stellardf8794352012-12-19 22:10:31 +000038class SIAnnotateControlFlow : public FunctionPass {
Tom Stellardbc4497b2016-02-12 23:45:29 +000039 DivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000040
Tom Stellardf8794352012-12-19 22:10:31 +000041 Type *Boolean;
42 Type *Void;
43 Type *Int64;
44 Type *ReturnStruct;
45
46 ConstantInt *BoolTrue;
47 ConstantInt *BoolFalse;
48 UndefValue *BoolUndef;
49 Constant *Int64Zero;
50
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000051 Function *If;
52 Function *Else;
53 Function *Break;
54 Function *IfBreak;
55 Function *ElseBreak;
56 Function *Loop;
57 Function *EndCf;
Tom Stellardf8794352012-12-19 22:10:31 +000058
59 DominatorTree *DT;
60 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000061
Tom Stellard0f29de72015-02-05 15:32:15 +000062 LoopInfo *LI;
63
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000064 bool isUniform(BranchInst *T);
65
Tom Stellardf8794352012-12-19 22:10:31 +000066 bool isTopOfStack(BasicBlock *BB);
67
68 Value *popSaved();
69
70 void push(BasicBlock *BB, Value *Saved);
71
72 bool isElse(PHINode *Phi);
73
74 void eraseIfUnused(PHINode *Phi);
75
76 void openIf(BranchInst *Term);
77
78 void insertElse(BranchInst *Term);
79
Changpeng Fange07f1aa2016-02-12 17:11:04 +000080 Value *handleLoopCondition(Value *Cond, PHINode *Broken,
Matt Arsenault0607a442017-03-24 20:57:10 +000081 llvm::Loop *L, BranchInst *Term,
82 SmallVectorImpl<WeakVH> &LoopPhiConditions);
Tom Stellardf8794352012-12-19 22:10:31 +000083
84 void handleLoop(BranchInst *Term);
85
86 void closeControlFlow(BasicBlock *BB);
87
88public:
Tom Stellard77a17772016-01-20 15:48:27 +000089 static char ID;
90
Tom Stellardf8794352012-12-19 22:10:31 +000091 SIAnnotateControlFlow():
92 FunctionPass(ID) { }
93
Craig Topper5656db42014-04-29 07:57:24 +000094 bool doInitialization(Module &M) override;
Tom Stellardf8794352012-12-19 22:10:31 +000095
Craig Topper5656db42014-04-29 07:57:24 +000096 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +000097
Mehdi Amini117296c2016-10-01 02:56:57 +000098 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +000099
Craig Topper5656db42014-04-29 07:57:24 +0000100 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +0000101 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000102 AU.addRequired<DominatorTreeWrapperPass>();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000103 AU.addRequired<DivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000104 AU.addPreserved<DominatorTreeWrapperPass>();
Tom Stellardf8794352012-12-19 22:10:31 +0000105 FunctionPass::getAnalysisUsage(AU);
106 }
107
108};
109
110} // end anonymous namespace
111
Tom Stellard77a17772016-01-20 15:48:27 +0000112INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
113 "Annotate SI Control Flow", false, false)
Matt Arsenault31a58c62017-03-02 23:50:51 +0000114INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Tom Stellardbc4497b2016-02-12 23:45:29 +0000115INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
Tom Stellard77a17772016-01-20 15:48:27 +0000116INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
117 "Annotate SI Control Flow", false, false)
118
Tom Stellardf8794352012-12-19 22:10:31 +0000119char SIAnnotateControlFlow::ID = 0;
120
121/// \brief Initialize all the types and constants used in the pass
122bool SIAnnotateControlFlow::doInitialization(Module &M) {
Tom Stellardf8794352012-12-19 22:10:31 +0000123 LLVMContext &Context = M.getContext();
124
125 Void = Type::getVoidTy(Context);
126 Boolean = Type::getInt1Ty(Context);
127 Int64 = Type::getInt64Ty(Context);
Craig Topper062a2ba2014-04-25 05:30:21 +0000128 ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000129
130 BoolTrue = ConstantInt::getTrue(Context);
131 BoolFalse = ConstantInt::getFalse(Context);
132 BoolUndef = UndefValue::get(Boolean);
133 Int64Zero = ConstantInt::get(Int64, 0);
134
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000135 If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if);
136 Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else);
137 Break = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_break);
138 IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break);
139 ElseBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else_break);
140 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop);
141 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf);
Tom Stellardf8794352012-12-19 22:10:31 +0000142 return false;
143}
144
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000145/// \brief Is the branch condition uniform or did the StructurizeCFG pass
146/// consider it as such?
147bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
148 return DA->isUniform(T->getCondition()) ||
149 T->getMetadata("structurizecfg.uniform") != nullptr;
150}
151
Tom Stellardf8794352012-12-19 22:10:31 +0000152/// \brief Is BB the last block saved on the stack ?
153bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
Michel Danzerae0a4032013-02-14 08:00:33 +0000154 return !Stack.empty() && Stack.back().first == BB;
Tom Stellardf8794352012-12-19 22:10:31 +0000155}
156
157/// \brief Pop the last saved value from the control flow stack
158Value *SIAnnotateControlFlow::popSaved() {
159 return Stack.pop_back_val().second;
160}
161
162/// \brief Push a BB and saved value to the control flow stack
163void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
164 Stack.push_back(std::make_pair(BB, Saved));
165}
166
167/// \brief Can the condition represented by this PHI node treated like
168/// an "Else" block?
169bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
Tom Stellardf8794352012-12-19 22:10:31 +0000170 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
171 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
172 if (Phi->getIncomingBlock(i) == IDom) {
173
174 if (Phi->getIncomingValue(i) != BoolTrue)
175 return false;
176
177 } else {
178 if (Phi->getIncomingValue(i) != BoolFalse)
179 return false;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000180
Tom Stellardf8794352012-12-19 22:10:31 +0000181 }
182 }
183 return true;
184}
185
186// \brief Erase "Phi" if it is not used any more
187void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
Matt Arsenault0607a442017-03-24 20:57:10 +0000188 if (llvm::RecursivelyDeleteDeadPHINode(Phi)) {
189 DEBUG(dbgs() << "Erased unused condition phi\n");
190 }
Tom Stellardf8794352012-12-19 22:10:31 +0000191}
192
193/// \brief Open a new "If" block
194void SIAnnotateControlFlow::openIf(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000195 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000196 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000197
Tom Stellardf8794352012-12-19 22:10:31 +0000198 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
199 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
200 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
201}
202
203/// \brief Close the last "If" block and open a new "Else" block
204void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000205 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000206 return;
207 }
Tom Stellardf8794352012-12-19 22:10:31 +0000208 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
209 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
210 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
211}
212
213/// \brief Recursively handle the condition leading to a loop
Matt Arsenault0607a442017-03-24 20:57:10 +0000214Value *SIAnnotateControlFlow::handleLoopCondition(
215 Value *Cond, PHINode *Broken,
216 llvm::Loop *L, BranchInst *Term,
217 SmallVectorImpl<WeakVH> &LoopPhiConditions) {
Tom Stellard0b7feb12015-05-01 03:44:08 +0000218
219 // Only search through PHI nodes which are inside the loop. If we try this
220 // with PHI nodes that are outside of the loop, we end up inserting new PHI
221 // nodes outside of the loop which depend on values defined inside the loop.
222 // This will break the module with
223 // 'Instruction does not dominate all users!' errors.
224 PHINode *Phi = nullptr;
225 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
226
Tom Stellardde16a2e2014-06-20 17:06:02 +0000227 BasicBlock *Parent = Phi->getParent();
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000228 PHINode *NewPhi = PHINode::Create(Int64, 0, "loop.phi", &Parent->front());
Tom Stellardde16a2e2014-06-20 17:06:02 +0000229 Value *Ret = NewPhi;
Tom Stellardf8794352012-12-19 22:10:31 +0000230
Alp Tokerf907b892013-12-05 05:44:44 +0000231 // Handle all non-constant incoming values first
Tom Stellardf8794352012-12-19 22:10:31 +0000232 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
233 Value *Incoming = Phi->getIncomingValue(i);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000234 BasicBlock *From = Phi->getIncomingBlock(i);
235 if (isa<ConstantInt>(Incoming)) {
236 NewPhi->addIncoming(Broken, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000237 continue;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000238 }
Tom Stellardf8794352012-12-19 22:10:31 +0000239
240 Phi->setIncomingValue(i, BoolFalse);
Matt Arsenault0607a442017-03-24 20:57:10 +0000241 Value *PhiArg = handleLoopCondition(Incoming, Broken, L,
242 Term, LoopPhiConditions);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000243 NewPhi->addIncoming(PhiArg, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000244 }
245
Tom Stellardf8794352012-12-19 22:10:31 +0000246 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
247
248 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
Tom Stellardf8794352012-12-19 22:10:31 +0000249 Value *Incoming = Phi->getIncomingValue(i);
250 if (Incoming != BoolTrue)
251 continue;
252
253 BasicBlock *From = Phi->getIncomingBlock(i);
254 if (From == IDom) {
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000255 // We're in the following situation:
256 // IDom/From
257 // | \
258 // | If-block
259 // | /
260 // Parent
261 // where we want to break out of the loop if the If-block is not taken.
262 // Due to the depth-first traversal, there should be an end.cf
263 // intrinsic in Parent, and we insert an else.break before it.
264 //
265 // Note that the end.cf need not be the first non-phi instruction
266 // of parent, particularly when we're dealing with a multi-level
267 // break, but it should occur within a group of intrinsic calls
268 // at the beginning of the block.
Tom Stellardf8794352012-12-19 22:10:31 +0000269 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000270 while (OldEnd && OldEnd->getCalledFunction() != EndCf)
271 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode());
Tom Stellardf8794352012-12-19 22:10:31 +0000272 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
Tom Stellardde16a2e2014-06-20 17:06:02 +0000273 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
274 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
Tom Stellardf8794352012-12-19 22:10:31 +0000275 continue;
276 }
277 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000278
Tom Stellardf8794352012-12-19 22:10:31 +0000279 TerminatorInst *Insert = From->getTerminator();
Tom Stellardde16a2e2014-06-20 17:06:02 +0000280 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
281 NewPhi->setIncomingValue(i, PhiArg);
Tom Stellardf8794352012-12-19 22:10:31 +0000282 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000283
Matt Arsenault0607a442017-03-24 20:57:10 +0000284 LoopPhiConditions.push_back(WeakVH(Phi));
Tom Stellardde16a2e2014-06-20 17:06:02 +0000285 return Ret;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000286 }
Tom Stellardf8794352012-12-19 22:10:31 +0000287
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000288 if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000289 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000290 Instruction *Insert;
291 if (L->contains(Inst)) {
292 Insert = Parent->getTerminator();
293 } else {
294 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
295 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000296
Tom Stellardde16a2e2014-06-20 17:06:02 +0000297 Value *Args[] = { Cond, Broken };
298 return CallInst::Create(IfBreak, Args, "", Insert);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000299 }
Tom Stellardf8794352012-12-19 22:10:31 +0000300
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000301 // Insert IfBreak in the loop header TERM for constant COND other than true.
302 if (isa<Constant>(Cond)) {
303 Instruction *Insert = Cond == BoolTrue ?
304 Term : L->getHeader()->getTerminator();
305
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000306 Value *Args[] = { Cond, Broken };
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000307 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000308 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000309
310 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000311}
312
313/// \brief Handle a back edge (loop)
314void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000315 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000316 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000317
Tom Stellardd4a19502015-04-14 14:36:45 +0000318 BasicBlock *BB = Term->getParent();
319 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000320 if (!L)
321 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000322
Tom Stellardf8794352012-12-19 22:10:31 +0000323 BasicBlock *Target = Term->getSuccessor(1);
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000324 PHINode *Broken = PHINode::Create(Int64, 0, "phi.broken", &Target->front());
Tom Stellardf8794352012-12-19 22:10:31 +0000325
Matt Arsenault0607a442017-03-24 20:57:10 +0000326 SmallVector<WeakVH, 8> LoopPhiConditions;
Tom Stellardf8794352012-12-19 22:10:31 +0000327 Value *Cond = Term->getCondition();
328 Term->setCondition(BoolTrue);
Matt Arsenault0607a442017-03-24 20:57:10 +0000329 Value *Arg = handleLoopCondition(Cond, Broken, L, Term, LoopPhiConditions);
Tom Stellardf8794352012-12-19 22:10:31 +0000330
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000331 for (BasicBlock *Pred : predecessors(Target))
332 Broken->addIncoming(Pred == BB ? Arg : Int64Zero, Pred);
Tom Stellardf8794352012-12-19 22:10:31 +0000333
334 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
Matt Arsenault0607a442017-03-24 20:57:10 +0000335
336 for (WeakVH Val : reverse(LoopPhiConditions)) {
337 if (PHINode *Cond = cast_or_null<PHINode>(Val))
338 eraseIfUnused(Cond);
339 }
340
Tom Stellardf8794352012-12-19 22:10:31 +0000341 push(Term->getSuccessor(0), Arg);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000342}
343
344/// \brief Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000345void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000346 llvm::Loop *L = LI->getLoopFor(BB);
347
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000348 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000349
Tom Stellard0f29de72015-02-05 15:32:15 +0000350 if (L && L->getHeader() == BB) {
351 // We can't insert an EndCF call into a loop header, because it will
352 // get executed on every iteration of the loop, when it should be
353 // executed only once before the loop.
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000354 SmallVector <BasicBlock *, 8> Latches;
Tom Stellard0f29de72015-02-05 15:32:15 +0000355 L->getLoopLatches(Latches);
356
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000357 SmallVector<BasicBlock *, 2> Preds;
358 for (BasicBlock *Pred : predecessors(BB)) {
359 if (!is_contained(Latches, Pred))
360 Preds.push_back(Pred);
Tom Stellard0f29de72015-02-05 15:32:15 +0000361 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000362
Chandler Carruth96ada252015-07-22 09:52:54 +0000363 BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000364 }
365
Tom Stellardbc4497b2016-02-12 23:45:29 +0000366 Value *Exec = popSaved();
Changpeng Fang6b49fa42017-03-07 23:29:36 +0000367 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
368 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
369 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
Tom Stellardf8794352012-12-19 22:10:31 +0000370}
371
372/// \brief Annotate the control flow with intrinsics so the backend can
373/// recognize if/then/else and loops.
374bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Chandler Carruth73523022014-01-13 13:07:17 +0000375 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000376 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000377 DA = &getAnalysis<DivergenceAnalysis>();
Tom Stellardf8794352012-12-19 22:10:31 +0000378
379 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
380 E = df_end(&F.getEntryBlock()); I != E; ++I) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000381 BasicBlock *BB = *I;
382 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
Tom Stellardf8794352012-12-19 22:10:31 +0000383
384 if (!Term || Term->isUnconditional()) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000385 if (isTopOfStack(BB))
386 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000387
Tom Stellardf8794352012-12-19 22:10:31 +0000388 continue;
389 }
390
391 if (I.nodeVisited(Term->getSuccessor(1))) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000392 if (isTopOfStack(BB))
393 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000394
Tom Stellardf8794352012-12-19 22:10:31 +0000395 handleLoop(Term);
396 continue;
397 }
398
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000399 if (isTopOfStack(BB)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000400 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000401 if (Phi && Phi->getParent() == BB && isElse(Phi)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000402 insertElse(Term);
403 eraseIfUnused(Phi);
404 continue;
405 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000406
407 closeControlFlow(BB);
Tom Stellardf8794352012-12-19 22:10:31 +0000408 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000409
Tom Stellardf8794352012-12-19 22:10:31 +0000410 openIf(Term);
411 }
412
413 assert(Stack.empty());
414 return true;
415}
416
417/// \brief Create the annotation pass
418FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000419 return new SIAnnotateControlFlow();
420}