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,43 @@ 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+ return getMachineFunctionPassPreservedAnalyses ()
188+ .preserve <MachineDominatorTreeAnalysis>()
189+ .preserve <MachineLoopAnalysis>()
190+ .preserveSet <CFGAnalyses>();
191+ }
192+
193+ bool XRayInstrumentationLegacy::runOnMachineFunction (MachineFunction &MF) {
194+ MachineDominatorTree *MDT = nullptr ;
195+ MachineLoopInfo *MLI = nullptr ;
196+ if (XRayInstrumentation::needMDTAndMLIAnalyses (MF.getFunction ())) {
197+ auto *MDTWrapper =
198+ getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
199+ MDT = MDTWrapper ? &MDTWrapper->getDomTree () : nullptr ;
200+ auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
201+ MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
202+ }
203+ return XRayInstrumentation (MDT, MLI).run (MF);
204+ }
205+
206+ bool XRayInstrumentation::run (MachineFunction &MF) {
147207 auto &F = MF.getFunction ();
148208 auto InstrAttr = F.getFnAttribute (" function-instrument" );
149- bool AlwaysInstrument = InstrAttr.isStringAttribute () &&
150- InstrAttr.getValueAsString () == " xray-always" ;
209+ bool AlwaysInstrument = alwaysInstrument (F);
151210 bool NeverInstrument = InstrAttr.isStringAttribute () &&
152211 InstrAttr.getValueAsString () == " xray-never" ;
153212 if (NeverInstrument && !AlwaysInstrument)
@@ -171,18 +230,19 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
171230
172231 if (!IgnoreLoops) {
173232 // 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 ;
233+ // auto *MDTWrapper =
234+ // getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
235+ // auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
177236 MachineDominatorTree ComputedMDT;
178237 if (!MDT) {
179238 ComputedMDT.recalculate (MF);
180239 MDT = &ComputedMDT;
181240 }
182241
183242 // Get MachineLoopInfo or compute it on the fly if it's unavailable
184- auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
185- auto *MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
243+ // auto *MLIWrapper =
244+ // getAnalysisIfAvailable<MachineLoopInfoWrapperPass>(); auto *MLI =
245+ // MLIWrapper ? &MLIWrapper->getLI() : nullptr;
186246 MachineLoopInfo ComputedMLI;
187247 if (!MLI) {
188248 ComputedMLI.analyze (*MDT);
@@ -272,10 +332,10 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
272332 return true ;
273333}
274334
275- char XRayInstrumentation ::ID = 0 ;
276- char &llvm::XRayInstrumentationID = XRayInstrumentation ::ID;
277- INITIALIZE_PASS_BEGIN (XRayInstrumentation , " xray-instrumentation" ,
335+ char XRayInstrumentationLegacy ::ID = 0 ;
336+ char &llvm::XRayInstrumentationID = XRayInstrumentationLegacy ::ID;
337+ INITIALIZE_PASS_BEGIN (XRayInstrumentationLegacy , " xray-instrumentation" ,
278338 " Insert XRay ops" , false , false )
279339INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
280- INITIALIZE_PASS_END(XRayInstrumentation , " xray-instrumentation" ,
340+ INITIALIZE_PASS_END(XRayInstrumentationLegacy , " xray-instrumentation" ,
281341 " Insert XRay ops" , false , false )
0 commit comments