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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/BasicBlock.h" |
| 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/Intrinsics.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/MDBuilder.h" |
| 24 | #include "llvm/IR/Metadata.h" |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Scalar.h" |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "lower-expect-intrinsic" |
| 33 | |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 34 | STATISTIC(ExpectIntrinsicsHandled, |
| 35 | "Number of 'expect' intrinsic instructions handled"); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 36 | |
Sanjay Patel | d2d2aa5 | 2016-04-26 22:23:38 +0000 | [diff] [blame] | 37 | // These default values are chosen to represent an extremely skewed outcome for |
| 38 | // a condition, but they leave some room for interpretation by later passes. |
| 39 | // |
| 40 | // If the documentation for __builtin_expect() was made explicit that it should |
| 41 | // only be used in extreme cases, we could make this ratio higher. As it stands, |
| 42 | // programmers may be using __builtin_expect() / llvm.expect to annotate that a |
| 43 | // branch is likely or unlikely to be taken. |
| 44 | // |
| 45 | // There is a known dependency on this ratio in CodeGenPrepare when transforming |
| 46 | // 'select' instructions. It may be worthwhile to hoist these values to some |
| 47 | // shared space, so they can be used directly by other passes. |
| 48 | |
| 49 | static cl::opt<uint32_t> LikelyBranchWeight( |
| 50 | "likely-branch-weight", cl::Hidden, cl::init(2000), |
| 51 | cl::desc("Weight of the branch likely to be taken (default = 2000)")); |
| 52 | static cl::opt<uint32_t> UnlikelyBranchWeight( |
| 53 | "unlikely-branch-weight", cl::Hidden, cl::init(1), |
| 54 | cl::desc("Weight of the branch unlikely to be taken (default = 1)")); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 55 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 56 | static bool handleSwitchExpect(SwitchInst &SI) { |
| 57 | CallInst *CI = dyn_cast<CallInst>(SI.getCondition()); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 58 | if (!CI) |
| 59 | return false; |
| 60 | |
| 61 | Function *Fn = CI->getCalledFunction(); |
| 62 | if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) |
| 63 | return false; |
| 64 | |
| 65 | Value *ArgValue = CI->getArgOperand(0); |
| 66 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 67 | if (!ExpectedValue) |
| 68 | return false; |
| 69 | |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 70 | SwitchInst::CaseHandle Case = *SI.findCaseValue(ExpectedValue); |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 71 | unsigned n = SI.getNumCases(); // +1 for default case. |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 72 | SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 73 | |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 74 | if (Case == *SI.case_default()) |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 75 | Weights[0] = LikelyBranchWeight; |
| 76 | else |
| 77 | Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 78 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 79 | SI.setMetadata(LLVMContext::MD_prof, |
| 80 | MDBuilder(CI->getContext()).createBranchWeights(Weights)); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 81 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 82 | SI.setCondition(ArgValue); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 86 | // Handle both BranchInst and SelectInst. |
| 87 | template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) { |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 88 | |
| 89 | // Handle non-optimized IR code like: |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 90 | // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 91 | // %tobool = icmp ne i64 %expval, 0 |
| 92 | // br i1 %tobool, label %if.then, label %if.end |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 93 | // |
| 94 | // Or the following simpler case: |
| 95 | // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1) |
| 96 | // br i1 %expval, label %if.then, label %if.end |
| 97 | |
| 98 | CallInst *CI; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 99 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 100 | ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition()); |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame^] | 101 | CmpInst::Predicate Predicate; |
| 102 | uint64_t ValueComparedTo = 0; |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 103 | if (!CmpI) { |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 104 | CI = dyn_cast<CallInst>(BSI.getCondition()); |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame^] | 105 | Predicate = CmpInst::ICMP_NE; |
| 106 | ValueComparedTo = 0; |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 107 | } else { |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame^] | 108 | Predicate = CmpI->getPredicate(); |
| 109 | if (Predicate != CmpInst::ICMP_NE && Predicate != CmpInst::ICMP_EQ) |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 110 | return false; |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame^] | 111 | ConstantInt *CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1)); |
| 112 | if (!CmpConstOperand) |
| 113 | return false; |
| 114 | ValueComparedTo = CmpConstOperand->getZExtValue(); |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 115 | CI = dyn_cast<CallInst>(CmpI->getOperand(0)); |
| 116 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 117 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 118 | if (!CI) |
| 119 | return false; |
| 120 | |
| 121 | Function *Fn = CI->getCalledFunction(); |
| 122 | if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) |
| 123 | return false; |
| 124 | |
| 125 | Value *ArgValue = CI->getArgOperand(0); |
| 126 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 127 | if (!ExpectedValue) |
| 128 | return false; |
| 129 | |
Benjamin Kramer | 65e7566 | 2012-05-26 13:59:43 +0000 | [diff] [blame] | 130 | MDBuilder MDB(CI->getContext()); |
| 131 | MDNode *Node; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 132 | |
Xinliang David Li | ee8d6ac | 2017-06-01 19:05:55 +0000 | [diff] [blame^] | 133 | if ((ExpectedValue->getZExtValue() == ValueComparedTo) == |
| 134 | (Predicate == CmpInst::ICMP_EQ)) |
Benjamin Kramer | 65e7566 | 2012-05-26 13:59:43 +0000 | [diff] [blame] | 135 | Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight); |
| 136 | else |
| 137 | Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 138 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 139 | BSI.setMetadata(LLVMContext::MD_prof, Node); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 140 | |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 141 | if (CmpI) |
| 142 | CmpI->setOperand(0, ArgValue); |
| 143 | else |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 144 | BSI.setCondition(ArgValue); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 148 | static bool handleBranchExpect(BranchInst &BI) { |
| 149 | if (BI.isUnconditional()) |
| 150 | return false; |
| 151 | |
| 152 | return handleBrSelExpect<BranchInst>(BI); |
| 153 | } |
| 154 | |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 155 | static bool lowerExpectIntrinsic(Function &F) { |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 156 | bool Changed = false; |
| 157 | |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 158 | for (BasicBlock &BB : F) { |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 159 | // Create "block_weights" metadata. |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 160 | if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 161 | if (handleBranchExpect(*BI)) |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 162 | ExpectIntrinsicsHandled++; |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 163 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 164 | if (handleSwitchExpect(*SI)) |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 165 | ExpectIntrinsicsHandled++; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 168 | // Remove llvm.expect intrinsics. Iterate backwards in order |
| 169 | // to process select instructions before the intrinsic gets |
| 170 | // removed. |
| 171 | for (auto BI = BB.rbegin(), BE = BB.rend(); BI != BE;) { |
| 172 | Instruction *Inst = &*BI++; |
| 173 | CallInst *CI = dyn_cast<CallInst>(Inst); |
| 174 | if (!CI) { |
| 175 | if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) { |
| 176 | if (handleBrSelExpect(*SI)) |
| 177 | ExpectIntrinsicsHandled++; |
| 178 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 179 | continue; |
Xinliang David Li | 40afd5c | 2016-09-02 22:03:40 +0000 | [diff] [blame] | 180 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 181 | |
| 182 | Function *Fn = CI->getCalledFunction(); |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 183 | if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) { |
| 184 | Value *Exp = CI->getArgOperand(0); |
| 185 | CI->replaceAllUsesWith(Exp); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 186 | CI->eraseFromParent(); |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 187 | Changed = true; |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 188 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 192 | return Changed; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 195 | PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F, |
| 196 | FunctionAnalysisManager &) { |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 197 | if (lowerExpectIntrinsic(F)) |
| 198 | return PreservedAnalyses::none(); |
| 199 | |
| 200 | return PreservedAnalyses::all(); |
| 201 | } |
| 202 | |
| 203 | namespace { |
| 204 | /// \brief Legacy pass for lowering expect intrinsics out of the IR. |
| 205 | /// |
| 206 | /// When this pass is run over a function it uses expect intrinsics which feed |
| 207 | /// branches and switches to provide branch weight metadata for those |
| 208 | /// terminators. It then removes the expect intrinsics from the IR so the rest |
| 209 | /// of the optimizer can ignore them. |
| 210 | class LowerExpectIntrinsic : public FunctionPass { |
| 211 | public: |
| 212 | static char ID; |
| 213 | LowerExpectIntrinsic() : FunctionPass(ID) { |
| 214 | initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry()); |
| 215 | } |
| 216 | |
| 217 | bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); } |
| 218 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 219 | } |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 220 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 221 | char LowerExpectIntrinsic::ID = 0; |
Chandler Carruth | 0ea746b | 2015-01-24 10:30:14 +0000 | [diff] [blame] | 222 | INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", |
| 223 | "Lower 'expect' Intrinsics", false, false) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 224 | |
| 225 | FunctionPass *llvm::createLowerExpectIntrinsicPass() { |
| 226 | return new LowerExpectIntrinsic(); |
| 227 | } |