1 //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the TargetRegisterInfo interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/BitVector.h" 15 #include "llvm/CodeGen/MachineFrameInfo.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/CodeGen/MachineRegisterInfo.h" 18 #include "llvm/CodeGen/VirtRegMap.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/Format.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/Target/TargetFrameLowering.h" 24 #include "llvm/Target/TargetRegisterInfo.h" 25 26 #define DEBUG_TYPE "target-reg-info" 27 28 using namespace llvm; 29 30 TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID, 31 regclass_iterator RCB, regclass_iterator RCE, 32 const char *const *SRINames, 33 const unsigned *SRILaneMasks, 34 unsigned SRICoveringLanes) 35 : InfoDesc(ID), SubRegIndexNames(SRINames), 36 SubRegIndexLaneMasks(SRILaneMasks), 37 RegClassBegin(RCB), RegClassEnd(RCE), 38 CoveringLanes(SRICoveringLanes) { 39 } 40 41 TargetRegisterInfo::~TargetRegisterInfo() {} 42 43 void TargetRegisterInfo::markSuperRegs(BitVector &RegisterSet, unsigned Reg) 44 const { 45 for (MCSuperRegIterator AI(Reg, this, true); AI.isValid(); ++AI) 46 RegisterSet.set(*AI); 47 } 48 49 bool TargetRegisterInfo::checkAllSuperRegsMarked(const BitVector &RegisterSet, 50 ArrayRef<MCPhysReg> Exceptions) const { 51 // Check that all super registers of reserved regs are reserved as well. 52 BitVector Checked(getNumRegs()); 53 for (int Reg = RegisterSet.find_first(); Reg>=0; 54 Reg = RegisterSet.find_next(Reg)) { 55 if (Checked[Reg]) 56 continue; 57 for (MCSuperRegIterator SR(Reg, this); SR.isValid(); ++SR) { 58 if (!RegisterSet[*SR] && !is_contained(Exceptions, Reg)) { 59 dbgs() << "Error: Super register " << PrintReg(*SR, this) 60 << " of reserved register " << PrintReg(Reg, this) 61 << " is not reserved.\n"; 62 return false; 63 } 64 65 // We transitively check superregs. So we can remember this for later 66 // to avoid compiletime explosion in deep register hierarchies. 67 Checked.set(*SR); 68 } 69 } 70 return true; 71 } 72 73 namespace llvm { 74 75 Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI, 76 unsigned SubIdx) { 77 return Printable([Reg, TRI, SubIdx](raw_ostream &OS) { 78 if (!Reg) 79 OS << "%noreg"; 80 else if (TargetRegisterInfo::isStackSlot(Reg)) 81 OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg); 82 else if (TargetRegisterInfo::isVirtualRegister(Reg)) 83 OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg); 84 else if (TRI && Reg < TRI->getNumRegs()) 85 OS << '%' << TRI->getName(Reg); 86 else 87 OS << "%physreg" << Reg; 88 if (SubIdx) { 89 if (TRI) 90 OS << ':' << TRI->getSubRegIndexName(SubIdx); 91 else 92 OS << ":sub(" << SubIdx << ')'; 93 } 94 }); 95 } 96 97 Printable PrintRegUnit(unsigned Unit, const TargetRegisterInfo *TRI) { 98 return Printable([Unit, TRI](raw_ostream &OS) { 99 // Generic printout when TRI is missing. 100 if (!TRI) { 101 OS << "Unit~" << Unit; 102 return; 103 } 104 105 // Check for invalid register units. 106 if (Unit >= TRI->getNumRegUnits()) { 107 OS << "BadUnit~" << Unit; 108 return; 109 } 110 111 // Normal units have at least one root. 112 MCRegUnitRootIterator Roots(Unit, TRI); 113 assert(Roots.isValid() && "Unit has no roots."); 114 OS << TRI->getName(*Roots); 115 for (++Roots; Roots.isValid(); ++Roots) 116 OS << '~' << TRI->getName(*Roots); 117 }); 118 } 119 120 Printable PrintVRegOrUnit(unsigned Unit, const TargetRegisterInfo *TRI) { 121 return Printable([Unit, TRI](raw_ostream &OS) { 122 if (TRI && TRI->isVirtualRegister(Unit)) { 123 OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Unit); 124 } else { 125 OS << PrintRegUnit(Unit, TRI); 126 } 127 }); 128 } 129 130 Printable PrintLaneMask(LaneBitmask LaneMask) { 131 return Printable([LaneMask](raw_ostream &OS) { 132 OS << format("%08X", LaneMask); 133 }); 134 } 135 136 } // End of llvm namespace 137 138 /// getAllocatableClass - Return the maximal subclass of the given register 139 /// class that is alloctable, or NULL. 140 const TargetRegisterClass * 141 TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const { 142 if (!RC || RC->isAllocatable()) 143 return RC; 144 145 for (BitMaskClassIterator It(RC->getSubClassMask(), *this); It.isValid(); 146 ++It) { 147 const TargetRegisterClass *SubRC = getRegClass(It.getID()); 148 if (SubRC->isAllocatable()) 149 return SubRC; 150 } 151 return nullptr; 152 } 153 154 /// getMinimalPhysRegClass - Returns the Register Class of a physical 155 /// register of the given type, picking the most sub register class of 156 /// the right type that contains this physreg. 157 const TargetRegisterClass * 158 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, MVT VT) const { 159 assert(isPhysicalRegister(reg) && "reg must be a physical register"); 160 161 // Pick the most sub register class of the right type that contains 162 // this physreg. 163 const TargetRegisterClass* BestRC = nullptr; 164 for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){ 165 const TargetRegisterClass* RC = *I; 166 if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) && 167 (!BestRC || BestRC->hasSubClass(RC))) 168 BestRC = RC; 169 } 170 171 assert(BestRC && "Couldn't find the register class"); 172 return BestRC; 173 } 174 175 /// getAllocatableSetForRC - Toggle the bits that represent allocatable 176 /// registers for the specific register class. 177 static void getAllocatableSetForRC(const MachineFunction &MF, 178 const TargetRegisterClass *RC, BitVector &R){ 179 assert(RC->isAllocatable() && "invalid for nonallocatable sets"); 180 ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF); 181 for (unsigned i = 0; i != Order.size(); ++i) 182 R.set(Order[i]); 183 } 184 185 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF, 186 const TargetRegisterClass *RC) const { 187 BitVector Allocatable(getNumRegs()); 188 if (RC) { 189 // A register class with no allocatable subclass returns an empty set. 190 const TargetRegisterClass *SubClass = getAllocatableClass(RC); 191 if (SubClass) 192 getAllocatableSetForRC(MF, SubClass, Allocatable); 193 } else { 194 for (TargetRegisterInfo::regclass_iterator I = regclass_begin(), 195 E = regclass_end(); I != E; ++I) 196 if ((*I)->isAllocatable()) 197 getAllocatableSetForRC(MF, *I, Allocatable); 198 } 199 200 // Mask out the reserved registers 201 BitVector Reserved = getReservedRegs(MF); 202 Allocatable &= Reserved.flip(); 203 204 return Allocatable; 205 } 206 207 static inline 208 const TargetRegisterClass *firstCommonClass(const uint32_t *A, 209 const uint32_t *B, 210 const TargetRegisterInfo *TRI, 211 const MVT::SimpleValueType SVT = 212 MVT::SimpleValueType::Any) { 213 const MVT VT(SVT); 214 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32) 215 if (unsigned Common = *A++ & *B++) { 216 const TargetRegisterClass *RC = 217 TRI->getRegClass(I + countTrailingZeros(Common)); 218 if (SVT == MVT::SimpleValueType::Any || RC->hasType(VT)) 219 return RC; 220 } 221 return nullptr; 222 } 223 224 const TargetRegisterClass * 225 TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A, 226 const TargetRegisterClass *B, 227 const MVT::SimpleValueType SVT) const { 228 // First take care of the trivial cases. 229 if (A == B) 230 return A; 231 if (!A || !B) 232 return nullptr; 233 234 // Register classes are ordered topologically, so the largest common 235 // sub-class it the common sub-class with the smallest ID. 236 return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this, SVT); 237 } 238 239 const TargetRegisterClass * 240 TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A, 241 const TargetRegisterClass *B, 242 unsigned Idx) const { 243 assert(A && B && "Missing register class"); 244 assert(Idx && "Bad sub-register index"); 245 246 // Find Idx in the list of super-register indices. 247 for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI) 248 if (RCI.getSubReg() == Idx) 249 // The bit mask contains all register classes that are projected into B 250 // by Idx. Find a class that is also a sub-class of A. 251 return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this); 252 return nullptr; 253 } 254 255 const TargetRegisterClass *TargetRegisterInfo:: 256 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA, 257 const TargetRegisterClass *RCB, unsigned SubB, 258 unsigned &PreA, unsigned &PreB) const { 259 assert(RCA && SubA && RCB && SubB && "Invalid arguments"); 260 261 // Search all pairs of sub-register indices that project into RCA and RCB 262 // respectively. This is quadratic, but usually the sets are very small. On 263 // most targets like X86, there will only be a single sub-register index 264 // (e.g., sub_16bit projecting into GR16). 265 // 266 // The worst case is a register class like DPR on ARM. 267 // We have indices dsub_0..dsub_7 projecting into that class. 268 // 269 // It is very common that one register class is a sub-register of the other. 270 // Arrange for RCA to be the larger register so the answer will be found in 271 // the first iteration. This makes the search linear for the most common 272 // case. 273 const TargetRegisterClass *BestRC = nullptr; 274 unsigned *BestPreA = &PreA; 275 unsigned *BestPreB = &PreB; 276 if (RCA->getSize() < RCB->getSize()) { 277 std::swap(RCA, RCB); 278 std::swap(SubA, SubB); 279 std::swap(BestPreA, BestPreB); 280 } 281 282 // Also terminate the search one we have found a register class as small as 283 // RCA. 284 unsigned MinSize = RCA->getSize(); 285 286 for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) { 287 unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA); 288 for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) { 289 // Check if a common super-register class exists for this index pair. 290 const TargetRegisterClass *RC = 291 firstCommonClass(IA.getMask(), IB.getMask(), this); 292 if (!RC || RC->getSize() < MinSize) 293 continue; 294 295 // The indexes must compose identically: PreA+SubA == PreB+SubB. 296 unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB); 297 if (FinalA != FinalB) 298 continue; 299 300 // Is RC a better candidate than BestRC? 301 if (BestRC && RC->getSize() >= BestRC->getSize()) 302 continue; 303 304 // Yes, RC is the smallest super-register seen so far. 305 BestRC = RC; 306 *BestPreA = IA.getSubReg(); 307 *BestPreB = IB.getSubReg(); 308 309 // Bail early if we reached MinSize. We won't find a better candidate. 310 if (BestRC->getSize() == MinSize) 311 return BestRC; 312 } 313 } 314 return BestRC; 315 } 316 317 /// \brief Check if the registers defined by the pair (RegisterClass, SubReg) 318 /// share the same register file. 319 static bool shareSameRegisterFile(const TargetRegisterInfo &TRI, 320 const TargetRegisterClass *DefRC, 321 unsigned DefSubReg, 322 const TargetRegisterClass *SrcRC, 323 unsigned SrcSubReg) { 324 // Same register class. 325 if (DefRC == SrcRC) 326 return true; 327 328 // Both operands are sub registers. Check if they share a register class. 329 unsigned SrcIdx, DefIdx; 330 if (SrcSubReg && DefSubReg) { 331 return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg, 332 SrcIdx, DefIdx) != nullptr; 333 } 334 335 // At most one of the register is a sub register, make it Src to avoid 336 // duplicating the test. 337 if (!SrcSubReg) { 338 std::swap(DefSubReg, SrcSubReg); 339 std::swap(DefRC, SrcRC); 340 } 341 342 // One of the register is a sub register, check if we can get a superclass. 343 if (SrcSubReg) 344 return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr; 345 346 // Plain copy. 347 return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr; 348 } 349 350 bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC, 351 unsigned DefSubReg, 352 const TargetRegisterClass *SrcRC, 353 unsigned SrcSubReg) const { 354 // If this source does not incur a cross register bank copy, use it. 355 return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg); 356 } 357 358 // Compute target-independent register allocator hints to help eliminate copies. 359 void 360 TargetRegisterInfo::getRegAllocationHints(unsigned VirtReg, 361 ArrayRef<MCPhysReg> Order, 362 SmallVectorImpl<MCPhysReg> &Hints, 363 const MachineFunction &MF, 364 const VirtRegMap *VRM, 365 const LiveRegMatrix *Matrix) const { 366 const MachineRegisterInfo &MRI = MF.getRegInfo(); 367 std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg); 368 369 // Hints with HintType != 0 were set by target-dependent code. 370 // Such targets must provide their own implementation of 371 // TRI::getRegAllocationHints to interpret those hint types. 372 assert(Hint.first == 0 && "Target must implement TRI::getRegAllocationHints"); 373 374 // Target-independent hints are either a physical or a virtual register. 375 unsigned Phys = Hint.second; 376 if (VRM && isVirtualRegister(Phys)) 377 Phys = VRM->getPhys(Phys); 378 379 // Check that Phys is a valid hint in VirtReg's register class. 380 if (!isPhysicalRegister(Phys)) 381 return; 382 if (MRI.isReserved(Phys)) 383 return; 384 // Check that Phys is in the allocation order. We shouldn't heed hints 385 // from VirtReg's register class if they aren't in the allocation order. The 386 // target probably has a reason for removing the register. 387 if (!is_contained(Order, Phys)) 388 return; 389 390 // All clear, tell the register allocator to prefer this register. 391 Hints.push_back(Phys); 392 } 393 394 bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const { 395 return !MF.getFunction()->hasFnAttribute("no-realign-stack"); 396 } 397 398 bool TargetRegisterInfo::needsStackRealignment( 399 const MachineFunction &MF) const { 400 const MachineFrameInfo &MFI = MF.getFrameInfo(); 401 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 402 const Function *F = MF.getFunction(); 403 unsigned StackAlign = TFI->getStackAlignment(); 404 bool requiresRealignment = ((MFI.getMaxAlignment() > StackAlign) || 405 F->hasFnAttribute(Attribute::StackAlignment)); 406 if (MF.getFunction()->hasFnAttribute("stackrealign") || requiresRealignment) { 407 if (canRealignStack(MF)) 408 return true; 409 DEBUG(dbgs() << "Can't realign function's stack: " << F->getName() << "\n"); 410 } 411 return false; 412 } 413 414 bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0, 415 const uint32_t *mask1) const { 416 unsigned N = (getNumRegs()+31) / 32; 417 for (unsigned I = 0; I < N; ++I) 418 if ((mask0[I] & mask1[I]) != mask0[I]) 419 return false; 420 return true; 421 } 422 423 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 424 void 425 TargetRegisterInfo::dumpReg(unsigned Reg, unsigned SubRegIndex, 426 const TargetRegisterInfo *TRI) { 427 dbgs() << PrintReg(Reg, TRI, SubRegIndex) << "\n"; 428 } 429 #endif 430