blob: 48c5f2266acfabc2b9de0a1bf6c219d80ae8247d [file] [log] [blame]
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +00001//===--- BitTracker.h -------------------------------------------*- C++ -*-===//
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00002//
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
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000010#ifndef LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H
11#define LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000012
13#include "llvm/ADT/SetVector.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000016#include "llvm/CodeGen/MachineOperand.h"
17#include <cassert>
18#include <cstdint>
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000019#include <map>
20#include <queue>
21#include <set>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000022#include <utility>
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000023
24namespace llvm {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000025
26class ConstantInt;
27class MachineRegisterInfo;
28class MachineBasicBlock;
29class MachineInstr;
30class raw_ostream;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000031
32struct BitTracker {
33 struct BitRef;
34 struct RegisterRef;
35 struct BitValue;
36 struct BitMask;
37 struct RegisterCell;
38 struct MachineEvaluator;
39
Benjamin Kramerd8861512015-07-13 20:38:16 +000040 typedef SetVector<const MachineBasicBlock *> BranchTargetList;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000041
Benjamin Kramer9a5d7882015-07-18 17:43:23 +000042 typedef std::map<unsigned, RegisterCell> CellMapType;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000043
Benjamin Kramerd8861512015-07-13 20:38:16 +000044 BitTracker(const MachineEvaluator &E, MachineFunction &F);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000045 ~BitTracker();
46
47 void run();
48 void trace(bool On = false) { Trace = On; }
49 bool has(unsigned Reg) const;
50 const RegisterCell &lookup(unsigned Reg) const;
51 RegisterCell get(RegisterRef RR) const;
52 void put(RegisterRef RR, const RegisterCell &RC);
53 void subst(RegisterRef OldRR, RegisterRef NewRR);
Benjamin Kramerd8861512015-07-13 20:38:16 +000054 bool reached(const MachineBasicBlock *B) const;
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +000055 void visit(const MachineInstr &MI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000056
Krzysztof Parzyszek623afbd2016-08-03 18:13:32 +000057 void print_cells(raw_ostream &OS) const;
58
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000059private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000060 void visitPHI(const MachineInstr &PI);
61 void visitNonBranch(const MachineInstr &MI);
62 void visitBranchesFrom(const MachineInstr &BI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000063 void visitUsesOf(unsigned Reg);
64 void reset();
65
66 typedef std::pair<int,int> CFGEdge;
67 typedef std::set<CFGEdge> EdgeSetType;
Benjamin Kramerd8861512015-07-13 20:38:16 +000068 typedef std::set<const MachineInstr *> InstrSetType;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000069 typedef std::queue<CFGEdge> EdgeQueueType;
70
71 EdgeSetType EdgeExec; // Executable flow graph edges.
72 InstrSetType InstrExec; // Executable instructions.
73 EdgeQueueType FlowQ; // Work queue of CFG edges.
74 bool Trace; // Enable tracing for debugging.
75
76 const MachineEvaluator &ME;
Benjamin Kramerd8861512015-07-13 20:38:16 +000077 MachineFunction &MF;
78 MachineRegisterInfo &MRI;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000079 CellMapType &Map;
80};
81
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000082// Abstraction of a reference to bit at position Pos from a register Reg.
83struct BitTracker::BitRef {
84 BitRef(unsigned R = 0, uint16_t P = 0) : Reg(R), Pos(P) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000085
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000086 bool operator== (const BitRef &BR) const {
87 // If Reg is 0, disregard Pos.
88 return Reg == BR.Reg && (Reg == 0 || Pos == BR.Pos);
89 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000090
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000091 unsigned Reg;
92 uint16_t Pos;
93};
94
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000095// Abstraction of a register reference in MachineOperand. It contains the
96// register number and the subregister index.
97struct BitTracker::RegisterRef {
98 RegisterRef(unsigned R = 0, unsigned S = 0)
99 : Reg(R), Sub(S) {}
Benjamin Kramerd8861512015-07-13 20:38:16 +0000100 RegisterRef(const MachineOperand &MO)
101 : Reg(MO.getReg()), Sub(MO.getSubReg()) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000102
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000103 unsigned Reg, Sub;
104};
105
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000106// Value that a single bit can take. This is outside of the context of
107// any register, it is more of an abstraction of the two-element set of
108// possible bit values. One extension here is the "Ref" type, which
109// indicates that this bit takes the same value as the bit described by
110// RefInfo.
111struct BitTracker::BitValue {
112 enum ValueType {
113 Top, // Bit not yet defined.
114 Zero, // Bit = 0.
115 One, // Bit = 1.
116 Ref // Bit value same as the one described in RefI.
117 // Conceptually, there is no explicit "bottom" value: the lattice's
118 // bottom will be expressed as a "ref to itself", which, in the context
119 // of registers, could be read as "this value of this bit is defined by
120 // this bit".
121 // The ordering is:
122 // x <= Top,
123 // Self <= x, where "Self" is "ref to itself".
124 // This makes the value lattice different for each virtual register
125 // (even for each bit in the same virtual register), since the "bottom"
126 // for one register will be a simple "ref" for another register.
127 // Since we do not store the "Self" bit and register number, the meet
128 // operation will need to take it as a parameter.
129 //
130 // In practice there is a special case for values that are not associa-
131 // ted with any specific virtual register. An example would be a value
132 // corresponding to a bit of a physical register, or an intermediate
133 // value obtained in some computation (such as instruction evaluation).
134 // Such cases are identical to the usual Ref type, but the register
135 // number is 0. In such case the Pos field of the reference is ignored.
136 //
137 // What is worthy of notice is that in value V (that is a "ref"), as long
138 // as the RefI.Reg is not 0, it may actually be the same register as the
139 // one in which V will be contained. If the RefI.Pos refers to the posi-
140 // tion of V, then V is assumed to be "bottom" (as a "ref to itself"),
141 // otherwise V is taken to be identical to the referenced bit of the
142 // same register.
143 // If RefI.Reg is 0, however, such a reference to the same register is
144 // not possible. Any value V that is a "ref", and whose RefI.Reg is 0
145 // is treated as "bottom".
146 };
147 ValueType Type;
148 BitRef RefI;
149
150 BitValue(ValueType T = Top) : Type(T) {}
151 BitValue(bool B) : Type(B ? One : Zero) {}
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000152 BitValue(unsigned Reg, uint16_t Pos) : Type(Ref), RefI(Reg, Pos) {}
153
154 bool operator== (const BitValue &V) const {
155 if (Type != V.Type)
156 return false;
157 if (Type == Ref && !(RefI == V.RefI))
158 return false;
159 return true;
160 }
161 bool operator!= (const BitValue &V) const {
162 return !operator==(V);
163 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000164
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000165 bool is(unsigned T) const {
166 assert(T == 0 || T == 1);
167 return T == 0 ? Type == Zero
168 : (T == 1 ? Type == One : false);
169 }
170
171 // The "meet" operation is the "." operation in a semilattice (L, ., T, B):
172 // (1) x.x = x
173 // (2) x.y = y.x
174 // (3) x.(y.z) = (x.y).z
175 // (4) x.T = x (i.e. T = "top")
176 // (5) x.B = B (i.e. B = "bottom")
177 //
178 // This "meet" function will update the value of the "*this" object with
179 // the newly calculated one, and return "true" if the value of *this has
180 // changed, and "false" otherwise.
181 // To prove that it satisfies the conditions (1)-(5), it is sufficient
182 // to show that a relation
183 // x <= y <=> x.y = x
184 // defines a partial order (i.e. that "meet" is same as "infimum").
185 bool meet(const BitValue &V, const BitRef &Self) {
186 // First, check the cases where there is nothing to be done.
187 if (Type == Ref && RefI == Self) // Bottom.meet(V) = Bottom (i.e. This)
188 return false;
189 if (V.Type == Top) // This.meet(Top) = This
190 return false;
191 if (*this == V) // This.meet(This) = This
192 return false;
193
194 // At this point, we know that the value of "this" will change.
195 // If it is Top, it will become the same as V, otherwise it will
196 // become "bottom" (i.e. Self).
197 if (Type == Top) {
198 Type = V.Type;
199 RefI = V.RefI; // This may be irrelevant, but copy anyway.
200 return true;
201 }
202 // Become "bottom".
203 Type = Ref;
204 RefI = Self;
205 return true;
206 }
207
208 // Create a reference to the bit value V.
209 static BitValue ref(const BitValue &V);
210 // Create a "self".
211 static BitValue self(const BitRef &Self = BitRef());
212
213 bool num() const {
214 return Type == Zero || Type == One;
215 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000216
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000217 operator bool() const {
218 assert(Type == Zero || Type == One);
219 return Type == One;
220 }
221
Benjamin Kramerd8861512015-07-13 20:38:16 +0000222 friend raw_ostream &operator<<(raw_ostream &OS, const BitValue &BV);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000223};
224
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000225// This operation must be idempotent, i.e. ref(ref(V)) == ref(V).
226inline BitTracker::BitValue
227BitTracker::BitValue::ref(const BitValue &V) {
228 if (V.Type != Ref)
229 return BitValue(V.Type);
230 if (V.RefI.Reg != 0)
231 return BitValue(V.RefI.Reg, V.RefI.Pos);
232 return self();
233}
234
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000235inline BitTracker::BitValue
236BitTracker::BitValue::self(const BitRef &Self) {
237 return BitValue(Self.Reg, Self.Pos);
238}
239
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000240// A sequence of bits starting from index B up to and including index E.
241// If E < B, the mask represents two sections: [0..E] and [B..W) where
242// W is the width of the register.
243struct BitTracker::BitMask {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000244 BitMask() = default;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000245 BitMask(uint16_t b, uint16_t e) : B(b), E(e) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000246
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000247 uint16_t first() const { return B; }
248 uint16_t last() const { return E; }
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000249
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000250private:
251 uint16_t B = 0;
252 uint16_t E = 0;
253};
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000254
255// Representation of a register: a list of BitValues.
256struct BitTracker::RegisterCell {
257 RegisterCell(uint16_t Width = DefaultBitN) : Bits(Width) {}
258
259 uint16_t width() const {
260 return Bits.size();
261 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000262
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000263 const BitValue &operator[](uint16_t BitN) const {
264 assert(BitN < Bits.size());
265 return Bits[BitN];
266 }
267 BitValue &operator[](uint16_t BitN) {
268 assert(BitN < Bits.size());
269 return Bits[BitN];
270 }
271
272 bool meet(const RegisterCell &RC, unsigned SelfR);
273 RegisterCell &insert(const RegisterCell &RC, const BitMask &M);
274 RegisterCell extract(const BitMask &M) const; // Returns a new cell.
275 RegisterCell &rol(uint16_t Sh); // Rotate left.
276 RegisterCell &fill(uint16_t B, uint16_t E, const BitValue &V);
277 RegisterCell &cat(const RegisterCell &RC); // Concatenate.
278 uint16_t cl(bool B) const;
279 uint16_t ct(bool B) const;
280
281 bool operator== (const RegisterCell &RC) const;
282 bool operator!= (const RegisterCell &RC) const {
283 return !operator==(RC);
284 }
285
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000286 // Generate a "ref" cell for the corresponding register. In the resulting
287 // cell each bit will be described as being the same as the corresponding
288 // bit in register Reg (i.e. the cell is "defined" by register Reg).
289 static RegisterCell self(unsigned Reg, uint16_t Width);
290 // Generate a "top" cell of given size.
291 static RegisterCell top(uint16_t Width);
292 // Generate a cell that is a "ref" to another cell.
293 static RegisterCell ref(const RegisterCell &C);
294
295private:
296 // The DefaultBitN is here only to avoid frequent reallocation of the
297 // memory in the vector.
298 static const unsigned DefaultBitN = 32;
Benjamin Kramerd8861512015-07-13 20:38:16 +0000299 typedef SmallVector<BitValue, DefaultBitN> BitValueList;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000300 BitValueList Bits;
301
Benjamin Kramerd8861512015-07-13 20:38:16 +0000302 friend raw_ostream &operator<<(raw_ostream &OS, const RegisterCell &RC);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000303};
304
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000305inline bool BitTracker::has(unsigned Reg) const {
306 return Map.find(Reg) != Map.end();
307}
308
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000309inline const BitTracker::RegisterCell&
310BitTracker::lookup(unsigned Reg) const {
311 CellMapType::const_iterator F = Map.find(Reg);
312 assert(F != Map.end());
313 return F->second;
314}
315
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000316inline BitTracker::RegisterCell
317BitTracker::RegisterCell::self(unsigned Reg, uint16_t Width) {
318 RegisterCell RC(Width);
319 for (uint16_t i = 0; i < Width; ++i)
320 RC.Bits[i] = BitValue::self(BitRef(Reg, i));
321 return RC;
322}
323
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000324inline BitTracker::RegisterCell
325BitTracker::RegisterCell::top(uint16_t Width) {
326 RegisterCell RC(Width);
327 for (uint16_t i = 0; i < Width; ++i)
328 RC.Bits[i] = BitValue(BitValue::Top);
329 return RC;
330}
331
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000332inline 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) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000348 virtual ~MachineEvaluator() = default;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000349
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;
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000354
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000355 // A result of any operation should use refs to the source cells, not
356 // the cells directly. This function is a convenience wrapper to quickly
357 // generate a ref for a cell corresponding to a register reference.
358 RegisterCell getRef(const RegisterRef &RR, const CellMapType &M) const {
359 RegisterCell RC = getCell(RR, M);
360 return RegisterCell::ref(RC);
361 }
362
363 // Helper functions.
364 // Check if a cell is an immediate value (i.e. all bits are either 0 or 1).
365 bool isInt(const RegisterCell &A) const;
366 // Convert cell to an immediate value.
367 uint64_t toInt(const RegisterCell &A) const;
368
369 // Generate cell from an immediate value.
370 RegisterCell eIMM(int64_t V, uint16_t W) const;
Benjamin Kramerd8861512015-07-13 20:38:16 +0000371 RegisterCell eIMM(const ConstantInt *CI) const;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000372
373 // Arithmetic.
374 RegisterCell eADD(const RegisterCell &A1, const RegisterCell &A2) const;
375 RegisterCell eSUB(const RegisterCell &A1, const RegisterCell &A2) const;
376 RegisterCell eMLS(const RegisterCell &A1, const RegisterCell &A2) const;
377 RegisterCell eMLU(const RegisterCell &A1, const RegisterCell &A2) const;
378
379 // Shifts.
380 RegisterCell eASL(const RegisterCell &A1, uint16_t Sh) const;
381 RegisterCell eLSR(const RegisterCell &A1, uint16_t Sh) const;
382 RegisterCell eASR(const RegisterCell &A1, uint16_t Sh) const;
383
384 // Logical.
385 RegisterCell eAND(const RegisterCell &A1, const RegisterCell &A2) const;
386 RegisterCell eORL(const RegisterCell &A1, const RegisterCell &A2) const;
387 RegisterCell eXOR(const RegisterCell &A1, const RegisterCell &A2) const;
388 RegisterCell eNOT(const RegisterCell &A1) const;
389
390 // Set bit, clear bit.
391 RegisterCell eSET(const RegisterCell &A1, uint16_t BitN) const;
392 RegisterCell eCLR(const RegisterCell &A1, uint16_t BitN) const;
393
394 // Count leading/trailing bits (zeros/ones).
395 RegisterCell eCLB(const RegisterCell &A1, bool B, uint16_t W) const;
396 RegisterCell eCTB(const RegisterCell &A1, bool B, uint16_t W) const;
397
398 // Sign/zero extension.
399 RegisterCell eSXT(const RegisterCell &A1, uint16_t FromN) const;
400 RegisterCell eZXT(const RegisterCell &A1, uint16_t FromN) const;
401
402 // Extract/insert
403 // XTR R,b,e: extract bits from A1 starting at bit b, ending at e-1.
404 // INS R,S,b: take R and replace bits starting from b with S.
405 RegisterCell eXTR(const RegisterCell &A1, uint16_t B, uint16_t E) const;
406 RegisterCell eINS(const RegisterCell &A1, const RegisterCell &A2,
407 uint16_t AtN) const;
408
409 // User-provided functions for individual targets:
410
411 // Return a sub-register mask that indicates which bits in Reg belong
412 // to the subregister Sub. These bits are assumed to be contiguous in
413 // the super-register, and have the same ordering in the sub-register
414 // as in the super-register. It is valid to call this function with
415 // Sub == 0, in this case, the function should return a mask that spans
416 // the entire register Reg (which is what the default implementation
417 // does).
418 virtual BitMask mask(unsigned Reg, unsigned Sub) const;
419 // Indicate whether a given register class should be tracked.
Benjamin Kramerd8861512015-07-13 20:38:16 +0000420 virtual bool track(const TargetRegisterClass *RC) const { return true; }
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000421 // Evaluate a non-branching machine instruction, given the cell map with
422 // the input values. Place the results in the Outputs map. Return "true"
423 // if evaluation succeeded, "false" otherwise.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000424 virtual bool evaluate(const MachineInstr &MI, const CellMapType &Inputs,
Benjamin Kramerd8861512015-07-13 20:38:16 +0000425 CellMapType &Outputs) const;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000426 // Evaluate a branch, given the cell map with the input values. Fill out
427 // a list of all possible branch targets and indicate (through a flag)
428 // whether the branch could fall-through. Return "true" if this information
429 // has been successfully computed, "false" otherwise.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000430 virtual bool evaluate(const MachineInstr &BI, const CellMapType &Inputs,
Benjamin Kramerd8861512015-07-13 20:38:16 +0000431 BranchTargetList &Targets, bool &FallsThru) const = 0;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000432
Benjamin Kramerd8861512015-07-13 20:38:16 +0000433 const TargetRegisterInfo &TRI;
434 MachineRegisterInfo &MRI;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000435};
436
Benjamin Kramerd8861512015-07-13 20:38:16 +0000437} // end namespace llvm
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000438
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000439#endif // LLVM_LIB_TARGET_HEXAGON_BITTRACKER_H