Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 1 | //===-------------------------- CodeRegion.h -------------------*- 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 | /// \file |
| 10 | /// |
| 11 | /// This file implements class CodeRegion and CodeRegions. |
| 12 | /// |
| 13 | /// A CodeRegion describes a region of assembly code guarded by special LLVM-MCA |
| 14 | /// comment directives. |
| 15 | /// |
| 16 | /// # LLVM-MCA-BEGIN foo |
| 17 | /// ... ## asm |
| 18 | /// # LLVM-MCA-END |
| 19 | /// |
| 20 | /// A comment starting with substring LLVM-MCA-BEGIN marks the beginning of a |
| 21 | /// new region of code. |
| 22 | /// A comment starting with substring LLVM-MCA-END marks the end of the |
| 23 | /// last-seen region of code. |
| 24 | /// |
| 25 | /// Code regions are not allowed to overlap. Each region can have a optional |
| 26 | /// description; internally, regions are described by a range of source |
| 27 | /// locations (SMLoc objects). |
| 28 | /// |
| 29 | /// An instruction (a MCInst) is added to a region R only if its location is in |
| 30 | /// range [R.RangeStart, R.RangeEnd]. |
| 31 | // |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_H |
| 35 | #define LLVM_TOOLS_LLVM_MCA_CODEREGION_H |
| 36 | |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/ArrayRef.h" |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/StringRef.h" |
| 39 | #include "llvm/MC/MCInst.h" |
| 40 | #include "llvm/Support/SMLoc.h" |
| 41 | #include "llvm/Support/SourceMgr.h" |
| 42 | #include <vector> |
| 43 | |
Fangrui Song | 467c307 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 44 | namespace llvm { |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 45 | namespace mca { |
| 46 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 47 | /// A region of assembly code. |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 48 | /// |
| 49 | /// It identifies a sequence of machine instructions. |
| 50 | class CodeRegion { |
| 51 | // An optional descriptor for this region. |
| 52 | llvm::StringRef Description; |
| 53 | // Instructions that form this region. |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 54 | std::vector<llvm::MCInst> Instructions; |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 55 | // Source location range. |
| 56 | llvm::SMLoc RangeStart; |
| 57 | llvm::SMLoc RangeEnd; |
| 58 | |
| 59 | CodeRegion(const CodeRegion &) = delete; |
| 60 | CodeRegion &operator=(const CodeRegion &) = delete; |
| 61 | |
| 62 | public: |
| 63 | CodeRegion(llvm::StringRef Desc, llvm::SMLoc Start) |
| 64 | : Description(Desc), RangeStart(Start), RangeEnd() {} |
| 65 | |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 66 | void addInstruction(const llvm::MCInst &Instruction) { |
| 67 | Instructions.emplace_back(Instruction); |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | llvm::SMLoc startLoc() const { return RangeStart; } |
| 71 | llvm::SMLoc endLoc() const { return RangeEnd; } |
| 72 | |
| 73 | void setEndLocation(llvm::SMLoc End) { RangeEnd = End; } |
| 74 | bool empty() const { return Instructions.empty(); } |
| 75 | bool isLocInRange(llvm::SMLoc Loc) const; |
| 76 | |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 77 | llvm::ArrayRef<llvm::MCInst> getInstructions() const { return Instructions; } |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 78 | |
| 79 | llvm::StringRef getDescription() const { return Description; } |
| 80 | }; |
| 81 | |
| 82 | class CodeRegions { |
| 83 | // A source manager. Used by the tool to generate meaningful warnings. |
| 84 | llvm::SourceMgr &SM; |
| 85 | |
| 86 | std::vector<std::unique_ptr<CodeRegion>> Regions; |
| 87 | |
| 88 | // Construct a new region of code guarded by LLVM-MCA comments. |
| 89 | void addRegion(llvm::StringRef Description, llvm::SMLoc Loc) { |
| 90 | Regions.emplace_back(llvm::make_unique<CodeRegion>(Description, Loc)); |
| 91 | } |
| 92 | |
| 93 | CodeRegions(const CodeRegions &) = delete; |
| 94 | CodeRegions &operator=(const CodeRegions &) = delete; |
| 95 | |
| 96 | public: |
| 97 | typedef std::vector<std::unique_ptr<CodeRegion>>::iterator iterator; |
| 98 | typedef std::vector<std::unique_ptr<CodeRegion>>::const_iterator |
| 99 | const_iterator; |
| 100 | |
| 101 | iterator begin() { return Regions.begin(); } |
| 102 | iterator end() { return Regions.end(); } |
| 103 | const_iterator begin() const { return Regions.cbegin(); } |
| 104 | const_iterator end() const { return Regions.cend(); } |
| 105 | |
| 106 | void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc); |
| 107 | void endRegion(llvm::SMLoc Loc); |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 108 | void addInstruction(const llvm::MCInst &Instruction); |
Matt Davis | 1969d26 | 2018-11-07 19:20:04 +0000 | [diff] [blame] | 109 | llvm::SourceMgr &getSourceMgr() const { return SM; } |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 110 | |
| 111 | CodeRegions(llvm::SourceMgr &S) : SM(S) { |
| 112 | // Create a default region for the input code sequence. |
| 113 | addRegion("Default", llvm::SMLoc()); |
| 114 | } |
| 115 | |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 116 | llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const { |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 117 | return Regions[Idx]->getInstructions(); |
| 118 | } |
| 119 | |
| 120 | bool empty() const { |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 121 | return llvm::all_of(Regions, [](const std::unique_ptr<CodeRegion> &Region) { |
| 122 | return Region->empty(); |
| 123 | }); |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 124 | } |
| 125 | }; |
| 126 | |
| 127 | } // namespace mca |
Fangrui Song | 467c307 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 128 | } // namespace llvm |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 129 | |
| 130 | #endif |