File indexing completed on 2026-05-10 08:44:26
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_SANDBOXIR_REGION_H
0010 #define LLVM_SANDBOXIR_REGION_H
0011
0012 #include <memory>
0013
0014 #include "llvm/ADT/SetVector.h"
0015 #include "llvm/ADT/iterator_range.h"
0016 #include "llvm/Analysis/TargetTransformInfo.h"
0017 #include "llvm/SandboxIR/Instruction.h"
0018 #include "llvm/Support/raw_ostream.h"
0019
0020 namespace llvm::sandboxir {
0021
0022 class Region;
0023
0024 class ScoreBoard {
0025 const Region &Rgn;
0026 TargetTransformInfo &TTI;
0027 constexpr static TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
0028
0029 InstructionCost AfterCost = 0;
0030
0031 InstructionCost BeforeCost = 0;
0032
0033 InstructionCost getCost(Instruction *I) const;
0034
0035 ScoreBoard(const ScoreBoard &) = delete;
0036 const ScoreBoard &operator=(const ScoreBoard &) = delete;
0037
0038 public:
0039 ScoreBoard(Region &Rgn, TargetTransformInfo &TTI) : Rgn(Rgn), TTI(TTI) {}
0040
0041 void add(Instruction *I) { AfterCost += getCost(I); }
0042
0043 void remove(Instruction *I);
0044
0045 InstructionCost getAfterCost() const { return AfterCost; }
0046
0047 InstructionCost getBeforeCost() const { return BeforeCost; }
0048
0049 #ifndef NDEBUG
0050 void dump(raw_ostream &OS) const {
0051 OS << "BeforeCost: " << BeforeCost << "\n";
0052 OS << "AfterCost: " << AfterCost << "\n";
0053 }
0054 LLVM_DUMP_METHOD void dump() const;
0055 #endif
0056 };
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091 class Region {
0092
0093
0094 SetVector<Instruction *> Insts;
0095
0096
0097 MDNode *RegionMDN;
0098 static constexpr const char *MDKind = "sandboxvec";
0099 static constexpr const char *RegionStr = "sandboxregion";
0100
0101 Context &Ctx;
0102
0103 ScoreBoard Scoreboard;
0104
0105
0106 Context::CallbackID CreateInstCB;
0107
0108 Context::CallbackID EraseInstCB;
0109
0110
0111
0112
0113 public:
0114 Region(Context &Ctx, TargetTransformInfo &TTI);
0115 ~Region();
0116
0117 Context &getContext() const { return Ctx; }
0118
0119
0120 void add(Instruction *I);
0121
0122 void remove(Instruction *I);
0123
0124 bool contains(Instruction *I) const { return Insts.contains(I); }
0125
0126 bool empty() const { return Insts.empty(); }
0127
0128 using iterator = decltype(Insts.begin());
0129 iterator begin() { return Insts.begin(); }
0130 iterator end() { return Insts.end(); }
0131 iterator_range<iterator> insts() { return make_range(begin(), end()); }
0132
0133 static SmallVector<std::unique_ptr<Region>>
0134 createRegionsFromMD(Function &F, TargetTransformInfo &TTI);
0135
0136 const ScoreBoard &getScoreboard() const { return Scoreboard; }
0137
0138 #ifndef NDEBUG
0139
0140 bool operator==(const Region &Other) const;
0141 bool operator!=(const Region &other) const { return !(*this == other); }
0142
0143 void dump(raw_ostream &OS) const;
0144 void dump() const;
0145 friend raw_ostream &operator<<(raw_ostream &OS, const Region &Rgn) {
0146 Rgn.dump(OS);
0147 return OS;
0148 }
0149 #endif
0150 };
0151
0152 }
0153
0154 #endif