1e4fc6ee7SEugene Zelenko //===- BitTracker.cpp -----------------------------------------------------===//
2e53b31a5SKrzysztof Parzyszek //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e53b31a5SKrzysztof Parzyszek //
7e53b31a5SKrzysztof Parzyszek //===----------------------------------------------------------------------===//
8e53b31a5SKrzysztof Parzyszek 
9e53b31a5SKrzysztof Parzyszek // SSA-based bit propagation.
10e53b31a5SKrzysztof Parzyszek //
11e53b31a5SKrzysztof Parzyszek // The purpose of this code is, for a given virtual register, to provide
12e53b31a5SKrzysztof Parzyszek // information about the value of each bit in the register. The values
13e53b31a5SKrzysztof Parzyszek // of bits are represented by the class BitValue, and take one of four
14e53b31a5SKrzysztof Parzyszek // cases: 0, 1, "ref" and "bottom". The 0 and 1 are rather clear, the
15e53b31a5SKrzysztof Parzyszek // "ref" value means that the bit is a copy of another bit (which itself
16e53b31a5SKrzysztof Parzyszek // cannot be a copy of yet another bit---such chains are not allowed).
17e53b31a5SKrzysztof Parzyszek // A "ref" value is associated with a BitRef structure, which indicates
18e53b31a5SKrzysztof Parzyszek // which virtual register, and which bit in that register is the origin
19e53b31a5SKrzysztof Parzyszek // of the value. For example, given an instruction
2093ef1458SFrancis Visoiu Mistrih //   %2 = ASL %1, 1
2193ef1458SFrancis Visoiu Mistrih // assuming that nothing is known about bits of %1, bit 1 of %2
2293ef1458SFrancis Visoiu Mistrih // will be a "ref" to (%1, 0). If there is a subsequent instruction
2393ef1458SFrancis Visoiu Mistrih //   %3 = ASL %2, 2
2493ef1458SFrancis Visoiu Mistrih // then bit 3 of %3 will be a "ref" to (%1, 0) as well.
25e53b31a5SKrzysztof Parzyszek // The "bottom" case means that the bit's value cannot be determined,
26e53b31a5SKrzysztof Parzyszek // and that this virtual register actually defines it. The "bottom" case
27e53b31a5SKrzysztof Parzyszek // is discussed in detail in BitTracker.h. In fact, "bottom" is a "ref
2893ef1458SFrancis Visoiu Mistrih // to self", so for the %1 above, the bit 0 of it will be a "ref" to
2993ef1458SFrancis Visoiu Mistrih // (%1, 0), bit 1 will be a "ref" to (%1, 1), etc.
30e53b31a5SKrzysztof Parzyszek //
31e53b31a5SKrzysztof Parzyszek // The tracker implements the Wegman-Zadeck algorithm, originally developed
32e53b31a5SKrzysztof Parzyszek // for SSA-based constant propagation. Each register is represented as
33e53b31a5SKrzysztof Parzyszek // a sequence of bits, with the convention that bit 0 is the least signi-
34e53b31a5SKrzysztof Parzyszek // ficant bit. Each bit is propagated individually. The class RegisterCell
35e53b31a5SKrzysztof Parzyszek // implements the register's representation, and is also the subject of
36e53b31a5SKrzysztof Parzyszek // the lattice operations in the tracker.
37e53b31a5SKrzysztof Parzyszek //
38e53b31a5SKrzysztof Parzyszek // The intended usage of the bit tracker is to create a target-specific
39e53b31a5SKrzysztof Parzyszek // machine instruction evaluator, pass the evaluator to the BitTracker
40e53b31a5SKrzysztof Parzyszek // object, and run the tracker. The tracker will then collect the bit
41e53b31a5SKrzysztof Parzyszek // value information for a given machine function. After that, it can be
42e53b31a5SKrzysztof Parzyszek // queried for the cells for each virtual register.
43e53b31a5SKrzysztof Parzyszek // Sample code:
44e53b31a5SKrzysztof Parzyszek //   const TargetSpecificEvaluator TSE(TRI, MRI);
45e53b31a5SKrzysztof Parzyszek //   BitTracker BT(TSE, MF);
46e53b31a5SKrzysztof Parzyszek //   BT.run();
47e53b31a5SKrzysztof Parzyszek //   ...
48e53b31a5SKrzysztof Parzyszek //   unsigned Reg = interestingRegister();
49e53b31a5SKrzysztof Parzyszek //   RegisterCell RC = BT.get(Reg);
50e53b31a5SKrzysztof Parzyszek //   if (RC[3].is(1))
51e53b31a5SKrzysztof Parzyszek //      Reg0bit3 = 1;
52e53b31a5SKrzysztof Parzyszek //
53e53b31a5SKrzysztof Parzyszek // The code below is intended to be fully target-independent.
54e53b31a5SKrzysztof Parzyszek 
55b2ca1b3fSEugene Zelenko #include "BitTracker.h"
56b2ca1b3fSEugene Zelenko #include "llvm/ADT/APInt.h"
57b2ca1b3fSEugene Zelenko #include "llvm/ADT/BitVector.h"
58e53b31a5SKrzysztof Parzyszek #include "llvm/CodeGen/MachineBasicBlock.h"
59e53b31a5SKrzysztof Parzyszek #include "llvm/CodeGen/MachineFunction.h"
60e53b31a5SKrzysztof Parzyszek #include "llvm/CodeGen/MachineInstr.h"
61b2ca1b3fSEugene Zelenko #include "llvm/CodeGen/MachineOperand.h"
62e53b31a5SKrzysztof Parzyszek #include "llvm/CodeGen/MachineRegisterInfo.h"
63b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetRegisterInfo.h"
64e53b31a5SKrzysztof Parzyszek #include "llvm/IR/Constants.h"
65e53b31a5SKrzysztof Parzyszek #include "llvm/Support/Debug.h"
66e53b31a5SKrzysztof Parzyszek #include "llvm/Support/raw_ostream.h"
67b2ca1b3fSEugene Zelenko #include <cassert>
68b2ca1b3fSEugene Zelenko #include <cstdint>
696bda14b3SChandler Carruth #include <iterator>
70e53b31a5SKrzysztof Parzyszek 
71e53b31a5SKrzysztof Parzyszek using namespace llvm;
72e53b31a5SKrzysztof Parzyszek 
73e4fc6ee7SEugene Zelenko using BT = BitTracker;
74e53b31a5SKrzysztof Parzyszek 
75e53b31a5SKrzysztof Parzyszek namespace {
76b2ca1b3fSEugene Zelenko 
7793ef1458SFrancis Visoiu Mistrih   // Local trickery to pretty print a register (without the whole "%number"
78e53b31a5SKrzysztof Parzyszek   // business).
79e53b31a5SKrzysztof Parzyszek   struct printv {
printv__anon6fde7a9a0111::printv80e53b31a5SKrzysztof Parzyszek     printv(unsigned r) : R(r) {}
81b2ca1b3fSEugene Zelenko 
82e53b31a5SKrzysztof Parzyszek     unsigned R;
83e53b31a5SKrzysztof Parzyszek   };
84b2ca1b3fSEugene Zelenko 
operator <<(raw_ostream & OS,const printv & PV)85e53b31a5SKrzysztof Parzyszek   raw_ostream &operator<< (raw_ostream &OS, const printv &PV) {
86e53b31a5SKrzysztof Parzyszek     if (PV.R)
872bea69bfSDaniel Sanders       OS << 'v' << Register::virtReg2Index(PV.R);
88e53b31a5SKrzysztof Parzyszek     else
89e53b31a5SKrzysztof Parzyszek       OS << 's';
90e53b31a5SKrzysztof Parzyszek     return OS;
91e53b31a5SKrzysztof Parzyszek   }
92b2ca1b3fSEugene Zelenko 
93b2ca1b3fSEugene Zelenko } // end anonymous namespace
94e53b31a5SKrzysztof Parzyszek 
95754bad88SKrzysztof Parzyszek namespace llvm {
96b2ca1b3fSEugene Zelenko 
operator <<(raw_ostream & OS,const BT::BitValue & BV)97754bad88SKrzysztof Parzyszek   raw_ostream &operator<<(raw_ostream &OS, const BT::BitValue &BV) {
98e53b31a5SKrzysztof Parzyszek     switch (BV.Type) {
99e53b31a5SKrzysztof Parzyszek       case BT::BitValue::Top:
100e53b31a5SKrzysztof Parzyszek         OS << 'T';
101e53b31a5SKrzysztof Parzyszek         break;
102e53b31a5SKrzysztof Parzyszek       case BT::BitValue::Zero:
103e53b31a5SKrzysztof Parzyszek         OS << '0';
104e53b31a5SKrzysztof Parzyszek         break;
105e53b31a5SKrzysztof Parzyszek       case BT::BitValue::One:
106e53b31a5SKrzysztof Parzyszek         OS << '1';
107e53b31a5SKrzysztof Parzyszek         break;
108e53b31a5SKrzysztof Parzyszek       case BT::BitValue::Ref:
109e53b31a5SKrzysztof Parzyszek         OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']';
110e53b31a5SKrzysztof Parzyszek         break;
111e53b31a5SKrzysztof Parzyszek     }
112e53b31a5SKrzysztof Parzyszek     return OS;
113e53b31a5SKrzysztof Parzyszek   }
114e53b31a5SKrzysztof Parzyszek 
operator <<(raw_ostream & OS,const BT::RegisterCell & RC)115754bad88SKrzysztof Parzyszek   raw_ostream &operator<<(raw_ostream &OS, const BT::RegisterCell &RC) {
116e53b31a5SKrzysztof Parzyszek     unsigned n = RC.Bits.size();
117e53b31a5SKrzysztof Parzyszek     OS << "{ w:" << n;
118e53b31a5SKrzysztof Parzyszek     // Instead of printing each bit value individually, try to group them
119e53b31a5SKrzysztof Parzyszek     // into logical segments, such as sequences of 0 or 1 bits or references
120e53b31a5SKrzysztof Parzyszek     // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz").
121e53b31a5SKrzysztof Parzyszek     // "Start" will be the index of the beginning of the most recent segment.
122e53b31a5SKrzysztof Parzyszek     unsigned Start = 0;
123e53b31a5SKrzysztof Parzyszek     bool SeqRef = false;    // A sequence of refs to consecutive bits.
124e53b31a5SKrzysztof Parzyszek     bool ConstRef = false;  // A sequence of refs to the same bit.
125e53b31a5SKrzysztof Parzyszek 
126e53b31a5SKrzysztof Parzyszek     for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) {
127e53b31a5SKrzysztof Parzyszek       const BT::BitValue &V = RC[i];
128e53b31a5SKrzysztof Parzyszek       const BT::BitValue &SV = RC[Start];
129e53b31a5SKrzysztof Parzyszek       bool IsRef = (V.Type == BT::BitValue::Ref);
130e53b31a5SKrzysztof Parzyszek       // If the current value is the same as Start, skip to the next one.
131e53b31a5SKrzysztof Parzyszek       if (!IsRef && V == SV)
132e53b31a5SKrzysztof Parzyszek         continue;
133e53b31a5SKrzysztof Parzyszek       if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) {
134e53b31a5SKrzysztof Parzyszek         if (Start+1 == i) {
135e53b31a5SKrzysztof Parzyszek           SeqRef = (V.RefI.Pos == SV.RefI.Pos+1);
136e53b31a5SKrzysztof Parzyszek           ConstRef = (V.RefI.Pos == SV.RefI.Pos);
137e53b31a5SKrzysztof Parzyszek         }
138e53b31a5SKrzysztof Parzyszek         if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start))
139e53b31a5SKrzysztof Parzyszek           continue;
140e53b31a5SKrzysztof Parzyszek         if (ConstRef && V.RefI.Pos == SV.RefI.Pos)
141e53b31a5SKrzysztof Parzyszek           continue;
142e53b31a5SKrzysztof Parzyszek       }
143e53b31a5SKrzysztof Parzyszek 
144e53b31a5SKrzysztof Parzyszek       // The current value is different. Print the previous one and reset
145e53b31a5SKrzysztof Parzyszek       // the Start.
146e53b31a5SKrzysztof Parzyszek       OS << " [" << Start;
147e53b31a5SKrzysztof Parzyszek       unsigned Count = i - Start;
148e53b31a5SKrzysztof Parzyszek       if (Count == 1) {
149e53b31a5SKrzysztof Parzyszek         OS << "]:" << SV;
150e53b31a5SKrzysztof Parzyszek       } else {
151e53b31a5SKrzysztof Parzyszek         OS << '-' << i-1 << "]:";
152e53b31a5SKrzysztof Parzyszek         if (SV.Type == BT::BitValue::Ref && SeqRef)
153e53b31a5SKrzysztof Parzyszek           OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
154e53b31a5SKrzysztof Parzyszek              << SV.RefI.Pos+(Count-1) << ']';
155e53b31a5SKrzysztof Parzyszek         else
156e53b31a5SKrzysztof Parzyszek           OS << SV;
157e53b31a5SKrzysztof Parzyszek       }
158e53b31a5SKrzysztof Parzyszek       Start = i;
159e53b31a5SKrzysztof Parzyszek       SeqRef = ConstRef = false;
160e53b31a5SKrzysztof Parzyszek     }
161e53b31a5SKrzysztof Parzyszek 
162e53b31a5SKrzysztof Parzyszek     OS << " [" << Start;
163e53b31a5SKrzysztof Parzyszek     unsigned Count = n - Start;
164e53b31a5SKrzysztof Parzyszek     if (n-Start == 1) {
165e53b31a5SKrzysztof Parzyszek       OS << "]:" << RC[Start];
166e53b31a5SKrzysztof Parzyszek     } else {
167e53b31a5SKrzysztof Parzyszek       OS << '-' << n-1 << "]:";
168e53b31a5SKrzysztof Parzyszek       const BT::BitValue &SV = RC[Start];
169e53b31a5SKrzysztof Parzyszek       if (SV.Type == BT::BitValue::Ref && SeqRef)
170e53b31a5SKrzysztof Parzyszek         OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
171e53b31a5SKrzysztof Parzyszek            << SV.RefI.Pos+(Count-1) << ']';
172e53b31a5SKrzysztof Parzyszek       else
173e53b31a5SKrzysztof Parzyszek         OS << SV;
174e53b31a5SKrzysztof Parzyszek     }
175e53b31a5SKrzysztof Parzyszek     OS << " }";
176e53b31a5SKrzysztof Parzyszek 
177e53b31a5SKrzysztof Parzyszek     return OS;
178e53b31a5SKrzysztof Parzyszek   }
179b2ca1b3fSEugene Zelenko 
180b2ca1b3fSEugene Zelenko } // end namespace llvm
181e53b31a5SKrzysztof Parzyszek 
print_cells(raw_ostream & OS) const182623afbdbSKrzysztof Parzyszek void BitTracker::print_cells(raw_ostream &OS) const {
18302893de4SKrzysztof Parzyszek   for (const std::pair<unsigned, RegisterCell> P : Map)
1849d419d3bSFrancis Visoiu Mistrih     dbgs() << printReg(P.first, &ME.TRI) << " -> " << P.second << "\n";
185623afbdbSKrzysztof Parzyszek }
186623afbdbSKrzysztof Parzyszek 
BitTracker(const MachineEvaluator & E,MachineFunction & F)187d8861517SBenjamin Kramer BitTracker::BitTracker(const MachineEvaluator &E, MachineFunction &F)
188058d3cecSKrzysztof Parzyszek     : ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType), Trace(false) {
189058d3cecSKrzysztof Parzyszek }
190e53b31a5SKrzysztof Parzyszek 
~BitTracker()191e53b31a5SKrzysztof Parzyszek BitTracker::~BitTracker() {
192e53b31a5SKrzysztof Parzyszek   delete &Map;
193e53b31a5SKrzysztof Parzyszek }
194e53b31a5SKrzysztof Parzyszek 
195e53b31a5SKrzysztof Parzyszek // If we were allowed to update a cell for a part of a register, the meet
196e53b31a5SKrzysztof Parzyszek // operation would need to be parametrized by the register number and the
197e53b31a5SKrzysztof Parzyszek // exact part of the register, so that the computer BitRefs correspond to
198e53b31a5SKrzysztof Parzyszek // the actual bits of the "self" register.
199e53b31a5SKrzysztof Parzyszek // While this cannot happen in the current implementation, I'm not sure
200e53b31a5SKrzysztof Parzyszek // if this should be ruled out in the future.
meet(const RegisterCell & RC,Register SelfR)20106fcc4f0SGaurav Jain bool BT::RegisterCell::meet(const RegisterCell &RC, Register SelfR) {
202e53b31a5SKrzysztof Parzyszek   // An example when "meet" can be invoked with SelfR == 0 is a phi node
203e53b31a5SKrzysztof Parzyszek   // with a physical register as an operand.
20406fcc4f0SGaurav Jain   assert(SelfR == 0 || SelfR.isVirtual());
205e53b31a5SKrzysztof Parzyszek   bool Changed = false;
206e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0, n = Bits.size(); i < n; ++i) {
207e53b31a5SKrzysztof Parzyszek     const BitValue &RCV = RC[i];
208e53b31a5SKrzysztof Parzyszek     Changed |= Bits[i].meet(RCV, BitRef(SelfR, i));
209e53b31a5SKrzysztof Parzyszek   }
210e53b31a5SKrzysztof Parzyszek   return Changed;
211e53b31a5SKrzysztof Parzyszek }
212e53b31a5SKrzysztof Parzyszek 
213e53b31a5SKrzysztof Parzyszek // Insert the entire cell RC into the current cell at position given by M.
insert(const BT::RegisterCell & RC,const BitMask & M)214e53b31a5SKrzysztof Parzyszek BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC,
215e53b31a5SKrzysztof Parzyszek       const BitMask &M) {
216e53b31a5SKrzysztof Parzyszek   uint16_t B = M.first(), E = M.last(), W = width();
217dc9b5550SZarko Todorovski   // M must be a valid mask for *this.
218e53b31a5SKrzysztof Parzyszek   assert(B < W && E < W);
219dc9b5550SZarko Todorovski   // The masked part of *this must have the same number of bits
220e53b31a5SKrzysztof Parzyszek   // as the source.
221e53b31a5SKrzysztof Parzyszek   assert(B > E || E-B+1 == RC.width());      // B <= E  =>  E-B+1 = |RC|.
222e53b31a5SKrzysztof Parzyszek   assert(B <= E || E+(W-B)+1 == RC.width()); // E < B   =>  E+(W-B)+1 = |RC|.
223e53b31a5SKrzysztof Parzyszek   if (B <= E) {
224e53b31a5SKrzysztof Parzyszek     for (uint16_t i = 0; i <= E-B; ++i)
225e53b31a5SKrzysztof Parzyszek       Bits[i+B] = RC[i];
226e53b31a5SKrzysztof Parzyszek   } else {
227e53b31a5SKrzysztof Parzyszek     for (uint16_t i = 0; i < W-B; ++i)
228e53b31a5SKrzysztof Parzyszek       Bits[i+B] = RC[i];
229e53b31a5SKrzysztof Parzyszek     for (uint16_t i = 0; i <= E; ++i)
230e53b31a5SKrzysztof Parzyszek       Bits[i] = RC[i+(W-B)];
231e53b31a5SKrzysztof Parzyszek   }
232e53b31a5SKrzysztof Parzyszek   return *this;
233e53b31a5SKrzysztof Parzyszek }
234e53b31a5SKrzysztof Parzyszek 
extract(const BitMask & M) const235e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const {
236e53b31a5SKrzysztof Parzyszek   uint16_t B = M.first(), E = M.last(), W = width();
237e53b31a5SKrzysztof Parzyszek   assert(B < W && E < W);
238e53b31a5SKrzysztof Parzyszek   if (B <= E) {
239e53b31a5SKrzysztof Parzyszek     RegisterCell RC(E-B+1);
240e53b31a5SKrzysztof Parzyszek     for (uint16_t i = B; i <= E; ++i)
241e53b31a5SKrzysztof Parzyszek       RC.Bits[i-B] = Bits[i];
242e53b31a5SKrzysztof Parzyszek     return RC;
243e53b31a5SKrzysztof Parzyszek   }
244e53b31a5SKrzysztof Parzyszek 
245e53b31a5SKrzysztof Parzyszek   RegisterCell RC(E+(W-B)+1);
246e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W-B; ++i)
247e53b31a5SKrzysztof Parzyszek     RC.Bits[i] = Bits[i+B];
248e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i <= E; ++i)
249e53b31a5SKrzysztof Parzyszek     RC.Bits[i+(W-B)] = Bits[i];
250e53b31a5SKrzysztof Parzyszek   return RC;
251e53b31a5SKrzysztof Parzyszek }
252e53b31a5SKrzysztof Parzyszek 
rol(uint16_t Sh)253e53b31a5SKrzysztof Parzyszek BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) {
254e53b31a5SKrzysztof Parzyszek   // Rotate left (i.e. towards increasing bit indices).
255e53b31a5SKrzysztof Parzyszek   // Swap the two parts:  [0..W-Sh-1] [W-Sh..W-1]
256e53b31a5SKrzysztof Parzyszek   uint16_t W = width();
257e53b31a5SKrzysztof Parzyszek   Sh = Sh % W;
258e53b31a5SKrzysztof Parzyszek   if (Sh == 0)
259e53b31a5SKrzysztof Parzyszek     return *this;
260e53b31a5SKrzysztof Parzyszek 
261e53b31a5SKrzysztof Parzyszek   RegisterCell Tmp(W-Sh);
262e53b31a5SKrzysztof Parzyszek   // Tmp = [0..W-Sh-1].
263e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W-Sh; ++i)
264e53b31a5SKrzysztof Parzyszek     Tmp[i] = Bits[i];
265e53b31a5SKrzysztof Parzyszek   // Shift [W-Sh..W-1] to [0..Sh-1].
266e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < Sh; ++i)
267e53b31a5SKrzysztof Parzyszek     Bits[i] = Bits[W-Sh+i];
268e53b31a5SKrzysztof Parzyszek   // Copy Tmp to [Sh..W-1].
269e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W-Sh; ++i)
270e53b31a5SKrzysztof Parzyszek     Bits[i+Sh] = Tmp.Bits[i];
271e53b31a5SKrzysztof Parzyszek   return *this;
272e53b31a5SKrzysztof Parzyszek }
273e53b31a5SKrzysztof Parzyszek 
fill(uint16_t B,uint16_t E,const BitValue & V)274e53b31a5SKrzysztof Parzyszek BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E,
275e53b31a5SKrzysztof Parzyszek       const BitValue &V) {
276e53b31a5SKrzysztof Parzyszek   assert(B <= E);
277e53b31a5SKrzysztof Parzyszek   while (B < E)
278e53b31a5SKrzysztof Parzyszek     Bits[B++] = V;
279e53b31a5SKrzysztof Parzyszek   return *this;
280e53b31a5SKrzysztof Parzyszek }
281e53b31a5SKrzysztof Parzyszek 
cat(const RegisterCell & RC)282e53b31a5SKrzysztof Parzyszek BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) {
283e53b31a5SKrzysztof Parzyszek   // Append the cell given as the argument to the "this" cell.
284e53b31a5SKrzysztof Parzyszek   // Bit 0 of RC becomes bit W of the result, where W is this->width().
285e53b31a5SKrzysztof Parzyszek   uint16_t W = width(), WRC = RC.width();
286e53b31a5SKrzysztof Parzyszek   Bits.resize(W+WRC);
287e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < WRC; ++i)
288e53b31a5SKrzysztof Parzyszek     Bits[i+W] = RC.Bits[i];
289e53b31a5SKrzysztof Parzyszek   return *this;
290e53b31a5SKrzysztof Parzyszek }
291e53b31a5SKrzysztof Parzyszek 
ct(bool B) const292e53b31a5SKrzysztof Parzyszek uint16_t BT::RegisterCell::ct(bool B) const {
293e53b31a5SKrzysztof Parzyszek   uint16_t W = width();
294e53b31a5SKrzysztof Parzyszek   uint16_t C = 0;
295e53b31a5SKrzysztof Parzyszek   BitValue V = B;
296e53b31a5SKrzysztof Parzyszek   while (C < W && Bits[C] == V)
297e53b31a5SKrzysztof Parzyszek     C++;
298e53b31a5SKrzysztof Parzyszek   return C;
299e53b31a5SKrzysztof Parzyszek }
300e53b31a5SKrzysztof Parzyszek 
cl(bool B) const301e53b31a5SKrzysztof Parzyszek uint16_t BT::RegisterCell::cl(bool B) const {
302e53b31a5SKrzysztof Parzyszek   uint16_t W = width();
303e53b31a5SKrzysztof Parzyszek   uint16_t C = 0;
304e53b31a5SKrzysztof Parzyszek   BitValue V = B;
305e53b31a5SKrzysztof Parzyszek   while (C < W && Bits[W-(C+1)] == V)
306e53b31a5SKrzysztof Parzyszek     C++;
307e53b31a5SKrzysztof Parzyszek   return C;
308e53b31a5SKrzysztof Parzyszek }
309e53b31a5SKrzysztof Parzyszek 
operator ==(const RegisterCell & RC) const310e53b31a5SKrzysztof Parzyszek bool BT::RegisterCell::operator== (const RegisterCell &RC) const {
311e53b31a5SKrzysztof Parzyszek   uint16_t W = Bits.size();
312e53b31a5SKrzysztof Parzyszek   if (RC.Bits.size() != W)
313e53b31a5SKrzysztof Parzyszek     return false;
314e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i)
315e53b31a5SKrzysztof Parzyszek     if (Bits[i] != RC[i])
316e53b31a5SKrzysztof Parzyszek       return false;
317e53b31a5SKrzysztof Parzyszek   return true;
318e53b31a5SKrzysztof Parzyszek }
319e53b31a5SKrzysztof Parzyszek 
regify(unsigned R)320998e49e5SKrzysztof Parzyszek BT::RegisterCell &BT::RegisterCell::regify(unsigned R) {
321998e49e5SKrzysztof Parzyszek   for (unsigned i = 0, n = width(); i < n; ++i) {
322998e49e5SKrzysztof Parzyszek     const BitValue &V = Bits[i];
323998e49e5SKrzysztof Parzyszek     if (V.Type == BitValue::Ref && V.RefI.Reg == 0)
324998e49e5SKrzysztof Parzyszek       Bits[i].RefI = BitRef(R, i);
325998e49e5SKrzysztof Parzyszek   }
326998e49e5SKrzysztof Parzyszek   return *this;
327998e49e5SKrzysztof Parzyszek }
328998e49e5SKrzysztof Parzyszek 
getRegBitWidth(const RegisterRef & RR) const329e53b31a5SKrzysztof Parzyszek uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const {
330e53b31a5SKrzysztof Parzyszek   // The general problem is with finding a register class that corresponds
331e53b31a5SKrzysztof Parzyszek   // to a given reference reg:sub. There can be several such classes, and
332e53b31a5SKrzysztof Parzyszek   // since we only care about the register size, it does not matter which
333e53b31a5SKrzysztof Parzyszek   // such class we would find.
334e53b31a5SKrzysztof Parzyszek   // The easiest way to accomplish what we want is to
335e53b31a5SKrzysztof Parzyszek   // 1. find a physical register PhysR from the same class as RR.Reg,
336e53b31a5SKrzysztof Parzyszek   // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub,
337e53b31a5SKrzysztof Parzyszek   // 3. find a register class that contains PhysS.
33806fcc4f0SGaurav Jain   if (RR.Reg.isVirtual()) {
3397e604decSKrzysztof Parzyszek     const auto &VC = composeWithSubRegIndex(*MRI.getRegClass(RR.Reg), RR.Sub);
3407e604decSKrzysztof Parzyszek     return TRI.getRegSizeInBits(VC);
341e53b31a5SKrzysztof Parzyszek   }
34206fcc4f0SGaurav Jain   assert(RR.Reg.isPhysical());
343bab72dd5SMircea Trofin   MCRegister PhysR =
344bab72dd5SMircea Trofin       (RR.Sub == 0) ? RR.Reg.asMCReg() : TRI.getSubReg(RR.Reg, RR.Sub);
3457e604decSKrzysztof Parzyszek   return getPhysRegBitWidth(PhysR);
346e53b31a5SKrzysztof Parzyszek }
347e53b31a5SKrzysztof Parzyszek 
getCell(const RegisterRef & RR,const CellMapType & M) const348e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR,
349e53b31a5SKrzysztof Parzyszek       const CellMapType &M) const {
350e53b31a5SKrzysztof Parzyszek   uint16_t BW = getRegBitWidth(RR);
351e53b31a5SKrzysztof Parzyszek 
352e53b31a5SKrzysztof Parzyszek   // Physical registers are assumed to be present in the map with an unknown
353e53b31a5SKrzysztof Parzyszek   // value. Don't actually insert anything in the map, just return the cell.
35406fcc4f0SGaurav Jain   if (RR.Reg.isPhysical())
355e53b31a5SKrzysztof Parzyszek     return RegisterCell::self(0, BW);
356e53b31a5SKrzysztof Parzyszek 
35706fcc4f0SGaurav Jain   assert(RR.Reg.isVirtual());
358e53b31a5SKrzysztof Parzyszek   // For virtual registers that belong to a class that is not tracked,
359e53b31a5SKrzysztof Parzyszek   // generate an "unknown" value as well.
360e53b31a5SKrzysztof Parzyszek   const TargetRegisterClass *C = MRI.getRegClass(RR.Reg);
361e53b31a5SKrzysztof Parzyszek   if (!track(C))
362e53b31a5SKrzysztof Parzyszek     return RegisterCell::self(0, BW);
363e53b31a5SKrzysztof Parzyszek 
364e53b31a5SKrzysztof Parzyszek   CellMapType::const_iterator F = M.find(RR.Reg);
365e53b31a5SKrzysztof Parzyszek   if (F != M.end()) {
366e53b31a5SKrzysztof Parzyszek     if (!RR.Sub)
367e53b31a5SKrzysztof Parzyszek       return F->second;
368e53b31a5SKrzysztof Parzyszek     BitMask M = mask(RR.Reg, RR.Sub);
369e53b31a5SKrzysztof Parzyszek     return F->second.extract(M);
370e53b31a5SKrzysztof Parzyszek   }
371e53b31a5SKrzysztof Parzyszek   // If not found, create a "top" entry, but do not insert it in the map.
372e53b31a5SKrzysztof Parzyszek   return RegisterCell::top(BW);
373e53b31a5SKrzysztof Parzyszek }
374e53b31a5SKrzysztof Parzyszek 
putCell(const RegisterRef & RR,RegisterCell RC,CellMapType & M) const375e53b31a5SKrzysztof Parzyszek void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC,
376e53b31a5SKrzysztof Parzyszek       CellMapType &M) const {
377e53b31a5SKrzysztof Parzyszek   // While updating the cell map can be done in a meaningful way for
378e53b31a5SKrzysztof Parzyszek   // a part of a register, it makes little sense to implement it as the
379e53b31a5SKrzysztof Parzyszek   // SSA representation would never contain such "partial definitions".
38006fcc4f0SGaurav Jain   if (!RR.Reg.isVirtual())
381e53b31a5SKrzysztof Parzyszek     return;
382e53b31a5SKrzysztof Parzyszek   assert(RR.Sub == 0 && "Unexpected sub-register in definition");
383e53b31a5SKrzysztof Parzyszek   // Eliminate all ref-to-reg-0 bit values: replace them with "self".
384998e49e5SKrzysztof Parzyszek   M[RR.Reg] = RC.regify(RR.Reg);
385e53b31a5SKrzysztof Parzyszek }
386e53b31a5SKrzysztof Parzyszek 
387e53b31a5SKrzysztof Parzyszek // Check if the cell represents a compile-time integer value.
isInt(const RegisterCell & A) const388e53b31a5SKrzysztof Parzyszek bool BT::MachineEvaluator::isInt(const RegisterCell &A) const {
389e53b31a5SKrzysztof Parzyszek   uint16_t W = A.width();
390e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i)
391e53b31a5SKrzysztof Parzyszek     if (!A[i].is(0) && !A[i].is(1))
392e53b31a5SKrzysztof Parzyszek       return false;
393e53b31a5SKrzysztof Parzyszek   return true;
394e53b31a5SKrzysztof Parzyszek }
395e53b31a5SKrzysztof Parzyszek 
396e53b31a5SKrzysztof Parzyszek // Convert a cell to the integer value. The result must fit in uint64_t.
toInt(const RegisterCell & A) const397e53b31a5SKrzysztof Parzyszek uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const {
398e53b31a5SKrzysztof Parzyszek   assert(isInt(A));
399e53b31a5SKrzysztof Parzyszek   uint64_t Val = 0;
400e53b31a5SKrzysztof Parzyszek   uint16_t W = A.width();
401e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i) {
402e53b31a5SKrzysztof Parzyszek     Val <<= 1;
403e53b31a5SKrzysztof Parzyszek     Val |= A[i].is(1);
404e53b31a5SKrzysztof Parzyszek   }
405e53b31a5SKrzysztof Parzyszek   return Val;
406e53b31a5SKrzysztof Parzyszek }
407e53b31a5SKrzysztof Parzyszek 
408e53b31a5SKrzysztof Parzyszek // Evaluator helper functions. These implement some common operation on
409e53b31a5SKrzysztof Parzyszek // register cells that can be used to implement target-specific instructions
410e53b31a5SKrzysztof Parzyszek // in a target-specific evaluator.
411e53b31a5SKrzysztof Parzyszek 
eIMM(int64_t V,uint16_t W) const412e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const {
413e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
414e53b31a5SKrzysztof Parzyszek   // For bits beyond the 63rd, this will generate the sign bit of V.
415e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i) {
416e53b31a5SKrzysztof Parzyszek     Res[i] = BitValue(V & 1);
417e53b31a5SKrzysztof Parzyszek     V >>= 1;
418e53b31a5SKrzysztof Parzyszek   }
419e53b31a5SKrzysztof Parzyszek   return Res;
420e53b31a5SKrzysztof Parzyszek }
421e53b31a5SKrzysztof Parzyszek 
eIMM(const ConstantInt * CI) const422e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const {
42346e38f36SBenjamin Kramer   const APInt &A = CI->getValue();
424e53b31a5SKrzysztof Parzyszek   uint16_t BW = A.getBitWidth();
425e53b31a5SKrzysztof Parzyszek   assert((unsigned)BW == A.getBitWidth() && "BitWidth overflow");
426e53b31a5SKrzysztof Parzyszek   RegisterCell Res(BW);
427e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < BW; ++i)
428e53b31a5SKrzysztof Parzyszek     Res[i] = A[i];
429e53b31a5SKrzysztof Parzyszek   return Res;
430e53b31a5SKrzysztof Parzyszek }
431e53b31a5SKrzysztof Parzyszek 
eADD(const RegisterCell & A1,const RegisterCell & A2) const432e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1,
433e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
434e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
435e53b31a5SKrzysztof Parzyszek   assert(W == A2.width());
436e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
437e53b31a5SKrzysztof Parzyszek   bool Carry = false;
438e53b31a5SKrzysztof Parzyszek   uint16_t I;
439e53b31a5SKrzysztof Parzyszek   for (I = 0; I < W; ++I) {
440e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[I];
441e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[I];
442e53b31a5SKrzysztof Parzyszek     if (!V1.num() || !V2.num())
443e53b31a5SKrzysztof Parzyszek       break;
444e53b31a5SKrzysztof Parzyszek     unsigned S = bool(V1) + bool(V2) + Carry;
445e53b31a5SKrzysztof Parzyszek     Res[I] = BitValue(S & 1);
446e53b31a5SKrzysztof Parzyszek     Carry = (S > 1);
447e53b31a5SKrzysztof Parzyszek   }
448e53b31a5SKrzysztof Parzyszek   for (; I < W; ++I) {
449e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[I];
450e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[I];
451e53b31a5SKrzysztof Parzyszek     // If the next bit is same as Carry, the result will be 0 plus the
452e53b31a5SKrzysztof Parzyszek     // other bit. The Carry bit will remain unchanged.
453e53b31a5SKrzysztof Parzyszek     if (V1.is(Carry))
454e53b31a5SKrzysztof Parzyszek       Res[I] = BitValue::ref(V2);
455e53b31a5SKrzysztof Parzyszek     else if (V2.is(Carry))
456e53b31a5SKrzysztof Parzyszek       Res[I] = BitValue::ref(V1);
457e53b31a5SKrzysztof Parzyszek     else
458e53b31a5SKrzysztof Parzyszek       break;
459e53b31a5SKrzysztof Parzyszek   }
460e53b31a5SKrzysztof Parzyszek   for (; I < W; ++I)
461e53b31a5SKrzysztof Parzyszek     Res[I] = BitValue::self();
462e53b31a5SKrzysztof Parzyszek   return Res;
463e53b31a5SKrzysztof Parzyszek }
464e53b31a5SKrzysztof Parzyszek 
eSUB(const RegisterCell & A1,const RegisterCell & A2) const465e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1,
466e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
467e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
468e53b31a5SKrzysztof Parzyszek   assert(W == A2.width());
469e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
470e53b31a5SKrzysztof Parzyszek   bool Borrow = false;
471e53b31a5SKrzysztof Parzyszek   uint16_t I;
472e53b31a5SKrzysztof Parzyszek   for (I = 0; I < W; ++I) {
473e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[I];
474e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[I];
475e53b31a5SKrzysztof Parzyszek     if (!V1.num() || !V2.num())
476e53b31a5SKrzysztof Parzyszek       break;
477e53b31a5SKrzysztof Parzyszek     unsigned S = bool(V1) - bool(V2) - Borrow;
478e53b31a5SKrzysztof Parzyszek     Res[I] = BitValue(S & 1);
479e53b31a5SKrzysztof Parzyszek     Borrow = (S > 1);
480e53b31a5SKrzysztof Parzyszek   }
481e53b31a5SKrzysztof Parzyszek   for (; I < W; ++I) {
482e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[I];
483e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[I];
484e53b31a5SKrzysztof Parzyszek     if (V1.is(Borrow)) {
485e53b31a5SKrzysztof Parzyszek       Res[I] = BitValue::ref(V2);
486e53b31a5SKrzysztof Parzyszek       break;
487e53b31a5SKrzysztof Parzyszek     }
488e53b31a5SKrzysztof Parzyszek     if (V2.is(Borrow))
489e53b31a5SKrzysztof Parzyszek       Res[I] = BitValue::ref(V1);
490e53b31a5SKrzysztof Parzyszek     else
491e53b31a5SKrzysztof Parzyszek       break;
492e53b31a5SKrzysztof Parzyszek   }
493e53b31a5SKrzysztof Parzyszek   for (; I < W; ++I)
494e53b31a5SKrzysztof Parzyszek     Res[I] = BitValue::self();
495e53b31a5SKrzysztof Parzyszek   return Res;
496e53b31a5SKrzysztof Parzyszek }
497e53b31a5SKrzysztof Parzyszek 
eMLS(const RegisterCell & A1,const RegisterCell & A2) const498e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1,
499e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
500e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width() + A2.width();
501b2ca1b3fSEugene Zelenko   uint16_t Z = A1.ct(false) + A2.ct(false);
502e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
503e53b31a5SKrzysztof Parzyszek   Res.fill(0, Z, BitValue::Zero);
504e53b31a5SKrzysztof Parzyszek   Res.fill(Z, W, BitValue::self());
505e53b31a5SKrzysztof Parzyszek   return Res;
506e53b31a5SKrzysztof Parzyszek }
507e53b31a5SKrzysztof Parzyszek 
eMLU(const RegisterCell & A1,const RegisterCell & A2) const508e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1,
509e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
510e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width() + A2.width();
511b2ca1b3fSEugene Zelenko   uint16_t Z = A1.ct(false) + A2.ct(false);
512e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
513e53b31a5SKrzysztof Parzyszek   Res.fill(0, Z, BitValue::Zero);
514e53b31a5SKrzysztof Parzyszek   Res.fill(Z, W, BitValue::self());
515e53b31a5SKrzysztof Parzyszek   return Res;
516e53b31a5SKrzysztof Parzyszek }
517e53b31a5SKrzysztof Parzyszek 
eASL(const RegisterCell & A1,uint16_t Sh) const518e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1,
519e53b31a5SKrzysztof Parzyszek       uint16_t Sh) const {
520a45971acSKrzysztof Parzyszek   assert(Sh <= A1.width());
521e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
522e53b31a5SKrzysztof Parzyszek   Res.rol(Sh);
523e53b31a5SKrzysztof Parzyszek   Res.fill(0, Sh, BitValue::Zero);
524e53b31a5SKrzysztof Parzyszek   return Res;
525e53b31a5SKrzysztof Parzyszek }
526e53b31a5SKrzysztof Parzyszek 
eLSR(const RegisterCell & A1,uint16_t Sh) const527e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1,
528e53b31a5SKrzysztof Parzyszek       uint16_t Sh) const {
529e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
530e53b31a5SKrzysztof Parzyszek   assert(Sh <= W);
531e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
532e53b31a5SKrzysztof Parzyszek   Res.rol(W-Sh);
533e53b31a5SKrzysztof Parzyszek   Res.fill(W-Sh, W, BitValue::Zero);
534e53b31a5SKrzysztof Parzyszek   return Res;
535e53b31a5SKrzysztof Parzyszek }
536e53b31a5SKrzysztof Parzyszek 
eASR(const RegisterCell & A1,uint16_t Sh) const537e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1,
538e53b31a5SKrzysztof Parzyszek       uint16_t Sh) const {
539e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
540e53b31a5SKrzysztof Parzyszek   assert(Sh <= W);
541e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
542e53b31a5SKrzysztof Parzyszek   BitValue Sign = Res[W-1];
543e53b31a5SKrzysztof Parzyszek   Res.rol(W-Sh);
544e53b31a5SKrzysztof Parzyszek   Res.fill(W-Sh, W, Sign);
545e53b31a5SKrzysztof Parzyszek   return Res;
546e53b31a5SKrzysztof Parzyszek }
547e53b31a5SKrzysztof Parzyszek 
eAND(const RegisterCell & A1,const RegisterCell & A2) const548e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1,
549e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
550e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
551e53b31a5SKrzysztof Parzyszek   assert(W == A2.width());
552e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
553e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i) {
554e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[i];
555e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[i];
556e53b31a5SKrzysztof Parzyszek     if (V1.is(1))
557e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::ref(V2);
558e53b31a5SKrzysztof Parzyszek     else if (V2.is(1))
559e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::ref(V1);
560e53b31a5SKrzysztof Parzyszek     else if (V1.is(0) || V2.is(0))
561e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::Zero;
562e53b31a5SKrzysztof Parzyszek     else if (V1 == V2)
563e53b31a5SKrzysztof Parzyszek       Res[i] = V1;
564e53b31a5SKrzysztof Parzyszek     else
565e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::self();
566e53b31a5SKrzysztof Parzyszek   }
567e53b31a5SKrzysztof Parzyszek   return Res;
568e53b31a5SKrzysztof Parzyszek }
569e53b31a5SKrzysztof Parzyszek 
eORL(const RegisterCell & A1,const RegisterCell & A2) const570e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1,
571e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
572e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
573e53b31a5SKrzysztof Parzyszek   assert(W == A2.width());
574e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
575e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i) {
576e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[i];
577e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[i];
578e53b31a5SKrzysztof Parzyszek     if (V1.is(1) || V2.is(1))
579e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::One;
580e53b31a5SKrzysztof Parzyszek     else if (V1.is(0))
581e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::ref(V2);
582e53b31a5SKrzysztof Parzyszek     else if (V2.is(0))
583e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::ref(V1);
584e53b31a5SKrzysztof Parzyszek     else if (V1 == V2)
585e53b31a5SKrzysztof Parzyszek       Res[i] = V1;
586e53b31a5SKrzysztof Parzyszek     else
587e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::self();
588e53b31a5SKrzysztof Parzyszek   }
589e53b31a5SKrzysztof Parzyszek   return Res;
590e53b31a5SKrzysztof Parzyszek }
591e53b31a5SKrzysztof Parzyszek 
eXOR(const RegisterCell & A1,const RegisterCell & A2) const592e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1,
593e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2) const {
594e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
595e53b31a5SKrzysztof Parzyszek   assert(W == A2.width());
596e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
597e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i) {
598e53b31a5SKrzysztof Parzyszek     const BitValue &V1 = A1[i];
599e53b31a5SKrzysztof Parzyszek     const BitValue &V2 = A2[i];
600e53b31a5SKrzysztof Parzyszek     if (V1.is(0))
601e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::ref(V2);
602e53b31a5SKrzysztof Parzyszek     else if (V2.is(0))
603e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::ref(V1);
604e53b31a5SKrzysztof Parzyszek     else if (V1 == V2)
605e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::Zero;
606e53b31a5SKrzysztof Parzyszek     else
607e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::self();
608e53b31a5SKrzysztof Parzyszek   }
609e53b31a5SKrzysztof Parzyszek   return Res;
610e53b31a5SKrzysztof Parzyszek }
611e53b31a5SKrzysztof Parzyszek 
eNOT(const RegisterCell & A1) const612e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const {
613e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
614e53b31a5SKrzysztof Parzyszek   RegisterCell Res(W);
615e53b31a5SKrzysztof Parzyszek   for (uint16_t i = 0; i < W; ++i) {
616e53b31a5SKrzysztof Parzyszek     const BitValue &V = A1[i];
617e53b31a5SKrzysztof Parzyszek     if (V.is(0))
618e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::One;
619e53b31a5SKrzysztof Parzyszek     else if (V.is(1))
620e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::Zero;
621e53b31a5SKrzysztof Parzyszek     else
622e53b31a5SKrzysztof Parzyszek       Res[i] = BitValue::self();
623e53b31a5SKrzysztof Parzyszek   }
624e53b31a5SKrzysztof Parzyszek   return Res;
625e53b31a5SKrzysztof Parzyszek }
626e53b31a5SKrzysztof Parzyszek 
eSET(const RegisterCell & A1,uint16_t BitN) const627e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1,
628e53b31a5SKrzysztof Parzyszek       uint16_t BitN) const {
629a45971acSKrzysztof Parzyszek   assert(BitN < A1.width());
630e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
631e53b31a5SKrzysztof Parzyszek   Res[BitN] = BitValue::One;
632e53b31a5SKrzysztof Parzyszek   return Res;
633e53b31a5SKrzysztof Parzyszek }
634e53b31a5SKrzysztof Parzyszek 
eCLR(const RegisterCell & A1,uint16_t BitN) const635e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1,
636e53b31a5SKrzysztof Parzyszek       uint16_t BitN) const {
637a45971acSKrzysztof Parzyszek   assert(BitN < A1.width());
638e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
639e53b31a5SKrzysztof Parzyszek   Res[BitN] = BitValue::Zero;
640e53b31a5SKrzysztof Parzyszek   return Res;
641e53b31a5SKrzysztof Parzyszek }
642e53b31a5SKrzysztof Parzyszek 
eCLB(const RegisterCell & A1,bool B,uint16_t W) const643e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B,
644e53b31a5SKrzysztof Parzyszek       uint16_t W) const {
645e53b31a5SKrzysztof Parzyszek   uint16_t C = A1.cl(B), AW = A1.width();
646e53b31a5SKrzysztof Parzyszek   // If the last leading non-B bit is not a constant, then we don't know
647e53b31a5SKrzysztof Parzyszek   // the real count.
648e53b31a5SKrzysztof Parzyszek   if ((C < AW && A1[AW-1-C].num()) || C == AW)
649e53b31a5SKrzysztof Parzyszek     return eIMM(C, W);
650e53b31a5SKrzysztof Parzyszek   return RegisterCell::self(0, W);
651e53b31a5SKrzysztof Parzyszek }
652e53b31a5SKrzysztof Parzyszek 
eCTB(const RegisterCell & A1,bool B,uint16_t W) const653e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B,
654e53b31a5SKrzysztof Parzyszek       uint16_t W) const {
655e53b31a5SKrzysztof Parzyszek   uint16_t C = A1.ct(B), AW = A1.width();
656e53b31a5SKrzysztof Parzyszek   // If the last trailing non-B bit is not a constant, then we don't know
657e53b31a5SKrzysztof Parzyszek   // the real count.
658e53b31a5SKrzysztof Parzyszek   if ((C < AW && A1[C].num()) || C == AW)
659e53b31a5SKrzysztof Parzyszek     return eIMM(C, W);
660e53b31a5SKrzysztof Parzyszek   return RegisterCell::self(0, W);
661e53b31a5SKrzysztof Parzyszek }
662e53b31a5SKrzysztof Parzyszek 
eSXT(const RegisterCell & A1,uint16_t FromN) const663e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1,
664e53b31a5SKrzysztof Parzyszek       uint16_t FromN) const {
665e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
666e53b31a5SKrzysztof Parzyszek   assert(FromN <= W);
667e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
668e53b31a5SKrzysztof Parzyszek   BitValue Sign = Res[FromN-1];
669e53b31a5SKrzysztof Parzyszek   // Sign-extend "inreg".
670e53b31a5SKrzysztof Parzyszek   Res.fill(FromN, W, Sign);
671e53b31a5SKrzysztof Parzyszek   return Res;
672e53b31a5SKrzysztof Parzyszek }
673e53b31a5SKrzysztof Parzyszek 
eZXT(const RegisterCell & A1,uint16_t FromN) const674e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1,
675e53b31a5SKrzysztof Parzyszek       uint16_t FromN) const {
676e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
677e53b31a5SKrzysztof Parzyszek   assert(FromN <= W);
678e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
679e53b31a5SKrzysztof Parzyszek   Res.fill(FromN, W, BitValue::Zero);
680e53b31a5SKrzysztof Parzyszek   return Res;
681e53b31a5SKrzysztof Parzyszek }
682e53b31a5SKrzysztof Parzyszek 
eXTR(const RegisterCell & A1,uint16_t B,uint16_t E) const683e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1,
684e53b31a5SKrzysztof Parzyszek       uint16_t B, uint16_t E) const {
685e53b31a5SKrzysztof Parzyszek   uint16_t W = A1.width();
686e53b31a5SKrzysztof Parzyszek   assert(B < W && E <= W);
687e53b31a5SKrzysztof Parzyszek   if (B == E)
688e53b31a5SKrzysztof Parzyszek     return RegisterCell(0);
689e53b31a5SKrzysztof Parzyszek   uint16_t Last = (E > 0) ? E-1 : W-1;
690e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last));
691e53b31a5SKrzysztof Parzyszek   // Return shorter cell.
692e53b31a5SKrzysztof Parzyszek   return Res;
693e53b31a5SKrzysztof Parzyszek }
694e53b31a5SKrzysztof Parzyszek 
eINS(const RegisterCell & A1,const RegisterCell & A2,uint16_t AtN) const695e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1,
696e53b31a5SKrzysztof Parzyszek       const RegisterCell &A2, uint16_t AtN) const {
697e53b31a5SKrzysztof Parzyszek   uint16_t W1 = A1.width(), W2 = A2.width();
698a45971acSKrzysztof Parzyszek   (void)W1;
699e53b31a5SKrzysztof Parzyszek   assert(AtN < W1 && AtN+W2 <= W1);
700e53b31a5SKrzysztof Parzyszek   // Copy bits from A1, insert A2 at position AtN.
701e53b31a5SKrzysztof Parzyszek   RegisterCell Res = RegisterCell::ref(A1);
702e53b31a5SKrzysztof Parzyszek   if (W2 > 0)
703e53b31a5SKrzysztof Parzyszek     Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1));
704e53b31a5SKrzysztof Parzyszek   return Res;
705e53b31a5SKrzysztof Parzyszek }
706e53b31a5SKrzysztof Parzyszek 
mask(Register Reg,unsigned Sub) const70706fcc4f0SGaurav Jain BT::BitMask BT::MachineEvaluator::mask(Register Reg, unsigned Sub) const {
708e53b31a5SKrzysztof Parzyszek   assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0");
709e53b31a5SKrzysztof Parzyszek   uint16_t W = getRegBitWidth(Reg);
710e53b31a5SKrzysztof Parzyszek   assert(W > 0 && "Cannot generate mask for empty register");
711e53b31a5SKrzysztof Parzyszek   return BitMask(0, W-1);
712e53b31a5SKrzysztof Parzyszek }
713e53b31a5SKrzysztof Parzyszek 
getPhysRegBitWidth(MCRegister Reg) const714bab72dd5SMircea Trofin uint16_t BT::MachineEvaluator::getPhysRegBitWidth(MCRegister Reg) const {
7157e604decSKrzysztof Parzyszek   const TargetRegisterClass &PC = *TRI.getMinimalPhysRegClass(Reg);
7167e604decSKrzysztof Parzyszek   return TRI.getRegSizeInBits(PC);
7177e604decSKrzysztof Parzyszek }
7187e604decSKrzysztof Parzyszek 
evaluate(const MachineInstr & MI,const CellMapType & Inputs,CellMapType & Outputs) const71998226e3dSDuncan P. N. Exon Smith bool BT::MachineEvaluator::evaluate(const MachineInstr &MI,
72098226e3dSDuncan P. N. Exon Smith                                     const CellMapType &Inputs,
72198226e3dSDuncan P. N. Exon Smith                                     CellMapType &Outputs) const {
72298226e3dSDuncan P. N. Exon Smith   unsigned Opc = MI.getOpcode();
723e53b31a5SKrzysztof Parzyszek   switch (Opc) {
724e53b31a5SKrzysztof Parzyszek     case TargetOpcode::REG_SEQUENCE: {
72598226e3dSDuncan P. N. Exon Smith       RegisterRef RD = MI.getOperand(0);
726e53b31a5SKrzysztof Parzyszek       assert(RD.Sub == 0);
72798226e3dSDuncan P. N. Exon Smith       RegisterRef RS = MI.getOperand(1);
72898226e3dSDuncan P. N. Exon Smith       unsigned SS = MI.getOperand(2).getImm();
72998226e3dSDuncan P. N. Exon Smith       RegisterRef RT = MI.getOperand(3);
73098226e3dSDuncan P. N. Exon Smith       unsigned ST = MI.getOperand(4).getImm();
731e53b31a5SKrzysztof Parzyszek       assert(SS != ST);
732e53b31a5SKrzysztof Parzyszek 
733e53b31a5SKrzysztof Parzyszek       uint16_t W = getRegBitWidth(RD);
734e53b31a5SKrzysztof Parzyszek       RegisterCell Res(W);
735e53b31a5SKrzysztof Parzyszek       Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS));
736e53b31a5SKrzysztof Parzyszek       Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST));
737e53b31a5SKrzysztof Parzyszek       putCell(RD, Res, Outputs);
738e53b31a5SKrzysztof Parzyszek       break;
739e53b31a5SKrzysztof Parzyszek     }
740e53b31a5SKrzysztof Parzyszek 
741e53b31a5SKrzysztof Parzyszek     case TargetOpcode::COPY: {
742e53b31a5SKrzysztof Parzyszek       // COPY can transfer a smaller register into a wider one.
743e53b31a5SKrzysztof Parzyszek       // If that is the case, fill the remaining high bits with 0.
74498226e3dSDuncan P. N. Exon Smith       RegisterRef RD = MI.getOperand(0);
74598226e3dSDuncan P. N. Exon Smith       RegisterRef RS = MI.getOperand(1);
746e53b31a5SKrzysztof Parzyszek       assert(RD.Sub == 0);
747e53b31a5SKrzysztof Parzyszek       uint16_t WD = getRegBitWidth(RD);
748e53b31a5SKrzysztof Parzyszek       uint16_t WS = getRegBitWidth(RS);
749e53b31a5SKrzysztof Parzyszek       assert(WD >= WS);
750e53b31a5SKrzysztof Parzyszek       RegisterCell Src = getCell(RS, Inputs);
751e53b31a5SKrzysztof Parzyszek       RegisterCell Res(WD);
752e53b31a5SKrzysztof Parzyszek       Res.insert(Src, BitMask(0, WS-1));
753e53b31a5SKrzysztof Parzyszek       Res.fill(WS, WD, BitValue::Zero);
754e53b31a5SKrzysztof Parzyszek       putCell(RD, Res, Outputs);
755e53b31a5SKrzysztof Parzyszek       break;
756e53b31a5SKrzysztof Parzyszek     }
757e53b31a5SKrzysztof Parzyszek 
758e53b31a5SKrzysztof Parzyszek     default:
759e53b31a5SKrzysztof Parzyszek       return false;
760e53b31a5SKrzysztof Parzyszek   }
761e53b31a5SKrzysztof Parzyszek 
762e53b31a5SKrzysztof Parzyszek   return true;
763e53b31a5SKrzysztof Parzyszek }
764e53b31a5SKrzysztof Parzyszek 
operator ()(const MachineInstr * InstA,const MachineInstr * InstB) const765058d3cecSKrzysztof Parzyszek bool BT::UseQueueType::Cmp::operator()(const MachineInstr *InstA,
766058d3cecSKrzysztof Parzyszek                                        const MachineInstr *InstB) const {
767058d3cecSKrzysztof Parzyszek   // This is a comparison function for a priority queue: give higher priority
768058d3cecSKrzysztof Parzyszek   // to earlier instructions.
769058d3cecSKrzysztof Parzyszek   // This operator is used as "less", so returning "true" gives InstB higher
770058d3cecSKrzysztof Parzyszek   // priority (because then InstA < InstB).
771058d3cecSKrzysztof Parzyszek   if (InstA == InstB)
772058d3cecSKrzysztof Parzyszek     return false;
773058d3cecSKrzysztof Parzyszek   const MachineBasicBlock *BA = InstA->getParent();
774058d3cecSKrzysztof Parzyszek   const MachineBasicBlock *BB = InstB->getParent();
775058d3cecSKrzysztof Parzyszek   if (BA != BB) {
776058d3cecSKrzysztof Parzyszek     // If the blocks are different, ideally the dominating block would
777058d3cecSKrzysztof Parzyszek     // have a higher priority, but it may be too expensive to check.
778058d3cecSKrzysztof Parzyszek     return BA->getNumber() > BB->getNumber();
779058d3cecSKrzysztof Parzyszek   }
780058d3cecSKrzysztof Parzyszek 
781e3ef6e07SKrzysztof Parzyszek   auto getDist = [this] (const MachineInstr *MI) {
782e3ef6e07SKrzysztof Parzyszek     auto F = Dist.find(MI);
783e3ef6e07SKrzysztof Parzyszek     if (F != Dist.end())
784e3ef6e07SKrzysztof Parzyszek       return F->second;
785e3ef6e07SKrzysztof Parzyszek     MachineBasicBlock::const_iterator I = MI->getParent()->begin();
786e3ef6e07SKrzysztof Parzyszek     MachineBasicBlock::const_iterator E = MI->getIterator();
787e3ef6e07SKrzysztof Parzyszek     unsigned D = std::distance(I, E);
788e3ef6e07SKrzysztof Parzyszek     Dist.insert(std::make_pair(MI, D));
789e3ef6e07SKrzysztof Parzyszek     return D;
790e3ef6e07SKrzysztof Parzyszek   };
791e3ef6e07SKrzysztof Parzyszek 
792e3ef6e07SKrzysztof Parzyszek   return getDist(InstA) > getDist(InstB);
793058d3cecSKrzysztof Parzyszek }
794058d3cecSKrzysztof Parzyszek 
795e53b31a5SKrzysztof Parzyszek // Main W-Z implementation.
796e53b31a5SKrzysztof Parzyszek 
visitPHI(const MachineInstr & PI)79798226e3dSDuncan P. N. Exon Smith void BT::visitPHI(const MachineInstr &PI) {
79898226e3dSDuncan P. N. Exon Smith   int ThisN = PI.getParent()->getNumber();
799e53b31a5SKrzysztof Parzyszek   if (Trace)
80025528d6dSFrancis Visoiu Mistrih     dbgs() << "Visit FI(" << printMBBReference(*PI.getParent()) << "): " << PI;
801e53b31a5SKrzysztof Parzyszek 
80298226e3dSDuncan P. N. Exon Smith   const MachineOperand &MD = PI.getOperand(0);
803e53b31a5SKrzysztof Parzyszek   assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition");
804e53b31a5SKrzysztof Parzyszek   RegisterRef DefRR(MD);
805e53b31a5SKrzysztof Parzyszek   uint16_t DefBW = ME.getRegBitWidth(DefRR);
806e53b31a5SKrzysztof Parzyszek 
807e53b31a5SKrzysztof Parzyszek   RegisterCell DefC = ME.getCell(DefRR, Map);
808e53b31a5SKrzysztof Parzyszek   if (DefC == RegisterCell::self(DefRR.Reg, DefBW))    // XXX slow
809e53b31a5SKrzysztof Parzyszek     return;
810e53b31a5SKrzysztof Parzyszek 
811e53b31a5SKrzysztof Parzyszek   bool Changed = false;
812e53b31a5SKrzysztof Parzyszek 
81398226e3dSDuncan P. N. Exon Smith   for (unsigned i = 1, n = PI.getNumOperands(); i < n; i += 2) {
81498226e3dSDuncan P. N. Exon Smith     const MachineBasicBlock *PB = PI.getOperand(i + 1).getMBB();
815e53b31a5SKrzysztof Parzyszek     int PredN = PB->getNumber();
816e53b31a5SKrzysztof Parzyszek     if (Trace)
81725528d6dSFrancis Visoiu Mistrih       dbgs() << "  edge " << printMBBReference(*PB) << "->"
81825528d6dSFrancis Visoiu Mistrih              << printMBBReference(*PI.getParent());
819e53b31a5SKrzysztof Parzyszek     if (!EdgeExec.count(CFGEdge(PredN, ThisN))) {
820e53b31a5SKrzysztof Parzyszek       if (Trace)
821e53b31a5SKrzysztof Parzyszek         dbgs() << " not executable\n";
822e53b31a5SKrzysztof Parzyszek       continue;
823e53b31a5SKrzysztof Parzyszek     }
824e53b31a5SKrzysztof Parzyszek 
82598226e3dSDuncan P. N. Exon Smith     RegisterRef RU = PI.getOperand(i);
826e53b31a5SKrzysztof Parzyszek     RegisterCell ResC = ME.getCell(RU, Map);
827e53b31a5SKrzysztof Parzyszek     if (Trace)
8289d419d3bSFrancis Visoiu Mistrih       dbgs() << " input reg: " << printReg(RU.Reg, &ME.TRI, RU.Sub)
829e53b31a5SKrzysztof Parzyszek              << " cell: " << ResC << "\n";
830e53b31a5SKrzysztof Parzyszek     Changed |= DefC.meet(ResC, DefRR.Reg);
831e53b31a5SKrzysztof Parzyszek   }
832e53b31a5SKrzysztof Parzyszek 
833e53b31a5SKrzysztof Parzyszek   if (Changed) {
834e53b31a5SKrzysztof Parzyszek     if (Trace)
8359d419d3bSFrancis Visoiu Mistrih       dbgs() << "Output: " << printReg(DefRR.Reg, &ME.TRI, DefRR.Sub)
836e53b31a5SKrzysztof Parzyszek              << " cell: " << DefC << "\n";
837e53b31a5SKrzysztof Parzyszek     ME.putCell(DefRR, DefC, Map);
838e53b31a5SKrzysztof Parzyszek     visitUsesOf(DefRR.Reg);
839e53b31a5SKrzysztof Parzyszek   }
840e53b31a5SKrzysztof Parzyszek }
841e53b31a5SKrzysztof Parzyszek 
visitNonBranch(const MachineInstr & MI)84298226e3dSDuncan P. N. Exon Smith void BT::visitNonBranch(const MachineInstr &MI) {
84325528d6dSFrancis Visoiu Mistrih   if (Trace)
84425528d6dSFrancis Visoiu Mistrih     dbgs() << "Visit MI(" << printMBBReference(*MI.getParent()) << "): " << MI;
845801bf7ebSShiva Chen   if (MI.isDebugInstr())
846e53b31a5SKrzysztof Parzyszek     return;
84798226e3dSDuncan P. N. Exon Smith   assert(!MI.isBranch() && "Unexpected branch instruction");
848e53b31a5SKrzysztof Parzyszek 
849e53b31a5SKrzysztof Parzyszek   CellMapType ResMap;
850e53b31a5SKrzysztof Parzyszek   bool Eval = ME.evaluate(MI, Map, ResMap);
851e53b31a5SKrzysztof Parzyszek 
852e53b31a5SKrzysztof Parzyszek   if (Trace && Eval) {
853562356d6SKazu Hirata     for (const MachineOperand &MO : MI.operands()) {
854e53b31a5SKrzysztof Parzyszek       if (!MO.isReg() || !MO.isUse())
855e53b31a5SKrzysztof Parzyszek         continue;
856e53b31a5SKrzysztof Parzyszek       RegisterRef RU(MO);
8579d419d3bSFrancis Visoiu Mistrih       dbgs() << "  input reg: " << printReg(RU.Reg, &ME.TRI, RU.Sub)
858e53b31a5SKrzysztof Parzyszek              << " cell: " << ME.getCell(RU, Map) << "\n";
859e53b31a5SKrzysztof Parzyszek     }
860e53b31a5SKrzysztof Parzyszek     dbgs() << "Outputs:\n";
8619c11026cSMark de Wever     for (const std::pair<const unsigned, RegisterCell> &P : ResMap) {
86202893de4SKrzysztof Parzyszek       RegisterRef RD(P.first);
8639d419d3bSFrancis Visoiu Mistrih       dbgs() << "  " << printReg(P.first, &ME.TRI) << " cell: "
864e53b31a5SKrzysztof Parzyszek              << ME.getCell(RD, ResMap) << "\n";
865e53b31a5SKrzysztof Parzyszek     }
866e53b31a5SKrzysztof Parzyszek   }
867e53b31a5SKrzysztof Parzyszek 
868e53b31a5SKrzysztof Parzyszek   // Iterate over all definitions of the instruction, and update the
869e53b31a5SKrzysztof Parzyszek   // cells accordingly.
87002893de4SKrzysztof Parzyszek   for (const MachineOperand &MO : MI.operands()) {
871e53b31a5SKrzysztof Parzyszek     // Visit register defs only.
872e53b31a5SKrzysztof Parzyszek     if (!MO.isReg() || !MO.isDef())
873e53b31a5SKrzysztof Parzyszek       continue;
874e53b31a5SKrzysztof Parzyszek     RegisterRef RD(MO);
875e53b31a5SKrzysztof Parzyszek     assert(RD.Sub == 0 && "Unexpected sub-register in definition");
87606fcc4f0SGaurav Jain     if (!RD.Reg.isVirtual())
877e53b31a5SKrzysztof Parzyszek       continue;
878e53b31a5SKrzysztof Parzyszek 
879e53b31a5SKrzysztof Parzyszek     bool Changed = false;
8809a5d7889SBenjamin Kramer     if (!Eval || ResMap.count(RD.Reg) == 0) {
881e53b31a5SKrzysztof Parzyszek       // Set to "ref" (aka "bottom").
882e53b31a5SKrzysztof Parzyszek       uint16_t DefBW = ME.getRegBitWidth(RD);
883e53b31a5SKrzysztof Parzyszek       RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW);
884e53b31a5SKrzysztof Parzyszek       if (RefC != ME.getCell(RD, Map)) {
885e53b31a5SKrzysztof Parzyszek         ME.putCell(RD, RefC, Map);
886e53b31a5SKrzysztof Parzyszek         Changed = true;
887e53b31a5SKrzysztof Parzyszek       }
888e53b31a5SKrzysztof Parzyszek     } else {
889e53b31a5SKrzysztof Parzyszek       RegisterCell DefC = ME.getCell(RD, Map);
890e53b31a5SKrzysztof Parzyszek       RegisterCell ResC = ME.getCell(RD, ResMap);
891e53b31a5SKrzysztof Parzyszek       // This is a non-phi instruction, so the values of the inputs come
892e53b31a5SKrzysztof Parzyszek       // from the same registers each time this instruction is evaluated.
893e53b31a5SKrzysztof Parzyszek       // During the propagation, the values of the inputs can become lowered
894e53b31a5SKrzysztof Parzyszek       // in the sense of the lattice operation, which may cause different
895e53b31a5SKrzysztof Parzyszek       // results to be calculated in subsequent evaluations. This should
896e53b31a5SKrzysztof Parzyszek       // not cause the bottoming of the result in the map, since the new
897e53b31a5SKrzysztof Parzyszek       // result is already reflecting the lowered inputs.
898e53b31a5SKrzysztof Parzyszek       for (uint16_t i = 0, w = DefC.width(); i < w; ++i) {
899e53b31a5SKrzysztof Parzyszek         BitValue &V = DefC[i];
900e53b31a5SKrzysztof Parzyszek         // Bits that are already "bottom" should not be updated.
901e53b31a5SKrzysztof Parzyszek         if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg)
902e53b31a5SKrzysztof Parzyszek           continue;
903e53b31a5SKrzysztof Parzyszek         // Same for those that are identical in DefC and ResC.
904e53b31a5SKrzysztof Parzyszek         if (V == ResC[i])
905e53b31a5SKrzysztof Parzyszek           continue;
906e53b31a5SKrzysztof Parzyszek         V = ResC[i];
907e53b31a5SKrzysztof Parzyszek         Changed = true;
908e53b31a5SKrzysztof Parzyszek       }
909e53b31a5SKrzysztof Parzyszek       if (Changed)
910e53b31a5SKrzysztof Parzyszek         ME.putCell(RD, DefC, Map);
911e53b31a5SKrzysztof Parzyszek     }
912e53b31a5SKrzysztof Parzyszek     if (Changed)
913e53b31a5SKrzysztof Parzyszek       visitUsesOf(RD.Reg);
914e53b31a5SKrzysztof Parzyszek   }
915e53b31a5SKrzysztof Parzyszek }
916e53b31a5SKrzysztof Parzyszek 
visitBranchesFrom(const MachineInstr & BI)91798226e3dSDuncan P. N. Exon Smith void BT::visitBranchesFrom(const MachineInstr &BI) {
91898226e3dSDuncan P. N. Exon Smith   const MachineBasicBlock &B = *BI.getParent();
919e53b31a5SKrzysztof Parzyszek   MachineBasicBlock::const_iterator It = BI, End = B.end();
920e53b31a5SKrzysztof Parzyszek   BranchTargetList Targets, BTs;
921e53b31a5SKrzysztof Parzyszek   bool FallsThrough = true, DefaultToAll = false;
922e53b31a5SKrzysztof Parzyszek   int ThisN = B.getNumber();
923e53b31a5SKrzysztof Parzyszek 
924e53b31a5SKrzysztof Parzyszek   do {
925e53b31a5SKrzysztof Parzyszek     BTs.clear();
92698226e3dSDuncan P. N. Exon Smith     const MachineInstr &MI = *It;
927e53b31a5SKrzysztof Parzyszek     if (Trace)
92825528d6dSFrancis Visoiu Mistrih       dbgs() << "Visit BR(" << printMBBReference(B) << "): " << MI;
92998226e3dSDuncan P. N. Exon Smith     assert(MI.isBranch() && "Expecting branch instruction");
93098226e3dSDuncan P. N. Exon Smith     InstrExec.insert(&MI);
931e53b31a5SKrzysztof Parzyszek     bool Eval = ME.evaluate(MI, Map, BTs, FallsThrough);
932e53b31a5SKrzysztof Parzyszek     if (!Eval) {
933e53b31a5SKrzysztof Parzyszek       // If the evaluation failed, we will add all targets. Keep going in
934e53b31a5SKrzysztof Parzyszek       // the loop to mark all executable branches as such.
935e53b31a5SKrzysztof Parzyszek       DefaultToAll = true;
936e53b31a5SKrzysztof Parzyszek       FallsThrough = true;
937e53b31a5SKrzysztof Parzyszek       if (Trace)
938e53b31a5SKrzysztof Parzyszek         dbgs() << "  failed to evaluate: will add all CFG successors\n";
939e53b31a5SKrzysztof Parzyszek     } else if (!DefaultToAll) {
940e53b31a5SKrzysztof Parzyszek       // If evaluated successfully add the targets to the cumulative list.
941e53b31a5SKrzysztof Parzyszek       if (Trace) {
942e53b31a5SKrzysztof Parzyszek         dbgs() << "  adding targets:";
9430a5788abSKazu Hirata         for (const MachineBasicBlock *BT : BTs)
9440a5788abSKazu Hirata           dbgs() << " " << printMBBReference(*BT);
945e53b31a5SKrzysztof Parzyszek         if (FallsThrough)
946e53b31a5SKrzysztof Parzyszek           dbgs() << "\n  falls through\n";
947e53b31a5SKrzysztof Parzyszek         else
948e53b31a5SKrzysztof Parzyszek           dbgs() << "\n  does not fall through\n";
949e53b31a5SKrzysztof Parzyszek       }
950e53b31a5SKrzysztof Parzyszek       Targets.insert(BTs.begin(), BTs.end());
951e53b31a5SKrzysztof Parzyszek     }
952e53b31a5SKrzysztof Parzyszek     ++It;
953e53b31a5SKrzysztof Parzyszek   } while (FallsThrough && It != End);
954e53b31a5SKrzysztof Parzyszek 
9554b0aa572SJames Y Knight   if (B.mayHaveInlineAsmBr())
9564b0aa572SJames Y Knight     DefaultToAll = true;
9574b0aa572SJames Y Knight 
958e53b31a5SKrzysztof Parzyszek   if (!DefaultToAll) {
959e53b31a5SKrzysztof Parzyszek     // Need to add all CFG successors that lead to EH landing pads.
960e53b31a5SKrzysztof Parzyszek     // There won't be explicit branches to these blocks, but they must
961e53b31a5SKrzysztof Parzyszek     // be processed.
96202893de4SKrzysztof Parzyszek     for (const MachineBasicBlock *SB : B.successors()) {
9630e288234SReid Kleckner       if (SB->isEHPad())
964e53b31a5SKrzysztof Parzyszek         Targets.insert(SB);
965e53b31a5SKrzysztof Parzyszek     }
966e53b31a5SKrzysztof Parzyszek     if (FallsThrough) {
967a72c6e25SDuncan P. N. Exon Smith       MachineFunction::const_iterator BIt = B.getIterator();
968e53b31a5SKrzysztof Parzyszek       MachineFunction::const_iterator Next = std::next(BIt);
969e53b31a5SKrzysztof Parzyszek       if (Next != MF.end())
970e53b31a5SKrzysztof Parzyszek         Targets.insert(&*Next);
971e53b31a5SKrzysztof Parzyszek     }
972e53b31a5SKrzysztof Parzyszek   } else {
97302893de4SKrzysztof Parzyszek     for (const MachineBasicBlock *SB : B.successors())
97402893de4SKrzysztof Parzyszek       Targets.insert(SB);
975e53b31a5SKrzysztof Parzyszek   }
976e53b31a5SKrzysztof Parzyszek 
97702893de4SKrzysztof Parzyszek   for (const MachineBasicBlock *TB : Targets)
97802893de4SKrzysztof Parzyszek     FlowQ.push(CFGEdge(ThisN, TB->getNumber()));
979e53b31a5SKrzysztof Parzyszek }
980e53b31a5SKrzysztof Parzyszek 
visitUsesOf(Register Reg)98106fcc4f0SGaurav Jain void BT::visitUsesOf(Register Reg) {
982e53b31a5SKrzysztof Parzyszek   if (Trace)
983058d3cecSKrzysztof Parzyszek     dbgs() << "queuing uses of modified reg " << printReg(Reg, &ME.TRI)
984058d3cecSKrzysztof Parzyszek            << " cell: " << ME.getCell(Reg, Map) << '\n';
985e53b31a5SKrzysztof Parzyszek 
986058d3cecSKrzysztof Parzyszek   for (MachineInstr &UseI : MRI.use_nodbg_instructions(Reg))
987058d3cecSKrzysztof Parzyszek     UseQ.push(&UseI);
988e53b31a5SKrzysztof Parzyszek }
989e53b31a5SKrzysztof Parzyszek 
get(RegisterRef RR) const990e53b31a5SKrzysztof Parzyszek BT::RegisterCell BT::get(RegisterRef RR) const {
991e53b31a5SKrzysztof Parzyszek   return ME.getCell(RR, Map);
992e53b31a5SKrzysztof Parzyszek }
993e53b31a5SKrzysztof Parzyszek 
put(RegisterRef RR,const RegisterCell & RC)994e53b31a5SKrzysztof Parzyszek void BT::put(RegisterRef RR, const RegisterCell &RC) {
995e53b31a5SKrzysztof Parzyszek   ME.putCell(RR, RC, Map);
996e53b31a5SKrzysztof Parzyszek }
997e53b31a5SKrzysztof Parzyszek 
998e53b31a5SKrzysztof Parzyszek // Replace all references to bits from OldRR with the corresponding bits
999e53b31a5SKrzysztof Parzyszek // in NewRR.
subst(RegisterRef OldRR,RegisterRef NewRR)1000e53b31a5SKrzysztof Parzyszek void BT::subst(RegisterRef OldRR, RegisterRef NewRR) {
10019a5d7889SBenjamin Kramer   assert(Map.count(OldRR.Reg) > 0 && "OldRR not present in map");
1002e53b31a5SKrzysztof Parzyszek   BitMask OM = ME.mask(OldRR.Reg, OldRR.Sub);
1003e53b31a5SKrzysztof Parzyszek   BitMask NM = ME.mask(NewRR.Reg, NewRR.Sub);
1004e53b31a5SKrzysztof Parzyszek   uint16_t OMB = OM.first(), OME = OM.last();
1005e53b31a5SKrzysztof Parzyszek   uint16_t NMB = NM.first(), NME = NM.last();
1006a45971acSKrzysztof Parzyszek   (void)NME;
1007e53b31a5SKrzysztof Parzyszek   assert((OME-OMB == NME-NMB) &&
1008e53b31a5SKrzysztof Parzyszek          "Substituting registers of different lengths");
100902893de4SKrzysztof Parzyszek   for (std::pair<const unsigned, RegisterCell> &P : Map) {
101002893de4SKrzysztof Parzyszek     RegisterCell &RC = P.second;
1011e53b31a5SKrzysztof Parzyszek     for (uint16_t i = 0, w = RC.width(); i < w; ++i) {
1012e53b31a5SKrzysztof Parzyszek       BitValue &V = RC[i];
1013e53b31a5SKrzysztof Parzyszek       if (V.Type != BitValue::Ref || V.RefI.Reg != OldRR.Reg)
1014e53b31a5SKrzysztof Parzyszek         continue;
1015e53b31a5SKrzysztof Parzyszek       if (V.RefI.Pos < OMB || V.RefI.Pos > OME)
1016e53b31a5SKrzysztof Parzyszek         continue;
1017e53b31a5SKrzysztof Parzyszek       V.RefI.Reg = NewRR.Reg;
1018e53b31a5SKrzysztof Parzyszek       V.RefI.Pos += NMB-OMB;
1019e53b31a5SKrzysztof Parzyszek     }
1020e53b31a5SKrzysztof Parzyszek   }
1021e53b31a5SKrzysztof Parzyszek }
1022e53b31a5SKrzysztof Parzyszek 
1023e53b31a5SKrzysztof Parzyszek // Check if the block has been "executed" during propagation. (If not, the
1024e53b31a5SKrzysztof Parzyszek // block is dead, but it may still appear to be reachable.)
reached(const MachineBasicBlock * B) const1025e53b31a5SKrzysztof Parzyszek bool BT::reached(const MachineBasicBlock *B) const {
1026e53b31a5SKrzysztof Parzyszek   int BN = B->getNumber();
1027e53b31a5SKrzysztof Parzyszek   assert(BN >= 0);
10285bfaf56eSKrzysztof Parzyszek   return ReachedBB.count(BN);
1029e53b31a5SKrzysztof Parzyszek }
1030e53b31a5SKrzysztof Parzyszek 
10316eba5b8cSKrzysztof Parzyszek // Visit an individual instruction. This could be a newly added instruction,
10326eba5b8cSKrzysztof Parzyszek // or one that has been modified by an optimization.
visit(const MachineInstr & MI)10339273ecc1SKrzysztof Parzyszek void BT::visit(const MachineInstr &MI) {
10346eba5b8cSKrzysztof Parzyszek   assert(!MI.isBranch() && "Only non-branches are allowed");
10356eba5b8cSKrzysztof Parzyszek   InstrExec.insert(&MI);
10366eba5b8cSKrzysztof Parzyszek   visitNonBranch(MI);
1037058d3cecSKrzysztof Parzyszek   // Make sure to flush all the pending use updates.
1038058d3cecSKrzysztof Parzyszek   runUseQueue();
1039b558ae21SKrzysztof Parzyszek   // The call to visitNonBranch could propagate the changes until a branch
1040b558ae21SKrzysztof Parzyszek   // is actually visited. This could result in adding CFG edges to the flow
1041b558ae21SKrzysztof Parzyszek   // queue. Since the queue won't be processed, clear it.
1042b558ae21SKrzysztof Parzyszek   while (!FlowQ.empty())
1043b558ae21SKrzysztof Parzyszek     FlowQ.pop();
10446eba5b8cSKrzysztof Parzyszek }
10456eba5b8cSKrzysztof Parzyszek 
reset()1046e53b31a5SKrzysztof Parzyszek void BT::reset() {
1047e53b31a5SKrzysztof Parzyszek   EdgeExec.clear();
1048e53b31a5SKrzysztof Parzyszek   InstrExec.clear();
1049e53b31a5SKrzysztof Parzyszek   Map.clear();
10505bfaf56eSKrzysztof Parzyszek   ReachedBB.clear();
10515bfaf56eSKrzysztof Parzyszek   ReachedBB.reserve(MF.size());
1052e53b31a5SKrzysztof Parzyszek }
1053e53b31a5SKrzysztof Parzyszek 
runEdgeQueue(BitVector & BlockScanned)1054058d3cecSKrzysztof Parzyszek void BT::runEdgeQueue(BitVector &BlockScanned) {
1055e53b31a5SKrzysztof Parzyszek   while (!FlowQ.empty()) {
1056e53b31a5SKrzysztof Parzyszek     CFGEdge Edge = FlowQ.front();
1057e53b31a5SKrzysztof Parzyszek     FlowQ.pop();
1058e53b31a5SKrzysztof Parzyszek 
1059*b254d671SKazu Hirata     if (!EdgeExec.insert(Edge).second)
1060058d3cecSKrzysztof Parzyszek       return;
10615bfaf56eSKrzysztof Parzyszek     ReachedBB.insert(Edge.second);
1062e53b31a5SKrzysztof Parzyszek 
1063e53b31a5SKrzysztof Parzyszek     const MachineBasicBlock &B = *MF.getBlockNumbered(Edge.second);
1064e53b31a5SKrzysztof Parzyszek     MachineBasicBlock::const_iterator It = B.begin(), End = B.end();
1065e53b31a5SKrzysztof Parzyszek     // Visit PHI nodes first.
1066e53b31a5SKrzysztof Parzyszek     while (It != End && It->isPHI()) {
106798226e3dSDuncan P. N. Exon Smith       const MachineInstr &PI = *It++;
106898226e3dSDuncan P. N. Exon Smith       InstrExec.insert(&PI);
1069e53b31a5SKrzysztof Parzyszek       visitPHI(PI);
1070e53b31a5SKrzysztof Parzyszek     }
1071e53b31a5SKrzysztof Parzyszek 
1072e53b31a5SKrzysztof Parzyszek     // If this block has already been visited through a flow graph edge,
1073e53b31a5SKrzysztof Parzyszek     // then the instructions have already been processed. Any updates to
1074e53b31a5SKrzysztof Parzyszek     // the cells would now only happen through visitUsesOf...
1075e53b31a5SKrzysztof Parzyszek     if (BlockScanned[Edge.second])
1076058d3cecSKrzysztof Parzyszek       return;
1077e53b31a5SKrzysztof Parzyszek     BlockScanned[Edge.second] = true;
1078e53b31a5SKrzysztof Parzyszek 
1079e53b31a5SKrzysztof Parzyszek     // Visit non-branch instructions.
1080e53b31a5SKrzysztof Parzyszek     while (It != End && !It->isBranch()) {
108198226e3dSDuncan P. N. Exon Smith       const MachineInstr &MI = *It++;
108298226e3dSDuncan P. N. Exon Smith       InstrExec.insert(&MI);
1083e53b31a5SKrzysztof Parzyszek       visitNonBranch(MI);
1084e53b31a5SKrzysztof Parzyszek     }
1085e53b31a5SKrzysztof Parzyszek     // If block end has been reached, add the fall-through edge to the queue.
1086e53b31a5SKrzysztof Parzyszek     if (It == End) {
1087a72c6e25SDuncan P. N. Exon Smith       MachineFunction::const_iterator BIt = B.getIterator();
1088e53b31a5SKrzysztof Parzyszek       MachineFunction::const_iterator Next = std::next(BIt);
108983c4b687SDuncan P. N. Exon Smith       if (Next != MF.end() && B.isSuccessor(&*Next)) {
1090e53b31a5SKrzysztof Parzyszek         int ThisN = B.getNumber();
1091e53b31a5SKrzysztof Parzyszek         int NextN = Next->getNumber();
1092e53b31a5SKrzysztof Parzyszek         FlowQ.push(CFGEdge(ThisN, NextN));
1093e53b31a5SKrzysztof Parzyszek       }
1094e53b31a5SKrzysztof Parzyszek     } else {
1095e53b31a5SKrzysztof Parzyszek       // Handle the remaining sequence of branches. This function will update
1096e53b31a5SKrzysztof Parzyszek       // the work queue.
109798226e3dSDuncan P. N. Exon Smith       visitBranchesFrom(*It);
1098e53b31a5SKrzysztof Parzyszek     }
1099e53b31a5SKrzysztof Parzyszek   } // while (!FlowQ->empty())
1100058d3cecSKrzysztof Parzyszek }
1101058d3cecSKrzysztof Parzyszek 
runUseQueue()1102058d3cecSKrzysztof Parzyszek void BT::runUseQueue() {
1103058d3cecSKrzysztof Parzyszek   while (!UseQ.empty()) {
1104058d3cecSKrzysztof Parzyszek     MachineInstr &UseI = *UseQ.front();
1105058d3cecSKrzysztof Parzyszek     UseQ.pop();
1106058d3cecSKrzysztof Parzyszek 
1107058d3cecSKrzysztof Parzyszek     if (!InstrExec.count(&UseI))
1108058d3cecSKrzysztof Parzyszek       continue;
1109058d3cecSKrzysztof Parzyszek     if (UseI.isPHI())
1110058d3cecSKrzysztof Parzyszek       visitPHI(UseI);
1111058d3cecSKrzysztof Parzyszek     else if (!UseI.isBranch())
1112058d3cecSKrzysztof Parzyszek       visitNonBranch(UseI);
1113058d3cecSKrzysztof Parzyszek     else
1114058d3cecSKrzysztof Parzyszek       visitBranchesFrom(UseI);
1115058d3cecSKrzysztof Parzyszek   }
1116058d3cecSKrzysztof Parzyszek }
1117058d3cecSKrzysztof Parzyszek 
run()1118058d3cecSKrzysztof Parzyszek void BT::run() {
1119058d3cecSKrzysztof Parzyszek   reset();
1120058d3cecSKrzysztof Parzyszek   assert(FlowQ.empty());
1121058d3cecSKrzysztof Parzyszek 
1122058d3cecSKrzysztof Parzyszek   using MachineFlowGraphTraits = GraphTraits<const MachineFunction*>;
1123058d3cecSKrzysztof Parzyszek   const MachineBasicBlock *Entry = MachineFlowGraphTraits::getEntryNode(&MF);
1124058d3cecSKrzysztof Parzyszek 
1125058d3cecSKrzysztof Parzyszek   unsigned MaxBN = 0;
1126058d3cecSKrzysztof Parzyszek   for (const MachineBasicBlock &B : MF) {
1127058d3cecSKrzysztof Parzyszek     assert(B.getNumber() >= 0 && "Disconnected block");
1128058d3cecSKrzysztof Parzyszek     unsigned BN = B.getNumber();
1129058d3cecSKrzysztof Parzyszek     if (BN > MaxBN)
1130058d3cecSKrzysztof Parzyszek       MaxBN = BN;
1131058d3cecSKrzysztof Parzyszek   }
1132058d3cecSKrzysztof Parzyszek 
1133058d3cecSKrzysztof Parzyszek   // Keep track of visited blocks.
1134058d3cecSKrzysztof Parzyszek   BitVector BlockScanned(MaxBN+1);
1135058d3cecSKrzysztof Parzyszek 
1136058d3cecSKrzysztof Parzyszek   int EntryN = Entry->getNumber();
1137058d3cecSKrzysztof Parzyszek   // Generate a fake edge to get something to start with.
1138058d3cecSKrzysztof Parzyszek   FlowQ.push(CFGEdge(-1, EntryN));
1139058d3cecSKrzysztof Parzyszek 
1140058d3cecSKrzysztof Parzyszek   while (!FlowQ.empty() || !UseQ.empty()) {
1141058d3cecSKrzysztof Parzyszek     runEdgeQueue(BlockScanned);
1142058d3cecSKrzysztof Parzyszek     runUseQueue();
1143058d3cecSKrzysztof Parzyszek   }
1144e3ef6e07SKrzysztof Parzyszek   UseQ.reset();
1145e53b31a5SKrzysztof Parzyszek 
1146623afbdbSKrzysztof Parzyszek   if (Trace)
1147623afbdbSKrzysztof Parzyszek     print_cells(dbgs() << "Cells after propagation:\n");
1148e53b31a5SKrzysztof Parzyszek }
1149