Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:42

0001 //===- CallPromotionUtils.h - Utilities for call promotion ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares utilities useful for promoting indirect call sites to
0010 // direct call sites.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
0015 #define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
0016 
0017 #include "llvm/Analysis/CtxProfAnalysis.h"
0018 namespace llvm {
0019 template <typename T> class ArrayRef;
0020 class Constant;
0021 class CallBase;
0022 class CastInst;
0023 class Function;
0024 class Instruction;
0025 class MDNode;
0026 class Value;
0027 
0028 /// Return true if the given indirect call site can be made to call \p Callee.
0029 ///
0030 /// This function ensures that the number and type of the call site's arguments
0031 /// and return value match those of the given function. If the types do not
0032 /// match exactly, they must at least be bitcast compatible. If \p FailureReason
0033 /// is non-null and the indirect call cannot be promoted, the failure reason
0034 /// will be stored in it.
0035 bool isLegalToPromote(const CallBase &CB, Function *Callee,
0036                       const char **FailureReason = nullptr);
0037 
0038 /// Promote the given indirect call site to unconditionally call \p Callee.
0039 ///
0040 /// This function promotes the given call site, returning the direct call or
0041 /// invoke instruction. If the function type of the call site doesn't match that
0042 /// of the callee, bitcast instructions are inserted where appropriate. If \p
0043 /// RetBitCast is non-null, it will be used to store the return value bitcast,
0044 /// if created.
0045 CallBase &promoteCall(CallBase &CB, Function *Callee,
0046                       CastInst **RetBitCast = nullptr);
0047 
0048 /// Promote the given indirect call site to conditionally call \p Callee. The
0049 /// promoted direct call instruction is predicated on `CB.getCalledOperand() ==
0050 /// Callee`.
0051 ///
0052 /// This function creates an if-then-else structure at the location of the call
0053 /// site. The original call site is moved into the "else" block. A clone of the
0054 /// indirect call site is promoted, placed in the "then" block, and returned. If
0055 /// \p BranchWeights is non-null, it will be used to set !prof metadata on the
0056 /// new conditional branch.
0057 CallBase &promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
0058                                     MDNode *BranchWeights = nullptr);
0059 
0060 CallBase *promoteCallWithIfThenElse(CallBase &CB, Function &Callee,
0061                                     PGOContextualProfile &CtxProf);
0062 
0063 /// This is similar to `promoteCallWithIfThenElse` except that the condition to
0064 /// promote a virtual call is that \p VPtr is the same as any of \p
0065 /// AddressPoints.
0066 ///
0067 /// This function is expected to be used on virtual calls (a subset of indirect
0068 /// calls). \p VPtr is the virtual table address stored in the objects, and
0069 /// \p AddressPoints contains vtable address points. A vtable address point is
0070 /// a location inside the vtable that's referenced by vpointer in C++ objects.
0071 ///
0072 /// TODO: sink the address-calculation instructions of indirect callee to the
0073 /// indirect call fallback after transformation.
0074 CallBase &promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr,
0075                                    Function *Callee,
0076                                    ArrayRef<Constant *> AddressPoints,
0077                                    MDNode *BranchWeights);
0078 
0079 /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on
0080 /// success.
0081 ///
0082 /// Look for a pattern like:
0083 ///
0084 ///  %o = alloca %class.Impl
0085 ///  %1 = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0
0086 ///  store i32 (...)** bitcast (i8** getelementptr inbounds
0087 ///      ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2)
0088 ///      to i32 (...)**), i32 (...)*** %1
0089 ///  %2 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0
0090 ///  %3 = bitcast %class.Interface* %2 to void (%class.Interface*)***
0091 ///  %vtable.i = load void (%class.Interface*)**, void (%class.Interface*)*** %3
0092 ///  %4 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable.i
0093 ///  call void %4(%class.Interface* nonnull %2)
0094 ///
0095 /// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] }
0096 ///     { [3 x i8*]
0097 ///     [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*),
0098 ///     i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }
0099 ///
0100 bool tryPromoteCall(CallBase &CB);
0101 
0102 /// Predicate and clone the given call site.
0103 ///
0104 /// This function creates an if-then-else structure at the location of the
0105 /// call site. The "if" condition compares the call site's called value to
0106 /// the given callee. The original call site is moved into the "else" block,
0107 /// and a clone of the call site is placed in the "then" block. The cloned
0108 /// instruction is returned.
0109 CallBase &versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights);
0110 
0111 } // end namespace llvm
0112 
0113 #endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H