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