41struct InstrumentationOptions {
47 bool HandleAllReturns;
53 XRayInstrumentationLegacy() : MachineFunctionPass(ID) {}
55 void getAnalysisUsage(AnalysisUsage &AU)
const override {
63struct XRayInstrumentation {
64 XRayInstrumentation(MachineDominatorTree *MDT, MachineLoopInfo *MLI)
65 : MDT(MDT), MLI(MLI) {}
71 static bool alwaysInstrument(
Function &
F) {
72 auto InstrAttr =
F.getFnAttribute(
"function-instrument");
73 return InstrAttr.isStringAttribute() &&
74 InstrAttr.getValueAsString() ==
"xray-always";
77 static bool needMDTAndMLIAnalyses(
Function &
F) {
78 auto IgnoreLoopsAttr =
F.getFnAttribute(
"xray-ignore-loops");
79 auto AlwaysInstrument = XRayInstrumentation::alwaysInstrument(
F);
80 return !AlwaysInstrument && !IgnoreLoopsAttr.isValid();
91 const TargetInstrInfo *
TII,
92 InstrumentationOptions);
103 const TargetInstrInfo *
TII,
104 InstrumentationOptions);
106 MachineDominatorTree *MDT;
107 MachineLoopInfo *MLI;
112void XRayInstrumentation::replaceRetWithPatchableRet(
114 InstrumentationOptions
op) {
117 SmallVector<MachineInstr *, 4> Terminators;
118 for (
auto &
MBB : MF) {
122 (
op.HandleAllReturns ||
T.getOpcode() ==
TII->getReturnOpcode())) {
125 Opc = TargetOpcode::PATCHABLE_RET;
130 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
135 for (
auto &MO :
T.operands())
138 if (
T.shouldUpdateAdditionalCallInfo())
139 MF.eraseAdditionalCallInfo(&
T);
144 for (
auto &
I : Terminators)
145 I->eraseFromParent();
148void XRayInstrumentation::prependRetWithPatchableExit(
150 InstrumentationOptions
op) {
155 (
op.HandleAllReturns ||
T.getOpcode() ==
TII->getReturnOpcode())) {
156 Opc = TargetOpcode::PATCHABLE_FUNCTION_EXIT;
159 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
175 if (XRayInstrumentation::needMDTAndMLIAnalyses(MF.
getFunction())) {
180 if (!XRayInstrumentation(MDT, MLI).
run(MF))
188bool XRayInstrumentationLegacy::runOnMachineFunction(
MachineFunction &MF) {
191 if (XRayInstrumentation::needMDTAndMLIAnalyses(MF.
getFunction())) {
193 getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
194 MDT = MDTWrapper ? &MDTWrapper->getDomTree() :
nullptr;
195 auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
196 MLI = MLIWrapper ? &MLIWrapper->getLI() :
nullptr;
198 return XRayInstrumentation(MDT, MLI).run(MF);
204 bool AlwaysInstrument = alwaysInstrument(
F);
205 bool NeverInstrument = InstrAttr.isStringAttribute() &&
206 InstrAttr.getValueAsString() ==
"xray-never";
207 if (NeverInstrument && !AlwaysInstrument)
209 auto IgnoreLoopsAttr =
F.getFnAttribute(
"xray-ignore-loops");
212 if (!AlwaysInstrument) {
213 bool IgnoreLoops = IgnoreLoopsAttr.isValid();
214 XRayThreshold =
F.getFnAttributeAsParsedInteger(
215 "xray-instruction-threshold", std::numeric_limits<uint64_t>::max());
216 if (XRayThreshold == std::numeric_limits<uint64_t>::max())
221 for (
const auto &
MBB : MF)
224 bool TooFewInstrs = MICount < XRayThreshold;
229 MachineDominatorTree ComputedMDT;
230 MachineLoopInfo ComputedMLI;
232 ComputedMLI.
calculate(MF, [&]() ->
const MachineDominatorTree & {
245 if (MLI->
empty() && TooFewInstrs)
247 }
else if (TooFewInstrs) {
256 MF, [&](
const MachineBasicBlock &
MBB) {
return !
MBB.
empty(); });
260 auto *
TII = MF.getSubtarget().getInstrInfo();
261 auto &FirstMBB = *MBI;
262 auto &FirstMI = *FirstMBB.begin();
264 if (!MF.getSubtarget().isXRaySupported()) {
266 const Function &Fn = FirstMBB.getParent()->getFunction();
268 Fn,
"An attempt to perform XRay instrumentation for an"
269 " unsupported target."));
274 if (!
F.hasFnAttribute(
"xray-skip-entry")) {
277 BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
278 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
281 if (!
F.hasFnAttribute(
"xray-skip-exit")) {
282 switch (MF.getTarget().getTargetTriple().getArch()) {
283 case Triple::ArchType::arm:
284 case Triple::ArchType::thumb:
285 case Triple::ArchType::aarch64:
286 case Triple::ArchType::hexagon:
287 case Triple::ArchType::loongarch64:
288 case Triple::ArchType::mips:
289 case Triple::ArchType::mipsel:
290 case Triple::ArchType::mips64:
291 case Triple::ArchType::mips64el:
292 case Triple::ArchType::riscv32:
293 case Triple::ArchType::riscv64: {
295 InstrumentationOptions
op;
297 op.HandleTailcall = MF.getTarget().getTargetTriple().isAArch64() ||
298 MF.getTarget().getTargetTriple().isRISCV();
299 op.HandleAllReturns =
true;
300 prependRetWithPatchableExit(MF,
TII,
op);
303 case Triple::ArchType::ppc64le:
304 case Triple::ArchType::systemz: {
306 InstrumentationOptions
op;
307 op.HandleTailcall =
false;
308 op.HandleAllReturns =
true;
309 replaceRetWithPatchableRet(MF,
TII,
op);
315 InstrumentationOptions
op;
316 op.HandleTailcall =
true;
317 op.HandleAllReturns =
false;
318 replaceRetWithPatchableRet(MF,
TII,
op);
326char XRayInstrumentationLegacy::ID = 0;
329 "Insert XRay ops",
false,
false)
This file contains the simple types necessary to represent the attributes associated with functions a...
const HexagonInstrInfo * TII
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the SmallVector class.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
bool isTailCall(const MachineInstr &MI) const override
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
iterator_range< iterator > terminators()
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Analysis pass that exposes the MachineLoopInfo for a machine function.
LLVM_ABI void calculate(MachineDominatorTree &MDT)
Calculate the natural loop information.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void push_back(const T &Elt)
TargetInstrInfo - Interface to description of machine instruction set.
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
DXILDebugInfoMap run(Module &M)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & XRayInstrumentationID
This pass inserts the XRay instrumentation sleds if they are supported by the target platform.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.