blob: 74cafcd00b6012b663ed4cba8272375ad0e3615b [file] [log] [blame]
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001//===--- BitTracker.h -----------------------------------------------------===//
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
10#ifndef BITTRACKER_H
11#define BITTRACKER_H
12
13#include "llvm/ADT/SetVector.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/CodeGen/MachineFunction.h"
16
17#include <map>
18#include <queue>
19#include <set>
20
21namespace llvm {
22 class ConstantInt;
23 class MachineRegisterInfo;
24 class MachineBasicBlock;
25 class MachineInstr;
26 class MachineOperand;
27 class raw_ostream;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000028
29struct BitTracker {
30 struct BitRef;
31 struct RegisterRef;
32 struct BitValue;
33 struct BitMask;
34 struct RegisterCell;
35 struct MachineEvaluator;
36
Benjamin Kramerd8861512015-07-13 20:38:16 +000037 typedef SetVector<const MachineBasicBlock *> BranchTargetList;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000038
Benjamin Kramer9a5d7882015-07-18 17:43:23 +000039 typedef std::map<unsigned, RegisterCell> CellMapType;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000040
Benjamin Kramerd8861512015-07-13 20:38:16 +000041 BitTracker(const MachineEvaluator &E, MachineFunction &F);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000042 ~BitTracker();
43
44 void run();
45 void trace(bool On = false) { Trace = On; }
46 bool has(unsigned Reg) const;
47 const RegisterCell &lookup(unsigned Reg) const;
48 RegisterCell get(RegisterRef RR) const;
49 void put(RegisterRef RR, const RegisterCell &RC);
50 void subst(RegisterRef OldRR, RegisterRef NewRR);
Benjamin Kramerd8861512015-07-13 20:38:16 +000051 bool reached(const MachineBasicBlock *B) const;
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +000052 void visit(const MachineInstr &MI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000053
Krzysztof Parzyszek623afbd2016-08-03 18:13:32 +000054 void print_cells(raw_ostream &OS) const;
55
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000056private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000057 void visitPHI(const MachineInstr &PI);
58 void visitNonBranch(const MachineInstr &MI);
59 void visitBranchesFrom(const MachineInstr &BI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000060 void visitUsesOf(unsigned Reg);
61 void reset();
62
63 typedef std::pair<int,int> CFGEdge;
64 typedef std::set<CFGEdge> EdgeSetType;
Benjamin Kramerd8861512015-07-13 20:38:16 +000065 typedef std::set<const MachineInstr *> InstrSetType;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000066 typedef std::queue<CFGEdge> EdgeQueueType;
67
68 EdgeSetType EdgeExec; // Executable flow graph edges.
69 InstrSetType InstrExec; // Executable instructions.
70 EdgeQueueType FlowQ; // Work queue of CFG edges.
71 bool Trace; // Enable tracing for debugging.
72
73 const MachineEvaluator &ME;
Benjamin Kramerd8861512015-07-13 20:38:16 +000074 MachineFunction &MF;
75 MachineRegisterInfo &MRI;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000076 CellMapType &Map;
77};
78
79
80// Abstraction of a reference to bit at position Pos from a register Reg.
81struct BitTracker::BitRef {
82 BitRef(unsigned R = 0, uint16_t P = 0) : Reg(R), Pos(P) {}
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000083 bool operator== (const BitRef &BR) const {
84 // If Reg is 0, disregard Pos.
85 return Reg == BR.Reg && (Reg == 0 || Pos == BR.Pos);
86 }
87 unsigned Reg;
88 uint16_t Pos;
89};
90
91
92// Abstraction of a register reference in MachineOperand. It contains the
93// register number and the subregister index.
94struct BitTracker::RegisterRef {
95 RegisterRef(unsigned R = 0, unsigned S = 0)
96 : Reg(R), Sub(S) {}
Benjamin Kramerd8861512015-07-13 20:38:16 +000097 RegisterRef(const MachineOperand &MO)
98 : Reg(MO.getReg()), Sub(MO.getSubReg()) {}
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000099 unsigned Reg, Sub;
100};
101
102
103// Value that a single bit can take. This is outside of the context of
104// any register, it is more of an abstraction of the two-element set of
105// possible bit values. One extension here is the "Ref" type, which
106// indicates that this bit takes the same value as the bit described by
107// RefInfo.
108struct BitTracker::BitValue {
109 enum ValueType {
110 Top, // Bit not yet defined.
111 Zero, // Bit = 0.
112 One, // Bit = 1.
113 Ref // Bit value same as the one described in RefI.
114 // Conceptually, there is no explicit "bottom" value: the lattice's
115 // bottom will be expressed as a "ref to itself", which, in the context
116 // of registers, could be read as "this value of this bit is defined by
117 // this bit".
118 // The ordering is:
119 // x <= Top,
120 // Self <= x, where "Self" is "ref to itself".
121 // This makes the value lattice different for each virtual register
122 // (even for each bit in the same virtual register), since the "bottom"
123 // for one register will be a simple "ref" for another register.
124 // Since we do not store the "Self" bit and register number, the meet
125 // operation will need to take it as a parameter.
126 //
127 // In practice there is a special case for values that are not associa-
128 // ted with any specific virtual register. An example would be a value
129 // corresponding to a bit of a physical register, or an intermediate
130 // value obtained in some computation (such as instruction evaluation).
131 // Such cases are identical to the usual Ref type, but the register
132 // number is 0. In such case the Pos field of the reference is ignored.
133 //
134 // What is worthy of notice is that in value V (that is a "ref"), as long
135 // as the RefI.Reg is not 0, it may actually be the same register as the
136 // one in which V will be contained. If the RefI.Pos refers to the posi-
137 // tion of V, then V is assumed to be "bottom" (as a "ref to itself"),
138 // otherwise V is taken to be identical to the referenced bit of the
139 // same register.
140 // If RefI.Reg is 0, however, such a reference to the same register is
141 // not possible. Any value V that is a "ref", and whose RefI.Reg is 0
142 // is treated as "bottom".
143 };
144 ValueType Type;
145 BitRef RefI;
146
147 BitValue(ValueType T = Top) : Type(T) {}
148 BitValue(bool B) : Type(B ? One : Zero) {}
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000149 BitValue(unsigned Reg, uint16_t Pos) : Type(Ref), RefI(Reg, Pos) {}
150
151 bool operator== (const BitValue &V) const {
152 if (Type != V.Type)
153 return false;
154 if (Type == Ref && !(RefI == V.RefI))
155 return false;
156 return true;
157 }
158 bool operator!= (const BitValue &V) const {
159 return !operator==(V);
160 }
161 bool is(unsigned T) const {
162 assert(T == 0 || T == 1);
163 return T == 0 ? Type == Zero
164 : (T == 1 ? Type == One : false);
165 }
166
167 // The "meet" operation is the "." operation in a semilattice (L, ., T, B):
168 // (1) x.x = x
169 // (2) x.y = y.x
170 // (3) x.(y.z) = (x.y).z
171 // (4) x.T = x (i.e. T = "top")
172 // (5) x.B = B (i.e. B = "bottom")
173 //
174 // This "meet" function will update the value of the "*this" object with
175 // the newly calculated one, and return "true" if the value of *this has
176 // changed, and "false" otherwise.
177 // To prove that it satisfies the conditions (1)-(5), it is sufficient
178 // to show that a relation
179 // x <= y <=> x.y = x
180 // defines a partial order (i.e. that "meet" is same as "infimum").
181 bool meet(const BitValue &V, const BitRef &Self) {
182 // First, check the cases where there is nothing to be done.
183 if (Type == Ref && RefI == Self) // Bottom.meet(V) = Bottom (i.e. This)
184 return false;
185 if (V.Type == Top) // This.meet(Top) = This
186 return false;
187 if (*this == V) // This.meet(This) = This
188 return false;
189
190 // At this point, we know that the value of "this" will change.
191 // If it is Top, it will become the same as V, otherwise it will
192 // become "bottom" (i.e. Self).
193 if (Type == Top) {
194 Type = V.Type;
195 RefI = V.RefI; // This may be irrelevant, but copy anyway.
196 return true;
197 }
198 // Become "bottom".
199 Type = Ref;
200 RefI = Self;
201 return true;
202 }
203
204 // Create a reference to the bit value V.
205 static BitValue ref(const BitValue &V);
206 // Create a "self".
207 static BitValue self(const BitRef &Self = BitRef());
208
209 bool num() const {
210 return Type == Zero || Type == One;
211 }
212 operator bool() const {
213 assert(Type == Zero || Type == One);
214 return Type == One;
215 }
216
Benjamin Kramerd8861512015-07-13 20:38:16 +0000217 friend raw_ostream &operator<<(raw_ostream &OS, const BitValue &BV);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000218};
219
220
221// This operation must be idempotent, i.e. ref(ref(V)) == ref(V).
222inline BitTracker::BitValue
223BitTracker::BitValue::ref(const BitValue &V) {
224 if (V.Type != Ref)
225 return BitValue(V.Type);
226 if (V.RefI.Reg != 0)
227 return BitValue(V.RefI.Reg, V.RefI.Pos);
228 return self();
229}
230
231
232inline BitTracker::BitValue
233BitTracker::BitValue::self(const BitRef &Self) {
234 return BitValue(Self.Reg, Self.Pos);
235}
236
237
238// A sequence of bits starting from index B up to and including index E.
239// If E < B, the mask represents two sections: [0..E] and [B..W) where
240// W is the width of the register.
241struct BitTracker::BitMask {
242 BitMask() : B(0), E(0) {}
243 BitMask(uint16_t b, uint16_t e) : B(b), E(e) {}
244 uint16_t first() const { return B; }
245 uint16_t last() const { return E; }
246private:
247 uint16_t B, E;
248};
249
250
251// Representation of a register: a list of BitValues.
252struct BitTracker::RegisterCell {
253 RegisterCell(uint16_t Width = DefaultBitN) : Bits(Width) {}
254
255 uint16_t width() const {
256 return Bits.size();
257 }
258 const BitValue &operator[](uint16_t BitN) const {
259 assert(BitN < Bits.size());
260 return Bits[BitN];
261 }
262 BitValue &operator[](uint16_t BitN) {
263 assert(BitN < Bits.size());
264 return Bits[BitN];
265 }
266
267 bool meet(const RegisterCell &RC, unsigned SelfR);
268 RegisterCell &insert(const RegisterCell &RC, const BitMask &M);
269 RegisterCell extract(const BitMask &M) const; // Returns a new cell.
270 RegisterCell &rol(uint16_t Sh); // Rotate left.
271 RegisterCell &fill(uint16_t B, uint16_t E, const BitValue &V);
272 RegisterCell &cat(const RegisterCell &RC); // Concatenate.
273 uint16_t cl(bool B) const;
274 uint16_t ct(bool B) const;
275
276 bool operator== (const RegisterCell &RC) const;
277 bool operator!= (const RegisterCell &RC) const {
278 return !operator==(RC);
279 }
280
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000281 // Generate a "ref" cell for the corresponding register. In the resulting
282 // cell each bit will be described as being the same as the corresponding
283 // bit in register Reg (i.e. the cell is "defined" by register Reg).
284 static RegisterCell self(unsigned Reg, uint16_t Width);
285 // Generate a "top" cell of given size.
286 static RegisterCell top(uint16_t Width);
287 // Generate a cell that is a "ref" to another cell.
288 static RegisterCell ref(const RegisterCell &C);
289
290private:
291 // The DefaultBitN is here only to avoid frequent reallocation of the
292 // memory in the vector.
293 static const unsigned DefaultBitN = 32;
Benjamin Kramerd8861512015-07-13 20:38:16 +0000294 typedef SmallVector<BitValue, DefaultBitN> BitValueList;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000295 BitValueList Bits;
296
Benjamin Kramerd8861512015-07-13 20:38:16 +0000297 friend raw_ostream &operator<<(raw_ostream &OS, const RegisterCell &RC);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000298};
299
300
301inline bool BitTracker::has(unsigned Reg) const {
302 return Map.find(Reg) != Map.end();
303}
304
305
306inline const BitTracker::RegisterCell&
307BitTracker::lookup(unsigned Reg) const {
308 CellMapType::const_iterator F = Map.find(Reg);
309 assert(F != Map.end());
310 return F->second;
311}
312
313
314inline BitTracker::RegisterCell
315BitTracker::RegisterCell::self(unsigned Reg, uint16_t Width) {
316 RegisterCell RC(Width);
317 for (uint16_t i = 0; i < Width; ++i)
318 RC.Bits[i] = BitValue::self(BitRef(Reg, i));
319 return RC;
320}
321
322
323inline BitTracker::RegisterCell
324BitTracker::RegisterCell::top(uint16_t Width) {
325 RegisterCell RC(Width);
326 for (uint16_t i = 0; i < Width; ++i)
327 RC.Bits[i] = BitValue(BitValue::Top);
328 return RC;
329}
330
331
332inline BitTracker::RegisterCell
333BitTracker::RegisterCell::ref(const RegisterCell &C) {
334 uint16_t W = C.width();
335 RegisterCell RC(W);
336 for (unsigned i = 0; i < W; ++i)
337 RC[i] = BitValue::ref(C[i]);
338 return RC;
339}
340
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000341// A class to evaluate target's instructions and update the cell maps.
342// This is used internally by the bit tracker. A target that wants to
343// utilize this should implement the evaluation functions (noted below)
344// in a subclass of this class.
345struct BitTracker::MachineEvaluator {
Benjamin Kramerd8861512015-07-13 20:38:16 +0000346 MachineEvaluator(const TargetRegisterInfo &T, MachineRegisterInfo &M)
347 : TRI(T), MRI(M) {}
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000348 virtual ~MachineEvaluator() {}
349
350 uint16_t getRegBitWidth(const RegisterRef &RR) const;
351
352 RegisterCell getCell(const RegisterRef &RR, const CellMapType &M) const;
353 void putCell(const RegisterRef &RR, RegisterCell RC, CellMapType &M) const;
354 // A result of any operation should use refs to the source cells, not
355 // the cells directly. This function is a convenience wrapper to quickly
356 // generate a ref for a cell corresponding to a register reference.
357 RegisterCell getRef(const RegisterRef &RR, const CellMapType &M) const {
358 RegisterCell RC = getCell(RR, M);
359 return RegisterCell::ref(RC);
360 }
361
362 // Helper functions.
363 // Check if a cell is an immediate value (i.e. all bits are either 0 or 1).
364 bool isInt(const RegisterCell &A) const;
365 // Convert cell to an immediate value.
366 uint64_t toInt(const RegisterCell &A) const;
367
368 // Generate cell from an immediate value.
369 RegisterCell eIMM(int64_t V, uint16_t W) const;
Benjamin Kramerd8861512015-07-13 20:38:16 +0000370 RegisterCell eIMM(const ConstantInt *CI) const;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000371
372 // Arithmetic.
373 RegisterCell eADD(const RegisterCell &A1, const RegisterCell &A2) const;
374 RegisterCell eSUB(const RegisterCell &A1, const RegisterCell &A2) const;
375 RegisterCell eMLS(const RegisterCell &A1, const RegisterCell &A2) const;
376 RegisterCell eMLU(const RegisterCell &A1, const RegisterCell &A2) const;
377
378 // Shifts.
379 RegisterCell eASL(const RegisterCell &A1, uint16_t Sh) const;
380 RegisterCell eLSR(const RegisterCell &A1, uint16_t Sh) const;
381 RegisterCell eASR(const RegisterCell &A1, uint16_t Sh) const;
382
383 // Logical.
384 RegisterCell eAND(const RegisterCell &A1, const RegisterCell &A2) const;
385 RegisterCell eORL(const RegisterCell &A1, const RegisterCell &A2) const;
386 RegisterCell eXOR(const RegisterCell &A1, const RegisterCell &A2) const;
387 RegisterCell eNOT(const RegisterCell &A1) const;
388
389 // Set bit, clear bit.
390 RegisterCell eSET(const RegisterCell &A1, uint16_t BitN) const;
391 RegisterCell eCLR(const RegisterCell &A1, uint16_t BitN) const;
392
393 // Count leading/trailing bits (zeros/ones).
394 RegisterCell eCLB(const RegisterCell &A1, bool B, uint16_t W) const;
395 RegisterCell eCTB(const RegisterCell &A1, bool B, uint16_t W) const;
396
397 // Sign/zero extension.
398 RegisterCell eSXT(const RegisterCell &A1, uint16_t FromN) const;
399 RegisterCell eZXT(const RegisterCell &A1, uint16_t FromN) const;
400
401 // Extract/insert
402 // XTR R,b,e: extract bits from A1 starting at bit b, ending at e-1.
403 // INS R,S,b: take R and replace bits starting from b with S.
404 RegisterCell eXTR(const RegisterCell &A1, uint16_t B, uint16_t E) const;
405 RegisterCell eINS(const RegisterCell &A1, const RegisterCell &A2,
406 uint16_t AtN) const;
407
408 // User-provided functions for individual targets:
409
410 // Return a sub-register mask that indicates which bits in Reg belong
411 // to the subregister Sub. These bits are assumed to be contiguous in
412 // the super-register, and have the same ordering in the sub-register
413 // as in the super-register. It is valid to call this function with
414 // Sub == 0, in this case, the function should return a mask that spans
415 // the entire register Reg (which is what the default implementation
416 // does).
417 virtual BitMask mask(unsigned Reg, unsigned Sub) const;
418 // Indicate whether a given register class should be tracked.
Benjamin Kramerd8861512015-07-13 20:38:16 +0000419 virtual bool track(const TargetRegisterClass *RC) const { return true; }
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000420 // Evaluate a non-branching machine instruction, given the cell map with
421 // the input values. Place the results in the Outputs map. Return "true"
422 // if evaluation succeeded, "false" otherwise.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000423 virtual bool evaluate(const MachineInstr &MI, const CellMapType &Inputs,
Benjamin Kramerd8861512015-07-13 20:38:16 +0000424 CellMapType &Outputs) const;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000425 // Evaluate a branch, given the cell map with the input values. Fill out
426 // a list of all possible branch targets and indicate (through a flag)
427 // whether the branch could fall-through. Return "true" if this information
428 // has been successfully computed, "false" otherwise.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000429 virtual bool evaluate(const MachineInstr &BI, const CellMapType &Inputs,
Benjamin Kramerd8861512015-07-13 20:38:16 +0000430 BranchTargetList &Targets, bool &FallsThru) const = 0;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000431
Benjamin Kramerd8861512015-07-13 20:38:16 +0000432 const TargetRegisterInfo &TRI;
433 MachineRegisterInfo &MRI;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000434};
435
Benjamin Kramerd8861512015-07-13 20:38:16 +0000436} // end namespace llvm
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000437
Benjamin Kramerd8861512015-07-13 20:38:16 +0000438#endif