19
19
#include " llvm/Analysis/CallGraphSCCPass.h"
20
20
#include " llvm/Analysis/LazyCallGraph.h"
21
21
#include " llvm/Analysis/LoopInfo.h"
22
+ #include " llvm/CodeGen/FreeMachineFunction.h"
23
+ #include " llvm/CodeGen/MIRPrinter.h"
22
24
#include " llvm/CodeGen/MachineFunction.h"
25
+ #include " llvm/CodeGen/MachineModuleInfo.h"
23
26
#include " llvm/IR/Constants.h"
24
27
#include " llvm/IR/Function.h"
25
28
#include " llvm/IR/Module.h"
@@ -180,6 +183,12 @@ const Module *unwrapModule(Any IR, bool Force = false) {
180
183
return F->getParent ();
181
184
}
182
185
186
+ if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
187
+ if (!Force && !isFunctionInPrintList (MF->getName ()))
188
+ return nullptr ;
189
+ return MF->getFunction ().getParent ();
190
+ }
191
+
183
192
llvm_unreachable (" Unknown IR unit" );
184
193
}
185
194
@@ -215,6 +224,12 @@ void printIR(raw_ostream &OS, const Loop *L) {
215
224
printLoop (const_cast <Loop &>(*L), OS);
216
225
}
217
226
227
+ void printIR (raw_ostream &OS, const MachineFunction *MF) {
228
+ if (!isFunctionInPrintList (MF->getName ()))
229
+ return ;
230
+ MF->print (OS);
231
+ }
232
+
218
233
std::string getIRName (Any IR) {
219
234
if (unwrapIR<Module>(IR))
220
235
return " [module]" ;
@@ -262,6 +277,9 @@ bool shouldPrintIR(Any IR) {
262
277
263
278
if (const auto *L = unwrapIR<Loop>(IR))
264
279
return isFunctionInPrintList (L->getHeader ()->getParent ()->getName ());
280
+
281
+ if (const auto *MF = unwrapIR<MachineFunction>(IR))
282
+ return isFunctionInPrintList (MF->getName ());
265
283
llvm_unreachable (" Unknown wrapped IR type" );
266
284
}
267
285
@@ -275,6 +293,14 @@ void unwrapAndPrint(raw_ostream &OS, Any IR) {
275
293
auto *M = unwrapModule (IR);
276
294
assert (M && " should have unwrapped module" );
277
295
printIR (OS, M);
296
+
297
+ if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
298
+ auto &MMI = MF->getMMI ();
299
+ for (const auto &F : *M) {
300
+ if (auto *MF = MMI.getMachineFunction (F))
301
+ MF->print (OS);
302
+ }
303
+ }
278
304
return ;
279
305
}
280
306
@@ -297,6 +323,11 @@ void unwrapAndPrint(raw_ostream &OS, Any IR) {
297
323
printIR (OS, L);
298
324
return ;
299
325
}
326
+
327
+ if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
328
+ printIR (OS, MF);
329
+ return ;
330
+ }
300
331
llvm_unreachable (" Unknown wrapped IR type" );
301
332
}
302
333
@@ -305,7 +336,8 @@ bool isIgnored(StringRef PassID) {
305
336
return isSpecialPass (PassID,
306
337
{" PassManager" , " PassAdaptor" , " AnalysisManagerProxy" ,
307
338
" DevirtSCCRepeatedPass" , " ModuleInlinerWrapperPass" ,
308
- " VerifierPass" , " PrintModulePass" });
339
+ " VerifierPass" , " PrintModulePass" , " PrintMIRPass" ,
340
+ " PrintMIRPreparePass" });
309
341
}
310
342
311
343
std::string makeHTMLReady (StringRef SR) {
@@ -409,6 +441,10 @@ template <typename T>
409
441
void ChangeReporter<T>::handleInvalidatedPass(StringRef PassID) {
410
442
assert (!BeforeStack.empty () && " Unexpected empty stack encountered." );
411
443
444
+ // Prepare to process the next MIR.
445
+ if (PassID == FreeMachineFunctionPass::name ())
446
+ InitialIR = true ;
447
+
412
448
// Always flag it as invalidated as we cannot determine when
413
449
// a pass for a filtered function is invalidated since we do not
414
450
// get the IR in the call. Also, the output is just alternate
@@ -440,6 +476,13 @@ TextChangeReporter<T>::TextChangeReporter(bool Verbose)
440
476
: ChangeReporter<T>(Verbose), Out(dbgs()) {}
441
477
442
478
template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
479
+ // MIR is special, not all MIRs are available at the beginning.
480
+ if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
481
+ Out << " *** MIR Dump At Start ***\n " ;
482
+ MF->print (Out);
483
+ return ;
484
+ }
485
+
443
486
// Always print the module.
444
487
// Unwrap and print directly to avoid filtering problems in general routines.
445
488
auto *M = unwrapModule (IR, /* Force=*/ true );
@@ -664,20 +707,38 @@ template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
664
707
return ;
665
708
}
666
709
667
- const auto *F = unwrapIR<Function>(IR);
668
- if (!F) {
669
- const auto *L = unwrapIR<Loop>(IR);
670
- assert (L && " Unknown IR unit." );
671
- F = L->getHeader ()->getParent ();
710
+ if (const auto *F = unwrapIR<Function>(IR)) {
711
+ generateFunctionData (Data, *F);
712
+ return ;
713
+ }
714
+
715
+ if (const auto *L = unwrapIR<Loop>(IR)) {
716
+ auto *F = L->getHeader ()->getParent ();
717
+ generateFunctionData (Data, *F);
718
+ return ;
719
+ }
720
+
721
+ if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
722
+ generateFunctionData (Data, *MF);
723
+ return ;
672
724
}
673
- assert (F && " Unknown IR unit." );
674
- generateFunctionData (Data, *F);
725
+
726
+ llvm_unreachable (" Unknown IR unit" );
727
+ }
728
+
729
+ static bool shouldGenerateData (const Function &F) {
730
+ return !F.isDeclaration () && isFunctionInPrintList (F.getName ());
731
+ }
732
+
733
+ static bool shouldGenerateData (const MachineFunction &MF) {
734
+ return isFunctionInPrintList (MF.getName ());
675
735
}
676
736
677
737
template <typename T>
678
- bool IRComparer<T>::generateFunctionData(IRDataT<T> &Data, const Function &F) {
679
- if (!F.isDeclaration () && isFunctionInPrintList (F.getName ())) {
680
- FuncDataT<T> FD (F.getEntryBlock ().getName ().str ());
738
+ template <typename FunctionT>
739
+ bool IRComparer<T>::generateFunctionData(IRDataT<T> &Data, const FunctionT &F) {
740
+ if (shouldGenerateData (F)) {
741
+ FuncDataT<T> FD (F.front ().getName ().str ());
681
742
int I = 0 ;
682
743
for (const auto &B : F) {
683
744
std::string BBName = B.getName ().str ();
@@ -722,6 +783,12 @@ static SmallString<32> getIRFileDisplayName(Any IR) {
722
783
ResultStream << " -loop-" ;
723
784
stable_hash LoopNameHash = stable_hash_combine_string (L->getName ());
724
785
write_hex (ResultStream, LoopNameHash, HexPrintStyle::Lower, MaxHashWidth);
786
+ } else if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
787
+ ResultStream << " -machine-function-" ;
788
+ stable_hash MachineFunctionNameHash =
789
+ stable_hash_combine_string (MF->getName ());
790
+ write_hex (ResultStream, MachineFunctionNameHash, HexPrintStyle::Lower,
791
+ MaxHashWidth);
725
792
} else {
726
793
llvm_unreachable (" Unknown wrapped IR type" );
727
794
}
@@ -2122,6 +2189,11 @@ DCData::DCData(const BasicBlock &B) {
2122
2189
addSuccessorLabel (Succ->getName ().str (), " " );
2123
2190
}
2124
2191
2192
+ DCData::DCData (const MachineBasicBlock &B) {
2193
+ for (const MachineBasicBlock *Succ : successors (&B))
2194
+ addSuccessorLabel (Succ->getName ().str (), " " );
2195
+ }
2196
+
2125
2197
DotCfgChangeReporter::DotCfgChangeReporter (bool Verbose)
2126
2198
: ChangeReporter<IRDataT<DCData>>(Verbose) {}
2127
2199
@@ -2188,7 +2260,7 @@ std::string DotCfgChangeReporter::genHTML(StringRef Text, StringRef DotFile,
2188
2260
2189
2261
void DotCfgChangeReporter::handleInitialIR (Any IR) {
2190
2262
assert (HTML && " Expected outstream to be set" );
2191
- *HTML << " <button type=\" button\" class=\" collapsible\" >0 . "
2263
+ *HTML << " <button type=\" button\" class=\" collapsible\" >" << N << " . "
2192
2264
<< " Initial IR (by function)</button>\n "
2193
2265
<< " <div class=\" content\" >\n "
2194
2266
<< " <p>\n " ;
0 commit comments