blob: 9992395fb6e5da7852918e149e13ae407a35b58c [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>
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000072 ArchName("march",
73 cl::desc("Target arch to assemble for, "
74 "see -version for available targets"),
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +000075 cl::cat(ToolOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000076
77static cl::opt<std::string>
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000078 TripleName("mtriple",
79 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",
Matt Davis5ceaa982018-07-31 20:05:08 +0000103 cl::desc("Maximum number of physical registers which can "
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000104 "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 if (TripleName.empty())
188 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
189 Triple TheTriple(TripleName);
190
191 // Get the target specific parser.
192 std::string Error;
193 const Target *TheTarget =
194 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
195 if (!TheTarget) {
196 errs() << ProgName << ": " << Error;
197 return nullptr;
198 }
199
200 // Return the found target.
201 return TheTarget;
202}
203
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000204// A comment consumer that parses strings.
205// The only valid tokens are strings.
206class MCACommentConsumer : public AsmCommentConsumer {
207public:
208 mca::CodeRegions &Regions;
209
210 MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {}
211 void HandleComment(SMLoc Loc, StringRef CommentText) override {
212 // Skip empty comments.
213 StringRef Comment(CommentText);
214 if (Comment.empty())
215 return;
216
217 // Skip spaces and tabs
218 unsigned Position = Comment.find_first_not_of(" \t");
219 if (Position >= Comment.size())
Owen Rodley73d18aa2018-09-28 04:51:45 +0000220 // We reached the end of the comment. Bail out.
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000221 return;
222
223 Comment = Comment.drop_front(Position);
224 if (Comment.consume_front("LLVM-MCA-END")) {
225 Regions.endRegion(Loc);
226 return;
227 }
228
229 // Now try to parse string LLVM-MCA-BEGIN
230 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
231 return;
232
233 // Skip spaces and tabs
234 Position = Comment.find_first_not_of(" \t");
235 if (Position < Comment.size())
Fangrui Songbb082572018-04-09 17:06:57 +0000236 Comment = Comment.drop_front(Position);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000237 // Use the rest of the string as a descriptor for this code snippet.
238 Regions.beginRegion(Comment, Loc);
239 }
240};
241
Matt Davisaccb5112018-08-20 22:41:27 +0000242int AssembleInput(MCAsmParser &Parser, const Target *TheTarget,
243 MCSubtargetInfo &STI, MCInstrInfo &MCII,
244 MCTargetOptions &MCOptions) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000245 std::unique_ptr<MCTargetAsmParser> TAP(
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000246 TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000247
248 if (!TAP) {
Andrea Di Biagio24fb4fc2018-05-04 13:52:12 +0000249 WithColor::error() << "this target does not support assembly parsing.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000250 return 1;
251 }
252
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000253 Parser.setTargetParser(*TAP);
254 return Parser.Run(false);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000255}
256
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000257ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000258 if (OutputFilename == "")
259 OutputFilename = "-";
260 std::error_code EC;
261 auto Out =
262 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
263 if (!EC)
264 return std::move(Out);
265 return EC;
266}
267
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000268class MCStreamerWrapper final : public MCStreamer {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000269 mca::CodeRegions &Regions;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000270
271public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000272 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
273 : MCStreamer(Context), Regions(R) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000274
275 // We only want to intercept the emission of new instructions.
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000276 virtual void EmitInstruction(const MCInst &Inst,
277 const MCSubtargetInfo & /* unused */,
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000278 bool /* unused */) override {
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000279 Regions.addInstruction(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,
Francis Visoiu Mistrih4d5b1072018-07-02 17:29:43 +0000289 uint64_t Size = 0, unsigned ByteAlignment = 0,
290 SMLoc Loc = SMLoc()) override {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000291 void EmitGPRel32Value(const MCExpr *Value) override {}
292 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
293 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
294 void EmitCOFFSymbolType(int Type) override {}
295 void EndCOFFSymbolDef() override {}
296
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000297 ArrayRef<MCInst> GetInstructionSequence(unsigned Index) const {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000298 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;
324 processOptionImpl(PrintRegisterFileStats, Default);
325 processOptionImpl(PrintDispatchStats, Default);
326 processOptionImpl(PrintSchedulerStats, Default);
327 processOptionImpl(PrintRetireStats, Default);
328}
329
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000330// Returns true on success.
331static bool runPipeline(mca::Pipeline &P, MCInstPrinter &MCIP,
332 const MCSubtargetInfo &STI) {
333 // Handle pipeline errors here.
334 if (auto Err = P.run()) {
335 if (auto NewE = handleErrors(
336 std::move(Err),
337 [&MCIP, &STI](const mca::InstructionError<MCInst> &IE) {
338 std::string InstructionStr;
339 raw_string_ostream SS(InstructionStr);
340 WithColor::error() << IE.Message << '\n';
341 MCIP.printInst(&IE.Inst, SS, "", STI);
342 SS.flush();
343 WithColor::note() << "instruction: " << InstructionStr << '\n';
344 })) {
345 // Default case.
346 WithColor::error() << toString(std::move(NewE));
347 }
348 return false;
349 }
350
351 return true;
352}
353
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000354int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000355 InitLLVM X(argc, argv);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000356
357 // Initialize targets and assembly parsers.
358 llvm::InitializeAllTargetInfos();
359 llvm::InitializeAllTargetMCs();
360 llvm::InitializeAllAsmParsers();
361
362 // Enable printing of available targets when flag --version is specified.
363 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
364
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000365 cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
366
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000367 // Parse flags and initialize target options.
368 cl::ParseCommandLineOptions(argc, argv,
369 "llvm machine code performance analyzer.\n");
Andrea Di Biagio55e9e0f2018-05-17 15:35:14 +0000370
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000371 MCTargetOptions MCOptions;
372 MCOptions.PreserveAsmComments = false;
373
374 // Get the target from the triple. If a triple is not specified, then select
375 // the default triple for the host. If the triple doesn't correspond to any
376 // registered target, then exit with an error message.
377 const char *ProgName = argv[0];
378 const Target *TheTarget = getTarget(ProgName);
379 if (!TheTarget)
380 return 1;
381
382 // GetTarget() may replaced TripleName with a default triple.
383 // For safety, reconstruct the Triple object.
384 Triple TheTriple(TripleName);
385
386 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
387 MemoryBuffer::getFileOrSTDIN(InputFilename);
388 if (std::error_code EC = BufferPtr.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000389 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000390 return 1;
391 }
392
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000393 // Apply overrides to llvm-mca specific options.
394 processViewOptions();
395
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000396 SourceMgr SrcMgr;
397
398 // Tell SrcMgr about this buffer, which is what the parser will pick up.
399 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
400
401 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
402 assert(MRI && "Unable to create target register info!");
403
404 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
405 assert(MAI && "Unable to create target asm info!");
406
407 MCObjectFileInfo MOFI;
408 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
409 MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
410
411 std::unique_ptr<buffer_ostream> BOS;
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000412
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000413 mca::CodeRegions Regions(SrcMgr);
414 MCStreamerWrapper Str(Ctx, Regions);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000415
416 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000417
Andrea Di Biagio2145b132018-06-20 10:08:11 +0000418 std::unique_ptr<MCInstrAnalysis> MCIA(
419 TheTarget->createMCInstrAnalysis(MCII.get()));
420
Andrea Di Biagio93c49d52018-04-25 10:18:25 +0000421 if (!MCPU.compare("native"))
422 MCPU = llvm::sys::getHostCPUName();
423
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000424 std::unique_ptr<MCSubtargetInfo> STI(
425 TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
426 if (!STI->isCPUStringValid(MCPU))
427 return 1;
428
Andrea Di Biagioe9384eb2018-04-30 12:05:34 +0000429 if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000430 WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
431 << "' is an in-order cpu.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000432 return 1;
433 }
434
435 if (!STI->getSchedModel().hasInstrSchedModel()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000436 WithColor::error()
437 << "unable to find instruction-level scheduling information for"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000438 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
439 << "'.\n";
440
441 if (STI->getSchedModel().InstrItineraries)
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000442 WithColor::note()
443 << "cpu '" << MCPU << "' provides itineraries. However, "
444 << "instruction itineraries are currently unsupported.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000445 return 1;
446 }
447
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000448 std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000449 MCAsmLexer &Lexer = P->getLexer();
450 MCACommentConsumer CC(Regions);
451 Lexer.setCommentConsumer(&CC);
452
Matt Davisaccb5112018-08-20 22:41:27 +0000453 if (AssembleInput(*P, TheTarget, *STI, *MCII, MCOptions))
Andrea Di Biagio5c469442018-04-08 15:10:19 +0000454 return 1;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000455
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000456 if (Regions.empty()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000457 WithColor::error() << "no assembly instructions found.\n";
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000458 return 1;
459 }
460
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000461 // Now initialize the output file.
462 auto OF = getOutputStream();
463 if (std::error_code EC = OF.getError()) {
Jonas Devlieghere6adef092018-04-18 15:26:51 +0000464 WithColor::error() << EC.message() << '\n';
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000465 return 1;
466 }
467
Andrea Di Biagio06268642018-04-24 16:19:08 +0000468 unsigned AssemblerDialect = P->getAssemblerDialect();
469 if (OutputAsmVariant >= 0)
470 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
471 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
472 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
473 if (!IP) {
474 WithColor::error()
475 << "unable to create instruction printer for target triple '"
476 << TheTriple.normalize() << "' with assembly variant "
477 << AssemblerDialect << ".\n";
478 return 1;
479 }
480
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000481 std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
482
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000483 const MCSchedModel &SM = STI->getSchedModel();
484
485 unsigned Width = SM.IssueWidth;
486 if (DispatchWidth)
487 Width = DispatchWidth;
488
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000489 // Create an instruction builder.
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000490 mca::InstrBuilder IB(*STI, *MCII, *MRI, *MCIA);
Andrea Di Biagiob5088da2018-03-23 11:50:43 +0000491
Matt Davis362ea5f2018-07-06 18:03:14 +0000492 // Create a context to control ownership of the pipeline hardware.
493 mca::Context MCA(*MRI, *STI);
494
495 mca::PipelineOptions PO(Width, RegisterFileSize, LoadQueueSize,
496 StoreQueueSize, AssumeNoAlias);
497
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000498 // Number each region in the sequence.
499 unsigned RegionIdx = 0;
500 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
501 // Skip empty code regions.
502 if (Region->empty())
503 continue;
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000504
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000505 // Don't print the header of this region if it is the default region, and
506 // it doesn't have an end location.
507 if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
508 TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
509 StringRef Desc = Region->getDescription();
510 if (!Desc.empty())
511 TOF->os() << " - " << Desc;
512 TOF->os() << "\n\n";
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000513 }
514
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000515 mca::SourceMgr S(Region->getInstructions(),
516 PrintInstructionTables ? 1 : Iterations);
517
518 if (PrintInstructionTables) {
Matt Davis0e8402e2018-07-14 23:52:50 +0000519 // Create a pipeline, stages, and a printer.
520 auto P = llvm::make_unique<mca::Pipeline>();
521 P->appendStage(llvm::make_unique<mca::FetchStage>(IB, S));
Andrea Di Biagio65c77d72018-10-24 16:56:43 +0000522 P->appendStage(llvm::make_unique<mca::InstructionTables>(SM));
Matt Davis0e8402e2018-07-14 23:52:50 +0000523 mca::PipelinePrinter Printer(*P);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000524
Matt Davis0e8402e2018-07-14 23:52:50 +0000525 // Create the views for this pipeline, execute, and emit a report.
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000526 if (PrintInstructionInfoView) {
Matt Davis0e8402e2018-07-14 23:52:50 +0000527 Printer.addView(
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000528 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
529 }
Matt Davis0e8402e2018-07-14 23:52:50 +0000530 Printer.addView(
531 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000532
533 if (!runPipeline(*P, *IP, *STI))
534 return 1;
535
Matt Davis0e8402e2018-07-14 23:52:50 +0000536 Printer.printReport(TOF->os());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000537 continue;
538 }
539
Matt Davis362ea5f2018-07-06 18:03:14 +0000540 // Create a basic pipeline simulating an out-of-order backend.
541 auto P = MCA.createDefaultPipeline(PO, IB, S);
Matt Davisdea343d2018-06-25 16:53:00 +0000542 mca::PipelinePrinter Printer(*P);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000543
Roman Lebedev9ddf1282018-06-15 14:01:43 +0000544 if (PrintSummaryView)
545 Printer.addView(llvm::make_unique<mca::SummaryView>(SM, S, Width));
546
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000547 if (PrintInstructionInfoView)
548 Printer.addView(
549 llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP));
550
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000551 if (PrintDispatchStats)
Andrea Di Biagio074ff7c2018-04-11 12:31:44 +0000552 Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000553
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000554 if (PrintSchedulerStats)
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000555 Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
556
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000557 if (PrintRetireStats)
558 Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>());
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000559
560 if (PrintRegisterFileStats)
561 Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
562
563 if (PrintResourcePressureView)
564 Printer.addView(
565 llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S));
566
567 if (PrintTimelineView) {
568 Printer.addView(llvm::make_unique<mca::TimelineView>(
569 *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles));
570 }
571
Andrea Di Biagio083addf2018-10-24 10:56:47 +0000572 if (!runPipeline(*P, *IP, *STI))
573 return 1;
574
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000575 Printer.printReport(TOF->os());
Andrea Di Biagio9b3cb082018-07-02 20:39:57 +0000576
577 // Clear the InstrBuilder internal state in preparation for another round.
578 IB.clear();
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000579 }
580
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000581 TOF->keep();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000582 return 0;
583}