1 //===- Standard pass instrumentations handling ----------------*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file defines IR-printing pass instrumentation callbacks as well as
11 /// StandardInstrumentations class that manages standard pass instrumentations.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Passes/StandardInstrumentations.h"
16 #include "llvm/ADT/Any.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/CallGraphSCCPass.h"
20 #include "llvm/Analysis/LazyCallGraph.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/LegacyPassManager.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/PassInstrumentation.h"
27 #include "llvm/IR/PassManager.h"
28 #include "llvm/IR/PrintPasses.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/CrashRecoveryContext.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/FormatVariadic.h"
34 #include "llvm/Support/GraphWriter.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Program.h"
37 #include "llvm/Support/Regex.h"
38 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include <unordered_map>
41 #include <unordered_set>
42 #include <utility>
43 #include <vector>
44 
45 using namespace llvm;
46 
47 cl::opt<bool> PreservedCFGCheckerInstrumentation::VerifyPreservedCFG(
48     "verify-cfg-preserved", cl::Hidden,
49 #ifdef NDEBUG
50     cl::init(false)
51 #else
52     cl::init(true)
53 #endif
54     );
55 
56 // An option that supports the -print-changed option.  See
57 // the description for -print-changed for an explanation of the use
58 // of this option.  Note that this option has no effect without -print-changed.
59 static cl::list<std::string>
60     PrintPassesList("filter-passes", cl::value_desc("pass names"),
61                     cl::desc("Only consider IR changes for passes whose names "
62                              "match for the print-changed option"),
63                     cl::CommaSeparated, cl::Hidden);
64 // An option that supports the -print-changed option.  See
65 // the description for -print-changed for an explanation of the use
66 // of this option.  Note that this option has no effect without -print-changed.
67 static cl::opt<bool>
68     PrintChangedBefore("print-before-changed",
69                        cl::desc("Print before passes that change them"),
70                        cl::init(false), cl::Hidden);
71 
72 // An option for specifying the diff used by print-changed=[diff | diff-quiet]
73 static cl::opt<std::string>
74     DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"),
75                cl::desc("system diff used by change reporters"));
76 
77 // An option for specifying the dot used by
78 // print-changed=[dot-cfg | dot-cfg-quiet]
79 static cl::opt<std::string>
80     DotBinary("print-changed-dot-path", cl::Hidden, cl::init("dot"),
81               cl::desc("system dot used by change reporters"));
82 
83 // An option that determines the colour used for elements that are only
84 // in the before part.  Must be a colour named in appendix J of
85 // https://graphviz.org/pdf/dotguide.pdf
86 cl::opt<std::string>
87     BeforeColour("dot-cfg-before-color",
88                  cl::desc("Color for dot-cfg before elements."), cl::Hidden,
89                  cl::init("red"));
90 // An option that determines the colour used for elements that are only
91 // in the after part.  Must be a colour named in appendix J of
92 // https://graphviz.org/pdf/dotguide.pdf
93 cl::opt<std::string> AfterColour("dot-cfg-after-color",
94                                  cl::desc("Color for dot-cfg after elements."),
95                                  cl::Hidden, cl::init("forestgreen"));
96 // An option that determines the colour used for elements that are in both
97 // the before and after parts.  Must be a colour named in appendix J of
98 // https://graphviz.org/pdf/dotguide.pdf
99 cl::opt<std::string>
100     CommonColour("dot-cfg-common-color",
101                  cl::desc("Color for dot-cfg common elements."), cl::Hidden,
102                  cl::init("black"));
103 
104 // An option that determines where the generated website file (named
105 // passes.html) and the associated pdf files (named diff_*.pdf) are saved.
106 static cl::opt<std::string> DotCfgDir(
107     "dot-cfg-dir",
108     cl::desc("Generate dot files into specified directory for changed IRs"),
109     cl::Hidden, cl::init("./"));
110 
111 // An option to print the IR that was being processed when a pass crashes.
112 static cl::opt<bool>
113     PrintCrashIR("print-on-crash",
114                  cl::desc("Print the last form of the IR before crash"),
115                  cl::init(false), cl::Hidden);
116 
117 namespace {
118 
119 // Perform a system based diff between \p Before and \p After, using
120 // \p OldLineFormat, \p NewLineFormat, and \p UnchangedLineFormat
121 // to control the formatting of the output.  Return an error message
122 // for any failures instead of the diff.
123 std::string doSystemDiff(StringRef Before, StringRef After,
124                          StringRef OldLineFormat, StringRef NewLineFormat,
125                          StringRef UnchangedLineFormat) {
126   StringRef SR[2]{Before, After};
127   // Store the 2 bodies into temporary files and call diff on them
128   // to get the body of the node.
129   const unsigned NumFiles = 3;
130   static std::string FileName[NumFiles];
131   static int FD[NumFiles]{-1, -1, -1};
132   for (unsigned I = 0; I < NumFiles; ++I) {
133     if (FD[I] == -1) {
134       SmallVector<char, 200> SV;
135       std::error_code EC =
136           sys::fs::createTemporaryFile("tmpdiff", "txt", FD[I], SV);
137       if (EC)
138         return "Unable to create temporary file.";
139       FileName[I] = Twine(SV).str();
140     }
141     // The third file is used as the result of the diff.
142     if (I == NumFiles - 1)
143       break;
144 
145     std::error_code EC = sys::fs::openFileForWrite(FileName[I], FD[I]);
146     if (EC)
147       return "Unable to open temporary file for writing.";
148 
149     raw_fd_ostream OutStream(FD[I], /*shouldClose=*/true);
150     if (FD[I] == -1)
151       return "Error opening file for writing.";
152     OutStream << SR[I];
153   }
154 
155   static ErrorOr<std::string> DiffExe = sys::findProgramByName(DiffBinary);
156   if (!DiffExe)
157     return "Unable to find diff executable.";
158 
159   SmallString<128> OLF = formatv("--old-line-format={0}", OldLineFormat);
160   SmallString<128> NLF = formatv("--new-line-format={0}", NewLineFormat);
161   SmallString<128> ULF =
162       formatv("--unchanged-line-format={0}", UnchangedLineFormat);
163 
164   StringRef Args[] = {DiffBinary, "-w", "-d",        OLF,
165                       NLF,        ULF,  FileName[0], FileName[1]};
166   Optional<StringRef> Redirects[] = {None, StringRef(FileName[2]), None};
167   int Result = sys::ExecuteAndWait(*DiffExe, Args, None, Redirects);
168   if (Result < 0)
169     return "Error executing system diff.";
170   std::string Diff;
171   auto B = MemoryBuffer::getFile(FileName[2]);
172   if (B && *B)
173     Diff = (*B)->getBuffer().str();
174   else
175     return "Unable to read result.";
176 
177   // Clean up.
178   for (const std::string &I : FileName) {
179     std::error_code EC = sys::fs::remove(I);
180     if (EC)
181       return "Unable to remove temporary file.";
182   }
183   return Diff;
184 }
185 
186 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
187 /// certain global filters. Will never return nullptr if \p Force is true.
188 const Module *unwrapModule(Any IR, bool Force = false) {
189   if (any_isa<const Module *>(IR))
190     return any_cast<const Module *>(IR);
191 
192   if (any_isa<const Function *>(IR)) {
193     const Function *F = any_cast<const Function *>(IR);
194     if (!Force && !isFunctionInPrintList(F->getName()))
195       return nullptr;
196 
197     return F->getParent();
198   }
199 
200   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
201     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
202     for (const LazyCallGraph::Node &N : *C) {
203       const Function &F = N.getFunction();
204       if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
205         return F.getParent();
206       }
207     }
208     assert(!Force && "Expected a module");
209     return nullptr;
210   }
211 
212   if (any_isa<const Loop *>(IR)) {
213     const Loop *L = any_cast<const Loop *>(IR);
214     const Function *F = L->getHeader()->getParent();
215     if (!Force && !isFunctionInPrintList(F->getName()))
216       return nullptr;
217     return F->getParent();
218   }
219 
220   llvm_unreachable("Unknown IR unit");
221 }
222 
223 void printIR(raw_ostream &OS, const Function *F) {
224   if (!isFunctionInPrintList(F->getName()))
225     return;
226   OS << *F;
227 }
228 
229 void printIR(raw_ostream &OS, const Module *M) {
230   if (isFunctionInPrintList("*") || forcePrintModuleIR()) {
231     M->print(OS, nullptr);
232   } else {
233     for (const auto &F : M->functions()) {
234       printIR(OS, &F);
235     }
236   }
237 }
238 
239 void printIR(raw_ostream &OS, const LazyCallGraph::SCC *C) {
240   for (const LazyCallGraph::Node &N : *C) {
241     const Function &F = N.getFunction();
242     if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
243       F.print(OS);
244     }
245   }
246 }
247 
248 void printIR(raw_ostream &OS, const Loop *L) {
249   const Function *F = L->getHeader()->getParent();
250   if (!isFunctionInPrintList(F->getName()))
251     return;
252   printLoop(const_cast<Loop &>(*L), OS);
253 }
254 
255 std::string getIRName(Any IR) {
256   if (any_isa<const Module *>(IR))
257     return "[module]";
258 
259   if (any_isa<const Function *>(IR)) {
260     const Function *F = any_cast<const Function *>(IR);
261     return F->getName().str();
262   }
263 
264   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
265     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
266     return C->getName();
267   }
268 
269   if (any_isa<const Loop *>(IR)) {
270     const Loop *L = any_cast<const Loop *>(IR);
271     std::string S;
272     raw_string_ostream OS(S);
273     L->print(OS, /*Verbose*/ false, /*PrintNested*/ false);
274     return OS.str();
275   }
276 
277   llvm_unreachable("Unknown wrapped IR type");
278 }
279 
280 bool moduleContainsFilterPrintFunc(const Module &M) {
281   return any_of(M.functions(),
282                 [](const Function &F) {
283                   return isFunctionInPrintList(F.getName());
284                 }) ||
285          isFunctionInPrintList("*");
286 }
287 
288 bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
289   return any_of(C,
290                 [](const LazyCallGraph::Node &N) {
291                   return isFunctionInPrintList(N.getName());
292                 }) ||
293          isFunctionInPrintList("*");
294 }
295 
296 bool shouldPrintIR(Any IR) {
297   if (any_isa<const Module *>(IR)) {
298     const Module *M = any_cast<const Module *>(IR);
299     return moduleContainsFilterPrintFunc(*M);
300   }
301 
302   if (any_isa<const Function *>(IR)) {
303     const Function *F = any_cast<const Function *>(IR);
304     return isFunctionInPrintList(F->getName());
305   }
306 
307   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
308     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
309     return sccContainsFilterPrintFunc(*C);
310   }
311 
312   if (any_isa<const Loop *>(IR)) {
313     const Loop *L = any_cast<const Loop *>(IR);
314     return isFunctionInPrintList(L->getHeader()->getParent()->getName());
315   }
316   llvm_unreachable("Unknown wrapped IR type");
317 }
318 
319 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
320 /// llvm::Any and does actual print job.
321 void unwrapAndPrint(raw_ostream &OS, Any IR) {
322   if (!shouldPrintIR(IR))
323     return;
324 
325   if (forcePrintModuleIR()) {
326     auto *M = unwrapModule(IR);
327     assert(M && "should have unwrapped module");
328     printIR(OS, M);
329     return;
330   }
331 
332   if (any_isa<const Module *>(IR)) {
333     const Module *M = any_cast<const Module *>(IR);
334     printIR(OS, M);
335     return;
336   }
337 
338   if (any_isa<const Function *>(IR)) {
339     const Function *F = any_cast<const Function *>(IR);
340     printIR(OS, F);
341     return;
342   }
343 
344   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
345     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
346     printIR(OS, C);
347     return;
348   }
349 
350   if (any_isa<const Loop *>(IR)) {
351     const Loop *L = any_cast<const Loop *>(IR);
352     printIR(OS, L);
353     return;
354   }
355   llvm_unreachable("Unknown wrapped IR type");
356 }
357 
358 // Return true when this is a pass for which changes should be ignored
359 bool isIgnored(StringRef PassID) {
360   return isSpecialPass(PassID,
361                        {"PassManager", "PassAdaptor", "AnalysisManagerProxy",
362                         "DevirtSCCRepeatedPass", "ModuleInlinerWrapperPass"});
363 }
364 
365 std::string makeHTMLReady(StringRef SR) {
366   std::string S;
367   while (true) {
368     StringRef Clean =
369         SR.take_until([](char C) { return C == '<' || C == '>'; });
370     S.append(Clean.str());
371     SR = SR.drop_front(Clean.size());
372     if (SR.size() == 0)
373       return S;
374     S.append(SR[0] == '<' ? "&lt;" : "&gt;");
375     SR = SR.drop_front();
376   }
377   llvm_unreachable("problems converting string to HTML");
378 }
379 
380 // Return the module when that is the appropriate level of comparison for \p IR.
381 const Module *getModuleForComparison(Any IR) {
382   if (any_isa<const Module *>(IR))
383     return any_cast<const Module *>(IR);
384   if (any_isa<const LazyCallGraph::SCC *>(IR))
385     return any_cast<const LazyCallGraph::SCC *>(IR)
386         ->begin()
387         ->getFunction()
388         .getParent();
389   return nullptr;
390 }
391 
392 bool isInterestingFunction(const Function &F) {
393   return isFunctionInPrintList(F.getName());
394 }
395 
396 bool isInterestingPass(StringRef PassID) {
397   if (isIgnored(PassID))
398     return false;
399 
400   static std::unordered_set<std::string> PrintPassNames(PrintPassesList.begin(),
401                                                         PrintPassesList.end());
402   return PrintPassNames.empty() || PrintPassNames.count(PassID.str());
403 }
404 
405 // Return true when this is a pass on IR for which printing
406 // of changes is desired.
407 bool isInteresting(Any IR, StringRef PassID) {
408   if (!isInterestingPass(PassID))
409     return false;
410   if (any_isa<const Function *>(IR))
411     return isInterestingFunction(*any_cast<const Function *>(IR));
412   return true;
413 }
414 
415 } // namespace
416 
417 template <typename T> ChangeReporter<T>::~ChangeReporter() {
418   assert(BeforeStack.empty() && "Problem with Change Printer stack.");
419 }
420 
421 template <typename T>
422 void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID) {
423   // Always need to place something on the stack because invalidated passes
424   // are not given the IR so it cannot be determined whether the pass was for
425   // something that was filtered out.
426   BeforeStack.emplace_back();
427 
428   if (!isInteresting(IR, PassID))
429     return;
430   // Is this the initial IR?
431   if (InitialIR) {
432     InitialIR = false;
433     if (VerboseMode)
434       handleInitialIR(IR);
435   }
436 
437   // Save the IR representation on the stack.
438   T &Data = BeforeStack.back();
439   generateIRRepresentation(IR, PassID, Data);
440 }
441 
442 template <typename T>
443 void ChangeReporter<T>::handleIRAfterPass(Any IR, StringRef PassID) {
444   assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
445 
446   std::string Name = getIRName(IR);
447 
448   if (isIgnored(PassID)) {
449     if (VerboseMode)
450       handleIgnored(PassID, Name);
451   } else if (!isInteresting(IR, PassID)) {
452     if (VerboseMode)
453       handleFiltered(PassID, Name);
454   } else {
455     // Get the before rep from the stack
456     T &Before = BeforeStack.back();
457     // Create the after rep
458     T After;
459     generateIRRepresentation(IR, PassID, After);
460 
461     // Was there a change in IR?
462     if (Before == After) {
463       if (VerboseMode)
464         omitAfter(PassID, Name);
465     } else
466       handleAfter(PassID, Name, Before, After, IR);
467   }
468   BeforeStack.pop_back();
469 }
470 
471 template <typename T>
472 void ChangeReporter<T>::handleInvalidatedPass(StringRef PassID) {
473   assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
474 
475   // Always flag it as invalidated as we cannot determine when
476   // a pass for a filtered function is invalidated since we do not
477   // get the IR in the call.  Also, the output is just alternate
478   // forms of the banner anyway.
479   if (VerboseMode)
480     handleInvalidated(PassID);
481   BeforeStack.pop_back();
482 }
483 
484 template <typename T>
485 void ChangeReporter<T>::registerRequiredCallbacks(
486     PassInstrumentationCallbacks &PIC) {
487   PIC.registerBeforeNonSkippedPassCallback(
488       [this](StringRef P, Any IR) { saveIRBeforePass(IR, P); });
489 
490   PIC.registerAfterPassCallback(
491       [this](StringRef P, Any IR, const PreservedAnalyses &) {
492         handleIRAfterPass(IR, P);
493       });
494   PIC.registerAfterPassInvalidatedCallback(
495       [this](StringRef P, const PreservedAnalyses &) {
496         handleInvalidatedPass(P);
497       });
498 }
499 
500 template <typename T>
501 TextChangeReporter<T>::TextChangeReporter(bool Verbose)
502     : ChangeReporter<T>(Verbose), Out(dbgs()) {}
503 
504 template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
505   // Always print the module.
506   // Unwrap and print directly to avoid filtering problems in general routines.
507   auto *M = unwrapModule(IR, /*Force=*/true);
508   assert(M && "Expected module to be unwrapped when forced.");
509   Out << "*** IR Dump At Start ***\n";
510   M->print(Out, nullptr);
511 }
512 
513 template <typename T>
514 void TextChangeReporter<T>::omitAfter(StringRef PassID, std::string &Name) {
515   Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n",
516                  PassID, Name);
517 }
518 
519 template <typename T>
520 void TextChangeReporter<T>::handleInvalidated(StringRef PassID) {
521   Out << formatv("*** IR Pass {0} invalidated ***\n", PassID);
522 }
523 
524 template <typename T>
525 void TextChangeReporter<T>::handleFiltered(StringRef PassID,
526                                            std::string &Name) {
527   SmallString<20> Banner =
528       formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name);
529   Out << Banner;
530 }
531 
532 template <typename T>
533 void TextChangeReporter<T>::handleIgnored(StringRef PassID, std::string &Name) {
534   Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name);
535 }
536 
537 IRChangedPrinter::~IRChangedPrinter() = default;
538 
539 void IRChangedPrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
540   if (PrintChanged == ChangePrinter::Verbose ||
541       PrintChanged == ChangePrinter::Quiet)
542     TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
543 }
544 
545 void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
546                                                 std::string &Output) {
547   raw_string_ostream OS(Output);
548   unwrapAndPrint(OS, IR);
549   OS.str();
550 }
551 
552 void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name,
553                                    const std::string &Before,
554                                    const std::string &After, Any) {
555   // Report the IR before the changes when requested.
556   if (PrintChangedBefore)
557     Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n"
558         << Before;
559 
560   // We might not get anything to print if we only want to print a specific
561   // function but it gets deleted.
562   if (After.empty()) {
563     Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n";
564     return;
565   }
566 
567   Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After;
568 }
569 
570 template <typename T>
571 void OrderedChangedData<T>::report(
572     const OrderedChangedData &Before, const OrderedChangedData &After,
573     function_ref<void(const T *, const T *)> HandlePair) {
574   const auto &BFD = Before.getData();
575   const auto &AFD = After.getData();
576   std::vector<std::string>::const_iterator BI = Before.getOrder().begin();
577   std::vector<std::string>::const_iterator BE = Before.getOrder().end();
578   std::vector<std::string>::const_iterator AI = After.getOrder().begin();
579   std::vector<std::string>::const_iterator AE = After.getOrder().end();
580 
581   auto HandlePotentiallyRemovedData = [&](std::string S) {
582     // The order in LLVM may have changed so check if still exists.
583     if (!AFD.count(S)) {
584       // This has been removed.
585       HandlePair(&BFD.find(*BI)->getValue(), nullptr);
586     }
587   };
588   auto HandleNewData = [&](std::vector<const T *> &Q) {
589     // Print out any queued up new sections
590     for (const T *NBI : Q)
591       HandlePair(nullptr, NBI);
592     Q.clear();
593   };
594 
595   // Print out the data in the after order, with before ones interspersed
596   // appropriately (ie, somewhere near where they were in the before list).
597   // Start at the beginning of both lists.  Loop through the
598   // after list.  If an element is common, then advance in the before list
599   // reporting the removed ones until the common one is reached.  Report any
600   // queued up new ones and then report the common one.  If an element is not
601   // common, then enqueue it for reporting.  When the after list is exhausted,
602   // loop through the before list, reporting any removed ones.  Finally,
603   // report the rest of the enqueued new ones.
604   std::vector<const T *> NewDataQueue;
605   while (AI != AE) {
606     if (!BFD.count(*AI)) {
607       // This section is new so place it in the queue.  This will cause it
608       // to be reported after deleted sections.
609       NewDataQueue.emplace_back(&AFD.find(*AI)->getValue());
610       ++AI;
611       continue;
612     }
613     // This section is in both; advance and print out any before-only
614     // until we get to it.
615     while (*BI != *AI) {
616       HandlePotentiallyRemovedData(*BI);
617       ++BI;
618     }
619     // Report any new sections that were queued up and waiting.
620     HandleNewData(NewDataQueue);
621 
622     const T &AData = AFD.find(*AI)->getValue();
623     const T &BData = BFD.find(*AI)->getValue();
624     HandlePair(&BData, &AData);
625     ++BI;
626     ++AI;
627   }
628 
629   // Check any remaining before sections to see if they have been removed
630   while (BI != BE) {
631     HandlePotentiallyRemovedData(*BI);
632     ++BI;
633   }
634 
635   HandleNewData(NewDataQueue);
636 }
637 
638 template <typename T>
639 void IRComparer<T>::compare(
640     bool CompareModule,
641     std::function<void(bool InModule, unsigned Minor,
642                        const FuncDataT<T> &Before, const FuncDataT<T> &After)>
643         CompareFunc) {
644   if (!CompareModule) {
645     // Just handle the single function.
646     assert(Before.getData().size() == 1 && After.getData().size() == 1 &&
647            "Expected only one function.");
648     CompareFunc(false, 0, Before.getData().begin()->getValue(),
649                 After.getData().begin()->getValue());
650     return;
651   }
652 
653   unsigned Minor = 0;
654   FuncDataT<T> Missing("");
655   IRDataT<T>::report(Before, After,
656                      [&](const FuncDataT<T> *B, const FuncDataT<T> *A) {
657                        assert((B || A) && "Both functions cannot be missing.");
658                        if (!B)
659                          B = &Missing;
660                        else if (!A)
661                          A = &Missing;
662                        CompareFunc(true, Minor++, *B, *A);
663                      });
664 }
665 
666 template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
667   if (const Module *M = getModuleForComparison(IR)) {
668     // Create data for each existing/interesting function in the module.
669     for (const Function &F : *M)
670       generateFunctionData(Data, F);
671     return;
672   }
673 
674   const Function *F = nullptr;
675   if (any_isa<const Function *>(IR))
676     F = any_cast<const Function *>(IR);
677   else {
678     assert(any_isa<const Loop *>(IR) && "Unknown IR unit.");
679     const Loop *L = any_cast<const Loop *>(IR);
680     F = L->getHeader()->getParent();
681   }
682   assert(F && "Unknown IR unit.");
683   generateFunctionData(Data, *F);
684 }
685 
686 template <typename T>
687 bool IRComparer<T>::generateFunctionData(IRDataT<T> &Data, const Function &F) {
688   if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
689     FuncDataT<T> FD(F.getEntryBlock().getName().str());
690     for (const auto &B : F) {
691       FD.getOrder().emplace_back(B.getName());
692       FD.getData().insert({B.getName(), B});
693     }
694     Data.getOrder().emplace_back(F.getName());
695     Data.getData().insert({F.getName(), FD});
696     return true;
697   }
698   return false;
699 }
700 
701 PrintIRInstrumentation::~PrintIRInstrumentation() {
702   assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit");
703 }
704 
705 void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
706   const Module *M = unwrapModule(IR);
707   ModuleDescStack.emplace_back(M, getIRName(IR), PassID);
708 }
709 
710 PrintIRInstrumentation::PrintModuleDesc
711 PrintIRInstrumentation::popModuleDesc(StringRef PassID) {
712   assert(!ModuleDescStack.empty() && "empty ModuleDescStack");
713   PrintModuleDesc ModuleDesc = ModuleDescStack.pop_back_val();
714   assert(std::get<2>(ModuleDesc).equals(PassID) && "malformed ModuleDescStack");
715   return ModuleDesc;
716 }
717 
718 void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
719   if (isIgnored(PassID))
720     return;
721 
722   // Saving Module for AfterPassInvalidated operations.
723   // Note: here we rely on a fact that we do not change modules while
724   // traversing the pipeline, so the latest captured module is good
725   // for all print operations that has not happen yet.
726   if (shouldPrintAfterPass(PassID))
727     pushModuleDesc(PassID, IR);
728 
729   if (!shouldPrintBeforePass(PassID))
730     return;
731 
732   if (!shouldPrintIR(IR))
733     return;
734 
735   dbgs() << "*** IR Dump Before " << PassID << " on " << getIRName(IR)
736          << " ***\n";
737   unwrapAndPrint(dbgs(), IR);
738 }
739 
740 void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
741   if (isIgnored(PassID))
742     return;
743 
744   if (!shouldPrintAfterPass(PassID))
745     return;
746 
747   const Module *M;
748   std::string IRName;
749   StringRef StoredPassID;
750   std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
751   assert(StoredPassID == PassID && "mismatched PassID");
752 
753   if (!shouldPrintIR(IR))
754     return;
755 
756   dbgs() << "*** IR Dump After " << PassID << " on " << IRName << " ***\n";
757   unwrapAndPrint(dbgs(), IR);
758 }
759 
760 void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
761   StringRef PassName = PIC->getPassNameForClassName(PassID);
762   if (!shouldPrintAfterPass(PassName))
763     return;
764 
765   if (isIgnored(PassID))
766     return;
767 
768   const Module *M;
769   std::string IRName;
770   StringRef StoredPassID;
771   std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
772   assert(StoredPassID == PassID && "mismatched PassID");
773   // Additional filtering (e.g. -filter-print-func) can lead to module
774   // printing being skipped.
775   if (!M)
776     return;
777 
778   SmallString<20> Banner =
779       formatv("*** IR Dump After {0} on {1} (invalidated) ***", PassID, IRName);
780   dbgs() << Banner << "\n";
781   printIR(dbgs(), M);
782 }
783 
784 bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
785   if (shouldPrintBeforeAll())
786     return true;
787 
788   StringRef PassName = PIC->getPassNameForClassName(PassID);
789   return is_contained(printBeforePasses(), PassName);
790 }
791 
792 bool PrintIRInstrumentation::shouldPrintAfterPass(StringRef PassID) {
793   if (shouldPrintAfterAll())
794     return true;
795 
796   StringRef PassName = PIC->getPassNameForClassName(PassID);
797   return is_contained(printAfterPasses(), PassName);
798 }
799 
800 void PrintIRInstrumentation::registerCallbacks(
801     PassInstrumentationCallbacks &PIC) {
802   this->PIC = &PIC;
803 
804   // BeforePass callback is not just for printing, it also saves a Module
805   // for later use in AfterPassInvalidated.
806   if (shouldPrintBeforeSomePass() || shouldPrintAfterSomePass())
807     PIC.registerBeforeNonSkippedPassCallback(
808         [this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
809 
810   if (shouldPrintAfterSomePass()) {
811     PIC.registerAfterPassCallback(
812         [this](StringRef P, Any IR, const PreservedAnalyses &) {
813           this->printAfterPass(P, IR);
814         });
815     PIC.registerAfterPassInvalidatedCallback(
816         [this](StringRef P, const PreservedAnalyses &) {
817           this->printAfterPassInvalidated(P);
818         });
819   }
820 }
821 
822 void OptNoneInstrumentation::registerCallbacks(
823     PassInstrumentationCallbacks &PIC) {
824   PIC.registerShouldRunOptionalPassCallback(
825       [this](StringRef P, Any IR) { return this->shouldRun(P, IR); });
826 }
827 
828 bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
829   const Function *F = nullptr;
830   if (any_isa<const Function *>(IR)) {
831     F = any_cast<const Function *>(IR);
832   } else if (any_isa<const Loop *>(IR)) {
833     F = any_cast<const Loop *>(IR)->getHeader()->getParent();
834   }
835   bool ShouldRun = !(F && F->hasOptNone());
836   if (!ShouldRun && DebugLogging) {
837     errs() << "Skipping pass " << PassID << " on " << F->getName()
838            << " due to optnone attribute\n";
839   }
840   return ShouldRun;
841 }
842 
843 void OptBisectInstrumentation::registerCallbacks(
844     PassInstrumentationCallbacks &PIC) {
845   if (!getOptBisector().isEnabled())
846     return;
847   PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, Any IR) {
848     return isIgnored(PassID) ||
849            getOptBisector().checkPass(PassID, getIRName(IR));
850   });
851 }
852 
853 raw_ostream &PrintPassInstrumentation::print() {
854   if (Opts.Indent) {
855     assert(Indent >= 0);
856     dbgs().indent(Indent);
857   }
858   return dbgs();
859 }
860 
861 void PrintPassInstrumentation::registerCallbacks(
862     PassInstrumentationCallbacks &PIC) {
863   if (!Enabled)
864     return;
865 
866   std::vector<StringRef> SpecialPasses;
867   if (!Opts.Verbose) {
868     SpecialPasses.emplace_back("PassManager");
869     SpecialPasses.emplace_back("PassAdaptor");
870   }
871 
872   PIC.registerBeforeSkippedPassCallback([this, SpecialPasses](StringRef PassID,
873                                                               Any IR) {
874     assert(!isSpecialPass(PassID, SpecialPasses) &&
875            "Unexpectedly skipping special pass");
876 
877     print() << "Skipping pass: " << PassID << " on " << getIRName(IR) << "\n";
878   });
879   PIC.registerBeforeNonSkippedPassCallback([this, SpecialPasses](
880                                                StringRef PassID, Any IR) {
881     if (isSpecialPass(PassID, SpecialPasses))
882       return;
883 
884     auto &OS = print();
885     OS << "Running pass: " << PassID << " on " << getIRName(IR);
886     if (any_isa<const Function *>(IR)) {
887       unsigned Count = any_cast<const Function *>(IR)->getInstructionCount();
888       OS << " (" << Count << " instruction";
889       if (Count != 1)
890         OS << 's';
891       OS << ')';
892     } else if (any_isa<const LazyCallGraph::SCC *>(IR)) {
893       int Count = any_cast<const LazyCallGraph::SCC *>(IR)->size();
894       OS << " (" << Count << " node";
895       if (Count != 1)
896         OS << 's';
897       OS << ')';
898     }
899     OS << "\n";
900     Indent += 2;
901   });
902   PIC.registerAfterPassCallback(
903       [this, SpecialPasses](StringRef PassID, Any IR,
904                             const PreservedAnalyses &) {
905         if (isSpecialPass(PassID, SpecialPasses))
906           return;
907 
908         Indent -= 2;
909       });
910   PIC.registerAfterPassInvalidatedCallback(
911       [this, SpecialPasses](StringRef PassID, Any IR) {
912         if (isSpecialPass(PassID, SpecialPasses))
913           return;
914 
915         Indent -= 2;
916       });
917 
918   if (!Opts.SkipAnalyses) {
919     PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) {
920       print() << "Running analysis: " << PassID << " on " << getIRName(IR)
921               << "\n";
922       Indent += 2;
923     });
924     PIC.registerAfterAnalysisCallback(
925         [this](StringRef PassID, Any IR) { Indent -= 2; });
926     PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) {
927       print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR)
928               << "\n";
929     });
930     PIC.registerAnalysesClearedCallback([this](StringRef IRName) {
931       print() << "Clearing all analysis results for: " << IRName << "\n";
932     });
933   }
934 }
935 
936 PreservedCFGCheckerInstrumentation::CFG::CFG(const Function *F,
937                                              bool TrackBBLifetime) {
938   if (TrackBBLifetime)
939     BBGuards = DenseMap<intptr_t, BBGuard>(F->size());
940   for (const auto &BB : *F) {
941     if (BBGuards)
942       BBGuards->try_emplace(intptr_t(&BB), &BB);
943     for (auto *Succ : successors(&BB)) {
944       Graph[&BB][Succ]++;
945       if (BBGuards)
946         BBGuards->try_emplace(intptr_t(Succ), Succ);
947     }
948   }
949 }
950 
951 static void printBBName(raw_ostream &out, const BasicBlock *BB) {
952   if (BB->hasName()) {
953     out << BB->getName() << "<" << BB << ">";
954     return;
955   }
956 
957   if (!BB->getParent()) {
958     out << "unnamed_removed<" << BB << ">";
959     return;
960   }
961 
962   if (BB->isEntryBlock()) {
963     out << "entry"
964         << "<" << BB << ">";
965     return;
966   }
967 
968   unsigned FuncOrderBlockNum = 0;
969   for (auto &FuncBB : *BB->getParent()) {
970     if (&FuncBB == BB)
971       break;
972     FuncOrderBlockNum++;
973   }
974   out << "unnamed_" << FuncOrderBlockNum << "<" << BB << ">";
975 }
976 
977 void PreservedCFGCheckerInstrumentation::CFG::printDiff(raw_ostream &out,
978                                                         const CFG &Before,
979                                                         const CFG &After) {
980   assert(!After.isPoisoned());
981   if (Before.isPoisoned()) {
982     out << "Some blocks were deleted\n";
983     return;
984   }
985 
986   // Find and print graph differences.
987   if (Before.Graph.size() != After.Graph.size())
988     out << "Different number of non-leaf basic blocks: before="
989         << Before.Graph.size() << ", after=" << After.Graph.size() << "\n";
990 
991   for (auto &BB : Before.Graph) {
992     auto BA = After.Graph.find(BB.first);
993     if (BA == After.Graph.end()) {
994       out << "Non-leaf block ";
995       printBBName(out, BB.first);
996       out << " is removed (" << BB.second.size() << " successors)\n";
997     }
998   }
999 
1000   for (auto &BA : After.Graph) {
1001     auto BB = Before.Graph.find(BA.first);
1002     if (BB == Before.Graph.end()) {
1003       out << "Non-leaf block ";
1004       printBBName(out, BA.first);
1005       out << " is added (" << BA.second.size() << " successors)\n";
1006       continue;
1007     }
1008 
1009     if (BB->second == BA.second)
1010       continue;
1011 
1012     out << "Different successors of block ";
1013     printBBName(out, BA.first);
1014     out << " (unordered):\n";
1015     out << "- before (" << BB->second.size() << "): ";
1016     for (auto &SuccB : BB->second) {
1017       printBBName(out, SuccB.first);
1018       if (SuccB.second != 1)
1019         out << "(" << SuccB.second << "), ";
1020       else
1021         out << ", ";
1022     }
1023     out << "\n";
1024     out << "- after (" << BA.second.size() << "): ";
1025     for (auto &SuccA : BA.second) {
1026       printBBName(out, SuccA.first);
1027       if (SuccA.second != 1)
1028         out << "(" << SuccA.second << "), ";
1029       else
1030         out << ", ";
1031     }
1032     out << "\n";
1033   }
1034 }
1035 
1036 // PreservedCFGCheckerInstrumentation uses PreservedCFGCheckerAnalysis to check
1037 // passes, that reported they kept CFG analyses up-to-date, did not actually
1038 // change CFG. This check is done as follows. Before every functional pass in
1039 // BeforeNonSkippedPassCallback a CFG snapshot (an instance of
1040 // PreservedCFGCheckerInstrumentation::CFG) is requested from
1041 // FunctionAnalysisManager as a result of PreservedCFGCheckerAnalysis. When the
1042 // functional pass finishes and reports that CFGAnalyses or AllAnalyses are
1043 // up-to-date then the cached result of PreservedCFGCheckerAnalysis (if
1044 // available) is checked to be equal to a freshly created CFG snapshot.
1045 struct PreservedCFGCheckerAnalysis
1046     : public AnalysisInfoMixin<PreservedCFGCheckerAnalysis> {
1047   friend AnalysisInfoMixin<PreservedCFGCheckerAnalysis>;
1048 
1049   static AnalysisKey Key;
1050 
1051 public:
1052   /// Provide the result type for this analysis pass.
1053   using Result = PreservedCFGCheckerInstrumentation::CFG;
1054 
1055   /// Run the analysis pass over a function and produce CFG.
1056   Result run(Function &F, FunctionAnalysisManager &FAM) {
1057     return Result(&F, /* TrackBBLifetime */ true);
1058   }
1059 };
1060 
1061 AnalysisKey PreservedCFGCheckerAnalysis::Key;
1062 
1063 bool PreservedCFGCheckerInstrumentation::CFG::invalidate(
1064     Function &F, const PreservedAnalyses &PA,
1065     FunctionAnalysisManager::Invalidator &) {
1066   auto PAC = PA.getChecker<PreservedCFGCheckerAnalysis>();
1067   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
1068            PAC.preservedSet<CFGAnalyses>());
1069 }
1070 
1071 void PreservedCFGCheckerInstrumentation::registerCallbacks(
1072     PassInstrumentationCallbacks &PIC, FunctionAnalysisManager &FAM) {
1073   if (!VerifyPreservedCFG)
1074     return;
1075 
1076   FAM.registerPass([&] { return PreservedCFGCheckerAnalysis(); });
1077 
1078   auto checkCFG = [](StringRef Pass, StringRef FuncName, const CFG &GraphBefore,
1079                      const CFG &GraphAfter) {
1080     if (GraphAfter == GraphBefore)
1081       return;
1082 
1083     dbgs() << "Error: " << Pass
1084            << " does not invalidate CFG analyses but CFG changes detected in "
1085               "function @"
1086            << FuncName << ":\n";
1087     CFG::printDiff(dbgs(), GraphBefore, GraphAfter);
1088     report_fatal_error(Twine("CFG unexpectedly changed by ", Pass));
1089   };
1090 
1091   PIC.registerBeforeNonSkippedPassCallback([this, &FAM](StringRef P, Any IR) {
1092 #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
1093     assert(&PassStack.emplace_back(P));
1094 #endif
1095     (void)this;
1096     if (!any_isa<const Function *>(IR))
1097       return;
1098 
1099     const auto *F = any_cast<const Function *>(IR);
1100     // Make sure a fresh CFG snapshot is available before the pass.
1101     FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(F));
1102   });
1103 
1104   PIC.registerAfterPassInvalidatedCallback(
1105       [this](StringRef P, const PreservedAnalyses &PassPA) {
1106 #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
1107         assert(PassStack.pop_back_val() == P &&
1108                "Before and After callbacks must correspond");
1109 #endif
1110         (void)this;
1111       });
1112 
1113   PIC.registerAfterPassCallback([this, &FAM,
1114                                  checkCFG](StringRef P, Any IR,
1115                                            const PreservedAnalyses &PassPA) {
1116 #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
1117     assert(PassStack.pop_back_val() == P &&
1118            "Before and After callbacks must correspond");
1119 #endif
1120     (void)this;
1121 
1122     if (!any_isa<const Function *>(IR))
1123       return;
1124 
1125     if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
1126         !PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
1127       return;
1128 
1129     const auto *F = any_cast<const Function *>(IR);
1130     if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>(
1131             *const_cast<Function *>(F)))
1132       checkCFG(P, F->getName(), *GraphBefore,
1133                CFG(F, /* TrackBBLifetime */ false));
1134   });
1135 }
1136 
1137 void VerifyInstrumentation::registerCallbacks(
1138     PassInstrumentationCallbacks &PIC) {
1139   PIC.registerAfterPassCallback(
1140       [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1141         if (isIgnored(P) || P == "VerifierPass")
1142           return;
1143         if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) {
1144           const Function *F;
1145           if (any_isa<const Loop *>(IR))
1146             F = any_cast<const Loop *>(IR)->getHeader()->getParent();
1147           else
1148             F = any_cast<const Function *>(IR);
1149           if (DebugLogging)
1150             dbgs() << "Verifying function " << F->getName() << "\n";
1151 
1152           if (verifyFunction(*F, &errs()))
1153             report_fatal_error("Broken function found, compilation aborted!");
1154         } else if (any_isa<const Module *>(IR) ||
1155                    any_isa<const LazyCallGraph::SCC *>(IR)) {
1156           const Module *M;
1157           if (any_isa<const LazyCallGraph::SCC *>(IR))
1158             M = any_cast<const LazyCallGraph::SCC *>(IR)
1159                     ->begin()
1160                     ->getFunction()
1161                     .getParent();
1162           else
1163             M = any_cast<const Module *>(IR);
1164           if (DebugLogging)
1165             dbgs() << "Verifying module " << M->getName() << "\n";
1166 
1167           if (verifyModule(*M, &errs()))
1168             report_fatal_error("Broken module found, compilation aborted!");
1169         }
1170       });
1171 }
1172 
1173 InLineChangePrinter::~InLineChangePrinter() = default;
1174 
1175 void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID,
1176                                                    IRDataT<EmptyData> &D) {
1177   IRComparer<EmptyData>::analyzeIR(IR, D);
1178 }
1179 
1180 void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name,
1181                                       const IRDataT<EmptyData> &Before,
1182                                       const IRDataT<EmptyData> &After, Any IR) {
1183   SmallString<20> Banner =
1184       formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
1185   Out << Banner;
1186   IRComparer<EmptyData>(Before, After)
1187       .compare(getModuleForComparison(IR),
1188                [&](bool InModule, unsigned Minor,
1189                    const FuncDataT<EmptyData> &Before,
1190                    const FuncDataT<EmptyData> &After) -> void {
1191                  handleFunctionCompare(Name, "", PassID, " on ", InModule,
1192                                        Minor, Before, After);
1193                });
1194   Out << "\n";
1195 }
1196 
1197 void InLineChangePrinter::handleFunctionCompare(
1198     StringRef Name, StringRef Prefix, StringRef PassID, StringRef Divider,
1199     bool InModule, unsigned Minor, const FuncDataT<EmptyData> &Before,
1200     const FuncDataT<EmptyData> &After) {
1201   // Print a banner when this is being shown in the context of a module
1202   if (InModule)
1203     Out << "\n*** IR for function " << Name << " ***\n";
1204 
1205   FuncDataT<EmptyData>::report(
1206       Before, After,
1207       [&](const BlockDataT<EmptyData> *B, const BlockDataT<EmptyData> *A) {
1208         StringRef BStr = B ? B->getBody() : "\n";
1209         StringRef AStr = A ? A->getBody() : "\n";
1210         const std::string Removed =
1211             UseColour ? "\033[31m-%l\033[0m\n" : "-%l\n";
1212         const std::string Added = UseColour ? "\033[32m+%l\033[0m\n" : "+%l\n";
1213         const std::string NoChange = " %l\n";
1214         Out << doSystemDiff(BStr, AStr, Removed, Added, NoChange);
1215       });
1216 }
1217 
1218 void InLineChangePrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
1219   if (PrintChanged == ChangePrinter::DiffVerbose ||
1220       PrintChanged == ChangePrinter::DiffQuiet ||
1221       PrintChanged == ChangePrinter::ColourDiffVerbose ||
1222       PrintChanged == ChangePrinter::ColourDiffQuiet)
1223     TextChangeReporter<IRDataT<EmptyData>>::registerRequiredCallbacks(PIC);
1224 }
1225 
1226 namespace {
1227 
1228 class DisplayNode;
1229 class DotCfgDiffDisplayGraph;
1230 
1231 // Base class for a node or edge in the dot-cfg-changes graph.
1232 class DisplayElement {
1233 public:
1234   // Is this in before, after, or both?
1235   StringRef getColour() const { return Colour; }
1236 
1237 protected:
1238   DisplayElement(StringRef Colour) : Colour(Colour) {}
1239   const StringRef Colour;
1240 };
1241 
1242 // An edge representing a transition between basic blocks in the
1243 // dot-cfg-changes graph.
1244 class DisplayEdge : public DisplayElement {
1245 public:
1246   DisplayEdge(std::string Value, DisplayNode &Node, StringRef Colour)
1247       : DisplayElement(Colour), Value(Value), Node(Node) {}
1248   // The value on which the transition is made.
1249   std::string getValue() const { return Value; }
1250   // The node (representing a basic block) reached by this transition.
1251   const DisplayNode &getDestinationNode() const { return Node; }
1252 
1253 protected:
1254   std::string Value;
1255   const DisplayNode &Node;
1256 };
1257 
1258 // A node in the dot-cfg-changes graph which represents a basic block.
1259 class DisplayNode : public DisplayElement {
1260 public:
1261   // \p C is the content for the node, \p T indicates the colour for the
1262   // outline of the node
1263   DisplayNode(std::string Content, StringRef Colour)
1264       : DisplayElement(Colour), Content(Content) {}
1265 
1266   // Iterator to the child nodes.  Required by GraphWriter.
1267   using ChildIterator = std::unordered_set<DisplayNode *>::const_iterator;
1268   ChildIterator children_begin() const { return Children.cbegin(); }
1269   ChildIterator children_end() const { return Children.cend(); }
1270 
1271   // Iterator for the edges.  Required by GraphWriter.
1272   using EdgeIterator = std::vector<DisplayEdge *>::const_iterator;
1273   EdgeIterator edges_begin() const { return EdgePtrs.cbegin(); }
1274   EdgeIterator edges_end() const { return EdgePtrs.cend(); }
1275 
1276   // Create an edge to \p Node on value \p Value, with colour \p Colour.
1277   void createEdge(StringRef Value, DisplayNode &Node, StringRef Colour);
1278 
1279   // Return the content of this node.
1280   std::string getContent() const { return Content; }
1281 
1282   // Return the edge to node \p S.
1283   const DisplayEdge &getEdge(const DisplayNode &To) const {
1284     assert(EdgeMap.find(&To) != EdgeMap.end() && "Expected to find edge.");
1285     return *EdgeMap.find(&To)->second;
1286   }
1287 
1288   // Return the value for the transition to basic block \p S.
1289   // Required by GraphWriter.
1290   std::string getEdgeSourceLabel(const DisplayNode &Sink) const {
1291     return getEdge(Sink).getValue();
1292   }
1293 
1294   void createEdgeMap();
1295 
1296 protected:
1297   const std::string Content;
1298 
1299   // Place to collect all of the edges.  Once they are all in the vector,
1300   // the vector will not reallocate so then we can use pointers to them,
1301   // which are required by the graph writing routines.
1302   std::vector<DisplayEdge> Edges;
1303 
1304   std::vector<DisplayEdge *> EdgePtrs;
1305   std::unordered_set<DisplayNode *> Children;
1306   std::unordered_map<const DisplayNode *, const DisplayEdge *> EdgeMap;
1307 
1308   // Safeguard adding of edges.
1309   bool AllEdgesCreated = false;
1310 };
1311 
1312 // Class representing a difference display (corresponds to a pdf file).
1313 class DotCfgDiffDisplayGraph {
1314 public:
1315   DotCfgDiffDisplayGraph(std::string Name) : GraphName(Name) {}
1316 
1317   // Generate the file into \p DotFile.
1318   void generateDotFile(StringRef DotFile);
1319 
1320   // Iterator to the nodes.  Required by GraphWriter.
1321   using NodeIterator = std::vector<DisplayNode *>::const_iterator;
1322   NodeIterator nodes_begin() const {
1323     assert(NodeGenerationComplete && "Unexpected children iterator creation");
1324     return NodePtrs.cbegin();
1325   }
1326   NodeIterator nodes_end() const {
1327     assert(NodeGenerationComplete && "Unexpected children iterator creation");
1328     return NodePtrs.cend();
1329   }
1330 
1331   // Record the index of the entry node.  At this point, we can build up
1332   // vectors of pointers that are required by the graph routines.
1333   void setEntryNode(unsigned N) {
1334     // At this point, there will be no new nodes.
1335     assert(!NodeGenerationComplete && "Unexpected node creation");
1336     NodeGenerationComplete = true;
1337     for (auto &N : Nodes)
1338       NodePtrs.emplace_back(&N);
1339 
1340     EntryNode = NodePtrs[N];
1341   }
1342 
1343   // Create a node.
1344   void createNode(std::string C, StringRef Colour) {
1345     assert(!NodeGenerationComplete && "Unexpected node creation");
1346     Nodes.emplace_back(C, Colour);
1347   }
1348   // Return the node at index \p N to avoid problems with vectors reallocating.
1349   DisplayNode &getNode(unsigned N) {
1350     assert(N < Nodes.size() && "Node is out of bounds");
1351     return Nodes[N];
1352   }
1353   unsigned size() const {
1354     assert(NodeGenerationComplete && "Unexpected children iterator creation");
1355     return Nodes.size();
1356   }
1357 
1358   // Return the name of the graph.  Required by GraphWriter.
1359   std::string getGraphName() const { return GraphName; }
1360 
1361   // Return the string representing the differences for basic block \p Node.
1362   // Required by GraphWriter.
1363   std::string getNodeLabel(const DisplayNode &Node) const {
1364     return Node.getContent();
1365   }
1366 
1367   // Return a string with colour information for Dot.  Required by GraphWriter.
1368   std::string getNodeAttributes(const DisplayNode &Node) const {
1369     return attribute(Node.getColour());
1370   }
1371 
1372   // Return a string with colour information for Dot.  Required by GraphWriter.
1373   std::string getEdgeColorAttr(const DisplayNode &From,
1374                                const DisplayNode &To) const {
1375     return attribute(From.getEdge(To).getColour());
1376   }
1377 
1378   // Get the starting basic block.  Required by GraphWriter.
1379   DisplayNode *getEntryNode() const {
1380     assert(NodeGenerationComplete && "Unexpected children iterator creation");
1381     return EntryNode;
1382   }
1383 
1384 protected:
1385   // Return the string containing the colour to use as a Dot attribute.
1386   std::string attribute(StringRef Colour) const {
1387     return "color=" + Colour.str();
1388   }
1389 
1390   bool NodeGenerationComplete = false;
1391   const std::string GraphName;
1392   std::vector<DisplayNode> Nodes;
1393   std::vector<DisplayNode *> NodePtrs;
1394   DisplayNode *EntryNode = nullptr;
1395 };
1396 
1397 void DisplayNode::createEdge(StringRef Value, DisplayNode &Node,
1398                              StringRef Colour) {
1399   assert(!AllEdgesCreated && "Expected to be able to still create edges.");
1400   Edges.emplace_back(Value.str(), Node, Colour);
1401   Children.insert(&Node);
1402 }
1403 
1404 void DisplayNode::createEdgeMap() {
1405   // No more edges will be added so we can now use pointers to the edges
1406   // as the vector will not grow and reallocate.
1407   AllEdgesCreated = true;
1408   for (auto &E : Edges)
1409     EdgeMap.insert({&E.getDestinationNode(), &E});
1410 }
1411 
1412 class DotCfgDiffNode;
1413 class DotCfgDiff;
1414 
1415 // A class representing a basic block in the Dot difference graph.
1416 class DotCfgDiffNode {
1417 public:
1418   DotCfgDiffNode() = delete;
1419 
1420   // Create a node in Dot difference graph \p G representing the basic block
1421   // represented by \p BD with colour \p Colour (where it exists).
1422   DotCfgDiffNode(DotCfgDiff &G, unsigned N, const BlockDataT<DCData> &BD,
1423                  StringRef Colour)
1424       : Graph(G), N(N), Data{&BD, nullptr}, Colour(Colour) {}
1425   DotCfgDiffNode(const DotCfgDiffNode &DN)
1426       : Graph(DN.Graph), N(DN.N), Data{DN.Data[0], DN.Data[1]},
1427         Colour(DN.Colour), EdgesMap(DN.EdgesMap), Children(DN.Children),
1428         Edges(DN.Edges) {}
1429 
1430   unsigned getIndex() const { return N; }
1431 
1432   // The label of the basic block
1433   StringRef getLabel() const {
1434     assert(Data[0] && "Expected Data[0] to be set.");
1435     return Data[0]->getLabel();
1436   }
1437   // Return the colour for this block
1438   StringRef getColour() const { return Colour; }
1439   // Change this basic block from being only in before to being common.
1440   // Save the pointer to \p Other.
1441   void setCommon(const BlockDataT<DCData> &Other) {
1442     assert(!Data[1] && "Expected only one block datum");
1443     Data[1] = &Other;
1444     Colour = CommonColour;
1445   }
1446   // Add an edge to \p E of colour {\p Value, \p Colour}.
1447   void addEdge(unsigned E, StringRef Value, StringRef Colour) {
1448     // This is a new edge or it is an edge being made common.
1449     assert((EdgesMap.count(E) == 0 || Colour == CommonColour) &&
1450            "Unexpected edge count and color.");
1451     EdgesMap[E] = {Value.str(), Colour};
1452   }
1453   // Record the children and create edges.
1454   void finalize(DotCfgDiff &G);
1455 
1456   // Return the colour of the edge to node \p S.
1457   StringRef getEdgeColour(const unsigned S) const {
1458     assert(EdgesMap.count(S) == 1 && "Expected to find edge.");
1459     return EdgesMap.at(S).second;
1460   }
1461 
1462   // Return the string representing the basic block.
1463   std::string getBodyContent() const;
1464 
1465   void createDisplayEdges(DotCfgDiffDisplayGraph &Graph, unsigned DisplayNode,
1466                           std::map<const unsigned, unsigned> &NodeMap) const;
1467 
1468 protected:
1469   DotCfgDiff &Graph;
1470   const unsigned N;
1471   const BlockDataT<DCData> *Data[2];
1472   StringRef Colour;
1473   std::map<const unsigned, std::pair<std::string, StringRef>> EdgesMap;
1474   std::vector<unsigned> Children;
1475   std::vector<unsigned> Edges;
1476 };
1477 
1478 // Class representing the difference graph between two functions.
1479 class DotCfgDiff {
1480 public:
1481   // \p Title is the title given to the graph.  \p EntryNodeName is the
1482   // entry node for the function.  \p Before and \p After are the before
1483   // after versions of the function, respectively.  \p Dir is the directory
1484   // in which to store the results.
1485   DotCfgDiff(StringRef Title, const FuncDataT<DCData> &Before,
1486              const FuncDataT<DCData> &After);
1487 
1488   DotCfgDiff(const DotCfgDiff &) = delete;
1489   DotCfgDiff &operator=(const DotCfgDiff &) = delete;
1490 
1491   DotCfgDiffDisplayGraph createDisplayGraph(StringRef Title,
1492                                             StringRef EntryNodeName);
1493 
1494   // Return a string consisting of the labels for the \p Source and \p Sink.
1495   // The combination allows distinguishing changing transitions on the
1496   // same value (ie, a transition went to X before and goes to Y after).
1497   // Required by GraphWriter.
1498   StringRef getEdgeSourceLabel(const unsigned &Source,
1499                                const unsigned &Sink) const {
1500     std::string S =
1501         getNode(Source).getLabel().str() + " " + getNode(Sink).getLabel().str();
1502     assert(EdgeLabels.count(S) == 1 && "Expected to find edge label.");
1503     return EdgeLabels.find(S)->getValue();
1504   }
1505 
1506   // Return the number of basic blocks (nodes).  Required by GraphWriter.
1507   unsigned size() const { return Nodes.size(); }
1508 
1509   const DotCfgDiffNode &getNode(unsigned N) const {
1510     assert(N < Nodes.size() && "Unexpected index for node reference");
1511     return Nodes[N];
1512   }
1513 
1514 protected:
1515   // Return the string surrounded by HTML to make it the appropriate colour.
1516   std::string colourize(std::string S, StringRef Colour) const;
1517 
1518   void createNode(StringRef Label, const BlockDataT<DCData> &BD, StringRef C) {
1519     unsigned Pos = Nodes.size();
1520     Nodes.emplace_back(*this, Pos, BD, C);
1521     NodePosition.insert({Label, Pos});
1522   }
1523 
1524   // TODO Nodes should probably be a StringMap<DotCfgDiffNode> after the
1525   // display graph is separated out, which would remove the need for
1526   // NodePosition.
1527   std::vector<DotCfgDiffNode> Nodes;
1528   StringMap<unsigned> NodePosition;
1529   const std::string GraphName;
1530 
1531   StringMap<std::string> EdgeLabels;
1532 };
1533 
1534 std::string DotCfgDiffNode::getBodyContent() const {
1535   if (Colour == CommonColour) {
1536     assert(Data[1] && "Expected Data[1] to be set.");
1537 
1538     StringRef SR[2];
1539     for (unsigned I = 0; I < 2; ++I) {
1540       SR[I] = Data[I]->getBody();
1541       // drop initial '\n' if present
1542       if (SR[I][0] == '\n')
1543         SR[I] = SR[I].drop_front();
1544       // drop predecessors as they can be big and are redundant
1545       SR[I] = SR[I].drop_until([](char C) { return C == '\n'; }).drop_front();
1546     }
1547 
1548     SmallString<80> OldLineFormat = formatv(
1549         "<FONT COLOR=\"{0}\">%l</FONT><BR align=\"left\"/>", BeforeColour);
1550     SmallString<80> NewLineFormat = formatv(
1551         "<FONT COLOR=\"{0}\">%l</FONT><BR align=\"left\"/>", AfterColour);
1552     SmallString<80> UnchangedLineFormat = formatv(
1553         "<FONT COLOR=\"{0}\">%l</FONT><BR align=\"left\"/>", CommonColour);
1554     std::string Diff = Data[0]->getLabel().str();
1555     Diff += ":\n<BR align=\"left\"/>" +
1556             doSystemDiff(makeHTMLReady(SR[0]), makeHTMLReady(SR[1]),
1557                          OldLineFormat, NewLineFormat, UnchangedLineFormat);
1558 
1559     // Diff adds in some empty colour changes which are not valid HTML
1560     // so remove them.  Colours are all lowercase alpha characters (as
1561     // listed in https://graphviz.org/pdf/dotguide.pdf).
1562     Regex R("<FONT COLOR=\"\\w+\"></FONT>");
1563     while (true) {
1564       std::string Error;
1565       std::string S = R.sub("", Diff, &Error);
1566       if (Error != "")
1567         return Error;
1568       if (S == Diff)
1569         return Diff;
1570       Diff = S;
1571     }
1572     llvm_unreachable("Should not get here");
1573   }
1574 
1575   // Put node out in the appropriate colour.
1576   assert(!Data[1] && "Data[1] is set unexpectedly.");
1577   std::string Body = makeHTMLReady(Data[0]->getBody());
1578   const StringRef BS = Body;
1579   StringRef BS1 = BS;
1580   // Drop leading newline, if present.
1581   if (BS.front() == '\n')
1582     BS1 = BS1.drop_front(1);
1583   // Get label.
1584   StringRef Label = BS1.take_until([](char C) { return C == ':'; });
1585   // drop predecessors as they can be big and are redundant
1586   BS1 = BS1.drop_until([](char C) { return C == '\n'; }).drop_front();
1587 
1588   std::string S = "<FONT COLOR=\"" + Colour.str() + "\">" + Label.str() + ":";
1589 
1590   // align each line to the left.
1591   while (BS1.size()) {
1592     S.append("<BR align=\"left\"/>");
1593     StringRef Line = BS1.take_until([](char C) { return C == '\n'; });
1594     S.append(Line.str());
1595     BS1 = BS1.drop_front(Line.size() + 1);
1596   }
1597   S.append("<BR align=\"left\"/></FONT>");
1598   return S;
1599 }
1600 
1601 std::string DotCfgDiff::colourize(std::string S, StringRef Colour) const {
1602   if (S.length() == 0)
1603     return S;
1604   return "<FONT COLOR=\"" + Colour.str() + "\">" + S + "</FONT>";
1605 }
1606 
1607 DotCfgDiff::DotCfgDiff(StringRef Title, const FuncDataT<DCData> &Before,
1608                        const FuncDataT<DCData> &After)
1609     : GraphName(Title.str()) {
1610   StringMap<StringRef> EdgesMap;
1611 
1612   // Handle each basic block in the before IR.
1613   for (auto &B : Before.getData()) {
1614     StringRef Label = B.getKey();
1615     const BlockDataT<DCData> &BD = B.getValue();
1616     createNode(Label, BD, BeforeColour);
1617 
1618     // Create transitions with names made up of the from block label, the value
1619     // on which the transition is made and the to block label.
1620     for (StringMap<std::string>::const_iterator Sink = BD.getData().begin(),
1621                                                 E = BD.getData().end();
1622          Sink != E; ++Sink) {
1623       std::string Key = (Label + " " + Sink->getKey().str()).str() + " " +
1624                         BD.getData().getSuccessorLabel(Sink->getKey()).str();
1625       EdgesMap.insert({Key, BeforeColour});
1626     }
1627   }
1628 
1629   // Handle each basic block in the after IR
1630   for (auto &A : After.getData()) {
1631     StringRef Label = A.getKey();
1632     const BlockDataT<DCData> &BD = A.getValue();
1633     unsigned C = NodePosition.count(Label);
1634     if (C == 0)
1635       // This only exists in the after IR.  Create the node.
1636       createNode(Label, BD, AfterColour);
1637     else {
1638       assert(C == 1 && "Unexpected multiple nodes.");
1639       Nodes[NodePosition[Label]].setCommon(BD);
1640     }
1641     // Add in the edges between the nodes (as common or only in after).
1642     for (StringMap<std::string>::const_iterator Sink = BD.getData().begin(),
1643                                                 E = BD.getData().end();
1644          Sink != E; ++Sink) {
1645       std::string Key = (Label + " " + Sink->getKey().str()).str() + " " +
1646                         BD.getData().getSuccessorLabel(Sink->getKey()).str();
1647       unsigned C = EdgesMap.count(Key);
1648       if (C == 0)
1649         EdgesMap.insert({Key, AfterColour});
1650       else {
1651         EdgesMap[Key] = CommonColour;
1652       }
1653     }
1654   }
1655 
1656   // Now go through the map of edges and add them to the node.
1657   for (auto &E : EdgesMap) {
1658     // Extract the source, sink and value from the edge key.
1659     StringRef S = E.getKey();
1660     auto SP1 = S.rsplit(' ');
1661     auto &SourceSink = SP1.first;
1662     auto SP2 = SourceSink.split(' ');
1663     StringRef Source = SP2.first;
1664     StringRef Sink = SP2.second;
1665     StringRef Value = SP1.second;
1666 
1667     assert(NodePosition.count(Source) == 1 && "Expected to find node.");
1668     DotCfgDiffNode &SourceNode = Nodes[NodePosition[Source]];
1669     assert(NodePosition.count(Sink) == 1 && "Expected to find node.");
1670     unsigned SinkNode = NodePosition[Sink];
1671     StringRef Colour = E.second;
1672 
1673     // Look for an edge from Source to Sink
1674     if (EdgeLabels.count(SourceSink) == 0)
1675       EdgeLabels.insert({SourceSink, colourize(Value.str(), Colour)});
1676     else {
1677       StringRef V = EdgeLabels.find(SourceSink)->getValue();
1678       std::string NV = colourize(V.str() + " " + Value.str(), Colour);
1679       Colour = CommonColour;
1680       EdgeLabels[SourceSink] = NV;
1681     }
1682     SourceNode.addEdge(SinkNode, Value, Colour);
1683   }
1684   for (auto &I : Nodes)
1685     I.finalize(*this);
1686 }
1687 
1688 DotCfgDiffDisplayGraph DotCfgDiff::createDisplayGraph(StringRef Title,
1689                                                       StringRef EntryNodeName) {
1690   assert(NodePosition.count(EntryNodeName) == 1 &&
1691          "Expected to find entry block in map.");
1692   unsigned Entry = NodePosition[EntryNodeName];
1693   assert(Entry < Nodes.size() && "Expected to find entry node");
1694   DotCfgDiffDisplayGraph G(Title.str());
1695 
1696   std::map<const unsigned, unsigned> NodeMap;
1697 
1698   int EntryIndex = -1;
1699   unsigned Index = 0;
1700   for (auto &I : Nodes) {
1701     if (I.getIndex() == Entry)
1702       EntryIndex = Index;
1703     G.createNode(I.getBodyContent(), I.getColour());
1704     NodeMap.insert({I.getIndex(), Index++});
1705   }
1706   assert(EntryIndex >= 0 && "Expected entry node index to be set.");
1707   G.setEntryNode(EntryIndex);
1708 
1709   for (auto &I : NodeMap) {
1710     unsigned SourceNode = I.first;
1711     unsigned DisplayNode = I.second;
1712     getNode(SourceNode).createDisplayEdges(G, DisplayNode, NodeMap);
1713   }
1714   return G;
1715 }
1716 
1717 void DotCfgDiffNode::createDisplayEdges(
1718     DotCfgDiffDisplayGraph &DisplayGraph, unsigned DisplayNodeIndex,
1719     std::map<const unsigned, unsigned> &NodeMap) const {
1720 
1721   DisplayNode &SourceDisplayNode = DisplayGraph.getNode(DisplayNodeIndex);
1722 
1723   for (auto I : Edges) {
1724     unsigned SinkNodeIndex = I;
1725     StringRef Colour = getEdgeColour(SinkNodeIndex);
1726     const DotCfgDiffNode *SinkNode = &Graph.getNode(SinkNodeIndex);
1727 
1728     StringRef Label = Graph.getEdgeSourceLabel(getIndex(), SinkNodeIndex);
1729     DisplayNode &SinkDisplayNode = DisplayGraph.getNode(SinkNode->getIndex());
1730     SourceDisplayNode.createEdge(Label, SinkDisplayNode, Colour);
1731   }
1732   SourceDisplayNode.createEdgeMap();
1733 }
1734 
1735 void DotCfgDiffNode::finalize(DotCfgDiff &G) {
1736   for (auto E : EdgesMap) {
1737     Children.emplace_back(E.first);
1738     Edges.emplace_back(E.first);
1739   }
1740 }
1741 
1742 } // namespace
1743 
1744 namespace llvm {
1745 
1746 template <> struct GraphTraits<DotCfgDiffDisplayGraph *> {
1747   using NodeRef = const DisplayNode *;
1748   using ChildIteratorType = DisplayNode::ChildIterator;
1749   using nodes_iterator = DotCfgDiffDisplayGraph::NodeIterator;
1750   using EdgeRef = const DisplayEdge *;
1751   using ChildEdgeIterator = DisplayNode::EdgeIterator;
1752 
1753   static NodeRef getEntryNode(const DotCfgDiffDisplayGraph *G) {
1754     return G->getEntryNode();
1755   }
1756   static ChildIteratorType child_begin(NodeRef N) {
1757     return N->children_begin();
1758   }
1759   static ChildIteratorType child_end(NodeRef N) { return N->children_end(); }
1760   static nodes_iterator nodes_begin(const DotCfgDiffDisplayGraph *G) {
1761     return G->nodes_begin();
1762   }
1763   static nodes_iterator nodes_end(const DotCfgDiffDisplayGraph *G) {
1764     return G->nodes_end();
1765   }
1766   static ChildEdgeIterator child_edge_begin(NodeRef N) {
1767     return N->edges_begin();
1768   }
1769   static ChildEdgeIterator child_edge_end(NodeRef N) { return N->edges_end(); }
1770   static NodeRef edge_dest(EdgeRef E) { return &E->getDestinationNode(); }
1771   static unsigned size(const DotCfgDiffDisplayGraph *G) { return G->size(); }
1772 };
1773 
1774 template <>
1775 struct DOTGraphTraits<DotCfgDiffDisplayGraph *> : public DefaultDOTGraphTraits {
1776   explicit DOTGraphTraits(bool Simple = false)
1777       : DefaultDOTGraphTraits(Simple) {}
1778 
1779   static bool renderNodesUsingHTML() { return true; }
1780   static std::string getGraphName(const DotCfgDiffDisplayGraph *DiffData) {
1781     return DiffData->getGraphName();
1782   }
1783   static std::string
1784   getGraphProperties(const DotCfgDiffDisplayGraph *DiffData) {
1785     return "\tsize=\"190, 190\";\n";
1786   }
1787   static std::string getNodeLabel(const DisplayNode *Node,
1788                                   const DotCfgDiffDisplayGraph *DiffData) {
1789     return DiffData->getNodeLabel(*Node);
1790   }
1791   static std::string getNodeAttributes(const DisplayNode *Node,
1792                                        const DotCfgDiffDisplayGraph *DiffData) {
1793     return DiffData->getNodeAttributes(*Node);
1794   }
1795   static std::string getEdgeSourceLabel(const DisplayNode *From,
1796                                         DisplayNode::ChildIterator &To) {
1797     return From->getEdgeSourceLabel(**To);
1798   }
1799   static std::string getEdgeAttributes(const DisplayNode *From,
1800                                        DisplayNode::ChildIterator &To,
1801                                        const DotCfgDiffDisplayGraph *DiffData) {
1802     return DiffData->getEdgeColorAttr(*From, **To);
1803   }
1804 };
1805 
1806 } // namespace llvm
1807 
1808 namespace {
1809 
1810 void DotCfgDiffDisplayGraph::generateDotFile(StringRef DotFile) {
1811   std::error_code EC;
1812   raw_fd_ostream OutStream(DotFile, EC);
1813   if (EC) {
1814     errs() << "Error: " << EC.message() << "\n";
1815     return;
1816   }
1817   WriteGraph(OutStream, this, false);
1818   OutStream.flush();
1819   OutStream.close();
1820 }
1821 
1822 } // namespace
1823 
1824 namespace llvm {
1825 
1826 DCData::DCData(const BasicBlock &B) {
1827   // Build up transition labels.
1828   const Instruction *Term = B.getTerminator();
1829   if (const BranchInst *Br = dyn_cast<const BranchInst>(Term))
1830     if (Br->isUnconditional())
1831       addSuccessorLabel(Br->getSuccessor(0)->getName().str(), "");
1832     else {
1833       addSuccessorLabel(Br->getSuccessor(0)->getName().str(), "true");
1834       addSuccessorLabel(Br->getSuccessor(1)->getName().str(), "false");
1835     }
1836   else if (const SwitchInst *Sw = dyn_cast<const SwitchInst>(Term)) {
1837     addSuccessorLabel(Sw->case_default()->getCaseSuccessor()->getName().str(),
1838                       "default");
1839     for (auto &C : Sw->cases()) {
1840       assert(C.getCaseValue() && "Expected to find case value.");
1841       SmallString<20> Value = formatv("{0}", C.getCaseValue()->getSExtValue());
1842       addSuccessorLabel(C.getCaseSuccessor()->getName().str(), Value);
1843     }
1844   } else
1845     for (const_succ_iterator I = succ_begin(&B), E = succ_end(&B); I != E; ++I)
1846       addSuccessorLabel((*I)->getName().str(), "");
1847 }
1848 
1849 DotCfgChangeReporter::DotCfgChangeReporter(bool Verbose)
1850     : ChangeReporter<IRDataT<DCData>>(Verbose) {}
1851 
1852 void DotCfgChangeReporter::handleFunctionCompare(
1853     StringRef Name, StringRef Prefix, StringRef PassID, StringRef Divider,
1854     bool InModule, unsigned Minor, const FuncDataT<DCData> &Before,
1855     const FuncDataT<DCData> &After) {
1856   assert(HTML && "Expected outstream to be set");
1857   SmallString<8> Extender;
1858   SmallString<8> Number;
1859   // Handle numbering and file names.
1860   if (InModule) {
1861     Extender = formatv("{0}_{1}", N, Minor);
1862     Number = formatv("{0}.{1}", N, Minor);
1863   } else {
1864     Extender = formatv("{0}", N);
1865     Number = formatv("{0}", N);
1866   }
1867   // Create a temporary file name for the dot file.
1868   SmallVector<char, 128> SV;
1869   sys::fs::createUniquePath("cfgdot-%%%%%%.dot", SV, true);
1870   std::string DotFile = Twine(SV).str();
1871 
1872   SmallString<20> PDFFileName = formatv("diff_{0}.pdf", Extender);
1873   SmallString<200> Text;
1874 
1875   Text = formatv("{0}.{1}{2}{3}{4}", Number, Prefix, makeHTMLReady(PassID),
1876                  Divider, Name);
1877 
1878   DotCfgDiff Diff(Text, Before, After);
1879   std::string EntryBlockName = After.getEntryBlockName();
1880   // Use the before entry block if the after entry block was removed.
1881   if (EntryBlockName == "")
1882     EntryBlockName = Before.getEntryBlockName();
1883   assert(EntryBlockName != "" && "Expected to find entry block");
1884 
1885   DotCfgDiffDisplayGraph DG = Diff.createDisplayGraph(Text, EntryBlockName);
1886   DG.generateDotFile(DotFile);
1887 
1888   *HTML << genHTML(Text, DotFile, PDFFileName);
1889   std::error_code EC = sys::fs::remove(DotFile);
1890   if (EC)
1891     errs() << "Error: " << EC.message() << "\n";
1892 }
1893 
1894 std::string DotCfgChangeReporter::genHTML(StringRef Text, StringRef DotFile,
1895                                           StringRef PDFFileName) {
1896   SmallString<20> PDFFile = formatv("{0}/{1}", DotCfgDir, PDFFileName);
1897   // Create the PDF file.
1898   static ErrorOr<std::string> DotExe = sys::findProgramByName(DotBinary);
1899   if (!DotExe)
1900     return "Unable to find dot executable.";
1901 
1902   StringRef Args[] = {DotBinary, "-Tpdf", "-o", PDFFile, DotFile};
1903   int Result = sys::ExecuteAndWait(*DotExe, Args, None);
1904   if (Result < 0)
1905     return "Error executing system dot.";
1906 
1907   // Create the HTML tag refering to the PDF file.
1908   SmallString<200> S = formatv(
1909       "  <a href=\"{0}\" target=\"_blank\">{1}</a><br/>\n", PDFFileName, Text);
1910   return S.c_str();
1911 }
1912 
1913 void DotCfgChangeReporter::handleInitialIR(Any IR) {
1914   assert(HTML && "Expected outstream to be set");
1915   *HTML << "<button type=\"button\" class=\"collapsible\">0. "
1916         << "Initial IR (by function)</button>\n"
1917         << "<div class=\"content\">\n"
1918         << "  <p>\n";
1919   // Create representation of IR
1920   IRDataT<DCData> Data;
1921   IRComparer<DCData>::analyzeIR(IR, Data);
1922   // Now compare it against itself, which will have everything the
1923   // same and will generate the files.
1924   IRComparer<DCData>(Data, Data)
1925       .compare(getModuleForComparison(IR),
1926                [&](bool InModule, unsigned Minor,
1927                    const FuncDataT<DCData> &Before,
1928                    const FuncDataT<DCData> &After) -> void {
1929                  handleFunctionCompare("", " ", "Initial IR", "", InModule,
1930                                        Minor, Before, After);
1931                });
1932   *HTML << "  </p>\n"
1933         << "</div><br/>\n";
1934   ++N;
1935 }
1936 
1937 void DotCfgChangeReporter::generateIRRepresentation(Any IR, StringRef PassID,
1938                                                     IRDataT<DCData> &Data) {
1939   IRComparer<DCData>::analyzeIR(IR, Data);
1940 }
1941 
1942 void DotCfgChangeReporter::omitAfter(StringRef PassID, std::string &Name) {
1943   assert(HTML && "Expected outstream to be set");
1944   SmallString<20> Banner =
1945       formatv("  <a>{0}. Pass {1} on {2} omitted because no change</a><br/>\n",
1946               N, makeHTMLReady(PassID), Name);
1947   *HTML << Banner;
1948   ++N;
1949 }
1950 
1951 void DotCfgChangeReporter::handleAfter(StringRef PassID, std::string &Name,
1952                                        const IRDataT<DCData> &Before,
1953                                        const IRDataT<DCData> &After, Any IR) {
1954   assert(HTML && "Expected outstream to be set");
1955   IRComparer<DCData>(Before, After)
1956       .compare(getModuleForComparison(IR),
1957                [&](bool InModule, unsigned Minor,
1958                    const FuncDataT<DCData> &Before,
1959                    const FuncDataT<DCData> &After) -> void {
1960                  handleFunctionCompare(Name, " Pass ", PassID, " on ", InModule,
1961                                        Minor, Before, After);
1962                });
1963   *HTML << "    </p></div>\n";
1964   ++N;
1965 }
1966 
1967 void DotCfgChangeReporter::handleInvalidated(StringRef PassID) {
1968   assert(HTML && "Expected outstream to be set");
1969   SmallString<20> Banner =
1970       formatv("  <a>{0}. {1} invalidated</a><br/>\n", N, makeHTMLReady(PassID));
1971   *HTML << Banner;
1972   ++N;
1973 }
1974 
1975 void DotCfgChangeReporter::handleFiltered(StringRef PassID, std::string &Name) {
1976   assert(HTML && "Expected outstream to be set");
1977   SmallString<20> Banner =
1978       formatv("  <a>{0}. Pass {1} on {2} filtered out</a><br/>\n", N,
1979               makeHTMLReady(PassID), Name);
1980   *HTML << Banner;
1981   ++N;
1982 }
1983 
1984 void DotCfgChangeReporter::handleIgnored(StringRef PassID, std::string &Name) {
1985   assert(HTML && "Expected outstream to be set");
1986   SmallString<20> Banner = formatv("  <a>{0}. {1} on {2} ignored</a><br/>\n", N,
1987                                    makeHTMLReady(PassID), Name);
1988   *HTML << Banner;
1989   ++N;
1990 }
1991 
1992 bool DotCfgChangeReporter::initializeHTML() {
1993   std::error_code EC;
1994   HTML = std::make_unique<raw_fd_ostream>(DotCfgDir + "/passes.html", EC);
1995   if (EC) {
1996     HTML = nullptr;
1997     return false;
1998   }
1999 
2000   *HTML << "<!doctype html>"
2001         << "<html>"
2002         << "<head>"
2003         << "<style>.collapsible { "
2004         << "background-color: #777;"
2005         << " color: white;"
2006         << " cursor: pointer;"
2007         << " padding: 18px;"
2008         << " width: 100%;"
2009         << " border: none;"
2010         << " text-align: left;"
2011         << " outline: none;"
2012         << " font-size: 15px;"
2013         << "} .active, .collapsible:hover {"
2014         << " background-color: #555;"
2015         << "} .content {"
2016         << " padding: 0 18px;"
2017         << " display: none;"
2018         << " overflow: hidden;"
2019         << " background-color: #f1f1f1;"
2020         << "}"
2021         << "</style>"
2022         << "<title>passes.html</title>"
2023         << "</head>\n"
2024         << "<body>";
2025   return true;
2026 }
2027 
2028 DotCfgChangeReporter::~DotCfgChangeReporter() {
2029   if (!HTML)
2030     return;
2031   *HTML
2032       << "<script>var coll = document.getElementsByClassName(\"collapsible\");"
2033       << "var i;"
2034       << "for (i = 0; i < coll.length; i++) {"
2035       << "coll[i].addEventListener(\"click\", function() {"
2036       << " this.classList.toggle(\"active\");"
2037       << " var content = this.nextElementSibling;"
2038       << " if (content.style.display === \"block\"){"
2039       << " content.style.display = \"none\";"
2040       << " }"
2041       << " else {"
2042       << " content.style.display= \"block\";"
2043       << " }"
2044       << " });"
2045       << " }"
2046       << "</script>"
2047       << "</body>"
2048       << "</html>\n";
2049   HTML->flush();
2050   HTML->close();
2051 }
2052 
2053 void DotCfgChangeReporter::registerCallbacks(
2054     PassInstrumentationCallbacks &PIC) {
2055   if (PrintChanged == ChangePrinter::DotCfgVerbose ||
2056        PrintChanged == ChangePrinter::DotCfgQuiet) {
2057     SmallString<128> OutputDir;
2058     sys::fs::expand_tilde(DotCfgDir, OutputDir);
2059     sys::fs::make_absolute(OutputDir);
2060     assert(!OutputDir.empty() && "expected output dir to be non-empty");
2061     DotCfgDir = OutputDir.c_str();
2062     if (initializeHTML()) {
2063       ChangeReporter<IRDataT<DCData>>::registerRequiredCallbacks(PIC);
2064       return;
2065     }
2066     dbgs() << "Unable to open output stream for -cfg-dot-changed\n";
2067   }
2068 }
2069 
2070 StandardInstrumentations::StandardInstrumentations(
2071     bool DebugLogging, bool VerifyEach, PrintPassOptions PrintPassOpts)
2072     : PrintPass(DebugLogging, PrintPassOpts), OptNone(DebugLogging),
2073       PrintChangedIR(PrintChanged == ChangePrinter::Verbose),
2074       PrintChangedDiff(PrintChanged == ChangePrinter::DiffVerbose ||
2075                            PrintChanged == ChangePrinter::ColourDiffVerbose,
2076                        PrintChanged == ChangePrinter::ColourDiffVerbose ||
2077                            PrintChanged == ChangePrinter::ColourDiffQuiet),
2078       WebsiteChangeReporter(PrintChanged == ChangePrinter::DotCfgVerbose),
2079       Verify(DebugLogging), VerifyEach(VerifyEach) {}
2080 
2081 PrintCrashIRInstrumentation *PrintCrashIRInstrumentation::CrashReporter =
2082     nullptr;
2083 
2084 void PrintCrashIRInstrumentation::reportCrashIR() { dbgs() << SavedIR; }
2085 
2086 void PrintCrashIRInstrumentation::SignalHandler(void *) {
2087   // Called by signal handlers so do not lock here
2088   // Is the PrintCrashIRInstrumentation still alive?
2089   if (!CrashReporter)
2090     return;
2091 
2092   assert(PrintCrashIR && "Did not expect to get here without option set.");
2093   CrashReporter->reportCrashIR();
2094 }
2095 
2096 PrintCrashIRInstrumentation::~PrintCrashIRInstrumentation() {
2097   if (!CrashReporter)
2098     return;
2099 
2100   assert(PrintCrashIR && "Did not expect to get here without option set.");
2101   CrashReporter = nullptr;
2102 }
2103 
2104 void PrintCrashIRInstrumentation::registerCallbacks(
2105     PassInstrumentationCallbacks &PIC) {
2106   if (!PrintCrashIR || CrashReporter)
2107     return;
2108 
2109   sys::AddSignalHandler(SignalHandler, nullptr);
2110   CrashReporter = this;
2111 
2112   PIC.registerBeforeNonSkippedPassCallback([this](StringRef PassID, Any IR) {
2113     SavedIR.clear();
2114     raw_string_ostream OS(SavedIR);
2115     OS << formatv("*** Dump of {0}IR Before Last Pass {1}",
2116                   llvm::forcePrintModuleIR() ? "Module " : "", PassID);
2117     if (!isInteresting(IR, PassID)) {
2118       OS << " Filtered Out ***\n";
2119       return;
2120     }
2121     OS << " Started ***\n";
2122     unwrapAndPrint(OS, IR);
2123   });
2124 }
2125 
2126 void StandardInstrumentations::registerCallbacks(
2127     PassInstrumentationCallbacks &PIC, FunctionAnalysisManager *FAM) {
2128   PrintIR.registerCallbacks(PIC);
2129   PrintPass.registerCallbacks(PIC);
2130   TimePasses.registerCallbacks(PIC);
2131   OptNone.registerCallbacks(PIC);
2132   OptBisect.registerCallbacks(PIC);
2133   if (FAM)
2134     PreservedCFGChecker.registerCallbacks(PIC, *FAM);
2135   PrintChangedIR.registerCallbacks(PIC);
2136   PseudoProbeVerification.registerCallbacks(PIC);
2137   if (VerifyEach)
2138     Verify.registerCallbacks(PIC);
2139   PrintChangedDiff.registerCallbacks(PIC);
2140   WebsiteChangeReporter.registerCallbacks(PIC);
2141   PrintCrashIR.registerCallbacks(PIC);
2142 }
2143 
2144 template class ChangeReporter<std::string>;
2145 template class TextChangeReporter<std::string>;
2146 
2147 template class BlockDataT<EmptyData>;
2148 template class FuncDataT<EmptyData>;
2149 template class IRDataT<EmptyData>;
2150 template class ChangeReporter<IRDataT<EmptyData>>;
2151 template class TextChangeReporter<IRDataT<EmptyData>>;
2152 template class IRComparer<EmptyData>;
2153 
2154 } // namespace llvm
2155