blob: 4d10704c10c9185bc1e4825c806f44174f67f6c1 [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
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000112static cl::opt<bool>
113 PrintSummaryView("summary-view", cl::Hidden,
114 cl::desc("Print summary view (enabled by default)"),
115 cl::cat(ViewOptions), cl::init(true));
116
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000117static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
118 cl::desc("Print scheduler statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000119 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000120
121static cl::opt<bool>
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000122 PrintRetireStats("retire-stats",
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000123 cl::desc("Print retire control unit statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000124 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000125
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000126static cl::opt<bool> PrintResourcePressureView(
127 "resource-pressure",
128 cl::desc("Print the resource pressure view (enabled by default)"),
129 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000130
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000131static cl::opt<bool> PrintTimelineView("timeline",
132 cl::desc("Print the timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000133 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000134
135static cl::opt<unsigned> TimelineMaxIterations(
136 "timeline-max-iterations",
137 cl::desc("Maximum number of iterations to print in timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000138 cl::cat(ViewOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000139
140static cl::opt<unsigned> TimelineMaxCycles(
141 "timeline-max-cycles",
142 cl::desc(
143 "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000144 cl::cat(ViewOptions), cl::init(80));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000145
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000146static cl::opt<bool>
147 AssumeNoAlias("noalias",
148 cl::desc("If set, assume that loads and stores do not alias"),
149 cl::cat(ToolOptions), cl::init(true));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000150
151static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000152 LoadQueueSize("lqueue",
153 cl::desc("Size of the load queue (unbound by default)"),
154 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000155
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000156static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000157 StoreQueueSize("squeue",
158 cl::desc("Size of the store queue (unbound by default)"),
159 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000160
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000161static cl::opt<bool>
162 PrintInstructionTables("instruction-tables",
163 cl::desc("Print instruction tables"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000164 cl::cat(ToolOptions), cl::init(false));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000165
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000166static cl::opt<bool> PrintInstructionInfoView(
167 "instruction-info",
168 cl::desc("Print the instruction info view (enabled by default)"),
169 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000170
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000171static cl::opt<bool> EnableAllStats("all-stats",
172 cl::desc("Print all hardware statistics"),
173 cl::cat(ViewOptions), cl::init(false));
174
175static cl::opt<bool>
176 EnableAllViews("all-views",
177 cl::desc("Print all views including hardware statistics"),
178 cl::cat(ViewOptions), cl::init(false));
179
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000180namespace {
181
182const Target *getTarget(const char *ProgName) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000183 TripleName = Triple::normalize(TripleName);
184 if (TripleName.empty())
185 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
186 Triple TheTriple(TripleName);
187
188 // Get the target specific parser.
189 std::string Error;
190 const Target *TheTarget =
191 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
192 if (!TheTarget) {
193 errs() << ProgName << ": " << Error;
194 return nullptr;
195 }
196
197 // Return the found target.
198 return TheTarget;
199}
200
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000201// A comment consumer that parses strings.
202// The only valid tokens are strings.
203class MCACommentConsumer : public AsmCommentConsumer {
204public:
205 mca::CodeRegions &Regions;
206
207 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
208 void HandleComment(SMLoc Loc, StringRef CommentText) override {
209 // Skip empty comments.
210 StringRef Comment(CommentText);
211 if (Comment.empty())
212 return;
213
214 // Skip spaces and tabs
215 unsigned Position = Comment.find_first_not_of(" \t");
216 if (Position >= Comment.size())
217 // we reached the end of the comment. Bail out.
218 return;
219
220 Comment = Comment.drop_front(Position);
221 if (Comment.consume_front("LLVM-MCA-END")) {
222 Regions.endRegion(Loc);
223 return;
224 }
225
226 // Now try to parse string LLVM-MCA-BEGIN
227 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
228 return;
229
230 // Skip spaces and tabs
231 Position = Comment.find_first_not_of(" \t");
232 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000233 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000234 // Use the rest of the string as a descriptor for this code snippet.
235 Regions.beginRegion(Comment, Loc);
236 }
237};
238
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000239int AssembleInput(const char *ProgName, MCAsmParser &Parser,
240 const Target *TheTarget, MCSubtargetInfo &STI,
241 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000242 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000243 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000244
245 if (!TAP) {
Andrea Di Biagio24fb4fc2018-05-04 13:52:12 +0000246 WithColor::error() << "this target does not support assembly parsing.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000247 return 1;
248 }
249
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000250 Parser.setTargetParser(*TAP);
251 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000252}
253
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000254ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000255 if (OutputFilename == "")
256 OutputFilename = "-";
257 std::error_code EC;
258 auto Out =
259 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
260 if (!EC)
261 return std::move(Out);
262 return EC;
263}
264
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000265class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000266 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000267
268public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000269 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
270 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000271
272 // We only want to intercept the emission of new instructions.
273 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
274 bool /* unused */) override {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000275 Regions.addInstruction(llvm::make_unique<const MCInst>(Inst));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000276 }
277
278 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
279 return true;
280 }
281
282 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
283 unsigned ByteAlignment) override {}
284 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
285 uint64_t Size = 0, unsigned ByteAlignment = 0) override {}
286 void EmitGPRel32Value(const MCExpr *Value) override {}
287 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
288 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
289 void EmitCOFFSymbolType(int Type) override {}
290 void EndCOFFSymbolDef() override {}
291
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000292 const std::vector<std::unique_ptr<const MCInst>> &
293 GetInstructionSequence(unsigned Index) const {
294 return Regions.getInstructionSequence(Index);
295 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000296};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000297} // end of anonymous namespace
298
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000299static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
300 if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
301 O = Default.getValue();
302}
303
304static void processViewOptions() {
305 if (!EnableAllViews.getNumOccurrences() &&
306 !EnableAllStats.getNumOccurrences())
307 return;
308
309 if (EnableAllViews.getNumOccurrences()) {
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000310 processOptionImpl(PrintSummaryView, EnableAllViews);
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000311 processOptionImpl(PrintResourcePressureView, EnableAllViews);
312 processOptionImpl(PrintTimelineView, EnableAllViews);
313 processOptionImpl(PrintInstructionInfoView, EnableAllViews);
314 }
315
316 const cl::opt<bool> &Default =
317 EnableAllViews.getPosition() < EnableAllStats.getPosition()
318 ? EnableAllStats
319 : EnableAllViews;
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000320 processOptionImpl(PrintSummaryView, Default);
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000321 processOptionImpl(PrintRegisterFileStats, Default);
322 processOptionImpl(PrintDispatchStats, Default);
323 processOptionImpl(PrintSchedulerStats, Default);
324 processOptionImpl(PrintRetireStats, Default);
325}
326
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000327int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000328 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000329
330 // Initialize targets and assembly parsers.
331 llvm::InitializeAllTargetInfos();
332 llvm::InitializeAllTargetMCs();
333 llvm::InitializeAllAsmParsers();
334
335 // Enable printing of available targets when flag --version is specified.
336 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
337
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000338 cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
339
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000340 // Parse flags and initialize target options.
341 cl::ParseCommandLineOptions(argc, argv,
342 "llvm machine code performance analyzer.\n");
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000343
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000344 MCTargetOptions MCOptions;
345 MCOptions.PreserveAsmComments = false;
346
347 // Get the target from the triple. If a triple is not specified, then select
348 // the default triple for the host. If the triple doesn't correspond to any
349 // registered target, then exit with an error message.
350 const char *ProgName = argv[0];
351 const Target *TheTarget = getTarget(ProgName);
352 if (!TheTarget)
353 return 1;
354
355 // GetTarget() may replaced TripleName with a default triple.
356 // For safety, reconstruct the Triple object.
357 Triple TheTriple(TripleName);
358
359 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
360 MemoryBuffer::getFileOrSTDIN(InputFilename);
361 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000362 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000363 return 1;
364 }
365
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000366 // Apply overrides to llvm-mca specific options.
367 processViewOptions();
368
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000369 SourceMgr SrcMgr;
370
371 // Tell SrcMgr about this buffer, which is what the parser will pick up.
372 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
373
374 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
375 assert(MRI && "Unable to create target register info!");
376
377 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
378 assert(MAI && "Unable to create target asm info!");
379
380 MCObjectFileInfo MOFI;
381 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
382 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
383
384 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000385
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000386 mca::CodeRegions Regions(SrcMgr);
387 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000388
389 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000390
391 if (!MCPU.compare("native"))
392 MCPU = llvm::sys::getHostCPUName();
393
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000394 std::unique_ptr<MCSubtargetInfo> STI(
395 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
396 if (!STI->isCPUStringValid(MCPU))
397 return 1;
398
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000399 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000400 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
401 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000402 return 1;
403 }
404
405 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000406 WithColor::error()
407 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000408 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
409 << "'.\n";
410
411 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000412 WithColor::note()
413 << "cpu '" << MCPU << "' provides itineraries. However, "
414 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000415 return 1;
416 }
417
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000418 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000419 MCAsmLexer &Lexer = P->getLexer();
420 MCACommentConsumer CC(Regions);
421 Lexer.setCommentConsumer(&CC);
422
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000423 if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions))
424 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000425
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000426 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000427 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000428 return 1;
429 }
430
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000431 // Now initialize the output file.
432 auto OF = getOutputStream();
433 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000434 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000435 return 1;
436 }
437
Andrea Di Biagio06268642018-04-24 16:19:08 +0000438 unsigned AssemblerDialect = P->getAssemblerDialect();
439 if (OutputAsmVariant >= 0)
440 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
441 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
442 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
443 if (!IP) {
444 WithColor::error()
445 << "unable to create instruction printer for target triple '"
446 << TheTriple.normalize() << "' with assembly variant "
447 << AssemblerDialect << ".\n";
448 return 1;
449 }
450
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000451 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
452
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000453 const MCSchedModel &SM = STI->getSchedModel();
454
455 unsigned Width = SM.IssueWidth;
456 if (DispatchWidth)
457 Width = DispatchWidth;
458
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000459 // Create an instruction builder.
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000460 mca::InstrBuilder IB(*STI, *MCII);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000461
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000462 // Number each region in the sequence.
463 unsigned RegionIdx = 0;
464 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
465 // Skip empty code regions.
466 if (Region->empty())
467 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000468
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000469 // Don't print the header of this region if it is the default region, and
470 // it doesn't have an end location.
471 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
472 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
473 StringRef Desc = Region->getDescription();
474 if (!Desc.empty())
475 TOF->os() << " - " << Desc;
476 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000477 }
478
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000479 mca::SourceMgr S(Region->getInstructions(),
480 PrintInstructionTables ? 1 : Iterations);
481
482 if (PrintInstructionTables) {
483 mca::InstructionTables IT(STI->getSchedModel(), IB, S);
484
485 if (PrintInstructionInfoView) {
486 IT.addView(
487 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
488 }
489
490 IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
491 IT.run();
492 IT.printReport(TOF->os());
493 continue;
494 }
495
Matt Davis5d1cda12018-05-15 20:21:04 +0000496 // Ideally, I'd like to expose the pipeline building here,
497 // by registering all of the Stage instances.
498 // But for now, it's just this single puppy.
499 std::unique_ptr<mca::FetchStage> Fetch =
500 llvm::make_unique<mca::FetchStage>(IB, S);
501 mca::Backend B(*STI, *MRI, std::move(Fetch), Width, RegisterFileSize,
502 LoadQueueSize, StoreQueueSize, AssumeNoAlias);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000503 mca::BackendPrinter Printer(B);
504
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000505 if (PrintSummaryView)
506 Printer.addView(llvm::make_unique<mca::SummaryView>(SM, S, Width));
507
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000508 if (PrintInstructionInfoView)
509 Printer.addView(
510 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
511
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000512 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000513 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000514
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000515 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000516 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
517
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000518 if (PrintRetireStats)
519 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000520
521 if (PrintRegisterFileStats)
522 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
523
524 if (PrintResourcePressureView)
525 Printer.addView(
526 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
527
528 if (PrintTimelineView) {
529 Printer.addView(llvm::make_unique<mca::TimelineView>(
530 *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles));
531 }
532
533 B.run();
534 Printer.printReport(TOF->os());
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000535 }
536
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000537 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000538 return 0;
539}