blob: 14be43568637391456e7f8a83ec1eec13632a3e4 [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"
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000027#include "InstructionInfoView.h"
Andrea Di Biagiod1569292018-03-26 12:04:53 +000028#include "InstructionTables.h"
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +000029#include "RegisterFileStatistics.h"
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000030#include "ResourcePressureView.h"
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +000031#include "RetireControlUnitStatistics.h"
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +000032#include "SchedulerStatistics.h"
Andrea Di Biagio0cc66c72018-03-09 13:52:03 +000033#include "SummaryView.h"
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000034#include "TimelineView.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000035#include "llvm/MC/MCAsmInfo.h"
36#include "llvm/MC/MCContext.h"
37#include "llvm/MC/MCObjectFileInfo.h"
38#include "llvm/MC/MCParser/MCTargetAsmParser.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/MC/MCStreamer.h"
41#include "llvm/Support/CommandLine.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000042#include "llvm/Support/ErrorOr.h"
43#include "llvm/Support/FileSystem.h"
Andrea Di Biagio641cca32018-04-25 10:27:30 +000044#include "llvm/Support/Host.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000045#include "llvm/Support/InitLLVM.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000046#include "llvm/Support/MemoryBuffer.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000047#include "llvm/Support/SourceMgr.h"
48#include "llvm/Support/TargetRegistry.h"
49#include "llvm/Support/TargetSelect.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000050#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghere6adef092018-04-18 15:26:51 +000051#include "llvm/Support/WithColor.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000052
53using namespace llvm;
54
Filipe Cabecinhasdef742c2018-04-25 14:39:16 +000055static llvm::cl::OptionCategory ViewOptions("View Options");
Andrea Di Biagio534e1da2018-04-25 11:33:14 +000056
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000057static cl::opt<std::string>
58 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
59
60static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
61 cl::init("-"),
62 cl::value_desc("filename"));
63
64static cl::opt<std::string>
65 ArchName("march", cl::desc("Target arch to assemble for, "
66 "see -version for available targets"));
67
68static cl::opt<std::string>
69 TripleName("mtriple", cl::desc("Target triple to assemble for, "
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000070 "see -version for available targets"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000071
72static cl::opt<std::string>
73 MCPU("mcpu",
74 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Andrea Di Biagio93c49d52018-04-25 10:18:25 +000075 cl::value_desc("cpu-name"), cl::init("native"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000076
Andrea Di Biagio06268642018-04-24 16:19:08 +000077static cl::opt<int>
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000078 OutputAsmVariant("output-asm-variant",
Andrea Di Biagio06268642018-04-24 16:19:08 +000079 cl::desc("Syntax variant to use for output printing"),
80 cl::init(-1));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000081
82static cl::opt<unsigned> Iterations("iterations",
83 cl::desc("Number of iterations to run"),
84 cl::init(0));
85
86static cl::opt<unsigned> DispatchWidth(
87 "dispatch",
88 cl::desc("Dispatch Width. By default it is set equal to IssueWidth"),
89 cl::init(0));
90
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000091static cl::opt<unsigned>
92 RegisterFileSize("register-file-size",
93 cl::desc("Maximum number of temporary registers which can "
94 "be used for register mappings"),
95 cl::init(0));
96
Andrea Di Biagio29538c62018-03-23 11:33:09 +000097static cl::opt<bool>
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +000098 PrintRegisterFileStats("register-file-stats",
99 cl::desc("Print register file statistics"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000100 cl::cat(ViewOptions),
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000101 cl::init(false));
102
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000103static cl::opt<bool> PrintDispatchStats("dispatch-stats",
104 cl::desc("Print dispatch statistics"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000105 cl::cat(ViewOptions),
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000106 cl::init(false));
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000107
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000108static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
109 cl::desc("Print scheduler statistics"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000110 cl::cat(ViewOptions),
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000111 cl::init(false));
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000112
113static cl::opt<bool>
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000114 PrintRetireStats("retire-stats",
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000115 cl::desc("Print retire control unit statistics"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000116 cl::cat(ViewOptions),
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000117 cl::init(false));
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000118
119static cl::opt<bool>
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000120 PrintResourcePressureView("resource-pressure",
121 cl::desc("Print the resource pressure view"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000122 cl::cat(ViewOptions),
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000123 cl::init(true));
124
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000125static cl::opt<bool> PrintTimelineView("timeline",
126 cl::desc("Print the timeline view"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000127 cl::cat(ViewOptions),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000128 cl::init(false));
129
130static cl::opt<unsigned> TimelineMaxIterations(
131 "timeline-max-iterations",
132 cl::desc("Maximum number of iterations to print in timeline view"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000133 cl::cat(ViewOptions),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000134 cl::init(0));
135
136static cl::opt<unsigned> TimelineMaxCycles(
137 "timeline-max-cycles",
138 cl::desc(
139 "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000140 cl::cat(ViewOptions),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000141 cl::init(80));
142
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000143static cl::opt<bool> AssumeNoAlias(
144 "noalias",
145 cl::desc("If set, it assumes that loads and stores do not alias"),
146 cl::init(true));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000147
148static cl::opt<unsigned>
149 LoadQueueSize("lqueue", cl::desc("Size of the load queue"), cl::init(0));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000150
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000151static cl::opt<unsigned>
152 StoreQueueSize("squeue", cl::desc("Size of the store queue"), cl::init(0));
153
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000154static cl::opt<bool>
155 PrintInstructionTables("instruction-tables",
156 cl::desc("Print instruction tables"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000157 cl::cat(ViewOptions),
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000158 cl::init(false));
159
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000160static cl::opt<bool>
161 PrintInstructionInfoView("instruction-info",
162 cl::desc("Print the instruction info view"),
Andrea Di Biagio534e1da2018-04-25 11:33:14 +0000163 cl::cat(ViewOptions),
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000164 cl::init(true));
165
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000166namespace {
167
168const Target *getTarget(const char *ProgName) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000169 TripleName = Triple::normalize(TripleName);
170 if (TripleName.empty())
171 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
172 Triple TheTriple(TripleName);
173
174 // Get the target specific parser.
175 std::string Error;
176 const Target *TheTarget =
177 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
178 if (!TheTarget) {
179 errs() << ProgName << ": " << Error;
180 return nullptr;
181 }
182
183 // Return the found target.
184 return TheTarget;
185}
186
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000187// A comment consumer that parses strings.
188// The only valid tokens are strings.
189class MCACommentConsumer : public AsmCommentConsumer {
190public:
191 mca::CodeRegions &Regions;
192
193 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
194 void HandleComment(SMLoc Loc, StringRef CommentText) override {
195 // Skip empty comments.
196 StringRef Comment(CommentText);
197 if (Comment.empty())
198 return;
199
200 // Skip spaces and tabs
201 unsigned Position = Comment.find_first_not_of(" \t");
202 if (Position >= Comment.size())
203 // we reached the end of the comment. Bail out.
204 return;
205
206 Comment = Comment.drop_front(Position);
207 if (Comment.consume_front("LLVM-MCA-END")) {
208 Regions.endRegion(Loc);
209 return;
210 }
211
212 // Now try to parse string LLVM-MCA-BEGIN
213 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
214 return;
215
216 // Skip spaces and tabs
217 Position = Comment.find_first_not_of(" \t");
218 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000219 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000220 // Use the rest of the string as a descriptor for this code snippet.
221 Regions.beginRegion(Comment, Loc);
222 }
223};
224
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000225int AssembleInput(const char *ProgName, MCAsmParser &Parser,
226 const Target *TheTarget, MCSubtargetInfo &STI,
227 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000228 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000229 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000230
231 if (!TAP) {
232 errs() << ProgName
233 << ": error: this target does not support assembly parsing.\n";
234 return 1;
235 }
236
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000237 Parser.setTargetParser(*TAP);
238 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000239}
240
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000241ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000242 if (OutputFilename == "")
243 OutputFilename = "-";
244 std::error_code EC;
245 auto Out =
246 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
247 if (!EC)
248 return std::move(Out);
249 return EC;
250}
251
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000252class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000253 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000254
255public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000256 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
257 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000258
259 // We only want to intercept the emission of new instructions.
260 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
261 bool /* unused */) override {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000262 Regions.addInstruction(llvm::make_unique<const MCInst>(Inst));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000263 }
264
265 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
266 return true;
267 }
268
269 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
270 unsigned ByteAlignment) override {}
271 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
272 uint64_t Size = 0, unsigned ByteAlignment = 0) override {}
273 void EmitGPRel32Value(const MCExpr *Value) override {}
274 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
275 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
276 void EmitCOFFSymbolType(int Type) override {}
277 void EndCOFFSymbolDef() override {}
278
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000279 const std::vector<std::unique_ptr<const MCInst>> &
280 GetInstructionSequence(unsigned Index) const {
281 return Regions.getInstructionSequence(Index);
282 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000283};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000284} // end of anonymous namespace
285
286int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000287 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000288
289 // Initialize targets and assembly parsers.
290 llvm::InitializeAllTargetInfos();
291 llvm::InitializeAllTargetMCs();
292 llvm::InitializeAllAsmParsers();
293
294 // Enable printing of available targets when flag --version is specified.
295 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
296
297 // Parse flags and initialize target options.
298 cl::ParseCommandLineOptions(argc, argv,
299 "llvm machine code performance analyzer.\n");
300 MCTargetOptions MCOptions;
301 MCOptions.PreserveAsmComments = false;
302
303 // Get the target from the triple. If a triple is not specified, then select
304 // the default triple for the host. If the triple doesn't correspond to any
305 // registered target, then exit with an error message.
306 const char *ProgName = argv[0];
307 const Target *TheTarget = getTarget(ProgName);
308 if (!TheTarget)
309 return 1;
310
311 // GetTarget() may replaced TripleName with a default triple.
312 // For safety, reconstruct the Triple object.
313 Triple TheTriple(TripleName);
314
315 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
316 MemoryBuffer::getFileOrSTDIN(InputFilename);
317 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000318 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000319 return 1;
320 }
321
322 SourceMgr SrcMgr;
323
324 // Tell SrcMgr about this buffer, which is what the parser will pick up.
325 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
326
327 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
328 assert(MRI && "Unable to create target register info!");
329
330 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
331 assert(MAI && "Unable to create target asm info!");
332
333 MCObjectFileInfo MOFI;
334 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
335 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
336
337 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000338
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000339 mca::CodeRegions Regions(SrcMgr);
340 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000341
342 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000343
344 if (!MCPU.compare("native"))
345 MCPU = llvm::sys::getHostCPUName();
346
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000347 std::unique_ptr<MCSubtargetInfo> STI(
348 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
349 if (!STI->isCPUStringValid(MCPU))
350 return 1;
351
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000352 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000353 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
354 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000355 return 1;
356 }
357
358 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000359 WithColor::error()
360 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000361 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
362 << "'.\n";
363
364 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000365 WithColor::note()
366 << "cpu '" << MCPU << "' provides itineraries. However, "
367 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000368 return 1;
369 }
370
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000371 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000372 MCAsmLexer &Lexer = P->getLexer();
373 MCACommentConsumer CC(Regions);
374 Lexer.setCommentConsumer(&CC);
375
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000376 if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions))
377 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000378
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000379 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000380 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000381 return 1;
382 }
383
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000384 // Now initialize the output file.
385 auto OF = getOutputStream();
386 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000387 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000388 return 1;
389 }
390
Andrea Di Biagio06268642018-04-24 16:19:08 +0000391 unsigned AssemblerDialect = P->getAssemblerDialect();
392 if (OutputAsmVariant >= 0)
393 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
394 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
395 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
396 if (!IP) {
397 WithColor::error()
398 << "unable to create instruction printer for target triple '"
399 << TheTriple.normalize() << "' with assembly variant "
400 << AssemblerDialect << ".\n";
401 return 1;
402 }
403
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000404 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
405
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000406 const MCSchedModel &SM = STI->getSchedModel();
407
408 unsigned Width = SM.IssueWidth;
409 if (DispatchWidth)
410 Width = DispatchWidth;
411
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000412 // Create an instruction builder.
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000413 mca::InstrBuilder IB(*STI, *MCII);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000414
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000415 // Number each region in the sequence.
416 unsigned RegionIdx = 0;
417 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
418 // Skip empty code regions.
419 if (Region->empty())
420 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000421
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000422 // Don't print the header of this region if it is the default region, and
423 // it doesn't have an end location.
424 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
425 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
426 StringRef Desc = Region->getDescription();
427 if (!Desc.empty())
428 TOF->os() << " - " << Desc;
429 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000430 }
431
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000432 mca::SourceMgr S(Region->getInstructions(),
433 PrintInstructionTables ? 1 : Iterations);
434
435 if (PrintInstructionTables) {
436 mca::InstructionTables IT(STI->getSchedModel(), IB, S);
437
438 if (PrintInstructionInfoView) {
439 IT.addView(
440 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
441 }
442
443 IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
444 IT.run();
445 IT.printReport(TOF->os());
446 continue;
447 }
448
449 mca::Backend B(*STI, *MRI, IB, S, Width, RegisterFileSize, LoadQueueSize,
450 StoreQueueSize, AssumeNoAlias);
451 mca::BackendPrinter Printer(B);
452
453 Printer.addView(llvm::make_unique<mca::SummaryView>(S, Width));
454 if (PrintInstructionInfoView)
455 Printer.addView(
456 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
457
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000458 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000459 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000460
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000461 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000462 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
463
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000464 if (PrintRetireStats)
465 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000466
467 if (PrintRegisterFileStats)
468 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
469
470 if (PrintResourcePressureView)
471 Printer.addView(
472 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
473
474 if (PrintTimelineView) {
475 Printer.addView(llvm::make_unique<mca::TimelineView>(
476 *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles));
477 }
478
479 B.run();
480 Printer.printReport(TOF->os());
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000481 }
482
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000483 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000484 return 0;
485}