blob: 21ca8da9b5310ff2e2d6625735841455e9e3f088 [file] [log] [blame]
Andrea Di Biagioc6590122018-04-09 16:39:52 +00001//===-------------------------- 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 Biagio01b9fd62018-10-22 15:36:15 +000037#include "llvm/ADT/ArrayRef.h"
Andrea Di Biagioc6590122018-04-09 16:39:52 +000038#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
44namespace mca {
45
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000046/// A region of assembly code.
Andrea Di Biagioc6590122018-04-09 16:39:52 +000047///
48/// It identifies a sequence of machine instructions.
49class CodeRegion {
50 // An optional descriptor for this region.
51 llvm::StringRef Description;
52 // Instructions that form this region.
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000053 std::vector<llvm::MCInst> Instructions;
Andrea Di Biagioc6590122018-04-09 16:39:52 +000054 // Source location range.
55 llvm::SMLoc RangeStart;
56 llvm::SMLoc RangeEnd;
57
58 CodeRegion(const CodeRegion &) = delete;
59 CodeRegion &operator=(const CodeRegion &) = delete;
60
61public:
62 CodeRegion(llvm::StringRef Desc, llvm::SMLoc Start)
63 : Description(Desc), RangeStart(Start), RangeEnd() {}
64
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000065 void addInstruction(const llvm::MCInst &Instruction) {
66 Instructions.emplace_back(Instruction);
Andrea Di Biagioc6590122018-04-09 16:39:52 +000067 }
68
69 llvm::SMLoc startLoc() const { return RangeStart; }
70 llvm::SMLoc endLoc() const { return RangeEnd; }
71
72 void setEndLocation(llvm::SMLoc End) { RangeEnd = End; }
73 bool empty() const { return Instructions.empty(); }
74 bool isLocInRange(llvm::SMLoc Loc) const;
75
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000076 llvm::ArrayRef<llvm::MCInst> getInstructions() const { return Instructions; }
Andrea Di Biagioc6590122018-04-09 16:39:52 +000077
78 llvm::StringRef getDescription() const { return Description; }
79};
80
81class CodeRegions {
82 // A source manager. Used by the tool to generate meaningful warnings.
83 llvm::SourceMgr &SM;
84
85 std::vector<std::unique_ptr<CodeRegion>> Regions;
86
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));
90 }
91
92 CodeRegions(const CodeRegions &) = delete;
93 CodeRegions &operator=(const CodeRegions &) = delete;
94
95public:
96 typedef std::vector<std::unique_ptr<CodeRegion>>::iterator iterator;
97 typedef std::vector<std::unique_ptr<CodeRegion>>::const_iterator
98 const_iterator;
99
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(); }
104
105 void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc);
106 void endRegion(llvm::SMLoc Loc);
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000107 void addInstruction(const llvm::MCInst &Instruction);
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000108
109 CodeRegions(llvm::SourceMgr &S) : SM(S) {
110 // Create a default region for the input code sequence.
111 addRegion("Default", llvm::SMLoc());
112 }
113
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000114 llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000115 return Regions[Idx]->getInstructions();
116 }
117
118 bool empty() const {
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000119 return llvm::all_of(Regions, [](const std::unique_ptr<CodeRegion> &Region) {
120 return Region->empty();
121 });
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000122 }
123};
124
125} // namespace mca
126
127#endif