xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- CallPromotionUtils.cpp - Utilities for call promotion ----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements utilities useful for promoting indirect call sites to
100b57cec5SDimitry Andric // direct call sites.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/Transforms/Utils/CallPromotionUtils.h"
15*0fca6ea1SDimitry Andric #include "llvm/ADT/STLExtras.h"
165ffd83dbSDimitry Andric #include "llvm/Analysis/Loads.h"
175ffd83dbSDimitry Andric #include "llvm/Analysis/TypeMetadataUtils.h"
1806c3fb27SDimitry Andric #include "llvm/IR/AttributeMask.h"
19*0fca6ea1SDimitry Andric #include "llvm/IR/Constant.h"
200b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
215ffd83dbSDimitry Andric #include "llvm/IR/Instructions.h"
22*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h"
230b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #define DEBUG_TYPE "call-promotion-utils"
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric /// Fix-up phi nodes in an invoke instruction's normal destination.
300b57cec5SDimitry Andric ///
310b57cec5SDimitry Andric /// After versioning an invoke instruction, values coming from the original
320b57cec5SDimitry Andric /// block will now be coming from the "merge" block. For example, in the code
330b57cec5SDimitry Andric /// below:
340b57cec5SDimitry Andric ///
350b57cec5SDimitry Andric ///   then_bb:
360b57cec5SDimitry Andric ///     %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
370b57cec5SDimitry Andric ///
380b57cec5SDimitry Andric ///   else_bb:
390b57cec5SDimitry Andric ///     %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
400b57cec5SDimitry Andric ///
410b57cec5SDimitry Andric ///   merge_bb:
420b57cec5SDimitry Andric ///     %t2 = phi i32 [ %t0, %then_bb ], [ %t1, %else_bb ]
430b57cec5SDimitry Andric ///     br %normal_dst
440b57cec5SDimitry Andric ///
450b57cec5SDimitry Andric ///   normal_dst:
460b57cec5SDimitry Andric ///     %t3 = phi i32 [ %x, %orig_bb ], ...
470b57cec5SDimitry Andric ///
480b57cec5SDimitry Andric /// "orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in
490b57cec5SDimitry Andric /// "normal_dst" must be fixed to refer to "merge_bb":
500b57cec5SDimitry Andric ///
510b57cec5SDimitry Andric ///    normal_dst:
520b57cec5SDimitry Andric ///      %t3 = phi i32 [ %x, %merge_bb ], ...
530b57cec5SDimitry Andric ///
540b57cec5SDimitry Andric static void fixupPHINodeForNormalDest(InvokeInst *Invoke, BasicBlock *OrigBlock,
550b57cec5SDimitry Andric                                       BasicBlock *MergeBlock) {
560b57cec5SDimitry Andric   for (PHINode &Phi : Invoke->getNormalDest()->phis()) {
570b57cec5SDimitry Andric     int Idx = Phi.getBasicBlockIndex(OrigBlock);
580b57cec5SDimitry Andric     if (Idx == -1)
590b57cec5SDimitry Andric       continue;
600b57cec5SDimitry Andric     Phi.setIncomingBlock(Idx, MergeBlock);
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric /// Fix-up phi nodes in an invoke instruction's unwind destination.
650b57cec5SDimitry Andric ///
660b57cec5SDimitry Andric /// After versioning an invoke instruction, values coming from the original
670b57cec5SDimitry Andric /// block will now be coming from either the "then" block or the "else" block.
680b57cec5SDimitry Andric /// For example, in the code below:
690b57cec5SDimitry Andric ///
700b57cec5SDimitry Andric ///   then_bb:
710b57cec5SDimitry Andric ///     %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
720b57cec5SDimitry Andric ///
730b57cec5SDimitry Andric ///   else_bb:
740b57cec5SDimitry Andric ///     %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
750b57cec5SDimitry Andric ///
760b57cec5SDimitry Andric ///   unwind_dst:
770b57cec5SDimitry Andric ///     %t3 = phi i32 [ %x, %orig_bb ], ...
780b57cec5SDimitry Andric ///
790b57cec5SDimitry Andric /// "orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in
800b57cec5SDimitry Andric /// "unwind_dst" must be fixed to refer to "then_bb" and "else_bb":
810b57cec5SDimitry Andric ///
820b57cec5SDimitry Andric ///   unwind_dst:
830b57cec5SDimitry Andric ///     %t3 = phi i32 [ %x, %then_bb ], [ %x, %else_bb ], ...
840b57cec5SDimitry Andric ///
850b57cec5SDimitry Andric static void fixupPHINodeForUnwindDest(InvokeInst *Invoke, BasicBlock *OrigBlock,
860b57cec5SDimitry Andric                                       BasicBlock *ThenBlock,
870b57cec5SDimitry Andric                                       BasicBlock *ElseBlock) {
880b57cec5SDimitry Andric   for (PHINode &Phi : Invoke->getUnwindDest()->phis()) {
890b57cec5SDimitry Andric     int Idx = Phi.getBasicBlockIndex(OrigBlock);
900b57cec5SDimitry Andric     if (Idx == -1)
910b57cec5SDimitry Andric       continue;
920b57cec5SDimitry Andric     auto *V = Phi.getIncomingValue(Idx);
930b57cec5SDimitry Andric     Phi.setIncomingBlock(Idx, ThenBlock);
940b57cec5SDimitry Andric     Phi.addIncoming(V, ElseBlock);
950b57cec5SDimitry Andric   }
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric /// Create a phi node for the returned value of a call or invoke instruction.
990b57cec5SDimitry Andric ///
1000b57cec5SDimitry Andric /// After versioning a call or invoke instruction that returns a value, we have
1010b57cec5SDimitry Andric /// to merge the value of the original and new instructions. We do this by
1020b57cec5SDimitry Andric /// creating a phi node and replacing uses of the original instruction with this
1030b57cec5SDimitry Andric /// phi node.
1040b57cec5SDimitry Andric ///
1050b57cec5SDimitry Andric /// For example, if \p OrigInst is defined in "else_bb" and \p NewInst is
1060b57cec5SDimitry Andric /// defined in "then_bb", we create the following phi node:
1070b57cec5SDimitry Andric ///
1080b57cec5SDimitry Andric ///   ; Uses of the original instruction are replaced by uses of the phi node.
1090b57cec5SDimitry Andric ///   %t0 = phi i32 [ %orig_inst, %else_bb ], [ %new_inst, %then_bb ],
1100b57cec5SDimitry Andric ///
1110b57cec5SDimitry Andric static void createRetPHINode(Instruction *OrigInst, Instruction *NewInst,
1120b57cec5SDimitry Andric                              BasicBlock *MergeBlock, IRBuilder<> &Builder) {
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty())
1150b57cec5SDimitry Andric     return;
1160b57cec5SDimitry Andric 
1175f757f3fSDimitry Andric   Builder.SetInsertPoint(MergeBlock, MergeBlock->begin());
1180b57cec5SDimitry Andric   PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0);
119e8d8bef9SDimitry Andric   SmallVector<User *, 16> UsersToUpdate(OrigInst->users());
1200b57cec5SDimitry Andric   for (User *U : UsersToUpdate)
1210b57cec5SDimitry Andric     U->replaceUsesOfWith(OrigInst, Phi);
1220b57cec5SDimitry Andric   Phi->addIncoming(OrigInst, OrigInst->getParent());
1230b57cec5SDimitry Andric   Phi->addIncoming(NewInst, NewInst->getParent());
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric /// Cast a call or invoke instruction to the given type.
1270b57cec5SDimitry Andric ///
1280b57cec5SDimitry Andric /// When promoting a call site, the return type of the call site might not match
1290b57cec5SDimitry Andric /// that of the callee. If this is the case, we have to cast the returned value
1300b57cec5SDimitry Andric /// to the correct type. The location of the cast depends on if we have a call
1310b57cec5SDimitry Andric /// or invoke instruction.
1320b57cec5SDimitry Andric ///
1330b57cec5SDimitry Andric /// For example, if the call instruction below requires a bitcast after
1340b57cec5SDimitry Andric /// promotion:
1350b57cec5SDimitry Andric ///
1360b57cec5SDimitry Andric ///   orig_bb:
1370b57cec5SDimitry Andric ///     %t0 = call i32 @func()
1380b57cec5SDimitry Andric ///     ...
1390b57cec5SDimitry Andric ///
1400b57cec5SDimitry Andric /// The bitcast is placed after the call instruction:
1410b57cec5SDimitry Andric ///
1420b57cec5SDimitry Andric ///   orig_bb:
1430b57cec5SDimitry Andric ///     ; Uses of the original return value are replaced by uses of the bitcast.
1440b57cec5SDimitry Andric ///     %t0 = call i32 @func()
1450b57cec5SDimitry Andric ///     %t1 = bitcast i32 %t0 to ...
1460b57cec5SDimitry Andric ///     ...
1470b57cec5SDimitry Andric ///
1480b57cec5SDimitry Andric /// A similar transformation is performed for invoke instructions. However,
1490b57cec5SDimitry Andric /// since invokes are terminating, a new block is created for the bitcast. For
1500b57cec5SDimitry Andric /// example, if the invoke instruction below requires a bitcast after promotion:
1510b57cec5SDimitry Andric ///
1520b57cec5SDimitry Andric ///   orig_bb:
1530b57cec5SDimitry Andric ///     %t0 = invoke i32 @func() to label %normal_dst unwind label %unwind_dst
1540b57cec5SDimitry Andric ///
1550b57cec5SDimitry Andric /// The edge between the original block and the invoke's normal destination is
1560b57cec5SDimitry Andric /// split, and the bitcast is placed there:
1570b57cec5SDimitry Andric ///
1580b57cec5SDimitry Andric ///   orig_bb:
1590b57cec5SDimitry Andric ///     %t0 = invoke i32 @func() to label %split_bb unwind label %unwind_dst
1600b57cec5SDimitry Andric ///
1610b57cec5SDimitry Andric ///   split_bb:
1620b57cec5SDimitry Andric ///     ; Uses of the original return value are replaced by uses of the bitcast.
1630b57cec5SDimitry Andric ///     %t1 = bitcast i32 %t0 to ...
1640b57cec5SDimitry Andric ///     br label %normal_dst
1650b57cec5SDimitry Andric ///
1665ffd83dbSDimitry Andric static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   // Save the users of the calling instruction. These uses will be changed to
1690b57cec5SDimitry Andric   // use the bitcast after we create it.
170e8d8bef9SDimitry Andric   SmallVector<User *, 16> UsersToUpdate(CB.users());
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   // Determine an appropriate location to create the bitcast for the return
1730b57cec5SDimitry Andric   // value. The location depends on if we have a call or invoke instruction.
174*0fca6ea1SDimitry Andric   BasicBlock::iterator InsertBefore;
1755ffd83dbSDimitry Andric   if (auto *Invoke = dyn_cast<InvokeInst>(&CB))
1760b57cec5SDimitry Andric     InsertBefore =
177*0fca6ea1SDimitry Andric         SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->begin();
1780b57cec5SDimitry Andric   else
179*0fca6ea1SDimitry Andric     InsertBefore = std::next(CB.getIterator());
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // Bitcast the return value to the correct type.
1825ffd83dbSDimitry Andric   auto *Cast = CastInst::CreateBitOrPointerCast(&CB, RetTy, "", InsertBefore);
1830b57cec5SDimitry Andric   if (RetBitCast)
1840b57cec5SDimitry Andric     *RetBitCast = Cast;
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   // Replace all the original uses of the calling instruction with the bitcast.
1870b57cec5SDimitry Andric   for (User *U : UsersToUpdate)
1885ffd83dbSDimitry Andric     U->replaceUsesOfWith(&CB, Cast);
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric /// Predicate and clone the given call site.
1920b57cec5SDimitry Andric ///
1930b57cec5SDimitry Andric /// This function creates an if-then-else structure at the location of the call
194*0fca6ea1SDimitry Andric /// site. The "if" condition is specified by `Cond`.
195*0fca6ea1SDimitry Andric /// The original call site is moved into the "else" block, and a clone of the
196*0fca6ea1SDimitry Andric /// call site is placed in the "then" block. The cloned instruction is returned.
1970b57cec5SDimitry Andric ///
1980b57cec5SDimitry Andric /// For example, the call instruction below:
1990b57cec5SDimitry Andric ///
2000b57cec5SDimitry Andric ///   orig_bb:
2010b57cec5SDimitry Andric ///     %t0 = call i32 %ptr()
2020b57cec5SDimitry Andric ///     ...
2030b57cec5SDimitry Andric ///
2040b57cec5SDimitry Andric /// Is replace by the following:
2050b57cec5SDimitry Andric ///
2060b57cec5SDimitry Andric ///   orig_bb:
207*0fca6ea1SDimitry Andric ///     %cond = Cond
2080b57cec5SDimitry Andric ///     br i1 %cond, %then_bb, %else_bb
2090b57cec5SDimitry Andric ///
2100b57cec5SDimitry Andric ///   then_bb:
2110b57cec5SDimitry Andric ///     ; The clone of the original call instruction is placed in the "then"
2120b57cec5SDimitry Andric ///     ; block. It is not yet promoted.
2130b57cec5SDimitry Andric ///     %t1 = call i32 %ptr()
2140b57cec5SDimitry Andric ///     br merge_bb
2150b57cec5SDimitry Andric ///
2160b57cec5SDimitry Andric ///   else_bb:
2170b57cec5SDimitry Andric ///     ; The original call instruction is moved to the "else" block.
2180b57cec5SDimitry Andric ///     %t0 = call i32 %ptr()
2190b57cec5SDimitry Andric ///     br merge_bb
2200b57cec5SDimitry Andric ///
2210b57cec5SDimitry Andric ///   merge_bb:
2220b57cec5SDimitry Andric ///     ; Uses of the original call instruction are replaced by uses of the phi
2230b57cec5SDimitry Andric ///     ; node.
2240b57cec5SDimitry Andric ///     %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]
2250b57cec5SDimitry Andric ///     ...
2260b57cec5SDimitry Andric ///
2270b57cec5SDimitry Andric /// A similar transformation is performed for invoke instructions. However,
2280b57cec5SDimitry Andric /// since invokes are terminating, more work is required. For example, the
2290b57cec5SDimitry Andric /// invoke instruction below:
2300b57cec5SDimitry Andric ///
2310b57cec5SDimitry Andric ///   orig_bb:
2320b57cec5SDimitry Andric ///     %t0 = invoke %ptr() to label %normal_dst unwind label %unwind_dst
2330b57cec5SDimitry Andric ///
2340b57cec5SDimitry Andric /// Is replace by the following:
2350b57cec5SDimitry Andric ///
2360b57cec5SDimitry Andric ///   orig_bb:
237*0fca6ea1SDimitry Andric ///     %cond = Cond
2380b57cec5SDimitry Andric ///     br i1 %cond, %then_bb, %else_bb
2390b57cec5SDimitry Andric ///
2400b57cec5SDimitry Andric ///   then_bb:
2410b57cec5SDimitry Andric ///     ; The clone of the original invoke instruction is placed in the "then"
2420b57cec5SDimitry Andric ///     ; block, and its normal destination is set to the "merge" block. It is
2430b57cec5SDimitry Andric ///     ; not yet promoted.
2440b57cec5SDimitry Andric ///     %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
2450b57cec5SDimitry Andric ///
2460b57cec5SDimitry Andric ///   else_bb:
2470b57cec5SDimitry Andric ///     ; The original invoke instruction is moved into the "else" block, and
2480b57cec5SDimitry Andric ///     ; its normal destination is set to the "merge" block.
2490b57cec5SDimitry Andric ///     %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
2500b57cec5SDimitry Andric ///
2510b57cec5SDimitry Andric ///   merge_bb:
2520b57cec5SDimitry Andric ///     ; Uses of the original invoke instruction are replaced by uses of the
2530b57cec5SDimitry Andric ///     ; phi node, and the merge block branches to the normal destination.
2540b57cec5SDimitry Andric ///     %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]
2550b57cec5SDimitry Andric ///     br %normal_dst
2560b57cec5SDimitry Andric ///
2575ffd83dbSDimitry Andric /// An indirect musttail call is processed slightly differently in that:
2585ffd83dbSDimitry Andric /// 1. No merge block needed for the orginal and the cloned callsite, since
2595ffd83dbSDimitry Andric ///    either one ends the flow. No phi node is needed either.
2605ffd83dbSDimitry Andric /// 2. The return statement following the original call site is duplicated too
2615ffd83dbSDimitry Andric ///    and placed immediately after the cloned call site per the IR convention.
2625ffd83dbSDimitry Andric ///
2635ffd83dbSDimitry Andric /// For example, the musttail call instruction below:
2645ffd83dbSDimitry Andric ///
2655ffd83dbSDimitry Andric ///   orig_bb:
2665ffd83dbSDimitry Andric ///     %t0 = musttail call i32 %ptr()
2675ffd83dbSDimitry Andric ///     ...
2685ffd83dbSDimitry Andric ///
2695ffd83dbSDimitry Andric /// Is replaced by the following:
2705ffd83dbSDimitry Andric ///
2715ffd83dbSDimitry Andric ///   cond_bb:
272*0fca6ea1SDimitry Andric ///     %cond = Cond
2735ffd83dbSDimitry Andric ///     br i1 %cond, %then_bb, %orig_bb
2745ffd83dbSDimitry Andric ///
2755ffd83dbSDimitry Andric ///   then_bb:
2765ffd83dbSDimitry Andric ///     ; The clone of the original call instruction is placed in the "then"
2775ffd83dbSDimitry Andric ///     ; block. It is not yet promoted.
2785ffd83dbSDimitry Andric ///     %t1 = musttail call i32 %ptr()
2795ffd83dbSDimitry Andric ///     ret %t1
2805ffd83dbSDimitry Andric ///
2815ffd83dbSDimitry Andric ///   orig_bb:
2825ffd83dbSDimitry Andric ///     ; The original call instruction stays in its original block.
2835ffd83dbSDimitry Andric ///     %t0 = musttail call i32 %ptr()
2845ffd83dbSDimitry Andric ///     ret %t0
285*0fca6ea1SDimitry Andric static CallBase &versionCallSiteWithCond(CallBase &CB, Value *Cond,
2860b57cec5SDimitry Andric                                          MDNode *BranchWeights) {
2870b57cec5SDimitry Andric 
2885ffd83dbSDimitry Andric   IRBuilder<> Builder(&CB);
2895ffd83dbSDimitry Andric   CallBase *OrigInst = &CB;
2900b57cec5SDimitry Andric   BasicBlock *OrigBlock = OrigInst->getParent();
2910b57cec5SDimitry Andric 
2925ffd83dbSDimitry Andric   if (OrigInst->isMustTailCall()) {
2935ffd83dbSDimitry Andric     // Create an if-then structure. The original instruction stays in its block,
2945ffd83dbSDimitry Andric     // and a clone of the original instruction is placed in the "then" block.
2955ffd83dbSDimitry Andric     Instruction *ThenTerm =
2965ffd83dbSDimitry Andric         SplitBlockAndInsertIfThen(Cond, &CB, false, BranchWeights);
2975ffd83dbSDimitry Andric     BasicBlock *ThenBlock = ThenTerm->getParent();
2985ffd83dbSDimitry Andric     ThenBlock->setName("if.true.direct_targ");
2995ffd83dbSDimitry Andric     CallBase *NewInst = cast<CallBase>(OrigInst->clone());
3005ffd83dbSDimitry Andric     NewInst->insertBefore(ThenTerm);
3015ffd83dbSDimitry Andric 
3025ffd83dbSDimitry Andric     // Place a clone of the optional bitcast after the new call site.
3035ffd83dbSDimitry Andric     Value *NewRetVal = NewInst;
3045ffd83dbSDimitry Andric     auto Next = OrigInst->getNextNode();
3055ffd83dbSDimitry Andric     if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) {
3065ffd83dbSDimitry Andric       assert(BitCast->getOperand(0) == OrigInst &&
3075ffd83dbSDimitry Andric              "bitcast following musttail call must use the call");
3085ffd83dbSDimitry Andric       auto NewBitCast = BitCast->clone();
3095ffd83dbSDimitry Andric       NewBitCast->replaceUsesOfWith(OrigInst, NewInst);
3105ffd83dbSDimitry Andric       NewBitCast->insertBefore(ThenTerm);
3115ffd83dbSDimitry Andric       NewRetVal = NewBitCast;
3125ffd83dbSDimitry Andric       Next = BitCast->getNextNode();
3135ffd83dbSDimitry Andric     }
3145ffd83dbSDimitry Andric 
3155ffd83dbSDimitry Andric     // Place a clone of the return instruction after the new call site.
3165ffd83dbSDimitry Andric     ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3175ffd83dbSDimitry Andric     assert(Ret && "musttail call must precede a ret with an optional bitcast");
3185ffd83dbSDimitry Andric     auto NewRet = Ret->clone();
3195ffd83dbSDimitry Andric     if (Ret->getReturnValue())
3205ffd83dbSDimitry Andric       NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal);
3215ffd83dbSDimitry Andric     NewRet->insertBefore(ThenTerm);
3225ffd83dbSDimitry Andric 
3235ffd83dbSDimitry Andric     // A return instructions is terminating, so we don't need the terminator
3245ffd83dbSDimitry Andric     // instruction just created.
3255ffd83dbSDimitry Andric     ThenTerm->eraseFromParent();
3265ffd83dbSDimitry Andric 
3275ffd83dbSDimitry Andric     return *NewInst;
3285ffd83dbSDimitry Andric   }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   // Create an if-then-else structure. The original instruction is moved into
3310b57cec5SDimitry Andric   // the "else" block, and a clone of the original instruction is placed in the
3320b57cec5SDimitry Andric   // "then" block.
3330b57cec5SDimitry Andric   Instruction *ThenTerm = nullptr;
3340b57cec5SDimitry Andric   Instruction *ElseTerm = nullptr;
3355ffd83dbSDimitry Andric   SplitBlockAndInsertIfThenElse(Cond, &CB, &ThenTerm, &ElseTerm, BranchWeights);
3360b57cec5SDimitry Andric   BasicBlock *ThenBlock = ThenTerm->getParent();
3370b57cec5SDimitry Andric   BasicBlock *ElseBlock = ElseTerm->getParent();
3380b57cec5SDimitry Andric   BasicBlock *MergeBlock = OrigInst->getParent();
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   ThenBlock->setName("if.true.direct_targ");
3410b57cec5SDimitry Andric   ElseBlock->setName("if.false.orig_indirect");
3420b57cec5SDimitry Andric   MergeBlock->setName("if.end.icp");
3430b57cec5SDimitry Andric 
3445ffd83dbSDimitry Andric   CallBase *NewInst = cast<CallBase>(OrigInst->clone());
3450b57cec5SDimitry Andric   OrigInst->moveBefore(ElseTerm);
3460b57cec5SDimitry Andric   NewInst->insertBefore(ThenTerm);
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // If the original call site is an invoke instruction, we have extra work to
3490b57cec5SDimitry Andric   // do since invoke instructions are terminating. We have to fix-up phi nodes
3500b57cec5SDimitry Andric   // in the invoke's normal and unwind destinations.
3510b57cec5SDimitry Andric   if (auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) {
3520b57cec5SDimitry Andric     auto *NewInvoke = cast<InvokeInst>(NewInst);
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric     // Invoke instructions are terminating, so we don't need the terminator
3550b57cec5SDimitry Andric     // instructions that were just created.
3560b57cec5SDimitry Andric     ThenTerm->eraseFromParent();
3570b57cec5SDimitry Andric     ElseTerm->eraseFromParent();
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     // Branch from the "merge" block to the original normal destination.
3600b57cec5SDimitry Andric     Builder.SetInsertPoint(MergeBlock);
3610b57cec5SDimitry Andric     Builder.CreateBr(OrigInvoke->getNormalDest());
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric     // Fix-up phi nodes in the original invoke's normal and unwind destinations.
3640b57cec5SDimitry Andric     fixupPHINodeForNormalDest(OrigInvoke, OrigBlock, MergeBlock);
3650b57cec5SDimitry Andric     fixupPHINodeForUnwindDest(OrigInvoke, MergeBlock, ThenBlock, ElseBlock);
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric     // Now set the normal destinations of the invoke instructions to be the
3680b57cec5SDimitry Andric     // "merge" block.
3690b57cec5SDimitry Andric     OrigInvoke->setNormalDest(MergeBlock);
3700b57cec5SDimitry Andric     NewInvoke->setNormalDest(MergeBlock);
3710b57cec5SDimitry Andric   }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   // Create a phi node for the returned value of the call site.
3740b57cec5SDimitry Andric   createRetPHINode(OrigInst, NewInst, MergeBlock, Builder);
3750b57cec5SDimitry Andric 
3765ffd83dbSDimitry Andric   return *NewInst;
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric 
379*0fca6ea1SDimitry Andric // Predicate and clone the given call site using condition `CB.callee ==
380*0fca6ea1SDimitry Andric // Callee`. See the comment `versionCallSiteWithCond` for the transformation.
381*0fca6ea1SDimitry Andric CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
382*0fca6ea1SDimitry Andric                                 MDNode *BranchWeights) {
383*0fca6ea1SDimitry Andric 
384*0fca6ea1SDimitry Andric   IRBuilder<> Builder(&CB);
385*0fca6ea1SDimitry Andric 
386*0fca6ea1SDimitry Andric   // Create the compare. The called value and callee must have the same type to
387*0fca6ea1SDimitry Andric   // be compared.
388*0fca6ea1SDimitry Andric   if (CB.getCalledOperand()->getType() != Callee->getType())
389*0fca6ea1SDimitry Andric     Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType());
390*0fca6ea1SDimitry Andric   auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee);
391*0fca6ea1SDimitry Andric 
392*0fca6ea1SDimitry Andric   return versionCallSiteWithCond(CB, Cond, BranchWeights);
393*0fca6ea1SDimitry Andric }
394*0fca6ea1SDimitry Andric 
3955ffd83dbSDimitry Andric bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
3960b57cec5SDimitry Andric                             const char **FailureReason) {
3975ffd83dbSDimitry Andric   assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");
3980b57cec5SDimitry Andric 
399*0fca6ea1SDimitry Andric   auto &DL = Callee->getDataLayout();
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric   // Check the return type. The callee's return value type must be bitcast
4020b57cec5SDimitry Andric   // compatible with the call site's type.
4035ffd83dbSDimitry Andric   Type *CallRetTy = CB.getType();
4040b57cec5SDimitry Andric   Type *FuncRetTy = Callee->getReturnType();
4050b57cec5SDimitry Andric   if (CallRetTy != FuncRetTy)
4060b57cec5SDimitry Andric     if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) {
4070b57cec5SDimitry Andric       if (FailureReason)
4080b57cec5SDimitry Andric         *FailureReason = "Return type mismatch";
4090b57cec5SDimitry Andric       return false;
4100b57cec5SDimitry Andric     }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   // The number of formal arguments of the callee.
4130b57cec5SDimitry Andric   unsigned NumParams = Callee->getFunctionType()->getNumParams();
4140b57cec5SDimitry Andric 
4155ffd83dbSDimitry Andric   // The number of actual arguments in the call.
4165ffd83dbSDimitry Andric   unsigned NumArgs = CB.arg_size();
4175ffd83dbSDimitry Andric 
4180b57cec5SDimitry Andric   // Check the number of arguments. The callee and call site must agree on the
4190b57cec5SDimitry Andric   // number of arguments.
4205ffd83dbSDimitry Andric   if (NumArgs != NumParams && !Callee->isVarArg()) {
4210b57cec5SDimitry Andric     if (FailureReason)
4220b57cec5SDimitry Andric       *FailureReason = "The number of arguments mismatch";
4230b57cec5SDimitry Andric     return false;
4240b57cec5SDimitry Andric   }
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   // Check the argument types. The callee's formal argument types must be
4270b57cec5SDimitry Andric   // bitcast compatible with the corresponding actual argument types of the call
4280b57cec5SDimitry Andric   // site.
4295ffd83dbSDimitry Andric   unsigned I = 0;
4305ffd83dbSDimitry Andric   for (; I < NumParams; ++I) {
431349cc55cSDimitry Andric     // Make sure that the callee and call agree on byval/inalloca. The types do
432349cc55cSDimitry Andric     // not have to match.
433349cc55cSDimitry Andric     if (Callee->hasParamAttribute(I, Attribute::ByVal) !=
434349cc55cSDimitry Andric         CB.getAttributes().hasParamAttr(I, Attribute::ByVal)) {
435349cc55cSDimitry Andric       if (FailureReason)
436349cc55cSDimitry Andric         *FailureReason = "byval mismatch";
437349cc55cSDimitry Andric       return false;
438349cc55cSDimitry Andric     }
439349cc55cSDimitry Andric     if (Callee->hasParamAttribute(I, Attribute::InAlloca) !=
440349cc55cSDimitry Andric         CB.getAttributes().hasParamAttr(I, Attribute::InAlloca)) {
441349cc55cSDimitry Andric       if (FailureReason)
442349cc55cSDimitry Andric         *FailureReason = "inalloca mismatch";
443349cc55cSDimitry Andric       return false;
444349cc55cSDimitry Andric     }
445bdd1243dSDimitry Andric 
446bdd1243dSDimitry Andric     Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I);
447bdd1243dSDimitry Andric     Type *ActualTy = CB.getArgOperand(I)->getType();
448bdd1243dSDimitry Andric     if (FormalTy == ActualTy)
449bdd1243dSDimitry Andric       continue;
450bdd1243dSDimitry Andric     if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {
451bdd1243dSDimitry Andric       if (FailureReason)
452bdd1243dSDimitry Andric         *FailureReason = "Argument type mismatch";
453bdd1243dSDimitry Andric       return false;
454bdd1243dSDimitry Andric     }
455bdd1243dSDimitry Andric 
456bdd1243dSDimitry Andric     // MustTail call needs stricter type match. See
457bdd1243dSDimitry Andric     // Verifier::verifyMustTailCall().
458bdd1243dSDimitry Andric     if (CB.isMustTailCall()) {
459bdd1243dSDimitry Andric       PointerType *PF = dyn_cast<PointerType>(FormalTy);
460bdd1243dSDimitry Andric       PointerType *PA = dyn_cast<PointerType>(ActualTy);
461bdd1243dSDimitry Andric       if (!PF || !PA || PF->getAddressSpace() != PA->getAddressSpace()) {
462bdd1243dSDimitry Andric         if (FailureReason)
463bdd1243dSDimitry Andric           *FailureReason = "Musttail call Argument type mismatch";
464bdd1243dSDimitry Andric         return false;
465bdd1243dSDimitry Andric       }
466bdd1243dSDimitry Andric     }
4670b57cec5SDimitry Andric   }
4685ffd83dbSDimitry Andric   for (; I < NumArgs; I++) {
469e8d8bef9SDimitry Andric     // Vararg functions can have more arguments than parameters.
4705ffd83dbSDimitry Andric     assert(Callee->isVarArg());
4715ffd83dbSDimitry Andric     if (CB.paramHasAttr(I, Attribute::StructRet)) {
472e8d8bef9SDimitry Andric       if (FailureReason)
4735ffd83dbSDimitry Andric         *FailureReason = "SRet arg to vararg function";
4745ffd83dbSDimitry Andric       return false;
4755ffd83dbSDimitry Andric     }
4765ffd83dbSDimitry Andric   }
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   return true;
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4815ffd83dbSDimitry Andric CallBase &llvm::promoteCall(CallBase &CB, Function *Callee,
4820b57cec5SDimitry Andric                             CastInst **RetBitCast) {
4835ffd83dbSDimitry Andric   assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   // Set the called function of the call site to be the given callee (but don't
4860b57cec5SDimitry Andric   // change the type).
4875ffd83dbSDimitry Andric   CB.setCalledOperand(Callee);
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   // Since the call site will no longer be direct, we must clear metadata that
4900b57cec5SDimitry Andric   // is only appropriate for indirect calls. This includes !prof and !callees
4910b57cec5SDimitry Andric   // metadata.
4925ffd83dbSDimitry Andric   CB.setMetadata(LLVMContext::MD_prof, nullptr);
4935ffd83dbSDimitry Andric   CB.setMetadata(LLVMContext::MD_callees, nullptr);
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   // If the function type of the call site matches that of the callee, no
4960b57cec5SDimitry Andric   // additional work is required.
4975ffd83dbSDimitry Andric   if (CB.getFunctionType() == Callee->getFunctionType())
4985ffd83dbSDimitry Andric     return CB;
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric   // Save the return types of the call site and callee.
5015ffd83dbSDimitry Andric   Type *CallSiteRetTy = CB.getType();
5020b57cec5SDimitry Andric   Type *CalleeRetTy = Callee->getReturnType();
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   // Change the function type of the call site the match that of the callee.
5055ffd83dbSDimitry Andric   CB.mutateFunctionType(Callee->getFunctionType());
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric   // Inspect the arguments of the call site. If an argument's type doesn't
5080b57cec5SDimitry Andric   // match the corresponding formal argument's type in the callee, bitcast it
5090b57cec5SDimitry Andric   // to the correct type.
5100b57cec5SDimitry Andric   auto CalleeType = Callee->getFunctionType();
5110b57cec5SDimitry Andric   auto CalleeParamNum = CalleeType->getNumParams();
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric   LLVMContext &Ctx = Callee->getContext();
5145ffd83dbSDimitry Andric   const AttributeList &CallerPAL = CB.getAttributes();
5150b57cec5SDimitry Andric   // The new list of argument attributes.
5160b57cec5SDimitry Andric   SmallVector<AttributeSet, 4> NewArgAttrs;
5170b57cec5SDimitry Andric   bool AttributeChanged = false;
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
5205ffd83dbSDimitry Andric     auto *Arg = CB.getArgOperand(ArgNo);
5210b57cec5SDimitry Andric     Type *FormalTy = CalleeType->getParamType(ArgNo);
5220b57cec5SDimitry Andric     Type *ActualTy = Arg->getType();
5230b57cec5SDimitry Andric     if (FormalTy != ActualTy) {
524*0fca6ea1SDimitry Andric       auto *Cast =
525*0fca6ea1SDimitry Andric           CastInst::CreateBitOrPointerCast(Arg, FormalTy, "", CB.getIterator());
5265ffd83dbSDimitry Andric       CB.setArgOperand(ArgNo, Cast);
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric       // Remove any incompatible attributes for the argument.
52904eeddc0SDimitry Andric       AttrBuilder ArgAttrs(Ctx, CallerPAL.getParamAttrs(ArgNo));
5300b57cec5SDimitry Andric       ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy));
5310b57cec5SDimitry Andric 
532349cc55cSDimitry Andric       // We may have a different byval/inalloca type.
533fe6060f1SDimitry Andric       if (ArgAttrs.getByValType())
534fe6060f1SDimitry Andric         ArgAttrs.addByValAttr(Callee->getParamByValType(ArgNo));
535349cc55cSDimitry Andric       if (ArgAttrs.getInAllocaType())
536349cc55cSDimitry Andric         ArgAttrs.addInAllocaAttr(Callee->getParamInAllocaType(ArgNo));
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric       NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs));
5390b57cec5SDimitry Andric       AttributeChanged = true;
5400b57cec5SDimitry Andric     } else
541349cc55cSDimitry Andric       NewArgAttrs.push_back(CallerPAL.getParamAttrs(ArgNo));
5420b57cec5SDimitry Andric   }
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric   // If the return type of the call site doesn't match that of the callee, cast
5450b57cec5SDimitry Andric   // the returned value to the appropriate type.
5460b57cec5SDimitry Andric   // Remove any incompatible return value attribute.
54704eeddc0SDimitry Andric   AttrBuilder RAttrs(Ctx, CallerPAL.getRetAttrs());
5480b57cec5SDimitry Andric   if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) {
5495ffd83dbSDimitry Andric     createRetBitCast(CB, CallSiteRetTy, RetBitCast);
5500b57cec5SDimitry Andric     RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy));
5510b57cec5SDimitry Andric     AttributeChanged = true;
5520b57cec5SDimitry Andric   }
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric   // Set the new callsite attribute.
5550b57cec5SDimitry Andric   if (AttributeChanged)
556349cc55cSDimitry Andric     CB.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttrs(),
5570b57cec5SDimitry Andric                                         AttributeSet::get(Ctx, RAttrs),
5580b57cec5SDimitry Andric                                         NewArgAttrs));
5590b57cec5SDimitry Andric 
5605ffd83dbSDimitry Andric   return CB;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric 
5635ffd83dbSDimitry Andric CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
5640b57cec5SDimitry Andric                                           MDNode *BranchWeights) {
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric   // Version the indirect call site. If the called value is equal to the given
5670b57cec5SDimitry Andric   // callee, 'NewInst' will be executed, otherwise the original call site will
5680b57cec5SDimitry Andric   // be executed.
5695ffd83dbSDimitry Andric   CallBase &NewInst = versionCallSite(CB, Callee, BranchWeights);
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   // Promote 'NewInst' so that it directly calls the desired function.
5725ffd83dbSDimitry Andric   return promoteCall(NewInst, Callee);
5735ffd83dbSDimitry Andric }
5745ffd83dbSDimitry Andric 
575*0fca6ea1SDimitry Andric CallBase &llvm::promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr,
576*0fca6ea1SDimitry Andric                                          Function *Callee,
577*0fca6ea1SDimitry Andric                                          ArrayRef<Constant *> AddressPoints,
578*0fca6ea1SDimitry Andric                                          MDNode *BranchWeights) {
579*0fca6ea1SDimitry Andric   assert(!AddressPoints.empty() && "Caller should guarantee");
580*0fca6ea1SDimitry Andric   IRBuilder<> Builder(&CB);
581*0fca6ea1SDimitry Andric   SmallVector<Value *, 2> ICmps;
582*0fca6ea1SDimitry Andric   for (auto &AddressPoint : AddressPoints)
583*0fca6ea1SDimitry Andric     ICmps.push_back(Builder.CreateICmpEQ(VPtr, AddressPoint));
584*0fca6ea1SDimitry Andric 
585*0fca6ea1SDimitry Andric   // TODO: Perform tree height reduction if the number of ICmps is high.
586*0fca6ea1SDimitry Andric   Value *Cond = Builder.CreateOr(ICmps);
587*0fca6ea1SDimitry Andric 
588*0fca6ea1SDimitry Andric   // Version the indirect call site. If Cond is true, 'NewInst' will be
589*0fca6ea1SDimitry Andric   // executed, otherwise the original call site will be executed.
590*0fca6ea1SDimitry Andric   CallBase &NewInst = versionCallSiteWithCond(CB, Cond, BranchWeights);
591*0fca6ea1SDimitry Andric 
592*0fca6ea1SDimitry Andric   // Promote 'NewInst' so that it directly calls the desired function.
593*0fca6ea1SDimitry Andric   return promoteCall(NewInst, Callee);
594*0fca6ea1SDimitry Andric }
595*0fca6ea1SDimitry Andric 
5965ffd83dbSDimitry Andric bool llvm::tryPromoteCall(CallBase &CB) {
5975ffd83dbSDimitry Andric   assert(!CB.getCalledFunction());
5985ffd83dbSDimitry Andric   Module *M = CB.getCaller()->getParent();
5995ffd83dbSDimitry Andric   const DataLayout &DL = M->getDataLayout();
6005ffd83dbSDimitry Andric   Value *Callee = CB.getCalledOperand();
6015ffd83dbSDimitry Andric 
6025ffd83dbSDimitry Andric   LoadInst *VTableEntryLoad = dyn_cast<LoadInst>(Callee);
6035ffd83dbSDimitry Andric   if (!VTableEntryLoad)
6045ffd83dbSDimitry Andric     return false; // Not a vtable entry load.
6055ffd83dbSDimitry Andric   Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand();
6065ffd83dbSDimitry Andric   APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0);
6075ffd83dbSDimitry Andric   Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets(
6085ffd83dbSDimitry Andric       DL, VTableOffset, /* AllowNonInbounds */ true);
6095ffd83dbSDimitry Andric   LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);
6105ffd83dbSDimitry Andric   if (!VTablePtrLoad)
6115ffd83dbSDimitry Andric     return false; // Not a vtable load.
6125ffd83dbSDimitry Andric   Value *Object = VTablePtrLoad->getPointerOperand();
6135ffd83dbSDimitry Andric   APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0);
6145ffd83dbSDimitry Andric   Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(
6155ffd83dbSDimitry Andric       DL, ObjectOffset, /* AllowNonInbounds */ true);
6165ffd83dbSDimitry Andric   if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))
6175ffd83dbSDimitry Andric     // Not an Alloca or the offset isn't zero.
6185ffd83dbSDimitry Andric     return false;
6195ffd83dbSDimitry Andric 
6205ffd83dbSDimitry Andric   // Look for the vtable pointer store into the object by the ctor.
6215ffd83dbSDimitry Andric   BasicBlock::iterator BBI(VTablePtrLoad);
6225ffd83dbSDimitry Andric   Value *VTablePtr = FindAvailableLoadedValue(
6235ffd83dbSDimitry Andric       VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr);
6245ffd83dbSDimitry Andric   if (!VTablePtr)
6255ffd83dbSDimitry Andric     return false; // No vtable found.
6265ffd83dbSDimitry Andric   APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0);
6275ffd83dbSDimitry Andric   Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets(
6285ffd83dbSDimitry Andric       DL, VTableOffsetGVBase, /* AllowNonInbounds */ true);
6295ffd83dbSDimitry Andric   GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase);
6305ffd83dbSDimitry Andric   if (!(GV && GV->isConstant() && GV->hasDefinitiveInitializer()))
6315ffd83dbSDimitry Andric     // Not in the form of a global constant variable with an initializer.
6325ffd83dbSDimitry Andric     return false;
6335ffd83dbSDimitry Andric 
6345ffd83dbSDimitry Andric   APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
6355ffd83dbSDimitry Andric   if (!(VTableGVOffset.getActiveBits() <= 64))
6365ffd83dbSDimitry Andric     return false; // Out of range.
637*0fca6ea1SDimitry Andric 
638*0fca6ea1SDimitry Andric   Function *DirectCallee = nullptr;
639*0fca6ea1SDimitry Andric   std::tie(DirectCallee, std::ignore) =
640*0fca6ea1SDimitry Andric       getFunctionAtVTableOffset(GV, VTableGVOffset.getZExtValue(), *M);
6415ffd83dbSDimitry Andric   if (!DirectCallee)
6425ffd83dbSDimitry Andric     return false; // No function pointer found.
6435ffd83dbSDimitry Andric 
6445ffd83dbSDimitry Andric   if (!isLegalToPromote(CB, DirectCallee))
6455ffd83dbSDimitry Andric     return false;
6465ffd83dbSDimitry Andric 
6475ffd83dbSDimitry Andric   // Success.
6485ffd83dbSDimitry Andric   promoteCall(CB, DirectCallee);
6495ffd83dbSDimitry Andric   return true;
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric #undef DEBUG_TYPE
653