blob: 51d6ad1d9841c6c391f591f252da6215e919476e [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- C++ -* -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This utility is a simple driver that allows static performance analysis on
11// machine code similarly to how IACA (Intel Architecture Code Analyzer) works.
12//
13// llvm-mca [options] <file-name>
14// -march <type>
15// -mcpu <cpu>
16// -o <file>
17//
18// The target defaults to the host target.
Andrea Di Biagio93c49d52018-04-25 10:18:25 +000019// The cpu defaults to the 'native' host cpu.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000020// The output defaults to standard output.
21//
22//===----------------------------------------------------------------------===//
23
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000024#include "BackendPrinter.h"
Andrea Di Biagioc6590122018-04-09 16:39:52 +000025#include "CodeRegion.h"
Andrea Di Biagio821f6502018-04-10 14:55:14 +000026#include "DispatchStatistics.h"
Matt Davis5d1cda12018-05-15 20:21:04 +000027#include "FetchStage.h"
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000028#include "InstructionInfoView.h"
Andrea Di Biagiod1569292018-03-26 12:04:53 +000029#include "InstructionTables.h"
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +000030#include "RegisterFileStatistics.h"
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000031#include "ResourcePressureView.h"
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +000032#include "RetireControlUnitStatistics.h"
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +000033#include "SchedulerStatistics.h"
Andrea Di Biagio0cc66c72018-03-09 13:52:03 +000034#include "SummaryView.h"
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000035#include "TimelineView.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000036#include "llvm/MC/MCAsmInfo.h"
37#include "llvm/MC/MCContext.h"
38#include "llvm/MC/MCObjectFileInfo.h"
39#include "llvm/MC/MCParser/MCTargetAsmParser.h"
40#include "llvm/MC/MCRegisterInfo.h"
41#include "llvm/MC/MCStreamer.h"
42#include "llvm/Support/CommandLine.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000043#include "llvm/Support/ErrorOr.h"
44#include "llvm/Support/FileSystem.h"
Andrea Di Biagio641cca32018-04-25 10:27:30 +000045#include "llvm/Support/Host.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000046#include "llvm/Support/InitLLVM.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000047#include "llvm/Support/MemoryBuffer.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000048#include "llvm/Support/SourceMgr.h"
49#include "llvm/Support/TargetRegistry.h"
50#include "llvm/Support/TargetSelect.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000051#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghere6adef092018-04-18 15:26:51 +000052#include "llvm/Support/WithColor.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000053
54using namespace llvm;
55
Filipe Cabecinhasdef742c2018-04-25 14:39:16 +000056static llvm::cl::OptionCategory ViewOptions("View Options");
Andrea Di Biagio534e1da2018-04-25 11:33:14 +000057
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000058static cl::opt<std::string>
59 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
60
61static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
62 cl::init("-"),
63 cl::value_desc("filename"));
64
65static cl::opt<std::string>
66 ArchName("march", cl::desc("Target arch to assemble for, "
67 "see -version for available targets"));
68
69static cl::opt<std::string>
70 TripleName("mtriple", cl::desc("Target triple to assemble for, "
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000071 "see -version for available targets"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000072
73static cl::opt<std::string>
74 MCPU("mcpu",
75 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Andrea Di Biagio93c49d52018-04-25 10:18:25 +000076 cl::value_desc("cpu-name"), cl::init("native"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000077
Andrea Di Biagio06268642018-04-24 16:19:08 +000078static cl::opt<int>
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000079 OutputAsmVariant("output-asm-variant",
Andrea Di Biagio06268642018-04-24 16:19:08 +000080 cl::desc("Syntax variant to use for output printing"),
81 cl::init(-1));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000082
83static cl::opt<unsigned> Iterations("iterations",
84 cl::desc("Number of iterations to run"),
85 cl::init(0));
86
87static cl::opt<unsigned> DispatchWidth(
88 "dispatch",
89 cl::desc("Dispatch Width. By default it is set equal to IssueWidth"),
90 cl::init(0));
91
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000092static cl::opt<unsigned>
93 RegisterFileSize("register-file-size",
94 cl::desc("Maximum number of temporary registers which can "
95 "be used for register mappings"),
96 cl::init(0));
97
Andrea Di Biagio29538c62018-03-23 11:33:09 +000098static cl::opt<bool>
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +000099 PrintRegisterFileStats("register-file-stats",
100 cl::desc("Print register file statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000101 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000102
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000103static cl::opt<bool> PrintDispatchStats("dispatch-stats",
104 cl::desc("Print dispatch statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000105 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000106
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000107static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
108 cl::desc("Print scheduler statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000109 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000110
111static cl::opt<bool>
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000112 PrintRetireStats("retire-stats",
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000113 cl::desc("Print retire control unit statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000114 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000115
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000116static cl::opt<bool> PrintResourcePressureView(
117 "resource-pressure",
118 cl::desc("Print the resource pressure view (enabled by default)"),
119 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000120
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000121static cl::opt<bool> PrintTimelineView("timeline",
122 cl::desc("Print the timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000123 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000124
125static cl::opt<unsigned> TimelineMaxIterations(
126 "timeline-max-iterations",
127 cl::desc("Maximum number of iterations to print in timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000128 cl::cat(ViewOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000129
130static cl::opt<unsigned> TimelineMaxCycles(
131 "timeline-max-cycles",
132 cl::desc(
133 "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000134 cl::cat(ViewOptions), cl::init(80));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000135
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000136static cl::opt<bool> AssumeNoAlias(
137 "noalias",
138 cl::desc("If set, it assumes that loads and stores do not alias"),
139 cl::init(true));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000140
141static cl::opt<unsigned>
142 LoadQueueSize("lqueue", cl::desc("Size of the load queue"), cl::init(0));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000143
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000144static cl::opt<unsigned>
145 StoreQueueSize("squeue", cl::desc("Size of the store queue"), cl::init(0));
146
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000147static cl::opt<bool>
148 PrintInstructionTables("instruction-tables",
149 cl::desc("Print instruction tables"),
150 cl::init(false));
151
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000152static cl::opt<bool> PrintInstructionInfoView(
153 "instruction-info",
154 cl::desc("Print the instruction info view (enabled by default)"),
155 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000156
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000157namespace {
158
159const Target *getTarget(const char *ProgName) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000160 TripleName = Triple::normalize(TripleName);
161 if (TripleName.empty())
162 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
163 Triple TheTriple(TripleName);
164
165 // Get the target specific parser.
166 std::string Error;
167 const Target *TheTarget =
168 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
169 if (!TheTarget) {
170 errs() << ProgName << ": " << Error;
171 return nullptr;
172 }
173
174 // Return the found target.
175 return TheTarget;
176}
177
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000178// A comment consumer that parses strings.
179// The only valid tokens are strings.
180class MCACommentConsumer : public AsmCommentConsumer {
181public:
182 mca::CodeRegions &Regions;
183
184 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
185 void HandleComment(SMLoc Loc, StringRef CommentText) override {
186 // Skip empty comments.
187 StringRef Comment(CommentText);
188 if (Comment.empty())
189 return;
190
191 // Skip spaces and tabs
192 unsigned Position = Comment.find_first_not_of(" \t");
193 if (Position >= Comment.size())
194 // we reached the end of the comment. Bail out.
195 return;
196
197 Comment = Comment.drop_front(Position);
198 if (Comment.consume_front("LLVM-MCA-END")) {
199 Regions.endRegion(Loc);
200 return;
201 }
202
203 // Now try to parse string LLVM-MCA-BEGIN
204 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
205 return;
206
207 // Skip spaces and tabs
208 Position = Comment.find_first_not_of(" \t");
209 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000210 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000211 // Use the rest of the string as a descriptor for this code snippet.
212 Regions.beginRegion(Comment, Loc);
213 }
214};
215
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000216int AssembleInput(const char *ProgName, MCAsmParser &Parser,
217 const Target *TheTarget, MCSubtargetInfo &STI,
218 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000219 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000220 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000221
222 if (!TAP) {
Andrea Di Biagio24fb4fc2018-05-04 13:52:12 +0000223 WithColor::error() << "this target does not support assembly parsing.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000224 return 1;
225 }
226
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000227 Parser.setTargetParser(*TAP);
228 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000229}
230
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000231ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000232 if (OutputFilename == "")
233 OutputFilename = "-";
234 std::error_code EC;
235 auto Out =
236 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
237 if (!EC)
238 return std::move(Out);
239 return EC;
240}
241
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000242class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000243 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000244
245public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000246 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
247 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000248
249 // We only want to intercept the emission of new instructions.
250 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
251 bool /* unused */) override {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000252 Regions.addInstruction(llvm::make_unique<const MCInst>(Inst));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000253 }
254
255 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
256 return true;
257 }
258
259 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
260 unsigned ByteAlignment) override {}
261 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
262 uint64_t Size = 0, unsigned ByteAlignment = 0) override {}
263 void EmitGPRel32Value(const MCExpr *Value) override {}
264 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
265 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
266 void EmitCOFFSymbolType(int Type) override {}
267 void EndCOFFSymbolDef() override {}
268
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000269 const std::vector<std::unique_ptr<const MCInst>> &
270 GetInstructionSequence(unsigned Index) const {
271 return Regions.getInstructionSequence(Index);
272 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000273};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000274} // end of anonymous namespace
275
276int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000277 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000278
279 // Initialize targets and assembly parsers.
280 llvm::InitializeAllTargetInfos();
281 llvm::InitializeAllTargetMCs();
282 llvm::InitializeAllAsmParsers();
283
284 // Enable printing of available targets when flag --version is specified.
285 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
286
287 // Parse flags and initialize target options.
288 cl::ParseCommandLineOptions(argc, argv,
289 "llvm machine code performance analyzer.\n");
290 MCTargetOptions MCOptions;
291 MCOptions.PreserveAsmComments = false;
292
293 // Get the target from the triple. If a triple is not specified, then select
294 // the default triple for the host. If the triple doesn't correspond to any
295 // registered target, then exit with an error message.
296 const char *ProgName = argv[0];
297 const Target *TheTarget = getTarget(ProgName);
298 if (!TheTarget)
299 return 1;
300
301 // GetTarget() may replaced TripleName with a default triple.
302 // For safety, reconstruct the Triple object.
303 Triple TheTriple(TripleName);
304
305 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
306 MemoryBuffer::getFileOrSTDIN(InputFilename);
307 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000308 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000309 return 1;
310 }
311
312 SourceMgr SrcMgr;
313
314 // Tell SrcMgr about this buffer, which is what the parser will pick up.
315 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
316
317 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
318 assert(MRI && "Unable to create target register info!");
319
320 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
321 assert(MAI && "Unable to create target asm info!");
322
323 MCObjectFileInfo MOFI;
324 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
325 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
326
327 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000328
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000329 mca::CodeRegions Regions(SrcMgr);
330 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000331
332 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000333
334 if (!MCPU.compare("native"))
335 MCPU = llvm::sys::getHostCPUName();
336
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000337 std::unique_ptr<MCSubtargetInfo> STI(
338 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
339 if (!STI->isCPUStringValid(MCPU))
340 return 1;
341
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000342 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000343 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
344 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000345 return 1;
346 }
347
348 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000349 WithColor::error()
350 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000351 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
352 << "'.\n";
353
354 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000355 WithColor::note()
356 << "cpu '" << MCPU << "' provides itineraries. However, "
357 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000358 return 1;
359 }
360
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000361 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000362 MCAsmLexer &Lexer = P->getLexer();
363 MCACommentConsumer CC(Regions);
364 Lexer.setCommentConsumer(&CC);
365
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000366 if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions))
367 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000368
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000369 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000370 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000371 return 1;
372 }
373
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000374 // Now initialize the output file.
375 auto OF = getOutputStream();
376 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000377 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000378 return 1;
379 }
380
Andrea Di Biagio06268642018-04-24 16:19:08 +0000381 unsigned AssemblerDialect = P->getAssemblerDialect();
382 if (OutputAsmVariant >= 0)
383 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
384 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
385 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
386 if (!IP) {
387 WithColor::error()
388 << "unable to create instruction printer for target triple '"
389 << TheTriple.normalize() << "' with assembly variant "
390 << AssemblerDialect << ".\n";
391 return 1;
392 }
393
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000394 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
395
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000396 const MCSchedModel &SM = STI->getSchedModel();
397
398 unsigned Width = SM.IssueWidth;
399 if (DispatchWidth)
400 Width = DispatchWidth;
401
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000402 // Create an instruction builder.
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000403 mca::InstrBuilder IB(*STI, *MCII);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000404
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000405 // Number each region in the sequence.
406 unsigned RegionIdx = 0;
407 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
408 // Skip empty code regions.
409 if (Region->empty())
410 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000411
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000412 // Don't print the header of this region if it is the default region, and
413 // it doesn't have an end location.
414 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
415 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
416 StringRef Desc = Region->getDescription();
417 if (!Desc.empty())
418 TOF->os() << " - " << Desc;
419 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000420 }
421
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000422 mca::SourceMgr S(Region->getInstructions(),
423 PrintInstructionTables ? 1 : Iterations);
424
425 if (PrintInstructionTables) {
426 mca::InstructionTables IT(STI->getSchedModel(), IB, S);
427
428 if (PrintInstructionInfoView) {
429 IT.addView(
430 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
431 }
432
433 IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
434 IT.run();
435 IT.printReport(TOF->os());
436 continue;
437 }
438
Matt Davis5d1cda12018-05-15 20:21:04 +0000439 // Ideally, I'd like to expose the pipeline building here,
440 // by registering all of the Stage instances.
441 // But for now, it's just this single puppy.
442 std::unique_ptr<mca::FetchStage> Fetch =
443 llvm::make_unique<mca::FetchStage>(IB, S);
444 mca::Backend B(*STI, *MRI, std::move(Fetch), Width, RegisterFileSize,
445 LoadQueueSize, StoreQueueSize, AssumeNoAlias);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000446 mca::BackendPrinter Printer(B);
447
448 Printer.addView(llvm::make_unique<mca::SummaryView>(S, Width));
449 if (PrintInstructionInfoView)
450 Printer.addView(
451 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
452
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000453 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000454 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000455
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000456 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000457 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
458
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000459 if (PrintRetireStats)
460 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000461
462 if (PrintRegisterFileStats)
463 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
464
465 if (PrintResourcePressureView)
466 Printer.addView(
467 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
468
469 if (PrintTimelineView) {
470 Printer.addView(llvm::make_unique<mca::TimelineView>(
471 *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles));
472 }
473
474 B.run();
475 Printer.printReport(TOF->os());
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000476 }
477
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000478 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000479 return 0;
480}