2011-10-17 06:15:07 +08:00
|
|
|
//===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass lowers the 'expect' intrinsic to LLVM metadata.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-24 19:13:02 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
|
2015-01-24 18:47:13 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/MDBuilder.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2011-07-07 02:22:43 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-01-24 19:13:02 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2011-07-07 02:22:43 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "lower-expect-intrinsic"
|
|
|
|
|
2015-01-24 18:57:25 +08:00
|
|
|
STATISTIC(ExpectIntrinsicsHandled,
|
|
|
|
"Number of 'expect' intrinsic instructions handled");
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2016-04-27 06:23:38 +08:00
|
|
|
// These default values are chosen to represent an extremely skewed outcome for
|
|
|
|
// a condition, but they leave some room for interpretation by later passes.
|
|
|
|
//
|
|
|
|
// If the documentation for __builtin_expect() was made explicit that it should
|
|
|
|
// only be used in extreme cases, we could make this ratio higher. As it stands,
|
|
|
|
// programmers may be using __builtin_expect() / llvm.expect to annotate that a
|
|
|
|
// branch is likely or unlikely to be taken.
|
|
|
|
//
|
|
|
|
// There is a known dependency on this ratio in CodeGenPrepare when transforming
|
|
|
|
// 'select' instructions. It may be worthwhile to hoist these values to some
|
|
|
|
// shared space, so they can be used directly by other passes.
|
|
|
|
|
|
|
|
static cl::opt<uint32_t> LikelyBranchWeight(
|
|
|
|
"likely-branch-weight", cl::Hidden, cl::init(2000),
|
|
|
|
cl::desc("Weight of the branch likely to be taken (default = 2000)"));
|
|
|
|
static cl::opt<uint32_t> UnlikelyBranchWeight(
|
|
|
|
"unlikely-branch-weight", cl::Hidden, cl::init(1),
|
|
|
|
cl::desc("Weight of the branch unlikely to be taken (default = 1)"));
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
static bool handleSwitchExpect(SwitchInst &SI) {
|
|
|
|
CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
|
2011-07-07 02:22:43 +08:00
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Function *Fn = CI->getCalledFunction();
|
|
|
|
if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Value *ArgValue = CI->getArgOperand(0);
|
|
|
|
ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
|
|
|
|
if (!ExpectedValue)
|
|
|
|
return false;
|
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
|
|
|
|
unsigned n = SI.getNumCases(); // +1 for default case.
|
2015-01-24 18:47:13 +08:00
|
|
|
SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2015-01-24 18:47:13 +08:00
|
|
|
if (Case == SI.case_default())
|
|
|
|
Weights[0] = LikelyBranchWeight;
|
|
|
|
else
|
|
|
|
Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
SI.setMetadata(LLVMContext::MD_prof,
|
|
|
|
MDBuilder(CI->getContext()).createBranchWeights(Weights));
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
SI.setCondition(ArgValue);
|
2011-07-07 02:22:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
static bool handleBranchExpect(BranchInst &BI) {
|
|
|
|
if (BI.isUnconditional())
|
2011-07-07 02:22:43 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Handle non-optimized IR code like:
|
2014-02-03 06:43:55 +08:00
|
|
|
// %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
|
2011-07-07 02:22:43 +08:00
|
|
|
// %tobool = icmp ne i64 %expval, 0
|
|
|
|
// br i1 %tobool, label %if.then, label %if.end
|
2014-02-03 06:43:55 +08:00
|
|
|
//
|
|
|
|
// Or the following simpler case:
|
|
|
|
// %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
|
|
|
|
// br i1 %expval, label %if.then, label %if.end
|
|
|
|
|
|
|
|
CallInst *CI;
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition());
|
2014-02-03 06:43:55 +08:00
|
|
|
if (!CmpI) {
|
2015-01-24 18:39:24 +08:00
|
|
|
CI = dyn_cast<CallInst>(BI.getCondition());
|
2014-02-03 06:43:55 +08:00
|
|
|
} else {
|
|
|
|
if (CmpI->getPredicate() != CmpInst::ICMP_NE)
|
|
|
|
return false;
|
|
|
|
CI = dyn_cast<CallInst>(CmpI->getOperand(0));
|
|
|
|
}
|
2011-07-07 02:22:43 +08:00
|
|
|
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Function *Fn = CI->getCalledFunction();
|
|
|
|
if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Value *ArgValue = CI->getArgOperand(0);
|
|
|
|
ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
|
|
|
|
if (!ExpectedValue)
|
|
|
|
return false;
|
|
|
|
|
2012-05-26 21:59:43 +08:00
|
|
|
MDBuilder MDB(CI->getContext());
|
|
|
|
MDNode *Node;
|
2011-07-07 02:22:43 +08:00
|
|
|
|
|
|
|
// If expect value is equal to 1 it means that we are more likely to take
|
|
|
|
// branch 0, in other case more likely is branch 1.
|
2012-05-26 21:59:43 +08:00
|
|
|
if (ExpectedValue->isOne())
|
|
|
|
Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
|
|
|
|
else
|
|
|
|
Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2015-01-24 18:39:24 +08:00
|
|
|
BI.setMetadata(LLVMContext::MD_prof, Node);
|
2011-07-07 02:22:43 +08:00
|
|
|
|
2014-02-03 06:43:55 +08:00
|
|
|
if (CmpI)
|
|
|
|
CmpI->setOperand(0, ArgValue);
|
|
|
|
else
|
2015-01-24 18:39:24 +08:00
|
|
|
BI.setCondition(ArgValue);
|
2011-07-07 02:22:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-24 19:13:02 +08:00
|
|
|
static bool lowerExpectIntrinsic(Function &F) {
|
2015-01-24 19:12:57 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
2015-01-24 18:57:19 +08:00
|
|
|
for (BasicBlock &BB : F) {
|
2011-07-07 02:22:43 +08:00
|
|
|
// Create "block_weights" metadata.
|
2015-01-24 18:57:19 +08:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
|
2015-01-24 18:39:24 +08:00
|
|
|
if (handleBranchExpect(*BI))
|
2015-01-24 18:57:25 +08:00
|
|
|
ExpectIntrinsicsHandled++;
|
2015-01-24 18:57:19 +08:00
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
|
2015-01-24 18:39:24 +08:00
|
|
|
if (handleSwitchExpect(*SI))
|
2015-01-24 18:57:25 +08:00
|
|
|
ExpectIntrinsicsHandled++;
|
2011-07-07 02:22:43 +08:00
|
|
|
}
|
|
|
|
|
2015-08-25 04:11:14 +08:00
|
|
|
// Remove llvm.expect intrinsics.
|
2015-01-24 18:57:19 +08:00
|
|
|
for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
|
2011-07-07 02:22:43 +08:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(BI++);
|
|
|
|
if (!CI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Function *Fn = CI->getCalledFunction();
|
2011-07-07 07:50:16 +08:00
|
|
|
if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
|
|
|
|
Value *Exp = CI->getArgOperand(0);
|
|
|
|
CI->replaceAllUsesWith(Exp);
|
2011-07-07 02:22:43 +08:00
|
|
|
CI->eraseFromParent();
|
2015-01-24 19:12:57 +08:00
|
|
|
Changed = true;
|
2011-07-07 07:50:16 +08:00
|
|
|
}
|
2011-07-07 02:22:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 19:12:57 +08:00
|
|
|
return Changed;
|
2011-07-07 02:22:43 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:11:01 +08:00
|
|
|
PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &) {
|
2015-01-24 19:13:02 +08:00
|
|
|
if (lowerExpectIntrinsic(F))
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// \brief Legacy pass for lowering expect intrinsics out of the IR.
|
|
|
|
///
|
|
|
|
/// When this pass is run over a function it uses expect intrinsics which feed
|
|
|
|
/// branches and switches to provide branch weight metadata for those
|
|
|
|
/// terminators. It then removes the expect intrinsics from the IR so the rest
|
|
|
|
/// of the optimizer can ignore them.
|
|
|
|
class LowerExpectIntrinsic : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
LowerExpectIntrinsic() : FunctionPass(ID) {
|
|
|
|
initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2015-01-24 19:13:02 +08:00
|
|
|
|
2011-07-07 02:22:43 +08:00
|
|
|
char LowerExpectIntrinsic::ID = 0;
|
2015-01-24 18:30:14 +08:00
|
|
|
INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
|
|
|
|
"Lower 'expect' Intrinsics", false, false)
|
2011-07-07 02:22:43 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createLowerExpectIntrinsicPass() {
|
|
|
|
return new LowerExpectIntrinsic();
|
|
|
|
}
|