blob: 1cdfeabfef4d3d2534f07a8ba17b9a396eedf036 [file] [log] [blame]
Andrea Di Biagioc6590122018-04-09 16:39:52 +00001//===-------------------------- CodeRegion.h -------------------*- C++ -* -===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Andrea Di Biagioc6590122018-04-09 16:39:52 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file implements class CodeRegion and CodeRegions.
11///
12/// A CodeRegion describes a region of assembly code guarded by special LLVM-MCA
13/// comment directives.
14///
15/// # LLVM-MCA-BEGIN foo
16/// ... ## asm
17/// # LLVM-MCA-END
18///
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.
23///
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).
27///
28/// An instruction (a MCInst) is added to a region R only if its location is in
29/// range [R.RangeStart, R.RangeEnd].
30//
31//===----------------------------------------------------------------------===//
32
33#ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_H
34#define LLVM_TOOLS_LLVM_MCA_CODEREGION_H
35
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000036#include "llvm/ADT/ArrayRef.h"
Andrea Di Biagioc6590122018-04-09 16:39:52 +000037#include "llvm/ADT/StringRef.h"
38#include "llvm/MC/MCInst.h"
39#include "llvm/Support/SMLoc.h"
40#include "llvm/Support/SourceMgr.h"
41#include <vector>
42
Fangrui Song5a8fd652018-10-30 15:56:08 +000043namespace llvm {
Andrea Di Biagioc6590122018-04-09 16:39:52 +000044namespace 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);
Matt Davis23f71062018-11-07 19:20:04 +0000108 llvm::SourceMgr &getSourceMgr() const { return SM; }
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000109
110 CodeRegions(llvm::SourceMgr &S) : SM(S) {
111 // Create a default region for the input code sequence.
112 addRegion("Default", llvm::SMLoc());
113 }
114
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000115 llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const {
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000116 return Regions[Idx]->getInstructions();
117 }
118
119 bool empty() const {
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +0000120 return llvm::all_of(Regions, [](const std::unique_ptr<CodeRegion> &Region) {
121 return Region->empty();
122 });
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000123 }
124};
125
126} // namespace mca
Fangrui Song5a8fd652018-10-30 15:56:08 +0000127} // namespace llvm
Andrea Di Biagioc6590122018-04-09 16:39:52 +0000128
129#endif