Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===-- 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 Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame^] | 19 | // The cpu defaults to the 'native' host cpu. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 20 | // The output defaults to standard output. |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 24 | #include "BackendPrinter.h" |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 25 | #include "CodeRegion.h" |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 26 | #include "DispatchStatistics.h" |
Andrea Di Biagio | df5d948 | 2018-03-23 19:40:04 +0000 | [diff] [blame] | 27 | #include "InstructionInfoView.h" |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 28 | #include "InstructionTables.h" |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 29 | #include "RegisterFileStatistics.h" |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 30 | #include "ResourcePressureView.h" |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 31 | #include "RetireControlUnitStatistics.h" |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 32 | #include "SchedulerStatistics.h" |
Andrea Di Biagio | 0cc66c7 | 2018-03-09 13:52:03 +0000 | [diff] [blame] | 33 | #include "SummaryView.h" |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 34 | #include "TimelineView.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCAsmInfo.h" |
| 36 | #include "llvm/MC/MCContext.h" |
| 37 | #include "llvm/MC/MCObjectFileInfo.h" |
| 38 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
| 39 | #include "llvm/MC/MCRegisterInfo.h" |
| 40 | #include "llvm/MC/MCStreamer.h" |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame^] | 41 | #include "llvm/Support/Host.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorOr.h" |
| 44 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 45 | #include "llvm/Support/InitLLVM.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 46 | #include "llvm/Support/MemoryBuffer.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 47 | #include "llvm/Support/SourceMgr.h" |
| 48 | #include "llvm/Support/TargetRegistry.h" |
| 49 | #include "llvm/Support/TargetSelect.h" |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 51 | #include "llvm/Support/WithColor.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 52 | |
| 53 | using namespace llvm; |
| 54 | |
| 55 | static cl::opt<std::string> |
| 56 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 57 | |
| 58 | static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), |
| 59 | cl::init("-"), |
| 60 | cl::value_desc("filename")); |
| 61 | |
| 62 | static cl::opt<std::string> |
| 63 | ArchName("march", cl::desc("Target arch to assemble for, " |
| 64 | "see -version for available targets")); |
| 65 | |
| 66 | static cl::opt<std::string> |
| 67 | TripleName("mtriple", cl::desc("Target triple to assemble for, " |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 68 | "see -version for available targets")); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 69 | |
| 70 | static cl::opt<std::string> |
| 71 | MCPU("mcpu", |
| 72 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame^] | 73 | cl::value_desc("cpu-name"), cl::init("native")); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 74 | |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 75 | static cl::opt<int> |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 76 | OutputAsmVariant("output-asm-variant", |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 77 | cl::desc("Syntax variant to use for output printing"), |
| 78 | cl::init(-1)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 79 | |
| 80 | static cl::opt<unsigned> Iterations("iterations", |
| 81 | cl::desc("Number of iterations to run"), |
| 82 | cl::init(0)); |
| 83 | |
| 84 | static cl::opt<unsigned> DispatchWidth( |
| 85 | "dispatch", |
| 86 | cl::desc("Dispatch Width. By default it is set equal to IssueWidth"), |
| 87 | cl::init(0)); |
| 88 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 89 | static cl::opt<unsigned> |
| 90 | RegisterFileSize("register-file-size", |
| 91 | cl::desc("Maximum number of temporary registers which can " |
| 92 | "be used for register mappings"), |
| 93 | cl::init(0)); |
| 94 | |
Andrea Di Biagio | 29538c6 | 2018-03-23 11:33:09 +0000 | [diff] [blame] | 95 | static cl::opt<bool> |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 96 | PrintRegisterFileStats("register-file-stats", |
| 97 | cl::desc("Print register file statistics"), |
| 98 | cl::init(false)); |
| 99 | |
| 100 | static cl::opt<bool> |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 101 | PrintDispatchStats("dispatch-stats", |
| 102 | cl::desc("Print dispatch statistics"), |
| 103 | cl::init(false)); |
| 104 | |
| 105 | static cl::opt<bool> |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 106 | PrintSchedulerStats("scheduler-stats", |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 107 | cl::desc("Print scheduler statistics"), |
| 108 | cl::init(false)); |
| 109 | |
| 110 | static cl::opt<bool> |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 111 | PrintRetireStats("retire-stats", |
| 112 | cl::desc("Print retire control unit statistics"), |
| 113 | cl::init(false)); |
| 114 | |
| 115 | static cl::opt<bool> |
Andrea Di Biagio | 29538c6 | 2018-03-23 11:33:09 +0000 | [diff] [blame] | 116 | PrintResourcePressureView("resource-pressure", |
| 117 | cl::desc("Print the resource pressure view"), |
| 118 | cl::init(true)); |
| 119 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 120 | static cl::opt<bool> PrintTimelineView("timeline", |
| 121 | cl::desc("Print the timeline view"), |
| 122 | cl::init(false)); |
| 123 | |
| 124 | static cl::opt<unsigned> TimelineMaxIterations( |
| 125 | "timeline-max-iterations", |
| 126 | cl::desc("Maximum number of iterations to print in timeline view"), |
| 127 | cl::init(0)); |
| 128 | |
| 129 | static cl::opt<unsigned> TimelineMaxCycles( |
| 130 | "timeline-max-cycles", |
| 131 | cl::desc( |
| 132 | "Maximum number of cycles in the timeline view. Defaults to 80 cycles"), |
| 133 | cl::init(80)); |
| 134 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 135 | static cl::opt<bool> AssumeNoAlias( |
| 136 | "noalias", |
| 137 | cl::desc("If set, it assumes that loads and stores do not alias"), |
| 138 | cl::init(true)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 139 | |
| 140 | static cl::opt<unsigned> |
| 141 | LoadQueueSize("lqueue", cl::desc("Size of the load queue"), cl::init(0)); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 142 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 143 | static cl::opt<unsigned> |
| 144 | StoreQueueSize("squeue", cl::desc("Size of the store queue"), cl::init(0)); |
| 145 | |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 146 | static cl::opt<bool> |
| 147 | PrintInstructionTables("instruction-tables", |
| 148 | cl::desc("Print instruction tables"), |
| 149 | cl::init(false)); |
| 150 | |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 151 | static cl::opt<bool> |
| 152 | PrintInstructionInfoView("instruction-info", |
| 153 | cl::desc("Print the instruction info view"), |
| 154 | cl::init(true)); |
| 155 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 156 | namespace { |
| 157 | |
| 158 | const Target *getTarget(const char *ProgName) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 159 | TripleName = Triple::normalize(TripleName); |
| 160 | if (TripleName.empty()) |
| 161 | TripleName = Triple::normalize(sys::getDefaultTargetTriple()); |
| 162 | Triple TheTriple(TripleName); |
| 163 | |
| 164 | // Get the target specific parser. |
| 165 | std::string Error; |
| 166 | const Target *TheTarget = |
| 167 | TargetRegistry::lookupTarget(ArchName, TheTriple, Error); |
| 168 | if (!TheTarget) { |
| 169 | errs() << ProgName << ": " << Error; |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | // Return the found target. |
| 174 | return TheTarget; |
| 175 | } |
| 176 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 177 | // A comment consumer that parses strings. |
| 178 | // The only valid tokens are strings. |
| 179 | class MCACommentConsumer : public AsmCommentConsumer { |
| 180 | public: |
| 181 | mca::CodeRegions &Regions; |
| 182 | |
| 183 | MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {} |
| 184 | void HandleComment(SMLoc Loc, StringRef CommentText) override { |
| 185 | // Skip empty comments. |
| 186 | StringRef Comment(CommentText); |
| 187 | if (Comment.empty()) |
| 188 | return; |
| 189 | |
| 190 | // Skip spaces and tabs |
| 191 | unsigned Position = Comment.find_first_not_of(" \t"); |
| 192 | if (Position >= Comment.size()) |
| 193 | // we reached the end of the comment. Bail out. |
| 194 | return; |
| 195 | |
| 196 | Comment = Comment.drop_front(Position); |
| 197 | if (Comment.consume_front("LLVM-MCA-END")) { |
| 198 | Regions.endRegion(Loc); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Now try to parse string LLVM-MCA-BEGIN |
| 203 | if (!Comment.consume_front("LLVM-MCA-BEGIN")) |
| 204 | return; |
| 205 | |
| 206 | // Skip spaces and tabs |
| 207 | Position = Comment.find_first_not_of(" \t"); |
| 208 | if (Position < Comment.size()) |
Fangrui Song | bb08257 | 2018-04-09 17:06:57 +0000 | [diff] [blame] | 209 | Comment = Comment.drop_front(Position); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 210 | // Use the rest of the string as a descriptor for this code snippet. |
| 211 | Regions.beginRegion(Comment, Loc); |
| 212 | } |
| 213 | }; |
| 214 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 215 | int AssembleInput(const char *ProgName, MCAsmParser &Parser, |
| 216 | const Target *TheTarget, MCSubtargetInfo &STI, |
| 217 | MCInstrInfo &MCII, MCTargetOptions &MCOptions) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 218 | std::unique_ptr<MCTargetAsmParser> TAP( |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 219 | TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 220 | |
| 221 | if (!TAP) { |
| 222 | errs() << ProgName |
| 223 | << ": error: this target does not support assembly parsing.\n"; |
| 224 | return 1; |
| 225 | } |
| 226 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 227 | Parser.setTargetParser(*TAP); |
| 228 | return Parser.Run(false); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 231 | ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() { |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 232 | if (OutputFilename == "") |
| 233 | OutputFilename = "-"; |
| 234 | std::error_code EC; |
| 235 | auto Out = |
| 236 | llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None); |
| 237 | if (!EC) |
| 238 | return std::move(Out); |
| 239 | return EC; |
| 240 | } |
| 241 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 242 | class MCStreamerWrapper final : public MCStreamer { |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 243 | mca::CodeRegions &Regions; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 244 | |
| 245 | public: |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 246 | MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R) |
| 247 | : MCStreamer(Context), Regions(R) {} |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 248 | |
| 249 | // We only want to intercept the emission of new instructions. |
| 250 | virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, |
| 251 | bool /* unused */) override { |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 252 | Regions.addInstruction(llvm::make_unique<const MCInst>(Inst)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 260 | unsigned ByteAlignment) override {} |
| 261 | void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, |
| 262 | uint64_t Size = 0, unsigned ByteAlignment = 0) override {} |
| 263 | void EmitGPRel32Value(const MCExpr *Value) override {} |
| 264 | void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {} |
| 265 | void EmitCOFFSymbolStorageClass(int StorageClass) override {} |
| 266 | void EmitCOFFSymbolType(int Type) override {} |
| 267 | void EndCOFFSymbolDef() override {} |
| 268 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 269 | const std::vector<std::unique_ptr<const MCInst>> & |
| 270 | GetInstructionSequence(unsigned Index) const { |
| 271 | return Regions.getInstructionSequence(Index); |
| 272 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 273 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 274 | } // end of anonymous namespace |
| 275 | |
| 276 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 277 | InitLLVM X(argc, argv); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 278 | |
| 279 | // Initialize targets and assembly parsers. |
| 280 | llvm::InitializeAllTargetInfos(); |
| 281 | llvm::InitializeAllTargetMCs(); |
| 282 | llvm::InitializeAllAsmParsers(); |
| 283 | |
| 284 | // Enable printing of available targets when flag --version is specified. |
| 285 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 286 | |
| 287 | // Parse flags and initialize target options. |
| 288 | cl::ParseCommandLineOptions(argc, argv, |
| 289 | "llvm machine code performance analyzer.\n"); |
| 290 | MCTargetOptions MCOptions; |
| 291 | MCOptions.PreserveAsmComments = false; |
| 292 | |
| 293 | // Get the target from the triple. If a triple is not specified, then select |
| 294 | // the default triple for the host. If the triple doesn't correspond to any |
| 295 | // registered target, then exit with an error message. |
| 296 | const char *ProgName = argv[0]; |
| 297 | const Target *TheTarget = getTarget(ProgName); |
| 298 | if (!TheTarget) |
| 299 | return 1; |
| 300 | |
| 301 | // GetTarget() may replaced TripleName with a default triple. |
| 302 | // For safety, reconstruct the Triple object. |
| 303 | Triple TheTriple(TripleName); |
| 304 | |
| 305 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr = |
| 306 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 307 | if (std::error_code EC = BufferPtr.getError()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 308 | WithColor::error() << InputFilename << ": " << EC.message() << '\n'; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | SourceMgr SrcMgr; |
| 313 | |
| 314 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 315 | SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); |
| 316 | |
| 317 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 318 | assert(MRI && "Unable to create target register info!"); |
| 319 | |
| 320 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 321 | assert(MAI && "Unable to create target asm info!"); |
| 322 | |
| 323 | MCObjectFileInfo MOFI; |
| 324 | MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr); |
| 325 | MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx); |
| 326 | |
| 327 | std::unique_ptr<buffer_ostream> BOS; |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 328 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 329 | mca::CodeRegions Regions(SrcMgr); |
| 330 | MCStreamerWrapper Str(Ctx, Regions); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 331 | |
| 332 | std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame^] | 333 | |
| 334 | if (!MCPU.compare("native")) |
| 335 | MCPU = llvm::sys::getHostCPUName(); |
| 336 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 337 | std::unique_ptr<MCSubtargetInfo> STI( |
| 338 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ "")); |
| 339 | if (!STI->isCPUStringValid(MCPU)) |
| 340 | return 1; |
| 341 | |
| 342 | if (!STI->getSchedModel().isOutOfOrder()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 343 | WithColor::error() << "please specify an out-of-order cpu. '" << MCPU |
| 344 | << "' is an in-order cpu.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 345 | return 1; |
| 346 | } |
| 347 | |
| 348 | if (!STI->getSchedModel().hasInstrSchedModel()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 349 | WithColor::error() |
| 350 | << "unable to find instruction-level scheduling information for" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 351 | << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU |
| 352 | << "'.\n"; |
| 353 | |
| 354 | if (STI->getSchedModel().InstrItineraries) |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 355 | WithColor::note() |
| 356 | << "cpu '" << MCPU << "' provides itineraries. However, " |
| 357 | << "instruction itineraries are currently unsupported.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 358 | return 1; |
| 359 | } |
| 360 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 361 | std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 362 | MCAsmLexer &Lexer = P->getLexer(); |
| 363 | MCACommentConsumer CC(Regions); |
| 364 | Lexer.setCommentConsumer(&CC); |
| 365 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 366 | if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions)) |
| 367 | return 1; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 368 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 369 | if (Regions.empty()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 370 | WithColor::error() << "no assembly instructions found.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 371 | return 1; |
| 372 | } |
| 373 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 374 | // Now initialize the output file. |
| 375 | auto OF = getOutputStream(); |
| 376 | if (std::error_code EC = OF.getError()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 377 | WithColor::error() << EC.message() << '\n'; |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 378 | return 1; |
| 379 | } |
| 380 | |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 381 | unsigned AssemblerDialect = P->getAssemblerDialect(); |
| 382 | if (OutputAsmVariant >= 0) |
| 383 | AssemblerDialect = static_cast<unsigned>(OutputAsmVariant); |
| 384 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 385 | Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI)); |
| 386 | if (!IP) { |
| 387 | WithColor::error() |
| 388 | << "unable to create instruction printer for target triple '" |
| 389 | << TheTriple.normalize() << "' with assembly variant " |
| 390 | << AssemblerDialect << ".\n"; |
| 391 | return 1; |
| 392 | } |
| 393 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 394 | std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF); |
| 395 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 396 | const MCSchedModel &SM = STI->getSchedModel(); |
| 397 | |
| 398 | unsigned Width = SM.IssueWidth; |
| 399 | if (DispatchWidth) |
| 400 | Width = DispatchWidth; |
| 401 | |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 402 | // Create an instruction builder. |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 403 | mca::InstrBuilder IB(*STI, *MCII); |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 404 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 405 | // Number each region in the sequence. |
| 406 | unsigned RegionIdx = 0; |
| 407 | for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) { |
| 408 | // Skip empty code regions. |
| 409 | if (Region->empty()) |
| 410 | continue; |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 411 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 412 | // Don't print the header of this region if it is the default region, and |
| 413 | // it doesn't have an end location. |
| 414 | if (Region->startLoc().isValid() || Region->endLoc().isValid()) { |
| 415 | TOF->os() << "\n[" << RegionIdx++ << "] Code Region"; |
| 416 | StringRef Desc = Region->getDescription(); |
| 417 | if (!Desc.empty()) |
| 418 | TOF->os() << " - " << Desc; |
| 419 | TOF->os() << "\n\n"; |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 422 | mca::SourceMgr S(Region->getInstructions(), |
| 423 | PrintInstructionTables ? 1 : Iterations); |
| 424 | |
| 425 | if (PrintInstructionTables) { |
| 426 | mca::InstructionTables IT(STI->getSchedModel(), IB, S); |
| 427 | |
| 428 | if (PrintInstructionInfoView) { |
| 429 | IT.addView( |
| 430 | llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP)); |
| 431 | } |
| 432 | |
| 433 | IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S)); |
| 434 | IT.run(); |
| 435 | IT.printReport(TOF->os()); |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | mca::Backend B(*STI, *MRI, IB, S, Width, RegisterFileSize, LoadQueueSize, |
| 440 | StoreQueueSize, AssumeNoAlias); |
| 441 | mca::BackendPrinter Printer(B); |
| 442 | |
| 443 | Printer.addView(llvm::make_unique<mca::SummaryView>(S, Width)); |
| 444 | if (PrintInstructionInfoView) |
| 445 | Printer.addView( |
| 446 | llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP)); |
| 447 | |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 448 | if (PrintDispatchStats) |
Andrea Di Biagio | 074ff7c | 2018-04-11 12:31:44 +0000 | [diff] [blame] | 449 | Printer.addView(llvm::make_unique<mca::DispatchStatistics>()); |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 450 | |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 451 | if (PrintSchedulerStats) |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 452 | Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI)); |
| 453 | |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 454 | if (PrintRetireStats) |
| 455 | Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>()); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 456 | |
| 457 | if (PrintRegisterFileStats) |
| 458 | Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI)); |
| 459 | |
| 460 | if (PrintResourcePressureView) |
| 461 | Printer.addView( |
| 462 | llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S)); |
| 463 | |
| 464 | if (PrintTimelineView) { |
| 465 | Printer.addView(llvm::make_unique<mca::TimelineView>( |
| 466 | *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles)); |
| 467 | } |
| 468 | |
| 469 | B.run(); |
| 470 | Printer.printReport(TOF->os()); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 473 | TOF->keep(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |