1044eb2f6SDimitry Andric //===- CallPromotionUtils.cpp - Utilities for call promotion ----*- C++ -*-===//
2044eb2f6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6044eb2f6SDimitry Andric //
7044eb2f6SDimitry Andric //===----------------------------------------------------------------------===//
8044eb2f6SDimitry Andric //
9044eb2f6SDimitry Andric // This file implements utilities useful for promoting indirect call sites to
10044eb2f6SDimitry Andric // direct call sites.
11044eb2f6SDimitry Andric //
12044eb2f6SDimitry Andric //===----------------------------------------------------------------------===//
13044eb2f6SDimitry Andric
14044eb2f6SDimitry Andric #include "llvm/Transforms/Utils/CallPromotionUtils.h"
15ac9a064cSDimitry Andric #include "llvm/ADT/STLExtras.h"
16cfca06d7SDimitry Andric #include "llvm/Analysis/Loads.h"
17cfca06d7SDimitry Andric #include "llvm/Analysis/TypeMetadataUtils.h"
187fa27ce4SDimitry Andric #include "llvm/IR/AttributeMask.h"
19ac9a064cSDimitry Andric #include "llvm/IR/Constant.h"
20044eb2f6SDimitry Andric #include "llvm/IR/IRBuilder.h"
21cfca06d7SDimitry Andric #include "llvm/IR/Instructions.h"
22ac9a064cSDimitry Andric #include "llvm/IR/Module.h"
23044eb2f6SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
24044eb2f6SDimitry Andric
25044eb2f6SDimitry Andric using namespace llvm;
26044eb2f6SDimitry Andric
27044eb2f6SDimitry Andric #define DEBUG_TYPE "call-promotion-utils"
28044eb2f6SDimitry Andric
29044eb2f6SDimitry Andric /// Fix-up phi nodes in an invoke instruction's normal destination.
30044eb2f6SDimitry Andric ///
31044eb2f6SDimitry Andric /// After versioning an invoke instruction, values coming from the original
32c7dac04cSDimitry Andric /// block will now be coming from the "merge" block. For example, in the code
33c7dac04cSDimitry Andric /// below:
34c7dac04cSDimitry Andric ///
35c7dac04cSDimitry Andric /// then_bb:
36c7dac04cSDimitry Andric /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
37c7dac04cSDimitry Andric ///
38c7dac04cSDimitry Andric /// else_bb:
39c7dac04cSDimitry Andric /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
40c7dac04cSDimitry Andric ///
41c7dac04cSDimitry Andric /// merge_bb:
42c7dac04cSDimitry Andric /// %t2 = phi i32 [ %t0, %then_bb ], [ %t1, %else_bb ]
43c7dac04cSDimitry Andric /// br %normal_dst
44c7dac04cSDimitry Andric ///
45c7dac04cSDimitry Andric /// normal_dst:
46c7dac04cSDimitry Andric /// %t3 = phi i32 [ %x, %orig_bb ], ...
47c7dac04cSDimitry Andric ///
48c7dac04cSDimitry Andric /// "orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in
49c7dac04cSDimitry Andric /// "normal_dst" must be fixed to refer to "merge_bb":
50c7dac04cSDimitry Andric ///
51c7dac04cSDimitry Andric /// normal_dst:
52c7dac04cSDimitry Andric /// %t3 = phi i32 [ %x, %merge_bb ], ...
53c7dac04cSDimitry Andric ///
fixupPHINodeForNormalDest(InvokeInst * Invoke,BasicBlock * OrigBlock,BasicBlock * MergeBlock)54044eb2f6SDimitry Andric static void fixupPHINodeForNormalDest(InvokeInst *Invoke, BasicBlock *OrigBlock,
55c7dac04cSDimitry Andric BasicBlock *MergeBlock) {
56eb11fae6SDimitry Andric for (PHINode &Phi : Invoke->getNormalDest()->phis()) {
57eb11fae6SDimitry Andric int Idx = Phi.getBasicBlockIndex(OrigBlock);
58044eb2f6SDimitry Andric if (Idx == -1)
59044eb2f6SDimitry Andric continue;
60eb11fae6SDimitry Andric Phi.setIncomingBlock(Idx, MergeBlock);
61044eb2f6SDimitry Andric }
62044eb2f6SDimitry Andric }
63044eb2f6SDimitry Andric
64044eb2f6SDimitry Andric /// Fix-up phi nodes in an invoke instruction's unwind destination.
65044eb2f6SDimitry Andric ///
66044eb2f6SDimitry Andric /// After versioning an invoke instruction, values coming from the original
67044eb2f6SDimitry Andric /// block will now be coming from either the "then" block or the "else" block.
68c7dac04cSDimitry Andric /// For example, in the code below:
69c7dac04cSDimitry Andric ///
70c7dac04cSDimitry Andric /// then_bb:
71c7dac04cSDimitry Andric /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
72c7dac04cSDimitry Andric ///
73c7dac04cSDimitry Andric /// else_bb:
74c7dac04cSDimitry Andric /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
75c7dac04cSDimitry Andric ///
76c7dac04cSDimitry Andric /// unwind_dst:
77c7dac04cSDimitry Andric /// %t3 = phi i32 [ %x, %orig_bb ], ...
78c7dac04cSDimitry Andric ///
79c7dac04cSDimitry Andric /// "orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in
80c7dac04cSDimitry Andric /// "unwind_dst" must be fixed to refer to "then_bb" and "else_bb":
81c7dac04cSDimitry Andric ///
82c7dac04cSDimitry Andric /// unwind_dst:
83c7dac04cSDimitry Andric /// %t3 = phi i32 [ %x, %then_bb ], [ %x, %else_bb ], ...
84c7dac04cSDimitry Andric ///
fixupPHINodeForUnwindDest(InvokeInst * Invoke,BasicBlock * OrigBlock,BasicBlock * ThenBlock,BasicBlock * ElseBlock)85044eb2f6SDimitry Andric static void fixupPHINodeForUnwindDest(InvokeInst *Invoke, BasicBlock *OrigBlock,
86044eb2f6SDimitry Andric BasicBlock *ThenBlock,
87044eb2f6SDimitry Andric BasicBlock *ElseBlock) {
88eb11fae6SDimitry Andric for (PHINode &Phi : Invoke->getUnwindDest()->phis()) {
89eb11fae6SDimitry Andric int Idx = Phi.getBasicBlockIndex(OrigBlock);
90044eb2f6SDimitry Andric if (Idx == -1)
91044eb2f6SDimitry Andric continue;
92eb11fae6SDimitry Andric auto *V = Phi.getIncomingValue(Idx);
93eb11fae6SDimitry Andric Phi.setIncomingBlock(Idx, ThenBlock);
94eb11fae6SDimitry Andric Phi.addIncoming(V, ElseBlock);
95044eb2f6SDimitry Andric }
96044eb2f6SDimitry Andric }
97044eb2f6SDimitry Andric
98044eb2f6SDimitry Andric /// Create a phi node for the returned value of a call or invoke instruction.
99044eb2f6SDimitry Andric ///
100044eb2f6SDimitry Andric /// After versioning a call or invoke instruction that returns a value, we have
101044eb2f6SDimitry Andric /// to merge the value of the original and new instructions. We do this by
102044eb2f6SDimitry Andric /// creating a phi node and replacing uses of the original instruction with this
103044eb2f6SDimitry Andric /// phi node.
104c7dac04cSDimitry Andric ///
105c7dac04cSDimitry Andric /// For example, if \p OrigInst is defined in "else_bb" and \p NewInst is
106c7dac04cSDimitry Andric /// defined in "then_bb", we create the following phi node:
107c7dac04cSDimitry Andric ///
108c7dac04cSDimitry Andric /// ; Uses of the original instruction are replaced by uses of the phi node.
109c7dac04cSDimitry Andric /// %t0 = phi i32 [ %orig_inst, %else_bb ], [ %new_inst, %then_bb ],
110c7dac04cSDimitry Andric ///
createRetPHINode(Instruction * OrigInst,Instruction * NewInst,BasicBlock * MergeBlock,IRBuilder<> & Builder)111c7dac04cSDimitry Andric static void createRetPHINode(Instruction *OrigInst, Instruction *NewInst,
112c7dac04cSDimitry Andric BasicBlock *MergeBlock, IRBuilder<> &Builder) {
113044eb2f6SDimitry Andric
114044eb2f6SDimitry Andric if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty())
115044eb2f6SDimitry Andric return;
116044eb2f6SDimitry Andric
117b1c73532SDimitry Andric Builder.SetInsertPoint(MergeBlock, MergeBlock->begin());
118044eb2f6SDimitry Andric PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0);
119b60736ecSDimitry Andric SmallVector<User *, 16> UsersToUpdate(OrigInst->users());
120044eb2f6SDimitry Andric for (User *U : UsersToUpdate)
121044eb2f6SDimitry Andric U->replaceUsesOfWith(OrigInst, Phi);
122044eb2f6SDimitry Andric Phi->addIncoming(OrigInst, OrigInst->getParent());
123c7dac04cSDimitry Andric Phi->addIncoming(NewInst, NewInst->getParent());
124044eb2f6SDimitry Andric }
125044eb2f6SDimitry Andric
126044eb2f6SDimitry Andric /// Cast a call or invoke instruction to the given type.
127044eb2f6SDimitry Andric ///
128044eb2f6SDimitry Andric /// When promoting a call site, the return type of the call site might not match
129044eb2f6SDimitry Andric /// that of the callee. If this is the case, we have to cast the returned value
130044eb2f6SDimitry Andric /// to the correct type. The location of the cast depends on if we have a call
131044eb2f6SDimitry Andric /// or invoke instruction.
132c7dac04cSDimitry Andric ///
133c7dac04cSDimitry Andric /// For example, if the call instruction below requires a bitcast after
134c7dac04cSDimitry Andric /// promotion:
135c7dac04cSDimitry Andric ///
136c7dac04cSDimitry Andric /// orig_bb:
137c7dac04cSDimitry Andric /// %t0 = call i32 @func()
138c7dac04cSDimitry Andric /// ...
139c7dac04cSDimitry Andric ///
140c7dac04cSDimitry Andric /// The bitcast is placed after the call instruction:
141c7dac04cSDimitry Andric ///
142c7dac04cSDimitry Andric /// orig_bb:
143c7dac04cSDimitry Andric /// ; Uses of the original return value are replaced by uses of the bitcast.
144c7dac04cSDimitry Andric /// %t0 = call i32 @func()
145c7dac04cSDimitry Andric /// %t1 = bitcast i32 %t0 to ...
146c7dac04cSDimitry Andric /// ...
147c7dac04cSDimitry Andric ///
148c7dac04cSDimitry Andric /// A similar transformation is performed for invoke instructions. However,
149c7dac04cSDimitry Andric /// since invokes are terminating, a new block is created for the bitcast. For
150c7dac04cSDimitry Andric /// example, if the invoke instruction below requires a bitcast after promotion:
151c7dac04cSDimitry Andric ///
152c7dac04cSDimitry Andric /// orig_bb:
153c7dac04cSDimitry Andric /// %t0 = invoke i32 @func() to label %normal_dst unwind label %unwind_dst
154c7dac04cSDimitry Andric ///
155c7dac04cSDimitry Andric /// The edge between the original block and the invoke's normal destination is
156c7dac04cSDimitry Andric /// split, and the bitcast is placed there:
157c7dac04cSDimitry Andric ///
158c7dac04cSDimitry Andric /// orig_bb:
159c7dac04cSDimitry Andric /// %t0 = invoke i32 @func() to label %split_bb unwind label %unwind_dst
160c7dac04cSDimitry Andric ///
161c7dac04cSDimitry Andric /// split_bb:
162c7dac04cSDimitry Andric /// ; Uses of the original return value are replaced by uses of the bitcast.
163c7dac04cSDimitry Andric /// %t1 = bitcast i32 %t0 to ...
164c7dac04cSDimitry Andric /// br label %normal_dst
165c7dac04cSDimitry Andric ///
createRetBitCast(CallBase & CB,Type * RetTy,CastInst ** RetBitCast)166cfca06d7SDimitry Andric static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
167044eb2f6SDimitry Andric
168044eb2f6SDimitry Andric // Save the users of the calling instruction. These uses will be changed to
169044eb2f6SDimitry Andric // use the bitcast after we create it.
170b60736ecSDimitry Andric SmallVector<User *, 16> UsersToUpdate(CB.users());
171044eb2f6SDimitry Andric
172044eb2f6SDimitry Andric // Determine an appropriate location to create the bitcast for the return
173044eb2f6SDimitry Andric // value. The location depends on if we have a call or invoke instruction.
174ac9a064cSDimitry Andric BasicBlock::iterator InsertBefore;
175cfca06d7SDimitry Andric if (auto *Invoke = dyn_cast<InvokeInst>(&CB))
176c7dac04cSDimitry Andric InsertBefore =
177ac9a064cSDimitry Andric SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->begin();
178044eb2f6SDimitry Andric else
179ac9a064cSDimitry Andric InsertBefore = std::next(CB.getIterator());
180044eb2f6SDimitry Andric
181044eb2f6SDimitry Andric // Bitcast the return value to the correct type.
182cfca06d7SDimitry Andric auto *Cast = CastInst::CreateBitOrPointerCast(&CB, RetTy, "", InsertBefore);
183c7dac04cSDimitry Andric if (RetBitCast)
184c7dac04cSDimitry Andric *RetBitCast = Cast;
185044eb2f6SDimitry Andric
186044eb2f6SDimitry Andric // Replace all the original uses of the calling instruction with the bitcast.
187044eb2f6SDimitry Andric for (User *U : UsersToUpdate)
188cfca06d7SDimitry Andric U->replaceUsesOfWith(&CB, Cast);
189044eb2f6SDimitry Andric }
190044eb2f6SDimitry Andric
191044eb2f6SDimitry Andric /// Predicate and clone the given call site.
192044eb2f6SDimitry Andric ///
193044eb2f6SDimitry Andric /// This function creates an if-then-else structure at the location of the call
194ac9a064cSDimitry Andric /// site. The "if" condition is specified by `Cond`.
195ac9a064cSDimitry Andric /// The original call site is moved into the "else" block, and a clone of the
196ac9a064cSDimitry Andric /// call site is placed in the "then" block. The cloned instruction is returned.
197c7dac04cSDimitry Andric ///
198c7dac04cSDimitry Andric /// For example, the call instruction below:
199c7dac04cSDimitry Andric ///
200c7dac04cSDimitry Andric /// orig_bb:
201c7dac04cSDimitry Andric /// %t0 = call i32 %ptr()
202c7dac04cSDimitry Andric /// ...
203c7dac04cSDimitry Andric ///
204c7dac04cSDimitry Andric /// Is replace by the following:
205c7dac04cSDimitry Andric ///
206c7dac04cSDimitry Andric /// orig_bb:
207ac9a064cSDimitry Andric /// %cond = Cond
208c7dac04cSDimitry Andric /// br i1 %cond, %then_bb, %else_bb
209c7dac04cSDimitry Andric ///
210c7dac04cSDimitry Andric /// then_bb:
211c7dac04cSDimitry Andric /// ; The clone of the original call instruction is placed in the "then"
212c7dac04cSDimitry Andric /// ; block. It is not yet promoted.
213c7dac04cSDimitry Andric /// %t1 = call i32 %ptr()
214c7dac04cSDimitry Andric /// br merge_bb
215c7dac04cSDimitry Andric ///
216c7dac04cSDimitry Andric /// else_bb:
217c7dac04cSDimitry Andric /// ; The original call instruction is moved to the "else" block.
218c7dac04cSDimitry Andric /// %t0 = call i32 %ptr()
219c7dac04cSDimitry Andric /// br merge_bb
220c7dac04cSDimitry Andric ///
221c7dac04cSDimitry Andric /// merge_bb:
222c7dac04cSDimitry Andric /// ; Uses of the original call instruction are replaced by uses of the phi
223c7dac04cSDimitry Andric /// ; node.
224c7dac04cSDimitry Andric /// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]
225c7dac04cSDimitry Andric /// ...
226c7dac04cSDimitry Andric ///
227c7dac04cSDimitry Andric /// A similar transformation is performed for invoke instructions. However,
228c7dac04cSDimitry Andric /// since invokes are terminating, more work is required. For example, the
229c7dac04cSDimitry Andric /// invoke instruction below:
230c7dac04cSDimitry Andric ///
231c7dac04cSDimitry Andric /// orig_bb:
232c7dac04cSDimitry Andric /// %t0 = invoke %ptr() to label %normal_dst unwind label %unwind_dst
233c7dac04cSDimitry Andric ///
234c7dac04cSDimitry Andric /// Is replace by the following:
235c7dac04cSDimitry Andric ///
236c7dac04cSDimitry Andric /// orig_bb:
237ac9a064cSDimitry Andric /// %cond = Cond
238c7dac04cSDimitry Andric /// br i1 %cond, %then_bb, %else_bb
239c7dac04cSDimitry Andric ///
240c7dac04cSDimitry Andric /// then_bb:
241c7dac04cSDimitry Andric /// ; The clone of the original invoke instruction is placed in the "then"
242c7dac04cSDimitry Andric /// ; block, and its normal destination is set to the "merge" block. It is
243c7dac04cSDimitry Andric /// ; not yet promoted.
244c7dac04cSDimitry Andric /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
245c7dac04cSDimitry Andric ///
246c7dac04cSDimitry Andric /// else_bb:
247c7dac04cSDimitry Andric /// ; The original invoke instruction is moved into the "else" block, and
248c7dac04cSDimitry Andric /// ; its normal destination is set to the "merge" block.
249c7dac04cSDimitry Andric /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
250c7dac04cSDimitry Andric ///
251c7dac04cSDimitry Andric /// merge_bb:
252c7dac04cSDimitry Andric /// ; Uses of the original invoke instruction are replaced by uses of the
253c7dac04cSDimitry Andric /// ; phi node, and the merge block branches to the normal destination.
254c7dac04cSDimitry Andric /// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]
255c7dac04cSDimitry Andric /// br %normal_dst
256c7dac04cSDimitry Andric ///
257cfca06d7SDimitry Andric /// An indirect musttail call is processed slightly differently in that:
258cfca06d7SDimitry Andric /// 1. No merge block needed for the orginal and the cloned callsite, since
259cfca06d7SDimitry Andric /// either one ends the flow. No phi node is needed either.
260cfca06d7SDimitry Andric /// 2. The return statement following the original call site is duplicated too
261cfca06d7SDimitry Andric /// and placed immediately after the cloned call site per the IR convention.
262cfca06d7SDimitry Andric ///
263cfca06d7SDimitry Andric /// For example, the musttail call instruction below:
264cfca06d7SDimitry Andric ///
265cfca06d7SDimitry Andric /// orig_bb:
266cfca06d7SDimitry Andric /// %t0 = musttail call i32 %ptr()
267cfca06d7SDimitry Andric /// ...
268cfca06d7SDimitry Andric ///
269cfca06d7SDimitry Andric /// Is replaced by the following:
270cfca06d7SDimitry Andric ///
271cfca06d7SDimitry Andric /// cond_bb:
272ac9a064cSDimitry Andric /// %cond = Cond
273cfca06d7SDimitry Andric /// br i1 %cond, %then_bb, %orig_bb
274cfca06d7SDimitry Andric ///
275cfca06d7SDimitry Andric /// then_bb:
276cfca06d7SDimitry Andric /// ; The clone of the original call instruction is placed in the "then"
277cfca06d7SDimitry Andric /// ; block. It is not yet promoted.
278cfca06d7SDimitry Andric /// %t1 = musttail call i32 %ptr()
279cfca06d7SDimitry Andric /// ret %t1
280cfca06d7SDimitry Andric ///
281cfca06d7SDimitry Andric /// orig_bb:
282cfca06d7SDimitry Andric /// ; The original call instruction stays in its original block.
283cfca06d7SDimitry Andric /// %t0 = musttail call i32 %ptr()
284cfca06d7SDimitry Andric /// ret %t0
versionCallSiteWithCond(CallBase & CB,Value * Cond,MDNode * BranchWeights)285ac9a064cSDimitry Andric static CallBase &versionCallSiteWithCond(CallBase &CB, Value *Cond,
286c7dac04cSDimitry Andric MDNode *BranchWeights) {
287044eb2f6SDimitry Andric
288cfca06d7SDimitry Andric IRBuilder<> Builder(&CB);
289cfca06d7SDimitry Andric CallBase *OrigInst = &CB;
290c7dac04cSDimitry Andric BasicBlock *OrigBlock = OrigInst->getParent();
291044eb2f6SDimitry Andric
292cfca06d7SDimitry Andric if (OrigInst->isMustTailCall()) {
293cfca06d7SDimitry Andric // Create an if-then structure. The original instruction stays in its block,
294cfca06d7SDimitry Andric // and a clone of the original instruction is placed in the "then" block.
295cfca06d7SDimitry Andric Instruction *ThenTerm =
296cfca06d7SDimitry Andric SplitBlockAndInsertIfThen(Cond, &CB, false, BranchWeights);
297cfca06d7SDimitry Andric BasicBlock *ThenBlock = ThenTerm->getParent();
298cfca06d7SDimitry Andric ThenBlock->setName("if.true.direct_targ");
299cfca06d7SDimitry Andric CallBase *NewInst = cast<CallBase>(OrigInst->clone());
300cfca06d7SDimitry Andric NewInst->insertBefore(ThenTerm);
301cfca06d7SDimitry Andric
302cfca06d7SDimitry Andric // Place a clone of the optional bitcast after the new call site.
303cfca06d7SDimitry Andric Value *NewRetVal = NewInst;
304cfca06d7SDimitry Andric auto Next = OrigInst->getNextNode();
305cfca06d7SDimitry Andric if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) {
306cfca06d7SDimitry Andric assert(BitCast->getOperand(0) == OrigInst &&
307cfca06d7SDimitry Andric "bitcast following musttail call must use the call");
308cfca06d7SDimitry Andric auto NewBitCast = BitCast->clone();
309cfca06d7SDimitry Andric NewBitCast->replaceUsesOfWith(OrigInst, NewInst);
310cfca06d7SDimitry Andric NewBitCast->insertBefore(ThenTerm);
311cfca06d7SDimitry Andric NewRetVal = NewBitCast;
312cfca06d7SDimitry Andric Next = BitCast->getNextNode();
313cfca06d7SDimitry Andric }
314cfca06d7SDimitry Andric
315cfca06d7SDimitry Andric // Place a clone of the return instruction after the new call site.
316cfca06d7SDimitry Andric ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
317cfca06d7SDimitry Andric assert(Ret && "musttail call must precede a ret with an optional bitcast");
318cfca06d7SDimitry Andric auto NewRet = Ret->clone();
319cfca06d7SDimitry Andric if (Ret->getReturnValue())
320cfca06d7SDimitry Andric NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal);
321cfca06d7SDimitry Andric NewRet->insertBefore(ThenTerm);
322cfca06d7SDimitry Andric
323cfca06d7SDimitry Andric // A return instructions is terminating, so we don't need the terminator
324cfca06d7SDimitry Andric // instruction just created.
325cfca06d7SDimitry Andric ThenTerm->eraseFromParent();
326cfca06d7SDimitry Andric
327cfca06d7SDimitry Andric return *NewInst;
328cfca06d7SDimitry Andric }
329044eb2f6SDimitry Andric
330044eb2f6SDimitry Andric // Create an if-then-else structure. The original instruction is moved into
331044eb2f6SDimitry Andric // the "else" block, and a clone of the original instruction is placed in the
332044eb2f6SDimitry Andric // "then" block.
333d8e91e46SDimitry Andric Instruction *ThenTerm = nullptr;
334d8e91e46SDimitry Andric Instruction *ElseTerm = nullptr;
335cfca06d7SDimitry Andric SplitBlockAndInsertIfThenElse(Cond, &CB, &ThenTerm, &ElseTerm, BranchWeights);
336c7dac04cSDimitry Andric BasicBlock *ThenBlock = ThenTerm->getParent();
337c7dac04cSDimitry Andric BasicBlock *ElseBlock = ElseTerm->getParent();
338c7dac04cSDimitry Andric BasicBlock *MergeBlock = OrigInst->getParent();
339044eb2f6SDimitry Andric
340044eb2f6SDimitry Andric ThenBlock->setName("if.true.direct_targ");
341044eb2f6SDimitry Andric ElseBlock->setName("if.false.orig_indirect");
342044eb2f6SDimitry Andric MergeBlock->setName("if.end.icp");
343044eb2f6SDimitry Andric
344cfca06d7SDimitry Andric CallBase *NewInst = cast<CallBase>(OrigInst->clone());
345044eb2f6SDimitry Andric OrigInst->moveBefore(ElseTerm);
346044eb2f6SDimitry Andric NewInst->insertBefore(ThenTerm);
347044eb2f6SDimitry Andric
348044eb2f6SDimitry Andric // If the original call site is an invoke instruction, we have extra work to
349c7dac04cSDimitry Andric // do since invoke instructions are terminating. We have to fix-up phi nodes
350c7dac04cSDimitry Andric // in the invoke's normal and unwind destinations.
351044eb2f6SDimitry Andric if (auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) {
352044eb2f6SDimitry Andric auto *NewInvoke = cast<InvokeInst>(NewInst);
353044eb2f6SDimitry Andric
354044eb2f6SDimitry Andric // Invoke instructions are terminating, so we don't need the terminator
355044eb2f6SDimitry Andric // instructions that were just created.
356044eb2f6SDimitry Andric ThenTerm->eraseFromParent();
357044eb2f6SDimitry Andric ElseTerm->eraseFromParent();
358044eb2f6SDimitry Andric
359044eb2f6SDimitry Andric // Branch from the "merge" block to the original normal destination.
360044eb2f6SDimitry Andric Builder.SetInsertPoint(MergeBlock);
361044eb2f6SDimitry Andric Builder.CreateBr(OrigInvoke->getNormalDest());
362044eb2f6SDimitry Andric
363c7dac04cSDimitry Andric // Fix-up phi nodes in the original invoke's normal and unwind destinations.
364c7dac04cSDimitry Andric fixupPHINodeForNormalDest(OrigInvoke, OrigBlock, MergeBlock);
365c7dac04cSDimitry Andric fixupPHINodeForUnwindDest(OrigInvoke, MergeBlock, ThenBlock, ElseBlock);
366c7dac04cSDimitry Andric
367c7dac04cSDimitry Andric // Now set the normal destinations of the invoke instructions to be the
368044eb2f6SDimitry Andric // "merge" block.
369c7dac04cSDimitry Andric OrigInvoke->setNormalDest(MergeBlock);
370044eb2f6SDimitry Andric NewInvoke->setNormalDest(MergeBlock);
371044eb2f6SDimitry Andric }
372044eb2f6SDimitry Andric
373c7dac04cSDimitry Andric // Create a phi node for the returned value of the call site.
374c7dac04cSDimitry Andric createRetPHINode(OrigInst, NewInst, MergeBlock, Builder);
375c7dac04cSDimitry Andric
376cfca06d7SDimitry Andric return *NewInst;
377044eb2f6SDimitry Andric }
378044eb2f6SDimitry Andric
379ac9a064cSDimitry Andric // Predicate and clone the given call site using condition `CB.callee ==
380ac9a064cSDimitry Andric // Callee`. See the comment `versionCallSiteWithCond` for the transformation.
versionCallSite(CallBase & CB,Value * Callee,MDNode * BranchWeights)381ac9a064cSDimitry Andric CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
382ac9a064cSDimitry Andric MDNode *BranchWeights) {
383ac9a064cSDimitry Andric
384ac9a064cSDimitry Andric IRBuilder<> Builder(&CB);
385ac9a064cSDimitry Andric
386ac9a064cSDimitry Andric // Create the compare. The called value and callee must have the same type to
387ac9a064cSDimitry Andric // be compared.
388ac9a064cSDimitry Andric if (CB.getCalledOperand()->getType() != Callee->getType())
389ac9a064cSDimitry Andric Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType());
390ac9a064cSDimitry Andric auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee);
391ac9a064cSDimitry Andric
392ac9a064cSDimitry Andric return versionCallSiteWithCond(CB, Cond, BranchWeights);
393ac9a064cSDimitry Andric }
394ac9a064cSDimitry Andric
isLegalToPromote(const CallBase & CB,Function * Callee,const char ** FailureReason)395cfca06d7SDimitry Andric bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
396044eb2f6SDimitry Andric const char **FailureReason) {
397cfca06d7SDimitry Andric assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");
398044eb2f6SDimitry Andric
399ac9a064cSDimitry Andric auto &DL = Callee->getDataLayout();
400d8e91e46SDimitry Andric
401044eb2f6SDimitry Andric // Check the return type. The callee's return value type must be bitcast
402044eb2f6SDimitry Andric // compatible with the call site's type.
403cfca06d7SDimitry Andric Type *CallRetTy = CB.getType();
404044eb2f6SDimitry Andric Type *FuncRetTy = Callee->getReturnType();
405044eb2f6SDimitry Andric if (CallRetTy != FuncRetTy)
406d8e91e46SDimitry Andric if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) {
407044eb2f6SDimitry Andric if (FailureReason)
408044eb2f6SDimitry Andric *FailureReason = "Return type mismatch";
409044eb2f6SDimitry Andric return false;
410044eb2f6SDimitry Andric }
411044eb2f6SDimitry Andric
412044eb2f6SDimitry Andric // The number of formal arguments of the callee.
413044eb2f6SDimitry Andric unsigned NumParams = Callee->getFunctionType()->getNumParams();
414044eb2f6SDimitry Andric
415cfca06d7SDimitry Andric // The number of actual arguments in the call.
416cfca06d7SDimitry Andric unsigned NumArgs = CB.arg_size();
417cfca06d7SDimitry Andric
418044eb2f6SDimitry Andric // Check the number of arguments. The callee and call site must agree on the
419044eb2f6SDimitry Andric // number of arguments.
420cfca06d7SDimitry Andric if (NumArgs != NumParams && !Callee->isVarArg()) {
421044eb2f6SDimitry Andric if (FailureReason)
422044eb2f6SDimitry Andric *FailureReason = "The number of arguments mismatch";
423044eb2f6SDimitry Andric return false;
424044eb2f6SDimitry Andric }
425044eb2f6SDimitry Andric
426044eb2f6SDimitry Andric // Check the argument types. The callee's formal argument types must be
427044eb2f6SDimitry Andric // bitcast compatible with the corresponding actual argument types of the call
428044eb2f6SDimitry Andric // site.
429cfca06d7SDimitry Andric unsigned I = 0;
430cfca06d7SDimitry Andric for (; I < NumParams; ++I) {
431c0981da4SDimitry Andric // Make sure that the callee and call agree on byval/inalloca. The types do
432c0981da4SDimitry Andric // not have to match.
433c0981da4SDimitry Andric if (Callee->hasParamAttribute(I, Attribute::ByVal) !=
434c0981da4SDimitry Andric CB.getAttributes().hasParamAttr(I, Attribute::ByVal)) {
435c0981da4SDimitry Andric if (FailureReason)
436c0981da4SDimitry Andric *FailureReason = "byval mismatch";
437c0981da4SDimitry Andric return false;
438c0981da4SDimitry Andric }
439c0981da4SDimitry Andric if (Callee->hasParamAttribute(I, Attribute::InAlloca) !=
440c0981da4SDimitry Andric CB.getAttributes().hasParamAttr(I, Attribute::InAlloca)) {
441c0981da4SDimitry Andric if (FailureReason)
442c0981da4SDimitry Andric *FailureReason = "inalloca mismatch";
443c0981da4SDimitry Andric return false;
444c0981da4SDimitry Andric }
445e3b55780SDimitry Andric
446e3b55780SDimitry Andric Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I);
447e3b55780SDimitry Andric Type *ActualTy = CB.getArgOperand(I)->getType();
448e3b55780SDimitry Andric if (FormalTy == ActualTy)
449e3b55780SDimitry Andric continue;
450e3b55780SDimitry Andric if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {
451e3b55780SDimitry Andric if (FailureReason)
452e3b55780SDimitry Andric *FailureReason = "Argument type mismatch";
453e3b55780SDimitry Andric return false;
454e3b55780SDimitry Andric }
455e3b55780SDimitry Andric
456e3b55780SDimitry Andric // MustTail call needs stricter type match. See
457e3b55780SDimitry Andric // Verifier::verifyMustTailCall().
458e3b55780SDimitry Andric if (CB.isMustTailCall()) {
459e3b55780SDimitry Andric PointerType *PF = dyn_cast<PointerType>(FormalTy);
460e3b55780SDimitry Andric PointerType *PA = dyn_cast<PointerType>(ActualTy);
461e3b55780SDimitry Andric if (!PF || !PA || PF->getAddressSpace() != PA->getAddressSpace()) {
462e3b55780SDimitry Andric if (FailureReason)
463e3b55780SDimitry Andric *FailureReason = "Musttail call Argument type mismatch";
464e3b55780SDimitry Andric return false;
465e3b55780SDimitry Andric }
466e3b55780SDimitry Andric }
467044eb2f6SDimitry Andric }
468cfca06d7SDimitry Andric for (; I < NumArgs; I++) {
469b60736ecSDimitry Andric // Vararg functions can have more arguments than parameters.
470cfca06d7SDimitry Andric assert(Callee->isVarArg());
471cfca06d7SDimitry Andric if (CB.paramHasAttr(I, Attribute::StructRet)) {
472b60736ecSDimitry Andric if (FailureReason)
473cfca06d7SDimitry Andric *FailureReason = "SRet arg to vararg function";
474cfca06d7SDimitry Andric return false;
475cfca06d7SDimitry Andric }
476cfca06d7SDimitry Andric }
477044eb2f6SDimitry Andric
478044eb2f6SDimitry Andric return true;
479044eb2f6SDimitry Andric }
480044eb2f6SDimitry Andric
promoteCall(CallBase & CB,Function * Callee,CastInst ** RetBitCast)481cfca06d7SDimitry Andric CallBase &llvm::promoteCall(CallBase &CB, Function *Callee,
482c7dac04cSDimitry Andric CastInst **RetBitCast) {
483cfca06d7SDimitry Andric assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");
484044eb2f6SDimitry Andric
485e6d15924SDimitry Andric // Set the called function of the call site to be the given callee (but don't
486e6d15924SDimitry Andric // change the type).
487cfca06d7SDimitry Andric CB.setCalledOperand(Callee);
488044eb2f6SDimitry Andric
489044eb2f6SDimitry Andric // Since the call site will no longer be direct, we must clear metadata that
490044eb2f6SDimitry Andric // is only appropriate for indirect calls. This includes !prof and !callees
491044eb2f6SDimitry Andric // metadata.
492cfca06d7SDimitry Andric CB.setMetadata(LLVMContext::MD_prof, nullptr);
493cfca06d7SDimitry Andric CB.setMetadata(LLVMContext::MD_callees, nullptr);
494044eb2f6SDimitry Andric
495044eb2f6SDimitry Andric // If the function type of the call site matches that of the callee, no
496044eb2f6SDimitry Andric // additional work is required.
497cfca06d7SDimitry Andric if (CB.getFunctionType() == Callee->getFunctionType())
498cfca06d7SDimitry Andric return CB;
499044eb2f6SDimitry Andric
500044eb2f6SDimitry Andric // Save the return types of the call site and callee.
501cfca06d7SDimitry Andric Type *CallSiteRetTy = CB.getType();
502044eb2f6SDimitry Andric Type *CalleeRetTy = Callee->getReturnType();
503044eb2f6SDimitry Andric
504044eb2f6SDimitry Andric // Change the function type of the call site the match that of the callee.
505cfca06d7SDimitry Andric CB.mutateFunctionType(Callee->getFunctionType());
506044eb2f6SDimitry Andric
507044eb2f6SDimitry Andric // Inspect the arguments of the call site. If an argument's type doesn't
508044eb2f6SDimitry Andric // match the corresponding formal argument's type in the callee, bitcast it
509044eb2f6SDimitry Andric // to the correct type.
510eb11fae6SDimitry Andric auto CalleeType = Callee->getFunctionType();
511eb11fae6SDimitry Andric auto CalleeParamNum = CalleeType->getNumParams();
512d8e91e46SDimitry Andric
513d8e91e46SDimitry Andric LLVMContext &Ctx = Callee->getContext();
514cfca06d7SDimitry Andric const AttributeList &CallerPAL = CB.getAttributes();
515d8e91e46SDimitry Andric // The new list of argument attributes.
516d8e91e46SDimitry Andric SmallVector<AttributeSet, 4> NewArgAttrs;
517d8e91e46SDimitry Andric bool AttributeChanged = false;
518d8e91e46SDimitry Andric
519eb11fae6SDimitry Andric for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
520cfca06d7SDimitry Andric auto *Arg = CB.getArgOperand(ArgNo);
521eb11fae6SDimitry Andric Type *FormalTy = CalleeType->getParamType(ArgNo);
522eb11fae6SDimitry Andric Type *ActualTy = Arg->getType();
523044eb2f6SDimitry Andric if (FormalTy != ActualTy) {
524ac9a064cSDimitry Andric auto *Cast =
525ac9a064cSDimitry Andric CastInst::CreateBitOrPointerCast(Arg, FormalTy, "", CB.getIterator());
526cfca06d7SDimitry Andric CB.setArgOperand(ArgNo, Cast);
527d8e91e46SDimitry Andric
528d8e91e46SDimitry Andric // Remove any incompatible attributes for the argument.
5296f8fc217SDimitry Andric AttrBuilder ArgAttrs(Ctx, CallerPAL.getParamAttrs(ArgNo));
530d8e91e46SDimitry Andric ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy));
531e6d15924SDimitry Andric
532c0981da4SDimitry Andric // We may have a different byval/inalloca type.
533344a3780SDimitry Andric if (ArgAttrs.getByValType())
534344a3780SDimitry Andric ArgAttrs.addByValAttr(Callee->getParamByValType(ArgNo));
535c0981da4SDimitry Andric if (ArgAttrs.getInAllocaType())
536c0981da4SDimitry Andric ArgAttrs.addInAllocaAttr(Callee->getParamInAllocaType(ArgNo));
537e6d15924SDimitry Andric
538d8e91e46SDimitry Andric NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs));
539d8e91e46SDimitry Andric AttributeChanged = true;
540d8e91e46SDimitry Andric } else
541c0981da4SDimitry Andric NewArgAttrs.push_back(CallerPAL.getParamAttrs(ArgNo));
542044eb2f6SDimitry Andric }
543044eb2f6SDimitry Andric
544044eb2f6SDimitry Andric // If the return type of the call site doesn't match that of the callee, cast
545044eb2f6SDimitry Andric // the returned value to the appropriate type.
546d8e91e46SDimitry Andric // Remove any incompatible return value attribute.
5476f8fc217SDimitry Andric AttrBuilder RAttrs(Ctx, CallerPAL.getRetAttrs());
548d8e91e46SDimitry Andric if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) {
549cfca06d7SDimitry Andric createRetBitCast(CB, CallSiteRetTy, RetBitCast);
550d8e91e46SDimitry Andric RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy));
551d8e91e46SDimitry Andric AttributeChanged = true;
552d8e91e46SDimitry Andric }
553d8e91e46SDimitry Andric
554d8e91e46SDimitry Andric // Set the new callsite attribute.
555d8e91e46SDimitry Andric if (AttributeChanged)
556c0981da4SDimitry Andric CB.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttrs(),
557d8e91e46SDimitry Andric AttributeSet::get(Ctx, RAttrs),
558d8e91e46SDimitry Andric NewArgAttrs));
559c7dac04cSDimitry Andric
560cfca06d7SDimitry Andric return CB;
561044eb2f6SDimitry Andric }
562044eb2f6SDimitry Andric
promoteCallWithIfThenElse(CallBase & CB,Function * Callee,MDNode * BranchWeights)563cfca06d7SDimitry Andric CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
564044eb2f6SDimitry Andric MDNode *BranchWeights) {
565044eb2f6SDimitry Andric
566044eb2f6SDimitry Andric // Version the indirect call site. If the called value is equal to the given
567044eb2f6SDimitry Andric // callee, 'NewInst' will be executed, otherwise the original call site will
568044eb2f6SDimitry Andric // be executed.
569cfca06d7SDimitry Andric CallBase &NewInst = versionCallSite(CB, Callee, BranchWeights);
570044eb2f6SDimitry Andric
571044eb2f6SDimitry Andric // Promote 'NewInst' so that it directly calls the desired function.
572cfca06d7SDimitry Andric return promoteCall(NewInst, Callee);
573cfca06d7SDimitry Andric }
574cfca06d7SDimitry Andric
promoteCallWithVTableCmp(CallBase & CB,Instruction * VPtr,Function * Callee,ArrayRef<Constant * > AddressPoints,MDNode * BranchWeights)575ac9a064cSDimitry Andric CallBase &llvm::promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr,
576ac9a064cSDimitry Andric Function *Callee,
577ac9a064cSDimitry Andric ArrayRef<Constant *> AddressPoints,
578ac9a064cSDimitry Andric MDNode *BranchWeights) {
579ac9a064cSDimitry Andric assert(!AddressPoints.empty() && "Caller should guarantee");
580ac9a064cSDimitry Andric IRBuilder<> Builder(&CB);
581ac9a064cSDimitry Andric SmallVector<Value *, 2> ICmps;
582ac9a064cSDimitry Andric for (auto &AddressPoint : AddressPoints)
583ac9a064cSDimitry Andric ICmps.push_back(Builder.CreateICmpEQ(VPtr, AddressPoint));
584ac9a064cSDimitry Andric
585ac9a064cSDimitry Andric // TODO: Perform tree height reduction if the number of ICmps is high.
586ac9a064cSDimitry Andric Value *Cond = Builder.CreateOr(ICmps);
587ac9a064cSDimitry Andric
588ac9a064cSDimitry Andric // Version the indirect call site. If Cond is true, 'NewInst' will be
589ac9a064cSDimitry Andric // executed, otherwise the original call site will be executed.
590ac9a064cSDimitry Andric CallBase &NewInst = versionCallSiteWithCond(CB, Cond, BranchWeights);
591ac9a064cSDimitry Andric
592ac9a064cSDimitry Andric // Promote 'NewInst' so that it directly calls the desired function.
593ac9a064cSDimitry Andric return promoteCall(NewInst, Callee);
594ac9a064cSDimitry Andric }
595ac9a064cSDimitry Andric
tryPromoteCall(CallBase & CB)596cfca06d7SDimitry Andric bool llvm::tryPromoteCall(CallBase &CB) {
597cfca06d7SDimitry Andric assert(!CB.getCalledFunction());
598cfca06d7SDimitry Andric Module *M = CB.getCaller()->getParent();
599cfca06d7SDimitry Andric const DataLayout &DL = M->getDataLayout();
600cfca06d7SDimitry Andric Value *Callee = CB.getCalledOperand();
601cfca06d7SDimitry Andric
602cfca06d7SDimitry Andric LoadInst *VTableEntryLoad = dyn_cast<LoadInst>(Callee);
603cfca06d7SDimitry Andric if (!VTableEntryLoad)
604cfca06d7SDimitry Andric return false; // Not a vtable entry load.
605cfca06d7SDimitry Andric Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand();
606cfca06d7SDimitry Andric APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0);
607cfca06d7SDimitry Andric Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets(
608cfca06d7SDimitry Andric DL, VTableOffset, /* AllowNonInbounds */ true);
609cfca06d7SDimitry Andric LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);
610cfca06d7SDimitry Andric if (!VTablePtrLoad)
611cfca06d7SDimitry Andric return false; // Not a vtable load.
612cfca06d7SDimitry Andric Value *Object = VTablePtrLoad->getPointerOperand();
613cfca06d7SDimitry Andric APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0);
614cfca06d7SDimitry Andric Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(
615cfca06d7SDimitry Andric DL, ObjectOffset, /* AllowNonInbounds */ true);
616cfca06d7SDimitry Andric if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))
617cfca06d7SDimitry Andric // Not an Alloca or the offset isn't zero.
618cfca06d7SDimitry Andric return false;
619cfca06d7SDimitry Andric
620cfca06d7SDimitry Andric // Look for the vtable pointer store into the object by the ctor.
621cfca06d7SDimitry Andric BasicBlock::iterator BBI(VTablePtrLoad);
622cfca06d7SDimitry Andric Value *VTablePtr = FindAvailableLoadedValue(
623cfca06d7SDimitry Andric VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr);
624cfca06d7SDimitry Andric if (!VTablePtr)
625cfca06d7SDimitry Andric return false; // No vtable found.
626cfca06d7SDimitry Andric APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0);
627cfca06d7SDimitry Andric Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets(
628cfca06d7SDimitry Andric DL, VTableOffsetGVBase, /* AllowNonInbounds */ true);
629cfca06d7SDimitry Andric GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase);
630cfca06d7SDimitry Andric if (!(GV && GV->isConstant() && GV->hasDefinitiveInitializer()))
631cfca06d7SDimitry Andric // Not in the form of a global constant variable with an initializer.
632cfca06d7SDimitry Andric return false;
633cfca06d7SDimitry Andric
634cfca06d7SDimitry Andric APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
635cfca06d7SDimitry Andric if (!(VTableGVOffset.getActiveBits() <= 64))
636cfca06d7SDimitry Andric return false; // Out of range.
637ac9a064cSDimitry Andric
638ac9a064cSDimitry Andric Function *DirectCallee = nullptr;
639ac9a064cSDimitry Andric std::tie(DirectCallee, std::ignore) =
640ac9a064cSDimitry Andric getFunctionAtVTableOffset(GV, VTableGVOffset.getZExtValue(), *M);
641cfca06d7SDimitry Andric if (!DirectCallee)
642cfca06d7SDimitry Andric return false; // No function pointer found.
643cfca06d7SDimitry Andric
644cfca06d7SDimitry Andric if (!isLegalToPromote(CB, DirectCallee))
645cfca06d7SDimitry Andric return false;
646cfca06d7SDimitry Andric
647cfca06d7SDimitry Andric // Success.
648cfca06d7SDimitry Andric promoteCall(CB, DirectCallee);
649cfca06d7SDimitry Andric return true;
650044eb2f6SDimitry Andric }
651044eb2f6SDimitry Andric
652044eb2f6SDimitry Andric #undef DEBUG_TYPE
653