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 
87875ed548SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const BT::BitValue &BV) {
88875ed548SDimitry Andric   switch (BV.Type) {
89875ed548SDimitry Andric     case BT::BitValue::Top:
90875ed548SDimitry Andric       OS << 'T';
91875ed548SDimitry Andric       break;
92875ed548SDimitry Andric     case BT::BitValue::Zero:
93875ed548SDimitry Andric       OS << '0';
94875ed548SDimitry Andric       break;
95875ed548SDimitry Andric     case BT::BitValue::One:
96875ed548SDimitry Andric       OS << '1';
97875ed548SDimitry Andric       break;
98875ed548SDimitry Andric     case BT::BitValue::Ref:
99875ed548SDimitry Andric       OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']';
100875ed548SDimitry Andric       break;
101875ed548SDimitry Andric   }
102875ed548SDimitry Andric   return OS;
103875ed548SDimitry Andric }
104875ed548SDimitry Andric 
105875ed548SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const BT::RegisterCell &RC) {
106875ed548SDimitry Andric   unsigned n = RC.Bits.size();
107875ed548SDimitry Andric   OS << "{ w:" << n;
108875ed548SDimitry Andric   // Instead of printing each bit value individually, try to group them
109875ed548SDimitry Andric   // into logical segments, such as sequences of 0 or 1 bits or references
110875ed548SDimitry Andric   // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz").
111875ed548SDimitry Andric   // "Start" will be the index of the beginning of the most recent segment.
112875ed548SDimitry Andric   unsigned Start = 0;
113875ed548SDimitry Andric   bool SeqRef = false;    // A sequence of refs to consecutive bits.
114875ed548SDimitry Andric   bool ConstRef = false;  // A sequence of refs to the same bit.
115875ed548SDimitry Andric 
116875ed548SDimitry Andric   for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) {
117875ed548SDimitry Andric     const BT::BitValue &V = RC[i];
118875ed548SDimitry Andric     const BT::BitValue &SV = RC[Start];
119875ed548SDimitry Andric     bool IsRef = (V.Type == BT::BitValue::Ref);
120875ed548SDimitry Andric     // If the current value is the same as Start, skip to the next one.
121875ed548SDimitry Andric     if (!IsRef && V == SV)
122875ed548SDimitry Andric       continue;
123875ed548SDimitry Andric     if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) {
124875ed548SDimitry Andric       if (Start+1 == i) {
125875ed548SDimitry Andric         SeqRef = (V.RefI.Pos == SV.RefI.Pos+1);
126875ed548SDimitry Andric         ConstRef = (V.RefI.Pos == SV.RefI.Pos);
127875ed548SDimitry Andric       }
128875ed548SDimitry Andric       if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start))
129875ed548SDimitry Andric         continue;
130875ed548SDimitry Andric       if (ConstRef && V.RefI.Pos == SV.RefI.Pos)
131875ed548SDimitry Andric         continue;
132875ed548SDimitry Andric     }
133875ed548SDimitry Andric 
134875ed548SDimitry Andric     // The current value is different. Print the previous one and reset
135875ed548SDimitry Andric     // the Start.
136875ed548SDimitry Andric     OS << " [" << Start;
137875ed548SDimitry Andric     unsigned Count = i - Start;
138875ed548SDimitry Andric     if (Count == 1) {
139875ed548SDimitry Andric       OS << "]:" << SV;
140875ed548SDimitry Andric     } else {
141875ed548SDimitry Andric       OS << '-' << i-1 << "]:";
142875ed548SDimitry Andric       if (SV.Type == BT::BitValue::Ref && SeqRef)
143875ed548SDimitry Andric         OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
144875ed548SDimitry Andric            << SV.RefI.Pos+(Count-1) << ']';
145875ed548SDimitry Andric       else
146875ed548SDimitry Andric         OS << SV;
147875ed548SDimitry Andric     }
148875ed548SDimitry Andric     Start = i;
149875ed548SDimitry Andric     SeqRef = ConstRef = false;
150875ed548SDimitry Andric   }
151875ed548SDimitry Andric 
152875ed548SDimitry Andric   OS << " [" << Start;
153875ed548SDimitry Andric   unsigned Count = n - Start;
154875ed548SDimitry Andric   if (n-Start == 1) {
155875ed548SDimitry Andric     OS << "]:" << RC[Start];
156875ed548SDimitry Andric   } else {
157875ed548SDimitry Andric     OS << '-' << n-1 << "]:";
158875ed548SDimitry Andric     const BT::BitValue &SV = RC[Start];
159875ed548SDimitry Andric     if (SV.Type == BT::BitValue::Ref && SeqRef)
160875ed548SDimitry Andric       OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
161875ed548SDimitry Andric          << SV.RefI.Pos+(Count-1) << ']';
162875ed548SDimitry Andric     else
163875ed548SDimitry Andric       OS << SV;
164875ed548SDimitry Andric   }
165875ed548SDimitry Andric   OS << " }";
166875ed548SDimitry Andric 
167875ed548SDimitry Andric   return OS;
168875ed548SDimitry Andric }
169875ed548SDimitry Andric 
170875ed548SDimitry Andric BitTracker::BitTracker(const MachineEvaluator &E, MachineFunction &F)
171875ed548SDimitry Andric     : Trace(false), ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType) {}
172875ed548SDimitry Andric 
173875ed548SDimitry Andric BitTracker::~BitTracker() {
174875ed548SDimitry Andric   delete &Map;
175875ed548SDimitry Andric }
176875ed548SDimitry Andric 
177875ed548SDimitry Andric 
178875ed548SDimitry Andric // If we were allowed to update a cell for a part of a register, the meet
179875ed548SDimitry Andric // operation would need to be parametrized by the register number and the
180875ed548SDimitry Andric // exact part of the register, so that the computer BitRefs correspond to
181875ed548SDimitry Andric // the actual bits of the "self" register.
182875ed548SDimitry Andric // While this cannot happen in the current implementation, I'm not sure
183875ed548SDimitry Andric // if this should be ruled out in the future.
184875ed548SDimitry Andric bool BT::RegisterCell::meet(const RegisterCell &RC, unsigned SelfR) {
185875ed548SDimitry Andric   // An example when "meet" can be invoked with SelfR == 0 is a phi node
186875ed548SDimitry Andric   // with a physical register as an operand.
187875ed548SDimitry Andric   assert(SelfR == 0 || TargetRegisterInfo::isVirtualRegister(SelfR));
188875ed548SDimitry Andric   bool Changed = false;
189875ed548SDimitry Andric   for (uint16_t i = 0, n = Bits.size(); i < n; ++i) {
190875ed548SDimitry Andric     const BitValue &RCV = RC[i];
191875ed548SDimitry Andric     Changed |= Bits[i].meet(RCV, BitRef(SelfR, i));
192875ed548SDimitry Andric   }
193875ed548SDimitry Andric   return Changed;
194875ed548SDimitry Andric }
195875ed548SDimitry Andric 
196875ed548SDimitry Andric 
197875ed548SDimitry Andric // Insert the entire cell RC into the current cell at position given by M.
198875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC,
199875ed548SDimitry Andric       const BitMask &M) {
200875ed548SDimitry Andric   uint16_t B = M.first(), E = M.last(), W = width();
201875ed548SDimitry Andric   // Sanity: M must be a valid mask for *this.
202875ed548SDimitry Andric   assert(B < W && E < W);
203875ed548SDimitry Andric   // Sanity: the masked part of *this must have the same number of bits
204875ed548SDimitry Andric   // as the source.
205875ed548SDimitry Andric   assert(B > E || E-B+1 == RC.width());      // B <= E  =>  E-B+1 = |RC|.
206875ed548SDimitry Andric   assert(B <= E || E+(W-B)+1 == RC.width()); // E < B   =>  E+(W-B)+1 = |RC|.
207875ed548SDimitry Andric   if (B <= E) {
208875ed548SDimitry Andric     for (uint16_t i = 0; i <= E-B; ++i)
209875ed548SDimitry Andric       Bits[i+B] = RC[i];
210875ed548SDimitry Andric   } else {
211875ed548SDimitry Andric     for (uint16_t i = 0; i < W-B; ++i)
212875ed548SDimitry Andric       Bits[i+B] = RC[i];
213875ed548SDimitry Andric     for (uint16_t i = 0; i <= E; ++i)
214875ed548SDimitry Andric       Bits[i] = RC[i+(W-B)];
215875ed548SDimitry Andric   }
216875ed548SDimitry Andric   return *this;
217875ed548SDimitry Andric }
218875ed548SDimitry Andric 
219875ed548SDimitry Andric 
220875ed548SDimitry Andric BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const {
221875ed548SDimitry Andric   uint16_t B = M.first(), E = M.last(), W = width();
222875ed548SDimitry Andric   assert(B < W && E < W);
223875ed548SDimitry Andric   if (B <= E) {
224875ed548SDimitry Andric     RegisterCell RC(E-B+1);
225875ed548SDimitry Andric     for (uint16_t i = B; i <= E; ++i)
226875ed548SDimitry Andric       RC.Bits[i-B] = Bits[i];
227875ed548SDimitry Andric     return RC;
228875ed548SDimitry Andric   }
229875ed548SDimitry Andric 
230875ed548SDimitry Andric   RegisterCell RC(E+(W-B)+1);
231875ed548SDimitry Andric   for (uint16_t i = 0; i < W-B; ++i)
232875ed548SDimitry Andric     RC.Bits[i] = Bits[i+B];
233875ed548SDimitry Andric   for (uint16_t i = 0; i <= E; ++i)
234875ed548SDimitry Andric     RC.Bits[i+(W-B)] = Bits[i];
235875ed548SDimitry Andric   return RC;
236875ed548SDimitry Andric }
237875ed548SDimitry Andric 
238875ed548SDimitry Andric 
239875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) {
240875ed548SDimitry Andric   // Rotate left (i.e. towards increasing bit indices).
241875ed548SDimitry Andric   // Swap the two parts:  [0..W-Sh-1] [W-Sh..W-1]
242875ed548SDimitry Andric   uint16_t W = width();
243875ed548SDimitry Andric   Sh = Sh % W;
244875ed548SDimitry Andric   if (Sh == 0)
245875ed548SDimitry Andric     return *this;
246875ed548SDimitry Andric 
247875ed548SDimitry Andric   RegisterCell Tmp(W-Sh);
248875ed548SDimitry Andric   // Tmp = [0..W-Sh-1].
249875ed548SDimitry Andric   for (uint16_t i = 0; i < W-Sh; ++i)
250875ed548SDimitry Andric     Tmp[i] = Bits[i];
251875ed548SDimitry Andric   // Shift [W-Sh..W-1] to [0..Sh-1].
252875ed548SDimitry Andric   for (uint16_t i = 0; i < Sh; ++i)
253875ed548SDimitry Andric     Bits[i] = Bits[W-Sh+i];
254875ed548SDimitry Andric   // Copy Tmp to [Sh..W-1].
255875ed548SDimitry Andric   for (uint16_t i = 0; i < W-Sh; ++i)
256875ed548SDimitry Andric     Bits[i+Sh] = Tmp.Bits[i];
257875ed548SDimitry Andric   return *this;
258875ed548SDimitry Andric }
259875ed548SDimitry Andric 
260875ed548SDimitry Andric 
261875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E,
262875ed548SDimitry Andric       const BitValue &V) {
263875ed548SDimitry Andric   assert(B <= E);
264875ed548SDimitry Andric   while (B < E)
265875ed548SDimitry Andric     Bits[B++] = V;
266875ed548SDimitry Andric   return *this;
267875ed548SDimitry Andric }
268875ed548SDimitry Andric 
269875ed548SDimitry Andric 
270875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) {
271875ed548SDimitry Andric   // Append the cell given as the argument to the "this" cell.
272875ed548SDimitry Andric   // Bit 0 of RC becomes bit W of the result, where W is this->width().
273875ed548SDimitry Andric   uint16_t W = width(), WRC = RC.width();
274875ed548SDimitry Andric   Bits.resize(W+WRC);
275875ed548SDimitry Andric   for (uint16_t i = 0; i < WRC; ++i)
276875ed548SDimitry Andric     Bits[i+W] = RC.Bits[i];
277875ed548SDimitry Andric   return *this;
278875ed548SDimitry Andric }
279875ed548SDimitry Andric 
280875ed548SDimitry Andric 
281875ed548SDimitry Andric uint16_t BT::RegisterCell::ct(bool B) const {
282875ed548SDimitry Andric   uint16_t W = width();
283875ed548SDimitry Andric   uint16_t C = 0;
284875ed548SDimitry Andric   BitValue V = B;
285875ed548SDimitry Andric   while (C < W && Bits[C] == V)
286875ed548SDimitry Andric     C++;
287875ed548SDimitry Andric   return C;
288875ed548SDimitry Andric }
289875ed548SDimitry Andric 
290875ed548SDimitry Andric 
291875ed548SDimitry Andric uint16_t BT::RegisterCell::cl(bool B) const {
292875ed548SDimitry Andric   uint16_t W = width();
293875ed548SDimitry Andric   uint16_t C = 0;
294875ed548SDimitry Andric   BitValue V = B;
295875ed548SDimitry Andric   while (C < W && Bits[W-(C+1)] == V)
296875ed548SDimitry Andric     C++;
297875ed548SDimitry Andric   return C;
298875ed548SDimitry Andric }
299875ed548SDimitry Andric 
300875ed548SDimitry Andric 
301875ed548SDimitry Andric bool BT::RegisterCell::operator== (const RegisterCell &RC) const {
302875ed548SDimitry Andric   uint16_t W = Bits.size();
303875ed548SDimitry Andric   if (RC.Bits.size() != W)
304875ed548SDimitry Andric     return false;
305875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i)
306875ed548SDimitry Andric     if (Bits[i] != RC[i])
307875ed548SDimitry Andric       return false;
308875ed548SDimitry Andric   return true;
309875ed548SDimitry Andric }
310875ed548SDimitry Andric 
311875ed548SDimitry Andric 
312875ed548SDimitry Andric uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const {
313875ed548SDimitry Andric   // The general problem is with finding a register class that corresponds
314875ed548SDimitry Andric   // to a given reference reg:sub. There can be several such classes, and
315875ed548SDimitry Andric   // since we only care about the register size, it does not matter which
316875ed548SDimitry Andric   // such class we would find.
317875ed548SDimitry Andric   // The easiest way to accomplish what we want is to
318875ed548SDimitry Andric   // 1. find a physical register PhysR from the same class as RR.Reg,
319875ed548SDimitry Andric   // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub,
320875ed548SDimitry Andric   // 3. find a register class that contains PhysS.
321875ed548SDimitry Andric   unsigned PhysR;
322875ed548SDimitry Andric   if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
323875ed548SDimitry Andric     const TargetRegisterClass *VC = MRI.getRegClass(RR.Reg);
324875ed548SDimitry Andric     assert(VC->begin() != VC->end() && "Empty register class");
325875ed548SDimitry Andric     PhysR = *VC->begin();
326875ed548SDimitry Andric   } else {
327875ed548SDimitry Andric     assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
328875ed548SDimitry Andric     PhysR = RR.Reg;
329875ed548SDimitry Andric   }
330875ed548SDimitry Andric 
331875ed548SDimitry Andric   unsigned PhysS = (RR.Sub == 0) ? PhysR : TRI.getSubReg(PhysR, RR.Sub);
332875ed548SDimitry Andric   const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PhysS);
333875ed548SDimitry Andric   uint16_t BW = RC->getSize()*8;
334875ed548SDimitry Andric   return BW;
335875ed548SDimitry Andric }
336875ed548SDimitry Andric 
337875ed548SDimitry Andric 
338875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR,
339875ed548SDimitry Andric       const CellMapType &M) const {
340875ed548SDimitry Andric   uint16_t BW = getRegBitWidth(RR);
341875ed548SDimitry Andric 
342875ed548SDimitry Andric   // Physical registers are assumed to be present in the map with an unknown
343875ed548SDimitry Andric   // value. Don't actually insert anything in the map, just return the cell.
344875ed548SDimitry Andric   if (TargetRegisterInfo::isPhysicalRegister(RR.Reg))
345875ed548SDimitry Andric     return RegisterCell::self(0, BW);
346875ed548SDimitry Andric 
347875ed548SDimitry Andric   assert(TargetRegisterInfo::isVirtualRegister(RR.Reg));
348875ed548SDimitry Andric   // For virtual registers that belong to a class that is not tracked,
349875ed548SDimitry Andric   // generate an "unknown" value as well.
350875ed548SDimitry Andric   const TargetRegisterClass *C = MRI.getRegClass(RR.Reg);
351875ed548SDimitry Andric   if (!track(C))
352875ed548SDimitry Andric     return RegisterCell::self(0, BW);
353875ed548SDimitry Andric 
354875ed548SDimitry Andric   CellMapType::const_iterator F = M.find(RR.Reg);
355875ed548SDimitry Andric   if (F != M.end()) {
356875ed548SDimitry Andric     if (!RR.Sub)
357875ed548SDimitry Andric       return F->second;
358875ed548SDimitry Andric     BitMask M = mask(RR.Reg, RR.Sub);
359875ed548SDimitry Andric     return F->second.extract(M);
360875ed548SDimitry Andric   }
361875ed548SDimitry Andric   // If not found, create a "top" entry, but do not insert it in the map.
362875ed548SDimitry Andric   return RegisterCell::top(BW);
363875ed548SDimitry Andric }
364875ed548SDimitry Andric 
365875ed548SDimitry Andric 
366875ed548SDimitry Andric void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC,
367875ed548SDimitry Andric       CellMapType &M) const {
368875ed548SDimitry Andric   // While updating the cell map can be done in a meaningful way for
369875ed548SDimitry Andric   // a part of a register, it makes little sense to implement it as the
370875ed548SDimitry Andric   // SSA representation would never contain such "partial definitions".
371875ed548SDimitry Andric   if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
372875ed548SDimitry Andric     return;
373875ed548SDimitry Andric   assert(RR.Sub == 0 && "Unexpected sub-register in definition");
374875ed548SDimitry Andric   // Eliminate all ref-to-reg-0 bit values: replace them with "self".
375875ed548SDimitry Andric   for (unsigned i = 0, n = RC.width(); i < n; ++i) {
376875ed548SDimitry Andric     const BitValue &V = RC[i];
377875ed548SDimitry Andric     if (V.Type == BitValue::Ref && V.RefI.Reg == 0)
378875ed548SDimitry Andric       RC[i].RefI = BitRef(RR.Reg, i);
379875ed548SDimitry Andric   }
380875ed548SDimitry Andric   M[RR.Reg] = RC;
381875ed548SDimitry Andric }
382875ed548SDimitry Andric 
383875ed548SDimitry Andric 
384875ed548SDimitry Andric // Check if the cell represents a compile-time integer value.
385875ed548SDimitry Andric bool BT::MachineEvaluator::isInt(const RegisterCell &A) const {
386875ed548SDimitry Andric   uint16_t W = A.width();
387875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i)
388875ed548SDimitry Andric     if (!A[i].is(0) && !A[i].is(1))
389875ed548SDimitry Andric       return false;
390875ed548SDimitry Andric   return true;
391875ed548SDimitry Andric }
392875ed548SDimitry Andric 
393875ed548SDimitry Andric 
394875ed548SDimitry Andric // Convert a cell to the integer value. The result must fit in uint64_t.
395875ed548SDimitry Andric uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const {
396875ed548SDimitry Andric   assert(isInt(A));
397875ed548SDimitry Andric   uint64_t Val = 0;
398875ed548SDimitry Andric   uint16_t W = A.width();
399875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
400875ed548SDimitry Andric     Val <<= 1;
401875ed548SDimitry Andric     Val |= A[i].is(1);
402875ed548SDimitry Andric   }
403875ed548SDimitry Andric   return Val;
404875ed548SDimitry Andric }
405875ed548SDimitry Andric 
406875ed548SDimitry Andric 
407875ed548SDimitry Andric // Evaluator helper functions. These implement some common operation on
408875ed548SDimitry Andric // register cells that can be used to implement target-specific instructions
409875ed548SDimitry Andric // in a target-specific evaluator.
410875ed548SDimitry Andric 
411875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const {
412875ed548SDimitry Andric   RegisterCell Res(W);
413875ed548SDimitry Andric   // For bits beyond the 63rd, this will generate the sign bit of V.
414875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
415875ed548SDimitry Andric     Res[i] = BitValue(V & 1);
416875ed548SDimitry Andric     V >>= 1;
417875ed548SDimitry Andric   }
418875ed548SDimitry Andric   return Res;
419875ed548SDimitry Andric }
420875ed548SDimitry Andric 
421875ed548SDimitry Andric 
422875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const {
423875ed548SDimitry Andric   APInt A = CI->getValue();
424875ed548SDimitry Andric   uint16_t BW = A.getBitWidth();
425875ed548SDimitry Andric   assert((unsigned)BW == A.getBitWidth() && "BitWidth overflow");
426875ed548SDimitry Andric   RegisterCell Res(BW);
427875ed548SDimitry Andric   for (uint16_t i = 0; i < BW; ++i)
428875ed548SDimitry Andric     Res[i] = A[i];
429875ed548SDimitry Andric   return Res;
430875ed548SDimitry Andric }
431875ed548SDimitry Andric 
432875ed548SDimitry Andric 
433875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1,
434875ed548SDimitry Andric       const RegisterCell &A2) const {
435875ed548SDimitry Andric   uint16_t W = A1.width();
436875ed548SDimitry Andric   assert(W == A2.width());
437875ed548SDimitry Andric   RegisterCell Res(W);
438875ed548SDimitry Andric   bool Carry = false;
439875ed548SDimitry Andric   uint16_t I;
440875ed548SDimitry Andric   for (I = 0; I < W; ++I) {
441875ed548SDimitry Andric     const BitValue &V1 = A1[I];
442875ed548SDimitry Andric     const BitValue &V2 = A2[I];
443875ed548SDimitry Andric     if (!V1.num() || !V2.num())
444875ed548SDimitry Andric       break;
445875ed548SDimitry Andric     unsigned S = bool(V1) + bool(V2) + Carry;
446875ed548SDimitry Andric     Res[I] = BitValue(S & 1);
447875ed548SDimitry Andric     Carry = (S > 1);
448875ed548SDimitry Andric   }
449875ed548SDimitry Andric   for (; I < W; ++I) {
450875ed548SDimitry Andric     const BitValue &V1 = A1[I];
451875ed548SDimitry Andric     const BitValue &V2 = A2[I];
452875ed548SDimitry Andric     // If the next bit is same as Carry, the result will be 0 plus the
453875ed548SDimitry Andric     // other bit. The Carry bit will remain unchanged.
454875ed548SDimitry Andric     if (V1.is(Carry))
455875ed548SDimitry Andric       Res[I] = BitValue::ref(V2);
456875ed548SDimitry Andric     else if (V2.is(Carry))
457875ed548SDimitry Andric       Res[I] = BitValue::ref(V1);
458875ed548SDimitry Andric     else
459875ed548SDimitry Andric       break;
460875ed548SDimitry Andric   }
461875ed548SDimitry Andric   for (; I < W; ++I)
462875ed548SDimitry Andric     Res[I] = BitValue::self();
463875ed548SDimitry Andric   return Res;
464875ed548SDimitry Andric }
465875ed548SDimitry Andric 
466875ed548SDimitry Andric 
467875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1,
468875ed548SDimitry Andric       const RegisterCell &A2) const {
469875ed548SDimitry Andric   uint16_t W = A1.width();
470875ed548SDimitry Andric   assert(W == A2.width());
471875ed548SDimitry Andric   RegisterCell Res(W);
472875ed548SDimitry Andric   bool Borrow = false;
473875ed548SDimitry Andric   uint16_t I;
474875ed548SDimitry Andric   for (I = 0; I < W; ++I) {
475875ed548SDimitry Andric     const BitValue &V1 = A1[I];
476875ed548SDimitry Andric     const BitValue &V2 = A2[I];
477875ed548SDimitry Andric     if (!V1.num() || !V2.num())
478875ed548SDimitry Andric       break;
479875ed548SDimitry Andric     unsigned S = bool(V1) - bool(V2) - Borrow;
480875ed548SDimitry Andric     Res[I] = BitValue(S & 1);
481875ed548SDimitry Andric     Borrow = (S > 1);
482875ed548SDimitry Andric   }
483875ed548SDimitry Andric   for (; I < W; ++I) {
484875ed548SDimitry Andric     const BitValue &V1 = A1[I];
485875ed548SDimitry Andric     const BitValue &V2 = A2[I];
486875ed548SDimitry Andric     if (V1.is(Borrow)) {
487875ed548SDimitry Andric       Res[I] = BitValue::ref(V2);
488875ed548SDimitry Andric       break;
489875ed548SDimitry Andric     }
490875ed548SDimitry Andric     if (V2.is(Borrow))
491875ed548SDimitry Andric       Res[I] = BitValue::ref(V1);
492875ed548SDimitry Andric     else
493875ed548SDimitry Andric       break;
494875ed548SDimitry Andric   }
495875ed548SDimitry Andric   for (; I < W; ++I)
496875ed548SDimitry Andric     Res[I] = BitValue::self();
497875ed548SDimitry Andric   return Res;
498875ed548SDimitry Andric }
499875ed548SDimitry Andric 
500875ed548SDimitry Andric 
501875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1,
502875ed548SDimitry Andric       const RegisterCell &A2) const {
503875ed548SDimitry Andric   uint16_t W = A1.width() + A2.width();
504875ed548SDimitry Andric   uint16_t Z = A1.ct(0) + A2.ct(0);
505875ed548SDimitry Andric   RegisterCell Res(W);
506875ed548SDimitry Andric   Res.fill(0, Z, BitValue::Zero);
507875ed548SDimitry Andric   Res.fill(Z, W, BitValue::self());
508875ed548SDimitry Andric   return Res;
509875ed548SDimitry Andric }
510875ed548SDimitry Andric 
511875ed548SDimitry Andric 
512875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1,
513875ed548SDimitry Andric       const RegisterCell &A2) const {
514875ed548SDimitry Andric   uint16_t W = A1.width() + A2.width();
515875ed548SDimitry Andric   uint16_t Z = A1.ct(0) + A2.ct(0);
516875ed548SDimitry Andric   RegisterCell Res(W);
517875ed548SDimitry Andric   Res.fill(0, Z, BitValue::Zero);
518875ed548SDimitry Andric   Res.fill(Z, W, BitValue::self());
519875ed548SDimitry Andric   return Res;
520875ed548SDimitry Andric }
521875ed548SDimitry Andric 
522875ed548SDimitry Andric 
523875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1,
524875ed548SDimitry Andric       uint16_t Sh) const {
525875ed548SDimitry Andric   assert(Sh <= A1.width());
526875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
527875ed548SDimitry Andric   Res.rol(Sh);
528875ed548SDimitry Andric   Res.fill(0, Sh, BitValue::Zero);
529875ed548SDimitry Andric   return Res;
530875ed548SDimitry Andric }
531875ed548SDimitry Andric 
532875ed548SDimitry Andric 
533875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1,
534875ed548SDimitry Andric       uint16_t Sh) const {
535875ed548SDimitry Andric   uint16_t W = A1.width();
536875ed548SDimitry Andric   assert(Sh <= W);
537875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
538875ed548SDimitry Andric   Res.rol(W-Sh);
539875ed548SDimitry Andric   Res.fill(W-Sh, W, BitValue::Zero);
540875ed548SDimitry Andric   return Res;
541875ed548SDimitry Andric }
542875ed548SDimitry Andric 
543875ed548SDimitry Andric 
544875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1,
545875ed548SDimitry Andric       uint16_t Sh) const {
546875ed548SDimitry Andric   uint16_t W = A1.width();
547875ed548SDimitry Andric   assert(Sh <= W);
548875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
549875ed548SDimitry Andric   BitValue Sign = Res[W-1];
550875ed548SDimitry Andric   Res.rol(W-Sh);
551875ed548SDimitry Andric   Res.fill(W-Sh, W, Sign);
552875ed548SDimitry Andric   return Res;
553875ed548SDimitry Andric }
554875ed548SDimitry Andric 
555875ed548SDimitry Andric 
556875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1,
557875ed548SDimitry Andric       const RegisterCell &A2) const {
558875ed548SDimitry Andric   uint16_t W = A1.width();
559875ed548SDimitry Andric   assert(W == A2.width());
560875ed548SDimitry Andric   RegisterCell Res(W);
561875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
562875ed548SDimitry Andric     const BitValue &V1 = A1[i];
563875ed548SDimitry Andric     const BitValue &V2 = A2[i];
564875ed548SDimitry Andric     if (V1.is(1))
565875ed548SDimitry Andric       Res[i] = BitValue::ref(V2);
566875ed548SDimitry Andric     else if (V2.is(1))
567875ed548SDimitry Andric       Res[i] = BitValue::ref(V1);
568875ed548SDimitry Andric     else if (V1.is(0) || V2.is(0))
569875ed548SDimitry Andric       Res[i] = BitValue::Zero;
570875ed548SDimitry Andric     else if (V1 == V2)
571875ed548SDimitry Andric       Res[i] = V1;
572875ed548SDimitry Andric     else
573875ed548SDimitry Andric       Res[i] = BitValue::self();
574875ed548SDimitry Andric   }
575875ed548SDimitry Andric   return Res;
576875ed548SDimitry Andric }
577875ed548SDimitry Andric 
578875ed548SDimitry Andric 
579875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1,
580875ed548SDimitry Andric       const RegisterCell &A2) const {
581875ed548SDimitry Andric   uint16_t W = A1.width();
582875ed548SDimitry Andric   assert(W == A2.width());
583875ed548SDimitry Andric   RegisterCell Res(W);
584875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
585875ed548SDimitry Andric     const BitValue &V1 = A1[i];
586875ed548SDimitry Andric     const BitValue &V2 = A2[i];
587875ed548SDimitry Andric     if (V1.is(1) || V2.is(1))
588875ed548SDimitry Andric       Res[i] = BitValue::One;
589875ed548SDimitry Andric     else if (V1.is(0))
590875ed548SDimitry Andric       Res[i] = BitValue::ref(V2);
591875ed548SDimitry Andric     else if (V2.is(0))
592875ed548SDimitry Andric       Res[i] = BitValue::ref(V1);
593875ed548SDimitry Andric     else if (V1 == V2)
594875ed548SDimitry Andric       Res[i] = V1;
595875ed548SDimitry Andric     else
596875ed548SDimitry Andric       Res[i] = BitValue::self();
597875ed548SDimitry Andric   }
598875ed548SDimitry Andric   return Res;
599875ed548SDimitry Andric }
600875ed548SDimitry Andric 
601875ed548SDimitry Andric 
602875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1,
603875ed548SDimitry Andric       const RegisterCell &A2) const {
604875ed548SDimitry Andric   uint16_t W = A1.width();
605875ed548SDimitry Andric   assert(W == A2.width());
606875ed548SDimitry Andric   RegisterCell Res(W);
607875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
608875ed548SDimitry Andric     const BitValue &V1 = A1[i];
609875ed548SDimitry Andric     const BitValue &V2 = A2[i];
610875ed548SDimitry Andric     if (V1.is(0))
611875ed548SDimitry Andric       Res[i] = BitValue::ref(V2);
612875ed548SDimitry Andric     else if (V2.is(0))
613875ed548SDimitry Andric       Res[i] = BitValue::ref(V1);
614875ed548SDimitry Andric     else if (V1 == V2)
615875ed548SDimitry Andric       Res[i] = BitValue::Zero;
616875ed548SDimitry Andric     else
617875ed548SDimitry Andric       Res[i] = BitValue::self();
618875ed548SDimitry Andric   }
619875ed548SDimitry Andric   return Res;
620875ed548SDimitry Andric }
621875ed548SDimitry Andric 
622875ed548SDimitry Andric 
623875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const {
624875ed548SDimitry Andric   uint16_t W = A1.width();
625875ed548SDimitry Andric   RegisterCell Res(W);
626875ed548SDimitry Andric   for (uint16_t i = 0; i < W; ++i) {
627875ed548SDimitry Andric     const BitValue &V = A1[i];
628875ed548SDimitry Andric     if (V.is(0))
629875ed548SDimitry Andric       Res[i] = BitValue::One;
630875ed548SDimitry Andric     else if (V.is(1))
631875ed548SDimitry Andric       Res[i] = BitValue::Zero;
632875ed548SDimitry Andric     else
633875ed548SDimitry Andric       Res[i] = BitValue::self();
634875ed548SDimitry Andric   }
635875ed548SDimitry Andric   return Res;
636875ed548SDimitry Andric }
637875ed548SDimitry Andric 
638875ed548SDimitry Andric 
639875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1,
640875ed548SDimitry Andric       uint16_t BitN) const {
641875ed548SDimitry Andric   assert(BitN < A1.width());
642875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
643875ed548SDimitry Andric   Res[BitN] = BitValue::One;
644875ed548SDimitry Andric   return Res;
645875ed548SDimitry Andric }
646875ed548SDimitry Andric 
647875ed548SDimitry Andric 
648875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1,
649875ed548SDimitry Andric       uint16_t BitN) const {
650875ed548SDimitry Andric   assert(BitN < A1.width());
651875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
652875ed548SDimitry Andric   Res[BitN] = BitValue::Zero;
653875ed548SDimitry Andric   return Res;
654875ed548SDimitry Andric }
655875ed548SDimitry Andric 
656875ed548SDimitry Andric 
657875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B,
658875ed548SDimitry Andric       uint16_t W) const {
659875ed548SDimitry Andric   uint16_t C = A1.cl(B), AW = A1.width();
660875ed548SDimitry Andric   // If the last leading non-B bit is not a constant, then we don't know
661875ed548SDimitry Andric   // the real count.
662875ed548SDimitry Andric   if ((C < AW && A1[AW-1-C].num()) || C == AW)
663875ed548SDimitry Andric     return eIMM(C, W);
664875ed548SDimitry Andric   return RegisterCell::self(0, W);
665875ed548SDimitry Andric }
666875ed548SDimitry Andric 
667875ed548SDimitry Andric 
668875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B,
669875ed548SDimitry Andric       uint16_t W) const {
670875ed548SDimitry Andric   uint16_t C = A1.ct(B), AW = A1.width();
671875ed548SDimitry Andric   // If the last trailing non-B bit is not a constant, then we don't know
672875ed548SDimitry Andric   // the real count.
673875ed548SDimitry Andric   if ((C < AW && A1[C].num()) || C == AW)
674875ed548SDimitry Andric     return eIMM(C, W);
675875ed548SDimitry Andric   return RegisterCell::self(0, W);
676875ed548SDimitry Andric }
677875ed548SDimitry Andric 
678875ed548SDimitry Andric 
679875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1,
680875ed548SDimitry Andric       uint16_t FromN) const {
681875ed548SDimitry Andric   uint16_t W = A1.width();
682875ed548SDimitry Andric   assert(FromN <= W);
683875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
684875ed548SDimitry Andric   BitValue Sign = Res[FromN-1];
685875ed548SDimitry Andric   // Sign-extend "inreg".
686875ed548SDimitry Andric   Res.fill(FromN, W, Sign);
687875ed548SDimitry Andric   return Res;
688875ed548SDimitry Andric }
689875ed548SDimitry Andric 
690875ed548SDimitry Andric 
691875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1,
692875ed548SDimitry Andric       uint16_t FromN) const {
693875ed548SDimitry Andric   uint16_t W = A1.width();
694875ed548SDimitry Andric   assert(FromN <= W);
695875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
696875ed548SDimitry Andric   Res.fill(FromN, W, BitValue::Zero);
697875ed548SDimitry Andric   return Res;
698875ed548SDimitry Andric }
699875ed548SDimitry Andric 
700875ed548SDimitry Andric 
701875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1,
702875ed548SDimitry Andric       uint16_t B, uint16_t E) const {
703875ed548SDimitry Andric   uint16_t W = A1.width();
704875ed548SDimitry Andric   assert(B < W && E <= W);
705875ed548SDimitry Andric   if (B == E)
706875ed548SDimitry Andric     return RegisterCell(0);
707875ed548SDimitry Andric   uint16_t Last = (E > 0) ? E-1 : W-1;
708875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last));
709875ed548SDimitry Andric   // Return shorter cell.
710875ed548SDimitry Andric   return Res;
711875ed548SDimitry Andric }
712875ed548SDimitry Andric 
713875ed548SDimitry Andric 
714875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1,
715875ed548SDimitry Andric       const RegisterCell &A2, uint16_t AtN) const {
716875ed548SDimitry Andric   uint16_t W1 = A1.width(), W2 = A2.width();
717875ed548SDimitry Andric   (void)W1;
718875ed548SDimitry Andric   assert(AtN < W1 && AtN+W2 <= W1);
719875ed548SDimitry Andric   // Copy bits from A1, insert A2 at position AtN.
720875ed548SDimitry Andric   RegisterCell Res = RegisterCell::ref(A1);
721875ed548SDimitry Andric   if (W2 > 0)
722875ed548SDimitry Andric     Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1));
723875ed548SDimitry Andric   return Res;
724875ed548SDimitry Andric }
725875ed548SDimitry Andric 
726875ed548SDimitry Andric 
727875ed548SDimitry Andric BT::BitMask BT::MachineEvaluator::mask(unsigned Reg, unsigned Sub) const {
728875ed548SDimitry Andric   assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0");
729875ed548SDimitry Andric   uint16_t W = getRegBitWidth(Reg);
730875ed548SDimitry Andric   assert(W > 0 && "Cannot generate mask for empty register");
731875ed548SDimitry Andric   return BitMask(0, W-1);
732875ed548SDimitry Andric }
733875ed548SDimitry Andric 
734875ed548SDimitry Andric 
735875ed548SDimitry Andric bool BT::MachineEvaluator::evaluate(const MachineInstr *MI,
736875ed548SDimitry Andric       const CellMapType &Inputs, CellMapType &Outputs) const {
737875ed548SDimitry Andric   unsigned Opc = MI->getOpcode();
738875ed548SDimitry Andric   switch (Opc) {
739875ed548SDimitry Andric     case TargetOpcode::REG_SEQUENCE: {
740875ed548SDimitry Andric       RegisterRef RD = MI->getOperand(0);
741875ed548SDimitry Andric       assert(RD.Sub == 0);
742875ed548SDimitry Andric       RegisterRef RS = MI->getOperand(1);
743875ed548SDimitry Andric       unsigned SS = MI->getOperand(2).getImm();
744875ed548SDimitry Andric       RegisterRef RT = MI->getOperand(3);
745875ed548SDimitry Andric       unsigned ST = MI->getOperand(4).getImm();
746875ed548SDimitry Andric       assert(SS != ST);
747875ed548SDimitry Andric 
748875ed548SDimitry Andric       uint16_t W = getRegBitWidth(RD);
749875ed548SDimitry Andric       RegisterCell Res(W);
750875ed548SDimitry Andric       Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS));
751875ed548SDimitry Andric       Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST));
752875ed548SDimitry Andric       putCell(RD, Res, Outputs);
753875ed548SDimitry Andric       break;
754875ed548SDimitry Andric     }
755875ed548SDimitry Andric 
756875ed548SDimitry Andric     case TargetOpcode::COPY: {
757875ed548SDimitry Andric       // COPY can transfer a smaller register into a wider one.
758875ed548SDimitry Andric       // If that is the case, fill the remaining high bits with 0.
759875ed548SDimitry Andric       RegisterRef RD = MI->getOperand(0);
760875ed548SDimitry Andric       RegisterRef RS = MI->getOperand(1);
761875ed548SDimitry Andric       assert(RD.Sub == 0);
762875ed548SDimitry Andric       uint16_t WD = getRegBitWidth(RD);
763875ed548SDimitry Andric       uint16_t WS = getRegBitWidth(RS);
764875ed548SDimitry Andric       assert(WD >= WS);
765875ed548SDimitry Andric       RegisterCell Src = getCell(RS, Inputs);
766875ed548SDimitry Andric       RegisterCell Res(WD);
767875ed548SDimitry Andric       Res.insert(Src, BitMask(0, WS-1));
768875ed548SDimitry Andric       Res.fill(WS, WD, BitValue::Zero);
769875ed548SDimitry Andric       putCell(RD, Res, Outputs);
770875ed548SDimitry Andric       break;
771875ed548SDimitry Andric     }
772875ed548SDimitry Andric 
773875ed548SDimitry Andric     default:
774875ed548SDimitry Andric       return false;
775875ed548SDimitry Andric   }
776875ed548SDimitry Andric 
777875ed548SDimitry Andric   return true;
778875ed548SDimitry Andric }
779875ed548SDimitry Andric 
780875ed548SDimitry Andric 
781875ed548SDimitry Andric // Main W-Z implementation.
782875ed548SDimitry Andric 
783875ed548SDimitry Andric void BT::visitPHI(const MachineInstr *PI) {
784875ed548SDimitry Andric   int ThisN = PI->getParent()->getNumber();
785875ed548SDimitry Andric   if (Trace)
786875ed548SDimitry Andric     dbgs() << "Visit FI(BB#" << ThisN << "): " << *PI;
787875ed548SDimitry Andric 
788875ed548SDimitry Andric   const MachineOperand &MD = PI->getOperand(0);
789875ed548SDimitry Andric   assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition");
790875ed548SDimitry Andric   RegisterRef DefRR(MD);
791875ed548SDimitry Andric   uint16_t DefBW = ME.getRegBitWidth(DefRR);
792875ed548SDimitry Andric 
793875ed548SDimitry Andric   RegisterCell DefC = ME.getCell(DefRR, Map);
794875ed548SDimitry Andric   if (DefC == RegisterCell::self(DefRR.Reg, DefBW))    // XXX slow
795875ed548SDimitry Andric     return;
796875ed548SDimitry Andric 
797875ed548SDimitry Andric   bool Changed = false;
798875ed548SDimitry Andric 
799875ed548SDimitry Andric   for (unsigned i = 1, n = PI->getNumOperands(); i < n; i += 2) {
800875ed548SDimitry Andric     const MachineBasicBlock *PB = PI->getOperand(i+1).getMBB();
801875ed548SDimitry Andric     int PredN = PB->getNumber();
802875ed548SDimitry Andric     if (Trace)
803875ed548SDimitry Andric       dbgs() << "  edge BB#" << PredN << "->BB#" << ThisN;
804875ed548SDimitry Andric     if (!EdgeExec.count(CFGEdge(PredN, ThisN))) {
805875ed548SDimitry Andric       if (Trace)
806875ed548SDimitry Andric         dbgs() << " not executable\n";
807875ed548SDimitry Andric       continue;
808875ed548SDimitry Andric     }
809875ed548SDimitry Andric 
810875ed548SDimitry Andric     RegisterRef RU = PI->getOperand(i);
811875ed548SDimitry Andric     RegisterCell ResC = ME.getCell(RU, Map);
812875ed548SDimitry Andric     if (Trace)
813875ed548SDimitry Andric       dbgs() << " input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
814875ed548SDimitry Andric              << " cell: " << ResC << "\n";
815875ed548SDimitry Andric     Changed |= DefC.meet(ResC, DefRR.Reg);
816875ed548SDimitry Andric   }
817875ed548SDimitry Andric 
818875ed548SDimitry Andric   if (Changed) {
819875ed548SDimitry Andric     if (Trace)
820875ed548SDimitry Andric       dbgs() << "Output: " << PrintReg(DefRR.Reg, &ME.TRI, DefRR.Sub)
821875ed548SDimitry Andric              << " cell: " << DefC << "\n";
822875ed548SDimitry Andric     ME.putCell(DefRR, DefC, Map);
823875ed548SDimitry Andric     visitUsesOf(DefRR.Reg);
824875ed548SDimitry Andric   }
825875ed548SDimitry Andric }
826875ed548SDimitry Andric 
827875ed548SDimitry Andric 
828875ed548SDimitry Andric void BT::visitNonBranch(const MachineInstr *MI) {
829875ed548SDimitry Andric   if (Trace) {
830875ed548SDimitry Andric     int ThisN = MI->getParent()->getNumber();
831875ed548SDimitry Andric     dbgs() << "Visit MI(BB#" << ThisN << "): " << *MI;
832875ed548SDimitry Andric   }
833875ed548SDimitry Andric   if (MI->isDebugValue())
834875ed548SDimitry Andric     return;
835875ed548SDimitry Andric   assert(!MI->isBranch() && "Unexpected branch instruction");
836875ed548SDimitry Andric 
837875ed548SDimitry Andric   CellMapType ResMap;
838875ed548SDimitry Andric   bool Eval = ME.evaluate(MI, Map, ResMap);
839875ed548SDimitry Andric 
840875ed548SDimitry Andric   if (Trace && Eval) {
841875ed548SDimitry Andric     for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
842875ed548SDimitry Andric       const MachineOperand &MO = MI->getOperand(i);
843875ed548SDimitry Andric       if (!MO.isReg() || !MO.isUse())
844875ed548SDimitry Andric         continue;
845875ed548SDimitry Andric       RegisterRef RU(MO);
846875ed548SDimitry Andric       dbgs() << "  input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
847875ed548SDimitry Andric              << " cell: " << ME.getCell(RU, Map) << "\n";
848875ed548SDimitry Andric     }
849875ed548SDimitry Andric     dbgs() << "Outputs:\n";
850875ed548SDimitry Andric     for (CellMapType::iterator I = ResMap.begin(), E = ResMap.end();
851875ed548SDimitry Andric          I != E; ++I) {
852875ed548SDimitry Andric       RegisterRef RD(I->first);
853875ed548SDimitry Andric       dbgs() << "  " << PrintReg(I->first, &ME.TRI) << " cell: "
854875ed548SDimitry Andric              << ME.getCell(RD, ResMap) << "\n";
855875ed548SDimitry Andric     }
856875ed548SDimitry Andric   }
857875ed548SDimitry Andric 
858875ed548SDimitry Andric   // Iterate over all definitions of the instruction, and update the
859875ed548SDimitry Andric   // cells accordingly.
860875ed548SDimitry Andric   for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
861875ed548SDimitry Andric     const MachineOperand &MO = MI->getOperand(i);
862875ed548SDimitry Andric     // Visit register defs only.
863875ed548SDimitry Andric     if (!MO.isReg() || !MO.isDef())
864875ed548SDimitry Andric       continue;
865875ed548SDimitry Andric     RegisterRef RD(MO);
866875ed548SDimitry Andric     assert(RD.Sub == 0 && "Unexpected sub-register in definition");
867875ed548SDimitry Andric     if (!TargetRegisterInfo::isVirtualRegister(RD.Reg))
868875ed548SDimitry Andric       continue;
869875ed548SDimitry Andric 
870875ed548SDimitry Andric     bool Changed = false;
871875ed548SDimitry Andric     if (!Eval || !ResMap.has(RD.Reg)) {
872875ed548SDimitry Andric       // Set to "ref" (aka "bottom").
873875ed548SDimitry Andric       uint16_t DefBW = ME.getRegBitWidth(RD);
874875ed548SDimitry Andric       RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW);
875875ed548SDimitry Andric       if (RefC != ME.getCell(RD, Map)) {
876875ed548SDimitry Andric         ME.putCell(RD, RefC, Map);
877875ed548SDimitry Andric         Changed = true;
878875ed548SDimitry Andric       }
879875ed548SDimitry Andric     } else {
880875ed548SDimitry Andric       RegisterCell DefC = ME.getCell(RD, Map);
881875ed548SDimitry Andric       RegisterCell ResC = ME.getCell(RD, ResMap);
882875ed548SDimitry Andric       // This is a non-phi instruction, so the values of the inputs come
883875ed548SDimitry Andric       // from the same registers each time this instruction is evaluated.
884875ed548SDimitry Andric       // During the propagation, the values of the inputs can become lowered
885875ed548SDimitry Andric       // in the sense of the lattice operation, which may cause different
886875ed548SDimitry Andric       // results to be calculated in subsequent evaluations. This should
887875ed548SDimitry Andric       // not cause the bottoming of the result in the map, since the new
888875ed548SDimitry Andric       // result is already reflecting the lowered inputs.
889875ed548SDimitry Andric       for (uint16_t i = 0, w = DefC.width(); i < w; ++i) {
890875ed548SDimitry Andric         BitValue &V = DefC[i];
891875ed548SDimitry Andric         // Bits that are already "bottom" should not be updated.
892875ed548SDimitry Andric         if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg)
893875ed548SDimitry Andric           continue;
894875ed548SDimitry Andric         // Same for those that are identical in DefC and ResC.
895875ed548SDimitry Andric         if (V == ResC[i])
896875ed548SDimitry Andric           continue;
897875ed548SDimitry Andric         V = ResC[i];
898875ed548SDimitry Andric         Changed = true;
899875ed548SDimitry Andric       }
900875ed548SDimitry Andric       if (Changed)
901875ed548SDimitry Andric         ME.putCell(RD, DefC, Map);
902875ed548SDimitry Andric     }
903875ed548SDimitry Andric     if (Changed)
904875ed548SDimitry Andric       visitUsesOf(RD.Reg);
905875ed548SDimitry Andric   }
906875ed548SDimitry Andric }
907875ed548SDimitry Andric 
908875ed548SDimitry Andric 
909875ed548SDimitry Andric void BT::visitBranchesFrom(const MachineInstr *BI) {
910875ed548SDimitry 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();
918875ed548SDimitry Andric     const MachineInstr *MI = &*It;
919875ed548SDimitry Andric     if (Trace)
920875ed548SDimitry Andric       dbgs() << "Visit BR(BB#" << ThisN << "): " << *MI;
921875ed548SDimitry Andric     assert(MI->isBranch() && "Expecting branch instruction");
922875ed548SDimitry 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;
954875ed548SDimitry Andric       if (SB->isLandingPad())
955875ed548SDimitry Andric         Targets.insert(SB);
956875ed548SDimitry Andric     }
957875ed548SDimitry Andric     if (FallsThrough) {
958875ed548SDimitry Andric       MachineFunction::const_iterator BIt = &B;
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())
986875ed548SDimitry Andric       visitPHI(UseI);
987875ed548SDimitry Andric     else if (!UseI->isBranch())
988875ed548SDimitry Andric       visitNonBranch(UseI);
989875ed548SDimitry Andric     else
990875ed548SDimitry 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) {
1008875ed548SDimitry Andric   assert(Map.has(OldRR.Reg) && "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()) {
1087875ed548SDimitry Andric       const MachineInstr *PI = &*It++;
1088875ed548SDimitry 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()) {
1101875ed548SDimitry Andric       const MachineInstr *MI = &*It++;
1102875ed548SDimitry 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) {
1107875ed548SDimitry Andric       MachineFunction::const_iterator BIt = &B;
1108875ed548SDimitry Andric       MachineFunction::const_iterator Next = std::next(BIt);
1109875ed548SDimitry Andric       if (Next != MF.end()) {
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.
1117875ed548SDimitry 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