12cab237bSDimitry 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 212cab237bSDimitry Andric // %2 = ASL %1, 1 222cab237bSDimitry Andric // assuming that nothing is known about bits of %1, bit 1 of %2 232cab237bSDimitry Andric // will be a "ref" to (%1, 0). If there is a subsequent instruction 242cab237bSDimitry Andric // %3 = ASL %2, 2 252cab237bSDimitry Andric // then bit 3 of %3 will be a "ref" to (%1, 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 292cab237bSDimitry Andric // to self", so for the %1 above, the bit 0 of it will be a "ref" to 302cab237bSDimitry Andric // (%1, 0), bit 1 will be a "ref" to (%1, 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 568e0f8b8cSDimitry Andric #include "BitTracker.h" 578e0f8b8cSDimitry Andric #include "llvm/ADT/APInt.h" 588e0f8b8cSDimitry Andric #include "llvm/ADT/BitVector.h" 59875ed548SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 60875ed548SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 61875ed548SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 628e0f8b8cSDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 63875ed548SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 642cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 65875ed548SDimitry Andric #include "llvm/IR/Constants.h" 66875ed548SDimitry Andric #include "llvm/Support/Debug.h" 67875ed548SDimitry Andric #include "llvm/Support/raw_ostream.h" 688e0f8b8cSDimitry Andric #include <cassert> 698e0f8b8cSDimitry Andric #include <cstdint> 70db17bf38SDimitry Andric #include <iterator> 71875ed548SDimitry Andric 72875ed548SDimitry Andric using namespace llvm; 73875ed548SDimitry Andric 742cab237bSDimitry Andric using BT = BitTracker; 75875ed548SDimitry Andric 76875ed548SDimitry Andric namespace { 778e0f8b8cSDimitry Andric 782cab237bSDimitry Andric // Local trickery to pretty print a register (without the whole "%number" 79875ed548SDimitry Andric // business). 80875ed548SDimitry Andric struct printv { 81875ed548SDimitry Andric printv(unsigned r) : R(r) {} 828e0f8b8cSDimitry Andric 83875ed548SDimitry Andric unsigned R; 84875ed548SDimitry Andric }; 858e0f8b8cSDimitry Andric 86875ed548SDimitry Andric raw_ostream &operator<< (raw_ostream &OS, const printv &PV) { 87875ed548SDimitry Andric if (PV.R) 88875ed548SDimitry Andric OS << 'v' << TargetRegisterInfo::virtReg2Index(PV.R); 89875ed548SDimitry Andric else 90875ed548SDimitry Andric OS << 's'; 91875ed548SDimitry Andric return OS; 92875ed548SDimitry Andric } 938e0f8b8cSDimitry Andric 948e0f8b8cSDimitry Andric } // end anonymous namespace 95875ed548SDimitry Andric 963ca95b02SDimitry Andric namespace llvm { 978e0f8b8cSDimitry Andric 983ca95b02SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const BT::BitValue &BV) { 99875ed548SDimitry Andric switch (BV.Type) { 100875ed548SDimitry Andric case BT::BitValue::Top: 101875ed548SDimitry Andric OS << 'T'; 102875ed548SDimitry Andric break; 103875ed548SDimitry Andric case BT::BitValue::Zero: 104875ed548SDimitry Andric OS << '0'; 105875ed548SDimitry Andric break; 106875ed548SDimitry Andric case BT::BitValue::One: 107875ed548SDimitry Andric OS << '1'; 108875ed548SDimitry Andric break; 109875ed548SDimitry Andric case BT::BitValue::Ref: 110875ed548SDimitry Andric OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']'; 111875ed548SDimitry Andric break; 112875ed548SDimitry Andric } 113875ed548SDimitry Andric return OS; 114875ed548SDimitry Andric } 115875ed548SDimitry Andric 1163ca95b02SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const BT::RegisterCell &RC) { 117875ed548SDimitry Andric unsigned n = RC.Bits.size(); 118875ed548SDimitry Andric OS << "{ w:" << n; 119875ed548SDimitry Andric // Instead of printing each bit value individually, try to group them 120875ed548SDimitry Andric // into logical segments, such as sequences of 0 or 1 bits or references 121875ed548SDimitry Andric // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz"). 122875ed548SDimitry Andric // "Start" will be the index of the beginning of the most recent segment. 123875ed548SDimitry Andric unsigned Start = 0; 124875ed548SDimitry Andric bool SeqRef = false; // A sequence of refs to consecutive bits. 125875ed548SDimitry Andric bool ConstRef = false; // A sequence of refs to the same bit. 126875ed548SDimitry Andric 127875ed548SDimitry Andric for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) { 128875ed548SDimitry Andric const BT::BitValue &V = RC[i]; 129875ed548SDimitry Andric const BT::BitValue &SV = RC[Start]; 130875ed548SDimitry Andric bool IsRef = (V.Type == BT::BitValue::Ref); 131875ed548SDimitry Andric // If the current value is the same as Start, skip to the next one. 132875ed548SDimitry Andric if (!IsRef && V == SV) 133875ed548SDimitry Andric continue; 134875ed548SDimitry Andric if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) { 135875ed548SDimitry Andric if (Start+1 == i) { 136875ed548SDimitry Andric SeqRef = (V.RefI.Pos == SV.RefI.Pos+1); 137875ed548SDimitry Andric ConstRef = (V.RefI.Pos == SV.RefI.Pos); 138875ed548SDimitry Andric } 139875ed548SDimitry Andric if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start)) 140875ed548SDimitry Andric continue; 141875ed548SDimitry Andric if (ConstRef && V.RefI.Pos == SV.RefI.Pos) 142875ed548SDimitry Andric continue; 143875ed548SDimitry Andric } 144875ed548SDimitry Andric 145875ed548SDimitry Andric // The current value is different. Print the previous one and reset 146875ed548SDimitry Andric // the Start. 147875ed548SDimitry Andric OS << " [" << Start; 148875ed548SDimitry Andric unsigned Count = i - Start; 149875ed548SDimitry Andric if (Count == 1) { 150875ed548SDimitry Andric OS << "]:" << SV; 151875ed548SDimitry Andric } else { 152875ed548SDimitry Andric OS << '-' << i-1 << "]:"; 153875ed548SDimitry Andric if (SV.Type == BT::BitValue::Ref && SeqRef) 154875ed548SDimitry Andric OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-' 155875ed548SDimitry Andric << SV.RefI.Pos+(Count-1) << ']'; 156875ed548SDimitry Andric else 157875ed548SDimitry Andric OS << SV; 158875ed548SDimitry Andric } 159875ed548SDimitry Andric Start = i; 160875ed548SDimitry Andric SeqRef = ConstRef = false; 161875ed548SDimitry Andric } 162875ed548SDimitry Andric 163875ed548SDimitry Andric OS << " [" << Start; 164875ed548SDimitry Andric unsigned Count = n - Start; 165875ed548SDimitry Andric if (n-Start == 1) { 166875ed548SDimitry Andric OS << "]:" << RC[Start]; 167875ed548SDimitry Andric } else { 168875ed548SDimitry Andric OS << '-' << n-1 << "]:"; 169875ed548SDimitry Andric const BT::BitValue &SV = RC[Start]; 170875ed548SDimitry Andric if (SV.Type == BT::BitValue::Ref && SeqRef) 171875ed548SDimitry Andric OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-' 172875ed548SDimitry Andric << SV.RefI.Pos+(Count-1) << ']'; 173875ed548SDimitry Andric else 174875ed548SDimitry Andric OS << SV; 175875ed548SDimitry Andric } 176875ed548SDimitry Andric OS << " }"; 177875ed548SDimitry Andric 178875ed548SDimitry Andric return OS; 179875ed548SDimitry Andric } 1808e0f8b8cSDimitry Andric 1818e0f8b8cSDimitry Andric } // end namespace llvm 182875ed548SDimitry Andric 183d88c1a5aSDimitry Andric void BitTracker::print_cells(raw_ostream &OS) const { 1842cab237bSDimitry Andric for (const std::pair<unsigned, RegisterCell> P : Map) 1852cab237bSDimitry Andric dbgs() << printReg(P.first, &ME.TRI) << " -> " << P.second << "\n"; 186d88c1a5aSDimitry Andric } 187d88c1a5aSDimitry Andric 188875ed548SDimitry Andric BitTracker::BitTracker(const MachineEvaluator &E, MachineFunction &F) 1892cab237bSDimitry Andric : ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType), Trace(false) { 1902cab237bSDimitry Andric } 191875ed548SDimitry Andric 192875ed548SDimitry Andric BitTracker::~BitTracker() { 193875ed548SDimitry Andric delete ⤅ 194875ed548SDimitry Andric } 195875ed548SDimitry Andric 196875ed548SDimitry Andric // If we were allowed to update a cell for a part of a register, the meet 197875ed548SDimitry Andric // operation would need to be parametrized by the register number and the 198875ed548SDimitry Andric // exact part of the register, so that the computer BitRefs correspond to 199875ed548SDimitry Andric // the actual bits of the "self" register. 200875ed548SDimitry Andric // While this cannot happen in the current implementation, I'm not sure 201875ed548SDimitry Andric // if this should be ruled out in the future. 202875ed548SDimitry Andric bool BT::RegisterCell::meet(const RegisterCell &RC, unsigned SelfR) { 203875ed548SDimitry Andric // An example when "meet" can be invoked with SelfR == 0 is a phi node 204875ed548SDimitry Andric // with a physical register as an operand. 205875ed548SDimitry Andric assert(SelfR == 0 || TargetRegisterInfo::isVirtualRegister(SelfR)); 206875ed548SDimitry Andric bool Changed = false; 207875ed548SDimitry Andric for (uint16_t i = 0, n = Bits.size(); i < n; ++i) { 208875ed548SDimitry Andric const BitValue &RCV = RC[i]; 209875ed548SDimitry Andric Changed |= Bits[i].meet(RCV, BitRef(SelfR, i)); 210875ed548SDimitry Andric } 211875ed548SDimitry Andric return Changed; 212875ed548SDimitry Andric } 213875ed548SDimitry Andric 214875ed548SDimitry Andric // Insert the entire cell RC into the current cell at position given by M. 215875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC, 216875ed548SDimitry Andric const BitMask &M) { 217875ed548SDimitry Andric uint16_t B = M.first(), E = M.last(), W = width(); 218875ed548SDimitry Andric // Sanity: M must be a valid mask for *this. 219875ed548SDimitry Andric assert(B < W && E < W); 220875ed548SDimitry Andric // Sanity: the masked part of *this must have the same number of bits 221875ed548SDimitry Andric // as the source. 222875ed548SDimitry Andric assert(B > E || E-B+1 == RC.width()); // B <= E => E-B+1 = |RC|. 223875ed548SDimitry Andric assert(B <= E || E+(W-B)+1 == RC.width()); // E < B => E+(W-B)+1 = |RC|. 224875ed548SDimitry Andric if (B <= E) { 225875ed548SDimitry Andric for (uint16_t i = 0; i <= E-B; ++i) 226875ed548SDimitry Andric Bits[i+B] = RC[i]; 227875ed548SDimitry Andric } else { 228875ed548SDimitry Andric for (uint16_t i = 0; i < W-B; ++i) 229875ed548SDimitry Andric Bits[i+B] = RC[i]; 230875ed548SDimitry Andric for (uint16_t i = 0; i <= E; ++i) 231875ed548SDimitry Andric Bits[i] = RC[i+(W-B)]; 232875ed548SDimitry Andric } 233875ed548SDimitry Andric return *this; 234875ed548SDimitry Andric } 235875ed548SDimitry Andric 236875ed548SDimitry Andric BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const { 237875ed548SDimitry Andric uint16_t B = M.first(), E = M.last(), W = width(); 238875ed548SDimitry Andric assert(B < W && E < W); 239875ed548SDimitry Andric if (B <= E) { 240875ed548SDimitry Andric RegisterCell RC(E-B+1); 241875ed548SDimitry Andric for (uint16_t i = B; i <= E; ++i) 242875ed548SDimitry Andric RC.Bits[i-B] = Bits[i]; 243875ed548SDimitry Andric return RC; 244875ed548SDimitry Andric } 245875ed548SDimitry Andric 246875ed548SDimitry Andric RegisterCell RC(E+(W-B)+1); 247875ed548SDimitry Andric for (uint16_t i = 0; i < W-B; ++i) 248875ed548SDimitry Andric RC.Bits[i] = Bits[i+B]; 249875ed548SDimitry Andric for (uint16_t i = 0; i <= E; ++i) 250875ed548SDimitry Andric RC.Bits[i+(W-B)] = Bits[i]; 251875ed548SDimitry Andric return RC; 252875ed548SDimitry Andric } 253875ed548SDimitry Andric 254875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) { 255875ed548SDimitry Andric // Rotate left (i.e. towards increasing bit indices). 256875ed548SDimitry Andric // Swap the two parts: [0..W-Sh-1] [W-Sh..W-1] 257875ed548SDimitry Andric uint16_t W = width(); 258875ed548SDimitry Andric Sh = Sh % W; 259875ed548SDimitry Andric if (Sh == 0) 260875ed548SDimitry Andric return *this; 261875ed548SDimitry Andric 262875ed548SDimitry Andric RegisterCell Tmp(W-Sh); 263875ed548SDimitry Andric // Tmp = [0..W-Sh-1]. 264875ed548SDimitry Andric for (uint16_t i = 0; i < W-Sh; ++i) 265875ed548SDimitry Andric Tmp[i] = Bits[i]; 266875ed548SDimitry Andric // Shift [W-Sh..W-1] to [0..Sh-1]. 267875ed548SDimitry Andric for (uint16_t i = 0; i < Sh; ++i) 268875ed548SDimitry Andric Bits[i] = Bits[W-Sh+i]; 269875ed548SDimitry Andric // Copy Tmp to [Sh..W-1]. 270875ed548SDimitry Andric for (uint16_t i = 0; i < W-Sh; ++i) 271875ed548SDimitry Andric Bits[i+Sh] = Tmp.Bits[i]; 272875ed548SDimitry Andric return *this; 273875ed548SDimitry Andric } 274875ed548SDimitry Andric 275875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E, 276875ed548SDimitry Andric const BitValue &V) { 277875ed548SDimitry Andric assert(B <= E); 278875ed548SDimitry Andric while (B < E) 279875ed548SDimitry Andric Bits[B++] = V; 280875ed548SDimitry Andric return *this; 281875ed548SDimitry Andric } 282875ed548SDimitry Andric 283875ed548SDimitry Andric BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) { 284875ed548SDimitry Andric // Append the cell given as the argument to the "this" cell. 285875ed548SDimitry Andric // Bit 0 of RC becomes bit W of the result, where W is this->width(). 286875ed548SDimitry Andric uint16_t W = width(), WRC = RC.width(); 287875ed548SDimitry Andric Bits.resize(W+WRC); 288875ed548SDimitry Andric for (uint16_t i = 0; i < WRC; ++i) 289875ed548SDimitry Andric Bits[i+W] = RC.Bits[i]; 290875ed548SDimitry Andric return *this; 291875ed548SDimitry Andric } 292875ed548SDimitry Andric 293875ed548SDimitry Andric uint16_t BT::RegisterCell::ct(bool B) const { 294875ed548SDimitry Andric uint16_t W = width(); 295875ed548SDimitry Andric uint16_t C = 0; 296875ed548SDimitry Andric BitValue V = B; 297875ed548SDimitry Andric while (C < W && Bits[C] == V) 298875ed548SDimitry Andric C++; 299875ed548SDimitry Andric return C; 300875ed548SDimitry Andric } 301875ed548SDimitry Andric 302875ed548SDimitry Andric uint16_t BT::RegisterCell::cl(bool B) const { 303875ed548SDimitry Andric uint16_t W = width(); 304875ed548SDimitry Andric uint16_t C = 0; 305875ed548SDimitry Andric BitValue V = B; 306875ed548SDimitry Andric while (C < W && Bits[W-(C+1)] == V) 307875ed548SDimitry Andric C++; 308875ed548SDimitry Andric return C; 309875ed548SDimitry Andric } 310875ed548SDimitry Andric 311875ed548SDimitry Andric bool BT::RegisterCell::operator== (const RegisterCell &RC) const { 312875ed548SDimitry Andric uint16_t W = Bits.size(); 313875ed548SDimitry Andric if (RC.Bits.size() != W) 314875ed548SDimitry Andric return false; 315875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) 316875ed548SDimitry Andric if (Bits[i] != RC[i]) 317875ed548SDimitry Andric return false; 318875ed548SDimitry Andric return true; 319875ed548SDimitry Andric } 320875ed548SDimitry Andric 3217a7e6055SDimitry Andric BT::RegisterCell &BT::RegisterCell::regify(unsigned R) { 3227a7e6055SDimitry Andric for (unsigned i = 0, n = width(); i < n; ++i) { 3237a7e6055SDimitry Andric const BitValue &V = Bits[i]; 3247a7e6055SDimitry Andric if (V.Type == BitValue::Ref && V.RefI.Reg == 0) 3257a7e6055SDimitry Andric Bits[i].RefI = BitRef(R, i); 3267a7e6055SDimitry Andric } 3277a7e6055SDimitry Andric return *this; 3287a7e6055SDimitry Andric } 3297a7e6055SDimitry Andric 330875ed548SDimitry Andric uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const { 331875ed548SDimitry Andric // The general problem is with finding a register class that corresponds 332875ed548SDimitry Andric // to a given reference reg:sub. There can be several such classes, and 333875ed548SDimitry Andric // since we only care about the register size, it does not matter which 334875ed548SDimitry Andric // such class we would find. 335875ed548SDimitry Andric // The easiest way to accomplish what we want is to 336875ed548SDimitry Andric // 1. find a physical register PhysR from the same class as RR.Reg, 337875ed548SDimitry Andric // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub, 338875ed548SDimitry Andric // 3. find a register class that contains PhysS. 339875ed548SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) { 3402cab237bSDimitry Andric const auto &VC = composeWithSubRegIndex(*MRI.getRegClass(RR.Reg), RR.Sub); 3412cab237bSDimitry Andric return TRI.getRegSizeInBits(VC); 342875ed548SDimitry Andric } 3432cab237bSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg)); 3442cab237bSDimitry Andric unsigned PhysR = (RR.Sub == 0) ? RR.Reg : TRI.getSubReg(RR.Reg, RR.Sub); 3452cab237bSDimitry Andric return getPhysRegBitWidth(PhysR); 346875ed548SDimitry Andric } 347875ed548SDimitry Andric 348875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR, 349875ed548SDimitry Andric const CellMapType &M) const { 350875ed548SDimitry Andric uint16_t BW = getRegBitWidth(RR); 351875ed548SDimitry Andric 352875ed548SDimitry Andric // Physical registers are assumed to be present in the map with an unknown 353875ed548SDimitry Andric // value. Don't actually insert anything in the map, just return the cell. 354875ed548SDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(RR.Reg)) 355875ed548SDimitry Andric return RegisterCell::self(0, BW); 356875ed548SDimitry Andric 357875ed548SDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(RR.Reg)); 358875ed548SDimitry Andric // For virtual registers that belong to a class that is not tracked, 359875ed548SDimitry Andric // generate an "unknown" value as well. 360875ed548SDimitry Andric const TargetRegisterClass *C = MRI.getRegClass(RR.Reg); 361875ed548SDimitry Andric if (!track(C)) 362875ed548SDimitry Andric return RegisterCell::self(0, BW); 363875ed548SDimitry Andric 364875ed548SDimitry Andric CellMapType::const_iterator F = M.find(RR.Reg); 365875ed548SDimitry Andric if (F != M.end()) { 366875ed548SDimitry Andric if (!RR.Sub) 367875ed548SDimitry Andric return F->second; 368875ed548SDimitry Andric BitMask M = mask(RR.Reg, RR.Sub); 369875ed548SDimitry Andric return F->second.extract(M); 370875ed548SDimitry Andric } 371875ed548SDimitry Andric // If not found, create a "top" entry, but do not insert it in the map. 372875ed548SDimitry Andric return RegisterCell::top(BW); 373875ed548SDimitry Andric } 374875ed548SDimitry Andric 375875ed548SDimitry Andric void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC, 376875ed548SDimitry Andric CellMapType &M) const { 377875ed548SDimitry Andric // While updating the cell map can be done in a meaningful way for 378875ed548SDimitry Andric // a part of a register, it makes little sense to implement it as the 379875ed548SDimitry Andric // SSA representation would never contain such "partial definitions". 380875ed548SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(RR.Reg)) 381875ed548SDimitry Andric return; 382875ed548SDimitry Andric assert(RR.Sub == 0 && "Unexpected sub-register in definition"); 383875ed548SDimitry Andric // Eliminate all ref-to-reg-0 bit values: replace them with "self". 3847a7e6055SDimitry Andric M[RR.Reg] = RC.regify(RR.Reg); 385875ed548SDimitry Andric } 386875ed548SDimitry Andric 387875ed548SDimitry Andric // Check if the cell represents a compile-time integer value. 388875ed548SDimitry Andric bool BT::MachineEvaluator::isInt(const RegisterCell &A) const { 389875ed548SDimitry Andric uint16_t W = A.width(); 390875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) 391875ed548SDimitry Andric if (!A[i].is(0) && !A[i].is(1)) 392875ed548SDimitry Andric return false; 393875ed548SDimitry Andric return true; 394875ed548SDimitry Andric } 395875ed548SDimitry Andric 396875ed548SDimitry Andric // Convert a cell to the integer value. The result must fit in uint64_t. 397875ed548SDimitry Andric uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const { 398875ed548SDimitry Andric assert(isInt(A)); 399875ed548SDimitry Andric uint64_t Val = 0; 400875ed548SDimitry Andric uint16_t W = A.width(); 401875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 402875ed548SDimitry Andric Val <<= 1; 403875ed548SDimitry Andric Val |= A[i].is(1); 404875ed548SDimitry Andric } 405875ed548SDimitry Andric return Val; 406875ed548SDimitry Andric } 407875ed548SDimitry Andric 408875ed548SDimitry Andric // Evaluator helper functions. These implement some common operation on 409875ed548SDimitry Andric // register cells that can be used to implement target-specific instructions 410875ed548SDimitry Andric // in a target-specific evaluator. 411875ed548SDimitry Andric 412875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const { 413875ed548SDimitry Andric RegisterCell Res(W); 414875ed548SDimitry Andric // For bits beyond the 63rd, this will generate the sign bit of V. 415875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 416875ed548SDimitry Andric Res[i] = BitValue(V & 1); 417875ed548SDimitry Andric V >>= 1; 418875ed548SDimitry Andric } 419875ed548SDimitry Andric return Res; 420875ed548SDimitry Andric } 421875ed548SDimitry Andric 422875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const { 4233ca95b02SDimitry Andric const 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 BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1, 433875ed548SDimitry Andric const RegisterCell &A2) const { 434875ed548SDimitry Andric uint16_t W = A1.width(); 435875ed548SDimitry Andric assert(W == A2.width()); 436875ed548SDimitry Andric RegisterCell Res(W); 437875ed548SDimitry Andric bool Carry = false; 438875ed548SDimitry Andric uint16_t I; 439875ed548SDimitry Andric for (I = 0; I < W; ++I) { 440875ed548SDimitry Andric const BitValue &V1 = A1[I]; 441875ed548SDimitry Andric const BitValue &V2 = A2[I]; 442875ed548SDimitry Andric if (!V1.num() || !V2.num()) 443875ed548SDimitry Andric break; 444875ed548SDimitry Andric unsigned S = bool(V1) + bool(V2) + Carry; 445875ed548SDimitry Andric Res[I] = BitValue(S & 1); 446875ed548SDimitry Andric Carry = (S > 1); 447875ed548SDimitry Andric } 448875ed548SDimitry Andric for (; I < W; ++I) { 449875ed548SDimitry Andric const BitValue &V1 = A1[I]; 450875ed548SDimitry Andric const BitValue &V2 = A2[I]; 451875ed548SDimitry Andric // If the next bit is same as Carry, the result will be 0 plus the 452875ed548SDimitry Andric // other bit. The Carry bit will remain unchanged. 453875ed548SDimitry Andric if (V1.is(Carry)) 454875ed548SDimitry Andric Res[I] = BitValue::ref(V2); 455875ed548SDimitry Andric else if (V2.is(Carry)) 456875ed548SDimitry Andric Res[I] = BitValue::ref(V1); 457875ed548SDimitry Andric else 458875ed548SDimitry Andric break; 459875ed548SDimitry Andric } 460875ed548SDimitry Andric for (; I < W; ++I) 461875ed548SDimitry Andric Res[I] = BitValue::self(); 462875ed548SDimitry Andric return Res; 463875ed548SDimitry Andric } 464875ed548SDimitry Andric 465875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1, 466875ed548SDimitry Andric const RegisterCell &A2) const { 467875ed548SDimitry Andric uint16_t W = A1.width(); 468875ed548SDimitry Andric assert(W == A2.width()); 469875ed548SDimitry Andric RegisterCell Res(W); 470875ed548SDimitry Andric bool Borrow = false; 471875ed548SDimitry Andric uint16_t I; 472875ed548SDimitry Andric for (I = 0; I < W; ++I) { 473875ed548SDimitry Andric const BitValue &V1 = A1[I]; 474875ed548SDimitry Andric const BitValue &V2 = A2[I]; 475875ed548SDimitry Andric if (!V1.num() || !V2.num()) 476875ed548SDimitry Andric break; 477875ed548SDimitry Andric unsigned S = bool(V1) - bool(V2) - Borrow; 478875ed548SDimitry Andric Res[I] = BitValue(S & 1); 479875ed548SDimitry Andric Borrow = (S > 1); 480875ed548SDimitry Andric } 481875ed548SDimitry Andric for (; I < W; ++I) { 482875ed548SDimitry Andric const BitValue &V1 = A1[I]; 483875ed548SDimitry Andric const BitValue &V2 = A2[I]; 484875ed548SDimitry Andric if (V1.is(Borrow)) { 485875ed548SDimitry Andric Res[I] = BitValue::ref(V2); 486875ed548SDimitry Andric break; 487875ed548SDimitry Andric } 488875ed548SDimitry Andric if (V2.is(Borrow)) 489875ed548SDimitry Andric Res[I] = BitValue::ref(V1); 490875ed548SDimitry Andric else 491875ed548SDimitry Andric break; 492875ed548SDimitry Andric } 493875ed548SDimitry Andric for (; I < W; ++I) 494875ed548SDimitry Andric Res[I] = BitValue::self(); 495875ed548SDimitry Andric return Res; 496875ed548SDimitry Andric } 497875ed548SDimitry Andric 498875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1, 499875ed548SDimitry Andric const RegisterCell &A2) const { 500875ed548SDimitry Andric uint16_t W = A1.width() + A2.width(); 5018e0f8b8cSDimitry Andric uint16_t Z = A1.ct(false) + A2.ct(false); 502875ed548SDimitry Andric RegisterCell Res(W); 503875ed548SDimitry Andric Res.fill(0, Z, BitValue::Zero); 504875ed548SDimitry Andric Res.fill(Z, W, BitValue::self()); 505875ed548SDimitry Andric return Res; 506875ed548SDimitry Andric } 507875ed548SDimitry Andric 508875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1, 509875ed548SDimitry Andric const RegisterCell &A2) const { 510875ed548SDimitry Andric uint16_t W = A1.width() + A2.width(); 5118e0f8b8cSDimitry Andric uint16_t Z = A1.ct(false) + A2.ct(false); 512875ed548SDimitry Andric RegisterCell Res(W); 513875ed548SDimitry Andric Res.fill(0, Z, BitValue::Zero); 514875ed548SDimitry Andric Res.fill(Z, W, BitValue::self()); 515875ed548SDimitry Andric return Res; 516875ed548SDimitry Andric } 517875ed548SDimitry Andric 518875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1, 519875ed548SDimitry Andric uint16_t Sh) const { 520875ed548SDimitry Andric assert(Sh <= A1.width()); 521875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 522875ed548SDimitry Andric Res.rol(Sh); 523875ed548SDimitry Andric Res.fill(0, Sh, BitValue::Zero); 524875ed548SDimitry Andric return Res; 525875ed548SDimitry Andric } 526875ed548SDimitry Andric 527875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1, 528875ed548SDimitry Andric uint16_t Sh) const { 529875ed548SDimitry Andric uint16_t W = A1.width(); 530875ed548SDimitry Andric assert(Sh <= W); 531875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 532875ed548SDimitry Andric Res.rol(W-Sh); 533875ed548SDimitry Andric Res.fill(W-Sh, W, BitValue::Zero); 534875ed548SDimitry Andric return Res; 535875ed548SDimitry Andric } 536875ed548SDimitry Andric 537875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1, 538875ed548SDimitry Andric uint16_t Sh) const { 539875ed548SDimitry Andric uint16_t W = A1.width(); 540875ed548SDimitry Andric assert(Sh <= W); 541875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 542875ed548SDimitry Andric BitValue Sign = Res[W-1]; 543875ed548SDimitry Andric Res.rol(W-Sh); 544875ed548SDimitry Andric Res.fill(W-Sh, W, Sign); 545875ed548SDimitry Andric return Res; 546875ed548SDimitry Andric } 547875ed548SDimitry Andric 548875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1, 549875ed548SDimitry Andric const RegisterCell &A2) const { 550875ed548SDimitry Andric uint16_t W = A1.width(); 551875ed548SDimitry Andric assert(W == A2.width()); 552875ed548SDimitry Andric RegisterCell Res(W); 553875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 554875ed548SDimitry Andric const BitValue &V1 = A1[i]; 555875ed548SDimitry Andric const BitValue &V2 = A2[i]; 556875ed548SDimitry Andric if (V1.is(1)) 557875ed548SDimitry Andric Res[i] = BitValue::ref(V2); 558875ed548SDimitry Andric else if (V2.is(1)) 559875ed548SDimitry Andric Res[i] = BitValue::ref(V1); 560875ed548SDimitry Andric else if (V1.is(0) || V2.is(0)) 561875ed548SDimitry Andric Res[i] = BitValue::Zero; 562875ed548SDimitry Andric else if (V1 == V2) 563875ed548SDimitry Andric Res[i] = V1; 564875ed548SDimitry Andric else 565875ed548SDimitry Andric Res[i] = BitValue::self(); 566875ed548SDimitry Andric } 567875ed548SDimitry Andric return Res; 568875ed548SDimitry Andric } 569875ed548SDimitry Andric 570875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1, 571875ed548SDimitry Andric const RegisterCell &A2) const { 572875ed548SDimitry Andric uint16_t W = A1.width(); 573875ed548SDimitry Andric assert(W == A2.width()); 574875ed548SDimitry Andric RegisterCell Res(W); 575875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 576875ed548SDimitry Andric const BitValue &V1 = A1[i]; 577875ed548SDimitry Andric const BitValue &V2 = A2[i]; 578875ed548SDimitry Andric if (V1.is(1) || V2.is(1)) 579875ed548SDimitry Andric Res[i] = BitValue::One; 580875ed548SDimitry Andric else if (V1.is(0)) 581875ed548SDimitry Andric Res[i] = BitValue::ref(V2); 582875ed548SDimitry Andric else if (V2.is(0)) 583875ed548SDimitry Andric Res[i] = BitValue::ref(V1); 584875ed548SDimitry Andric else if (V1 == V2) 585875ed548SDimitry Andric Res[i] = V1; 586875ed548SDimitry Andric else 587875ed548SDimitry Andric Res[i] = BitValue::self(); 588875ed548SDimitry Andric } 589875ed548SDimitry Andric return Res; 590875ed548SDimitry Andric } 591875ed548SDimitry Andric 592875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1, 593875ed548SDimitry Andric const RegisterCell &A2) const { 594875ed548SDimitry Andric uint16_t W = A1.width(); 595875ed548SDimitry Andric assert(W == A2.width()); 596875ed548SDimitry Andric RegisterCell Res(W); 597875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 598875ed548SDimitry Andric const BitValue &V1 = A1[i]; 599875ed548SDimitry Andric const BitValue &V2 = A2[i]; 600875ed548SDimitry Andric if (V1.is(0)) 601875ed548SDimitry Andric Res[i] = BitValue::ref(V2); 602875ed548SDimitry Andric else if (V2.is(0)) 603875ed548SDimitry Andric Res[i] = BitValue::ref(V1); 604875ed548SDimitry Andric else if (V1 == V2) 605875ed548SDimitry Andric Res[i] = BitValue::Zero; 606875ed548SDimitry Andric else 607875ed548SDimitry Andric Res[i] = BitValue::self(); 608875ed548SDimitry Andric } 609875ed548SDimitry Andric return Res; 610875ed548SDimitry Andric } 611875ed548SDimitry Andric 612875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const { 613875ed548SDimitry Andric uint16_t W = A1.width(); 614875ed548SDimitry Andric RegisterCell Res(W); 615875ed548SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 616875ed548SDimitry Andric const BitValue &V = A1[i]; 617875ed548SDimitry Andric if (V.is(0)) 618875ed548SDimitry Andric Res[i] = BitValue::One; 619875ed548SDimitry Andric else if (V.is(1)) 620875ed548SDimitry Andric Res[i] = BitValue::Zero; 621875ed548SDimitry Andric else 622875ed548SDimitry Andric Res[i] = BitValue::self(); 623875ed548SDimitry Andric } 624875ed548SDimitry Andric return Res; 625875ed548SDimitry Andric } 626875ed548SDimitry Andric 627875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1, 628875ed548SDimitry Andric uint16_t BitN) const { 629875ed548SDimitry Andric assert(BitN < A1.width()); 630875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 631875ed548SDimitry Andric Res[BitN] = BitValue::One; 632875ed548SDimitry Andric return Res; 633875ed548SDimitry Andric } 634875ed548SDimitry Andric 635875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1, 636875ed548SDimitry Andric uint16_t BitN) const { 637875ed548SDimitry Andric assert(BitN < A1.width()); 638875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 639875ed548SDimitry Andric Res[BitN] = BitValue::Zero; 640875ed548SDimitry Andric return Res; 641875ed548SDimitry Andric } 642875ed548SDimitry Andric 643875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B, 644875ed548SDimitry Andric uint16_t W) const { 645875ed548SDimitry Andric uint16_t C = A1.cl(B), AW = A1.width(); 646875ed548SDimitry Andric // If the last leading non-B bit is not a constant, then we don't know 647875ed548SDimitry Andric // the real count. 648875ed548SDimitry Andric if ((C < AW && A1[AW-1-C].num()) || C == AW) 649875ed548SDimitry Andric return eIMM(C, W); 650875ed548SDimitry Andric return RegisterCell::self(0, W); 651875ed548SDimitry Andric } 652875ed548SDimitry Andric 653875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B, 654875ed548SDimitry Andric uint16_t W) const { 655875ed548SDimitry Andric uint16_t C = A1.ct(B), AW = A1.width(); 656875ed548SDimitry Andric // If the last trailing non-B bit is not a constant, then we don't know 657875ed548SDimitry Andric // the real count. 658875ed548SDimitry Andric if ((C < AW && A1[C].num()) || C == AW) 659875ed548SDimitry Andric return eIMM(C, W); 660875ed548SDimitry Andric return RegisterCell::self(0, W); 661875ed548SDimitry Andric } 662875ed548SDimitry Andric 663875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1, 664875ed548SDimitry Andric uint16_t FromN) const { 665875ed548SDimitry Andric uint16_t W = A1.width(); 666875ed548SDimitry Andric assert(FromN <= W); 667875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 668875ed548SDimitry Andric BitValue Sign = Res[FromN-1]; 669875ed548SDimitry Andric // Sign-extend "inreg". 670875ed548SDimitry Andric Res.fill(FromN, W, Sign); 671875ed548SDimitry Andric return Res; 672875ed548SDimitry Andric } 673875ed548SDimitry Andric 674875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1, 675875ed548SDimitry Andric uint16_t FromN) const { 676875ed548SDimitry Andric uint16_t W = A1.width(); 677875ed548SDimitry Andric assert(FromN <= W); 678875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 679875ed548SDimitry Andric Res.fill(FromN, W, BitValue::Zero); 680875ed548SDimitry Andric return Res; 681875ed548SDimitry Andric } 682875ed548SDimitry Andric 683875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1, 684875ed548SDimitry Andric uint16_t B, uint16_t E) const { 685875ed548SDimitry Andric uint16_t W = A1.width(); 686875ed548SDimitry Andric assert(B < W && E <= W); 687875ed548SDimitry Andric if (B == E) 688875ed548SDimitry Andric return RegisterCell(0); 689875ed548SDimitry Andric uint16_t Last = (E > 0) ? E-1 : W-1; 690875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last)); 691875ed548SDimitry Andric // Return shorter cell. 692875ed548SDimitry Andric return Res; 693875ed548SDimitry Andric } 694875ed548SDimitry Andric 695875ed548SDimitry Andric BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1, 696875ed548SDimitry Andric const RegisterCell &A2, uint16_t AtN) const { 697875ed548SDimitry Andric uint16_t W1 = A1.width(), W2 = A2.width(); 698875ed548SDimitry Andric (void)W1; 699875ed548SDimitry Andric assert(AtN < W1 && AtN+W2 <= W1); 700875ed548SDimitry Andric // Copy bits from A1, insert A2 at position AtN. 701875ed548SDimitry Andric RegisterCell Res = RegisterCell::ref(A1); 702875ed548SDimitry Andric if (W2 > 0) 703875ed548SDimitry Andric Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1)); 704875ed548SDimitry Andric return Res; 705875ed548SDimitry Andric } 706875ed548SDimitry Andric 707875ed548SDimitry Andric BT::BitMask BT::MachineEvaluator::mask(unsigned Reg, unsigned Sub) const { 708875ed548SDimitry Andric assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0"); 709875ed548SDimitry Andric uint16_t W = getRegBitWidth(Reg); 710875ed548SDimitry Andric assert(W > 0 && "Cannot generate mask for empty register"); 711875ed548SDimitry Andric return BitMask(0, W-1); 712875ed548SDimitry Andric } 713875ed548SDimitry Andric 7142cab237bSDimitry Andric uint16_t BT::MachineEvaluator::getPhysRegBitWidth(unsigned Reg) const { 7152cab237bSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 7162cab237bSDimitry Andric const TargetRegisterClass &PC = *TRI.getMinimalPhysRegClass(Reg); 7172cab237bSDimitry Andric return TRI.getRegSizeInBits(PC); 7182cab237bSDimitry Andric } 7192cab237bSDimitry Andric 7203ca95b02SDimitry Andric bool BT::MachineEvaluator::evaluate(const MachineInstr &MI, 7213ca95b02SDimitry Andric const CellMapType &Inputs, 7223ca95b02SDimitry Andric CellMapType &Outputs) const { 7233ca95b02SDimitry Andric unsigned Opc = MI.getOpcode(); 724875ed548SDimitry Andric switch (Opc) { 725875ed548SDimitry Andric case TargetOpcode::REG_SEQUENCE: { 7263ca95b02SDimitry Andric RegisterRef RD = MI.getOperand(0); 727875ed548SDimitry Andric assert(RD.Sub == 0); 7283ca95b02SDimitry Andric RegisterRef RS = MI.getOperand(1); 7293ca95b02SDimitry Andric unsigned SS = MI.getOperand(2).getImm(); 7303ca95b02SDimitry Andric RegisterRef RT = MI.getOperand(3); 7313ca95b02SDimitry Andric unsigned ST = MI.getOperand(4).getImm(); 732875ed548SDimitry Andric assert(SS != ST); 733875ed548SDimitry Andric 734875ed548SDimitry Andric uint16_t W = getRegBitWidth(RD); 735875ed548SDimitry Andric RegisterCell Res(W); 736875ed548SDimitry Andric Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS)); 737875ed548SDimitry Andric Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST)); 738875ed548SDimitry Andric putCell(RD, Res, Outputs); 739875ed548SDimitry Andric break; 740875ed548SDimitry Andric } 741875ed548SDimitry Andric 742875ed548SDimitry Andric case TargetOpcode::COPY: { 743875ed548SDimitry Andric // COPY can transfer a smaller register into a wider one. 744875ed548SDimitry Andric // If that is the case, fill the remaining high bits with 0. 7453ca95b02SDimitry Andric RegisterRef RD = MI.getOperand(0); 7463ca95b02SDimitry Andric RegisterRef RS = MI.getOperand(1); 747875ed548SDimitry Andric assert(RD.Sub == 0); 748875ed548SDimitry Andric uint16_t WD = getRegBitWidth(RD); 749875ed548SDimitry Andric uint16_t WS = getRegBitWidth(RS); 750875ed548SDimitry Andric assert(WD >= WS); 751875ed548SDimitry Andric RegisterCell Src = getCell(RS, Inputs); 752875ed548SDimitry Andric RegisterCell Res(WD); 753875ed548SDimitry Andric Res.insert(Src, BitMask(0, WS-1)); 754875ed548SDimitry Andric Res.fill(WS, WD, BitValue::Zero); 755875ed548SDimitry Andric putCell(RD, Res, Outputs); 756875ed548SDimitry Andric break; 757875ed548SDimitry Andric } 758875ed548SDimitry Andric 759875ed548SDimitry Andric default: 760875ed548SDimitry Andric return false; 761875ed548SDimitry Andric } 762875ed548SDimitry Andric 763875ed548SDimitry Andric return true; 764875ed548SDimitry Andric } 765875ed548SDimitry Andric 7662cab237bSDimitry Andric bool BT::UseQueueType::Cmp::operator()(const MachineInstr *InstA, 7672cab237bSDimitry Andric const MachineInstr *InstB) const { 7682cab237bSDimitry Andric // This is a comparison function for a priority queue: give higher priority 7692cab237bSDimitry Andric // to earlier instructions. 7702cab237bSDimitry Andric // This operator is used as "less", so returning "true" gives InstB higher 7712cab237bSDimitry Andric // priority (because then InstA < InstB). 7722cab237bSDimitry Andric if (InstA == InstB) 7732cab237bSDimitry Andric return false; 7742cab237bSDimitry Andric const MachineBasicBlock *BA = InstA->getParent(); 7752cab237bSDimitry Andric const MachineBasicBlock *BB = InstB->getParent(); 7762cab237bSDimitry Andric if (BA != BB) { 7772cab237bSDimitry Andric // If the blocks are different, ideally the dominating block would 7782cab237bSDimitry Andric // have a higher priority, but it may be too expensive to check. 7792cab237bSDimitry Andric return BA->getNumber() > BB->getNumber(); 7802cab237bSDimitry Andric } 7812cab237bSDimitry Andric 7822cab237bSDimitry Andric MachineBasicBlock::const_iterator ItA = InstA->getIterator(); 7832cab237bSDimitry Andric MachineBasicBlock::const_iterator ItB = InstB->getIterator(); 7842cab237bSDimitry Andric MachineBasicBlock::const_iterator End = BA->end(); 7852cab237bSDimitry Andric while (ItA != End) { 7862cab237bSDimitry Andric if (ItA == ItB) 7872cab237bSDimitry Andric return false; // ItA was before ItB. 7882cab237bSDimitry Andric ++ItA; 7892cab237bSDimitry Andric } 7902cab237bSDimitry Andric return true; 7912cab237bSDimitry Andric } 7922cab237bSDimitry Andric 793875ed548SDimitry Andric // Main W-Z implementation. 794875ed548SDimitry Andric 7953ca95b02SDimitry Andric void BT::visitPHI(const MachineInstr &PI) { 7963ca95b02SDimitry Andric int ThisN = PI.getParent()->getNumber(); 797875ed548SDimitry Andric if (Trace) 7982cab237bSDimitry Andric dbgs() << "Visit FI(" << printMBBReference(*PI.getParent()) << "): " << PI; 799875ed548SDimitry Andric 8003ca95b02SDimitry Andric const MachineOperand &MD = PI.getOperand(0); 801875ed548SDimitry Andric assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition"); 802875ed548SDimitry Andric RegisterRef DefRR(MD); 803875ed548SDimitry Andric uint16_t DefBW = ME.getRegBitWidth(DefRR); 804875ed548SDimitry Andric 805875ed548SDimitry Andric RegisterCell DefC = ME.getCell(DefRR, Map); 806875ed548SDimitry Andric if (DefC == RegisterCell::self(DefRR.Reg, DefBW)) // XXX slow 807875ed548SDimitry Andric return; 808875ed548SDimitry Andric 809875ed548SDimitry Andric bool Changed = false; 810875ed548SDimitry Andric 8113ca95b02SDimitry Andric for (unsigned i = 1, n = PI.getNumOperands(); i < n; i += 2) { 8123ca95b02SDimitry Andric const MachineBasicBlock *PB = PI.getOperand(i + 1).getMBB(); 813875ed548SDimitry Andric int PredN = PB->getNumber(); 814875ed548SDimitry Andric if (Trace) 8152cab237bSDimitry Andric dbgs() << " edge " << printMBBReference(*PB) << "->" 8162cab237bSDimitry Andric << printMBBReference(*PI.getParent()); 817875ed548SDimitry Andric if (!EdgeExec.count(CFGEdge(PredN, ThisN))) { 818875ed548SDimitry Andric if (Trace) 819875ed548SDimitry Andric dbgs() << " not executable\n"; 820875ed548SDimitry Andric continue; 821875ed548SDimitry Andric } 822875ed548SDimitry Andric 8233ca95b02SDimitry Andric RegisterRef RU = PI.getOperand(i); 824875ed548SDimitry Andric RegisterCell ResC = ME.getCell(RU, Map); 825875ed548SDimitry Andric if (Trace) 8262cab237bSDimitry Andric dbgs() << " input reg: " << printReg(RU.Reg, &ME.TRI, RU.Sub) 827875ed548SDimitry Andric << " cell: " << ResC << "\n"; 828875ed548SDimitry Andric Changed |= DefC.meet(ResC, DefRR.Reg); 829875ed548SDimitry Andric } 830875ed548SDimitry Andric 831875ed548SDimitry Andric if (Changed) { 832875ed548SDimitry Andric if (Trace) 8332cab237bSDimitry Andric dbgs() << "Output: " << printReg(DefRR.Reg, &ME.TRI, DefRR.Sub) 834875ed548SDimitry Andric << " cell: " << DefC << "\n"; 835875ed548SDimitry Andric ME.putCell(DefRR, DefC, Map); 836875ed548SDimitry Andric visitUsesOf(DefRR.Reg); 837875ed548SDimitry Andric } 838875ed548SDimitry Andric } 839875ed548SDimitry Andric 8403ca95b02SDimitry Andric void BT::visitNonBranch(const MachineInstr &MI) { 8412cab237bSDimitry Andric if (Trace) 8422cab237bSDimitry Andric dbgs() << "Visit MI(" << printMBBReference(*MI.getParent()) << "): " << MI; 8433ca95b02SDimitry Andric if (MI.isDebugValue()) 844875ed548SDimitry Andric return; 8453ca95b02SDimitry Andric assert(!MI.isBranch() && "Unexpected branch instruction"); 846875ed548SDimitry Andric 847875ed548SDimitry Andric CellMapType ResMap; 848875ed548SDimitry Andric bool Eval = ME.evaluate(MI, Map, ResMap); 849875ed548SDimitry Andric 850875ed548SDimitry Andric if (Trace && Eval) { 8513ca95b02SDimitry Andric for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) { 8523ca95b02SDimitry Andric const MachineOperand &MO = MI.getOperand(i); 853875ed548SDimitry Andric if (!MO.isReg() || !MO.isUse()) 854875ed548SDimitry Andric continue; 855875ed548SDimitry Andric RegisterRef RU(MO); 8562cab237bSDimitry Andric dbgs() << " input reg: " << printReg(RU.Reg, &ME.TRI, RU.Sub) 857875ed548SDimitry Andric << " cell: " << ME.getCell(RU, Map) << "\n"; 858875ed548SDimitry Andric } 859875ed548SDimitry Andric dbgs() << "Outputs:\n"; 8602cab237bSDimitry Andric for (const std::pair<unsigned, RegisterCell> &P : ResMap) { 8612cab237bSDimitry Andric RegisterRef RD(P.first); 8622cab237bSDimitry Andric dbgs() << " " << printReg(P.first, &ME.TRI) << " cell: " 863875ed548SDimitry Andric << ME.getCell(RD, ResMap) << "\n"; 864875ed548SDimitry Andric } 865875ed548SDimitry Andric } 866875ed548SDimitry Andric 867875ed548SDimitry Andric // Iterate over all definitions of the instruction, and update the 868875ed548SDimitry Andric // cells accordingly. 8692cab237bSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 870875ed548SDimitry Andric // Visit register defs only. 871875ed548SDimitry Andric if (!MO.isReg() || !MO.isDef()) 872875ed548SDimitry Andric continue; 873875ed548SDimitry Andric RegisterRef RD(MO); 874875ed548SDimitry Andric assert(RD.Sub == 0 && "Unexpected sub-register in definition"); 875875ed548SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(RD.Reg)) 876875ed548SDimitry Andric continue; 877875ed548SDimitry Andric 878875ed548SDimitry Andric bool Changed = false; 8797d523365SDimitry Andric if (!Eval || ResMap.count(RD.Reg) == 0) { 880875ed548SDimitry Andric // Set to "ref" (aka "bottom"). 881875ed548SDimitry Andric uint16_t DefBW = ME.getRegBitWidth(RD); 882875ed548SDimitry Andric RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW); 883875ed548SDimitry Andric if (RefC != ME.getCell(RD, Map)) { 884875ed548SDimitry Andric ME.putCell(RD, RefC, Map); 885875ed548SDimitry Andric Changed = true; 886875ed548SDimitry Andric } 887875ed548SDimitry Andric } else { 888875ed548SDimitry Andric RegisterCell DefC = ME.getCell(RD, Map); 889875ed548SDimitry Andric RegisterCell ResC = ME.getCell(RD, ResMap); 890875ed548SDimitry Andric // This is a non-phi instruction, so the values of the inputs come 891875ed548SDimitry Andric // from the same registers each time this instruction is evaluated. 892875ed548SDimitry Andric // During the propagation, the values of the inputs can become lowered 893875ed548SDimitry Andric // in the sense of the lattice operation, which may cause different 894875ed548SDimitry Andric // results to be calculated in subsequent evaluations. This should 895875ed548SDimitry Andric // not cause the bottoming of the result in the map, since the new 896875ed548SDimitry Andric // result is already reflecting the lowered inputs. 897875ed548SDimitry Andric for (uint16_t i = 0, w = DefC.width(); i < w; ++i) { 898875ed548SDimitry Andric BitValue &V = DefC[i]; 899875ed548SDimitry Andric // Bits that are already "bottom" should not be updated. 900875ed548SDimitry Andric if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg) 901875ed548SDimitry Andric continue; 902875ed548SDimitry Andric // Same for those that are identical in DefC and ResC. 903875ed548SDimitry Andric if (V == ResC[i]) 904875ed548SDimitry Andric continue; 905875ed548SDimitry Andric V = ResC[i]; 906875ed548SDimitry Andric Changed = true; 907875ed548SDimitry Andric } 908875ed548SDimitry Andric if (Changed) 909875ed548SDimitry Andric ME.putCell(RD, DefC, Map); 910875ed548SDimitry Andric } 911875ed548SDimitry Andric if (Changed) 912875ed548SDimitry Andric visitUsesOf(RD.Reg); 913875ed548SDimitry Andric } 914875ed548SDimitry Andric } 915875ed548SDimitry Andric 9163ca95b02SDimitry Andric void BT::visitBranchesFrom(const MachineInstr &BI) { 9173ca95b02SDimitry Andric const MachineBasicBlock &B = *BI.getParent(); 918875ed548SDimitry Andric MachineBasicBlock::const_iterator It = BI, End = B.end(); 919875ed548SDimitry Andric BranchTargetList Targets, BTs; 920875ed548SDimitry Andric bool FallsThrough = true, DefaultToAll = false; 921875ed548SDimitry Andric int ThisN = B.getNumber(); 922875ed548SDimitry Andric 923875ed548SDimitry Andric do { 924875ed548SDimitry Andric BTs.clear(); 9253ca95b02SDimitry Andric const MachineInstr &MI = *It; 926875ed548SDimitry Andric if (Trace) 9272cab237bSDimitry Andric dbgs() << "Visit BR(" << printMBBReference(B) << "): " << MI; 9283ca95b02SDimitry Andric assert(MI.isBranch() && "Expecting branch instruction"); 9293ca95b02SDimitry Andric InstrExec.insert(&MI); 930875ed548SDimitry Andric bool Eval = ME.evaluate(MI, Map, BTs, FallsThrough); 931875ed548SDimitry Andric if (!Eval) { 932875ed548SDimitry Andric // If the evaluation failed, we will add all targets. Keep going in 933875ed548SDimitry Andric // the loop to mark all executable branches as such. 934875ed548SDimitry Andric DefaultToAll = true; 935875ed548SDimitry Andric FallsThrough = true; 936875ed548SDimitry Andric if (Trace) 937875ed548SDimitry Andric dbgs() << " failed to evaluate: will add all CFG successors\n"; 938875ed548SDimitry Andric } else if (!DefaultToAll) { 939875ed548SDimitry Andric // If evaluated successfully add the targets to the cumulative list. 940875ed548SDimitry Andric if (Trace) { 941875ed548SDimitry Andric dbgs() << " adding targets:"; 942875ed548SDimitry Andric for (unsigned i = 0, n = BTs.size(); i < n; ++i) 9432cab237bSDimitry Andric dbgs() << " " << printMBBReference(*BTs[i]); 944875ed548SDimitry Andric if (FallsThrough) 945875ed548SDimitry Andric dbgs() << "\n falls through\n"; 946875ed548SDimitry Andric else 947875ed548SDimitry Andric dbgs() << "\n does not fall through\n"; 948875ed548SDimitry Andric } 949875ed548SDimitry Andric Targets.insert(BTs.begin(), BTs.end()); 950875ed548SDimitry Andric } 951875ed548SDimitry Andric ++It; 952875ed548SDimitry Andric } while (FallsThrough && It != End); 953875ed548SDimitry Andric 954875ed548SDimitry Andric if (!DefaultToAll) { 955875ed548SDimitry Andric // Need to add all CFG successors that lead to EH landing pads. 956875ed548SDimitry Andric // There won't be explicit branches to these blocks, but they must 957875ed548SDimitry Andric // be processed. 9582cab237bSDimitry Andric for (const MachineBasicBlock *SB : B.successors()) { 9597d523365SDimitry Andric if (SB->isEHPad()) 960875ed548SDimitry Andric Targets.insert(SB); 961875ed548SDimitry Andric } 962875ed548SDimitry Andric if (FallsThrough) { 9637d523365SDimitry Andric MachineFunction::const_iterator BIt = B.getIterator(); 964875ed548SDimitry Andric MachineFunction::const_iterator Next = std::next(BIt); 965875ed548SDimitry Andric if (Next != MF.end()) 966875ed548SDimitry Andric Targets.insert(&*Next); 967875ed548SDimitry Andric } 968875ed548SDimitry Andric } else { 9692cab237bSDimitry Andric for (const MachineBasicBlock *SB : B.successors()) 9702cab237bSDimitry Andric Targets.insert(SB); 971875ed548SDimitry Andric } 972875ed548SDimitry Andric 9732cab237bSDimitry Andric for (const MachineBasicBlock *TB : Targets) 9742cab237bSDimitry Andric FlowQ.push(CFGEdge(ThisN, TB->getNumber())); 975875ed548SDimitry Andric } 976875ed548SDimitry Andric 977875ed548SDimitry Andric void BT::visitUsesOf(unsigned Reg) { 978875ed548SDimitry Andric if (Trace) 9792cab237bSDimitry Andric dbgs() << "queuing uses of modified reg " << printReg(Reg, &ME.TRI) 9802cab237bSDimitry Andric << " cell: " << ME.getCell(Reg, Map) << '\n'; 981875ed548SDimitry Andric 9822cab237bSDimitry Andric for (MachineInstr &UseI : MRI.use_nodbg_instructions(Reg)) 9832cab237bSDimitry Andric UseQ.push(&UseI); 984875ed548SDimitry Andric } 985875ed548SDimitry Andric 986875ed548SDimitry Andric BT::RegisterCell BT::get(RegisterRef RR) const { 987875ed548SDimitry Andric return ME.getCell(RR, Map); 988875ed548SDimitry Andric } 989875ed548SDimitry Andric 990875ed548SDimitry Andric void BT::put(RegisterRef RR, const RegisterCell &RC) { 991875ed548SDimitry Andric ME.putCell(RR, RC, Map); 992875ed548SDimitry Andric } 993875ed548SDimitry Andric 994875ed548SDimitry Andric // Replace all references to bits from OldRR with the corresponding bits 995875ed548SDimitry Andric // in NewRR. 996875ed548SDimitry Andric void BT::subst(RegisterRef OldRR, RegisterRef NewRR) { 9977d523365SDimitry Andric assert(Map.count(OldRR.Reg) > 0 && "OldRR not present in map"); 998875ed548SDimitry Andric BitMask OM = ME.mask(OldRR.Reg, OldRR.Sub); 999875ed548SDimitry Andric BitMask NM = ME.mask(NewRR.Reg, NewRR.Sub); 1000875ed548SDimitry Andric uint16_t OMB = OM.first(), OME = OM.last(); 1001875ed548SDimitry Andric uint16_t NMB = NM.first(), NME = NM.last(); 1002875ed548SDimitry Andric (void)NME; 1003875ed548SDimitry Andric assert((OME-OMB == NME-NMB) && 1004875ed548SDimitry Andric "Substituting registers of different lengths"); 10052cab237bSDimitry Andric for (std::pair<const unsigned, RegisterCell> &P : Map) { 10062cab237bSDimitry Andric RegisterCell &RC = P.second; 1007875ed548SDimitry Andric for (uint16_t i = 0, w = RC.width(); i < w; ++i) { 1008875ed548SDimitry Andric BitValue &V = RC[i]; 1009875ed548SDimitry Andric if (V.Type != BitValue::Ref || V.RefI.Reg != OldRR.Reg) 1010875ed548SDimitry Andric continue; 1011875ed548SDimitry Andric if (V.RefI.Pos < OMB || V.RefI.Pos > OME) 1012875ed548SDimitry Andric continue; 1013875ed548SDimitry Andric V.RefI.Reg = NewRR.Reg; 1014875ed548SDimitry Andric V.RefI.Pos += NMB-OMB; 1015875ed548SDimitry Andric } 1016875ed548SDimitry Andric } 1017875ed548SDimitry Andric } 1018875ed548SDimitry Andric 1019875ed548SDimitry Andric // Check if the block has been "executed" during propagation. (If not, the 1020875ed548SDimitry Andric // block is dead, but it may still appear to be reachable.) 1021875ed548SDimitry Andric bool BT::reached(const MachineBasicBlock *B) const { 1022875ed548SDimitry Andric int BN = B->getNumber(); 1023875ed548SDimitry Andric assert(BN >= 0); 10246bc11b14SDimitry Andric return ReachedBB.count(BN); 1025875ed548SDimitry Andric } 1026875ed548SDimitry Andric 1027d88c1a5aSDimitry Andric // Visit an individual instruction. This could be a newly added instruction, 1028d88c1a5aSDimitry Andric // or one that has been modified by an optimization. 1029d88c1a5aSDimitry Andric void BT::visit(const MachineInstr &MI) { 1030d88c1a5aSDimitry Andric assert(!MI.isBranch() && "Only non-branches are allowed"); 1031d88c1a5aSDimitry Andric InstrExec.insert(&MI); 1032d88c1a5aSDimitry Andric visitNonBranch(MI); 10332cab237bSDimitry Andric // Make sure to flush all the pending use updates. 10342cab237bSDimitry Andric runUseQueue(); 1035d88c1a5aSDimitry Andric // The call to visitNonBranch could propagate the changes until a branch 1036d88c1a5aSDimitry Andric // is actually visited. This could result in adding CFG edges to the flow 1037d88c1a5aSDimitry Andric // queue. Since the queue won't be processed, clear it. 1038d88c1a5aSDimitry Andric while (!FlowQ.empty()) 1039d88c1a5aSDimitry Andric FlowQ.pop(); 1040d88c1a5aSDimitry Andric } 1041d88c1a5aSDimitry Andric 1042875ed548SDimitry Andric void BT::reset() { 1043875ed548SDimitry Andric EdgeExec.clear(); 1044875ed548SDimitry Andric InstrExec.clear(); 1045875ed548SDimitry Andric Map.clear(); 10466bc11b14SDimitry Andric ReachedBB.clear(); 10476bc11b14SDimitry Andric ReachedBB.reserve(MF.size()); 1048875ed548SDimitry Andric } 1049875ed548SDimitry Andric 10502cab237bSDimitry Andric void BT::runEdgeQueue(BitVector &BlockScanned) { 1051875ed548SDimitry Andric while (!FlowQ.empty()) { 1052875ed548SDimitry Andric CFGEdge Edge = FlowQ.front(); 1053875ed548SDimitry Andric FlowQ.pop(); 1054875ed548SDimitry Andric 1055875ed548SDimitry Andric if (EdgeExec.count(Edge)) 10562cab237bSDimitry Andric return; 1057875ed548SDimitry Andric EdgeExec.insert(Edge); 10586bc11b14SDimitry Andric ReachedBB.insert(Edge.second); 1059875ed548SDimitry Andric 1060875ed548SDimitry Andric const MachineBasicBlock &B = *MF.getBlockNumbered(Edge.second); 1061875ed548SDimitry Andric MachineBasicBlock::const_iterator It = B.begin(), End = B.end(); 1062875ed548SDimitry Andric // Visit PHI nodes first. 1063875ed548SDimitry Andric while (It != End && It->isPHI()) { 10643ca95b02SDimitry Andric const MachineInstr &PI = *It++; 10653ca95b02SDimitry Andric InstrExec.insert(&PI); 1066875ed548SDimitry Andric visitPHI(PI); 1067875ed548SDimitry Andric } 1068875ed548SDimitry Andric 1069875ed548SDimitry Andric // If this block has already been visited through a flow graph edge, 1070875ed548SDimitry Andric // then the instructions have already been processed. Any updates to 1071875ed548SDimitry Andric // the cells would now only happen through visitUsesOf... 1072875ed548SDimitry Andric if (BlockScanned[Edge.second]) 10732cab237bSDimitry Andric return; 1074875ed548SDimitry Andric BlockScanned[Edge.second] = true; 1075875ed548SDimitry Andric 1076875ed548SDimitry Andric // Visit non-branch instructions. 1077875ed548SDimitry Andric while (It != End && !It->isBranch()) { 10783ca95b02SDimitry Andric const MachineInstr &MI = *It++; 10793ca95b02SDimitry Andric InstrExec.insert(&MI); 1080875ed548SDimitry Andric visitNonBranch(MI); 1081875ed548SDimitry Andric } 1082875ed548SDimitry Andric // If block end has been reached, add the fall-through edge to the queue. 1083875ed548SDimitry Andric if (It == End) { 10847d523365SDimitry Andric MachineFunction::const_iterator BIt = B.getIterator(); 1085875ed548SDimitry Andric MachineFunction::const_iterator Next = std::next(BIt); 10867d523365SDimitry Andric if (Next != MF.end() && B.isSuccessor(&*Next)) { 1087875ed548SDimitry Andric int ThisN = B.getNumber(); 1088875ed548SDimitry Andric int NextN = Next->getNumber(); 1089875ed548SDimitry Andric FlowQ.push(CFGEdge(ThisN, NextN)); 1090875ed548SDimitry Andric } 1091875ed548SDimitry Andric } else { 1092875ed548SDimitry Andric // Handle the remaining sequence of branches. This function will update 1093875ed548SDimitry Andric // the work queue. 10943ca95b02SDimitry Andric visitBranchesFrom(*It); 1095875ed548SDimitry Andric } 1096875ed548SDimitry Andric } // while (!FlowQ->empty()) 10972cab237bSDimitry Andric } 10982cab237bSDimitry Andric 10992cab237bSDimitry Andric void BT::runUseQueue() { 11002cab237bSDimitry Andric while (!UseQ.empty()) { 11012cab237bSDimitry Andric MachineInstr &UseI = *UseQ.front(); 11022cab237bSDimitry Andric UseQ.pop(); 11032cab237bSDimitry Andric 11042cab237bSDimitry Andric if (!InstrExec.count(&UseI)) 11052cab237bSDimitry Andric continue; 11062cab237bSDimitry Andric if (UseI.isPHI()) 11072cab237bSDimitry Andric visitPHI(UseI); 11082cab237bSDimitry Andric else if (!UseI.isBranch()) 11092cab237bSDimitry Andric visitNonBranch(UseI); 11102cab237bSDimitry Andric else 11112cab237bSDimitry Andric visitBranchesFrom(UseI); 11122cab237bSDimitry Andric } 11132cab237bSDimitry Andric } 11142cab237bSDimitry Andric 11152cab237bSDimitry Andric void BT::run() { 11162cab237bSDimitry Andric reset(); 11172cab237bSDimitry Andric assert(FlowQ.empty()); 11182cab237bSDimitry Andric 11192cab237bSDimitry Andric using MachineFlowGraphTraits = GraphTraits<const MachineFunction*>; 11202cab237bSDimitry Andric const MachineBasicBlock *Entry = MachineFlowGraphTraits::getEntryNode(&MF); 11212cab237bSDimitry Andric 11222cab237bSDimitry Andric unsigned MaxBN = 0; 11232cab237bSDimitry Andric for (const MachineBasicBlock &B : MF) { 11242cab237bSDimitry Andric assert(B.getNumber() >= 0 && "Disconnected block"); 11252cab237bSDimitry Andric unsigned BN = B.getNumber(); 11262cab237bSDimitry Andric if (BN > MaxBN) 11272cab237bSDimitry Andric MaxBN = BN; 11282cab237bSDimitry Andric } 11292cab237bSDimitry Andric 11302cab237bSDimitry Andric // Keep track of visited blocks. 11312cab237bSDimitry Andric BitVector BlockScanned(MaxBN+1); 11322cab237bSDimitry Andric 11332cab237bSDimitry Andric int EntryN = Entry->getNumber(); 11342cab237bSDimitry Andric // Generate a fake edge to get something to start with. 11352cab237bSDimitry Andric FlowQ.push(CFGEdge(-1, EntryN)); 11362cab237bSDimitry Andric 11372cab237bSDimitry Andric while (!FlowQ.empty() || !UseQ.empty()) { 11382cab237bSDimitry Andric runEdgeQueue(BlockScanned); 11392cab237bSDimitry Andric runUseQueue(); 11402cab237bSDimitry Andric } 1141875ed548SDimitry Andric 1142d88c1a5aSDimitry Andric if (Trace) 1143d88c1a5aSDimitry Andric print_cells(dbgs() << "Cells after propagation:\n"); 1144875ed548SDimitry Andric } 1145