LLVM 20.0.0git
IndirectCallVisitor.h
Go to the documentation of this file.
1//===-- IndirectCallVisitor.h - indirect call visitor ---------------------===//
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 implements defines a visitor class and a helper function that find
10// all indirect call-sites in a function.
11
12#ifndef LLVM_ANALYSIS_INDIRECTCALLVISITOR_H
13#define LLVM_ANALYSIS_INDIRECTCALLVISITOR_H
14
15#include "llvm/IR/InstVisitor.h"
16#include <vector>
17
18namespace llvm {
19// Visitor class that finds indirect calls or instructions that gives vtable
20// value, depending on Type.
21struct PGOIndirectCallVisitor : public InstVisitor<PGOIndirectCallVisitor> {
22 enum class InstructionType {
23 kIndirectCall = 0,
24 kVTableVal = 1,
25 };
26 std::vector<CallBase *> IndirectCalls;
27 std::vector<Instruction *> ProfiledAddresses;
29
30 // Given an indirect call instruction, try to find the the following pattern
31 //
32 // %vtable = load ptr, ptr %obj
33 // %vfn = getelementptr inbounds ptr, ptr %vtable, i64 1
34 // %2 = load ptr, ptr %vfn
35 // $call = tail call i32 %2
36 //
37 // A heuristic is used to find the address feeding instructions.
39 assert(CB != nullptr && "Caller guaranteed");
40 if (!CB->isIndirectCall())
41 return nullptr;
42
43 LoadInst *LI = dyn_cast<LoadInst>(CB->getCalledOperand());
44 if (LI != nullptr) {
45 Value *FuncPtr = LI->getPointerOperand(); // GEP (or bitcast)
46 Value *VTablePtr = FuncPtr->stripInBoundsConstantOffsets();
47 // FIXME: Add support in the frontend so LLVM type intrinsics are
48 // emitted without LTO. This way, added intrinsics could filter
49 // non-vtable instructions and reduce instrumentation overhead.
50 // Since a non-vtable profiled address is not within the address
51 // range of vtable objects, it's stored as zero in indexed profiles.
52 // A pass that looks up symbol with an zero hash will (almost) always
53 // find nullptr and skip the actual transformation (e.g., comparison
54 // of symbols). So the performance overhead from non-vtable profiled
55 // address is negligible if exists at all. Comparing loaded address
56 // with symbol address guarantees correctness.
57 if (VTablePtr != nullptr && isa<Instruction>(VTablePtr))
58 return cast<Instruction>(VTablePtr);
59 }
60 return nullptr;
61 }
62
63 void visitCallBase(CallBase &Call) {
64 if (Call.isIndirectCall()) {
65 IndirectCalls.push_back(&Call);
66
68 return;
69
70 Instruction *VPtr =
72 if (VPtr)
73 ProfiledAddresses.push_back(VPtr);
74 }
75 }
76
77private:
79};
80
81inline std::vector<CallBase *> findIndirectCalls(Function &F) {
84 ICV.visit(F);
85 return ICV.IndirectCalls;
86}
87
88inline std::vector<Instruction *> findVTableAddrs(Function &F) {
91 ICV.visit(F);
92 return ICV.ProfiledAddresses;
93}
94
95} // namespace llvm
96
97#endif
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
bool isIndirectCall() const
Return true if the callsite is an indirect call.
Value * getCalledOperand() const
Definition: InstrTypes.h:1342
Base class for instruction visitors.
Definition: InstVisitor.h:78
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:87
An instruction for reading from memory.
Definition: Instructions.h:176
Value * getPointerOperand()
Definition: Instructions.h:255
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
Definition: Value.cpp:706
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::vector< CallBase * > findIndirectCalls(Function &F)
std::vector< Instruction * > findVTableAddrs(Function &F)
std::vector< CallBase * > IndirectCalls
PGOIndirectCallVisitor(InstructionType Type)
std::vector< Instruction * > ProfiledAddresses
static Instruction * tryGetVTableInstruction(CallBase *CB)
void visitCallBase(CallBase &Call)