2011-10-16 22:15:07 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-06 18:22:43 +00:00
|
|
|
#define DEBUG_TYPE "lower-expect-intrinsic"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-07-15 23:26:50 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2011-07-06 18:22:43 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2012-07-15 23:26:50 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
|
|
|
#include "llvm/MDBuilder.h"
|
2011-07-06 18:22:43 +00:00
|
|
|
#include "llvm/Metadata.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(IfHandled, "Number of 'expect' intrinsic intructions handled");
|
|
|
|
|
|
|
|
static cl::opt<uint32_t>
|
|
|
|
LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
|
|
|
|
cl::desc("Weight of the branch likely to be taken (default = 64)"));
|
|
|
|
static cl::opt<uint32_t>
|
|
|
|
UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
|
|
|
|
cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class LowerExpectIntrinsic : public FunctionPass {
|
|
|
|
|
|
|
|
bool HandleSwitchExpect(SwitchInst *SI);
|
|
|
|
|
|
|
|
bool HandleIfExpect(BranchInst *BI);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
LowerExpectIntrinsic() : FunctionPass(ID) {
|
|
|
|
initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
|
|
|
|
CallInst *CI = dyn_cast<CallInst>(SI->getCondition());
|
|
|
|
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-03-08 07:06:20 +00:00
|
|
|
SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
|
2012-05-26 13:59:43 +00:00
|
|
|
unsigned n = SI->getNumCases(); // +1 for default case.
|
|
|
|
std::vector<uint32_t> Weights(n + 1);
|
2011-07-06 18:22:43 +00:00
|
|
|
|
2012-05-26 13:59:43 +00:00
|
|
|
Weights[0] = Case == SI->case_default() ? LikelyBranchWeight
|
|
|
|
: UnlikelyBranchWeight;
|
|
|
|
for (unsigned i = 0; i != n; ++i)
|
|
|
|
Weights[i + 1] = i == Case.getCaseIndex() ? LikelyBranchWeight
|
|
|
|
: UnlikelyBranchWeight;
|
2011-07-06 18:22:43 +00:00
|
|
|
|
2012-05-26 13:59:43 +00:00
|
|
|
SI->setMetadata(LLVMContext::MD_prof,
|
|
|
|
MDBuilder(CI->getContext()).createBranchWeights(Weights));
|
2011-07-06 18:22:43 +00:00
|
|
|
|
|
|
|
SI->setCondition(ArgValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
|
|
|
|
if (BI->isUnconditional())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Handle non-optimized IR code like:
|
|
|
|
// %expval = call i64 @llvm.expect.i64.i64(i64 %conv1, i64 1)
|
|
|
|
// %tobool = icmp ne i64 %expval, 0
|
|
|
|
// br i1 %tobool, label %if.then, label %if.end
|
|
|
|
|
|
|
|
ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
|
|
|
|
if (!CmpI || CmpI->getPredicate() != CmpInst::ICMP_NE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CallInst *CI = dyn_cast<CallInst>(CmpI->getOperand(0));
|
|
|
|
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 13:59:43 +00:00
|
|
|
MDBuilder MDB(CI->getContext());
|
|
|
|
MDNode *Node;
|
2011-07-06 18:22:43 +00: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 13:59:43 +00:00
|
|
|
if (ExpectedValue->isOne())
|
|
|
|
Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
|
|
|
|
else
|
|
|
|
Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
|
2011-07-06 18:22:43 +00:00
|
|
|
|
2012-05-26 13:59:43 +00:00
|
|
|
BI->setMetadata(LLVMContext::MD_prof, Node);
|
2011-07-06 18:22:43 +00:00
|
|
|
|
|
|
|
CmpI->setOperand(0, ArgValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LowerExpectIntrinsic::runOnFunction(Function &F) {
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
|
|
|
|
BasicBlock *BB = I++;
|
|
|
|
|
|
|
|
// Create "block_weights" metadata.
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
|
|
|
|
if (HandleIfExpect(BI))
|
|
|
|
IfHandled++;
|
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
|
|
|
|
if (HandleSwitchExpect(SI))
|
|
|
|
IfHandled++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove llvm.expect intrinsics.
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
|
|
|
BI != BE; ) {
|
|
|
|
CallInst *CI = dyn_cast<CallInst>(BI++);
|
|
|
|
if (!CI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Function *Fn = CI->getCalledFunction();
|
2011-07-06 23:50:16 +00:00
|
|
|
if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
|
|
|
|
Value *Exp = CI->getArgOperand(0);
|
|
|
|
CI->replaceAllUsesWith(Exp);
|
2011-07-06 18:22:43 +00:00
|
|
|
CI->eraseFromParent();
|
2011-07-06 23:50:16 +00:00
|
|
|
}
|
2011-07-06 18:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char LowerExpectIntrinsic::ID = 0;
|
|
|
|
INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", "Lower 'expect' "
|
|
|
|
"Intrinsics", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createLowerExpectIntrinsicPass() {
|
|
|
|
return new LowerExpectIntrinsic();
|
|
|
|
}
|