1 //===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- 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 //
9 // This utility is a simple driver that allows static performance analysis on
10 // machine code similarly to how IACA (Intel Architecture Code Analyzer) works.
11 //
12 //   llvm-mca [options] <file-name>
13 //      -march <type>
14 //      -mcpu <cpu>
15 //      -o <file>
16 //
17 // The target defaults to the host target.
18 // The cpu defaults to the 'native' host cpu.
19 // The output defaults to standard output.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #include "CodeRegion.h"
24 #include "CodeRegionGenerator.h"
25 #include "PipelinePrinter.h"
26 #include "Views/BottleneckAnalysis.h"
27 #include "Views/DispatchStatistics.h"
28 #include "Views/InstructionInfoView.h"
29 #include "Views/RegisterFileStatistics.h"
30 #include "Views/ResourcePressureView.h"
31 #include "Views/RetireControlUnitStatistics.h"
32 #include "Views/SchedulerStatistics.h"
33 #include "Views/SummaryView.h"
34 #include "Views/TimelineView.h"
35 #ifdef HAS_AMDGPU
36 #include "lib/AMDGPU/AMDGPUCustomBehaviour.h"
37 #endif
38 #include "llvm/MC/MCAsmBackend.h"
39 #include "llvm/MC/MCAsmInfo.h"
40 #include "llvm/MC/MCCodeEmitter.h"
41 #include "llvm/MC/MCContext.h"
42 #include "llvm/MC/MCObjectFileInfo.h"
43 #include "llvm/MC/MCRegisterInfo.h"
44 #include "llvm/MC/MCSubtargetInfo.h"
45 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
46 #include "llvm/MCA/CodeEmitter.h"
47 #include "llvm/MCA/Context.h"
48 #include "llvm/MCA/CustomBehaviour.h"
49 #include "llvm/MCA/InstrBuilder.h"
50 #include "llvm/MCA/Pipeline.h"
51 #include "llvm/MCA/Stages/EntryStage.h"
52 #include "llvm/MCA/Stages/InstructionTables.h"
53 #include "llvm/MCA/Support.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/ErrorOr.h"
57 #include "llvm/Support/FileSystem.h"
58 #include "llvm/Support/Host.h"
59 #include "llvm/Support/InitLLVM.h"
60 #include "llvm/Support/MemoryBuffer.h"
61 #include "llvm/Support/SourceMgr.h"
62 #include "llvm/Support/TargetRegistry.h"
63 #include "llvm/Support/TargetSelect.h"
64 #include "llvm/Support/ToolOutputFile.h"
65 #include "llvm/Support/WithColor.h"
66 
67 using namespace llvm;
68 
69 static mc::RegisterMCTargetOptionsFlags MOF;
70 
71 static cl::OptionCategory ToolOptions("Tool Options");
72 static cl::OptionCategory ViewOptions("View Options");
73 
74 static cl::opt<std::string> InputFilename(cl::Positional,
75                                           cl::desc("<input file>"),
76                                           cl::cat(ToolOptions), cl::init("-"));
77 
78 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
79                                            cl::init("-"), cl::cat(ToolOptions),
80                                            cl::value_desc("filename"));
81 
82 static cl::opt<std::string>
83     ArchName("march",
84              cl::desc("Target architecture. "
85                       "See -version for available targets"),
86              cl::cat(ToolOptions));
87 
88 static cl::opt<std::string>
89     TripleName("mtriple",
90                cl::desc("Target triple. See -version for available targets"),
91                cl::cat(ToolOptions));
92 
93 static cl::opt<std::string>
94     MCPU("mcpu",
95          cl::desc("Target a specific cpu type (-mcpu=help for details)"),
96          cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native"));
97 
98 static cl::opt<std::string> MATTR("mattr",
99                                   cl::desc("Additional target features."),
100                                   cl::cat(ToolOptions));
101 
102 static cl::opt<bool> PrintJson("json",
103                                cl::desc("Print the output in json format"),
104                                cl::cat(ToolOptions), cl::init(false));
105 
106 static cl::opt<int>
107     OutputAsmVariant("output-asm-variant",
108                      cl::desc("Syntax variant to use for output printing"),
109                      cl::cat(ToolOptions), cl::init(-1));
110 
111 static cl::opt<bool>
112     PrintImmHex("print-imm-hex", cl::cat(ToolOptions), cl::init(false),
113                 cl::desc("Prefer hex format when printing immediate values"));
114 
115 static cl::opt<unsigned> Iterations("iterations",
116                                     cl::desc("Number of iterations to run"),
117                                     cl::cat(ToolOptions), cl::init(0));
118 
119 static cl::opt<unsigned>
120     DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
121                   cl::cat(ToolOptions), cl::init(0));
122 
123 static cl::opt<unsigned>
124     RegisterFileSize("register-file-size",
125                      cl::desc("Maximum number of physical registers which can "
126                               "be used for register mappings"),
127                      cl::cat(ToolOptions), cl::init(0));
128 
129 static cl::opt<unsigned>
130     MicroOpQueue("micro-op-queue-size", cl::Hidden,
131                  cl::desc("Number of entries in the micro-op queue"),
132                  cl::cat(ToolOptions), cl::init(0));
133 
134 static cl::opt<unsigned>
135     DecoderThroughput("decoder-throughput", cl::Hidden,
136                       cl::desc("Maximum throughput from the decoders "
137                                "(instructions per cycle)"),
138                       cl::cat(ToolOptions), cl::init(0));
139 
140 static cl::opt<bool>
141     PrintRegisterFileStats("register-file-stats",
142                            cl::desc("Print register file statistics"),
143                            cl::cat(ViewOptions), cl::init(false));
144 
145 static cl::opt<bool> PrintDispatchStats("dispatch-stats",
146                                         cl::desc("Print dispatch statistics"),
147                                         cl::cat(ViewOptions), cl::init(false));
148 
149 static cl::opt<bool>
150     PrintSummaryView("summary-view", cl::Hidden,
151                      cl::desc("Print summary view (enabled by default)"),
152                      cl::cat(ViewOptions), cl::init(true));
153 
154 static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
155                                          cl::desc("Print scheduler statistics"),
156                                          cl::cat(ViewOptions), cl::init(false));
157 
158 static cl::opt<bool>
159     PrintRetireStats("retire-stats",
160                      cl::desc("Print retire control unit statistics"),
161                      cl::cat(ViewOptions), cl::init(false));
162 
163 static cl::opt<bool> PrintResourcePressureView(
164     "resource-pressure",
165     cl::desc("Print the resource pressure view (enabled by default)"),
166     cl::cat(ViewOptions), cl::init(true));
167 
168 static cl::opt<bool> PrintTimelineView("timeline",
169                                        cl::desc("Print the timeline view"),
170                                        cl::cat(ViewOptions), cl::init(false));
171 
172 static cl::opt<unsigned> TimelineMaxIterations(
173     "timeline-max-iterations",
174     cl::desc("Maximum number of iterations to print in timeline view"),
175     cl::cat(ViewOptions), cl::init(0));
176 
177 static cl::opt<unsigned>
178     TimelineMaxCycles("timeline-max-cycles",
179                       cl::desc("Maximum number of cycles in the timeline view, "
180                                "or 0 for unlimited. Defaults to 80 cycles"),
181                       cl::cat(ViewOptions), cl::init(80));
182 
183 static cl::opt<bool>
184     AssumeNoAlias("noalias",
185                   cl::desc("If set, assume that loads and stores do not alias"),
186                   cl::cat(ToolOptions), cl::init(true));
187 
188 static cl::opt<unsigned> LoadQueueSize("lqueue",
189                                        cl::desc("Size of the load queue"),
190                                        cl::cat(ToolOptions), cl::init(0));
191 
192 static cl::opt<unsigned> StoreQueueSize("squeue",
193                                         cl::desc("Size of the store queue"),
194                                         cl::cat(ToolOptions), cl::init(0));
195 
196 static cl::opt<bool>
197     PrintInstructionTables("instruction-tables",
198                            cl::desc("Print instruction tables"),
199                            cl::cat(ToolOptions), cl::init(false));
200 
201 static cl::opt<bool> PrintInstructionInfoView(
202     "instruction-info",
203     cl::desc("Print the instruction info view (enabled by default)"),
204     cl::cat(ViewOptions), cl::init(true));
205 
206 static cl::opt<bool> EnableAllStats("all-stats",
207                                     cl::desc("Print all hardware statistics"),
208                                     cl::cat(ViewOptions), cl::init(false));
209 
210 static cl::opt<bool>
211     EnableAllViews("all-views",
212                    cl::desc("Print all views including hardware statistics"),
213                    cl::cat(ViewOptions), cl::init(false));
214 
215 static cl::opt<bool> EnableBottleneckAnalysis(
216     "bottleneck-analysis",
217     cl::desc("Enable bottleneck analysis (disabled by default)"),
218     cl::cat(ViewOptions), cl::init(false));
219 
220 static cl::opt<bool> ShowEncoding(
221     "show-encoding",
222     cl::desc("Print encoding information in the instruction info view"),
223     cl::cat(ViewOptions), cl::init(false));
224 
225 static cl::opt<bool> DisableCustomBehaviour(
226     "disable-cb",
227     cl::desc(
228         "Disable custom behaviour (use the default class which does nothing)."),
229     cl::cat(ViewOptions), cl::init(false));
230 
231 namespace {
232 
233 const Target *getTarget(const char *ProgName) {
234   if (TripleName.empty())
235     TripleName = Triple::normalize(sys::getDefaultTargetTriple());
236   Triple TheTriple(TripleName);
237 
238   // Get the target specific parser.
239   std::string Error;
240   const Target *TheTarget =
241       TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
242   if (!TheTarget) {
243     errs() << ProgName << ": " << Error;
244     return nullptr;
245   }
246 
247   // Update TripleName with the updated triple from the target lookup.
248   TripleName = TheTriple.str();
249 
250   // Return the found target.
251   return TheTarget;
252 }
253 
254 ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
255   if (OutputFilename == "")
256     OutputFilename = "-";
257   std::error_code EC;
258   auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
259                                               sys::fs::OF_TextWithCRLF);
260   if (!EC)
261     return std::move(Out);
262   return EC;
263 }
264 } // end of anonymous namespace
265 
266 static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
267   if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
268     O = Default.getValue();
269 }
270 
271 static void processViewOptions(bool IsOutOfOrder) {
272   if (!EnableAllViews.getNumOccurrences() &&
273       !EnableAllStats.getNumOccurrences())
274     return;
275 
276   if (EnableAllViews.getNumOccurrences()) {
277     processOptionImpl(PrintSummaryView, EnableAllViews);
278     if (IsOutOfOrder)
279       processOptionImpl(EnableBottleneckAnalysis, EnableAllViews);
280     processOptionImpl(PrintResourcePressureView, EnableAllViews);
281     processOptionImpl(PrintTimelineView, EnableAllViews);
282     processOptionImpl(PrintInstructionInfoView, EnableAllViews);
283   }
284 
285   const cl::opt<bool> &Default =
286       EnableAllViews.getPosition() < EnableAllStats.getPosition()
287           ? EnableAllStats
288           : EnableAllViews;
289   processOptionImpl(PrintRegisterFileStats, Default);
290   processOptionImpl(PrintDispatchStats, Default);
291   processOptionImpl(PrintSchedulerStats, Default);
292   if (IsOutOfOrder)
293     processOptionImpl(PrintRetireStats, Default);
294 }
295 
296 std::unique_ptr<mca::InstrPostProcess>
297 createInstrPostProcess(const Triple &TheTriple, const MCSubtargetInfo &STI,
298                        const MCInstrInfo &MCII) {
299   // Might be a good idea to have a separate flag so that InstrPostProcess
300   // can be used with or without CustomBehaviour
301   if (DisableCustomBehaviour)
302     return std::make_unique<mca::InstrPostProcess>(STI, MCII);
303 #ifdef HAS_AMDGPU
304   if (TheTriple.isAMDGPU())
305     return std::make_unique<mca::AMDGPUInstrPostProcess>(STI, MCII);
306 #endif
307   return std::make_unique<mca::InstrPostProcess>(STI, MCII);
308 }
309 
310 std::unique_ptr<mca::CustomBehaviour>
311 createCustomBehaviour(const Triple &TheTriple, const MCSubtargetInfo &STI,
312                       const mca::SourceMgr &SrcMgr, const MCInstrInfo &MCII) {
313   // Build the appropriate CustomBehaviour object for the current target.
314   // The CustomBehaviour class should never depend on the source code,
315   // but it can depend on the list of mca::Instruction and any classes
316   // that can be built using just the target info. If you need extra
317   // information from the source code or the list of MCInst, consider
318   // adding that information to the mca::Instruction class and setting
319   // it during InstrBuilder::createInstruction().
320   if (DisableCustomBehaviour)
321     return std::make_unique<mca::CustomBehaviour>(STI, SrcMgr, MCII);
322 #ifdef HAS_AMDGPU
323   if (TheTriple.isAMDGPU())
324     return std::make_unique<mca::AMDGPUCustomBehaviour>(STI, SrcMgr, MCII);
325 #endif
326   return std::make_unique<mca::CustomBehaviour>(STI, SrcMgr, MCII);
327 }
328 
329 // Returns true on success.
330 static bool runPipeline(mca::Pipeline &P) {
331   // Handle pipeline errors here.
332   Expected<unsigned> Cycles = P.run();
333   if (!Cycles) {
334     WithColor::error() << toString(Cycles.takeError());
335     return false;
336   }
337   return true;
338 }
339 
340 int main(int argc, char **argv) {
341   InitLLVM X(argc, argv);
342 
343   // Initialize targets and assembly parsers.
344   InitializeAllTargetInfos();
345   InitializeAllTargetMCs();
346   InitializeAllAsmParsers();
347 
348   // Enable printing of available targets when flag --version is specified.
349   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
350 
351   cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
352 
353   // Parse flags and initialize target options.
354   cl::ParseCommandLineOptions(argc, argv,
355                               "llvm machine code performance analyzer.\n");
356 
357   // Get the target from the triple. If a triple is not specified, then select
358   // the default triple for the host. If the triple doesn't correspond to any
359   // registered target, then exit with an error message.
360   const char *ProgName = argv[0];
361   const Target *TheTarget = getTarget(ProgName);
362   if (!TheTarget)
363     return 1;
364 
365   // GetTarget() may replaced TripleName with a default triple.
366   // For safety, reconstruct the Triple object.
367   Triple TheTriple(TripleName);
368 
369   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
370       MemoryBuffer::getFileOrSTDIN(InputFilename);
371   if (std::error_code EC = BufferPtr.getError()) {
372     WithColor::error() << InputFilename << ": " << EC.message() << '\n';
373     return 1;
374   }
375 
376   if (MCPU == "native")
377     MCPU = std::string(llvm::sys::getHostCPUName());
378 
379   std::unique_ptr<MCSubtargetInfo> STI(
380       TheTarget->createMCSubtargetInfo(TripleName, MCPU, MATTR));
381   assert(STI && "Unable to create subtarget info!");
382   if (!STI->isCPUStringValid(MCPU))
383     return 1;
384 
385   bool IsOutOfOrder = STI->getSchedModel().isOutOfOrder();
386   if (!PrintInstructionTables && !IsOutOfOrder) {
387     WithColor::warning() << "support for in-order CPU '" << MCPU
388                          << "' is experimental.\n";
389   }
390 
391   if (!STI->getSchedModel().hasInstrSchedModel()) {
392     WithColor::error()
393         << "unable to find instruction-level scheduling information for"
394         << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
395         << "'.\n";
396 
397     if (STI->getSchedModel().InstrItineraries)
398       WithColor::note()
399           << "cpu '" << MCPU << "' provides itineraries. However, "
400           << "instruction itineraries are currently unsupported.\n";
401     return 1;
402   }
403 
404   // Apply overrides to llvm-mca specific options.
405   processViewOptions(IsOutOfOrder);
406 
407   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
408   assert(MRI && "Unable to create target register info!");
409 
410   MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
411   std::unique_ptr<MCAsmInfo> MAI(
412       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
413   assert(MAI && "Unable to create target asm info!");
414 
415   SourceMgr SrcMgr;
416 
417   // Tell SrcMgr about this buffer, which is what the parser will pick up.
418   SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
419 
420   MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
421   std::unique_ptr<MCObjectFileInfo> MOFI(
422       TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
423   Ctx.setObjectFileInfo(MOFI.get());
424 
425   std::unique_ptr<buffer_ostream> BOS;
426 
427   std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
428   assert(MCII && "Unable to create instruction info!");
429 
430   std::unique_ptr<MCInstrAnalysis> MCIA(
431       TheTarget->createMCInstrAnalysis(MCII.get()));
432 
433   // Need to initialize an MCInstPrinter as it is
434   // required for initializing the MCTargetStreamer
435   // which needs to happen within the CRG.parseCodeRegions() call below.
436   // Without an MCTargetStreamer, certain assembly directives can trigger a
437   // segfault. (For example, the .cv_fpo_proc directive on x86 will segfault if
438   // we don't initialize the MCTargetStreamer.)
439   unsigned IPtempOutputAsmVariant =
440       OutputAsmVariant == -1 ? 0 : OutputAsmVariant;
441   std::unique_ptr<MCInstPrinter> IPtemp(TheTarget->createMCInstPrinter(
442       Triple(TripleName), IPtempOutputAsmVariant, *MAI, *MCII, *MRI));
443   if (!IPtemp) {
444     WithColor::error()
445         << "unable to create instruction printer for target triple '"
446         << TheTriple.normalize() << "' with assembly variant "
447         << IPtempOutputAsmVariant << ".\n";
448     return 1;
449   }
450 
451   // Parse the input and create CodeRegions that llvm-mca can analyze.
452   mca::AsmCodeRegionGenerator CRG(*TheTarget, SrcMgr, Ctx, *MAI, *STI, *MCII);
453   Expected<const mca::CodeRegions &> RegionsOrErr =
454       CRG.parseCodeRegions(std::move(IPtemp));
455   if (!RegionsOrErr) {
456     if (auto Err =
457             handleErrors(RegionsOrErr.takeError(), [](const StringError &E) {
458               WithColor::error() << E.getMessage() << '\n';
459             })) {
460       // Default case.
461       WithColor::error() << toString(std::move(Err)) << '\n';
462     }
463     return 1;
464   }
465   const mca::CodeRegions &Regions = *RegionsOrErr;
466 
467   // Early exit if errors were found by the code region parsing logic.
468   if (!Regions.isValid())
469     return 1;
470 
471   if (Regions.empty()) {
472     WithColor::error() << "no assembly instructions found.\n";
473     return 1;
474   }
475 
476   // Now initialize the output file.
477   auto OF = getOutputStream();
478   if (std::error_code EC = OF.getError()) {
479     WithColor::error() << EC.message() << '\n';
480     return 1;
481   }
482 
483   unsigned AssemblerDialect = CRG.getAssemblerDialect();
484   if (OutputAsmVariant >= 0)
485     AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
486   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
487       Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
488   if (!IP) {
489     WithColor::error()
490         << "unable to create instruction printer for target triple '"
491         << TheTriple.normalize() << "' with assembly variant "
492         << AssemblerDialect << ".\n";
493     return 1;
494   }
495 
496   // Set the display preference for hex vs. decimal immediates.
497   IP->setPrintImmHex(PrintImmHex);
498 
499   std::unique_ptr<ToolOutputFile> TOF = std::move(*OF);
500 
501   const MCSchedModel &SM = STI->getSchedModel();
502 
503   // Create an instruction builder.
504   mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get());
505 
506   // Create a context to control ownership of the pipeline hardware.
507   mca::Context MCA(*MRI, *STI);
508 
509   mca::PipelineOptions PO(MicroOpQueue, DecoderThroughput, DispatchWidth,
510                           RegisterFileSize, LoadQueueSize, StoreQueueSize,
511                           AssumeNoAlias, EnableBottleneckAnalysis);
512 
513   // Number each region in the sequence.
514   unsigned RegionIdx = 0;
515 
516   std::unique_ptr<MCCodeEmitter> MCE(
517       TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
518   assert(MCE && "Unable to create code emitter!");
519 
520   std::unique_ptr<MCAsmBackend> MAB(TheTarget->createMCAsmBackend(
521       *STI, *MRI, mc::InitMCTargetOptionsFromFlags()));
522   assert(MAB && "Unable to create asm backend!");
523 
524   json::Object JSONOutput;
525   for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
526     // Skip empty code regions.
527     if (Region->empty())
528       continue;
529 
530     IB.clear();
531 
532     // Lower the MCInst sequence into an mca::Instruction sequence.
533     ArrayRef<MCInst> Insts = Region->getInstructions();
534     mca::CodeEmitter CE(*STI, *MAB, *MCE, Insts);
535     std::unique_ptr<mca::InstrPostProcess> IPP =
536         createInstrPostProcess(TheTriple, *STI, *MCII);
537     std::vector<std::unique_ptr<mca::Instruction>> LoweredSequence;
538     for (const MCInst &MCI : Insts) {
539       Expected<std::unique_ptr<mca::Instruction>> Inst =
540           IB.createInstruction(MCI);
541       if (!Inst) {
542         if (auto NewE = handleErrors(
543                 Inst.takeError(),
544                 [&IP, &STI](const mca::InstructionError<MCInst> &IE) {
545                   std::string InstructionStr;
546                   raw_string_ostream SS(InstructionStr);
547                   WithColor::error() << IE.Message << '\n';
548                   IP->printInst(&IE.Inst, 0, "", *STI, SS);
549                   SS.flush();
550                   WithColor::note()
551                       << "instruction: " << InstructionStr << '\n';
552                 })) {
553           // Default case.
554           WithColor::error() << toString(std::move(NewE));
555         }
556         return 1;
557       }
558 
559       IPP->postProcessInstruction(Inst.get(), MCI);
560 
561       LoweredSequence.emplace_back(std::move(Inst.get()));
562     }
563 
564     mca::SourceMgr S(LoweredSequence, PrintInstructionTables ? 1 : Iterations);
565 
566     if (PrintInstructionTables) {
567       //  Create a pipeline, stages, and a printer.
568       auto P = std::make_unique<mca::Pipeline>();
569       P->appendStage(std::make_unique<mca::EntryStage>(S));
570       P->appendStage(std::make_unique<mca::InstructionTables>(SM));
571 
572       mca::PipelinePrinter Printer(*P, *Region, RegionIdx, *STI, PO);
573       if (PrintJson) {
574         Printer.addView(
575             std::make_unique<mca::InstructionView>(*STI, *IP, Insts));
576       }
577 
578       // Create the views for this pipeline, execute, and emit a report.
579       if (PrintInstructionInfoView) {
580         Printer.addView(std::make_unique<mca::InstructionInfoView>(
581             *STI, *MCII, CE, ShowEncoding, Insts, *IP));
582       }
583       Printer.addView(
584           std::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts));
585 
586       if (!runPipeline(*P))
587         return 1;
588 
589       if (PrintJson) {
590         Printer.printReport(JSONOutput);
591       } else {
592         Printer.printReport(TOF->os());
593       }
594 
595       ++RegionIdx;
596       continue;
597     }
598 
599     // Create the CustomBehaviour object for enforcing Target Specific
600     // behaviours and dependencies that aren't expressed well enough
601     // in the tablegen. CB cannot depend on the list of MCInst or
602     // the source code (but it can depend on the list of
603     // mca::Instruction or any objects that can be reconstructed
604     // from the target information).
605     std::unique_ptr<mca::CustomBehaviour> CB =
606         createCustomBehaviour(TheTriple, *STI, S, *MCII);
607 
608     // Create a basic pipeline simulating an out-of-order backend.
609     auto P = MCA.createDefaultPipeline(PO, S, *CB);
610 
611     mca::PipelinePrinter Printer(*P, *Region, RegionIdx, *STI, PO);
612 
613     // When we output JSON, we add a view that contains the instructions
614     // and CPU resource information.
615     if (PrintJson) {
616       auto IV = std::make_unique<mca::InstructionView>(*STI, *IP, Insts);
617       Printer.addView(std::move(IV));
618     }
619 
620     if (PrintSummaryView)
621       Printer.addView(
622           std::make_unique<mca::SummaryView>(SM, Insts, DispatchWidth));
623 
624     if (EnableBottleneckAnalysis) {
625       if (!IsOutOfOrder) {
626         WithColor::warning()
627             << "bottleneck analysis is not supported for in-order CPU '" << MCPU
628             << "'.\n";
629       }
630       Printer.addView(std::make_unique<mca::BottleneckAnalysis>(
631           *STI, *IP, Insts, S.getNumIterations()));
632     }
633 
634     if (PrintInstructionInfoView)
635       Printer.addView(std::make_unique<mca::InstructionInfoView>(
636           *STI, *MCII, CE, ShowEncoding, Insts, *IP));
637 
638     if (PrintDispatchStats)
639       Printer.addView(std::make_unique<mca::DispatchStatistics>());
640 
641     if (PrintSchedulerStats)
642       Printer.addView(std::make_unique<mca::SchedulerStatistics>(*STI));
643 
644     if (PrintRetireStats)
645       Printer.addView(std::make_unique<mca::RetireControlUnitStatistics>(SM));
646 
647     if (PrintRegisterFileStats)
648       Printer.addView(std::make_unique<mca::RegisterFileStatistics>(*STI));
649 
650     if (PrintResourcePressureView)
651       Printer.addView(
652           std::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts));
653 
654     if (PrintTimelineView) {
655       unsigned TimelineIterations =
656           TimelineMaxIterations ? TimelineMaxIterations : 10;
657       Printer.addView(std::make_unique<mca::TimelineView>(
658           *STI, *IP, Insts, std::min(TimelineIterations, S.getNumIterations()),
659           TimelineMaxCycles));
660     }
661 
662     if (!runPipeline(*P))
663       return 1;
664 
665     if (PrintJson) {
666       Printer.printReport(JSONOutput);
667     } else {
668       Printer.printReport(TOF->os());
669     }
670 
671     ++RegionIdx;
672   }
673 
674   if (PrintJson)
675     TOF->os() << formatv("{0:2}", json::Value(std::move(JSONOutput))) << "\n";
676 
677   TOF->keep();
678   return 0;
679 }
680