Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 1 | //===- SIAnnotateControlFlow.cpp ------------------------------------------===// |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
| 10 | /// Annotates the control flow with hardware specific intrinsics. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "AMDGPU.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DepthFirstIterator.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
Nicolai Haehnle | 35617ed | 2018-08-30 14:21:36 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LegacyDivergenceAnalysis.h" |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/Local.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 21 | #include "llvm/IR/BasicBlock.h" |
| 22 | #include "llvm/IR/CFG.h" |
| 23 | #include "llvm/IR/Constant.h" |
Tom Stellard | b0804ec | 2013-06-07 20:28:43 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Function.h" |
| 28 | #include "llvm/IR/Instruction.h" |
Tom Stellard | b0804ec | 2013-06-07 20:28:43 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Type.h" |
| 33 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 34 | #include "llvm/Pass.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 35 | #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 Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 40 | #include <cassert> |
| 41 | #include <utility> |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace llvm; |
| 44 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 45 | #define DEBUG_TYPE "si-annotate-control-flow" |
| 46 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | |
| 49 | // Complex types used in this pass |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 50 | using StackEntry = std::pair<BasicBlock *, Value *>; |
| 51 | using StackVector = SmallVector<StackEntry, 16>; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 52 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 53 | class SIAnnotateControlFlow : public FunctionPass { |
Nicolai Haehnle | 35617ed | 2018-08-30 14:21:36 +0000 | [diff] [blame] | 54 | LegacyDivergenceAnalysis *DA; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 55 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 56 | 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 Arsenault | c5b641a | 2017-03-17 20:41:45 +0000 | [diff] [blame] | 66 | Function *If; |
| 67 | Function *Else; |
Matt Arsenault | c5b641a | 2017-03-17 20:41:45 +0000 | [diff] [blame] | 68 | Function *IfBreak; |
Matt Arsenault | c5b641a | 2017-03-17 20:41:45 +0000 | [diff] [blame] | 69 | Function *Loop; |
| 70 | Function *EndCf; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 71 | |
| 72 | DominatorTree *DT; |
| 73 | StackVector Stack; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 74 | |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 75 | LoopInfo *LI; |
| 76 | |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 77 | bool isUniform(BranchInst *T); |
| 78 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 79 | 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 Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 93 | Value * |
| 94 | handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L, |
Nicolai Haehnle | 28212cc | 2018-10-31 13:26:48 +0000 | [diff] [blame] | 95 | BranchInst *Term); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 96 | |
| 97 | void handleLoop(BranchInst *Term); |
| 98 | |
| 99 | void closeControlFlow(BasicBlock *BB); |
| 100 | |
| 101 | public: |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 102 | static char ID; |
| 103 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 104 | SIAnnotateControlFlow() : FunctionPass(ID) {} |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 105 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 106 | bool doInitialization(Module &M) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 107 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 108 | bool runOnFunction(Function &F) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 109 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 110 | StringRef getPassName() const override { return "SI annotate control flow"; } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 111 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 112 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 113 | AU.addRequired<LoopInfoWrapperPass>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 114 | AU.addRequired<DominatorTreeWrapperPass>(); |
Nicolai Haehnle | 35617ed | 2018-08-30 14:21:36 +0000 | [diff] [blame] | 115 | AU.addRequired<LegacyDivergenceAnalysis>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 116 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 117 | FunctionPass::getAnalysisUsage(AU); |
| 118 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // end anonymous namespace |
| 122 | |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 123 | INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE, |
| 124 | "Annotate SI Control Flow", false, false) |
Matt Arsenault | 31a58c6 | 2017-03-02 23:50:51 +0000 | [diff] [blame] | 125 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Nicolai Haehnle | 35617ed | 2018-08-30 14:21:36 +0000 | [diff] [blame] | 126 | INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis) |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 127 | INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE, |
| 128 | "Annotate SI Control Flow", false, false) |
| 129 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 130 | char SIAnnotateControlFlow::ID = 0; |
| 131 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 132 | /// Initialize all the types and constants used in the pass |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 133 | bool SIAnnotateControlFlow::doInitialization(Module &M) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 134 | LLVMContext &Context = M.getContext(); |
| 135 | |
| 136 | Void = Type::getVoidTy(Context); |
| 137 | Boolean = Type::getInt1Ty(Context); |
| 138 | Int64 = Type::getInt64Ty(Context); |
Serge Guelton | 1b421c2 | 2017-05-11 08:46:02 +0000 | [diff] [blame] | 139 | ReturnStruct = StructType::get(Boolean, Int64); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 140 | |
| 141 | BoolTrue = ConstantInt::getTrue(Context); |
| 142 | BoolFalse = ConstantInt::getFalse(Context); |
| 143 | BoolUndef = UndefValue::get(Boolean); |
| 144 | Int64Zero = ConstantInt::get(Int64, 0); |
| 145 | |
Matt Arsenault | c5b641a | 2017-03-17 20:41:45 +0000 | [diff] [blame] | 146 | If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if); |
| 147 | Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else); |
Matt Arsenault | c5b641a | 2017-03-17 20:41:45 +0000 | [diff] [blame] | 148 | IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break); |
Matt Arsenault | c5b641a | 2017-03-17 20:41:45 +0000 | [diff] [blame] | 149 | Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop); |
| 150 | EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 154 | /// Is the branch condition uniform or did the StructurizeCFG pass |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 155 | /// consider it as such? |
| 156 | bool SIAnnotateControlFlow::isUniform(BranchInst *T) { |
Rhys Perry | f77e2e8 | 2019-01-07 15:52:28 +0000 | [diff] [blame] | 157 | return DA->isUniform(T) || |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 158 | T->getMetadata("structurizecfg.uniform") != nullptr; |
| 159 | } |
| 160 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 161 | /// Is BB the last block saved on the stack ? |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 166 | /// Pop the last saved value from the control flow stack |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 167 | Value *SIAnnotateControlFlow::popSaved() { |
| 168 | return Stack.pop_back_val().second; |
| 169 | } |
| 170 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 171 | /// Push a BB and saved value to the control flow stack |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 172 | void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) { |
| 173 | Stack.push_back(std::make_pair(BB, Saved)); |
| 174 | } |
| 175 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 176 | /// Can the condition represented by this PHI node treated like |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 195 | // Erase "Phi" if it is not used any more |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 196 | void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) { |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 197 | if (RecursivelyDeleteDeadPHINode(Phi)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 198 | LLVM_DEBUG(dbgs() << "Erased unused condition phi\n"); |
Matt Arsenault | 0607a44 | 2017-03-24 20:57:10 +0000 | [diff] [blame] | 199 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 202 | /// Open a new "If" block |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 203 | void SIAnnotateControlFlow::openIf(BranchInst *Term) { |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 204 | if (isUniform(Term)) |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 205 | return; |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 206 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 207 | 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 Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 212 | /// Close the last "If" block and open a new "Else" block |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 213 | void SIAnnotateControlFlow::insertElse(BranchInst *Term) { |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 214 | if (isUniform(Term)) { |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 217 | 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 Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 222 | /// Recursively handle the condition leading to a loop |
Matt Arsenault | 0607a44 | 2017-03-24 20:57:10 +0000 | [diff] [blame] | 223 | Value *SIAnnotateControlFlow::handleLoopCondition( |
Nicolai Haehnle | 28212cc | 2018-10-31 13:26:48 +0000 | [diff] [blame] | 224 | Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) { |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 225 | if (Instruction *Inst = dyn_cast<Instruction>(Cond)) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 226 | BasicBlock *Parent = Inst->getParent(); |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 227 | Instruction *Insert; |
| 228 | if (L->contains(Inst)) { |
| 229 | Insert = Parent->getTerminator(); |
| 230 | } else { |
| 231 | Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime(); |
| 232 | } |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 233 | |
Tom Stellard | de16a2e | 2014-06-20 17:06:02 +0000 | [diff] [blame] | 234 | Value *Args[] = { Cond, Broken }; |
| 235 | return CallInst::Create(IfBreak, Args, "", Insert); |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 236 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 237 | |
Matt Arsenault | e70d5dc | 2017-03-17 20:52:21 +0000 | [diff] [blame] | 238 | // 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 Fang | e07f1aa | 2016-02-12 17:11:04 +0000 | [diff] [blame] | 243 | Value *Args[] = { Cond, Broken }; |
Matt Arsenault | e70d5dc | 2017-03-17 20:52:21 +0000 | [diff] [blame] | 244 | return CallInst::Create(IfBreak, Args, "", Insert); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 245 | } |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 246 | |
| 247 | llvm_unreachable("Unhandled loop condition!"); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 250 | /// Handle a back edge (loop) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 251 | void SIAnnotateControlFlow::handleLoop(BranchInst *Term) { |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 252 | if (isUniform(Term)) |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 253 | return; |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 254 | |
Tom Stellard | d4a1950 | 2015-04-14 14:36:45 +0000 | [diff] [blame] | 255 | BasicBlock *BB = Term->getParent(); |
| 256 | llvm::Loop *L = LI->getLoopFor(BB); |
Changpeng Fang | 26fb9d2 | 2016-07-28 23:01:45 +0000 | [diff] [blame] | 257 | if (!L) |
| 258 | return; |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 259 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 260 | BasicBlock *Target = Term->getSuccessor(1); |
Matt Arsenault | e70d5dc | 2017-03-17 20:52:21 +0000 | [diff] [blame] | 261 | PHINode *Broken = PHINode::Create(Int64, 0, "phi.broken", &Target->front()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 262 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 263 | Value *Cond = Term->getCondition(); |
| 264 | Term->setCondition(BoolTrue); |
Nicolai Haehnle | 28212cc | 2018-10-31 13:26:48 +0000 | [diff] [blame] | 265 | Value *Arg = handleLoopCondition(Cond, Broken, L, Term); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 266 | |
Changpeng Fang | 989ec59 | 2019-03-15 21:02:48 +0000 | [diff] [blame] | 267 | 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 Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 278 | |
| 279 | Term->setCondition(CallInst::Create(Loop, Arg, "", Term)); |
Matt Arsenault | 0607a44 | 2017-03-24 20:57:10 +0000 | [diff] [blame] | 280 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 281 | push(Term->getSuccessor(0), Arg); |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 284 | /// Close the last opened control flow |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 285 | void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) { |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 286 | llvm::Loop *L = LI->getLoopFor(BB); |
| 287 | |
Nicolai Haehnle | 19f0f51 | 2016-04-14 17:42:18 +0000 | [diff] [blame] | 288 | assert(Stack.back().first == BB); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 289 | |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 290 | 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 Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 294 | SmallVector <BasicBlock *, 8> Latches; |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 295 | L->getLoopLatches(Latches); |
| 296 | |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 297 | SmallVector<BasicBlock *, 2> Preds; |
| 298 | for (BasicBlock *Pred : predecessors(BB)) { |
| 299 | if (!is_contained(Latches, Pred)) |
| 300 | Preds.push_back(Pred); |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 301 | } |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 302 | |
Alina Sbirlea | ab6f84f7 | 2018-08-21 23:32:03 +0000 | [diff] [blame] | 303 | BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr, |
| 304 | false); |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 307 | Value *Exec = popSaved(); |
Changpeng Fang | 6b49fa4 | 2017-03-07 23:29:36 +0000 | [diff] [blame] | 308 | Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt(); |
| 309 | if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt)) |
| 310 | CallInst::Create(EndCf, Exec, "", FirstInsertionPt); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 313 | /// Annotate the control flow with intrinsics so the backend can |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 314 | /// recognize if/then/else and loops. |
| 315 | bool SIAnnotateControlFlow::runOnFunction(Function &F) { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 316 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Tom Stellard | 0f29de7 | 2015-02-05 15:32:15 +0000 | [diff] [blame] | 317 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Nicolai Haehnle | 35617ed | 2018-08-30 14:21:36 +0000 | [diff] [blame] | 318 | DA = &getAnalysis<LegacyDivergenceAnalysis>(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 319 | |
| 320 | for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()), |
| 321 | E = df_end(&F.getEntryBlock()); I != E; ++I) { |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 322 | BasicBlock *BB = *I; |
| 323 | BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 324 | |
| 325 | if (!Term || Term->isUnconditional()) { |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 326 | if (isTopOfStack(BB)) |
| 327 | closeControlFlow(BB); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 328 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 329 | continue; |
| 330 | } |
| 331 | |
| 332 | if (I.nodeVisited(Term->getSuccessor(1))) { |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 333 | if (isTopOfStack(BB)) |
| 334 | closeControlFlow(BB); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 335 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 336 | handleLoop(Term); |
| 337 | continue; |
| 338 | } |
| 339 | |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 340 | if (isTopOfStack(BB)) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 341 | PHINode *Phi = dyn_cast<PHINode>(Term->getCondition()); |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 342 | if (Phi && Phi->getParent() == BB && isElse(Phi)) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 343 | insertElse(Term); |
| 344 | eraseIfUnused(Phi); |
| 345 | continue; |
| 346 | } |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 347 | |
| 348 | closeControlFlow(BB); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 349 | } |
Matt Arsenault | 0e6e018 | 2017-03-15 18:00:12 +0000 | [diff] [blame] | 350 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 351 | openIf(Term); |
| 352 | } |
| 353 | |
Matt Arsenault | 1491ca8 | 2018-01-17 16:30:01 +0000 | [diff] [blame] | 354 | if (!Stack.empty()) { |
| 355 | // CFG was probably not structured. |
| 356 | report_fatal_error("failed to annotate CFG"); |
| 357 | } |
| 358 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 362 | /// Create the annotation pass |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 363 | FunctionPass *llvm::createSIAnnotateControlFlowPass() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 364 | return new SIAnnotateControlFlow(); |
| 365 | } |