blob: a26c12bb934dc0458abefd3676df26a66de24acb [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"
Tom Stellardf8794352012-12-19 22:10:31 +000015#include "llvm/ADT/DepthFirstIterator.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallVector.h"
Nicolai Haehnle35617ed2018-08-30 14:21:36 +000018#include "llvm/Analysis/LegacyDivergenceAnalysis.h"
Tom Stellard0f29de72015-02-05 15:32:15 +000019#include "llvm/Analysis/LoopInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000020#include "llvm/Transforms/Utils/Local.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000021#include "llvm/IR/BasicBlock.h"
22#include "llvm/IR/CFG.h"
23#include "llvm/IR/Constant.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000024#include "llvm/IR/Constants.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000025#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000026#include "llvm/IR/Dominators.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000027#include "llvm/IR/Function.h"
28#include "llvm/IR/Instruction.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000029#include "llvm/IR/Instructions.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000030#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Module.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000032#include "llvm/IR/Type.h"
33#include "llvm/IR/ValueHandle.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000034#include "llvm/Pass.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000035#include "llvm/Support/Casting.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000039#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000040#include <cassert>
41#include <utility>
Tom Stellardf8794352012-12-19 22:10:31 +000042
43using namespace llvm;
44
Chandler Carruth84e68b22014-04-22 02:41:26 +000045#define DEBUG_TYPE "si-annotate-control-flow"
46
Tom Stellardf8794352012-12-19 22:10:31 +000047namespace {
48
49// Complex types used in this pass
Eugene Zelenko59e12822017-08-08 00:47:13 +000050using StackEntry = std::pair<BasicBlock *, Value *>;
51using StackVector = SmallVector<StackEntry, 16>;
Tom Stellardf8794352012-12-19 22:10:31 +000052
Tom Stellardf8794352012-12-19 22:10:31 +000053class SIAnnotateControlFlow : public FunctionPass {
Nicolai Haehnle35617ed2018-08-30 14:21:36 +000054 LegacyDivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000055
Tom Stellardf8794352012-12-19 22:10:31 +000056 Type *Boolean;
57 Type *Void;
58 Type *Int64;
59 Type *ReturnStruct;
60
61 ConstantInt *BoolTrue;
62 ConstantInt *BoolFalse;
63 UndefValue *BoolUndef;
64 Constant *Int64Zero;
65
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000066 Function *If;
67 Function *Else;
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000068 Function *IfBreak;
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000069 Function *Loop;
70 Function *EndCf;
Tom Stellardf8794352012-12-19 22:10:31 +000071
72 DominatorTree *DT;
73 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000074
Tom Stellard0f29de72015-02-05 15:32:15 +000075 LoopInfo *LI;
76
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000077 bool isUniform(BranchInst *T);
78
Tom Stellardf8794352012-12-19 22:10:31 +000079 bool isTopOfStack(BasicBlock *BB);
80
81 Value *popSaved();
82
83 void push(BasicBlock *BB, Value *Saved);
84
85 bool isElse(PHINode *Phi);
86
87 void eraseIfUnused(PHINode *Phi);
88
89 void openIf(BranchInst *Term);
90
91 void insertElse(BranchInst *Term);
92
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000093 Value *
94 handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
Nicolai Haehnle28212cc2018-10-31 13:26:48 +000095 BranchInst *Term);
Tom Stellardf8794352012-12-19 22:10:31 +000096
97 void handleLoop(BranchInst *Term);
98
99 void closeControlFlow(BasicBlock *BB);
100
101public:
Tom Stellard77a17772016-01-20 15:48:27 +0000102 static char ID;
103
Eugene Zelenko59e12822017-08-08 00:47:13 +0000104 SIAnnotateControlFlow() : FunctionPass(ID) {}
Tom Stellardf8794352012-12-19 22:10:31 +0000105
Craig Topper5656db42014-04-29 07:57:24 +0000106 bool doInitialization(Module &M) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000107
Craig Topper5656db42014-04-29 07:57:24 +0000108 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000109
Mehdi Amini117296c2016-10-01 02:56:57 +0000110 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +0000111
Craig Topper5656db42014-04-29 07:57:24 +0000112 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +0000113 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000114 AU.addRequired<DominatorTreeWrapperPass>();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000115 AU.addRequired<LegacyDivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000116 AU.addPreserved<DominatorTreeWrapperPass>();
Tom Stellardf8794352012-12-19 22:10:31 +0000117 FunctionPass::getAnalysisUsage(AU);
118 }
Tom Stellardf8794352012-12-19 22:10:31 +0000119};
120
121} // end anonymous namespace
122
Tom Stellard77a17772016-01-20 15:48:27 +0000123INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
124 "Annotate SI Control Flow", false, false)
Matt Arsenault31a58c62017-03-02 23:50:51 +0000125INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000126INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
Tom Stellard77a17772016-01-20 15:48:27 +0000127INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
128 "Annotate SI Control Flow", false, false)
129
Tom Stellardf8794352012-12-19 22:10:31 +0000130char SIAnnotateControlFlow::ID = 0;
131
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000132/// Initialize all the types and constants used in the pass
Tom Stellardf8794352012-12-19 22:10:31 +0000133bool SIAnnotateControlFlow::doInitialization(Module &M) {
Tom Stellardf8794352012-12-19 22:10:31 +0000134 LLVMContext &Context = M.getContext();
135
136 Void = Type::getVoidTy(Context);
137 Boolean = Type::getInt1Ty(Context);
138 Int64 = Type::getInt64Ty(Context);
Serge Guelton1b421c22017-05-11 08:46:02 +0000139 ReturnStruct = StructType::get(Boolean, Int64);
Tom Stellardf8794352012-12-19 22:10:31 +0000140
141 BoolTrue = ConstantInt::getTrue(Context);
142 BoolFalse = ConstantInt::getFalse(Context);
143 BoolUndef = UndefValue::get(Boolean);
144 Int64Zero = ConstantInt::get(Int64, 0);
145
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000146 If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if);
147 Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else);
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000148 IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break);
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000149 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop);
150 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf);
Tom Stellardf8794352012-12-19 22:10:31 +0000151 return false;
152}
153
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000154/// Is the branch condition uniform or did the StructurizeCFG pass
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000155/// consider it as such?
156bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
Rhys Perryf77e2e82019-01-07 15:52:28 +0000157 return DA->isUniform(T) ||
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000158 T->getMetadata("structurizecfg.uniform") != nullptr;
159}
160
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000161/// Is BB the last block saved on the stack ?
Tom Stellardf8794352012-12-19 22:10:31 +0000162bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
Michel Danzerae0a4032013-02-14 08:00:33 +0000163 return !Stack.empty() && Stack.back().first == BB;
Tom Stellardf8794352012-12-19 22:10:31 +0000164}
165
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000166/// Pop the last saved value from the control flow stack
Tom Stellardf8794352012-12-19 22:10:31 +0000167Value *SIAnnotateControlFlow::popSaved() {
168 return Stack.pop_back_val().second;
169}
170
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000171/// Push a BB and saved value to the control flow stack
Tom Stellardf8794352012-12-19 22:10:31 +0000172void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
173 Stack.push_back(std::make_pair(BB, Saved));
174}
175
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000176/// Can the condition represented by this PHI node treated like
Tom Stellardf8794352012-12-19 22:10:31 +0000177/// an "Else" block?
178bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
Tom Stellardf8794352012-12-19 22:10:31 +0000179 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
180 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
181 if (Phi->getIncomingBlock(i) == IDom) {
182
183 if (Phi->getIncomingValue(i) != BoolTrue)
184 return false;
185
186 } else {
187 if (Phi->getIncomingValue(i) != BoolFalse)
188 return false;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000189
Tom Stellardf8794352012-12-19 22:10:31 +0000190 }
191 }
192 return true;
193}
194
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000195// Erase "Phi" if it is not used any more
Tom Stellardf8794352012-12-19 22:10:31 +0000196void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
Eugene Zelenko59e12822017-08-08 00:47:13 +0000197 if (RecursivelyDeleteDeadPHINode(Phi)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000198 LLVM_DEBUG(dbgs() << "Erased unused condition phi\n");
Matt Arsenault0607a442017-03-24 20:57:10 +0000199 }
Tom Stellardf8794352012-12-19 22:10:31 +0000200}
201
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000202/// Open a new "If" block
Tom Stellardf8794352012-12-19 22:10:31 +0000203void SIAnnotateControlFlow::openIf(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000204 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000205 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000206
Tom Stellardf8794352012-12-19 22:10:31 +0000207 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
208 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
209 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
210}
211
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000212/// Close the last "If" block and open a new "Else" block
Tom Stellardf8794352012-12-19 22:10:31 +0000213void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000214 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000215 return;
216 }
Tom Stellardf8794352012-12-19 22:10:31 +0000217 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
218 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
219 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
220}
221
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000222/// Recursively handle the condition leading to a loop
Matt Arsenault0607a442017-03-24 20:57:10 +0000223Value *SIAnnotateControlFlow::handleLoopCondition(
Nicolai Haehnle28212cc2018-10-31 13:26:48 +0000224 Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000225 if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000226 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000227 Instruction *Insert;
228 if (L->contains(Inst)) {
229 Insert = Parent->getTerminator();
230 } else {
231 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
232 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000233
Tom Stellardde16a2e2014-06-20 17:06:02 +0000234 Value *Args[] = { Cond, Broken };
235 return CallInst::Create(IfBreak, Args, "", Insert);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000236 }
Tom Stellardf8794352012-12-19 22:10:31 +0000237
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000238 // Insert IfBreak in the loop header TERM for constant COND other than true.
239 if (isa<Constant>(Cond)) {
240 Instruction *Insert = Cond == BoolTrue ?
241 Term : L->getHeader()->getTerminator();
242
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000243 Value *Args[] = { Cond, Broken };
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000244 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000245 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000246
247 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000248}
249
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000250/// Handle a back edge (loop)
Tom Stellardf8794352012-12-19 22:10:31 +0000251void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000252 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000253 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000254
Tom Stellardd4a19502015-04-14 14:36:45 +0000255 BasicBlock *BB = Term->getParent();
256 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000257 if (!L)
258 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000259
Tom Stellardf8794352012-12-19 22:10:31 +0000260 BasicBlock *Target = Term->getSuccessor(1);
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000261 PHINode *Broken = PHINode::Create(Int64, 0, "phi.broken", &Target->front());
Tom Stellardf8794352012-12-19 22:10:31 +0000262
Tom Stellardf8794352012-12-19 22:10:31 +0000263 Value *Cond = Term->getCondition();
264 Term->setCondition(BoolTrue);
Nicolai Haehnle28212cc2018-10-31 13:26:48 +0000265 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
Tom Stellardf8794352012-12-19 22:10:31 +0000266
Changpeng Fang989ec592019-03-15 21:02:48 +0000267 for (BasicBlock *Pred : predecessors(Target)) {
268 Value *PHIValue = Int64Zero;
269 if (Pred == BB) // Remember the value of the previous iteration.
270 PHIValue = Arg;
271 // If the backedge from Pred to Target could be executed before the exit
272 // of the loop at BB, it should not reset or change "Broken", which keeps
273 // track of the number of threads exited the loop at BB.
274 else if (L->contains(Pred) && DT->dominates(Pred, BB))
275 PHIValue = Broken;
276 Broken->addIncoming(PHIValue, Pred);
277 }
Tom Stellardf8794352012-12-19 22:10:31 +0000278
279 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
Matt Arsenault0607a442017-03-24 20:57:10 +0000280
Tom Stellardf8794352012-12-19 22:10:31 +0000281 push(Term->getSuccessor(0), Arg);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000282}
283
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000284/// Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000285void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000286 llvm::Loop *L = LI->getLoopFor(BB);
287
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000288 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000289
Tom Stellard0f29de72015-02-05 15:32:15 +0000290 if (L && L->getHeader() == BB) {
291 // We can't insert an EndCF call into a loop header, because it will
292 // get executed on every iteration of the loop, when it should be
293 // executed only once before the loop.
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000294 SmallVector <BasicBlock *, 8> Latches;
Tom Stellard0f29de72015-02-05 15:32:15 +0000295 L->getLoopLatches(Latches);
296
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000297 SmallVector<BasicBlock *, 2> Preds;
298 for (BasicBlock *Pred : predecessors(BB)) {
299 if (!is_contained(Latches, Pred))
300 Preds.push_back(Pred);
Tom Stellard0f29de72015-02-05 15:32:15 +0000301 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000302
Alina Sbirleaab6f84f72018-08-21 23:32:03 +0000303 BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
304 false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000305 }
306
Tom Stellardbc4497b2016-02-12 23:45:29 +0000307 Value *Exec = popSaved();
Changpeng Fang6b49fa42017-03-07 23:29:36 +0000308 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
309 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
310 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
Tom Stellardf8794352012-12-19 22:10:31 +0000311}
312
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000313/// Annotate the control flow with intrinsics so the backend can
Tom Stellardf8794352012-12-19 22:10:31 +0000314/// recognize if/then/else and loops.
315bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Chandler Carruth73523022014-01-13 13:07:17 +0000316 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000317 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000318 DA = &getAnalysis<LegacyDivergenceAnalysis>();
Tom Stellardf8794352012-12-19 22:10:31 +0000319
320 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
321 E = df_end(&F.getEntryBlock()); I != E; ++I) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000322 BasicBlock *BB = *I;
323 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
Tom Stellardf8794352012-12-19 22:10:31 +0000324
325 if (!Term || Term->isUnconditional()) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000326 if (isTopOfStack(BB))
327 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000328
Tom Stellardf8794352012-12-19 22:10:31 +0000329 continue;
330 }
331
332 if (I.nodeVisited(Term->getSuccessor(1))) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000333 if (isTopOfStack(BB))
334 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000335
Tom Stellardf8794352012-12-19 22:10:31 +0000336 handleLoop(Term);
337 continue;
338 }
339
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000340 if (isTopOfStack(BB)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000341 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000342 if (Phi && Phi->getParent() == BB && isElse(Phi)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000343 insertElse(Term);
344 eraseIfUnused(Phi);
345 continue;
346 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000347
348 closeControlFlow(BB);
Tom Stellardf8794352012-12-19 22:10:31 +0000349 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000350
Tom Stellardf8794352012-12-19 22:10:31 +0000351 openIf(Term);
352 }
353
Matt Arsenault1491ca82018-01-17 16:30:01 +0000354 if (!Stack.empty()) {
355 // CFG was probably not structured.
356 report_fatal_error("failed to annotate CFG");
357 }
358
Tom Stellardf8794352012-12-19 22:10:31 +0000359 return true;
360}
361
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000362/// Create the annotation pass
Tom Stellardf8794352012-12-19 22:10:31 +0000363FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000364 return new SIAnnotateControlFlow();
365}