cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
namespace {
+class LowerExpectIntrinsic : public FunctionPass {
- class LowerExpectIntrinsic : public FunctionPass {
+ bool HandleSwitchExpect(SwitchInst *SI);
- bool HandleSwitchExpect(SwitchInst *SI);
+ bool HandleIfExpect(BranchInst *BI);
- bool HandleIfExpect(BranchInst *BI);
-
- public:
- static char ID;
- LowerExpectIntrinsic() : FunctionPass(ID) {
- initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
- }
+public:
+ static char ID;
+ LowerExpectIntrinsic() : FunctionPass(ID) {
+ initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
+ }
- bool runOnFunction(Function &F) override;
- };
+ bool runOnFunction(Function &F) override;
+};
}
-
bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
CallInst *CI = dyn_cast<CallInst>(SI->getCondition());
if (!CI)
unsigned n = SI->getNumCases(); // +1 for default case.
std::vector<uint32_t> Weights(n + 1);
- Weights[0] = Case == SI->case_default() ? LikelyBranchWeight
- : UnlikelyBranchWeight;
+ Weights[0] =
+ Case == SI->case_default() ? LikelyBranchWeight : UnlikelyBranchWeight;
for (unsigned i = 0; i != n; ++i)
- Weights[i + 1] = i == Case.getCaseIndex() ? LikelyBranchWeight
- : UnlikelyBranchWeight;
+ Weights[i + 1] =
+ i == Case.getCaseIndex() ? LikelyBranchWeight : UnlikelyBranchWeight;
SI->setMetadata(LLVMContext::MD_prof,
MDBuilder(CI->getContext()).createBranchWeights(Weights));
return true;
}
-
bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
if (BI->isUnconditional())
return false;
return true;
}
-
bool LowerExpectIntrinsic::runOnFunction(Function &F) {
for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
BasicBlock *BB = I++;
}
// remove llvm.expect intrinsics.
- for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
- BI != BE; ) {
+ for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
CallInst *CI = dyn_cast<CallInst>(BI++);
if (!CI)
continue;
return false;
}
-
char LowerExpectIntrinsic::ID = 0;
-INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", "Lower 'expect' "
- "Intrinsics", false, false)
+INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
+ "Lower 'expect' Intrinsics", false, false)
FunctionPass *llvm::createLowerExpectIntrinsicPass() {
return new LowerExpectIntrinsic();