Lines Matching defs:IR

10 /// This file defines IR-printing pass instrumentation callbacks as well as
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/PassInstrumentation.h"
26 #include "llvm/IR/PassManager.h"
27 #include "llvm/IR/PrintPasses.h"
28 #include "llvm/IR/Verifier.h"
48 // An option that prints out the IR after passes, similar to
49 // -print-after-all except that it only prints the IR after passes that
50 // change the IR. Those passes that do not make changes to the IR are
51 // reported as not making any changes. In addition, the initial IR is
54 // that actually change the IR and other passes are reported as filtered out.
56 // no IR reported) or the changed IR will be reported. Also, the
60 // or indicating that the IR has been filtered out. The extra options
64 // the IR as it was before each pass that changed it. The optional
65 // value of quiet will only report when the IR changes, suppressing
66 // all other messages, including the initial IR. The values "diff" and
106 cl::desc("Only consider IR changes for passes whose names "
190 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
192 const Module *unwrapModule(Any IR, bool Force = false) {
193 if (any_isa<const Module *>(IR))
194 return any_cast<const Module *>(IR);
196 if (any_isa<const Function *>(IR)) {
197 const Function *F = any_cast<const Function *>(IR);
204 if (any_isa<const LazyCallGraph::SCC *>(IR)) {
205 const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
216 if (any_isa<const Loop *>(IR)) {
217 const Loop *L = any_cast<const Loop *>(IR);
224 llvm_unreachable("Unknown IR unit");
260 std::string getIRName(Any IR) {
261 if (any_isa<const Module *>(IR))
264 if (any_isa<const Function *>(IR)) {
265 const Function *F = any_cast<const Function *>(IR);
269 if (any_isa<const LazyCallGraph::SCC *>(IR)) {
270 const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
274 if (any_isa<const Loop *>(IR)) {
275 const Loop *L = any_cast<const Loop *>(IR);
282 llvm_unreachable("Unknown wrapped IR type");
301 bool shouldPrintIR(Any IR) {
302 if (any_isa<const Module *>(IR)) {
303 const Module *M = any_cast<const Module *>(IR);
307 if (any_isa<const Function *>(IR)) {
308 const Function *F = any_cast<const Function *>(IR);
312 if (any_isa<const LazyCallGraph::SCC *>(IR)) {
313 const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
317 if (any_isa<const Loop *>(IR)) {
318 const Loop *L = any_cast<const Loop *>(IR);
321 llvm_unreachable("Unknown wrapped IR type");
324 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
326 void unwrapAndPrint(raw_ostream &OS, Any IR,
328 if (!shouldPrintIR(IR))
332 auto *M = unwrapModule(IR);
338 if (any_isa<const Module *>(IR)) {
339 const Module *M = any_cast<const Module *>(IR);
344 if (any_isa<const Function *>(IR)) {
345 const Function *F = any_cast<const Function *>(IR);
350 if (any_isa<const LazyCallGraph::SCC *>(IR)) {
351 const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
356 if (any_isa<const Loop *>(IR)) {
357 const Loop *L = any_cast<const Loop *>(IR);
361 llvm_unreachable("Unknown wrapped IR type");
393 // Return true when this is a pass on IR for which printing
396 bool ChangeReporter<IRUnitT>::isInteresting(Any IR, StringRef PassID) {
399 if (any_isa<const Function *>(IR))
400 return isInterestingFunction(*any_cast<const Function *>(IR));
405 void ChangeReporter<IRUnitT>::saveIRBeforePass(Any IR, StringRef PassID) {
407 // are not given the IR so it cannot be determined whether the pass was for
411 if (!isInteresting(IR, PassID))
413 // Is this the initial IR?
417 handleInitialIR(IR);
420 // Save the IR representation on the stack.
422 generateIRRepresentation(IR, PassID, Data);
426 void ChangeReporter<IRUnitT>::handleIRAfterPass(Any IR, StringRef PassID) {
429 std::string Name = getIRName(IR);
434 } else if (!isInteresting(IR, PassID)) {
442 generateIRRepresentation(IR, PassID, After);
444 // Was there a change in IR?
449 handleAfter(PassID, Name, Before, After, IR);
460 // get the IR in the call. Also, the output is just alternate
471 [this](StringRef P, Any IR) { saveIRBeforePass(IR, P); });
474 [this](StringRef P, Any IR, const PreservedAnalyses &) {
475 handleIRAfterPass(IR, P);
494 void TextChangeReporter<IRUnitT>::handleInitialIR(Any IR) {
497 auto *M = unwrapModule(IR, /*Force=*/true);
499 Out << "*** IR Dump At Start ***\n";
507 Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n",
513 Out << formatv("*** IR Pass {0} invalidated ***\n", PassID);
520 formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name);
527 Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name);
538 void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
541 unwrapAndPrint(OS, IR,
549 // Report the IR before the changes when requested.
551 Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n"
557 Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n";
561 Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After;
636 void ChangedIRComparer::compare(Any IR, StringRef Prefix, StringRef PassID,
638 if (!getModuleForComparison(IR)) {
661 void ChangedIRComparer::analyzeIR(Any IR, ChangedIRData &Data) {
662 if (const Module *M = getModuleForComparison(IR)) {
670 if (any_isa<const Function *>(IR))
671 F = any_cast<const Function *>(IR);
673 assert(any_isa<const Loop *>(IR) && "Unknown IR unit.");
674 const Loop *L = any_cast<const Loop *>(IR);
677 assert(F && "Unknown IR unit.");
681 const Module *ChangedIRComparer::getModuleForComparison(Any IR) {
682 if (any_isa<const Module *>(IR))
683 return any_cast<const Module *>(IR);
684 if (any_isa<const LazyCallGraph::SCC *>(IR))
685 return any_cast<const LazyCallGraph::SCC *>(IR)
711 void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
713 const Module *M = unwrapModule(IR);
714 ModuleDescStack.emplace_back(M, getIRName(IR), PassID);
725 void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
734 pushModuleDesc(PassID, IR);
739 if (!shouldPrintIR(IR))
742 dbgs() << "*** IR Dump Before " << PassID << " on " << getIRName(IR)
744 unwrapAndPrint(dbgs(), IR);
747 void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
762 if (!shouldPrintIR(IR))
765 dbgs() << "*** IR Dump After " << PassID << " on " << getIRName(IR)
767 unwrapAndPrint(dbgs(), IR);
789 formatv("*** IR Dump After {0} on {1} (invalidated) ***", PassID, IRName);
819 [this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
823 [this](StringRef P, Any IR, const PreservedAnalyses &) {
824 this->printAfterPass(P, IR);
836 [this](StringRef P, Any IR) { return this->shouldRun(P, IR); });
839 bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
841 if (any_isa<const Function *>(IR)) {
842 F = any_cast<const Function *>(IR);
843 } else if (any_isa<const Loop *>(IR)) {
844 F = any_cast<const Loop *>(IR)->getHeader()->getParent();
858 PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, Any IR) {
859 return isIgnored(PassID) || OptBisector->checkPass(PassID, getIRName(IR));
883 [this, SpecialPasses](StringRef PassID, Any IR) {
887 print() << "Skipping pass: " << PassID << " on " << getIRName(IR)
891 StringRef PassID, Any IR) {
895 print() << "Running pass: " << PassID << " on " << getIRName(IR) << "\n";
899 [this, SpecialPasses](StringRef PassID, Any IR,
907 [this, SpecialPasses](StringRef PassID, Any IR) {
915 PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) {
916 print() << "Running analysis: " << PassID << " on " << getIRName(IR)
921 [this](StringRef PassID, Any IR) { Indent -= 2; });
922 PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) {
923 print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR)
1088 [this, &FAM](StringRef P, Any IR) {
1091 if (!any_isa<const Function *>(IR))
1094 const auto *F = any_cast<const Function *>(IR);
1107 checkCFG](StringRef P, Any IR,
1113 if (!any_isa<const Function *>(IR))
1120 const auto *F = any_cast<const Function *>(IR);
1131 [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1134 if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) {
1136 if (any_isa<const Loop *>(IR))
1137 F = any_cast<const Loop *>(IR)->getHeader()->getParent();
1139 F = any_cast<const Function *>(IR);
1145 } else if (any_isa<const Module *>(IR) ||
1146 any_isa<const LazyCallGraph::SCC *>(IR)) {
1148 if (any_isa<const LazyCallGraph::SCC *>(IR))
1149 M = any_cast<const LazyCallGraph::SCC *>(IR)
1154 M = any_cast<const Module *>(IR);
1166 void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID,
1168 ChangedIRComparer::analyzeIR(IR, D);
1173 const ChangedIRData &After, Any IR) {
1175 formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
1178 .compare(IR, "", PassID, Name);
1193 Out << "\n*** IR for function " << Name << " ***\n";