LLVM 22.0.0git
CallPromotionUtils.h
Go to the documentation of this file.
1//===- CallPromotionUtils.h - Utilities for call promotion ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares utilities useful for promoting indirect call sites to
10// direct call sites.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
15#define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
16
19namespace llvm {
20template <typename T> class ArrayRef;
21class Constant;
22class CallBase;
23class CastInst;
24class Function;
25class Instruction;
26class MDNode;
27class Value;
28
29/// Return true if the given indirect call site can be made to call \p Callee.
30///
31/// This function ensures that the number and type of the call site's arguments
32/// and return value match those of the given function. If the types do not
33/// match exactly, they must at least be bitcast compatible. If \p FailureReason
34/// is non-null and the indirect call cannot be promoted, the failure reason
35/// will be stored in it.
36LLVM_ABI bool isLegalToPromote(const CallBase &CB, Function *Callee,
37 const char **FailureReason = nullptr);
38
39/// Promote the given indirect call site to unconditionally call \p Callee.
40///
41/// This function promotes the given call site, returning the direct call or
42/// invoke instruction. If the function type of the call site doesn't match that
43/// of the callee, bitcast instructions are inserted where appropriate. If \p
44/// RetBitCast is non-null, it will be used to store the return value bitcast,
45/// if created.
46LLVM_ABI CallBase &promoteCall(CallBase &CB, Function *Callee,
47 CastInst **RetBitCast = nullptr);
48
49/// Promote the given indirect call site to conditionally call \p Callee. The
50/// promoted direct call instruction is predicated on `CB.getCalledOperand() ==
51/// Callee`.
52///
53/// This function creates an if-then-else structure at the location of the call
54/// site. The original call site is moved into the "else" block. A clone of the
55/// indirect call site is promoted, placed in the "then" block, and returned. If
56/// \p BranchWeights is non-null, it will be used to set !prof metadata on the
57/// new conditional branch.
58LLVM_ABI CallBase &promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
59 MDNode *BranchWeights = nullptr);
60
61LLVM_ABI CallBase *promoteCallWithIfThenElse(CallBase &CB, Function &Callee,
62 PGOContextualProfile &CtxProf);
63
64/// This is similar to `promoteCallWithIfThenElse` except that the condition to
65/// promote a virtual call is that \p VPtr is the same as any of \p
66/// AddressPoints.
67///
68/// This function is expected to be used on virtual calls (a subset of indirect
69/// calls). \p VPtr is the virtual table address stored in the objects, and
70/// \p AddressPoints contains vtable address points. A vtable address point is
71/// a location inside the vtable that's referenced by vpointer in C++ objects.
72///
73/// TODO: sink the address-calculation instructions of indirect callee to the
74/// indirect call fallback after transformation.
75LLVM_ABI CallBase &promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr,
76 Function *Callee,
77 ArrayRef<Constant *> AddressPoints,
78 MDNode *BranchWeights);
79
80/// Try to promote (devirtualize) a virtual call on an Alloca. Return true on
81/// success.
82///
83/// Look for a pattern like:
84///
85/// %o = alloca %class.Impl
86/// %1 = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0
87/// store i32 (...)** bitcast (i8** getelementptr inbounds
88/// ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2)
89/// to i32 (...)**), i32 (...)*** %1
90/// %2 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0
91/// %3 = bitcast %class.Interface* %2 to void (%class.Interface*)***
92/// %vtable.i = load void (%class.Interface*)**, void (%class.Interface*)*** %3
93/// %4 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable.i
94/// call void %4(%class.Interface* nonnull %2)
95///
96/// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] }
97/// { [3 x i8*]
98/// [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*),
99/// i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }
100///
101LLVM_ABI bool tryPromoteCall(CallBase &CB);
102
103/// Predicate and clone the given call site.
104///
105/// This function creates an if-then-else structure at the location of the
106/// call site. The "if" condition compares the call site's called value to
107/// the given callee. The original call site is moved into the "else" block,
108/// and a clone of the call site is placed in the "then" block. The cloned
109/// instruction is returned.
110LLVM_ABI CallBase &versionCallSite(CallBase &CB, Value *Callee,
111 MDNode *BranchWeights);
112
113} // end namespace llvm
114
115#endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
#define LLVM_ABI
Definition: Compiler.h:213
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI bool isLegalToPromote(const CallBase &CB, Function *Callee, const char **FailureReason=nullptr)
Return true if the given indirect call site can be made to call Callee.
LLVM_ABI CallBase & promoteCallWithIfThenElse(CallBase &CB, Function *Callee, MDNode *BranchWeights=nullptr)
Promote the given indirect call site to conditionally call Callee.
LLVM_ABI CallBase & versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights)
Predicate and clone the given call site.
LLVM_ABI CallBase & promoteCall(CallBase &CB, Function *Callee, CastInst **RetBitCast=nullptr)
Promote the given indirect call site to unconditionally call Callee.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI bool tryPromoteCall(CallBase &CB)
Try to promote (devirtualize) a virtual call on an Alloca.
LLVM_ABI CallBase & promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr, Function *Callee, ArrayRef< Constant * > AddressPoints, MDNode *BranchWeights)
This is similar to promoteCallWithIfThenElse except that the condition to promote a virtual call is t...