blob: 150d8c3dc3d3aed4c3d438bc82d6ac1a5f01b093 [file] [log] [blame]
Eugene Zelenko59e12822017-08-08 00:47:13 +00001//===- SIAnnotateControlFlow.cpp ------------------------------------------===//
Tom Stellardf8794352012-12-19 22:10:31 +00002//
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"
Eugene Zelenko59e12822017-08-08 00:47:13 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
Tom Stellardbc4497b2016-02-12 23:45:29 +000019#include "llvm/Analysis/DivergenceAnalysis.h"
Tom Stellard0f29de72015-02-05 15:32:15 +000020#include "llvm/Analysis/LoopInfo.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"
Matt Arsenault0607a442017-03-24 20:57:10 +000040#include "llvm/Transforms/Utils/Local.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000041#include <cassert>
42#include <utility>
Tom Stellardf8794352012-12-19 22:10:31 +000043
44using namespace llvm;
45
Chandler Carruth84e68b22014-04-22 02:41:26 +000046#define DEBUG_TYPE "si-annotate-control-flow"
47
Tom Stellardf8794352012-12-19 22:10:31 +000048namespace {
49
50// Complex types used in this pass
Eugene Zelenko59e12822017-08-08 00:47:13 +000051using StackEntry = std::pair<BasicBlock *, Value *>;
52using StackVector = SmallVector<StackEntry, 16>;
Tom Stellardf8794352012-12-19 22:10:31 +000053
Tom Stellardf8794352012-12-19 22:10:31 +000054class SIAnnotateControlFlow : public FunctionPass {
Tom Stellardbc4497b2016-02-12 23:45:29 +000055 DivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000056
Tom Stellardf8794352012-12-19 22:10:31 +000057 Type *Boolean;
58 Type *Void;
59 Type *Int64;
60 Type *ReturnStruct;
61
62 ConstantInt *BoolTrue;
63 ConstantInt *BoolFalse;
64 UndefValue *BoolUndef;
65 Constant *Int64Zero;
66
Matt Arsenaultc5b641a2017-03-17 20:41:45 +000067 Function *If;
68 Function *Else;
69 Function *Break;
70 Function *IfBreak;
71 Function *ElseBreak;
72 Function *Loop;
73 Function *EndCf;
Tom Stellardf8794352012-12-19 22:10:31 +000074
75 DominatorTree *DT;
76 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000077
Tom Stellard0f29de72015-02-05 15:32:15 +000078 LoopInfo *LI;
79
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000080 bool isUniform(BranchInst *T);
81
Tom Stellardf8794352012-12-19 22:10:31 +000082 bool isTopOfStack(BasicBlock *BB);
83
84 Value *popSaved();
85
86 void push(BasicBlock *BB, Value *Saved);
87
88 bool isElse(PHINode *Phi);
89
90 void eraseIfUnused(PHINode *Phi);
91
92 void openIf(BranchInst *Term);
93
94 void insertElse(BranchInst *Term);
95
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000096 Value *
97 handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
98 BranchInst *Term,
99 SmallVectorImpl<WeakTrackingVH> &LoopPhiConditions);
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 doInitialization(Module &M) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000111
Craig Topper5656db42014-04-29 07:57:24 +0000112 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000113
Mehdi Amini117296c2016-10-01 02:56:57 +0000114 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +0000115
Craig Topper5656db42014-04-29 07:57:24 +0000116 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +0000117 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000118 AU.addRequired<DominatorTreeWrapperPass>();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000119 AU.addRequired<DivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000120 AU.addPreserved<DominatorTreeWrapperPass>();
Tom Stellardf8794352012-12-19 22:10:31 +0000121 FunctionPass::getAnalysisUsage(AU);
122 }
Tom Stellardf8794352012-12-19 22:10:31 +0000123};
124
125} // end anonymous namespace
126
Tom Stellard77a17772016-01-20 15:48:27 +0000127INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
128 "Annotate SI Control Flow", false, false)
Matt Arsenault31a58c62017-03-02 23:50:51 +0000129INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Tom Stellardbc4497b2016-02-12 23:45:29 +0000130INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
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
136/// \brief Initialize all the types and constants used in the pass
137bool SIAnnotateControlFlow::doInitialization(Module &M) {
Tom Stellardf8794352012-12-19 22:10:31 +0000138 LLVMContext &Context = M.getContext();
139
140 Void = Type::getVoidTy(Context);
141 Boolean = Type::getInt1Ty(Context);
142 Int64 = Type::getInt64Ty(Context);
Serge Guelton1b421c22017-05-11 08:46:02 +0000143 ReturnStruct = StructType::get(Boolean, Int64);
Tom Stellardf8794352012-12-19 22:10:31 +0000144
145 BoolTrue = ConstantInt::getTrue(Context);
146 BoolFalse = ConstantInt::getFalse(Context);
147 BoolUndef = UndefValue::get(Boolean);
148 Int64Zero = ConstantInt::get(Int64, 0);
149
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000150 If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if);
151 Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else);
152 Break = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_break);
153 IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break);
154 ElseBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else_break);
155 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop);
156 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf);
Tom Stellardf8794352012-12-19 22:10:31 +0000157 return false;
158}
159
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000160/// \brief Is the branch condition uniform or did the StructurizeCFG pass
161/// consider it as such?
162bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
163 return DA->isUniform(T->getCondition()) ||
164 T->getMetadata("structurizecfg.uniform") != nullptr;
165}
166
Tom Stellardf8794352012-12-19 22:10:31 +0000167/// \brief Is BB the last block saved on the stack ?
168bool 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
172/// \brief Pop the last saved value from the control flow stack
173Value *SIAnnotateControlFlow::popSaved() {
174 return Stack.pop_back_val().second;
175}
176
177/// \brief Push a BB and saved value to the control flow stack
178void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
179 Stack.push_back(std::make_pair(BB, Saved));
180}
181
182/// \brief Can the condition represented by this PHI node treated like
183/// 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
201// \brief Erase "Phi" if it is not used any more
202void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
Eugene Zelenko59e12822017-08-08 00:47:13 +0000203 if (RecursivelyDeleteDeadPHINode(Phi)) {
Matt Arsenault0607a442017-03-24 20:57:10 +0000204 DEBUG(dbgs() << "Erased unused condition phi\n");
205 }
Tom Stellardf8794352012-12-19 22:10:31 +0000206}
207
208/// \brief Open a new "If" block
209void 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
218/// \brief Close the last "If" block and open a new "Else" block
219void 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
228/// \brief Recursively handle the condition leading to a loop
Matt Arsenault0607a442017-03-24 20:57:10 +0000229Value *SIAnnotateControlFlow::handleLoopCondition(
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000230 Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term,
231 SmallVectorImpl<WeakTrackingVH> &LoopPhiConditions) {
Tom Stellard0b7feb12015-05-01 03:44:08 +0000232 // Only search through PHI nodes which are inside the loop. If we try this
233 // with PHI nodes that are outside of the loop, we end up inserting new PHI
234 // nodes outside of the loop which depend on values defined inside the loop.
235 // This will break the module with
236 // 'Instruction does not dominate all users!' errors.
237 PHINode *Phi = nullptr;
238 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
Tom Stellardde16a2e2014-06-20 17:06:02 +0000239 BasicBlock *Parent = Phi->getParent();
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000240 PHINode *NewPhi = PHINode::Create(Int64, 0, "loop.phi", &Parent->front());
Tom Stellardde16a2e2014-06-20 17:06:02 +0000241 Value *Ret = NewPhi;
Tom Stellardf8794352012-12-19 22:10:31 +0000242
Alp Tokerf907b892013-12-05 05:44:44 +0000243 // Handle all non-constant incoming values first
Tom Stellardf8794352012-12-19 22:10:31 +0000244 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
245 Value *Incoming = Phi->getIncomingValue(i);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000246 BasicBlock *From = Phi->getIncomingBlock(i);
247 if (isa<ConstantInt>(Incoming)) {
248 NewPhi->addIncoming(Broken, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000249 continue;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000250 }
Tom Stellardf8794352012-12-19 22:10:31 +0000251
252 Phi->setIncomingValue(i, BoolFalse);
Matt Arsenault0607a442017-03-24 20:57:10 +0000253 Value *PhiArg = handleLoopCondition(Incoming, Broken, L,
254 Term, LoopPhiConditions);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000255 NewPhi->addIncoming(PhiArg, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000256 }
257
Tom Stellardf8794352012-12-19 22:10:31 +0000258 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
259
260 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
Tom Stellardf8794352012-12-19 22:10:31 +0000261 Value *Incoming = Phi->getIncomingValue(i);
262 if (Incoming != BoolTrue)
263 continue;
264
265 BasicBlock *From = Phi->getIncomingBlock(i);
266 if (From == IDom) {
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000267 // We're in the following situation:
268 // IDom/From
269 // | \
270 // | If-block
271 // | /
272 // Parent
273 // where we want to break out of the loop if the If-block is not taken.
274 // Due to the depth-first traversal, there should be an end.cf
275 // intrinsic in Parent, and we insert an else.break before it.
276 //
277 // Note that the end.cf need not be the first non-phi instruction
278 // of parent, particularly when we're dealing with a multi-level
279 // break, but it should occur within a group of intrinsic calls
280 // at the beginning of the block.
Tom Stellardf8794352012-12-19 22:10:31 +0000281 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000282 while (OldEnd && OldEnd->getCalledFunction() != EndCf)
283 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode());
Tom Stellardf8794352012-12-19 22:10:31 +0000284 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
Tom Stellardde16a2e2014-06-20 17:06:02 +0000285 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
286 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
Tom Stellardf8794352012-12-19 22:10:31 +0000287 continue;
288 }
289 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000290
Tom Stellardf8794352012-12-19 22:10:31 +0000291 TerminatorInst *Insert = From->getTerminator();
Tom Stellardde16a2e2014-06-20 17:06:02 +0000292 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
293 NewPhi->setIncomingValue(i, PhiArg);
Tom Stellardf8794352012-12-19 22:10:31 +0000294 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000295
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000296 LoopPhiConditions.push_back(WeakTrackingVH(Phi));
Tom Stellardde16a2e2014-06-20 17:06:02 +0000297 return Ret;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000298 }
Tom Stellardf8794352012-12-19 22:10:31 +0000299
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000300 if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000301 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000302 Instruction *Insert;
303 if (L->contains(Inst)) {
304 Insert = Parent->getTerminator();
305 } else {
306 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
307 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000308
Tom Stellardde16a2e2014-06-20 17:06:02 +0000309 Value *Args[] = { Cond, Broken };
310 return CallInst::Create(IfBreak, Args, "", Insert);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000311 }
Tom Stellardf8794352012-12-19 22:10:31 +0000312
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000313 // Insert IfBreak in the loop header TERM for constant COND other than true.
314 if (isa<Constant>(Cond)) {
315 Instruction *Insert = Cond == BoolTrue ?
316 Term : L->getHeader()->getTerminator();
317
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000318 Value *Args[] = { Cond, Broken };
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000319 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000320 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000321
322 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000323}
324
325/// \brief Handle a back edge (loop)
326void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000327 if (isUniform(Term))
Tom Stellardbc4497b2016-02-12 23:45:29 +0000328 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000329
Tom Stellardd4a19502015-04-14 14:36:45 +0000330 BasicBlock *BB = Term->getParent();
331 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000332 if (!L)
333 return;
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000334
Tom Stellardf8794352012-12-19 22:10:31 +0000335 BasicBlock *Target = Term->getSuccessor(1);
Matt Arsenaulte70d5dc2017-03-17 20:52:21 +0000336 PHINode *Broken = PHINode::Create(Int64, 0, "phi.broken", &Target->front());
Tom Stellardf8794352012-12-19 22:10:31 +0000337
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000338 SmallVector<WeakTrackingVH, 8> LoopPhiConditions;
Tom Stellardf8794352012-12-19 22:10:31 +0000339 Value *Cond = Term->getCondition();
340 Term->setCondition(BoolTrue);
Matt Arsenault0607a442017-03-24 20:57:10 +0000341 Value *Arg = handleLoopCondition(Cond, Broken, L, Term, LoopPhiConditions);
Tom Stellardf8794352012-12-19 22:10:31 +0000342
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000343 for (BasicBlock *Pred : predecessors(Target))
344 Broken->addIncoming(Pred == BB ? Arg : Int64Zero, Pred);
Tom Stellardf8794352012-12-19 22:10:31 +0000345
346 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
Matt Arsenault0607a442017-03-24 20:57:10 +0000347
Eugene Zelenko59e12822017-08-08 00:47:13 +0000348 for (WeakTrackingVH Val : llvm::reverse(LoopPhiConditions)) {
Matt Arsenault0607a442017-03-24 20:57:10 +0000349 if (PHINode *Cond = cast_or_null<PHINode>(Val))
350 eraseIfUnused(Cond);
351 }
352
Tom Stellardf8794352012-12-19 22:10:31 +0000353 push(Term->getSuccessor(0), Arg);
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000354}
355
356/// \brief Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000357void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000358 llvm::Loop *L = LI->getLoopFor(BB);
359
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000360 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000361
Tom Stellard0f29de72015-02-05 15:32:15 +0000362 if (L && L->getHeader() == BB) {
363 // We can't insert an EndCF call into a loop header, because it will
364 // get executed on every iteration of the loop, when it should be
365 // executed only once before the loop.
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000366 SmallVector <BasicBlock *, 8> Latches;
Tom Stellard0f29de72015-02-05 15:32:15 +0000367 L->getLoopLatches(Latches);
368
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000369 SmallVector<BasicBlock *, 2> Preds;
370 for (BasicBlock *Pred : predecessors(BB)) {
371 if (!is_contained(Latches, Pred))
372 Preds.push_back(Pred);
Tom Stellard0f29de72015-02-05 15:32:15 +0000373 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000374
Eugene Zelenko59e12822017-08-08 00:47:13 +0000375 BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000376 }
377
Tom Stellardbc4497b2016-02-12 23:45:29 +0000378 Value *Exec = popSaved();
Changpeng Fang6b49fa42017-03-07 23:29:36 +0000379 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
380 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
381 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
Tom Stellardf8794352012-12-19 22:10:31 +0000382}
383
384/// \brief Annotate the control flow with intrinsics so the backend can
385/// recognize if/then/else and loops.
386bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Chandler Carruth73523022014-01-13 13:07:17 +0000387 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000388 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000389 DA = &getAnalysis<DivergenceAnalysis>();
Tom Stellardf8794352012-12-19 22:10:31 +0000390
391 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
392 E = df_end(&F.getEntryBlock()); I != E; ++I) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000393 BasicBlock *BB = *I;
394 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
Tom Stellardf8794352012-12-19 22:10:31 +0000395
396 if (!Term || Term->isUnconditional()) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000397 if (isTopOfStack(BB))
398 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000399
Tom Stellardf8794352012-12-19 22:10:31 +0000400 continue;
401 }
402
403 if (I.nodeVisited(Term->getSuccessor(1))) {
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000404 if (isTopOfStack(BB))
405 closeControlFlow(BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000406
Tom Stellardf8794352012-12-19 22:10:31 +0000407 handleLoop(Term);
408 continue;
409 }
410
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000411 if (isTopOfStack(BB)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000412 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000413 if (Phi && Phi->getParent() == BB && isElse(Phi)) {
Tom Stellardf8794352012-12-19 22:10:31 +0000414 insertElse(Term);
415 eraseIfUnused(Phi);
416 continue;
417 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000418
419 closeControlFlow(BB);
Tom Stellardf8794352012-12-19 22:10:31 +0000420 }
Matt Arsenault0e6e0182017-03-15 18:00:12 +0000421
Tom Stellardf8794352012-12-19 22:10:31 +0000422 openIf(Term);
423 }
424
425 assert(Stack.empty());
426 return true;
427}
428
429/// \brief Create the annotation pass
430FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000431 return new SIAnnotateControlFlow();
432}