1 //===- CallPromotionUtils.h - Utilities for call promotion ------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares utilities useful for promoting indirect call sites to
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
23 /// Return true if the given indirect call site can be made to call \p Callee.
25 /// This function ensures that the number and type of the call site's arguments
26 /// and return value match those of the given function. If the types do not
27 /// match exactly, they must at least be bitcast compatible. If \p FailureReason
28 /// is non-null and the indirect call cannot be promoted, the failure reason
29 /// will be stored in it.
30 bool isLegalToPromote(const CallBase &CB, Function *Callee,
31 const char **FailureReason = nullptr);
33 /// Promote the given indirect call site to unconditionally call \p Callee.
35 /// This function promotes the given call site, returning the direct call or
36 /// invoke instruction. If the function type of the call site doesn't match that
37 /// of the callee, bitcast instructions are inserted where appropriate. If \p
38 /// RetBitCast is non-null, it will be used to store the return value bitcast,
40 CallBase &promoteCall(CallBase &CB, Function *Callee,
41 CastInst **RetBitCast = nullptr);
43 /// Promote the given indirect call site to conditionally call \p Callee.
45 /// This function creates an if-then-else structure at the location of the call
46 /// site. The original call site is moved into the "else" block. A clone of the
47 /// indirect call site is promoted, placed in the "then" block, and returned. If
48 /// \p BranchWeights is non-null, it will be used to set !prof metadata on the
49 /// new conditional branch.
50 CallBase &promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
51 MDNode *BranchWeights = nullptr);
53 /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on
56 /// Look for a pattern like:
58 /// %o = alloca %class.Impl
59 /// %1 = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0
60 /// store i32 (...)** bitcast (i8** getelementptr inbounds
61 /// ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2)
62 /// to i32 (...)**), i32 (...)*** %1
63 /// %2 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0
64 /// %3 = bitcast %class.Interface* %2 to void (%class.Interface*)***
65 /// %vtable.i = load void (%class.Interface*)**, void (%class.Interface*)*** %3
66 /// %4 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable.i
67 /// call void %4(%class.Interface* nonnull %2)
69 /// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] }
71 /// [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*),
72 /// i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }
74 bool tryPromoteCall(CallBase &CB);
76 } // end namespace llvm
78 #endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H