blob: b3a4c495d7ef4df3022b44194007155d3b5e6e32 [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 Biagioc6590122018-04-09 16:39:52 +000024#include "CodeRegion.h"
Matt Davisdea343d2018-06-25 16:53:00 +000025#include "PipelinePrinter.h"
Matt Davis271ce762018-08-27 17:16:32 +000026#include "Stages/FetchStage.h"
27#include "Stages/InstructionTables.h"
Matt Davis10aa09f2018-08-24 20:24:53 +000028#include "Views/DispatchStatistics.h"
29#include "Views/InstructionInfoView.h"
30#include "Views/RegisterFileStatistics.h"
31#include "Views/ResourcePressureView.h"
32#include "Views/RetireControlUnitStatistics.h"
33#include "Views/SchedulerStatistics.h"
34#include "Views/SummaryView.h"
35#include "Views/TimelineView.h"
Matt Davis271ce762018-08-27 17:16:32 +000036#include "include/Context.h"
37#include "include/Pipeline.h"
Andrea Di Biagio083addf2018-10-24 10:56:47 +000038#include "include/Support.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000039#include "llvm/MC/MCAsmInfo.h"
40#include "llvm/MC/MCContext.h"
41#include "llvm/MC/MCObjectFileInfo.h"
42#include "llvm/MC/MCParser/MCTargetAsmParser.h"
43#include "llvm/MC/MCRegisterInfo.h"
44#include "llvm/MC/MCStreamer.h"
45#include "llvm/Support/CommandLine.h"
Matt Davis4bcf3692018-08-13 18:11:48 +000046#include "llvm/Support/ErrorHandling.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000047#include "llvm/Support/ErrorOr.h"
48#include "llvm/Support/FileSystem.h"
Andrea Di Biagio641cca32018-04-25 10:27:30 +000049#include "llvm/Support/Host.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000050#include "llvm/Support/InitLLVM.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000051#include "llvm/Support/MemoryBuffer.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000052#include "llvm/Support/SourceMgr.h"
53#include "llvm/Support/TargetRegistry.h"
54#include "llvm/Support/TargetSelect.h"
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000055#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghere6adef092018-04-18 15:26:51 +000056#include "llvm/Support/WithColor.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000057
58using namespace llvm;
59
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000060static cl::OptionCategory ToolOptions("Tool Options");
61static cl::OptionCategory ViewOptions("View Options");
Andrea Di Biagio534e1da2018-04-25 11:33:14 +000062
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000063static cl::opt<std::string> InputFilename(cl::Positional,
64 cl::desc("<input file>"),
65 cl::cat(ToolOptions), cl::init("-"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000066
67static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000068 cl::init("-"), cl::cat(ToolOptions),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000069 cl::value_desc("filename"));
70
71static cl::opt<std::string>
Matt Davis9e64a4c2018-10-31 17:47:25 +000072 ArchName("march", cl::desc("Target architecture. "
73 "See -version for available targets"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000074 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000075
76static cl::opt<std::string>
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000077 TripleName("mtriple",
Matt Davis9e64a4c2018-10-31 17:47:25 +000078 cl::desc("Target triple. See -version for available targets"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000079 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000080
81static cl::opt<std::string>
82 MCPU("mcpu",
83 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000084 cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native"));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000085
Andrea Di Biagio06268642018-04-24 16:19:08 +000086static cl::opt<int>
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000087 OutputAsmVariant("output-asm-variant",
Andrea Di Biagio06268642018-04-24 16:19:08 +000088 cl::desc("Syntax variant to use for output printing"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000089 cl::cat(ToolOptions), cl::init(-1));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000090
91static cl::opt<unsigned> Iterations("iterations",
92 cl::desc("Number of iterations to run"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000093 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000094
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000095static cl::opt<unsigned>
96 DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
97 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000098
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000099static cl::opt<unsigned>
100 RegisterFileSize("register-file-size",
Matt Davis5ceaa982018-07-31 20:05:08 +0000101 cl::desc("Maximum number of physical registers which can "
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000102 "be used for register mappings"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000103 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000104
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000105static cl::opt<bool>
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000106 PrintRegisterFileStats("register-file-stats",
107 cl::desc("Print register file statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000108 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000109
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000110static cl::opt<bool> PrintDispatchStats("dispatch-stats",
111 cl::desc("Print dispatch statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000112 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000113
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000114static cl::opt<bool>
115 PrintSummaryView("summary-view", cl::Hidden,
116 cl::desc("Print summary view (enabled by default)"),
117 cl::cat(ViewOptions), cl::init(true));
118
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000119static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
120 cl::desc("Print scheduler statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000121 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000122
123static cl::opt<bool>
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000124 PrintRetireStats("retire-stats",
Andrea Di Biagio641cca32018-04-25 10:27:30 +0000125 cl::desc("Print retire control unit statistics"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000126 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000127
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000128static cl::opt<bool> PrintResourcePressureView(
129 "resource-pressure",
130 cl::desc("Print the resource pressure view (enabled by default)"),
131 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagio29538c62018-03-23 11:33:09 +0000132
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000133static cl::opt<bool> PrintTimelineView("timeline",
134 cl::desc("Print the timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000135 cl::cat(ViewOptions), cl::init(false));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000136
137static cl::opt<unsigned> TimelineMaxIterations(
138 "timeline-max-iterations",
139 cl::desc("Maximum number of iterations to print in timeline view"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000140 cl::cat(ViewOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000141
142static cl::opt<unsigned> TimelineMaxCycles(
143 "timeline-max-cycles",
144 cl::desc(
145 "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000146 cl::cat(ViewOptions), cl::init(80));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000147
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000148static cl::opt<bool>
149 AssumeNoAlias("noalias",
150 cl::desc("If set, assume that loads and stores do not alias"),
151 cl::cat(ToolOptions), cl::init(true));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000152
153static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000154 LoadQueueSize("lqueue",
155 cl::desc("Size of the load queue (unbound by default)"),
156 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000157
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000158static cl::opt<unsigned>
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000159 StoreQueueSize("squeue",
160 cl::desc("Size of the store queue (unbound by default)"),
161 cl::cat(ToolOptions), cl::init(0));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000162
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000163static cl::opt<bool>
164 PrintInstructionTables("instruction-tables",
165 cl::desc("Print instruction tables"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000166 cl::cat(ToolOptions), cl::init(false));
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000167
Andrea Di Biagio450ea7a2018-05-05 15:36:47 +0000168static cl::opt<bool> PrintInstructionInfoView(
169 "instruction-info",
170 cl::desc("Print the instruction info view (enabled by default)"),
171 cl::cat(ViewOptions), cl::init(true));
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000172
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000173static cl::opt<bool> EnableAllStats("all-stats",
174 cl::desc("Print all hardware statistics"),
175 cl::cat(ViewOptions), cl::init(false));
176
177static cl::opt<bool>
178 EnableAllViews("all-views",
179 cl::desc("Print all views including hardware statistics"),
180 cl::cat(ViewOptions), cl::init(false));
181
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000182namespace {
183
184const Target *getTarget(const char *ProgName) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000185 if (TripleName.empty())
186 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
187 Triple TheTriple(TripleName);
188
189 // Get the target specific parser.
190 std::string Error;
191 const Target *TheTarget =
192 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
193 if (!TheTarget) {
194 errs() << ProgName << ": " << Error;
195 return nullptr;
196 }
197
198 // Return the found target.
199 return TheTarget;
200}
201
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000202// A comment consumer that parses strings.
203// The only valid tokens are strings.
204class MCACommentConsumer : public AsmCommentConsumer {
205public:
206 mca::CodeRegions &Regions;
207
208 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
209 void HandleComment(SMLoc Loc, StringRef CommentText) override {
210 // Skip empty comments.
211 StringRef Comment(CommentText);
212 if (Comment.empty())
213 return;
214
215 // Skip spaces and tabs
216 unsigned Position = Comment.find_first_not_of(" \t");
217 if (Position >= Comment.size())
Owen Rodley73d18aa2018-09-28 04:51:45 +0000218 // We reached the end of the comment. Bail out.
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000219 return;
220
221 Comment = Comment.drop_front(Position);
222 if (Comment.consume_front("LLVM-MCA-END")) {
223 Regions.endRegion(Loc);
224 return;
225 }
226
227 // Now try to parse string LLVM-MCA-BEGIN
228 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
229 return;
230
231 // Skip spaces and tabs
232 Position = Comment.find_first_not_of(" \t");
233 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000234 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000235 // Use the rest of the string as a descriptor for this code snippet.
236 Regions.beginRegion(Comment, Loc);
237 }
238};
239
Matt Davisaccb5112018-08-20 22:41:27 +0000240int AssembleInput(MCAsmParser &Parser, const Target *TheTarget,
241 MCSubtargetInfo &STI, MCInstrInfo &MCII,
242 MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000243 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000244 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000245
246 if (!TAP) {
Andrea Di Biagio24fb4fc2018-05-04 13:52:12 +0000247 WithColor::error() << "this target does not support assembly parsing.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000248 return 1;
249 }
250
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000251 Parser.setTargetParser(*TAP);
252 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000253}
254
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000255ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000256 if (OutputFilename == "")
257 OutputFilename = "-";
258 std::error_code EC;
259 auto Out =
260 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
261 if (!EC)
262 return std::move(Out);
263 return EC;
264}
265
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000266class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000267 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000268
269public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000270 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
271 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000272
273 // We only want to intercept the emission of new instructions.
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000274 virtual void EmitInstruction(const MCInst &Inst,
275 const MCSubtargetInfo & /* unused */,
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000276 bool /* unused */) override {
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000277 Regions.addInstruction(Inst);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000278 }
279
280 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
281 return true;
282 }
283
284 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
285 unsigned ByteAlignment) override {}
286 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Francis Visoiu Mistrih4d5b1072018-07-02 17:29:43 +0000287 uint64_t Size = 0, unsigned ByteAlignment = 0,
288 SMLoc Loc = SMLoc()) override {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000289 void EmitGPRel32Value(const MCExpr *Value) override {}
290 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
291 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
292 void EmitCOFFSymbolType(int Type) override {}
293 void EndCOFFSymbolDef() override {}
294
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000295 ArrayRef<MCInst> GetInstructionSequence(unsigned Index) const {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000296 return Regions.getInstructionSequence(Index);
297 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000298};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000299} // end of anonymous namespace
300
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000301static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
302 if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
303 O = Default.getValue();
304}
305
306static void processViewOptions() {
307 if (!EnableAllViews.getNumOccurrences() &&
308 !EnableAllStats.getNumOccurrences())
309 return;
310
311 if (EnableAllViews.getNumOccurrences()) {
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000312 processOptionImpl(PrintSummaryView, EnableAllViews);
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000313 processOptionImpl(PrintResourcePressureView, EnableAllViews);
314 processOptionImpl(PrintTimelineView, EnableAllViews);
315 processOptionImpl(PrintInstructionInfoView, EnableAllViews);
316 }
317
318 const cl::opt<bool> &Default =
319 EnableAllViews.getPosition() < EnableAllStats.getPosition()
320 ? EnableAllStats
321 : EnableAllViews;
322 processOptionImpl(PrintRegisterFileStats, Default);
323 processOptionImpl(PrintDispatchStats, Default);
324 processOptionImpl(PrintSchedulerStats, Default);
325 processOptionImpl(PrintRetireStats, Default);
326}
327
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000328// Returns true on success.
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000329static bool runPipeline(mca::Pipeline &P) {
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000330 // Handle pipeline errors here.
331 if (auto Err = P.run()) {
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000332 WithColor::error() << toString(std::move(Err));
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000333 return false;
334 }
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000335 return true;
336}
337
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000338int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000339 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000340
341 // Initialize targets and assembly parsers.
342 llvm::InitializeAllTargetInfos();
343 llvm::InitializeAllTargetMCs();
344 llvm::InitializeAllAsmParsers();
345
346 // Enable printing of available targets when flag --version is specified.
347 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
348
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000349 cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
350
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000351 // Parse flags and initialize target options.
352 cl::ParseCommandLineOptions(argc, argv,
353 "llvm machine code performance analyzer.\n");
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000354
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000355 MCTargetOptions MCOptions;
356 MCOptions.PreserveAsmComments = false;
357
358 // Get the target from the triple. If a triple is not specified, then select
359 // the default triple for the host. If the triple doesn't correspond to any
360 // registered target, then exit with an error message.
361 const char *ProgName = argv[0];
362 const Target *TheTarget = getTarget(ProgName);
363 if (!TheTarget)
364 return 1;
365
366 // GetTarget() may replaced TripleName with a default triple.
367 // For safety, reconstruct the Triple object.
368 Triple TheTriple(TripleName);
369
370 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
371 MemoryBuffer::getFileOrSTDIN(InputFilename);
372 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000373 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000374 return 1;
375 }
376
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000377 // Apply overrides to llvm-mca specific options.
378 processViewOptions();
379
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000380 SourceMgr SrcMgr;
381
382 // Tell SrcMgr about this buffer, which is what the parser will pick up.
383 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
384
385 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
386 assert(MRI && "Unable to create target register info!");
387
388 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
389 assert(MAI && "Unable to create target asm info!");
390
391 MCObjectFileInfo MOFI;
392 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
393 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
394
395 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000396
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000397 mca::CodeRegions Regions(SrcMgr);
398 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000399
400 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000401
Andrea Di Biagio2145b132018-06-20 10:08:11 +0000402 std::unique_ptr<MCInstrAnalysis> MCIA(
403 TheTarget->createMCInstrAnalysis(MCII.get()));
404
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000405 if (!MCPU.compare("native"))
406 MCPU = llvm::sys::getHostCPUName();
407
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000408 std::unique_ptr<MCSubtargetInfo> STI(
409 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
410 if (!STI->isCPUStringValid(MCPU))
411 return 1;
412
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000413 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000414 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
415 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000416 return 1;
417 }
418
419 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000420 WithColor::error()
421 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000422 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
423 << "'.\n";
424
425 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000426 WithColor::note()
427 << "cpu '" << MCPU << "' provides itineraries. However, "
428 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000429 return 1;
430 }
431
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000432 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000433 MCAsmLexer &Lexer = P->getLexer();
434 MCACommentConsumer CC(Regions);
435 Lexer.setCommentConsumer(&CC);
436
Matt Davisaccb5112018-08-20 22:41:27 +0000437 if (AssembleInput(*P, TheTarget, *STI, *MCII, MCOptions))
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000438 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000439
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000440 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000441 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000442 return 1;
443 }
444
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000445 // Now initialize the output file.
446 auto OF = getOutputStream();
447 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000448 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000449 return 1;
450 }
451
Andrea Di Biagio06268642018-04-24 16:19:08 +0000452 unsigned AssemblerDialect = P->getAssemblerDialect();
453 if (OutputAsmVariant >= 0)
454 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
455 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
456 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
457 if (!IP) {
458 WithColor::error()
459 << "unable to create instruction printer for target triple '"
460 << TheTriple.normalize() << "' with assembly variant "
461 << AssemblerDialect << ".\n";
462 return 1;
463 }
464
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000465 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
466
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000467 const MCSchedModel &SM = STI->getSchedModel();
468
469 unsigned Width = SM.IssueWidth;
470 if (DispatchWidth)
471 Width = DispatchWidth;
472
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000473 // Create an instruction builder.
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000474 mca::InstrBuilder IB(*STI, *MCII, *MRI, *MCIA);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000475
Matt Davis362ea5f2018-07-06 18:03:14 +0000476 // Create a context to control ownership of the pipeline hardware.
477 mca::Context MCA(*MRI, *STI);
478
479 mca::PipelineOptions PO(Width, RegisterFileSize, LoadQueueSize,
480 StoreQueueSize, AssumeNoAlias);
481
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000482 // Number each region in the sequence.
483 unsigned RegionIdx = 0;
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000484
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000485 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
486 // Skip empty code regions.
487 if (Region->empty())
488 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000489
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000490 // Don't print the header of this region if it is the default region, and
491 // it doesn't have an end location.
492 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
493 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
494 StringRef Desc = Region->getDescription();
495 if (!Desc.empty())
496 TOF->os() << " - " << Desc;
497 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000498 }
499
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000500 // Lower the MCInst sequence into an mca::Instruction sequence.
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000501 ArrayRef<MCInst> Insts = Region->getInstructions();
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000502 std::vector<std::unique_ptr<mca::Instruction>> LoweredSequence;
503 for (const MCInst &MCI : Insts) {
Matt Davis9e64a4c2018-10-31 17:47:25 +0000504 llvm::Expected<std::unique_ptr<mca::Instruction>> Inst =
505 IB.createInstruction(MCI);
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000506 if (!Inst) {
Matt Davis9e64a4c2018-10-31 17:47:25 +0000507 if (auto NewE = handleErrors(
508 Inst.takeError(),
509 [&IP, &STI](const mca::InstructionError<MCInst> &IE) {
510 std::string InstructionStr;
511 raw_string_ostream SS(InstructionStr);
512 WithColor::error() << IE.Message << '\n';
513 IP->printInst(&IE.Inst, SS, "", *STI);
514 SS.flush();
515 WithColor::note() << "instruction: " << InstructionStr
516 << '\n';
517 })) {
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000518 // Default case.
519 WithColor::error() << toString(std::move(NewE));
520 }
521 return 1;
522 }
523
524 LoweredSequence.emplace_back(std::move(Inst.get()));
525 }
526
Matt Davis9e64a4c2018-10-31 17:47:25 +0000527 mca::SourceMgr S(LoweredSequence, PrintInstructionTables ? 1 : Iterations);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000528
529 if (PrintInstructionTables) {
Matt Davis0e8402e2018-07-14 23:52:50 +0000530 // Create a pipeline, stages, and a printer.
531 auto P = llvm::make_unique<mca::Pipeline>();
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000532 P->appendStage(llvm::make_unique<mca::FetchStage>(S));
Andrea Di Biagio65c77d72018-10-24 16:56:43 +0000533 P->appendStage(llvm::make_unique<mca::InstructionTables>(SM));
Matt Davis0e8402e2018-07-14 23:52:50 +0000534 mca::PipelinePrinter Printer(*P);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000535
Matt Davis0e8402e2018-07-14 23:52:50 +0000536 // Create the views for this pipeline, execute, and emit a report.
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000537 if (PrintInstructionInfoView) {
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000538 Printer.addView(llvm::make_unique<mca::InstructionInfoView>(
539 *STI, *MCII, Insts, *IP));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000540 }
Matt Davis0e8402e2018-07-14 23:52:50 +0000541 Printer.addView(
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000542 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts));
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000543
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000544 if (!runPipeline(*P))
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000545 return 1;
546
Matt Davis0e8402e2018-07-14 23:52:50 +0000547 Printer.printReport(TOF->os());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000548 continue;
549 }
550
Matt Davis362ea5f2018-07-06 18:03:14 +0000551 // Create a basic pipeline simulating an out-of-order backend.
552 auto P = MCA.createDefaultPipeline(PO, IB, S);
Matt Davisdea343d2018-06-25 16:53:00 +0000553 mca::PipelinePrinter Printer(*P);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000554
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000555 if (PrintSummaryView)
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000556 Printer.addView(llvm::make_unique<mca::SummaryView>(SM, Insts, Width));
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000557
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000558 if (PrintInstructionInfoView)
559 Printer.addView(
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000560 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, Insts, *IP));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000561
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000562 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000563 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000564
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000565 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000566 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
567
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000568 if (PrintRetireStats)
569 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000570
571 if (PrintRegisterFileStats)
572 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
573
574 if (PrintResourcePressureView)
575 Printer.addView(
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000576 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000577
578 if (PrintTimelineView) {
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000579 unsigned TimelineIterations =
580 TimelineMaxIterations ? TimelineMaxIterations : 10;
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000581 Printer.addView(llvm::make_unique<mca::TimelineView>(
Andrea Di Biagio84d00512018-10-26 10:48:04 +0000582 *STI, *IP, Insts, std::min(TimelineIterations, S.getNumIterations()),
583 TimelineMaxCycles));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000584 }
585
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +0000586 if (!runPipeline(*P))
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000587 return 1;
588
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000589 Printer.printReport(TOF->os());
Andrea Di Biagio9b3cb082018-07-02 20:39:57 +0000590
591 // Clear the InstrBuilder internal state in preparation for another round.
592 IB.clear();
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000593 }
594
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000595 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000596 return 0;
597}