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 | |
| 37 | static cl::opt<uint32_t> |
| 38 | LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64), |
| 39 | cl::desc("Weight of the branch likely to be taken (default = 64)")); |
| 40 | static cl::opt<uint32_t> |
| 41 | UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4), |
| 42 | cl::desc("Weight of the branch unlikely to be taken (default = 4)")); |
| 43 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 44 | static bool handleSwitchExpect(SwitchInst &SI) { |
| 45 | CallInst *CI = dyn_cast<CallInst>(SI.getCondition()); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 46 | if (!CI) |
| 47 | return false; |
| 48 | |
| 49 | Function *Fn = CI->getCalledFunction(); |
| 50 | if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) |
| 51 | return false; |
| 52 | |
| 53 | Value *ArgValue = CI->getArgOperand(0); |
| 54 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 55 | if (!ExpectedValue) |
| 56 | return false; |
| 57 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 58 | SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue); |
| 59 | unsigned n = SI.getNumCases(); // +1 for default case. |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 60 | SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | 3f5e7b1 | 2015-01-24 10:47:13 +0000 | [diff] [blame] | 62 | if (Case == SI.case_default()) |
| 63 | Weights[0] = LikelyBranchWeight; |
| 64 | else |
| 65 | Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 66 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 67 | SI.setMetadata(LLVMContext::MD_prof, |
| 68 | MDBuilder(CI->getContext()).createBranchWeights(Weights)); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 69 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 70 | SI.setCondition(ArgValue); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 74 | static bool handleBranchExpect(BranchInst &BI) { |
| 75 | if (BI.isUnconditional()) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 76 | return false; |
| 77 | |
| 78 | // Handle non-optimized IR code like: |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 79 | // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 80 | // %tobool = icmp ne i64 %expval, 0 |
| 81 | // br i1 %tobool, label %if.then, label %if.end |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 82 | // |
| 83 | // Or the following simpler case: |
| 84 | // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1) |
| 85 | // br i1 %expval, label %if.then, label %if.end |
| 86 | |
| 87 | CallInst *CI; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 88 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 89 | ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition()); |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 90 | if (!CmpI) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 91 | CI = dyn_cast<CallInst>(BI.getCondition()); |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 92 | } else { |
| 93 | if (CmpI->getPredicate() != CmpInst::ICMP_NE) |
| 94 | return false; |
| 95 | CI = dyn_cast<CallInst>(CmpI->getOperand(0)); |
| 96 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 97 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 98 | if (!CI) |
| 99 | return false; |
| 100 | |
| 101 | Function *Fn = CI->getCalledFunction(); |
| 102 | if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) |
| 103 | return false; |
| 104 | |
| 105 | Value *ArgValue = CI->getArgOperand(0); |
| 106 | ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 107 | if (!ExpectedValue) |
| 108 | return false; |
| 109 | |
Benjamin Kramer | 65e7566 | 2012-05-26 13:59:43 +0000 | [diff] [blame] | 110 | MDBuilder MDB(CI->getContext()); |
| 111 | MDNode *Node; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 112 | |
| 113 | // If expect value is equal to 1 it means that we are more likely to take |
| 114 | // branch 0, in other case more likely is branch 1. |
Benjamin Kramer | 65e7566 | 2012-05-26 13:59:43 +0000 | [diff] [blame] | 115 | if (ExpectedValue->isOne()) |
| 116 | Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight); |
| 117 | else |
| 118 | Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 119 | |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 120 | BI.setMetadata(LLVMContext::MD_prof, Node); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 121 | |
Duncan P. N. Exon Smith | 1ff08e3 | 2014-02-02 22:43:55 +0000 | [diff] [blame] | 122 | if (CmpI) |
| 123 | CmpI->setOperand(0, ArgValue); |
| 124 | else |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 125 | BI.setCondition(ArgValue); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 129 | static bool lowerExpectIntrinsic(Function &F) { |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 130 | bool Changed = false; |
| 131 | |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 132 | for (BasicBlock &BB : F) { |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 133 | // Create "block_weights" metadata. |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 134 | if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 135 | if (handleBranchExpect(*BI)) |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 136 | ExpectIntrinsicsHandled++; |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 137 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) { |
Chandler Carruth | 0012c77 | 2015-01-24 10:39:24 +0000 | [diff] [blame] | 138 | if (handleSwitchExpect(*SI)) |
Chandler Carruth | 6eb60eb | 2015-01-24 10:57:25 +0000 | [diff] [blame] | 139 | ExpectIntrinsicsHandled++; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Sanjay Patel | 6b2765f | 2015-08-24 20:11:14 +0000 | [diff] [blame] | 142 | // Remove llvm.expect intrinsics. |
Chandler Carruth | d12741e | 2015-01-24 10:57:19 +0000 | [diff] [blame] | 143 | for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 144 | CallInst *CI = dyn_cast<CallInst>(BI++); |
| 145 | if (!CI) |
| 146 | continue; |
| 147 | |
| 148 | Function *Fn = CI->getCalledFunction(); |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 149 | if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) { |
| 150 | Value *Exp = CI->getArgOperand(0); |
| 151 | CI->replaceAllUsesWith(Exp); |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 152 | CI->eraseFromParent(); |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 153 | Changed = true; |
Jakub Staszak | a11f7ec | 2011-07-06 23:50:16 +0000 | [diff] [blame] | 154 | } |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Chandler Carruth | c3bf5bd | 2015-01-24 11:12:57 +0000 | [diff] [blame] | 158 | return Changed; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 161 | PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F) { |
| 162 | if (lowerExpectIntrinsic(F)) |
| 163 | return PreservedAnalyses::none(); |
| 164 | |
| 165 | return PreservedAnalyses::all(); |
| 166 | } |
| 167 | |
| 168 | namespace { |
| 169 | /// \brief Legacy pass for lowering expect intrinsics out of the IR. |
| 170 | /// |
| 171 | /// When this pass is run over a function it uses expect intrinsics which feed |
| 172 | /// branches and switches to provide branch weight metadata for those |
| 173 | /// terminators. It then removes the expect intrinsics from the IR so the rest |
| 174 | /// of the optimizer can ignore them. |
| 175 | class LowerExpectIntrinsic : public FunctionPass { |
| 176 | public: |
| 177 | static char ID; |
| 178 | LowerExpectIntrinsic() : FunctionPass(ID) { |
| 179 | initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry()); |
| 180 | } |
| 181 | |
| 182 | bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); } |
| 183 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 184 | } |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 185 | |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 186 | char LowerExpectIntrinsic::ID = 0; |
Chandler Carruth | 0ea746b | 2015-01-24 10:30:14 +0000 | [diff] [blame] | 187 | INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", |
| 188 | "Lower 'expect' Intrinsics", false, false) |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 189 | |
| 190 | FunctionPass *llvm::createLowerExpectIntrinsicPass() { |
| 191 | return new LowerExpectIntrinsic(); |
| 192 | } |