Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
35271 views
//===- CallPromotionUtils.cpp - Utilities for call promotion ----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements utilities useful for promoting indirect call sites to9// direct call sites.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Transforms/Utils/CallPromotionUtils.h"14#include "llvm/ADT/STLExtras.h"15#include "llvm/Analysis/Loads.h"16#include "llvm/Analysis/TypeMetadataUtils.h"17#include "llvm/IR/AttributeMask.h"18#include "llvm/IR/Constant.h"19#include "llvm/IR/IRBuilder.h"20#include "llvm/IR/Instructions.h"21#include "llvm/IR/Module.h"22#include "llvm/Transforms/Utils/BasicBlockUtils.h"2324using namespace llvm;2526#define DEBUG_TYPE "call-promotion-utils"2728/// Fix-up phi nodes in an invoke instruction's normal destination.29///30/// After versioning an invoke instruction, values coming from the original31/// block will now be coming from the "merge" block. For example, in the code32/// below:33///34/// then_bb:35/// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst36///37/// else_bb:38/// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst39///40/// merge_bb:41/// %t2 = phi i32 [ %t0, %then_bb ], [ %t1, %else_bb ]42/// br %normal_dst43///44/// normal_dst:45/// %t3 = phi i32 [ %x, %orig_bb ], ...46///47/// "orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in48/// "normal_dst" must be fixed to refer to "merge_bb":49///50/// normal_dst:51/// %t3 = phi i32 [ %x, %merge_bb ], ...52///53static void fixupPHINodeForNormalDest(InvokeInst *Invoke, BasicBlock *OrigBlock,54BasicBlock *MergeBlock) {55for (PHINode &Phi : Invoke->getNormalDest()->phis()) {56int Idx = Phi.getBasicBlockIndex(OrigBlock);57if (Idx == -1)58continue;59Phi.setIncomingBlock(Idx, MergeBlock);60}61}6263/// Fix-up phi nodes in an invoke instruction's unwind destination.64///65/// After versioning an invoke instruction, values coming from the original66/// block will now be coming from either the "then" block or the "else" block.67/// For example, in the code below:68///69/// then_bb:70/// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst71///72/// else_bb:73/// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst74///75/// unwind_dst:76/// %t3 = phi i32 [ %x, %orig_bb ], ...77///78/// "orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in79/// "unwind_dst" must be fixed to refer to "then_bb" and "else_bb":80///81/// unwind_dst:82/// %t3 = phi i32 [ %x, %then_bb ], [ %x, %else_bb ], ...83///84static void fixupPHINodeForUnwindDest(InvokeInst *Invoke, BasicBlock *OrigBlock,85BasicBlock *ThenBlock,86BasicBlock *ElseBlock) {87for (PHINode &Phi : Invoke->getUnwindDest()->phis()) {88int Idx = Phi.getBasicBlockIndex(OrigBlock);89if (Idx == -1)90continue;91auto *V = Phi.getIncomingValue(Idx);92Phi.setIncomingBlock(Idx, ThenBlock);93Phi.addIncoming(V, ElseBlock);94}95}9697/// Create a phi node for the returned value of a call or invoke instruction.98///99/// After versioning a call or invoke instruction that returns a value, we have100/// to merge the value of the original and new instructions. We do this by101/// creating a phi node and replacing uses of the original instruction with this102/// phi node.103///104/// For example, if \p OrigInst is defined in "else_bb" and \p NewInst is105/// defined in "then_bb", we create the following phi node:106///107/// ; Uses of the original instruction are replaced by uses of the phi node.108/// %t0 = phi i32 [ %orig_inst, %else_bb ], [ %new_inst, %then_bb ],109///110static void createRetPHINode(Instruction *OrigInst, Instruction *NewInst,111BasicBlock *MergeBlock, IRBuilder<> &Builder) {112113if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty())114return;115116Builder.SetInsertPoint(MergeBlock, MergeBlock->begin());117PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0);118SmallVector<User *, 16> UsersToUpdate(OrigInst->users());119for (User *U : UsersToUpdate)120U->replaceUsesOfWith(OrigInst, Phi);121Phi->addIncoming(OrigInst, OrigInst->getParent());122Phi->addIncoming(NewInst, NewInst->getParent());123}124125/// Cast a call or invoke instruction to the given type.126///127/// When promoting a call site, the return type of the call site might not match128/// that of the callee. If this is the case, we have to cast the returned value129/// to the correct type. The location of the cast depends on if we have a call130/// or invoke instruction.131///132/// For example, if the call instruction below requires a bitcast after133/// promotion:134///135/// orig_bb:136/// %t0 = call i32 @func()137/// ...138///139/// The bitcast is placed after the call instruction:140///141/// orig_bb:142/// ; Uses of the original return value are replaced by uses of the bitcast.143/// %t0 = call i32 @func()144/// %t1 = bitcast i32 %t0 to ...145/// ...146///147/// A similar transformation is performed for invoke instructions. However,148/// since invokes are terminating, a new block is created for the bitcast. For149/// example, if the invoke instruction below requires a bitcast after promotion:150///151/// orig_bb:152/// %t0 = invoke i32 @func() to label %normal_dst unwind label %unwind_dst153///154/// The edge between the original block and the invoke's normal destination is155/// split, and the bitcast is placed there:156///157/// orig_bb:158/// %t0 = invoke i32 @func() to label %split_bb unwind label %unwind_dst159///160/// split_bb:161/// ; Uses of the original return value are replaced by uses of the bitcast.162/// %t1 = bitcast i32 %t0 to ...163/// br label %normal_dst164///165static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {166167// Save the users of the calling instruction. These uses will be changed to168// use the bitcast after we create it.169SmallVector<User *, 16> UsersToUpdate(CB.users());170171// Determine an appropriate location to create the bitcast for the return172// value. The location depends on if we have a call or invoke instruction.173BasicBlock::iterator InsertBefore;174if (auto *Invoke = dyn_cast<InvokeInst>(&CB))175InsertBefore =176SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->begin();177else178InsertBefore = std::next(CB.getIterator());179180// Bitcast the return value to the correct type.181auto *Cast = CastInst::CreateBitOrPointerCast(&CB, RetTy, "", InsertBefore);182if (RetBitCast)183*RetBitCast = Cast;184185// Replace all the original uses of the calling instruction with the bitcast.186for (User *U : UsersToUpdate)187U->replaceUsesOfWith(&CB, Cast);188}189190/// Predicate and clone the given call site.191///192/// This function creates an if-then-else structure at the location of the call193/// site. The "if" condition is specified by `Cond`.194/// The original call site is moved into the "else" block, and a clone of the195/// call site is placed in the "then" block. The cloned instruction is 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 = Cond207/// br i1 %cond, %then_bb, %else_bb208///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_bb214///215/// else_bb:216/// ; The original call instruction is moved to the "else" block.217/// %t0 = call i32 %ptr()218/// br merge_bb219///220/// merge_bb:221/// ; Uses of the original call instruction are replaced by uses of the phi222/// ; 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, the228/// invoke instruction below:229///230/// orig_bb:231/// %t0 = invoke %ptr() to label %normal_dst unwind label %unwind_dst232///233/// Is replace by the following:234///235/// orig_bb:236/// %cond = Cond237/// br i1 %cond, %then_bb, %else_bb238///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 is242/// ; not yet promoted.243/// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst244///245/// else_bb:246/// ; The original invoke instruction is moved into the "else" block, and247/// ; its normal destination is set to the "merge" block.248/// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst249///250/// merge_bb:251/// ; Uses of the original invoke instruction are replaced by uses of the252/// ; phi node, and the merge block branches to the normal destination.253/// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]254/// br %normal_dst255///256/// An indirect musttail call is processed slightly differently in that:257/// 1. No merge block needed for the orginal and the cloned callsite, since258/// either one ends the flow. No phi node is needed either.259/// 2. The return statement following the original call site is duplicated too260/// 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 = Cond272/// br i1 %cond, %then_bb, %orig_bb273///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 %t1279///280/// orig_bb:281/// ; The original call instruction stays in its original block.282/// %t0 = musttail call i32 %ptr()283/// ret %t0284static CallBase &versionCallSiteWithCond(CallBase &CB, Value *Cond,285MDNode *BranchWeights) {286287IRBuilder<> Builder(&CB);288CallBase *OrigInst = &CB;289BasicBlock *OrigBlock = OrigInst->getParent();290291if (OrigInst->isMustTailCall()) {292// Create an if-then structure. The original instruction stays in its block,293// and a clone of the original instruction is placed in the "then" block.294Instruction *ThenTerm =295SplitBlockAndInsertIfThen(Cond, &CB, false, BranchWeights);296BasicBlock *ThenBlock = ThenTerm->getParent();297ThenBlock->setName("if.true.direct_targ");298CallBase *NewInst = cast<CallBase>(OrigInst->clone());299NewInst->insertBefore(ThenTerm);300301// Place a clone of the optional bitcast after the new call site.302Value *NewRetVal = NewInst;303auto Next = OrigInst->getNextNode();304if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) {305assert(BitCast->getOperand(0) == OrigInst &&306"bitcast following musttail call must use the call");307auto NewBitCast = BitCast->clone();308NewBitCast->replaceUsesOfWith(OrigInst, NewInst);309NewBitCast->insertBefore(ThenTerm);310NewRetVal = NewBitCast;311Next = BitCast->getNextNode();312}313314// Place a clone of the return instruction after the new call site.315ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);316assert(Ret && "musttail call must precede a ret with an optional bitcast");317auto NewRet = Ret->clone();318if (Ret->getReturnValue())319NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal);320NewRet->insertBefore(ThenTerm);321322// A return instructions is terminating, so we don't need the terminator323// instruction just created.324ThenTerm->eraseFromParent();325326return *NewInst;327}328329// Create an if-then-else structure. The original instruction is moved into330// the "else" block, and a clone of the original instruction is placed in the331// "then" block.332Instruction *ThenTerm = nullptr;333Instruction *ElseTerm = nullptr;334SplitBlockAndInsertIfThenElse(Cond, &CB, &ThenTerm, &ElseTerm, BranchWeights);335BasicBlock *ThenBlock = ThenTerm->getParent();336BasicBlock *ElseBlock = ElseTerm->getParent();337BasicBlock *MergeBlock = OrigInst->getParent();338339ThenBlock->setName("if.true.direct_targ");340ElseBlock->setName("if.false.orig_indirect");341MergeBlock->setName("if.end.icp");342343CallBase *NewInst = cast<CallBase>(OrigInst->clone());344OrigInst->moveBefore(ElseTerm);345NewInst->insertBefore(ThenTerm);346347// If the original call site is an invoke instruction, we have extra work to348// do since invoke instructions are terminating. We have to fix-up phi nodes349// in the invoke's normal and unwind destinations.350if (auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) {351auto *NewInvoke = cast<InvokeInst>(NewInst);352353// Invoke instructions are terminating, so we don't need the terminator354// instructions that were just created.355ThenTerm->eraseFromParent();356ElseTerm->eraseFromParent();357358// Branch from the "merge" block to the original normal destination.359Builder.SetInsertPoint(MergeBlock);360Builder.CreateBr(OrigInvoke->getNormalDest());361362// Fix-up phi nodes in the original invoke's normal and unwind destinations.363fixupPHINodeForNormalDest(OrigInvoke, OrigBlock, MergeBlock);364fixupPHINodeForUnwindDest(OrigInvoke, MergeBlock, ThenBlock, ElseBlock);365366// Now set the normal destinations of the invoke instructions to be the367// "merge" block.368OrigInvoke->setNormalDest(MergeBlock);369NewInvoke->setNormalDest(MergeBlock);370}371372// Create a phi node for the returned value of the call site.373createRetPHINode(OrigInst, NewInst, MergeBlock, Builder);374375return *NewInst;376}377378// Predicate and clone the given call site using condition `CB.callee ==379// Callee`. See the comment `versionCallSiteWithCond` for the transformation.380CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,381MDNode *BranchWeights) {382383IRBuilder<> Builder(&CB);384385// Create the compare. The called value and callee must have the same type to386// be compared.387if (CB.getCalledOperand()->getType() != Callee->getType())388Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType());389auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee);390391return versionCallSiteWithCond(CB, Cond, BranchWeights);392}393394bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,395const char **FailureReason) {396assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");397398auto &DL = Callee->getDataLayout();399400// Check the return type. The callee's return value type must be bitcast401// compatible with the call site's type.402Type *CallRetTy = CB.getType();403Type *FuncRetTy = Callee->getReturnType();404if (CallRetTy != FuncRetTy)405if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) {406if (FailureReason)407*FailureReason = "Return type mismatch";408return false;409}410411// The number of formal arguments of the callee.412unsigned NumParams = Callee->getFunctionType()->getNumParams();413414// The number of actual arguments in the call.415unsigned NumArgs = CB.arg_size();416417// Check the number of arguments. The callee and call site must agree on the418// number of arguments.419if (NumArgs != NumParams && !Callee->isVarArg()) {420if (FailureReason)421*FailureReason = "The number of arguments mismatch";422return false;423}424425// Check the argument types. The callee's formal argument types must be426// bitcast compatible with the corresponding actual argument types of the call427// site.428unsigned I = 0;429for (; I < NumParams; ++I) {430// Make sure that the callee and call agree on byval/inalloca. The types do431// not have to match.432if (Callee->hasParamAttribute(I, Attribute::ByVal) !=433CB.getAttributes().hasParamAttr(I, Attribute::ByVal)) {434if (FailureReason)435*FailureReason = "byval mismatch";436return false;437}438if (Callee->hasParamAttribute(I, Attribute::InAlloca) !=439CB.getAttributes().hasParamAttr(I, Attribute::InAlloca)) {440if (FailureReason)441*FailureReason = "inalloca mismatch";442return false;443}444445Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I);446Type *ActualTy = CB.getArgOperand(I)->getType();447if (FormalTy == ActualTy)448continue;449if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {450if (FailureReason)451*FailureReason = "Argument type mismatch";452return false;453}454455// MustTail call needs stricter type match. See456// Verifier::verifyMustTailCall().457if (CB.isMustTailCall()) {458PointerType *PF = dyn_cast<PointerType>(FormalTy);459PointerType *PA = dyn_cast<PointerType>(ActualTy);460if (!PF || !PA || PF->getAddressSpace() != PA->getAddressSpace()) {461if (FailureReason)462*FailureReason = "Musttail call Argument type mismatch";463return false;464}465}466}467for (; I < NumArgs; I++) {468// Vararg functions can have more arguments than parameters.469assert(Callee->isVarArg());470if (CB.paramHasAttr(I, Attribute::StructRet)) {471if (FailureReason)472*FailureReason = "SRet arg to vararg function";473return false;474}475}476477return true;478}479480CallBase &llvm::promoteCall(CallBase &CB, Function *Callee,481CastInst **RetBitCast) {482assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");483484// Set the called function of the call site to be the given callee (but don't485// change the type).486CB.setCalledOperand(Callee);487488// Since the call site will no longer be direct, we must clear metadata that489// is only appropriate for indirect calls. This includes !prof and !callees490// metadata.491CB.setMetadata(LLVMContext::MD_prof, nullptr);492CB.setMetadata(LLVMContext::MD_callees, nullptr);493494// If the function type of the call site matches that of the callee, no495// additional work is required.496if (CB.getFunctionType() == Callee->getFunctionType())497return CB;498499// Save the return types of the call site and callee.500Type *CallSiteRetTy = CB.getType();501Type *CalleeRetTy = Callee->getReturnType();502503// Change the function type of the call site the match that of the callee.504CB.mutateFunctionType(Callee->getFunctionType());505506// Inspect the arguments of the call site. If an argument's type doesn't507// match the corresponding formal argument's type in the callee, bitcast it508// to the correct type.509auto CalleeType = Callee->getFunctionType();510auto CalleeParamNum = CalleeType->getNumParams();511512LLVMContext &Ctx = Callee->getContext();513const AttributeList &CallerPAL = CB.getAttributes();514// The new list of argument attributes.515SmallVector<AttributeSet, 4> NewArgAttrs;516bool AttributeChanged = false;517518for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {519auto *Arg = CB.getArgOperand(ArgNo);520Type *FormalTy = CalleeType->getParamType(ArgNo);521Type *ActualTy = Arg->getType();522if (FormalTy != ActualTy) {523auto *Cast =524CastInst::CreateBitOrPointerCast(Arg, FormalTy, "", CB.getIterator());525CB.setArgOperand(ArgNo, Cast);526527// Remove any incompatible attributes for the argument.528AttrBuilder ArgAttrs(Ctx, CallerPAL.getParamAttrs(ArgNo));529ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy));530531// We may have a different byval/inalloca type.532if (ArgAttrs.getByValType())533ArgAttrs.addByValAttr(Callee->getParamByValType(ArgNo));534if (ArgAttrs.getInAllocaType())535ArgAttrs.addInAllocaAttr(Callee->getParamInAllocaType(ArgNo));536537NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs));538AttributeChanged = true;539} else540NewArgAttrs.push_back(CallerPAL.getParamAttrs(ArgNo));541}542543// If the return type of the call site doesn't match that of the callee, cast544// the returned value to the appropriate type.545// Remove any incompatible return value attribute.546AttrBuilder RAttrs(Ctx, CallerPAL.getRetAttrs());547if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) {548createRetBitCast(CB, CallSiteRetTy, RetBitCast);549RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy));550AttributeChanged = true;551}552553// Set the new callsite attribute.554if (AttributeChanged)555CB.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttrs(),556AttributeSet::get(Ctx, RAttrs),557NewArgAttrs));558559return CB;560}561562CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee,563MDNode *BranchWeights) {564565// Version the indirect call site. If the called value is equal to the given566// callee, 'NewInst' will be executed, otherwise the original call site will567// be executed.568CallBase &NewInst = versionCallSite(CB, Callee, BranchWeights);569570// Promote 'NewInst' so that it directly calls the desired function.571return promoteCall(NewInst, Callee);572}573574CallBase &llvm::promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr,575Function *Callee,576ArrayRef<Constant *> AddressPoints,577MDNode *BranchWeights) {578assert(!AddressPoints.empty() && "Caller should guarantee");579IRBuilder<> Builder(&CB);580SmallVector<Value *, 2> ICmps;581for (auto &AddressPoint : AddressPoints)582ICmps.push_back(Builder.CreateICmpEQ(VPtr, AddressPoint));583584// TODO: Perform tree height reduction if the number of ICmps is high.585Value *Cond = Builder.CreateOr(ICmps);586587// Version the indirect call site. If Cond is true, 'NewInst' will be588// executed, otherwise the original call site will be executed.589CallBase &NewInst = versionCallSiteWithCond(CB, Cond, BranchWeights);590591// Promote 'NewInst' so that it directly calls the desired function.592return promoteCall(NewInst, Callee);593}594595bool llvm::tryPromoteCall(CallBase &CB) {596assert(!CB.getCalledFunction());597Module *M = CB.getCaller()->getParent();598const DataLayout &DL = M->getDataLayout();599Value *Callee = CB.getCalledOperand();600601LoadInst *VTableEntryLoad = dyn_cast<LoadInst>(Callee);602if (!VTableEntryLoad)603return false; // Not a vtable entry load.604Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand();605APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0);606Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets(607DL, VTableOffset, /* AllowNonInbounds */ true);608LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);609if (!VTablePtrLoad)610return false; // Not a vtable load.611Value *Object = VTablePtrLoad->getPointerOperand();612APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0);613Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(614DL, ObjectOffset, /* AllowNonInbounds */ true);615if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))616// Not an Alloca or the offset isn't zero.617return false;618619// Look for the vtable pointer store into the object by the ctor.620BasicBlock::iterator BBI(VTablePtrLoad);621Value *VTablePtr = FindAvailableLoadedValue(622VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr);623if (!VTablePtr)624return false; // No vtable found.625APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0);626Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets(627DL, VTableOffsetGVBase, /* AllowNonInbounds */ true);628GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase);629if (!(GV && GV->isConstant() && GV->hasDefinitiveInitializer()))630// Not in the form of a global constant variable with an initializer.631return false;632633APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;634if (!(VTableGVOffset.getActiveBits() <= 64))635return false; // Out of range.636637Function *DirectCallee = nullptr;638std::tie(DirectCallee, std::ignore) =639getFunctionAtVTableOffset(GV, VTableGVOffset.getZExtValue(), *M);640if (!DirectCallee)641return false; // No function pointer found.642643if (!isLegalToPromote(CB, DirectCallee))644return false;645646// Success.647promoteCall(CB, DirectCallee);648return true;649}650651#undef DEBUG_TYPE652653654