13
13
//
14
14
// ===---------------------------------------------------------------------===//
15
15
16
+ #include " llvm/CodeGen/XRayInstrumentation.h"
16
17
#include " llvm/ADT/STLExtras.h"
17
18
#include " llvm/ADT/SmallVector.h"
18
19
#include " llvm/CodeGen/MachineBasicBlock.h"
19
20
#include " llvm/CodeGen/MachineDominators.h"
20
21
#include " llvm/CodeGen/MachineFunction.h"
22
+ #include " llvm/CodeGen/MachineFunctionAnalysis.h"
21
23
#include " llvm/CodeGen/MachineFunctionPass.h"
22
24
#include " llvm/CodeGen/MachineInstrBuilder.h"
23
25
#include " llvm/CodeGen/MachineLoopInfo.h"
26
+ #include " llvm/CodeGen/MachinePassManager.h"
24
27
#include " llvm/CodeGen/TargetInstrInfo.h"
25
28
#include " llvm/CodeGen/TargetSubtargetInfo.h"
26
29
#include " llvm/IR/Attributes.h"
@@ -44,11 +47,11 @@ struct InstrumentationOptions {
44
47
bool HandleAllReturns;
45
48
};
46
49
47
- struct XRayInstrumentation : public MachineFunctionPass {
50
+ struct XRayInstrumentationLegacy : public MachineFunctionPass {
48
51
static char ID;
49
52
50
- XRayInstrumentation () : MachineFunctionPass(ID) {
51
- initializeXRayInstrumentationPass (*PassRegistry::getPassRegistry ());
53
+ XRayInstrumentationLegacy () : MachineFunctionPass(ID) {
54
+ initializeXRayInstrumentationLegacyPass (*PassRegistry::getPassRegistry ());
52
55
}
53
56
54
57
void getAnalysisUsage (AnalysisUsage &AU) const override {
@@ -59,6 +62,27 @@ struct XRayInstrumentation : public MachineFunctionPass {
59
62
}
60
63
61
64
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
+ }
62
86
63
87
private:
64
88
// Replace the original RET instruction with the exit sled code ("patchable
@@ -82,6 +106,9 @@ struct XRayInstrumentation : public MachineFunctionPass {
82
106
void prependRetWithPatchableExit (MachineFunction &MF,
83
107
const TargetInstrInfo *TII,
84
108
InstrumentationOptions);
109
+
110
+ MachineDominatorTree *MDT;
111
+ MachineLoopInfo *MLI;
85
112
};
86
113
87
114
} // end anonymous namespace
@@ -143,11 +170,43 @@ void XRayInstrumentation::prependRetWithPatchableExit(
143
170
}
144
171
}
145
172
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) {
147
207
auto &F = MF.getFunction ();
148
208
auto InstrAttr = F.getFnAttribute (" function-instrument" );
149
- bool AlwaysInstrument = InstrAttr.isStringAttribute () &&
150
- InstrAttr.getValueAsString () == " xray-always" ;
209
+ bool AlwaysInstrument = alwaysInstrument (F);
151
210
bool NeverInstrument = InstrAttr.isStringAttribute () &&
152
211
InstrAttr.getValueAsString () == " xray-never" ;
153
212
if (NeverInstrument && !AlwaysInstrument)
@@ -171,18 +230,19 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
171
230
172
231
if (!IgnoreLoops) {
173
232
// 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;
177
236
MachineDominatorTree ComputedMDT;
178
237
if (!MDT) {
179
238
ComputedMDT.recalculate (MF);
180
239
MDT = &ComputedMDT;
181
240
}
182
241
183
242
// 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;
186
246
MachineLoopInfo ComputedMLI;
187
247
if (!MLI) {
188
248
ComputedMLI.analyze (*MDT);
@@ -272,10 +332,10 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
272
332
return true ;
273
333
}
274
334
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" ,
278
338
" Insert XRay ops" , false , false )
279
339
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
280
- INITIALIZE_PASS_END(XRayInstrumentation , " xray-instrumentation" ,
340
+ INITIALIZE_PASS_END(XRayInstrumentationLegacy , " xray-instrumentation" ,
281
341
" Insert XRay ops" , false , false )
0 commit comments