1 //===-------------------------- CodeRegion.h -------------------*- C++ -* -===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements class CodeRegion and CodeRegions.
12 /// A CodeRegion describes a region of assembly code guarded by special LLVM-MCA
13 /// comment directives.
15 /// # LLVM-MCA-BEGIN foo
19 /// A comment starting with substring LLVM-MCA-BEGIN marks the beginning of a
20 /// new region of code.
21 /// A comment starting with substring LLVM-MCA-END marks the end of the
22 /// last-seen region of code.
24 /// Code regions are not allowed to overlap. Each region can have a optional
25 /// description; internally, regions are described by a range of source
26 /// locations (SMLoc objects).
28 /// An instruction (a MCInst) is added to a region R only if its location is in
29 /// range [R.RangeStart, R.RangeEnd].
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_H
34 #define LLVM_TOOLS_LLVM_MCA_CODEREGION_H
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/StringRef.h"
38 #include "llvm/MC/MCInst.h"
39 #include "llvm/Support/SMLoc.h"
40 #include "llvm/Support/SourceMgr.h"
46 /// A region of assembly code.
48 /// It identifies a sequence of machine instructions.
50 // An optional descriptor for this region.
51 llvm::StringRef Description
;
52 // Instructions that form this region.
53 std::vector
<llvm::MCInst
> Instructions
;
54 // Source location range.
55 llvm::SMLoc RangeStart
;
58 CodeRegion(const CodeRegion
&) = delete;
59 CodeRegion
&operator=(const CodeRegion
&) = delete;
62 CodeRegion(llvm::StringRef Desc
, llvm::SMLoc Start
)
63 : Description(Desc
), RangeStart(Start
), RangeEnd() {}
65 void addInstruction(const llvm::MCInst
&Instruction
) {
66 Instructions
.emplace_back(Instruction
);
69 llvm::SMLoc
startLoc() const { return RangeStart
; }
70 llvm::SMLoc
endLoc() const { return RangeEnd
; }
72 void setEndLocation(llvm::SMLoc End
) { RangeEnd
= End
; }
73 bool empty() const { return Instructions
.empty(); }
74 bool isLocInRange(llvm::SMLoc Loc
) const;
76 llvm::ArrayRef
<llvm::MCInst
> getInstructions() const { return Instructions
; }
78 llvm::StringRef
getDescription() const { return Description
; }
82 // A source manager. Used by the tool to generate meaningful warnings.
85 std::vector
<std::unique_ptr
<CodeRegion
>> Regions
;
87 // Construct a new region of code guarded by LLVM-MCA comments.
88 void addRegion(llvm::StringRef Description
, llvm::SMLoc Loc
) {
89 Regions
.emplace_back(llvm::make_unique
<CodeRegion
>(Description
, Loc
));
92 CodeRegions(const CodeRegions
&) = delete;
93 CodeRegions
&operator=(const CodeRegions
&) = delete;
96 typedef std::vector
<std::unique_ptr
<CodeRegion
>>::iterator iterator
;
97 typedef std::vector
<std::unique_ptr
<CodeRegion
>>::const_iterator
100 iterator
begin() { return Regions
.begin(); }
101 iterator
end() { return Regions
.end(); }
102 const_iterator
begin() const { return Regions
.cbegin(); }
103 const_iterator
end() const { return Regions
.cend(); }
105 void beginRegion(llvm::StringRef Description
, llvm::SMLoc Loc
);
106 void endRegion(llvm::SMLoc Loc
);
107 void addInstruction(const llvm::MCInst
&Instruction
);
108 llvm::SourceMgr
&getSourceMgr() const { return SM
; }
110 CodeRegions(llvm::SourceMgr
&S
) : SM(S
) {
111 // Create a default region for the input code sequence.
112 addRegion("Default", llvm::SMLoc());
115 llvm::ArrayRef
<llvm::MCInst
> getInstructionSequence(unsigned Idx
) const {
116 return Regions
[Idx
]->getInstructions();
120 return llvm::all_of(Regions
, [](const std::unique_ptr
<CodeRegion
> &Region
) {
121 return Region
->empty();