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