1875ed548SDimitry Andric //===--- BitTracker.cpp ---------------------------------------------------===//
2875ed548SDimitry Andric //
3875ed548SDimitry Andric //                     The LLVM Compiler Infrastructure
4875ed548SDimitry Andric //
5875ed548SDimitry Andric // This file is distributed under the University of Illinois Open Source
6875ed548SDimitry Andric // License. See LICENSE.TXT for details.
7875ed548SDimitry Andric //
8875ed548SDimitry Andric //===----------------------------------------------------------------------===//
9875ed548SDimitry Andric 
10875ed548SDimitry Andric // SSA-based bit propagation.
11875ed548SDimitry Andric //
12875ed548SDimitry Andric // The purpose of this code is, for a given virtual register, to provide
13875ed548SDimitry Andric // information about the value of each bit in the register. The values
14875ed548SDimitry Andric // of bits are represented by the class BitValue, and take one of four
15875ed548SDimitry Andric // cases: 0, 1, "ref" and "bottom". The 0 and 1 are rather clear, the
16875ed548SDimitry Andric // "ref" value means that the bit is a copy of another bit (which itself
17875ed548SDimitry Andric // cannot be a copy of yet another bit---such chains are not allowed).
18875ed548SDimitry Andric // A "ref" value is associated with a BitRef structure, which indicates
19875ed548SDimitry Andric // which virtual register, and which bit in that register is the origin
20875ed548SDimitry Andric // of the value. For example, given an instruction
21875ed548SDimitry Andric //   vreg2 = ASL vreg1, 1
22875ed548SDimitry Andric // assuming that nothing is known about bits of vreg1, bit 1 of vreg2
23875ed548SDimitry Andric // will be a "ref" to (vreg1, 0). If there is a subsequent instruction
24875ed548SDimitry Andric //   vreg3 = ASL vreg2, 2
25875ed548SDimitry Andric // then bit 3 of vreg3 will be a "ref" to (vreg1, 0) as well.
26875ed548SDimitry Andric // The "bottom" case means that the bit's value cannot be determined,
27875ed548SDimitry Andric // and that this virtual register actually defines it. The "bottom" case
28875ed548SDimitry Andric // is discussed in detail in BitTracker.h. In fact, "bottom" is a "ref
29875ed548SDimitry Andric // to self", so for the vreg1 above, the bit 0 of it will be a "ref" to
30875ed548SDimitry Andric // (vreg1, 0), bit 1 will be a "ref" to (vreg1, 1), etc.
31875ed548SDimitry Andric //
32875ed548SDimitry Andric // The tracker implements the Wegman-Zadeck algorithm, originally developed
33875ed548SDimitry Andric // for SSA-based constant propagation. Each register is represented as
34875ed548SDimitry Andric // a sequence of bits, with the convention that bit 0 is the least signi-
35875ed548SDimitry Andric // ficant bit. Each bit is propagated individually. The class RegisterCell
36875ed548SDimitry Andric // implements the register's representation, and is also the subject of
37875ed548SDimitry Andric // the lattice operations in the tracker.
38875ed548SDimitry Andric //
39875ed548SDimitry Andric // The intended usage of the bit tracker is to create a target-specific
40875ed548SDimitry Andric // machine instruction evaluator, pass the evaluator to the BitTracker
41875ed548SDimitry Andric // object, and run the tracker. The tracker will then collect the bit
42875ed548SDimitry Andric // value information for a given machine function. After that, it can be
43875ed548SDimitry Andric // queried for the cells for each virtual register.
44875ed548SDimitry Andric // Sample code:
45875ed548SDimitry Andric //   const TargetSpecificEvaluator TSE(TRI, MRI);
46875ed548SDimitry Andric //   BitTracker BT(TSE, MF);
47875ed548SDimitry Andric //   BT.run();
48875ed548SDimitry Andric //   ...
49875ed548SDimitry Andric //   unsigned Reg = interestingRegister();
50875ed548SDimitry Andric //   RegisterCell RC = BT.get(Reg);
51875ed548SDimitry Andric //   if (RC[3].is(1))
52875ed548SDimitry Andric //      Reg0bit3 = 1;
53875ed548SDimitry Andric //
54875ed548SDimitry Andric // The code below is intended to be fully target-independent.
55875ed548SDimitry Andric 
56875ed548SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
57875ed548SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
58875ed548SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
59875ed548SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
60875ed548SDimitry Andric #include "llvm/IR/Constants.h"
61875ed548SDimitry Andric #include "llvm/Support/Debug.h"
62875ed548SDimitry Andric #include "llvm/Support/raw_ostream.h"
63875ed548SDimitry Andric #include "llvm/Target/TargetRegisterInfo.h"
64875ed548SDimitry Andric 
65875ed548SDimitry Andric #include "BitTracker.h"
66875ed548SDimitry Andric 
67875ed548SDimitry Andric using namespace llvm;
68875ed548SDimitry Andric 
69875ed548SDimitry Andric typedef BitTracker BT;
70875ed548SDimitry Andric 
71875ed548SDimitry Andric namespace {
72875ed548SDimitry Andric   // Local trickery to pretty print a register (without the whole "%vreg"
73875ed548SDimitry Andric   // business).
74875ed548SDimitry Andric   struct printv {
75875ed548SDimitry Andric     printv(unsigned r) : R(r) {}
76875ed548SDimitry Andric     unsigned R;
77875ed548SDimitry Andric   };
78875ed548SDimitry Andric   raw_ostream &operator<< (raw_ostream &OS, const printv &PV) {
79875ed548SDimitry Andric     if (PV.R)
80875ed548SDimitry Andric       OS << 'v' << TargetRegisterInfo::virtReg2Index(PV.R);
81875ed548SDimitry Andric     else
82875ed548SDimitry Andric       OS << 's';
83875ed548SDimitry Andric     return OS;
84875ed548SDimitry Andric   }
85875ed548SDimitry Andric }
86875ed548SDimitry Andric 
873ca95b02SDimitry Andric namespace llvm {
883ca95b02SDimitry Andric   raw_ostream &operator<<(raw_ostream &OS, const BT::BitValue &BV) {
89875ed548SDimitry Andric     switch (BV.Type) {
90875ed548SDimitry Andric       case BT::BitValue::Top:
91875ed548SDimitry Andric         OS << 'T';
92875ed548SDimitry Andric         break;
93875ed548SDimitry Andric       case BT::BitValue::Zero:
94875ed548SDimitry Andric         OS << '0';
95875ed548SDimitry Andric         break;
96875ed548SDimitry Andric       case BT::BitValue::One:
97875ed548SDimitry Andric         OS << '1';
98875ed548SDimitry Andric         break;
99875ed548SDimitry Andric       case BT::BitValue::Ref:
100875ed548SDimitry Andric         OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']';
101875ed548SDimitry Andric         break;
102875ed548SDimitry Andric     }
103875ed548SDimitry Andric     return OS;
104875ed548SDimitry Andric   }
105875ed548SDimitry Andric 
1063ca95b02SDimitry Andric   raw_ostream &operator<<(raw_ostream &OS, const BT::RegisterCell &RC) {
107875ed548SDimitry Andric     unsigned n = RC.Bits.size();
108875ed548SDimitry Andric     OS << "{ w:" << n;
109875ed548SDimitry Andric     // Instead of printing each bit value individually, try to group them
110875ed548SDimitry Andric     // into logical segments, such as sequences of 0 or 1 bits or references
111875ed548SDimitry Andric     // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz").
112875ed548SDimitry Andric     // "Start" will be the index of the beginning of the most recent segment.
113875ed548SDimitry Andric     unsigned Start = 0;
114875ed548SDimitry Andric     bool SeqRef = false;    // A sequence of refs to consecutive bits.
115875ed548SDimitry Andric     bool ConstRef = false;  // A sequence of refs to the same bit.
116875ed548SDimitry Andric 
117875ed548SDimitry Andric     for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) {
118875ed548SDimitry Andric       const BT::BitValue &V = RC[i];
119875ed548SDimitry Andric       const BT::BitValue &SV = RC[Start];
120875ed548SDimitry Andric       bool IsRef = (V.Type == BT::BitValue::Ref);
121875ed548SDimitry Andric       // If the current value is the same as Start, skip to the next one.
122875ed548SDimitry Andric       if (!IsRef && V == SV)
123875ed548SDimitry Andric         continue;
124875ed548SDimitry Andric       if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) {
125875ed548SDimitry Andric         if (Start+1 == i) {
126875ed548SDimitry Andric           SeqRef = (V.RefI.Pos == SV.RefI.Pos+1);
127875ed548SDimitry Andric           ConstRef = (V.RefI.Pos == SV.RefI.Pos);
128875ed548SDimitry Andric         }
129875ed548SDimitry Andric         if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start))
130875ed548SDimitry Andric           continue;
131875ed548SDimitry Andric         if (ConstRef && V.RefI.Pos == SV.RefI.Pos)
132875ed548SDimitry Andric           continue;
133875ed548SDimitry Andric       }
134875ed548SDimitry Andric 
135875ed548SDimitry Andric       // The current value is different. Print the previous one and reset
136875ed548SDimitry Andric       // the Start.
137875ed548SDimitry Andric       OS << " [" << Start;
138875ed548SDimitry Andric       unsigned Count = i - Start;
139875ed548SDimitry Andric       if (Count == 1) {
140875ed548SDimitry Andric         OS << "]:" << SV;
141875ed548SDimitry Andric       } else {
142875ed548SDimitry Andric         OS << '-' << i-1 << "]:";
143875ed548SDimitry Andric         if (SV.Type == BT::BitValue::Ref && SeqRef)
144875ed548SDimitry Andric           OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
145875ed548SDimitry Andric              << SV.RefI.Pos+(Count-1) << ']';
146875ed548SDimitry Andric         else
147875ed548SDimitry Andric           OS << SV;
148875ed548SDimitry Andric       }
149875ed548SDimitry Andric       Start = i;
150875ed548SDimitry Andric       SeqRef = ConstRef = false;
151875ed548SDimitry Andric     }
152875ed548SDimitry Andric 
153875ed548SDimitry Andric     OS << " [" << Start;
154875ed548SDimitry Andric     unsigned Count = n - Start;
155875ed548SDimitry Andric     if (n-Start == 1) {
156875ed548SDimitry Andric       OS << "]:" << RC[Start];
157875ed548SDimitry Andric     } else {
158875ed548SDimitry Andric       OS << '-' << n-1 << "]:";
159875ed548SDimitry Andric       const BT::BitValue &SV = RC[Start];
160875ed548SDimitry Andric       if (SV.Type == BT::BitValue::Ref && SeqRef)
161875ed548SDimitry Andric         OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
162875ed548SDimitry Andric            << SV.RefI.Pos+(Count-1) << ']';
163875ed548SDimitry Andric       else
164875ed548SDimitry Andric         OS << SV;
165875ed548SDimitry Andric     }
166875ed548SDimitry Andric     OS << " }";
167875ed548SDimitry Andric 
168875ed548SDimitry Andric     return OS;
169875ed548SDimitry Andric   }
1703ca95b02SDimitry Andric }
171875ed548SDimitry Andric 
172875ed548SDimitry Andric BitTracker::BitTracker(const MachineEvaluator &E, MachineFunction &F)
173875ed548SDimitry Andric     : Trace(false), ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType) {}
174875ed548SDimitry Andric 
175875ed548SDimitry Andric BitTracker::~BitTracker() {
176875ed548SDimitry Andric   delete &Map;
177875ed548SDimitry Andric }
178875ed548SDimitry Andric 
179875ed548SDimitry Andric 
180875ed548SDimitry Andric // If we were allowed to update a cell for a part of a register, the meet
181875ed548SDimitry Andric // operation would need to be parametrized by the register number and the
182875ed548SDimitry Andric // exact part of the register, so that the computer BitRefs correspond to
183875ed548SDimitry Andric // the actual bits of the "self" register.
184875ed548SDimitry Andric // While this cannot happen in the current implementation, I'm not sure
185875ed548SDimitry Andric // if this should be ruled out in the future.
186875ed548SDimitry Andric bool BT::RegisterCell::meet(const RegisterCell &RC, unsigned SelfR) {
187875ed548SDimitry Andric   // An example when "meet" can be invoked with SelfR == 0 is a phi node
188875ed548SDimitry Andric   // with a physical register as an operand.
189875ed548SDimitry Andric   assert(SelfR == 0 || TargetRegisterInfo::isVirtualRegister(SelfR));
190875ed548SDimitry Andric   bool Changed = false;
191875ed548SDimitry Andric   for (uint16_t i = 0, n = Bits.size(); i < n; ++i) {
192875ed548SDimitry Andric     const BitValue &RCV = RC[i];
193875ed548SDimitry Andric     Changed |= Bits[i].meet(RCV, BitRef(SelfR, i));
194875ed548SDimitry Andric   }
195875ed548SDimitry Andric   return Changed;
196875ed548SDimitry Andric }
197875ed548SDimitry Andric 
198875ed548SDimitry Andric 
199875ed548SDimitry Andric // Insert the entire cell RC into the current cell at position given by M.
200875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC,
201875ed548SDimitry Andric       const BitMask &M) {
202875ed548SDimitry Andric   uint16_t B = M.first(), E = M.last(), W = width();
203875ed548SDimitry Andric   // Sanity: M must be a valid mask for *this.
204875ed548SDimitry Andric   assert(B < W && E < W);
205875ed548SDimitry Andric   // Sanity: the masked part of *this must have the same number of bits
206875ed548SDimitry Andric   // as the source.
207875ed548SDimitry Andric   assert(B > E || E-B+1 == RC.width());      // B <= E  =>  E-B+1 = |RC|.
208875ed548SDimitry Andric   assert(B <= E || E+(W-B)+1 == RC.width()); // E < B   =>  E+(W-B)+1 = |RC|.
209875ed548SDimitry Andric   if (B <= E) {
210875ed548SDimitry Andric     for (uint16_t i = 0; i <= E-B; ++i)
211875ed548SDimitry Andric       Bits[i+B] = RC[i];
212875ed548SDimitry Andric   } else {
213875ed548SDimitry Andric     for (uint16_t i = 0; i < W-B; ++i)
214875ed548SDimitry Andric       Bits[i+B] = RC[i];
215875ed548SDimitry Andric     for (uint16_t i = 0; i <= E; ++i)
216875ed548SDimitry Andric       Bits[i] = RC[i+(W-B)];
217875ed548SDimitry Andric   }
218875ed548SDimitry Andric   return *this;
219875ed548SDimitry Andric }
220875ed548SDimitry Andric 
221875ed548SDimitry Andric 
222875ed548SDimitry Andric BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const {
223875ed548SDimitry Andric   uint16_t B = M.first(), E = M.last(), W = width();
224875ed548SDimitry Andric   assert(B < W && E < W);
225875ed548SDimitry Andric   if (B <= E) {
226875ed548SDimitry Andric     RegisterCell RC(E-B+1);
227875ed548SDimitry Andric     for (uint16_t i = B; i <= E; ++i)
228875ed548SDimitry Andric       RC.Bits[i-B] = Bits[i];
229875ed548SDimitry Andric     return RC;
230875ed548SDimitry Andric   }
231875ed548SDimitry Andric 
232875ed548SDimitry Andric   RegisterCell RC(E+(W-B)+1);
233875ed548SDimitry Andric   for (uint16_t i = 0; i < W-B; ++i)
234875ed548SDimitry Andric     RC.Bits[i] = Bits[i+B];
235875ed548SDimitry Andric   for (uint16_t i = 0; i <= E; ++i)
236875ed548SDimitry Andric     RC.Bits[i+(W-B)] = Bits[i];
237875ed548SDimitry Andric   return RC;
238875ed548SDimitry Andric }
239875ed548SDimitry Andric 
240875ed548SDimitry Andric 
241875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) {
242875ed548SDimitry Andric   // Rotate left (i.e. towards increasing bit indices).
243875ed548SDimitry Andric   // Swap the two parts:  [0..W-Sh-1] [W-Sh..W-1]
244875ed548SDimitry Andric   uint16_t W = width();
245875ed548SDimitry Andric   Sh = Sh % W;
246875ed548SDimitry Andric   if (Sh == 0)
247875ed548SDimitry Andric     return *this;
248875ed548SDimitry Andric 
249875ed548SDimitry Andric   RegisterCell Tmp(W-Sh);
250875ed548SDimitry Andric   // Tmp = [0..W-Sh-1].
251875ed548SDimitry Andric   for (uint16_t i = 0; i < W-Sh; ++i)
252875ed548SDimitry Andric     Tmp[i] = Bits[i];
253875ed548SDimitry Andric   // Shift [W-Sh..W-1] to [0..Sh-1].
254875ed548SDimitry Andric   for (uint16_t i = 0; i < Sh; ++i)
255875ed548SDimitry Andric     Bits[i] = Bits[W-Sh+i];
256875ed548SDimitry Andric   // Copy Tmp to [Sh..W-1].
257875ed548SDimitry Andric   for (uint16_t i = 0; i < W-Sh; ++i)
258875ed548SDimitry Andric     Bits[i+Sh] = Tmp.Bits[i];
259875ed548SDimitry Andric   return *this;
260875ed548SDimitry Andric }
261875ed548SDimitry Andric 
262875ed548SDimitry Andric 
263875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E,
264875ed548SDimitry Andric       const BitValue &V) {
265875ed548SDimitry Andric   assert(B <= E);
266875ed548SDimitry Andric   while (B < E)
267875ed548SDimitry Andric     Bits[B++] = V;
268875ed548SDimitry Andric   return *this;
269875ed548SDimitry Andric }
270875ed548SDimitry Andric 
271875ed548SDimitry Andric 
272875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) {
273875ed548SDimitry Andric   // Append the cell given as the argument to the "this" cell.
274875ed548SDimitry Andric   // Bit 0 of RC becomes bit W of the result, where W is this->width().
275875ed548SDimitry Andric   uint16_t W = width(), WRC = RC.width();
276875ed548SDimitry Andric   Bits.resize(W+WRC);
277875ed548SDimitry Andric   for (uint16_t i = 0; i < WRC; ++i)
278875ed548SDimitry Andric     Bits[i+W] = RC.Bits[i];
279875ed548SDimitry Andric   return *this;
280875ed548SDimitry Andric }
281875ed548SDimitry Andric 
282875ed548SDimitry Andric 
283875ed548SDimitry Andric uint16_t BT::RegisterCell::ct(bool B) const {
284875ed548SDimitry Andric   uint16_t W = width();
285875ed548SDimitry Andric   uint16_t C = 0;
286875ed548SDimitry Andric   BitValue V = B;
287875ed548SDimitry Andric   while (C < W && Bits[C] == V)
288875ed548SDimitry Andric     C++;
289875ed548SDimitry Andric   return C;
290875ed548SDimitry Andric }
291875ed548SDimitry Andric 
292875ed548SDimitry Andric 
293875ed548SDimitry Andric uint16_t BT::RegisterCell::cl(bool B) const {
294875ed548SDimitry Andric   uint16_t W = width();
295875ed548SDimitry Andric   uint16_t C = 0;
296875ed548SDimitry Andric   BitValue V = B;
297875ed548SDimitry Andric   while (C < W && Bits[W-(C+1)] == V)
298875ed548SDimitry Andric     C++;
299875ed548SDimitry Andric   return C;
300875ed548SDimitry Andric }
301875ed548SDimitry Andric 
302875ed548SDimitry Andric 
303875ed548SDimitry Andric bool BT::RegisterCell::operator== (const RegisterCell &RC) const {
304875ed548SDimitry Andric   uint16_t W = Bits.size();
305875ed548SDimitry Andric   if (RC.Bits.size() != W)
306875ed548SDimitry Andric     return false;
307875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i)
308875ed548SDimitry Andric     if (Bits[i] != RC[i])
309875ed548SDimitry Andric       return false;
310875ed548SDimitry Andric   return true;
311875ed548SDimitry Andric }
312875ed548SDimitry Andric 
313875ed548SDimitry Andric 
314875ed548SDimitry Andric uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const {
315875ed548SDimitry Andric   // The general problem is with finding a register class that corresponds
316875ed548SDimitry Andric   // to a given reference reg:sub. There can be several such classes, and
317875ed548SDimitry Andric   // since we only care about the register size, it does not matter which
318875ed548SDimitry Andric   // such class we would find.
319875ed548SDimitry Andric   // The easiest way to accomplish what we want is to
320875ed548SDimitry Andric   // 1. find a physical register PhysR from the same class as RR.Reg,
321875ed548SDimitry Andric   // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub,
322875ed548SDimitry Andric   // 3. find a register class that contains PhysS.
323875ed548SDimitry Andric   unsigned PhysR;
324875ed548SDimitry Andric   if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
325875ed548SDimitry Andric     const TargetRegisterClass *VC = MRI.getRegClass(RR.Reg);
326875ed548SDimitry Andric     assert(VC->begin() != VC->end() && "Empty register class");
327875ed548SDimitry Andric     PhysR = *VC->begin();
328875ed548SDimitry Andric   } else {
329875ed548SDimitry Andric     assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
330875ed548SDimitry Andric     PhysR = RR.Reg;
331875ed548SDimitry Andric   }
332875ed548SDimitry Andric 
333875ed548SDimitry Andric   unsigned PhysS = (RR.Sub == 0) ? PhysR : TRI.getSubReg(PhysR, RR.Sub);
334875ed548SDimitry Andric   const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PhysS);
335875ed548SDimitry Andric   uint16_t BW = RC->getSize()*8;
336875ed548SDimitry Andric   return BW;
337875ed548SDimitry Andric }
338875ed548SDimitry Andric 
339875ed548SDimitry Andric 
340875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR,
341875ed548SDimitry Andric       const CellMapType &M) const {
342875ed548SDimitry Andric   uint16_t BW = getRegBitWidth(RR);
343875ed548SDimitry Andric 
344875ed548SDimitry Andric   // Physical registers are assumed to be present in the map with an unknown
345875ed548SDimitry Andric   // value. Don't actually insert anything in the map, just return the cell.
346875ed548SDimitry Andric   if (TargetRegisterInfo::isPhysicalRegister(RR.Reg))
347875ed548SDimitry Andric     return RegisterCell::self(0, BW);
348875ed548SDimitry Andric 
349875ed548SDimitry Andric   assert(TargetRegisterInfo::isVirtualRegister(RR.Reg));
350875ed548SDimitry Andric   // For virtual registers that belong to a class that is not tracked,
351875ed548SDimitry Andric   // generate an "unknown" value as well.
352875ed548SDimitry Andric   const TargetRegisterClass *C = MRI.getRegClass(RR.Reg);
353875ed548SDimitry Andric   if (!track(C))
354875ed548SDimitry Andric     return RegisterCell::self(0, BW);
355875ed548SDimitry Andric 
356875ed548SDimitry Andric   CellMapType::const_iterator F = M.find(RR.Reg);
357875ed548SDimitry Andric   if (F != M.end()) {
358875ed548SDimitry Andric     if (!RR.Sub)
359875ed548SDimitry Andric       return F->second;
360875ed548SDimitry Andric     BitMask M = mask(RR.Reg, RR.Sub);
361875ed548SDimitry Andric     return F->second.extract(M);
362875ed548SDimitry Andric   }
363875ed548SDimitry Andric   // If not found, create a "top" entry, but do not insert it in the map.
364875ed548SDimitry Andric   return RegisterCell::top(BW);
365875ed548SDimitry Andric }
366875ed548SDimitry Andric 
367875ed548SDimitry Andric 
368875ed548SDimitry Andric void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC,
369875ed548SDimitry Andric       CellMapType &M) const {
370875ed548SDimitry Andric   // While updating the cell map can be done in a meaningful way for
371875ed548SDimitry Andric   // a part of a register, it makes little sense to implement it as the
372875ed548SDimitry Andric   // SSA representation would never contain such "partial definitions".
373875ed548SDimitry Andric   if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
374875ed548SDimitry Andric     return;
375875ed548SDimitry Andric   assert(RR.Sub == 0 && "Unexpected sub-register in definition");
376875ed548SDimitry Andric   // Eliminate all ref-to-reg-0 bit values: replace them with "self".
377875ed548SDimitry Andric   for (unsigned i = 0, n = RC.width(); i < n; ++i) {
378875ed548SDimitry Andric     const BitValue &V = RC[i];
379875ed548SDimitry Andric     if (V.Type == BitValue::Ref && V.RefI.Reg == 0)
380875ed548SDimitry Andric       RC[i].RefI = BitRef(RR.Reg, i);
381875ed548SDimitry Andric   }
382875ed548SDimitry Andric   M[RR.Reg] = RC;
383875ed548SDimitry Andric }
384875ed548SDimitry Andric 
385875ed548SDimitry Andric 
386875ed548SDimitry Andric // Check if the cell represents a compile-time integer value.
387875ed548SDimitry Andric bool BT::MachineEvaluator::isInt(const RegisterCell &A) const {
388875ed548SDimitry Andric   uint16_t W = A.width();
389875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i)
390875ed548SDimitry Andric     if (!A[i].is(0) && !A[i].is(1))
391875ed548SDimitry Andric       return false;
392875ed548SDimitry Andric   return true;
393875ed548SDimitry Andric }
394875ed548SDimitry Andric 
395875ed548SDimitry Andric 
396875ed548SDimitry Andric // Convert a cell to the integer value. The result must fit in uint64_t.
397875ed548SDimitry Andric uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const {
398875ed548SDimitry Andric   assert(isInt(A));
399875ed548SDimitry Andric   uint64_t Val = 0;
400875ed548SDimitry Andric   uint16_t W = A.width();
401875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
402875ed548SDimitry Andric     Val <<= 1;
403875ed548SDimitry Andric     Val |= A[i].is(1);
404875ed548SDimitry Andric   }
405875ed548SDimitry Andric   return Val;
406875ed548SDimitry Andric }
407875ed548SDimitry Andric 
408875ed548SDimitry Andric 
409875ed548SDimitry Andric // Evaluator helper functions. These implement some common operation on
410875ed548SDimitry Andric // register cells that can be used to implement target-specific instructions
411875ed548SDimitry Andric // in a target-specific evaluator.
412875ed548SDimitry Andric 
413875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const {
414875ed548SDimitry Andric   RegisterCell Res(W);
415875ed548SDimitry Andric   // For bits beyond the 63rd, this will generate the sign bit of V.
416875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
417875ed548SDimitry Andric     Res[i] = BitValue(V & 1);
418875ed548SDimitry Andric     V >>= 1;
419875ed548SDimitry Andric   }
420875ed548SDimitry Andric   return Res;
421875ed548SDimitry Andric }
422875ed548SDimitry Andric 
423875ed548SDimitry Andric 
424875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const {
4253ca95b02SDimitry Andric   const APInt &A = CI->getValue();
426875ed548SDimitry Andric   uint16_t BW = A.getBitWidth();
427875ed548SDimitry Andric   assert((unsigned)BW == A.getBitWidth() && "BitWidth overflow");
428875ed548SDimitry Andric   RegisterCell Res(BW);
429875ed548SDimitry Andric   for (uint16_t i = 0; i < BW; ++i)
430875ed548SDimitry Andric     Res[i] = A[i];
431875ed548SDimitry Andric   return Res;
432875ed548SDimitry Andric }
433875ed548SDimitry Andric 
434875ed548SDimitry Andric 
435875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1,
436875ed548SDimitry Andric       const RegisterCell &A2) const {
437875ed548SDimitry Andric   uint16_t W = A1.width();
438875ed548SDimitry Andric   assert(W == A2.width());
439875ed548SDimitry Andric   RegisterCell Res(W);
440875ed548SDimitry Andric   bool Carry = false;
441875ed548SDimitry Andric   uint16_t I;
442875ed548SDimitry Andric   for (I = 0; I < W; ++I) {
443875ed548SDimitry Andric     const BitValue &V1 = A1[I];
444875ed548SDimitry Andric     const BitValue &V2 = A2[I];
445875ed548SDimitry Andric     if (!V1.num() || !V2.num())
446875ed548SDimitry Andric       break;
447875ed548SDimitry Andric     unsigned S = bool(V1) + bool(V2) + Carry;
448875ed548SDimitry Andric     Res[I] = BitValue(S & 1);
449875ed548SDimitry Andric     Carry = (S > 1);
450875ed548SDimitry Andric   }
451875ed548SDimitry Andric   for (; I < W; ++I) {
452875ed548SDimitry Andric     const BitValue &V1 = A1[I];
453875ed548SDimitry Andric     const BitValue &V2 = A2[I];
454875ed548SDimitry Andric     // If the next bit is same as Carry, the result will be 0 plus the
455875ed548SDimitry Andric     // other bit. The Carry bit will remain unchanged.
456875ed548SDimitry Andric     if (V1.is(Carry))
457875ed548SDimitry Andric       Res[I] = BitValue::ref(V2);
458875ed548SDimitry Andric     else if (V2.is(Carry))
459875ed548SDimitry Andric       Res[I] = BitValue::ref(V1);
460875ed548SDimitry Andric     else
461875ed548SDimitry Andric       break;
462875ed548SDimitry Andric   }
463875ed548SDimitry Andric   for (; I < W; ++I)
464875ed548SDimitry Andric     Res[I] = BitValue::self();
465875ed548SDimitry Andric   return Res;
466875ed548SDimitry Andric }
467875ed548SDimitry Andric 
468875ed548SDimitry Andric 
469875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1,
470875ed548SDimitry Andric       const RegisterCell &A2) const {
471875ed548SDimitry Andric   uint16_t W = A1.width();
472875ed548SDimitry Andric   assert(W == A2.width());
473875ed548SDimitry Andric   RegisterCell Res(W);
474875ed548SDimitry Andric   bool Borrow = false;
475875ed548SDimitry Andric   uint16_t I;
476875ed548SDimitry Andric   for (I = 0; I < W; ++I) {
477875ed548SDimitry Andric     const BitValue &V1 = A1[I];
478875ed548SDimitry Andric     const BitValue &V2 = A2[I];
479875ed548SDimitry Andric     if (!V1.num() || !V2.num())
480875ed548SDimitry Andric       break;
481875ed548SDimitry Andric     unsigned S = bool(V1) - bool(V2) - Borrow;
482875ed548SDimitry Andric     Res[I] = BitValue(S & 1);
483875ed548SDimitry Andric     Borrow = (S > 1);
484875ed548SDimitry Andric   }
485875ed548SDimitry Andric   for (; I < W; ++I) {
486875ed548SDimitry Andric     const BitValue &V1 = A1[I];
487875ed548SDimitry Andric     const BitValue &V2 = A2[I];
488875ed548SDimitry Andric     if (V1.is(Borrow)) {
489875ed548SDimitry Andric       Res[I] = BitValue::ref(V2);
490875ed548SDimitry Andric       break;
491875ed548SDimitry Andric     }
492875ed548SDimitry Andric     if (V2.is(Borrow))
493875ed548SDimitry Andric       Res[I] = BitValue::ref(V1);
494875ed548SDimitry Andric     else
495875ed548SDimitry Andric       break;
496875ed548SDimitry Andric   }
497875ed548SDimitry Andric   for (; I < W; ++I)
498875ed548SDimitry Andric     Res[I] = BitValue::self();
499875ed548SDimitry Andric   return Res;
500875ed548SDimitry Andric }
501875ed548SDimitry Andric 
502875ed548SDimitry Andric 
503875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1,
504875ed548SDimitry Andric       const RegisterCell &A2) const {
505875ed548SDimitry Andric   uint16_t W = A1.width() + A2.width();
506875ed548SDimitry Andric   uint16_t Z = A1.ct(0) + A2.ct(0);
507875ed548SDimitry Andric   RegisterCell Res(W);
508875ed548SDimitry Andric   Res.fill(0, Z, BitValue::Zero);
509875ed548SDimitry Andric   Res.fill(Z, W, BitValue::self());
510875ed548SDimitry Andric   return Res;
511875ed548SDimitry Andric }
512875ed548SDimitry Andric 
513875ed548SDimitry Andric 
514875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1,
515875ed548SDimitry Andric       const RegisterCell &A2) const {
516875ed548SDimitry Andric   uint16_t W = A1.width() + A2.width();
517875ed548SDimitry Andric   uint16_t Z = A1.ct(0) + A2.ct(0);
518875ed548SDimitry Andric   RegisterCell Res(W);
519875ed548SDimitry Andric   Res.fill(0, Z, BitValue::Zero);
520875ed548SDimitry Andric   Res.fill(Z, W, BitValue::self());
521875ed548SDimitry Andric   return Res;
522875ed548SDimitry Andric }
523875ed548SDimitry Andric 
524875ed548SDimitry Andric 
525875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1,
526875ed548SDimitry Andric       uint16_t Sh) const {
527875ed548SDimitry Andric   assert(Sh <= A1.width());
528875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
529875ed548SDimitry Andric   Res.rol(Sh);
530875ed548SDimitry Andric   Res.fill(0, Sh, BitValue::Zero);
531875ed548SDimitry Andric   return Res;
532875ed548SDimitry Andric }
533875ed548SDimitry Andric 
534875ed548SDimitry Andric 
535875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1,
536875ed548SDimitry Andric       uint16_t Sh) const {
537875ed548SDimitry Andric   uint16_t W = A1.width();
538875ed548SDimitry Andric   assert(Sh <= W);
539875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
540875ed548SDimitry Andric   Res.rol(W-Sh);
541875ed548SDimitry Andric   Res.fill(W-Sh, W, BitValue::Zero);
542875ed548SDimitry Andric   return Res;
543875ed548SDimitry Andric }
544875ed548SDimitry Andric 
545875ed548SDimitry Andric 
546875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1,
547875ed548SDimitry Andric       uint16_t Sh) const {
548875ed548SDimitry Andric   uint16_t W = A1.width();
549875ed548SDimitry Andric   assert(Sh <= W);
550875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
551875ed548SDimitry Andric   BitValue Sign = Res[W-1];
552875ed548SDimitry Andric   Res.rol(W-Sh);
553875ed548SDimitry Andric   Res.fill(W-Sh, W, Sign);
554875ed548SDimitry Andric   return Res;
555875ed548SDimitry Andric }
556875ed548SDimitry Andric 
557875ed548SDimitry Andric 
558875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1,
559875ed548SDimitry Andric       const RegisterCell &A2) const {
560875ed548SDimitry Andric   uint16_t W = A1.width();
561875ed548SDimitry Andric   assert(W == A2.width());
562875ed548SDimitry Andric   RegisterCell Res(W);
563875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
564875ed548SDimitry Andric     const BitValue &V1 = A1[i];
565875ed548SDimitry Andric     const BitValue &V2 = A2[i];
566875ed548SDimitry Andric     if (V1.is(1))
567875ed548SDimitry Andric       Res[i] = BitValue::ref(V2);
568875ed548SDimitry Andric     else if (V2.is(1))
569875ed548SDimitry Andric       Res[i] = BitValue::ref(V1);
570875ed548SDimitry Andric     else if (V1.is(0) || V2.is(0))
571875ed548SDimitry Andric       Res[i] = BitValue::Zero;
572875ed548SDimitry Andric     else if (V1 == V2)
573875ed548SDimitry Andric       Res[i] = V1;
574875ed548SDimitry Andric     else
575875ed548SDimitry Andric       Res[i] = BitValue::self();
576875ed548SDimitry Andric   }
577875ed548SDimitry Andric   return Res;
578875ed548SDimitry Andric }
579875ed548SDimitry Andric 
580875ed548SDimitry Andric 
581875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1,
582875ed548SDimitry Andric       const RegisterCell &A2) const {
583875ed548SDimitry Andric   uint16_t W = A1.width();
584875ed548SDimitry Andric   assert(W == A2.width());
585875ed548SDimitry Andric   RegisterCell Res(W);
586875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
587875ed548SDimitry Andric     const BitValue &V1 = A1[i];
588875ed548SDimitry Andric     const BitValue &V2 = A2[i];
589875ed548SDimitry Andric     if (V1.is(1) || V2.is(1))
590875ed548SDimitry Andric       Res[i] = BitValue::One;
591875ed548SDimitry Andric     else if (V1.is(0))
592875ed548SDimitry Andric       Res[i] = BitValue::ref(V2);
593875ed548SDimitry Andric     else if (V2.is(0))
594875ed548SDimitry Andric       Res[i] = BitValue::ref(V1);
595875ed548SDimitry Andric     else if (V1 == V2)
596875ed548SDimitry Andric       Res[i] = V1;
597875ed548SDimitry Andric     else
598875ed548SDimitry Andric       Res[i] = BitValue::self();
599875ed548SDimitry Andric   }
600875ed548SDimitry Andric   return Res;
601875ed548SDimitry Andric }
602875ed548SDimitry Andric 
603875ed548SDimitry Andric 
604875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1,
605875ed548SDimitry Andric       const RegisterCell &A2) const {
606875ed548SDimitry Andric   uint16_t W = A1.width();
607875ed548SDimitry Andric   assert(W == A2.width());
608875ed548SDimitry Andric   RegisterCell Res(W);
609875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
610875ed548SDimitry Andric     const BitValue &V1 = A1[i];
611875ed548SDimitry Andric     const BitValue &V2 = A2[i];
612875ed548SDimitry Andric     if (V1.is(0))
613875ed548SDimitry Andric       Res[i] = BitValue::ref(V2);
614875ed548SDimitry Andric     else if (V2.is(0))
615875ed548SDimitry Andric       Res[i] = BitValue::ref(V1);
616875ed548SDimitry Andric     else if (V1 == V2)
617875ed548SDimitry Andric       Res[i] = BitValue::Zero;
618875ed548SDimitry Andric     else
619875ed548SDimitry Andric       Res[i] = BitValue::self();
620875ed548SDimitry Andric   }
621875ed548SDimitry Andric   return Res;
622875ed548SDimitry Andric }
623875ed548SDimitry Andric 
624875ed548SDimitry Andric 
625875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const {
626875ed548SDimitry Andric   uint16_t W = A1.width();
627875ed548SDimitry Andric   RegisterCell Res(W);
628875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
629875ed548SDimitry Andric     const BitValue &V = A1[i];
630875ed548SDimitry Andric     if (V.is(0))
631875ed548SDimitry Andric       Res[i] = BitValue::One;
632875ed548SDimitry Andric     else if (V.is(1))
633875ed548SDimitry Andric       Res[i] = BitValue::Zero;
634875ed548SDimitry Andric     else
635875ed548SDimitry Andric       Res[i] = BitValue::self();
636875ed548SDimitry Andric   }
637875ed548SDimitry Andric   return Res;
638875ed548SDimitry Andric }
639875ed548SDimitry Andric 
640875ed548SDimitry Andric 
641875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1,
642875ed548SDimitry Andric       uint16_t BitN) const {
643875ed548SDimitry Andric   assert(BitN < A1.width());
644875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
645875ed548SDimitry Andric   Res[BitN] = BitValue::One;
646875ed548SDimitry Andric   return Res;
647875ed548SDimitry Andric }
648875ed548SDimitry Andric 
649875ed548SDimitry Andric 
650875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1,
651875ed548SDimitry Andric       uint16_t BitN) const {
652875ed548SDimitry Andric   assert(BitN < A1.width());
653875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
654875ed548SDimitry Andric   Res[BitN] = BitValue::Zero;
655875ed548SDimitry Andric   return Res;
656875ed548SDimitry Andric }
657875ed548SDimitry Andric 
658875ed548SDimitry Andric 
659875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B,
660875ed548SDimitry Andric       uint16_t W) const {
661875ed548SDimitry Andric   uint16_t C = A1.cl(B), AW = A1.width();
662875ed548SDimitry Andric   // If the last leading non-B bit is not a constant, then we don't know
663875ed548SDimitry Andric   // the real count.
664875ed548SDimitry Andric   if ((C < AW && A1[AW-1-C].num()) || C == AW)
665875ed548SDimitry Andric     return eIMM(C, W);
666875ed548SDimitry Andric   return RegisterCell::self(0, W);
667875ed548SDimitry Andric }
668875ed548SDimitry Andric 
669875ed548SDimitry Andric 
670875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B,
671875ed548SDimitry Andric       uint16_t W) const {
672875ed548SDimitry Andric   uint16_t C = A1.ct(B), AW = A1.width();
673875ed548SDimitry Andric   // If the last trailing non-B bit is not a constant, then we don't know
674875ed548SDimitry Andric   // the real count.
675875ed548SDimitry Andric   if ((C < AW && A1[C].num()) || C == AW)
676875ed548SDimitry Andric     return eIMM(C, W);
677875ed548SDimitry Andric   return RegisterCell::self(0, W);
678875ed548SDimitry Andric }
679875ed548SDimitry Andric 
680875ed548SDimitry Andric 
681875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1,
682875ed548SDimitry Andric       uint16_t FromN) const {
683875ed548SDimitry Andric   uint16_t W = A1.width();
684875ed548SDimitry Andric   assert(FromN <= W);
685875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
686875ed548SDimitry Andric   BitValue Sign = Res[FromN-1];
687875ed548SDimitry Andric   // Sign-extend "inreg".
688875ed548SDimitry Andric   Res.fill(FromN, W, Sign);
689875ed548SDimitry Andric   return Res;
690875ed548SDimitry Andric }
691875ed548SDimitry Andric 
692875ed548SDimitry Andric 
693875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1,
694875ed548SDimitry Andric       uint16_t FromN) const {
695875ed548SDimitry Andric   uint16_t W = A1.width();
696875ed548SDimitry Andric   assert(FromN <= W);
697875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
698875ed548SDimitry Andric   Res.fill(FromN, W, BitValue::Zero);
699875ed548SDimitry Andric   return Res;
700875ed548SDimitry Andric }
701875ed548SDimitry Andric 
702875ed548SDimitry Andric 
703875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1,
704875ed548SDimitry Andric       uint16_t B, uint16_t E) const {
705875ed548SDimitry Andric   uint16_t W = A1.width();
706875ed548SDimitry Andric   assert(B < W && E <= W);
707875ed548SDimitry Andric   if (B == E)
708875ed548SDimitry Andric     return RegisterCell(0);
709875ed548SDimitry Andric   uint16_t Last = (E > 0) ? E-1 : W-1;
710875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last));
711875ed548SDimitry Andric   // Return shorter cell.
712875ed548SDimitry Andric   return Res;
713875ed548SDimitry Andric }
714875ed548SDimitry Andric 
715875ed548SDimitry Andric 
716875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1,
717875ed548SDimitry Andric       const RegisterCell &A2, uint16_t AtN) const {
718875ed548SDimitry Andric   uint16_t W1 = A1.width(), W2 = A2.width();
719875ed548SDimitry Andric   (void)W1;
720875ed548SDimitry Andric   assert(AtN < W1 && AtN+W2 <= W1);
721875ed548SDimitry Andric   // Copy bits from A1, insert A2 at position AtN.
722875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
723875ed548SDimitry Andric   if (W2 > 0)
724875ed548SDimitry Andric     Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1));
725875ed548SDimitry Andric   return Res;
726875ed548SDimitry Andric }
727875ed548SDimitry Andric 
728875ed548SDimitry Andric 
729875ed548SDimitry Andric BT::BitMask BT::MachineEvaluator::mask(unsigned Reg, unsigned Sub) const {
730875ed548SDimitry Andric   assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0");
731875ed548SDimitry Andric   uint16_t W = getRegBitWidth(Reg);
732875ed548SDimitry Andric   assert(W > 0 && "Cannot generate mask for empty register");
733875ed548SDimitry Andric   return BitMask(0, W-1);
734875ed548SDimitry Andric }
735875ed548SDimitry Andric 
7363ca95b02SDimitry Andric bool BT::MachineEvaluator::evaluate(const MachineInstr &MI,
7373ca95b02SDimitry Andric                                     const CellMapType &Inputs,
7383ca95b02SDimitry Andric                                     CellMapType &Outputs) const {
7393ca95b02SDimitry Andric   unsigned Opc = MI.getOpcode();
740875ed548SDimitry Andric   switch (Opc) {
741875ed548SDimitry Andric     case TargetOpcode::REG_SEQUENCE: {
7423ca95b02SDimitry Andric       RegisterRef RD = MI.getOperand(0);
743875ed548SDimitry Andric       assert(RD.Sub == 0);
7443ca95b02SDimitry Andric       RegisterRef RS = MI.getOperand(1);
7453ca95b02SDimitry Andric       unsigned SS = MI.getOperand(2).getImm();
7463ca95b02SDimitry Andric       RegisterRef RT = MI.getOperand(3);
7473ca95b02SDimitry Andric       unsigned ST = MI.getOperand(4).getImm();
748875ed548SDimitry Andric       assert(SS != ST);
749875ed548SDimitry Andric 
750875ed548SDimitry Andric       uint16_t W = getRegBitWidth(RD);
751875ed548SDimitry Andric       RegisterCell Res(W);
752875ed548SDimitry Andric       Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS));
753875ed548SDimitry Andric       Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST));
754875ed548SDimitry Andric       putCell(RD, Res, Outputs);
755875ed548SDimitry Andric       break;
756875ed548SDimitry Andric     }
757875ed548SDimitry Andric 
758875ed548SDimitry Andric     case TargetOpcode::COPY: {
759875ed548SDimitry Andric       // COPY can transfer a smaller register into a wider one.
760875ed548SDimitry Andric       // If that is the case, fill the remaining high bits with 0.
7613ca95b02SDimitry Andric       RegisterRef RD = MI.getOperand(0);
7623ca95b02SDimitry Andric       RegisterRef RS = MI.getOperand(1);
763875ed548SDimitry Andric       assert(RD.Sub == 0);
764875ed548SDimitry Andric       uint16_t WD = getRegBitWidth(RD);
765875ed548SDimitry Andric       uint16_t WS = getRegBitWidth(RS);
766875ed548SDimitry Andric       assert(WD >= WS);
767875ed548SDimitry Andric       RegisterCell Src = getCell(RS, Inputs);
768875ed548SDimitry Andric       RegisterCell Res(WD);
769875ed548SDimitry Andric       Res.insert(Src, BitMask(0, WS-1));
770875ed548SDimitry Andric       Res.fill(WS, WD, BitValue::Zero);
771875ed548SDimitry Andric       putCell(RD, Res, Outputs);
772875ed548SDimitry Andric       break;
773875ed548SDimitry Andric     }
774875ed548SDimitry Andric 
775875ed548SDimitry Andric     default:
776875ed548SDimitry Andric       return false;
777875ed548SDimitry Andric   }
778875ed548SDimitry Andric 
779875ed548SDimitry Andric   return true;
780875ed548SDimitry Andric }
781875ed548SDimitry Andric 
782875ed548SDimitry Andric 
783875ed548SDimitry Andric // Main W-Z implementation.
784875ed548SDimitry Andric 
7853ca95b02SDimitry Andric void BT::visitPHI(const MachineInstr &PI) {
7863ca95b02SDimitry Andric   int ThisN = PI.getParent()->getNumber();
787875ed548SDimitry Andric   if (Trace)
7883ca95b02SDimitry Andric     dbgs() << "Visit FI(BB#" << ThisN << "): " << PI;
789875ed548SDimitry Andric 
7903ca95b02SDimitry Andric   const MachineOperand &MD = PI.getOperand(0);
791875ed548SDimitry Andric   assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition");
792875ed548SDimitry Andric   RegisterRef DefRR(MD);
793875ed548SDimitry Andric   uint16_t DefBW = ME.getRegBitWidth(DefRR);
794875ed548SDimitry Andric 
795875ed548SDimitry Andric   RegisterCell DefC = ME.getCell(DefRR, Map);
796875ed548SDimitry Andric   if (DefC == RegisterCell::self(DefRR.Reg, DefBW))    // XXX slow
797875ed548SDimitry Andric     return;
798875ed548SDimitry Andric 
799875ed548SDimitry Andric   bool Changed = false;
800875ed548SDimitry Andric 
8013ca95b02SDimitry Andric   for (unsigned i = 1, n = PI.getNumOperands(); i < n; i += 2) {
8023ca95b02SDimitry Andric     const MachineBasicBlock *PB = PI.getOperand(i + 1).getMBB();
803875ed548SDimitry Andric     int PredN = PB->getNumber();
804875ed548SDimitry Andric     if (Trace)
805875ed548SDimitry Andric       dbgs() << "  edge BB#" << PredN << "->BB#" << ThisN;
806875ed548SDimitry Andric     if (!EdgeExec.count(CFGEdge(PredN, ThisN))) {
807875ed548SDimitry Andric       if (Trace)
808875ed548SDimitry Andric         dbgs() << " not executable\n";
809875ed548SDimitry Andric       continue;
810875ed548SDimitry Andric     }
811875ed548SDimitry Andric 
8123ca95b02SDimitry Andric     RegisterRef RU = PI.getOperand(i);
813875ed548SDimitry Andric     RegisterCell ResC = ME.getCell(RU, Map);
814875ed548SDimitry Andric     if (Trace)
815875ed548SDimitry Andric       dbgs() << " input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
816875ed548SDimitry Andric              << " cell: " << ResC << "\n";
817875ed548SDimitry Andric     Changed |= DefC.meet(ResC, DefRR.Reg);
818875ed548SDimitry Andric   }
819875ed548SDimitry Andric 
820875ed548SDimitry Andric   if (Changed) {
821875ed548SDimitry Andric     if (Trace)
822875ed548SDimitry Andric       dbgs() << "Output: " << PrintReg(DefRR.Reg, &ME.TRI, DefRR.Sub)
823875ed548SDimitry Andric              << " cell: " << DefC << "\n";
824875ed548SDimitry Andric     ME.putCell(DefRR, DefC, Map);
825875ed548SDimitry Andric     visitUsesOf(DefRR.Reg);
826875ed548SDimitry Andric   }
827875ed548SDimitry Andric }
828875ed548SDimitry Andric 
8293ca95b02SDimitry Andric void BT::visitNonBranch(const MachineInstr &MI) {
830875ed548SDimitry Andric   if (Trace) {
8313ca95b02SDimitry Andric     int ThisN = MI.getParent()->getNumber();
8323ca95b02SDimitry Andric     dbgs() << "Visit MI(BB#" << ThisN << "): " << MI;
833875ed548SDimitry Andric   }
8343ca95b02SDimitry Andric   if (MI.isDebugValue())
835875ed548SDimitry Andric     return;
8363ca95b02SDimitry Andric   assert(!MI.isBranch() && "Unexpected branch instruction");
837875ed548SDimitry Andric 
838875ed548SDimitry Andric   CellMapType ResMap;
839875ed548SDimitry Andric   bool Eval = ME.evaluate(MI, Map, ResMap);
840875ed548SDimitry Andric 
841875ed548SDimitry Andric   if (Trace && Eval) {
8423ca95b02SDimitry Andric     for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
8433ca95b02SDimitry Andric       const MachineOperand &MO = MI.getOperand(i);
844875ed548SDimitry Andric       if (!MO.isReg() || !MO.isUse())
845875ed548SDimitry Andric         continue;
846875ed548SDimitry Andric       RegisterRef RU(MO);
847875ed548SDimitry Andric       dbgs() << "  input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
848875ed548SDimitry Andric              << " cell: " << ME.getCell(RU, Map) << "\n";
849875ed548SDimitry Andric     }
850875ed548SDimitry Andric     dbgs() << "Outputs:\n";
851875ed548SDimitry Andric     for (CellMapType::iterator I = ResMap.begin(), E = ResMap.end();
852875ed548SDimitry Andric          I != E; ++I) {
853875ed548SDimitry Andric       RegisterRef RD(I->first);
854875ed548SDimitry Andric       dbgs() << "  " << PrintReg(I->first, &ME.TRI) << " cell: "
855875ed548SDimitry Andric              << ME.getCell(RD, ResMap) << "\n";
856875ed548SDimitry Andric     }
857875ed548SDimitry Andric   }
858875ed548SDimitry Andric 
859875ed548SDimitry Andric   // Iterate over all definitions of the instruction, and update the
860875ed548SDimitry Andric   // cells accordingly.
8613ca95b02SDimitry Andric   for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
8623ca95b02SDimitry Andric     const MachineOperand &MO = MI.getOperand(i);
863875ed548SDimitry Andric     // Visit register defs only.
864875ed548SDimitry Andric     if (!MO.isReg() || !MO.isDef())
865875ed548SDimitry Andric       continue;
866875ed548SDimitry Andric     RegisterRef RD(MO);
867875ed548SDimitry Andric     assert(RD.Sub == 0 && "Unexpected sub-register in definition");
868875ed548SDimitry Andric     if (!TargetRegisterInfo::isVirtualRegister(RD.Reg))
869875ed548SDimitry Andric       continue;
870875ed548SDimitry Andric 
871875ed548SDimitry Andric     bool Changed = false;
8727d523365SDimitry Andric     if (!Eval || ResMap.count(RD.Reg) == 0) {
873875ed548SDimitry Andric       // Set to "ref" (aka "bottom").
874875ed548SDimitry Andric       uint16_t DefBW = ME.getRegBitWidth(RD);
875875ed548SDimitry Andric       RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW);
876875ed548SDimitry Andric       if (RefC != ME.getCell(RD, Map)) {
877875ed548SDimitry Andric         ME.putCell(RD, RefC, Map);
878875ed548SDimitry Andric         Changed = true;
879875ed548SDimitry Andric       }
880875ed548SDimitry Andric     } else {
881875ed548SDimitry Andric       RegisterCell DefC = ME.getCell(RD, Map);
882875ed548SDimitry Andric       RegisterCell ResC = ME.getCell(RD, ResMap);
883875ed548SDimitry Andric       // This is a non-phi instruction, so the values of the inputs come
884875ed548SDimitry Andric       // from the same registers each time this instruction is evaluated.
885875ed548SDimitry Andric       // During the propagation, the values of the inputs can become lowered
886875ed548SDimitry Andric       // in the sense of the lattice operation, which may cause different
887875ed548SDimitry Andric       // results to be calculated in subsequent evaluations. This should
888875ed548SDimitry Andric       // not cause the bottoming of the result in the map, since the new
889875ed548SDimitry Andric       // result is already reflecting the lowered inputs.
890875ed548SDimitry Andric       for (uint16_t i = 0, w = DefC.width(); i < w; ++i) {
891875ed548SDimitry Andric         BitValue &V = DefC[i];
892875ed548SDimitry Andric         // Bits that are already "bottom" should not be updated.
893875ed548SDimitry Andric         if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg)
894875ed548SDimitry Andric           continue;
895875ed548SDimitry Andric         // Same for those that are identical in DefC and ResC.
896875ed548SDimitry Andric         if (V == ResC[i])
897875ed548SDimitry Andric           continue;
898875ed548SDimitry Andric         V = ResC[i];
899875ed548SDimitry Andric         Changed = true;
900875ed548SDimitry Andric       }
901875ed548SDimitry Andric       if (Changed)
902875ed548SDimitry Andric         ME.putCell(RD, DefC, Map);
903875ed548SDimitry Andric     }
904875ed548SDimitry Andric     if (Changed)
905875ed548SDimitry Andric       visitUsesOf(RD.Reg);
906875ed548SDimitry Andric   }
907875ed548SDimitry Andric }
908875ed548SDimitry Andric 
9093ca95b02SDimitry Andric void BT::visitBranchesFrom(const MachineInstr &BI) {
9103ca95b02SDimitry Andric   const MachineBasicBlock &B = *BI.getParent();
911875ed548SDimitry Andric   MachineBasicBlock::const_iterator It = BI, End = B.end();
912875ed548SDimitry Andric   BranchTargetList Targets, BTs;
913875ed548SDimitry Andric   bool FallsThrough = true, DefaultToAll = false;
914875ed548SDimitry Andric   int ThisN = B.getNumber();
915875ed548SDimitry Andric 
916875ed548SDimitry Andric   do {
917875ed548SDimitry Andric     BTs.clear();
9183ca95b02SDimitry Andric     const MachineInstr &MI = *It;
919875ed548SDimitry Andric     if (Trace)
9203ca95b02SDimitry Andric       dbgs() << "Visit BR(BB#" << ThisN << "): " << MI;
9213ca95b02SDimitry Andric     assert(MI.isBranch() && "Expecting branch instruction");
9223ca95b02SDimitry Andric     InstrExec.insert(&MI);
923875ed548SDimitry Andric     bool Eval = ME.evaluate(MI, Map, BTs, FallsThrough);
924875ed548SDimitry Andric     if (!Eval) {
925875ed548SDimitry Andric       // If the evaluation failed, we will add all targets. Keep going in
926875ed548SDimitry Andric       // the loop to mark all executable branches as such.
927875ed548SDimitry Andric       DefaultToAll = true;
928875ed548SDimitry Andric       FallsThrough = true;
929875ed548SDimitry Andric       if (Trace)
930875ed548SDimitry Andric         dbgs() << "  failed to evaluate: will add all CFG successors\n";
931875ed548SDimitry Andric     } else if (!DefaultToAll) {
932875ed548SDimitry Andric       // If evaluated successfully add the targets to the cumulative list.
933875ed548SDimitry Andric       if (Trace) {
934875ed548SDimitry Andric         dbgs() << "  adding targets:";
935875ed548SDimitry Andric         for (unsigned i = 0, n = BTs.size(); i < n; ++i)
936875ed548SDimitry Andric           dbgs() << " BB#" << BTs[i]->getNumber();
937875ed548SDimitry Andric         if (FallsThrough)
938875ed548SDimitry Andric           dbgs() << "\n  falls through\n";
939875ed548SDimitry Andric         else
940875ed548SDimitry Andric           dbgs() << "\n  does not fall through\n";
941875ed548SDimitry Andric       }
942875ed548SDimitry Andric       Targets.insert(BTs.begin(), BTs.end());
943875ed548SDimitry Andric     }
944875ed548SDimitry Andric     ++It;
945875ed548SDimitry Andric   } while (FallsThrough && It != End);
946875ed548SDimitry Andric 
947875ed548SDimitry Andric   typedef MachineBasicBlock::const_succ_iterator succ_iterator;
948875ed548SDimitry Andric   if (!DefaultToAll) {
949875ed548SDimitry Andric     // Need to add all CFG successors that lead to EH landing pads.
950875ed548SDimitry Andric     // There won't be explicit branches to these blocks, but they must
951875ed548SDimitry Andric     // be processed.
952875ed548SDimitry Andric     for (succ_iterator I = B.succ_begin(), E = B.succ_end(); I != E; ++I) {
953875ed548SDimitry Andric       const MachineBasicBlock *SB = *I;
9547d523365SDimitry Andric       if (SB->isEHPad())
955875ed548SDimitry Andric         Targets.insert(SB);
956875ed548SDimitry Andric     }
957875ed548SDimitry Andric     if (FallsThrough) {
9587d523365SDimitry Andric       MachineFunction::const_iterator BIt = B.getIterator();
959875ed548SDimitry Andric       MachineFunction::const_iterator Next = std::next(BIt);
960875ed548SDimitry Andric       if (Next != MF.end())
961875ed548SDimitry Andric         Targets.insert(&*Next);
962875ed548SDimitry Andric     }
963875ed548SDimitry Andric   } else {
964875ed548SDimitry Andric     for (succ_iterator I = B.succ_begin(), E = B.succ_end(); I != E; ++I)
965875ed548SDimitry Andric       Targets.insert(*I);
966875ed548SDimitry Andric   }
967875ed548SDimitry Andric 
968875ed548SDimitry Andric   for (unsigned i = 0, n = Targets.size(); i < n; ++i) {
969875ed548SDimitry Andric     int TargetN = Targets[i]->getNumber();
970875ed548SDimitry Andric     FlowQ.push(CFGEdge(ThisN, TargetN));
971875ed548SDimitry Andric   }
972875ed548SDimitry Andric }
973875ed548SDimitry Andric 
974875ed548SDimitry Andric 
975875ed548SDimitry Andric void BT::visitUsesOf(unsigned Reg) {
976875ed548SDimitry Andric   if (Trace)
977875ed548SDimitry Andric     dbgs() << "visiting uses of " << PrintReg(Reg, &ME.TRI) << "\n";
978875ed548SDimitry Andric 
979875ed548SDimitry Andric   typedef MachineRegisterInfo::use_nodbg_iterator use_iterator;
980875ed548SDimitry Andric   use_iterator End = MRI.use_nodbg_end();
981875ed548SDimitry Andric   for (use_iterator I = MRI.use_nodbg_begin(Reg); I != End; ++I) {
982875ed548SDimitry Andric     MachineInstr *UseI = I->getParent();
983875ed548SDimitry Andric     if (!InstrExec.count(UseI))
984875ed548SDimitry Andric       continue;
985875ed548SDimitry Andric     if (UseI->isPHI())
9863ca95b02SDimitry Andric       visitPHI(*UseI);
987875ed548SDimitry Andric     else if (!UseI->isBranch())
9883ca95b02SDimitry Andric       visitNonBranch(*UseI);
989875ed548SDimitry Andric     else
9903ca95b02SDimitry Andric       visitBranchesFrom(*UseI);
991875ed548SDimitry Andric   }
992875ed548SDimitry Andric }
993875ed548SDimitry Andric 
994875ed548SDimitry Andric 
995875ed548SDimitry Andric BT::RegisterCell BT::get(RegisterRef RR) const {
996875ed548SDimitry Andric   return ME.getCell(RR, Map);
997875ed548SDimitry Andric }
998875ed548SDimitry Andric 
999875ed548SDimitry Andric 
1000875ed548SDimitry Andric void BT::put(RegisterRef RR, const RegisterCell &RC) {
1001875ed548SDimitry Andric   ME.putCell(RR, RC, Map);
1002875ed548SDimitry Andric }
1003875ed548SDimitry Andric 
1004875ed548SDimitry Andric 
1005875ed548SDimitry Andric // Replace all references to bits from OldRR with the corresponding bits
1006875ed548SDimitry Andric // in NewRR.
1007875ed548SDimitry Andric void BT::subst(RegisterRef OldRR, RegisterRef NewRR) {
10087d523365SDimitry Andric   assert(Map.count(OldRR.Reg) > 0 && "OldRR not present in map");
1009875ed548SDimitry Andric   BitMask OM = ME.mask(OldRR.Reg, OldRR.Sub);
1010875ed548SDimitry Andric   BitMask NM = ME.mask(NewRR.Reg, NewRR.Sub);
1011875ed548SDimitry Andric   uint16_t OMB = OM.first(), OME = OM.last();
1012875ed548SDimitry Andric   uint16_t NMB = NM.first(), NME = NM.last();
1013875ed548SDimitry Andric   (void)NME;
1014875ed548SDimitry Andric   assert((OME-OMB == NME-NMB) &&
1015875ed548SDimitry Andric          "Substituting registers of different lengths");
1016875ed548SDimitry Andric   for (CellMapType::iterator I = Map.begin(), E = Map.end(); I != E; ++I) {
1017875ed548SDimitry Andric     RegisterCell &RC = I->second;
1018875ed548SDimitry Andric     for (uint16_t i = 0, w = RC.width(); i < w; ++i) {
1019875ed548SDimitry Andric       BitValue &V = RC[i];
1020875ed548SDimitry Andric       if (V.Type != BitValue::Ref || V.RefI.Reg != OldRR.Reg)
1021875ed548SDimitry Andric         continue;
1022875ed548SDimitry Andric       if (V.RefI.Pos < OMB || V.RefI.Pos > OME)
1023875ed548SDimitry Andric         continue;
1024875ed548SDimitry Andric       V.RefI.Reg = NewRR.Reg;
1025875ed548SDimitry Andric       V.RefI.Pos += NMB-OMB;
1026875ed548SDimitry Andric     }
1027875ed548SDimitry Andric   }
1028875ed548SDimitry Andric }
1029875ed548SDimitry Andric 
1030875ed548SDimitry Andric 
1031875ed548SDimitry Andric // Check if the block has been "executed" during propagation. (If not, the
1032875ed548SDimitry Andric // block is dead, but it may still appear to be reachable.)
1033875ed548SDimitry Andric bool BT::reached(const MachineBasicBlock *B) const {
1034875ed548SDimitry Andric   int BN = B->getNumber();
1035875ed548SDimitry Andric   assert(BN >= 0);
1036875ed548SDimitry Andric   for (EdgeSetType::iterator I = EdgeExec.begin(), E = EdgeExec.end();
1037875ed548SDimitry Andric        I != E; ++I) {
1038875ed548SDimitry Andric     if (I->second == BN)
1039875ed548SDimitry Andric       return true;
1040875ed548SDimitry Andric   }
1041875ed548SDimitry Andric   return false;
1042875ed548SDimitry Andric }
1043875ed548SDimitry Andric 
1044875ed548SDimitry Andric 
1045875ed548SDimitry Andric void BT::reset() {
1046875ed548SDimitry Andric   EdgeExec.clear();
1047875ed548SDimitry Andric   InstrExec.clear();
1048875ed548SDimitry Andric   Map.clear();
1049875ed548SDimitry Andric }
1050875ed548SDimitry Andric 
1051875ed548SDimitry Andric 
1052875ed548SDimitry Andric void BT::run() {
1053875ed548SDimitry Andric   reset();
1054875ed548SDimitry Andric   assert(FlowQ.empty());
1055875ed548SDimitry Andric 
1056875ed548SDimitry Andric   typedef GraphTraits<const MachineFunction*> MachineFlowGraphTraits;
1057875ed548SDimitry Andric   const MachineBasicBlock *Entry = MachineFlowGraphTraits::getEntryNode(&MF);
1058875ed548SDimitry Andric 
1059875ed548SDimitry Andric   unsigned MaxBN = 0;
1060875ed548SDimitry Andric   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
1061875ed548SDimitry Andric        I != E; ++I) {
1062875ed548SDimitry Andric     assert(I->getNumber() >= 0 && "Disconnected block");
1063875ed548SDimitry Andric     unsigned BN = I->getNumber();
1064875ed548SDimitry Andric     if (BN > MaxBN)
1065875ed548SDimitry Andric       MaxBN = BN;
1066875ed548SDimitry Andric   }
1067875ed548SDimitry Andric 
1068875ed548SDimitry Andric   // Keep track of visited blocks.
1069875ed548SDimitry Andric   BitVector BlockScanned(MaxBN+1);
1070875ed548SDimitry Andric 
1071875ed548SDimitry Andric   int EntryN = Entry->getNumber();
1072875ed548SDimitry Andric   // Generate a fake edge to get something to start with.
1073875ed548SDimitry Andric   FlowQ.push(CFGEdge(-1, EntryN));
1074875ed548SDimitry Andric 
1075875ed548SDimitry Andric   while (!FlowQ.empty()) {
1076875ed548SDimitry Andric     CFGEdge Edge = FlowQ.front();
1077875ed548SDimitry Andric     FlowQ.pop();
1078875ed548SDimitry Andric 
1079875ed548SDimitry Andric     if (EdgeExec.count(Edge))
1080875ed548SDimitry Andric       continue;
1081875ed548SDimitry Andric     EdgeExec.insert(Edge);
1082875ed548SDimitry Andric 
1083875ed548SDimitry Andric     const MachineBasicBlock &B = *MF.getBlockNumbered(Edge.second);
1084875ed548SDimitry Andric     MachineBasicBlock::const_iterator It = B.begin(), End = B.end();
1085875ed548SDimitry Andric     // Visit PHI nodes first.
1086875ed548SDimitry Andric     while (It != End && It->isPHI()) {
10873ca95b02SDimitry Andric       const MachineInstr &PI = *It++;
10883ca95b02SDimitry Andric       InstrExec.insert(&PI);
1089875ed548SDimitry Andric       visitPHI(PI);
1090875ed548SDimitry Andric     }
1091875ed548SDimitry Andric 
1092875ed548SDimitry Andric     // If this block has already been visited through a flow graph edge,
1093875ed548SDimitry Andric     // then the instructions have already been processed. Any updates to
1094875ed548SDimitry Andric     // the cells would now only happen through visitUsesOf...
1095875ed548SDimitry Andric     if (BlockScanned[Edge.second])
1096875ed548SDimitry Andric       continue;
1097875ed548SDimitry Andric     BlockScanned[Edge.second] = true;
1098875ed548SDimitry Andric 
1099875ed548SDimitry Andric     // Visit non-branch instructions.
1100875ed548SDimitry Andric     while (It != End && !It->isBranch()) {
11013ca95b02SDimitry Andric       const MachineInstr &MI = *It++;
11023ca95b02SDimitry Andric       InstrExec.insert(&MI);
1103875ed548SDimitry Andric       visitNonBranch(MI);
1104875ed548SDimitry Andric     }
1105875ed548SDimitry Andric     // If block end has been reached, add the fall-through edge to the queue.
1106875ed548SDimitry Andric     if (It == End) {
11077d523365SDimitry Andric       MachineFunction::const_iterator BIt = B.getIterator();
1108875ed548SDimitry Andric       MachineFunction::const_iterator Next = std::next(BIt);
11097d523365SDimitry Andric       if (Next != MF.end() && B.isSuccessor(&*Next)) {
1110875ed548SDimitry Andric         int ThisN = B.getNumber();
1111875ed548SDimitry Andric         int NextN = Next->getNumber();
1112875ed548SDimitry Andric         FlowQ.push(CFGEdge(ThisN, NextN));
1113875ed548SDimitry Andric       }
1114875ed548SDimitry Andric     } else {
1115875ed548SDimitry Andric       // Handle the remaining sequence of branches. This function will update
1116875ed548SDimitry Andric       // the work queue.
11173ca95b02SDimitry Andric       visitBranchesFrom(*It);
1118875ed548SDimitry Andric     }
1119875ed548SDimitry Andric   } // while (!FlowQ->empty())
1120875ed548SDimitry Andric 
1121875ed548SDimitry Andric   if (Trace) {
1122875ed548SDimitry Andric     dbgs() << "Cells after propagation:\n";
1123875ed548SDimitry Andric     for (CellMapType::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
1124875ed548SDimitry Andric       dbgs() << PrintReg(I->first, &ME.TRI) << " -> " << I->second << "\n";
1125875ed548SDimitry Andric   }
1126875ed548SDimitry Andric }
1127875ed548SDimitry Andric 
1128