172793727SChandler Carruth //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===// 272793727SChandler Carruth // 372793727SChandler Carruth // The LLVM Compiler Infrastructure 472793727SChandler Carruth // 572793727SChandler Carruth // This file is distributed under the University of Illinois Open Source 672793727SChandler Carruth // License. See LICENSE.TXT for details. 772793727SChandler Carruth // 872793727SChandler Carruth //===----------------------------------------------------------------------===// 972793727SChandler Carruth // 1072793727SChandler Carruth // This pass lowers the 'expect' intrinsic to LLVM metadata. 1172793727SChandler Carruth // 1272793727SChandler Carruth //===----------------------------------------------------------------------===// 1372793727SChandler Carruth 14*43e590e5SChandler Carruth #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" 153f5e7b1fSChandler Carruth #include "llvm/ADT/SmallVector.h" 1672793727SChandler Carruth #include "llvm/ADT/Statistic.h" 1772793727SChandler Carruth #include "llvm/IR/BasicBlock.h" 1872793727SChandler Carruth #include "llvm/IR/Constants.h" 1972793727SChandler Carruth #include "llvm/IR/Function.h" 2072793727SChandler Carruth #include "llvm/IR/Instructions.h" 2172793727SChandler Carruth #include "llvm/IR/Intrinsics.h" 2272793727SChandler Carruth #include "llvm/IR/LLVMContext.h" 2372793727SChandler Carruth #include "llvm/IR/MDBuilder.h" 2472793727SChandler Carruth #include "llvm/IR/Metadata.h" 2572793727SChandler Carruth #include "llvm/Pass.h" 2672793727SChandler Carruth #include "llvm/Support/CommandLine.h" 2772793727SChandler Carruth #include "llvm/Support/Debug.h" 28*43e590e5SChandler Carruth #include "llvm/Transforms/Scalar.h" 2972793727SChandler Carruth 3072793727SChandler Carruth using namespace llvm; 3172793727SChandler Carruth 3272793727SChandler Carruth #define DEBUG_TYPE "lower-expect-intrinsic" 3372793727SChandler Carruth 346eb60eb5SChandler Carruth STATISTIC(ExpectIntrinsicsHandled, 356eb60eb5SChandler Carruth "Number of 'expect' intrinsic instructions handled"); 3672793727SChandler Carruth 3772793727SChandler Carruth static cl::opt<uint32_t> 3872793727SChandler Carruth LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64), 3972793727SChandler Carruth cl::desc("Weight of the branch likely to be taken (default = 64)")); 4072793727SChandler Carruth static cl::opt<uint32_t> 4172793727SChandler Carruth UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4), 4272793727SChandler Carruth cl::desc("Weight of the branch unlikely to be taken (default = 4)")); 4372793727SChandler Carruth 440012c778SChandler Carruth static bool handleSwitchExpect(SwitchInst &SI) { 450012c778SChandler Carruth CallInst *CI = dyn_cast<CallInst>(SI.getCondition()); 4672793727SChandler Carruth if (!CI) 4772793727SChandler Carruth return false; 4872793727SChandler Carruth 4972793727SChandler Carruth Function *Fn = CI->getCalledFunction(); 5072793727SChandler Carruth if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) 5172793727SChandler Carruth return false; 5272793727SChandler Carruth 5372793727SChandler Carruth Value *ArgValue = CI->getArgOperand(0); 5472793727SChandler Carruth ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 5572793727SChandler Carruth if (!ExpectedValue) 5672793727SChandler Carruth return false; 5772793727SChandler Carruth 580012c778SChandler Carruth SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue); 590012c778SChandler Carruth unsigned n = SI.getNumCases(); // +1 for default case. 603f5e7b1fSChandler Carruth SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight); 6172793727SChandler Carruth 623f5e7b1fSChandler Carruth if (Case == SI.case_default()) 633f5e7b1fSChandler Carruth Weights[0] = LikelyBranchWeight; 643f5e7b1fSChandler Carruth else 653f5e7b1fSChandler Carruth Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight; 6672793727SChandler Carruth 670012c778SChandler Carruth SI.setMetadata(LLVMContext::MD_prof, 6872793727SChandler Carruth MDBuilder(CI->getContext()).createBranchWeights(Weights)); 6972793727SChandler Carruth 700012c778SChandler Carruth SI.setCondition(ArgValue); 7172793727SChandler Carruth return true; 7272793727SChandler Carruth } 7372793727SChandler Carruth 740012c778SChandler Carruth static bool handleBranchExpect(BranchInst &BI) { 750012c778SChandler Carruth if (BI.isUnconditional()) 7672793727SChandler Carruth return false; 7772793727SChandler Carruth 7872793727SChandler Carruth // Handle non-optimized IR code like: 7972793727SChandler Carruth // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1) 8072793727SChandler Carruth // %tobool = icmp ne i64 %expval, 0 8172793727SChandler Carruth // br i1 %tobool, label %if.then, label %if.end 8272793727SChandler Carruth // 8372793727SChandler Carruth // Or the following simpler case: 8472793727SChandler Carruth // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1) 8572793727SChandler Carruth // br i1 %expval, label %if.then, label %if.end 8672793727SChandler Carruth 8772793727SChandler Carruth CallInst *CI; 8872793727SChandler Carruth 890012c778SChandler Carruth ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition()); 9072793727SChandler Carruth if (!CmpI) { 910012c778SChandler Carruth CI = dyn_cast<CallInst>(BI.getCondition()); 9272793727SChandler Carruth } else { 9372793727SChandler Carruth if (CmpI->getPredicate() != CmpInst::ICMP_NE) 9472793727SChandler Carruth return false; 9572793727SChandler Carruth CI = dyn_cast<CallInst>(CmpI->getOperand(0)); 9672793727SChandler Carruth } 9772793727SChandler Carruth 9872793727SChandler Carruth if (!CI) 9972793727SChandler Carruth return false; 10072793727SChandler Carruth 10172793727SChandler Carruth Function *Fn = CI->getCalledFunction(); 10272793727SChandler Carruth if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect) 10372793727SChandler Carruth return false; 10472793727SChandler Carruth 10572793727SChandler Carruth Value *ArgValue = CI->getArgOperand(0); 10672793727SChandler Carruth ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 10772793727SChandler Carruth if (!ExpectedValue) 10872793727SChandler Carruth return false; 10972793727SChandler Carruth 11072793727SChandler Carruth MDBuilder MDB(CI->getContext()); 11172793727SChandler Carruth MDNode *Node; 11272793727SChandler Carruth 11372793727SChandler Carruth // If expect value is equal to 1 it means that we are more likely to take 11472793727SChandler Carruth // branch 0, in other case more likely is branch 1. 11572793727SChandler Carruth if (ExpectedValue->isOne()) 11672793727SChandler Carruth Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight); 11772793727SChandler Carruth else 11872793727SChandler Carruth Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight); 11972793727SChandler Carruth 1200012c778SChandler Carruth BI.setMetadata(LLVMContext::MD_prof, Node); 12172793727SChandler Carruth 12272793727SChandler Carruth if (CmpI) 12372793727SChandler Carruth CmpI->setOperand(0, ArgValue); 12472793727SChandler Carruth else 1250012c778SChandler Carruth BI.setCondition(ArgValue); 12672793727SChandler Carruth return true; 12772793727SChandler Carruth } 12872793727SChandler Carruth 129*43e590e5SChandler Carruth static bool lowerExpectIntrinsic(Function &F) { 130c3bf5bd8SChandler Carruth bool Changed = false; 131c3bf5bd8SChandler Carruth 132d12741e0SChandler Carruth for (BasicBlock &BB : F) { 13372793727SChandler Carruth // Create "block_weights" metadata. 134d12741e0SChandler Carruth if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) { 1350012c778SChandler Carruth if (handleBranchExpect(*BI)) 1366eb60eb5SChandler Carruth ExpectIntrinsicsHandled++; 137d12741e0SChandler Carruth } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) { 1380012c778SChandler Carruth if (handleSwitchExpect(*SI)) 1396eb60eb5SChandler Carruth ExpectIntrinsicsHandled++; 14072793727SChandler Carruth } 14172793727SChandler Carruth 14272793727SChandler Carruth // remove llvm.expect intrinsics. 143d12741e0SChandler Carruth for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { 14472793727SChandler Carruth CallInst *CI = dyn_cast<CallInst>(BI++); 14572793727SChandler Carruth if (!CI) 14672793727SChandler Carruth continue; 14772793727SChandler Carruth 14872793727SChandler Carruth Function *Fn = CI->getCalledFunction(); 14972793727SChandler Carruth if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) { 15072793727SChandler Carruth Value *Exp = CI->getArgOperand(0); 15172793727SChandler Carruth CI->replaceAllUsesWith(Exp); 15272793727SChandler Carruth CI->eraseFromParent(); 153c3bf5bd8SChandler Carruth Changed = true; 15472793727SChandler Carruth } 15572793727SChandler Carruth } 15672793727SChandler Carruth } 15772793727SChandler Carruth 158c3bf5bd8SChandler Carruth return Changed; 15972793727SChandler Carruth } 16072793727SChandler Carruth 161*43e590e5SChandler Carruth PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F) { 162*43e590e5SChandler Carruth if (lowerExpectIntrinsic(F)) 163*43e590e5SChandler Carruth return PreservedAnalyses::none(); 164*43e590e5SChandler Carruth 165*43e590e5SChandler Carruth return PreservedAnalyses::all(); 166*43e590e5SChandler Carruth } 167*43e590e5SChandler Carruth 168*43e590e5SChandler Carruth namespace { 169*43e590e5SChandler Carruth /// \brief Legacy pass for lowering expect intrinsics out of the IR. 170*43e590e5SChandler Carruth /// 171*43e590e5SChandler Carruth /// When this pass is run over a function it uses expect intrinsics which feed 172*43e590e5SChandler Carruth /// branches and switches to provide branch weight metadata for those 173*43e590e5SChandler Carruth /// terminators. It then removes the expect intrinsics from the IR so the rest 174*43e590e5SChandler Carruth /// of the optimizer can ignore them. 175*43e590e5SChandler Carruth class LowerExpectIntrinsic : public FunctionPass { 176*43e590e5SChandler Carruth public: 177*43e590e5SChandler Carruth static char ID; 178*43e590e5SChandler Carruth LowerExpectIntrinsic() : FunctionPass(ID) { 179*43e590e5SChandler Carruth initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry()); 180*43e590e5SChandler Carruth } 181*43e590e5SChandler Carruth 182*43e590e5SChandler Carruth bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); } 183*43e590e5SChandler Carruth }; 184*43e590e5SChandler Carruth } 185*43e590e5SChandler Carruth 18672793727SChandler Carruth char LowerExpectIntrinsic::ID = 0; 1870ea746bfSChandler Carruth INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", 1880ea746bfSChandler Carruth "Lower 'expect' Intrinsics", false, false) 18972793727SChandler Carruth 19072793727SChandler Carruth FunctionPass *llvm::createLowerExpectIntrinsicPass() { 19172793727SChandler Carruth return new LowerExpectIntrinsic(); 19272793727SChandler Carruth } 193