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 | |
17 | namespace llvm { |
18 | class CallBase; |
19 | class CastInst; |
20 | class Function; |
21 | class MDNode; |
22 | class Value; |
23 | |
24 | /// Return true if the given indirect call site can be made to call \p Callee. |
25 | /// |
26 | /// This function ensures that the number and type of the call site's arguments |
27 | /// and return value match those of the given function. If the types do not |
28 | /// match exactly, they must at least be bitcast compatible. If \p FailureReason |
29 | /// is non-null and the indirect call cannot be promoted, the failure reason |
30 | /// will be stored in it. |
31 | bool isLegalToPromote(const CallBase &CB, Function *Callee, |
32 | const char **FailureReason = nullptr); |
33 | |
34 | /// Promote the given indirect call site to unconditionally call \p Callee. |
35 | /// |
36 | /// This function promotes the given call site, returning the direct call or |
37 | /// invoke instruction. If the function type of the call site doesn't match that |
38 | /// of the callee, bitcast instructions are inserted where appropriate. If \p |
39 | /// RetBitCast is non-null, it will be used to store the return value bitcast, |
40 | /// if created. |
41 | CallBase &promoteCall(CallBase &CB, Function *Callee, |
42 | CastInst **RetBitCast = nullptr); |
43 | |
44 | /// Promote the given indirect call site to conditionally call \p Callee. |
45 | /// |
46 | /// This function creates an if-then-else structure at the location of the call |
47 | /// site. The original call site is moved into the "else" block. A clone of the |
48 | /// indirect call site is promoted, placed in the "then" block, and returned. If |
49 | /// \p BranchWeights is non-null, it will be used to set !prof metadata on the |
50 | /// new conditional branch. |
51 | CallBase &promoteCallWithIfThenElse(CallBase &CB, Function *Callee, |
52 | MDNode *BranchWeights = nullptr); |
53 | |
54 | /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on |
55 | /// success. |
56 | /// |
57 | /// Look for a pattern like: |
58 | /// |
59 | /// %o = alloca %class.Impl |
60 | /// %1 = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0 |
61 | /// store i32 (...)** bitcast (i8** getelementptr inbounds |
62 | /// ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2) |
63 | /// to i32 (...)**), i32 (...)*** %1 |
64 | /// %2 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0 |
65 | /// %3 = bitcast %class.Interface* %2 to void (%class.Interface*)*** |
66 | /// %vtable.i = load void (%class.Interface*)**, void (%class.Interface*)*** %3 |
67 | /// %4 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable.i |
68 | /// call void %4(%class.Interface* nonnull %2) |
69 | /// |
70 | /// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] } |
71 | /// { [3 x i8*] |
72 | /// [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*), |
73 | /// i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] } |
74 | /// |
75 | bool tryPromoteCall(CallBase &CB); |
76 | |
77 | /// Predicate and clone the given call site. |
78 | /// |
79 | /// This function creates an if-then-else structure at the location of the call |
80 | /// site. The "if" condition compares the call site's called value to the given |
81 | /// callee. The original call site is moved into the "else" block, and a clone |
82 | /// of the call site is placed in the "then" block. The cloned instruction is |
83 | /// returned. |
84 | CallBase &versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights); |
85 | |
86 | } // end namespace llvm |
87 | |
88 | #endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H |
89 | |