1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the PowerPC implementation of the TargetInstrInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "PPCInstrInfo.h" 14 #include "MCTargetDesc/PPCPredicates.h" 15 #include "PPC.h" 16 #include "PPCHazardRecognizers.h" 17 #include "PPCInstrBuilder.h" 18 #include "PPCMachineFunctionInfo.h" 19 #include "PPCTargetMachine.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/CodeGen/LiveIntervals.h" 23 #include "llvm/CodeGen/MachineFrameInfo.h" 24 #include "llvm/CodeGen/MachineFunctionPass.h" 25 #include "llvm/CodeGen/MachineInstrBuilder.h" 26 #include "llvm/CodeGen/MachineMemOperand.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/CodeGen/PseudoSourceValue.h" 29 #include "llvm/CodeGen/ScheduleDAG.h" 30 #include "llvm/CodeGen/SlotIndexes.h" 31 #include "llvm/CodeGen/StackMaps.h" 32 #include "llvm/MC/MCAsmInfo.h" 33 #include "llvm/MC/MCInst.h" 34 #include "llvm/Support/CommandLine.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/TargetRegistry.h" 38 #include "llvm/Support/raw_ostream.h" 39 40 using namespace llvm; 41 42 #define DEBUG_TYPE "ppc-instr-info" 43 44 #define GET_INSTRMAP_INFO 45 #define GET_INSTRINFO_CTOR_DTOR 46 #include "PPCGenInstrInfo.inc" 47 48 STATISTIC(NumStoreSPILLVSRRCAsVec, 49 "Number of spillvsrrc spilled to stack as vec"); 50 STATISTIC(NumStoreSPILLVSRRCAsGpr, 51 "Number of spillvsrrc spilled to stack as gpr"); 52 STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc"); 53 STATISTIC(CmpIselsConverted, 54 "Number of ISELs that depend on comparison of constants converted"); 55 STATISTIC(MissedConvertibleImmediateInstrs, 56 "Number of compare-immediate instructions fed by constants"); 57 STATISTIC(NumRcRotatesConvertedToRcAnd, 58 "Number of record-form rotates converted to record-form andi"); 59 60 static cl:: 61 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, 62 cl::desc("Disable analysis for CTR loops")); 63 64 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", 65 cl::desc("Disable compare instruction optimization"), cl::Hidden); 66 67 static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", 68 cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), 69 cl::Hidden); 70 71 static cl::opt<bool> 72 UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden, 73 cl::desc("Use the old (incorrect) instruction latency calculation")); 74 75 // Pin the vtable to this file. 76 void PPCInstrInfo::anchor() {} 77 78 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) 79 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP, 80 /* CatchRetOpcode */ -1, 81 STI.isPPC64() ? PPC::BLR8 : PPC::BLR), 82 Subtarget(STI), RI(STI.getTargetMachine()) {} 83 84 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for 85 /// this target when scheduling the DAG. 86 ScheduleHazardRecognizer * 87 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, 88 const ScheduleDAG *DAG) const { 89 unsigned Directive = 90 static_cast<const PPCSubtarget *>(STI)->getCPUDirective(); 91 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || 92 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { 93 const InstrItineraryData *II = 94 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); 95 return new ScoreboardHazardRecognizer(II, DAG); 96 } 97 98 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); 99 } 100 101 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer 102 /// to use for this target when scheduling the DAG. 103 ScheduleHazardRecognizer * 104 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 105 const ScheduleDAG *DAG) const { 106 unsigned Directive = 107 DAG->MF.getSubtarget<PPCSubtarget>().getCPUDirective(); 108 109 // FIXME: Leaving this as-is until we have POWER9 scheduling info 110 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) 111 return new PPCDispatchGroupSBHazardRecognizer(II, DAG); 112 113 // Most subtargets use a PPC970 recognizer. 114 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && 115 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { 116 assert(DAG->TII && "No InstrInfo?"); 117 118 return new PPCHazardRecognizer970(*DAG); 119 } 120 121 return new ScoreboardHazardRecognizer(II, DAG); 122 } 123 124 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 125 const MachineInstr &MI, 126 unsigned *PredCost) const { 127 if (!ItinData || UseOldLatencyCalc) 128 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost); 129 130 // The default implementation of getInstrLatency calls getStageLatency, but 131 // getStageLatency does not do the right thing for us. While we have 132 // itinerary, most cores are fully pipelined, and so the itineraries only 133 // express the first part of the pipeline, not every stage. Instead, we need 134 // to use the listed output operand cycle number (using operand 0 here, which 135 // is an output). 136 137 unsigned Latency = 1; 138 unsigned DefClass = MI.getDesc().getSchedClass(); 139 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 140 const MachineOperand &MO = MI.getOperand(i); 141 if (!MO.isReg() || !MO.isDef() || MO.isImplicit()) 142 continue; 143 144 int Cycle = ItinData->getOperandCycle(DefClass, i); 145 if (Cycle < 0) 146 continue; 147 148 Latency = std::max(Latency, (unsigned) Cycle); 149 } 150 151 return Latency; 152 } 153 154 int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 155 const MachineInstr &DefMI, unsigned DefIdx, 156 const MachineInstr &UseMI, 157 unsigned UseIdx) const { 158 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, 159 UseMI, UseIdx); 160 161 if (!DefMI.getParent()) 162 return Latency; 163 164 const MachineOperand &DefMO = DefMI.getOperand(DefIdx); 165 Register Reg = DefMO.getReg(); 166 167 bool IsRegCR; 168 if (Register::isVirtualRegister(Reg)) { 169 const MachineRegisterInfo *MRI = 170 &DefMI.getParent()->getParent()->getRegInfo(); 171 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || 172 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); 173 } else { 174 IsRegCR = PPC::CRRCRegClass.contains(Reg) || 175 PPC::CRBITRCRegClass.contains(Reg); 176 } 177 178 if (UseMI.isBranch() && IsRegCR) { 179 if (Latency < 0) 180 Latency = getInstrLatency(ItinData, DefMI); 181 182 // On some cores, there is an additional delay between writing to a condition 183 // register, and using it from a branch. 184 unsigned Directive = Subtarget.getCPUDirective(); 185 switch (Directive) { 186 default: break; 187 case PPC::DIR_7400: 188 case PPC::DIR_750: 189 case PPC::DIR_970: 190 case PPC::DIR_E5500: 191 case PPC::DIR_PWR4: 192 case PPC::DIR_PWR5: 193 case PPC::DIR_PWR5X: 194 case PPC::DIR_PWR6: 195 case PPC::DIR_PWR6X: 196 case PPC::DIR_PWR7: 197 case PPC::DIR_PWR8: 198 // FIXME: Is this needed for POWER9? 199 Latency += 2; 200 break; 201 } 202 } 203 204 return Latency; 205 } 206 207 /// This is an architecture-specific helper function of reassociateOps. 208 /// Set special operand attributes for new instructions after reassociation. 209 void PPCInstrInfo::setSpecialOperandAttr(MachineInstr &OldMI1, 210 MachineInstr &OldMI2, 211 MachineInstr &NewMI1, 212 MachineInstr &NewMI2) const { 213 // Propagate FP flags from the original instructions. 214 // But clear poison-generating flags because those may not be valid now. 215 uint16_t IntersectedFlags = OldMI1.getFlags() & OldMI2.getFlags(); 216 NewMI1.setFlags(IntersectedFlags); 217 NewMI1.clearFlag(MachineInstr::MIFlag::NoSWrap); 218 NewMI1.clearFlag(MachineInstr::MIFlag::NoUWrap); 219 NewMI1.clearFlag(MachineInstr::MIFlag::IsExact); 220 221 NewMI2.setFlags(IntersectedFlags); 222 NewMI2.clearFlag(MachineInstr::MIFlag::NoSWrap); 223 NewMI2.clearFlag(MachineInstr::MIFlag::NoUWrap); 224 NewMI2.clearFlag(MachineInstr::MIFlag::IsExact); 225 } 226 227 void PPCInstrInfo::setSpecialOperandAttr(MachineInstr &MI, 228 uint16_t Flags) const { 229 MI.setFlags(Flags); 230 MI.clearFlag(MachineInstr::MIFlag::NoSWrap); 231 MI.clearFlag(MachineInstr::MIFlag::NoUWrap); 232 MI.clearFlag(MachineInstr::MIFlag::IsExact); 233 } 234 235 // This function does not list all associative and commutative operations, but 236 // only those worth feeding through the machine combiner in an attempt to 237 // reduce the critical path. Mostly, this means floating-point operations, 238 // because they have high latencies(>=5) (compared to other operations, such as 239 // and/or, which are also associative and commutative, but have low latencies). 240 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const { 241 switch (Inst.getOpcode()) { 242 // Floating point: 243 // FP Add: 244 case PPC::FADD: 245 case PPC::FADDS: 246 // FP Multiply: 247 case PPC::FMUL: 248 case PPC::FMULS: 249 // Altivec Add: 250 case PPC::VADDFP: 251 // VSX Add: 252 case PPC::XSADDDP: 253 case PPC::XVADDDP: 254 case PPC::XVADDSP: 255 case PPC::XSADDSP: 256 // VSX Multiply: 257 case PPC::XSMULDP: 258 case PPC::XVMULDP: 259 case PPC::XVMULSP: 260 case PPC::XSMULSP: 261 // QPX Add: 262 case PPC::QVFADD: 263 case PPC::QVFADDS: 264 case PPC::QVFADDSs: 265 // QPX Multiply: 266 case PPC::QVFMUL: 267 case PPC::QVFMULS: 268 case PPC::QVFMULSs: 269 return Inst.getFlag(MachineInstr::MIFlag::FmReassoc) && 270 Inst.getFlag(MachineInstr::MIFlag::FmNsz); 271 // Fixed point: 272 // Multiply: 273 case PPC::MULHD: 274 case PPC::MULLD: 275 case PPC::MULHW: 276 case PPC::MULLW: 277 return true; 278 default: 279 return false; 280 } 281 } 282 283 #define InfoArrayIdxFMAInst 0 284 #define InfoArrayIdxFAddInst 1 285 #define InfoArrayIdxFMULInst 2 286 #define InfoArrayIdxAddOpIdx 3 287 #define InfoArrayIdxMULOpIdx 4 288 // Array keeps info for FMA instructions: 289 // Index 0(InfoArrayIdxFMAInst): FMA instruction; 290 // Index 1(InfoArrayIdxFAddInst): ADD instruction assoaicted with FMA; 291 // Index 2(InfoArrayIdxFMULInst): MUL instruction assoaicted with FMA; 292 // Index 3(InfoArrayIdxAddOpIdx): ADD operand index in the FMA operand list; 293 // Index 4(InfoArrayIdxMULOpIdx): first MUL operand index in the FMA operand 294 // list; 295 // second MUL operand index is plus 1. 296 static const uint16_t FMAOpIdxInfo[][5] = { 297 // FIXME: add more FMA instructions like XSNMADDADP and so on. 298 {PPC::XSMADDADP, PPC::XSADDDP, PPC::XSMULDP, 1, 2}, 299 {PPC::XSMADDASP, PPC::XSADDSP, PPC::XSMULSP, 1, 2}, 300 {PPC::XVMADDADP, PPC::XVADDDP, PPC::XVMULDP, 1, 2}, 301 {PPC::XVMADDASP, PPC::XVADDSP, PPC::XVMULSP, 1, 2}, 302 {PPC::FMADD, PPC::FADD, PPC::FMUL, 3, 1}, 303 {PPC::FMADDS, PPC::FADDS, PPC::FMULS, 3, 1}, 304 {PPC::QVFMADDSs, PPC::QVFADDSs, PPC::QVFMULSs, 3, 1}, 305 {PPC::QVFMADD, PPC::QVFADD, PPC::QVFMUL, 3, 1}}; 306 307 // Check if an opcode is a FMA instruction. If it is, return the index in array 308 // FMAOpIdxInfo. Otherwise, return -1. 309 int16_t PPCInstrInfo::getFMAOpIdxInfo(unsigned Opcode) const { 310 for (unsigned I = 0; I < array_lengthof(FMAOpIdxInfo); I++) 311 if (FMAOpIdxInfo[I][InfoArrayIdxFMAInst] == Opcode) 312 return I; 313 return -1; 314 } 315 316 // Try to reassociate FMA chains like below: 317 // 318 // Pattern 1: 319 // A = FADD X, Y (Leaf) 320 // B = FMA A, M21, M22 (Prev) 321 // C = FMA B, M31, M32 (Root) 322 // --> 323 // A = FMA X, M21, M22 324 // B = FMA Y, M31, M32 325 // C = FADD A, B 326 // 327 // Pattern 2: 328 // A = FMA X, M11, M12 (Leaf) 329 // B = FMA A, M21, M22 (Prev) 330 // C = FMA B, M31, M32 (Root) 331 // --> 332 // A = FMUL M11, M12 333 // B = FMA X, M21, M22 334 // D = FMA A, M31, M32 335 // C = FADD B, D 336 // 337 // breaking the dependency between A and B, allowing FMA to be executed in 338 // parallel (or back-to-back in a pipeline) instead of depending on each other. 339 bool PPCInstrInfo::getFMAPatterns( 340 MachineInstr &Root, 341 SmallVectorImpl<MachineCombinerPattern> &Patterns) const { 342 MachineBasicBlock *MBB = Root.getParent(); 343 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 344 345 auto IsAllOpsVirtualReg = [](const MachineInstr &Instr) { 346 for (const auto &MO : Instr.explicit_operands()) 347 if (!(MO.isReg() && Register::isVirtualRegister(MO.getReg()))) 348 return false; 349 return true; 350 }; 351 352 auto IsReassociable = [&](const MachineInstr &Instr, int16_t &AddOpIdx, 353 bool IsLeaf, bool IsAdd) { 354 int16_t Idx = -1; 355 if (!IsAdd) { 356 Idx = getFMAOpIdxInfo(Instr.getOpcode()); 357 if (Idx < 0) 358 return false; 359 } else if (Instr.getOpcode() != 360 FMAOpIdxInfo[getFMAOpIdxInfo(Root.getOpcode())] 361 [InfoArrayIdxFAddInst]) 362 return false; 363 364 // Instruction can be reassociated. 365 // fast match flags may prohibit reassociation. 366 if (!(Instr.getFlag(MachineInstr::MIFlag::FmReassoc) && 367 Instr.getFlag(MachineInstr::MIFlag::FmNsz))) 368 return false; 369 370 // Instruction operands are virtual registers for reassociating. 371 if (!IsAllOpsVirtualReg(Instr)) 372 return false; 373 374 if (IsAdd && IsLeaf) 375 return true; 376 377 AddOpIdx = FMAOpIdxInfo[Idx][InfoArrayIdxAddOpIdx]; 378 379 const MachineOperand &OpAdd = Instr.getOperand(AddOpIdx); 380 MachineInstr *MIAdd = MRI.getUniqueVRegDef(OpAdd.getReg()); 381 // If 'add' operand's def is not in current block, don't do ILP related opt. 382 if (!MIAdd || MIAdd->getParent() != MBB) 383 return false; 384 385 // If this is not Leaf FMA Instr, its 'add' operand should only have one use 386 // as this fma will be changed later. 387 return IsLeaf ? true : MRI.hasOneNonDBGUse(OpAdd.getReg()); 388 }; 389 390 int16_t AddOpIdx = -1; 391 // Root must be a valid FMA like instruction. 392 if (!IsReassociable(Root, AddOpIdx, false, false)) 393 return false; 394 395 assert((AddOpIdx >= 0) && "add operand index not right!"); 396 397 Register RegB = Root.getOperand(AddOpIdx).getReg(); 398 MachineInstr *Prev = MRI.getUniqueVRegDef(RegB); 399 400 // Prev must be a valid FMA like instruction. 401 AddOpIdx = -1; 402 if (!IsReassociable(*Prev, AddOpIdx, false, false)) 403 return false; 404 405 assert((AddOpIdx >= 0) && "add operand index not right!"); 406 407 Register RegA = Prev->getOperand(AddOpIdx).getReg(); 408 MachineInstr *Leaf = MRI.getUniqueVRegDef(RegA); 409 AddOpIdx = -1; 410 if (IsReassociable(*Leaf, AddOpIdx, true, false)) { 411 Patterns.push_back(MachineCombinerPattern::REASSOC_XMM_AMM_BMM); 412 return true; 413 } 414 if (IsReassociable(*Leaf, AddOpIdx, true, true)) { 415 Patterns.push_back(MachineCombinerPattern::REASSOC_XY_AMM_BMM); 416 return true; 417 } 418 return false; 419 } 420 421 bool PPCInstrInfo::getMachineCombinerPatterns( 422 MachineInstr &Root, 423 SmallVectorImpl<MachineCombinerPattern> &Patterns) const { 424 // Using the machine combiner in this way is potentially expensive, so 425 // restrict to when aggressive optimizations are desired. 426 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive) 427 return false; 428 429 if (getFMAPatterns(Root, Patterns)) 430 return true; 431 432 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns); 433 } 434 435 void PPCInstrInfo::genAlternativeCodeSequence( 436 MachineInstr &Root, MachineCombinerPattern Pattern, 437 SmallVectorImpl<MachineInstr *> &InsInstrs, 438 SmallVectorImpl<MachineInstr *> &DelInstrs, 439 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const { 440 switch (Pattern) { 441 case MachineCombinerPattern::REASSOC_XY_AMM_BMM: 442 case MachineCombinerPattern::REASSOC_XMM_AMM_BMM: 443 reassociateFMA(Root, Pattern, InsInstrs, DelInstrs, InstrIdxForVirtReg); 444 break; 445 default: 446 // Reassociate default patterns. 447 TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs, 448 DelInstrs, InstrIdxForVirtReg); 449 break; 450 } 451 } 452 453 // Currently, only handle two patterns REASSOC_XY_AMM_BMM and 454 // REASSOC_XMM_AMM_BMM. See comments for getFMAPatterns. 455 void PPCInstrInfo::reassociateFMA( 456 MachineInstr &Root, MachineCombinerPattern Pattern, 457 SmallVectorImpl<MachineInstr *> &InsInstrs, 458 SmallVectorImpl<MachineInstr *> &DelInstrs, 459 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const { 460 MachineFunction *MF = Root.getMF(); 461 MachineRegisterInfo &MRI = MF->getRegInfo(); 462 MachineOperand &OpC = Root.getOperand(0); 463 Register RegC = OpC.getReg(); 464 const TargetRegisterClass *RC = MRI.getRegClass(RegC); 465 MRI.constrainRegClass(RegC, RC); 466 467 unsigned FmaOp = Root.getOpcode(); 468 int16_t Idx = getFMAOpIdxInfo(FmaOp); 469 assert(Idx >= 0 && "Root must be a FMA instruction"); 470 471 uint16_t AddOpIdx = FMAOpIdxInfo[Idx][InfoArrayIdxAddOpIdx]; 472 uint16_t FirstMulOpIdx = FMAOpIdxInfo[Idx][InfoArrayIdxMULOpIdx]; 473 MachineInstr *Prev = MRI.getUniqueVRegDef(Root.getOperand(AddOpIdx).getReg()); 474 MachineInstr *Leaf = 475 MRI.getUniqueVRegDef(Prev->getOperand(AddOpIdx).getReg()); 476 uint16_t IntersectedFlags = 477 Root.getFlags() & Prev->getFlags() & Leaf->getFlags(); 478 479 auto GetOperandInfo = [&](const MachineOperand &Operand, Register &Reg, 480 bool &KillFlag) { 481 Reg = Operand.getReg(); 482 MRI.constrainRegClass(Reg, RC); 483 KillFlag = Operand.isKill(); 484 }; 485 486 auto GetFMAInstrInfo = [&](const MachineInstr &Instr, Register &MulOp1, 487 Register &MulOp2, bool &MulOp1KillFlag, 488 bool &MulOp2KillFlag) { 489 GetOperandInfo(Instr.getOperand(FirstMulOpIdx), MulOp1, MulOp1KillFlag); 490 GetOperandInfo(Instr.getOperand(FirstMulOpIdx + 1), MulOp2, MulOp2KillFlag); 491 }; 492 493 Register RegM11, RegM12, RegX, RegY, RegM21, RegM22, RegM31, RegM32; 494 bool KillX = false, KillY = false, KillM11 = false, KillM12 = false, 495 KillM21 = false, KillM22 = false, KillM31 = false, KillM32 = false; 496 497 GetFMAInstrInfo(Root, RegM31, RegM32, KillM31, KillM32); 498 GetFMAInstrInfo(*Prev, RegM21, RegM22, KillM21, KillM22); 499 500 if (Pattern == MachineCombinerPattern::REASSOC_XMM_AMM_BMM) { 501 GetFMAInstrInfo(*Leaf, RegM11, RegM12, KillM11, KillM12); 502 GetOperandInfo(Leaf->getOperand(AddOpIdx), RegX, KillX); 503 } else if (Pattern == MachineCombinerPattern::REASSOC_XY_AMM_BMM) { 504 GetOperandInfo(Leaf->getOperand(1), RegX, KillX); 505 GetOperandInfo(Leaf->getOperand(2), RegY, KillY); 506 } 507 508 // Create new virtual registers for the new results instead of 509 // recycling legacy ones because the MachineCombiner's computation of the 510 // critical path requires a new register definition rather than an existing 511 // one. 512 Register NewVRA = MRI.createVirtualRegister(RC); 513 InstrIdxForVirtReg.insert(std::make_pair(NewVRA, 0)); 514 515 Register NewVRB = MRI.createVirtualRegister(RC); 516 InstrIdxForVirtReg.insert(std::make_pair(NewVRB, 1)); 517 518 Register NewVRD = 0; 519 if (Pattern == MachineCombinerPattern::REASSOC_XMM_AMM_BMM) { 520 NewVRD = MRI.createVirtualRegister(RC); 521 InstrIdxForVirtReg.insert(std::make_pair(NewVRD, 2)); 522 } 523 524 auto AdjustOperandOrder = [&](MachineInstr *MI, Register RegAdd, bool KillAdd, 525 Register RegMul1, bool KillRegMul1, 526 Register RegMul2, bool KillRegMul2) { 527 MI->getOperand(AddOpIdx).setReg(RegAdd); 528 MI->getOperand(AddOpIdx).setIsKill(KillAdd); 529 MI->getOperand(FirstMulOpIdx).setReg(RegMul1); 530 MI->getOperand(FirstMulOpIdx).setIsKill(KillRegMul1); 531 MI->getOperand(FirstMulOpIdx + 1).setReg(RegMul2); 532 MI->getOperand(FirstMulOpIdx + 1).setIsKill(KillRegMul2); 533 }; 534 535 if (Pattern == MachineCombinerPattern::REASSOC_XY_AMM_BMM) { 536 // Create new instructions for insertion. 537 MachineInstrBuilder MINewB = 538 BuildMI(*MF, Prev->getDebugLoc(), get(FmaOp), NewVRB) 539 .addReg(RegX, getKillRegState(KillX)) 540 .addReg(RegM21, getKillRegState(KillM21)) 541 .addReg(RegM22, getKillRegState(KillM22)); 542 MachineInstrBuilder MINewA = 543 BuildMI(*MF, Root.getDebugLoc(), get(FmaOp), NewVRA) 544 .addReg(RegY, getKillRegState(KillY)) 545 .addReg(RegM31, getKillRegState(KillM31)) 546 .addReg(RegM32, getKillRegState(KillM32)); 547 // if AddOpIdx is not 1, adjust the order. 548 if (AddOpIdx != 1) { 549 AdjustOperandOrder(MINewB, RegX, KillX, RegM21, KillM21, RegM22, KillM22); 550 AdjustOperandOrder(MINewA, RegY, KillY, RegM31, KillM31, RegM32, KillM32); 551 } 552 553 MachineInstrBuilder MINewC = 554 BuildMI(*MF, Root.getDebugLoc(), 555 get(FMAOpIdxInfo[Idx][InfoArrayIdxFAddInst]), RegC) 556 .addReg(NewVRB, getKillRegState(true)) 557 .addReg(NewVRA, getKillRegState(true)); 558 559 // update flags for new created instructions. 560 setSpecialOperandAttr(*MINewA, IntersectedFlags); 561 setSpecialOperandAttr(*MINewB, IntersectedFlags); 562 setSpecialOperandAttr(*MINewC, IntersectedFlags); 563 564 // Record new instructions for insertion. 565 InsInstrs.push_back(MINewA); 566 InsInstrs.push_back(MINewB); 567 InsInstrs.push_back(MINewC); 568 } else if (Pattern == MachineCombinerPattern::REASSOC_XMM_AMM_BMM) { 569 assert(NewVRD && "new FMA register not created!"); 570 // Create new instructions for insertion. 571 MachineInstrBuilder MINewA = 572 BuildMI(*MF, Leaf->getDebugLoc(), 573 get(FMAOpIdxInfo[Idx][InfoArrayIdxFMULInst]), NewVRA) 574 .addReg(RegM11, getKillRegState(KillM11)) 575 .addReg(RegM12, getKillRegState(KillM12)); 576 MachineInstrBuilder MINewB = 577 BuildMI(*MF, Prev->getDebugLoc(), get(FmaOp), NewVRB) 578 .addReg(RegX, getKillRegState(KillX)) 579 .addReg(RegM21, getKillRegState(KillM21)) 580 .addReg(RegM22, getKillRegState(KillM22)); 581 MachineInstrBuilder MINewD = 582 BuildMI(*MF, Root.getDebugLoc(), get(FmaOp), NewVRD) 583 .addReg(NewVRA, getKillRegState(true)) 584 .addReg(RegM31, getKillRegState(KillM31)) 585 .addReg(RegM32, getKillRegState(KillM32)); 586 // If AddOpIdx is not 1, adjust the order. 587 if (AddOpIdx != 1) { 588 AdjustOperandOrder(MINewB, RegX, KillX, RegM21, KillM21, RegM22, KillM22); 589 AdjustOperandOrder(MINewD, NewVRA, true, RegM31, KillM31, RegM32, 590 KillM32); 591 } 592 593 MachineInstrBuilder MINewC = 594 BuildMI(*MF, Root.getDebugLoc(), 595 get(FMAOpIdxInfo[Idx][InfoArrayIdxFAddInst]), RegC) 596 .addReg(NewVRB, getKillRegState(true)) 597 .addReg(NewVRD, getKillRegState(true)); 598 599 // update flags for new created instructions. 600 setSpecialOperandAttr(*MINewA, IntersectedFlags); 601 setSpecialOperandAttr(*MINewB, IntersectedFlags); 602 setSpecialOperandAttr(*MINewD, IntersectedFlags); 603 setSpecialOperandAttr(*MINewC, IntersectedFlags); 604 605 // Record new instructions for insertion. 606 InsInstrs.push_back(MINewA); 607 InsInstrs.push_back(MINewB); 608 InsInstrs.push_back(MINewD); 609 InsInstrs.push_back(MINewC); 610 } 611 612 assert(!InsInstrs.empty() && 613 "Insertion instructions set should not be empty!"); 614 615 // Record old instructions for deletion. 616 DelInstrs.push_back(Leaf); 617 DelInstrs.push_back(Prev); 618 DelInstrs.push_back(&Root); 619 } 620 621 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. 622 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, 623 Register &SrcReg, Register &DstReg, 624 unsigned &SubIdx) const { 625 switch (MI.getOpcode()) { 626 default: return false; 627 case PPC::EXTSW: 628 case PPC::EXTSW_32: 629 case PPC::EXTSW_32_64: 630 SrcReg = MI.getOperand(1).getReg(); 631 DstReg = MI.getOperand(0).getReg(); 632 SubIdx = PPC::sub_32; 633 return true; 634 } 635 } 636 637 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 638 int &FrameIndex) const { 639 unsigned Opcode = MI.getOpcode(); 640 const unsigned *OpcodesForSpill = getLoadOpcodesForSpillArray(); 641 const unsigned *End = OpcodesForSpill + SOK_LastOpcodeSpill; 642 643 if (End != std::find(OpcodesForSpill, End, Opcode)) { 644 // Check for the operands added by addFrameReference (the immediate is the 645 // offset which defaults to 0). 646 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 647 MI.getOperand(2).isFI()) { 648 FrameIndex = MI.getOperand(2).getIndex(); 649 return MI.getOperand(0).getReg(); 650 } 651 } 652 return 0; 653 } 654 655 // For opcodes with the ReMaterializable flag set, this function is called to 656 // verify the instruction is really rematable. 657 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, 658 AliasAnalysis *AA) const { 659 switch (MI.getOpcode()) { 660 default: 661 // This function should only be called for opcodes with the ReMaterializable 662 // flag set. 663 llvm_unreachable("Unknown rematerializable operation!"); 664 break; 665 case PPC::LI: 666 case PPC::LI8: 667 case PPC::LIS: 668 case PPC::LIS8: 669 case PPC::QVGPCI: 670 case PPC::ADDIStocHA: 671 case PPC::ADDIStocHA8: 672 case PPC::ADDItocL: 673 case PPC::LOAD_STACK_GUARD: 674 case PPC::XXLXORz: 675 case PPC::XXLXORspz: 676 case PPC::XXLXORdpz: 677 case PPC::XXLEQVOnes: 678 case PPC::V_SET0B: 679 case PPC::V_SET0H: 680 case PPC::V_SET0: 681 case PPC::V_SETALLONESB: 682 case PPC::V_SETALLONESH: 683 case PPC::V_SETALLONES: 684 case PPC::CRSET: 685 case PPC::CRUNSET: 686 return true; 687 } 688 return false; 689 } 690 691 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 692 int &FrameIndex) const { 693 unsigned Opcode = MI.getOpcode(); 694 const unsigned *OpcodesForSpill = getStoreOpcodesForSpillArray(); 695 const unsigned *End = OpcodesForSpill + SOK_LastOpcodeSpill; 696 697 if (End != std::find(OpcodesForSpill, End, Opcode)) { 698 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 699 MI.getOperand(2).isFI()) { 700 FrameIndex = MI.getOperand(2).getIndex(); 701 return MI.getOperand(0).getReg(); 702 } 703 } 704 return 0; 705 } 706 707 MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 708 unsigned OpIdx1, 709 unsigned OpIdx2) const { 710 MachineFunction &MF = *MI.getParent()->getParent(); 711 712 // Normal instructions can be commuted the obvious way. 713 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMI_rec) 714 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 715 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a 716 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because 717 // changing the relative order of the mask operands might change what happens 718 // to the high-bits of the mask (and, thus, the result). 719 720 // Cannot commute if it has a non-zero rotate count. 721 if (MI.getOperand(3).getImm() != 0) 722 return nullptr; 723 724 // If we have a zero rotate count, we have: 725 // M = mask(MB,ME) 726 // Op0 = (Op1 & ~M) | (Op2 & M) 727 // Change this to: 728 // M = mask((ME+1)&31, (MB-1)&31) 729 // Op0 = (Op2 & ~M) | (Op1 & M) 730 731 // Swap op1/op2 732 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) && 733 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMI_rec."); 734 Register Reg0 = MI.getOperand(0).getReg(); 735 Register Reg1 = MI.getOperand(1).getReg(); 736 Register Reg2 = MI.getOperand(2).getReg(); 737 unsigned SubReg1 = MI.getOperand(1).getSubReg(); 738 unsigned SubReg2 = MI.getOperand(2).getSubReg(); 739 bool Reg1IsKill = MI.getOperand(1).isKill(); 740 bool Reg2IsKill = MI.getOperand(2).isKill(); 741 bool ChangeReg0 = false; 742 // If machine instrs are no longer in two-address forms, update 743 // destination register as well. 744 if (Reg0 == Reg1) { 745 // Must be two address instruction! 746 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) && 747 "Expecting a two-address instruction!"); 748 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); 749 Reg2IsKill = false; 750 ChangeReg0 = true; 751 } 752 753 // Masks. 754 unsigned MB = MI.getOperand(4).getImm(); 755 unsigned ME = MI.getOperand(5).getImm(); 756 757 // We can't commute a trivial mask (there is no way to represent an all-zero 758 // mask). 759 if (MB == 0 && ME == 31) 760 return nullptr; 761 762 if (NewMI) { 763 // Create a new instruction. 764 Register Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg(); 765 bool Reg0IsDead = MI.getOperand(0).isDead(); 766 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc()) 767 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) 768 .addReg(Reg2, getKillRegState(Reg2IsKill)) 769 .addReg(Reg1, getKillRegState(Reg1IsKill)) 770 .addImm((ME + 1) & 31) 771 .addImm((MB - 1) & 31); 772 } 773 774 if (ChangeReg0) { 775 MI.getOperand(0).setReg(Reg2); 776 MI.getOperand(0).setSubReg(SubReg2); 777 } 778 MI.getOperand(2).setReg(Reg1); 779 MI.getOperand(1).setReg(Reg2); 780 MI.getOperand(2).setSubReg(SubReg1); 781 MI.getOperand(1).setSubReg(SubReg2); 782 MI.getOperand(2).setIsKill(Reg1IsKill); 783 MI.getOperand(1).setIsKill(Reg2IsKill); 784 785 // Swap the mask around. 786 MI.getOperand(4).setImm((ME + 1) & 31); 787 MI.getOperand(5).setImm((MB - 1) & 31); 788 return &MI; 789 } 790 791 bool PPCInstrInfo::findCommutedOpIndices(const MachineInstr &MI, 792 unsigned &SrcOpIdx1, 793 unsigned &SrcOpIdx2) const { 794 // For VSX A-Type FMA instructions, it is the first two operands that can be 795 // commuted, however, because the non-encoded tied input operand is listed 796 // first, the operands to swap are actually the second and third. 797 798 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode()); 799 if (AltOpc == -1) 800 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 801 802 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1 803 // and SrcOpIdx2. 804 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3); 805 } 806 807 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, 808 MachineBasicBlock::iterator MI) const { 809 // This function is used for scheduling, and the nop wanted here is the type 810 // that terminates dispatch groups on the POWER cores. 811 unsigned Directive = Subtarget.getCPUDirective(); 812 unsigned Opcode; 813 switch (Directive) { 814 default: Opcode = PPC::NOP; break; 815 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; 816 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; 817 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */ 818 // FIXME: Update when POWER9 scheduling model is ready. 819 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break; 820 } 821 822 DebugLoc DL; 823 BuildMI(MBB, MI, DL, get(Opcode)); 824 } 825 826 /// Return the noop instruction to use for a noop. 827 void PPCInstrInfo::getNoop(MCInst &NopInst) const { 828 NopInst.setOpcode(PPC::NOP); 829 } 830 831 // Branch analysis. 832 // Note: If the condition register is set to CTR or CTR8 then this is a 833 // BDNZ (imm == 1) or BDZ (imm == 0) branch. 834 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 835 MachineBasicBlock *&TBB, 836 MachineBasicBlock *&FBB, 837 SmallVectorImpl<MachineOperand> &Cond, 838 bool AllowModify) const { 839 bool isPPC64 = Subtarget.isPPC64(); 840 841 // If the block has no terminators, it just falls into the block after it. 842 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 843 if (I == MBB.end()) 844 return false; 845 846 if (!isUnpredicatedTerminator(*I)) 847 return false; 848 849 if (AllowModify) { 850 // If the BB ends with an unconditional branch to the fallthrough BB, 851 // we eliminate the branch instruction. 852 if (I->getOpcode() == PPC::B && 853 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { 854 I->eraseFromParent(); 855 856 // We update iterator after deleting the last branch. 857 I = MBB.getLastNonDebugInstr(); 858 if (I == MBB.end() || !isUnpredicatedTerminator(*I)) 859 return false; 860 } 861 } 862 863 // Get the last instruction in the block. 864 MachineInstr &LastInst = *I; 865 866 // If there is only one terminator instruction, process it. 867 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { 868 if (LastInst.getOpcode() == PPC::B) { 869 if (!LastInst.getOperand(0).isMBB()) 870 return true; 871 TBB = LastInst.getOperand(0).getMBB(); 872 return false; 873 } else if (LastInst.getOpcode() == PPC::BCC) { 874 if (!LastInst.getOperand(2).isMBB()) 875 return true; 876 // Block ends with fall-through condbranch. 877 TBB = LastInst.getOperand(2).getMBB(); 878 Cond.push_back(LastInst.getOperand(0)); 879 Cond.push_back(LastInst.getOperand(1)); 880 return false; 881 } else if (LastInst.getOpcode() == PPC::BC) { 882 if (!LastInst.getOperand(1).isMBB()) 883 return true; 884 // Block ends with fall-through condbranch. 885 TBB = LastInst.getOperand(1).getMBB(); 886 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 887 Cond.push_back(LastInst.getOperand(0)); 888 return false; 889 } else if (LastInst.getOpcode() == PPC::BCn) { 890 if (!LastInst.getOperand(1).isMBB()) 891 return true; 892 // Block ends with fall-through condbranch. 893 TBB = LastInst.getOperand(1).getMBB(); 894 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 895 Cond.push_back(LastInst.getOperand(0)); 896 return false; 897 } else if (LastInst.getOpcode() == PPC::BDNZ8 || 898 LastInst.getOpcode() == PPC::BDNZ) { 899 if (!LastInst.getOperand(0).isMBB()) 900 return true; 901 if (DisableCTRLoopAnal) 902 return true; 903 TBB = LastInst.getOperand(0).getMBB(); 904 Cond.push_back(MachineOperand::CreateImm(1)); 905 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 906 true)); 907 return false; 908 } else if (LastInst.getOpcode() == PPC::BDZ8 || 909 LastInst.getOpcode() == PPC::BDZ) { 910 if (!LastInst.getOperand(0).isMBB()) 911 return true; 912 if (DisableCTRLoopAnal) 913 return true; 914 TBB = LastInst.getOperand(0).getMBB(); 915 Cond.push_back(MachineOperand::CreateImm(0)); 916 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 917 true)); 918 return false; 919 } 920 921 // Otherwise, don't know what this is. 922 return true; 923 } 924 925 // Get the instruction before it if it's a terminator. 926 MachineInstr &SecondLastInst = *I; 927 928 // If there are three terminators, we don't know what sort of block this is. 929 if (I != MBB.begin() && isUnpredicatedTerminator(*--I)) 930 return true; 931 932 // If the block ends with PPC::B and PPC:BCC, handle it. 933 if (SecondLastInst.getOpcode() == PPC::BCC && 934 LastInst.getOpcode() == PPC::B) { 935 if (!SecondLastInst.getOperand(2).isMBB() || 936 !LastInst.getOperand(0).isMBB()) 937 return true; 938 TBB = SecondLastInst.getOperand(2).getMBB(); 939 Cond.push_back(SecondLastInst.getOperand(0)); 940 Cond.push_back(SecondLastInst.getOperand(1)); 941 FBB = LastInst.getOperand(0).getMBB(); 942 return false; 943 } else if (SecondLastInst.getOpcode() == PPC::BC && 944 LastInst.getOpcode() == PPC::B) { 945 if (!SecondLastInst.getOperand(1).isMBB() || 946 !LastInst.getOperand(0).isMBB()) 947 return true; 948 TBB = SecondLastInst.getOperand(1).getMBB(); 949 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 950 Cond.push_back(SecondLastInst.getOperand(0)); 951 FBB = LastInst.getOperand(0).getMBB(); 952 return false; 953 } else if (SecondLastInst.getOpcode() == PPC::BCn && 954 LastInst.getOpcode() == PPC::B) { 955 if (!SecondLastInst.getOperand(1).isMBB() || 956 !LastInst.getOperand(0).isMBB()) 957 return true; 958 TBB = SecondLastInst.getOperand(1).getMBB(); 959 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 960 Cond.push_back(SecondLastInst.getOperand(0)); 961 FBB = LastInst.getOperand(0).getMBB(); 962 return false; 963 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 || 964 SecondLastInst.getOpcode() == PPC::BDNZ) && 965 LastInst.getOpcode() == PPC::B) { 966 if (!SecondLastInst.getOperand(0).isMBB() || 967 !LastInst.getOperand(0).isMBB()) 968 return true; 969 if (DisableCTRLoopAnal) 970 return true; 971 TBB = SecondLastInst.getOperand(0).getMBB(); 972 Cond.push_back(MachineOperand::CreateImm(1)); 973 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 974 true)); 975 FBB = LastInst.getOperand(0).getMBB(); 976 return false; 977 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 || 978 SecondLastInst.getOpcode() == PPC::BDZ) && 979 LastInst.getOpcode() == PPC::B) { 980 if (!SecondLastInst.getOperand(0).isMBB() || 981 !LastInst.getOperand(0).isMBB()) 982 return true; 983 if (DisableCTRLoopAnal) 984 return true; 985 TBB = SecondLastInst.getOperand(0).getMBB(); 986 Cond.push_back(MachineOperand::CreateImm(0)); 987 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 988 true)); 989 FBB = LastInst.getOperand(0).getMBB(); 990 return false; 991 } 992 993 // If the block ends with two PPC:Bs, handle it. The second one is not 994 // executed, so remove it. 995 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) { 996 if (!SecondLastInst.getOperand(0).isMBB()) 997 return true; 998 TBB = SecondLastInst.getOperand(0).getMBB(); 999 I = LastInst; 1000 if (AllowModify) 1001 I->eraseFromParent(); 1002 return false; 1003 } 1004 1005 // Otherwise, can't handle this. 1006 return true; 1007 } 1008 1009 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB, 1010 int *BytesRemoved) const { 1011 assert(!BytesRemoved && "code size not handled"); 1012 1013 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 1014 if (I == MBB.end()) 1015 return 0; 1016 1017 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && 1018 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 1019 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 1020 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 1021 return 0; 1022 1023 // Remove the branch. 1024 I->eraseFromParent(); 1025 1026 I = MBB.end(); 1027 1028 if (I == MBB.begin()) return 1; 1029 --I; 1030 if (I->getOpcode() != PPC::BCC && 1031 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 1032 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 1033 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 1034 return 1; 1035 1036 // Remove the branch. 1037 I->eraseFromParent(); 1038 return 2; 1039 } 1040 1041 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB, 1042 MachineBasicBlock *TBB, 1043 MachineBasicBlock *FBB, 1044 ArrayRef<MachineOperand> Cond, 1045 const DebugLoc &DL, 1046 int *BytesAdded) const { 1047 // Shouldn't be a fall through. 1048 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 1049 assert((Cond.size() == 2 || Cond.size() == 0) && 1050 "PPC branch conditions have two components!"); 1051 assert(!BytesAdded && "code size not handled"); 1052 1053 bool isPPC64 = Subtarget.isPPC64(); 1054 1055 // One-way branch. 1056 if (!FBB) { 1057 if (Cond.empty()) // Unconditional branch 1058 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); 1059 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 1060 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 1061 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 1062 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 1063 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 1064 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); 1065 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 1066 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); 1067 else // Conditional branch 1068 BuildMI(&MBB, DL, get(PPC::BCC)) 1069 .addImm(Cond[0].getImm()) 1070 .add(Cond[1]) 1071 .addMBB(TBB); 1072 return 1; 1073 } 1074 1075 // Two-way Conditional Branch. 1076 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 1077 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 1078 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 1079 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 1080 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 1081 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); 1082 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 1083 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); 1084 else 1085 BuildMI(&MBB, DL, get(PPC::BCC)) 1086 .addImm(Cond[0].getImm()) 1087 .add(Cond[1]) 1088 .addMBB(TBB); 1089 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); 1090 return 2; 1091 } 1092 1093 // Select analysis. 1094 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 1095 ArrayRef<MachineOperand> Cond, 1096 Register DstReg, Register TrueReg, 1097 Register FalseReg, int &CondCycles, 1098 int &TrueCycles, int &FalseCycles) const { 1099 if (Cond.size() != 2) 1100 return false; 1101 1102 // If this is really a bdnz-like condition, then it cannot be turned into a 1103 // select. 1104 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 1105 return false; 1106 1107 // Check register classes. 1108 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1109 const TargetRegisterClass *RC = 1110 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 1111 if (!RC) 1112 return false; 1113 1114 // isel is for regular integer GPRs only. 1115 if (!PPC::GPRCRegClass.hasSubClassEq(RC) && 1116 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && 1117 !PPC::G8RCRegClass.hasSubClassEq(RC) && 1118 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) 1119 return false; 1120 1121 // FIXME: These numbers are for the A2, how well they work for other cores is 1122 // an open question. On the A2, the isel instruction has a 2-cycle latency 1123 // but single-cycle throughput. These numbers are used in combination with 1124 // the MispredictPenalty setting from the active SchedMachineModel. 1125 CondCycles = 1; 1126 TrueCycles = 1; 1127 FalseCycles = 1; 1128 1129 return true; 1130 } 1131 1132 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, 1133 MachineBasicBlock::iterator MI, 1134 const DebugLoc &dl, Register DestReg, 1135 ArrayRef<MachineOperand> Cond, Register TrueReg, 1136 Register FalseReg) const { 1137 assert(Cond.size() == 2 && 1138 "PPC branch conditions have two components!"); 1139 1140 // Get the register classes. 1141 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1142 const TargetRegisterClass *RC = 1143 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 1144 assert(RC && "TrueReg and FalseReg must have overlapping register classes"); 1145 1146 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || 1147 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); 1148 assert((Is64Bit || 1149 PPC::GPRCRegClass.hasSubClassEq(RC) || 1150 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && 1151 "isel is for regular integer GPRs only"); 1152 1153 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; 1154 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm()); 1155 1156 unsigned SubIdx = 0; 1157 bool SwapOps = false; 1158 switch (SelectPred) { 1159 case PPC::PRED_EQ: 1160 case PPC::PRED_EQ_MINUS: 1161 case PPC::PRED_EQ_PLUS: 1162 SubIdx = PPC::sub_eq; SwapOps = false; break; 1163 case PPC::PRED_NE: 1164 case PPC::PRED_NE_MINUS: 1165 case PPC::PRED_NE_PLUS: 1166 SubIdx = PPC::sub_eq; SwapOps = true; break; 1167 case PPC::PRED_LT: 1168 case PPC::PRED_LT_MINUS: 1169 case PPC::PRED_LT_PLUS: 1170 SubIdx = PPC::sub_lt; SwapOps = false; break; 1171 case PPC::PRED_GE: 1172 case PPC::PRED_GE_MINUS: 1173 case PPC::PRED_GE_PLUS: 1174 SubIdx = PPC::sub_lt; SwapOps = true; break; 1175 case PPC::PRED_GT: 1176 case PPC::PRED_GT_MINUS: 1177 case PPC::PRED_GT_PLUS: 1178 SubIdx = PPC::sub_gt; SwapOps = false; break; 1179 case PPC::PRED_LE: 1180 case PPC::PRED_LE_MINUS: 1181 case PPC::PRED_LE_PLUS: 1182 SubIdx = PPC::sub_gt; SwapOps = true; break; 1183 case PPC::PRED_UN: 1184 case PPC::PRED_UN_MINUS: 1185 case PPC::PRED_UN_PLUS: 1186 SubIdx = PPC::sub_un; SwapOps = false; break; 1187 case PPC::PRED_NU: 1188 case PPC::PRED_NU_MINUS: 1189 case PPC::PRED_NU_PLUS: 1190 SubIdx = PPC::sub_un; SwapOps = true; break; 1191 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; 1192 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; 1193 } 1194 1195 Register FirstReg = SwapOps ? FalseReg : TrueReg, 1196 SecondReg = SwapOps ? TrueReg : FalseReg; 1197 1198 // The first input register of isel cannot be r0. If it is a member 1199 // of a register class that can be r0, then copy it first (the 1200 // register allocator should eliminate the copy). 1201 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || 1202 MRI.getRegClass(FirstReg)->contains(PPC::X0)) { 1203 const TargetRegisterClass *FirstRC = 1204 MRI.getRegClass(FirstReg)->contains(PPC::X0) ? 1205 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; 1206 Register OldFirstReg = FirstReg; 1207 FirstReg = MRI.createVirtualRegister(FirstRC); 1208 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) 1209 .addReg(OldFirstReg); 1210 } 1211 1212 BuildMI(MBB, MI, dl, get(OpCode), DestReg) 1213 .addReg(FirstReg).addReg(SecondReg) 1214 .addReg(Cond[1].getReg(), 0, SubIdx); 1215 } 1216 1217 static unsigned getCRBitValue(unsigned CRBit) { 1218 unsigned Ret = 4; 1219 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || 1220 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || 1221 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || 1222 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) 1223 Ret = 3; 1224 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || 1225 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || 1226 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || 1227 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) 1228 Ret = 2; 1229 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || 1230 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || 1231 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || 1232 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) 1233 Ret = 1; 1234 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || 1235 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || 1236 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || 1237 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) 1238 Ret = 0; 1239 1240 assert(Ret != 4 && "Invalid CR bit register"); 1241 return Ret; 1242 } 1243 1244 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 1245 MachineBasicBlock::iterator I, 1246 const DebugLoc &DL, MCRegister DestReg, 1247 MCRegister SrcReg, bool KillSrc) const { 1248 // We can end up with self copies and similar things as a result of VSX copy 1249 // legalization. Promote them here. 1250 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1251 if (PPC::F8RCRegClass.contains(DestReg) && 1252 PPC::VSRCRegClass.contains(SrcReg)) { 1253 MCRegister SuperReg = 1254 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); 1255 1256 if (VSXSelfCopyCrash && SrcReg == SuperReg) 1257 llvm_unreachable("nop VSX copy"); 1258 1259 DestReg = SuperReg; 1260 } else if (PPC::F8RCRegClass.contains(SrcReg) && 1261 PPC::VSRCRegClass.contains(DestReg)) { 1262 MCRegister SuperReg = 1263 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); 1264 1265 if (VSXSelfCopyCrash && DestReg == SuperReg) 1266 llvm_unreachable("nop VSX copy"); 1267 1268 SrcReg = SuperReg; 1269 } 1270 1271 // Different class register copy 1272 if (PPC::CRBITRCRegClass.contains(SrcReg) && 1273 PPC::GPRCRegClass.contains(DestReg)) { 1274 MCRegister CRReg = getCRFromCRBit(SrcReg); 1275 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg); 1276 getKillRegState(KillSrc); 1277 // Rotate the CR bit in the CR fields to be the least significant bit and 1278 // then mask with 0x1 (MB = ME = 31). 1279 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) 1280 .addReg(DestReg, RegState::Kill) 1281 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) 1282 .addImm(31) 1283 .addImm(31); 1284 return; 1285 } else if (PPC::CRRCRegClass.contains(SrcReg) && 1286 PPC::G8RCRegClass.contains(DestReg)) { 1287 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg); 1288 getKillRegState(KillSrc); 1289 return; 1290 } else if (PPC::CRRCRegClass.contains(SrcReg) && 1291 PPC::GPRCRegClass.contains(DestReg)) { 1292 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); 1293 getKillRegState(KillSrc); 1294 return; 1295 } else if (PPC::G8RCRegClass.contains(SrcReg) && 1296 PPC::VSFRCRegClass.contains(DestReg)) { 1297 assert(Subtarget.hasDirectMove() && 1298 "Subtarget doesn't support directmove, don't know how to copy."); 1299 BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg); 1300 NumGPRtoVSRSpill++; 1301 getKillRegState(KillSrc); 1302 return; 1303 } else if (PPC::VSFRCRegClass.contains(SrcReg) && 1304 PPC::G8RCRegClass.contains(DestReg)) { 1305 assert(Subtarget.hasDirectMove() && 1306 "Subtarget doesn't support directmove, don't know how to copy."); 1307 BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg); 1308 getKillRegState(KillSrc); 1309 return; 1310 } else if (PPC::SPERCRegClass.contains(SrcReg) && 1311 PPC::GPRCRegClass.contains(DestReg)) { 1312 BuildMI(MBB, I, DL, get(PPC::EFSCFD), DestReg).addReg(SrcReg); 1313 getKillRegState(KillSrc); 1314 return; 1315 } else if (PPC::GPRCRegClass.contains(SrcReg) && 1316 PPC::SPERCRegClass.contains(DestReg)) { 1317 BuildMI(MBB, I, DL, get(PPC::EFDCFS), DestReg).addReg(SrcReg); 1318 getKillRegState(KillSrc); 1319 return; 1320 } 1321 1322 unsigned Opc; 1323 if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) 1324 Opc = PPC::OR; 1325 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) 1326 Opc = PPC::OR8; 1327 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) 1328 Opc = PPC::FMR; 1329 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) 1330 Opc = PPC::MCRF; 1331 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) 1332 Opc = PPC::VOR; 1333 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) 1334 // There are two different ways this can be done: 1335 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only 1336 // issue in VSU pipeline 0. 1337 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but 1338 // can go to either pipeline. 1339 // We'll always use xxlor here, because in practically all cases where 1340 // copies are generated, they are close enough to some use that the 1341 // lower-latency form is preferable. 1342 Opc = PPC::XXLOR; 1343 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || 1344 PPC::VSSRCRegClass.contains(DestReg, SrcReg)) 1345 Opc = (Subtarget.hasP9Vector()) ? PPC::XSCPSGNDP : PPC::XXLORf; 1346 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) 1347 Opc = PPC::QVFMR; 1348 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) 1349 Opc = PPC::QVFMRs; 1350 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) 1351 Opc = PPC::QVFMRb; 1352 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) 1353 Opc = PPC::CROR; 1354 else if (PPC::SPERCRegClass.contains(DestReg, SrcReg)) 1355 Opc = PPC::EVOR; 1356 else 1357 llvm_unreachable("Impossible reg-to-reg copy"); 1358 1359 const MCInstrDesc &MCID = get(Opc); 1360 if (MCID.getNumOperands() == 3) 1361 BuildMI(MBB, I, DL, MCID, DestReg) 1362 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); 1363 else 1364 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); 1365 } 1366 1367 static unsigned getSpillIndex(const TargetRegisterClass *RC) { 1368 int OpcodeIndex = 0; 1369 1370 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 1371 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 1372 OpcodeIndex = SOK_Int4Spill; 1373 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 1374 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 1375 OpcodeIndex = SOK_Int8Spill; 1376 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 1377 OpcodeIndex = SOK_Float8Spill; 1378 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 1379 OpcodeIndex = SOK_Float4Spill; 1380 } else if (PPC::SPERCRegClass.hasSubClassEq(RC)) { 1381 OpcodeIndex = SOK_SPESpill; 1382 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 1383 OpcodeIndex = SOK_CRSpill; 1384 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 1385 OpcodeIndex = SOK_CRBitSpill; 1386 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1387 OpcodeIndex = SOK_VRVectorSpill; 1388 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1389 OpcodeIndex = SOK_VSXVectorSpill; 1390 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1391 OpcodeIndex = SOK_VectorFloat8Spill; 1392 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1393 OpcodeIndex = SOK_VectorFloat4Spill; 1394 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1395 OpcodeIndex = SOK_VRSaveSpill; 1396 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1397 OpcodeIndex = SOK_QuadFloat8Spill; 1398 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1399 OpcodeIndex = SOK_QuadFloat4Spill; 1400 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1401 OpcodeIndex = SOK_QuadBitSpill; 1402 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { 1403 OpcodeIndex = SOK_SpillToVSR; 1404 } else { 1405 llvm_unreachable("Unknown regclass!"); 1406 } 1407 return OpcodeIndex; 1408 } 1409 1410 unsigned 1411 PPCInstrInfo::getStoreOpcodeForSpill(const TargetRegisterClass *RC) const { 1412 const unsigned *OpcodesForSpill = getStoreOpcodesForSpillArray(); 1413 return OpcodesForSpill[getSpillIndex(RC)]; 1414 } 1415 1416 unsigned 1417 PPCInstrInfo::getLoadOpcodeForSpill(const TargetRegisterClass *RC) const { 1418 const unsigned *OpcodesForSpill = getLoadOpcodesForSpillArray(); 1419 return OpcodesForSpill[getSpillIndex(RC)]; 1420 } 1421 1422 void PPCInstrInfo::StoreRegToStackSlot( 1423 MachineFunction &MF, unsigned SrcReg, bool isKill, int FrameIdx, 1424 const TargetRegisterClass *RC, 1425 SmallVectorImpl<MachineInstr *> &NewMIs) const { 1426 unsigned Opcode = getStoreOpcodeForSpill(RC); 1427 DebugLoc DL; 1428 1429 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1430 FuncInfo->setHasSpills(); 1431 1432 NewMIs.push_back(addFrameReference( 1433 BuildMI(MF, DL, get(Opcode)).addReg(SrcReg, getKillRegState(isKill)), 1434 FrameIdx)); 1435 1436 if (PPC::CRRCRegClass.hasSubClassEq(RC) || 1437 PPC::CRBITRCRegClass.hasSubClassEq(RC)) 1438 FuncInfo->setSpillsCR(); 1439 1440 if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) 1441 FuncInfo->setSpillsVRSAVE(); 1442 1443 if (isXFormMemOp(Opcode)) 1444 FuncInfo->setHasNonRISpills(); 1445 } 1446 1447 void PPCInstrInfo::storeRegToStackSlotNoUpd( 1448 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned SrcReg, 1449 bool isKill, int FrameIdx, const TargetRegisterClass *RC, 1450 const TargetRegisterInfo *TRI) const { 1451 MachineFunction &MF = *MBB.getParent(); 1452 SmallVector<MachineInstr *, 4> NewMIs; 1453 1454 StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs); 1455 1456 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1457 MBB.insert(MI, NewMIs[i]); 1458 1459 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1460 MachineMemOperand *MMO = MF.getMachineMemOperand( 1461 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1462 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx), 1463 MFI.getObjectAlign(FrameIdx)); 1464 NewMIs.back()->addMemOperand(MF, MMO); 1465 } 1466 1467 void PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 1468 MachineBasicBlock::iterator MI, 1469 Register SrcReg, bool isKill, 1470 int FrameIdx, 1471 const TargetRegisterClass *RC, 1472 const TargetRegisterInfo *TRI) const { 1473 // We need to avoid a situation in which the value from a VRRC register is 1474 // spilled using an Altivec instruction and reloaded into a VSRC register 1475 // using a VSX instruction. The issue with this is that the VSX 1476 // load/store instructions swap the doublewords in the vector and the Altivec 1477 // ones don't. The register classes on the spill/reload may be different if 1478 // the register is defined using an Altivec instruction and is then used by a 1479 // VSX instruction. 1480 RC = updatedRC(RC); 1481 storeRegToStackSlotNoUpd(MBB, MI, SrcReg, isKill, FrameIdx, RC, TRI); 1482 } 1483 1484 void PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL, 1485 unsigned DestReg, int FrameIdx, 1486 const TargetRegisterClass *RC, 1487 SmallVectorImpl<MachineInstr *> &NewMIs) 1488 const { 1489 unsigned Opcode = getLoadOpcodeForSpill(RC); 1490 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opcode), DestReg), 1491 FrameIdx)); 1492 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1493 1494 if (PPC::CRRCRegClass.hasSubClassEq(RC) || 1495 PPC::CRBITRCRegClass.hasSubClassEq(RC)) 1496 FuncInfo->setSpillsCR(); 1497 1498 if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) 1499 FuncInfo->setSpillsVRSAVE(); 1500 1501 if (isXFormMemOp(Opcode)) 1502 FuncInfo->setHasNonRISpills(); 1503 } 1504 1505 void PPCInstrInfo::loadRegFromStackSlotNoUpd( 1506 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg, 1507 int FrameIdx, const TargetRegisterClass *RC, 1508 const TargetRegisterInfo *TRI) const { 1509 MachineFunction &MF = *MBB.getParent(); 1510 SmallVector<MachineInstr*, 4> NewMIs; 1511 DebugLoc DL; 1512 if (MI != MBB.end()) DL = MI->getDebugLoc(); 1513 1514 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1515 FuncInfo->setHasSpills(); 1516 1517 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs); 1518 1519 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1520 MBB.insert(MI, NewMIs[i]); 1521 1522 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1523 MachineMemOperand *MMO = MF.getMachineMemOperand( 1524 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1525 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx), 1526 MFI.getObjectAlign(FrameIdx)); 1527 NewMIs.back()->addMemOperand(MF, MMO); 1528 } 1529 1530 void PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 1531 MachineBasicBlock::iterator MI, 1532 Register DestReg, int FrameIdx, 1533 const TargetRegisterClass *RC, 1534 const TargetRegisterInfo *TRI) const { 1535 // We need to avoid a situation in which the value from a VRRC register is 1536 // spilled using an Altivec instruction and reloaded into a VSRC register 1537 // using a VSX instruction. The issue with this is that the VSX 1538 // load/store instructions swap the doublewords in the vector and the Altivec 1539 // ones don't. The register classes on the spill/reload may be different if 1540 // the register is defined using an Altivec instruction and is then used by a 1541 // VSX instruction. 1542 RC = updatedRC(RC); 1543 1544 loadRegFromStackSlotNoUpd(MBB, MI, DestReg, FrameIdx, RC, TRI); 1545 } 1546 1547 bool PPCInstrInfo:: 1548 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 1549 assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); 1550 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) 1551 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); 1552 else 1553 // Leave the CR# the same, but invert the condition. 1554 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); 1555 return false; 1556 } 1557 1558 // For some instructions, it is legal to fold ZERO into the RA register field. 1559 // This function performs that fold by replacing the operand with PPC::ZERO, 1560 // it does not consider whether the load immediate zero is no longer in use. 1561 bool PPCInstrInfo::onlyFoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 1562 Register Reg) const { 1563 // A zero immediate should always be loaded with a single li. 1564 unsigned DefOpc = DefMI.getOpcode(); 1565 if (DefOpc != PPC::LI && DefOpc != PPC::LI8) 1566 return false; 1567 if (!DefMI.getOperand(1).isImm()) 1568 return false; 1569 if (DefMI.getOperand(1).getImm() != 0) 1570 return false; 1571 1572 // Note that we cannot here invert the arguments of an isel in order to fold 1573 // a ZERO into what is presented as the second argument. All we have here 1574 // is the condition bit, and that might come from a CR-logical bit operation. 1575 1576 const MCInstrDesc &UseMCID = UseMI.getDesc(); 1577 1578 // Only fold into real machine instructions. 1579 if (UseMCID.isPseudo()) 1580 return false; 1581 1582 // We need to find which of the User's operands is to be folded, that will be 1583 // the operand that matches the given register ID. 1584 unsigned UseIdx; 1585 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx) 1586 if (UseMI.getOperand(UseIdx).isReg() && 1587 UseMI.getOperand(UseIdx).getReg() == Reg) 1588 break; 1589 1590 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI"); 1591 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); 1592 1593 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; 1594 1595 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 1596 // register (which might also be specified as a pointer class kind). 1597 if (UseInfo->isLookupPtrRegClass()) { 1598 if (UseInfo->RegClass /* Kind */ != 1) 1599 return false; 1600 } else { 1601 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && 1602 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) 1603 return false; 1604 } 1605 1606 // Make sure this is not tied to an output register (or otherwise 1607 // constrained). This is true for ST?UX registers, for example, which 1608 // are tied to their output registers. 1609 if (UseInfo->Constraints != 0) 1610 return false; 1611 1612 MCRegister ZeroReg; 1613 if (UseInfo->isLookupPtrRegClass()) { 1614 bool isPPC64 = Subtarget.isPPC64(); 1615 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; 1616 } else { 1617 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? 1618 PPC::ZERO8 : PPC::ZERO; 1619 } 1620 1621 UseMI.getOperand(UseIdx).setReg(ZeroReg); 1622 return true; 1623 } 1624 1625 // Folds zero into instructions which have a load immediate zero as an operand 1626 // but also recognize zero as immediate zero. If the definition of the load 1627 // has no more users it is deleted. 1628 bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 1629 Register Reg, MachineRegisterInfo *MRI) const { 1630 bool Changed = onlyFoldImmediate(UseMI, DefMI, Reg); 1631 if (MRI->use_nodbg_empty(Reg)) 1632 DefMI.eraseFromParent(); 1633 return Changed; 1634 } 1635 1636 static bool MBBDefinesCTR(MachineBasicBlock &MBB) { 1637 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 1638 I != IE; ++I) 1639 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) 1640 return true; 1641 return false; 1642 } 1643 1644 // We should make sure that, if we're going to predicate both sides of a 1645 // condition (a diamond), that both sides don't define the counter register. We 1646 // can predicate counter-decrement-based branches, but while that predicates 1647 // the branching, it does not predicate the counter decrement. If we tried to 1648 // merge the triangle into one predicated block, we'd decrement the counter 1649 // twice. 1650 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, 1651 unsigned NumT, unsigned ExtraT, 1652 MachineBasicBlock &FMBB, 1653 unsigned NumF, unsigned ExtraF, 1654 BranchProbability Probability) const { 1655 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); 1656 } 1657 1658 1659 bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const { 1660 // The predicated branches are identified by their type, not really by the 1661 // explicit presence of a predicate. Furthermore, some of them can be 1662 // predicated more than once. Because if conversion won't try to predicate 1663 // any instruction which already claims to be predicated (by returning true 1664 // here), always return false. In doing so, we let isPredicable() be the 1665 // final word on whether not the instruction can be (further) predicated. 1666 1667 return false; 1668 } 1669 1670 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI, 1671 ArrayRef<MachineOperand> Pred) const { 1672 unsigned OpC = MI.getOpcode(); 1673 if (OpC == PPC::BLR || OpC == PPC::BLR8) { 1674 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1675 bool isPPC64 = Subtarget.isPPC64(); 1676 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) 1677 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); 1678 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1679 MI.setDesc(get(PPC::BCLR)); 1680 MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]); 1681 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1682 MI.setDesc(get(PPC::BCLRn)); 1683 MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]); 1684 } else { 1685 MI.setDesc(get(PPC::BCCLR)); 1686 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1687 .addImm(Pred[0].getImm()) 1688 .add(Pred[1]); 1689 } 1690 1691 return true; 1692 } else if (OpC == PPC::B) { 1693 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1694 bool isPPC64 = Subtarget.isPPC64(); 1695 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) 1696 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); 1697 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1698 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1699 MI.RemoveOperand(0); 1700 1701 MI.setDesc(get(PPC::BC)); 1702 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1703 .add(Pred[1]) 1704 .addMBB(MBB); 1705 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1706 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1707 MI.RemoveOperand(0); 1708 1709 MI.setDesc(get(PPC::BCn)); 1710 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1711 .add(Pred[1]) 1712 .addMBB(MBB); 1713 } else { 1714 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1715 MI.RemoveOperand(0); 1716 1717 MI.setDesc(get(PPC::BCC)); 1718 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1719 .addImm(Pred[0].getImm()) 1720 .add(Pred[1]) 1721 .addMBB(MBB); 1722 } 1723 1724 return true; 1725 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || OpC == PPC::BCTRL || 1726 OpC == PPC::BCTRL8) { 1727 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) 1728 llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); 1729 1730 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; 1731 bool isPPC64 = Subtarget.isPPC64(); 1732 1733 if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1734 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) 1735 : (setLR ? PPC::BCCTRL : PPC::BCCTR))); 1736 MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]); 1737 return true; 1738 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1739 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) 1740 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); 1741 MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]); 1742 return true; 1743 } 1744 1745 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) 1746 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); 1747 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1748 .addImm(Pred[0].getImm()) 1749 .add(Pred[1]); 1750 return true; 1751 } 1752 1753 return false; 1754 } 1755 1756 bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, 1757 ArrayRef<MachineOperand> Pred2) const { 1758 assert(Pred1.size() == 2 && "Invalid PPC first predicate"); 1759 assert(Pred2.size() == 2 && "Invalid PPC second predicate"); 1760 1761 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) 1762 return false; 1763 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) 1764 return false; 1765 1766 // P1 can only subsume P2 if they test the same condition register. 1767 if (Pred1[1].getReg() != Pred2[1].getReg()) 1768 return false; 1769 1770 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); 1771 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); 1772 1773 if (P1 == P2) 1774 return true; 1775 1776 // Does P1 subsume P2, e.g. GE subsumes GT. 1777 if (P1 == PPC::PRED_LE && 1778 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) 1779 return true; 1780 if (P1 == PPC::PRED_GE && 1781 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) 1782 return true; 1783 1784 return false; 1785 } 1786 1787 bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI, 1788 std::vector<MachineOperand> &Pred) const { 1789 // Note: At the present time, the contents of Pred from this function is 1790 // unused by IfConversion. This implementation follows ARM by pushing the 1791 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of 1792 // predicate, instructions defining CTR or CTR8 are also included as 1793 // predicate-defining instructions. 1794 1795 const TargetRegisterClass *RCs[] = 1796 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, 1797 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; 1798 1799 bool Found = false; 1800 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1801 const MachineOperand &MO = MI.getOperand(i); 1802 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { 1803 const TargetRegisterClass *RC = RCs[c]; 1804 if (MO.isReg()) { 1805 if (MO.isDef() && RC->contains(MO.getReg())) { 1806 Pred.push_back(MO); 1807 Found = true; 1808 } 1809 } else if (MO.isRegMask()) { 1810 for (TargetRegisterClass::iterator I = RC->begin(), 1811 IE = RC->end(); I != IE; ++I) 1812 if (MO.clobbersPhysReg(*I)) { 1813 Pred.push_back(MO); 1814 Found = true; 1815 } 1816 } 1817 } 1818 } 1819 1820 return Found; 1821 } 1822 1823 bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 1824 Register &SrcReg2, int &Mask, 1825 int &Value) const { 1826 unsigned Opc = MI.getOpcode(); 1827 1828 switch (Opc) { 1829 default: return false; 1830 case PPC::CMPWI: 1831 case PPC::CMPLWI: 1832 case PPC::CMPDI: 1833 case PPC::CMPLDI: 1834 SrcReg = MI.getOperand(1).getReg(); 1835 SrcReg2 = 0; 1836 Value = MI.getOperand(2).getImm(); 1837 Mask = 0xFFFF; 1838 return true; 1839 case PPC::CMPW: 1840 case PPC::CMPLW: 1841 case PPC::CMPD: 1842 case PPC::CMPLD: 1843 case PPC::FCMPUS: 1844 case PPC::FCMPUD: 1845 SrcReg = MI.getOperand(1).getReg(); 1846 SrcReg2 = MI.getOperand(2).getReg(); 1847 Value = 0; 1848 Mask = 0; 1849 return true; 1850 } 1851 } 1852 1853 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, 1854 Register SrcReg2, int Mask, int Value, 1855 const MachineRegisterInfo *MRI) const { 1856 if (DisableCmpOpt) 1857 return false; 1858 1859 int OpC = CmpInstr.getOpcode(); 1860 Register CRReg = CmpInstr.getOperand(0).getReg(); 1861 1862 // FP record forms set CR1 based on the exception status bits, not a 1863 // comparison with zero. 1864 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) 1865 return false; 1866 1867 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1868 // The record forms set the condition register based on a signed comparison 1869 // with zero (so says the ISA manual). This is not as straightforward as it 1870 // seems, however, because this is always a 64-bit comparison on PPC64, even 1871 // for instructions that are 32-bit in nature (like slw for example). 1872 // So, on PPC32, for unsigned comparisons, we can use the record forms only 1873 // for equality checks (as those don't depend on the sign). On PPC64, 1874 // we are restricted to equality for unsigned 64-bit comparisons and for 1875 // signed 32-bit comparisons the applicability is more restricted. 1876 bool isPPC64 = Subtarget.isPPC64(); 1877 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; 1878 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; 1879 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; 1880 1881 // Look through copies unless that gets us to a physical register. 1882 Register ActualSrc = TRI->lookThruCopyLike(SrcReg, MRI); 1883 if (ActualSrc.isVirtual()) 1884 SrcReg = ActualSrc; 1885 1886 // Get the unique definition of SrcReg. 1887 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 1888 if (!MI) return false; 1889 1890 bool equalityOnly = false; 1891 bool noSub = false; 1892 if (isPPC64) { 1893 if (is32BitSignedCompare) { 1894 // We can perform this optimization only if MI is sign-extending. 1895 if (isSignExtended(*MI)) 1896 noSub = true; 1897 else 1898 return false; 1899 } else if (is32BitUnsignedCompare) { 1900 // We can perform this optimization, equality only, if MI is 1901 // zero-extending. 1902 if (isZeroExtended(*MI)) { 1903 noSub = true; 1904 equalityOnly = true; 1905 } else 1906 return false; 1907 } else 1908 equalityOnly = is64BitUnsignedCompare; 1909 } else 1910 equalityOnly = is32BitUnsignedCompare; 1911 1912 if (equalityOnly) { 1913 // We need to check the uses of the condition register in order to reject 1914 // non-equality comparisons. 1915 for (MachineRegisterInfo::use_instr_iterator 1916 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 1917 I != IE; ++I) { 1918 MachineInstr *UseMI = &*I; 1919 if (UseMI->getOpcode() == PPC::BCC) { 1920 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); 1921 unsigned PredCond = PPC::getPredicateCondition(Pred); 1922 // We ignore hint bits when checking for non-equality comparisons. 1923 if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE) 1924 return false; 1925 } else if (UseMI->getOpcode() == PPC::ISEL || 1926 UseMI->getOpcode() == PPC::ISEL8) { 1927 unsigned SubIdx = UseMI->getOperand(3).getSubReg(); 1928 if (SubIdx != PPC::sub_eq) 1929 return false; 1930 } else 1931 return false; 1932 } 1933 } 1934 1935 MachineBasicBlock::iterator I = CmpInstr; 1936 1937 // Scan forward to find the first use of the compare. 1938 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL; 1939 ++I) { 1940 bool FoundUse = false; 1941 for (MachineRegisterInfo::use_instr_iterator 1942 J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end(); 1943 J != JE; ++J) 1944 if (&*J == &*I) { 1945 FoundUse = true; 1946 break; 1947 } 1948 1949 if (FoundUse) 1950 break; 1951 } 1952 1953 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; 1954 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; 1955 1956 // There are two possible candidates which can be changed to set CR[01]. 1957 // One is MI, the other is a SUB instruction. 1958 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 1959 MachineInstr *Sub = nullptr; 1960 if (SrcReg2 != 0) 1961 // MI is not a candidate for CMPrr. 1962 MI = nullptr; 1963 // FIXME: Conservatively refuse to convert an instruction which isn't in the 1964 // same BB as the comparison. This is to allow the check below to avoid calls 1965 // (and other explicit clobbers); instead we should really check for these 1966 // more explicitly (in at least a few predecessors). 1967 else if (MI->getParent() != CmpInstr.getParent()) 1968 return false; 1969 else if (Value != 0) { 1970 // The record-form instructions set CR bit based on signed comparison 1971 // against 0. We try to convert a compare against 1 or -1 into a compare 1972 // against 0 to exploit record-form instructions. For example, we change 1973 // the condition "greater than -1" into "greater than or equal to 0" 1974 // and "less than 1" into "less than or equal to 0". 1975 1976 // Since we optimize comparison based on a specific branch condition, 1977 // we don't optimize if condition code is used by more than once. 1978 if (equalityOnly || !MRI->hasOneUse(CRReg)) 1979 return false; 1980 1981 MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg); 1982 if (UseMI->getOpcode() != PPC::BCC) 1983 return false; 1984 1985 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); 1986 unsigned PredCond = PPC::getPredicateCondition(Pred); 1987 unsigned PredHint = PPC::getPredicateHint(Pred); 1988 int16_t Immed = (int16_t)Value; 1989 1990 // When modifying the condition in the predicate, we propagate hint bits 1991 // from the original predicate to the new one. 1992 if (Immed == -1 && PredCond == PPC::PRED_GT) 1993 // We convert "greater than -1" into "greater than or equal to 0", 1994 // since we are assuming signed comparison by !equalityOnly 1995 Pred = PPC::getPredicate(PPC::PRED_GE, PredHint); 1996 else if (Immed == -1 && PredCond == PPC::PRED_LE) 1997 // We convert "less than or equal to -1" into "less than 0". 1998 Pred = PPC::getPredicate(PPC::PRED_LT, PredHint); 1999 else if (Immed == 1 && PredCond == PPC::PRED_LT) 2000 // We convert "less than 1" into "less than or equal to 0". 2001 Pred = PPC::getPredicate(PPC::PRED_LE, PredHint); 2002 else if (Immed == 1 && PredCond == PPC::PRED_GE) 2003 // We convert "greater than or equal to 1" into "greater than 0". 2004 Pred = PPC::getPredicate(PPC::PRED_GT, PredHint); 2005 else 2006 return false; 2007 2008 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), Pred)); 2009 } 2010 2011 // Search for Sub. 2012 --I; 2013 2014 // Get ready to iterate backward from CmpInstr. 2015 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin(); 2016 2017 for (; I != E && !noSub; --I) { 2018 const MachineInstr &Instr = *I; 2019 unsigned IOpC = Instr.getOpcode(); 2020 2021 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) || 2022 Instr.readsRegister(PPC::CR0, TRI))) 2023 // This instruction modifies or uses the record condition register after 2024 // the one we want to change. While we could do this transformation, it 2025 // would likely not be profitable. This transformation removes one 2026 // instruction, and so even forcing RA to generate one move probably 2027 // makes it unprofitable. 2028 return false; 2029 2030 // Check whether CmpInstr can be made redundant by the current instruction. 2031 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || 2032 OpC == PPC::CMPD || OpC == PPC::CMPLD) && 2033 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && 2034 ((Instr.getOperand(1).getReg() == SrcReg && 2035 Instr.getOperand(2).getReg() == SrcReg2) || 2036 (Instr.getOperand(1).getReg() == SrcReg2 && 2037 Instr.getOperand(2).getReg() == SrcReg))) { 2038 Sub = &*I; 2039 break; 2040 } 2041 2042 if (I == B) 2043 // The 'and' is below the comparison instruction. 2044 return false; 2045 } 2046 2047 // Return false if no candidates exist. 2048 if (!MI && !Sub) 2049 return false; 2050 2051 // The single candidate is called MI. 2052 if (!MI) MI = Sub; 2053 2054 int NewOpC = -1; 2055 int MIOpC = MI->getOpcode(); 2056 if (MIOpC == PPC::ANDI_rec || MIOpC == PPC::ANDI8_rec || 2057 MIOpC == PPC::ANDIS_rec || MIOpC == PPC::ANDIS8_rec) 2058 NewOpC = MIOpC; 2059 else { 2060 NewOpC = PPC::getRecordFormOpcode(MIOpC); 2061 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) 2062 NewOpC = MIOpC; 2063 } 2064 2065 // FIXME: On the non-embedded POWER architectures, only some of the record 2066 // forms are fast, and we should use only the fast ones. 2067 2068 // The defining instruction has a record form (or is already a record 2069 // form). It is possible, however, that we'll need to reverse the condition 2070 // code of the users. 2071 if (NewOpC == -1) 2072 return false; 2073 2074 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP 2075 // needs to be updated to be based on SUB. Push the condition code 2076 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the 2077 // condition code of these operands will be modified. 2078 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to 2079 // comparison against 0, which may modify predicate. 2080 bool ShouldSwap = false; 2081 if (Sub && Value == 0) { 2082 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 2083 Sub->getOperand(2).getReg() == SrcReg; 2084 2085 // The operands to subf are the opposite of sub, so only in the fixed-point 2086 // case, invert the order. 2087 ShouldSwap = !ShouldSwap; 2088 } 2089 2090 if (ShouldSwap) 2091 for (MachineRegisterInfo::use_instr_iterator 2092 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 2093 I != IE; ++I) { 2094 MachineInstr *UseMI = &*I; 2095 if (UseMI->getOpcode() == PPC::BCC) { 2096 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); 2097 unsigned PredCond = PPC::getPredicateCondition(Pred); 2098 assert((!equalityOnly || 2099 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) && 2100 "Invalid predicate for equality-only optimization"); 2101 (void)PredCond; // To suppress warning in release build. 2102 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), 2103 PPC::getSwappedPredicate(Pred))); 2104 } else if (UseMI->getOpcode() == PPC::ISEL || 2105 UseMI->getOpcode() == PPC::ISEL8) { 2106 unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); 2107 assert((!equalityOnly || NewSubReg == PPC::sub_eq) && 2108 "Invalid CR bit for equality-only optimization"); 2109 2110 if (NewSubReg == PPC::sub_lt) 2111 NewSubReg = PPC::sub_gt; 2112 else if (NewSubReg == PPC::sub_gt) 2113 NewSubReg = PPC::sub_lt; 2114 2115 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), 2116 NewSubReg)); 2117 } else // We need to abort on a user we don't understand. 2118 return false; 2119 } 2120 assert(!(Value != 0 && ShouldSwap) && 2121 "Non-zero immediate support and ShouldSwap" 2122 "may conflict in updating predicate"); 2123 2124 // Create a new virtual register to hold the value of the CR set by the 2125 // record-form instruction. If the instruction was not previously in 2126 // record form, then set the kill flag on the CR. 2127 CmpInstr.eraseFromParent(); 2128 2129 MachineBasicBlock::iterator MII = MI; 2130 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), 2131 get(TargetOpcode::COPY), CRReg) 2132 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); 2133 2134 // Even if CR0 register were dead before, it is alive now since the 2135 // instruction we just built uses it. 2136 MI->clearRegisterDeads(PPC::CR0); 2137 2138 if (MIOpC != NewOpC) { 2139 // We need to be careful here: we're replacing one instruction with 2140 // another, and we need to make sure that we get all of the right 2141 // implicit uses and defs. On the other hand, the caller may be holding 2142 // an iterator to this instruction, and so we can't delete it (this is 2143 // specifically the case if this is the instruction directly after the 2144 // compare). 2145 2146 // Rotates are expensive instructions. If we're emitting a record-form 2147 // rotate that can just be an andi/andis, we should just emit that. 2148 if (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINM8) { 2149 Register GPRRes = MI->getOperand(0).getReg(); 2150 int64_t SH = MI->getOperand(2).getImm(); 2151 int64_t MB = MI->getOperand(3).getImm(); 2152 int64_t ME = MI->getOperand(4).getImm(); 2153 // We can only do this if both the start and end of the mask are in the 2154 // same halfword. 2155 bool MBInLoHWord = MB >= 16; 2156 bool MEInLoHWord = ME >= 16; 2157 uint64_t Mask = ~0LLU; 2158 2159 if (MB <= ME && MBInLoHWord == MEInLoHWord && SH == 0) { 2160 Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1); 2161 // The mask value needs to shift right 16 if we're emitting andis. 2162 Mask >>= MBInLoHWord ? 0 : 16; 2163 NewOpC = MIOpC == PPC::RLWINM 2164 ? (MBInLoHWord ? PPC::ANDI_rec : PPC::ANDIS_rec) 2165 : (MBInLoHWord ? PPC::ANDI8_rec : PPC::ANDIS8_rec); 2166 } else if (MRI->use_empty(GPRRes) && (ME == 31) && 2167 (ME - MB + 1 == SH) && (MB >= 16)) { 2168 // If we are rotating by the exact number of bits as are in the mask 2169 // and the mask is in the least significant bits of the register, 2170 // that's just an andis. (as long as the GPR result has no uses). 2171 Mask = ((1LLU << 32) - 1) & ~((1LLU << (32 - SH)) - 1); 2172 Mask >>= 16; 2173 NewOpC = MIOpC == PPC::RLWINM ? PPC::ANDIS_rec : PPC::ANDIS8_rec; 2174 } 2175 // If we've set the mask, we can transform. 2176 if (Mask != ~0LLU) { 2177 MI->RemoveOperand(4); 2178 MI->RemoveOperand(3); 2179 MI->getOperand(2).setImm(Mask); 2180 NumRcRotatesConvertedToRcAnd++; 2181 } 2182 } else if (MIOpC == PPC::RLDICL && MI->getOperand(2).getImm() == 0) { 2183 int64_t MB = MI->getOperand(3).getImm(); 2184 if (MB >= 48) { 2185 uint64_t Mask = (1LLU << (63 - MB + 1)) - 1; 2186 NewOpC = PPC::ANDI8_rec; 2187 MI->RemoveOperand(3); 2188 MI->getOperand(2).setImm(Mask); 2189 NumRcRotatesConvertedToRcAnd++; 2190 } 2191 } 2192 2193 const MCInstrDesc &NewDesc = get(NewOpC); 2194 MI->setDesc(NewDesc); 2195 2196 if (NewDesc.ImplicitDefs) 2197 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs(); 2198 *ImpDefs; ++ImpDefs) 2199 if (!MI->definesRegister(*ImpDefs)) 2200 MI->addOperand(*MI->getParent()->getParent(), 2201 MachineOperand::CreateReg(*ImpDefs, true, true)); 2202 if (NewDesc.ImplicitUses) 2203 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses(); 2204 *ImpUses; ++ImpUses) 2205 if (!MI->readsRegister(*ImpUses)) 2206 MI->addOperand(*MI->getParent()->getParent(), 2207 MachineOperand::CreateReg(*ImpUses, false, true)); 2208 } 2209 assert(MI->definesRegister(PPC::CR0) && 2210 "Record-form instruction does not define cr0?"); 2211 2212 // Modify the condition code of operands in OperandsToUpdate. 2213 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 2214 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 2215 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) 2216 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); 2217 2218 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) 2219 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); 2220 2221 return true; 2222 } 2223 2224 /// GetInstSize - Return the number of bytes of code the specified 2225 /// instruction may be. This returns the maximum number of bytes. 2226 /// 2227 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 2228 unsigned Opcode = MI.getOpcode(); 2229 2230 if (Opcode == PPC::INLINEASM || Opcode == PPC::INLINEASM_BR) { 2231 const MachineFunction *MF = MI.getParent()->getParent(); 2232 const char *AsmStr = MI.getOperand(0).getSymbolName(); 2233 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 2234 } else if (Opcode == TargetOpcode::STACKMAP) { 2235 StackMapOpers Opers(&MI); 2236 return Opers.getNumPatchBytes(); 2237 } else if (Opcode == TargetOpcode::PATCHPOINT) { 2238 PatchPointOpers Opers(&MI); 2239 return Opers.getNumPatchBytes(); 2240 } else { 2241 return get(Opcode).getSize(); 2242 } 2243 } 2244 2245 std::pair<unsigned, unsigned> 2246 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 2247 const unsigned Mask = PPCII::MO_ACCESS_MASK; 2248 return std::make_pair(TF & Mask, TF & ~Mask); 2249 } 2250 2251 ArrayRef<std::pair<unsigned, const char *>> 2252 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 2253 using namespace PPCII; 2254 static const std::pair<unsigned, const char *> TargetFlags[] = { 2255 {MO_LO, "ppc-lo"}, 2256 {MO_HA, "ppc-ha"}, 2257 {MO_TPREL_LO, "ppc-tprel-lo"}, 2258 {MO_TPREL_HA, "ppc-tprel-ha"}, 2259 {MO_DTPREL_LO, "ppc-dtprel-lo"}, 2260 {MO_TLSLD_LO, "ppc-tlsld-lo"}, 2261 {MO_TOC_LO, "ppc-toc-lo"}, 2262 {MO_TLS, "ppc-tls"}}; 2263 return makeArrayRef(TargetFlags); 2264 } 2265 2266 ArrayRef<std::pair<unsigned, const char *>> 2267 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { 2268 using namespace PPCII; 2269 static const std::pair<unsigned, const char *> TargetFlags[] = { 2270 {MO_PLT, "ppc-plt"}, 2271 {MO_PIC_FLAG, "ppc-pic"}, 2272 {MO_PCREL_FLAG, "ppc-pcrel"}, 2273 {MO_GOT_FLAG, "ppc-got"}}; 2274 return makeArrayRef(TargetFlags); 2275 } 2276 2277 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction. 2278 // The VSX versions have the advantage of a full 64-register target whereas 2279 // the FP ones have the advantage of lower latency and higher throughput. So 2280 // what we are after is using the faster instructions in low register pressure 2281 // situations and using the larger register file in high register pressure 2282 // situations. 2283 bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr &MI) const { 2284 unsigned UpperOpcode, LowerOpcode; 2285 switch (MI.getOpcode()) { 2286 case PPC::DFLOADf32: 2287 UpperOpcode = PPC::LXSSP; 2288 LowerOpcode = PPC::LFS; 2289 break; 2290 case PPC::DFLOADf64: 2291 UpperOpcode = PPC::LXSD; 2292 LowerOpcode = PPC::LFD; 2293 break; 2294 case PPC::DFSTOREf32: 2295 UpperOpcode = PPC::STXSSP; 2296 LowerOpcode = PPC::STFS; 2297 break; 2298 case PPC::DFSTOREf64: 2299 UpperOpcode = PPC::STXSD; 2300 LowerOpcode = PPC::STFD; 2301 break; 2302 case PPC::XFLOADf32: 2303 UpperOpcode = PPC::LXSSPX; 2304 LowerOpcode = PPC::LFSX; 2305 break; 2306 case PPC::XFLOADf64: 2307 UpperOpcode = PPC::LXSDX; 2308 LowerOpcode = PPC::LFDX; 2309 break; 2310 case PPC::XFSTOREf32: 2311 UpperOpcode = PPC::STXSSPX; 2312 LowerOpcode = PPC::STFSX; 2313 break; 2314 case PPC::XFSTOREf64: 2315 UpperOpcode = PPC::STXSDX; 2316 LowerOpcode = PPC::STFDX; 2317 break; 2318 case PPC::LIWAX: 2319 UpperOpcode = PPC::LXSIWAX; 2320 LowerOpcode = PPC::LFIWAX; 2321 break; 2322 case PPC::LIWZX: 2323 UpperOpcode = PPC::LXSIWZX; 2324 LowerOpcode = PPC::LFIWZX; 2325 break; 2326 case PPC::STIWX: 2327 UpperOpcode = PPC::STXSIWX; 2328 LowerOpcode = PPC::STFIWX; 2329 break; 2330 default: 2331 llvm_unreachable("Unknown Operation!"); 2332 } 2333 2334 Register TargetReg = MI.getOperand(0).getReg(); 2335 unsigned Opcode; 2336 if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) || 2337 (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31)) 2338 Opcode = LowerOpcode; 2339 else 2340 Opcode = UpperOpcode; 2341 MI.setDesc(get(Opcode)); 2342 return true; 2343 } 2344 2345 static bool isAnImmediateOperand(const MachineOperand &MO) { 2346 return MO.isCPI() || MO.isGlobal() || MO.isImm(); 2347 } 2348 2349 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 2350 auto &MBB = *MI.getParent(); 2351 auto DL = MI.getDebugLoc(); 2352 2353 switch (MI.getOpcode()) { 2354 case TargetOpcode::LOAD_STACK_GUARD: { 2355 assert(Subtarget.isTargetLinux() && 2356 "Only Linux target is expected to contain LOAD_STACK_GUARD"); 2357 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; 2358 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2; 2359 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ)); 2360 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2361 .addImm(Offset) 2362 .addReg(Reg); 2363 return true; 2364 } 2365 case PPC::DFLOADf32: 2366 case PPC::DFLOADf64: 2367 case PPC::DFSTOREf32: 2368 case PPC::DFSTOREf64: { 2369 assert(Subtarget.hasP9Vector() && 2370 "Invalid D-Form Pseudo-ops on Pre-P9 target."); 2371 assert(MI.getOperand(2).isReg() && 2372 isAnImmediateOperand(MI.getOperand(1)) && 2373 "D-form op must have register and immediate operands"); 2374 return expandVSXMemPseudo(MI); 2375 } 2376 case PPC::XFLOADf32: 2377 case PPC::XFSTOREf32: 2378 case PPC::LIWAX: 2379 case PPC::LIWZX: 2380 case PPC::STIWX: { 2381 assert(Subtarget.hasP8Vector() && 2382 "Invalid X-Form Pseudo-ops on Pre-P8 target."); 2383 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() && 2384 "X-form op must have register and register operands"); 2385 return expandVSXMemPseudo(MI); 2386 } 2387 case PPC::XFLOADf64: 2388 case PPC::XFSTOREf64: { 2389 assert(Subtarget.hasVSX() && 2390 "Invalid X-Form Pseudo-ops on target that has no VSX."); 2391 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() && 2392 "X-form op must have register and register operands"); 2393 return expandVSXMemPseudo(MI); 2394 } 2395 case PPC::SPILLTOVSR_LD: { 2396 Register TargetReg = MI.getOperand(0).getReg(); 2397 if (PPC::VSFRCRegClass.contains(TargetReg)) { 2398 MI.setDesc(get(PPC::DFLOADf64)); 2399 return expandPostRAPseudo(MI); 2400 } 2401 else 2402 MI.setDesc(get(PPC::LD)); 2403 return true; 2404 } 2405 case PPC::SPILLTOVSR_ST: { 2406 Register SrcReg = MI.getOperand(0).getReg(); 2407 if (PPC::VSFRCRegClass.contains(SrcReg)) { 2408 NumStoreSPILLVSRRCAsVec++; 2409 MI.setDesc(get(PPC::DFSTOREf64)); 2410 return expandPostRAPseudo(MI); 2411 } else { 2412 NumStoreSPILLVSRRCAsGpr++; 2413 MI.setDesc(get(PPC::STD)); 2414 } 2415 return true; 2416 } 2417 case PPC::SPILLTOVSR_LDX: { 2418 Register TargetReg = MI.getOperand(0).getReg(); 2419 if (PPC::VSFRCRegClass.contains(TargetReg)) 2420 MI.setDesc(get(PPC::LXSDX)); 2421 else 2422 MI.setDesc(get(PPC::LDX)); 2423 return true; 2424 } 2425 case PPC::SPILLTOVSR_STX: { 2426 Register SrcReg = MI.getOperand(0).getReg(); 2427 if (PPC::VSFRCRegClass.contains(SrcReg)) { 2428 NumStoreSPILLVSRRCAsVec++; 2429 MI.setDesc(get(PPC::STXSDX)); 2430 } else { 2431 NumStoreSPILLVSRRCAsGpr++; 2432 MI.setDesc(get(PPC::STDX)); 2433 } 2434 return true; 2435 } 2436 2437 case PPC::CFENCE8: { 2438 auto Val = MI.getOperand(0).getReg(); 2439 BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val); 2440 BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP)) 2441 .addImm(PPC::PRED_NE_MINUS) 2442 .addReg(PPC::CR7) 2443 .addImm(1); 2444 MI.setDesc(get(PPC::ISYNC)); 2445 MI.RemoveOperand(0); 2446 return true; 2447 } 2448 } 2449 return false; 2450 } 2451 2452 // Essentially a compile-time implementation of a compare->isel sequence. 2453 // It takes two constants to compare, along with the true/false registers 2454 // and the comparison type (as a subreg to a CR field) and returns one 2455 // of the true/false registers, depending on the comparison results. 2456 static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc, 2457 unsigned TrueReg, unsigned FalseReg, 2458 unsigned CRSubReg) { 2459 // Signed comparisons. The immediates are assumed to be sign-extended. 2460 if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) { 2461 switch (CRSubReg) { 2462 default: llvm_unreachable("Unknown integer comparison type."); 2463 case PPC::sub_lt: 2464 return Imm1 < Imm2 ? TrueReg : FalseReg; 2465 case PPC::sub_gt: 2466 return Imm1 > Imm2 ? TrueReg : FalseReg; 2467 case PPC::sub_eq: 2468 return Imm1 == Imm2 ? TrueReg : FalseReg; 2469 } 2470 } 2471 // Unsigned comparisons. 2472 else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) { 2473 switch (CRSubReg) { 2474 default: llvm_unreachable("Unknown integer comparison type."); 2475 case PPC::sub_lt: 2476 return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg; 2477 case PPC::sub_gt: 2478 return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg; 2479 case PPC::sub_eq: 2480 return Imm1 == Imm2 ? TrueReg : FalseReg; 2481 } 2482 } 2483 return PPC::NoRegister; 2484 } 2485 2486 void PPCInstrInfo::replaceInstrOperandWithImm(MachineInstr &MI, 2487 unsigned OpNo, 2488 int64_t Imm) const { 2489 assert(MI.getOperand(OpNo).isReg() && "Operand must be a REG"); 2490 // Replace the REG with the Immediate. 2491 Register InUseReg = MI.getOperand(OpNo).getReg(); 2492 MI.getOperand(OpNo).ChangeToImmediate(Imm); 2493 2494 if (MI.implicit_operands().empty()) 2495 return; 2496 2497 // We need to make sure that the MI didn't have any implicit use 2498 // of this REG any more. 2499 const TargetRegisterInfo *TRI = &getRegisterInfo(); 2500 int UseOpIdx = MI.findRegisterUseOperandIdx(InUseReg, false, TRI); 2501 if (UseOpIdx >= 0) { 2502 MachineOperand &MO = MI.getOperand(UseOpIdx); 2503 if (MO.isImplicit()) 2504 // The operands must always be in the following order: 2505 // - explicit reg defs, 2506 // - other explicit operands (reg uses, immediates, etc.), 2507 // - implicit reg defs 2508 // - implicit reg uses 2509 // Therefore, removing the implicit operand won't change the explicit 2510 // operands layout. 2511 MI.RemoveOperand(UseOpIdx); 2512 } 2513 } 2514 2515 // Replace an instruction with one that materializes a constant (and sets 2516 // CR0 if the original instruction was a record-form instruction). 2517 void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI, 2518 const LoadImmediateInfo &LII) const { 2519 // Remove existing operands. 2520 int OperandToKeep = LII.SetCR ? 1 : 0; 2521 for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--) 2522 MI.RemoveOperand(i); 2523 2524 // Replace the instruction. 2525 if (LII.SetCR) { 2526 MI.setDesc(get(LII.Is64Bit ? PPC::ANDI8_rec : PPC::ANDI_rec)); 2527 // Set the immediate. 2528 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2529 .addImm(LII.Imm).addReg(PPC::CR0, RegState::ImplicitDefine); 2530 return; 2531 } 2532 else 2533 MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI)); 2534 2535 // Set the immediate. 2536 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2537 .addImm(LII.Imm); 2538 } 2539 2540 MachineInstr *PPCInstrInfo::getDefMIPostRA(unsigned Reg, MachineInstr &MI, 2541 bool &SeenIntermediateUse) const { 2542 assert(!MI.getParent()->getParent()->getRegInfo().isSSA() && 2543 "Should be called after register allocation."); 2544 const TargetRegisterInfo *TRI = &getRegisterInfo(); 2545 MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI; 2546 It++; 2547 SeenIntermediateUse = false; 2548 for (; It != E; ++It) { 2549 if (It->modifiesRegister(Reg, TRI)) 2550 return &*It; 2551 if (It->readsRegister(Reg, TRI)) 2552 SeenIntermediateUse = true; 2553 } 2554 return nullptr; 2555 } 2556 2557 MachineInstr *PPCInstrInfo::getForwardingDefMI( 2558 MachineInstr &MI, 2559 unsigned &OpNoForForwarding, 2560 bool &SeenIntermediateUse) const { 2561 OpNoForForwarding = ~0U; 2562 MachineInstr *DefMI = nullptr; 2563 MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo(); 2564 const TargetRegisterInfo *TRI = &getRegisterInfo(); 2565 // If we're in SSA, get the defs through the MRI. Otherwise, only look 2566 // within the basic block to see if the register is defined using an LI/LI8. 2567 if (MRI->isSSA()) { 2568 for (int i = 1, e = MI.getNumOperands(); i < e; i++) { 2569 if (!MI.getOperand(i).isReg()) 2570 continue; 2571 Register Reg = MI.getOperand(i).getReg(); 2572 if (!Register::isVirtualRegister(Reg)) 2573 continue; 2574 unsigned TrueReg = TRI->lookThruCopyLike(Reg, MRI); 2575 if (Register::isVirtualRegister(TrueReg)) { 2576 DefMI = MRI->getVRegDef(TrueReg); 2577 if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8) { 2578 OpNoForForwarding = i; 2579 break; 2580 } 2581 } 2582 } 2583 } else { 2584 // Looking back through the definition for each operand could be expensive, 2585 // so exit early if this isn't an instruction that either has an immediate 2586 // form or is already an immediate form that we can handle. 2587 ImmInstrInfo III; 2588 unsigned Opc = MI.getOpcode(); 2589 bool ConvertibleImmForm = 2590 Opc == PPC::CMPWI || Opc == PPC::CMPLWI || Opc == PPC::CMPDI || 2591 Opc == PPC::CMPLDI || Opc == PPC::ADDI || Opc == PPC::ADDI8 || 2592 Opc == PPC::ORI || Opc == PPC::ORI8 || Opc == PPC::XORI || 2593 Opc == PPC::XORI8 || Opc == PPC::RLDICL || Opc == PPC::RLDICL_rec || 2594 Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 || 2595 Opc == PPC::RLWINM || Opc == PPC::RLWINM_rec || Opc == PPC::RLWINM8 || 2596 Opc == PPC::RLWINM8_rec; 2597 bool IsVFReg = (MI.getNumOperands() && MI.getOperand(0).isReg()) 2598 ? isVFRegister(MI.getOperand(0).getReg()) 2599 : false; 2600 if (!ConvertibleImmForm && !instrHasImmForm(Opc, IsVFReg, III, true)) 2601 return nullptr; 2602 2603 // Don't convert or %X, %Y, %Y since that's just a register move. 2604 if ((Opc == PPC::OR || Opc == PPC::OR8) && 2605 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 2606 return nullptr; 2607 for (int i = 1, e = MI.getNumOperands(); i < e; i++) { 2608 MachineOperand &MO = MI.getOperand(i); 2609 SeenIntermediateUse = false; 2610 if (MO.isReg() && MO.isUse() && !MO.isImplicit()) { 2611 Register Reg = MI.getOperand(i).getReg(); 2612 // If we see another use of this reg between the def and the MI, 2613 // we want to flat it so the def isn't deleted. 2614 MachineInstr *DefMI = getDefMIPostRA(Reg, MI, SeenIntermediateUse); 2615 if (DefMI) { 2616 // Is this register defined by some form of add-immediate (including 2617 // load-immediate) within this basic block? 2618 switch (DefMI->getOpcode()) { 2619 default: 2620 break; 2621 case PPC::LI: 2622 case PPC::LI8: 2623 case PPC::ADDItocL: 2624 case PPC::ADDI: 2625 case PPC::ADDI8: 2626 OpNoForForwarding = i; 2627 return DefMI; 2628 } 2629 } 2630 } 2631 } 2632 } 2633 return OpNoForForwarding == ~0U ? nullptr : DefMI; 2634 } 2635 2636 unsigned PPCInstrInfo::getSpillTarget() const { 2637 return Subtarget.hasP9Vector() ? 1 : 0; 2638 } 2639 2640 const unsigned *PPCInstrInfo::getStoreOpcodesForSpillArray() const { 2641 return StoreSpillOpcodesArray[getSpillTarget()]; 2642 } 2643 2644 const unsigned *PPCInstrInfo::getLoadOpcodesForSpillArray() const { 2645 return LoadSpillOpcodesArray[getSpillTarget()]; 2646 } 2647 2648 void PPCInstrInfo::fixupIsDeadOrKill(MachineInstr &StartMI, MachineInstr &EndMI, 2649 unsigned RegNo) const { 2650 const MachineRegisterInfo &MRI = 2651 StartMI.getParent()->getParent()->getRegInfo(); 2652 if (MRI.isSSA()) 2653 return; 2654 2655 // Instructions between [StartMI, EndMI] should be in same basic block. 2656 assert((StartMI.getParent() == EndMI.getParent()) && 2657 "Instructions are not in same basic block"); 2658 2659 bool IsKillSet = false; 2660 2661 auto clearOperandKillInfo = [=] (MachineInstr &MI, unsigned Index) { 2662 MachineOperand &MO = MI.getOperand(Index); 2663 if (MO.isReg() && MO.isUse() && MO.isKill() && 2664 getRegisterInfo().regsOverlap(MO.getReg(), RegNo)) 2665 MO.setIsKill(false); 2666 }; 2667 2668 // Set killed flag for EndMI. 2669 // No need to do anything if EndMI defines RegNo. 2670 int UseIndex = 2671 EndMI.findRegisterUseOperandIdx(RegNo, false, &getRegisterInfo()); 2672 if (UseIndex != -1) { 2673 EndMI.getOperand(UseIndex).setIsKill(true); 2674 IsKillSet = true; 2675 // Clear killed flag for other EndMI operands related to RegNo. In some 2676 // upexpected cases, killed may be set multiple times for same register 2677 // operand in same MI. 2678 for (int i = 0, e = EndMI.getNumOperands(); i != e; ++i) 2679 if (i != UseIndex) 2680 clearOperandKillInfo(EndMI, i); 2681 } 2682 2683 // Walking the inst in reverse order (EndMI -> StartMI]. 2684 MachineBasicBlock::reverse_iterator It = EndMI; 2685 MachineBasicBlock::reverse_iterator E = EndMI.getParent()->rend(); 2686 // EndMI has been handled above, skip it here. 2687 It++; 2688 MachineOperand *MO = nullptr; 2689 for (; It != E; ++It) { 2690 // Skip insturctions which could not be a def/use of RegNo. 2691 if (It->isDebugInstr() || It->isPosition()) 2692 continue; 2693 2694 // Clear killed flag for all It operands related to RegNo. In some 2695 // upexpected cases, killed may be set multiple times for same register 2696 // operand in same MI. 2697 for (int i = 0, e = It->getNumOperands(); i != e; ++i) 2698 clearOperandKillInfo(*It, i); 2699 2700 // If killed is not set, set killed for its last use or set dead for its def 2701 // if no use found. 2702 if (!IsKillSet) { 2703 if ((MO = It->findRegisterUseOperand(RegNo, false, &getRegisterInfo()))) { 2704 // Use found, set it killed. 2705 IsKillSet = true; 2706 MO->setIsKill(true); 2707 continue; 2708 } else if ((MO = It->findRegisterDefOperand(RegNo, false, true, 2709 &getRegisterInfo()))) { 2710 // No use found, set dead for its def. 2711 assert(&*It == &StartMI && "No new def between StartMI and EndMI."); 2712 MO->setIsDead(true); 2713 break; 2714 } 2715 } 2716 2717 if ((&*It) == &StartMI) 2718 break; 2719 } 2720 // Ensure RegMo liveness is killed after EndMI. 2721 assert((IsKillSet || (MO && MO->isDead())) && 2722 "RegNo should be killed or dead"); 2723 } 2724 2725 // This opt tries to convert the following imm form to an index form to save an 2726 // add for stack variables. 2727 // Return false if no such pattern found. 2728 // 2729 // ADDI instr: ToBeChangedReg = ADDI FrameBaseReg, OffsetAddi 2730 // ADD instr: ToBeDeletedReg = ADD ToBeChangedReg(killed), ScaleReg 2731 // Imm instr: Reg = op OffsetImm, ToBeDeletedReg(killed) 2732 // 2733 // can be converted to: 2734 // 2735 // new ADDI instr: ToBeChangedReg = ADDI FrameBaseReg, (OffsetAddi + OffsetImm) 2736 // Index instr: Reg = opx ScaleReg, ToBeChangedReg(killed) 2737 // 2738 // In order to eliminate ADD instr, make sure that: 2739 // 1: (OffsetAddi + OffsetImm) must be int16 since this offset will be used in 2740 // new ADDI instr and ADDI can only take int16 Imm. 2741 // 2: ToBeChangedReg must be killed in ADD instr and there is no other use 2742 // between ADDI and ADD instr since its original def in ADDI will be changed 2743 // in new ADDI instr. And also there should be no new def for it between 2744 // ADD and Imm instr as ToBeChangedReg will be used in Index instr. 2745 // 3: ToBeDeletedReg must be killed in Imm instr and there is no other use 2746 // between ADD and Imm instr since ADD instr will be eliminated. 2747 // 4: ScaleReg must not be redefined between ADD and Imm instr since it will be 2748 // moved to Index instr. 2749 bool PPCInstrInfo::foldFrameOffset(MachineInstr &MI) const { 2750 MachineFunction *MF = MI.getParent()->getParent(); 2751 MachineRegisterInfo *MRI = &MF->getRegInfo(); 2752 bool PostRA = !MRI->isSSA(); 2753 // Do this opt after PEI which is after RA. The reason is stack slot expansion 2754 // in PEI may expose such opportunities since in PEI, stack slot offsets to 2755 // frame base(OffsetAddi) are determined. 2756 if (!PostRA) 2757 return false; 2758 unsigned ToBeDeletedReg = 0; 2759 int64_t OffsetImm = 0; 2760 unsigned XFormOpcode = 0; 2761 ImmInstrInfo III; 2762 2763 // Check if Imm instr meets requirement. 2764 if (!isImmInstrEligibleForFolding(MI, ToBeDeletedReg, XFormOpcode, OffsetImm, 2765 III)) 2766 return false; 2767 2768 bool OtherIntermediateUse = false; 2769 MachineInstr *ADDMI = getDefMIPostRA(ToBeDeletedReg, MI, OtherIntermediateUse); 2770 2771 // Exit if there is other use between ADD and Imm instr or no def found. 2772 if (OtherIntermediateUse || !ADDMI) 2773 return false; 2774 2775 // Check if ADD instr meets requirement. 2776 if (!isADDInstrEligibleForFolding(*ADDMI)) 2777 return false; 2778 2779 unsigned ScaleRegIdx = 0; 2780 int64_t OffsetAddi = 0; 2781 MachineInstr *ADDIMI = nullptr; 2782 2783 // Check if there is a valid ToBeChangedReg in ADDMI. 2784 // 1: It must be killed. 2785 // 2: Its definition must be a valid ADDIMI. 2786 // 3: It must satify int16 offset requirement. 2787 if (isValidToBeChangedReg(ADDMI, 1, ADDIMI, OffsetAddi, OffsetImm)) 2788 ScaleRegIdx = 2; 2789 else if (isValidToBeChangedReg(ADDMI, 2, ADDIMI, OffsetAddi, OffsetImm)) 2790 ScaleRegIdx = 1; 2791 else 2792 return false; 2793 2794 assert(ADDIMI && "There should be ADDIMI for valid ToBeChangedReg."); 2795 unsigned ToBeChangedReg = ADDIMI->getOperand(0).getReg(); 2796 unsigned ScaleReg = ADDMI->getOperand(ScaleRegIdx).getReg(); 2797 auto NewDefFor = [&](unsigned Reg, MachineBasicBlock::iterator Start, 2798 MachineBasicBlock::iterator End) { 2799 for (auto It = ++Start; It != End; It++) 2800 if (It->modifiesRegister(Reg, &getRegisterInfo())) 2801 return true; 2802 return false; 2803 }; 2804 2805 // We are trying to replace the ImmOpNo with ScaleReg. Give up if it is 2806 // treated as special zero when ScaleReg is R0/X0 register. 2807 if (III.ZeroIsSpecialOrig == III.ImmOpNo && 2808 (ScaleReg == PPC::R0 || ScaleReg == PPC::X0)) 2809 return false; 2810 2811 // Make sure no other def for ToBeChangedReg and ScaleReg between ADD Instr 2812 // and Imm Instr. 2813 if (NewDefFor(ToBeChangedReg, *ADDMI, MI) || NewDefFor(ScaleReg, *ADDMI, MI)) 2814 return false; 2815 2816 // Now start to do the transformation. 2817 LLVM_DEBUG(dbgs() << "Replace instruction: " 2818 << "\n"); 2819 LLVM_DEBUG(ADDIMI->dump()); 2820 LLVM_DEBUG(ADDMI->dump()); 2821 LLVM_DEBUG(MI.dump()); 2822 LLVM_DEBUG(dbgs() << "with: " 2823 << "\n"); 2824 2825 // Update ADDI instr. 2826 ADDIMI->getOperand(2).setImm(OffsetAddi + OffsetImm); 2827 2828 // Update Imm instr. 2829 MI.setDesc(get(XFormOpcode)); 2830 MI.getOperand(III.ImmOpNo) 2831 .ChangeToRegister(ScaleReg, false, false, 2832 ADDMI->getOperand(ScaleRegIdx).isKill()); 2833 2834 MI.getOperand(III.OpNoForForwarding) 2835 .ChangeToRegister(ToBeChangedReg, false, false, true); 2836 2837 // Eliminate ADD instr. 2838 ADDMI->eraseFromParent(); 2839 2840 LLVM_DEBUG(ADDIMI->dump()); 2841 LLVM_DEBUG(MI.dump()); 2842 2843 return true; 2844 } 2845 2846 bool PPCInstrInfo::isADDIInstrEligibleForFolding(MachineInstr &ADDIMI, 2847 int64_t &Imm) const { 2848 unsigned Opc = ADDIMI.getOpcode(); 2849 2850 // Exit if the instruction is not ADDI. 2851 if (Opc != PPC::ADDI && Opc != PPC::ADDI8) 2852 return false; 2853 2854 // The operand may not necessarily be an immediate - it could be a relocation. 2855 if (!ADDIMI.getOperand(2).isImm()) 2856 return false; 2857 2858 Imm = ADDIMI.getOperand(2).getImm(); 2859 2860 return true; 2861 } 2862 2863 bool PPCInstrInfo::isADDInstrEligibleForFolding(MachineInstr &ADDMI) const { 2864 unsigned Opc = ADDMI.getOpcode(); 2865 2866 // Exit if the instruction is not ADD. 2867 return Opc == PPC::ADD4 || Opc == PPC::ADD8; 2868 } 2869 2870 bool PPCInstrInfo::isImmInstrEligibleForFolding(MachineInstr &MI, 2871 unsigned &ToBeDeletedReg, 2872 unsigned &XFormOpcode, 2873 int64_t &OffsetImm, 2874 ImmInstrInfo &III) const { 2875 // Only handle load/store. 2876 if (!MI.mayLoadOrStore()) 2877 return false; 2878 2879 unsigned Opc = MI.getOpcode(); 2880 2881 XFormOpcode = RI.getMappedIdxOpcForImmOpc(Opc); 2882 2883 // Exit if instruction has no index form. 2884 if (XFormOpcode == PPC::INSTRUCTION_LIST_END) 2885 return false; 2886 2887 // TODO: sync the logic between instrHasImmForm() and ImmToIdxMap. 2888 if (!instrHasImmForm(XFormOpcode, isVFRegister(MI.getOperand(0).getReg()), 2889 III, true)) 2890 return false; 2891 2892 if (!III.IsSummingOperands) 2893 return false; 2894 2895 MachineOperand ImmOperand = MI.getOperand(III.ImmOpNo); 2896 MachineOperand RegOperand = MI.getOperand(III.OpNoForForwarding); 2897 // Only support imm operands, not relocation slots or others. 2898 if (!ImmOperand.isImm()) 2899 return false; 2900 2901 assert(RegOperand.isReg() && "Instruction format is not right"); 2902 2903 // There are other use for ToBeDeletedReg after Imm instr, can not delete it. 2904 if (!RegOperand.isKill()) 2905 return false; 2906 2907 ToBeDeletedReg = RegOperand.getReg(); 2908 OffsetImm = ImmOperand.getImm(); 2909 2910 return true; 2911 } 2912 2913 bool PPCInstrInfo::isValidToBeChangedReg(MachineInstr *ADDMI, unsigned Index, 2914 MachineInstr *&ADDIMI, 2915 int64_t &OffsetAddi, 2916 int64_t OffsetImm) const { 2917 assert((Index == 1 || Index == 2) && "Invalid operand index for add."); 2918 MachineOperand &MO = ADDMI->getOperand(Index); 2919 2920 if (!MO.isKill()) 2921 return false; 2922 2923 bool OtherIntermediateUse = false; 2924 2925 ADDIMI = getDefMIPostRA(MO.getReg(), *ADDMI, OtherIntermediateUse); 2926 // Currently handle only one "add + Imminstr" pair case, exit if other 2927 // intermediate use for ToBeChangedReg found. 2928 // TODO: handle the cases where there are other "add + Imminstr" pairs 2929 // with same offset in Imminstr which is like: 2930 // 2931 // ADDI instr: ToBeChangedReg = ADDI FrameBaseReg, OffsetAddi 2932 // ADD instr1: ToBeDeletedReg1 = ADD ToBeChangedReg, ScaleReg1 2933 // Imm instr1: Reg1 = op1 OffsetImm, ToBeDeletedReg1(killed) 2934 // ADD instr2: ToBeDeletedReg2 = ADD ToBeChangedReg(killed), ScaleReg2 2935 // Imm instr2: Reg2 = op2 OffsetImm, ToBeDeletedReg2(killed) 2936 // 2937 // can be converted to: 2938 // 2939 // new ADDI instr: ToBeChangedReg = ADDI FrameBaseReg, 2940 // (OffsetAddi + OffsetImm) 2941 // Index instr1: Reg1 = opx1 ScaleReg1, ToBeChangedReg 2942 // Index instr2: Reg2 = opx2 ScaleReg2, ToBeChangedReg(killed) 2943 2944 if (OtherIntermediateUse || !ADDIMI) 2945 return false; 2946 // Check if ADDI instr meets requirement. 2947 if (!isADDIInstrEligibleForFolding(*ADDIMI, OffsetAddi)) 2948 return false; 2949 2950 if (isInt<16>(OffsetAddi + OffsetImm)) 2951 return true; 2952 return false; 2953 } 2954 2955 // If this instruction has an immediate form and one of its operands is a 2956 // result of a load-immediate or an add-immediate, convert it to 2957 // the immediate form if the constant is in range. 2958 bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI, 2959 MachineInstr **KilledDef) const { 2960 MachineFunction *MF = MI.getParent()->getParent(); 2961 MachineRegisterInfo *MRI = &MF->getRegInfo(); 2962 bool PostRA = !MRI->isSSA(); 2963 bool SeenIntermediateUse = true; 2964 unsigned ForwardingOperand = ~0U; 2965 MachineInstr *DefMI = getForwardingDefMI(MI, ForwardingOperand, 2966 SeenIntermediateUse); 2967 if (!DefMI) 2968 return false; 2969 assert(ForwardingOperand < MI.getNumOperands() && 2970 "The forwarding operand needs to be valid at this point"); 2971 bool IsForwardingOperandKilled = MI.getOperand(ForwardingOperand).isKill(); 2972 bool KillFwdDefMI = !SeenIntermediateUse && IsForwardingOperandKilled; 2973 if (KilledDef && KillFwdDefMI) 2974 *KilledDef = DefMI; 2975 2976 ImmInstrInfo III; 2977 bool IsVFReg = MI.getOperand(0).isReg() 2978 ? isVFRegister(MI.getOperand(0).getReg()) 2979 : false; 2980 bool HasImmForm = instrHasImmForm(MI.getOpcode(), IsVFReg, III, PostRA); 2981 // If this is a reg+reg instruction that has a reg+imm form, 2982 // and one of the operands is produced by an add-immediate, 2983 // try to convert it. 2984 if (HasImmForm && 2985 transformToImmFormFedByAdd(MI, III, ForwardingOperand, *DefMI, 2986 KillFwdDefMI)) 2987 return true; 2988 2989 // If this is a reg+reg instruction that has a reg+imm form, 2990 // and one of the operands is produced by LI, convert it now. 2991 if (HasImmForm && 2992 transformToImmFormFedByLI(MI, III, ForwardingOperand, *DefMI)) 2993 return true; 2994 2995 // If this is not a reg+reg, but the DefMI is LI/LI8, check if its user MI 2996 // can be simpified to LI. 2997 if (!HasImmForm && simplifyToLI(MI, *DefMI, ForwardingOperand, KilledDef)) 2998 return true; 2999 3000 return false; 3001 } 3002 3003 bool PPCInstrInfo::instrHasImmForm(unsigned Opc, bool IsVFReg, 3004 ImmInstrInfo &III, bool PostRA) const { 3005 // The vast majority of the instructions would need their operand 2 replaced 3006 // with an immediate when switching to the reg+imm form. A marked exception 3007 // are the update form loads/stores for which a constant operand 2 would need 3008 // to turn into a displacement and move operand 1 to the operand 2 position. 3009 III.ImmOpNo = 2; 3010 III.OpNoForForwarding = 2; 3011 III.ImmWidth = 16; 3012 III.ImmMustBeMultipleOf = 1; 3013 III.TruncateImmTo = 0; 3014 III.IsSummingOperands = false; 3015 switch (Opc) { 3016 default: return false; 3017 case PPC::ADD4: 3018 case PPC::ADD8: 3019 III.SignedImm = true; 3020 III.ZeroIsSpecialOrig = 0; 3021 III.ZeroIsSpecialNew = 1; 3022 III.IsCommutative = true; 3023 III.IsSummingOperands = true; 3024 III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8; 3025 break; 3026 case PPC::ADDC: 3027 case PPC::ADDC8: 3028 III.SignedImm = true; 3029 III.ZeroIsSpecialOrig = 0; 3030 III.ZeroIsSpecialNew = 0; 3031 III.IsCommutative = true; 3032 III.IsSummingOperands = true; 3033 III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8; 3034 break; 3035 case PPC::ADDC_rec: 3036 III.SignedImm = true; 3037 III.ZeroIsSpecialOrig = 0; 3038 III.ZeroIsSpecialNew = 0; 3039 III.IsCommutative = true; 3040 III.IsSummingOperands = true; 3041 III.ImmOpcode = PPC::ADDIC_rec; 3042 break; 3043 case PPC::SUBFC: 3044 case PPC::SUBFC8: 3045 III.SignedImm = true; 3046 III.ZeroIsSpecialOrig = 0; 3047 III.ZeroIsSpecialNew = 0; 3048 III.IsCommutative = false; 3049 III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8; 3050 break; 3051 case PPC::CMPW: 3052 case PPC::CMPD: 3053 III.SignedImm = true; 3054 III.ZeroIsSpecialOrig = 0; 3055 III.ZeroIsSpecialNew = 0; 3056 III.IsCommutative = false; 3057 III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI; 3058 break; 3059 case PPC::CMPLW: 3060 case PPC::CMPLD: 3061 III.SignedImm = false; 3062 III.ZeroIsSpecialOrig = 0; 3063 III.ZeroIsSpecialNew = 0; 3064 III.IsCommutative = false; 3065 III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI; 3066 break; 3067 case PPC::AND_rec: 3068 case PPC::AND8_rec: 3069 case PPC::OR: 3070 case PPC::OR8: 3071 case PPC::XOR: 3072 case PPC::XOR8: 3073 III.SignedImm = false; 3074 III.ZeroIsSpecialOrig = 0; 3075 III.ZeroIsSpecialNew = 0; 3076 III.IsCommutative = true; 3077 switch(Opc) { 3078 default: llvm_unreachable("Unknown opcode"); 3079 case PPC::AND_rec: 3080 III.ImmOpcode = PPC::ANDI_rec; 3081 break; 3082 case PPC::AND8_rec: 3083 III.ImmOpcode = PPC::ANDI8_rec; 3084 break; 3085 case PPC::OR: III.ImmOpcode = PPC::ORI; break; 3086 case PPC::OR8: III.ImmOpcode = PPC::ORI8; break; 3087 case PPC::XOR: III.ImmOpcode = PPC::XORI; break; 3088 case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break; 3089 } 3090 break; 3091 case PPC::RLWNM: 3092 case PPC::RLWNM8: 3093 case PPC::RLWNM_rec: 3094 case PPC::RLWNM8_rec: 3095 case PPC::SLW: 3096 case PPC::SLW8: 3097 case PPC::SLW_rec: 3098 case PPC::SLW8_rec: 3099 case PPC::SRW: 3100 case PPC::SRW8: 3101 case PPC::SRW_rec: 3102 case PPC::SRW8_rec: 3103 case PPC::SRAW: 3104 case PPC::SRAW_rec: 3105 III.SignedImm = false; 3106 III.ZeroIsSpecialOrig = 0; 3107 III.ZeroIsSpecialNew = 0; 3108 III.IsCommutative = false; 3109 // This isn't actually true, but the instructions ignore any of the 3110 // upper bits, so any immediate loaded with an LI is acceptable. 3111 // This does not apply to shift right algebraic because a value 3112 // out of range will produce a -1/0. 3113 III.ImmWidth = 16; 3114 if (Opc == PPC::RLWNM || Opc == PPC::RLWNM8 || Opc == PPC::RLWNM_rec || 3115 Opc == PPC::RLWNM8_rec) 3116 III.TruncateImmTo = 5; 3117 else 3118 III.TruncateImmTo = 6; 3119 switch(Opc) { 3120 default: llvm_unreachable("Unknown opcode"); 3121 case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break; 3122 case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break; 3123 case PPC::RLWNM_rec: 3124 III.ImmOpcode = PPC::RLWINM_rec; 3125 break; 3126 case PPC::RLWNM8_rec: 3127 III.ImmOpcode = PPC::RLWINM8_rec; 3128 break; 3129 case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break; 3130 case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break; 3131 case PPC::SLW_rec: 3132 III.ImmOpcode = PPC::RLWINM_rec; 3133 break; 3134 case PPC::SLW8_rec: 3135 III.ImmOpcode = PPC::RLWINM8_rec; 3136 break; 3137 case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break; 3138 case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break; 3139 case PPC::SRW_rec: 3140 III.ImmOpcode = PPC::RLWINM_rec; 3141 break; 3142 case PPC::SRW8_rec: 3143 III.ImmOpcode = PPC::RLWINM8_rec; 3144 break; 3145 case PPC::SRAW: 3146 III.ImmWidth = 5; 3147 III.TruncateImmTo = 0; 3148 III.ImmOpcode = PPC::SRAWI; 3149 break; 3150 case PPC::SRAW_rec: 3151 III.ImmWidth = 5; 3152 III.TruncateImmTo = 0; 3153 III.ImmOpcode = PPC::SRAWI_rec; 3154 break; 3155 } 3156 break; 3157 case PPC::RLDCL: 3158 case PPC::RLDCL_rec: 3159 case PPC::RLDCR: 3160 case PPC::RLDCR_rec: 3161 case PPC::SLD: 3162 case PPC::SLD_rec: 3163 case PPC::SRD: 3164 case PPC::SRD_rec: 3165 case PPC::SRAD: 3166 case PPC::SRAD_rec: 3167 III.SignedImm = false; 3168 III.ZeroIsSpecialOrig = 0; 3169 III.ZeroIsSpecialNew = 0; 3170 III.IsCommutative = false; 3171 // This isn't actually true, but the instructions ignore any of the 3172 // upper bits, so any immediate loaded with an LI is acceptable. 3173 // This does not apply to shift right algebraic because a value 3174 // out of range will produce a -1/0. 3175 III.ImmWidth = 16; 3176 if (Opc == PPC::RLDCL || Opc == PPC::RLDCL_rec || Opc == PPC::RLDCR || 3177 Opc == PPC::RLDCR_rec) 3178 III.TruncateImmTo = 6; 3179 else 3180 III.TruncateImmTo = 7; 3181 switch(Opc) { 3182 default: llvm_unreachable("Unknown opcode"); 3183 case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break; 3184 case PPC::RLDCL_rec: 3185 III.ImmOpcode = PPC::RLDICL_rec; 3186 break; 3187 case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break; 3188 case PPC::RLDCR_rec: 3189 III.ImmOpcode = PPC::RLDICR_rec; 3190 break; 3191 case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break; 3192 case PPC::SLD_rec: 3193 III.ImmOpcode = PPC::RLDICR_rec; 3194 break; 3195 case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break; 3196 case PPC::SRD_rec: 3197 III.ImmOpcode = PPC::RLDICL_rec; 3198 break; 3199 case PPC::SRAD: 3200 III.ImmWidth = 6; 3201 III.TruncateImmTo = 0; 3202 III.ImmOpcode = PPC::SRADI; 3203 break; 3204 case PPC::SRAD_rec: 3205 III.ImmWidth = 6; 3206 III.TruncateImmTo = 0; 3207 III.ImmOpcode = PPC::SRADI_rec; 3208 break; 3209 } 3210 break; 3211 // Loads and stores: 3212 case PPC::LBZX: 3213 case PPC::LBZX8: 3214 case PPC::LHZX: 3215 case PPC::LHZX8: 3216 case PPC::LHAX: 3217 case PPC::LHAX8: 3218 case PPC::LWZX: 3219 case PPC::LWZX8: 3220 case PPC::LWAX: 3221 case PPC::LDX: 3222 case PPC::LFSX: 3223 case PPC::LFDX: 3224 case PPC::STBX: 3225 case PPC::STBX8: 3226 case PPC::STHX: 3227 case PPC::STHX8: 3228 case PPC::STWX: 3229 case PPC::STWX8: 3230 case PPC::STDX: 3231 case PPC::STFSX: 3232 case PPC::STFDX: 3233 III.SignedImm = true; 3234 III.ZeroIsSpecialOrig = 1; 3235 III.ZeroIsSpecialNew = 2; 3236 III.IsCommutative = true; 3237 III.IsSummingOperands = true; 3238 III.ImmOpNo = 1; 3239 III.OpNoForForwarding = 2; 3240 switch(Opc) { 3241 default: llvm_unreachable("Unknown opcode"); 3242 case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break; 3243 case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break; 3244 case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break; 3245 case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break; 3246 case PPC::LHAX: III.ImmOpcode = PPC::LHA; break; 3247 case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break; 3248 case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break; 3249 case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break; 3250 case PPC::LWAX: 3251 III.ImmOpcode = PPC::LWA; 3252 III.ImmMustBeMultipleOf = 4; 3253 break; 3254 case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break; 3255 case PPC::LFSX: III.ImmOpcode = PPC::LFS; break; 3256 case PPC::LFDX: III.ImmOpcode = PPC::LFD; break; 3257 case PPC::STBX: III.ImmOpcode = PPC::STB; break; 3258 case PPC::STBX8: III.ImmOpcode = PPC::STB8; break; 3259 case PPC::STHX: III.ImmOpcode = PPC::STH; break; 3260 case PPC::STHX8: III.ImmOpcode = PPC::STH8; break; 3261 case PPC::STWX: III.ImmOpcode = PPC::STW; break; 3262 case PPC::STWX8: III.ImmOpcode = PPC::STW8; break; 3263 case PPC::STDX: 3264 III.ImmOpcode = PPC::STD; 3265 III.ImmMustBeMultipleOf = 4; 3266 break; 3267 case PPC::STFSX: III.ImmOpcode = PPC::STFS; break; 3268 case PPC::STFDX: III.ImmOpcode = PPC::STFD; break; 3269 } 3270 break; 3271 case PPC::LBZUX: 3272 case PPC::LBZUX8: 3273 case PPC::LHZUX: 3274 case PPC::LHZUX8: 3275 case PPC::LHAUX: 3276 case PPC::LHAUX8: 3277 case PPC::LWZUX: 3278 case PPC::LWZUX8: 3279 case PPC::LDUX: 3280 case PPC::LFSUX: 3281 case PPC::LFDUX: 3282 case PPC::STBUX: 3283 case PPC::STBUX8: 3284 case PPC::STHUX: 3285 case PPC::STHUX8: 3286 case PPC::STWUX: 3287 case PPC::STWUX8: 3288 case PPC::STDUX: 3289 case PPC::STFSUX: 3290 case PPC::STFDUX: 3291 III.SignedImm = true; 3292 III.ZeroIsSpecialOrig = 2; 3293 III.ZeroIsSpecialNew = 3; 3294 III.IsCommutative = false; 3295 III.IsSummingOperands = true; 3296 III.ImmOpNo = 2; 3297 III.OpNoForForwarding = 3; 3298 switch(Opc) { 3299 default: llvm_unreachable("Unknown opcode"); 3300 case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break; 3301 case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break; 3302 case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break; 3303 case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break; 3304 case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break; 3305 case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break; 3306 case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break; 3307 case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break; 3308 case PPC::LDUX: 3309 III.ImmOpcode = PPC::LDU; 3310 III.ImmMustBeMultipleOf = 4; 3311 break; 3312 case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break; 3313 case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break; 3314 case PPC::STBUX: III.ImmOpcode = PPC::STBU; break; 3315 case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break; 3316 case PPC::STHUX: III.ImmOpcode = PPC::STHU; break; 3317 case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break; 3318 case PPC::STWUX: III.ImmOpcode = PPC::STWU; break; 3319 case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break; 3320 case PPC::STDUX: 3321 III.ImmOpcode = PPC::STDU; 3322 III.ImmMustBeMultipleOf = 4; 3323 break; 3324 case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break; 3325 case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break; 3326 } 3327 break; 3328 // Power9 and up only. For some of these, the X-Form version has access to all 3329 // 64 VSR's whereas the D-Form only has access to the VR's. We replace those 3330 // with pseudo-ops pre-ra and for post-ra, we check that the register loaded 3331 // into or stored from is one of the VR registers. 3332 case PPC::LXVX: 3333 case PPC::LXSSPX: 3334 case PPC::LXSDX: 3335 case PPC::STXVX: 3336 case PPC::STXSSPX: 3337 case PPC::STXSDX: 3338 case PPC::XFLOADf32: 3339 case PPC::XFLOADf64: 3340 case PPC::XFSTOREf32: 3341 case PPC::XFSTOREf64: 3342 if (!Subtarget.hasP9Vector()) 3343 return false; 3344 III.SignedImm = true; 3345 III.ZeroIsSpecialOrig = 1; 3346 III.ZeroIsSpecialNew = 2; 3347 III.IsCommutative = true; 3348 III.IsSummingOperands = true; 3349 III.ImmOpNo = 1; 3350 III.OpNoForForwarding = 2; 3351 III.ImmMustBeMultipleOf = 4; 3352 switch(Opc) { 3353 default: llvm_unreachable("Unknown opcode"); 3354 case PPC::LXVX: 3355 III.ImmOpcode = PPC::LXV; 3356 III.ImmMustBeMultipleOf = 16; 3357 break; 3358 case PPC::LXSSPX: 3359 if (PostRA) { 3360 if (IsVFReg) 3361 III.ImmOpcode = PPC::LXSSP; 3362 else { 3363 III.ImmOpcode = PPC::LFS; 3364 III.ImmMustBeMultipleOf = 1; 3365 } 3366 break; 3367 } 3368 LLVM_FALLTHROUGH; 3369 case PPC::XFLOADf32: 3370 III.ImmOpcode = PPC::DFLOADf32; 3371 break; 3372 case PPC::LXSDX: 3373 if (PostRA) { 3374 if (IsVFReg) 3375 III.ImmOpcode = PPC::LXSD; 3376 else { 3377 III.ImmOpcode = PPC::LFD; 3378 III.ImmMustBeMultipleOf = 1; 3379 } 3380 break; 3381 } 3382 LLVM_FALLTHROUGH; 3383 case PPC::XFLOADf64: 3384 III.ImmOpcode = PPC::DFLOADf64; 3385 break; 3386 case PPC::STXVX: 3387 III.ImmOpcode = PPC::STXV; 3388 III.ImmMustBeMultipleOf = 16; 3389 break; 3390 case PPC::STXSSPX: 3391 if (PostRA) { 3392 if (IsVFReg) 3393 III.ImmOpcode = PPC::STXSSP; 3394 else { 3395 III.ImmOpcode = PPC::STFS; 3396 III.ImmMustBeMultipleOf = 1; 3397 } 3398 break; 3399 } 3400 LLVM_FALLTHROUGH; 3401 case PPC::XFSTOREf32: 3402 III.ImmOpcode = PPC::DFSTOREf32; 3403 break; 3404 case PPC::STXSDX: 3405 if (PostRA) { 3406 if (IsVFReg) 3407 III.ImmOpcode = PPC::STXSD; 3408 else { 3409 III.ImmOpcode = PPC::STFD; 3410 III.ImmMustBeMultipleOf = 1; 3411 } 3412 break; 3413 } 3414 LLVM_FALLTHROUGH; 3415 case PPC::XFSTOREf64: 3416 III.ImmOpcode = PPC::DFSTOREf64; 3417 break; 3418 } 3419 break; 3420 } 3421 return true; 3422 } 3423 3424 // Utility function for swaping two arbitrary operands of an instruction. 3425 static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) { 3426 assert(Op1 != Op2 && "Cannot swap operand with itself."); 3427 3428 unsigned MaxOp = std::max(Op1, Op2); 3429 unsigned MinOp = std::min(Op1, Op2); 3430 MachineOperand MOp1 = MI.getOperand(MinOp); 3431 MachineOperand MOp2 = MI.getOperand(MaxOp); 3432 MI.RemoveOperand(std::max(Op1, Op2)); 3433 MI.RemoveOperand(std::min(Op1, Op2)); 3434 3435 // If the operands we are swapping are the two at the end (the common case) 3436 // we can just remove both and add them in the opposite order. 3437 if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) { 3438 MI.addOperand(MOp2); 3439 MI.addOperand(MOp1); 3440 } else { 3441 // Store all operands in a temporary vector, remove them and re-add in the 3442 // right order. 3443 SmallVector<MachineOperand, 2> MOps; 3444 unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops. 3445 for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) { 3446 MOps.push_back(MI.getOperand(i)); 3447 MI.RemoveOperand(i); 3448 } 3449 // MOp2 needs to be added next. 3450 MI.addOperand(MOp2); 3451 // Now add the rest. 3452 for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) { 3453 if (i == MaxOp) 3454 MI.addOperand(MOp1); 3455 else { 3456 MI.addOperand(MOps.back()); 3457 MOps.pop_back(); 3458 } 3459 } 3460 } 3461 } 3462 3463 // Check if the 'MI' that has the index OpNoForForwarding 3464 // meets the requirement described in the ImmInstrInfo. 3465 bool PPCInstrInfo::isUseMIElgibleForForwarding(MachineInstr &MI, 3466 const ImmInstrInfo &III, 3467 unsigned OpNoForForwarding 3468 ) const { 3469 // As the algorithm of checking for PPC::ZERO/PPC::ZERO8 3470 // would not work pre-RA, we can only do the check post RA. 3471 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3472 if (MRI.isSSA()) 3473 return false; 3474 3475 // Cannot do the transform if MI isn't summing the operands. 3476 if (!III.IsSummingOperands) 3477 return false; 3478 3479 // The instruction we are trying to replace must have the ZeroIsSpecialOrig set. 3480 if (!III.ZeroIsSpecialOrig) 3481 return false; 3482 3483 // We cannot do the transform if the operand we are trying to replace 3484 // isn't the same as the operand the instruction allows. 3485 if (OpNoForForwarding != III.OpNoForForwarding) 3486 return false; 3487 3488 // Check if the instruction we are trying to transform really has 3489 // the special zero register as its operand. 3490 if (MI.getOperand(III.ZeroIsSpecialOrig).getReg() != PPC::ZERO && 3491 MI.getOperand(III.ZeroIsSpecialOrig).getReg() != PPC::ZERO8) 3492 return false; 3493 3494 // This machine instruction is convertible if it is, 3495 // 1. summing the operands. 3496 // 2. one of the operands is special zero register. 3497 // 3. the operand we are trying to replace is allowed by the MI. 3498 return true; 3499 } 3500 3501 // Check if the DefMI is the add inst and set the ImmMO and RegMO 3502 // accordingly. 3503 bool PPCInstrInfo::isDefMIElgibleForForwarding(MachineInstr &DefMI, 3504 const ImmInstrInfo &III, 3505 MachineOperand *&ImmMO, 3506 MachineOperand *&RegMO) const { 3507 unsigned Opc = DefMI.getOpcode(); 3508 if (Opc != PPC::ADDItocL && Opc != PPC::ADDI && Opc != PPC::ADDI8) 3509 return false; 3510 3511 assert(DefMI.getNumOperands() >= 3 && 3512 "Add inst must have at least three operands"); 3513 RegMO = &DefMI.getOperand(1); 3514 ImmMO = &DefMI.getOperand(2); 3515 3516 // This DefMI is elgible for forwarding if it is: 3517 // 1. add inst 3518 // 2. one of the operands is Imm/CPI/Global. 3519 return isAnImmediateOperand(*ImmMO); 3520 } 3521 3522 bool PPCInstrInfo::isRegElgibleForForwarding( 3523 const MachineOperand &RegMO, const MachineInstr &DefMI, 3524 const MachineInstr &MI, bool KillDefMI, 3525 bool &IsFwdFeederRegKilled) const { 3526 // x = addi y, imm 3527 // ... 3528 // z = lfdx 0, x -> z = lfd imm(y) 3529 // The Reg "y" can be forwarded to the MI(z) only when there is no DEF 3530 // of "y" between the DEF of "x" and "z". 3531 // The query is only valid post RA. 3532 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3533 if (MRI.isSSA()) 3534 return false; 3535 3536 Register Reg = RegMO.getReg(); 3537 3538 // Walking the inst in reverse(MI-->DefMI) to get the last DEF of the Reg. 3539 MachineBasicBlock::const_reverse_iterator It = MI; 3540 MachineBasicBlock::const_reverse_iterator E = MI.getParent()->rend(); 3541 It++; 3542 for (; It != E; ++It) { 3543 if (It->modifiesRegister(Reg, &getRegisterInfo()) && (&*It) != &DefMI) 3544 return false; 3545 else if (It->killsRegister(Reg, &getRegisterInfo()) && (&*It) != &DefMI) 3546 IsFwdFeederRegKilled = true; 3547 // Made it to DefMI without encountering a clobber. 3548 if ((&*It) == &DefMI) 3549 break; 3550 } 3551 assert((&*It) == &DefMI && "DefMI is missing"); 3552 3553 // If DefMI also defines the register to be forwarded, we can only forward it 3554 // if DefMI is being erased. 3555 if (DefMI.modifiesRegister(Reg, &getRegisterInfo())) 3556 return KillDefMI; 3557 3558 return true; 3559 } 3560 3561 bool PPCInstrInfo::isImmElgibleForForwarding(const MachineOperand &ImmMO, 3562 const MachineInstr &DefMI, 3563 const ImmInstrInfo &III, 3564 int64_t &Imm) const { 3565 assert(isAnImmediateOperand(ImmMO) && "ImmMO is NOT an immediate"); 3566 if (DefMI.getOpcode() == PPC::ADDItocL) { 3567 // The operand for ADDItocL is CPI, which isn't imm at compiling time, 3568 // However, we know that, it is 16-bit width, and has the alignment of 4. 3569 // Check if the instruction met the requirement. 3570 if (III.ImmMustBeMultipleOf > 4 || 3571 III.TruncateImmTo || III.ImmWidth != 16) 3572 return false; 3573 3574 // Going from XForm to DForm loads means that the displacement needs to be 3575 // not just an immediate but also a multiple of 4, or 16 depending on the 3576 // load. A DForm load cannot be represented if it is a multiple of say 2. 3577 // XForm loads do not have this restriction. 3578 if (ImmMO.isGlobal() && 3579 ImmMO.getGlobal()->getAlignment() < III.ImmMustBeMultipleOf) 3580 return false; 3581 3582 return true; 3583 } 3584 3585 if (ImmMO.isImm()) { 3586 // It is Imm, we need to check if the Imm fit the range. 3587 int64_t Immediate = ImmMO.getImm(); 3588 // Sign-extend to 64-bits. 3589 Imm = ((uint64_t)Immediate & ~0x7FFFuLL) != 0 ? 3590 (Immediate | 0xFFFFFFFFFFFF0000) : Immediate; 3591 3592 if (Imm % III.ImmMustBeMultipleOf) 3593 return false; 3594 if (III.TruncateImmTo) 3595 Imm &= ((1 << III.TruncateImmTo) - 1); 3596 if (III.SignedImm) { 3597 APInt ActualValue(64, Imm, true); 3598 if (!ActualValue.isSignedIntN(III.ImmWidth)) 3599 return false; 3600 } else { 3601 uint64_t UnsignedMax = (1 << III.ImmWidth) - 1; 3602 if ((uint64_t)Imm > UnsignedMax) 3603 return false; 3604 } 3605 } 3606 else 3607 return false; 3608 3609 // This ImmMO is forwarded if it meets the requriement describle 3610 // in ImmInstrInfo 3611 return true; 3612 } 3613 3614 bool PPCInstrInfo::simplifyToLI(MachineInstr &MI, MachineInstr &DefMI, 3615 unsigned OpNoForForwarding, 3616 MachineInstr **KilledDef) const { 3617 if ((DefMI.getOpcode() != PPC::LI && DefMI.getOpcode() != PPC::LI8) || 3618 !DefMI.getOperand(1).isImm()) 3619 return false; 3620 3621 MachineFunction *MF = MI.getParent()->getParent(); 3622 MachineRegisterInfo *MRI = &MF->getRegInfo(); 3623 bool PostRA = !MRI->isSSA(); 3624 3625 int64_t Immediate = DefMI.getOperand(1).getImm(); 3626 // Sign-extend to 64-bits. 3627 int64_t SExtImm = SignExtend64<16>(Immediate); 3628 3629 bool IsForwardingOperandKilled = MI.getOperand(OpNoForForwarding).isKill(); 3630 Register ForwardingOperandReg = MI.getOperand(OpNoForForwarding).getReg(); 3631 3632 bool ReplaceWithLI = false; 3633 bool Is64BitLI = false; 3634 int64_t NewImm = 0; 3635 bool SetCR = false; 3636 unsigned Opc = MI.getOpcode(); 3637 switch (Opc) { 3638 default: 3639 return false; 3640 3641 // FIXME: Any branches conditional on such a comparison can be made 3642 // unconditional. At this time, this happens too infrequently to be worth 3643 // the implementation effort, but if that ever changes, we could convert 3644 // such a pattern here. 3645 case PPC::CMPWI: 3646 case PPC::CMPLWI: 3647 case PPC::CMPDI: 3648 case PPC::CMPLDI: { 3649 // Doing this post-RA would require dataflow analysis to reliably find uses 3650 // of the CR register set by the compare. 3651 // No need to fixup killed/dead flag since this transformation is only valid 3652 // before RA. 3653 if (PostRA) 3654 return false; 3655 // If a compare-immediate is fed by an immediate and is itself an input of 3656 // an ISEL (the most common case) into a COPY of the correct register. 3657 bool Changed = false; 3658 Register DefReg = MI.getOperand(0).getReg(); 3659 int64_t Comparand = MI.getOperand(2).getImm(); 3660 int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0 3661 ? (Comparand | 0xFFFFFFFFFFFF0000) 3662 : Comparand; 3663 3664 for (auto &CompareUseMI : MRI->use_instructions(DefReg)) { 3665 unsigned UseOpc = CompareUseMI.getOpcode(); 3666 if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8) 3667 continue; 3668 unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg(); 3669 Register TrueReg = CompareUseMI.getOperand(1).getReg(); 3670 Register FalseReg = CompareUseMI.getOperand(2).getReg(); 3671 unsigned RegToCopy = 3672 selectReg(SExtImm, SExtComparand, Opc, TrueReg, FalseReg, CRSubReg); 3673 if (RegToCopy == PPC::NoRegister) 3674 continue; 3675 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0. 3676 if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) { 3677 CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI)); 3678 replaceInstrOperandWithImm(CompareUseMI, 1, 0); 3679 CompareUseMI.RemoveOperand(3); 3680 CompareUseMI.RemoveOperand(2); 3681 continue; 3682 } 3683 LLVM_DEBUG( 3684 dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n"); 3685 LLVM_DEBUG(DefMI.dump(); MI.dump(); CompareUseMI.dump()); 3686 LLVM_DEBUG(dbgs() << "Is converted to:\n"); 3687 // Convert to copy and remove unneeded operands. 3688 CompareUseMI.setDesc(get(PPC::COPY)); 3689 CompareUseMI.RemoveOperand(3); 3690 CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1); 3691 CmpIselsConverted++; 3692 Changed = true; 3693 LLVM_DEBUG(CompareUseMI.dump()); 3694 } 3695 if (Changed) 3696 return true; 3697 // This may end up incremented multiple times since this function is called 3698 // during a fixed-point transformation, but it is only meant to indicate the 3699 // presence of this opportunity. 3700 MissedConvertibleImmediateInstrs++; 3701 return false; 3702 } 3703 3704 // Immediate forms - may simply be convertable to an LI. 3705 case PPC::ADDI: 3706 case PPC::ADDI8: { 3707 // Does the sum fit in a 16-bit signed field? 3708 int64_t Addend = MI.getOperand(2).getImm(); 3709 if (isInt<16>(Addend + SExtImm)) { 3710 ReplaceWithLI = true; 3711 Is64BitLI = Opc == PPC::ADDI8; 3712 NewImm = Addend + SExtImm; 3713 break; 3714 } 3715 return false; 3716 } 3717 case PPC::RLDICL: 3718 case PPC::RLDICL_rec: 3719 case PPC::RLDICL_32: 3720 case PPC::RLDICL_32_64: { 3721 // Use APInt's rotate function. 3722 int64_t SH = MI.getOperand(2).getImm(); 3723 int64_t MB = MI.getOperand(3).getImm(); 3724 APInt InVal((Opc == PPC::RLDICL || Opc == PPC::RLDICL_rec) ? 64 : 32, 3725 SExtImm, true); 3726 InVal = InVal.rotl(SH); 3727 uint64_t Mask = MB == 0 ? -1LLU : (1LLU << (63 - MB + 1)) - 1; 3728 InVal &= Mask; 3729 // Can't replace negative values with an LI as that will sign-extend 3730 // and not clear the left bits. If we're setting the CR bit, we will use 3731 // ANDI_rec which won't sign extend, so that's safe. 3732 if (isUInt<15>(InVal.getSExtValue()) || 3733 (Opc == PPC::RLDICL_rec && isUInt<16>(InVal.getSExtValue()))) { 3734 ReplaceWithLI = true; 3735 Is64BitLI = Opc != PPC::RLDICL_32; 3736 NewImm = InVal.getSExtValue(); 3737 SetCR = Opc == PPC::RLDICL_rec; 3738 break; 3739 } 3740 return false; 3741 } 3742 case PPC::RLWINM: 3743 case PPC::RLWINM8: 3744 case PPC::RLWINM_rec: 3745 case PPC::RLWINM8_rec: { 3746 int64_t SH = MI.getOperand(2).getImm(); 3747 int64_t MB = MI.getOperand(3).getImm(); 3748 int64_t ME = MI.getOperand(4).getImm(); 3749 APInt InVal(32, SExtImm, true); 3750 InVal = InVal.rotl(SH); 3751 APInt Mask = APInt::getBitsSetWithWrap(32, 32 - ME - 1, 32 - MB); 3752 InVal &= Mask; 3753 // Can't replace negative values with an LI as that will sign-extend 3754 // and not clear the left bits. If we're setting the CR bit, we will use 3755 // ANDI_rec which won't sign extend, so that's safe. 3756 bool ValueFits = isUInt<15>(InVal.getSExtValue()); 3757 ValueFits |= ((Opc == PPC::RLWINM_rec || Opc == PPC::RLWINM8_rec) && 3758 isUInt<16>(InVal.getSExtValue())); 3759 if (ValueFits) { 3760 ReplaceWithLI = true; 3761 Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8_rec; 3762 NewImm = InVal.getSExtValue(); 3763 SetCR = Opc == PPC::RLWINM_rec || Opc == PPC::RLWINM8_rec; 3764 break; 3765 } 3766 return false; 3767 } 3768 case PPC::ORI: 3769 case PPC::ORI8: 3770 case PPC::XORI: 3771 case PPC::XORI8: { 3772 int64_t LogicalImm = MI.getOperand(2).getImm(); 3773 int64_t Result = 0; 3774 if (Opc == PPC::ORI || Opc == PPC::ORI8) 3775 Result = LogicalImm | SExtImm; 3776 else 3777 Result = LogicalImm ^ SExtImm; 3778 if (isInt<16>(Result)) { 3779 ReplaceWithLI = true; 3780 Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8; 3781 NewImm = Result; 3782 break; 3783 } 3784 return false; 3785 } 3786 } 3787 3788 if (ReplaceWithLI) { 3789 // We need to be careful with CR-setting instructions we're replacing. 3790 if (SetCR) { 3791 // We don't know anything about uses when we're out of SSA, so only 3792 // replace if the new immediate will be reproduced. 3793 bool ImmChanged = (SExtImm & NewImm) != NewImm; 3794 if (PostRA && ImmChanged) 3795 return false; 3796 3797 if (!PostRA) { 3798 // If the defining load-immediate has no other uses, we can just replace 3799 // the immediate with the new immediate. 3800 if (MRI->hasOneUse(DefMI.getOperand(0).getReg())) 3801 DefMI.getOperand(1).setImm(NewImm); 3802 3803 // If we're not using the GPR result of the CR-setting instruction, we 3804 // just need to and with zero/non-zero depending on the new immediate. 3805 else if (MRI->use_empty(MI.getOperand(0).getReg())) { 3806 if (NewImm) { 3807 assert(Immediate && "Transformation converted zero to non-zero?"); 3808 NewImm = Immediate; 3809 } 3810 } else if (ImmChanged) 3811 return false; 3812 } 3813 } 3814 3815 LLVM_DEBUG(dbgs() << "Replacing instruction:\n"); 3816 LLVM_DEBUG(MI.dump()); 3817 LLVM_DEBUG(dbgs() << "Fed by:\n"); 3818 LLVM_DEBUG(DefMI.dump()); 3819 LoadImmediateInfo LII; 3820 LII.Imm = NewImm; 3821 LII.Is64Bit = Is64BitLI; 3822 LII.SetCR = SetCR; 3823 // If we're setting the CR, the original load-immediate must be kept (as an 3824 // operand to ANDI_rec/ANDI8_rec). 3825 if (KilledDef && SetCR) 3826 *KilledDef = nullptr; 3827 replaceInstrWithLI(MI, LII); 3828 3829 // Fixup killed/dead flag after transformation. 3830 // Pattern: 3831 // ForwardingOperandReg = LI imm1 3832 // y = op2 imm2, ForwardingOperandReg(killed) 3833 if (IsForwardingOperandKilled) 3834 fixupIsDeadOrKill(DefMI, MI, ForwardingOperandReg); 3835 3836 LLVM_DEBUG(dbgs() << "With:\n"); 3837 LLVM_DEBUG(MI.dump()); 3838 return true; 3839 } 3840 return false; 3841 } 3842 3843 // If an X-Form instruction is fed by an add-immediate and one of its operands 3844 // is the literal zero, attempt to forward the source of the add-immediate to 3845 // the corresponding D-Form instruction with the displacement coming from 3846 // the immediate being added. 3847 bool PPCInstrInfo::transformToImmFormFedByAdd( 3848 MachineInstr &MI, const ImmInstrInfo &III, unsigned OpNoForForwarding, 3849 MachineInstr &DefMI, bool KillDefMI) const { 3850 // RegMO ImmMO 3851 // | | 3852 // x = addi reg, imm <----- DefMI 3853 // y = op 0 , x <----- MI 3854 // | 3855 // OpNoForForwarding 3856 // Check if the MI meet the requirement described in the III. 3857 if (!isUseMIElgibleForForwarding(MI, III, OpNoForForwarding)) 3858 return false; 3859 3860 // Check if the DefMI meet the requirement 3861 // described in the III. If yes, set the ImmMO and RegMO accordingly. 3862 MachineOperand *ImmMO = nullptr; 3863 MachineOperand *RegMO = nullptr; 3864 if (!isDefMIElgibleForForwarding(DefMI, III, ImmMO, RegMO)) 3865 return false; 3866 assert(ImmMO && RegMO && "Imm and Reg operand must have been set"); 3867 3868 // As we get the Imm operand now, we need to check if the ImmMO meet 3869 // the requirement described in the III. If yes set the Imm. 3870 int64_t Imm = 0; 3871 if (!isImmElgibleForForwarding(*ImmMO, DefMI, III, Imm)) 3872 return false; 3873 3874 bool IsFwdFeederRegKilled = false; 3875 // Check if the RegMO can be forwarded to MI. 3876 if (!isRegElgibleForForwarding(*RegMO, DefMI, MI, KillDefMI, 3877 IsFwdFeederRegKilled)) 3878 return false; 3879 3880 // Get killed info in case fixup needed after transformation. 3881 unsigned ForwardKilledOperandReg = ~0U; 3882 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3883 bool PostRA = !MRI.isSSA(); 3884 if (PostRA && MI.getOperand(OpNoForForwarding).isKill()) 3885 ForwardKilledOperandReg = MI.getOperand(OpNoForForwarding).getReg(); 3886 3887 // We know that, the MI and DefMI both meet the pattern, and 3888 // the Imm also meet the requirement with the new Imm-form. 3889 // It is safe to do the transformation now. 3890 LLVM_DEBUG(dbgs() << "Replacing instruction:\n"); 3891 LLVM_DEBUG(MI.dump()); 3892 LLVM_DEBUG(dbgs() << "Fed by:\n"); 3893 LLVM_DEBUG(DefMI.dump()); 3894 3895 // Update the base reg first. 3896 MI.getOperand(III.OpNoForForwarding).ChangeToRegister(RegMO->getReg(), 3897 false, false, 3898 RegMO->isKill()); 3899 3900 // Then, update the imm. 3901 if (ImmMO->isImm()) { 3902 // If the ImmMO is Imm, change the operand that has ZERO to that Imm 3903 // directly. 3904 replaceInstrOperandWithImm(MI, III.ZeroIsSpecialOrig, Imm); 3905 } 3906 else { 3907 // Otherwise, it is Constant Pool Index(CPI) or Global, 3908 // which is relocation in fact. We need to replace the special zero 3909 // register with ImmMO. 3910 // Before that, we need to fixup the target flags for imm. 3911 // For some reason, we miss to set the flag for the ImmMO if it is CPI. 3912 if (DefMI.getOpcode() == PPC::ADDItocL) 3913 ImmMO->setTargetFlags(PPCII::MO_TOC_LO); 3914 3915 // MI didn't have the interface such as MI.setOperand(i) though 3916 // it has MI.getOperand(i). To repalce the ZERO MachineOperand with 3917 // ImmMO, we need to remove ZERO operand and all the operands behind it, 3918 // and, add the ImmMO, then, move back all the operands behind ZERO. 3919 SmallVector<MachineOperand, 2> MOps; 3920 for (unsigned i = MI.getNumOperands() - 1; i >= III.ZeroIsSpecialOrig; i--) { 3921 MOps.push_back(MI.getOperand(i)); 3922 MI.RemoveOperand(i); 3923 } 3924 3925 // Remove the last MO in the list, which is ZERO operand in fact. 3926 MOps.pop_back(); 3927 // Add the imm operand. 3928 MI.addOperand(*ImmMO); 3929 // Now add the rest back. 3930 for (auto &MO : MOps) 3931 MI.addOperand(MO); 3932 } 3933 3934 // Update the opcode. 3935 MI.setDesc(get(III.ImmOpcode)); 3936 3937 // Fix up killed/dead flag after transformation. 3938 // Pattern 1: 3939 // x = ADD KilledFwdFeederReg, imm 3940 // n = opn KilledFwdFeederReg(killed), regn 3941 // y = XOP 0, x 3942 // Pattern 2: 3943 // x = ADD reg(killed), imm 3944 // y = XOP 0, x 3945 if (IsFwdFeederRegKilled || RegMO->isKill()) 3946 fixupIsDeadOrKill(DefMI, MI, RegMO->getReg()); 3947 // Pattern 3: 3948 // ForwardKilledOperandReg = ADD reg, imm 3949 // y = XOP 0, ForwardKilledOperandReg(killed) 3950 if (ForwardKilledOperandReg != ~0U) 3951 fixupIsDeadOrKill(DefMI, MI, ForwardKilledOperandReg); 3952 3953 LLVM_DEBUG(dbgs() << "With:\n"); 3954 LLVM_DEBUG(MI.dump()); 3955 3956 return true; 3957 } 3958 3959 bool PPCInstrInfo::transformToImmFormFedByLI(MachineInstr &MI, 3960 const ImmInstrInfo &III, 3961 unsigned ConstantOpNo, 3962 MachineInstr &DefMI) const { 3963 // DefMI must be LI or LI8. 3964 if ((DefMI.getOpcode() != PPC::LI && DefMI.getOpcode() != PPC::LI8) || 3965 !DefMI.getOperand(1).isImm()) 3966 return false; 3967 3968 // Get Imm operand and Sign-extend to 64-bits. 3969 int64_t Imm = SignExtend64<16>(DefMI.getOperand(1).getImm()); 3970 3971 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3972 bool PostRA = !MRI.isSSA(); 3973 // Exit early if we can't convert this. 3974 if ((ConstantOpNo != III.OpNoForForwarding) && !III.IsCommutative) 3975 return false; 3976 if (Imm % III.ImmMustBeMultipleOf) 3977 return false; 3978 if (III.TruncateImmTo) 3979 Imm &= ((1 << III.TruncateImmTo) - 1); 3980 if (III.SignedImm) { 3981 APInt ActualValue(64, Imm, true); 3982 if (!ActualValue.isSignedIntN(III.ImmWidth)) 3983 return false; 3984 } else { 3985 uint64_t UnsignedMax = (1 << III.ImmWidth) - 1; 3986 if ((uint64_t)Imm > UnsignedMax) 3987 return false; 3988 } 3989 3990 // If we're post-RA, the instructions don't agree on whether register zero is 3991 // special, we can transform this as long as the register operand that will 3992 // end up in the location where zero is special isn't R0. 3993 if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { 3994 unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig : 3995 III.ZeroIsSpecialNew + 1; 3996 Register OrigZeroReg = MI.getOperand(PosForOrigZero).getReg(); 3997 Register NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg(); 3998 // If R0 is in the operand where zero is special for the new instruction, 3999 // it is unsafe to transform if the constant operand isn't that operand. 4000 if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) && 4001 ConstantOpNo != III.ZeroIsSpecialNew) 4002 return false; 4003 if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) && 4004 ConstantOpNo != PosForOrigZero) 4005 return false; 4006 } 4007 4008 // Get killed info in case fixup needed after transformation. 4009 unsigned ForwardKilledOperandReg = ~0U; 4010 if (PostRA && MI.getOperand(ConstantOpNo).isKill()) 4011 ForwardKilledOperandReg = MI.getOperand(ConstantOpNo).getReg(); 4012 4013 unsigned Opc = MI.getOpcode(); 4014 bool SpecialShift32 = Opc == PPC::SLW || Opc == PPC::SLW_rec || 4015 Opc == PPC::SRW || Opc == PPC::SRW_rec || 4016 Opc == PPC::SLW8 || Opc == PPC::SLW8_rec || 4017 Opc == PPC::SRW8 || Opc == PPC::SRW8_rec; 4018 bool SpecialShift64 = Opc == PPC::SLD || Opc == PPC::SLD_rec || 4019 Opc == PPC::SRD || Opc == PPC::SRD_rec; 4020 bool SetCR = Opc == PPC::SLW_rec || Opc == PPC::SRW_rec || 4021 Opc == PPC::SLD_rec || Opc == PPC::SRD_rec; 4022 bool RightShift = Opc == PPC::SRW || Opc == PPC::SRW_rec || Opc == PPC::SRD || 4023 Opc == PPC::SRD_rec; 4024 4025 MI.setDesc(get(III.ImmOpcode)); 4026 if (ConstantOpNo == III.OpNoForForwarding) { 4027 // Converting shifts to immediate form is a bit tricky since they may do 4028 // one of three things: 4029 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero 4030 // 2. If the shift amount is zero, the result is unchanged (save for maybe 4031 // setting CR0) 4032 // 3. If the shift amount is in [1, OpSize), it's just a shift 4033 if (SpecialShift32 || SpecialShift64) { 4034 LoadImmediateInfo LII; 4035 LII.Imm = 0; 4036 LII.SetCR = SetCR; 4037 LII.Is64Bit = SpecialShift64; 4038 uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F); 4039 if (Imm & (SpecialShift32 ? 0x20 : 0x40)) 4040 replaceInstrWithLI(MI, LII); 4041 // Shifts by zero don't change the value. If we don't need to set CR0, 4042 // just convert this to a COPY. Can't do this post-RA since we've already 4043 // cleaned up the copies. 4044 else if (!SetCR && ShAmt == 0 && !PostRA) { 4045 MI.RemoveOperand(2); 4046 MI.setDesc(get(PPC::COPY)); 4047 } else { 4048 // The 32 bit and 64 bit instructions are quite different. 4049 if (SpecialShift32) { 4050 // Left shifts use (N, 0, 31-N). 4051 // Right shifts use (32-N, N, 31) if 0 < N < 32. 4052 // use (0, 0, 31) if N == 0. 4053 uint64_t SH = ShAmt == 0 ? 0 : RightShift ? 32 - ShAmt : ShAmt; 4054 uint64_t MB = RightShift ? ShAmt : 0; 4055 uint64_t ME = RightShift ? 31 : 31 - ShAmt; 4056 replaceInstrOperandWithImm(MI, III.OpNoForForwarding, SH); 4057 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB) 4058 .addImm(ME); 4059 } else { 4060 // Left shifts use (N, 63-N). 4061 // Right shifts use (64-N, N) if 0 < N < 64. 4062 // use (0, 0) if N == 0. 4063 uint64_t SH = ShAmt == 0 ? 0 : RightShift ? 64 - ShAmt : ShAmt; 4064 uint64_t ME = RightShift ? ShAmt : 63 - ShAmt; 4065 replaceInstrOperandWithImm(MI, III.OpNoForForwarding, SH); 4066 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME); 4067 } 4068 } 4069 } else 4070 replaceInstrOperandWithImm(MI, ConstantOpNo, Imm); 4071 } 4072 // Convert commutative instructions (switch the operands and convert the 4073 // desired one to an immediate. 4074 else if (III.IsCommutative) { 4075 replaceInstrOperandWithImm(MI, ConstantOpNo, Imm); 4076 swapMIOperands(MI, ConstantOpNo, III.OpNoForForwarding); 4077 } else 4078 llvm_unreachable("Should have exited early!"); 4079 4080 // For instructions for which the constant register replaces a different 4081 // operand than where the immediate goes, we need to swap them. 4082 if (III.OpNoForForwarding != III.ImmOpNo) 4083 swapMIOperands(MI, III.OpNoForForwarding, III.ImmOpNo); 4084 4085 // If the special R0/X0 register index are different for original instruction 4086 // and new instruction, we need to fix up the register class in new 4087 // instruction. 4088 if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { 4089 if (III.ZeroIsSpecialNew) { 4090 // If operand at III.ZeroIsSpecialNew is physical reg(eg: ZERO/ZERO8), no 4091 // need to fix up register class. 4092 Register RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg(); 4093 if (Register::isVirtualRegister(RegToModify)) { 4094 const TargetRegisterClass *NewRC = 4095 MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ? 4096 &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass; 4097 MRI.setRegClass(RegToModify, NewRC); 4098 } 4099 } 4100 } 4101 4102 // Fix up killed/dead flag after transformation. 4103 // Pattern: 4104 // ForwardKilledOperandReg = LI imm 4105 // y = XOP reg, ForwardKilledOperandReg(killed) 4106 if (ForwardKilledOperandReg != ~0U) 4107 fixupIsDeadOrKill(DefMI, MI, ForwardKilledOperandReg); 4108 return true; 4109 } 4110 4111 const TargetRegisterClass * 4112 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const { 4113 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) 4114 return &PPC::VSRCRegClass; 4115 return RC; 4116 } 4117 4118 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) { 4119 return PPC::getRecordFormOpcode(Opcode); 4120 } 4121 4122 // This function returns true if the machine instruction 4123 // always outputs a value by sign-extending a 32 bit value, 4124 // i.e. 0 to 31-th bits are same as 32-th bit. 4125 static bool isSignExtendingOp(const MachineInstr &MI) { 4126 int Opcode = MI.getOpcode(); 4127 if (Opcode == PPC::LI || Opcode == PPC::LI8 || Opcode == PPC::LIS || 4128 Opcode == PPC::LIS8 || Opcode == PPC::SRAW || Opcode == PPC::SRAW_rec || 4129 Opcode == PPC::SRAWI || Opcode == PPC::SRAWI_rec || Opcode == PPC::LWA || 4130 Opcode == PPC::LWAX || Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 || 4131 Opcode == PPC::LHA || Opcode == PPC::LHAX || Opcode == PPC::LHA8 || 4132 Opcode == PPC::LHAX8 || Opcode == PPC::LBZ || Opcode == PPC::LBZX || 4133 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || Opcode == PPC::LBZU || 4134 Opcode == PPC::LBZUX || Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || 4135 Opcode == PPC::LHZ || Opcode == PPC::LHZX || Opcode == PPC::LHZ8 || 4136 Opcode == PPC::LHZX8 || Opcode == PPC::LHZU || Opcode == PPC::LHZUX || 4137 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || Opcode == PPC::EXTSB || 4138 Opcode == PPC::EXTSB_rec || Opcode == PPC::EXTSH || 4139 Opcode == PPC::EXTSH_rec || Opcode == PPC::EXTSB8 || 4140 Opcode == PPC::EXTSH8 || Opcode == PPC::EXTSW || 4141 Opcode == PPC::EXTSW_rec || Opcode == PPC::SETB || Opcode == PPC::SETB8 || 4142 Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 || 4143 Opcode == PPC::EXTSB8_32_64) 4144 return true; 4145 4146 if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33) 4147 return true; 4148 4149 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINM_rec || 4150 Opcode == PPC::RLWNM || Opcode == PPC::RLWNM_rec) && 4151 MI.getOperand(3).getImm() > 0 && 4152 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) 4153 return true; 4154 4155 return false; 4156 } 4157 4158 // This function returns true if the machine instruction 4159 // always outputs zeros in higher 32 bits. 4160 static bool isZeroExtendingOp(const MachineInstr &MI) { 4161 int Opcode = MI.getOpcode(); 4162 // The 16-bit immediate is sign-extended in li/lis. 4163 // If the most significant bit is zero, all higher bits are zero. 4164 if (Opcode == PPC::LI || Opcode == PPC::LI8 || 4165 Opcode == PPC::LIS || Opcode == PPC::LIS8) { 4166 int64_t Imm = MI.getOperand(1).getImm(); 4167 if (((uint64_t)Imm & ~0x7FFFuLL) == 0) 4168 return true; 4169 } 4170 4171 // We have some variations of rotate-and-mask instructions 4172 // that clear higher 32-bits. 4173 if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICL_rec || 4174 Opcode == PPC::RLDCL || Opcode == PPC::RLDCL_rec || 4175 Opcode == PPC::RLDICL_32_64) && 4176 MI.getOperand(3).getImm() >= 32) 4177 return true; 4178 4179 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDIC_rec) && 4180 MI.getOperand(3).getImm() >= 32 && 4181 MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm()) 4182 return true; 4183 4184 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINM_rec || 4185 Opcode == PPC::RLWNM || Opcode == PPC::RLWNM_rec || 4186 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) && 4187 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) 4188 return true; 4189 4190 // There are other instructions that clear higher 32-bits. 4191 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZW_rec || 4192 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZW_rec || 4193 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 || 4194 Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZD_rec || 4195 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZD_rec || 4196 Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW || Opcode == PPC::SLW || 4197 Opcode == PPC::SLW_rec || Opcode == PPC::SRW || Opcode == PPC::SRW_rec || 4198 Opcode == PPC::SLW8 || Opcode == PPC::SRW8 || Opcode == PPC::SLWI || 4199 Opcode == PPC::SLWI_rec || Opcode == PPC::SRWI || 4200 Opcode == PPC::SRWI_rec || Opcode == PPC::LWZ || Opcode == PPC::LWZX || 4201 Opcode == PPC::LWZU || Opcode == PPC::LWZUX || Opcode == PPC::LWBRX || 4202 Opcode == PPC::LHBRX || Opcode == PPC::LHZ || Opcode == PPC::LHZX || 4203 Opcode == PPC::LHZU || Opcode == PPC::LHZUX || Opcode == PPC::LBZ || 4204 Opcode == PPC::LBZX || Opcode == PPC::LBZU || Opcode == PPC::LBZUX || 4205 Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 || Opcode == PPC::LWZU8 || 4206 Opcode == PPC::LWZUX8 || Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 || 4207 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || Opcode == PPC::LHZU8 || 4208 Opcode == PPC::LHZUX8 || Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || 4209 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || 4210 Opcode == PPC::ANDI_rec || Opcode == PPC::ANDIS_rec || 4211 Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWI_rec || 4212 Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWI_rec || 4213 Opcode == PPC::MFVSRWZ) 4214 return true; 4215 4216 return false; 4217 } 4218 4219 // This function returns true if the input MachineInstr is a TOC save 4220 // instruction. 4221 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const { 4222 if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg()) 4223 return false; 4224 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 4225 unsigned StackOffset = MI.getOperand(1).getImm(); 4226 Register StackReg = MI.getOperand(2).getReg(); 4227 if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset) 4228 return true; 4229 4230 return false; 4231 } 4232 4233 // We limit the max depth to track incoming values of PHIs or binary ops 4234 // (e.g. AND) to avoid excessive cost. 4235 const unsigned MAX_DEPTH = 1; 4236 4237 bool 4238 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt, 4239 const unsigned Depth) const { 4240 const MachineFunction *MF = MI.getParent()->getParent(); 4241 const MachineRegisterInfo *MRI = &MF->getRegInfo(); 4242 4243 // If we know this instruction returns sign- or zero-extended result, 4244 // return true. 4245 if (SignExt ? isSignExtendingOp(MI): 4246 isZeroExtendingOp(MI)) 4247 return true; 4248 4249 switch (MI.getOpcode()) { 4250 case PPC::COPY: { 4251 Register SrcReg = MI.getOperand(1).getReg(); 4252 4253 // In both ELFv1 and v2 ABI, method parameters and the return value 4254 // are sign- or zero-extended. 4255 if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) { 4256 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); 4257 // We check the ZExt/SExt flags for a method parameter. 4258 if (MI.getParent()->getBasicBlock() == 4259 &MF->getFunction().getEntryBlock()) { 4260 Register VReg = MI.getOperand(0).getReg(); 4261 if (MF->getRegInfo().isLiveIn(VReg)) 4262 return SignExt ? FuncInfo->isLiveInSExt(VReg) : 4263 FuncInfo->isLiveInZExt(VReg); 4264 } 4265 4266 // For a method return value, we check the ZExt/SExt flags in attribute. 4267 // We assume the following code sequence for method call. 4268 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1 4269 // BL8_NOP @func,... 4270 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1 4271 // %5 = COPY %x3; G8RC:%5 4272 if (SrcReg == PPC::X3) { 4273 const MachineBasicBlock *MBB = MI.getParent(); 4274 MachineBasicBlock::const_instr_iterator II = 4275 MachineBasicBlock::const_instr_iterator(&MI); 4276 if (II != MBB->instr_begin() && 4277 (--II)->getOpcode() == PPC::ADJCALLSTACKUP) { 4278 const MachineInstr &CallMI = *(--II); 4279 if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) { 4280 const Function *CalleeFn = 4281 dyn_cast<Function>(CallMI.getOperand(0).getGlobal()); 4282 if (!CalleeFn) 4283 return false; 4284 const IntegerType *IntTy = 4285 dyn_cast<IntegerType>(CalleeFn->getReturnType()); 4286 const AttributeSet &Attrs = 4287 CalleeFn->getAttributes().getRetAttributes(); 4288 if (IntTy && IntTy->getBitWidth() <= 32) 4289 return Attrs.hasAttribute(SignExt ? Attribute::SExt : 4290 Attribute::ZExt); 4291 } 4292 } 4293 } 4294 } 4295 4296 // If this is a copy from another register, we recursively check source. 4297 if (!Register::isVirtualRegister(SrcReg)) 4298 return false; 4299 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 4300 if (SrcMI != NULL) 4301 return isSignOrZeroExtended(*SrcMI, SignExt, Depth); 4302 4303 return false; 4304 } 4305 4306 case PPC::ANDI_rec: 4307 case PPC::ANDIS_rec: 4308 case PPC::ORI: 4309 case PPC::ORIS: 4310 case PPC::XORI: 4311 case PPC::XORIS: 4312 case PPC::ANDI8_rec: 4313 case PPC::ANDIS8_rec: 4314 case PPC::ORI8: 4315 case PPC::ORIS8: 4316 case PPC::XORI8: 4317 case PPC::XORIS8: { 4318 // logical operation with 16-bit immediate does not change the upper bits. 4319 // So, we track the operand register as we do for register copy. 4320 Register SrcReg = MI.getOperand(1).getReg(); 4321 if (!Register::isVirtualRegister(SrcReg)) 4322 return false; 4323 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 4324 if (SrcMI != NULL) 4325 return isSignOrZeroExtended(*SrcMI, SignExt, Depth); 4326 4327 return false; 4328 } 4329 4330 // If all incoming values are sign-/zero-extended, 4331 // the output of OR, ISEL or PHI is also sign-/zero-extended. 4332 case PPC::OR: 4333 case PPC::OR8: 4334 case PPC::ISEL: 4335 case PPC::PHI: { 4336 if (Depth >= MAX_DEPTH) 4337 return false; 4338 4339 // The input registers for PHI are operand 1, 3, ... 4340 // The input registers for others are operand 1 and 2. 4341 unsigned E = 3, D = 1; 4342 if (MI.getOpcode() == PPC::PHI) { 4343 E = MI.getNumOperands(); 4344 D = 2; 4345 } 4346 4347 for (unsigned I = 1; I != E; I += D) { 4348 if (MI.getOperand(I).isReg()) { 4349 Register SrcReg = MI.getOperand(I).getReg(); 4350 if (!Register::isVirtualRegister(SrcReg)) 4351 return false; 4352 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 4353 if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1)) 4354 return false; 4355 } 4356 else 4357 return false; 4358 } 4359 return true; 4360 } 4361 4362 // If at least one of the incoming values of an AND is zero extended 4363 // then the output is also zero-extended. If both of the incoming values 4364 // are sign-extended then the output is also sign extended. 4365 case PPC::AND: 4366 case PPC::AND8: { 4367 if (Depth >= MAX_DEPTH) 4368 return false; 4369 4370 assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg()); 4371 4372 Register SrcReg1 = MI.getOperand(1).getReg(); 4373 Register SrcReg2 = MI.getOperand(2).getReg(); 4374 4375 if (!Register::isVirtualRegister(SrcReg1) || 4376 !Register::isVirtualRegister(SrcReg2)) 4377 return false; 4378 4379 const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1); 4380 const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2); 4381 if (!MISrc1 || !MISrc2) 4382 return false; 4383 4384 if(SignExt) 4385 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) && 4386 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); 4387 else 4388 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) || 4389 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); 4390 } 4391 4392 default: 4393 break; 4394 } 4395 return false; 4396 } 4397 4398 bool PPCInstrInfo::isBDNZ(unsigned Opcode) const { 4399 return (Opcode == (Subtarget.isPPC64() ? PPC::BDNZ8 : PPC::BDNZ)); 4400 } 4401 4402 namespace { 4403 class PPCPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo { 4404 MachineInstr *Loop, *EndLoop, *LoopCount; 4405 MachineFunction *MF; 4406 const TargetInstrInfo *TII; 4407 int64_t TripCount; 4408 4409 public: 4410 PPCPipelinerLoopInfo(MachineInstr *Loop, MachineInstr *EndLoop, 4411 MachineInstr *LoopCount) 4412 : Loop(Loop), EndLoop(EndLoop), LoopCount(LoopCount), 4413 MF(Loop->getParent()->getParent()), 4414 TII(MF->getSubtarget().getInstrInfo()) { 4415 // Inspect the Loop instruction up-front, as it may be deleted when we call 4416 // createTripCountGreaterCondition. 4417 if (LoopCount->getOpcode() == PPC::LI8 || LoopCount->getOpcode() == PPC::LI) 4418 TripCount = LoopCount->getOperand(1).getImm(); 4419 else 4420 TripCount = -1; 4421 } 4422 4423 bool shouldIgnoreForPipelining(const MachineInstr *MI) const override { 4424 // Only ignore the terminator. 4425 return MI == EndLoop; 4426 } 4427 4428 Optional<bool> 4429 createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB, 4430 SmallVectorImpl<MachineOperand> &Cond) override { 4431 if (TripCount == -1) { 4432 // Since BDZ/BDZ8 that we will insert will also decrease the ctr by 1, 4433 // so we don't need to generate any thing here. 4434 Cond.push_back(MachineOperand::CreateImm(0)); 4435 Cond.push_back(MachineOperand::CreateReg( 4436 MF->getSubtarget<PPCSubtarget>().isPPC64() ? PPC::CTR8 : PPC::CTR, 4437 true)); 4438 return {}; 4439 } 4440 4441 return TripCount > TC; 4442 } 4443 4444 void setPreheader(MachineBasicBlock *NewPreheader) override { 4445 // Do nothing. We want the LOOP setup instruction to stay in the *old* 4446 // preheader, so we can use BDZ in the prologs to adapt the loop trip count. 4447 } 4448 4449 void adjustTripCount(int TripCountAdjust) override { 4450 // If the loop trip count is a compile-time value, then just change the 4451 // value. 4452 if (LoopCount->getOpcode() == PPC::LI8 || 4453 LoopCount->getOpcode() == PPC::LI) { 4454 int64_t TripCount = LoopCount->getOperand(1).getImm() + TripCountAdjust; 4455 LoopCount->getOperand(1).setImm(TripCount); 4456 return; 4457 } 4458 4459 // Since BDZ/BDZ8 that we will insert will also decrease the ctr by 1, 4460 // so we don't need to generate any thing here. 4461 } 4462 4463 void disposed() override { 4464 Loop->eraseFromParent(); 4465 // Ensure the loop setup instruction is deleted too. 4466 LoopCount->eraseFromParent(); 4467 } 4468 }; 4469 } // namespace 4470 4471 std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo> 4472 PPCInstrInfo::analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const { 4473 // We really "analyze" only hardware loops right now. 4474 MachineBasicBlock::iterator I = LoopBB->getFirstTerminator(); 4475 MachineBasicBlock *Preheader = *LoopBB->pred_begin(); 4476 if (Preheader == LoopBB) 4477 Preheader = *std::next(LoopBB->pred_begin()); 4478 MachineFunction *MF = Preheader->getParent(); 4479 4480 if (I != LoopBB->end() && isBDNZ(I->getOpcode())) { 4481 SmallPtrSet<MachineBasicBlock *, 8> Visited; 4482 if (MachineInstr *LoopInst = findLoopInstr(*Preheader, Visited)) { 4483 Register LoopCountReg = LoopInst->getOperand(0).getReg(); 4484 MachineRegisterInfo &MRI = MF->getRegInfo(); 4485 MachineInstr *LoopCount = MRI.getUniqueVRegDef(LoopCountReg); 4486 return std::make_unique<PPCPipelinerLoopInfo>(LoopInst, &*I, LoopCount); 4487 } 4488 } 4489 return nullptr; 4490 } 4491 4492 MachineInstr *PPCInstrInfo::findLoopInstr( 4493 MachineBasicBlock &PreHeader, 4494 SmallPtrSet<MachineBasicBlock *, 8> &Visited) const { 4495 4496 unsigned LOOPi = (Subtarget.isPPC64() ? PPC::MTCTR8loop : PPC::MTCTRloop); 4497 4498 // The loop set-up instruction should be in preheader 4499 for (auto &I : PreHeader.instrs()) 4500 if (I.getOpcode() == LOOPi) 4501 return &I; 4502 return nullptr; 4503 } 4504 4505 // Return true if get the base operand, byte offset of an instruction and the 4506 // memory width. Width is the size of memory that is being loaded/stored. 4507 bool PPCInstrInfo::getMemOperandWithOffsetWidth( 4508 const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset, 4509 unsigned &Width, const TargetRegisterInfo *TRI) const { 4510 if (!LdSt.mayLoadOrStore()) 4511 return false; 4512 4513 // Handle only loads/stores with base register followed by immediate offset. 4514 if (LdSt.getNumExplicitOperands() != 3) 4515 return false; 4516 if (!LdSt.getOperand(1).isImm() || !LdSt.getOperand(2).isReg()) 4517 return false; 4518 4519 if (!LdSt.hasOneMemOperand()) 4520 return false; 4521 4522 Width = (*LdSt.memoperands_begin())->getSize(); 4523 Offset = LdSt.getOperand(1).getImm(); 4524 BaseReg = &LdSt.getOperand(2); 4525 return true; 4526 } 4527 4528 bool PPCInstrInfo::areMemAccessesTriviallyDisjoint( 4529 const MachineInstr &MIa, const MachineInstr &MIb) const { 4530 assert(MIa.mayLoadOrStore() && "MIa must be a load or store."); 4531 assert(MIb.mayLoadOrStore() && "MIb must be a load or store."); 4532 4533 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() || 4534 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 4535 return false; 4536 4537 // Retrieve the base register, offset from the base register and width. Width 4538 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If 4539 // base registers are identical, and the offset of a lower memory access + 4540 // the width doesn't overlap the offset of a higher memory access, 4541 // then the memory accesses are different. 4542 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4543 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr; 4544 int64_t OffsetA = 0, OffsetB = 0; 4545 unsigned int WidthA = 0, WidthB = 0; 4546 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) && 4547 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) { 4548 if (BaseOpA->isIdenticalTo(*BaseOpB)) { 4549 int LowOffset = std::min(OffsetA, OffsetB); 4550 int HighOffset = std::max(OffsetA, OffsetB); 4551 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 4552 if (LowOffset + LowWidth <= HighOffset) 4553 return true; 4554 } 4555 } 4556 return false; 4557 } 4558