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