1 //===- CallPromotionUtils.cpp - Utilities for call promotion ----*- C++ -*-===// 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 file implements utilities useful for promoting indirect call sites to 10 // direct call sites. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/CallPromotionUtils.h" 15 #include "llvm/Analysis/Loads.h" 16 #include "llvm/Analysis/TypeMetadataUtils.h" 17 #include "llvm/IR/IRBuilder.h" 18 #include "llvm/IR/Instructions.h" 19 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 20 21 using namespace llvm; 22 23 #define DEBUG_TYPE "call-promotion-utils" 24 25 /// Fix-up phi nodes in an invoke instruction's normal destination. 26 /// 27 /// After versioning an invoke instruction, values coming from the original 28 /// block will now be coming from the "merge" block. For example, in the code 29 /// below: 30 /// 31 /// then_bb: 32 /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst 33 /// 34 /// else_bb: 35 /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst 36 /// 37 /// merge_bb: 38 /// %t2 = phi i32 [ %t0, %then_bb ], [ %t1, %else_bb ] 39 /// br %normal_dst 40 /// 41 /// normal_dst: 42 /// %t3 = phi i32 [ %x, %orig_bb ], ... 43 /// 44 /// "orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in 45 /// "normal_dst" must be fixed to refer to "merge_bb": 46 /// 47 /// normal_dst: 48 /// %t3 = phi i32 [ %x, %merge_bb ], ... 49 /// 50 static void fixupPHINodeForNormalDest(InvokeInst *Invoke, BasicBlock *OrigBlock, 51 BasicBlock *MergeBlock) { 52 for (PHINode &Phi : Invoke->getNormalDest()->phis()) { 53 int Idx = Phi.getBasicBlockIndex(OrigBlock); 54 if (Idx == -1) 55 continue; 56 Phi.setIncomingBlock(Idx, MergeBlock); 57 } 58 } 59 60 /// Fix-up phi nodes in an invoke instruction's unwind destination. 61 /// 62 /// After versioning an invoke instruction, values coming from the original 63 /// block will now be coming from either the "then" block or the "else" block. 64 /// For example, in the code below: 65 /// 66 /// then_bb: 67 /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst 68 /// 69 /// else_bb: 70 /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst 71 /// 72 /// unwind_dst: 73 /// %t3 = phi i32 [ %x, %orig_bb ], ... 74 /// 75 /// "orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in 76 /// "unwind_dst" must be fixed to refer to "then_bb" and "else_bb": 77 /// 78 /// unwind_dst: 79 /// %t3 = phi i32 [ %x, %then_bb ], [ %x, %else_bb ], ... 80 /// 81 static void fixupPHINodeForUnwindDest(InvokeInst *Invoke, BasicBlock *OrigBlock, 82 BasicBlock *ThenBlock, 83 BasicBlock *ElseBlock) { 84 for (PHINode &Phi : Invoke->getUnwindDest()->phis()) { 85 int Idx = Phi.getBasicBlockIndex(OrigBlock); 86 if (Idx == -1) 87 continue; 88 auto *V = Phi.getIncomingValue(Idx); 89 Phi.setIncomingBlock(Idx, ThenBlock); 90 Phi.addIncoming(V, ElseBlock); 91 } 92 } 93 94 /// Create a phi node for the returned value of a call or invoke instruction. 95 /// 96 /// After versioning a call or invoke instruction that returns a value, we have 97 /// to merge the value of the original and new instructions. We do this by 98 /// creating a phi node and replacing uses of the original instruction with this 99 /// phi node. 100 /// 101 /// For example, if \p OrigInst is defined in "else_bb" and \p NewInst is 102 /// defined in "then_bb", we create the following phi node: 103 /// 104 /// ; Uses of the original instruction are replaced by uses of the phi node. 105 /// %t0 = phi i32 [ %orig_inst, %else_bb ], [ %new_inst, %then_bb ], 106 /// 107 static void createRetPHINode(Instruction *OrigInst, Instruction *NewInst, 108 BasicBlock *MergeBlock, IRBuilder<> &Builder) { 109 110 if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty()) 111 return; 112 113 Builder.SetInsertPoint(&MergeBlock->front()); 114 PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0); 115 SmallVector<User *, 16> UsersToUpdate; 116 for (User *U : OrigInst->users()) 117 UsersToUpdate.push_back(U); 118 for (User *U : UsersToUpdate) 119 U->replaceUsesOfWith(OrigInst, Phi); 120 Phi->addIncoming(OrigInst, OrigInst->getParent()); 121 Phi->addIncoming(NewInst, NewInst->getParent()); 122 } 123 124 /// Cast a call or invoke instruction to the given type. 125 /// 126 /// When promoting a call site, the return type of the call site might not match 127 /// that of the callee. If this is the case, we have to cast the returned value 128 /// to the correct type. The location of the cast depends on if we have a call 129 /// or invoke instruction. 130 /// 131 /// For example, if the call instruction below requires a bitcast after 132 /// promotion: 133 /// 134 /// orig_bb: 135 /// %t0 = call i32 @func() 136 /// ... 137 /// 138 /// The bitcast is placed after the call instruction: 139 /// 140 /// orig_bb: 141 /// ; Uses of the original return value are replaced by uses of the bitcast. 142 /// %t0 = call i32 @func() 143 /// %t1 = bitcast i32 %t0 to ... 144 /// ... 145 /// 146 /// A similar transformation is performed for invoke instructions. However, 147 /// since invokes are terminating, a new block is created for the bitcast. For 148 /// example, if the invoke instruction below requires a bitcast after promotion: 149 /// 150 /// orig_bb: 151 /// %t0 = invoke i32 @func() to label %normal_dst unwind label %unwind_dst 152 /// 153 /// The edge between the original block and the invoke's normal destination is 154 /// split, and the bitcast is placed there: 155 /// 156 /// orig_bb: 157 /// %t0 = invoke i32 @func() to label %split_bb unwind label %unwind_dst 158 /// 159 /// split_bb: 160 /// ; Uses of the original return value are replaced by uses of the bitcast. 161 /// %t1 = bitcast i32 %t0 to ... 162 /// br label %normal_dst 163 /// 164 static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) { 165 166 // Save the users of the calling instruction. These uses will be changed to 167 // use the bitcast after we create it. 168 SmallVector<User *, 16> UsersToUpdate(CB.users()); 169 170 // Determine an appropriate location to create the bitcast for the return 171 // value. The location depends on if we have a call or invoke instruction. 172 Instruction *InsertBefore = nullptr; 173 if (auto *Invoke = dyn_cast<InvokeInst>(&CB)) 174 InsertBefore = 175 &SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->front(); 176 else 177 InsertBefore = &*std::next(CB.getIterator()); 178 179 // Bitcast the return value to the correct type. 180 auto *Cast = CastInst::CreateBitOrPointerCast(&CB, RetTy, "", InsertBefore); 181 if (RetBitCast) 182 *RetBitCast = Cast; 183 184 // Replace all the original uses of the calling instruction with the bitcast. 185 for (User *U : UsersToUpdate) 186 U->replaceUsesOfWith(&CB, Cast); 187 } 188 189 /// Predicate and clone the given call site. 190 /// 191 /// This function creates an if-then-else structure at the location of the call 192 /// site. The "if" condition compares the call site's called value to the given 193 /// callee. The original call site is moved into the "else" block, and a clone 194 /// of the call site is placed in the "then" block. The cloned instruction is 195 /// returned. 196 /// 197 /// For example, the call instruction below: 198 /// 199 /// orig_bb: 200 /// %t0 = call i32 %ptr() 201 /// ... 202 /// 203 /// Is replace by the following: 204 /// 205 /// orig_bb: 206 /// %cond = icmp eq i32 ()* %ptr, @func 207 /// br i1 %cond, %then_bb, %else_bb 208 /// 209 /// then_bb: 210 /// ; The clone of the original call instruction is placed in the "then" 211 /// ; block. It is not yet promoted. 212 /// %t1 = call i32 %ptr() 213 /// br merge_bb 214 /// 215 /// else_bb: 216 /// ; The original call instruction is moved to the "else" block. 217 /// %t0 = call i32 %ptr() 218 /// br merge_bb 219 /// 220 /// merge_bb: 221 /// ; Uses of the original call instruction are replaced by uses of the phi 222 /// ; node. 223 /// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ] 224 /// ... 225 /// 226 /// A similar transformation is performed for invoke instructions. However, 227 /// since invokes are terminating, more work is required. For example, the 228 /// invoke instruction below: 229 /// 230 /// orig_bb: 231 /// %t0 = invoke %ptr() to label %normal_dst unwind label %unwind_dst 232 /// 233 /// Is replace by the following: 234 /// 235 /// orig_bb: 236 /// %cond = icmp eq i32 ()* %ptr, @func 237 /// br i1 %cond, %then_bb, %else_bb 238 /// 239 /// then_bb: 240 /// ; The clone of the original invoke instruction is placed in the "then" 241 /// ; block, and its normal destination is set to the "merge" block. It is 242 /// ; not yet promoted. 243 /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst 244 /// 245 /// else_bb: 246 /// ; The original invoke instruction is moved into the "else" block, and 247 /// ; its normal destination is set to the "merge" block. 248 /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst 249 /// 250 /// merge_bb: 251 /// ; Uses of the original invoke instruction are replaced by uses of the 252 /// ; phi node, and the merge block branches to the normal destination. 253 /// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ] 254 /// br %normal_dst 255 /// 256 /// An indirect musttail call is processed slightly differently in that: 257 /// 1. No merge block needed for the orginal and the cloned callsite, since 258 /// either one ends the flow. No phi node is needed either. 259 /// 2. The return statement following the original call site is duplicated too 260 /// and placed immediately after the cloned call site per the IR convention. 261 /// 262 /// For example, the musttail call instruction below: 263 /// 264 /// orig_bb: 265 /// %t0 = musttail call i32 %ptr() 266 /// ... 267 /// 268 /// Is replaced by the following: 269 /// 270 /// cond_bb: 271 /// %cond = icmp eq i32 ()* %ptr, @func 272 /// br i1 %cond, %then_bb, %orig_bb 273 /// 274 /// then_bb: 275 /// ; The clone of the original call instruction is placed in the "then" 276 /// ; block. It is not yet promoted. 277 /// %t1 = musttail call i32 %ptr() 278 /// ret %t1 279 /// 280 /// orig_bb: 281 /// ; The original call instruction stays in its original block. 282 /// %t0 = musttail call i32 %ptr() 283 /// ret %t0 284 static CallBase &versionCallSite(CallBase &CB, Value *Callee, 285 MDNode *BranchWeights) { 286 287 IRBuilder<> Builder(&CB); 288 CallBase *OrigInst = &CB; 289 BasicBlock *OrigBlock = OrigInst->getParent(); 290 291 // Create the compare. The called value and callee must have the same type to 292 // be compared. 293 if (CB.getCalledOperand()->getType() != Callee->getType()) 294 Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType()); 295 auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee); 296 297 if (OrigInst->isMustTailCall()) { 298 // Create an if-then structure. The original instruction stays in its block, 299 // and a clone of the original instruction is placed in the "then" block. 300 Instruction *ThenTerm = 301 SplitBlockAndInsertIfThen(Cond, &CB, false, BranchWeights); 302 BasicBlock *ThenBlock = ThenTerm->getParent(); 303 ThenBlock->setName("if.true.direct_targ"); 304 CallBase *NewInst = cast<CallBase>(OrigInst->clone()); 305 NewInst->insertBefore(ThenTerm); 306 307 // Place a clone of the optional bitcast after the new call site. 308 Value *NewRetVal = NewInst; 309 auto Next = OrigInst->getNextNode(); 310 if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) { 311 assert(BitCast->getOperand(0) == OrigInst && 312 "bitcast following musttail call must use the call"); 313 auto NewBitCast = BitCast->clone(); 314 NewBitCast->replaceUsesOfWith(OrigInst, NewInst); 315 NewBitCast->insertBefore(ThenTerm); 316 NewRetVal = NewBitCast; 317 Next = BitCast->getNextNode(); 318 } 319 320 // Place a clone of the return instruction after the new call site. 321 ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next); 322 assert(Ret && "musttail call must precede a ret with an optional bitcast"); 323 auto NewRet = Ret->clone(); 324 if (Ret->getReturnValue()) 325 NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal); 326 NewRet->insertBefore(ThenTerm); 327 328 // A return instructions is terminating, so we don't need the terminator 329 // instruction just created. 330 ThenTerm->eraseFromParent(); 331 332 return *NewInst; 333 } 334 335 // Create an if-then-else structure. The original instruction is moved into 336 // the "else" block, and a clone of the original instruction is placed in the 337 // "then" block. 338 Instruction *ThenTerm = nullptr; 339 Instruction *ElseTerm = nullptr; 340 SplitBlockAndInsertIfThenElse(Cond, &CB, &ThenTerm, &ElseTerm, BranchWeights); 341 BasicBlock *ThenBlock = ThenTerm->getParent(); 342 BasicBlock *ElseBlock = ElseTerm->getParent(); 343 BasicBlock *MergeBlock = OrigInst->getParent(); 344 345 ThenBlock->setName("if.true.direct_targ"); 346 ElseBlock->setName("if.false.orig_indirect"); 347 MergeBlock->setName("if.end.icp"); 348 349 CallBase *NewInst = cast<CallBase>(OrigInst->clone()); 350 OrigInst->moveBefore(ElseTerm); 351 NewInst->insertBefore(ThenTerm); 352 353 // If the original call site is an invoke instruction, we have extra work to 354 // do since invoke instructions are terminating. We have to fix-up phi nodes 355 // in the invoke's normal and unwind destinations. 356 if (auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) { 357 auto *NewInvoke = cast<InvokeInst>(NewInst); 358 359 // Invoke instructions are terminating, so we don't need the terminator 360 // instructions that were just created. 361 ThenTerm->eraseFromParent(); 362 ElseTerm->eraseFromParent(); 363 364 // Branch from the "merge" block to the original normal destination. 365 Builder.SetInsertPoint(MergeBlock); 366 Builder.CreateBr(OrigInvoke->getNormalDest()); 367 368 // Fix-up phi nodes in the original invoke's normal and unwind destinations. 369 fixupPHINodeForNormalDest(OrigInvoke, OrigBlock, MergeBlock); 370 fixupPHINodeForUnwindDest(OrigInvoke, MergeBlock, ThenBlock, ElseBlock); 371 372 // Now set the normal destinations of the invoke instructions to be the 373 // "merge" block. 374 OrigInvoke->setNormalDest(MergeBlock); 375 NewInvoke->setNormalDest(MergeBlock); 376 } 377 378 // Create a phi node for the returned value of the call site. 379 createRetPHINode(OrigInst, NewInst, MergeBlock, Builder); 380 381 return *NewInst; 382 } 383 384 bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee, 385 const char **FailureReason) { 386 assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted"); 387 388 auto &DL = Callee->getParent()->getDataLayout(); 389 390 // Check the return type. The callee's return value type must be bitcast 391 // compatible with the call site's type. 392 Type *CallRetTy = CB.getType(); 393 Type *FuncRetTy = Callee->getReturnType(); 394 if (CallRetTy != FuncRetTy) 395 if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) { 396 if (FailureReason) 397 *FailureReason = "Return type mismatch"; 398 return false; 399 } 400 401 // The number of formal arguments of the callee. 402 unsigned NumParams = Callee->getFunctionType()->getNumParams(); 403 404 // The number of actual arguments in the call. 405 unsigned NumArgs = CB.arg_size(); 406 407 // Check the number of arguments. The callee and call site must agree on the 408 // number of arguments. 409 if (NumArgs != NumParams && !Callee->isVarArg()) { 410 if (FailureReason) 411 *FailureReason = "The number of arguments mismatch"; 412 return false; 413 } 414 415 // Check the argument types. The callee's formal argument types must be 416 // bitcast compatible with the corresponding actual argument types of the call 417 // site. 418 unsigned I = 0; 419 for (; I < NumParams; ++I) { 420 Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I); 421 Type *ActualTy = CB.getArgOperand(I)->getType(); 422 if (FormalTy == ActualTy) 423 continue; 424 if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) { 425 if (FailureReason) 426 *FailureReason = "Argument type mismatch"; 427 return false; 428 } 429 } 430 for (; I < NumArgs; I++) { 431 // Vararg functions can have more arguments than parameters. 432 assert(Callee->isVarArg()); 433 if (CB.paramHasAttr(I, Attribute::StructRet)) { 434 if (FailureReason) 435 *FailureReason = "SRet arg to vararg function"; 436 return false; 437 } 438 } 439 440 return true; 441 } 442 443 CallBase &llvm::promoteCall(CallBase &CB, Function *Callee, 444 CastInst **RetBitCast) { 445 assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted"); 446 447 // Set the called function of the call site to be the given callee (but don't 448 // change the type). 449 CB.setCalledOperand(Callee); 450 451 // Since the call site will no longer be direct, we must clear metadata that 452 // is only appropriate for indirect calls. This includes !prof and !callees 453 // metadata. 454 CB.setMetadata(LLVMContext::MD_prof, nullptr); 455 CB.setMetadata(LLVMContext::MD_callees, nullptr); 456 457 // If the function type of the call site matches that of the callee, no 458 // additional work is required. 459 if (CB.getFunctionType() == Callee->getFunctionType()) 460 return CB; 461 462 // Save the return types of the call site and callee. 463 Type *CallSiteRetTy = CB.getType(); 464 Type *CalleeRetTy = Callee->getReturnType(); 465 466 // Change the function type of the call site the match that of the callee. 467 CB.mutateFunctionType(Callee->getFunctionType()); 468 469 // Inspect the arguments of the call site. If an argument's type doesn't 470 // match the corresponding formal argument's type in the callee, bitcast it 471 // to the correct type. 472 auto CalleeType = Callee->getFunctionType(); 473 auto CalleeParamNum = CalleeType->getNumParams(); 474 475 LLVMContext &Ctx = Callee->getContext(); 476 const AttributeList &CallerPAL = CB.getAttributes(); 477 // The new list of argument attributes. 478 SmallVector<AttributeSet, 4> NewArgAttrs; 479 bool AttributeChanged = false; 480 481 for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) { 482 auto *Arg = CB.getArgOperand(ArgNo); 483 Type *FormalTy = CalleeType->getParamType(ArgNo); 484 Type *ActualTy = Arg->getType(); 485 if (FormalTy != ActualTy) { 486 auto *Cast = CastInst::CreateBitOrPointerCast(Arg, FormalTy, "", &CB); 487 CB.setArgOperand(ArgNo, Cast); 488 489 // Remove any incompatible attributes for the argument. 490 AttrBuilder ArgAttrs(CallerPAL.getParamAttributes(ArgNo)); 491 ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy)); 492 493 // If byval is used, this must be a pointer type, and the byval type must 494 // match the element type. Update it if present. 495 if (ArgAttrs.getByValType()) { 496 Type *NewTy = Callee->getParamByValType(ArgNo); 497 ArgAttrs.addByValAttr( 498 NewTy ? NewTy : cast<PointerType>(FormalTy)->getElementType()); 499 } 500 501 NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs)); 502 AttributeChanged = true; 503 } else 504 NewArgAttrs.push_back(CallerPAL.getParamAttributes(ArgNo)); 505 } 506 507 // If the return type of the call site doesn't match that of the callee, cast 508 // the returned value to the appropriate type. 509 // Remove any incompatible return value attribute. 510 AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex); 511 if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) { 512 createRetBitCast(CB, CallSiteRetTy, RetBitCast); 513 RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy)); 514 AttributeChanged = true; 515 } 516 517 // Set the new callsite attribute. 518 if (AttributeChanged) 519 CB.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttributes(), 520 AttributeSet::get(Ctx, RAttrs), 521 NewArgAttrs)); 522 523 return CB; 524 } 525 526 CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee, 527 MDNode *BranchWeights) { 528 529 // Version the indirect call site. If the called value is equal to the given 530 // callee, 'NewInst' will be executed, otherwise the original call site will 531 // be executed. 532 CallBase &NewInst = versionCallSite(CB, Callee, BranchWeights); 533 534 // Promote 'NewInst' so that it directly calls the desired function. 535 return promoteCall(NewInst, Callee); 536 } 537 538 bool llvm::tryPromoteCall(CallBase &CB) { 539 assert(!CB.getCalledFunction()); 540 Module *M = CB.getCaller()->getParent(); 541 const DataLayout &DL = M->getDataLayout(); 542 Value *Callee = CB.getCalledOperand(); 543 544 LoadInst *VTableEntryLoad = dyn_cast<LoadInst>(Callee); 545 if (!VTableEntryLoad) 546 return false; // Not a vtable entry load. 547 Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand(); 548 APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0); 549 Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets( 550 DL, VTableOffset, /* AllowNonInbounds */ true); 551 LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr); 552 if (!VTablePtrLoad) 553 return false; // Not a vtable load. 554 Value *Object = VTablePtrLoad->getPointerOperand(); 555 APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0); 556 Value *ObjectBase = Object->stripAndAccumulateConstantOffsets( 557 DL, ObjectOffset, /* AllowNonInbounds */ true); 558 if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0)) 559 // Not an Alloca or the offset isn't zero. 560 return false; 561 562 // Look for the vtable pointer store into the object by the ctor. 563 BasicBlock::iterator BBI(VTablePtrLoad); 564 Value *VTablePtr = FindAvailableLoadedValue( 565 VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr); 566 if (!VTablePtr) 567 return false; // No vtable found. 568 APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0); 569 Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets( 570 DL, VTableOffsetGVBase, /* AllowNonInbounds */ true); 571 GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase); 572 if (!(GV && GV->isConstant() && GV->hasDefinitiveInitializer())) 573 // Not in the form of a global constant variable with an initializer. 574 return false; 575 576 Constant *VTableGVInitializer = GV->getInitializer(); 577 APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset; 578 if (!(VTableGVOffset.getActiveBits() <= 64)) 579 return false; // Out of range. 580 Constant *Ptr = getPointerAtOffset(VTableGVInitializer, 581 VTableGVOffset.getZExtValue(), 582 *M); 583 if (!Ptr) 584 return false; // No constant (function) pointer found. 585 Function *DirectCallee = dyn_cast<Function>(Ptr->stripPointerCasts()); 586 if (!DirectCallee) 587 return false; // No function pointer found. 588 589 if (!isLegalToPromote(CB, DirectCallee)) 590 return false; 591 592 // Success. 593 promoteCall(CB, DirectCallee); 594 return true; 595 } 596 597 #undef DEBUG_TYPE 598