Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / llvm-mca / CodeRegionGenerator.cpp
blob4660af2c40aca37fca05172259029d6171867d05
1 //===----------------------- CodeRegionGenerator.cpp ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file defines classes responsible for generating llvm-mca
11 /// CodeRegions from various types of input. llvm-mca only analyzes CodeRegions,
12 /// so the classes here provide the input-to-CodeRegions translation.
14 //===----------------------------------------------------------------------===//
16 #include "CodeRegionGenerator.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCTargetOptions.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/SMLoc.h"
24 #include <memory>
26 namespace llvm {
27 namespace mca {
29 // This virtual dtor serves as the anchor for the CodeRegionGenerator class.
30 CodeRegionGenerator::~CodeRegionGenerator() {}
32 // A comment consumer that parses strings. The only valid tokens are strings.
33 class MCACommentConsumer : public AsmCommentConsumer {
34 public:
35 CodeRegions &Regions;
37 MCACommentConsumer(CodeRegions &R) : Regions(R) {}
38 void HandleComment(SMLoc Loc, StringRef CommentText) override;
41 // This class provides the callbacks that occur when parsing input assembly.
42 class MCStreamerWrapper final : public MCStreamer {
43 CodeRegions &Regions;
45 public:
46 MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R)
47 : MCStreamer(Context), Regions(R) {}
49 // We only want to intercept the emission of new instructions.
50 virtual void EmitInstruction(const MCInst &Inst,
51 const MCSubtargetInfo &/* unused */) override {
52 Regions.addInstruction(Inst);
55 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
56 return true;
59 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
60 unsigned ByteAlignment) override {}
61 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
62 uint64_t Size = 0, unsigned ByteAlignment = 0,
63 SMLoc Loc = SMLoc()) override {}
64 void EmitGPRel32Value(const MCExpr *Value) override {}
65 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
66 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
67 void EmitCOFFSymbolType(int Type) override {}
68 void EndCOFFSymbolDef() override {}
70 ArrayRef<MCInst> GetInstructionSequence(unsigned Index) const {
71 return Regions.getInstructionSequence(Index);
75 void MCACommentConsumer::HandleComment(SMLoc Loc, StringRef CommentText) {
76 // Skip empty comments.
77 StringRef Comment(CommentText);
78 if (Comment.empty())
79 return;
81 // Skip spaces and tabs.
82 unsigned Position = Comment.find_first_not_of(" \t");
83 if (Position >= Comment.size())
84 // We reached the end of the comment. Bail out.
85 return;
87 Comment = Comment.drop_front(Position);
88 if (Comment.consume_front("LLVM-MCA-END")) {
89 Regions.endRegion(Loc);
90 return;
93 // Try to parse the LLVM-MCA-BEGIN comment.
94 if (!Comment.consume_front("LLVM-MCA-BEGIN"))
95 return;
97 // Skip spaces and tabs.
98 Position = Comment.find_first_not_of(" \t");
99 if (Position < Comment.size())
100 Comment = Comment.drop_front(Position);
101 // Use the rest of the string as a descriptor for this code snippet.
102 Regions.beginRegion(Comment, Loc);
105 Expected<const CodeRegions &> AsmCodeRegionGenerator::parseCodeRegions() {
106 MCTargetOptions Opts;
107 Opts.PreserveAsmComments = false;
108 MCStreamerWrapper Str(Ctx, Regions);
110 // Create a MCAsmParser and setup the lexer to recognize llvm-mca ASM
111 // comments.
112 std::unique_ptr<MCAsmParser> Parser(
113 createMCAsmParser(Regions.getSourceMgr(), Ctx, Str, MAI));
114 MCAsmLexer &Lexer = Parser->getLexer();
115 MCACommentConsumer CC(Regions);
116 Lexer.setCommentConsumer(&CC);
118 // Create a target-specific parser and perform the parse.
119 std::unique_ptr<MCTargetAsmParser> TAP(
120 TheTarget.createMCAsmParser(STI, *Parser, MCII, Opts));
121 if (!TAP)
122 return make_error<StringError>(
123 "This target does not support assembly parsing.",
124 inconvertibleErrorCode());
125 Parser->setTargetParser(*TAP);
126 Parser->Run(false);
128 // Get the assembler dialect from the input. llvm-mca will use this as the
129 // default dialect when printing reports.
130 AssemblerDialect = Parser->getAssemblerDialect();
131 return Regions;
134 } // namespace mca
135 } // namespace llvm