1*0b57cec5SDimitry Andric //===- HexagonBitSimplify.cpp ---------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "BitTracker.h" 10*0b57cec5SDimitry Andric #include "HexagonBitTracker.h" 11*0b57cec5SDimitry Andric #include "HexagonInstrInfo.h" 12*0b57cec5SDimitry Andric #include "HexagonRegisterInfo.h" 13*0b57cec5SDimitry Andric #include "HexagonSubtarget.h" 14*0b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 15*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/GraphTraits.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 28*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 30*0b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h" 31*0b57cec5SDimitry Andric #include "llvm/Pass.h" 32*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 33*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 34*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 35*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 36*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 37*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 38*0b57cec5SDimitry Andric #include <algorithm> 39*0b57cec5SDimitry Andric #include <cassert> 40*0b57cec5SDimitry Andric #include <cstdint> 41*0b57cec5SDimitry Andric #include <iterator> 42*0b57cec5SDimitry Andric #include <limits> 43*0b57cec5SDimitry Andric #include <utility> 44*0b57cec5SDimitry Andric #include <vector> 45*0b57cec5SDimitry Andric 46*0b57cec5SDimitry Andric #define DEBUG_TYPE "hexbit" 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric using namespace llvm; 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric static cl::opt<bool> PreserveTiedOps("hexbit-keep-tied", cl::Hidden, 51*0b57cec5SDimitry Andric cl::init(true), cl::desc("Preserve subregisters in tied operands")); 52*0b57cec5SDimitry Andric static cl::opt<bool> GenExtract("hexbit-extract", cl::Hidden, 53*0b57cec5SDimitry Andric cl::init(true), cl::desc("Generate extract instructions")); 54*0b57cec5SDimitry Andric static cl::opt<bool> GenBitSplit("hexbit-bitsplit", cl::Hidden, 55*0b57cec5SDimitry Andric cl::init(true), cl::desc("Generate bitsplit instructions")); 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric static cl::opt<unsigned> MaxExtract("hexbit-max-extract", cl::Hidden, 58*0b57cec5SDimitry Andric cl::init(std::numeric_limits<unsigned>::max())); 59*0b57cec5SDimitry Andric static unsigned CountExtract = 0; 60*0b57cec5SDimitry Andric static cl::opt<unsigned> MaxBitSplit("hexbit-max-bitsplit", cl::Hidden, 61*0b57cec5SDimitry Andric cl::init(std::numeric_limits<unsigned>::max())); 62*0b57cec5SDimitry Andric static unsigned CountBitSplit = 0; 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric namespace llvm { 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric void initializeHexagonBitSimplifyPass(PassRegistry& Registry); 67*0b57cec5SDimitry Andric FunctionPass *createHexagonBitSimplify(); 68*0b57cec5SDimitry Andric 69*0b57cec5SDimitry Andric } // end namespace llvm 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric namespace { 72*0b57cec5SDimitry Andric 73*0b57cec5SDimitry Andric // Set of virtual registers, based on BitVector. 74*0b57cec5SDimitry Andric struct RegisterSet : private BitVector { 75*0b57cec5SDimitry Andric RegisterSet() = default; 76*0b57cec5SDimitry Andric explicit RegisterSet(unsigned s, bool t = false) : BitVector(s, t) {} 77*0b57cec5SDimitry Andric RegisterSet(const RegisterSet &RS) = default; 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric using BitVector::clear; 80*0b57cec5SDimitry Andric using BitVector::count; 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric unsigned find_first() const { 83*0b57cec5SDimitry Andric int First = BitVector::find_first(); 84*0b57cec5SDimitry Andric if (First < 0) 85*0b57cec5SDimitry Andric return 0; 86*0b57cec5SDimitry Andric return x2v(First); 87*0b57cec5SDimitry Andric } 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric unsigned find_next(unsigned Prev) const { 90*0b57cec5SDimitry Andric int Next = BitVector::find_next(v2x(Prev)); 91*0b57cec5SDimitry Andric if (Next < 0) 92*0b57cec5SDimitry Andric return 0; 93*0b57cec5SDimitry Andric return x2v(Next); 94*0b57cec5SDimitry Andric } 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric RegisterSet &insert(unsigned R) { 97*0b57cec5SDimitry Andric unsigned Idx = v2x(R); 98*0b57cec5SDimitry Andric ensure(Idx); 99*0b57cec5SDimitry Andric return static_cast<RegisterSet&>(BitVector::set(Idx)); 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric RegisterSet &remove(unsigned R) { 102*0b57cec5SDimitry Andric unsigned Idx = v2x(R); 103*0b57cec5SDimitry Andric if (Idx >= size()) 104*0b57cec5SDimitry Andric return *this; 105*0b57cec5SDimitry Andric return static_cast<RegisterSet&>(BitVector::reset(Idx)); 106*0b57cec5SDimitry Andric } 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric RegisterSet &insert(const RegisterSet &Rs) { 109*0b57cec5SDimitry Andric return static_cast<RegisterSet&>(BitVector::operator|=(Rs)); 110*0b57cec5SDimitry Andric } 111*0b57cec5SDimitry Andric RegisterSet &remove(const RegisterSet &Rs) { 112*0b57cec5SDimitry Andric return static_cast<RegisterSet&>(BitVector::reset(Rs)); 113*0b57cec5SDimitry Andric } 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric reference operator[](unsigned R) { 116*0b57cec5SDimitry Andric unsigned Idx = v2x(R); 117*0b57cec5SDimitry Andric ensure(Idx); 118*0b57cec5SDimitry Andric return BitVector::operator[](Idx); 119*0b57cec5SDimitry Andric } 120*0b57cec5SDimitry Andric bool operator[](unsigned R) const { 121*0b57cec5SDimitry Andric unsigned Idx = v2x(R); 122*0b57cec5SDimitry Andric assert(Idx < size()); 123*0b57cec5SDimitry Andric return BitVector::operator[](Idx); 124*0b57cec5SDimitry Andric } 125*0b57cec5SDimitry Andric bool has(unsigned R) const { 126*0b57cec5SDimitry Andric unsigned Idx = v2x(R); 127*0b57cec5SDimitry Andric if (Idx >= size()) 128*0b57cec5SDimitry Andric return false; 129*0b57cec5SDimitry Andric return BitVector::test(Idx); 130*0b57cec5SDimitry Andric } 131*0b57cec5SDimitry Andric 132*0b57cec5SDimitry Andric bool empty() const { 133*0b57cec5SDimitry Andric return !BitVector::any(); 134*0b57cec5SDimitry Andric } 135*0b57cec5SDimitry Andric bool includes(const RegisterSet &Rs) const { 136*0b57cec5SDimitry Andric // A.BitVector::test(B) <=> A-B != {} 137*0b57cec5SDimitry Andric return !Rs.BitVector::test(*this); 138*0b57cec5SDimitry Andric } 139*0b57cec5SDimitry Andric bool intersects(const RegisterSet &Rs) const { 140*0b57cec5SDimitry Andric return BitVector::anyCommon(Rs); 141*0b57cec5SDimitry Andric } 142*0b57cec5SDimitry Andric 143*0b57cec5SDimitry Andric private: 144*0b57cec5SDimitry Andric void ensure(unsigned Idx) { 145*0b57cec5SDimitry Andric if (size() <= Idx) 146*0b57cec5SDimitry Andric resize(std::max(Idx+1, 32U)); 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric static inline unsigned v2x(unsigned v) { 150*0b57cec5SDimitry Andric return TargetRegisterInfo::virtReg2Index(v); 151*0b57cec5SDimitry Andric } 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric static inline unsigned x2v(unsigned x) { 154*0b57cec5SDimitry Andric return TargetRegisterInfo::index2VirtReg(x); 155*0b57cec5SDimitry Andric } 156*0b57cec5SDimitry Andric }; 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric struct PrintRegSet { 159*0b57cec5SDimitry Andric PrintRegSet(const RegisterSet &S, const TargetRegisterInfo *RI) 160*0b57cec5SDimitry Andric : RS(S), TRI(RI) {} 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric friend raw_ostream &operator<< (raw_ostream &OS, 163*0b57cec5SDimitry Andric const PrintRegSet &P); 164*0b57cec5SDimitry Andric 165*0b57cec5SDimitry Andric private: 166*0b57cec5SDimitry Andric const RegisterSet &RS; 167*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI; 168*0b57cec5SDimitry Andric }; 169*0b57cec5SDimitry Andric 170*0b57cec5SDimitry Andric raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P) 171*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_UNUSED; 172*0b57cec5SDimitry Andric raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P) { 173*0b57cec5SDimitry Andric OS << '{'; 174*0b57cec5SDimitry Andric for (unsigned R = P.RS.find_first(); R; R = P.RS.find_next(R)) 175*0b57cec5SDimitry Andric OS << ' ' << printReg(R, P.TRI); 176*0b57cec5SDimitry Andric OS << " }"; 177*0b57cec5SDimitry Andric return OS; 178*0b57cec5SDimitry Andric } 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric class Transformation; 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric class HexagonBitSimplify : public MachineFunctionPass { 183*0b57cec5SDimitry Andric public: 184*0b57cec5SDimitry Andric static char ID; 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric HexagonBitSimplify() : MachineFunctionPass(ID) {} 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric StringRef getPassName() const override { 189*0b57cec5SDimitry Andric return "Hexagon bit simplification"; 190*0b57cec5SDimitry Andric } 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 193*0b57cec5SDimitry Andric AU.addRequired<MachineDominatorTree>(); 194*0b57cec5SDimitry Andric AU.addPreserved<MachineDominatorTree>(); 195*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 196*0b57cec5SDimitry Andric } 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 199*0b57cec5SDimitry Andric 200*0b57cec5SDimitry Andric static void getInstrDefs(const MachineInstr &MI, RegisterSet &Defs); 201*0b57cec5SDimitry Andric static void getInstrUses(const MachineInstr &MI, RegisterSet &Uses); 202*0b57cec5SDimitry Andric static bool isEqual(const BitTracker::RegisterCell &RC1, uint16_t B1, 203*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC2, uint16_t B2, uint16_t W); 204*0b57cec5SDimitry Andric static bool isZero(const BitTracker::RegisterCell &RC, uint16_t B, 205*0b57cec5SDimitry Andric uint16_t W); 206*0b57cec5SDimitry Andric static bool getConst(const BitTracker::RegisterCell &RC, uint16_t B, 207*0b57cec5SDimitry Andric uint16_t W, uint64_t &U); 208*0b57cec5SDimitry Andric static bool replaceReg(unsigned OldR, unsigned NewR, 209*0b57cec5SDimitry Andric MachineRegisterInfo &MRI); 210*0b57cec5SDimitry Andric static bool getSubregMask(const BitTracker::RegisterRef &RR, 211*0b57cec5SDimitry Andric unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI); 212*0b57cec5SDimitry Andric static bool replaceRegWithSub(unsigned OldR, unsigned NewR, 213*0b57cec5SDimitry Andric unsigned NewSR, MachineRegisterInfo &MRI); 214*0b57cec5SDimitry Andric static bool replaceSubWithSub(unsigned OldR, unsigned OldSR, 215*0b57cec5SDimitry Andric unsigned NewR, unsigned NewSR, MachineRegisterInfo &MRI); 216*0b57cec5SDimitry Andric static bool parseRegSequence(const MachineInstr &I, 217*0b57cec5SDimitry Andric BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH, 218*0b57cec5SDimitry Andric const MachineRegisterInfo &MRI); 219*0b57cec5SDimitry Andric 220*0b57cec5SDimitry Andric static bool getUsedBitsInStore(unsigned Opc, BitVector &Bits, 221*0b57cec5SDimitry Andric uint16_t Begin); 222*0b57cec5SDimitry Andric static bool getUsedBits(unsigned Opc, unsigned OpN, BitVector &Bits, 223*0b57cec5SDimitry Andric uint16_t Begin, const HexagonInstrInfo &HII); 224*0b57cec5SDimitry Andric 225*0b57cec5SDimitry Andric static const TargetRegisterClass *getFinalVRegClass( 226*0b57cec5SDimitry Andric const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI); 227*0b57cec5SDimitry Andric static bool isTransparentCopy(const BitTracker::RegisterRef &RD, 228*0b57cec5SDimitry Andric const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI); 229*0b57cec5SDimitry Andric 230*0b57cec5SDimitry Andric private: 231*0b57cec5SDimitry Andric MachineDominatorTree *MDT = nullptr; 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric bool visitBlock(MachineBasicBlock &B, Transformation &T, RegisterSet &AVs); 234*0b57cec5SDimitry Andric static bool hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI, 235*0b57cec5SDimitry Andric unsigned NewSub = Hexagon::NoSubRegister); 236*0b57cec5SDimitry Andric }; 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric using HBS = HexagonBitSimplify; 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric // The purpose of this class is to provide a common facility to traverse 241*0b57cec5SDimitry Andric // the function top-down or bottom-up via the dominator tree, and keep 242*0b57cec5SDimitry Andric // track of the available registers. 243*0b57cec5SDimitry Andric class Transformation { 244*0b57cec5SDimitry Andric public: 245*0b57cec5SDimitry Andric bool TopDown; 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric Transformation(bool TD) : TopDown(TD) {} 248*0b57cec5SDimitry Andric virtual ~Transformation() = default; 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric virtual bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) = 0; 251*0b57cec5SDimitry Andric }; 252*0b57cec5SDimitry Andric 253*0b57cec5SDimitry Andric } // end anonymous namespace 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric char HexagonBitSimplify::ID = 0; 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(HexagonBitSimplify, "hexagon-bit-simplify", 258*0b57cec5SDimitry Andric "Hexagon bit simplification", false, false) 259*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 260*0b57cec5SDimitry Andric INITIALIZE_PASS_END(HexagonBitSimplify, "hexagon-bit-simplify", 261*0b57cec5SDimitry Andric "Hexagon bit simplification", false, false) 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T, 264*0b57cec5SDimitry Andric RegisterSet &AVs) { 265*0b57cec5SDimitry Andric bool Changed = false; 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric if (T.TopDown) 268*0b57cec5SDimitry Andric Changed = T.processBlock(B, AVs); 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric RegisterSet Defs; 271*0b57cec5SDimitry Andric for (auto &I : B) 272*0b57cec5SDimitry Andric getInstrDefs(I, Defs); 273*0b57cec5SDimitry Andric RegisterSet NewAVs = AVs; 274*0b57cec5SDimitry Andric NewAVs.insert(Defs); 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric for (auto *DTN : children<MachineDomTreeNode*>(MDT->getNode(&B))) 277*0b57cec5SDimitry Andric Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs); 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric if (!T.TopDown) 280*0b57cec5SDimitry Andric Changed |= T.processBlock(B, AVs); 281*0b57cec5SDimitry Andric 282*0b57cec5SDimitry Andric return Changed; 283*0b57cec5SDimitry Andric } 284*0b57cec5SDimitry Andric 285*0b57cec5SDimitry Andric // 286*0b57cec5SDimitry Andric // Utility functions: 287*0b57cec5SDimitry Andric // 288*0b57cec5SDimitry Andric void HexagonBitSimplify::getInstrDefs(const MachineInstr &MI, 289*0b57cec5SDimitry Andric RegisterSet &Defs) { 290*0b57cec5SDimitry Andric for (auto &Op : MI.operands()) { 291*0b57cec5SDimitry Andric if (!Op.isReg() || !Op.isDef()) 292*0b57cec5SDimitry Andric continue; 293*0b57cec5SDimitry Andric unsigned R = Op.getReg(); 294*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(R)) 295*0b57cec5SDimitry Andric continue; 296*0b57cec5SDimitry Andric Defs.insert(R); 297*0b57cec5SDimitry Andric } 298*0b57cec5SDimitry Andric } 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric void HexagonBitSimplify::getInstrUses(const MachineInstr &MI, 301*0b57cec5SDimitry Andric RegisterSet &Uses) { 302*0b57cec5SDimitry Andric for (auto &Op : MI.operands()) { 303*0b57cec5SDimitry Andric if (!Op.isReg() || !Op.isUse()) 304*0b57cec5SDimitry Andric continue; 305*0b57cec5SDimitry Andric unsigned R = Op.getReg(); 306*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(R)) 307*0b57cec5SDimitry Andric continue; 308*0b57cec5SDimitry Andric Uses.insert(R); 309*0b57cec5SDimitry Andric } 310*0b57cec5SDimitry Andric } 311*0b57cec5SDimitry Andric 312*0b57cec5SDimitry Andric // Check if all the bits in range [B, E) in both cells are equal. 313*0b57cec5SDimitry Andric bool HexagonBitSimplify::isEqual(const BitTracker::RegisterCell &RC1, 314*0b57cec5SDimitry Andric uint16_t B1, const BitTracker::RegisterCell &RC2, uint16_t B2, 315*0b57cec5SDimitry Andric uint16_t W) { 316*0b57cec5SDimitry Andric for (uint16_t i = 0; i < W; ++i) { 317*0b57cec5SDimitry Andric // If RC1[i] is "bottom", it cannot be proven equal to RC2[i]. 318*0b57cec5SDimitry Andric if (RC1[B1+i].Type == BitTracker::BitValue::Ref && RC1[B1+i].RefI.Reg == 0) 319*0b57cec5SDimitry Andric return false; 320*0b57cec5SDimitry Andric // Same for RC2[i]. 321*0b57cec5SDimitry Andric if (RC2[B2+i].Type == BitTracker::BitValue::Ref && RC2[B2+i].RefI.Reg == 0) 322*0b57cec5SDimitry Andric return false; 323*0b57cec5SDimitry Andric if (RC1[B1+i] != RC2[B2+i]) 324*0b57cec5SDimitry Andric return false; 325*0b57cec5SDimitry Andric } 326*0b57cec5SDimitry Andric return true; 327*0b57cec5SDimitry Andric } 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric bool HexagonBitSimplify::isZero(const BitTracker::RegisterCell &RC, 330*0b57cec5SDimitry Andric uint16_t B, uint16_t W) { 331*0b57cec5SDimitry Andric assert(B < RC.width() && B+W <= RC.width()); 332*0b57cec5SDimitry Andric for (uint16_t i = B; i < B+W; ++i) 333*0b57cec5SDimitry Andric if (!RC[i].is(0)) 334*0b57cec5SDimitry Andric return false; 335*0b57cec5SDimitry Andric return true; 336*0b57cec5SDimitry Andric } 337*0b57cec5SDimitry Andric 338*0b57cec5SDimitry Andric bool HexagonBitSimplify::getConst(const BitTracker::RegisterCell &RC, 339*0b57cec5SDimitry Andric uint16_t B, uint16_t W, uint64_t &U) { 340*0b57cec5SDimitry Andric assert(B < RC.width() && B+W <= RC.width()); 341*0b57cec5SDimitry Andric int64_t T = 0; 342*0b57cec5SDimitry Andric for (uint16_t i = B+W; i > B; --i) { 343*0b57cec5SDimitry Andric const BitTracker::BitValue &BV = RC[i-1]; 344*0b57cec5SDimitry Andric T <<= 1; 345*0b57cec5SDimitry Andric if (BV.is(1)) 346*0b57cec5SDimitry Andric T |= 1; 347*0b57cec5SDimitry Andric else if (!BV.is(0)) 348*0b57cec5SDimitry Andric return false; 349*0b57cec5SDimitry Andric } 350*0b57cec5SDimitry Andric U = T; 351*0b57cec5SDimitry Andric return true; 352*0b57cec5SDimitry Andric } 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric bool HexagonBitSimplify::replaceReg(unsigned OldR, unsigned NewR, 355*0b57cec5SDimitry Andric MachineRegisterInfo &MRI) { 356*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(OldR) || 357*0b57cec5SDimitry Andric !TargetRegisterInfo::isVirtualRegister(NewR)) 358*0b57cec5SDimitry Andric return false; 359*0b57cec5SDimitry Andric auto Begin = MRI.use_begin(OldR), End = MRI.use_end(); 360*0b57cec5SDimitry Andric decltype(End) NextI; 361*0b57cec5SDimitry Andric for (auto I = Begin; I != End; I = NextI) { 362*0b57cec5SDimitry Andric NextI = std::next(I); 363*0b57cec5SDimitry Andric I->setReg(NewR); 364*0b57cec5SDimitry Andric } 365*0b57cec5SDimitry Andric return Begin != End; 366*0b57cec5SDimitry Andric } 367*0b57cec5SDimitry Andric 368*0b57cec5SDimitry Andric bool HexagonBitSimplify::replaceRegWithSub(unsigned OldR, unsigned NewR, 369*0b57cec5SDimitry Andric unsigned NewSR, MachineRegisterInfo &MRI) { 370*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(OldR) || 371*0b57cec5SDimitry Andric !TargetRegisterInfo::isVirtualRegister(NewR)) 372*0b57cec5SDimitry Andric return false; 373*0b57cec5SDimitry Andric if (hasTiedUse(OldR, MRI, NewSR)) 374*0b57cec5SDimitry Andric return false; 375*0b57cec5SDimitry Andric auto Begin = MRI.use_begin(OldR), End = MRI.use_end(); 376*0b57cec5SDimitry Andric decltype(End) NextI; 377*0b57cec5SDimitry Andric for (auto I = Begin; I != End; I = NextI) { 378*0b57cec5SDimitry Andric NextI = std::next(I); 379*0b57cec5SDimitry Andric I->setReg(NewR); 380*0b57cec5SDimitry Andric I->setSubReg(NewSR); 381*0b57cec5SDimitry Andric } 382*0b57cec5SDimitry Andric return Begin != End; 383*0b57cec5SDimitry Andric } 384*0b57cec5SDimitry Andric 385*0b57cec5SDimitry Andric bool HexagonBitSimplify::replaceSubWithSub(unsigned OldR, unsigned OldSR, 386*0b57cec5SDimitry Andric unsigned NewR, unsigned NewSR, MachineRegisterInfo &MRI) { 387*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(OldR) || 388*0b57cec5SDimitry Andric !TargetRegisterInfo::isVirtualRegister(NewR)) 389*0b57cec5SDimitry Andric return false; 390*0b57cec5SDimitry Andric if (OldSR != NewSR && hasTiedUse(OldR, MRI, NewSR)) 391*0b57cec5SDimitry Andric return false; 392*0b57cec5SDimitry Andric auto Begin = MRI.use_begin(OldR), End = MRI.use_end(); 393*0b57cec5SDimitry Andric decltype(End) NextI; 394*0b57cec5SDimitry Andric for (auto I = Begin; I != End; I = NextI) { 395*0b57cec5SDimitry Andric NextI = std::next(I); 396*0b57cec5SDimitry Andric if (I->getSubReg() != OldSR) 397*0b57cec5SDimitry Andric continue; 398*0b57cec5SDimitry Andric I->setReg(NewR); 399*0b57cec5SDimitry Andric I->setSubReg(NewSR); 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric return Begin != End; 402*0b57cec5SDimitry Andric } 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric // For a register ref (pair Reg:Sub), set Begin to the position of the LSB 405*0b57cec5SDimitry Andric // of Sub in Reg, and set Width to the size of Sub in bits. Return true, 406*0b57cec5SDimitry Andric // if this succeeded, otherwise return false. 407*0b57cec5SDimitry Andric bool HexagonBitSimplify::getSubregMask(const BitTracker::RegisterRef &RR, 408*0b57cec5SDimitry Andric unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI) { 409*0b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI.getRegClass(RR.Reg); 410*0b57cec5SDimitry Andric if (RR.Sub == 0) { 411*0b57cec5SDimitry Andric Begin = 0; 412*0b57cec5SDimitry Andric Width = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC); 413*0b57cec5SDimitry Andric return true; 414*0b57cec5SDimitry Andric } 415*0b57cec5SDimitry Andric 416*0b57cec5SDimitry Andric Begin = 0; 417*0b57cec5SDimitry Andric 418*0b57cec5SDimitry Andric switch (RC->getID()) { 419*0b57cec5SDimitry Andric case Hexagon::DoubleRegsRegClassID: 420*0b57cec5SDimitry Andric case Hexagon::HvxWRRegClassID: 421*0b57cec5SDimitry Andric Width = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 2; 422*0b57cec5SDimitry Andric if (RR.Sub == Hexagon::isub_hi || RR.Sub == Hexagon::vsub_hi) 423*0b57cec5SDimitry Andric Begin = Width; 424*0b57cec5SDimitry Andric break; 425*0b57cec5SDimitry Andric default: 426*0b57cec5SDimitry Andric return false; 427*0b57cec5SDimitry Andric } 428*0b57cec5SDimitry Andric return true; 429*0b57cec5SDimitry Andric } 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric 432*0b57cec5SDimitry Andric // For a REG_SEQUENCE, set SL to the low subregister and SH to the high 433*0b57cec5SDimitry Andric // subregister. 434*0b57cec5SDimitry Andric bool HexagonBitSimplify::parseRegSequence(const MachineInstr &I, 435*0b57cec5SDimitry Andric BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH, 436*0b57cec5SDimitry Andric const MachineRegisterInfo &MRI) { 437*0b57cec5SDimitry Andric assert(I.getOpcode() == TargetOpcode::REG_SEQUENCE); 438*0b57cec5SDimitry Andric unsigned Sub1 = I.getOperand(2).getImm(), Sub2 = I.getOperand(4).getImm(); 439*0b57cec5SDimitry Andric auto &DstRC = *MRI.getRegClass(I.getOperand(0).getReg()); 440*0b57cec5SDimitry Andric auto &HRI = static_cast<const HexagonRegisterInfo&>( 441*0b57cec5SDimitry Andric *MRI.getTargetRegisterInfo()); 442*0b57cec5SDimitry Andric unsigned SubLo = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_lo); 443*0b57cec5SDimitry Andric unsigned SubHi = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_hi); 444*0b57cec5SDimitry Andric assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo)); 445*0b57cec5SDimitry Andric if (Sub1 == SubLo && Sub2 == SubHi) { 446*0b57cec5SDimitry Andric SL = I.getOperand(1); 447*0b57cec5SDimitry Andric SH = I.getOperand(3); 448*0b57cec5SDimitry Andric return true; 449*0b57cec5SDimitry Andric } 450*0b57cec5SDimitry Andric if (Sub1 == SubHi && Sub2 == SubLo) { 451*0b57cec5SDimitry Andric SH = I.getOperand(1); 452*0b57cec5SDimitry Andric SL = I.getOperand(3); 453*0b57cec5SDimitry Andric return true; 454*0b57cec5SDimitry Andric } 455*0b57cec5SDimitry Andric return false; 456*0b57cec5SDimitry Andric } 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric // All stores (except 64-bit stores) take a 32-bit register as the source 459*0b57cec5SDimitry Andric // of the value to be stored. If the instruction stores into a location 460*0b57cec5SDimitry Andric // that is shorter than 32 bits, some bits of the source register are not 461*0b57cec5SDimitry Andric // used. For each store instruction, calculate the set of used bits in 462*0b57cec5SDimitry Andric // the source register, and set appropriate bits in Bits. Return true if 463*0b57cec5SDimitry Andric // the bits are calculated, false otherwise. 464*0b57cec5SDimitry Andric bool HexagonBitSimplify::getUsedBitsInStore(unsigned Opc, BitVector &Bits, 465*0b57cec5SDimitry Andric uint16_t Begin) { 466*0b57cec5SDimitry Andric using namespace Hexagon; 467*0b57cec5SDimitry Andric 468*0b57cec5SDimitry Andric switch (Opc) { 469*0b57cec5SDimitry Andric // Store byte 470*0b57cec5SDimitry Andric case S2_storerb_io: // memb(Rs32+#s11:0)=Rt32 471*0b57cec5SDimitry Andric case S2_storerbnew_io: // memb(Rs32+#s11:0)=Nt8.new 472*0b57cec5SDimitry Andric case S2_pstorerbt_io: // if (Pv4) memb(Rs32+#u6:0)=Rt32 473*0b57cec5SDimitry Andric case S2_pstorerbf_io: // if (!Pv4) memb(Rs32+#u6:0)=Rt32 474*0b57cec5SDimitry Andric case S4_pstorerbtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Rt32 475*0b57cec5SDimitry Andric case S4_pstorerbfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Rt32 476*0b57cec5SDimitry Andric case S2_pstorerbnewt_io: // if (Pv4) memb(Rs32+#u6:0)=Nt8.new 477*0b57cec5SDimitry Andric case S2_pstorerbnewf_io: // if (!Pv4) memb(Rs32+#u6:0)=Nt8.new 478*0b57cec5SDimitry Andric case S4_pstorerbnewtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Nt8.new 479*0b57cec5SDimitry Andric case S4_pstorerbnewfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Nt8.new 480*0b57cec5SDimitry Andric case S2_storerb_pi: // memb(Rx32++#s4:0)=Rt32 481*0b57cec5SDimitry Andric case S2_storerbnew_pi: // memb(Rx32++#s4:0)=Nt8.new 482*0b57cec5SDimitry Andric case S2_pstorerbt_pi: // if (Pv4) memb(Rx32++#s4:0)=Rt32 483*0b57cec5SDimitry Andric case S2_pstorerbf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Rt32 484*0b57cec5SDimitry Andric case S2_pstorerbtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Rt32 485*0b57cec5SDimitry Andric case S2_pstorerbfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Rt32 486*0b57cec5SDimitry Andric case S2_pstorerbnewt_pi: // if (Pv4) memb(Rx32++#s4:0)=Nt8.new 487*0b57cec5SDimitry Andric case S2_pstorerbnewf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Nt8.new 488*0b57cec5SDimitry Andric case S2_pstorerbnewtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Nt8.new 489*0b57cec5SDimitry Andric case S2_pstorerbnewfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Nt8.new 490*0b57cec5SDimitry Andric case S4_storerb_ap: // memb(Re32=#U6)=Rt32 491*0b57cec5SDimitry Andric case S4_storerbnew_ap: // memb(Re32=#U6)=Nt8.new 492*0b57cec5SDimitry Andric case S2_storerb_pr: // memb(Rx32++Mu2)=Rt32 493*0b57cec5SDimitry Andric case S2_storerbnew_pr: // memb(Rx32++Mu2)=Nt8.new 494*0b57cec5SDimitry Andric case S4_storerb_ur: // memb(Ru32<<#u2+#U6)=Rt32 495*0b57cec5SDimitry Andric case S4_storerbnew_ur: // memb(Ru32<<#u2+#U6)=Nt8.new 496*0b57cec5SDimitry Andric case S2_storerb_pbr: // memb(Rx32++Mu2:brev)=Rt32 497*0b57cec5SDimitry Andric case S2_storerbnew_pbr: // memb(Rx32++Mu2:brev)=Nt8.new 498*0b57cec5SDimitry Andric case S2_storerb_pci: // memb(Rx32++#s4:0:circ(Mu2))=Rt32 499*0b57cec5SDimitry Andric case S2_storerbnew_pci: // memb(Rx32++#s4:0:circ(Mu2))=Nt8.new 500*0b57cec5SDimitry Andric case S2_storerb_pcr: // memb(Rx32++I:circ(Mu2))=Rt32 501*0b57cec5SDimitry Andric case S2_storerbnew_pcr: // memb(Rx32++I:circ(Mu2))=Nt8.new 502*0b57cec5SDimitry Andric case S4_storerb_rr: // memb(Rs32+Ru32<<#u2)=Rt32 503*0b57cec5SDimitry Andric case S4_storerbnew_rr: // memb(Rs32+Ru32<<#u2)=Nt8.new 504*0b57cec5SDimitry Andric case S4_pstorerbt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Rt32 505*0b57cec5SDimitry Andric case S4_pstorerbf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Rt32 506*0b57cec5SDimitry Andric case S4_pstorerbtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32 507*0b57cec5SDimitry Andric case S4_pstorerbfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32 508*0b57cec5SDimitry Andric case S4_pstorerbnewt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new 509*0b57cec5SDimitry Andric case S4_pstorerbnewf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new 510*0b57cec5SDimitry Andric case S4_pstorerbnewtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new 511*0b57cec5SDimitry Andric case S4_pstorerbnewfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new 512*0b57cec5SDimitry Andric case S2_storerbgp: // memb(gp+#u16:0)=Rt32 513*0b57cec5SDimitry Andric case S2_storerbnewgp: // memb(gp+#u16:0)=Nt8.new 514*0b57cec5SDimitry Andric case S4_pstorerbt_abs: // if (Pv4) memb(#u6)=Rt32 515*0b57cec5SDimitry Andric case S4_pstorerbf_abs: // if (!Pv4) memb(#u6)=Rt32 516*0b57cec5SDimitry Andric case S4_pstorerbtnew_abs: // if (Pv4.new) memb(#u6)=Rt32 517*0b57cec5SDimitry Andric case S4_pstorerbfnew_abs: // if (!Pv4.new) memb(#u6)=Rt32 518*0b57cec5SDimitry Andric case S4_pstorerbnewt_abs: // if (Pv4) memb(#u6)=Nt8.new 519*0b57cec5SDimitry Andric case S4_pstorerbnewf_abs: // if (!Pv4) memb(#u6)=Nt8.new 520*0b57cec5SDimitry Andric case S4_pstorerbnewtnew_abs: // if (Pv4.new) memb(#u6)=Nt8.new 521*0b57cec5SDimitry Andric case S4_pstorerbnewfnew_abs: // if (!Pv4.new) memb(#u6)=Nt8.new 522*0b57cec5SDimitry Andric Bits.set(Begin, Begin+8); 523*0b57cec5SDimitry Andric return true; 524*0b57cec5SDimitry Andric 525*0b57cec5SDimitry Andric // Store low half 526*0b57cec5SDimitry Andric case S2_storerh_io: // memh(Rs32+#s11:1)=Rt32 527*0b57cec5SDimitry Andric case S2_storerhnew_io: // memh(Rs32+#s11:1)=Nt8.new 528*0b57cec5SDimitry Andric case S2_pstorerht_io: // if (Pv4) memh(Rs32+#u6:1)=Rt32 529*0b57cec5SDimitry Andric case S2_pstorerhf_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt32 530*0b57cec5SDimitry Andric case S4_pstorerhtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt32 531*0b57cec5SDimitry Andric case S4_pstorerhfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt32 532*0b57cec5SDimitry Andric case S2_pstorerhnewt_io: // if (Pv4) memh(Rs32+#u6:1)=Nt8.new 533*0b57cec5SDimitry Andric case S2_pstorerhnewf_io: // if (!Pv4) memh(Rs32+#u6:1)=Nt8.new 534*0b57cec5SDimitry Andric case S4_pstorerhnewtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Nt8.new 535*0b57cec5SDimitry Andric case S4_pstorerhnewfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Nt8.new 536*0b57cec5SDimitry Andric case S2_storerh_pi: // memh(Rx32++#s4:1)=Rt32 537*0b57cec5SDimitry Andric case S2_storerhnew_pi: // memh(Rx32++#s4:1)=Nt8.new 538*0b57cec5SDimitry Andric case S2_pstorerht_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt32 539*0b57cec5SDimitry Andric case S2_pstorerhf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt32 540*0b57cec5SDimitry Andric case S2_pstorerhtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt32 541*0b57cec5SDimitry Andric case S2_pstorerhfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt32 542*0b57cec5SDimitry Andric case S2_pstorerhnewt_pi: // if (Pv4) memh(Rx32++#s4:1)=Nt8.new 543*0b57cec5SDimitry Andric case S2_pstorerhnewf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Nt8.new 544*0b57cec5SDimitry Andric case S2_pstorerhnewtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Nt8.new 545*0b57cec5SDimitry Andric case S2_pstorerhnewfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Nt8.new 546*0b57cec5SDimitry Andric case S4_storerh_ap: // memh(Re32=#U6)=Rt32 547*0b57cec5SDimitry Andric case S4_storerhnew_ap: // memh(Re32=#U6)=Nt8.new 548*0b57cec5SDimitry Andric case S2_storerh_pr: // memh(Rx32++Mu2)=Rt32 549*0b57cec5SDimitry Andric case S2_storerhnew_pr: // memh(Rx32++Mu2)=Nt8.new 550*0b57cec5SDimitry Andric case S4_storerh_ur: // memh(Ru32<<#u2+#U6)=Rt32 551*0b57cec5SDimitry Andric case S4_storerhnew_ur: // memh(Ru32<<#u2+#U6)=Nt8.new 552*0b57cec5SDimitry Andric case S2_storerh_pbr: // memh(Rx32++Mu2:brev)=Rt32 553*0b57cec5SDimitry Andric case S2_storerhnew_pbr: // memh(Rx32++Mu2:brev)=Nt8.new 554*0b57cec5SDimitry Andric case S2_storerh_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt32 555*0b57cec5SDimitry Andric case S2_storerhnew_pci: // memh(Rx32++#s4:1:circ(Mu2))=Nt8.new 556*0b57cec5SDimitry Andric case S2_storerh_pcr: // memh(Rx32++I:circ(Mu2))=Rt32 557*0b57cec5SDimitry Andric case S2_storerhnew_pcr: // memh(Rx32++I:circ(Mu2))=Nt8.new 558*0b57cec5SDimitry Andric case S4_storerh_rr: // memh(Rs32+Ru32<<#u2)=Rt32 559*0b57cec5SDimitry Andric case S4_pstorerht_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt32 560*0b57cec5SDimitry Andric case S4_pstorerhf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt32 561*0b57cec5SDimitry Andric case S4_pstorerhtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32 562*0b57cec5SDimitry Andric case S4_pstorerhfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32 563*0b57cec5SDimitry Andric case S4_storerhnew_rr: // memh(Rs32+Ru32<<#u2)=Nt8.new 564*0b57cec5SDimitry Andric case S4_pstorerhnewt_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new 565*0b57cec5SDimitry Andric case S4_pstorerhnewf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new 566*0b57cec5SDimitry Andric case S4_pstorerhnewtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new 567*0b57cec5SDimitry Andric case S4_pstorerhnewfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new 568*0b57cec5SDimitry Andric case S2_storerhgp: // memh(gp+#u16:1)=Rt32 569*0b57cec5SDimitry Andric case S2_storerhnewgp: // memh(gp+#u16:1)=Nt8.new 570*0b57cec5SDimitry Andric case S4_pstorerht_abs: // if (Pv4) memh(#u6)=Rt32 571*0b57cec5SDimitry Andric case S4_pstorerhf_abs: // if (!Pv4) memh(#u6)=Rt32 572*0b57cec5SDimitry Andric case S4_pstorerhtnew_abs: // if (Pv4.new) memh(#u6)=Rt32 573*0b57cec5SDimitry Andric case S4_pstorerhfnew_abs: // if (!Pv4.new) memh(#u6)=Rt32 574*0b57cec5SDimitry Andric case S4_pstorerhnewt_abs: // if (Pv4) memh(#u6)=Nt8.new 575*0b57cec5SDimitry Andric case S4_pstorerhnewf_abs: // if (!Pv4) memh(#u6)=Nt8.new 576*0b57cec5SDimitry Andric case S4_pstorerhnewtnew_abs: // if (Pv4.new) memh(#u6)=Nt8.new 577*0b57cec5SDimitry Andric case S4_pstorerhnewfnew_abs: // if (!Pv4.new) memh(#u6)=Nt8.new 578*0b57cec5SDimitry Andric Bits.set(Begin, Begin+16); 579*0b57cec5SDimitry Andric return true; 580*0b57cec5SDimitry Andric 581*0b57cec5SDimitry Andric // Store high half 582*0b57cec5SDimitry Andric case S2_storerf_io: // memh(Rs32+#s11:1)=Rt.H32 583*0b57cec5SDimitry Andric case S2_pstorerft_io: // if (Pv4) memh(Rs32+#u6:1)=Rt.H32 584*0b57cec5SDimitry Andric case S2_pstorerff_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt.H32 585*0b57cec5SDimitry Andric case S4_pstorerftnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt.H32 586*0b57cec5SDimitry Andric case S4_pstorerffnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt.H32 587*0b57cec5SDimitry Andric case S2_storerf_pi: // memh(Rx32++#s4:1)=Rt.H32 588*0b57cec5SDimitry Andric case S2_pstorerft_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt.H32 589*0b57cec5SDimitry Andric case S2_pstorerff_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt.H32 590*0b57cec5SDimitry Andric case S2_pstorerftnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt.H32 591*0b57cec5SDimitry Andric case S2_pstorerffnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt.H32 592*0b57cec5SDimitry Andric case S4_storerf_ap: // memh(Re32=#U6)=Rt.H32 593*0b57cec5SDimitry Andric case S2_storerf_pr: // memh(Rx32++Mu2)=Rt.H32 594*0b57cec5SDimitry Andric case S4_storerf_ur: // memh(Ru32<<#u2+#U6)=Rt.H32 595*0b57cec5SDimitry Andric case S2_storerf_pbr: // memh(Rx32++Mu2:brev)=Rt.H32 596*0b57cec5SDimitry Andric case S2_storerf_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt.H32 597*0b57cec5SDimitry Andric case S2_storerf_pcr: // memh(Rx32++I:circ(Mu2))=Rt.H32 598*0b57cec5SDimitry Andric case S4_storerf_rr: // memh(Rs32+Ru32<<#u2)=Rt.H32 599*0b57cec5SDimitry Andric case S4_pstorerft_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32 600*0b57cec5SDimitry Andric case S4_pstorerff_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32 601*0b57cec5SDimitry Andric case S4_pstorerftnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32 602*0b57cec5SDimitry Andric case S4_pstorerffnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32 603*0b57cec5SDimitry Andric case S2_storerfgp: // memh(gp+#u16:1)=Rt.H32 604*0b57cec5SDimitry Andric case S4_pstorerft_abs: // if (Pv4) memh(#u6)=Rt.H32 605*0b57cec5SDimitry Andric case S4_pstorerff_abs: // if (!Pv4) memh(#u6)=Rt.H32 606*0b57cec5SDimitry Andric case S4_pstorerftnew_abs: // if (Pv4.new) memh(#u6)=Rt.H32 607*0b57cec5SDimitry Andric case S4_pstorerffnew_abs: // if (!Pv4.new) memh(#u6)=Rt.H32 608*0b57cec5SDimitry Andric Bits.set(Begin+16, Begin+32); 609*0b57cec5SDimitry Andric return true; 610*0b57cec5SDimitry Andric } 611*0b57cec5SDimitry Andric 612*0b57cec5SDimitry Andric return false; 613*0b57cec5SDimitry Andric } 614*0b57cec5SDimitry Andric 615*0b57cec5SDimitry Andric // For an instruction with opcode Opc, calculate the set of bits that it 616*0b57cec5SDimitry Andric // uses in a register in operand OpN. This only calculates the set of used 617*0b57cec5SDimitry Andric // bits for cases where it does not depend on any operands (as is the case 618*0b57cec5SDimitry Andric // in shifts, for example). For concrete instructions from a program, the 619*0b57cec5SDimitry Andric // operand may be a subregister of a larger register, while Bits would 620*0b57cec5SDimitry Andric // correspond to the larger register in its entirety. Because of that, 621*0b57cec5SDimitry Andric // the parameter Begin can be used to indicate which bit of Bits should be 622*0b57cec5SDimitry Andric // considered the LSB of the operand. 623*0b57cec5SDimitry Andric bool HexagonBitSimplify::getUsedBits(unsigned Opc, unsigned OpN, 624*0b57cec5SDimitry Andric BitVector &Bits, uint16_t Begin, const HexagonInstrInfo &HII) { 625*0b57cec5SDimitry Andric using namespace Hexagon; 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric const MCInstrDesc &D = HII.get(Opc); 628*0b57cec5SDimitry Andric if (D.mayStore()) { 629*0b57cec5SDimitry Andric if (OpN == D.getNumOperands()-1) 630*0b57cec5SDimitry Andric return getUsedBitsInStore(Opc, Bits, Begin); 631*0b57cec5SDimitry Andric return false; 632*0b57cec5SDimitry Andric } 633*0b57cec5SDimitry Andric 634*0b57cec5SDimitry Andric switch (Opc) { 635*0b57cec5SDimitry Andric // One register source. Used bits: R1[0-7]. 636*0b57cec5SDimitry Andric case A2_sxtb: 637*0b57cec5SDimitry Andric case A2_zxtb: 638*0b57cec5SDimitry Andric case A4_cmpbeqi: 639*0b57cec5SDimitry Andric case A4_cmpbgti: 640*0b57cec5SDimitry Andric case A4_cmpbgtui: 641*0b57cec5SDimitry Andric if (OpN == 1) { 642*0b57cec5SDimitry Andric Bits.set(Begin, Begin+8); 643*0b57cec5SDimitry Andric return true; 644*0b57cec5SDimitry Andric } 645*0b57cec5SDimitry Andric break; 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric // One register source. Used bits: R1[0-15]. 648*0b57cec5SDimitry Andric case A2_aslh: 649*0b57cec5SDimitry Andric case A2_sxth: 650*0b57cec5SDimitry Andric case A2_zxth: 651*0b57cec5SDimitry Andric case A4_cmpheqi: 652*0b57cec5SDimitry Andric case A4_cmphgti: 653*0b57cec5SDimitry Andric case A4_cmphgtui: 654*0b57cec5SDimitry Andric if (OpN == 1) { 655*0b57cec5SDimitry Andric Bits.set(Begin, Begin+16); 656*0b57cec5SDimitry Andric return true; 657*0b57cec5SDimitry Andric } 658*0b57cec5SDimitry Andric break; 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric // One register source. Used bits: R1[16-31]. 661*0b57cec5SDimitry Andric case A2_asrh: 662*0b57cec5SDimitry Andric if (OpN == 1) { 663*0b57cec5SDimitry Andric Bits.set(Begin+16, Begin+32); 664*0b57cec5SDimitry Andric return true; 665*0b57cec5SDimitry Andric } 666*0b57cec5SDimitry Andric break; 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric // Two register sources. Used bits: R1[0-7], R2[0-7]. 669*0b57cec5SDimitry Andric case A4_cmpbeq: 670*0b57cec5SDimitry Andric case A4_cmpbgt: 671*0b57cec5SDimitry Andric case A4_cmpbgtu: 672*0b57cec5SDimitry Andric if (OpN == 1) { 673*0b57cec5SDimitry Andric Bits.set(Begin, Begin+8); 674*0b57cec5SDimitry Andric return true; 675*0b57cec5SDimitry Andric } 676*0b57cec5SDimitry Andric break; 677*0b57cec5SDimitry Andric 678*0b57cec5SDimitry Andric // Two register sources. Used bits: R1[0-15], R2[0-15]. 679*0b57cec5SDimitry Andric case A4_cmpheq: 680*0b57cec5SDimitry Andric case A4_cmphgt: 681*0b57cec5SDimitry Andric case A4_cmphgtu: 682*0b57cec5SDimitry Andric case A2_addh_h16_ll: 683*0b57cec5SDimitry Andric case A2_addh_h16_sat_ll: 684*0b57cec5SDimitry Andric case A2_addh_l16_ll: 685*0b57cec5SDimitry Andric case A2_addh_l16_sat_ll: 686*0b57cec5SDimitry Andric case A2_combine_ll: 687*0b57cec5SDimitry Andric case A2_subh_h16_ll: 688*0b57cec5SDimitry Andric case A2_subh_h16_sat_ll: 689*0b57cec5SDimitry Andric case A2_subh_l16_ll: 690*0b57cec5SDimitry Andric case A2_subh_l16_sat_ll: 691*0b57cec5SDimitry Andric case M2_mpy_acc_ll_s0: 692*0b57cec5SDimitry Andric case M2_mpy_acc_ll_s1: 693*0b57cec5SDimitry Andric case M2_mpy_acc_sat_ll_s0: 694*0b57cec5SDimitry Andric case M2_mpy_acc_sat_ll_s1: 695*0b57cec5SDimitry Andric case M2_mpy_ll_s0: 696*0b57cec5SDimitry Andric case M2_mpy_ll_s1: 697*0b57cec5SDimitry Andric case M2_mpy_nac_ll_s0: 698*0b57cec5SDimitry Andric case M2_mpy_nac_ll_s1: 699*0b57cec5SDimitry Andric case M2_mpy_nac_sat_ll_s0: 700*0b57cec5SDimitry Andric case M2_mpy_nac_sat_ll_s1: 701*0b57cec5SDimitry Andric case M2_mpy_rnd_ll_s0: 702*0b57cec5SDimitry Andric case M2_mpy_rnd_ll_s1: 703*0b57cec5SDimitry Andric case M2_mpy_sat_ll_s0: 704*0b57cec5SDimitry Andric case M2_mpy_sat_ll_s1: 705*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_ll_s0: 706*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_ll_s1: 707*0b57cec5SDimitry Andric case M2_mpyd_acc_ll_s0: 708*0b57cec5SDimitry Andric case M2_mpyd_acc_ll_s1: 709*0b57cec5SDimitry Andric case M2_mpyd_ll_s0: 710*0b57cec5SDimitry Andric case M2_mpyd_ll_s1: 711*0b57cec5SDimitry Andric case M2_mpyd_nac_ll_s0: 712*0b57cec5SDimitry Andric case M2_mpyd_nac_ll_s1: 713*0b57cec5SDimitry Andric case M2_mpyd_rnd_ll_s0: 714*0b57cec5SDimitry Andric case M2_mpyd_rnd_ll_s1: 715*0b57cec5SDimitry Andric case M2_mpyu_acc_ll_s0: 716*0b57cec5SDimitry Andric case M2_mpyu_acc_ll_s1: 717*0b57cec5SDimitry Andric case M2_mpyu_ll_s0: 718*0b57cec5SDimitry Andric case M2_mpyu_ll_s1: 719*0b57cec5SDimitry Andric case M2_mpyu_nac_ll_s0: 720*0b57cec5SDimitry Andric case M2_mpyu_nac_ll_s1: 721*0b57cec5SDimitry Andric case M2_mpyud_acc_ll_s0: 722*0b57cec5SDimitry Andric case M2_mpyud_acc_ll_s1: 723*0b57cec5SDimitry Andric case M2_mpyud_ll_s0: 724*0b57cec5SDimitry Andric case M2_mpyud_ll_s1: 725*0b57cec5SDimitry Andric case M2_mpyud_nac_ll_s0: 726*0b57cec5SDimitry Andric case M2_mpyud_nac_ll_s1: 727*0b57cec5SDimitry Andric if (OpN == 1 || OpN == 2) { 728*0b57cec5SDimitry Andric Bits.set(Begin, Begin+16); 729*0b57cec5SDimitry Andric return true; 730*0b57cec5SDimitry Andric } 731*0b57cec5SDimitry Andric break; 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric // Two register sources. Used bits: R1[0-15], R2[16-31]. 734*0b57cec5SDimitry Andric case A2_addh_h16_lh: 735*0b57cec5SDimitry Andric case A2_addh_h16_sat_lh: 736*0b57cec5SDimitry Andric case A2_combine_lh: 737*0b57cec5SDimitry Andric case A2_subh_h16_lh: 738*0b57cec5SDimitry Andric case A2_subh_h16_sat_lh: 739*0b57cec5SDimitry Andric case M2_mpy_acc_lh_s0: 740*0b57cec5SDimitry Andric case M2_mpy_acc_lh_s1: 741*0b57cec5SDimitry Andric case M2_mpy_acc_sat_lh_s0: 742*0b57cec5SDimitry Andric case M2_mpy_acc_sat_lh_s1: 743*0b57cec5SDimitry Andric case M2_mpy_lh_s0: 744*0b57cec5SDimitry Andric case M2_mpy_lh_s1: 745*0b57cec5SDimitry Andric case M2_mpy_nac_lh_s0: 746*0b57cec5SDimitry Andric case M2_mpy_nac_lh_s1: 747*0b57cec5SDimitry Andric case M2_mpy_nac_sat_lh_s0: 748*0b57cec5SDimitry Andric case M2_mpy_nac_sat_lh_s1: 749*0b57cec5SDimitry Andric case M2_mpy_rnd_lh_s0: 750*0b57cec5SDimitry Andric case M2_mpy_rnd_lh_s1: 751*0b57cec5SDimitry Andric case M2_mpy_sat_lh_s0: 752*0b57cec5SDimitry Andric case M2_mpy_sat_lh_s1: 753*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_lh_s0: 754*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_lh_s1: 755*0b57cec5SDimitry Andric case M2_mpyd_acc_lh_s0: 756*0b57cec5SDimitry Andric case M2_mpyd_acc_lh_s1: 757*0b57cec5SDimitry Andric case M2_mpyd_lh_s0: 758*0b57cec5SDimitry Andric case M2_mpyd_lh_s1: 759*0b57cec5SDimitry Andric case M2_mpyd_nac_lh_s0: 760*0b57cec5SDimitry Andric case M2_mpyd_nac_lh_s1: 761*0b57cec5SDimitry Andric case M2_mpyd_rnd_lh_s0: 762*0b57cec5SDimitry Andric case M2_mpyd_rnd_lh_s1: 763*0b57cec5SDimitry Andric case M2_mpyu_acc_lh_s0: 764*0b57cec5SDimitry Andric case M2_mpyu_acc_lh_s1: 765*0b57cec5SDimitry Andric case M2_mpyu_lh_s0: 766*0b57cec5SDimitry Andric case M2_mpyu_lh_s1: 767*0b57cec5SDimitry Andric case M2_mpyu_nac_lh_s0: 768*0b57cec5SDimitry Andric case M2_mpyu_nac_lh_s1: 769*0b57cec5SDimitry Andric case M2_mpyud_acc_lh_s0: 770*0b57cec5SDimitry Andric case M2_mpyud_acc_lh_s1: 771*0b57cec5SDimitry Andric case M2_mpyud_lh_s0: 772*0b57cec5SDimitry Andric case M2_mpyud_lh_s1: 773*0b57cec5SDimitry Andric case M2_mpyud_nac_lh_s0: 774*0b57cec5SDimitry Andric case M2_mpyud_nac_lh_s1: 775*0b57cec5SDimitry Andric // These four are actually LH. 776*0b57cec5SDimitry Andric case A2_addh_l16_hl: 777*0b57cec5SDimitry Andric case A2_addh_l16_sat_hl: 778*0b57cec5SDimitry Andric case A2_subh_l16_hl: 779*0b57cec5SDimitry Andric case A2_subh_l16_sat_hl: 780*0b57cec5SDimitry Andric if (OpN == 1) { 781*0b57cec5SDimitry Andric Bits.set(Begin, Begin+16); 782*0b57cec5SDimitry Andric return true; 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric if (OpN == 2) { 785*0b57cec5SDimitry Andric Bits.set(Begin+16, Begin+32); 786*0b57cec5SDimitry Andric return true; 787*0b57cec5SDimitry Andric } 788*0b57cec5SDimitry Andric break; 789*0b57cec5SDimitry Andric 790*0b57cec5SDimitry Andric // Two register sources, used bits: R1[16-31], R2[0-15]. 791*0b57cec5SDimitry Andric case A2_addh_h16_hl: 792*0b57cec5SDimitry Andric case A2_addh_h16_sat_hl: 793*0b57cec5SDimitry Andric case A2_combine_hl: 794*0b57cec5SDimitry Andric case A2_subh_h16_hl: 795*0b57cec5SDimitry Andric case A2_subh_h16_sat_hl: 796*0b57cec5SDimitry Andric case M2_mpy_acc_hl_s0: 797*0b57cec5SDimitry Andric case M2_mpy_acc_hl_s1: 798*0b57cec5SDimitry Andric case M2_mpy_acc_sat_hl_s0: 799*0b57cec5SDimitry Andric case M2_mpy_acc_sat_hl_s1: 800*0b57cec5SDimitry Andric case M2_mpy_hl_s0: 801*0b57cec5SDimitry Andric case M2_mpy_hl_s1: 802*0b57cec5SDimitry Andric case M2_mpy_nac_hl_s0: 803*0b57cec5SDimitry Andric case M2_mpy_nac_hl_s1: 804*0b57cec5SDimitry Andric case M2_mpy_nac_sat_hl_s0: 805*0b57cec5SDimitry Andric case M2_mpy_nac_sat_hl_s1: 806*0b57cec5SDimitry Andric case M2_mpy_rnd_hl_s0: 807*0b57cec5SDimitry Andric case M2_mpy_rnd_hl_s1: 808*0b57cec5SDimitry Andric case M2_mpy_sat_hl_s0: 809*0b57cec5SDimitry Andric case M2_mpy_sat_hl_s1: 810*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_hl_s0: 811*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_hl_s1: 812*0b57cec5SDimitry Andric case M2_mpyd_acc_hl_s0: 813*0b57cec5SDimitry Andric case M2_mpyd_acc_hl_s1: 814*0b57cec5SDimitry Andric case M2_mpyd_hl_s0: 815*0b57cec5SDimitry Andric case M2_mpyd_hl_s1: 816*0b57cec5SDimitry Andric case M2_mpyd_nac_hl_s0: 817*0b57cec5SDimitry Andric case M2_mpyd_nac_hl_s1: 818*0b57cec5SDimitry Andric case M2_mpyd_rnd_hl_s0: 819*0b57cec5SDimitry Andric case M2_mpyd_rnd_hl_s1: 820*0b57cec5SDimitry Andric case M2_mpyu_acc_hl_s0: 821*0b57cec5SDimitry Andric case M2_mpyu_acc_hl_s1: 822*0b57cec5SDimitry Andric case M2_mpyu_hl_s0: 823*0b57cec5SDimitry Andric case M2_mpyu_hl_s1: 824*0b57cec5SDimitry Andric case M2_mpyu_nac_hl_s0: 825*0b57cec5SDimitry Andric case M2_mpyu_nac_hl_s1: 826*0b57cec5SDimitry Andric case M2_mpyud_acc_hl_s0: 827*0b57cec5SDimitry Andric case M2_mpyud_acc_hl_s1: 828*0b57cec5SDimitry Andric case M2_mpyud_hl_s0: 829*0b57cec5SDimitry Andric case M2_mpyud_hl_s1: 830*0b57cec5SDimitry Andric case M2_mpyud_nac_hl_s0: 831*0b57cec5SDimitry Andric case M2_mpyud_nac_hl_s1: 832*0b57cec5SDimitry Andric if (OpN == 1) { 833*0b57cec5SDimitry Andric Bits.set(Begin+16, Begin+32); 834*0b57cec5SDimitry Andric return true; 835*0b57cec5SDimitry Andric } 836*0b57cec5SDimitry Andric if (OpN == 2) { 837*0b57cec5SDimitry Andric Bits.set(Begin, Begin+16); 838*0b57cec5SDimitry Andric return true; 839*0b57cec5SDimitry Andric } 840*0b57cec5SDimitry Andric break; 841*0b57cec5SDimitry Andric 842*0b57cec5SDimitry Andric // Two register sources, used bits: R1[16-31], R2[16-31]. 843*0b57cec5SDimitry Andric case A2_addh_h16_hh: 844*0b57cec5SDimitry Andric case A2_addh_h16_sat_hh: 845*0b57cec5SDimitry Andric case A2_combine_hh: 846*0b57cec5SDimitry Andric case A2_subh_h16_hh: 847*0b57cec5SDimitry Andric case A2_subh_h16_sat_hh: 848*0b57cec5SDimitry Andric case M2_mpy_acc_hh_s0: 849*0b57cec5SDimitry Andric case M2_mpy_acc_hh_s1: 850*0b57cec5SDimitry Andric case M2_mpy_acc_sat_hh_s0: 851*0b57cec5SDimitry Andric case M2_mpy_acc_sat_hh_s1: 852*0b57cec5SDimitry Andric case M2_mpy_hh_s0: 853*0b57cec5SDimitry Andric case M2_mpy_hh_s1: 854*0b57cec5SDimitry Andric case M2_mpy_nac_hh_s0: 855*0b57cec5SDimitry Andric case M2_mpy_nac_hh_s1: 856*0b57cec5SDimitry Andric case M2_mpy_nac_sat_hh_s0: 857*0b57cec5SDimitry Andric case M2_mpy_nac_sat_hh_s1: 858*0b57cec5SDimitry Andric case M2_mpy_rnd_hh_s0: 859*0b57cec5SDimitry Andric case M2_mpy_rnd_hh_s1: 860*0b57cec5SDimitry Andric case M2_mpy_sat_hh_s0: 861*0b57cec5SDimitry Andric case M2_mpy_sat_hh_s1: 862*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_hh_s0: 863*0b57cec5SDimitry Andric case M2_mpy_sat_rnd_hh_s1: 864*0b57cec5SDimitry Andric case M2_mpyd_acc_hh_s0: 865*0b57cec5SDimitry Andric case M2_mpyd_acc_hh_s1: 866*0b57cec5SDimitry Andric case M2_mpyd_hh_s0: 867*0b57cec5SDimitry Andric case M2_mpyd_hh_s1: 868*0b57cec5SDimitry Andric case M2_mpyd_nac_hh_s0: 869*0b57cec5SDimitry Andric case M2_mpyd_nac_hh_s1: 870*0b57cec5SDimitry Andric case M2_mpyd_rnd_hh_s0: 871*0b57cec5SDimitry Andric case M2_mpyd_rnd_hh_s1: 872*0b57cec5SDimitry Andric case M2_mpyu_acc_hh_s0: 873*0b57cec5SDimitry Andric case M2_mpyu_acc_hh_s1: 874*0b57cec5SDimitry Andric case M2_mpyu_hh_s0: 875*0b57cec5SDimitry Andric case M2_mpyu_hh_s1: 876*0b57cec5SDimitry Andric case M2_mpyu_nac_hh_s0: 877*0b57cec5SDimitry Andric case M2_mpyu_nac_hh_s1: 878*0b57cec5SDimitry Andric case M2_mpyud_acc_hh_s0: 879*0b57cec5SDimitry Andric case M2_mpyud_acc_hh_s1: 880*0b57cec5SDimitry Andric case M2_mpyud_hh_s0: 881*0b57cec5SDimitry Andric case M2_mpyud_hh_s1: 882*0b57cec5SDimitry Andric case M2_mpyud_nac_hh_s0: 883*0b57cec5SDimitry Andric case M2_mpyud_nac_hh_s1: 884*0b57cec5SDimitry Andric if (OpN == 1 || OpN == 2) { 885*0b57cec5SDimitry Andric Bits.set(Begin+16, Begin+32); 886*0b57cec5SDimitry Andric return true; 887*0b57cec5SDimitry Andric } 888*0b57cec5SDimitry Andric break; 889*0b57cec5SDimitry Andric } 890*0b57cec5SDimitry Andric 891*0b57cec5SDimitry Andric return false; 892*0b57cec5SDimitry Andric } 893*0b57cec5SDimitry Andric 894*0b57cec5SDimitry Andric // Calculate the register class that matches Reg:Sub. For example, if 895*0b57cec5SDimitry Andric // %1 is a double register, then %1:isub_hi would match the "int" 896*0b57cec5SDimitry Andric // register class. 897*0b57cec5SDimitry Andric const TargetRegisterClass *HexagonBitSimplify::getFinalVRegClass( 898*0b57cec5SDimitry Andric const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI) { 899*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(RR.Reg)) 900*0b57cec5SDimitry Andric return nullptr; 901*0b57cec5SDimitry Andric auto *RC = MRI.getRegClass(RR.Reg); 902*0b57cec5SDimitry Andric if (RR.Sub == 0) 903*0b57cec5SDimitry Andric return RC; 904*0b57cec5SDimitry Andric auto &HRI = static_cast<const HexagonRegisterInfo&>( 905*0b57cec5SDimitry Andric *MRI.getTargetRegisterInfo()); 906*0b57cec5SDimitry Andric 907*0b57cec5SDimitry Andric auto VerifySR = [&HRI] (const TargetRegisterClass *RC, unsigned Sub) -> void { 908*0b57cec5SDimitry Andric (void)HRI; 909*0b57cec5SDimitry Andric assert(Sub == HRI.getHexagonSubRegIndex(*RC, Hexagon::ps_sub_lo) || 910*0b57cec5SDimitry Andric Sub == HRI.getHexagonSubRegIndex(*RC, Hexagon::ps_sub_hi)); 911*0b57cec5SDimitry Andric }; 912*0b57cec5SDimitry Andric 913*0b57cec5SDimitry Andric switch (RC->getID()) { 914*0b57cec5SDimitry Andric case Hexagon::DoubleRegsRegClassID: 915*0b57cec5SDimitry Andric VerifySR(RC, RR.Sub); 916*0b57cec5SDimitry Andric return &Hexagon::IntRegsRegClass; 917*0b57cec5SDimitry Andric case Hexagon::HvxWRRegClassID: 918*0b57cec5SDimitry Andric VerifySR(RC, RR.Sub); 919*0b57cec5SDimitry Andric return &Hexagon::HvxVRRegClass; 920*0b57cec5SDimitry Andric } 921*0b57cec5SDimitry Andric return nullptr; 922*0b57cec5SDimitry Andric } 923*0b57cec5SDimitry Andric 924*0b57cec5SDimitry Andric // Check if RD could be replaced with RS at any possible use of RD. 925*0b57cec5SDimitry Andric // For example a predicate register cannot be replaced with a integer 926*0b57cec5SDimitry Andric // register, but a 64-bit register with a subregister can be replaced 927*0b57cec5SDimitry Andric // with a 32-bit register. 928*0b57cec5SDimitry Andric bool HexagonBitSimplify::isTransparentCopy(const BitTracker::RegisterRef &RD, 929*0b57cec5SDimitry Andric const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI) { 930*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(RD.Reg) || 931*0b57cec5SDimitry Andric !TargetRegisterInfo::isVirtualRegister(RS.Reg)) 932*0b57cec5SDimitry Andric return false; 933*0b57cec5SDimitry Andric // Return false if one (or both) classes are nullptr. 934*0b57cec5SDimitry Andric auto *DRC = getFinalVRegClass(RD, MRI); 935*0b57cec5SDimitry Andric if (!DRC) 936*0b57cec5SDimitry Andric return false; 937*0b57cec5SDimitry Andric 938*0b57cec5SDimitry Andric return DRC == getFinalVRegClass(RS, MRI); 939*0b57cec5SDimitry Andric } 940*0b57cec5SDimitry Andric 941*0b57cec5SDimitry Andric bool HexagonBitSimplify::hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI, 942*0b57cec5SDimitry Andric unsigned NewSub) { 943*0b57cec5SDimitry Andric if (!PreserveTiedOps) 944*0b57cec5SDimitry Andric return false; 945*0b57cec5SDimitry Andric return llvm::any_of(MRI.use_operands(Reg), 946*0b57cec5SDimitry Andric [NewSub] (const MachineOperand &Op) -> bool { 947*0b57cec5SDimitry Andric return Op.getSubReg() != NewSub && Op.isTied(); 948*0b57cec5SDimitry Andric }); 949*0b57cec5SDimitry Andric } 950*0b57cec5SDimitry Andric 951*0b57cec5SDimitry Andric namespace { 952*0b57cec5SDimitry Andric 953*0b57cec5SDimitry Andric class DeadCodeElimination { 954*0b57cec5SDimitry Andric public: 955*0b57cec5SDimitry Andric DeadCodeElimination(MachineFunction &mf, MachineDominatorTree &mdt) 956*0b57cec5SDimitry Andric : MF(mf), HII(*MF.getSubtarget<HexagonSubtarget>().getInstrInfo()), 957*0b57cec5SDimitry Andric MDT(mdt), MRI(mf.getRegInfo()) {} 958*0b57cec5SDimitry Andric 959*0b57cec5SDimitry Andric bool run() { 960*0b57cec5SDimitry Andric return runOnNode(MDT.getRootNode()); 961*0b57cec5SDimitry Andric } 962*0b57cec5SDimitry Andric 963*0b57cec5SDimitry Andric private: 964*0b57cec5SDimitry Andric bool isDead(unsigned R) const; 965*0b57cec5SDimitry Andric bool runOnNode(MachineDomTreeNode *N); 966*0b57cec5SDimitry Andric 967*0b57cec5SDimitry Andric MachineFunction &MF; 968*0b57cec5SDimitry Andric const HexagonInstrInfo &HII; 969*0b57cec5SDimitry Andric MachineDominatorTree &MDT; 970*0b57cec5SDimitry Andric MachineRegisterInfo &MRI; 971*0b57cec5SDimitry Andric }; 972*0b57cec5SDimitry Andric 973*0b57cec5SDimitry Andric } // end anonymous namespace 974*0b57cec5SDimitry Andric 975*0b57cec5SDimitry Andric bool DeadCodeElimination::isDead(unsigned R) const { 976*0b57cec5SDimitry Andric for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) { 977*0b57cec5SDimitry Andric MachineInstr *UseI = I->getParent(); 978*0b57cec5SDimitry Andric if (UseI->isDebugValue()) 979*0b57cec5SDimitry Andric continue; 980*0b57cec5SDimitry Andric if (UseI->isPHI()) { 981*0b57cec5SDimitry Andric assert(!UseI->getOperand(0).getSubReg()); 982*0b57cec5SDimitry Andric unsigned DR = UseI->getOperand(0).getReg(); 983*0b57cec5SDimitry Andric if (DR == R) 984*0b57cec5SDimitry Andric continue; 985*0b57cec5SDimitry Andric } 986*0b57cec5SDimitry Andric return false; 987*0b57cec5SDimitry Andric } 988*0b57cec5SDimitry Andric return true; 989*0b57cec5SDimitry Andric } 990*0b57cec5SDimitry Andric 991*0b57cec5SDimitry Andric bool DeadCodeElimination::runOnNode(MachineDomTreeNode *N) { 992*0b57cec5SDimitry Andric bool Changed = false; 993*0b57cec5SDimitry Andric 994*0b57cec5SDimitry Andric for (auto *DTN : children<MachineDomTreeNode*>(N)) 995*0b57cec5SDimitry Andric Changed |= runOnNode(DTN); 996*0b57cec5SDimitry Andric 997*0b57cec5SDimitry Andric MachineBasicBlock *B = N->getBlock(); 998*0b57cec5SDimitry Andric std::vector<MachineInstr*> Instrs; 999*0b57cec5SDimitry Andric for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) 1000*0b57cec5SDimitry Andric Instrs.push_back(&*I); 1001*0b57cec5SDimitry Andric 1002*0b57cec5SDimitry Andric for (auto MI : Instrs) { 1003*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 1004*0b57cec5SDimitry Andric // Do not touch lifetime markers. This is why the target-independent DCE 1005*0b57cec5SDimitry Andric // cannot be used. 1006*0b57cec5SDimitry Andric if (Opc == TargetOpcode::LIFETIME_START || 1007*0b57cec5SDimitry Andric Opc == TargetOpcode::LIFETIME_END) 1008*0b57cec5SDimitry Andric continue; 1009*0b57cec5SDimitry Andric bool Store = false; 1010*0b57cec5SDimitry Andric if (MI->isInlineAsm()) 1011*0b57cec5SDimitry Andric continue; 1012*0b57cec5SDimitry Andric // Delete PHIs if possible. 1013*0b57cec5SDimitry Andric if (!MI->isPHI() && !MI->isSafeToMove(nullptr, Store)) 1014*0b57cec5SDimitry Andric continue; 1015*0b57cec5SDimitry Andric 1016*0b57cec5SDimitry Andric bool AllDead = true; 1017*0b57cec5SDimitry Andric SmallVector<unsigned,2> Regs; 1018*0b57cec5SDimitry Andric for (auto &Op : MI->operands()) { 1019*0b57cec5SDimitry Andric if (!Op.isReg() || !Op.isDef()) 1020*0b57cec5SDimitry Andric continue; 1021*0b57cec5SDimitry Andric unsigned R = Op.getReg(); 1022*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(R) || !isDead(R)) { 1023*0b57cec5SDimitry Andric AllDead = false; 1024*0b57cec5SDimitry Andric break; 1025*0b57cec5SDimitry Andric } 1026*0b57cec5SDimitry Andric Regs.push_back(R); 1027*0b57cec5SDimitry Andric } 1028*0b57cec5SDimitry Andric if (!AllDead) 1029*0b57cec5SDimitry Andric continue; 1030*0b57cec5SDimitry Andric 1031*0b57cec5SDimitry Andric B->erase(MI); 1032*0b57cec5SDimitry Andric for (unsigned i = 0, n = Regs.size(); i != n; ++i) 1033*0b57cec5SDimitry Andric MRI.markUsesInDebugValueAsUndef(Regs[i]); 1034*0b57cec5SDimitry Andric Changed = true; 1035*0b57cec5SDimitry Andric } 1036*0b57cec5SDimitry Andric 1037*0b57cec5SDimitry Andric return Changed; 1038*0b57cec5SDimitry Andric } 1039*0b57cec5SDimitry Andric 1040*0b57cec5SDimitry Andric namespace { 1041*0b57cec5SDimitry Andric 1042*0b57cec5SDimitry Andric // Eliminate redundant instructions 1043*0b57cec5SDimitry Andric // 1044*0b57cec5SDimitry Andric // This transformation will identify instructions where the output register 1045*0b57cec5SDimitry Andric // is the same as one of its input registers. This only works on instructions 1046*0b57cec5SDimitry Andric // that define a single register (unlike post-increment loads, for example). 1047*0b57cec5SDimitry Andric // The equality check is actually more detailed: the code calculates which 1048*0b57cec5SDimitry Andric // bits of the output are used, and only compares these bits with the input 1049*0b57cec5SDimitry Andric // registers. 1050*0b57cec5SDimitry Andric // If the output matches an input, the instruction is replaced with COPY. 1051*0b57cec5SDimitry Andric // The copies will be removed by another transformation. 1052*0b57cec5SDimitry Andric class RedundantInstrElimination : public Transformation { 1053*0b57cec5SDimitry Andric public: 1054*0b57cec5SDimitry Andric RedundantInstrElimination(BitTracker &bt, const HexagonInstrInfo &hii, 1055*0b57cec5SDimitry Andric const HexagonRegisterInfo &hri, MachineRegisterInfo &mri) 1056*0b57cec5SDimitry Andric : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {} 1057*0b57cec5SDimitry Andric 1058*0b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override; 1059*0b57cec5SDimitry Andric 1060*0b57cec5SDimitry Andric private: 1061*0b57cec5SDimitry Andric bool isLossyShiftLeft(const MachineInstr &MI, unsigned OpN, 1062*0b57cec5SDimitry Andric unsigned &LostB, unsigned &LostE); 1063*0b57cec5SDimitry Andric bool isLossyShiftRight(const MachineInstr &MI, unsigned OpN, 1064*0b57cec5SDimitry Andric unsigned &LostB, unsigned &LostE); 1065*0b57cec5SDimitry Andric bool computeUsedBits(unsigned Reg, BitVector &Bits); 1066*0b57cec5SDimitry Andric bool computeUsedBits(const MachineInstr &MI, unsigned OpN, BitVector &Bits, 1067*0b57cec5SDimitry Andric uint16_t Begin); 1068*0b57cec5SDimitry Andric bool usedBitsEqual(BitTracker::RegisterRef RD, BitTracker::RegisterRef RS); 1069*0b57cec5SDimitry Andric 1070*0b57cec5SDimitry Andric const HexagonInstrInfo &HII; 1071*0b57cec5SDimitry Andric const HexagonRegisterInfo &HRI; 1072*0b57cec5SDimitry Andric MachineRegisterInfo &MRI; 1073*0b57cec5SDimitry Andric BitTracker &BT; 1074*0b57cec5SDimitry Andric }; 1075*0b57cec5SDimitry Andric 1076*0b57cec5SDimitry Andric } // end anonymous namespace 1077*0b57cec5SDimitry Andric 1078*0b57cec5SDimitry Andric // Check if the instruction is a lossy shift left, where the input being 1079*0b57cec5SDimitry Andric // shifted is the operand OpN of MI. If true, [LostB, LostE) is the range 1080*0b57cec5SDimitry Andric // of bit indices that are lost. 1081*0b57cec5SDimitry Andric bool RedundantInstrElimination::isLossyShiftLeft(const MachineInstr &MI, 1082*0b57cec5SDimitry Andric unsigned OpN, unsigned &LostB, unsigned &LostE) { 1083*0b57cec5SDimitry Andric using namespace Hexagon; 1084*0b57cec5SDimitry Andric 1085*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1086*0b57cec5SDimitry Andric unsigned ImN, RegN, Width; 1087*0b57cec5SDimitry Andric switch (Opc) { 1088*0b57cec5SDimitry Andric case S2_asl_i_p: 1089*0b57cec5SDimitry Andric ImN = 2; 1090*0b57cec5SDimitry Andric RegN = 1; 1091*0b57cec5SDimitry Andric Width = 64; 1092*0b57cec5SDimitry Andric break; 1093*0b57cec5SDimitry Andric case S2_asl_i_p_acc: 1094*0b57cec5SDimitry Andric case S2_asl_i_p_and: 1095*0b57cec5SDimitry Andric case S2_asl_i_p_nac: 1096*0b57cec5SDimitry Andric case S2_asl_i_p_or: 1097*0b57cec5SDimitry Andric case S2_asl_i_p_xacc: 1098*0b57cec5SDimitry Andric ImN = 3; 1099*0b57cec5SDimitry Andric RegN = 2; 1100*0b57cec5SDimitry Andric Width = 64; 1101*0b57cec5SDimitry Andric break; 1102*0b57cec5SDimitry Andric case S2_asl_i_r: 1103*0b57cec5SDimitry Andric ImN = 2; 1104*0b57cec5SDimitry Andric RegN = 1; 1105*0b57cec5SDimitry Andric Width = 32; 1106*0b57cec5SDimitry Andric break; 1107*0b57cec5SDimitry Andric case S2_addasl_rrri: 1108*0b57cec5SDimitry Andric case S4_andi_asl_ri: 1109*0b57cec5SDimitry Andric case S4_ori_asl_ri: 1110*0b57cec5SDimitry Andric case S4_addi_asl_ri: 1111*0b57cec5SDimitry Andric case S4_subi_asl_ri: 1112*0b57cec5SDimitry Andric case S2_asl_i_r_acc: 1113*0b57cec5SDimitry Andric case S2_asl_i_r_and: 1114*0b57cec5SDimitry Andric case S2_asl_i_r_nac: 1115*0b57cec5SDimitry Andric case S2_asl_i_r_or: 1116*0b57cec5SDimitry Andric case S2_asl_i_r_sat: 1117*0b57cec5SDimitry Andric case S2_asl_i_r_xacc: 1118*0b57cec5SDimitry Andric ImN = 3; 1119*0b57cec5SDimitry Andric RegN = 2; 1120*0b57cec5SDimitry Andric Width = 32; 1121*0b57cec5SDimitry Andric break; 1122*0b57cec5SDimitry Andric default: 1123*0b57cec5SDimitry Andric return false; 1124*0b57cec5SDimitry Andric } 1125*0b57cec5SDimitry Andric 1126*0b57cec5SDimitry Andric if (RegN != OpN) 1127*0b57cec5SDimitry Andric return false; 1128*0b57cec5SDimitry Andric 1129*0b57cec5SDimitry Andric assert(MI.getOperand(ImN).isImm()); 1130*0b57cec5SDimitry Andric unsigned S = MI.getOperand(ImN).getImm(); 1131*0b57cec5SDimitry Andric if (S == 0) 1132*0b57cec5SDimitry Andric return false; 1133*0b57cec5SDimitry Andric LostB = Width-S; 1134*0b57cec5SDimitry Andric LostE = Width; 1135*0b57cec5SDimitry Andric return true; 1136*0b57cec5SDimitry Andric } 1137*0b57cec5SDimitry Andric 1138*0b57cec5SDimitry Andric // Check if the instruction is a lossy shift right, where the input being 1139*0b57cec5SDimitry Andric // shifted is the operand OpN of MI. If true, [LostB, LostE) is the range 1140*0b57cec5SDimitry Andric // of bit indices that are lost. 1141*0b57cec5SDimitry Andric bool RedundantInstrElimination::isLossyShiftRight(const MachineInstr &MI, 1142*0b57cec5SDimitry Andric unsigned OpN, unsigned &LostB, unsigned &LostE) { 1143*0b57cec5SDimitry Andric using namespace Hexagon; 1144*0b57cec5SDimitry Andric 1145*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1146*0b57cec5SDimitry Andric unsigned ImN, RegN; 1147*0b57cec5SDimitry Andric switch (Opc) { 1148*0b57cec5SDimitry Andric case S2_asr_i_p: 1149*0b57cec5SDimitry Andric case S2_lsr_i_p: 1150*0b57cec5SDimitry Andric ImN = 2; 1151*0b57cec5SDimitry Andric RegN = 1; 1152*0b57cec5SDimitry Andric break; 1153*0b57cec5SDimitry Andric case S2_asr_i_p_acc: 1154*0b57cec5SDimitry Andric case S2_asr_i_p_and: 1155*0b57cec5SDimitry Andric case S2_asr_i_p_nac: 1156*0b57cec5SDimitry Andric case S2_asr_i_p_or: 1157*0b57cec5SDimitry Andric case S2_lsr_i_p_acc: 1158*0b57cec5SDimitry Andric case S2_lsr_i_p_and: 1159*0b57cec5SDimitry Andric case S2_lsr_i_p_nac: 1160*0b57cec5SDimitry Andric case S2_lsr_i_p_or: 1161*0b57cec5SDimitry Andric case S2_lsr_i_p_xacc: 1162*0b57cec5SDimitry Andric ImN = 3; 1163*0b57cec5SDimitry Andric RegN = 2; 1164*0b57cec5SDimitry Andric break; 1165*0b57cec5SDimitry Andric case S2_asr_i_r: 1166*0b57cec5SDimitry Andric case S2_lsr_i_r: 1167*0b57cec5SDimitry Andric ImN = 2; 1168*0b57cec5SDimitry Andric RegN = 1; 1169*0b57cec5SDimitry Andric break; 1170*0b57cec5SDimitry Andric case S4_andi_lsr_ri: 1171*0b57cec5SDimitry Andric case S4_ori_lsr_ri: 1172*0b57cec5SDimitry Andric case S4_addi_lsr_ri: 1173*0b57cec5SDimitry Andric case S4_subi_lsr_ri: 1174*0b57cec5SDimitry Andric case S2_asr_i_r_acc: 1175*0b57cec5SDimitry Andric case S2_asr_i_r_and: 1176*0b57cec5SDimitry Andric case S2_asr_i_r_nac: 1177*0b57cec5SDimitry Andric case S2_asr_i_r_or: 1178*0b57cec5SDimitry Andric case S2_lsr_i_r_acc: 1179*0b57cec5SDimitry Andric case S2_lsr_i_r_and: 1180*0b57cec5SDimitry Andric case S2_lsr_i_r_nac: 1181*0b57cec5SDimitry Andric case S2_lsr_i_r_or: 1182*0b57cec5SDimitry Andric case S2_lsr_i_r_xacc: 1183*0b57cec5SDimitry Andric ImN = 3; 1184*0b57cec5SDimitry Andric RegN = 2; 1185*0b57cec5SDimitry Andric break; 1186*0b57cec5SDimitry Andric 1187*0b57cec5SDimitry Andric default: 1188*0b57cec5SDimitry Andric return false; 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric if (RegN != OpN) 1192*0b57cec5SDimitry Andric return false; 1193*0b57cec5SDimitry Andric 1194*0b57cec5SDimitry Andric assert(MI.getOperand(ImN).isImm()); 1195*0b57cec5SDimitry Andric unsigned S = MI.getOperand(ImN).getImm(); 1196*0b57cec5SDimitry Andric LostB = 0; 1197*0b57cec5SDimitry Andric LostE = S; 1198*0b57cec5SDimitry Andric return true; 1199*0b57cec5SDimitry Andric } 1200*0b57cec5SDimitry Andric 1201*0b57cec5SDimitry Andric // Calculate the bit vector that corresponds to the used bits of register Reg. 1202*0b57cec5SDimitry Andric // The vector Bits has the same size, as the size of Reg in bits. If the cal- 1203*0b57cec5SDimitry Andric // culation fails (i.e. the used bits are unknown), it returns false. Other- 1204*0b57cec5SDimitry Andric // wise, it returns true and sets the corresponding bits in Bits. 1205*0b57cec5SDimitry Andric bool RedundantInstrElimination::computeUsedBits(unsigned Reg, BitVector &Bits) { 1206*0b57cec5SDimitry Andric BitVector Used(Bits.size()); 1207*0b57cec5SDimitry Andric RegisterSet Visited; 1208*0b57cec5SDimitry Andric std::vector<unsigned> Pending; 1209*0b57cec5SDimitry Andric Pending.push_back(Reg); 1210*0b57cec5SDimitry Andric 1211*0b57cec5SDimitry Andric for (unsigned i = 0; i < Pending.size(); ++i) { 1212*0b57cec5SDimitry Andric unsigned R = Pending[i]; 1213*0b57cec5SDimitry Andric if (Visited.has(R)) 1214*0b57cec5SDimitry Andric continue; 1215*0b57cec5SDimitry Andric Visited.insert(R); 1216*0b57cec5SDimitry Andric for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) { 1217*0b57cec5SDimitry Andric BitTracker::RegisterRef UR = *I; 1218*0b57cec5SDimitry Andric unsigned B, W; 1219*0b57cec5SDimitry Andric if (!HBS::getSubregMask(UR, B, W, MRI)) 1220*0b57cec5SDimitry Andric return false; 1221*0b57cec5SDimitry Andric MachineInstr &UseI = *I->getParent(); 1222*0b57cec5SDimitry Andric if (UseI.isPHI() || UseI.isCopy()) { 1223*0b57cec5SDimitry Andric unsigned DefR = UseI.getOperand(0).getReg(); 1224*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DefR)) 1225*0b57cec5SDimitry Andric return false; 1226*0b57cec5SDimitry Andric Pending.push_back(DefR); 1227*0b57cec5SDimitry Andric } else { 1228*0b57cec5SDimitry Andric if (!computeUsedBits(UseI, I.getOperandNo(), Used, B)) 1229*0b57cec5SDimitry Andric return false; 1230*0b57cec5SDimitry Andric } 1231*0b57cec5SDimitry Andric } 1232*0b57cec5SDimitry Andric } 1233*0b57cec5SDimitry Andric Bits |= Used; 1234*0b57cec5SDimitry Andric return true; 1235*0b57cec5SDimitry Andric } 1236*0b57cec5SDimitry Andric 1237*0b57cec5SDimitry Andric // Calculate the bits used by instruction MI in a register in operand OpN. 1238*0b57cec5SDimitry Andric // Return true/false if the calculation succeeds/fails. If is succeeds, set 1239*0b57cec5SDimitry Andric // used bits in Bits. This function does not reset any bits in Bits, so 1240*0b57cec5SDimitry Andric // subsequent calls over different instructions will result in the union 1241*0b57cec5SDimitry Andric // of the used bits in all these instructions. 1242*0b57cec5SDimitry Andric // The register in question may be used with a sub-register, whereas Bits 1243*0b57cec5SDimitry Andric // holds the bits for the entire register. To keep track of that, the 1244*0b57cec5SDimitry Andric // argument Begin indicates where in Bits is the lowest-significant bit 1245*0b57cec5SDimitry Andric // of the register used in operand OpN. For example, in instruction: 1246*0b57cec5SDimitry Andric // %1 = S2_lsr_i_r %2:isub_hi, 10 1247*0b57cec5SDimitry Andric // the operand 1 is a 32-bit register, which happens to be a subregister 1248*0b57cec5SDimitry Andric // of the 64-bit register %2, and that subregister starts at position 32. 1249*0b57cec5SDimitry Andric // In this case Begin=32, since Bits[32] would be the lowest-significant bit 1250*0b57cec5SDimitry Andric // of %2:isub_hi. 1251*0b57cec5SDimitry Andric bool RedundantInstrElimination::computeUsedBits(const MachineInstr &MI, 1252*0b57cec5SDimitry Andric unsigned OpN, BitVector &Bits, uint16_t Begin) { 1253*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1254*0b57cec5SDimitry Andric BitVector T(Bits.size()); 1255*0b57cec5SDimitry Andric bool GotBits = HBS::getUsedBits(Opc, OpN, T, Begin, HII); 1256*0b57cec5SDimitry Andric // Even if we don't have bits yet, we could still provide some information 1257*0b57cec5SDimitry Andric // if the instruction is a lossy shift: the lost bits will be marked as 1258*0b57cec5SDimitry Andric // not used. 1259*0b57cec5SDimitry Andric unsigned LB, LE; 1260*0b57cec5SDimitry Andric if (isLossyShiftLeft(MI, OpN, LB, LE) || isLossyShiftRight(MI, OpN, LB, LE)) { 1261*0b57cec5SDimitry Andric assert(MI.getOperand(OpN).isReg()); 1262*0b57cec5SDimitry Andric BitTracker::RegisterRef RR = MI.getOperand(OpN); 1263*0b57cec5SDimitry Andric const TargetRegisterClass *RC = HBS::getFinalVRegClass(RR, MRI); 1264*0b57cec5SDimitry Andric uint16_t Width = HRI.getRegSizeInBits(*RC); 1265*0b57cec5SDimitry Andric 1266*0b57cec5SDimitry Andric if (!GotBits) 1267*0b57cec5SDimitry Andric T.set(Begin, Begin+Width); 1268*0b57cec5SDimitry Andric assert(LB <= LE && LB < Width && LE <= Width); 1269*0b57cec5SDimitry Andric T.reset(Begin+LB, Begin+LE); 1270*0b57cec5SDimitry Andric GotBits = true; 1271*0b57cec5SDimitry Andric } 1272*0b57cec5SDimitry Andric if (GotBits) 1273*0b57cec5SDimitry Andric Bits |= T; 1274*0b57cec5SDimitry Andric return GotBits; 1275*0b57cec5SDimitry Andric } 1276*0b57cec5SDimitry Andric 1277*0b57cec5SDimitry Andric // Calculates the used bits in RD ("defined register"), and checks if these 1278*0b57cec5SDimitry Andric // bits in RS ("used register") and RD are identical. 1279*0b57cec5SDimitry Andric bool RedundantInstrElimination::usedBitsEqual(BitTracker::RegisterRef RD, 1280*0b57cec5SDimitry Andric BitTracker::RegisterRef RS) { 1281*0b57cec5SDimitry Andric const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg); 1282*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg); 1283*0b57cec5SDimitry Andric 1284*0b57cec5SDimitry Andric unsigned DB, DW; 1285*0b57cec5SDimitry Andric if (!HBS::getSubregMask(RD, DB, DW, MRI)) 1286*0b57cec5SDimitry Andric return false; 1287*0b57cec5SDimitry Andric unsigned SB, SW; 1288*0b57cec5SDimitry Andric if (!HBS::getSubregMask(RS, SB, SW, MRI)) 1289*0b57cec5SDimitry Andric return false; 1290*0b57cec5SDimitry Andric if (SW != DW) 1291*0b57cec5SDimitry Andric return false; 1292*0b57cec5SDimitry Andric 1293*0b57cec5SDimitry Andric BitVector Used(DC.width()); 1294*0b57cec5SDimitry Andric if (!computeUsedBits(RD.Reg, Used)) 1295*0b57cec5SDimitry Andric return false; 1296*0b57cec5SDimitry Andric 1297*0b57cec5SDimitry Andric for (unsigned i = 0; i != DW; ++i) 1298*0b57cec5SDimitry Andric if (Used[i+DB] && DC[DB+i] != SC[SB+i]) 1299*0b57cec5SDimitry Andric return false; 1300*0b57cec5SDimitry Andric return true; 1301*0b57cec5SDimitry Andric } 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric bool RedundantInstrElimination::processBlock(MachineBasicBlock &B, 1304*0b57cec5SDimitry Andric const RegisterSet&) { 1305*0b57cec5SDimitry Andric if (!BT.reached(&B)) 1306*0b57cec5SDimitry Andric return false; 1307*0b57cec5SDimitry Andric bool Changed = false; 1308*0b57cec5SDimitry Andric 1309*0b57cec5SDimitry Andric for (auto I = B.begin(), E = B.end(), NextI = I; I != E; ++I) { 1310*0b57cec5SDimitry Andric NextI = std::next(I); 1311*0b57cec5SDimitry Andric MachineInstr *MI = &*I; 1312*0b57cec5SDimitry Andric 1313*0b57cec5SDimitry Andric if (MI->getOpcode() == TargetOpcode::COPY) 1314*0b57cec5SDimitry Andric continue; 1315*0b57cec5SDimitry Andric if (MI->isPHI() || MI->hasUnmodeledSideEffects() || MI->isInlineAsm()) 1316*0b57cec5SDimitry Andric continue; 1317*0b57cec5SDimitry Andric unsigned NumD = MI->getDesc().getNumDefs(); 1318*0b57cec5SDimitry Andric if (NumD != 1) 1319*0b57cec5SDimitry Andric continue; 1320*0b57cec5SDimitry Andric 1321*0b57cec5SDimitry Andric BitTracker::RegisterRef RD = MI->getOperand(0); 1322*0b57cec5SDimitry Andric if (!BT.has(RD.Reg)) 1323*0b57cec5SDimitry Andric continue; 1324*0b57cec5SDimitry Andric const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg); 1325*0b57cec5SDimitry Andric auto At = MachineBasicBlock::iterator(MI); 1326*0b57cec5SDimitry Andric 1327*0b57cec5SDimitry Andric // Find a source operand that is equal to the result. 1328*0b57cec5SDimitry Andric for (auto &Op : MI->uses()) { 1329*0b57cec5SDimitry Andric if (!Op.isReg()) 1330*0b57cec5SDimitry Andric continue; 1331*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = Op; 1332*0b57cec5SDimitry Andric if (!BT.has(RS.Reg)) 1333*0b57cec5SDimitry Andric continue; 1334*0b57cec5SDimitry Andric if (!HBS::isTransparentCopy(RD, RS, MRI)) 1335*0b57cec5SDimitry Andric continue; 1336*0b57cec5SDimitry Andric 1337*0b57cec5SDimitry Andric unsigned BN, BW; 1338*0b57cec5SDimitry Andric if (!HBS::getSubregMask(RS, BN, BW, MRI)) 1339*0b57cec5SDimitry Andric continue; 1340*0b57cec5SDimitry Andric 1341*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg); 1342*0b57cec5SDimitry Andric if (!usedBitsEqual(RD, RS) && !HBS::isEqual(DC, 0, SC, BN, BW)) 1343*0b57cec5SDimitry Andric continue; 1344*0b57cec5SDimitry Andric 1345*0b57cec5SDimitry Andric // If found, replace the instruction with a COPY. 1346*0b57cec5SDimitry Andric const DebugLoc &DL = MI->getDebugLoc(); 1347*0b57cec5SDimitry Andric const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI); 1348*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(FRC); 1349*0b57cec5SDimitry Andric MachineInstr *CopyI = 1350*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR) 1351*0b57cec5SDimitry Andric .addReg(RS.Reg, 0, RS.Sub); 1352*0b57cec5SDimitry Andric HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI); 1353*0b57cec5SDimitry Andric // This pass can create copies between registers that don't have the 1354*0b57cec5SDimitry Andric // exact same values. Updating the tracker has to involve updating 1355*0b57cec5SDimitry Andric // all dependent cells. Example: 1356*0b57cec5SDimitry Andric // %1 = inst %2 ; %1 != %2, but used bits are equal 1357*0b57cec5SDimitry Andric // 1358*0b57cec5SDimitry Andric // %3 = copy %2 ; <- inserted 1359*0b57cec5SDimitry Andric // ... = %3 ; <- replaced from %2 1360*0b57cec5SDimitry Andric // Indirectly, we can create a "copy" between %1 and %2 even 1361*0b57cec5SDimitry Andric // though their exact values do not match. 1362*0b57cec5SDimitry Andric BT.visit(*CopyI); 1363*0b57cec5SDimitry Andric Changed = true; 1364*0b57cec5SDimitry Andric break; 1365*0b57cec5SDimitry Andric } 1366*0b57cec5SDimitry Andric } 1367*0b57cec5SDimitry Andric 1368*0b57cec5SDimitry Andric return Changed; 1369*0b57cec5SDimitry Andric } 1370*0b57cec5SDimitry Andric 1371*0b57cec5SDimitry Andric namespace { 1372*0b57cec5SDimitry Andric 1373*0b57cec5SDimitry Andric // Recognize instructions that produce constant values known at compile-time. 1374*0b57cec5SDimitry Andric // Replace them with register definitions that load these constants directly. 1375*0b57cec5SDimitry Andric class ConstGeneration : public Transformation { 1376*0b57cec5SDimitry Andric public: 1377*0b57cec5SDimitry Andric ConstGeneration(BitTracker &bt, const HexagonInstrInfo &hii, 1378*0b57cec5SDimitry Andric MachineRegisterInfo &mri) 1379*0b57cec5SDimitry Andric : Transformation(true), HII(hii), MRI(mri), BT(bt) {} 1380*0b57cec5SDimitry Andric 1381*0b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override; 1382*0b57cec5SDimitry Andric static bool isTfrConst(const MachineInstr &MI); 1383*0b57cec5SDimitry Andric 1384*0b57cec5SDimitry Andric private: 1385*0b57cec5SDimitry Andric unsigned genTfrConst(const TargetRegisterClass *RC, int64_t C, 1386*0b57cec5SDimitry Andric MachineBasicBlock &B, MachineBasicBlock::iterator At, DebugLoc &DL); 1387*0b57cec5SDimitry Andric 1388*0b57cec5SDimitry Andric const HexagonInstrInfo &HII; 1389*0b57cec5SDimitry Andric MachineRegisterInfo &MRI; 1390*0b57cec5SDimitry Andric BitTracker &BT; 1391*0b57cec5SDimitry Andric }; 1392*0b57cec5SDimitry Andric 1393*0b57cec5SDimitry Andric } // end anonymous namespace 1394*0b57cec5SDimitry Andric 1395*0b57cec5SDimitry Andric bool ConstGeneration::isTfrConst(const MachineInstr &MI) { 1396*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1397*0b57cec5SDimitry Andric switch (Opc) { 1398*0b57cec5SDimitry Andric case Hexagon::A2_combineii: 1399*0b57cec5SDimitry Andric case Hexagon::A4_combineii: 1400*0b57cec5SDimitry Andric case Hexagon::A2_tfrsi: 1401*0b57cec5SDimitry Andric case Hexagon::A2_tfrpi: 1402*0b57cec5SDimitry Andric case Hexagon::PS_true: 1403*0b57cec5SDimitry Andric case Hexagon::PS_false: 1404*0b57cec5SDimitry Andric case Hexagon::CONST32: 1405*0b57cec5SDimitry Andric case Hexagon::CONST64: 1406*0b57cec5SDimitry Andric return true; 1407*0b57cec5SDimitry Andric } 1408*0b57cec5SDimitry Andric return false; 1409*0b57cec5SDimitry Andric } 1410*0b57cec5SDimitry Andric 1411*0b57cec5SDimitry Andric // Generate a transfer-immediate instruction that is appropriate for the 1412*0b57cec5SDimitry Andric // register class and the actual value being transferred. 1413*0b57cec5SDimitry Andric unsigned ConstGeneration::genTfrConst(const TargetRegisterClass *RC, int64_t C, 1414*0b57cec5SDimitry Andric MachineBasicBlock &B, MachineBasicBlock::iterator At, DebugLoc &DL) { 1415*0b57cec5SDimitry Andric unsigned Reg = MRI.createVirtualRegister(RC); 1416*0b57cec5SDimitry Andric if (RC == &Hexagon::IntRegsRegClass) { 1417*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), Reg) 1418*0b57cec5SDimitry Andric .addImm(int32_t(C)); 1419*0b57cec5SDimitry Andric return Reg; 1420*0b57cec5SDimitry Andric } 1421*0b57cec5SDimitry Andric 1422*0b57cec5SDimitry Andric if (RC == &Hexagon::DoubleRegsRegClass) { 1423*0b57cec5SDimitry Andric if (isInt<8>(C)) { 1424*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrpi), Reg) 1425*0b57cec5SDimitry Andric .addImm(C); 1426*0b57cec5SDimitry Andric return Reg; 1427*0b57cec5SDimitry Andric } 1428*0b57cec5SDimitry Andric 1429*0b57cec5SDimitry Andric unsigned Lo = Lo_32(C), Hi = Hi_32(C); 1430*0b57cec5SDimitry Andric if (isInt<8>(Lo) || isInt<8>(Hi)) { 1431*0b57cec5SDimitry Andric unsigned Opc = isInt<8>(Lo) ? Hexagon::A2_combineii 1432*0b57cec5SDimitry Andric : Hexagon::A4_combineii; 1433*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Opc), Reg) 1434*0b57cec5SDimitry Andric .addImm(int32_t(Hi)) 1435*0b57cec5SDimitry Andric .addImm(int32_t(Lo)); 1436*0b57cec5SDimitry Andric return Reg; 1437*0b57cec5SDimitry Andric } 1438*0b57cec5SDimitry Andric 1439*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::CONST64), Reg) 1440*0b57cec5SDimitry Andric .addImm(C); 1441*0b57cec5SDimitry Andric return Reg; 1442*0b57cec5SDimitry Andric } 1443*0b57cec5SDimitry Andric 1444*0b57cec5SDimitry Andric if (RC == &Hexagon::PredRegsRegClass) { 1445*0b57cec5SDimitry Andric unsigned Opc; 1446*0b57cec5SDimitry Andric if (C == 0) 1447*0b57cec5SDimitry Andric Opc = Hexagon::PS_false; 1448*0b57cec5SDimitry Andric else if ((C & 0xFF) == 0xFF) 1449*0b57cec5SDimitry Andric Opc = Hexagon::PS_true; 1450*0b57cec5SDimitry Andric else 1451*0b57cec5SDimitry Andric return 0; 1452*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Opc), Reg); 1453*0b57cec5SDimitry Andric return Reg; 1454*0b57cec5SDimitry Andric } 1455*0b57cec5SDimitry Andric 1456*0b57cec5SDimitry Andric return 0; 1457*0b57cec5SDimitry Andric } 1458*0b57cec5SDimitry Andric 1459*0b57cec5SDimitry Andric bool ConstGeneration::processBlock(MachineBasicBlock &B, const RegisterSet&) { 1460*0b57cec5SDimitry Andric if (!BT.reached(&B)) 1461*0b57cec5SDimitry Andric return false; 1462*0b57cec5SDimitry Andric bool Changed = false; 1463*0b57cec5SDimitry Andric RegisterSet Defs; 1464*0b57cec5SDimitry Andric 1465*0b57cec5SDimitry Andric for (auto I = B.begin(), E = B.end(); I != E; ++I) { 1466*0b57cec5SDimitry Andric if (isTfrConst(*I)) 1467*0b57cec5SDimitry Andric continue; 1468*0b57cec5SDimitry Andric Defs.clear(); 1469*0b57cec5SDimitry Andric HBS::getInstrDefs(*I, Defs); 1470*0b57cec5SDimitry Andric if (Defs.count() != 1) 1471*0b57cec5SDimitry Andric continue; 1472*0b57cec5SDimitry Andric unsigned DR = Defs.find_first(); 1473*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DR)) 1474*0b57cec5SDimitry Andric continue; 1475*0b57cec5SDimitry Andric uint64_t U; 1476*0b57cec5SDimitry Andric const BitTracker::RegisterCell &DRC = BT.lookup(DR); 1477*0b57cec5SDimitry Andric if (HBS::getConst(DRC, 0, DRC.width(), U)) { 1478*0b57cec5SDimitry Andric int64_t C = U; 1479*0b57cec5SDimitry Andric DebugLoc DL = I->getDebugLoc(); 1480*0b57cec5SDimitry Andric auto At = I->isPHI() ? B.getFirstNonPHI() : I; 1481*0b57cec5SDimitry Andric unsigned ImmReg = genTfrConst(MRI.getRegClass(DR), C, B, At, DL); 1482*0b57cec5SDimitry Andric if (ImmReg) { 1483*0b57cec5SDimitry Andric HBS::replaceReg(DR, ImmReg, MRI); 1484*0b57cec5SDimitry Andric BT.put(ImmReg, DRC); 1485*0b57cec5SDimitry Andric Changed = true; 1486*0b57cec5SDimitry Andric } 1487*0b57cec5SDimitry Andric } 1488*0b57cec5SDimitry Andric } 1489*0b57cec5SDimitry Andric return Changed; 1490*0b57cec5SDimitry Andric } 1491*0b57cec5SDimitry Andric 1492*0b57cec5SDimitry Andric namespace { 1493*0b57cec5SDimitry Andric 1494*0b57cec5SDimitry Andric // Identify pairs of available registers which hold identical values. 1495*0b57cec5SDimitry Andric // In such cases, only one of them needs to be calculated, the other one 1496*0b57cec5SDimitry Andric // will be defined as a copy of the first. 1497*0b57cec5SDimitry Andric class CopyGeneration : public Transformation { 1498*0b57cec5SDimitry Andric public: 1499*0b57cec5SDimitry Andric CopyGeneration(BitTracker &bt, const HexagonInstrInfo &hii, 1500*0b57cec5SDimitry Andric const HexagonRegisterInfo &hri, MachineRegisterInfo &mri) 1501*0b57cec5SDimitry Andric : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {} 1502*0b57cec5SDimitry Andric 1503*0b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override; 1504*0b57cec5SDimitry Andric 1505*0b57cec5SDimitry Andric private: 1506*0b57cec5SDimitry Andric bool findMatch(const BitTracker::RegisterRef &Inp, 1507*0b57cec5SDimitry Andric BitTracker::RegisterRef &Out, const RegisterSet &AVs); 1508*0b57cec5SDimitry Andric 1509*0b57cec5SDimitry Andric const HexagonInstrInfo &HII; 1510*0b57cec5SDimitry Andric const HexagonRegisterInfo &HRI; 1511*0b57cec5SDimitry Andric MachineRegisterInfo &MRI; 1512*0b57cec5SDimitry Andric BitTracker &BT; 1513*0b57cec5SDimitry Andric RegisterSet Forbidden; 1514*0b57cec5SDimitry Andric }; 1515*0b57cec5SDimitry Andric 1516*0b57cec5SDimitry Andric // Eliminate register copies RD = RS, by replacing the uses of RD with 1517*0b57cec5SDimitry Andric // with uses of RS. 1518*0b57cec5SDimitry Andric class CopyPropagation : public Transformation { 1519*0b57cec5SDimitry Andric public: 1520*0b57cec5SDimitry Andric CopyPropagation(const HexagonRegisterInfo &hri, MachineRegisterInfo &mri) 1521*0b57cec5SDimitry Andric : Transformation(false), HRI(hri), MRI(mri) {} 1522*0b57cec5SDimitry Andric 1523*0b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override; 1524*0b57cec5SDimitry Andric 1525*0b57cec5SDimitry Andric static bool isCopyReg(unsigned Opc, bool NoConv); 1526*0b57cec5SDimitry Andric 1527*0b57cec5SDimitry Andric private: 1528*0b57cec5SDimitry Andric bool propagateRegCopy(MachineInstr &MI); 1529*0b57cec5SDimitry Andric 1530*0b57cec5SDimitry Andric const HexagonRegisterInfo &HRI; 1531*0b57cec5SDimitry Andric MachineRegisterInfo &MRI; 1532*0b57cec5SDimitry Andric }; 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric } // end anonymous namespace 1535*0b57cec5SDimitry Andric 1536*0b57cec5SDimitry Andric /// Check if there is a register in AVs that is identical to Inp. If so, 1537*0b57cec5SDimitry Andric /// set Out to the found register. The output may be a pair Reg:Sub. 1538*0b57cec5SDimitry Andric bool CopyGeneration::findMatch(const BitTracker::RegisterRef &Inp, 1539*0b57cec5SDimitry Andric BitTracker::RegisterRef &Out, const RegisterSet &AVs) { 1540*0b57cec5SDimitry Andric if (!BT.has(Inp.Reg)) 1541*0b57cec5SDimitry Andric return false; 1542*0b57cec5SDimitry Andric const BitTracker::RegisterCell &InpRC = BT.lookup(Inp.Reg); 1543*0b57cec5SDimitry Andric auto *FRC = HBS::getFinalVRegClass(Inp, MRI); 1544*0b57cec5SDimitry Andric unsigned B, W; 1545*0b57cec5SDimitry Andric if (!HBS::getSubregMask(Inp, B, W, MRI)) 1546*0b57cec5SDimitry Andric return false; 1547*0b57cec5SDimitry Andric 1548*0b57cec5SDimitry Andric for (unsigned R = AVs.find_first(); R; R = AVs.find_next(R)) { 1549*0b57cec5SDimitry Andric if (!BT.has(R) || Forbidden[R]) 1550*0b57cec5SDimitry Andric continue; 1551*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC = BT.lookup(R); 1552*0b57cec5SDimitry Andric unsigned RW = RC.width(); 1553*0b57cec5SDimitry Andric if (W == RW) { 1554*0b57cec5SDimitry Andric if (FRC != MRI.getRegClass(R)) 1555*0b57cec5SDimitry Andric continue; 1556*0b57cec5SDimitry Andric if (!HBS::isTransparentCopy(R, Inp, MRI)) 1557*0b57cec5SDimitry Andric continue; 1558*0b57cec5SDimitry Andric if (!HBS::isEqual(InpRC, B, RC, 0, W)) 1559*0b57cec5SDimitry Andric continue; 1560*0b57cec5SDimitry Andric Out.Reg = R; 1561*0b57cec5SDimitry Andric Out.Sub = 0; 1562*0b57cec5SDimitry Andric return true; 1563*0b57cec5SDimitry Andric } 1564*0b57cec5SDimitry Andric // Check if there is a super-register, whose part (with a subregister) 1565*0b57cec5SDimitry Andric // is equal to the input. 1566*0b57cec5SDimitry Andric // Only do double registers for now. 1567*0b57cec5SDimitry Andric if (W*2 != RW) 1568*0b57cec5SDimitry Andric continue; 1569*0b57cec5SDimitry Andric if (MRI.getRegClass(R) != &Hexagon::DoubleRegsRegClass) 1570*0b57cec5SDimitry Andric continue; 1571*0b57cec5SDimitry Andric 1572*0b57cec5SDimitry Andric if (HBS::isEqual(InpRC, B, RC, 0, W)) 1573*0b57cec5SDimitry Andric Out.Sub = Hexagon::isub_lo; 1574*0b57cec5SDimitry Andric else if (HBS::isEqual(InpRC, B, RC, W, W)) 1575*0b57cec5SDimitry Andric Out.Sub = Hexagon::isub_hi; 1576*0b57cec5SDimitry Andric else 1577*0b57cec5SDimitry Andric continue; 1578*0b57cec5SDimitry Andric Out.Reg = R; 1579*0b57cec5SDimitry Andric if (HBS::isTransparentCopy(Out, Inp, MRI)) 1580*0b57cec5SDimitry Andric return true; 1581*0b57cec5SDimitry Andric } 1582*0b57cec5SDimitry Andric return false; 1583*0b57cec5SDimitry Andric } 1584*0b57cec5SDimitry Andric 1585*0b57cec5SDimitry Andric bool CopyGeneration::processBlock(MachineBasicBlock &B, 1586*0b57cec5SDimitry Andric const RegisterSet &AVs) { 1587*0b57cec5SDimitry Andric if (!BT.reached(&B)) 1588*0b57cec5SDimitry Andric return false; 1589*0b57cec5SDimitry Andric RegisterSet AVB(AVs); 1590*0b57cec5SDimitry Andric bool Changed = false; 1591*0b57cec5SDimitry Andric RegisterSet Defs; 1592*0b57cec5SDimitry Andric 1593*0b57cec5SDimitry Andric for (auto I = B.begin(), E = B.end(), NextI = I; I != E; 1594*0b57cec5SDimitry Andric ++I, AVB.insert(Defs)) { 1595*0b57cec5SDimitry Andric NextI = std::next(I); 1596*0b57cec5SDimitry Andric Defs.clear(); 1597*0b57cec5SDimitry Andric HBS::getInstrDefs(*I, Defs); 1598*0b57cec5SDimitry Andric 1599*0b57cec5SDimitry Andric unsigned Opc = I->getOpcode(); 1600*0b57cec5SDimitry Andric if (CopyPropagation::isCopyReg(Opc, false) || 1601*0b57cec5SDimitry Andric ConstGeneration::isTfrConst(*I)) 1602*0b57cec5SDimitry Andric continue; 1603*0b57cec5SDimitry Andric 1604*0b57cec5SDimitry Andric DebugLoc DL = I->getDebugLoc(); 1605*0b57cec5SDimitry Andric auto At = I->isPHI() ? B.getFirstNonPHI() : I; 1606*0b57cec5SDimitry Andric 1607*0b57cec5SDimitry Andric for (unsigned R = Defs.find_first(); R; R = Defs.find_next(R)) { 1608*0b57cec5SDimitry Andric BitTracker::RegisterRef MR; 1609*0b57cec5SDimitry Andric auto *FRC = HBS::getFinalVRegClass(R, MRI); 1610*0b57cec5SDimitry Andric 1611*0b57cec5SDimitry Andric if (findMatch(R, MR, AVB)) { 1612*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(FRC); 1613*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR) 1614*0b57cec5SDimitry Andric .addReg(MR.Reg, 0, MR.Sub); 1615*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), BT.get(MR)); 1616*0b57cec5SDimitry Andric HBS::replaceReg(R, NewR, MRI); 1617*0b57cec5SDimitry Andric Forbidden.insert(R); 1618*0b57cec5SDimitry Andric continue; 1619*0b57cec5SDimitry Andric } 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric if (FRC == &Hexagon::DoubleRegsRegClass || 1622*0b57cec5SDimitry Andric FRC == &Hexagon::HvxWRRegClass) { 1623*0b57cec5SDimitry Andric // Try to generate REG_SEQUENCE. 1624*0b57cec5SDimitry Andric unsigned SubLo = HRI.getHexagonSubRegIndex(*FRC, Hexagon::ps_sub_lo); 1625*0b57cec5SDimitry Andric unsigned SubHi = HRI.getHexagonSubRegIndex(*FRC, Hexagon::ps_sub_hi); 1626*0b57cec5SDimitry Andric BitTracker::RegisterRef TL = { R, SubLo }; 1627*0b57cec5SDimitry Andric BitTracker::RegisterRef TH = { R, SubHi }; 1628*0b57cec5SDimitry Andric BitTracker::RegisterRef ML, MH; 1629*0b57cec5SDimitry Andric if (findMatch(TL, ML, AVB) && findMatch(TH, MH, AVB)) { 1630*0b57cec5SDimitry Andric auto *FRC = HBS::getFinalVRegClass(R, MRI); 1631*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(FRC); 1632*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR) 1633*0b57cec5SDimitry Andric .addReg(ML.Reg, 0, ML.Sub) 1634*0b57cec5SDimitry Andric .addImm(SubLo) 1635*0b57cec5SDimitry Andric .addReg(MH.Reg, 0, MH.Sub) 1636*0b57cec5SDimitry Andric .addImm(SubHi); 1637*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), BT.get(R)); 1638*0b57cec5SDimitry Andric HBS::replaceReg(R, NewR, MRI); 1639*0b57cec5SDimitry Andric Forbidden.insert(R); 1640*0b57cec5SDimitry Andric } 1641*0b57cec5SDimitry Andric } 1642*0b57cec5SDimitry Andric } 1643*0b57cec5SDimitry Andric } 1644*0b57cec5SDimitry Andric 1645*0b57cec5SDimitry Andric return Changed; 1646*0b57cec5SDimitry Andric } 1647*0b57cec5SDimitry Andric 1648*0b57cec5SDimitry Andric bool CopyPropagation::isCopyReg(unsigned Opc, bool NoConv) { 1649*0b57cec5SDimitry Andric switch (Opc) { 1650*0b57cec5SDimitry Andric case TargetOpcode::COPY: 1651*0b57cec5SDimitry Andric case TargetOpcode::REG_SEQUENCE: 1652*0b57cec5SDimitry Andric case Hexagon::A4_combineir: 1653*0b57cec5SDimitry Andric case Hexagon::A4_combineri: 1654*0b57cec5SDimitry Andric return true; 1655*0b57cec5SDimitry Andric case Hexagon::A2_tfr: 1656*0b57cec5SDimitry Andric case Hexagon::A2_tfrp: 1657*0b57cec5SDimitry Andric case Hexagon::A2_combinew: 1658*0b57cec5SDimitry Andric case Hexagon::V6_vcombine: 1659*0b57cec5SDimitry Andric return NoConv; 1660*0b57cec5SDimitry Andric default: 1661*0b57cec5SDimitry Andric break; 1662*0b57cec5SDimitry Andric } 1663*0b57cec5SDimitry Andric return false; 1664*0b57cec5SDimitry Andric } 1665*0b57cec5SDimitry Andric 1666*0b57cec5SDimitry Andric bool CopyPropagation::propagateRegCopy(MachineInstr &MI) { 1667*0b57cec5SDimitry Andric bool Changed = false; 1668*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1669*0b57cec5SDimitry Andric BitTracker::RegisterRef RD = MI.getOperand(0); 1670*0b57cec5SDimitry Andric assert(MI.getOperand(0).getSubReg() == 0); 1671*0b57cec5SDimitry Andric 1672*0b57cec5SDimitry Andric switch (Opc) { 1673*0b57cec5SDimitry Andric case TargetOpcode::COPY: 1674*0b57cec5SDimitry Andric case Hexagon::A2_tfr: 1675*0b57cec5SDimitry Andric case Hexagon::A2_tfrp: { 1676*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = MI.getOperand(1); 1677*0b57cec5SDimitry Andric if (!HBS::isTransparentCopy(RD, RS, MRI)) 1678*0b57cec5SDimitry Andric break; 1679*0b57cec5SDimitry Andric if (RS.Sub != 0) 1680*0b57cec5SDimitry Andric Changed = HBS::replaceRegWithSub(RD.Reg, RS.Reg, RS.Sub, MRI); 1681*0b57cec5SDimitry Andric else 1682*0b57cec5SDimitry Andric Changed = HBS::replaceReg(RD.Reg, RS.Reg, MRI); 1683*0b57cec5SDimitry Andric break; 1684*0b57cec5SDimitry Andric } 1685*0b57cec5SDimitry Andric case TargetOpcode::REG_SEQUENCE: { 1686*0b57cec5SDimitry Andric BitTracker::RegisterRef SL, SH; 1687*0b57cec5SDimitry Andric if (HBS::parseRegSequence(MI, SL, SH, MRI)) { 1688*0b57cec5SDimitry Andric const TargetRegisterClass &RC = *MRI.getRegClass(RD.Reg); 1689*0b57cec5SDimitry Andric unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo); 1690*0b57cec5SDimitry Andric unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi); 1691*0b57cec5SDimitry Andric Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, SL.Reg, SL.Sub, MRI); 1692*0b57cec5SDimitry Andric Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, SH.Reg, SH.Sub, MRI); 1693*0b57cec5SDimitry Andric } 1694*0b57cec5SDimitry Andric break; 1695*0b57cec5SDimitry Andric } 1696*0b57cec5SDimitry Andric case Hexagon::A2_combinew: 1697*0b57cec5SDimitry Andric case Hexagon::V6_vcombine: { 1698*0b57cec5SDimitry Andric const TargetRegisterClass &RC = *MRI.getRegClass(RD.Reg); 1699*0b57cec5SDimitry Andric unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo); 1700*0b57cec5SDimitry Andric unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi); 1701*0b57cec5SDimitry Andric BitTracker::RegisterRef RH = MI.getOperand(1), RL = MI.getOperand(2); 1702*0b57cec5SDimitry Andric Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, RL.Reg, RL.Sub, MRI); 1703*0b57cec5SDimitry Andric Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, RH.Reg, RH.Sub, MRI); 1704*0b57cec5SDimitry Andric break; 1705*0b57cec5SDimitry Andric } 1706*0b57cec5SDimitry Andric case Hexagon::A4_combineir: 1707*0b57cec5SDimitry Andric case Hexagon::A4_combineri: { 1708*0b57cec5SDimitry Andric unsigned SrcX = (Opc == Hexagon::A4_combineir) ? 2 : 1; 1709*0b57cec5SDimitry Andric unsigned Sub = (Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo 1710*0b57cec5SDimitry Andric : Hexagon::isub_hi; 1711*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = MI.getOperand(SrcX); 1712*0b57cec5SDimitry Andric Changed = HBS::replaceSubWithSub(RD.Reg, Sub, RS.Reg, RS.Sub, MRI); 1713*0b57cec5SDimitry Andric break; 1714*0b57cec5SDimitry Andric } 1715*0b57cec5SDimitry Andric } 1716*0b57cec5SDimitry Andric return Changed; 1717*0b57cec5SDimitry Andric } 1718*0b57cec5SDimitry Andric 1719*0b57cec5SDimitry Andric bool CopyPropagation::processBlock(MachineBasicBlock &B, const RegisterSet&) { 1720*0b57cec5SDimitry Andric std::vector<MachineInstr*> Instrs; 1721*0b57cec5SDimitry Andric for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) 1722*0b57cec5SDimitry Andric Instrs.push_back(&*I); 1723*0b57cec5SDimitry Andric 1724*0b57cec5SDimitry Andric bool Changed = false; 1725*0b57cec5SDimitry Andric for (auto I : Instrs) { 1726*0b57cec5SDimitry Andric unsigned Opc = I->getOpcode(); 1727*0b57cec5SDimitry Andric if (!CopyPropagation::isCopyReg(Opc, true)) 1728*0b57cec5SDimitry Andric continue; 1729*0b57cec5SDimitry Andric Changed |= propagateRegCopy(*I); 1730*0b57cec5SDimitry Andric } 1731*0b57cec5SDimitry Andric 1732*0b57cec5SDimitry Andric return Changed; 1733*0b57cec5SDimitry Andric } 1734*0b57cec5SDimitry Andric 1735*0b57cec5SDimitry Andric namespace { 1736*0b57cec5SDimitry Andric 1737*0b57cec5SDimitry Andric // Recognize patterns that can be simplified and replace them with the 1738*0b57cec5SDimitry Andric // simpler forms. 1739*0b57cec5SDimitry Andric // This is by no means complete 1740*0b57cec5SDimitry Andric class BitSimplification : public Transformation { 1741*0b57cec5SDimitry Andric public: 1742*0b57cec5SDimitry Andric BitSimplification(BitTracker &bt, const MachineDominatorTree &mdt, 1743*0b57cec5SDimitry Andric const HexagonInstrInfo &hii, const HexagonRegisterInfo &hri, 1744*0b57cec5SDimitry Andric MachineRegisterInfo &mri, MachineFunction &mf) 1745*0b57cec5SDimitry Andric : Transformation(true), MDT(mdt), HII(hii), HRI(hri), MRI(mri), 1746*0b57cec5SDimitry Andric MF(mf), BT(bt) {} 1747*0b57cec5SDimitry Andric 1748*0b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override; 1749*0b57cec5SDimitry Andric 1750*0b57cec5SDimitry Andric private: 1751*0b57cec5SDimitry Andric struct RegHalf : public BitTracker::RegisterRef { 1752*0b57cec5SDimitry Andric bool Low; // Low/High halfword. 1753*0b57cec5SDimitry Andric }; 1754*0b57cec5SDimitry Andric 1755*0b57cec5SDimitry Andric bool matchHalf(unsigned SelfR, const BitTracker::RegisterCell &RC, 1756*0b57cec5SDimitry Andric unsigned B, RegHalf &RH); 1757*0b57cec5SDimitry Andric bool validateReg(BitTracker::RegisterRef R, unsigned Opc, unsigned OpNum); 1758*0b57cec5SDimitry Andric 1759*0b57cec5SDimitry Andric bool matchPackhl(unsigned SelfR, const BitTracker::RegisterCell &RC, 1760*0b57cec5SDimitry Andric BitTracker::RegisterRef &Rs, BitTracker::RegisterRef &Rt); 1761*0b57cec5SDimitry Andric unsigned getCombineOpcode(bool HLow, bool LLow); 1762*0b57cec5SDimitry Andric 1763*0b57cec5SDimitry Andric bool genStoreUpperHalf(MachineInstr *MI); 1764*0b57cec5SDimitry Andric bool genStoreImmediate(MachineInstr *MI); 1765*0b57cec5SDimitry Andric bool genPackhl(MachineInstr *MI, BitTracker::RegisterRef RD, 1766*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC); 1767*0b57cec5SDimitry Andric bool genExtractHalf(MachineInstr *MI, BitTracker::RegisterRef RD, 1768*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC); 1769*0b57cec5SDimitry Andric bool genCombineHalf(MachineInstr *MI, BitTracker::RegisterRef RD, 1770*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC); 1771*0b57cec5SDimitry Andric bool genExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD, 1772*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC); 1773*0b57cec5SDimitry Andric bool genBitSplit(MachineInstr *MI, BitTracker::RegisterRef RD, 1774*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC, const RegisterSet &AVs); 1775*0b57cec5SDimitry Andric bool simplifyTstbit(MachineInstr *MI, BitTracker::RegisterRef RD, 1776*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC); 1777*0b57cec5SDimitry Andric bool simplifyExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD, 1778*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC, const RegisterSet &AVs); 1779*0b57cec5SDimitry Andric bool simplifyRCmp0(MachineInstr *MI, BitTracker::RegisterRef RD); 1780*0b57cec5SDimitry Andric 1781*0b57cec5SDimitry Andric // Cache of created instructions to avoid creating duplicates. 1782*0b57cec5SDimitry Andric // XXX Currently only used by genBitSplit. 1783*0b57cec5SDimitry Andric std::vector<MachineInstr*> NewMIs; 1784*0b57cec5SDimitry Andric 1785*0b57cec5SDimitry Andric const MachineDominatorTree &MDT; 1786*0b57cec5SDimitry Andric const HexagonInstrInfo &HII; 1787*0b57cec5SDimitry Andric const HexagonRegisterInfo &HRI; 1788*0b57cec5SDimitry Andric MachineRegisterInfo &MRI; 1789*0b57cec5SDimitry Andric MachineFunction &MF; 1790*0b57cec5SDimitry Andric BitTracker &BT; 1791*0b57cec5SDimitry Andric }; 1792*0b57cec5SDimitry Andric 1793*0b57cec5SDimitry Andric } // end anonymous namespace 1794*0b57cec5SDimitry Andric 1795*0b57cec5SDimitry Andric // Check if the bits [B..B+16) in register cell RC form a valid halfword, 1796*0b57cec5SDimitry Andric // i.e. [0..16), [16..32), etc. of some register. If so, return true and 1797*0b57cec5SDimitry Andric // set the information about the found register in RH. 1798*0b57cec5SDimitry Andric bool BitSimplification::matchHalf(unsigned SelfR, 1799*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC, unsigned B, RegHalf &RH) { 1800*0b57cec5SDimitry Andric // XXX This could be searching in the set of available registers, in case 1801*0b57cec5SDimitry Andric // the match is not exact. 1802*0b57cec5SDimitry Andric 1803*0b57cec5SDimitry Andric // Match 16-bit chunks, where the RC[B..B+15] references exactly one 1804*0b57cec5SDimitry Andric // register and all the bits B..B+15 match between RC and the register. 1805*0b57cec5SDimitry Andric // This is meant to match "v1[0-15]", where v1 = { [0]:0 [1-15]:v1... }, 1806*0b57cec5SDimitry Andric // and RC = { [0]:0 [1-15]:v1[1-15]... }. 1807*0b57cec5SDimitry Andric bool Low = false; 1808*0b57cec5SDimitry Andric unsigned I = B; 1809*0b57cec5SDimitry Andric while (I < B+16 && RC[I].num()) 1810*0b57cec5SDimitry Andric I++; 1811*0b57cec5SDimitry Andric if (I == B+16) 1812*0b57cec5SDimitry Andric return false; 1813*0b57cec5SDimitry Andric 1814*0b57cec5SDimitry Andric unsigned Reg = RC[I].RefI.Reg; 1815*0b57cec5SDimitry Andric unsigned P = RC[I].RefI.Pos; // The RefI.Pos will be advanced by I-B. 1816*0b57cec5SDimitry Andric if (P < I-B) 1817*0b57cec5SDimitry Andric return false; 1818*0b57cec5SDimitry Andric unsigned Pos = P - (I-B); 1819*0b57cec5SDimitry Andric 1820*0b57cec5SDimitry Andric if (Reg == 0 || Reg == SelfR) // Don't match "self". 1821*0b57cec5SDimitry Andric return false; 1822*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg)) 1823*0b57cec5SDimitry Andric return false; 1824*0b57cec5SDimitry Andric if (!BT.has(Reg)) 1825*0b57cec5SDimitry Andric return false; 1826*0b57cec5SDimitry Andric 1827*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(Reg); 1828*0b57cec5SDimitry Andric if (Pos+16 > SC.width()) 1829*0b57cec5SDimitry Andric return false; 1830*0b57cec5SDimitry Andric 1831*0b57cec5SDimitry Andric for (unsigned i = 0; i < 16; ++i) { 1832*0b57cec5SDimitry Andric const BitTracker::BitValue &RV = RC[i+B]; 1833*0b57cec5SDimitry Andric if (RV.Type == BitTracker::BitValue::Ref) { 1834*0b57cec5SDimitry Andric if (RV.RefI.Reg != Reg) 1835*0b57cec5SDimitry Andric return false; 1836*0b57cec5SDimitry Andric if (RV.RefI.Pos != i+Pos) 1837*0b57cec5SDimitry Andric return false; 1838*0b57cec5SDimitry Andric continue; 1839*0b57cec5SDimitry Andric } 1840*0b57cec5SDimitry Andric if (RC[i+B] != SC[i+Pos]) 1841*0b57cec5SDimitry Andric return false; 1842*0b57cec5SDimitry Andric } 1843*0b57cec5SDimitry Andric 1844*0b57cec5SDimitry Andric unsigned Sub = 0; 1845*0b57cec5SDimitry Andric switch (Pos) { 1846*0b57cec5SDimitry Andric case 0: 1847*0b57cec5SDimitry Andric Sub = Hexagon::isub_lo; 1848*0b57cec5SDimitry Andric Low = true; 1849*0b57cec5SDimitry Andric break; 1850*0b57cec5SDimitry Andric case 16: 1851*0b57cec5SDimitry Andric Sub = Hexagon::isub_lo; 1852*0b57cec5SDimitry Andric Low = false; 1853*0b57cec5SDimitry Andric break; 1854*0b57cec5SDimitry Andric case 32: 1855*0b57cec5SDimitry Andric Sub = Hexagon::isub_hi; 1856*0b57cec5SDimitry Andric Low = true; 1857*0b57cec5SDimitry Andric break; 1858*0b57cec5SDimitry Andric case 48: 1859*0b57cec5SDimitry Andric Sub = Hexagon::isub_hi; 1860*0b57cec5SDimitry Andric Low = false; 1861*0b57cec5SDimitry Andric break; 1862*0b57cec5SDimitry Andric default: 1863*0b57cec5SDimitry Andric return false; 1864*0b57cec5SDimitry Andric } 1865*0b57cec5SDimitry Andric 1866*0b57cec5SDimitry Andric RH.Reg = Reg; 1867*0b57cec5SDimitry Andric RH.Sub = Sub; 1868*0b57cec5SDimitry Andric RH.Low = Low; 1869*0b57cec5SDimitry Andric // If the subregister is not valid with the register, set it to 0. 1870*0b57cec5SDimitry Andric if (!HBS::getFinalVRegClass(RH, MRI)) 1871*0b57cec5SDimitry Andric RH.Sub = 0; 1872*0b57cec5SDimitry Andric 1873*0b57cec5SDimitry Andric return true; 1874*0b57cec5SDimitry Andric } 1875*0b57cec5SDimitry Andric 1876*0b57cec5SDimitry Andric bool BitSimplification::validateReg(BitTracker::RegisterRef R, unsigned Opc, 1877*0b57cec5SDimitry Andric unsigned OpNum) { 1878*0b57cec5SDimitry Andric auto *OpRC = HII.getRegClass(HII.get(Opc), OpNum, &HRI, MF); 1879*0b57cec5SDimitry Andric auto *RRC = HBS::getFinalVRegClass(R, MRI); 1880*0b57cec5SDimitry Andric return OpRC->hasSubClassEq(RRC); 1881*0b57cec5SDimitry Andric } 1882*0b57cec5SDimitry Andric 1883*0b57cec5SDimitry Andric // Check if RC matches the pattern of a S2_packhl. If so, return true and 1884*0b57cec5SDimitry Andric // set the inputs Rs and Rt. 1885*0b57cec5SDimitry Andric bool BitSimplification::matchPackhl(unsigned SelfR, 1886*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC, BitTracker::RegisterRef &Rs, 1887*0b57cec5SDimitry Andric BitTracker::RegisterRef &Rt) { 1888*0b57cec5SDimitry Andric RegHalf L1, H1, L2, H2; 1889*0b57cec5SDimitry Andric 1890*0b57cec5SDimitry Andric if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1)) 1891*0b57cec5SDimitry Andric return false; 1892*0b57cec5SDimitry Andric if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1)) 1893*0b57cec5SDimitry Andric return false; 1894*0b57cec5SDimitry Andric 1895*0b57cec5SDimitry Andric // Rs = H1.L1, Rt = H2.L2 1896*0b57cec5SDimitry Andric if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low) 1897*0b57cec5SDimitry Andric return false; 1898*0b57cec5SDimitry Andric if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low) 1899*0b57cec5SDimitry Andric return false; 1900*0b57cec5SDimitry Andric 1901*0b57cec5SDimitry Andric Rs = H1; 1902*0b57cec5SDimitry Andric Rt = H2; 1903*0b57cec5SDimitry Andric return true; 1904*0b57cec5SDimitry Andric } 1905*0b57cec5SDimitry Andric 1906*0b57cec5SDimitry Andric unsigned BitSimplification::getCombineOpcode(bool HLow, bool LLow) { 1907*0b57cec5SDimitry Andric return HLow ? LLow ? Hexagon::A2_combine_ll 1908*0b57cec5SDimitry Andric : Hexagon::A2_combine_lh 1909*0b57cec5SDimitry Andric : LLow ? Hexagon::A2_combine_hl 1910*0b57cec5SDimitry Andric : Hexagon::A2_combine_hh; 1911*0b57cec5SDimitry Andric } 1912*0b57cec5SDimitry Andric 1913*0b57cec5SDimitry Andric // If MI stores the upper halfword of a register (potentially obtained via 1914*0b57cec5SDimitry Andric // shifts or extracts), replace it with a storerf instruction. This could 1915*0b57cec5SDimitry Andric // cause the "extraction" code to become dead. 1916*0b57cec5SDimitry Andric bool BitSimplification::genStoreUpperHalf(MachineInstr *MI) { 1917*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 1918*0b57cec5SDimitry Andric if (Opc != Hexagon::S2_storerh_io) 1919*0b57cec5SDimitry Andric return false; 1920*0b57cec5SDimitry Andric 1921*0b57cec5SDimitry Andric MachineOperand &ValOp = MI->getOperand(2); 1922*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = ValOp; 1923*0b57cec5SDimitry Andric if (!BT.has(RS.Reg)) 1924*0b57cec5SDimitry Andric return false; 1925*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg); 1926*0b57cec5SDimitry Andric RegHalf H; 1927*0b57cec5SDimitry Andric if (!matchHalf(0, RC, 0, H)) 1928*0b57cec5SDimitry Andric return false; 1929*0b57cec5SDimitry Andric if (H.Low) 1930*0b57cec5SDimitry Andric return false; 1931*0b57cec5SDimitry Andric MI->setDesc(HII.get(Hexagon::S2_storerf_io)); 1932*0b57cec5SDimitry Andric ValOp.setReg(H.Reg); 1933*0b57cec5SDimitry Andric ValOp.setSubReg(H.Sub); 1934*0b57cec5SDimitry Andric return true; 1935*0b57cec5SDimitry Andric } 1936*0b57cec5SDimitry Andric 1937*0b57cec5SDimitry Andric // If MI stores a value known at compile-time, and the value is within a range 1938*0b57cec5SDimitry Andric // that avoids using constant-extenders, replace it with a store-immediate. 1939*0b57cec5SDimitry Andric bool BitSimplification::genStoreImmediate(MachineInstr *MI) { 1940*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 1941*0b57cec5SDimitry Andric unsigned Align = 0; 1942*0b57cec5SDimitry Andric switch (Opc) { 1943*0b57cec5SDimitry Andric case Hexagon::S2_storeri_io: 1944*0b57cec5SDimitry Andric Align++; 1945*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1946*0b57cec5SDimitry Andric case Hexagon::S2_storerh_io: 1947*0b57cec5SDimitry Andric Align++; 1948*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1949*0b57cec5SDimitry Andric case Hexagon::S2_storerb_io: 1950*0b57cec5SDimitry Andric break; 1951*0b57cec5SDimitry Andric default: 1952*0b57cec5SDimitry Andric return false; 1953*0b57cec5SDimitry Andric } 1954*0b57cec5SDimitry Andric 1955*0b57cec5SDimitry Andric // Avoid stores to frame-indices (due to an unknown offset). 1956*0b57cec5SDimitry Andric if (!MI->getOperand(0).isReg()) 1957*0b57cec5SDimitry Andric return false; 1958*0b57cec5SDimitry Andric MachineOperand &OffOp = MI->getOperand(1); 1959*0b57cec5SDimitry Andric if (!OffOp.isImm()) 1960*0b57cec5SDimitry Andric return false; 1961*0b57cec5SDimitry Andric 1962*0b57cec5SDimitry Andric int64_t Off = OffOp.getImm(); 1963*0b57cec5SDimitry Andric // Offset is u6:a. Sadly, there is no isShiftedUInt(n,x). 1964*0b57cec5SDimitry Andric if (!isUIntN(6+Align, Off) || (Off & ((1<<Align)-1))) 1965*0b57cec5SDimitry Andric return false; 1966*0b57cec5SDimitry Andric // Source register: 1967*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = MI->getOperand(2); 1968*0b57cec5SDimitry Andric if (!BT.has(RS.Reg)) 1969*0b57cec5SDimitry Andric return false; 1970*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg); 1971*0b57cec5SDimitry Andric uint64_t U; 1972*0b57cec5SDimitry Andric if (!HBS::getConst(RC, 0, RC.width(), U)) 1973*0b57cec5SDimitry Andric return false; 1974*0b57cec5SDimitry Andric 1975*0b57cec5SDimitry Andric // Only consider 8-bit values to avoid constant-extenders. 1976*0b57cec5SDimitry Andric int V; 1977*0b57cec5SDimitry Andric switch (Opc) { 1978*0b57cec5SDimitry Andric case Hexagon::S2_storerb_io: 1979*0b57cec5SDimitry Andric V = int8_t(U); 1980*0b57cec5SDimitry Andric break; 1981*0b57cec5SDimitry Andric case Hexagon::S2_storerh_io: 1982*0b57cec5SDimitry Andric V = int16_t(U); 1983*0b57cec5SDimitry Andric break; 1984*0b57cec5SDimitry Andric case Hexagon::S2_storeri_io: 1985*0b57cec5SDimitry Andric V = int32_t(U); 1986*0b57cec5SDimitry Andric break; 1987*0b57cec5SDimitry Andric default: 1988*0b57cec5SDimitry Andric // Opc is already checked above to be one of the three store instructions. 1989*0b57cec5SDimitry Andric // This silences a -Wuninitialized false positive on GCC 5.4. 1990*0b57cec5SDimitry Andric llvm_unreachable("Unexpected store opcode"); 1991*0b57cec5SDimitry Andric } 1992*0b57cec5SDimitry Andric if (!isInt<8>(V)) 1993*0b57cec5SDimitry Andric return false; 1994*0b57cec5SDimitry Andric 1995*0b57cec5SDimitry Andric MI->RemoveOperand(2); 1996*0b57cec5SDimitry Andric switch (Opc) { 1997*0b57cec5SDimitry Andric case Hexagon::S2_storerb_io: 1998*0b57cec5SDimitry Andric MI->setDesc(HII.get(Hexagon::S4_storeirb_io)); 1999*0b57cec5SDimitry Andric break; 2000*0b57cec5SDimitry Andric case Hexagon::S2_storerh_io: 2001*0b57cec5SDimitry Andric MI->setDesc(HII.get(Hexagon::S4_storeirh_io)); 2002*0b57cec5SDimitry Andric break; 2003*0b57cec5SDimitry Andric case Hexagon::S2_storeri_io: 2004*0b57cec5SDimitry Andric MI->setDesc(HII.get(Hexagon::S4_storeiri_io)); 2005*0b57cec5SDimitry Andric break; 2006*0b57cec5SDimitry Andric } 2007*0b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateImm(V)); 2008*0b57cec5SDimitry Andric return true; 2009*0b57cec5SDimitry Andric } 2010*0b57cec5SDimitry Andric 2011*0b57cec5SDimitry Andric // If MI is equivalent o S2_packhl, generate the S2_packhl. MI could be the 2012*0b57cec5SDimitry Andric // last instruction in a sequence that results in something equivalent to 2013*0b57cec5SDimitry Andric // the pack-halfwords. The intent is to cause the entire sequence to become 2014*0b57cec5SDimitry Andric // dead. 2015*0b57cec5SDimitry Andric bool BitSimplification::genPackhl(MachineInstr *MI, 2016*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) { 2017*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2018*0b57cec5SDimitry Andric if (Opc == Hexagon::S2_packhl) 2019*0b57cec5SDimitry Andric return false; 2020*0b57cec5SDimitry Andric BitTracker::RegisterRef Rs, Rt; 2021*0b57cec5SDimitry Andric if (!matchPackhl(RD.Reg, RC, Rs, Rt)) 2022*0b57cec5SDimitry Andric return false; 2023*0b57cec5SDimitry Andric if (!validateReg(Rs, Hexagon::S2_packhl, 1) || 2024*0b57cec5SDimitry Andric !validateReg(Rt, Hexagon::S2_packhl, 2)) 2025*0b57cec5SDimitry Andric return false; 2026*0b57cec5SDimitry Andric 2027*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2028*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass); 2029*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 2030*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2031*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2032*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::S2_packhl), NewR) 2033*0b57cec5SDimitry Andric .addReg(Rs.Reg, 0, Rs.Sub) 2034*0b57cec5SDimitry Andric .addReg(Rt.Reg, 0, Rt.Sub); 2035*0b57cec5SDimitry Andric HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI); 2036*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), RC); 2037*0b57cec5SDimitry Andric return true; 2038*0b57cec5SDimitry Andric } 2039*0b57cec5SDimitry Andric 2040*0b57cec5SDimitry Andric // If MI produces halfword of the input in the low half of the output, 2041*0b57cec5SDimitry Andric // replace it with zero-extend or extractu. 2042*0b57cec5SDimitry Andric bool BitSimplification::genExtractHalf(MachineInstr *MI, 2043*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) { 2044*0b57cec5SDimitry Andric RegHalf L; 2045*0b57cec5SDimitry Andric // Check for halfword in low 16 bits, zeros elsewhere. 2046*0b57cec5SDimitry Andric if (!matchHalf(RD.Reg, RC, 0, L) || !HBS::isZero(RC, 16, 16)) 2047*0b57cec5SDimitry Andric return false; 2048*0b57cec5SDimitry Andric 2049*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2050*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2051*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 2052*0b57cec5SDimitry Andric 2053*0b57cec5SDimitry Andric // Prefer zxth, since zxth can go in any slot, while extractu only in 2054*0b57cec5SDimitry Andric // slots 2 and 3. 2055*0b57cec5SDimitry Andric unsigned NewR = 0; 2056*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2057*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2058*0b57cec5SDimitry Andric if (L.Low && Opc != Hexagon::A2_zxth) { 2059*0b57cec5SDimitry Andric if (validateReg(L, Hexagon::A2_zxth, 1)) { 2060*0b57cec5SDimitry Andric NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 2061*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::A2_zxth), NewR) 2062*0b57cec5SDimitry Andric .addReg(L.Reg, 0, L.Sub); 2063*0b57cec5SDimitry Andric } 2064*0b57cec5SDimitry Andric } else if (!L.Low && Opc != Hexagon::S2_lsr_i_r) { 2065*0b57cec5SDimitry Andric if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) { 2066*0b57cec5SDimitry Andric NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 2067*0b57cec5SDimitry Andric BuildMI(B, MI, DL, HII.get(Hexagon::S2_lsr_i_r), NewR) 2068*0b57cec5SDimitry Andric .addReg(L.Reg, 0, L.Sub) 2069*0b57cec5SDimitry Andric .addImm(16); 2070*0b57cec5SDimitry Andric } 2071*0b57cec5SDimitry Andric } 2072*0b57cec5SDimitry Andric if (NewR == 0) 2073*0b57cec5SDimitry Andric return false; 2074*0b57cec5SDimitry Andric HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI); 2075*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), RC); 2076*0b57cec5SDimitry Andric return true; 2077*0b57cec5SDimitry Andric } 2078*0b57cec5SDimitry Andric 2079*0b57cec5SDimitry Andric // If MI is equivalent to a combine(.L/.H, .L/.H) replace with with the 2080*0b57cec5SDimitry Andric // combine. 2081*0b57cec5SDimitry Andric bool BitSimplification::genCombineHalf(MachineInstr *MI, 2082*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) { 2083*0b57cec5SDimitry Andric RegHalf L, H; 2084*0b57cec5SDimitry Andric // Check for combine h/l 2085*0b57cec5SDimitry Andric if (!matchHalf(RD.Reg, RC, 0, L) || !matchHalf(RD.Reg, RC, 16, H)) 2086*0b57cec5SDimitry Andric return false; 2087*0b57cec5SDimitry Andric // Do nothing if this is just a reg copy. 2088*0b57cec5SDimitry Andric if (L.Reg == H.Reg && L.Sub == H.Sub && !H.Low && L.Low) 2089*0b57cec5SDimitry Andric return false; 2090*0b57cec5SDimitry Andric 2091*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2092*0b57cec5SDimitry Andric unsigned COpc = getCombineOpcode(H.Low, L.Low); 2093*0b57cec5SDimitry Andric if (COpc == Opc) 2094*0b57cec5SDimitry Andric return false; 2095*0b57cec5SDimitry Andric if (!validateReg(H, COpc, 1) || !validateReg(L, COpc, 2)) 2096*0b57cec5SDimitry Andric return false; 2097*0b57cec5SDimitry Andric 2098*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2099*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 2100*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 2101*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2102*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2103*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(COpc), NewR) 2104*0b57cec5SDimitry Andric .addReg(H.Reg, 0, H.Sub) 2105*0b57cec5SDimitry Andric .addReg(L.Reg, 0, L.Sub); 2106*0b57cec5SDimitry Andric HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI); 2107*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), RC); 2108*0b57cec5SDimitry Andric return true; 2109*0b57cec5SDimitry Andric } 2110*0b57cec5SDimitry Andric 2111*0b57cec5SDimitry Andric // If MI resets high bits of a register and keeps the lower ones, replace it 2112*0b57cec5SDimitry Andric // with zero-extend byte/half, and-immediate, or extractu, as appropriate. 2113*0b57cec5SDimitry Andric bool BitSimplification::genExtractLow(MachineInstr *MI, 2114*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) { 2115*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2116*0b57cec5SDimitry Andric switch (Opc) { 2117*0b57cec5SDimitry Andric case Hexagon::A2_zxtb: 2118*0b57cec5SDimitry Andric case Hexagon::A2_zxth: 2119*0b57cec5SDimitry Andric case Hexagon::S2_extractu: 2120*0b57cec5SDimitry Andric return false; 2121*0b57cec5SDimitry Andric } 2122*0b57cec5SDimitry Andric if (Opc == Hexagon::A2_andir && MI->getOperand(2).isImm()) { 2123*0b57cec5SDimitry Andric int32_t Imm = MI->getOperand(2).getImm(); 2124*0b57cec5SDimitry Andric if (isInt<10>(Imm)) 2125*0b57cec5SDimitry Andric return false; 2126*0b57cec5SDimitry Andric } 2127*0b57cec5SDimitry Andric 2128*0b57cec5SDimitry Andric if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm()) 2129*0b57cec5SDimitry Andric return false; 2130*0b57cec5SDimitry Andric unsigned W = RC.width(); 2131*0b57cec5SDimitry Andric while (W > 0 && RC[W-1].is(0)) 2132*0b57cec5SDimitry Andric W--; 2133*0b57cec5SDimitry Andric if (W == 0 || W == RC.width()) 2134*0b57cec5SDimitry Andric return false; 2135*0b57cec5SDimitry Andric unsigned NewOpc = (W == 8) ? Hexagon::A2_zxtb 2136*0b57cec5SDimitry Andric : (W == 16) ? Hexagon::A2_zxth 2137*0b57cec5SDimitry Andric : (W < 10) ? Hexagon::A2_andir 2138*0b57cec5SDimitry Andric : Hexagon::S2_extractu; 2139*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2140*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 2141*0b57cec5SDimitry Andric 2142*0b57cec5SDimitry Andric for (auto &Op : MI->uses()) { 2143*0b57cec5SDimitry Andric if (!Op.isReg()) 2144*0b57cec5SDimitry Andric continue; 2145*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = Op; 2146*0b57cec5SDimitry Andric if (!BT.has(RS.Reg)) 2147*0b57cec5SDimitry Andric continue; 2148*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg); 2149*0b57cec5SDimitry Andric unsigned BN, BW; 2150*0b57cec5SDimitry Andric if (!HBS::getSubregMask(RS, BN, BW, MRI)) 2151*0b57cec5SDimitry Andric continue; 2152*0b57cec5SDimitry Andric if (BW < W || !HBS::isEqual(RC, 0, SC, BN, W)) 2153*0b57cec5SDimitry Andric continue; 2154*0b57cec5SDimitry Andric if (!validateReg(RS, NewOpc, 1)) 2155*0b57cec5SDimitry Andric continue; 2156*0b57cec5SDimitry Andric 2157*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 2158*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2159*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2160*0b57cec5SDimitry Andric auto MIB = BuildMI(B, At, DL, HII.get(NewOpc), NewR) 2161*0b57cec5SDimitry Andric .addReg(RS.Reg, 0, RS.Sub); 2162*0b57cec5SDimitry Andric if (NewOpc == Hexagon::A2_andir) 2163*0b57cec5SDimitry Andric MIB.addImm((1 << W) - 1); 2164*0b57cec5SDimitry Andric else if (NewOpc == Hexagon::S2_extractu) 2165*0b57cec5SDimitry Andric MIB.addImm(W).addImm(0); 2166*0b57cec5SDimitry Andric HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI); 2167*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), RC); 2168*0b57cec5SDimitry Andric return true; 2169*0b57cec5SDimitry Andric } 2170*0b57cec5SDimitry Andric return false; 2171*0b57cec5SDimitry Andric } 2172*0b57cec5SDimitry Andric 2173*0b57cec5SDimitry Andric bool BitSimplification::genBitSplit(MachineInstr *MI, 2174*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC, 2175*0b57cec5SDimitry Andric const RegisterSet &AVs) { 2176*0b57cec5SDimitry Andric if (!GenBitSplit) 2177*0b57cec5SDimitry Andric return false; 2178*0b57cec5SDimitry Andric if (MaxBitSplit.getNumOccurrences()) { 2179*0b57cec5SDimitry Andric if (CountBitSplit >= MaxBitSplit) 2180*0b57cec5SDimitry Andric return false; 2181*0b57cec5SDimitry Andric } 2182*0b57cec5SDimitry Andric 2183*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2184*0b57cec5SDimitry Andric switch (Opc) { 2185*0b57cec5SDimitry Andric case Hexagon::A4_bitsplit: 2186*0b57cec5SDimitry Andric case Hexagon::A4_bitspliti: 2187*0b57cec5SDimitry Andric return false; 2188*0b57cec5SDimitry Andric } 2189*0b57cec5SDimitry Andric 2190*0b57cec5SDimitry Andric unsigned W = RC.width(); 2191*0b57cec5SDimitry Andric if (W != 32) 2192*0b57cec5SDimitry Andric return false; 2193*0b57cec5SDimitry Andric 2194*0b57cec5SDimitry Andric auto ctlz = [] (const BitTracker::RegisterCell &C) -> unsigned { 2195*0b57cec5SDimitry Andric unsigned Z = C.width(); 2196*0b57cec5SDimitry Andric while (Z > 0 && C[Z-1].is(0)) 2197*0b57cec5SDimitry Andric --Z; 2198*0b57cec5SDimitry Andric return C.width() - Z; 2199*0b57cec5SDimitry Andric }; 2200*0b57cec5SDimitry Andric 2201*0b57cec5SDimitry Andric // Count the number of leading zeros in the target RC. 2202*0b57cec5SDimitry Andric unsigned Z = ctlz(RC); 2203*0b57cec5SDimitry Andric if (Z == 0 || Z == W) 2204*0b57cec5SDimitry Andric return false; 2205*0b57cec5SDimitry Andric 2206*0b57cec5SDimitry Andric // A simplistic analysis: assume the source register (the one being split) 2207*0b57cec5SDimitry Andric // is fully unknown, and that all its bits are self-references. 2208*0b57cec5SDimitry Andric const BitTracker::BitValue &B0 = RC[0]; 2209*0b57cec5SDimitry Andric if (B0.Type != BitTracker::BitValue::Ref) 2210*0b57cec5SDimitry Andric return false; 2211*0b57cec5SDimitry Andric 2212*0b57cec5SDimitry Andric unsigned SrcR = B0.RefI.Reg; 2213*0b57cec5SDimitry Andric unsigned SrcSR = 0; 2214*0b57cec5SDimitry Andric unsigned Pos = B0.RefI.Pos; 2215*0b57cec5SDimitry Andric 2216*0b57cec5SDimitry Andric // All the non-zero bits should be consecutive bits from the same register. 2217*0b57cec5SDimitry Andric for (unsigned i = 1; i < W-Z; ++i) { 2218*0b57cec5SDimitry Andric const BitTracker::BitValue &V = RC[i]; 2219*0b57cec5SDimitry Andric if (V.Type != BitTracker::BitValue::Ref) 2220*0b57cec5SDimitry Andric return false; 2221*0b57cec5SDimitry Andric if (V.RefI.Reg != SrcR || V.RefI.Pos != Pos+i) 2222*0b57cec5SDimitry Andric return false; 2223*0b57cec5SDimitry Andric } 2224*0b57cec5SDimitry Andric 2225*0b57cec5SDimitry Andric // Now, find the other bitfield among AVs. 2226*0b57cec5SDimitry Andric for (unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) { 2227*0b57cec5SDimitry Andric // The number of leading zeros here should be the number of trailing 2228*0b57cec5SDimitry Andric // non-zeros in RC. 2229*0b57cec5SDimitry Andric unsigned SRC = MRI.getRegClass(S)->getID(); 2230*0b57cec5SDimitry Andric if (SRC != Hexagon::IntRegsRegClassID && 2231*0b57cec5SDimitry Andric SRC != Hexagon::DoubleRegsRegClassID) 2232*0b57cec5SDimitry Andric continue; 2233*0b57cec5SDimitry Andric if (!BT.has(S)) 2234*0b57cec5SDimitry Andric continue; 2235*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(S); 2236*0b57cec5SDimitry Andric if (SC.width() != W || ctlz(SC) != W-Z) 2237*0b57cec5SDimitry Andric continue; 2238*0b57cec5SDimitry Andric // The Z lower bits should now match SrcR. 2239*0b57cec5SDimitry Andric const BitTracker::BitValue &S0 = SC[0]; 2240*0b57cec5SDimitry Andric if (S0.Type != BitTracker::BitValue::Ref || S0.RefI.Reg != SrcR) 2241*0b57cec5SDimitry Andric continue; 2242*0b57cec5SDimitry Andric unsigned P = S0.RefI.Pos; 2243*0b57cec5SDimitry Andric 2244*0b57cec5SDimitry Andric if (Pos <= P && (Pos + W-Z) != P) 2245*0b57cec5SDimitry Andric continue; 2246*0b57cec5SDimitry Andric if (P < Pos && (P + Z) != Pos) 2247*0b57cec5SDimitry Andric continue; 2248*0b57cec5SDimitry Andric // The starting bitfield position must be at a subregister boundary. 2249*0b57cec5SDimitry Andric if (std::min(P, Pos) != 0 && std::min(P, Pos) != 32) 2250*0b57cec5SDimitry Andric continue; 2251*0b57cec5SDimitry Andric 2252*0b57cec5SDimitry Andric unsigned I; 2253*0b57cec5SDimitry Andric for (I = 1; I < Z; ++I) { 2254*0b57cec5SDimitry Andric const BitTracker::BitValue &V = SC[I]; 2255*0b57cec5SDimitry Andric if (V.Type != BitTracker::BitValue::Ref) 2256*0b57cec5SDimitry Andric break; 2257*0b57cec5SDimitry Andric if (V.RefI.Reg != SrcR || V.RefI.Pos != P+I) 2258*0b57cec5SDimitry Andric break; 2259*0b57cec5SDimitry Andric } 2260*0b57cec5SDimitry Andric if (I != Z) 2261*0b57cec5SDimitry Andric continue; 2262*0b57cec5SDimitry Andric 2263*0b57cec5SDimitry Andric // Generate bitsplit where S is defined. 2264*0b57cec5SDimitry Andric if (MaxBitSplit.getNumOccurrences()) 2265*0b57cec5SDimitry Andric CountBitSplit++; 2266*0b57cec5SDimitry Andric MachineInstr *DefS = MRI.getVRegDef(S); 2267*0b57cec5SDimitry Andric assert(DefS != nullptr); 2268*0b57cec5SDimitry Andric DebugLoc DL = DefS->getDebugLoc(); 2269*0b57cec5SDimitry Andric MachineBasicBlock &B = *DefS->getParent(); 2270*0b57cec5SDimitry Andric auto At = DefS->isPHI() ? B.getFirstNonPHI() 2271*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(DefS); 2272*0b57cec5SDimitry Andric if (MRI.getRegClass(SrcR)->getID() == Hexagon::DoubleRegsRegClassID) 2273*0b57cec5SDimitry Andric SrcSR = (std::min(Pos, P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo; 2274*0b57cec5SDimitry Andric if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1)) 2275*0b57cec5SDimitry Andric continue; 2276*0b57cec5SDimitry Andric unsigned ImmOp = Pos <= P ? W-Z : Z; 2277*0b57cec5SDimitry Andric 2278*0b57cec5SDimitry Andric // Find an existing bitsplit instruction if one already exists. 2279*0b57cec5SDimitry Andric unsigned NewR = 0; 2280*0b57cec5SDimitry Andric for (MachineInstr *In : NewMIs) { 2281*0b57cec5SDimitry Andric if (In->getOpcode() != Hexagon::A4_bitspliti) 2282*0b57cec5SDimitry Andric continue; 2283*0b57cec5SDimitry Andric MachineOperand &Op1 = In->getOperand(1); 2284*0b57cec5SDimitry Andric if (Op1.getReg() != SrcR || Op1.getSubReg() != SrcSR) 2285*0b57cec5SDimitry Andric continue; 2286*0b57cec5SDimitry Andric if (In->getOperand(2).getImm() != ImmOp) 2287*0b57cec5SDimitry Andric continue; 2288*0b57cec5SDimitry Andric // Check if the target register is available here. 2289*0b57cec5SDimitry Andric MachineOperand &Op0 = In->getOperand(0); 2290*0b57cec5SDimitry Andric MachineInstr *DefI = MRI.getVRegDef(Op0.getReg()); 2291*0b57cec5SDimitry Andric assert(DefI != nullptr); 2292*0b57cec5SDimitry Andric if (!MDT.dominates(DefI, &*At)) 2293*0b57cec5SDimitry Andric continue; 2294*0b57cec5SDimitry Andric 2295*0b57cec5SDimitry Andric // Found one that can be reused. 2296*0b57cec5SDimitry Andric assert(Op0.getSubReg() == 0); 2297*0b57cec5SDimitry Andric NewR = Op0.getReg(); 2298*0b57cec5SDimitry Andric break; 2299*0b57cec5SDimitry Andric } 2300*0b57cec5SDimitry Andric if (!NewR) { 2301*0b57cec5SDimitry Andric NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass); 2302*0b57cec5SDimitry Andric auto NewBS = BuildMI(B, At, DL, HII.get(Hexagon::A4_bitspliti), NewR) 2303*0b57cec5SDimitry Andric .addReg(SrcR, 0, SrcSR) 2304*0b57cec5SDimitry Andric .addImm(ImmOp); 2305*0b57cec5SDimitry Andric NewMIs.push_back(NewBS); 2306*0b57cec5SDimitry Andric } 2307*0b57cec5SDimitry Andric if (Pos <= P) { 2308*0b57cec5SDimitry Andric HBS::replaceRegWithSub(RD.Reg, NewR, Hexagon::isub_lo, MRI); 2309*0b57cec5SDimitry Andric HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi, MRI); 2310*0b57cec5SDimitry Andric } else { 2311*0b57cec5SDimitry Andric HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo, MRI); 2312*0b57cec5SDimitry Andric HBS::replaceRegWithSub(RD.Reg, NewR, Hexagon::isub_hi, MRI); 2313*0b57cec5SDimitry Andric } 2314*0b57cec5SDimitry Andric return true; 2315*0b57cec5SDimitry Andric } 2316*0b57cec5SDimitry Andric 2317*0b57cec5SDimitry Andric return false; 2318*0b57cec5SDimitry Andric } 2319*0b57cec5SDimitry Andric 2320*0b57cec5SDimitry Andric // Check for tstbit simplification opportunity, where the bit being checked 2321*0b57cec5SDimitry Andric // can be tracked back to another register. For example: 2322*0b57cec5SDimitry Andric // %2 = S2_lsr_i_r %1, 5 2323*0b57cec5SDimitry Andric // %3 = S2_tstbit_i %2, 0 2324*0b57cec5SDimitry Andric // => 2325*0b57cec5SDimitry Andric // %3 = S2_tstbit_i %1, 5 2326*0b57cec5SDimitry Andric bool BitSimplification::simplifyTstbit(MachineInstr *MI, 2327*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) { 2328*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2329*0b57cec5SDimitry Andric if (Opc != Hexagon::S2_tstbit_i) 2330*0b57cec5SDimitry Andric return false; 2331*0b57cec5SDimitry Andric 2332*0b57cec5SDimitry Andric unsigned BN = MI->getOperand(2).getImm(); 2333*0b57cec5SDimitry Andric BitTracker::RegisterRef RS = MI->getOperand(1); 2334*0b57cec5SDimitry Andric unsigned F, W; 2335*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 2336*0b57cec5SDimitry Andric if (!BT.has(RS.Reg) || !HBS::getSubregMask(RS, F, W, MRI)) 2337*0b57cec5SDimitry Andric return false; 2338*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2339*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2340*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2341*0b57cec5SDimitry Andric 2342*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg); 2343*0b57cec5SDimitry Andric const BitTracker::BitValue &V = SC[F+BN]; 2344*0b57cec5SDimitry Andric if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg != RS.Reg) { 2345*0b57cec5SDimitry Andric const TargetRegisterClass *TC = MRI.getRegClass(V.RefI.Reg); 2346*0b57cec5SDimitry Andric // Need to map V.RefI.Reg to a 32-bit register, i.e. if it is 2347*0b57cec5SDimitry Andric // a double register, need to use a subregister and adjust bit 2348*0b57cec5SDimitry Andric // number. 2349*0b57cec5SDimitry Andric unsigned P = std::numeric_limits<unsigned>::max(); 2350*0b57cec5SDimitry Andric BitTracker::RegisterRef RR(V.RefI.Reg, 0); 2351*0b57cec5SDimitry Andric if (TC == &Hexagon::DoubleRegsRegClass) { 2352*0b57cec5SDimitry Andric P = V.RefI.Pos; 2353*0b57cec5SDimitry Andric RR.Sub = Hexagon::isub_lo; 2354*0b57cec5SDimitry Andric if (P >= 32) { 2355*0b57cec5SDimitry Andric P -= 32; 2356*0b57cec5SDimitry Andric RR.Sub = Hexagon::isub_hi; 2357*0b57cec5SDimitry Andric } 2358*0b57cec5SDimitry Andric } else if (TC == &Hexagon::IntRegsRegClass) { 2359*0b57cec5SDimitry Andric P = V.RefI.Pos; 2360*0b57cec5SDimitry Andric } 2361*0b57cec5SDimitry Andric if (P != std::numeric_limits<unsigned>::max()) { 2362*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass); 2363*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::S2_tstbit_i), NewR) 2364*0b57cec5SDimitry Andric .addReg(RR.Reg, 0, RR.Sub) 2365*0b57cec5SDimitry Andric .addImm(P); 2366*0b57cec5SDimitry Andric HBS::replaceReg(RD.Reg, NewR, MRI); 2367*0b57cec5SDimitry Andric BT.put(NewR, RC); 2368*0b57cec5SDimitry Andric return true; 2369*0b57cec5SDimitry Andric } 2370*0b57cec5SDimitry Andric } else if (V.is(0) || V.is(1)) { 2371*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass); 2372*0b57cec5SDimitry Andric unsigned NewOpc = V.is(0) ? Hexagon::PS_false : Hexagon::PS_true; 2373*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(NewOpc), NewR); 2374*0b57cec5SDimitry Andric HBS::replaceReg(RD.Reg, NewR, MRI); 2375*0b57cec5SDimitry Andric return true; 2376*0b57cec5SDimitry Andric } 2377*0b57cec5SDimitry Andric 2378*0b57cec5SDimitry Andric return false; 2379*0b57cec5SDimitry Andric } 2380*0b57cec5SDimitry Andric 2381*0b57cec5SDimitry Andric // Detect whether RD is a bitfield extract (sign- or zero-extended) of 2382*0b57cec5SDimitry Andric // some register from the AVs set. Create a new corresponding instruction 2383*0b57cec5SDimitry Andric // at the location of MI. The intent is to recognize situations where 2384*0b57cec5SDimitry Andric // a sequence of instructions performs an operation that is equivalent to 2385*0b57cec5SDimitry Andric // an extract operation, such as a shift left followed by a shift right. 2386*0b57cec5SDimitry Andric bool BitSimplification::simplifyExtractLow(MachineInstr *MI, 2387*0b57cec5SDimitry Andric BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC, 2388*0b57cec5SDimitry Andric const RegisterSet &AVs) { 2389*0b57cec5SDimitry Andric if (!GenExtract) 2390*0b57cec5SDimitry Andric return false; 2391*0b57cec5SDimitry Andric if (MaxExtract.getNumOccurrences()) { 2392*0b57cec5SDimitry Andric if (CountExtract >= MaxExtract) 2393*0b57cec5SDimitry Andric return false; 2394*0b57cec5SDimitry Andric CountExtract++; 2395*0b57cec5SDimitry Andric } 2396*0b57cec5SDimitry Andric 2397*0b57cec5SDimitry Andric unsigned W = RC.width(); 2398*0b57cec5SDimitry Andric unsigned RW = W; 2399*0b57cec5SDimitry Andric unsigned Len; 2400*0b57cec5SDimitry Andric bool Signed; 2401*0b57cec5SDimitry Andric 2402*0b57cec5SDimitry Andric // The code is mostly class-independent, except for the part that generates 2403*0b57cec5SDimitry Andric // the extract instruction, and establishes the source register (in case it 2404*0b57cec5SDimitry Andric // needs to use a subregister). 2405*0b57cec5SDimitry Andric const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI); 2406*0b57cec5SDimitry Andric if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass) 2407*0b57cec5SDimitry Andric return false; 2408*0b57cec5SDimitry Andric assert(RD.Sub == 0); 2409*0b57cec5SDimitry Andric 2410*0b57cec5SDimitry Andric // Observation: 2411*0b57cec5SDimitry Andric // If the cell has a form of 00..0xx..x with k zeros and n remaining 2412*0b57cec5SDimitry Andric // bits, this could be an extractu of the n bits, but it could also be 2413*0b57cec5SDimitry Andric // an extractu of a longer field which happens to have 0s in the top 2414*0b57cec5SDimitry Andric // bit positions. 2415*0b57cec5SDimitry Andric // The same logic applies to sign-extended fields. 2416*0b57cec5SDimitry Andric // 2417*0b57cec5SDimitry Andric // Do not check for the extended extracts, since it would expand the 2418*0b57cec5SDimitry Andric // search space quite a bit. The search may be expensive as it is. 2419*0b57cec5SDimitry Andric 2420*0b57cec5SDimitry Andric const BitTracker::BitValue &TopV = RC[W-1]; 2421*0b57cec5SDimitry Andric 2422*0b57cec5SDimitry Andric // Eliminate candidates that have self-referential bits, since they 2423*0b57cec5SDimitry Andric // cannot be extracts from other registers. Also, skip registers that 2424*0b57cec5SDimitry Andric // have compile-time constant values. 2425*0b57cec5SDimitry Andric bool IsConst = true; 2426*0b57cec5SDimitry Andric for (unsigned I = 0; I != W; ++I) { 2427*0b57cec5SDimitry Andric const BitTracker::BitValue &V = RC[I]; 2428*0b57cec5SDimitry Andric if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg == RD.Reg) 2429*0b57cec5SDimitry Andric return false; 2430*0b57cec5SDimitry Andric IsConst = IsConst && (V.is(0) || V.is(1)); 2431*0b57cec5SDimitry Andric } 2432*0b57cec5SDimitry Andric if (IsConst) 2433*0b57cec5SDimitry Andric return false; 2434*0b57cec5SDimitry Andric 2435*0b57cec5SDimitry Andric if (TopV.is(0) || TopV.is(1)) { 2436*0b57cec5SDimitry Andric bool S = TopV.is(1); 2437*0b57cec5SDimitry Andric for (--W; W > 0 && RC[W-1].is(S); --W) 2438*0b57cec5SDimitry Andric ; 2439*0b57cec5SDimitry Andric Len = W; 2440*0b57cec5SDimitry Andric Signed = S; 2441*0b57cec5SDimitry Andric // The sign bit must be a part of the field being extended. 2442*0b57cec5SDimitry Andric if (Signed) 2443*0b57cec5SDimitry Andric ++Len; 2444*0b57cec5SDimitry Andric } else { 2445*0b57cec5SDimitry Andric // This could still be a sign-extended extract. 2446*0b57cec5SDimitry Andric assert(TopV.Type == BitTracker::BitValue::Ref); 2447*0b57cec5SDimitry Andric if (TopV.RefI.Reg == RD.Reg || TopV.RefI.Pos == W-1) 2448*0b57cec5SDimitry Andric return false; 2449*0b57cec5SDimitry Andric for (--W; W > 0 && RC[W-1] == TopV; --W) 2450*0b57cec5SDimitry Andric ; 2451*0b57cec5SDimitry Andric // The top bits of RC are copies of TopV. One occurrence of TopV will 2452*0b57cec5SDimitry Andric // be a part of the field. 2453*0b57cec5SDimitry Andric Len = W + 1; 2454*0b57cec5SDimitry Andric Signed = true; 2455*0b57cec5SDimitry Andric } 2456*0b57cec5SDimitry Andric 2457*0b57cec5SDimitry Andric // This would be just a copy. It should be handled elsewhere. 2458*0b57cec5SDimitry Andric if (Len == RW) 2459*0b57cec5SDimitry Andric return false; 2460*0b57cec5SDimitry Andric 2461*0b57cec5SDimitry Andric LLVM_DEBUG({ 2462*0b57cec5SDimitry Andric dbgs() << __func__ << " on reg: " << printReg(RD.Reg, &HRI, RD.Sub) 2463*0b57cec5SDimitry Andric << ", MI: " << *MI; 2464*0b57cec5SDimitry Andric dbgs() << "Cell: " << RC << '\n'; 2465*0b57cec5SDimitry Andric dbgs() << "Expected bitfield size: " << Len << " bits, " 2466*0b57cec5SDimitry Andric << (Signed ? "sign" : "zero") << "-extended\n"; 2467*0b57cec5SDimitry Andric }); 2468*0b57cec5SDimitry Andric 2469*0b57cec5SDimitry Andric bool Changed = false; 2470*0b57cec5SDimitry Andric 2471*0b57cec5SDimitry Andric for (unsigned R = AVs.find_first(); R != 0; R = AVs.find_next(R)) { 2472*0b57cec5SDimitry Andric if (!BT.has(R)) 2473*0b57cec5SDimitry Andric continue; 2474*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(R); 2475*0b57cec5SDimitry Andric unsigned SW = SC.width(); 2476*0b57cec5SDimitry Andric 2477*0b57cec5SDimitry Andric // The source can be longer than the destination, as long as its size is 2478*0b57cec5SDimitry Andric // a multiple of the size of the destination. Also, we would need to be 2479*0b57cec5SDimitry Andric // able to refer to the subregister in the source that would be of the 2480*0b57cec5SDimitry Andric // same size as the destination, but only check the sizes here. 2481*0b57cec5SDimitry Andric if (SW < RW || (SW % RW) != 0) 2482*0b57cec5SDimitry Andric continue; 2483*0b57cec5SDimitry Andric 2484*0b57cec5SDimitry Andric // The field can start at any offset in SC as long as it contains Len 2485*0b57cec5SDimitry Andric // bits and does not cross subregister boundary (if the source register 2486*0b57cec5SDimitry Andric // is longer than the destination). 2487*0b57cec5SDimitry Andric unsigned Off = 0; 2488*0b57cec5SDimitry Andric while (Off <= SW-Len) { 2489*0b57cec5SDimitry Andric unsigned OE = (Off+Len)/RW; 2490*0b57cec5SDimitry Andric if (OE != Off/RW) { 2491*0b57cec5SDimitry Andric // The assumption here is that if the source (R) is longer than the 2492*0b57cec5SDimitry Andric // destination, then the destination is a sequence of words of 2493*0b57cec5SDimitry Andric // size RW, and each such word in R can be accessed via a subregister. 2494*0b57cec5SDimitry Andric // 2495*0b57cec5SDimitry Andric // If the beginning and the end of the field cross the subregister 2496*0b57cec5SDimitry Andric // boundary, advance to the next subregister. 2497*0b57cec5SDimitry Andric Off = OE*RW; 2498*0b57cec5SDimitry Andric continue; 2499*0b57cec5SDimitry Andric } 2500*0b57cec5SDimitry Andric if (HBS::isEqual(RC, 0, SC, Off, Len)) 2501*0b57cec5SDimitry Andric break; 2502*0b57cec5SDimitry Andric ++Off; 2503*0b57cec5SDimitry Andric } 2504*0b57cec5SDimitry Andric 2505*0b57cec5SDimitry Andric if (Off > SW-Len) 2506*0b57cec5SDimitry Andric continue; 2507*0b57cec5SDimitry Andric 2508*0b57cec5SDimitry Andric // Found match. 2509*0b57cec5SDimitry Andric unsigned ExtOpc = 0; 2510*0b57cec5SDimitry Andric if (Off == 0) { 2511*0b57cec5SDimitry Andric if (Len == 8) 2512*0b57cec5SDimitry Andric ExtOpc = Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb; 2513*0b57cec5SDimitry Andric else if (Len == 16) 2514*0b57cec5SDimitry Andric ExtOpc = Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth; 2515*0b57cec5SDimitry Andric else if (Len < 10 && !Signed) 2516*0b57cec5SDimitry Andric ExtOpc = Hexagon::A2_andir; 2517*0b57cec5SDimitry Andric } 2518*0b57cec5SDimitry Andric if (ExtOpc == 0) { 2519*0b57cec5SDimitry Andric ExtOpc = 2520*0b57cec5SDimitry Andric Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp) 2521*0b57cec5SDimitry Andric : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup); 2522*0b57cec5SDimitry Andric } 2523*0b57cec5SDimitry Andric unsigned SR = 0; 2524*0b57cec5SDimitry Andric // This only recognizes isub_lo and isub_hi. 2525*0b57cec5SDimitry Andric if (RW != SW && RW*2 != SW) 2526*0b57cec5SDimitry Andric continue; 2527*0b57cec5SDimitry Andric if (RW != SW) 2528*0b57cec5SDimitry Andric SR = (Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi; 2529*0b57cec5SDimitry Andric Off = Off % RW; 2530*0b57cec5SDimitry Andric 2531*0b57cec5SDimitry Andric if (!validateReg({R,SR}, ExtOpc, 1)) 2532*0b57cec5SDimitry Andric continue; 2533*0b57cec5SDimitry Andric 2534*0b57cec5SDimitry Andric // Don't generate the same instruction as the one being optimized. 2535*0b57cec5SDimitry Andric if (MI->getOpcode() == ExtOpc) { 2536*0b57cec5SDimitry Andric // All possible ExtOpc's have the source in operand(1). 2537*0b57cec5SDimitry Andric const MachineOperand &SrcOp = MI->getOperand(1); 2538*0b57cec5SDimitry Andric if (SrcOp.getReg() == R) 2539*0b57cec5SDimitry Andric continue; 2540*0b57cec5SDimitry Andric } 2541*0b57cec5SDimitry Andric 2542*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 2543*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2544*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(FRC); 2545*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2546*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2547*0b57cec5SDimitry Andric auto MIB = BuildMI(B, At, DL, HII.get(ExtOpc), NewR) 2548*0b57cec5SDimitry Andric .addReg(R, 0, SR); 2549*0b57cec5SDimitry Andric switch (ExtOpc) { 2550*0b57cec5SDimitry Andric case Hexagon::A2_sxtb: 2551*0b57cec5SDimitry Andric case Hexagon::A2_zxtb: 2552*0b57cec5SDimitry Andric case Hexagon::A2_sxth: 2553*0b57cec5SDimitry Andric case Hexagon::A2_zxth: 2554*0b57cec5SDimitry Andric break; 2555*0b57cec5SDimitry Andric case Hexagon::A2_andir: 2556*0b57cec5SDimitry Andric MIB.addImm((1u << Len) - 1); 2557*0b57cec5SDimitry Andric break; 2558*0b57cec5SDimitry Andric case Hexagon::S4_extract: 2559*0b57cec5SDimitry Andric case Hexagon::S2_extractu: 2560*0b57cec5SDimitry Andric case Hexagon::S4_extractp: 2561*0b57cec5SDimitry Andric case Hexagon::S2_extractup: 2562*0b57cec5SDimitry Andric MIB.addImm(Len) 2563*0b57cec5SDimitry Andric .addImm(Off); 2564*0b57cec5SDimitry Andric break; 2565*0b57cec5SDimitry Andric default: 2566*0b57cec5SDimitry Andric llvm_unreachable("Unexpected opcode"); 2567*0b57cec5SDimitry Andric } 2568*0b57cec5SDimitry Andric 2569*0b57cec5SDimitry Andric HBS::replaceReg(RD.Reg, NewR, MRI); 2570*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), RC); 2571*0b57cec5SDimitry Andric Changed = true; 2572*0b57cec5SDimitry Andric break; 2573*0b57cec5SDimitry Andric } 2574*0b57cec5SDimitry Andric 2575*0b57cec5SDimitry Andric return Changed; 2576*0b57cec5SDimitry Andric } 2577*0b57cec5SDimitry Andric 2578*0b57cec5SDimitry Andric bool BitSimplification::simplifyRCmp0(MachineInstr *MI, 2579*0b57cec5SDimitry Andric BitTracker::RegisterRef RD) { 2580*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2581*0b57cec5SDimitry Andric if (Opc != Hexagon::A4_rcmpeqi && Opc != Hexagon::A4_rcmpneqi) 2582*0b57cec5SDimitry Andric return false; 2583*0b57cec5SDimitry Andric MachineOperand &CmpOp = MI->getOperand(2); 2584*0b57cec5SDimitry Andric if (!CmpOp.isImm() || CmpOp.getImm() != 0) 2585*0b57cec5SDimitry Andric return false; 2586*0b57cec5SDimitry Andric 2587*0b57cec5SDimitry Andric const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI); 2588*0b57cec5SDimitry Andric if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass) 2589*0b57cec5SDimitry Andric return false; 2590*0b57cec5SDimitry Andric assert(RD.Sub == 0); 2591*0b57cec5SDimitry Andric 2592*0b57cec5SDimitry Andric MachineBasicBlock &B = *MI->getParent(); 2593*0b57cec5SDimitry Andric const DebugLoc &DL = MI->getDebugLoc(); 2594*0b57cec5SDimitry Andric auto At = MI->isPHI() ? B.getFirstNonPHI() 2595*0b57cec5SDimitry Andric : MachineBasicBlock::iterator(MI); 2596*0b57cec5SDimitry Andric bool KnownZ = true; 2597*0b57cec5SDimitry Andric bool KnownNZ = false; 2598*0b57cec5SDimitry Andric 2599*0b57cec5SDimitry Andric BitTracker::RegisterRef SR = MI->getOperand(1); 2600*0b57cec5SDimitry Andric if (!BT.has(SR.Reg)) 2601*0b57cec5SDimitry Andric return false; 2602*0b57cec5SDimitry Andric const BitTracker::RegisterCell &SC = BT.lookup(SR.Reg); 2603*0b57cec5SDimitry Andric unsigned F, W; 2604*0b57cec5SDimitry Andric if (!HBS::getSubregMask(SR, F, W, MRI)) 2605*0b57cec5SDimitry Andric return false; 2606*0b57cec5SDimitry Andric 2607*0b57cec5SDimitry Andric for (uint16_t I = F; I != F+W; ++I) { 2608*0b57cec5SDimitry Andric const BitTracker::BitValue &V = SC[I]; 2609*0b57cec5SDimitry Andric if (!V.is(0)) 2610*0b57cec5SDimitry Andric KnownZ = false; 2611*0b57cec5SDimitry Andric if (V.is(1)) 2612*0b57cec5SDimitry Andric KnownNZ = true; 2613*0b57cec5SDimitry Andric } 2614*0b57cec5SDimitry Andric 2615*0b57cec5SDimitry Andric auto ReplaceWithConst = [&] (int C) { 2616*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(FRC); 2617*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), NewR) 2618*0b57cec5SDimitry Andric .addImm(C); 2619*0b57cec5SDimitry Andric HBS::replaceReg(RD.Reg, NewR, MRI); 2620*0b57cec5SDimitry Andric BitTracker::RegisterCell NewRC(W); 2621*0b57cec5SDimitry Andric for (uint16_t I = 0; I != W; ++I) { 2622*0b57cec5SDimitry Andric NewRC[I] = BitTracker::BitValue(C & 1); 2623*0b57cec5SDimitry Andric C = unsigned(C) >> 1; 2624*0b57cec5SDimitry Andric } 2625*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), NewRC); 2626*0b57cec5SDimitry Andric return true; 2627*0b57cec5SDimitry Andric }; 2628*0b57cec5SDimitry Andric 2629*0b57cec5SDimitry Andric auto IsNonZero = [] (const MachineOperand &Op) { 2630*0b57cec5SDimitry Andric if (Op.isGlobal() || Op.isBlockAddress()) 2631*0b57cec5SDimitry Andric return true; 2632*0b57cec5SDimitry Andric if (Op.isImm()) 2633*0b57cec5SDimitry Andric return Op.getImm() != 0; 2634*0b57cec5SDimitry Andric if (Op.isCImm()) 2635*0b57cec5SDimitry Andric return !Op.getCImm()->isZero(); 2636*0b57cec5SDimitry Andric if (Op.isFPImm()) 2637*0b57cec5SDimitry Andric return !Op.getFPImm()->isZero(); 2638*0b57cec5SDimitry Andric return false; 2639*0b57cec5SDimitry Andric }; 2640*0b57cec5SDimitry Andric 2641*0b57cec5SDimitry Andric auto IsZero = [] (const MachineOperand &Op) { 2642*0b57cec5SDimitry Andric if (Op.isGlobal() || Op.isBlockAddress()) 2643*0b57cec5SDimitry Andric return false; 2644*0b57cec5SDimitry Andric if (Op.isImm()) 2645*0b57cec5SDimitry Andric return Op.getImm() == 0; 2646*0b57cec5SDimitry Andric if (Op.isCImm()) 2647*0b57cec5SDimitry Andric return Op.getCImm()->isZero(); 2648*0b57cec5SDimitry Andric if (Op.isFPImm()) 2649*0b57cec5SDimitry Andric return Op.getFPImm()->isZero(); 2650*0b57cec5SDimitry Andric return false; 2651*0b57cec5SDimitry Andric }; 2652*0b57cec5SDimitry Andric 2653*0b57cec5SDimitry Andric // If the source register is known to be 0 or non-0, the comparison can 2654*0b57cec5SDimitry Andric // be folded to a load of a constant. 2655*0b57cec5SDimitry Andric if (KnownZ || KnownNZ) { 2656*0b57cec5SDimitry Andric assert(KnownZ != KnownNZ && "Register cannot be both 0 and non-0"); 2657*0b57cec5SDimitry Andric return ReplaceWithConst(KnownZ == (Opc == Hexagon::A4_rcmpeqi)); 2658*0b57cec5SDimitry Andric } 2659*0b57cec5SDimitry Andric 2660*0b57cec5SDimitry Andric // Special case: if the compare comes from a C2_muxii, then we know the 2661*0b57cec5SDimitry Andric // two possible constants that can be the source value. 2662*0b57cec5SDimitry Andric MachineInstr *InpDef = MRI.getVRegDef(SR.Reg); 2663*0b57cec5SDimitry Andric if (!InpDef) 2664*0b57cec5SDimitry Andric return false; 2665*0b57cec5SDimitry Andric if (SR.Sub == 0 && InpDef->getOpcode() == Hexagon::C2_muxii) { 2666*0b57cec5SDimitry Andric MachineOperand &Src1 = InpDef->getOperand(2); 2667*0b57cec5SDimitry Andric MachineOperand &Src2 = InpDef->getOperand(3); 2668*0b57cec5SDimitry Andric // Check if both are non-zero. 2669*0b57cec5SDimitry Andric bool KnownNZ1 = IsNonZero(Src1), KnownNZ2 = IsNonZero(Src2); 2670*0b57cec5SDimitry Andric if (KnownNZ1 && KnownNZ2) 2671*0b57cec5SDimitry Andric return ReplaceWithConst(Opc == Hexagon::A4_rcmpneqi); 2672*0b57cec5SDimitry Andric // Check if both are zero. 2673*0b57cec5SDimitry Andric bool KnownZ1 = IsZero(Src1), KnownZ2 = IsZero(Src2); 2674*0b57cec5SDimitry Andric if (KnownZ1 && KnownZ2) 2675*0b57cec5SDimitry Andric return ReplaceWithConst(Opc == Hexagon::A4_rcmpeqi); 2676*0b57cec5SDimitry Andric 2677*0b57cec5SDimitry Andric // If for both operands we know that they are either 0 or non-0, 2678*0b57cec5SDimitry Andric // replace the comparison with a C2_muxii, using the same predicate 2679*0b57cec5SDimitry Andric // register, but with operands substituted with 0/1 accordingly. 2680*0b57cec5SDimitry Andric if ((KnownZ1 || KnownNZ1) && (KnownZ2 || KnownNZ2)) { 2681*0b57cec5SDimitry Andric unsigned NewR = MRI.createVirtualRegister(FRC); 2682*0b57cec5SDimitry Andric BuildMI(B, At, DL, HII.get(Hexagon::C2_muxii), NewR) 2683*0b57cec5SDimitry Andric .addReg(InpDef->getOperand(1).getReg()) 2684*0b57cec5SDimitry Andric .addImm(KnownZ1 == (Opc == Hexagon::A4_rcmpeqi)) 2685*0b57cec5SDimitry Andric .addImm(KnownZ2 == (Opc == Hexagon::A4_rcmpeqi)); 2686*0b57cec5SDimitry Andric HBS::replaceReg(RD.Reg, NewR, MRI); 2687*0b57cec5SDimitry Andric // Create a new cell with only the least significant bit unknown. 2688*0b57cec5SDimitry Andric BitTracker::RegisterCell NewRC(W); 2689*0b57cec5SDimitry Andric NewRC[0] = BitTracker::BitValue::self(); 2690*0b57cec5SDimitry Andric NewRC.fill(1, W, BitTracker::BitValue::Zero); 2691*0b57cec5SDimitry Andric BT.put(BitTracker::RegisterRef(NewR), NewRC); 2692*0b57cec5SDimitry Andric return true; 2693*0b57cec5SDimitry Andric } 2694*0b57cec5SDimitry Andric } 2695*0b57cec5SDimitry Andric 2696*0b57cec5SDimitry Andric return false; 2697*0b57cec5SDimitry Andric } 2698*0b57cec5SDimitry Andric 2699*0b57cec5SDimitry Andric bool BitSimplification::processBlock(MachineBasicBlock &B, 2700*0b57cec5SDimitry Andric const RegisterSet &AVs) { 2701*0b57cec5SDimitry Andric if (!BT.reached(&B)) 2702*0b57cec5SDimitry Andric return false; 2703*0b57cec5SDimitry Andric bool Changed = false; 2704*0b57cec5SDimitry Andric RegisterSet AVB = AVs; 2705*0b57cec5SDimitry Andric RegisterSet Defs; 2706*0b57cec5SDimitry Andric 2707*0b57cec5SDimitry Andric for (auto I = B.begin(), E = B.end(); I != E; ++I, AVB.insert(Defs)) { 2708*0b57cec5SDimitry Andric MachineInstr *MI = &*I; 2709*0b57cec5SDimitry Andric Defs.clear(); 2710*0b57cec5SDimitry Andric HBS::getInstrDefs(*MI, Defs); 2711*0b57cec5SDimitry Andric 2712*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2713*0b57cec5SDimitry Andric if (Opc == TargetOpcode::COPY || Opc == TargetOpcode::REG_SEQUENCE) 2714*0b57cec5SDimitry Andric continue; 2715*0b57cec5SDimitry Andric 2716*0b57cec5SDimitry Andric if (MI->mayStore()) { 2717*0b57cec5SDimitry Andric bool T = genStoreUpperHalf(MI); 2718*0b57cec5SDimitry Andric T = T || genStoreImmediate(MI); 2719*0b57cec5SDimitry Andric Changed |= T; 2720*0b57cec5SDimitry Andric continue; 2721*0b57cec5SDimitry Andric } 2722*0b57cec5SDimitry Andric 2723*0b57cec5SDimitry Andric if (Defs.count() != 1) 2724*0b57cec5SDimitry Andric continue; 2725*0b57cec5SDimitry Andric const MachineOperand &Op0 = MI->getOperand(0); 2726*0b57cec5SDimitry Andric if (!Op0.isReg() || !Op0.isDef()) 2727*0b57cec5SDimitry Andric continue; 2728*0b57cec5SDimitry Andric BitTracker::RegisterRef RD = Op0; 2729*0b57cec5SDimitry Andric if (!BT.has(RD.Reg)) 2730*0b57cec5SDimitry Andric continue; 2731*0b57cec5SDimitry Andric const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI); 2732*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC = BT.lookup(RD.Reg); 2733*0b57cec5SDimitry Andric 2734*0b57cec5SDimitry Andric if (FRC->getID() == Hexagon::DoubleRegsRegClassID) { 2735*0b57cec5SDimitry Andric bool T = genPackhl(MI, RD, RC); 2736*0b57cec5SDimitry Andric T = T || simplifyExtractLow(MI, RD, RC, AVB); 2737*0b57cec5SDimitry Andric Changed |= T; 2738*0b57cec5SDimitry Andric continue; 2739*0b57cec5SDimitry Andric } 2740*0b57cec5SDimitry Andric 2741*0b57cec5SDimitry Andric if (FRC->getID() == Hexagon::IntRegsRegClassID) { 2742*0b57cec5SDimitry Andric bool T = genBitSplit(MI, RD, RC, AVB); 2743*0b57cec5SDimitry Andric T = T || simplifyExtractLow(MI, RD, RC, AVB); 2744*0b57cec5SDimitry Andric T = T || genExtractHalf(MI, RD, RC); 2745*0b57cec5SDimitry Andric T = T || genCombineHalf(MI, RD, RC); 2746*0b57cec5SDimitry Andric T = T || genExtractLow(MI, RD, RC); 2747*0b57cec5SDimitry Andric T = T || simplifyRCmp0(MI, RD); 2748*0b57cec5SDimitry Andric Changed |= T; 2749*0b57cec5SDimitry Andric continue; 2750*0b57cec5SDimitry Andric } 2751*0b57cec5SDimitry Andric 2752*0b57cec5SDimitry Andric if (FRC->getID() == Hexagon::PredRegsRegClassID) { 2753*0b57cec5SDimitry Andric bool T = simplifyTstbit(MI, RD, RC); 2754*0b57cec5SDimitry Andric Changed |= T; 2755*0b57cec5SDimitry Andric continue; 2756*0b57cec5SDimitry Andric } 2757*0b57cec5SDimitry Andric } 2758*0b57cec5SDimitry Andric return Changed; 2759*0b57cec5SDimitry Andric } 2760*0b57cec5SDimitry Andric 2761*0b57cec5SDimitry Andric bool HexagonBitSimplify::runOnMachineFunction(MachineFunction &MF) { 2762*0b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 2763*0b57cec5SDimitry Andric return false; 2764*0b57cec5SDimitry Andric 2765*0b57cec5SDimitry Andric auto &HST = MF.getSubtarget<HexagonSubtarget>(); 2766*0b57cec5SDimitry Andric auto &HRI = *HST.getRegisterInfo(); 2767*0b57cec5SDimitry Andric auto &HII = *HST.getInstrInfo(); 2768*0b57cec5SDimitry Andric 2769*0b57cec5SDimitry Andric MDT = &getAnalysis<MachineDominatorTree>(); 2770*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 2771*0b57cec5SDimitry Andric bool Changed; 2772*0b57cec5SDimitry Andric 2773*0b57cec5SDimitry Andric Changed = DeadCodeElimination(MF, *MDT).run(); 2774*0b57cec5SDimitry Andric 2775*0b57cec5SDimitry Andric const HexagonEvaluator HE(HRI, MRI, HII, MF); 2776*0b57cec5SDimitry Andric BitTracker BT(HE, MF); 2777*0b57cec5SDimitry Andric LLVM_DEBUG(BT.trace(true)); 2778*0b57cec5SDimitry Andric BT.run(); 2779*0b57cec5SDimitry Andric 2780*0b57cec5SDimitry Andric MachineBasicBlock &Entry = MF.front(); 2781*0b57cec5SDimitry Andric 2782*0b57cec5SDimitry Andric RegisterSet AIG; // Available registers for IG. 2783*0b57cec5SDimitry Andric ConstGeneration ImmG(BT, HII, MRI); 2784*0b57cec5SDimitry Andric Changed |= visitBlock(Entry, ImmG, AIG); 2785*0b57cec5SDimitry Andric 2786*0b57cec5SDimitry Andric RegisterSet ARE; // Available registers for RIE. 2787*0b57cec5SDimitry Andric RedundantInstrElimination RIE(BT, HII, HRI, MRI); 2788*0b57cec5SDimitry Andric bool Ried = visitBlock(Entry, RIE, ARE); 2789*0b57cec5SDimitry Andric if (Ried) { 2790*0b57cec5SDimitry Andric Changed = true; 2791*0b57cec5SDimitry Andric BT.run(); 2792*0b57cec5SDimitry Andric } 2793*0b57cec5SDimitry Andric 2794*0b57cec5SDimitry Andric RegisterSet ACG; // Available registers for CG. 2795*0b57cec5SDimitry Andric CopyGeneration CopyG(BT, HII, HRI, MRI); 2796*0b57cec5SDimitry Andric Changed |= visitBlock(Entry, CopyG, ACG); 2797*0b57cec5SDimitry Andric 2798*0b57cec5SDimitry Andric RegisterSet ACP; // Available registers for CP. 2799*0b57cec5SDimitry Andric CopyPropagation CopyP(HRI, MRI); 2800*0b57cec5SDimitry Andric Changed |= visitBlock(Entry, CopyP, ACP); 2801*0b57cec5SDimitry Andric 2802*0b57cec5SDimitry Andric Changed = DeadCodeElimination(MF, *MDT).run() || Changed; 2803*0b57cec5SDimitry Andric 2804*0b57cec5SDimitry Andric BT.run(); 2805*0b57cec5SDimitry Andric RegisterSet ABS; // Available registers for BS. 2806*0b57cec5SDimitry Andric BitSimplification BitS(BT, *MDT, HII, HRI, MRI, MF); 2807*0b57cec5SDimitry Andric Changed |= visitBlock(Entry, BitS, ABS); 2808*0b57cec5SDimitry Andric 2809*0b57cec5SDimitry Andric Changed = DeadCodeElimination(MF, *MDT).run() || Changed; 2810*0b57cec5SDimitry Andric 2811*0b57cec5SDimitry Andric if (Changed) { 2812*0b57cec5SDimitry Andric for (auto &B : MF) 2813*0b57cec5SDimitry Andric for (auto &I : B) 2814*0b57cec5SDimitry Andric I.clearKillInfo(); 2815*0b57cec5SDimitry Andric DeadCodeElimination(MF, *MDT).run(); 2816*0b57cec5SDimitry Andric } 2817*0b57cec5SDimitry Andric return Changed; 2818*0b57cec5SDimitry Andric } 2819*0b57cec5SDimitry Andric 2820*0b57cec5SDimitry Andric // Recognize loops where the code at the end of the loop matches the code 2821*0b57cec5SDimitry Andric // before the entry of the loop, and the matching code is such that is can 2822*0b57cec5SDimitry Andric // be simplified. This pass relies on the bit simplification above and only 2823*0b57cec5SDimitry Andric // prepares code in a way that can be handled by the bit simplifcation. 2824*0b57cec5SDimitry Andric // 2825*0b57cec5SDimitry Andric // This is the motivating testcase (and explanation): 2826*0b57cec5SDimitry Andric // 2827*0b57cec5SDimitry Andric // { 2828*0b57cec5SDimitry Andric // loop0(.LBB0_2, r1) // %for.body.preheader 2829*0b57cec5SDimitry Andric // r5:4 = memd(r0++#8) 2830*0b57cec5SDimitry Andric // } 2831*0b57cec5SDimitry Andric // { 2832*0b57cec5SDimitry Andric // r3 = lsr(r4, #16) 2833*0b57cec5SDimitry Andric // r7:6 = combine(r5, r5) 2834*0b57cec5SDimitry Andric // } 2835*0b57cec5SDimitry Andric // { 2836*0b57cec5SDimitry Andric // r3 = insert(r5, #16, #16) 2837*0b57cec5SDimitry Andric // r7:6 = vlsrw(r7:6, #16) 2838*0b57cec5SDimitry Andric // } 2839*0b57cec5SDimitry Andric // .LBB0_2: 2840*0b57cec5SDimitry Andric // { 2841*0b57cec5SDimitry Andric // memh(r2+#4) = r5 2842*0b57cec5SDimitry Andric // memh(r2+#6) = r6 # R6 is really R5.H 2843*0b57cec5SDimitry Andric // } 2844*0b57cec5SDimitry Andric // { 2845*0b57cec5SDimitry Andric // r2 = add(r2, #8) 2846*0b57cec5SDimitry Andric // memh(r2+#0) = r4 2847*0b57cec5SDimitry Andric // memh(r2+#2) = r3 # R3 is really R4.H 2848*0b57cec5SDimitry Andric // } 2849*0b57cec5SDimitry Andric // { 2850*0b57cec5SDimitry Andric // r5:4 = memd(r0++#8) 2851*0b57cec5SDimitry Andric // } 2852*0b57cec5SDimitry Andric // { # "Shuffling" code that sets up R3 and R6 2853*0b57cec5SDimitry Andric // r3 = lsr(r4, #16) # so that their halves can be stored in the 2854*0b57cec5SDimitry Andric // r7:6 = combine(r5, r5) # next iteration. This could be folded into 2855*0b57cec5SDimitry Andric // } # the stores if the code was at the beginning 2856*0b57cec5SDimitry Andric // { # of the loop iteration. Since the same code 2857*0b57cec5SDimitry Andric // r3 = insert(r5, #16, #16) # precedes the loop, it can actually be moved 2858*0b57cec5SDimitry Andric // r7:6 = vlsrw(r7:6, #16) # there. 2859*0b57cec5SDimitry Andric // }:endloop0 2860*0b57cec5SDimitry Andric // 2861*0b57cec5SDimitry Andric // 2862*0b57cec5SDimitry Andric // The outcome: 2863*0b57cec5SDimitry Andric // 2864*0b57cec5SDimitry Andric // { 2865*0b57cec5SDimitry Andric // loop0(.LBB0_2, r1) 2866*0b57cec5SDimitry Andric // r5:4 = memd(r0++#8) 2867*0b57cec5SDimitry Andric // } 2868*0b57cec5SDimitry Andric // .LBB0_2: 2869*0b57cec5SDimitry Andric // { 2870*0b57cec5SDimitry Andric // memh(r2+#4) = r5 2871*0b57cec5SDimitry Andric // memh(r2+#6) = r5.h 2872*0b57cec5SDimitry Andric // } 2873*0b57cec5SDimitry Andric // { 2874*0b57cec5SDimitry Andric // r2 = add(r2, #8) 2875*0b57cec5SDimitry Andric // memh(r2+#0) = r4 2876*0b57cec5SDimitry Andric // memh(r2+#2) = r4.h 2877*0b57cec5SDimitry Andric // } 2878*0b57cec5SDimitry Andric // { 2879*0b57cec5SDimitry Andric // r5:4 = memd(r0++#8) 2880*0b57cec5SDimitry Andric // }:endloop0 2881*0b57cec5SDimitry Andric 2882*0b57cec5SDimitry Andric namespace llvm { 2883*0b57cec5SDimitry Andric 2884*0b57cec5SDimitry Andric FunctionPass *createHexagonLoopRescheduling(); 2885*0b57cec5SDimitry Andric void initializeHexagonLoopReschedulingPass(PassRegistry&); 2886*0b57cec5SDimitry Andric 2887*0b57cec5SDimitry Andric } // end namespace llvm 2888*0b57cec5SDimitry Andric 2889*0b57cec5SDimitry Andric namespace { 2890*0b57cec5SDimitry Andric 2891*0b57cec5SDimitry Andric class HexagonLoopRescheduling : public MachineFunctionPass { 2892*0b57cec5SDimitry Andric public: 2893*0b57cec5SDimitry Andric static char ID; 2894*0b57cec5SDimitry Andric 2895*0b57cec5SDimitry Andric HexagonLoopRescheduling() : MachineFunctionPass(ID) { 2896*0b57cec5SDimitry Andric initializeHexagonLoopReschedulingPass(*PassRegistry::getPassRegistry()); 2897*0b57cec5SDimitry Andric } 2898*0b57cec5SDimitry Andric 2899*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 2900*0b57cec5SDimitry Andric 2901*0b57cec5SDimitry Andric private: 2902*0b57cec5SDimitry Andric const HexagonInstrInfo *HII = nullptr; 2903*0b57cec5SDimitry Andric const HexagonRegisterInfo *HRI = nullptr; 2904*0b57cec5SDimitry Andric MachineRegisterInfo *MRI = nullptr; 2905*0b57cec5SDimitry Andric BitTracker *BTP = nullptr; 2906*0b57cec5SDimitry Andric 2907*0b57cec5SDimitry Andric struct LoopCand { 2908*0b57cec5SDimitry Andric LoopCand(MachineBasicBlock *lb, MachineBasicBlock *pb, 2909*0b57cec5SDimitry Andric MachineBasicBlock *eb) : LB(lb), PB(pb), EB(eb) {} 2910*0b57cec5SDimitry Andric 2911*0b57cec5SDimitry Andric MachineBasicBlock *LB, *PB, *EB; 2912*0b57cec5SDimitry Andric }; 2913*0b57cec5SDimitry Andric using InstrList = std::vector<MachineInstr *>; 2914*0b57cec5SDimitry Andric struct InstrGroup { 2915*0b57cec5SDimitry Andric BitTracker::RegisterRef Inp, Out; 2916*0b57cec5SDimitry Andric InstrList Ins; 2917*0b57cec5SDimitry Andric }; 2918*0b57cec5SDimitry Andric struct PhiInfo { 2919*0b57cec5SDimitry Andric PhiInfo(MachineInstr &P, MachineBasicBlock &B); 2920*0b57cec5SDimitry Andric 2921*0b57cec5SDimitry Andric unsigned DefR; 2922*0b57cec5SDimitry Andric BitTracker::RegisterRef LR, PR; // Loop Register, Preheader Register 2923*0b57cec5SDimitry Andric MachineBasicBlock *LB, *PB; // Loop Block, Preheader Block 2924*0b57cec5SDimitry Andric }; 2925*0b57cec5SDimitry Andric 2926*0b57cec5SDimitry Andric static unsigned getDefReg(const MachineInstr *MI); 2927*0b57cec5SDimitry Andric bool isConst(unsigned Reg) const; 2928*0b57cec5SDimitry Andric bool isBitShuffle(const MachineInstr *MI, unsigned DefR) const; 2929*0b57cec5SDimitry Andric bool isStoreInput(const MachineInstr *MI, unsigned DefR) const; 2930*0b57cec5SDimitry Andric bool isShuffleOf(unsigned OutR, unsigned InpR) const; 2931*0b57cec5SDimitry Andric bool isSameShuffle(unsigned OutR1, unsigned InpR1, unsigned OutR2, 2932*0b57cec5SDimitry Andric unsigned &InpR2) const; 2933*0b57cec5SDimitry Andric void moveGroup(InstrGroup &G, MachineBasicBlock &LB, MachineBasicBlock &PB, 2934*0b57cec5SDimitry Andric MachineBasicBlock::iterator At, unsigned OldPhiR, unsigned NewPredR); 2935*0b57cec5SDimitry Andric bool processLoop(LoopCand &C); 2936*0b57cec5SDimitry Andric }; 2937*0b57cec5SDimitry Andric 2938*0b57cec5SDimitry Andric } // end anonymous namespace 2939*0b57cec5SDimitry Andric 2940*0b57cec5SDimitry Andric char HexagonLoopRescheduling::ID = 0; 2941*0b57cec5SDimitry Andric 2942*0b57cec5SDimitry Andric INITIALIZE_PASS(HexagonLoopRescheduling, "hexagon-loop-resched", 2943*0b57cec5SDimitry Andric "Hexagon Loop Rescheduling", false, false) 2944*0b57cec5SDimitry Andric 2945*0b57cec5SDimitry Andric HexagonLoopRescheduling::PhiInfo::PhiInfo(MachineInstr &P, 2946*0b57cec5SDimitry Andric MachineBasicBlock &B) { 2947*0b57cec5SDimitry Andric DefR = HexagonLoopRescheduling::getDefReg(&P); 2948*0b57cec5SDimitry Andric LB = &B; 2949*0b57cec5SDimitry Andric PB = nullptr; 2950*0b57cec5SDimitry Andric for (unsigned i = 1, n = P.getNumOperands(); i < n; i += 2) { 2951*0b57cec5SDimitry Andric const MachineOperand &OpB = P.getOperand(i+1); 2952*0b57cec5SDimitry Andric if (OpB.getMBB() == &B) { 2953*0b57cec5SDimitry Andric LR = P.getOperand(i); 2954*0b57cec5SDimitry Andric continue; 2955*0b57cec5SDimitry Andric } 2956*0b57cec5SDimitry Andric PB = OpB.getMBB(); 2957*0b57cec5SDimitry Andric PR = P.getOperand(i); 2958*0b57cec5SDimitry Andric } 2959*0b57cec5SDimitry Andric } 2960*0b57cec5SDimitry Andric 2961*0b57cec5SDimitry Andric unsigned HexagonLoopRescheduling::getDefReg(const MachineInstr *MI) { 2962*0b57cec5SDimitry Andric RegisterSet Defs; 2963*0b57cec5SDimitry Andric HBS::getInstrDefs(*MI, Defs); 2964*0b57cec5SDimitry Andric if (Defs.count() != 1) 2965*0b57cec5SDimitry Andric return 0; 2966*0b57cec5SDimitry Andric return Defs.find_first(); 2967*0b57cec5SDimitry Andric } 2968*0b57cec5SDimitry Andric 2969*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::isConst(unsigned Reg) const { 2970*0b57cec5SDimitry Andric if (!BTP->has(Reg)) 2971*0b57cec5SDimitry Andric return false; 2972*0b57cec5SDimitry Andric const BitTracker::RegisterCell &RC = BTP->lookup(Reg); 2973*0b57cec5SDimitry Andric for (unsigned i = 0, w = RC.width(); i < w; ++i) { 2974*0b57cec5SDimitry Andric const BitTracker::BitValue &V = RC[i]; 2975*0b57cec5SDimitry Andric if (!V.is(0) && !V.is(1)) 2976*0b57cec5SDimitry Andric return false; 2977*0b57cec5SDimitry Andric } 2978*0b57cec5SDimitry Andric return true; 2979*0b57cec5SDimitry Andric } 2980*0b57cec5SDimitry Andric 2981*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::isBitShuffle(const MachineInstr *MI, 2982*0b57cec5SDimitry Andric unsigned DefR) const { 2983*0b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 2984*0b57cec5SDimitry Andric switch (Opc) { 2985*0b57cec5SDimitry Andric case TargetOpcode::COPY: 2986*0b57cec5SDimitry Andric case Hexagon::S2_lsr_i_r: 2987*0b57cec5SDimitry Andric case Hexagon::S2_asr_i_r: 2988*0b57cec5SDimitry Andric case Hexagon::S2_asl_i_r: 2989*0b57cec5SDimitry Andric case Hexagon::S2_lsr_i_p: 2990*0b57cec5SDimitry Andric case Hexagon::S2_asr_i_p: 2991*0b57cec5SDimitry Andric case Hexagon::S2_asl_i_p: 2992*0b57cec5SDimitry Andric case Hexagon::S2_insert: 2993*0b57cec5SDimitry Andric case Hexagon::A2_or: 2994*0b57cec5SDimitry Andric case Hexagon::A2_orp: 2995*0b57cec5SDimitry Andric case Hexagon::A2_and: 2996*0b57cec5SDimitry Andric case Hexagon::A2_andp: 2997*0b57cec5SDimitry Andric case Hexagon::A2_combinew: 2998*0b57cec5SDimitry Andric case Hexagon::A4_combineri: 2999*0b57cec5SDimitry Andric case Hexagon::A4_combineir: 3000*0b57cec5SDimitry Andric case Hexagon::A2_combineii: 3001*0b57cec5SDimitry Andric case Hexagon::A4_combineii: 3002*0b57cec5SDimitry Andric case Hexagon::A2_combine_ll: 3003*0b57cec5SDimitry Andric case Hexagon::A2_combine_lh: 3004*0b57cec5SDimitry Andric case Hexagon::A2_combine_hl: 3005*0b57cec5SDimitry Andric case Hexagon::A2_combine_hh: 3006*0b57cec5SDimitry Andric return true; 3007*0b57cec5SDimitry Andric } 3008*0b57cec5SDimitry Andric return false; 3009*0b57cec5SDimitry Andric } 3010*0b57cec5SDimitry Andric 3011*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::isStoreInput(const MachineInstr *MI, 3012*0b57cec5SDimitry Andric unsigned InpR) const { 3013*0b57cec5SDimitry Andric for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) { 3014*0b57cec5SDimitry Andric const MachineOperand &Op = MI->getOperand(i); 3015*0b57cec5SDimitry Andric if (!Op.isReg()) 3016*0b57cec5SDimitry Andric continue; 3017*0b57cec5SDimitry Andric if (Op.getReg() == InpR) 3018*0b57cec5SDimitry Andric return i == n-1; 3019*0b57cec5SDimitry Andric } 3020*0b57cec5SDimitry Andric return false; 3021*0b57cec5SDimitry Andric } 3022*0b57cec5SDimitry Andric 3023*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::isShuffleOf(unsigned OutR, unsigned InpR) const { 3024*0b57cec5SDimitry Andric if (!BTP->has(OutR) || !BTP->has(InpR)) 3025*0b57cec5SDimitry Andric return false; 3026*0b57cec5SDimitry Andric const BitTracker::RegisterCell &OutC = BTP->lookup(OutR); 3027*0b57cec5SDimitry Andric for (unsigned i = 0, w = OutC.width(); i < w; ++i) { 3028*0b57cec5SDimitry Andric const BitTracker::BitValue &V = OutC[i]; 3029*0b57cec5SDimitry Andric if (V.Type != BitTracker::BitValue::Ref) 3030*0b57cec5SDimitry Andric continue; 3031*0b57cec5SDimitry Andric if (V.RefI.Reg != InpR) 3032*0b57cec5SDimitry Andric return false; 3033*0b57cec5SDimitry Andric } 3034*0b57cec5SDimitry Andric return true; 3035*0b57cec5SDimitry Andric } 3036*0b57cec5SDimitry Andric 3037*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::isSameShuffle(unsigned OutR1, unsigned InpR1, 3038*0b57cec5SDimitry Andric unsigned OutR2, unsigned &InpR2) const { 3039*0b57cec5SDimitry Andric if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2)) 3040*0b57cec5SDimitry Andric return false; 3041*0b57cec5SDimitry Andric const BitTracker::RegisterCell &OutC1 = BTP->lookup(OutR1); 3042*0b57cec5SDimitry Andric const BitTracker::RegisterCell &OutC2 = BTP->lookup(OutR2); 3043*0b57cec5SDimitry Andric unsigned W = OutC1.width(); 3044*0b57cec5SDimitry Andric unsigned MatchR = 0; 3045*0b57cec5SDimitry Andric if (W != OutC2.width()) 3046*0b57cec5SDimitry Andric return false; 3047*0b57cec5SDimitry Andric for (unsigned i = 0; i < W; ++i) { 3048*0b57cec5SDimitry Andric const BitTracker::BitValue &V1 = OutC1[i], &V2 = OutC2[i]; 3049*0b57cec5SDimitry Andric if (V1.Type != V2.Type || V1.Type == BitTracker::BitValue::One) 3050*0b57cec5SDimitry Andric return false; 3051*0b57cec5SDimitry Andric if (V1.Type != BitTracker::BitValue::Ref) 3052*0b57cec5SDimitry Andric continue; 3053*0b57cec5SDimitry Andric if (V1.RefI.Pos != V2.RefI.Pos) 3054*0b57cec5SDimitry Andric return false; 3055*0b57cec5SDimitry Andric if (V1.RefI.Reg != InpR1) 3056*0b57cec5SDimitry Andric return false; 3057*0b57cec5SDimitry Andric if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2) 3058*0b57cec5SDimitry Andric return false; 3059*0b57cec5SDimitry Andric if (!MatchR) 3060*0b57cec5SDimitry Andric MatchR = V2.RefI.Reg; 3061*0b57cec5SDimitry Andric else if (V2.RefI.Reg != MatchR) 3062*0b57cec5SDimitry Andric return false; 3063*0b57cec5SDimitry Andric } 3064*0b57cec5SDimitry Andric InpR2 = MatchR; 3065*0b57cec5SDimitry Andric return true; 3066*0b57cec5SDimitry Andric } 3067*0b57cec5SDimitry Andric 3068*0b57cec5SDimitry Andric void HexagonLoopRescheduling::moveGroup(InstrGroup &G, MachineBasicBlock &LB, 3069*0b57cec5SDimitry Andric MachineBasicBlock &PB, MachineBasicBlock::iterator At, unsigned OldPhiR, 3070*0b57cec5SDimitry Andric unsigned NewPredR) { 3071*0b57cec5SDimitry Andric DenseMap<unsigned,unsigned> RegMap; 3072*0b57cec5SDimitry Andric 3073*0b57cec5SDimitry Andric const TargetRegisterClass *PhiRC = MRI->getRegClass(NewPredR); 3074*0b57cec5SDimitry Andric unsigned PhiR = MRI->createVirtualRegister(PhiRC); 3075*0b57cec5SDimitry Andric BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR) 3076*0b57cec5SDimitry Andric .addReg(NewPredR) 3077*0b57cec5SDimitry Andric .addMBB(&PB) 3078*0b57cec5SDimitry Andric .addReg(G.Inp.Reg) 3079*0b57cec5SDimitry Andric .addMBB(&LB); 3080*0b57cec5SDimitry Andric RegMap.insert(std::make_pair(G.Inp.Reg, PhiR)); 3081*0b57cec5SDimitry Andric 3082*0b57cec5SDimitry Andric for (unsigned i = G.Ins.size(); i > 0; --i) { 3083*0b57cec5SDimitry Andric const MachineInstr *SI = G.Ins[i-1]; 3084*0b57cec5SDimitry Andric unsigned DR = getDefReg(SI); 3085*0b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(DR); 3086*0b57cec5SDimitry Andric unsigned NewDR = MRI->createVirtualRegister(RC); 3087*0b57cec5SDimitry Andric DebugLoc DL = SI->getDebugLoc(); 3088*0b57cec5SDimitry Andric 3089*0b57cec5SDimitry Andric auto MIB = BuildMI(LB, At, DL, HII->get(SI->getOpcode()), NewDR); 3090*0b57cec5SDimitry Andric for (unsigned j = 0, m = SI->getNumOperands(); j < m; ++j) { 3091*0b57cec5SDimitry Andric const MachineOperand &Op = SI->getOperand(j); 3092*0b57cec5SDimitry Andric if (!Op.isReg()) { 3093*0b57cec5SDimitry Andric MIB.add(Op); 3094*0b57cec5SDimitry Andric continue; 3095*0b57cec5SDimitry Andric } 3096*0b57cec5SDimitry Andric if (!Op.isUse()) 3097*0b57cec5SDimitry Andric continue; 3098*0b57cec5SDimitry Andric unsigned UseR = RegMap[Op.getReg()]; 3099*0b57cec5SDimitry Andric MIB.addReg(UseR, 0, Op.getSubReg()); 3100*0b57cec5SDimitry Andric } 3101*0b57cec5SDimitry Andric RegMap.insert(std::make_pair(DR, NewDR)); 3102*0b57cec5SDimitry Andric } 3103*0b57cec5SDimitry Andric 3104*0b57cec5SDimitry Andric HBS::replaceReg(OldPhiR, RegMap[G.Out.Reg], *MRI); 3105*0b57cec5SDimitry Andric } 3106*0b57cec5SDimitry Andric 3107*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::processLoop(LoopCand &C) { 3108*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Processing loop in " << printMBBReference(*C.LB) 3109*0b57cec5SDimitry Andric << "\n"); 3110*0b57cec5SDimitry Andric std::vector<PhiInfo> Phis; 3111*0b57cec5SDimitry Andric for (auto &I : *C.LB) { 3112*0b57cec5SDimitry Andric if (!I.isPHI()) 3113*0b57cec5SDimitry Andric break; 3114*0b57cec5SDimitry Andric unsigned PR = getDefReg(&I); 3115*0b57cec5SDimitry Andric if (isConst(PR)) 3116*0b57cec5SDimitry Andric continue; 3117*0b57cec5SDimitry Andric bool BadUse = false, GoodUse = false; 3118*0b57cec5SDimitry Andric for (auto UI = MRI->use_begin(PR), UE = MRI->use_end(); UI != UE; ++UI) { 3119*0b57cec5SDimitry Andric MachineInstr *UseI = UI->getParent(); 3120*0b57cec5SDimitry Andric if (UseI->getParent() != C.LB) { 3121*0b57cec5SDimitry Andric BadUse = true; 3122*0b57cec5SDimitry Andric break; 3123*0b57cec5SDimitry Andric } 3124*0b57cec5SDimitry Andric if (isBitShuffle(UseI, PR) || isStoreInput(UseI, PR)) 3125*0b57cec5SDimitry Andric GoodUse = true; 3126*0b57cec5SDimitry Andric } 3127*0b57cec5SDimitry Andric if (BadUse || !GoodUse) 3128*0b57cec5SDimitry Andric continue; 3129*0b57cec5SDimitry Andric 3130*0b57cec5SDimitry Andric Phis.push_back(PhiInfo(I, *C.LB)); 3131*0b57cec5SDimitry Andric } 3132*0b57cec5SDimitry Andric 3133*0b57cec5SDimitry Andric LLVM_DEBUG({ 3134*0b57cec5SDimitry Andric dbgs() << "Phis: {"; 3135*0b57cec5SDimitry Andric for (auto &I : Phis) { 3136*0b57cec5SDimitry Andric dbgs() << ' ' << printReg(I.DefR, HRI) << "=phi(" 3137*0b57cec5SDimitry Andric << printReg(I.PR.Reg, HRI, I.PR.Sub) << ":b" << I.PB->getNumber() 3138*0b57cec5SDimitry Andric << ',' << printReg(I.LR.Reg, HRI, I.LR.Sub) << ":b" 3139*0b57cec5SDimitry Andric << I.LB->getNumber() << ')'; 3140*0b57cec5SDimitry Andric } 3141*0b57cec5SDimitry Andric dbgs() << " }\n"; 3142*0b57cec5SDimitry Andric }); 3143*0b57cec5SDimitry Andric 3144*0b57cec5SDimitry Andric if (Phis.empty()) 3145*0b57cec5SDimitry Andric return false; 3146*0b57cec5SDimitry Andric 3147*0b57cec5SDimitry Andric bool Changed = false; 3148*0b57cec5SDimitry Andric InstrList ShufIns; 3149*0b57cec5SDimitry Andric 3150*0b57cec5SDimitry Andric // Go backwards in the block: for each bit shuffling instruction, check 3151*0b57cec5SDimitry Andric // if that instruction could potentially be moved to the front of the loop: 3152*0b57cec5SDimitry Andric // the output of the loop cannot be used in a non-shuffling instruction 3153*0b57cec5SDimitry Andric // in this loop. 3154*0b57cec5SDimitry Andric for (auto I = C.LB->rbegin(), E = C.LB->rend(); I != E; ++I) { 3155*0b57cec5SDimitry Andric if (I->isTerminator()) 3156*0b57cec5SDimitry Andric continue; 3157*0b57cec5SDimitry Andric if (I->isPHI()) 3158*0b57cec5SDimitry Andric break; 3159*0b57cec5SDimitry Andric 3160*0b57cec5SDimitry Andric RegisterSet Defs; 3161*0b57cec5SDimitry Andric HBS::getInstrDefs(*I, Defs); 3162*0b57cec5SDimitry Andric if (Defs.count() != 1) 3163*0b57cec5SDimitry Andric continue; 3164*0b57cec5SDimitry Andric unsigned DefR = Defs.find_first(); 3165*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DefR)) 3166*0b57cec5SDimitry Andric continue; 3167*0b57cec5SDimitry Andric if (!isBitShuffle(&*I, DefR)) 3168*0b57cec5SDimitry Andric continue; 3169*0b57cec5SDimitry Andric 3170*0b57cec5SDimitry Andric bool BadUse = false; 3171*0b57cec5SDimitry Andric for (auto UI = MRI->use_begin(DefR), UE = MRI->use_end(); UI != UE; ++UI) { 3172*0b57cec5SDimitry Andric MachineInstr *UseI = UI->getParent(); 3173*0b57cec5SDimitry Andric if (UseI->getParent() == C.LB) { 3174*0b57cec5SDimitry Andric if (UseI->isPHI()) { 3175*0b57cec5SDimitry Andric // If the use is in a phi node in this loop, then it should be 3176*0b57cec5SDimitry Andric // the value corresponding to the back edge. 3177*0b57cec5SDimitry Andric unsigned Idx = UI.getOperandNo(); 3178*0b57cec5SDimitry Andric if (UseI->getOperand(Idx+1).getMBB() != C.LB) 3179*0b57cec5SDimitry Andric BadUse = true; 3180*0b57cec5SDimitry Andric } else { 3181*0b57cec5SDimitry Andric auto F = find(ShufIns, UseI); 3182*0b57cec5SDimitry Andric if (F == ShufIns.end()) 3183*0b57cec5SDimitry Andric BadUse = true; 3184*0b57cec5SDimitry Andric } 3185*0b57cec5SDimitry Andric } else { 3186*0b57cec5SDimitry Andric // There is a use outside of the loop, but there is no epilog block 3187*0b57cec5SDimitry Andric // suitable for a copy-out. 3188*0b57cec5SDimitry Andric if (C.EB == nullptr) 3189*0b57cec5SDimitry Andric BadUse = true; 3190*0b57cec5SDimitry Andric } 3191*0b57cec5SDimitry Andric if (BadUse) 3192*0b57cec5SDimitry Andric break; 3193*0b57cec5SDimitry Andric } 3194*0b57cec5SDimitry Andric 3195*0b57cec5SDimitry Andric if (BadUse) 3196*0b57cec5SDimitry Andric continue; 3197*0b57cec5SDimitry Andric ShufIns.push_back(&*I); 3198*0b57cec5SDimitry Andric } 3199*0b57cec5SDimitry Andric 3200*0b57cec5SDimitry Andric // Partition the list of shuffling instructions into instruction groups, 3201*0b57cec5SDimitry Andric // where each group has to be moved as a whole (i.e. a group is a chain of 3202*0b57cec5SDimitry Andric // dependent instructions). A group produces a single live output register, 3203*0b57cec5SDimitry Andric // which is meant to be the input of the loop phi node (although this is 3204*0b57cec5SDimitry Andric // not checked here yet). It also uses a single register as its input, 3205*0b57cec5SDimitry Andric // which is some value produced in the loop body. After moving the group 3206*0b57cec5SDimitry Andric // to the beginning of the loop, that input register would need to be 3207*0b57cec5SDimitry Andric // the loop-carried register (through a phi node) instead of the (currently 3208*0b57cec5SDimitry Andric // loop-carried) output register. 3209*0b57cec5SDimitry Andric using InstrGroupList = std::vector<InstrGroup>; 3210*0b57cec5SDimitry Andric InstrGroupList Groups; 3211*0b57cec5SDimitry Andric 3212*0b57cec5SDimitry Andric for (unsigned i = 0, n = ShufIns.size(); i < n; ++i) { 3213*0b57cec5SDimitry Andric MachineInstr *SI = ShufIns[i]; 3214*0b57cec5SDimitry Andric if (SI == nullptr) 3215*0b57cec5SDimitry Andric continue; 3216*0b57cec5SDimitry Andric 3217*0b57cec5SDimitry Andric InstrGroup G; 3218*0b57cec5SDimitry Andric G.Ins.push_back(SI); 3219*0b57cec5SDimitry Andric G.Out.Reg = getDefReg(SI); 3220*0b57cec5SDimitry Andric RegisterSet Inputs; 3221*0b57cec5SDimitry Andric HBS::getInstrUses(*SI, Inputs); 3222*0b57cec5SDimitry Andric 3223*0b57cec5SDimitry Andric for (unsigned j = i+1; j < n; ++j) { 3224*0b57cec5SDimitry Andric MachineInstr *MI = ShufIns[j]; 3225*0b57cec5SDimitry Andric if (MI == nullptr) 3226*0b57cec5SDimitry Andric continue; 3227*0b57cec5SDimitry Andric RegisterSet Defs; 3228*0b57cec5SDimitry Andric HBS::getInstrDefs(*MI, Defs); 3229*0b57cec5SDimitry Andric // If this instruction does not define any pending inputs, skip it. 3230*0b57cec5SDimitry Andric if (!Defs.intersects(Inputs)) 3231*0b57cec5SDimitry Andric continue; 3232*0b57cec5SDimitry Andric // Otherwise, add it to the current group and remove the inputs that 3233*0b57cec5SDimitry Andric // are defined by MI. 3234*0b57cec5SDimitry Andric G.Ins.push_back(MI); 3235*0b57cec5SDimitry Andric Inputs.remove(Defs); 3236*0b57cec5SDimitry Andric // Then add all registers used by MI. 3237*0b57cec5SDimitry Andric HBS::getInstrUses(*MI, Inputs); 3238*0b57cec5SDimitry Andric ShufIns[j] = nullptr; 3239*0b57cec5SDimitry Andric } 3240*0b57cec5SDimitry Andric 3241*0b57cec5SDimitry Andric // Only add a group if it requires at most one register. 3242*0b57cec5SDimitry Andric if (Inputs.count() > 1) 3243*0b57cec5SDimitry Andric continue; 3244*0b57cec5SDimitry Andric auto LoopInpEq = [G] (const PhiInfo &P) -> bool { 3245*0b57cec5SDimitry Andric return G.Out.Reg == P.LR.Reg; 3246*0b57cec5SDimitry Andric }; 3247*0b57cec5SDimitry Andric if (llvm::find_if(Phis, LoopInpEq) == Phis.end()) 3248*0b57cec5SDimitry Andric continue; 3249*0b57cec5SDimitry Andric 3250*0b57cec5SDimitry Andric G.Inp.Reg = Inputs.find_first(); 3251*0b57cec5SDimitry Andric Groups.push_back(G); 3252*0b57cec5SDimitry Andric } 3253*0b57cec5SDimitry Andric 3254*0b57cec5SDimitry Andric LLVM_DEBUG({ 3255*0b57cec5SDimitry Andric for (unsigned i = 0, n = Groups.size(); i < n; ++i) { 3256*0b57cec5SDimitry Andric InstrGroup &G = Groups[i]; 3257*0b57cec5SDimitry Andric dbgs() << "Group[" << i << "] inp: " 3258*0b57cec5SDimitry Andric << printReg(G.Inp.Reg, HRI, G.Inp.Sub) 3259*0b57cec5SDimitry Andric << " out: " << printReg(G.Out.Reg, HRI, G.Out.Sub) << "\n"; 3260*0b57cec5SDimitry Andric for (unsigned j = 0, m = G.Ins.size(); j < m; ++j) 3261*0b57cec5SDimitry Andric dbgs() << " " << *G.Ins[j]; 3262*0b57cec5SDimitry Andric } 3263*0b57cec5SDimitry Andric }); 3264*0b57cec5SDimitry Andric 3265*0b57cec5SDimitry Andric for (unsigned i = 0, n = Groups.size(); i < n; ++i) { 3266*0b57cec5SDimitry Andric InstrGroup &G = Groups[i]; 3267*0b57cec5SDimitry Andric if (!isShuffleOf(G.Out.Reg, G.Inp.Reg)) 3268*0b57cec5SDimitry Andric continue; 3269*0b57cec5SDimitry Andric auto LoopInpEq = [G] (const PhiInfo &P) -> bool { 3270*0b57cec5SDimitry Andric return G.Out.Reg == P.LR.Reg; 3271*0b57cec5SDimitry Andric }; 3272*0b57cec5SDimitry Andric auto F = llvm::find_if(Phis, LoopInpEq); 3273*0b57cec5SDimitry Andric if (F == Phis.end()) 3274*0b57cec5SDimitry Andric continue; 3275*0b57cec5SDimitry Andric unsigned PrehR = 0; 3276*0b57cec5SDimitry Andric if (!isSameShuffle(G.Out.Reg, G.Inp.Reg, F->PR.Reg, PrehR)) { 3277*0b57cec5SDimitry Andric const MachineInstr *DefPrehR = MRI->getVRegDef(F->PR.Reg); 3278*0b57cec5SDimitry Andric unsigned Opc = DefPrehR->getOpcode(); 3279*0b57cec5SDimitry Andric if (Opc != Hexagon::A2_tfrsi && Opc != Hexagon::A2_tfrpi) 3280*0b57cec5SDimitry Andric continue; 3281*0b57cec5SDimitry Andric if (!DefPrehR->getOperand(1).isImm()) 3282*0b57cec5SDimitry Andric continue; 3283*0b57cec5SDimitry Andric if (DefPrehR->getOperand(1).getImm() != 0) 3284*0b57cec5SDimitry Andric continue; 3285*0b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(G.Inp.Reg); 3286*0b57cec5SDimitry Andric if (RC != MRI->getRegClass(F->PR.Reg)) { 3287*0b57cec5SDimitry Andric PrehR = MRI->createVirtualRegister(RC); 3288*0b57cec5SDimitry Andric unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi 3289*0b57cec5SDimitry Andric : Hexagon::A2_tfrpi; 3290*0b57cec5SDimitry Andric auto T = C.PB->getFirstTerminator(); 3291*0b57cec5SDimitry Andric DebugLoc DL = (T != C.PB->end()) ? T->getDebugLoc() : DebugLoc(); 3292*0b57cec5SDimitry Andric BuildMI(*C.PB, T, DL, HII->get(TfrI), PrehR) 3293*0b57cec5SDimitry Andric .addImm(0); 3294*0b57cec5SDimitry Andric } else { 3295*0b57cec5SDimitry Andric PrehR = F->PR.Reg; 3296*0b57cec5SDimitry Andric } 3297*0b57cec5SDimitry Andric } 3298*0b57cec5SDimitry Andric // isSameShuffle could match with PrehR being of a wider class than 3299*0b57cec5SDimitry Andric // G.Inp.Reg, for example if G shuffles the low 32 bits of its input, 3300*0b57cec5SDimitry Andric // it would match for the input being a 32-bit register, and PrehR 3301*0b57cec5SDimitry Andric // being a 64-bit register (where the low 32 bits match). This could 3302*0b57cec5SDimitry Andric // be handled, but for now skip these cases. 3303*0b57cec5SDimitry Andric if (MRI->getRegClass(PrehR) != MRI->getRegClass(G.Inp.Reg)) 3304*0b57cec5SDimitry Andric continue; 3305*0b57cec5SDimitry Andric moveGroup(G, *F->LB, *F->PB, F->LB->getFirstNonPHI(), F->DefR, PrehR); 3306*0b57cec5SDimitry Andric Changed = true; 3307*0b57cec5SDimitry Andric } 3308*0b57cec5SDimitry Andric 3309*0b57cec5SDimitry Andric return Changed; 3310*0b57cec5SDimitry Andric } 3311*0b57cec5SDimitry Andric 3312*0b57cec5SDimitry Andric bool HexagonLoopRescheduling::runOnMachineFunction(MachineFunction &MF) { 3313*0b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 3314*0b57cec5SDimitry Andric return false; 3315*0b57cec5SDimitry Andric 3316*0b57cec5SDimitry Andric auto &HST = MF.getSubtarget<HexagonSubtarget>(); 3317*0b57cec5SDimitry Andric HII = HST.getInstrInfo(); 3318*0b57cec5SDimitry Andric HRI = HST.getRegisterInfo(); 3319*0b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 3320*0b57cec5SDimitry Andric const HexagonEvaluator HE(*HRI, *MRI, *HII, MF); 3321*0b57cec5SDimitry Andric BitTracker BT(HE, MF); 3322*0b57cec5SDimitry Andric LLVM_DEBUG(BT.trace(true)); 3323*0b57cec5SDimitry Andric BT.run(); 3324*0b57cec5SDimitry Andric BTP = &BT; 3325*0b57cec5SDimitry Andric 3326*0b57cec5SDimitry Andric std::vector<LoopCand> Cand; 3327*0b57cec5SDimitry Andric 3328*0b57cec5SDimitry Andric for (auto &B : MF) { 3329*0b57cec5SDimitry Andric if (B.pred_size() != 2 || B.succ_size() != 2) 3330*0b57cec5SDimitry Andric continue; 3331*0b57cec5SDimitry Andric MachineBasicBlock *PB = nullptr; 3332*0b57cec5SDimitry Andric bool IsLoop = false; 3333*0b57cec5SDimitry Andric for (auto PI = B.pred_begin(), PE = B.pred_end(); PI != PE; ++PI) { 3334*0b57cec5SDimitry Andric if (*PI != &B) 3335*0b57cec5SDimitry Andric PB = *PI; 3336*0b57cec5SDimitry Andric else 3337*0b57cec5SDimitry Andric IsLoop = true; 3338*0b57cec5SDimitry Andric } 3339*0b57cec5SDimitry Andric if (!IsLoop) 3340*0b57cec5SDimitry Andric continue; 3341*0b57cec5SDimitry Andric 3342*0b57cec5SDimitry Andric MachineBasicBlock *EB = nullptr; 3343*0b57cec5SDimitry Andric for (auto SI = B.succ_begin(), SE = B.succ_end(); SI != SE; ++SI) { 3344*0b57cec5SDimitry Andric if (*SI == &B) 3345*0b57cec5SDimitry Andric continue; 3346*0b57cec5SDimitry Andric // Set EP to the epilog block, if it has only 1 predecessor (i.e. the 3347*0b57cec5SDimitry Andric // edge from B to EP is non-critical. 3348*0b57cec5SDimitry Andric if ((*SI)->pred_size() == 1) 3349*0b57cec5SDimitry Andric EB = *SI; 3350*0b57cec5SDimitry Andric break; 3351*0b57cec5SDimitry Andric } 3352*0b57cec5SDimitry Andric 3353*0b57cec5SDimitry Andric Cand.push_back(LoopCand(&B, PB, EB)); 3354*0b57cec5SDimitry Andric } 3355*0b57cec5SDimitry Andric 3356*0b57cec5SDimitry Andric bool Changed = false; 3357*0b57cec5SDimitry Andric for (auto &C : Cand) 3358*0b57cec5SDimitry Andric Changed |= processLoop(C); 3359*0b57cec5SDimitry Andric 3360*0b57cec5SDimitry Andric return Changed; 3361*0b57cec5SDimitry Andric } 3362*0b57cec5SDimitry Andric 3363*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3364*0b57cec5SDimitry Andric // Public Constructor Functions 3365*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3366*0b57cec5SDimitry Andric 3367*0b57cec5SDimitry Andric FunctionPass *llvm::createHexagonLoopRescheduling() { 3368*0b57cec5SDimitry Andric return new HexagonLoopRescheduling(); 3369*0b57cec5SDimitry Andric } 3370*0b57cec5SDimitry Andric 3371*0b57cec5SDimitry Andric FunctionPass *llvm::createHexagonBitSimplify() { 3372*0b57cec5SDimitry Andric return new HexagonBitSimplify(); 3373*0b57cec5SDimitry Andric } 3374