1*9880d681SAndroid Build Coastguard Worker //===--- BitTracker.cpp ---------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker // SSA-based bit propagation.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // The purpose of this code is, for a given virtual register, to provide
13*9880d681SAndroid Build Coastguard Worker // information about the value of each bit in the register. The values
14*9880d681SAndroid Build Coastguard Worker // of bits are represented by the class BitValue, and take one of four
15*9880d681SAndroid Build Coastguard Worker // cases: 0, 1, "ref" and "bottom". The 0 and 1 are rather clear, the
16*9880d681SAndroid Build Coastguard Worker // "ref" value means that the bit is a copy of another bit (which itself
17*9880d681SAndroid Build Coastguard Worker // cannot be a copy of yet another bit---such chains are not allowed).
18*9880d681SAndroid Build Coastguard Worker // A "ref" value is associated with a BitRef structure, which indicates
19*9880d681SAndroid Build Coastguard Worker // which virtual register, and which bit in that register is the origin
20*9880d681SAndroid Build Coastguard Worker // of the value. For example, given an instruction
21*9880d681SAndroid Build Coastguard Worker // vreg2 = ASL vreg1, 1
22*9880d681SAndroid Build Coastguard Worker // assuming that nothing is known about bits of vreg1, bit 1 of vreg2
23*9880d681SAndroid Build Coastguard Worker // will be a "ref" to (vreg1, 0). If there is a subsequent instruction
24*9880d681SAndroid Build Coastguard Worker // vreg3 = ASL vreg2, 2
25*9880d681SAndroid Build Coastguard Worker // then bit 3 of vreg3 will be a "ref" to (vreg1, 0) as well.
26*9880d681SAndroid Build Coastguard Worker // The "bottom" case means that the bit's value cannot be determined,
27*9880d681SAndroid Build Coastguard Worker // and that this virtual register actually defines it. The "bottom" case
28*9880d681SAndroid Build Coastguard Worker // is discussed in detail in BitTracker.h. In fact, "bottom" is a "ref
29*9880d681SAndroid Build Coastguard Worker // to self", so for the vreg1 above, the bit 0 of it will be a "ref" to
30*9880d681SAndroid Build Coastguard Worker // (vreg1, 0), bit 1 will be a "ref" to (vreg1, 1), etc.
31*9880d681SAndroid Build Coastguard Worker //
32*9880d681SAndroid Build Coastguard Worker // The tracker implements the Wegman-Zadeck algorithm, originally developed
33*9880d681SAndroid Build Coastguard Worker // for SSA-based constant propagation. Each register is represented as
34*9880d681SAndroid Build Coastguard Worker // a sequence of bits, with the convention that bit 0 is the least signi-
35*9880d681SAndroid Build Coastguard Worker // ficant bit. Each bit is propagated individually. The class RegisterCell
36*9880d681SAndroid Build Coastguard Worker // implements the register's representation, and is also the subject of
37*9880d681SAndroid Build Coastguard Worker // the lattice operations in the tracker.
38*9880d681SAndroid Build Coastguard Worker //
39*9880d681SAndroid Build Coastguard Worker // The intended usage of the bit tracker is to create a target-specific
40*9880d681SAndroid Build Coastguard Worker // machine instruction evaluator, pass the evaluator to the BitTracker
41*9880d681SAndroid Build Coastguard Worker // object, and run the tracker. The tracker will then collect the bit
42*9880d681SAndroid Build Coastguard Worker // value information for a given machine function. After that, it can be
43*9880d681SAndroid Build Coastguard Worker // queried for the cells for each virtual register.
44*9880d681SAndroid Build Coastguard Worker // Sample code:
45*9880d681SAndroid Build Coastguard Worker // const TargetSpecificEvaluator TSE(TRI, MRI);
46*9880d681SAndroid Build Coastguard Worker // BitTracker BT(TSE, MF);
47*9880d681SAndroid Build Coastguard Worker // BT.run();
48*9880d681SAndroid Build Coastguard Worker // ...
49*9880d681SAndroid Build Coastguard Worker // unsigned Reg = interestingRegister();
50*9880d681SAndroid Build Coastguard Worker // RegisterCell RC = BT.get(Reg);
51*9880d681SAndroid Build Coastguard Worker // if (RC[3].is(1))
52*9880d681SAndroid Build Coastguard Worker // Reg0bit3 = 1;
53*9880d681SAndroid Build Coastguard Worker //
54*9880d681SAndroid Build Coastguard Worker // The code below is intended to be fully target-independent.
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
57*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
58*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
59*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
60*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
61*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
62*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
63*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker #include "BitTracker.h"
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker using namespace llvm;
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker typedef BitTracker BT;
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker namespace {
72*9880d681SAndroid Build Coastguard Worker // Local trickery to pretty print a register (without the whole "%vreg"
73*9880d681SAndroid Build Coastguard Worker // business).
74*9880d681SAndroid Build Coastguard Worker struct printv {
printv__anon02492b020111::printv75*9880d681SAndroid Build Coastguard Worker printv(unsigned r) : R(r) {}
76*9880d681SAndroid Build Coastguard Worker unsigned R;
77*9880d681SAndroid Build Coastguard Worker };
operator <<(raw_ostream & OS,const printv & PV)78*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const printv &PV) {
79*9880d681SAndroid Build Coastguard Worker if (PV.R)
80*9880d681SAndroid Build Coastguard Worker OS << 'v' << TargetRegisterInfo::virtReg2Index(PV.R);
81*9880d681SAndroid Build Coastguard Worker else
82*9880d681SAndroid Build Coastguard Worker OS << 's';
83*9880d681SAndroid Build Coastguard Worker return OS;
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker namespace llvm {
operator <<(raw_ostream & OS,const BT::BitValue & BV)88*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<<(raw_ostream &OS, const BT::BitValue &BV) {
89*9880d681SAndroid Build Coastguard Worker switch (BV.Type) {
90*9880d681SAndroid Build Coastguard Worker case BT::BitValue::Top:
91*9880d681SAndroid Build Coastguard Worker OS << 'T';
92*9880d681SAndroid Build Coastguard Worker break;
93*9880d681SAndroid Build Coastguard Worker case BT::BitValue::Zero:
94*9880d681SAndroid Build Coastguard Worker OS << '0';
95*9880d681SAndroid Build Coastguard Worker break;
96*9880d681SAndroid Build Coastguard Worker case BT::BitValue::One:
97*9880d681SAndroid Build Coastguard Worker OS << '1';
98*9880d681SAndroid Build Coastguard Worker break;
99*9880d681SAndroid Build Coastguard Worker case BT::BitValue::Ref:
100*9880d681SAndroid Build Coastguard Worker OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']';
101*9880d681SAndroid Build Coastguard Worker break;
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker return OS;
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker
operator <<(raw_ostream & OS,const BT::RegisterCell & RC)106*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<<(raw_ostream &OS, const BT::RegisterCell &RC) {
107*9880d681SAndroid Build Coastguard Worker unsigned n = RC.Bits.size();
108*9880d681SAndroid Build Coastguard Worker OS << "{ w:" << n;
109*9880d681SAndroid Build Coastguard Worker // Instead of printing each bit value individually, try to group them
110*9880d681SAndroid Build Coastguard Worker // into logical segments, such as sequences of 0 or 1 bits or references
111*9880d681SAndroid Build Coastguard Worker // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz").
112*9880d681SAndroid Build Coastguard Worker // "Start" will be the index of the beginning of the most recent segment.
113*9880d681SAndroid Build Coastguard Worker unsigned Start = 0;
114*9880d681SAndroid Build Coastguard Worker bool SeqRef = false; // A sequence of refs to consecutive bits.
115*9880d681SAndroid Build Coastguard Worker bool ConstRef = false; // A sequence of refs to the same bit.
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) {
118*9880d681SAndroid Build Coastguard Worker const BT::BitValue &V = RC[i];
119*9880d681SAndroid Build Coastguard Worker const BT::BitValue &SV = RC[Start];
120*9880d681SAndroid Build Coastguard Worker bool IsRef = (V.Type == BT::BitValue::Ref);
121*9880d681SAndroid Build Coastguard Worker // If the current value is the same as Start, skip to the next one.
122*9880d681SAndroid Build Coastguard Worker if (!IsRef && V == SV)
123*9880d681SAndroid Build Coastguard Worker continue;
124*9880d681SAndroid Build Coastguard Worker if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) {
125*9880d681SAndroid Build Coastguard Worker if (Start+1 == i) {
126*9880d681SAndroid Build Coastguard Worker SeqRef = (V.RefI.Pos == SV.RefI.Pos+1);
127*9880d681SAndroid Build Coastguard Worker ConstRef = (V.RefI.Pos == SV.RefI.Pos);
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start))
130*9880d681SAndroid Build Coastguard Worker continue;
131*9880d681SAndroid Build Coastguard Worker if (ConstRef && V.RefI.Pos == SV.RefI.Pos)
132*9880d681SAndroid Build Coastguard Worker continue;
133*9880d681SAndroid Build Coastguard Worker }
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker // The current value is different. Print the previous one and reset
136*9880d681SAndroid Build Coastguard Worker // the Start.
137*9880d681SAndroid Build Coastguard Worker OS << " [" << Start;
138*9880d681SAndroid Build Coastguard Worker unsigned Count = i - Start;
139*9880d681SAndroid Build Coastguard Worker if (Count == 1) {
140*9880d681SAndroid Build Coastguard Worker OS << "]:" << SV;
141*9880d681SAndroid Build Coastguard Worker } else {
142*9880d681SAndroid Build Coastguard Worker OS << '-' << i-1 << "]:";
143*9880d681SAndroid Build Coastguard Worker if (SV.Type == BT::BitValue::Ref && SeqRef)
144*9880d681SAndroid Build Coastguard Worker OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
145*9880d681SAndroid Build Coastguard Worker << SV.RefI.Pos+(Count-1) << ']';
146*9880d681SAndroid Build Coastguard Worker else
147*9880d681SAndroid Build Coastguard Worker OS << SV;
148*9880d681SAndroid Build Coastguard Worker }
149*9880d681SAndroid Build Coastguard Worker Start = i;
150*9880d681SAndroid Build Coastguard Worker SeqRef = ConstRef = false;
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker OS << " [" << Start;
154*9880d681SAndroid Build Coastguard Worker unsigned Count = n - Start;
155*9880d681SAndroid Build Coastguard Worker if (n-Start == 1) {
156*9880d681SAndroid Build Coastguard Worker OS << "]:" << RC[Start];
157*9880d681SAndroid Build Coastguard Worker } else {
158*9880d681SAndroid Build Coastguard Worker OS << '-' << n-1 << "]:";
159*9880d681SAndroid Build Coastguard Worker const BT::BitValue &SV = RC[Start];
160*9880d681SAndroid Build Coastguard Worker if (SV.Type == BT::BitValue::Ref && SeqRef)
161*9880d681SAndroid Build Coastguard Worker OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
162*9880d681SAndroid Build Coastguard Worker << SV.RefI.Pos+(Count-1) << ']';
163*9880d681SAndroid Build Coastguard Worker else
164*9880d681SAndroid Build Coastguard Worker OS << SV;
165*9880d681SAndroid Build Coastguard Worker }
166*9880d681SAndroid Build Coastguard Worker OS << " }";
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker return OS;
169*9880d681SAndroid Build Coastguard Worker }
170*9880d681SAndroid Build Coastguard Worker }
171*9880d681SAndroid Build Coastguard Worker
BitTracker(const MachineEvaluator & E,MachineFunction & F)172*9880d681SAndroid Build Coastguard Worker BitTracker::BitTracker(const MachineEvaluator &E, MachineFunction &F)
173*9880d681SAndroid Build Coastguard Worker : Trace(false), ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType) {}
174*9880d681SAndroid Build Coastguard Worker
~BitTracker()175*9880d681SAndroid Build Coastguard Worker BitTracker::~BitTracker() {
176*9880d681SAndroid Build Coastguard Worker delete ⤅
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker // If we were allowed to update a cell for a part of a register, the meet
181*9880d681SAndroid Build Coastguard Worker // operation would need to be parametrized by the register number and the
182*9880d681SAndroid Build Coastguard Worker // exact part of the register, so that the computer BitRefs correspond to
183*9880d681SAndroid Build Coastguard Worker // the actual bits of the "self" register.
184*9880d681SAndroid Build Coastguard Worker // While this cannot happen in the current implementation, I'm not sure
185*9880d681SAndroid Build Coastguard Worker // if this should be ruled out in the future.
meet(const RegisterCell & RC,unsigned SelfR)186*9880d681SAndroid Build Coastguard Worker bool BT::RegisterCell::meet(const RegisterCell &RC, unsigned SelfR) {
187*9880d681SAndroid Build Coastguard Worker // An example when "meet" can be invoked with SelfR == 0 is a phi node
188*9880d681SAndroid Build Coastguard Worker // with a physical register as an operand.
189*9880d681SAndroid Build Coastguard Worker assert(SelfR == 0 || TargetRegisterInfo::isVirtualRegister(SelfR));
190*9880d681SAndroid Build Coastguard Worker bool Changed = false;
191*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0, n = Bits.size(); i < n; ++i) {
192*9880d681SAndroid Build Coastguard Worker const BitValue &RCV = RC[i];
193*9880d681SAndroid Build Coastguard Worker Changed |= Bits[i].meet(RCV, BitRef(SelfR, i));
194*9880d681SAndroid Build Coastguard Worker }
195*9880d681SAndroid Build Coastguard Worker return Changed;
196*9880d681SAndroid Build Coastguard Worker }
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker // Insert the entire cell RC into the current cell at position given by M.
insert(const BT::RegisterCell & RC,const BitMask & M)200*9880d681SAndroid Build Coastguard Worker BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC,
201*9880d681SAndroid Build Coastguard Worker const BitMask &M) {
202*9880d681SAndroid Build Coastguard Worker uint16_t B = M.first(), E = M.last(), W = width();
203*9880d681SAndroid Build Coastguard Worker // Sanity: M must be a valid mask for *this.
204*9880d681SAndroid Build Coastguard Worker assert(B < W && E < W);
205*9880d681SAndroid Build Coastguard Worker // Sanity: the masked part of *this must have the same number of bits
206*9880d681SAndroid Build Coastguard Worker // as the source.
207*9880d681SAndroid Build Coastguard Worker assert(B > E || E-B+1 == RC.width()); // B <= E => E-B+1 = |RC|.
208*9880d681SAndroid Build Coastguard Worker assert(B <= E || E+(W-B)+1 == RC.width()); // E < B => E+(W-B)+1 = |RC|.
209*9880d681SAndroid Build Coastguard Worker if (B <= E) {
210*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i <= E-B; ++i)
211*9880d681SAndroid Build Coastguard Worker Bits[i+B] = RC[i];
212*9880d681SAndroid Build Coastguard Worker } else {
213*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W-B; ++i)
214*9880d681SAndroid Build Coastguard Worker Bits[i+B] = RC[i];
215*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i <= E; ++i)
216*9880d681SAndroid Build Coastguard Worker Bits[i] = RC[i+(W-B)];
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker return *this;
219*9880d681SAndroid Build Coastguard Worker }
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker
extract(const BitMask & M) const222*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const {
223*9880d681SAndroid Build Coastguard Worker uint16_t B = M.first(), E = M.last(), W = width();
224*9880d681SAndroid Build Coastguard Worker assert(B < W && E < W);
225*9880d681SAndroid Build Coastguard Worker if (B <= E) {
226*9880d681SAndroid Build Coastguard Worker RegisterCell RC(E-B+1);
227*9880d681SAndroid Build Coastguard Worker for (uint16_t i = B; i <= E; ++i)
228*9880d681SAndroid Build Coastguard Worker RC.Bits[i-B] = Bits[i];
229*9880d681SAndroid Build Coastguard Worker return RC;
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker RegisterCell RC(E+(W-B)+1);
233*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W-B; ++i)
234*9880d681SAndroid Build Coastguard Worker RC.Bits[i] = Bits[i+B];
235*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i <= E; ++i)
236*9880d681SAndroid Build Coastguard Worker RC.Bits[i+(W-B)] = Bits[i];
237*9880d681SAndroid Build Coastguard Worker return RC;
238*9880d681SAndroid Build Coastguard Worker }
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker
rol(uint16_t Sh)241*9880d681SAndroid Build Coastguard Worker BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) {
242*9880d681SAndroid Build Coastguard Worker // Rotate left (i.e. towards increasing bit indices).
243*9880d681SAndroid Build Coastguard Worker // Swap the two parts: [0..W-Sh-1] [W-Sh..W-1]
244*9880d681SAndroid Build Coastguard Worker uint16_t W = width();
245*9880d681SAndroid Build Coastguard Worker Sh = Sh % W;
246*9880d681SAndroid Build Coastguard Worker if (Sh == 0)
247*9880d681SAndroid Build Coastguard Worker return *this;
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker RegisterCell Tmp(W-Sh);
250*9880d681SAndroid Build Coastguard Worker // Tmp = [0..W-Sh-1].
251*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W-Sh; ++i)
252*9880d681SAndroid Build Coastguard Worker Tmp[i] = Bits[i];
253*9880d681SAndroid Build Coastguard Worker // Shift [W-Sh..W-1] to [0..Sh-1].
254*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < Sh; ++i)
255*9880d681SAndroid Build Coastguard Worker Bits[i] = Bits[W-Sh+i];
256*9880d681SAndroid Build Coastguard Worker // Copy Tmp to [Sh..W-1].
257*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W-Sh; ++i)
258*9880d681SAndroid Build Coastguard Worker Bits[i+Sh] = Tmp.Bits[i];
259*9880d681SAndroid Build Coastguard Worker return *this;
260*9880d681SAndroid Build Coastguard Worker }
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker
fill(uint16_t B,uint16_t E,const BitValue & V)263*9880d681SAndroid Build Coastguard Worker BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E,
264*9880d681SAndroid Build Coastguard Worker const BitValue &V) {
265*9880d681SAndroid Build Coastguard Worker assert(B <= E);
266*9880d681SAndroid Build Coastguard Worker while (B < E)
267*9880d681SAndroid Build Coastguard Worker Bits[B++] = V;
268*9880d681SAndroid Build Coastguard Worker return *this;
269*9880d681SAndroid Build Coastguard Worker }
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker
cat(const RegisterCell & RC)272*9880d681SAndroid Build Coastguard Worker BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) {
273*9880d681SAndroid Build Coastguard Worker // Append the cell given as the argument to the "this" cell.
274*9880d681SAndroid Build Coastguard Worker // Bit 0 of RC becomes bit W of the result, where W is this->width().
275*9880d681SAndroid Build Coastguard Worker uint16_t W = width(), WRC = RC.width();
276*9880d681SAndroid Build Coastguard Worker Bits.resize(W+WRC);
277*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < WRC; ++i)
278*9880d681SAndroid Build Coastguard Worker Bits[i+W] = RC.Bits[i];
279*9880d681SAndroid Build Coastguard Worker return *this;
280*9880d681SAndroid Build Coastguard Worker }
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker
ct(bool B) const283*9880d681SAndroid Build Coastguard Worker uint16_t BT::RegisterCell::ct(bool B) const {
284*9880d681SAndroid Build Coastguard Worker uint16_t W = width();
285*9880d681SAndroid Build Coastguard Worker uint16_t C = 0;
286*9880d681SAndroid Build Coastguard Worker BitValue V = B;
287*9880d681SAndroid Build Coastguard Worker while (C < W && Bits[C] == V)
288*9880d681SAndroid Build Coastguard Worker C++;
289*9880d681SAndroid Build Coastguard Worker return C;
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker
292*9880d681SAndroid Build Coastguard Worker
cl(bool B) const293*9880d681SAndroid Build Coastguard Worker uint16_t BT::RegisterCell::cl(bool B) const {
294*9880d681SAndroid Build Coastguard Worker uint16_t W = width();
295*9880d681SAndroid Build Coastguard Worker uint16_t C = 0;
296*9880d681SAndroid Build Coastguard Worker BitValue V = B;
297*9880d681SAndroid Build Coastguard Worker while (C < W && Bits[W-(C+1)] == V)
298*9880d681SAndroid Build Coastguard Worker C++;
299*9880d681SAndroid Build Coastguard Worker return C;
300*9880d681SAndroid Build Coastguard Worker }
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard Worker
operator ==(const RegisterCell & RC) const303*9880d681SAndroid Build Coastguard Worker bool BT::RegisterCell::operator== (const RegisterCell &RC) const {
304*9880d681SAndroid Build Coastguard Worker uint16_t W = Bits.size();
305*9880d681SAndroid Build Coastguard Worker if (RC.Bits.size() != W)
306*9880d681SAndroid Build Coastguard Worker return false;
307*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i)
308*9880d681SAndroid Build Coastguard Worker if (Bits[i] != RC[i])
309*9880d681SAndroid Build Coastguard Worker return false;
310*9880d681SAndroid Build Coastguard Worker return true;
311*9880d681SAndroid Build Coastguard Worker }
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker
getRegBitWidth(const RegisterRef & RR) const314*9880d681SAndroid Build Coastguard Worker uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const {
315*9880d681SAndroid Build Coastguard Worker // The general problem is with finding a register class that corresponds
316*9880d681SAndroid Build Coastguard Worker // to a given reference reg:sub. There can be several such classes, and
317*9880d681SAndroid Build Coastguard Worker // since we only care about the register size, it does not matter which
318*9880d681SAndroid Build Coastguard Worker // such class we would find.
319*9880d681SAndroid Build Coastguard Worker // The easiest way to accomplish what we want is to
320*9880d681SAndroid Build Coastguard Worker // 1. find a physical register PhysR from the same class as RR.Reg,
321*9880d681SAndroid Build Coastguard Worker // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub,
322*9880d681SAndroid Build Coastguard Worker // 3. find a register class that contains PhysS.
323*9880d681SAndroid Build Coastguard Worker unsigned PhysR;
324*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
325*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *VC = MRI.getRegClass(RR.Reg);
326*9880d681SAndroid Build Coastguard Worker assert(VC->begin() != VC->end() && "Empty register class");
327*9880d681SAndroid Build Coastguard Worker PhysR = *VC->begin();
328*9880d681SAndroid Build Coastguard Worker } else {
329*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
330*9880d681SAndroid Build Coastguard Worker PhysR = RR.Reg;
331*9880d681SAndroid Build Coastguard Worker }
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker unsigned PhysS = (RR.Sub == 0) ? PhysR : TRI.getSubReg(PhysR, RR.Sub);
334*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PhysS);
335*9880d681SAndroid Build Coastguard Worker uint16_t BW = RC->getSize()*8;
336*9880d681SAndroid Build Coastguard Worker return BW;
337*9880d681SAndroid Build Coastguard Worker }
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard Worker
getCell(const RegisterRef & RR,const CellMapType & M) const340*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR,
341*9880d681SAndroid Build Coastguard Worker const CellMapType &M) const {
342*9880d681SAndroid Build Coastguard Worker uint16_t BW = getRegBitWidth(RR);
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard Worker // Physical registers are assumed to be present in the map with an unknown
345*9880d681SAndroid Build Coastguard Worker // value. Don't actually insert anything in the map, just return the cell.
346*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(RR.Reg))
347*9880d681SAndroid Build Coastguard Worker return RegisterCell::self(0, BW);
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(RR.Reg));
350*9880d681SAndroid Build Coastguard Worker // For virtual registers that belong to a class that is not tracked,
351*9880d681SAndroid Build Coastguard Worker // generate an "unknown" value as well.
352*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *C = MRI.getRegClass(RR.Reg);
353*9880d681SAndroid Build Coastguard Worker if (!track(C))
354*9880d681SAndroid Build Coastguard Worker return RegisterCell::self(0, BW);
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard Worker CellMapType::const_iterator F = M.find(RR.Reg);
357*9880d681SAndroid Build Coastguard Worker if (F != M.end()) {
358*9880d681SAndroid Build Coastguard Worker if (!RR.Sub)
359*9880d681SAndroid Build Coastguard Worker return F->second;
360*9880d681SAndroid Build Coastguard Worker BitMask M = mask(RR.Reg, RR.Sub);
361*9880d681SAndroid Build Coastguard Worker return F->second.extract(M);
362*9880d681SAndroid Build Coastguard Worker }
363*9880d681SAndroid Build Coastguard Worker // If not found, create a "top" entry, but do not insert it in the map.
364*9880d681SAndroid Build Coastguard Worker return RegisterCell::top(BW);
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker
putCell(const RegisterRef & RR,RegisterCell RC,CellMapType & M) const368*9880d681SAndroid Build Coastguard Worker void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC,
369*9880d681SAndroid Build Coastguard Worker CellMapType &M) const {
370*9880d681SAndroid Build Coastguard Worker // While updating the cell map can be done in a meaningful way for
371*9880d681SAndroid Build Coastguard Worker // a part of a register, it makes little sense to implement it as the
372*9880d681SAndroid Build Coastguard Worker // SSA representation would never contain such "partial definitions".
373*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
374*9880d681SAndroid Build Coastguard Worker return;
375*9880d681SAndroid Build Coastguard Worker assert(RR.Sub == 0 && "Unexpected sub-register in definition");
376*9880d681SAndroid Build Coastguard Worker // Eliminate all ref-to-reg-0 bit values: replace them with "self".
377*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = RC.width(); i < n; ++i) {
378*9880d681SAndroid Build Coastguard Worker const BitValue &V = RC[i];
379*9880d681SAndroid Build Coastguard Worker if (V.Type == BitValue::Ref && V.RefI.Reg == 0)
380*9880d681SAndroid Build Coastguard Worker RC[i].RefI = BitRef(RR.Reg, i);
381*9880d681SAndroid Build Coastguard Worker }
382*9880d681SAndroid Build Coastguard Worker M[RR.Reg] = RC;
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker // Check if the cell represents a compile-time integer value.
isInt(const RegisterCell & A) const387*9880d681SAndroid Build Coastguard Worker bool BT::MachineEvaluator::isInt(const RegisterCell &A) const {
388*9880d681SAndroid Build Coastguard Worker uint16_t W = A.width();
389*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i)
390*9880d681SAndroid Build Coastguard Worker if (!A[i].is(0) && !A[i].is(1))
391*9880d681SAndroid Build Coastguard Worker return false;
392*9880d681SAndroid Build Coastguard Worker return true;
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard Worker // Convert a cell to the integer value. The result must fit in uint64_t.
toInt(const RegisterCell & A) const397*9880d681SAndroid Build Coastguard Worker uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const {
398*9880d681SAndroid Build Coastguard Worker assert(isInt(A));
399*9880d681SAndroid Build Coastguard Worker uint64_t Val = 0;
400*9880d681SAndroid Build Coastguard Worker uint16_t W = A.width();
401*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i) {
402*9880d681SAndroid Build Coastguard Worker Val <<= 1;
403*9880d681SAndroid Build Coastguard Worker Val |= A[i].is(1);
404*9880d681SAndroid Build Coastguard Worker }
405*9880d681SAndroid Build Coastguard Worker return Val;
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker // Evaluator helper functions. These implement some common operation on
410*9880d681SAndroid Build Coastguard Worker // register cells that can be used to implement target-specific instructions
411*9880d681SAndroid Build Coastguard Worker // in a target-specific evaluator.
412*9880d681SAndroid Build Coastguard Worker
eIMM(int64_t V,uint16_t W) const413*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const {
414*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
415*9880d681SAndroid Build Coastguard Worker // For bits beyond the 63rd, this will generate the sign bit of V.
416*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i) {
417*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue(V & 1);
418*9880d681SAndroid Build Coastguard Worker V >>= 1;
419*9880d681SAndroid Build Coastguard Worker }
420*9880d681SAndroid Build Coastguard Worker return Res;
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker
423*9880d681SAndroid Build Coastguard Worker
eIMM(const ConstantInt * CI) const424*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const {
425*9880d681SAndroid Build Coastguard Worker const APInt &A = CI->getValue();
426*9880d681SAndroid Build Coastguard Worker uint16_t BW = A.getBitWidth();
427*9880d681SAndroid Build Coastguard Worker assert((unsigned)BW == A.getBitWidth() && "BitWidth overflow");
428*9880d681SAndroid Build Coastguard Worker RegisterCell Res(BW);
429*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < BW; ++i)
430*9880d681SAndroid Build Coastguard Worker Res[i] = A[i];
431*9880d681SAndroid Build Coastguard Worker return Res;
432*9880d681SAndroid Build Coastguard Worker }
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker
eADD(const RegisterCell & A1,const RegisterCell & A2) const435*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1,
436*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
437*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
438*9880d681SAndroid Build Coastguard Worker assert(W == A2.width());
439*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
440*9880d681SAndroid Build Coastguard Worker bool Carry = false;
441*9880d681SAndroid Build Coastguard Worker uint16_t I;
442*9880d681SAndroid Build Coastguard Worker for (I = 0; I < W; ++I) {
443*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[I];
444*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[I];
445*9880d681SAndroid Build Coastguard Worker if (!V1.num() || !V2.num())
446*9880d681SAndroid Build Coastguard Worker break;
447*9880d681SAndroid Build Coastguard Worker unsigned S = bool(V1) + bool(V2) + Carry;
448*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue(S & 1);
449*9880d681SAndroid Build Coastguard Worker Carry = (S > 1);
450*9880d681SAndroid Build Coastguard Worker }
451*9880d681SAndroid Build Coastguard Worker for (; I < W; ++I) {
452*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[I];
453*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[I];
454*9880d681SAndroid Build Coastguard Worker // If the next bit is same as Carry, the result will be 0 plus the
455*9880d681SAndroid Build Coastguard Worker // other bit. The Carry bit will remain unchanged.
456*9880d681SAndroid Build Coastguard Worker if (V1.is(Carry))
457*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue::ref(V2);
458*9880d681SAndroid Build Coastguard Worker else if (V2.is(Carry))
459*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue::ref(V1);
460*9880d681SAndroid Build Coastguard Worker else
461*9880d681SAndroid Build Coastguard Worker break;
462*9880d681SAndroid Build Coastguard Worker }
463*9880d681SAndroid Build Coastguard Worker for (; I < W; ++I)
464*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue::self();
465*9880d681SAndroid Build Coastguard Worker return Res;
466*9880d681SAndroid Build Coastguard Worker }
467*9880d681SAndroid Build Coastguard Worker
468*9880d681SAndroid Build Coastguard Worker
eSUB(const RegisterCell & A1,const RegisterCell & A2) const469*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1,
470*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
471*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
472*9880d681SAndroid Build Coastguard Worker assert(W == A2.width());
473*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
474*9880d681SAndroid Build Coastguard Worker bool Borrow = false;
475*9880d681SAndroid Build Coastguard Worker uint16_t I;
476*9880d681SAndroid Build Coastguard Worker for (I = 0; I < W; ++I) {
477*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[I];
478*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[I];
479*9880d681SAndroid Build Coastguard Worker if (!V1.num() || !V2.num())
480*9880d681SAndroid Build Coastguard Worker break;
481*9880d681SAndroid Build Coastguard Worker unsigned S = bool(V1) - bool(V2) - Borrow;
482*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue(S & 1);
483*9880d681SAndroid Build Coastguard Worker Borrow = (S > 1);
484*9880d681SAndroid Build Coastguard Worker }
485*9880d681SAndroid Build Coastguard Worker for (; I < W; ++I) {
486*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[I];
487*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[I];
488*9880d681SAndroid Build Coastguard Worker if (V1.is(Borrow)) {
489*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue::ref(V2);
490*9880d681SAndroid Build Coastguard Worker break;
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker if (V2.is(Borrow))
493*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue::ref(V1);
494*9880d681SAndroid Build Coastguard Worker else
495*9880d681SAndroid Build Coastguard Worker break;
496*9880d681SAndroid Build Coastguard Worker }
497*9880d681SAndroid Build Coastguard Worker for (; I < W; ++I)
498*9880d681SAndroid Build Coastguard Worker Res[I] = BitValue::self();
499*9880d681SAndroid Build Coastguard Worker return Res;
500*9880d681SAndroid Build Coastguard Worker }
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker
eMLS(const RegisterCell & A1,const RegisterCell & A2) const503*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1,
504*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
505*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width() + A2.width();
506*9880d681SAndroid Build Coastguard Worker uint16_t Z = A1.ct(0) + A2.ct(0);
507*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
508*9880d681SAndroid Build Coastguard Worker Res.fill(0, Z, BitValue::Zero);
509*9880d681SAndroid Build Coastguard Worker Res.fill(Z, W, BitValue::self());
510*9880d681SAndroid Build Coastguard Worker return Res;
511*9880d681SAndroid Build Coastguard Worker }
512*9880d681SAndroid Build Coastguard Worker
513*9880d681SAndroid Build Coastguard Worker
eMLU(const RegisterCell & A1,const RegisterCell & A2) const514*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1,
515*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
516*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width() + A2.width();
517*9880d681SAndroid Build Coastguard Worker uint16_t Z = A1.ct(0) + A2.ct(0);
518*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
519*9880d681SAndroid Build Coastguard Worker Res.fill(0, Z, BitValue::Zero);
520*9880d681SAndroid Build Coastguard Worker Res.fill(Z, W, BitValue::self());
521*9880d681SAndroid Build Coastguard Worker return Res;
522*9880d681SAndroid Build Coastguard Worker }
523*9880d681SAndroid Build Coastguard Worker
524*9880d681SAndroid Build Coastguard Worker
eASL(const RegisterCell & A1,uint16_t Sh) const525*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1,
526*9880d681SAndroid Build Coastguard Worker uint16_t Sh) const {
527*9880d681SAndroid Build Coastguard Worker assert(Sh <= A1.width());
528*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
529*9880d681SAndroid Build Coastguard Worker Res.rol(Sh);
530*9880d681SAndroid Build Coastguard Worker Res.fill(0, Sh, BitValue::Zero);
531*9880d681SAndroid Build Coastguard Worker return Res;
532*9880d681SAndroid Build Coastguard Worker }
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker
eLSR(const RegisterCell & A1,uint16_t Sh) const535*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1,
536*9880d681SAndroid Build Coastguard Worker uint16_t Sh) const {
537*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
538*9880d681SAndroid Build Coastguard Worker assert(Sh <= W);
539*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
540*9880d681SAndroid Build Coastguard Worker Res.rol(W-Sh);
541*9880d681SAndroid Build Coastguard Worker Res.fill(W-Sh, W, BitValue::Zero);
542*9880d681SAndroid Build Coastguard Worker return Res;
543*9880d681SAndroid Build Coastguard Worker }
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard Worker
eASR(const RegisterCell & A1,uint16_t Sh) const546*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1,
547*9880d681SAndroid Build Coastguard Worker uint16_t Sh) const {
548*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
549*9880d681SAndroid Build Coastguard Worker assert(Sh <= W);
550*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
551*9880d681SAndroid Build Coastguard Worker BitValue Sign = Res[W-1];
552*9880d681SAndroid Build Coastguard Worker Res.rol(W-Sh);
553*9880d681SAndroid Build Coastguard Worker Res.fill(W-Sh, W, Sign);
554*9880d681SAndroid Build Coastguard Worker return Res;
555*9880d681SAndroid Build Coastguard Worker }
556*9880d681SAndroid Build Coastguard Worker
557*9880d681SAndroid Build Coastguard Worker
eAND(const RegisterCell & A1,const RegisterCell & A2) const558*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1,
559*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
560*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
561*9880d681SAndroid Build Coastguard Worker assert(W == A2.width());
562*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
563*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i) {
564*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[i];
565*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[i];
566*9880d681SAndroid Build Coastguard Worker if (V1.is(1))
567*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::ref(V2);
568*9880d681SAndroid Build Coastguard Worker else if (V2.is(1))
569*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::ref(V1);
570*9880d681SAndroid Build Coastguard Worker else if (V1.is(0) || V2.is(0))
571*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::Zero;
572*9880d681SAndroid Build Coastguard Worker else if (V1 == V2)
573*9880d681SAndroid Build Coastguard Worker Res[i] = V1;
574*9880d681SAndroid Build Coastguard Worker else
575*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::self();
576*9880d681SAndroid Build Coastguard Worker }
577*9880d681SAndroid Build Coastguard Worker return Res;
578*9880d681SAndroid Build Coastguard Worker }
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker
eORL(const RegisterCell & A1,const RegisterCell & A2) const581*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1,
582*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
583*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
584*9880d681SAndroid Build Coastguard Worker assert(W == A2.width());
585*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
586*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i) {
587*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[i];
588*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[i];
589*9880d681SAndroid Build Coastguard Worker if (V1.is(1) || V2.is(1))
590*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::One;
591*9880d681SAndroid Build Coastguard Worker else if (V1.is(0))
592*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::ref(V2);
593*9880d681SAndroid Build Coastguard Worker else if (V2.is(0))
594*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::ref(V1);
595*9880d681SAndroid Build Coastguard Worker else if (V1 == V2)
596*9880d681SAndroid Build Coastguard Worker Res[i] = V1;
597*9880d681SAndroid Build Coastguard Worker else
598*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::self();
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker return Res;
601*9880d681SAndroid Build Coastguard Worker }
602*9880d681SAndroid Build Coastguard Worker
603*9880d681SAndroid Build Coastguard Worker
eXOR(const RegisterCell & A1,const RegisterCell & A2) const604*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1,
605*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2) const {
606*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
607*9880d681SAndroid Build Coastguard Worker assert(W == A2.width());
608*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
609*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i) {
610*9880d681SAndroid Build Coastguard Worker const BitValue &V1 = A1[i];
611*9880d681SAndroid Build Coastguard Worker const BitValue &V2 = A2[i];
612*9880d681SAndroid Build Coastguard Worker if (V1.is(0))
613*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::ref(V2);
614*9880d681SAndroid Build Coastguard Worker else if (V2.is(0))
615*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::ref(V1);
616*9880d681SAndroid Build Coastguard Worker else if (V1 == V2)
617*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::Zero;
618*9880d681SAndroid Build Coastguard Worker else
619*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::self();
620*9880d681SAndroid Build Coastguard Worker }
621*9880d681SAndroid Build Coastguard Worker return Res;
622*9880d681SAndroid Build Coastguard Worker }
623*9880d681SAndroid Build Coastguard Worker
624*9880d681SAndroid Build Coastguard Worker
eNOT(const RegisterCell & A1) const625*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const {
626*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
627*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
628*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0; i < W; ++i) {
629*9880d681SAndroid Build Coastguard Worker const BitValue &V = A1[i];
630*9880d681SAndroid Build Coastguard Worker if (V.is(0))
631*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::One;
632*9880d681SAndroid Build Coastguard Worker else if (V.is(1))
633*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::Zero;
634*9880d681SAndroid Build Coastguard Worker else
635*9880d681SAndroid Build Coastguard Worker Res[i] = BitValue::self();
636*9880d681SAndroid Build Coastguard Worker }
637*9880d681SAndroid Build Coastguard Worker return Res;
638*9880d681SAndroid Build Coastguard Worker }
639*9880d681SAndroid Build Coastguard Worker
640*9880d681SAndroid Build Coastguard Worker
eSET(const RegisterCell & A1,uint16_t BitN) const641*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1,
642*9880d681SAndroid Build Coastguard Worker uint16_t BitN) const {
643*9880d681SAndroid Build Coastguard Worker assert(BitN < A1.width());
644*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
645*9880d681SAndroid Build Coastguard Worker Res[BitN] = BitValue::One;
646*9880d681SAndroid Build Coastguard Worker return Res;
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker
eCLR(const RegisterCell & A1,uint16_t BitN) const650*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1,
651*9880d681SAndroid Build Coastguard Worker uint16_t BitN) const {
652*9880d681SAndroid Build Coastguard Worker assert(BitN < A1.width());
653*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
654*9880d681SAndroid Build Coastguard Worker Res[BitN] = BitValue::Zero;
655*9880d681SAndroid Build Coastguard Worker return Res;
656*9880d681SAndroid Build Coastguard Worker }
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker
eCLB(const RegisterCell & A1,bool B,uint16_t W) const659*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B,
660*9880d681SAndroid Build Coastguard Worker uint16_t W) const {
661*9880d681SAndroid Build Coastguard Worker uint16_t C = A1.cl(B), AW = A1.width();
662*9880d681SAndroid Build Coastguard Worker // If the last leading non-B bit is not a constant, then we don't know
663*9880d681SAndroid Build Coastguard Worker // the real count.
664*9880d681SAndroid Build Coastguard Worker if ((C < AW && A1[AW-1-C].num()) || C == AW)
665*9880d681SAndroid Build Coastguard Worker return eIMM(C, W);
666*9880d681SAndroid Build Coastguard Worker return RegisterCell::self(0, W);
667*9880d681SAndroid Build Coastguard Worker }
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard Worker
eCTB(const RegisterCell & A1,bool B,uint16_t W) const670*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B,
671*9880d681SAndroid Build Coastguard Worker uint16_t W) const {
672*9880d681SAndroid Build Coastguard Worker uint16_t C = A1.ct(B), AW = A1.width();
673*9880d681SAndroid Build Coastguard Worker // If the last trailing non-B bit is not a constant, then we don't know
674*9880d681SAndroid Build Coastguard Worker // the real count.
675*9880d681SAndroid Build Coastguard Worker if ((C < AW && A1[C].num()) || C == AW)
676*9880d681SAndroid Build Coastguard Worker return eIMM(C, W);
677*9880d681SAndroid Build Coastguard Worker return RegisterCell::self(0, W);
678*9880d681SAndroid Build Coastguard Worker }
679*9880d681SAndroid Build Coastguard Worker
680*9880d681SAndroid Build Coastguard Worker
eSXT(const RegisterCell & A1,uint16_t FromN) const681*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1,
682*9880d681SAndroid Build Coastguard Worker uint16_t FromN) const {
683*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
684*9880d681SAndroid Build Coastguard Worker assert(FromN <= W);
685*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
686*9880d681SAndroid Build Coastguard Worker BitValue Sign = Res[FromN-1];
687*9880d681SAndroid Build Coastguard Worker // Sign-extend "inreg".
688*9880d681SAndroid Build Coastguard Worker Res.fill(FromN, W, Sign);
689*9880d681SAndroid Build Coastguard Worker return Res;
690*9880d681SAndroid Build Coastguard Worker }
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard Worker
eZXT(const RegisterCell & A1,uint16_t FromN) const693*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1,
694*9880d681SAndroid Build Coastguard Worker uint16_t FromN) const {
695*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
696*9880d681SAndroid Build Coastguard Worker assert(FromN <= W);
697*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
698*9880d681SAndroid Build Coastguard Worker Res.fill(FromN, W, BitValue::Zero);
699*9880d681SAndroid Build Coastguard Worker return Res;
700*9880d681SAndroid Build Coastguard Worker }
701*9880d681SAndroid Build Coastguard Worker
702*9880d681SAndroid Build Coastguard Worker
eXTR(const RegisterCell & A1,uint16_t B,uint16_t E) const703*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1,
704*9880d681SAndroid Build Coastguard Worker uint16_t B, uint16_t E) const {
705*9880d681SAndroid Build Coastguard Worker uint16_t W = A1.width();
706*9880d681SAndroid Build Coastguard Worker assert(B < W && E <= W);
707*9880d681SAndroid Build Coastguard Worker if (B == E)
708*9880d681SAndroid Build Coastguard Worker return RegisterCell(0);
709*9880d681SAndroid Build Coastguard Worker uint16_t Last = (E > 0) ? E-1 : W-1;
710*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last));
711*9880d681SAndroid Build Coastguard Worker // Return shorter cell.
712*9880d681SAndroid Build Coastguard Worker return Res;
713*9880d681SAndroid Build Coastguard Worker }
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Worker
eINS(const RegisterCell & A1,const RegisterCell & A2,uint16_t AtN) const716*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1,
717*9880d681SAndroid Build Coastguard Worker const RegisterCell &A2, uint16_t AtN) const {
718*9880d681SAndroid Build Coastguard Worker uint16_t W1 = A1.width(), W2 = A2.width();
719*9880d681SAndroid Build Coastguard Worker (void)W1;
720*9880d681SAndroid Build Coastguard Worker assert(AtN < W1 && AtN+W2 <= W1);
721*9880d681SAndroid Build Coastguard Worker // Copy bits from A1, insert A2 at position AtN.
722*9880d681SAndroid Build Coastguard Worker RegisterCell Res = RegisterCell::ref(A1);
723*9880d681SAndroid Build Coastguard Worker if (W2 > 0)
724*9880d681SAndroid Build Coastguard Worker Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1));
725*9880d681SAndroid Build Coastguard Worker return Res;
726*9880d681SAndroid Build Coastguard Worker }
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard Worker
mask(unsigned Reg,unsigned Sub) const729*9880d681SAndroid Build Coastguard Worker BT::BitMask BT::MachineEvaluator::mask(unsigned Reg, unsigned Sub) const {
730*9880d681SAndroid Build Coastguard Worker assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0");
731*9880d681SAndroid Build Coastguard Worker uint16_t W = getRegBitWidth(Reg);
732*9880d681SAndroid Build Coastguard Worker assert(W > 0 && "Cannot generate mask for empty register");
733*9880d681SAndroid Build Coastguard Worker return BitMask(0, W-1);
734*9880d681SAndroid Build Coastguard Worker }
735*9880d681SAndroid Build Coastguard Worker
evaluate(const MachineInstr & MI,const CellMapType & Inputs,CellMapType & Outputs) const736*9880d681SAndroid Build Coastguard Worker bool BT::MachineEvaluator::evaluate(const MachineInstr &MI,
737*9880d681SAndroid Build Coastguard Worker const CellMapType &Inputs,
738*9880d681SAndroid Build Coastguard Worker CellMapType &Outputs) const {
739*9880d681SAndroid Build Coastguard Worker unsigned Opc = MI.getOpcode();
740*9880d681SAndroid Build Coastguard Worker switch (Opc) {
741*9880d681SAndroid Build Coastguard Worker case TargetOpcode::REG_SEQUENCE: {
742*9880d681SAndroid Build Coastguard Worker RegisterRef RD = MI.getOperand(0);
743*9880d681SAndroid Build Coastguard Worker assert(RD.Sub == 0);
744*9880d681SAndroid Build Coastguard Worker RegisterRef RS = MI.getOperand(1);
745*9880d681SAndroid Build Coastguard Worker unsigned SS = MI.getOperand(2).getImm();
746*9880d681SAndroid Build Coastguard Worker RegisterRef RT = MI.getOperand(3);
747*9880d681SAndroid Build Coastguard Worker unsigned ST = MI.getOperand(4).getImm();
748*9880d681SAndroid Build Coastguard Worker assert(SS != ST);
749*9880d681SAndroid Build Coastguard Worker
750*9880d681SAndroid Build Coastguard Worker uint16_t W = getRegBitWidth(RD);
751*9880d681SAndroid Build Coastguard Worker RegisterCell Res(W);
752*9880d681SAndroid Build Coastguard Worker Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS));
753*9880d681SAndroid Build Coastguard Worker Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST));
754*9880d681SAndroid Build Coastguard Worker putCell(RD, Res, Outputs);
755*9880d681SAndroid Build Coastguard Worker break;
756*9880d681SAndroid Build Coastguard Worker }
757*9880d681SAndroid Build Coastguard Worker
758*9880d681SAndroid Build Coastguard Worker case TargetOpcode::COPY: {
759*9880d681SAndroid Build Coastguard Worker // COPY can transfer a smaller register into a wider one.
760*9880d681SAndroid Build Coastguard Worker // If that is the case, fill the remaining high bits with 0.
761*9880d681SAndroid Build Coastguard Worker RegisterRef RD = MI.getOperand(0);
762*9880d681SAndroid Build Coastguard Worker RegisterRef RS = MI.getOperand(1);
763*9880d681SAndroid Build Coastguard Worker assert(RD.Sub == 0);
764*9880d681SAndroid Build Coastguard Worker uint16_t WD = getRegBitWidth(RD);
765*9880d681SAndroid Build Coastguard Worker uint16_t WS = getRegBitWidth(RS);
766*9880d681SAndroid Build Coastguard Worker assert(WD >= WS);
767*9880d681SAndroid Build Coastguard Worker RegisterCell Src = getCell(RS, Inputs);
768*9880d681SAndroid Build Coastguard Worker RegisterCell Res(WD);
769*9880d681SAndroid Build Coastguard Worker Res.insert(Src, BitMask(0, WS-1));
770*9880d681SAndroid Build Coastguard Worker Res.fill(WS, WD, BitValue::Zero);
771*9880d681SAndroid Build Coastguard Worker putCell(RD, Res, Outputs);
772*9880d681SAndroid Build Coastguard Worker break;
773*9880d681SAndroid Build Coastguard Worker }
774*9880d681SAndroid Build Coastguard Worker
775*9880d681SAndroid Build Coastguard Worker default:
776*9880d681SAndroid Build Coastguard Worker return false;
777*9880d681SAndroid Build Coastguard Worker }
778*9880d681SAndroid Build Coastguard Worker
779*9880d681SAndroid Build Coastguard Worker return true;
780*9880d681SAndroid Build Coastguard Worker }
781*9880d681SAndroid Build Coastguard Worker
782*9880d681SAndroid Build Coastguard Worker
783*9880d681SAndroid Build Coastguard Worker // Main W-Z implementation.
784*9880d681SAndroid Build Coastguard Worker
visitPHI(const MachineInstr & PI)785*9880d681SAndroid Build Coastguard Worker void BT::visitPHI(const MachineInstr &PI) {
786*9880d681SAndroid Build Coastguard Worker int ThisN = PI.getParent()->getNumber();
787*9880d681SAndroid Build Coastguard Worker if (Trace)
788*9880d681SAndroid Build Coastguard Worker dbgs() << "Visit FI(BB#" << ThisN << "): " << PI;
789*9880d681SAndroid Build Coastguard Worker
790*9880d681SAndroid Build Coastguard Worker const MachineOperand &MD = PI.getOperand(0);
791*9880d681SAndroid Build Coastguard Worker assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition");
792*9880d681SAndroid Build Coastguard Worker RegisterRef DefRR(MD);
793*9880d681SAndroid Build Coastguard Worker uint16_t DefBW = ME.getRegBitWidth(DefRR);
794*9880d681SAndroid Build Coastguard Worker
795*9880d681SAndroid Build Coastguard Worker RegisterCell DefC = ME.getCell(DefRR, Map);
796*9880d681SAndroid Build Coastguard Worker if (DefC == RegisterCell::self(DefRR.Reg, DefBW)) // XXX slow
797*9880d681SAndroid Build Coastguard Worker return;
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker bool Changed = false;
800*9880d681SAndroid Build Coastguard Worker
801*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, n = PI.getNumOperands(); i < n; i += 2) {
802*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *PB = PI.getOperand(i + 1).getMBB();
803*9880d681SAndroid Build Coastguard Worker int PredN = PB->getNumber();
804*9880d681SAndroid Build Coastguard Worker if (Trace)
805*9880d681SAndroid Build Coastguard Worker dbgs() << " edge BB#" << PredN << "->BB#" << ThisN;
806*9880d681SAndroid Build Coastguard Worker if (!EdgeExec.count(CFGEdge(PredN, ThisN))) {
807*9880d681SAndroid Build Coastguard Worker if (Trace)
808*9880d681SAndroid Build Coastguard Worker dbgs() << " not executable\n";
809*9880d681SAndroid Build Coastguard Worker continue;
810*9880d681SAndroid Build Coastguard Worker }
811*9880d681SAndroid Build Coastguard Worker
812*9880d681SAndroid Build Coastguard Worker RegisterRef RU = PI.getOperand(i);
813*9880d681SAndroid Build Coastguard Worker RegisterCell ResC = ME.getCell(RU, Map);
814*9880d681SAndroid Build Coastguard Worker if (Trace)
815*9880d681SAndroid Build Coastguard Worker dbgs() << " input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
816*9880d681SAndroid Build Coastguard Worker << " cell: " << ResC << "\n";
817*9880d681SAndroid Build Coastguard Worker Changed |= DefC.meet(ResC, DefRR.Reg);
818*9880d681SAndroid Build Coastguard Worker }
819*9880d681SAndroid Build Coastguard Worker
820*9880d681SAndroid Build Coastguard Worker if (Changed) {
821*9880d681SAndroid Build Coastguard Worker if (Trace)
822*9880d681SAndroid Build Coastguard Worker dbgs() << "Output: " << PrintReg(DefRR.Reg, &ME.TRI, DefRR.Sub)
823*9880d681SAndroid Build Coastguard Worker << " cell: " << DefC << "\n";
824*9880d681SAndroid Build Coastguard Worker ME.putCell(DefRR, DefC, Map);
825*9880d681SAndroid Build Coastguard Worker visitUsesOf(DefRR.Reg);
826*9880d681SAndroid Build Coastguard Worker }
827*9880d681SAndroid Build Coastguard Worker }
828*9880d681SAndroid Build Coastguard Worker
visitNonBranch(const MachineInstr & MI)829*9880d681SAndroid Build Coastguard Worker void BT::visitNonBranch(const MachineInstr &MI) {
830*9880d681SAndroid Build Coastguard Worker if (Trace) {
831*9880d681SAndroid Build Coastguard Worker int ThisN = MI.getParent()->getNumber();
832*9880d681SAndroid Build Coastguard Worker dbgs() << "Visit MI(BB#" << ThisN << "): " << MI;
833*9880d681SAndroid Build Coastguard Worker }
834*9880d681SAndroid Build Coastguard Worker if (MI.isDebugValue())
835*9880d681SAndroid Build Coastguard Worker return;
836*9880d681SAndroid Build Coastguard Worker assert(!MI.isBranch() && "Unexpected branch instruction");
837*9880d681SAndroid Build Coastguard Worker
838*9880d681SAndroid Build Coastguard Worker CellMapType ResMap;
839*9880d681SAndroid Build Coastguard Worker bool Eval = ME.evaluate(MI, Map, ResMap);
840*9880d681SAndroid Build Coastguard Worker
841*9880d681SAndroid Build Coastguard Worker if (Trace && Eval) {
842*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
843*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(i);
844*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse())
845*9880d681SAndroid Build Coastguard Worker continue;
846*9880d681SAndroid Build Coastguard Worker RegisterRef RU(MO);
847*9880d681SAndroid Build Coastguard Worker dbgs() << " input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
848*9880d681SAndroid Build Coastguard Worker << " cell: " << ME.getCell(RU, Map) << "\n";
849*9880d681SAndroid Build Coastguard Worker }
850*9880d681SAndroid Build Coastguard Worker dbgs() << "Outputs:\n";
851*9880d681SAndroid Build Coastguard Worker for (CellMapType::iterator I = ResMap.begin(), E = ResMap.end();
852*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
853*9880d681SAndroid Build Coastguard Worker RegisterRef RD(I->first);
854*9880d681SAndroid Build Coastguard Worker dbgs() << " " << PrintReg(I->first, &ME.TRI) << " cell: "
855*9880d681SAndroid Build Coastguard Worker << ME.getCell(RD, ResMap) << "\n";
856*9880d681SAndroid Build Coastguard Worker }
857*9880d681SAndroid Build Coastguard Worker }
858*9880d681SAndroid Build Coastguard Worker
859*9880d681SAndroid Build Coastguard Worker // Iterate over all definitions of the instruction, and update the
860*9880d681SAndroid Build Coastguard Worker // cells accordingly.
861*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
862*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(i);
863*9880d681SAndroid Build Coastguard Worker // Visit register defs only.
864*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
865*9880d681SAndroid Build Coastguard Worker continue;
866*9880d681SAndroid Build Coastguard Worker RegisterRef RD(MO);
867*9880d681SAndroid Build Coastguard Worker assert(RD.Sub == 0 && "Unexpected sub-register in definition");
868*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(RD.Reg))
869*9880d681SAndroid Build Coastguard Worker continue;
870*9880d681SAndroid Build Coastguard Worker
871*9880d681SAndroid Build Coastguard Worker bool Changed = false;
872*9880d681SAndroid Build Coastguard Worker if (!Eval || ResMap.count(RD.Reg) == 0) {
873*9880d681SAndroid Build Coastguard Worker // Set to "ref" (aka "bottom").
874*9880d681SAndroid Build Coastguard Worker uint16_t DefBW = ME.getRegBitWidth(RD);
875*9880d681SAndroid Build Coastguard Worker RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW);
876*9880d681SAndroid Build Coastguard Worker if (RefC != ME.getCell(RD, Map)) {
877*9880d681SAndroid Build Coastguard Worker ME.putCell(RD, RefC, Map);
878*9880d681SAndroid Build Coastguard Worker Changed = true;
879*9880d681SAndroid Build Coastguard Worker }
880*9880d681SAndroid Build Coastguard Worker } else {
881*9880d681SAndroid Build Coastguard Worker RegisterCell DefC = ME.getCell(RD, Map);
882*9880d681SAndroid Build Coastguard Worker RegisterCell ResC = ME.getCell(RD, ResMap);
883*9880d681SAndroid Build Coastguard Worker // This is a non-phi instruction, so the values of the inputs come
884*9880d681SAndroid Build Coastguard Worker // from the same registers each time this instruction is evaluated.
885*9880d681SAndroid Build Coastguard Worker // During the propagation, the values of the inputs can become lowered
886*9880d681SAndroid Build Coastguard Worker // in the sense of the lattice operation, which may cause different
887*9880d681SAndroid Build Coastguard Worker // results to be calculated in subsequent evaluations. This should
888*9880d681SAndroid Build Coastguard Worker // not cause the bottoming of the result in the map, since the new
889*9880d681SAndroid Build Coastguard Worker // result is already reflecting the lowered inputs.
890*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0, w = DefC.width(); i < w; ++i) {
891*9880d681SAndroid Build Coastguard Worker BitValue &V = DefC[i];
892*9880d681SAndroid Build Coastguard Worker // Bits that are already "bottom" should not be updated.
893*9880d681SAndroid Build Coastguard Worker if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg)
894*9880d681SAndroid Build Coastguard Worker continue;
895*9880d681SAndroid Build Coastguard Worker // Same for those that are identical in DefC and ResC.
896*9880d681SAndroid Build Coastguard Worker if (V == ResC[i])
897*9880d681SAndroid Build Coastguard Worker continue;
898*9880d681SAndroid Build Coastguard Worker V = ResC[i];
899*9880d681SAndroid Build Coastguard Worker Changed = true;
900*9880d681SAndroid Build Coastguard Worker }
901*9880d681SAndroid Build Coastguard Worker if (Changed)
902*9880d681SAndroid Build Coastguard Worker ME.putCell(RD, DefC, Map);
903*9880d681SAndroid Build Coastguard Worker }
904*9880d681SAndroid Build Coastguard Worker if (Changed)
905*9880d681SAndroid Build Coastguard Worker visitUsesOf(RD.Reg);
906*9880d681SAndroid Build Coastguard Worker }
907*9880d681SAndroid Build Coastguard Worker }
908*9880d681SAndroid Build Coastguard Worker
visitBranchesFrom(const MachineInstr & BI)909*9880d681SAndroid Build Coastguard Worker void BT::visitBranchesFrom(const MachineInstr &BI) {
910*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock &B = *BI.getParent();
911*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator It = BI, End = B.end();
912*9880d681SAndroid Build Coastguard Worker BranchTargetList Targets, BTs;
913*9880d681SAndroid Build Coastguard Worker bool FallsThrough = true, DefaultToAll = false;
914*9880d681SAndroid Build Coastguard Worker int ThisN = B.getNumber();
915*9880d681SAndroid Build Coastguard Worker
916*9880d681SAndroid Build Coastguard Worker do {
917*9880d681SAndroid Build Coastguard Worker BTs.clear();
918*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI = *It;
919*9880d681SAndroid Build Coastguard Worker if (Trace)
920*9880d681SAndroid Build Coastguard Worker dbgs() << "Visit BR(BB#" << ThisN << "): " << MI;
921*9880d681SAndroid Build Coastguard Worker assert(MI.isBranch() && "Expecting branch instruction");
922*9880d681SAndroid Build Coastguard Worker InstrExec.insert(&MI);
923*9880d681SAndroid Build Coastguard Worker bool Eval = ME.evaluate(MI, Map, BTs, FallsThrough);
924*9880d681SAndroid Build Coastguard Worker if (!Eval) {
925*9880d681SAndroid Build Coastguard Worker // If the evaluation failed, we will add all targets. Keep going in
926*9880d681SAndroid Build Coastguard Worker // the loop to mark all executable branches as such.
927*9880d681SAndroid Build Coastguard Worker DefaultToAll = true;
928*9880d681SAndroid Build Coastguard Worker FallsThrough = true;
929*9880d681SAndroid Build Coastguard Worker if (Trace)
930*9880d681SAndroid Build Coastguard Worker dbgs() << " failed to evaluate: will add all CFG successors\n";
931*9880d681SAndroid Build Coastguard Worker } else if (!DefaultToAll) {
932*9880d681SAndroid Build Coastguard Worker // If evaluated successfully add the targets to the cumulative list.
933*9880d681SAndroid Build Coastguard Worker if (Trace) {
934*9880d681SAndroid Build Coastguard Worker dbgs() << " adding targets:";
935*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = BTs.size(); i < n; ++i)
936*9880d681SAndroid Build Coastguard Worker dbgs() << " BB#" << BTs[i]->getNumber();
937*9880d681SAndroid Build Coastguard Worker if (FallsThrough)
938*9880d681SAndroid Build Coastguard Worker dbgs() << "\n falls through\n";
939*9880d681SAndroid Build Coastguard Worker else
940*9880d681SAndroid Build Coastguard Worker dbgs() << "\n does not fall through\n";
941*9880d681SAndroid Build Coastguard Worker }
942*9880d681SAndroid Build Coastguard Worker Targets.insert(BTs.begin(), BTs.end());
943*9880d681SAndroid Build Coastguard Worker }
944*9880d681SAndroid Build Coastguard Worker ++It;
945*9880d681SAndroid Build Coastguard Worker } while (FallsThrough && It != End);
946*9880d681SAndroid Build Coastguard Worker
947*9880d681SAndroid Build Coastguard Worker typedef MachineBasicBlock::const_succ_iterator succ_iterator;
948*9880d681SAndroid Build Coastguard Worker if (!DefaultToAll) {
949*9880d681SAndroid Build Coastguard Worker // Need to add all CFG successors that lead to EH landing pads.
950*9880d681SAndroid Build Coastguard Worker // There won't be explicit branches to these blocks, but they must
951*9880d681SAndroid Build Coastguard Worker // be processed.
952*9880d681SAndroid Build Coastguard Worker for (succ_iterator I = B.succ_begin(), E = B.succ_end(); I != E; ++I) {
953*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *SB = *I;
954*9880d681SAndroid Build Coastguard Worker if (SB->isEHPad())
955*9880d681SAndroid Build Coastguard Worker Targets.insert(SB);
956*9880d681SAndroid Build Coastguard Worker }
957*9880d681SAndroid Build Coastguard Worker if (FallsThrough) {
958*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator BIt = B.getIterator();
959*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator Next = std::next(BIt);
960*9880d681SAndroid Build Coastguard Worker if (Next != MF.end())
961*9880d681SAndroid Build Coastguard Worker Targets.insert(&*Next);
962*9880d681SAndroid Build Coastguard Worker }
963*9880d681SAndroid Build Coastguard Worker } else {
964*9880d681SAndroid Build Coastguard Worker for (succ_iterator I = B.succ_begin(), E = B.succ_end(); I != E; ++I)
965*9880d681SAndroid Build Coastguard Worker Targets.insert(*I);
966*9880d681SAndroid Build Coastguard Worker }
967*9880d681SAndroid Build Coastguard Worker
968*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = Targets.size(); i < n; ++i) {
969*9880d681SAndroid Build Coastguard Worker int TargetN = Targets[i]->getNumber();
970*9880d681SAndroid Build Coastguard Worker FlowQ.push(CFGEdge(ThisN, TargetN));
971*9880d681SAndroid Build Coastguard Worker }
972*9880d681SAndroid Build Coastguard Worker }
973*9880d681SAndroid Build Coastguard Worker
974*9880d681SAndroid Build Coastguard Worker
visitUsesOf(unsigned Reg)975*9880d681SAndroid Build Coastguard Worker void BT::visitUsesOf(unsigned Reg) {
976*9880d681SAndroid Build Coastguard Worker if (Trace)
977*9880d681SAndroid Build Coastguard Worker dbgs() << "visiting uses of " << PrintReg(Reg, &ME.TRI) << "\n";
978*9880d681SAndroid Build Coastguard Worker
979*9880d681SAndroid Build Coastguard Worker typedef MachineRegisterInfo::use_nodbg_iterator use_iterator;
980*9880d681SAndroid Build Coastguard Worker use_iterator End = MRI.use_nodbg_end();
981*9880d681SAndroid Build Coastguard Worker for (use_iterator I = MRI.use_nodbg_begin(Reg); I != End; ++I) {
982*9880d681SAndroid Build Coastguard Worker MachineInstr *UseI = I->getParent();
983*9880d681SAndroid Build Coastguard Worker if (!InstrExec.count(UseI))
984*9880d681SAndroid Build Coastguard Worker continue;
985*9880d681SAndroid Build Coastguard Worker if (UseI->isPHI())
986*9880d681SAndroid Build Coastguard Worker visitPHI(*UseI);
987*9880d681SAndroid Build Coastguard Worker else if (!UseI->isBranch())
988*9880d681SAndroid Build Coastguard Worker visitNonBranch(*UseI);
989*9880d681SAndroid Build Coastguard Worker else
990*9880d681SAndroid Build Coastguard Worker visitBranchesFrom(*UseI);
991*9880d681SAndroid Build Coastguard Worker }
992*9880d681SAndroid Build Coastguard Worker }
993*9880d681SAndroid Build Coastguard Worker
994*9880d681SAndroid Build Coastguard Worker
get(RegisterRef RR) const995*9880d681SAndroid Build Coastguard Worker BT::RegisterCell BT::get(RegisterRef RR) const {
996*9880d681SAndroid Build Coastguard Worker return ME.getCell(RR, Map);
997*9880d681SAndroid Build Coastguard Worker }
998*9880d681SAndroid Build Coastguard Worker
999*9880d681SAndroid Build Coastguard Worker
put(RegisterRef RR,const RegisterCell & RC)1000*9880d681SAndroid Build Coastguard Worker void BT::put(RegisterRef RR, const RegisterCell &RC) {
1001*9880d681SAndroid Build Coastguard Worker ME.putCell(RR, RC, Map);
1002*9880d681SAndroid Build Coastguard Worker }
1003*9880d681SAndroid Build Coastguard Worker
1004*9880d681SAndroid Build Coastguard Worker
1005*9880d681SAndroid Build Coastguard Worker // Replace all references to bits from OldRR with the corresponding bits
1006*9880d681SAndroid Build Coastguard Worker // in NewRR.
subst(RegisterRef OldRR,RegisterRef NewRR)1007*9880d681SAndroid Build Coastguard Worker void BT::subst(RegisterRef OldRR, RegisterRef NewRR) {
1008*9880d681SAndroid Build Coastguard Worker assert(Map.count(OldRR.Reg) > 0 && "OldRR not present in map");
1009*9880d681SAndroid Build Coastguard Worker BitMask OM = ME.mask(OldRR.Reg, OldRR.Sub);
1010*9880d681SAndroid Build Coastguard Worker BitMask NM = ME.mask(NewRR.Reg, NewRR.Sub);
1011*9880d681SAndroid Build Coastguard Worker uint16_t OMB = OM.first(), OME = OM.last();
1012*9880d681SAndroid Build Coastguard Worker uint16_t NMB = NM.first(), NME = NM.last();
1013*9880d681SAndroid Build Coastguard Worker (void)NME;
1014*9880d681SAndroid Build Coastguard Worker assert((OME-OMB == NME-NMB) &&
1015*9880d681SAndroid Build Coastguard Worker "Substituting registers of different lengths");
1016*9880d681SAndroid Build Coastguard Worker for (CellMapType::iterator I = Map.begin(), E = Map.end(); I != E; ++I) {
1017*9880d681SAndroid Build Coastguard Worker RegisterCell &RC = I->second;
1018*9880d681SAndroid Build Coastguard Worker for (uint16_t i = 0, w = RC.width(); i < w; ++i) {
1019*9880d681SAndroid Build Coastguard Worker BitValue &V = RC[i];
1020*9880d681SAndroid Build Coastguard Worker if (V.Type != BitValue::Ref || V.RefI.Reg != OldRR.Reg)
1021*9880d681SAndroid Build Coastguard Worker continue;
1022*9880d681SAndroid Build Coastguard Worker if (V.RefI.Pos < OMB || V.RefI.Pos > OME)
1023*9880d681SAndroid Build Coastguard Worker continue;
1024*9880d681SAndroid Build Coastguard Worker V.RefI.Reg = NewRR.Reg;
1025*9880d681SAndroid Build Coastguard Worker V.RefI.Pos += NMB-OMB;
1026*9880d681SAndroid Build Coastguard Worker }
1027*9880d681SAndroid Build Coastguard Worker }
1028*9880d681SAndroid Build Coastguard Worker }
1029*9880d681SAndroid Build Coastguard Worker
1030*9880d681SAndroid Build Coastguard Worker
1031*9880d681SAndroid Build Coastguard Worker // Check if the block has been "executed" during propagation. (If not, the
1032*9880d681SAndroid Build Coastguard Worker // block is dead, but it may still appear to be reachable.)
reached(const MachineBasicBlock * B) const1033*9880d681SAndroid Build Coastguard Worker bool BT::reached(const MachineBasicBlock *B) const {
1034*9880d681SAndroid Build Coastguard Worker int BN = B->getNumber();
1035*9880d681SAndroid Build Coastguard Worker assert(BN >= 0);
1036*9880d681SAndroid Build Coastguard Worker for (EdgeSetType::iterator I = EdgeExec.begin(), E = EdgeExec.end();
1037*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
1038*9880d681SAndroid Build Coastguard Worker if (I->second == BN)
1039*9880d681SAndroid Build Coastguard Worker return true;
1040*9880d681SAndroid Build Coastguard Worker }
1041*9880d681SAndroid Build Coastguard Worker return false;
1042*9880d681SAndroid Build Coastguard Worker }
1043*9880d681SAndroid Build Coastguard Worker
1044*9880d681SAndroid Build Coastguard Worker
reset()1045*9880d681SAndroid Build Coastguard Worker void BT::reset() {
1046*9880d681SAndroid Build Coastguard Worker EdgeExec.clear();
1047*9880d681SAndroid Build Coastguard Worker InstrExec.clear();
1048*9880d681SAndroid Build Coastguard Worker Map.clear();
1049*9880d681SAndroid Build Coastguard Worker }
1050*9880d681SAndroid Build Coastguard Worker
1051*9880d681SAndroid Build Coastguard Worker
run()1052*9880d681SAndroid Build Coastguard Worker void BT::run() {
1053*9880d681SAndroid Build Coastguard Worker reset();
1054*9880d681SAndroid Build Coastguard Worker assert(FlowQ.empty());
1055*9880d681SAndroid Build Coastguard Worker
1056*9880d681SAndroid Build Coastguard Worker typedef GraphTraits<const MachineFunction*> MachineFlowGraphTraits;
1057*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *Entry = MachineFlowGraphTraits::getEntryNode(&MF);
1058*9880d681SAndroid Build Coastguard Worker
1059*9880d681SAndroid Build Coastguard Worker unsigned MaxBN = 0;
1060*9880d681SAndroid Build Coastguard Worker for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
1061*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
1062*9880d681SAndroid Build Coastguard Worker assert(I->getNumber() >= 0 && "Disconnected block");
1063*9880d681SAndroid Build Coastguard Worker unsigned BN = I->getNumber();
1064*9880d681SAndroid Build Coastguard Worker if (BN > MaxBN)
1065*9880d681SAndroid Build Coastguard Worker MaxBN = BN;
1066*9880d681SAndroid Build Coastguard Worker }
1067*9880d681SAndroid Build Coastguard Worker
1068*9880d681SAndroid Build Coastguard Worker // Keep track of visited blocks.
1069*9880d681SAndroid Build Coastguard Worker BitVector BlockScanned(MaxBN+1);
1070*9880d681SAndroid Build Coastguard Worker
1071*9880d681SAndroid Build Coastguard Worker int EntryN = Entry->getNumber();
1072*9880d681SAndroid Build Coastguard Worker // Generate a fake edge to get something to start with.
1073*9880d681SAndroid Build Coastguard Worker FlowQ.push(CFGEdge(-1, EntryN));
1074*9880d681SAndroid Build Coastguard Worker
1075*9880d681SAndroid Build Coastguard Worker while (!FlowQ.empty()) {
1076*9880d681SAndroid Build Coastguard Worker CFGEdge Edge = FlowQ.front();
1077*9880d681SAndroid Build Coastguard Worker FlowQ.pop();
1078*9880d681SAndroid Build Coastguard Worker
1079*9880d681SAndroid Build Coastguard Worker if (EdgeExec.count(Edge))
1080*9880d681SAndroid Build Coastguard Worker continue;
1081*9880d681SAndroid Build Coastguard Worker EdgeExec.insert(Edge);
1082*9880d681SAndroid Build Coastguard Worker
1083*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock &B = *MF.getBlockNumbered(Edge.second);
1084*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator It = B.begin(), End = B.end();
1085*9880d681SAndroid Build Coastguard Worker // Visit PHI nodes first.
1086*9880d681SAndroid Build Coastguard Worker while (It != End && It->isPHI()) {
1087*9880d681SAndroid Build Coastguard Worker const MachineInstr &PI = *It++;
1088*9880d681SAndroid Build Coastguard Worker InstrExec.insert(&PI);
1089*9880d681SAndroid Build Coastguard Worker visitPHI(PI);
1090*9880d681SAndroid Build Coastguard Worker }
1091*9880d681SAndroid Build Coastguard Worker
1092*9880d681SAndroid Build Coastguard Worker // If this block has already been visited through a flow graph edge,
1093*9880d681SAndroid Build Coastguard Worker // then the instructions have already been processed. Any updates to
1094*9880d681SAndroid Build Coastguard Worker // the cells would now only happen through visitUsesOf...
1095*9880d681SAndroid Build Coastguard Worker if (BlockScanned[Edge.second])
1096*9880d681SAndroid Build Coastguard Worker continue;
1097*9880d681SAndroid Build Coastguard Worker BlockScanned[Edge.second] = true;
1098*9880d681SAndroid Build Coastguard Worker
1099*9880d681SAndroid Build Coastguard Worker // Visit non-branch instructions.
1100*9880d681SAndroid Build Coastguard Worker while (It != End && !It->isBranch()) {
1101*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI = *It++;
1102*9880d681SAndroid Build Coastguard Worker InstrExec.insert(&MI);
1103*9880d681SAndroid Build Coastguard Worker visitNonBranch(MI);
1104*9880d681SAndroid Build Coastguard Worker }
1105*9880d681SAndroid Build Coastguard Worker // If block end has been reached, add the fall-through edge to the queue.
1106*9880d681SAndroid Build Coastguard Worker if (It == End) {
1107*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator BIt = B.getIterator();
1108*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator Next = std::next(BIt);
1109*9880d681SAndroid Build Coastguard Worker if (Next != MF.end() && B.isSuccessor(&*Next)) {
1110*9880d681SAndroid Build Coastguard Worker int ThisN = B.getNumber();
1111*9880d681SAndroid Build Coastguard Worker int NextN = Next->getNumber();
1112*9880d681SAndroid Build Coastguard Worker FlowQ.push(CFGEdge(ThisN, NextN));
1113*9880d681SAndroid Build Coastguard Worker }
1114*9880d681SAndroid Build Coastguard Worker } else {
1115*9880d681SAndroid Build Coastguard Worker // Handle the remaining sequence of branches. This function will update
1116*9880d681SAndroid Build Coastguard Worker // the work queue.
1117*9880d681SAndroid Build Coastguard Worker visitBranchesFrom(*It);
1118*9880d681SAndroid Build Coastguard Worker }
1119*9880d681SAndroid Build Coastguard Worker } // while (!FlowQ->empty())
1120*9880d681SAndroid Build Coastguard Worker
1121*9880d681SAndroid Build Coastguard Worker if (Trace) {
1122*9880d681SAndroid Build Coastguard Worker dbgs() << "Cells after propagation:\n";
1123*9880d681SAndroid Build Coastguard Worker for (CellMapType::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
1124*9880d681SAndroid Build Coastguard Worker dbgs() << PrintReg(I->first, &ME.TRI) << " -> " << I->second << "\n";
1125*9880d681SAndroid Build Coastguard Worker }
1126*9880d681SAndroid Build Coastguard Worker }
1127*9880d681SAndroid Build Coastguard Worker
1128