1 //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass lowers the 'expect' intrinsic to LLVM metadata.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/iterator_range.h"
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"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Transforms/Scalar.h"
33 #define DEBUG_TYPE "lower-expect-intrinsic"
35 STATISTIC(ExpectIntrinsicsHandled,
36 "Number of 'expect' intrinsic instructions handled");
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.
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.
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.
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)"));
57 static bool handleSwitchExpect(SwitchInst &SI) {
58 CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
62 Function *Fn = CI->getCalledFunction();
63 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
66 Value *ArgValue = CI->getArgOperand(0);
67 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
71 SwitchInst::CaseHandle Case = *SI.findCaseValue(ExpectedValue);
72 unsigned n = SI.getNumCases(); // +1 for default case.
73 SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
75 if (Case == *SI.case_default())
76 Weights[0] = LikelyBranchWeight;
78 Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
80 SI.setMetadata(LLVMContext::MD_prof,
81 MDBuilder(CI->getContext()).createBranchWeights(Weights));
83 SI.setCondition(ArgValue);
87 /// Handler for PHINodes that define the value argument to an
88 /// @llvm.expect call.
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);
96 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(Expect->getArgOperand(1));
99 const APInt &ExpectedPhiValue = ExpectedValue->getValue();
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.
109 // D = __builtin_expect(A, 0);
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);
120 if (SExtInst *SExt = dyn_cast<SExtInst>(V)) {
121 V = SExt->getOperand(0);
122 Operations.push_back(SExt);
126 BinaryOperator *BinOp = dyn_cast<BinaryOperator>(V);
127 if (!BinOp || BinOp->getOpcode() != Instruction::Xor)
130 ConstantInt *CInt = dyn_cast<ConstantInt>(BinOp->getOperand(1));
134 V = BinOp->getOperand(0);
135 Operations.push_back(BinOp);
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();
146 case Instruction::ZExt:
147 Result = Result.zext(Op->getType()->getIntegerBitWidth());
149 case Instruction::SExt:
150 Result = Result.sext(Op->getType()->getIntegerBitWidth());
153 llvm_unreachable("Unexpected operation");
159 auto *PhiDef = dyn_cast<PHINode>(V);
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())
168 BB = BB->getSinglePredecessor();
171 BI = dyn_cast<BranchInst>(BB->getTerminator());
172 if (!BI || BI->isUnconditional())
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) {
182 Value *PhiOpnd = PhiDef->getIncomingValue(i);
183 ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd);
187 // Not an interesting case when IsUnlikely is false -- we can not infer
188 // anything useful when the operand value matches the expected phi
190 if (ExpectedPhiValue == ApplyOperations(CI->getValue()))
193 BranchInst *BI = GetDomConditional(i);
197 MDBuilder MDB(PhiDef->getContext());
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.
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.
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
221 if (IsOpndComingFromSuccessor(BI->getSuccessor(1)))
223 LLVMContext::MD_prof,
224 MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight));
225 else if (IsOpndComingFromSuccessor(BI->getSuccessor(0)))
227 LLVMContext::MD_prof,
228 MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight));
232 // Handle both BranchInst and SelectInst.
233 template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) {
235 // Handle non-optimized IR code like:
236 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
237 // %tobool = icmp ne i64 %expval, 0
238 // br i1 %tobool, label %if.then, label %if.end
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
246 ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition());
247 CmpInst::Predicate Predicate;
248 ConstantInt *CmpConstOperand = nullptr;
250 CI = dyn_cast<CallInst>(BSI.getCondition());
251 Predicate = CmpInst::ICMP_NE;
253 Predicate = CmpI->getPredicate();
254 if (Predicate != CmpInst::ICMP_NE && Predicate != CmpInst::ICMP_EQ)
257 CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1));
258 if (!CmpConstOperand)
260 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
266 uint64_t ValueComparedTo = 0;
267 if (CmpConstOperand) {
268 if (CmpConstOperand->getBitWidth() > 64)
270 ValueComparedTo = CmpConstOperand->getZExtValue();
273 Function *Fn = CI->getCalledFunction();
274 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
277 Value *ArgValue = CI->getArgOperand(0);
278 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
282 MDBuilder MDB(CI->getContext());
285 if ((ExpectedValue->getZExtValue() == ValueComparedTo) ==
286 (Predicate == CmpInst::ICMP_EQ))
287 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
289 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
291 BSI.setMetadata(LLVMContext::MD_prof, Node);
294 CmpI->setOperand(0, ArgValue);
296 BSI.setCondition(ArgValue);
300 static bool handleBranchExpect(BranchInst &BI) {
301 if (BI.isUnconditional())
304 return handleBrSelExpect<BranchInst>(BI);
307 static bool lowerExpectIntrinsic(Function &F) {
308 bool Changed = false;
310 for (BasicBlock &BB : F) {
311 // Create "block_weights" metadata.
312 if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
313 if (handleBranchExpect(*BI))
314 ExpectIntrinsicsHandled++;
315 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
316 if (handleSwitchExpect(*SI))
317 ExpectIntrinsicsHandled++;
320 // Remove llvm.expect intrinsics. Iterate backwards in order
321 // to process select instructions before the intrinsic gets
323 for (auto BI = BB.rbegin(), BE = BB.rend(); BI != BE;) {
324 Instruction *Inst = &*BI++;
325 CallInst *CI = dyn_cast<CallInst>(Inst);
327 if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) {
328 if (handleBrSelExpect(*SI))
329 ExpectIntrinsicsHandled++;
334 Function *Fn = CI->getCalledFunction();
335 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
336 // Before erasing the llvm.expect, walk backward to find
337 // phi that define llvm.expect's first arg, and
338 // infer branch probability:
340 Value *Exp = CI->getArgOperand(0);
341 CI->replaceAllUsesWith(Exp);
342 CI->eraseFromParent();
351 PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F,
352 FunctionAnalysisManager &) {
353 if (lowerExpectIntrinsic(F))
354 return PreservedAnalyses::none();
356 return PreservedAnalyses::all();
360 /// Legacy pass for lowering expect intrinsics out of the IR.
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 {
369 LowerExpectIntrinsic() : FunctionPass(ID) {
370 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
373 bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
377 char LowerExpectIntrinsic::ID = 0;
378 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
379 "Lower 'expect' Intrinsics", false, false)
381 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
382 return new LowerExpectIntrinsic();