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