annotate llvm/lib/Target/Hexagon/BitTracker.h @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 2e18cbf3894f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- BitTracker.h ---------------------------------------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #ifndef LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H
anatofuz
parents:
diff changeset
10 #define LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 #include "llvm/ADT/DenseSet.h"
anatofuz
parents:
diff changeset
13 #include "llvm/ADT/SetVector.h"
anatofuz
parents:
diff changeset
14 #include "llvm/ADT/SmallVector.h"
anatofuz
parents:
diff changeset
15 #include "llvm/CodeGen/MachineInstr.h"
anatofuz
parents:
diff changeset
16 #include "llvm/CodeGen/MachineOperand.h"
anatofuz
parents:
diff changeset
17 #include <cassert>
anatofuz
parents:
diff changeset
18 #include <cstdint>
anatofuz
parents:
diff changeset
19 #include <map>
anatofuz
parents:
diff changeset
20 #include <queue>
anatofuz
parents:
diff changeset
21 #include <set>
anatofuz
parents:
diff changeset
22 #include <utility>
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 namespace llvm {
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 class BitVector;
anatofuz
parents:
diff changeset
27 class ConstantInt;
anatofuz
parents:
diff changeset
28 class MachineRegisterInfo;
anatofuz
parents:
diff changeset
29 class MachineBasicBlock;
anatofuz
parents:
diff changeset
30 class MachineFunction;
anatofuz
parents:
diff changeset
31 class raw_ostream;
anatofuz
parents:
diff changeset
32 class TargetRegisterClass;
anatofuz
parents:
diff changeset
33 class TargetRegisterInfo;
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 struct BitTracker {
anatofuz
parents:
diff changeset
36 struct BitRef;
anatofuz
parents:
diff changeset
37 struct RegisterRef;
anatofuz
parents:
diff changeset
38 struct BitValue;
anatofuz
parents:
diff changeset
39 struct BitMask;
anatofuz
parents:
diff changeset
40 struct RegisterCell;
anatofuz
parents:
diff changeset
41 struct MachineEvaluator;
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 using BranchTargetList = SetVector<const MachineBasicBlock *>;
anatofuz
parents:
diff changeset
44 using CellMapType = std::map<unsigned, RegisterCell>;
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 BitTracker(const MachineEvaluator &E, MachineFunction &F);
anatofuz
parents:
diff changeset
47 ~BitTracker();
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 void run();
anatofuz
parents:
diff changeset
50 void trace(bool On = false) { Trace = On; }
anatofuz
parents:
diff changeset
51 bool has(unsigned Reg) const;
anatofuz
parents:
diff changeset
52 const RegisterCell &lookup(unsigned Reg) const;
anatofuz
parents:
diff changeset
53 RegisterCell get(RegisterRef RR) const;
anatofuz
parents:
diff changeset
54 void put(RegisterRef RR, const RegisterCell &RC);
anatofuz
parents:
diff changeset
55 void subst(RegisterRef OldRR, RegisterRef NewRR);
anatofuz
parents:
diff changeset
56 bool reached(const MachineBasicBlock *B) const;
anatofuz
parents:
diff changeset
57 void visit(const MachineInstr &MI);
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 void print_cells(raw_ostream &OS) const;
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 private:
anatofuz
parents:
diff changeset
62 void visitPHI(const MachineInstr &PI);
anatofuz
parents:
diff changeset
63 void visitNonBranch(const MachineInstr &MI);
anatofuz
parents:
diff changeset
64 void visitBranchesFrom(const MachineInstr &BI);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
65 void visitUsesOf(Register Reg);
150
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 using CFGEdge = std::pair<int, int>;
anatofuz
parents:
diff changeset
68 using EdgeSetType = std::set<CFGEdge>;
anatofuz
parents:
diff changeset
69 using InstrSetType = std::set<const MachineInstr *>;
anatofuz
parents:
diff changeset
70 using EdgeQueueType = std::queue<CFGEdge>;
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 // Priority queue of instructions using modified registers, ordered by
anatofuz
parents:
diff changeset
73 // their relative position in a basic block.
anatofuz
parents:
diff changeset
74 struct UseQueueType {
anatofuz
parents:
diff changeset
75 UseQueueType() : Uses(Dist) {}
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 unsigned size() const {
anatofuz
parents:
diff changeset
78 return Uses.size();
anatofuz
parents:
diff changeset
79 }
anatofuz
parents:
diff changeset
80 bool empty() const {
anatofuz
parents:
diff changeset
81 return size() == 0;
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83 MachineInstr *front() const {
anatofuz
parents:
diff changeset
84 return Uses.top();
anatofuz
parents:
diff changeset
85 }
anatofuz
parents:
diff changeset
86 void push(MachineInstr *MI) {
anatofuz
parents:
diff changeset
87 if (Set.insert(MI).second)
anatofuz
parents:
diff changeset
88 Uses.push(MI);
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90 void pop() {
anatofuz
parents:
diff changeset
91 Set.erase(front());
anatofuz
parents:
diff changeset
92 Uses.pop();
anatofuz
parents:
diff changeset
93 }
anatofuz
parents:
diff changeset
94 void reset() {
anatofuz
parents:
diff changeset
95 Dist.clear();
anatofuz
parents:
diff changeset
96 }
anatofuz
parents:
diff changeset
97 private:
anatofuz
parents:
diff changeset
98 struct Cmp {
anatofuz
parents:
diff changeset
99 Cmp(DenseMap<const MachineInstr*,unsigned> &Map) : Dist(Map) {}
anatofuz
parents:
diff changeset
100 bool operator()(const MachineInstr *MI, const MachineInstr *MJ) const;
anatofuz
parents:
diff changeset
101 DenseMap<const MachineInstr*,unsigned> &Dist;
anatofuz
parents:
diff changeset
102 };
anatofuz
parents:
diff changeset
103 std::priority_queue<MachineInstr*, std::vector<MachineInstr*>, Cmp> Uses;
anatofuz
parents:
diff changeset
104 DenseSet<const MachineInstr*> Set; // Set to avoid adding duplicate entries.
anatofuz
parents:
diff changeset
105 DenseMap<const MachineInstr*,unsigned> Dist;
anatofuz
parents:
diff changeset
106 };
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 void reset();
anatofuz
parents:
diff changeset
109 void runEdgeQueue(BitVector &BlockScanned);
anatofuz
parents:
diff changeset
110 void runUseQueue();
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 const MachineEvaluator &ME;
anatofuz
parents:
diff changeset
113 MachineFunction &MF;
anatofuz
parents:
diff changeset
114 MachineRegisterInfo &MRI;
anatofuz
parents:
diff changeset
115 CellMapType &Map;
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 EdgeSetType EdgeExec; // Executable flow graph edges.
anatofuz
parents:
diff changeset
118 InstrSetType InstrExec; // Executable instructions.
anatofuz
parents:
diff changeset
119 UseQueueType UseQ; // Work queue of register uses.
anatofuz
parents:
diff changeset
120 EdgeQueueType FlowQ; // Work queue of CFG edges.
anatofuz
parents:
diff changeset
121 DenseSet<unsigned> ReachedBB; // Cache of reached blocks.
anatofuz
parents:
diff changeset
122 bool Trace; // Enable tracing for debugging.
anatofuz
parents:
diff changeset
123 };
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 // Abstraction of a reference to bit at position Pos from a register Reg.
anatofuz
parents:
diff changeset
126 struct BitTracker::BitRef {
anatofuz
parents:
diff changeset
127 BitRef(unsigned R = 0, uint16_t P = 0) : Reg(R), Pos(P) {}
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 bool operator== (const BitRef &BR) const {
anatofuz
parents:
diff changeset
130 // If Reg is 0, disregard Pos.
anatofuz
parents:
diff changeset
131 return Reg == BR.Reg && (Reg == 0 || Pos == BR.Pos);
anatofuz
parents:
diff changeset
132 }
anatofuz
parents:
diff changeset
133
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
134 Register Reg;
150
anatofuz
parents:
diff changeset
135 uint16_t Pos;
anatofuz
parents:
diff changeset
136 };
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 // Abstraction of a register reference in MachineOperand. It contains the
anatofuz
parents:
diff changeset
139 // register number and the subregister index.
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
140 // FIXME: Consolidate duplicate definitions of RegisterRef
150
anatofuz
parents:
diff changeset
141 struct BitTracker::RegisterRef {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
142 RegisterRef(Register R = 0, unsigned S = 0) : Reg(R), Sub(S) {}
150
anatofuz
parents:
diff changeset
143 RegisterRef(const MachineOperand &MO)
anatofuz
parents:
diff changeset
144 : Reg(MO.getReg()), Sub(MO.getSubReg()) {}
anatofuz
parents:
diff changeset
145
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
146 Register Reg;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
147 unsigned Sub;
150
anatofuz
parents:
diff changeset
148 };
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 // Value that a single bit can take. This is outside of the context of
anatofuz
parents:
diff changeset
151 // any register, it is more of an abstraction of the two-element set of
anatofuz
parents:
diff changeset
152 // possible bit values. One extension here is the "Ref" type, which
anatofuz
parents:
diff changeset
153 // indicates that this bit takes the same value as the bit described by
anatofuz
parents:
diff changeset
154 // RefInfo.
anatofuz
parents:
diff changeset
155 struct BitTracker::BitValue {
anatofuz
parents:
diff changeset
156 enum ValueType {
anatofuz
parents:
diff changeset
157 Top, // Bit not yet defined.
anatofuz
parents:
diff changeset
158 Zero, // Bit = 0.
anatofuz
parents:
diff changeset
159 One, // Bit = 1.
anatofuz
parents:
diff changeset
160 Ref // Bit value same as the one described in RefI.
anatofuz
parents:
diff changeset
161 // Conceptually, there is no explicit "bottom" value: the lattice's
anatofuz
parents:
diff changeset
162 // bottom will be expressed as a "ref to itself", which, in the context
anatofuz
parents:
diff changeset
163 // of registers, could be read as "this value of this bit is defined by
anatofuz
parents:
diff changeset
164 // this bit".
anatofuz
parents:
diff changeset
165 // The ordering is:
anatofuz
parents:
diff changeset
166 // x <= Top,
anatofuz
parents:
diff changeset
167 // Self <= x, where "Self" is "ref to itself".
anatofuz
parents:
diff changeset
168 // This makes the value lattice different for each virtual register
anatofuz
parents:
diff changeset
169 // (even for each bit in the same virtual register), since the "bottom"
anatofuz
parents:
diff changeset
170 // for one register will be a simple "ref" for another register.
anatofuz
parents:
diff changeset
171 // Since we do not store the "Self" bit and register number, the meet
anatofuz
parents:
diff changeset
172 // operation will need to take it as a parameter.
anatofuz
parents:
diff changeset
173 //
anatofuz
parents:
diff changeset
174 // In practice there is a special case for values that are not associa-
anatofuz
parents:
diff changeset
175 // ted with any specific virtual register. An example would be a value
anatofuz
parents:
diff changeset
176 // corresponding to a bit of a physical register, or an intermediate
anatofuz
parents:
diff changeset
177 // value obtained in some computation (such as instruction evaluation).
anatofuz
parents:
diff changeset
178 // Such cases are identical to the usual Ref type, but the register
anatofuz
parents:
diff changeset
179 // number is 0. In such case the Pos field of the reference is ignored.
anatofuz
parents:
diff changeset
180 //
anatofuz
parents:
diff changeset
181 // What is worthy of notice is that in value V (that is a "ref"), as long
anatofuz
parents:
diff changeset
182 // as the RefI.Reg is not 0, it may actually be the same register as the
anatofuz
parents:
diff changeset
183 // one in which V will be contained. If the RefI.Pos refers to the posi-
anatofuz
parents:
diff changeset
184 // tion of V, then V is assumed to be "bottom" (as a "ref to itself"),
anatofuz
parents:
diff changeset
185 // otherwise V is taken to be identical to the referenced bit of the
anatofuz
parents:
diff changeset
186 // same register.
anatofuz
parents:
diff changeset
187 // If RefI.Reg is 0, however, such a reference to the same register is
anatofuz
parents:
diff changeset
188 // not possible. Any value V that is a "ref", and whose RefI.Reg is 0
anatofuz
parents:
diff changeset
189 // is treated as "bottom".
anatofuz
parents:
diff changeset
190 };
anatofuz
parents:
diff changeset
191 ValueType Type;
anatofuz
parents:
diff changeset
192 BitRef RefI;
anatofuz
parents:
diff changeset
193
anatofuz
parents:
diff changeset
194 BitValue(ValueType T = Top) : Type(T) {}
anatofuz
parents:
diff changeset
195 BitValue(bool B) : Type(B ? One : Zero) {}
anatofuz
parents:
diff changeset
196 BitValue(unsigned Reg, uint16_t Pos) : Type(Ref), RefI(Reg, Pos) {}
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 bool operator== (const BitValue &V) const {
anatofuz
parents:
diff changeset
199 if (Type != V.Type)
anatofuz
parents:
diff changeset
200 return false;
anatofuz
parents:
diff changeset
201 if (Type == Ref && !(RefI == V.RefI))
anatofuz
parents:
diff changeset
202 return false;
anatofuz
parents:
diff changeset
203 return true;
anatofuz
parents:
diff changeset
204 }
anatofuz
parents:
diff changeset
205 bool operator!= (const BitValue &V) const {
anatofuz
parents:
diff changeset
206 return !operator==(V);
anatofuz
parents:
diff changeset
207 }
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 bool is(unsigned T) const {
anatofuz
parents:
diff changeset
210 assert(T == 0 || T == 1);
anatofuz
parents:
diff changeset
211 return T == 0 ? Type == Zero
anatofuz
parents:
diff changeset
212 : (T == 1 ? Type == One : false);
anatofuz
parents:
diff changeset
213 }
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 // The "meet" operation is the "." operation in a semilattice (L, ., T, B):
anatofuz
parents:
diff changeset
216 // (1) x.x = x
anatofuz
parents:
diff changeset
217 // (2) x.y = y.x
anatofuz
parents:
diff changeset
218 // (3) x.(y.z) = (x.y).z
anatofuz
parents:
diff changeset
219 // (4) x.T = x (i.e. T = "top")
anatofuz
parents:
diff changeset
220 // (5) x.B = B (i.e. B = "bottom")
anatofuz
parents:
diff changeset
221 //
anatofuz
parents:
diff changeset
222 // This "meet" function will update the value of the "*this" object with
anatofuz
parents:
diff changeset
223 // the newly calculated one, and return "true" if the value of *this has
anatofuz
parents:
diff changeset
224 // changed, and "false" otherwise.
anatofuz
parents:
diff changeset
225 // To prove that it satisfies the conditions (1)-(5), it is sufficient
anatofuz
parents:
diff changeset
226 // to show that a relation
anatofuz
parents:
diff changeset
227 // x <= y <=> x.y = x
anatofuz
parents:
diff changeset
228 // defines a partial order (i.e. that "meet" is same as "infimum").
anatofuz
parents:
diff changeset
229 bool meet(const BitValue &V, const BitRef &Self) {
anatofuz
parents:
diff changeset
230 // First, check the cases where there is nothing to be done.
anatofuz
parents:
diff changeset
231 if (Type == Ref && RefI == Self) // Bottom.meet(V) = Bottom (i.e. This)
anatofuz
parents:
diff changeset
232 return false;
anatofuz
parents:
diff changeset
233 if (V.Type == Top) // This.meet(Top) = This
anatofuz
parents:
diff changeset
234 return false;
anatofuz
parents:
diff changeset
235 if (*this == V) // This.meet(This) = This
anatofuz
parents:
diff changeset
236 return false;
anatofuz
parents:
diff changeset
237
anatofuz
parents:
diff changeset
238 // At this point, we know that the value of "this" will change.
anatofuz
parents:
diff changeset
239 // If it is Top, it will become the same as V, otherwise it will
anatofuz
parents:
diff changeset
240 // become "bottom" (i.e. Self).
anatofuz
parents:
diff changeset
241 if (Type == Top) {
anatofuz
parents:
diff changeset
242 Type = V.Type;
anatofuz
parents:
diff changeset
243 RefI = V.RefI; // This may be irrelevant, but copy anyway.
anatofuz
parents:
diff changeset
244 return true;
anatofuz
parents:
diff changeset
245 }
anatofuz
parents:
diff changeset
246 // Become "bottom".
anatofuz
parents:
diff changeset
247 Type = Ref;
anatofuz
parents:
diff changeset
248 RefI = Self;
anatofuz
parents:
diff changeset
249 return true;
anatofuz
parents:
diff changeset
250 }
anatofuz
parents:
diff changeset
251
anatofuz
parents:
diff changeset
252 // Create a reference to the bit value V.
anatofuz
parents:
diff changeset
253 static BitValue ref(const BitValue &V);
anatofuz
parents:
diff changeset
254 // Create a "self".
anatofuz
parents:
diff changeset
255 static BitValue self(const BitRef &Self = BitRef());
anatofuz
parents:
diff changeset
256
anatofuz
parents:
diff changeset
257 bool num() const {
anatofuz
parents:
diff changeset
258 return Type == Zero || Type == One;
anatofuz
parents:
diff changeset
259 }
anatofuz
parents:
diff changeset
260
anatofuz
parents:
diff changeset
261 operator bool() const {
anatofuz
parents:
diff changeset
262 assert(Type == Zero || Type == One);
anatofuz
parents:
diff changeset
263 return Type == One;
anatofuz
parents:
diff changeset
264 }
anatofuz
parents:
diff changeset
265
anatofuz
parents:
diff changeset
266 friend raw_ostream &operator<<(raw_ostream &OS, const BitValue &BV);
anatofuz
parents:
diff changeset
267 };
anatofuz
parents:
diff changeset
268
anatofuz
parents:
diff changeset
269 // This operation must be idempotent, i.e. ref(ref(V)) == ref(V).
anatofuz
parents:
diff changeset
270 inline BitTracker::BitValue
anatofuz
parents:
diff changeset
271 BitTracker::BitValue::ref(const BitValue &V) {
anatofuz
parents:
diff changeset
272 if (V.Type != Ref)
anatofuz
parents:
diff changeset
273 return BitValue(V.Type);
anatofuz
parents:
diff changeset
274 if (V.RefI.Reg != 0)
anatofuz
parents:
diff changeset
275 return BitValue(V.RefI.Reg, V.RefI.Pos);
anatofuz
parents:
diff changeset
276 return self();
anatofuz
parents:
diff changeset
277 }
anatofuz
parents:
diff changeset
278
anatofuz
parents:
diff changeset
279 inline BitTracker::BitValue
anatofuz
parents:
diff changeset
280 BitTracker::BitValue::self(const BitRef &Self) {
anatofuz
parents:
diff changeset
281 return BitValue(Self.Reg, Self.Pos);
anatofuz
parents:
diff changeset
282 }
anatofuz
parents:
diff changeset
283
anatofuz
parents:
diff changeset
284 // A sequence of bits starting from index B up to and including index E.
anatofuz
parents:
diff changeset
285 // If E < B, the mask represents two sections: [0..E] and [B..W) where
anatofuz
parents:
diff changeset
286 // W is the width of the register.
anatofuz
parents:
diff changeset
287 struct BitTracker::BitMask {
anatofuz
parents:
diff changeset
288 BitMask() = default;
anatofuz
parents:
diff changeset
289 BitMask(uint16_t b, uint16_t e) : B(b), E(e) {}
anatofuz
parents:
diff changeset
290
anatofuz
parents:
diff changeset
291 uint16_t first() const { return B; }
anatofuz
parents:
diff changeset
292 uint16_t last() const { return E; }
anatofuz
parents:
diff changeset
293
anatofuz
parents:
diff changeset
294 private:
anatofuz
parents:
diff changeset
295 uint16_t B = 0;
anatofuz
parents:
diff changeset
296 uint16_t E = 0;
anatofuz
parents:
diff changeset
297 };
anatofuz
parents:
diff changeset
298
anatofuz
parents:
diff changeset
299 // Representation of a register: a list of BitValues.
anatofuz
parents:
diff changeset
300 struct BitTracker::RegisterCell {
anatofuz
parents:
diff changeset
301 RegisterCell(uint16_t Width = DefaultBitN) : Bits(Width) {}
anatofuz
parents:
diff changeset
302
anatofuz
parents:
diff changeset
303 uint16_t width() const {
anatofuz
parents:
diff changeset
304 return Bits.size();
anatofuz
parents:
diff changeset
305 }
anatofuz
parents:
diff changeset
306
anatofuz
parents:
diff changeset
307 const BitValue &operator[](uint16_t BitN) const {
anatofuz
parents:
diff changeset
308 assert(BitN < Bits.size());
anatofuz
parents:
diff changeset
309 return Bits[BitN];
anatofuz
parents:
diff changeset
310 }
anatofuz
parents:
diff changeset
311 BitValue &operator[](uint16_t BitN) {
anatofuz
parents:
diff changeset
312 assert(BitN < Bits.size());
anatofuz
parents:
diff changeset
313 return Bits[BitN];
anatofuz
parents:
diff changeset
314 }
anatofuz
parents:
diff changeset
315
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
316 bool meet(const RegisterCell &RC, Register SelfR);
150
anatofuz
parents:
diff changeset
317 RegisterCell &insert(const RegisterCell &RC, const BitMask &M);
anatofuz
parents:
diff changeset
318 RegisterCell extract(const BitMask &M) const; // Returns a new cell.
anatofuz
parents:
diff changeset
319 RegisterCell &rol(uint16_t Sh); // Rotate left.
anatofuz
parents:
diff changeset
320 RegisterCell &fill(uint16_t B, uint16_t E, const BitValue &V);
anatofuz
parents:
diff changeset
321 RegisterCell &cat(const RegisterCell &RC); // Concatenate.
anatofuz
parents:
diff changeset
322 uint16_t cl(bool B) const;
anatofuz
parents:
diff changeset
323 uint16_t ct(bool B) const;
anatofuz
parents:
diff changeset
324
anatofuz
parents:
diff changeset
325 bool operator== (const RegisterCell &RC) const;
anatofuz
parents:
diff changeset
326 bool operator!= (const RegisterCell &RC) const {
anatofuz
parents:
diff changeset
327 return !operator==(RC);
anatofuz
parents:
diff changeset
328 }
anatofuz
parents:
diff changeset
329
anatofuz
parents:
diff changeset
330 // Replace the ref-to-reg-0 bit values with the given register.
anatofuz
parents:
diff changeset
331 RegisterCell &regify(unsigned R);
anatofuz
parents:
diff changeset
332
anatofuz
parents:
diff changeset
333 // Generate a "ref" cell for the corresponding register. In the resulting
anatofuz
parents:
diff changeset
334 // cell each bit will be described as being the same as the corresponding
anatofuz
parents:
diff changeset
335 // bit in register Reg (i.e. the cell is "defined" by register Reg).
anatofuz
parents:
diff changeset
336 static RegisterCell self(unsigned Reg, uint16_t Width);
anatofuz
parents:
diff changeset
337 // Generate a "top" cell of given size.
anatofuz
parents:
diff changeset
338 static RegisterCell top(uint16_t Width);
anatofuz
parents:
diff changeset
339 // Generate a cell that is a "ref" to another cell.
anatofuz
parents:
diff changeset
340 static RegisterCell ref(const RegisterCell &C);
anatofuz
parents:
diff changeset
341
anatofuz
parents:
diff changeset
342 private:
anatofuz
parents:
diff changeset
343 // The DefaultBitN is here only to avoid frequent reallocation of the
anatofuz
parents:
diff changeset
344 // memory in the vector.
anatofuz
parents:
diff changeset
345 static const unsigned DefaultBitN = 32;
anatofuz
parents:
diff changeset
346 using BitValueList = SmallVector<BitValue, DefaultBitN>;
anatofuz
parents:
diff changeset
347 BitValueList Bits;
anatofuz
parents:
diff changeset
348
anatofuz
parents:
diff changeset
349 friend raw_ostream &operator<<(raw_ostream &OS, const RegisterCell &RC);
anatofuz
parents:
diff changeset
350 };
anatofuz
parents:
diff changeset
351
anatofuz
parents:
diff changeset
352 inline bool BitTracker::has(unsigned Reg) const {
anatofuz
parents:
diff changeset
353 return Map.find(Reg) != Map.end();
anatofuz
parents:
diff changeset
354 }
anatofuz
parents:
diff changeset
355
anatofuz
parents:
diff changeset
356 inline const BitTracker::RegisterCell&
anatofuz
parents:
diff changeset
357 BitTracker::lookup(unsigned Reg) const {
anatofuz
parents:
diff changeset
358 CellMapType::const_iterator F = Map.find(Reg);
anatofuz
parents:
diff changeset
359 assert(F != Map.end());
anatofuz
parents:
diff changeset
360 return F->second;
anatofuz
parents:
diff changeset
361 }
anatofuz
parents:
diff changeset
362
anatofuz
parents:
diff changeset
363 inline BitTracker::RegisterCell
anatofuz
parents:
diff changeset
364 BitTracker::RegisterCell::self(unsigned Reg, uint16_t Width) {
anatofuz
parents:
diff changeset
365 RegisterCell RC(Width);
anatofuz
parents:
diff changeset
366 for (uint16_t i = 0; i < Width; ++i)
anatofuz
parents:
diff changeset
367 RC.Bits[i] = BitValue::self(BitRef(Reg, i));
anatofuz
parents:
diff changeset
368 return RC;
anatofuz
parents:
diff changeset
369 }
anatofuz
parents:
diff changeset
370
anatofuz
parents:
diff changeset
371 inline BitTracker::RegisterCell
anatofuz
parents:
diff changeset
372 BitTracker::RegisterCell::top(uint16_t Width) {
anatofuz
parents:
diff changeset
373 RegisterCell RC(Width);
anatofuz
parents:
diff changeset
374 for (uint16_t i = 0; i < Width; ++i)
anatofuz
parents:
diff changeset
375 RC.Bits[i] = BitValue(BitValue::Top);
anatofuz
parents:
diff changeset
376 return RC;
anatofuz
parents:
diff changeset
377 }
anatofuz
parents:
diff changeset
378
anatofuz
parents:
diff changeset
379 inline BitTracker::RegisterCell
anatofuz
parents:
diff changeset
380 BitTracker::RegisterCell::ref(const RegisterCell &C) {
anatofuz
parents:
diff changeset
381 uint16_t W = C.width();
anatofuz
parents:
diff changeset
382 RegisterCell RC(W);
anatofuz
parents:
diff changeset
383 for (unsigned i = 0; i < W; ++i)
anatofuz
parents:
diff changeset
384 RC[i] = BitValue::ref(C[i]);
anatofuz
parents:
diff changeset
385 return RC;
anatofuz
parents:
diff changeset
386 }
anatofuz
parents:
diff changeset
387
anatofuz
parents:
diff changeset
388 // A class to evaluate target's instructions and update the cell maps.
anatofuz
parents:
diff changeset
389 // This is used internally by the bit tracker. A target that wants to
anatofuz
parents:
diff changeset
390 // utilize this should implement the evaluation functions (noted below)
anatofuz
parents:
diff changeset
391 // in a subclass of this class.
anatofuz
parents:
diff changeset
392 struct BitTracker::MachineEvaluator {
anatofuz
parents:
diff changeset
393 MachineEvaluator(const TargetRegisterInfo &T, MachineRegisterInfo &M)
anatofuz
parents:
diff changeset
394 : TRI(T), MRI(M) {}
anatofuz
parents:
diff changeset
395 virtual ~MachineEvaluator() = default;
anatofuz
parents:
diff changeset
396
anatofuz
parents:
diff changeset
397 uint16_t getRegBitWidth(const RegisterRef &RR) const;
anatofuz
parents:
diff changeset
398
anatofuz
parents:
diff changeset
399 RegisterCell getCell(const RegisterRef &RR, const CellMapType &M) const;
anatofuz
parents:
diff changeset
400 void putCell(const RegisterRef &RR, RegisterCell RC, CellMapType &M) const;
anatofuz
parents:
diff changeset
401
anatofuz
parents:
diff changeset
402 // A result of any operation should use refs to the source cells, not
anatofuz
parents:
diff changeset
403 // the cells directly. This function is a convenience wrapper to quickly
anatofuz
parents:
diff changeset
404 // generate a ref for a cell corresponding to a register reference.
anatofuz
parents:
diff changeset
405 RegisterCell getRef(const RegisterRef &RR, const CellMapType &M) const {
anatofuz
parents:
diff changeset
406 RegisterCell RC = getCell(RR, M);
anatofuz
parents:
diff changeset
407 return RegisterCell::ref(RC);
anatofuz
parents:
diff changeset
408 }
anatofuz
parents:
diff changeset
409
anatofuz
parents:
diff changeset
410 // Helper functions.
anatofuz
parents:
diff changeset
411 // Check if a cell is an immediate value (i.e. all bits are either 0 or 1).
anatofuz
parents:
diff changeset
412 bool isInt(const RegisterCell &A) const;
anatofuz
parents:
diff changeset
413 // Convert cell to an immediate value.
anatofuz
parents:
diff changeset
414 uint64_t toInt(const RegisterCell &A) const;
anatofuz
parents:
diff changeset
415
anatofuz
parents:
diff changeset
416 // Generate cell from an immediate value.
anatofuz
parents:
diff changeset
417 RegisterCell eIMM(int64_t V, uint16_t W) const;
anatofuz
parents:
diff changeset
418 RegisterCell eIMM(const ConstantInt *CI) const;
anatofuz
parents:
diff changeset
419
anatofuz
parents:
diff changeset
420 // Arithmetic.
anatofuz
parents:
diff changeset
421 RegisterCell eADD(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
422 RegisterCell eSUB(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
423 RegisterCell eMLS(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
424 RegisterCell eMLU(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
425
anatofuz
parents:
diff changeset
426 // Shifts.
anatofuz
parents:
diff changeset
427 RegisterCell eASL(const RegisterCell &A1, uint16_t Sh) const;
anatofuz
parents:
diff changeset
428 RegisterCell eLSR(const RegisterCell &A1, uint16_t Sh) const;
anatofuz
parents:
diff changeset
429 RegisterCell eASR(const RegisterCell &A1, uint16_t Sh) const;
anatofuz
parents:
diff changeset
430
anatofuz
parents:
diff changeset
431 // Logical.
anatofuz
parents:
diff changeset
432 RegisterCell eAND(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
433 RegisterCell eORL(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
434 RegisterCell eXOR(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
435 RegisterCell eNOT(const RegisterCell &A1) const;
anatofuz
parents:
diff changeset
436
anatofuz
parents:
diff changeset
437 // Set bit, clear bit.
anatofuz
parents:
diff changeset
438 RegisterCell eSET(const RegisterCell &A1, uint16_t BitN) const;
anatofuz
parents:
diff changeset
439 RegisterCell eCLR(const RegisterCell &A1, uint16_t BitN) const;
anatofuz
parents:
diff changeset
440
anatofuz
parents:
diff changeset
441 // Count leading/trailing bits (zeros/ones).
anatofuz
parents:
diff changeset
442 RegisterCell eCLB(const RegisterCell &A1, bool B, uint16_t W) const;
anatofuz
parents:
diff changeset
443 RegisterCell eCTB(const RegisterCell &A1, bool B, uint16_t W) const;
anatofuz
parents:
diff changeset
444
anatofuz
parents:
diff changeset
445 // Sign/zero extension.
anatofuz
parents:
diff changeset
446 RegisterCell eSXT(const RegisterCell &A1, uint16_t FromN) const;
anatofuz
parents:
diff changeset
447 RegisterCell eZXT(const RegisterCell &A1, uint16_t FromN) const;
anatofuz
parents:
diff changeset
448
anatofuz
parents:
diff changeset
449 // Extract/insert
anatofuz
parents:
diff changeset
450 // XTR R,b,e: extract bits from A1 starting at bit b, ending at e-1.
anatofuz
parents:
diff changeset
451 // INS R,S,b: take R and replace bits starting from b with S.
anatofuz
parents:
diff changeset
452 RegisterCell eXTR(const RegisterCell &A1, uint16_t B, uint16_t E) const;
anatofuz
parents:
diff changeset
453 RegisterCell eINS(const RegisterCell &A1, const RegisterCell &A2,
anatofuz
parents:
diff changeset
454 uint16_t AtN) const;
anatofuz
parents:
diff changeset
455
anatofuz
parents:
diff changeset
456 // User-provided functions for individual targets:
anatofuz
parents:
diff changeset
457
anatofuz
parents:
diff changeset
458 // Return a sub-register mask that indicates which bits in Reg belong
anatofuz
parents:
diff changeset
459 // to the subregister Sub. These bits are assumed to be contiguous in
anatofuz
parents:
diff changeset
460 // the super-register, and have the same ordering in the sub-register
anatofuz
parents:
diff changeset
461 // as in the super-register. It is valid to call this function with
anatofuz
parents:
diff changeset
462 // Sub == 0, in this case, the function should return a mask that spans
anatofuz
parents:
diff changeset
463 // the entire register Reg (which is what the default implementation
anatofuz
parents:
diff changeset
464 // does).
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
465 virtual BitMask mask(Register Reg, unsigned Sub) const;
150
anatofuz
parents:
diff changeset
466 // Indicate whether a given register class should be tracked.
anatofuz
parents:
diff changeset
467 virtual bool track(const TargetRegisterClass *RC) const { return true; }
anatofuz
parents:
diff changeset
468 // Evaluate a non-branching machine instruction, given the cell map with
anatofuz
parents:
diff changeset
469 // the input values. Place the results in the Outputs map. Return "true"
anatofuz
parents:
diff changeset
470 // if evaluation succeeded, "false" otherwise.
anatofuz
parents:
diff changeset
471 virtual bool evaluate(const MachineInstr &MI, const CellMapType &Inputs,
anatofuz
parents:
diff changeset
472 CellMapType &Outputs) const;
anatofuz
parents:
diff changeset
473 // Evaluate a branch, given the cell map with the input values. Fill out
anatofuz
parents:
diff changeset
474 // a list of all possible branch targets and indicate (through a flag)
anatofuz
parents:
diff changeset
475 // whether the branch could fall-through. Return "true" if this information
anatofuz
parents:
diff changeset
476 // has been successfully computed, "false" otherwise.
anatofuz
parents:
diff changeset
477 virtual bool evaluate(const MachineInstr &BI, const CellMapType &Inputs,
anatofuz
parents:
diff changeset
478 BranchTargetList &Targets, bool &FallsThru) const = 0;
anatofuz
parents:
diff changeset
479 // Given a register class RC, return a register class that should be assumed
anatofuz
parents:
diff changeset
480 // when a register from class RC is used with a subregister of index Idx.
anatofuz
parents:
diff changeset
481 virtual const TargetRegisterClass&
anatofuz
parents:
diff changeset
482 composeWithSubRegIndex(const TargetRegisterClass &RC, unsigned Idx) const {
anatofuz
parents:
diff changeset
483 if (Idx == 0)
anatofuz
parents:
diff changeset
484 return RC;
anatofuz
parents:
diff changeset
485 llvm_unreachable("Unimplemented composeWithSubRegIndex");
anatofuz
parents:
diff changeset
486 }
anatofuz
parents:
diff changeset
487 // Return the size in bits of the physical register Reg.
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
488 virtual uint16_t getPhysRegBitWidth(MCRegister Reg) const;
150
anatofuz
parents:
diff changeset
489
anatofuz
parents:
diff changeset
490 const TargetRegisterInfo &TRI;
anatofuz
parents:
diff changeset
491 MachineRegisterInfo &MRI;
anatofuz
parents:
diff changeset
492 };
anatofuz
parents:
diff changeset
493
anatofuz
parents:
diff changeset
494 } // end namespace llvm
anatofuz
parents:
diff changeset
495
anatofuz
parents:
diff changeset
496 #endif // LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H