blob: 1e93a7fdcdb13038c9d0f7c71a72ce6607f2bdbc [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
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000056static cl::OptionCategory ToolOptions("Tool Options");
57static cl::OptionCategory ViewOptions("View Options");
Andrea Di Biagio534e1da2018-04-25 11:33:14 +000058
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000059static cl::opt<std::string> InputFilename(cl::Positional,
60 cl::desc("<input file>"),
61 cl::cat(ToolOptions), cl::init("-"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000062
63static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000064 cl::init("-"), cl::cat(ToolOptions),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000065 cl::value_desc("filename"));
66
67static cl::opt<std::string>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000068 ArchName("march",
69 cl::desc("Target arch to assemble for, "
70 "see -version for available targets"),
71 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000072
73static cl::opt<std::string>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000074 TripleName("mtriple",
75 cl::desc("Target triple to assemble for, "
76 "see -version for available targets"),
77 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000078
79static cl::opt<std::string>
80 MCPU("mcpu",
81 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000082 cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000083
Andrea Di Biagio06268642018-04-24 16:19:08 +000084static cl::opt<int>
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000085 OutputAsmVariant("output-asm-variant",
Andrea Di Biagio06268642018-04-24 16:19:08 +000086 cl::desc("Syntax variant to use for output printing"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000087 cl::cat(ToolOptions), cl::init(-1));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000088
89static cl::opt<unsigned> Iterations("iterations",
90 cl::desc("Number of iterations to run"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000091 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000092
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000093static cl::opt<unsigned>
94 DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
95 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000096
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000097static cl::opt<unsigned>
98 RegisterFileSize("register-file-size",
99 cl::desc("Maximum number of temporary registers which can "
100 "be used for register mappings"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000101 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000102
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000103static cl::opt<bool>
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000104 PrintRegisterFileStats("register-file-stats",
105 cl::desc("Print register file statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000106 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000107
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000108static cl::opt<bool> PrintDispatchStats("dispatch-stats",
109 cl::desc("Print dispatch statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000110 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000111
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000112static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
113 cl::desc("Print scheduler statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000114 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000115
116static cl::opt<bool>
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000117 PrintRetireStats("retire-stats",
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000118 cl::desc("Print retire control unit statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000119 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000120
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000121static cl::opt<bool> PrintResourcePressureView(
122 "resource-pressure",
123 cl::desc("Print the resource pressure view (enabled by default)"),
124 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000125
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000126static cl::opt<bool> PrintTimelineView("timeline",
127 cl::desc("Print the timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000128 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000129
130static cl::opt<unsigned> TimelineMaxIterations(
131 "timeline-max-iterations",
132 cl::desc("Maximum number of iterations to print in timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000133 cl::cat(ViewOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000134
135static cl::opt<unsigned> TimelineMaxCycles(
136 "timeline-max-cycles",
137 cl::desc(
138 "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000139 cl::cat(ViewOptions), cl::init(80));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000140
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000141static cl::opt<bool>
142 AssumeNoAlias("noalias",
143 cl::desc("If set, assume that loads and stores do not alias"),
144 cl::cat(ToolOptions), cl::init(true));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000145
146static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000147 LoadQueueSize("lqueue",
148 cl::desc("Size of the load queue (unbound by default)"),
149 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000150
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000151static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000152 StoreQueueSize("squeue",
153 cl::desc("Size of the store queue (unbound by default)"),
154 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000155
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000156static cl::opt<bool>
157 PrintInstructionTables("instruction-tables",
158 cl::desc("Print instruction tables"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000159 cl::cat(ToolOptions), cl::init(false));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000160
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000161static cl::opt<bool> PrintInstructionInfoView(
162 "instruction-info",
163 cl::desc("Print the instruction info view (enabled by default)"),
164 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000165
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000166static cl::opt<bool> EnableAllStats("all-stats",
167 cl::desc("Print all hardware statistics"),
168 cl::cat(ViewOptions), cl::init(false));
169
170static cl::opt<bool>
171 EnableAllViews("all-views",
172 cl::desc("Print all views including hardware statistics"),
173 cl::cat(ViewOptions), cl::init(false));
174
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000175namespace {
176
177const Target *getTarget(const char *ProgName) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000178 TripleName = Triple::normalize(TripleName);
179 if (TripleName.empty())
180 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
181 Triple TheTriple(TripleName);
182
183 // Get the target specific parser.
184 std::string Error;
185 const Target *TheTarget =
186 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
187 if (!TheTarget) {
188 errs() << ProgName << ": " << Error;
189 return nullptr;
190 }
191
192 // Return the found target.
193 return TheTarget;
194}
195
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000196// A comment consumer that parses strings.
197// The only valid tokens are strings.
198class MCACommentConsumer : public AsmCommentConsumer {
199public:
200 mca::CodeRegions &Regions;
201
202 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
203 void HandleComment(SMLoc Loc, StringRef CommentText) override {
204 // Skip empty comments.
205 StringRef Comment(CommentText);
206 if (Comment.empty())
207 return;
208
209 // Skip spaces and tabs
210 unsigned Position = Comment.find_first_not_of(" \t");
211 if (Position >= Comment.size())
212 // we reached the end of the comment. Bail out.
213 return;
214
215 Comment = Comment.drop_front(Position);
216 if (Comment.consume_front("LLVM-MCA-END")) {
217 Regions.endRegion(Loc);
218 return;
219 }
220
221 // Now try to parse string LLVM-MCA-BEGIN
222 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
223 return;
224
225 // Skip spaces and tabs
226 Position = Comment.find_first_not_of(" \t");
227 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000228 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000229 // Use the rest of the string as a descriptor for this code snippet.
230 Regions.beginRegion(Comment, Loc);
231 }
232};
233
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000234int AssembleInput(const char *ProgName, MCAsmParser &Parser,
235 const Target *TheTarget, MCSubtargetInfo &STI,
236 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000237 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000238 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000239
240 if (!TAP) {
Andrea Di Biagio24fb4fc2018-05-04 13:52:12 +0000241 WithColor::error() << "this target does not support assembly parsing.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000242 return 1;
243 }
244
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000245 Parser.setTargetParser(*TAP);
246 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000247}
248
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000249ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000250 if (OutputFilename == "")
251 OutputFilename = "-";
252 std::error_code EC;
253 auto Out =
254 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
255 if (!EC)
256 return std::move(Out);
257 return EC;
258}
259
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000260class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000261 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000262
263public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000264 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
265 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000266
267 // We only want to intercept the emission of new instructions.
268 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
269 bool /* unused */) override {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000270 Regions.addInstruction(llvm::make_unique<const MCInst>(Inst));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000271 }
272
273 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
274 return true;
275 }
276
277 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
278 unsigned ByteAlignment) override {}
279 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
280 uint64_t Size = 0, unsigned ByteAlignment = 0) override {}
281 void EmitGPRel32Value(const MCExpr *Value) override {}
282 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
283 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
284 void EmitCOFFSymbolType(int Type) override {}
285 void EndCOFFSymbolDef() override {}
286
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000287 const std::vector<std::unique_ptr<const MCInst>> &
288 GetInstructionSequence(unsigned Index) const {
289 return Regions.getInstructionSequence(Index);
290 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000291};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000292} // end of anonymous namespace
293
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000294static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
295 if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
296 O = Default.getValue();
297}
298
299static void processViewOptions() {
300 if (!EnableAllViews.getNumOccurrences() &&
301 !EnableAllStats.getNumOccurrences())
302 return;
303
304 if (EnableAllViews.getNumOccurrences()) {
305 processOptionImpl(PrintResourcePressureView, EnableAllViews);
306 processOptionImpl(PrintTimelineView, EnableAllViews);
307 processOptionImpl(PrintInstructionInfoView, EnableAllViews);
308 }
309
310 const cl::opt<bool> &Default =
311 EnableAllViews.getPosition() < EnableAllStats.getPosition()
312 ? EnableAllStats
313 : EnableAllViews;
314 processOptionImpl(PrintRegisterFileStats, Default);
315 processOptionImpl(PrintDispatchStats, Default);
316 processOptionImpl(PrintSchedulerStats, Default);
317 processOptionImpl(PrintRetireStats, Default);
318}
319
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000320int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000321 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000322
323 // Initialize targets and assembly parsers.
324 llvm::InitializeAllTargetInfos();
325 llvm::InitializeAllTargetMCs();
326 llvm::InitializeAllAsmParsers();
327
328 // Enable printing of available targets when flag --version is specified.
329 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
330
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000331 cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
332
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000333 // Parse flags and initialize target options.
334 cl::ParseCommandLineOptions(argc, argv,
335 "llvm machine code performance analyzer.\n");
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000336
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000337 MCTargetOptions MCOptions;
338 MCOptions.PreserveAsmComments = false;
339
340 // Get the target from the triple. If a triple is not specified, then select
341 // the default triple for the host. If the triple doesn't correspond to any
342 // registered target, then exit with an error message.
343 const char *ProgName = argv[0];
344 const Target *TheTarget = getTarget(ProgName);
345 if (!TheTarget)
346 return 1;
347
348 // GetTarget() may replaced TripleName with a default triple.
349 // For safety, reconstruct the Triple object.
350 Triple TheTriple(TripleName);
351
352 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
353 MemoryBuffer::getFileOrSTDIN(InputFilename);
354 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000355 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000356 return 1;
357 }
358
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000359 // Apply overrides to llvm-mca specific options.
360 processViewOptions();
361
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000362 SourceMgr SrcMgr;
363
364 // Tell SrcMgr about this buffer, which is what the parser will pick up.
365 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
366
367 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
368 assert(MRI && "Unable to create target register info!");
369
370 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
371 assert(MAI && "Unable to create target asm info!");
372
373 MCObjectFileInfo MOFI;
374 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
375 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
376
377 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000378
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000379 mca::CodeRegions Regions(SrcMgr);
380 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000381
382 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000383
384 if (!MCPU.compare("native"))
385 MCPU = llvm::sys::getHostCPUName();
386
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000387 std::unique_ptr<MCSubtargetInfo> STI(
388 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
389 if (!STI->isCPUStringValid(MCPU))
390 return 1;
391
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000392 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000393 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
394 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000395 return 1;
396 }
397
398 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000399 WithColor::error()
400 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000401 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
402 << "'.\n";
403
404 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000405 WithColor::note()
406 << "cpu '" << MCPU << "' provides itineraries. However, "
407 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000408 return 1;
409 }
410
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000411 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000412 MCAsmLexer &Lexer = P->getLexer();
413 MCACommentConsumer CC(Regions);
414 Lexer.setCommentConsumer(&CC);
415
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000416 if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions))
417 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000418
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000419 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000420 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000421 return 1;
422 }
423
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000424 // Now initialize the output file.
425 auto OF = getOutputStream();
426 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000427 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000428 return 1;
429 }
430
Andrea Di Biagio06268642018-04-24 16:19:08 +0000431 unsigned AssemblerDialect = P->getAssemblerDialect();
432 if (OutputAsmVariant >= 0)
433 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
434 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
435 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
436 if (!IP) {
437 WithColor::error()
438 << "unable to create instruction printer for target triple '"
439 << TheTriple.normalize() << "' with assembly variant "
440 << AssemblerDialect << ".\n";
441 return 1;
442 }
443
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000444 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
445
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000446 const MCSchedModel &SM = STI->getSchedModel();
447
448 unsigned Width = SM.IssueWidth;
449 if (DispatchWidth)
450 Width = DispatchWidth;
451
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000452 // Create an instruction builder.
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000453 mca::InstrBuilder IB(*STI, *MCII);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000454
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000455 // Number each region in the sequence.
456 unsigned RegionIdx = 0;
457 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
458 // Skip empty code regions.
459 if (Region->empty())
460 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000461
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000462 // Don't print the header of this region if it is the default region, and
463 // it doesn't have an end location.
464 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
465 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
466 StringRef Desc = Region->getDescription();
467 if (!Desc.empty())
468 TOF->os() << " - " << Desc;
469 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000470 }
471
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000472 mca::SourceMgr S(Region->getInstructions(),
473 PrintInstructionTables ? 1 : Iterations);
474
475 if (PrintInstructionTables) {
476 mca::InstructionTables IT(STI->getSchedModel(), IB, S);
477
478 if (PrintInstructionInfoView) {
479 IT.addView(
480 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
481 }
482
483 IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
484 IT.run();
485 IT.printReport(TOF->os());
486 continue;
487 }
488
Matt Davis5d1cda12018-05-15 20:21:04 +0000489 // Ideally, I'd like to expose the pipeline building here,
490 // by registering all of the Stage instances.
491 // But for now, it's just this single puppy.
492 std::unique_ptr<mca::FetchStage> Fetch =
493 llvm::make_unique<mca::FetchStage>(IB, S);
494 mca::Backend B(*STI, *MRI, std::move(Fetch), Width, RegisterFileSize,
495 LoadQueueSize, StoreQueueSize, AssumeNoAlias);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000496 mca::BackendPrinter Printer(B);
497
498 Printer.addView(llvm::make_unique<mca::SummaryView>(S, Width));
499 if (PrintInstructionInfoView)
500 Printer.addView(
501 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
502
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000503 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000504 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000505
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000506 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000507 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
508
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000509 if (PrintRetireStats)
510 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000511
512 if (PrintRegisterFileStats)
513 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
514
515 if (PrintResourcePressureView)
516 Printer.addView(
517 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
518
519 if (PrintTimelineView) {
520 Printer.addView(llvm::make_unique<mca::TimelineView>(
521 *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles));
522 }
523
524 B.run();
525 Printer.printReport(TOF->os());
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000526 }
527
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000528 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000529 return 0;
530}