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 | |
23 | /// Return true if the given indirect call site can be made to call \p Callee. |
24 | /// |
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); |
32 | |
33 | /// Promote the given indirect call site to unconditionally call \p Callee. |
34 | /// |
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, |
39 | /// if created. |
40 | CallBase &promoteCall(CallBase &CB, Function *Callee, |
41 | CastInst **RetBitCast = nullptr); |
42 | |
43 | /// Promote the given indirect call site to conditionally call \p Callee. |
44 | /// |
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); |
52 | |
53 | /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on |
54 | /// success. |
55 | /// |
56 | /// Look for a pattern like: |
57 | /// |
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) |
68 | /// |
69 | /// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] } |
70 | /// { [3 x i8*] |
71 | /// [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*), |
72 | /// i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] } |
73 | /// |
74 | bool tryPromoteCall(CallBase &CB); |
75 | |
76 | } // end namespace llvm |
77 | |
78 | #endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H |
79 | |