1313//
1414// ===---------------------------------------------------------------------===//
1515
16+ #include " llvm/CodeGen/XRayInstrumentation.h"
1617#include " llvm/ADT/STLExtras.h"
1718#include " llvm/ADT/SmallVector.h"
1819#include " llvm/CodeGen/MachineBasicBlock.h"
1920#include " llvm/CodeGen/MachineDominators.h"
2021#include " llvm/CodeGen/MachineFunction.h"
22+ #include " llvm/CodeGen/MachineFunctionAnalysis.h"
2123#include " llvm/CodeGen/MachineFunctionPass.h"
2224#include " llvm/CodeGen/MachineInstrBuilder.h"
2325#include " llvm/CodeGen/MachineLoopInfo.h"
26+ #include " llvm/CodeGen/MachinePassManager.h"
2427#include " llvm/CodeGen/TargetInstrInfo.h"
2528#include " llvm/CodeGen/TargetSubtargetInfo.h"
2629#include " llvm/IR/Attributes.h"
@@ -44,11 +47,11 @@ struct InstrumentationOptions {
4447 bool HandleAllReturns;
4548};
4649
47- struct XRayInstrumentation : public MachineFunctionPass {
50+ struct XRayInstrumentationLegacy : public MachineFunctionPass {
4851 static char ID;
4952
50- XRayInstrumentation () : MachineFunctionPass(ID) {
51- initializeXRayInstrumentationPass (*PassRegistry::getPassRegistry ());
53+ XRayInstrumentationLegacy () : MachineFunctionPass(ID) {
54+ initializeXRayInstrumentationLegacyPass (*PassRegistry::getPassRegistry ());
5255 }
5356
5457 void getAnalysisUsage (AnalysisUsage &AU) const override {
@@ -59,6 +62,27 @@ struct XRayInstrumentation : public MachineFunctionPass {
5962 }
6063
6164 bool runOnMachineFunction (MachineFunction &MF) override ;
65+ };
66+
67+ struct XRayInstrumentation {
68+ XRayInstrumentation (MachineDominatorTree *MDT, MachineLoopInfo *MLI)
69+ : MDT(MDT), MLI(MLI) {}
70+
71+ bool run (MachineFunction &MF);
72+
73+ // Methods for use in the NPM and legacy passes, can be removed once migration
74+ // is complete.
75+ static bool alwaysInstrument (Function &F) {
76+ auto InstrAttr = F.getFnAttribute (" function-instrument" );
77+ return InstrAttr.isStringAttribute () &&
78+ InstrAttr.getValueAsString () == " xray-always" ;
79+ }
80+
81+ static bool needMDTAndMLIAnalyses (Function &F) {
82+ auto IgnoreLoopsAttr = F.getFnAttribute (" xray-ignore-loops" );
83+ auto AlwaysInstrument = XRayInstrumentation::alwaysInstrument (F);
84+ return !AlwaysInstrument && !IgnoreLoopsAttr.isValid ();
85+ }
6286
6387private:
6488 // Replace the original RET instruction with the exit sled code ("patchable
@@ -82,6 +106,9 @@ struct XRayInstrumentation : public MachineFunctionPass {
82106 void prependRetWithPatchableExit (MachineFunction &MF,
83107 const TargetInstrInfo *TII,
84108 InstrumentationOptions);
109+
110+ MachineDominatorTree *MDT;
111+ MachineLoopInfo *MLI;
85112};
86113
87114} // end anonymous namespace
@@ -143,11 +170,42 @@ void XRayInstrumentation::prependRetWithPatchableExit(
143170 }
144171}
145172
146- bool XRayInstrumentation::runOnMachineFunction (MachineFunction &MF) {
173+ PreservedAnalyses
174+ XRayInstrumentationPass::run (MachineFunction &MF,
175+ MachineFunctionAnalysisManager &MFAM) {
176+ MachineDominatorTree *MDT = nullptr ;
177+ MachineLoopInfo *MLI = nullptr ;
178+
179+ if (XRayInstrumentation::needMDTAndMLIAnalyses (MF.getFunction ())) {
180+ MDT = MFAM.getCachedResult <MachineDominatorTreeAnalysis>(MF);
181+ MLI = MFAM.getCachedResult <MachineLoopAnalysis>(MF);
182+ }
183+
184+ if (!XRayInstrumentation (MDT, MLI).run (MF))
185+ return PreservedAnalyses::all ();
186+
187+ auto PA = getMachineFunctionPassPreservedAnalyses ();
188+ PA.preserveSet <CFGAnalyses>();
189+ return PA;
190+ }
191+
192+ bool XRayInstrumentationLegacy::runOnMachineFunction (MachineFunction &MF) {
193+ MachineDominatorTree *MDT = nullptr ;
194+ MachineLoopInfo *MLI = nullptr ;
195+ if (XRayInstrumentation::needMDTAndMLIAnalyses (MF.getFunction ())) {
196+ auto *MDTWrapper =
197+ getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
198+ MDT = MDTWrapper ? &MDTWrapper->getDomTree () : nullptr ;
199+ auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
200+ MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
201+ }
202+ return XRayInstrumentation (MDT, MLI).run (MF);
203+ }
204+
205+ bool XRayInstrumentation::run (MachineFunction &MF) {
147206 auto &F = MF.getFunction ();
148207 auto InstrAttr = F.getFnAttribute (" function-instrument" );
149- bool AlwaysInstrument = InstrAttr.isStringAttribute () &&
150- InstrAttr.getValueAsString () == " xray-always" ;
208+ bool AlwaysInstrument = alwaysInstrument (F);
151209 bool NeverInstrument = InstrAttr.isStringAttribute () &&
152210 InstrAttr.getValueAsString () == " xray-never" ;
153211 if (NeverInstrument && !AlwaysInstrument)
@@ -171,18 +229,13 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
171229
172230 if (!IgnoreLoops) {
173231 // Get MachineDominatorTree or compute it on the fly if it's unavailable
174- auto *MDTWrapper =
175- getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
176- auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree () : nullptr ;
177232 MachineDominatorTree ComputedMDT;
178233 if (!MDT) {
179234 ComputedMDT.recalculate (MF);
180235 MDT = &ComputedMDT;
181236 }
182237
183238 // Get MachineLoopInfo or compute it on the fly if it's unavailable
184- auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
185- auto *MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
186239 MachineLoopInfo ComputedMLI;
187240 if (!MLI) {
188241 ComputedMLI.analyze (*MDT);
@@ -272,10 +325,10 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
272325 return true ;
273326}
274327
275- char XRayInstrumentation ::ID = 0 ;
276- char &llvm::XRayInstrumentationID = XRayInstrumentation ::ID;
277- INITIALIZE_PASS_BEGIN (XRayInstrumentation , " xray-instrumentation" ,
328+ char XRayInstrumentationLegacy ::ID = 0 ;
329+ char &llvm::XRayInstrumentationID = XRayInstrumentationLegacy ::ID;
330+ INITIALIZE_PASS_BEGIN (XRayInstrumentationLegacy , " xray-instrumentation" ,
278331 " Insert XRay ops" , false , false )
279332INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
280- INITIALIZE_PASS_END(XRayInstrumentation , " xray-instrumentation" ,
333+ INITIALIZE_PASS_END(XRayInstrumentationLegacy , " xray-instrumentation" ,
281334 " Insert XRay ops" , false , false )
0 commit comments