Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1 | //===-- 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 Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DepthFirstIterator.h" |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Tom Stellard | b0804ec | 2013-06-07 20:28:43 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Dominators.h" |
Tom Stellard | b0804ec | 2013-06-07 20:28:43 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
| 23 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "si-annotate-control-flow" |
| 29 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // Complex types used in this pass |
| 33 | typedef std::pair<BasicBlock *, Value *> StackEntry; |
| 34 | typedef SmallVector<StackEntry, 16> StackVector; |
| 35 | |
| 36 | // Intrinsic names the control flow is annotated with |
Matt Arsenault | 7898b90 | 2016-01-22 18:42:55 +0000 | [diff] [blame] | 37 | static const char *const IfIntrinsic = "llvm.amdgcn.if"; |
| 38 | static const char *const ElseIntrinsic = "llvm.amdgcn.else"; |
| 39 | static const char *const BreakIntrinsic = "llvm.amdgcn.break"; |
| 40 | static const char *const IfBreakIntrinsic = "llvm.amdgcn.if.break"; |
| 41 | static const char *const ElseBreakIntrinsic = "llvm.amdgcn.else.break"; |
| 42 | static const char *const LoopIntrinsic = "llvm.amdgcn.loop"; |
| 43 | static const char *const EndCfIntrinsic = "llvm.amdgcn.end.cf"; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 44 | |
| 45 | class SIAnnotateControlFlow : public FunctionPass { |
| 46 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 47 | Type *Boolean; |
| 48 | Type *Void; |
| 49 | Type *Int64; |
| 50 | Type *ReturnStruct; |
| 51 | |
| 52 | ConstantInt *BoolTrue; |
| 53 | ConstantInt *BoolFalse; |
| 54 | UndefValue *BoolUndef; |
| 55 | Constant *Int64Zero; |
| 56 | |
| 57 | Constant *If; |
| 58 | Constant *Else; |
| 59 | Constant *Break; |
| 60 | Constant *IfBreak; |
| 61 | Constant *ElseBreak; |
| 62 | Constant *Loop; |
| 63 | Constant *EndCf; |
| 64 | |
| 65 | DominatorTree *DT; |
| 66 | StackVector Stack; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 67 | |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 68 | LoopInfo *LI; |
| 69 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 70 | bool isTopOfStack(BasicBlock *BB); |
| 71 | |
| 72 | Value *popSaved(); |
| 73 | |
| 74 | void push(BasicBlock *BB, Value *Saved); |
| 75 | |
| 76 | bool isElse(PHINode *Phi); |
| 77 | |
| 78 | void eraseIfUnused(PHINode *Phi); |
| 79 | |
| 80 | void openIf(BranchInst *Term); |
| 81 | |
| 82 | void insertElse(BranchInst *Term); |
| 83 | |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 84 | Value *handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 85 | |
| 86 | void handleLoop(BranchInst *Term); |
| 87 | |
| 88 | void closeControlFlow(BasicBlock *BB); |
| 89 | |
| 90 | public: |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 91 | static char ID; |
| 92 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 93 | SIAnnotateControlFlow(): |
| 94 | FunctionPass(ID) { } |
| 95 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 96 | bool doInitialization(Module &M) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 97 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 98 | bool runOnFunction(Function &F) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 99 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 100 | const char *getPassName() const override { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 101 | return "SI annotate control flow"; |
| 102 | } |
| 103 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 104 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 105 | AU.addRequired<LoopInfoWrapperPass>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 106 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 107 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 108 | FunctionPass::getAnalysisUsage(AU); |
| 109 | } |
| 110 | |
| 111 | }; |
| 112 | |
| 113 | } // end anonymous namespace |
| 114 | |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 115 | INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE, |
| 116 | "Annotate SI Control Flow", false, false) |
| 117 | INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE, |
| 118 | "Annotate SI Control Flow", false, false) |
| 119 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 120 | char SIAnnotateControlFlow::ID = 0; |
| 121 | |
| 122 | /// \brief Initialize all the types and constants used in the pass |
| 123 | bool SIAnnotateControlFlow::doInitialization(Module &M) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 124 | LLVMContext &Context = M.getContext(); |
| 125 | |
| 126 | Void = Type::getVoidTy(Context); |
| 127 | Boolean = Type::getInt1Ty(Context); |
| 128 | Int64 = Type::getInt64Ty(Context); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 129 | ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 130 | |
| 131 | BoolTrue = ConstantInt::getTrue(Context); |
| 132 | BoolFalse = ConstantInt::getFalse(Context); |
| 133 | BoolUndef = UndefValue::get(Boolean); |
| 134 | Int64Zero = ConstantInt::get(Int64, 0); |
| 135 | |
| 136 | If = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 137 | IfIntrinsic, ReturnStruct, Boolean, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 138 | |
| 139 | Else = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 140 | ElseIntrinsic, ReturnStruct, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 141 | |
| 142 | Break = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 143 | BreakIntrinsic, Int64, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 144 | |
| 145 | IfBreak = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 146 | IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 147 | |
| 148 | ElseBreak = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 149 | ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 150 | |
| 151 | Loop = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 152 | LoopIntrinsic, Boolean, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 153 | |
| 154 | EndCf = M.getOrInsertFunction( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 155 | EndCfIntrinsic, Void, Int64, (Type *)nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /// \brief Is BB the last block saved on the stack ? |
| 161 | bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) { |
Michel Danzer | ae0a403 | 2013-02-14 08:00:33 +0000 | [diff] [blame] | 162 | return !Stack.empty() && Stack.back().first == BB; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /// \brief Pop the last saved value from the control flow stack |
| 166 | Value *SIAnnotateControlFlow::popSaved() { |
| 167 | return Stack.pop_back_val().second; |
| 168 | } |
| 169 | |
| 170 | /// \brief Push a BB and saved value to the control flow stack |
| 171 | void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) { |
| 172 | Stack.push_back(std::make_pair(BB, Saved)); |
| 173 | } |
| 174 | |
| 175 | /// \brief Can the condition represented by this PHI node treated like |
| 176 | /// an "Else" block? |
| 177 | bool SIAnnotateControlFlow::isElse(PHINode *Phi) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 178 | BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock(); |
| 179 | for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { |
| 180 | if (Phi->getIncomingBlock(i) == IDom) { |
| 181 | |
| 182 | if (Phi->getIncomingValue(i) != BoolTrue) |
| 183 | return false; |
| 184 | |
| 185 | } else { |
| 186 | if (Phi->getIncomingValue(i) != BoolFalse) |
| 187 | return false; |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 188 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | // \brief Erase "Phi" if it is not used any more |
| 195 | void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) { |
| 196 | if (!Phi->hasNUsesOrMore(1)) |
| 197 | Phi->eraseFromParent(); |
| 198 | } |
| 199 | |
| 200 | /// \brief Open a new "If" block |
| 201 | void SIAnnotateControlFlow::openIf(BranchInst *Term) { |
| 202 | Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term); |
| 203 | Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term)); |
| 204 | push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term)); |
| 205 | } |
| 206 | |
| 207 | /// \brief Close the last "If" block and open a new "Else" block |
| 208 | void SIAnnotateControlFlow::insertElse(BranchInst *Term) { |
| 209 | Value *Ret = CallInst::Create(Else, popSaved(), "", Term); |
| 210 | Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term)); |
| 211 | push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term)); |
| 212 | } |
| 213 | |
| 214 | /// \brief Recursively handle the condition leading to a loop |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 215 | Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken, |
| 216 | llvm::Loop *L) { |
Tom Stellard | 0b7feb1 | 2015-05-01 03:44:08 +0000 | [diff] [blame] | 217 | |
| 218 | // Only search through PHI nodes which are inside the loop. If we try this |
| 219 | // with PHI nodes that are outside of the loop, we end up inserting new PHI |
| 220 | // nodes outside of the loop which depend on values defined inside the loop. |
| 221 | // This will break the module with |
| 222 | // 'Instruction does not dominate all users!' errors. |
| 223 | PHINode *Phi = nullptr; |
| 224 | if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) { |
| 225 | |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 226 | BasicBlock *Parent = Phi->getParent(); |
| 227 | PHINode *NewPhi = PHINode::Create(Int64, 0, "", &Parent->front()); |
| 228 | Value *Ret = NewPhi; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 229 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 230 | // Handle all non-constant incoming values first |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 231 | for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { |
| 232 | Value *Incoming = Phi->getIncomingValue(i); |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 233 | BasicBlock *From = Phi->getIncomingBlock(i); |
| 234 | if (isa<ConstantInt>(Incoming)) { |
| 235 | NewPhi->addIncoming(Broken, From); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 236 | continue; |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 237 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 238 | |
| 239 | Phi->setIncomingValue(i, BoolFalse); |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 240 | Value *PhiArg = handleLoopCondition(Incoming, Broken, L); |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 241 | NewPhi->addIncoming(PhiArg, From); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 244 | BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock(); |
| 245 | |
| 246 | for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { |
| 247 | |
| 248 | Value *Incoming = Phi->getIncomingValue(i); |
| 249 | if (Incoming != BoolTrue) |
| 250 | continue; |
| 251 | |
| 252 | BasicBlock *From = Phi->getIncomingBlock(i); |
| 253 | if (From == IDom) { |
| 254 | CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt()); |
| 255 | if (OldEnd && OldEnd->getCalledFunction() == EndCf) { |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 256 | Value *Args[] = { OldEnd->getArgOperand(0), NewPhi }; |
| 257 | Ret = CallInst::Create(ElseBreak, Args, "", OldEnd); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 261 | TerminatorInst *Insert = From->getTerminator(); |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 262 | Value *PhiArg = CallInst::Create(Break, Broken, "", Insert); |
| 263 | NewPhi->setIncomingValue(i, PhiArg); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 264 | } |
| 265 | eraseIfUnused(Phi); |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 266 | return Ret; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 267 | |
| 268 | } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) { |
| 269 | BasicBlock *Parent = Inst->getParent(); |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 270 | Instruction *Insert; |
| 271 | if (L->contains(Inst)) { |
| 272 | Insert = Parent->getTerminator(); |
| 273 | } else { |
| 274 | Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime(); |
| 275 | } |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 276 | Value *Args[] = { Cond, Broken }; |
| 277 | return CallInst::Create(IfBreak, Args, "", Insert); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 278 | |
| 279 | } else { |
Matt Arsenault | eaa3a7e | 2013-12-10 21:37:42 +0000 | [diff] [blame] | 280 | llvm_unreachable("Unhandled loop condition!"); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 281 | } |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 282 | return 0; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /// \brief Handle a back edge (loop) |
| 286 | void SIAnnotateControlFlow::handleLoop(BranchInst *Term) { |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 287 | BasicBlock *BB = Term->getParent(); |
| 288 | llvm::Loop *L = LI->getLoopFor(BB); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 289 | BasicBlock *Target = Term->getSuccessor(1); |
| 290 | PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front()); |
| 291 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 292 | Value *Cond = Term->getCondition(); |
| 293 | Term->setCondition(BoolTrue); |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 294 | Value *Arg = handleLoopCondition(Cond, Broken, L); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 295 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 296 | for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target); |
| 297 | PI != PE; ++PI) { |
| 298 | |
| 299 | Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI); |
| 300 | } |
| 301 | |
| 302 | Term->setCondition(CallInst::Create(Loop, Arg, "", Term)); |
| 303 | push(Term->getSuccessor(0), Arg); |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 304 | }/// \brief Close the last opened control flow |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 305 | void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) { |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 306 | llvm::Loop *L = LI->getLoopFor(BB); |
| 307 | |
| 308 | if (L && L->getHeader() == BB) { |
| 309 | // We can't insert an EndCF call into a loop header, because it will |
| 310 | // get executed on every iteration of the loop, when it should be |
| 311 | // executed only once before the loop. |
| 312 | SmallVector <BasicBlock*, 8> Latches; |
| 313 | L->getLoopLatches(Latches); |
| 314 | |
| 315 | std::vector<BasicBlock*> Preds; |
| 316 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { |
| 317 | if (std::find(Latches.begin(), Latches.end(), *PI) == Latches.end()) |
| 318 | Preds.push_back(*PI); |
| 319 | } |
Chandler Carruth | 96ada25 | 2015-07-22 09:52:54 +0000 | [diff] [blame] | 320 | BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false); |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Duncan P. N. Exon Smith | a73371a | 2015-10-13 20:07:10 +0000 | [diff] [blame] | 323 | CallInst::Create(EndCf, popSaved(), "", &*BB->getFirstInsertionPt()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// \brief Annotate the control flow with intrinsics so the backend can |
| 327 | /// recognize if/then/else and loops. |
| 328 | bool SIAnnotateControlFlow::runOnFunction(Function &F) { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 329 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 330 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 331 | |
| 332 | for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()), |
| 333 | E = df_end(&F.getEntryBlock()); I != E; ++I) { |
| 334 | |
| 335 | BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator()); |
| 336 | |
| 337 | if (!Term || Term->isUnconditional()) { |
| 338 | if (isTopOfStack(*I)) |
| 339 | closeControlFlow(*I); |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | if (I.nodeVisited(Term->getSuccessor(1))) { |
| 344 | if (isTopOfStack(*I)) |
| 345 | closeControlFlow(*I); |
| 346 | handleLoop(Term); |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | if (isTopOfStack(*I)) { |
| 351 | PHINode *Phi = dyn_cast<PHINode>(Term->getCondition()); |
| 352 | if (Phi && Phi->getParent() == *I && isElse(Phi)) { |
| 353 | insertElse(Term); |
| 354 | eraseIfUnused(Phi); |
| 355 | continue; |
| 356 | } |
| 357 | closeControlFlow(*I); |
| 358 | } |
| 359 | openIf(Term); |
| 360 | } |
| 361 | |
| 362 | assert(Stack.empty()); |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | /// \brief Create the annotation pass |
| 367 | FunctionPass *llvm::createSIAnnotateControlFlowPass() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 368 | return new SIAnnotateControlFlow(); |
| 369 | } |