Chandler Carruth | 47e1db1 | 2011-10-16 22:15:07 +0000 | [diff] [blame] | 1 | //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===// |
| 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 | // This pass lowers the 'expect' intrinsic to LLVM metadata. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Xinliang David Li | 621e8dc | 2017-06-02 02:09:31 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/iterator_range.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/BasicBlock.h" |
| 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Intrinsics.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/MDBuilder.h" |
| 25 | #include "llvm/IR/Metadata.h" |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "lower-expect-intrinsic" |
| 34 | |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 35 | STATISTIC(ExpectIntrinsicsHandled, |
| 36 | "Number of 'expect' intrinsic instructions handled"); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 37 | |
Sanjay Patel | d2d2aa5 | 2016-04-26 22:23:38 +0000 | [diff] [blame] | 38 | // These default values are chosen to represent an extremely skewed outcome for |
| 39 | // a condition, but they leave some room for interpretation by later passes. |
| 40 | // |
| 41 | // If the documentation for __builtin_expect() was made explicit that it should |
| 42 | // only be used in extreme cases, we could make this ratio higher. As it stands, |
| 43 | // programmers may be using __builtin_expect() / llvm.expect to annotate that a |
| 44 | // branch is likely or unlikely to be taken. |
| 45 | // |
| 46 | // There is a known dependency on this ratio in CodeGenPrepare when transforming |
| 47 | // 'select' instructions. It may be worthwhile to hoist these values to some |
| 48 | // shared space, so they can be used directly by other passes. |
| 49 | |
| 50 | static cl::opt<uint32_t> LikelyBranchWeight( |
| 51 | "likely-branch-weight", cl::Hidden, cl::init(2000), |
| 52 | cl::desc("Weight of the branch likely to be taken (default = 2000)")); |
| 53 | static cl::opt<uint32_t> UnlikelyBranchWeight( |
| 54 | "unlikely-branch-weight", cl::Hidden, cl::init(1), |
| 55 | cl::desc("Weight of the branch unlikely to be taken (default = 1)")); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 56 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 57 | static bool handleSwitchExpect(SwitchInst &SI) { |
| 58 | CallInst *CI = dyn_cast<CallInst>(SI.getCondition()); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 59 | if (!CI) |
| 60 | return false; |
| 61 | |
| 62 | Function *Fn = CI->getCalledFunction(); |
| 63 | if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) |
| 64 | return false; |
| 65 | |
| 66 | Value *ArgValue = CI->getArgOperand(0); |
| 67 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 68 | if (!ExpectedValue) |
| 69 | return false; |
| 70 | |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 71 | SwitchInst::CaseHandle Case = *SI.findCaseValue(ExpectedValue); |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 72 | unsigned n = SI.getNumCases(); // +1 for default case. |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 73 | SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 74 | |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 75 | if (Case == *SI.case_default()) |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 76 | Weights[0] = LikelyBranchWeight; |
| 77 | else |
| 78 | Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 80 | SI.setMetadata(LLVMContext::MD_prof, |
| 81 | MDBuilder(CI->getContext()).createBranchWeights(Weights)); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 82 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 83 | SI.setCondition(ArgValue); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Xinliang David Li | 621e8dc | 2017-06-02 02:09:31 +0000 | [diff] [blame] | 87 | /// Handler for PHINodes that define the value argument to an |
| 88 | /// @llvm.expect call. |
| 89 | /// |
| 90 | /// If the operand of the phi has a constant value and it 'contradicts' |
| 91 | /// with the expected value of phi def, then the corresponding incoming |
| 92 | /// edge of the phi is unlikely to be taken. Using that information, |
| 93 | /// the branch probability info for the originating branch can be inferred. |
| 94 | static void handlePhiDef(CallInst *Expect) { |
| 95 | Value &Arg = *Expect->getArgOperand(0); |
Xinliang David Li | 4f49bee | 2017-06-07 18:32:24 +0000 | [diff] [blame] | 96 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(Expect->getArgOperand(1)); |
| 97 | if (!ExpectedValue) |
| 98 | return; |
Xinliang David Li | 621e8dc | 2017-06-02 02:09:31 +0000 | [diff] [blame] | 99 | const APInt &ExpectedPhiValue = ExpectedValue->getValue(); |
| 100 | |
| 101 | // Walk up in backward a list of instructions that |
| 102 | // have 'copy' semantics by 'stripping' the copies |
| 103 | // until a PHI node or an instruction of unknown kind |
| 104 | // is reached. Negation via xor is also handled. |
| 105 | // |
| 106 | // C = PHI(...); |
| 107 | // B = C; |
| 108 | // A = B; |
| 109 | // D = __builtin_expect(A, 0); |
| 110 | // |
| 111 | Value *V = &Arg; |
| 112 | SmallVector<Instruction *, 4> Operations; |
| 113 | while (!isa<PHINode>(V)) { |
| 114 | if (ZExtInst *ZExt = dyn_cast<ZExtInst>(V)) { |
| 115 | V = ZExt->getOperand(0); |
| 116 | Operations.push_back(ZExt); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | if (SExtInst *SExt = dyn_cast<SExtInst>(V)) { |
| 121 | V = SExt->getOperand(0); |
| 122 | Operations.push_back(SExt); |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | BinaryOperator *BinOp = dyn_cast<BinaryOperator>(V); |
| 127 | if (!BinOp || BinOp->getOpcode() != Instruction::Xor) |
| 128 | return; |
| 129 | |
| 130 | ConstantInt *CInt = dyn_cast<ConstantInt>(BinOp->getOperand(1)); |
| 131 | if (!CInt) |
| 132 | return; |
| 133 | |
| 134 | V = BinOp->getOperand(0); |
| 135 | Operations.push_back(BinOp); |
| 136 | } |
| 137 | |
| 138 | // Executes the recorded operations on input 'Value'. |
| 139 | auto ApplyOperations = [&](const APInt &Value) { |
| 140 | APInt Result = Value; |
| 141 | for (auto Op : llvm::reverse(Operations)) { |
| 142 | switch (Op->getOpcode()) { |
| 143 | case Instruction::Xor: |
| 144 | Result ^= cast<ConstantInt>(Op->getOperand(1))->getValue(); |
| 145 | break; |
| 146 | case Instruction::ZExt: |
| 147 | Result = Result.zext(Op->getType()->getIntegerBitWidth()); |
| 148 | break; |
| 149 | case Instruction::SExt: |
| 150 | Result = Result.sext(Op->getType()->getIntegerBitWidth()); |
| 151 | break; |
| 152 | default: |
| 153 | llvm_unreachable("Unexpected operation"); |
| 154 | } |
| 155 | } |
| 156 | return Result; |
| 157 | }; |
| 158 | |
| 159 | auto *PhiDef = dyn_cast<PHINode>(V); |
| 160 | |
| 161 | // Get the first dominating conditional branch of the operand |
| 162 | // i's incoming block. |
| 163 | auto GetDomConditional = [&](unsigned i) -> BranchInst * { |
| 164 | BasicBlock *BB = PhiDef->getIncomingBlock(i); |
| 165 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 166 | if (BI && BI->isConditional()) |
| 167 | return BI; |
| 168 | BB = BB->getSinglePredecessor(); |
| 169 | if (!BB) |
| 170 | return nullptr; |
| 171 | BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 172 | if (!BI || BI->isUnconditional()) |
| 173 | return nullptr; |
| 174 | return BI; |
| 175 | }; |
| 176 | |
| 177 | // Now walk through all Phi operands to find phi oprerands with values |
| 178 | // conflicting with the expected phi output value. Any such operand |
| 179 | // indicates the incoming edge to that operand is unlikely. |
| 180 | for (unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; ++i) { |
| 181 | |
| 182 | Value *PhiOpnd = PhiDef->getIncomingValue(i); |
| 183 | ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd); |
| 184 | if (!CI) |
| 185 | continue; |
| 186 | |
| 187 | // Not an interesting case when IsUnlikely is false -- we can not infer |
| 188 | // anything useful when the operand value matches the expected phi |
| 189 | // output. |
| 190 | if (ExpectedPhiValue == ApplyOperations(CI->getValue())) |
| 191 | continue; |
| 192 | |
| 193 | BranchInst *BI = GetDomConditional(i); |
| 194 | if (!BI) |
| 195 | continue; |
| 196 | |
| 197 | MDBuilder MDB(PhiDef->getContext()); |
| 198 | |
| 199 | // There are two situations in which an operand of the PhiDef comes |
| 200 | // from a given successor of a branch instruction BI. |
| 201 | // 1) When the incoming block of the operand is the successor block; |
| 202 | // 2) When the incoming block is BI's enclosing block and the |
| 203 | // successor is the PhiDef's enclosing block. |
| 204 | // |
| 205 | // Returns true if the operand which comes from OpndIncomingBB |
| 206 | // comes from outgoing edge of BI that leads to Succ block. |
| 207 | auto *OpndIncomingBB = PhiDef->getIncomingBlock(i); |
| 208 | auto IsOpndComingFromSuccessor = [&](BasicBlock *Succ) { |
| 209 | if (OpndIncomingBB == Succ) |
| 210 | // If this successor is the incoming block for this |
| 211 | // Phi operand, then this successor does lead to the Phi. |
| 212 | return true; |
| 213 | if (OpndIncomingBB == BI->getParent() && Succ == PhiDef->getParent()) |
| 214 | // Otherwise, if the edge is directly from the branch |
| 215 | // to the Phi, this successor is the one feeding this |
| 216 | // Phi operand. |
| 217 | return true; |
| 218 | return false; |
| 219 | }; |
| 220 | |
| 221 | if (IsOpndComingFromSuccessor(BI->getSuccessor(1))) |
| 222 | BI->setMetadata( |
| 223 | LLVMContext::MD_prof, |
| 224 | MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight)); |
| 225 | else if (IsOpndComingFromSuccessor(BI->getSuccessor(0))) |
| 226 | BI->setMetadata( |
| 227 | LLVMContext::MD_prof, |
| 228 | MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight)); |
| 229 | } |
| 230 | } |
| 231 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 232 | // Handle both BranchInst and SelectInst. |
| 233 | template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) { |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 234 | |
| 235 | // Handle non-optimized IR code like: |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 236 | // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 237 | // %tobool = icmp ne i64 %expval, 0 |
| 238 | // br i1 %tobool, label %if.then, label %if.end |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 239 | // |
| 240 | // Or the following simpler case: |
| 241 | // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1) |
| 242 | // br i1 %expval, label %if.then, label %if.end |
| 243 | |
| 244 | CallInst *CI; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 245 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 246 | ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition()); |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame] | 247 | CmpInst::Predicate Predicate; |
Xinliang David Li | d6cfba2 | 2017-06-01 23:05:11 +0000 | [diff] [blame] | 248 | ConstantInt *CmpConstOperand = nullptr; |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 249 | if (!CmpI) { |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 250 | CI = dyn_cast<CallInst>(BSI.getCondition()); |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame] | 251 | Predicate = CmpInst::ICMP_NE; |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 252 | } else { |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame] | 253 | Predicate = CmpI->getPredicate(); |
| 254 | if (Predicate != CmpInst::ICMP_NE && Predicate != CmpInst::ICMP_EQ) |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 255 | return false; |
Xinliang David Li | d6cfba2 | 2017-06-01 23:05:11 +0000 | [diff] [blame] | 256 | |
| 257 | CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1)); |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame] | 258 | if (!CmpConstOperand) |
| 259 | return false; |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 260 | CI = dyn_cast<CallInst>(CmpI->getOperand(0)); |
| 261 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 262 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 263 | if (!CI) |
| 264 | return false; |
| 265 | |
Xinliang David Li | d6cfba2 | 2017-06-01 23:05:11 +0000 | [diff] [blame] | 266 | uint64_t ValueComparedTo = 0; |
| 267 | if (CmpConstOperand) { |
| 268 | if (CmpConstOperand->getBitWidth() > 64) |
| 269 | return false; |
| 270 | ValueComparedTo = CmpConstOperand->getZExtValue(); |
| 271 | } |
| 272 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 273 | Function *Fn = CI->getCalledFunction(); |
| 274 | if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) |
| 275 | return false; |
| 276 | |
| 277 | Value *ArgValue = CI->getArgOperand(0); |
| 278 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 279 | if (!ExpectedValue) |
| 280 | return false; |
| 281 | |
Benjamin Kramer | 65e7566 | 2012-05-26 13:59:43 +0000 | [diff] [blame] | 282 | MDBuilder MDB(CI->getContext()); |
| 283 | MDNode *Node; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 284 | |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame] | 285 | if ((ExpectedValue->getZExtValue() == ValueComparedTo) == |
| 286 | (Predicate == CmpInst::ICMP_EQ)) |
Benjamin Kramer | 65e7566 | 2012-05-26 13:59:43 +0000 | [diff] [blame] | 287 | Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight); |
| 288 | else |
| 289 | Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 290 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 291 | BSI.setMetadata(LLVMContext::MD_prof, Node); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 292 | |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 293 | if (CmpI) |
| 294 | CmpI->setOperand(0, ArgValue); |
| 295 | else |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 296 | BSI.setCondition(ArgValue); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 300 | static bool handleBranchExpect(BranchInst &BI) { |
| 301 | if (BI.isUnconditional()) |
| 302 | return false; |
| 303 | |
| 304 | return handleBrSelExpect<BranchInst>(BI); |
| 305 | } |
| 306 | |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 307 | static bool lowerExpectIntrinsic(Function &F) { |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 308 | bool Changed = false; |
| 309 | |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 310 | for (BasicBlock &BB : F) { |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 311 | // Create "block_weights" metadata. |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 312 | if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 313 | if (handleBranchExpect(*BI)) |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 314 | ExpectIntrinsicsHandled++; |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 315 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 316 | if (handleSwitchExpect(*SI)) |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 317 | ExpectIntrinsicsHandled++; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 320 | // Remove llvm.expect intrinsics. Iterate backwards in order |
| 321 | // to process select instructions before the intrinsic gets |
| 322 | // removed. |
| 323 | for (auto BI = BB.rbegin(), BE = BB.rend(); BI != BE;) { |
| 324 | Instruction *Inst = &*BI++; |
| 325 | CallInst *CI = dyn_cast<CallInst>(Inst); |
| 326 | if (!CI) { |
| 327 | if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) { |
| 328 | if (handleBrSelExpect(*SI)) |
| 329 | ExpectIntrinsicsHandled++; |
| 330 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 331 | continue; |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 332 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 333 | |
| 334 | Function *Fn = CI->getCalledFunction(); |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 335 | if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) { |
Xinliang David Li | 621e8dc | 2017-06-02 02:09:31 +0000 | [diff] [blame] | 336 | // Before erasing the llvm.expect, walk backward to find |
| 337 | // phi that define llvm.expect's first arg, and |
| 338 | // infer branch probability: |
| 339 | handlePhiDef(CI); |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 340 | Value *Exp = CI->getArgOperand(0); |
| 341 | CI->replaceAllUsesWith(Exp); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 342 | CI->eraseFromParent(); |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 343 | Changed = true; |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 344 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 348 | return Changed; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 351 | PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F, |
| 352 | FunctionAnalysisManager &) { |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 353 | if (lowerExpectIntrinsic(F)) |
| 354 | return PreservedAnalyses::none(); |
| 355 | |
| 356 | return PreservedAnalyses::all(); |
| 357 | } |
| 358 | |
| 359 | namespace { |
| 360 | /// \brief Legacy pass for lowering expect intrinsics out of the IR. |
| 361 | /// |
| 362 | /// When this pass is run over a function it uses expect intrinsics which feed |
| 363 | /// branches and switches to provide branch weight metadata for those |
| 364 | /// terminators. It then removes the expect intrinsics from the IR so the rest |
| 365 | /// of the optimizer can ignore them. |
| 366 | class LowerExpectIntrinsic : public FunctionPass { |
| 367 | public: |
| 368 | static char ID; |
| 369 | LowerExpectIntrinsic() : FunctionPass(ID) { |
| 370 | initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry()); |
| 371 | } |
| 372 | |
| 373 | bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); } |
| 374 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 375 | } |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 376 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 377 | char LowerExpectIntrinsic::ID = 0; |
Chandler Carruth | 0ea746b | 2015-01-24 10:30:14 +0000 | [diff] [blame] | 378 | INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", |
| 379 | "Lower 'expect' Intrinsics", false, false) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 380 | |
| 381 | FunctionPass *llvm::createLowerExpectIntrinsicPass() { |
| 382 | return new LowerExpectIntrinsic(); |
| 383 | } |