1 //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass lowers the 'expect' intrinsic to LLVM metadata. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/ADT/iterator_range.h" 17 #include "llvm/IR/BasicBlock.h" 18 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/Intrinsics.h" 22 #include "llvm/IR/LLVMContext.h" 23 #include "llvm/IR/MDBuilder.h" 24 #include "llvm/InitializePasses.h" 25 #include "llvm/Pass.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Transforms/Scalar.h" 28 #include "llvm/Transforms/Utils/MisExpect.h" 29 30 using namespace llvm; 31 32 #define DEBUG_TYPE "lower-expect-intrinsic" 33 34 STATISTIC(ExpectIntrinsicsHandled, 35 "Number of 'expect' intrinsic instructions handled"); 36 37 // These default values are chosen to represent an extremely skewed outcome for 38 // a condition, but they leave some room for interpretation by later passes. 39 // 40 // If the documentation for __builtin_expect() was made explicit that it should 41 // only be used in extreme cases, we could make this ratio higher. As it stands, 42 // programmers may be using __builtin_expect() / llvm.expect to annotate that a 43 // branch is likely or unlikely to be taken. 44 45 // WARNING: these values are internal implementation detail of the pass. 46 // They should not be exposed to the outside of the pass, front-end codegen 47 // should emit @llvm.expect intrinsics instead of using these weights directly. 48 // Transforms should use TargetTransformInfo's getPredictableBranchThreshold(). 49 static cl::opt<uint32_t> LikelyBranchWeight( 50 "likely-branch-weight", cl::Hidden, cl::init(2000), 51 cl::desc("Weight of the branch likely to be taken (default = 2000)")); 52 static cl::opt<uint32_t> UnlikelyBranchWeight( 53 "unlikely-branch-weight", cl::Hidden, cl::init(1), 54 cl::desc("Weight of the branch unlikely to be taken (default = 1)")); 55 56 static std::tuple<uint32_t, uint32_t> 57 getBranchWeight(Intrinsic::ID IntrinsicID, CallInst *CI, int BranchCount) { 58 if (IntrinsicID == Intrinsic::expect) { 59 // __builtin_expect 60 return std::make_tuple(LikelyBranchWeight.getValue(), 61 UnlikelyBranchWeight.getValue()); 62 } else { 63 // __builtin_expect_with_probability 64 assert(CI->getNumOperands() >= 3 && 65 "expect with probability must have 3 arguments"); 66 auto *Confidence = cast<ConstantFP>(CI->getArgOperand(2)); 67 double TrueProb = Confidence->getValueAPF().convertToDouble(); 68 assert((TrueProb >= 0.0 && TrueProb <= 1.0) && 69 "probability value must be in the range [0.0, 1.0]"); 70 double FalseProb = (1.0 - TrueProb) / (BranchCount - 1); 71 uint32_t LikelyBW = ceil((TrueProb * (double)(INT32_MAX - 1)) + 1.0); 72 uint32_t UnlikelyBW = ceil((FalseProb * (double)(INT32_MAX - 1)) + 1.0); 73 return std::make_tuple(LikelyBW, UnlikelyBW); 74 } 75 } 76 77 static bool handleSwitchExpect(SwitchInst &SI) { 78 CallInst *CI = dyn_cast<CallInst>(SI.getCondition()); 79 if (!CI) 80 return false; 81 82 Function *Fn = CI->getCalledFunction(); 83 if (!Fn || (Fn->getIntrinsicID() != Intrinsic::expect && 84 Fn->getIntrinsicID() != Intrinsic::expect_with_probability)) 85 return false; 86 87 Value *ArgValue = CI->getArgOperand(0); 88 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 89 if (!ExpectedValue) 90 return false; 91 92 SwitchInst::CaseHandle Case = *SI.findCaseValue(ExpectedValue); 93 unsigned n = SI.getNumCases(); // +1 for default case. 94 uint32_t LikelyBranchWeightVal, UnlikelyBranchWeightVal; 95 std::tie(LikelyBranchWeightVal, UnlikelyBranchWeightVal) = 96 getBranchWeight(Fn->getIntrinsicID(), CI, n + 1); 97 98 SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeightVal); 99 100 uint64_t Index = (Case == *SI.case_default()) ? 0 : Case.getCaseIndex() + 1; 101 Weights[Index] = LikelyBranchWeightVal; 102 103 misexpect::checkExpectAnnotations(SI, Weights, /*IsFrontend=*/true); 104 105 SI.setCondition(ArgValue); 106 107 SI.setMetadata(LLVMContext::MD_prof, 108 MDBuilder(CI->getContext()).createBranchWeights(Weights)); 109 110 return true; 111 } 112 113 /// Handler for PHINodes that define the value argument to an 114 /// @llvm.expect call. 115 /// 116 /// If the operand of the phi has a constant value and it 'contradicts' 117 /// with the expected value of phi def, then the corresponding incoming 118 /// edge of the phi is unlikely to be taken. Using that information, 119 /// the branch probability info for the originating branch can be inferred. 120 static void handlePhiDef(CallInst *Expect) { 121 Value &Arg = *Expect->getArgOperand(0); 122 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(Expect->getArgOperand(1)); 123 if (!ExpectedValue) 124 return; 125 const APInt &ExpectedPhiValue = ExpectedValue->getValue(); 126 127 // Walk up in backward a list of instructions that 128 // have 'copy' semantics by 'stripping' the copies 129 // until a PHI node or an instruction of unknown kind 130 // is reached. Negation via xor is also handled. 131 // 132 // C = PHI(...); 133 // B = C; 134 // A = B; 135 // D = __builtin_expect(A, 0); 136 // 137 Value *V = &Arg; 138 SmallVector<Instruction *, 4> Operations; 139 while (!isa<PHINode>(V)) { 140 if (ZExtInst *ZExt = dyn_cast<ZExtInst>(V)) { 141 V = ZExt->getOperand(0); 142 Operations.push_back(ZExt); 143 continue; 144 } 145 146 if (SExtInst *SExt = dyn_cast<SExtInst>(V)) { 147 V = SExt->getOperand(0); 148 Operations.push_back(SExt); 149 continue; 150 } 151 152 BinaryOperator *BinOp = dyn_cast<BinaryOperator>(V); 153 if (!BinOp || BinOp->getOpcode() != Instruction::Xor) 154 return; 155 156 ConstantInt *CInt = dyn_cast<ConstantInt>(BinOp->getOperand(1)); 157 if (!CInt) 158 return; 159 160 V = BinOp->getOperand(0); 161 Operations.push_back(BinOp); 162 } 163 164 // Executes the recorded operations on input 'Value'. 165 auto ApplyOperations = [&](const APInt &Value) { 166 APInt Result = Value; 167 for (auto Op : llvm::reverse(Operations)) { 168 switch (Op->getOpcode()) { 169 case Instruction::Xor: 170 Result ^= cast<ConstantInt>(Op->getOperand(1))->getValue(); 171 break; 172 case Instruction::ZExt: 173 Result = Result.zext(Op->getType()->getIntegerBitWidth()); 174 break; 175 case Instruction::SExt: 176 Result = Result.sext(Op->getType()->getIntegerBitWidth()); 177 break; 178 default: 179 llvm_unreachable("Unexpected operation"); 180 } 181 } 182 return Result; 183 }; 184 185 auto *PhiDef = cast<PHINode>(V); 186 187 // Get the first dominating conditional branch of the operand 188 // i's incoming block. 189 auto GetDomConditional = [&](unsigned i) -> BranchInst * { 190 BasicBlock *BB = PhiDef->getIncomingBlock(i); 191 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 192 if (BI && BI->isConditional()) 193 return BI; 194 BB = BB->getSinglePredecessor(); 195 if (!BB) 196 return nullptr; 197 BI = dyn_cast<BranchInst>(BB->getTerminator()); 198 if (!BI || BI->isUnconditional()) 199 return nullptr; 200 return BI; 201 }; 202 203 // Now walk through all Phi operands to find phi oprerands with values 204 // conflicting with the expected phi output value. Any such operand 205 // indicates the incoming edge to that operand is unlikely. 206 for (unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; ++i) { 207 208 Value *PhiOpnd = PhiDef->getIncomingValue(i); 209 ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd); 210 if (!CI) 211 continue; 212 213 // Not an interesting case when IsUnlikely is false -- we can not infer 214 // anything useful when the operand value matches the expected phi 215 // output. 216 if (ExpectedPhiValue == ApplyOperations(CI->getValue())) 217 continue; 218 219 BranchInst *BI = GetDomConditional(i); 220 if (!BI) 221 continue; 222 223 MDBuilder MDB(PhiDef->getContext()); 224 225 // There are two situations in which an operand of the PhiDef comes 226 // from a given successor of a branch instruction BI. 227 // 1) When the incoming block of the operand is the successor block; 228 // 2) When the incoming block is BI's enclosing block and the 229 // successor is the PhiDef's enclosing block. 230 // 231 // Returns true if the operand which comes from OpndIncomingBB 232 // comes from outgoing edge of BI that leads to Succ block. 233 auto *OpndIncomingBB = PhiDef->getIncomingBlock(i); 234 auto IsOpndComingFromSuccessor = [&](BasicBlock *Succ) { 235 if (OpndIncomingBB == Succ) 236 // If this successor is the incoming block for this 237 // Phi operand, then this successor does lead to the Phi. 238 return true; 239 if (OpndIncomingBB == BI->getParent() && Succ == PhiDef->getParent()) 240 // Otherwise, if the edge is directly from the branch 241 // to the Phi, this successor is the one feeding this 242 // Phi operand. 243 return true; 244 return false; 245 }; 246 uint32_t LikelyBranchWeightVal, UnlikelyBranchWeightVal; 247 std::tie(LikelyBranchWeightVal, UnlikelyBranchWeightVal) = getBranchWeight( 248 Expect->getCalledFunction()->getIntrinsicID(), Expect, 2); 249 250 if (IsOpndComingFromSuccessor(BI->getSuccessor(1))) 251 BI->setMetadata(LLVMContext::MD_prof, 252 MDB.createBranchWeights(LikelyBranchWeightVal, 253 UnlikelyBranchWeightVal)); 254 else if (IsOpndComingFromSuccessor(BI->getSuccessor(0))) 255 BI->setMetadata(LLVMContext::MD_prof, 256 MDB.createBranchWeights(UnlikelyBranchWeightVal, 257 LikelyBranchWeightVal)); 258 } 259 } 260 261 // Handle both BranchInst and SelectInst. 262 template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) { 263 264 // Handle non-optimized IR code like: 265 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1) 266 // %tobool = icmp ne i64 %expval, 0 267 // br i1 %tobool, label %if.then, label %if.end 268 // 269 // Or the following simpler case: 270 // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1) 271 // br i1 %expval, label %if.then, label %if.end 272 273 CallInst *CI; 274 275 ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition()); 276 CmpInst::Predicate Predicate; 277 ConstantInt *CmpConstOperand = nullptr; 278 if (!CmpI) { 279 CI = dyn_cast<CallInst>(BSI.getCondition()); 280 Predicate = CmpInst::ICMP_NE; 281 } else { 282 Predicate = CmpI->getPredicate(); 283 if (Predicate != CmpInst::ICMP_NE && Predicate != CmpInst::ICMP_EQ) 284 return false; 285 286 CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1)); 287 if (!CmpConstOperand) 288 return false; 289 CI = dyn_cast<CallInst>(CmpI->getOperand(0)); 290 } 291 292 if (!CI) 293 return false; 294 295 uint64_t ValueComparedTo = 0; 296 if (CmpConstOperand) { 297 if (CmpConstOperand->getBitWidth() > 64) 298 return false; 299 ValueComparedTo = CmpConstOperand->getZExtValue(); 300 } 301 302 Function *Fn = CI->getCalledFunction(); 303 if (!Fn || (Fn->getIntrinsicID() != Intrinsic::expect && 304 Fn->getIntrinsicID() != Intrinsic::expect_with_probability)) 305 return false; 306 307 Value *ArgValue = CI->getArgOperand(0); 308 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 309 if (!ExpectedValue) 310 return false; 311 312 MDBuilder MDB(CI->getContext()); 313 MDNode *Node; 314 315 uint32_t LikelyBranchWeightVal, UnlikelyBranchWeightVal; 316 std::tie(LikelyBranchWeightVal, UnlikelyBranchWeightVal) = 317 getBranchWeight(Fn->getIntrinsicID(), CI, 2); 318 319 SmallVector<uint32_t, 4> ExpectedWeights; 320 if ((ExpectedValue->getZExtValue() == ValueComparedTo) == 321 (Predicate == CmpInst::ICMP_EQ)) { 322 Node = 323 MDB.createBranchWeights(LikelyBranchWeightVal, UnlikelyBranchWeightVal); 324 ExpectedWeights = {LikelyBranchWeightVal, UnlikelyBranchWeightVal}; 325 } else { 326 Node = 327 MDB.createBranchWeights(UnlikelyBranchWeightVal, LikelyBranchWeightVal); 328 ExpectedWeights = {UnlikelyBranchWeightVal, LikelyBranchWeightVal}; 329 } 330 331 if (CmpI) 332 CmpI->setOperand(0, ArgValue); 333 else 334 BSI.setCondition(ArgValue); 335 336 misexpect::checkFrontendInstrumentation(BSI, ExpectedWeights); 337 338 BSI.setMetadata(LLVMContext::MD_prof, Node); 339 340 return true; 341 } 342 343 static bool handleBranchExpect(BranchInst &BI) { 344 if (BI.isUnconditional()) 345 return false; 346 347 return handleBrSelExpect<BranchInst>(BI); 348 } 349 350 static bool lowerExpectIntrinsic(Function &F) { 351 bool Changed = false; 352 353 for (BasicBlock &BB : F) { 354 // Create "block_weights" metadata. 355 if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) { 356 if (handleBranchExpect(*BI)) 357 ExpectIntrinsicsHandled++; 358 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) { 359 if (handleSwitchExpect(*SI)) 360 ExpectIntrinsicsHandled++; 361 } 362 363 // Remove llvm.expect intrinsics. Iterate backwards in order 364 // to process select instructions before the intrinsic gets 365 // removed. 366 for (Instruction &Inst : llvm::make_early_inc_range(llvm::reverse(BB))) { 367 CallInst *CI = dyn_cast<CallInst>(&Inst); 368 if (!CI) { 369 if (SelectInst *SI = dyn_cast<SelectInst>(&Inst)) { 370 if (handleBrSelExpect(*SI)) 371 ExpectIntrinsicsHandled++; 372 } 373 continue; 374 } 375 376 Function *Fn = CI->getCalledFunction(); 377 if (Fn && (Fn->getIntrinsicID() == Intrinsic::expect || 378 Fn->getIntrinsicID() == Intrinsic::expect_with_probability)) { 379 // Before erasing the llvm.expect, walk backward to find 380 // phi that define llvm.expect's first arg, and 381 // infer branch probability: 382 handlePhiDef(CI); 383 Value *Exp = CI->getArgOperand(0); 384 CI->replaceAllUsesWith(Exp); 385 CI->eraseFromParent(); 386 Changed = true; 387 } 388 } 389 } 390 391 return Changed; 392 } 393 394 PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F, 395 FunctionAnalysisManager &) { 396 if (lowerExpectIntrinsic(F)) 397 return PreservedAnalyses::none(); 398 399 return PreservedAnalyses::all(); 400 } 401 402 namespace { 403 /// Legacy pass for lowering expect intrinsics out of the IR. 404 /// 405 /// When this pass is run over a function it uses expect intrinsics which feed 406 /// branches and switches to provide branch weight metadata for those 407 /// terminators. It then removes the expect intrinsics from the IR so the rest 408 /// of the optimizer can ignore them. 409 class LowerExpectIntrinsic : public FunctionPass { 410 public: 411 static char ID; 412 LowerExpectIntrinsic() : FunctionPass(ID) { 413 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry()); 414 } 415 416 bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); } 417 }; 418 } // namespace 419 420 char LowerExpectIntrinsic::ID = 0; 421 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", 422 "Lower 'expect' Intrinsics", false, false) 423 424 FunctionPass *llvm::createLowerExpectIntrinsicPass() { 425 return new LowerExpectIntrinsic(); 426 } 427