blob: fb019f1258091f2aacc54500d393843ed97180ab [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"
Matt Davis43de6db2018-06-22 16:17:26 +000026#include "DispatchStage.h"
Andrea Di Biagio821f6502018-04-10 14:55:14 +000027#include "DispatchStatistics.h"
Matt Davis43de6db2018-06-22 16:17:26 +000028#include "ExecuteStage.h"
Matt Davis5d1cda12018-05-15 20:21:04 +000029#include "FetchStage.h"
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000030#include "InstructionInfoView.h"
Andrea Di Biagiod1569292018-03-26 12:04:53 +000031#include "InstructionTables.h"
Matt Davis43de6db2018-06-22 16:17:26 +000032#include "RegisterFile.h"
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +000033#include "RegisterFileStatistics.h"
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000034#include "ResourcePressureView.h"
Matt Davis43de6db2018-06-22 16:17:26 +000035#include "RetireControlUnit.h"
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +000036#include "RetireControlUnitStatistics.h"
Matt Davis43de6db2018-06-22 16:17:26 +000037#include "RetireStage.h"
38#include "Scheduler.h"
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +000039#include "SchedulerStatistics.h"
Andrea Di Biagio0cc66c72018-03-09 13:52:03 +000040#include "SummaryView.h"
Andrea Di Biagio53e6ade2018-03-09 12:50:42 +000041#include "TimelineView.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000042#include "llvm/MC/MCAsmInfo.h"
43#include "llvm/MC/MCContext.h"
44#include "llvm/MC/MCObjectFileInfo.h"
45#include "llvm/MC/MCParser/MCTargetAsmParser.h"
46#include "llvm/MC/MCRegisterInfo.h"
47#include "llvm/MC/MCStreamer.h"
48#include "llvm/Support/CommandLine.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000049#include "llvm/Support/ErrorOr.h"
50#include "llvm/Support/FileSystem.h"
Andrea Di Biagio641cca32018-04-25 10:27:30 +000051#include "llvm/Support/Host.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000052#include "llvm/Support/InitLLVM.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000053#include "llvm/Support/MemoryBuffer.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000054#include "llvm/Support/SourceMgr.h"
55#include "llvm/Support/TargetRegistry.h"
56#include "llvm/Support/TargetSelect.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000057#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghere6adef092018-04-18 15:26:51 +000058#include "llvm/Support/WithColor.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000059
60using namespace llvm;
61
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000062static cl::OptionCategory ToolOptions("Tool Options");
63static cl::OptionCategory ViewOptions("View Options");
Andrea Di Biagio534e1da2018-04-25 11:33:14 +000064
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000065static cl::opt<std::string> InputFilename(cl::Positional,
66 cl::desc("<input file>"),
67 cl::cat(ToolOptions), cl::init("-"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000068
69static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000070 cl::init("-"), cl::cat(ToolOptions),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000071 cl::value_desc("filename"));
72
73static cl::opt<std::string>
Matt Davis43de6db2018-06-22 16:17:26 +000074 ArchName("march", cl::desc("Target arch to assemble for, "
75 "see -version for available targets"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000076 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000077
78static cl::opt<std::string>
Matt Davis43de6db2018-06-22 16:17:26 +000079 TripleName("mtriple", cl::desc("Target triple to assemble for, "
80 "see -version for available targets"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000081 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000082
83static cl::opt<std::string>
84 MCPU("mcpu",
85 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000086 cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000087
Andrea Di Biagio06268642018-04-24 16:19:08 +000088static cl::opt<int>
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000089 OutputAsmVariant("output-asm-variant",
Andrea Di Biagio06268642018-04-24 16:19:08 +000090 cl::desc("Syntax variant to use for output printing"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000091 cl::cat(ToolOptions), cl::init(-1));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000092
93static cl::opt<unsigned> Iterations("iterations",
94 cl::desc("Number of iterations to run"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000095 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000096
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000097static cl::opt<unsigned>
98 DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
99 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000100
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000101static cl::opt<unsigned>
102 RegisterFileSize("register-file-size",
103 cl::desc("Maximum number of temporary registers which can "
104 "be used for register mappings"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000105 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000106
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000107static cl::opt<bool>
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000108 PrintRegisterFileStats("register-file-stats",
109 cl::desc("Print register file statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000110 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000111
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000112static cl::opt<bool> PrintDispatchStats("dispatch-stats",
113 cl::desc("Print dispatch statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000114 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000115
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000116static cl::opt<bool>
117 PrintSummaryView("summary-view", cl::Hidden,
118 cl::desc("Print summary view (enabled by default)"),
119 cl::cat(ViewOptions), cl::init(true));
120
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000121static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
122 cl::desc("Print scheduler statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000123 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000124
125static cl::opt<bool>
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000126 PrintRetireStats("retire-stats",
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000127 cl::desc("Print retire control unit statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000128 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000129
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000130static cl::opt<bool> PrintResourcePressureView(
131 "resource-pressure",
132 cl::desc("Print the resource pressure view (enabled by default)"),
133 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000134
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000135static cl::opt<bool> PrintTimelineView("timeline",
136 cl::desc("Print the timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000137 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000138
139static cl::opt<unsigned> TimelineMaxIterations(
140 "timeline-max-iterations",
141 cl::desc("Maximum number of iterations to print in timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000142 cl::cat(ViewOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000143
144static cl::opt<unsigned> TimelineMaxCycles(
145 "timeline-max-cycles",
146 cl::desc(
147 "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000148 cl::cat(ViewOptions), cl::init(80));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000149
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000150static cl::opt<bool>
151 AssumeNoAlias("noalias",
152 cl::desc("If set, assume that loads and stores do not alias"),
153 cl::cat(ToolOptions), cl::init(true));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000154
155static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000156 LoadQueueSize("lqueue",
157 cl::desc("Size of the load queue (unbound by default)"),
158 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000159
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000160static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000161 StoreQueueSize("squeue",
162 cl::desc("Size of the store queue (unbound by default)"),
163 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000164
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000165static cl::opt<bool>
166 PrintInstructionTables("instruction-tables",
167 cl::desc("Print instruction tables"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000168 cl::cat(ToolOptions), cl::init(false));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000169
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000170static cl::opt<bool> PrintInstructionInfoView(
171 "instruction-info",
172 cl::desc("Print the instruction info view (enabled by default)"),
173 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000174
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000175static cl::opt<bool> EnableAllStats("all-stats",
176 cl::desc("Print all hardware statistics"),
177 cl::cat(ViewOptions), cl::init(false));
178
179static cl::opt<bool>
180 EnableAllViews("all-views",
181 cl::desc("Print all views including hardware statistics"),
182 cl::cat(ViewOptions), cl::init(false));
183
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000184namespace {
185
186const Target *getTarget(const char *ProgName) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000187 TripleName = Triple::normalize(TripleName);
188 if (TripleName.empty())
189 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
190 Triple TheTriple(TripleName);
191
192 // Get the target specific parser.
193 std::string Error;
194 const Target *TheTarget =
195 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
196 if (!TheTarget) {
197 errs() << ProgName << ": " << Error;
198 return nullptr;
199 }
200
201 // Return the found target.
202 return TheTarget;
203}
204
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000205// A comment consumer that parses strings.
206// The only valid tokens are strings.
207class MCACommentConsumer : public AsmCommentConsumer {
208public:
209 mca::CodeRegions &Regions;
210
211 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
212 void HandleComment(SMLoc Loc, StringRef CommentText) override {
213 // Skip empty comments.
214 StringRef Comment(CommentText);
215 if (Comment.empty())
216 return;
217
218 // Skip spaces and tabs
219 unsigned Position = Comment.find_first_not_of(" \t");
220 if (Position >= Comment.size())
221 // we reached the end of the comment. Bail out.
222 return;
223
224 Comment = Comment.drop_front(Position);
225 if (Comment.consume_front("LLVM-MCA-END")) {
226 Regions.endRegion(Loc);
227 return;
228 }
229
230 // Now try to parse string LLVM-MCA-BEGIN
231 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
232 return;
233
234 // Skip spaces and tabs
235 Position = Comment.find_first_not_of(" \t");
236 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000237 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000238 // Use the rest of the string as a descriptor for this code snippet.
239 Regions.beginRegion(Comment, Loc);
240 }
241};
242
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000243int AssembleInput(const char *ProgName, MCAsmParser &Parser,
244 const Target *TheTarget, MCSubtargetInfo &STI,
245 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000246 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000247 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000248
249 if (!TAP) {
Andrea Di Biagio24fb4fc2018-05-04 13:52:12 +0000250 WithColor::error() << "this target does not support assembly parsing.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000251 return 1;
252 }
253
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000254 Parser.setTargetParser(*TAP);
255 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000256}
257
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000258ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000259 if (OutputFilename == "")
260 OutputFilename = "-";
261 std::error_code EC;
262 auto Out =
263 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
264 if (!EC)
265 return std::move(Out);
266 return EC;
267}
268
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000269class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000270 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000271
272public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000273 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
274 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000275
276 // We only want to intercept the emission of new instructions.
277 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
278 bool /* unused */) override {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000279 Regions.addInstruction(llvm::make_unique<const MCInst>(Inst));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000280 }
281
282 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
283 return true;
284 }
285
286 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
287 unsigned ByteAlignment) override {}
288 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
289 uint64_t Size = 0, unsigned ByteAlignment = 0) override {}
290 void EmitGPRel32Value(const MCExpr *Value) override {}
291 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
292 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
293 void EmitCOFFSymbolType(int Type) override {}
294 void EndCOFFSymbolDef() override {}
295
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000296 const std::vector<std::unique_ptr<const MCInst>> &
297 GetInstructionSequence(unsigned Index) const {
298 return Regions.getInstructionSequence(Index);
299 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000300};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000301} // end of anonymous namespace
302
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000303static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
304 if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
305 O = Default.getValue();
306}
307
308static void processViewOptions() {
309 if (!EnableAllViews.getNumOccurrences() &&
310 !EnableAllStats.getNumOccurrences())
311 return;
312
313 if (EnableAllViews.getNumOccurrences()) {
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000314 processOptionImpl(PrintSummaryView, EnableAllViews);
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000315 processOptionImpl(PrintResourcePressureView, EnableAllViews);
316 processOptionImpl(PrintTimelineView, EnableAllViews);
317 processOptionImpl(PrintInstructionInfoView, EnableAllViews);
318 }
319
320 const cl::opt<bool> &Default =
321 EnableAllViews.getPosition() < EnableAllStats.getPosition()
322 ? EnableAllStats
323 : EnableAllViews;
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000324 processOptionImpl(PrintSummaryView, Default);
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000325 processOptionImpl(PrintRegisterFileStats, Default);
326 processOptionImpl(PrintDispatchStats, Default);
327 processOptionImpl(PrintSchedulerStats, Default);
328 processOptionImpl(PrintRetireStats, Default);
329}
330
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000331int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000332 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000333
334 // Initialize targets and assembly parsers.
335 llvm::InitializeAllTargetInfos();
336 llvm::InitializeAllTargetMCs();
337 llvm::InitializeAllAsmParsers();
338
339 // Enable printing of available targets when flag --version is specified.
340 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
341
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000342 cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
343
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000344 // Parse flags and initialize target options.
345 cl::ParseCommandLineOptions(argc, argv,
346 "llvm machine code performance analyzer.\n");
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000347
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000348 MCTargetOptions MCOptions;
349 MCOptions.PreserveAsmComments = false;
350
351 // Get the target from the triple. If a triple is not specified, then select
352 // the default triple for the host. If the triple doesn't correspond to any
353 // registered target, then exit with an error message.
354 const char *ProgName = argv[0];
355 const Target *TheTarget = getTarget(ProgName);
356 if (!TheTarget)
357 return 1;
358
359 // GetTarget() may replaced TripleName with a default triple.
360 // For safety, reconstruct the Triple object.
361 Triple TheTriple(TripleName);
362
363 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
364 MemoryBuffer::getFileOrSTDIN(InputFilename);
365 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000366 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000367 return 1;
368 }
369
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000370 // Apply overrides to llvm-mca specific options.
371 processViewOptions();
372
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000373 SourceMgr SrcMgr;
374
375 // Tell SrcMgr about this buffer, which is what the parser will pick up.
376 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
377
378 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
379 assert(MRI && "Unable to create target register info!");
380
381 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
382 assert(MAI && "Unable to create target asm info!");
383
384 MCObjectFileInfo MOFI;
385 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
386 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
387
388 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000389
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000390 mca::CodeRegions Regions(SrcMgr);
391 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000392
393 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000394
Andrea Di Biagio2145b132018-06-20 10:08:11 +0000395 std::unique_ptr<MCInstrAnalysis> MCIA(
396 TheTarget->createMCInstrAnalysis(MCII.get()));
397
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000398 if (!MCPU.compare("native"))
399 MCPU = llvm::sys::getHostCPUName();
400
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000401 std::unique_ptr<MCSubtargetInfo> STI(
402 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
403 if (!STI->isCPUStringValid(MCPU))
404 return 1;
405
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000406 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000407 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
408 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000409 return 1;
410 }
411
412 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000413 WithColor::error()
414 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000415 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
416 << "'.\n";
417
418 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000419 WithColor::note()
420 << "cpu '" << MCPU << "' provides itineraries. However, "
421 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000422 return 1;
423 }
424
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000425 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000426 MCAsmLexer &Lexer = P->getLexer();
427 MCACommentConsumer CC(Regions);
428 Lexer.setCommentConsumer(&CC);
429
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000430 if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions))
431 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000432
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000433 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000434 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000435 return 1;
436 }
437
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000438 // Now initialize the output file.
439 auto OF = getOutputStream();
440 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000441 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000442 return 1;
443 }
444
Andrea Di Biagio06268642018-04-24 16:19:08 +0000445 unsigned AssemblerDialect = P->getAssemblerDialect();
446 if (OutputAsmVariant >= 0)
447 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
448 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
449 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
450 if (!IP) {
451 WithColor::error()
452 << "unable to create instruction printer for target triple '"
453 << TheTriple.normalize() << "' with assembly variant "
454 << AssemblerDialect << ".\n";
455 return 1;
456 }
457
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000458 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
459
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000460 const MCSchedModel &SM = STI->getSchedModel();
461
462 unsigned Width = SM.IssueWidth;
463 if (DispatchWidth)
464 Width = DispatchWidth;
465
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000466 // Create an instruction builder.
Andrea Di Biagio2145b132018-06-20 10:08:11 +0000467 mca::InstrBuilder IB(*STI, *MCII, *MRI, *MCIA);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000468
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000469 // Number each region in the sequence.
470 unsigned RegionIdx = 0;
471 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
472 // Skip empty code regions.
473 if (Region->empty())
474 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000475
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000476 // Don't print the header of this region if it is the default region, and
477 // it doesn't have an end location.
478 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
479 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
480 StringRef Desc = Region->getDescription();
481 if (!Desc.empty())
482 TOF->os() << " - " << Desc;
483 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000484 }
485
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000486 mca::SourceMgr S(Region->getInstructions(),
487 PrintInstructionTables ? 1 : Iterations);
488
489 if (PrintInstructionTables) {
Matt Davis43de6db2018-06-22 16:17:26 +0000490 mca::InstructionTables IT(SM, IB, S);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000491
492 if (PrintInstructionInfoView) {
493 IT.addView(
494 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
495 }
496
497 IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
498 IT.run();
499 IT.printReport(TOF->os());
500 continue;
501 }
502
Matt Davis43de6db2018-06-22 16:17:26 +0000503 // Create the hardware components required for the pipeline.
504 mca::RetireControlUnit RCU(SM);
505 mca::RegisterFile PRF(SM, *MRI, RegisterFileSize);
506 mca::Scheduler HWS(SM, LoadQueueSize, StoreQueueSize, AssumeNoAlias);
507
508 // Create the pipeline and add stages to it.
509 auto B = llvm::make_unique<mca::Backend>(
510 Width, RegisterFileSize, LoadQueueSize, StoreQueueSize, AssumeNoAlias);
511 B->appendStage(llvm::make_unique<mca::FetchStage>(IB, S));
512 B->appendStage(llvm::make_unique<mca::DispatchStage>(
513 B.get(), *STI, *MRI, RegisterFileSize, Width, RCU, PRF, HWS));
514 B->appendStage(llvm::make_unique<mca::RetireStage>(B.get(), RCU, PRF));
515 B->appendStage(llvm::make_unique<mca::ExecuteStage>(B.get(), RCU, HWS));
516 mca::BackendPrinter Printer(*B);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000517
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000518 if (PrintSummaryView)
519 Printer.addView(llvm::make_unique<mca::SummaryView>(SM, S, Width));
520
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000521 if (PrintInstructionInfoView)
522 Printer.addView(
523 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
524
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000525 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000526 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000527
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000528 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000529 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
530
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000531 if (PrintRetireStats)
532 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000533
534 if (PrintRegisterFileStats)
535 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
536
537 if (PrintResourcePressureView)
538 Printer.addView(
539 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
540
541 if (PrintTimelineView) {
542 Printer.addView(llvm::make_unique<mca::TimelineView>(
543 *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles));
544 }
545
Matt Davis43de6db2018-06-22 16:17:26 +0000546 B->run();
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000547 Printer.printReport(TOF->os());
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000548 }
549
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000550 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000551 return 0;
552}