annotate llvm/lib/Target/Hexagon/BitTracker.h @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
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);
anatofuz
parents:
diff changeset
65 void visitUsesOf(unsigned Reg);
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
anatofuz
parents:
diff changeset
134 unsigned Reg;
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.
anatofuz
parents:
diff changeset
140 struct BitTracker::RegisterRef {
anatofuz
parents:
diff changeset
141 RegisterRef(unsigned R = 0, unsigned S = 0)
anatofuz
parents:
diff changeset
142 : Reg(R), Sub(S) {}
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
anatofuz
parents:
diff changeset
146 unsigned Reg, Sub;
anatofuz
parents:
diff changeset
147 };
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 // Value that a single bit can take. This is outside of the context of
anatofuz
parents:
diff changeset
150 // any register, it is more of an abstraction of the two-element set of
anatofuz
parents:
diff changeset
151 // possible bit values. One extension here is the "Ref" type, which
anatofuz
parents:
diff changeset
152 // indicates that this bit takes the same value as the bit described by
anatofuz
parents:
diff changeset
153 // RefInfo.
anatofuz
parents:
diff changeset
154 struct BitTracker::BitValue {
anatofuz
parents:
diff changeset
155 enum ValueType {
anatofuz
parents:
diff changeset
156 Top, // Bit not yet defined.
anatofuz
parents:
diff changeset
157 Zero, // Bit = 0.
anatofuz
parents:
diff changeset
158 One, // Bit = 1.
anatofuz
parents:
diff changeset
159 Ref // Bit value same as the one described in RefI.
anatofuz
parents:
diff changeset
160 // Conceptually, there is no explicit "bottom" value: the lattice's
anatofuz
parents:
diff changeset
161 // bottom will be expressed as a "ref to itself", which, in the context
anatofuz
parents:
diff changeset
162 // of registers, could be read as "this value of this bit is defined by
anatofuz
parents:
diff changeset
163 // this bit".
anatofuz
parents:
diff changeset
164 // The ordering is:
anatofuz
parents:
diff changeset
165 // x <= Top,
anatofuz
parents:
diff changeset
166 // Self <= x, where "Self" is "ref to itself".
anatofuz
parents:
diff changeset
167 // This makes the value lattice different for each virtual register
anatofuz
parents:
diff changeset
168 // (even for each bit in the same virtual register), since the "bottom"
anatofuz
parents:
diff changeset
169 // for one register will be a simple "ref" for another register.
anatofuz
parents:
diff changeset
170 // Since we do not store the "Self" bit and register number, the meet
anatofuz
parents:
diff changeset
171 // operation will need to take it as a parameter.
anatofuz
parents:
diff changeset
172 //
anatofuz
parents:
diff changeset
173 // In practice there is a special case for values that are not associa-
anatofuz
parents:
diff changeset
174 // ted with any specific virtual register. An example would be a value
anatofuz
parents:
diff changeset
175 // corresponding to a bit of a physical register, or an intermediate
anatofuz
parents:
diff changeset
176 // value obtained in some computation (such as instruction evaluation).
anatofuz
parents:
diff changeset
177 // Such cases are identical to the usual Ref type, but the register
anatofuz
parents:
diff changeset
178 // number is 0. In such case the Pos field of the reference is ignored.
anatofuz
parents:
diff changeset
179 //
anatofuz
parents:
diff changeset
180 // What is worthy of notice is that in value V (that is a "ref"), as long
anatofuz
parents:
diff changeset
181 // as the RefI.Reg is not 0, it may actually be the same register as the
anatofuz
parents:
diff changeset
182 // one in which V will be contained. If the RefI.Pos refers to the posi-
anatofuz
parents:
diff changeset
183 // tion of V, then V is assumed to be "bottom" (as a "ref to itself"),
anatofuz
parents:
diff changeset
184 // otherwise V is taken to be identical to the referenced bit of the
anatofuz
parents:
diff changeset
185 // same register.
anatofuz
parents:
diff changeset
186 // If RefI.Reg is 0, however, such a reference to the same register is
anatofuz
parents:
diff changeset
187 // not possible. Any value V that is a "ref", and whose RefI.Reg is 0
anatofuz
parents:
diff changeset
188 // is treated as "bottom".
anatofuz
parents:
diff changeset
189 };
anatofuz
parents:
diff changeset
190 ValueType Type;
anatofuz
parents:
diff changeset
191 BitRef RefI;
anatofuz
parents:
diff changeset
192
anatofuz
parents:
diff changeset
193 BitValue(ValueType T = Top) : Type(T) {}
anatofuz
parents:
diff changeset
194 BitValue(bool B) : Type(B ? One : Zero) {}
anatofuz
parents:
diff changeset
195 BitValue(unsigned Reg, uint16_t Pos) : Type(Ref), RefI(Reg, Pos) {}
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 bool operator== (const BitValue &V) const {
anatofuz
parents:
diff changeset
198 if (Type != V.Type)
anatofuz
parents:
diff changeset
199 return false;
anatofuz
parents:
diff changeset
200 if (Type == Ref && !(RefI == V.RefI))
anatofuz
parents:
diff changeset
201 return false;
anatofuz
parents:
diff changeset
202 return true;
anatofuz
parents:
diff changeset
203 }
anatofuz
parents:
diff changeset
204 bool operator!= (const BitValue &V) const {
anatofuz
parents:
diff changeset
205 return !operator==(V);
anatofuz
parents:
diff changeset
206 }
anatofuz
parents:
diff changeset
207
anatofuz
parents:
diff changeset
208 bool is(unsigned T) const {
anatofuz
parents:
diff changeset
209 assert(T == 0 || T == 1);
anatofuz
parents:
diff changeset
210 return T == 0 ? Type == Zero
anatofuz
parents:
diff changeset
211 : (T == 1 ? Type == One : false);
anatofuz
parents:
diff changeset
212 }
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214 // The "meet" operation is the "." operation in a semilattice (L, ., T, B):
anatofuz
parents:
diff changeset
215 // (1) x.x = x
anatofuz
parents:
diff changeset
216 // (2) x.y = y.x
anatofuz
parents:
diff changeset
217 // (3) x.(y.z) = (x.y).z
anatofuz
parents:
diff changeset
218 // (4) x.T = x (i.e. T = "top")
anatofuz
parents:
diff changeset
219 // (5) x.B = B (i.e. B = "bottom")
anatofuz
parents:
diff changeset
220 //
anatofuz
parents:
diff changeset
221 // This "meet" function will update the value of the "*this" object with
anatofuz
parents:
diff changeset
222 // the newly calculated one, and return "true" if the value of *this has
anatofuz
parents:
diff changeset
223 // changed, and "false" otherwise.
anatofuz
parents:
diff changeset
224 // To prove that it satisfies the conditions (1)-(5), it is sufficient
anatofuz
parents:
diff changeset
225 // to show that a relation
anatofuz
parents:
diff changeset
226 // x <= y <=> x.y = x
anatofuz
parents:
diff changeset
227 // defines a partial order (i.e. that "meet" is same as "infimum").
anatofuz
parents:
diff changeset
228 bool meet(const BitValue &V, const BitRef &Self) {
anatofuz
parents:
diff changeset
229 // First, check the cases where there is nothing to be done.
anatofuz
parents:
diff changeset
230 if (Type == Ref && RefI == Self) // Bottom.meet(V) = Bottom (i.e. This)
anatofuz
parents:
diff changeset
231 return false;
anatofuz
parents:
diff changeset
232 if (V.Type == Top) // This.meet(Top) = This
anatofuz
parents:
diff changeset
233 return false;
anatofuz
parents:
diff changeset
234 if (*this == V) // This.meet(This) = This
anatofuz
parents:
diff changeset
235 return false;
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 // At this point, we know that the value of "this" will change.
anatofuz
parents:
diff changeset
238 // If it is Top, it will become the same as V, otherwise it will
anatofuz
parents:
diff changeset
239 // become "bottom" (i.e. Self).
anatofuz
parents:
diff changeset
240 if (Type == Top) {
anatofuz
parents:
diff changeset
241 Type = V.Type;
anatofuz
parents:
diff changeset
242 RefI = V.RefI; // This may be irrelevant, but copy anyway.
anatofuz
parents:
diff changeset
243 return true;
anatofuz
parents:
diff changeset
244 }
anatofuz
parents:
diff changeset
245 // Become "bottom".
anatofuz
parents:
diff changeset
246 Type = Ref;
anatofuz
parents:
diff changeset
247 RefI = Self;
anatofuz
parents:
diff changeset
248 return true;
anatofuz
parents:
diff changeset
249 }
anatofuz
parents:
diff changeset
250
anatofuz
parents:
diff changeset
251 // Create a reference to the bit value V.
anatofuz
parents:
diff changeset
252 static BitValue ref(const BitValue &V);
anatofuz
parents:
diff changeset
253 // Create a "self".
anatofuz
parents:
diff changeset
254 static BitValue self(const BitRef &Self = BitRef());
anatofuz
parents:
diff changeset
255
anatofuz
parents:
diff changeset
256 bool num() const {
anatofuz
parents:
diff changeset
257 return Type == Zero || Type == One;
anatofuz
parents:
diff changeset
258 }
anatofuz
parents:
diff changeset
259
anatofuz
parents:
diff changeset
260 operator bool() const {
anatofuz
parents:
diff changeset
261 assert(Type == Zero || Type == One);
anatofuz
parents:
diff changeset
262 return Type == One;
anatofuz
parents:
diff changeset
263 }
anatofuz
parents:
diff changeset
264
anatofuz
parents:
diff changeset
265 friend raw_ostream &operator<<(raw_ostream &OS, const BitValue &BV);
anatofuz
parents:
diff changeset
266 };
anatofuz
parents:
diff changeset
267
anatofuz
parents:
diff changeset
268 // This operation must be idempotent, i.e. ref(ref(V)) == ref(V).
anatofuz
parents:
diff changeset
269 inline BitTracker::BitValue
anatofuz
parents:
diff changeset
270 BitTracker::BitValue::ref(const BitValue &V) {
anatofuz
parents:
diff changeset
271 if (V.Type != Ref)
anatofuz
parents:
diff changeset
272 return BitValue(V.Type);
anatofuz
parents:
diff changeset
273 if (V.RefI.Reg != 0)
anatofuz
parents:
diff changeset
274 return BitValue(V.RefI.Reg, V.RefI.Pos);
anatofuz
parents:
diff changeset
275 return self();
anatofuz
parents:
diff changeset
276 }
anatofuz
parents:
diff changeset
277
anatofuz
parents:
diff changeset
278 inline BitTracker::BitValue
anatofuz
parents:
diff changeset
279 BitTracker::BitValue::self(const BitRef &Self) {
anatofuz
parents:
diff changeset
280 return BitValue(Self.Reg, Self.Pos);
anatofuz
parents:
diff changeset
281 }
anatofuz
parents:
diff changeset
282
anatofuz
parents:
diff changeset
283 // A sequence of bits starting from index B up to and including index E.
anatofuz
parents:
diff changeset
284 // If E < B, the mask represents two sections: [0..E] and [B..W) where
anatofuz
parents:
diff changeset
285 // W is the width of the register.
anatofuz
parents:
diff changeset
286 struct BitTracker::BitMask {
anatofuz
parents:
diff changeset
287 BitMask() = default;
anatofuz
parents:
diff changeset
288 BitMask(uint16_t b, uint16_t e) : B(b), E(e) {}
anatofuz
parents:
diff changeset
289
anatofuz
parents:
diff changeset
290 uint16_t first() const { return B; }
anatofuz
parents:
diff changeset
291 uint16_t last() const { return E; }
anatofuz
parents:
diff changeset
292
anatofuz
parents:
diff changeset
293 private:
anatofuz
parents:
diff changeset
294 uint16_t B = 0;
anatofuz
parents:
diff changeset
295 uint16_t E = 0;
anatofuz
parents:
diff changeset
296 };
anatofuz
parents:
diff changeset
297
anatofuz
parents:
diff changeset
298 // Representation of a register: a list of BitValues.
anatofuz
parents:
diff changeset
299 struct BitTracker::RegisterCell {
anatofuz
parents:
diff changeset
300 RegisterCell(uint16_t Width = DefaultBitN) : Bits(Width) {}
anatofuz
parents:
diff changeset
301
anatofuz
parents:
diff changeset
302 uint16_t width() const {
anatofuz
parents:
diff changeset
303 return Bits.size();
anatofuz
parents:
diff changeset
304 }
anatofuz
parents:
diff changeset
305
anatofuz
parents:
diff changeset
306 const BitValue &operator[](uint16_t BitN) const {
anatofuz
parents:
diff changeset
307 assert(BitN < Bits.size());
anatofuz
parents:
diff changeset
308 return Bits[BitN];
anatofuz
parents:
diff changeset
309 }
anatofuz
parents:
diff changeset
310 BitValue &operator[](uint16_t BitN) {
anatofuz
parents:
diff changeset
311 assert(BitN < Bits.size());
anatofuz
parents:
diff changeset
312 return Bits[BitN];
anatofuz
parents:
diff changeset
313 }
anatofuz
parents:
diff changeset
314
anatofuz
parents:
diff changeset
315 bool meet(const RegisterCell &RC, unsigned SelfR);
anatofuz
parents:
diff changeset
316 RegisterCell &insert(const RegisterCell &RC, const BitMask &M);
anatofuz
parents:
diff changeset
317 RegisterCell extract(const BitMask &M) const; // Returns a new cell.
anatofuz
parents:
diff changeset
318 RegisterCell &rol(uint16_t Sh); // Rotate left.
anatofuz
parents:
diff changeset
319 RegisterCell &fill(uint16_t B, uint16_t E, const BitValue &V);
anatofuz
parents:
diff changeset
320 RegisterCell &cat(const RegisterCell &RC); // Concatenate.
anatofuz
parents:
diff changeset
321 uint16_t cl(bool B) const;
anatofuz
parents:
diff changeset
322 uint16_t ct(bool B) const;
anatofuz
parents:
diff changeset
323
anatofuz
parents:
diff changeset
324 bool operator== (const RegisterCell &RC) const;
anatofuz
parents:
diff changeset
325 bool operator!= (const RegisterCell &RC) const {
anatofuz
parents:
diff changeset
326 return !operator==(RC);
anatofuz
parents:
diff changeset
327 }
anatofuz
parents:
diff changeset
328
anatofuz
parents:
diff changeset
329 // Replace the ref-to-reg-0 bit values with the given register.
anatofuz
parents:
diff changeset
330 RegisterCell &regify(unsigned R);
anatofuz
parents:
diff changeset
331
anatofuz
parents:
diff changeset
332 // Generate a "ref" cell for the corresponding register. In the resulting
anatofuz
parents:
diff changeset
333 // cell each bit will be described as being the same as the corresponding
anatofuz
parents:
diff changeset
334 // bit in register Reg (i.e. the cell is "defined" by register Reg).
anatofuz
parents:
diff changeset
335 static RegisterCell self(unsigned Reg, uint16_t Width);
anatofuz
parents:
diff changeset
336 // Generate a "top" cell of given size.
anatofuz
parents:
diff changeset
337 static RegisterCell top(uint16_t Width);
anatofuz
parents:
diff changeset
338 // Generate a cell that is a "ref" to another cell.
anatofuz
parents:
diff changeset
339 static RegisterCell ref(const RegisterCell &C);
anatofuz
parents:
diff changeset
340
anatofuz
parents:
diff changeset
341 private:
anatofuz
parents:
diff changeset
342 // The DefaultBitN is here only to avoid frequent reallocation of the
anatofuz
parents:
diff changeset
343 // memory in the vector.
anatofuz
parents:
diff changeset
344 static const unsigned DefaultBitN = 32;
anatofuz
parents:
diff changeset
345 using BitValueList = SmallVector<BitValue, DefaultBitN>;
anatofuz
parents:
diff changeset
346 BitValueList Bits;
anatofuz
parents:
diff changeset
347
anatofuz
parents:
diff changeset
348 friend raw_ostream &operator<<(raw_ostream &OS, const RegisterCell &RC);
anatofuz
parents:
diff changeset
349 };
anatofuz
parents:
diff changeset
350
anatofuz
parents:
diff changeset
351 inline bool BitTracker::has(unsigned Reg) const {
anatofuz
parents:
diff changeset
352 return Map.find(Reg) != Map.end();
anatofuz
parents:
diff changeset
353 }
anatofuz
parents:
diff changeset
354
anatofuz
parents:
diff changeset
355 inline const BitTracker::RegisterCell&
anatofuz
parents:
diff changeset
356 BitTracker::lookup(unsigned Reg) const {
anatofuz
parents:
diff changeset
357 CellMapType::const_iterator F = Map.find(Reg);
anatofuz
parents:
diff changeset
358 assert(F != Map.end());
anatofuz
parents:
diff changeset
359 return F->second;
anatofuz
parents:
diff changeset
360 }
anatofuz
parents:
diff changeset
361
anatofuz
parents:
diff changeset
362 inline BitTracker::RegisterCell
anatofuz
parents:
diff changeset
363 BitTracker::RegisterCell::self(unsigned Reg, uint16_t Width) {
anatofuz
parents:
diff changeset
364 RegisterCell RC(Width);
anatofuz
parents:
diff changeset
365 for (uint16_t i = 0; i < Width; ++i)
anatofuz
parents:
diff changeset
366 RC.Bits[i] = BitValue::self(BitRef(Reg, i));
anatofuz
parents:
diff changeset
367 return RC;
anatofuz
parents:
diff changeset
368 }
anatofuz
parents:
diff changeset
369
anatofuz
parents:
diff changeset
370 inline BitTracker::RegisterCell
anatofuz
parents:
diff changeset
371 BitTracker::RegisterCell::top(uint16_t Width) {
anatofuz
parents:
diff changeset
372 RegisterCell RC(Width);
anatofuz
parents:
diff changeset
373 for (uint16_t i = 0; i < Width; ++i)
anatofuz
parents:
diff changeset
374 RC.Bits[i] = BitValue(BitValue::Top);
anatofuz
parents:
diff changeset
375 return RC;
anatofuz
parents:
diff changeset
376 }
anatofuz
parents:
diff changeset
377
anatofuz
parents:
diff changeset
378 inline BitTracker::RegisterCell
anatofuz
parents:
diff changeset
379 BitTracker::RegisterCell::ref(const RegisterCell &C) {
anatofuz
parents:
diff changeset
380 uint16_t W = C.width();
anatofuz
parents:
diff changeset
381 RegisterCell RC(W);
anatofuz
parents:
diff changeset
382 for (unsigned i = 0; i < W; ++i)
anatofuz
parents:
diff changeset
383 RC[i] = BitValue::ref(C[i]);
anatofuz
parents:
diff changeset
384 return RC;
anatofuz
parents:
diff changeset
385 }
anatofuz
parents:
diff changeset
386
anatofuz
parents:
diff changeset
387 // A class to evaluate target's instructions and update the cell maps.
anatofuz
parents:
diff changeset
388 // This is used internally by the bit tracker. A target that wants to
anatofuz
parents:
diff changeset
389 // utilize this should implement the evaluation functions (noted below)
anatofuz
parents:
diff changeset
390 // in a subclass of this class.
anatofuz
parents:
diff changeset
391 struct BitTracker::MachineEvaluator {
anatofuz
parents:
diff changeset
392 MachineEvaluator(const TargetRegisterInfo &T, MachineRegisterInfo &M)
anatofuz
parents:
diff changeset
393 : TRI(T), MRI(M) {}
anatofuz
parents:
diff changeset
394 virtual ~MachineEvaluator() = default;
anatofuz
parents:
diff changeset
395
anatofuz
parents:
diff changeset
396 uint16_t getRegBitWidth(const RegisterRef &RR) const;
anatofuz
parents:
diff changeset
397
anatofuz
parents:
diff changeset
398 RegisterCell getCell(const RegisterRef &RR, const CellMapType &M) const;
anatofuz
parents:
diff changeset
399 void putCell(const RegisterRef &RR, RegisterCell RC, CellMapType &M) const;
anatofuz
parents:
diff changeset
400
anatofuz
parents:
diff changeset
401 // A result of any operation should use refs to the source cells, not
anatofuz
parents:
diff changeset
402 // the cells directly. This function is a convenience wrapper to quickly
anatofuz
parents:
diff changeset
403 // generate a ref for a cell corresponding to a register reference.
anatofuz
parents:
diff changeset
404 RegisterCell getRef(const RegisterRef &RR, const CellMapType &M) const {
anatofuz
parents:
diff changeset
405 RegisterCell RC = getCell(RR, M);
anatofuz
parents:
diff changeset
406 return RegisterCell::ref(RC);
anatofuz
parents:
diff changeset
407 }
anatofuz
parents:
diff changeset
408
anatofuz
parents:
diff changeset
409 // Helper functions.
anatofuz
parents:
diff changeset
410 // Check if a cell is an immediate value (i.e. all bits are either 0 or 1).
anatofuz
parents:
diff changeset
411 bool isInt(const RegisterCell &A) const;
anatofuz
parents:
diff changeset
412 // Convert cell to an immediate value.
anatofuz
parents:
diff changeset
413 uint64_t toInt(const RegisterCell &A) const;
anatofuz
parents:
diff changeset
414
anatofuz
parents:
diff changeset
415 // Generate cell from an immediate value.
anatofuz
parents:
diff changeset
416 RegisterCell eIMM(int64_t V, uint16_t W) const;
anatofuz
parents:
diff changeset
417 RegisterCell eIMM(const ConstantInt *CI) const;
anatofuz
parents:
diff changeset
418
anatofuz
parents:
diff changeset
419 // Arithmetic.
anatofuz
parents:
diff changeset
420 RegisterCell eADD(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
421 RegisterCell eSUB(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
422 RegisterCell eMLS(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
423 RegisterCell eMLU(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
424
anatofuz
parents:
diff changeset
425 // Shifts.
anatofuz
parents:
diff changeset
426 RegisterCell eASL(const RegisterCell &A1, uint16_t Sh) const;
anatofuz
parents:
diff changeset
427 RegisterCell eLSR(const RegisterCell &A1, uint16_t Sh) const;
anatofuz
parents:
diff changeset
428 RegisterCell eASR(const RegisterCell &A1, uint16_t Sh) const;
anatofuz
parents:
diff changeset
429
anatofuz
parents:
diff changeset
430 // Logical.
anatofuz
parents:
diff changeset
431 RegisterCell eAND(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
432 RegisterCell eORL(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
433 RegisterCell eXOR(const RegisterCell &A1, const RegisterCell &A2) const;
anatofuz
parents:
diff changeset
434 RegisterCell eNOT(const RegisterCell &A1) const;
anatofuz
parents:
diff changeset
435
anatofuz
parents:
diff changeset
436 // Set bit, clear bit.
anatofuz
parents:
diff changeset
437 RegisterCell eSET(const RegisterCell &A1, uint16_t BitN) const;
anatofuz
parents:
diff changeset
438 RegisterCell eCLR(const RegisterCell &A1, uint16_t BitN) const;
anatofuz
parents:
diff changeset
439
anatofuz
parents:
diff changeset
440 // Count leading/trailing bits (zeros/ones).
anatofuz
parents:
diff changeset
441 RegisterCell eCLB(const RegisterCell &A1, bool B, uint16_t W) const;
anatofuz
parents:
diff changeset
442 RegisterCell eCTB(const RegisterCell &A1, bool B, uint16_t W) const;
anatofuz
parents:
diff changeset
443
anatofuz
parents:
diff changeset
444 // Sign/zero extension.
anatofuz
parents:
diff changeset
445 RegisterCell eSXT(const RegisterCell &A1, uint16_t FromN) const;
anatofuz
parents:
diff changeset
446 RegisterCell eZXT(const RegisterCell &A1, uint16_t FromN) const;
anatofuz
parents:
diff changeset
447
anatofuz
parents:
diff changeset
448 // Extract/insert
anatofuz
parents:
diff changeset
449 // XTR R,b,e: extract bits from A1 starting at bit b, ending at e-1.
anatofuz
parents:
diff changeset
450 // INS R,S,b: take R and replace bits starting from b with S.
anatofuz
parents:
diff changeset
451 RegisterCell eXTR(const RegisterCell &A1, uint16_t B, uint16_t E) const;
anatofuz
parents:
diff changeset
452 RegisterCell eINS(const RegisterCell &A1, const RegisterCell &A2,
anatofuz
parents:
diff changeset
453 uint16_t AtN) const;
anatofuz
parents:
diff changeset
454
anatofuz
parents:
diff changeset
455 // User-provided functions for individual targets:
anatofuz
parents:
diff changeset
456
anatofuz
parents:
diff changeset
457 // Return a sub-register mask that indicates which bits in Reg belong
anatofuz
parents:
diff changeset
458 // to the subregister Sub. These bits are assumed to be contiguous in
anatofuz
parents:
diff changeset
459 // the super-register, and have the same ordering in the sub-register
anatofuz
parents:
diff changeset
460 // as in the super-register. It is valid to call this function with
anatofuz
parents:
diff changeset
461 // Sub == 0, in this case, the function should return a mask that spans
anatofuz
parents:
diff changeset
462 // the entire register Reg (which is what the default implementation
anatofuz
parents:
diff changeset
463 // does).
anatofuz
parents:
diff changeset
464 virtual BitMask mask(unsigned Reg, unsigned Sub) const;
anatofuz
parents:
diff changeset
465 // Indicate whether a given register class should be tracked.
anatofuz
parents:
diff changeset
466 virtual bool track(const TargetRegisterClass *RC) const { return true; }
anatofuz
parents:
diff changeset
467 // Evaluate a non-branching machine instruction, given the cell map with
anatofuz
parents:
diff changeset
468 // the input values. Place the results in the Outputs map. Return "true"
anatofuz
parents:
diff changeset
469 // if evaluation succeeded, "false" otherwise.
anatofuz
parents:
diff changeset
470 virtual bool evaluate(const MachineInstr &MI, const CellMapType &Inputs,
anatofuz
parents:
diff changeset
471 CellMapType &Outputs) const;
anatofuz
parents:
diff changeset
472 // Evaluate a branch, given the cell map with the input values. Fill out
anatofuz
parents:
diff changeset
473 // a list of all possible branch targets and indicate (through a flag)
anatofuz
parents:
diff changeset
474 // whether the branch could fall-through. Return "true" if this information
anatofuz
parents:
diff changeset
475 // has been successfully computed, "false" otherwise.
anatofuz
parents:
diff changeset
476 virtual bool evaluate(const MachineInstr &BI, const CellMapType &Inputs,
anatofuz
parents:
diff changeset
477 BranchTargetList &Targets, bool &FallsThru) const = 0;
anatofuz
parents:
diff changeset
478 // Given a register class RC, return a register class that should be assumed
anatofuz
parents:
diff changeset
479 // when a register from class RC is used with a subregister of index Idx.
anatofuz
parents:
diff changeset
480 virtual const TargetRegisterClass&
anatofuz
parents:
diff changeset
481 composeWithSubRegIndex(const TargetRegisterClass &RC, unsigned Idx) const {
anatofuz
parents:
diff changeset
482 if (Idx == 0)
anatofuz
parents:
diff changeset
483 return RC;
anatofuz
parents:
diff changeset
484 llvm_unreachable("Unimplemented composeWithSubRegIndex");
anatofuz
parents:
diff changeset
485 }
anatofuz
parents:
diff changeset
486 // Return the size in bits of the physical register Reg.
anatofuz
parents:
diff changeset
487 virtual uint16_t getPhysRegBitWidth(unsigned Reg) const;
anatofuz
parents:
diff changeset
488
anatofuz
parents:
diff changeset
489 const TargetRegisterInfo &TRI;
anatofuz
parents:
diff changeset
490 MachineRegisterInfo &MRI;
anatofuz
parents:
diff changeset
491 };
anatofuz
parents:
diff changeset
492
anatofuz
parents:
diff changeset
493 } // end namespace llvm
anatofuz
parents:
diff changeset
494
anatofuz
parents:
diff changeset
495 #endif // LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H