1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the PowerPC implementation of the TargetInstrInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "PPCInstrInfo.h" 15 #include "MCTargetDesc/PPCPredicates.h" 16 #include "PPC.h" 17 #include "PPCHazardRecognizers.h" 18 #include "PPCInstrBuilder.h" 19 #include "PPCMachineFunctionInfo.h" 20 #include "PPCTargetMachine.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/CodeGen/LiveIntervals.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineFunctionPass.h" 26 #include "llvm/CodeGen/MachineInstrBuilder.h" 27 #include "llvm/CodeGen/MachineMemOperand.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/CodeGen/PseudoSourceValue.h" 30 #include "llvm/CodeGen/ScheduleDAG.h" 31 #include "llvm/CodeGen/SlotIndexes.h" 32 #include "llvm/CodeGen/StackMaps.h" 33 #include "llvm/MC/MCAsmInfo.h" 34 #include "llvm/MC/MCInst.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/ErrorHandling.h" 38 #include "llvm/Support/TargetRegistry.h" 39 #include "llvm/Support/raw_ostream.h" 40 41 using namespace llvm; 42 43 #define DEBUG_TYPE "ppc-instr-info" 44 45 #define GET_INSTRMAP_INFO 46 #define GET_INSTRINFO_CTOR_DTOR 47 #include "PPCGenInstrInfo.inc" 48 49 STATISTIC(NumStoreSPILLVSRRCAsVec, 50 "Number of spillvsrrc spilled to stack as vec"); 51 STATISTIC(NumStoreSPILLVSRRCAsGpr, 52 "Number of spillvsrrc spilled to stack as gpr"); 53 STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc"); 54 STATISTIC(CmpIselsConverted, 55 "Number of ISELs that depend on comparison of constants converted"); 56 STATISTIC(MissedConvertibleImmediateInstrs, 57 "Number of compare-immediate instructions fed by constants"); 58 STATISTIC(NumRcRotatesConvertedToRcAnd, 59 "Number of record-form rotates converted to record-form andi"); 60 61 static cl:: 62 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, 63 cl::desc("Disable analysis for CTR loops")); 64 65 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", 66 cl::desc("Disable compare instruction optimization"), cl::Hidden); 67 68 static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", 69 cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), 70 cl::Hidden); 71 72 static cl::opt<bool> 73 UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden, 74 cl::desc("Use the old (incorrect) instruction latency calculation")); 75 76 // Pin the vtable to this file. 77 void PPCInstrInfo::anchor() {} 78 79 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) 80 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP, 81 /* CatchRetOpcode */ -1, 82 STI.isPPC64() ? PPC::BLR8 : PPC::BLR), 83 Subtarget(STI), RI(STI.getTargetMachine()) {} 84 85 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for 86 /// this target when scheduling the DAG. 87 ScheduleHazardRecognizer * 88 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, 89 const ScheduleDAG *DAG) const { 90 unsigned Directive = 91 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective(); 92 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || 93 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { 94 const InstrItineraryData *II = 95 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); 96 return new ScoreboardHazardRecognizer(II, DAG); 97 } 98 99 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); 100 } 101 102 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer 103 /// to use for this target when scheduling the DAG. 104 ScheduleHazardRecognizer * 105 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 106 const ScheduleDAG *DAG) const { 107 unsigned Directive = 108 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective(); 109 110 // FIXME: Leaving this as-is until we have POWER9 scheduling info 111 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) 112 return new PPCDispatchGroupSBHazardRecognizer(II, DAG); 113 114 // Most subtargets use a PPC970 recognizer. 115 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && 116 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { 117 assert(DAG->TII && "No InstrInfo?"); 118 119 return new PPCHazardRecognizer970(*DAG); 120 } 121 122 return new ScoreboardHazardRecognizer(II, DAG); 123 } 124 125 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 126 const MachineInstr &MI, 127 unsigned *PredCost) const { 128 if (!ItinData || UseOldLatencyCalc) 129 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost); 130 131 // The default implementation of getInstrLatency calls getStageLatency, but 132 // getStageLatency does not do the right thing for us. While we have 133 // itinerary, most cores are fully pipelined, and so the itineraries only 134 // express the first part of the pipeline, not every stage. Instead, we need 135 // to use the listed output operand cycle number (using operand 0 here, which 136 // is an output). 137 138 unsigned Latency = 1; 139 unsigned DefClass = MI.getDesc().getSchedClass(); 140 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 141 const MachineOperand &MO = MI.getOperand(i); 142 if (!MO.isReg() || !MO.isDef() || MO.isImplicit()) 143 continue; 144 145 int Cycle = ItinData->getOperandCycle(DefClass, i); 146 if (Cycle < 0) 147 continue; 148 149 Latency = std::max(Latency, (unsigned) Cycle); 150 } 151 152 return Latency; 153 } 154 155 int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 156 const MachineInstr &DefMI, unsigned DefIdx, 157 const MachineInstr &UseMI, 158 unsigned UseIdx) const { 159 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, 160 UseMI, UseIdx); 161 162 if (!DefMI.getParent()) 163 return Latency; 164 165 const MachineOperand &DefMO = DefMI.getOperand(DefIdx); 166 unsigned Reg = DefMO.getReg(); 167 168 bool IsRegCR; 169 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 170 const MachineRegisterInfo *MRI = 171 &DefMI.getParent()->getParent()->getRegInfo(); 172 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || 173 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); 174 } else { 175 IsRegCR = PPC::CRRCRegClass.contains(Reg) || 176 PPC::CRBITRCRegClass.contains(Reg); 177 } 178 179 if (UseMI.isBranch() && IsRegCR) { 180 if (Latency < 0) 181 Latency = getInstrLatency(ItinData, DefMI); 182 183 // On some cores, there is an additional delay between writing to a condition 184 // register, and using it from a branch. 185 unsigned Directive = Subtarget.getDarwinDirective(); 186 switch (Directive) { 187 default: break; 188 case PPC::DIR_7400: 189 case PPC::DIR_750: 190 case PPC::DIR_970: 191 case PPC::DIR_E5500: 192 case PPC::DIR_PWR4: 193 case PPC::DIR_PWR5: 194 case PPC::DIR_PWR5X: 195 case PPC::DIR_PWR6: 196 case PPC::DIR_PWR6X: 197 case PPC::DIR_PWR7: 198 case PPC::DIR_PWR8: 199 // FIXME: Is this needed for POWER9? 200 Latency += 2; 201 break; 202 } 203 } 204 205 return Latency; 206 } 207 208 // This function does not list all associative and commutative operations, but 209 // only those worth feeding through the machine combiner in an attempt to 210 // reduce the critical path. Mostly, this means floating-point operations, 211 // because they have high latencies (compared to other operations, such and 212 // and/or, which are also associative and commutative, but have low latencies). 213 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const { 214 switch (Inst.getOpcode()) { 215 // FP Add: 216 case PPC::FADD: 217 case PPC::FADDS: 218 // FP Multiply: 219 case PPC::FMUL: 220 case PPC::FMULS: 221 // Altivec Add: 222 case PPC::VADDFP: 223 // VSX Add: 224 case PPC::XSADDDP: 225 case PPC::XVADDDP: 226 case PPC::XVADDSP: 227 case PPC::XSADDSP: 228 // VSX Multiply: 229 case PPC::XSMULDP: 230 case PPC::XVMULDP: 231 case PPC::XVMULSP: 232 case PPC::XSMULSP: 233 // QPX Add: 234 case PPC::QVFADD: 235 case PPC::QVFADDS: 236 case PPC::QVFADDSs: 237 // QPX Multiply: 238 case PPC::QVFMUL: 239 case PPC::QVFMULS: 240 case PPC::QVFMULSs: 241 return true; 242 default: 243 return false; 244 } 245 } 246 247 bool PPCInstrInfo::getMachineCombinerPatterns( 248 MachineInstr &Root, 249 SmallVectorImpl<MachineCombinerPattern> &Patterns) const { 250 // Using the machine combiner in this way is potentially expensive, so 251 // restrict to when aggressive optimizations are desired. 252 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive) 253 return false; 254 255 // FP reassociation is only legal when we don't need strict IEEE semantics. 256 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath) 257 return false; 258 259 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns); 260 } 261 262 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. 263 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, 264 unsigned &SrcReg, unsigned &DstReg, 265 unsigned &SubIdx) const { 266 switch (MI.getOpcode()) { 267 default: return false; 268 case PPC::EXTSW: 269 case PPC::EXTSW_32: 270 case PPC::EXTSW_32_64: 271 SrcReg = MI.getOperand(1).getReg(); 272 DstReg = MI.getOperand(0).getReg(); 273 SubIdx = PPC::sub_32; 274 return true; 275 } 276 } 277 278 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 279 int &FrameIndex) const { 280 // Note: This list must be kept consistent with LoadRegFromStackSlot. 281 switch (MI.getOpcode()) { 282 default: break; 283 case PPC::LD: 284 case PPC::LWZ: 285 case PPC::LFS: 286 case PPC::LFD: 287 case PPC::RESTORE_CR: 288 case PPC::RESTORE_CRBIT: 289 case PPC::LVX: 290 case PPC::LXVD2X: 291 case PPC::LXV: 292 case PPC::QVLFDX: 293 case PPC::QVLFSXs: 294 case PPC::QVLFDXb: 295 case PPC::RESTORE_VRSAVE: 296 case PPC::SPILLTOVSR_LD: 297 // Check for the operands added by addFrameReference (the immediate is the 298 // offset which defaults to 0). 299 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 300 MI.getOperand(2).isFI()) { 301 FrameIndex = MI.getOperand(2).getIndex(); 302 return MI.getOperand(0).getReg(); 303 } 304 break; 305 } 306 return 0; 307 } 308 309 // For opcodes with the ReMaterializable flag set, this function is called to 310 // verify the instruction is really rematable. 311 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, 312 AliasAnalysis *AA) const { 313 switch (MI.getOpcode()) { 314 default: 315 // This function should only be called for opcodes with the ReMaterializable 316 // flag set. 317 llvm_unreachable("Unknown rematerializable operation!"); 318 break; 319 case PPC::LI: 320 case PPC::LI8: 321 case PPC::LIS: 322 case PPC::LIS8: 323 case PPC::QVGPCI: 324 case PPC::ADDIStocHA: 325 case PPC::ADDItocL: 326 case PPC::LOAD_STACK_GUARD: 327 return true; 328 } 329 return false; 330 } 331 332 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 333 int &FrameIndex) const { 334 // Note: This list must be kept consistent with StoreRegToStackSlot. 335 switch (MI.getOpcode()) { 336 default: break; 337 case PPC::STD: 338 case PPC::STW: 339 case PPC::STFS: 340 case PPC::STFD: 341 case PPC::SPILL_CR: 342 case PPC::SPILL_CRBIT: 343 case PPC::STVX: 344 case PPC::STXVD2X: 345 case PPC::STXV: 346 case PPC::QVSTFDX: 347 case PPC::QVSTFSXs: 348 case PPC::QVSTFDXb: 349 case PPC::SPILL_VRSAVE: 350 case PPC::SPILLTOVSR_ST: 351 // Check for the operands added by addFrameReference (the immediate is the 352 // offset which defaults to 0). 353 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 354 MI.getOperand(2).isFI()) { 355 FrameIndex = MI.getOperand(2).getIndex(); 356 return MI.getOperand(0).getReg(); 357 } 358 break; 359 } 360 return 0; 361 } 362 363 MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 364 unsigned OpIdx1, 365 unsigned OpIdx2) const { 366 MachineFunction &MF = *MI.getParent()->getParent(); 367 368 // Normal instructions can be commuted the obvious way. 369 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo) 370 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 371 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a 372 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because 373 // changing the relative order of the mask operands might change what happens 374 // to the high-bits of the mask (and, thus, the result). 375 376 // Cannot commute if it has a non-zero rotate count. 377 if (MI.getOperand(3).getImm() != 0) 378 return nullptr; 379 380 // If we have a zero rotate count, we have: 381 // M = mask(MB,ME) 382 // Op0 = (Op1 & ~M) | (Op2 & M) 383 // Change this to: 384 // M = mask((ME+1)&31, (MB-1)&31) 385 // Op0 = (Op2 & ~M) | (Op1 & M) 386 387 // Swap op1/op2 388 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) && 389 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo."); 390 unsigned Reg0 = MI.getOperand(0).getReg(); 391 unsigned Reg1 = MI.getOperand(1).getReg(); 392 unsigned Reg2 = MI.getOperand(2).getReg(); 393 unsigned SubReg1 = MI.getOperand(1).getSubReg(); 394 unsigned SubReg2 = MI.getOperand(2).getSubReg(); 395 bool Reg1IsKill = MI.getOperand(1).isKill(); 396 bool Reg2IsKill = MI.getOperand(2).isKill(); 397 bool ChangeReg0 = false; 398 // If machine instrs are no longer in two-address forms, update 399 // destination register as well. 400 if (Reg0 == Reg1) { 401 // Must be two address instruction! 402 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) && 403 "Expecting a two-address instruction!"); 404 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); 405 Reg2IsKill = false; 406 ChangeReg0 = true; 407 } 408 409 // Masks. 410 unsigned MB = MI.getOperand(4).getImm(); 411 unsigned ME = MI.getOperand(5).getImm(); 412 413 // We can't commute a trivial mask (there is no way to represent an all-zero 414 // mask). 415 if (MB == 0 && ME == 31) 416 return nullptr; 417 418 if (NewMI) { 419 // Create a new instruction. 420 unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg(); 421 bool Reg0IsDead = MI.getOperand(0).isDead(); 422 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc()) 423 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) 424 .addReg(Reg2, getKillRegState(Reg2IsKill)) 425 .addReg(Reg1, getKillRegState(Reg1IsKill)) 426 .addImm((ME + 1) & 31) 427 .addImm((MB - 1) & 31); 428 } 429 430 if (ChangeReg0) { 431 MI.getOperand(0).setReg(Reg2); 432 MI.getOperand(0).setSubReg(SubReg2); 433 } 434 MI.getOperand(2).setReg(Reg1); 435 MI.getOperand(1).setReg(Reg2); 436 MI.getOperand(2).setSubReg(SubReg1); 437 MI.getOperand(1).setSubReg(SubReg2); 438 MI.getOperand(2).setIsKill(Reg1IsKill); 439 MI.getOperand(1).setIsKill(Reg2IsKill); 440 441 // Swap the mask around. 442 MI.getOperand(4).setImm((ME + 1) & 31); 443 MI.getOperand(5).setImm((MB - 1) & 31); 444 return &MI; 445 } 446 447 bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1, 448 unsigned &SrcOpIdx2) const { 449 // For VSX A-Type FMA instructions, it is the first two operands that can be 450 // commuted, however, because the non-encoded tied input operand is listed 451 // first, the operands to swap are actually the second and third. 452 453 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode()); 454 if (AltOpc == -1) 455 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 456 457 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1 458 // and SrcOpIdx2. 459 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3); 460 } 461 462 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, 463 MachineBasicBlock::iterator MI) const { 464 // This function is used for scheduling, and the nop wanted here is the type 465 // that terminates dispatch groups on the POWER cores. 466 unsigned Directive = Subtarget.getDarwinDirective(); 467 unsigned Opcode; 468 switch (Directive) { 469 default: Opcode = PPC::NOP; break; 470 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; 471 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; 472 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */ 473 // FIXME: Update when POWER9 scheduling model is ready. 474 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break; 475 } 476 477 DebugLoc DL; 478 BuildMI(MBB, MI, DL, get(Opcode)); 479 } 480 481 /// Return the noop instruction to use for a noop. 482 void PPCInstrInfo::getNoop(MCInst &NopInst) const { 483 NopInst.setOpcode(PPC::NOP); 484 } 485 486 // Branch analysis. 487 // Note: If the condition register is set to CTR or CTR8 then this is a 488 // BDNZ (imm == 1) or BDZ (imm == 0) branch. 489 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 490 MachineBasicBlock *&TBB, 491 MachineBasicBlock *&FBB, 492 SmallVectorImpl<MachineOperand> &Cond, 493 bool AllowModify) const { 494 bool isPPC64 = Subtarget.isPPC64(); 495 496 // If the block has no terminators, it just falls into the block after it. 497 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 498 if (I == MBB.end()) 499 return false; 500 501 if (!isUnpredicatedTerminator(*I)) 502 return false; 503 504 if (AllowModify) { 505 // If the BB ends with an unconditional branch to the fallthrough BB, 506 // we eliminate the branch instruction. 507 if (I->getOpcode() == PPC::B && 508 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { 509 I->eraseFromParent(); 510 511 // We update iterator after deleting the last branch. 512 I = MBB.getLastNonDebugInstr(); 513 if (I == MBB.end() || !isUnpredicatedTerminator(*I)) 514 return false; 515 } 516 } 517 518 // Get the last instruction in the block. 519 MachineInstr &LastInst = *I; 520 521 // If there is only one terminator instruction, process it. 522 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { 523 if (LastInst.getOpcode() == PPC::B) { 524 if (!LastInst.getOperand(0).isMBB()) 525 return true; 526 TBB = LastInst.getOperand(0).getMBB(); 527 return false; 528 } else if (LastInst.getOpcode() == PPC::BCC) { 529 if (!LastInst.getOperand(2).isMBB()) 530 return true; 531 // Block ends with fall-through condbranch. 532 TBB = LastInst.getOperand(2).getMBB(); 533 Cond.push_back(LastInst.getOperand(0)); 534 Cond.push_back(LastInst.getOperand(1)); 535 return false; 536 } else if (LastInst.getOpcode() == PPC::BC) { 537 if (!LastInst.getOperand(1).isMBB()) 538 return true; 539 // Block ends with fall-through condbranch. 540 TBB = LastInst.getOperand(1).getMBB(); 541 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 542 Cond.push_back(LastInst.getOperand(0)); 543 return false; 544 } else if (LastInst.getOpcode() == PPC::BCn) { 545 if (!LastInst.getOperand(1).isMBB()) 546 return true; 547 // Block ends with fall-through condbranch. 548 TBB = LastInst.getOperand(1).getMBB(); 549 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 550 Cond.push_back(LastInst.getOperand(0)); 551 return false; 552 } else if (LastInst.getOpcode() == PPC::BDNZ8 || 553 LastInst.getOpcode() == PPC::BDNZ) { 554 if (!LastInst.getOperand(0).isMBB()) 555 return true; 556 if (DisableCTRLoopAnal) 557 return true; 558 TBB = LastInst.getOperand(0).getMBB(); 559 Cond.push_back(MachineOperand::CreateImm(1)); 560 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 561 true)); 562 return false; 563 } else if (LastInst.getOpcode() == PPC::BDZ8 || 564 LastInst.getOpcode() == PPC::BDZ) { 565 if (!LastInst.getOperand(0).isMBB()) 566 return true; 567 if (DisableCTRLoopAnal) 568 return true; 569 TBB = LastInst.getOperand(0).getMBB(); 570 Cond.push_back(MachineOperand::CreateImm(0)); 571 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 572 true)); 573 return false; 574 } 575 576 // Otherwise, don't know what this is. 577 return true; 578 } 579 580 // Get the instruction before it if it's a terminator. 581 MachineInstr &SecondLastInst = *I; 582 583 // If there are three terminators, we don't know what sort of block this is. 584 if (I != MBB.begin() && isUnpredicatedTerminator(*--I)) 585 return true; 586 587 // If the block ends with PPC::B and PPC:BCC, handle it. 588 if (SecondLastInst.getOpcode() == PPC::BCC && 589 LastInst.getOpcode() == PPC::B) { 590 if (!SecondLastInst.getOperand(2).isMBB() || 591 !LastInst.getOperand(0).isMBB()) 592 return true; 593 TBB = SecondLastInst.getOperand(2).getMBB(); 594 Cond.push_back(SecondLastInst.getOperand(0)); 595 Cond.push_back(SecondLastInst.getOperand(1)); 596 FBB = LastInst.getOperand(0).getMBB(); 597 return false; 598 } else if (SecondLastInst.getOpcode() == PPC::BC && 599 LastInst.getOpcode() == PPC::B) { 600 if (!SecondLastInst.getOperand(1).isMBB() || 601 !LastInst.getOperand(0).isMBB()) 602 return true; 603 TBB = SecondLastInst.getOperand(1).getMBB(); 604 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 605 Cond.push_back(SecondLastInst.getOperand(0)); 606 FBB = LastInst.getOperand(0).getMBB(); 607 return false; 608 } else if (SecondLastInst.getOpcode() == PPC::BCn && 609 LastInst.getOpcode() == PPC::B) { 610 if (!SecondLastInst.getOperand(1).isMBB() || 611 !LastInst.getOperand(0).isMBB()) 612 return true; 613 TBB = SecondLastInst.getOperand(1).getMBB(); 614 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 615 Cond.push_back(SecondLastInst.getOperand(0)); 616 FBB = LastInst.getOperand(0).getMBB(); 617 return false; 618 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 || 619 SecondLastInst.getOpcode() == PPC::BDNZ) && 620 LastInst.getOpcode() == PPC::B) { 621 if (!SecondLastInst.getOperand(0).isMBB() || 622 !LastInst.getOperand(0).isMBB()) 623 return true; 624 if (DisableCTRLoopAnal) 625 return true; 626 TBB = SecondLastInst.getOperand(0).getMBB(); 627 Cond.push_back(MachineOperand::CreateImm(1)); 628 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 629 true)); 630 FBB = LastInst.getOperand(0).getMBB(); 631 return false; 632 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 || 633 SecondLastInst.getOpcode() == PPC::BDZ) && 634 LastInst.getOpcode() == PPC::B) { 635 if (!SecondLastInst.getOperand(0).isMBB() || 636 !LastInst.getOperand(0).isMBB()) 637 return true; 638 if (DisableCTRLoopAnal) 639 return true; 640 TBB = SecondLastInst.getOperand(0).getMBB(); 641 Cond.push_back(MachineOperand::CreateImm(0)); 642 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 643 true)); 644 FBB = LastInst.getOperand(0).getMBB(); 645 return false; 646 } 647 648 // If the block ends with two PPC:Bs, handle it. The second one is not 649 // executed, so remove it. 650 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) { 651 if (!SecondLastInst.getOperand(0).isMBB()) 652 return true; 653 TBB = SecondLastInst.getOperand(0).getMBB(); 654 I = LastInst; 655 if (AllowModify) 656 I->eraseFromParent(); 657 return false; 658 } 659 660 // Otherwise, can't handle this. 661 return true; 662 } 663 664 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB, 665 int *BytesRemoved) const { 666 assert(!BytesRemoved && "code size not handled"); 667 668 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 669 if (I == MBB.end()) 670 return 0; 671 672 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && 673 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 674 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 675 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 676 return 0; 677 678 // Remove the branch. 679 I->eraseFromParent(); 680 681 I = MBB.end(); 682 683 if (I == MBB.begin()) return 1; 684 --I; 685 if (I->getOpcode() != PPC::BCC && 686 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 687 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 688 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 689 return 1; 690 691 // Remove the branch. 692 I->eraseFromParent(); 693 return 2; 694 } 695 696 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB, 697 MachineBasicBlock *TBB, 698 MachineBasicBlock *FBB, 699 ArrayRef<MachineOperand> Cond, 700 const DebugLoc &DL, 701 int *BytesAdded) const { 702 // Shouldn't be a fall through. 703 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 704 assert((Cond.size() == 2 || Cond.size() == 0) && 705 "PPC branch conditions have two components!"); 706 assert(!BytesAdded && "code size not handled"); 707 708 bool isPPC64 = Subtarget.isPPC64(); 709 710 // One-way branch. 711 if (!FBB) { 712 if (Cond.empty()) // Unconditional branch 713 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); 714 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 715 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 716 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 717 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 718 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 719 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); 720 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 721 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); 722 else // Conditional branch 723 BuildMI(&MBB, DL, get(PPC::BCC)) 724 .addImm(Cond[0].getImm()) 725 .add(Cond[1]) 726 .addMBB(TBB); 727 return 1; 728 } 729 730 // Two-way Conditional Branch. 731 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 732 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 733 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 734 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 735 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 736 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); 737 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 738 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); 739 else 740 BuildMI(&MBB, DL, get(PPC::BCC)) 741 .addImm(Cond[0].getImm()) 742 .add(Cond[1]) 743 .addMBB(TBB); 744 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); 745 return 2; 746 } 747 748 // Select analysis. 749 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 750 ArrayRef<MachineOperand> Cond, 751 unsigned TrueReg, unsigned FalseReg, 752 int &CondCycles, int &TrueCycles, int &FalseCycles) const { 753 if (Cond.size() != 2) 754 return false; 755 756 // If this is really a bdnz-like condition, then it cannot be turned into a 757 // select. 758 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 759 return false; 760 761 // Check register classes. 762 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 763 const TargetRegisterClass *RC = 764 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 765 if (!RC) 766 return false; 767 768 // isel is for regular integer GPRs only. 769 if (!PPC::GPRCRegClass.hasSubClassEq(RC) && 770 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && 771 !PPC::G8RCRegClass.hasSubClassEq(RC) && 772 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) 773 return false; 774 775 // FIXME: These numbers are for the A2, how well they work for other cores is 776 // an open question. On the A2, the isel instruction has a 2-cycle latency 777 // but single-cycle throughput. These numbers are used in combination with 778 // the MispredictPenalty setting from the active SchedMachineModel. 779 CondCycles = 1; 780 TrueCycles = 1; 781 FalseCycles = 1; 782 783 return true; 784 } 785 786 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, 787 MachineBasicBlock::iterator MI, 788 const DebugLoc &dl, unsigned DestReg, 789 ArrayRef<MachineOperand> Cond, unsigned TrueReg, 790 unsigned FalseReg) const { 791 assert(Cond.size() == 2 && 792 "PPC branch conditions have two components!"); 793 794 // Get the register classes. 795 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 796 const TargetRegisterClass *RC = 797 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 798 assert(RC && "TrueReg and FalseReg must have overlapping register classes"); 799 800 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || 801 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); 802 assert((Is64Bit || 803 PPC::GPRCRegClass.hasSubClassEq(RC) || 804 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && 805 "isel is for regular integer GPRs only"); 806 807 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; 808 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm()); 809 810 unsigned SubIdx = 0; 811 bool SwapOps = false; 812 switch (SelectPred) { 813 case PPC::PRED_EQ: 814 case PPC::PRED_EQ_MINUS: 815 case PPC::PRED_EQ_PLUS: 816 SubIdx = PPC::sub_eq; SwapOps = false; break; 817 case PPC::PRED_NE: 818 case PPC::PRED_NE_MINUS: 819 case PPC::PRED_NE_PLUS: 820 SubIdx = PPC::sub_eq; SwapOps = true; break; 821 case PPC::PRED_LT: 822 case PPC::PRED_LT_MINUS: 823 case PPC::PRED_LT_PLUS: 824 SubIdx = PPC::sub_lt; SwapOps = false; break; 825 case PPC::PRED_GE: 826 case PPC::PRED_GE_MINUS: 827 case PPC::PRED_GE_PLUS: 828 SubIdx = PPC::sub_lt; SwapOps = true; break; 829 case PPC::PRED_GT: 830 case PPC::PRED_GT_MINUS: 831 case PPC::PRED_GT_PLUS: 832 SubIdx = PPC::sub_gt; SwapOps = false; break; 833 case PPC::PRED_LE: 834 case PPC::PRED_LE_MINUS: 835 case PPC::PRED_LE_PLUS: 836 SubIdx = PPC::sub_gt; SwapOps = true; break; 837 case PPC::PRED_UN: 838 case PPC::PRED_UN_MINUS: 839 case PPC::PRED_UN_PLUS: 840 SubIdx = PPC::sub_un; SwapOps = false; break; 841 case PPC::PRED_NU: 842 case PPC::PRED_NU_MINUS: 843 case PPC::PRED_NU_PLUS: 844 SubIdx = PPC::sub_un; SwapOps = true; break; 845 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; 846 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; 847 } 848 849 unsigned FirstReg = SwapOps ? FalseReg : TrueReg, 850 SecondReg = SwapOps ? TrueReg : FalseReg; 851 852 // The first input register of isel cannot be r0. If it is a member 853 // of a register class that can be r0, then copy it first (the 854 // register allocator should eliminate the copy). 855 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || 856 MRI.getRegClass(FirstReg)->contains(PPC::X0)) { 857 const TargetRegisterClass *FirstRC = 858 MRI.getRegClass(FirstReg)->contains(PPC::X0) ? 859 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; 860 unsigned OldFirstReg = FirstReg; 861 FirstReg = MRI.createVirtualRegister(FirstRC); 862 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) 863 .addReg(OldFirstReg); 864 } 865 866 BuildMI(MBB, MI, dl, get(OpCode), DestReg) 867 .addReg(FirstReg).addReg(SecondReg) 868 .addReg(Cond[1].getReg(), 0, SubIdx); 869 } 870 871 static unsigned getCRBitValue(unsigned CRBit) { 872 unsigned Ret = 4; 873 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || 874 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || 875 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || 876 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) 877 Ret = 3; 878 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || 879 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || 880 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || 881 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) 882 Ret = 2; 883 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || 884 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || 885 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || 886 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) 887 Ret = 1; 888 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || 889 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || 890 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || 891 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) 892 Ret = 0; 893 894 assert(Ret != 4 && "Invalid CR bit register"); 895 return Ret; 896 } 897 898 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 899 MachineBasicBlock::iterator I, 900 const DebugLoc &DL, unsigned DestReg, 901 unsigned SrcReg, bool KillSrc) const { 902 // We can end up with self copies and similar things as a result of VSX copy 903 // legalization. Promote them here. 904 const TargetRegisterInfo *TRI = &getRegisterInfo(); 905 if (PPC::F8RCRegClass.contains(DestReg) && 906 PPC::VSRCRegClass.contains(SrcReg)) { 907 unsigned SuperReg = 908 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); 909 910 if (VSXSelfCopyCrash && SrcReg == SuperReg) 911 llvm_unreachable("nop VSX copy"); 912 913 DestReg = SuperReg; 914 } else if (PPC::F8RCRegClass.contains(SrcReg) && 915 PPC::VSRCRegClass.contains(DestReg)) { 916 unsigned SuperReg = 917 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); 918 919 if (VSXSelfCopyCrash && DestReg == SuperReg) 920 llvm_unreachable("nop VSX copy"); 921 922 SrcReg = SuperReg; 923 } 924 925 // Different class register copy 926 if (PPC::CRBITRCRegClass.contains(SrcReg) && 927 PPC::GPRCRegClass.contains(DestReg)) { 928 unsigned CRReg = getCRFromCRBit(SrcReg); 929 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg); 930 getKillRegState(KillSrc); 931 // Rotate the CR bit in the CR fields to be the least significant bit and 932 // then mask with 0x1 (MB = ME = 31). 933 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) 934 .addReg(DestReg, RegState::Kill) 935 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) 936 .addImm(31) 937 .addImm(31); 938 return; 939 } else if (PPC::CRRCRegClass.contains(SrcReg) && 940 PPC::G8RCRegClass.contains(DestReg)) { 941 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg); 942 getKillRegState(KillSrc); 943 return; 944 } else if (PPC::CRRCRegClass.contains(SrcReg) && 945 PPC::GPRCRegClass.contains(DestReg)) { 946 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); 947 getKillRegState(KillSrc); 948 return; 949 } else if (PPC::G8RCRegClass.contains(SrcReg) && 950 PPC::VSFRCRegClass.contains(DestReg)) { 951 BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg); 952 NumGPRtoVSRSpill++; 953 getKillRegState(KillSrc); 954 return; 955 } else if (PPC::VSFRCRegClass.contains(SrcReg) && 956 PPC::G8RCRegClass.contains(DestReg)) { 957 BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg); 958 getKillRegState(KillSrc); 959 return; 960 } 961 962 unsigned Opc; 963 if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) 964 Opc = PPC::OR; 965 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) 966 Opc = PPC::OR8; 967 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) 968 Opc = PPC::FMR; 969 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) 970 Opc = PPC::MCRF; 971 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) 972 Opc = PPC::VOR; 973 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) 974 // There are two different ways this can be done: 975 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only 976 // issue in VSU pipeline 0. 977 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but 978 // can go to either pipeline. 979 // We'll always use xxlor here, because in practically all cases where 980 // copies are generated, they are close enough to some use that the 981 // lower-latency form is preferable. 982 Opc = PPC::XXLOR; 983 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || 984 PPC::VSSRCRegClass.contains(DestReg, SrcReg)) 985 Opc = PPC::XXLORf; 986 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) 987 Opc = PPC::QVFMR; 988 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) 989 Opc = PPC::QVFMRs; 990 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) 991 Opc = PPC::QVFMRb; 992 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) 993 Opc = PPC::CROR; 994 else 995 llvm_unreachable("Impossible reg-to-reg copy"); 996 997 const MCInstrDesc &MCID = get(Opc); 998 if (MCID.getNumOperands() == 3) 999 BuildMI(MBB, I, DL, MCID, DestReg) 1000 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); 1001 else 1002 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); 1003 } 1004 1005 // This function returns true if a CR spill is necessary and false otherwise. 1006 bool 1007 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, 1008 unsigned SrcReg, bool isKill, 1009 int FrameIdx, 1010 const TargetRegisterClass *RC, 1011 SmallVectorImpl<MachineInstr*> &NewMIs, 1012 bool &NonRI, bool &SpillsVRS) const{ 1013 // Note: If additional store instructions are added here, 1014 // update isStoreToStackSlot. 1015 1016 DebugLoc DL; 1017 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 1018 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 1019 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) 1020 .addReg(SrcReg, 1021 getKillRegState(isKill)), 1022 FrameIdx)); 1023 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 1024 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 1025 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) 1026 .addReg(SrcReg, 1027 getKillRegState(isKill)), 1028 FrameIdx)); 1029 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 1030 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) 1031 .addReg(SrcReg, 1032 getKillRegState(isKill)), 1033 FrameIdx)); 1034 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 1035 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) 1036 .addReg(SrcReg, 1037 getKillRegState(isKill)), 1038 FrameIdx)); 1039 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 1040 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) 1041 .addReg(SrcReg, 1042 getKillRegState(isKill)), 1043 FrameIdx)); 1044 return true; 1045 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 1046 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) 1047 .addReg(SrcReg, 1048 getKillRegState(isKill)), 1049 FrameIdx)); 1050 return true; 1051 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1052 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) 1053 .addReg(SrcReg, 1054 getKillRegState(isKill)), 1055 FrameIdx)); 1056 NonRI = true; 1057 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1058 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXV : PPC::STXVD2X; 1059 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op)) 1060 .addReg(SrcReg, 1061 getKillRegState(isKill)), 1062 FrameIdx)); 1063 NonRI = true; 1064 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1065 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX; 1066 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc)) 1067 .addReg(SrcReg, 1068 getKillRegState(isKill)), 1069 FrameIdx)); 1070 NonRI = true; 1071 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1072 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX; 1073 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc)) 1074 .addReg(SrcReg, 1075 getKillRegState(isKill)), 1076 FrameIdx)); 1077 NonRI = true; 1078 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1079 assert(Subtarget.isDarwin() && 1080 "VRSAVE only needs spill/restore on Darwin"); 1081 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) 1082 .addReg(SrcReg, 1083 getKillRegState(isKill)), 1084 FrameIdx)); 1085 SpillsVRS = true; 1086 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1087 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX)) 1088 .addReg(SrcReg, 1089 getKillRegState(isKill)), 1090 FrameIdx)); 1091 NonRI = true; 1092 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1093 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs)) 1094 .addReg(SrcReg, 1095 getKillRegState(isKill)), 1096 FrameIdx)); 1097 NonRI = true; 1098 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1099 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) 1100 .addReg(SrcReg, 1101 getKillRegState(isKill)), 1102 FrameIdx)); 1103 NonRI = true; 1104 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { 1105 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST)) 1106 .addReg(SrcReg, 1107 getKillRegState(isKill)), 1108 FrameIdx)); 1109 } else { 1110 llvm_unreachable("Unknown regclass!"); 1111 } 1112 1113 return false; 1114 } 1115 1116 void 1117 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 1118 MachineBasicBlock::iterator MI, 1119 unsigned SrcReg, bool isKill, int FrameIdx, 1120 const TargetRegisterClass *RC, 1121 const TargetRegisterInfo *TRI) const { 1122 MachineFunction &MF = *MBB.getParent(); 1123 SmallVector<MachineInstr*, 4> NewMIs; 1124 1125 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1126 FuncInfo->setHasSpills(); 1127 1128 // We need to avoid a situation in which the value from a VRRC register is 1129 // spilled using an Altivec instruction and reloaded into a VSRC register 1130 // using a VSX instruction. The issue with this is that the VSX 1131 // load/store instructions swap the doublewords in the vector and the Altivec 1132 // ones don't. The register classes on the spill/reload may be different if 1133 // the register is defined using an Altivec instruction and is then used by a 1134 // VSX instruction. 1135 RC = updatedRC(RC); 1136 1137 bool NonRI = false, SpillsVRS = false; 1138 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, 1139 NonRI, SpillsVRS)) 1140 FuncInfo->setSpillsCR(); 1141 1142 if (SpillsVRS) 1143 FuncInfo->setSpillsVRSAVE(); 1144 1145 if (NonRI) 1146 FuncInfo->setHasNonRISpills(); 1147 1148 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1149 MBB.insert(MI, NewMIs[i]); 1150 1151 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1152 MachineMemOperand *MMO = MF.getMachineMemOperand( 1153 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1154 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx), 1155 MFI.getObjectAlignment(FrameIdx)); 1156 NewMIs.back()->addMemOperand(MF, MMO); 1157 } 1158 1159 bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL, 1160 unsigned DestReg, int FrameIdx, 1161 const TargetRegisterClass *RC, 1162 SmallVectorImpl<MachineInstr *> &NewMIs, 1163 bool &NonRI, bool &SpillsVRS) const { 1164 // Note: If additional load instructions are added here, 1165 // update isLoadFromStackSlot. 1166 1167 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 1168 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 1169 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), 1170 DestReg), FrameIdx)); 1171 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 1172 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 1173 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), 1174 FrameIdx)); 1175 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 1176 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), 1177 FrameIdx)); 1178 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 1179 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), 1180 FrameIdx)); 1181 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 1182 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1183 get(PPC::RESTORE_CR), DestReg), 1184 FrameIdx)); 1185 return true; 1186 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 1187 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1188 get(PPC::RESTORE_CRBIT), DestReg), 1189 FrameIdx)); 1190 return true; 1191 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1192 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), 1193 FrameIdx)); 1194 NonRI = true; 1195 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1196 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXV : PPC::LXVD2X; 1197 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg), 1198 FrameIdx)); 1199 NonRI = true; 1200 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1201 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX; 1202 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc), 1203 DestReg), FrameIdx)); 1204 NonRI = true; 1205 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1206 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX; 1207 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc), 1208 DestReg), FrameIdx)); 1209 NonRI = true; 1210 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1211 assert(Subtarget.isDarwin() && 1212 "VRSAVE only needs spill/restore on Darwin"); 1213 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1214 get(PPC::RESTORE_VRSAVE), 1215 DestReg), 1216 FrameIdx)); 1217 SpillsVRS = true; 1218 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1219 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg), 1220 FrameIdx)); 1221 NonRI = true; 1222 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1223 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg), 1224 FrameIdx)); 1225 NonRI = true; 1226 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1227 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), 1228 FrameIdx)); 1229 NonRI = true; 1230 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { 1231 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD), 1232 DestReg), FrameIdx)); 1233 } else { 1234 llvm_unreachable("Unknown regclass!"); 1235 } 1236 1237 return false; 1238 } 1239 1240 void 1241 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 1242 MachineBasicBlock::iterator MI, 1243 unsigned DestReg, int FrameIdx, 1244 const TargetRegisterClass *RC, 1245 const TargetRegisterInfo *TRI) const { 1246 MachineFunction &MF = *MBB.getParent(); 1247 SmallVector<MachineInstr*, 4> NewMIs; 1248 DebugLoc DL; 1249 if (MI != MBB.end()) DL = MI->getDebugLoc(); 1250 1251 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1252 FuncInfo->setHasSpills(); 1253 1254 // We need to avoid a situation in which the value from a VRRC register is 1255 // spilled using an Altivec instruction and reloaded into a VSRC register 1256 // using a VSX instruction. The issue with this is that the VSX 1257 // load/store instructions swap the doublewords in the vector and the Altivec 1258 // ones don't. The register classes on the spill/reload may be different if 1259 // the register is defined using an Altivec instruction and is then used by a 1260 // VSX instruction. 1261 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) 1262 RC = &PPC::VSRCRegClass; 1263 1264 bool NonRI = false, SpillsVRS = false; 1265 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, 1266 NonRI, SpillsVRS)) 1267 FuncInfo->setSpillsCR(); 1268 1269 if (SpillsVRS) 1270 FuncInfo->setSpillsVRSAVE(); 1271 1272 if (NonRI) 1273 FuncInfo->setHasNonRISpills(); 1274 1275 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1276 MBB.insert(MI, NewMIs[i]); 1277 1278 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1279 MachineMemOperand *MMO = MF.getMachineMemOperand( 1280 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1281 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx), 1282 MFI.getObjectAlignment(FrameIdx)); 1283 NewMIs.back()->addMemOperand(MF, MMO); 1284 } 1285 1286 bool PPCInstrInfo:: 1287 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 1288 assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); 1289 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) 1290 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); 1291 else 1292 // Leave the CR# the same, but invert the condition. 1293 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); 1294 return false; 1295 } 1296 1297 bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 1298 unsigned Reg, MachineRegisterInfo *MRI) const { 1299 // For some instructions, it is legal to fold ZERO into the RA register field. 1300 // A zero immediate should always be loaded with a single li. 1301 unsigned DefOpc = DefMI.getOpcode(); 1302 if (DefOpc != PPC::LI && DefOpc != PPC::LI8) 1303 return false; 1304 if (!DefMI.getOperand(1).isImm()) 1305 return false; 1306 if (DefMI.getOperand(1).getImm() != 0) 1307 return false; 1308 1309 // Note that we cannot here invert the arguments of an isel in order to fold 1310 // a ZERO into what is presented as the second argument. All we have here 1311 // is the condition bit, and that might come from a CR-logical bit operation. 1312 1313 const MCInstrDesc &UseMCID = UseMI.getDesc(); 1314 1315 // Only fold into real machine instructions. 1316 if (UseMCID.isPseudo()) 1317 return false; 1318 1319 unsigned UseIdx; 1320 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx) 1321 if (UseMI.getOperand(UseIdx).isReg() && 1322 UseMI.getOperand(UseIdx).getReg() == Reg) 1323 break; 1324 1325 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI"); 1326 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); 1327 1328 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; 1329 1330 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 1331 // register (which might also be specified as a pointer class kind). 1332 if (UseInfo->isLookupPtrRegClass()) { 1333 if (UseInfo->RegClass /* Kind */ != 1) 1334 return false; 1335 } else { 1336 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && 1337 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) 1338 return false; 1339 } 1340 1341 // Make sure this is not tied to an output register (or otherwise 1342 // constrained). This is true for ST?UX registers, for example, which 1343 // are tied to their output registers. 1344 if (UseInfo->Constraints != 0) 1345 return false; 1346 1347 unsigned ZeroReg; 1348 if (UseInfo->isLookupPtrRegClass()) { 1349 bool isPPC64 = Subtarget.isPPC64(); 1350 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; 1351 } else { 1352 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? 1353 PPC::ZERO8 : PPC::ZERO; 1354 } 1355 1356 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 1357 UseMI.getOperand(UseIdx).setReg(ZeroReg); 1358 1359 if (DeleteDef) 1360 DefMI.eraseFromParent(); 1361 1362 return true; 1363 } 1364 1365 static bool MBBDefinesCTR(MachineBasicBlock &MBB) { 1366 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 1367 I != IE; ++I) 1368 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) 1369 return true; 1370 return false; 1371 } 1372 1373 // We should make sure that, if we're going to predicate both sides of a 1374 // condition (a diamond), that both sides don't define the counter register. We 1375 // can predicate counter-decrement-based branches, but while that predicates 1376 // the branching, it does not predicate the counter decrement. If we tried to 1377 // merge the triangle into one predicated block, we'd decrement the counter 1378 // twice. 1379 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, 1380 unsigned NumT, unsigned ExtraT, 1381 MachineBasicBlock &FMBB, 1382 unsigned NumF, unsigned ExtraF, 1383 BranchProbability Probability) const { 1384 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); 1385 } 1386 1387 1388 bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const { 1389 // The predicated branches are identified by their type, not really by the 1390 // explicit presence of a predicate. Furthermore, some of them can be 1391 // predicated more than once. Because if conversion won't try to predicate 1392 // any instruction which already claims to be predicated (by returning true 1393 // here), always return false. In doing so, we let isPredicable() be the 1394 // final word on whether not the instruction can be (further) predicated. 1395 1396 return false; 1397 } 1398 1399 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const { 1400 if (!MI.isTerminator()) 1401 return false; 1402 1403 // Conditional branch is a special case. 1404 if (MI.isBranch() && !MI.isBarrier()) 1405 return true; 1406 1407 return !isPredicated(MI); 1408 } 1409 1410 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI, 1411 ArrayRef<MachineOperand> Pred) const { 1412 unsigned OpC = MI.getOpcode(); 1413 if (OpC == PPC::BLR || OpC == PPC::BLR8) { 1414 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1415 bool isPPC64 = Subtarget.isPPC64(); 1416 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) 1417 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); 1418 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1419 MI.setDesc(get(PPC::BCLR)); 1420 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1421 .addReg(Pred[1].getReg()); 1422 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1423 MI.setDesc(get(PPC::BCLRn)); 1424 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1425 .addReg(Pred[1].getReg()); 1426 } else { 1427 MI.setDesc(get(PPC::BCCLR)); 1428 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1429 .addImm(Pred[0].getImm()) 1430 .addReg(Pred[1].getReg()); 1431 } 1432 1433 return true; 1434 } else if (OpC == PPC::B) { 1435 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1436 bool isPPC64 = Subtarget.isPPC64(); 1437 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) 1438 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); 1439 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1440 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1441 MI.RemoveOperand(0); 1442 1443 MI.setDesc(get(PPC::BC)); 1444 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1445 .addReg(Pred[1].getReg()) 1446 .addMBB(MBB); 1447 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1448 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1449 MI.RemoveOperand(0); 1450 1451 MI.setDesc(get(PPC::BCn)); 1452 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1453 .addReg(Pred[1].getReg()) 1454 .addMBB(MBB); 1455 } else { 1456 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1457 MI.RemoveOperand(0); 1458 1459 MI.setDesc(get(PPC::BCC)); 1460 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1461 .addImm(Pred[0].getImm()) 1462 .addReg(Pred[1].getReg()) 1463 .addMBB(MBB); 1464 } 1465 1466 return true; 1467 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || 1468 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { 1469 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) 1470 llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); 1471 1472 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; 1473 bool isPPC64 = Subtarget.isPPC64(); 1474 1475 if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1476 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) 1477 : (setLR ? PPC::BCCTRL : PPC::BCCTR))); 1478 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1479 .addReg(Pred[1].getReg()); 1480 return true; 1481 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1482 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) 1483 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); 1484 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1485 .addReg(Pred[1].getReg()); 1486 return true; 1487 } 1488 1489 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) 1490 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); 1491 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1492 .addImm(Pred[0].getImm()) 1493 .addReg(Pred[1].getReg()); 1494 return true; 1495 } 1496 1497 return false; 1498 } 1499 1500 bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, 1501 ArrayRef<MachineOperand> Pred2) const { 1502 assert(Pred1.size() == 2 && "Invalid PPC first predicate"); 1503 assert(Pred2.size() == 2 && "Invalid PPC second predicate"); 1504 1505 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) 1506 return false; 1507 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) 1508 return false; 1509 1510 // P1 can only subsume P2 if they test the same condition register. 1511 if (Pred1[1].getReg() != Pred2[1].getReg()) 1512 return false; 1513 1514 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); 1515 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); 1516 1517 if (P1 == P2) 1518 return true; 1519 1520 // Does P1 subsume P2, e.g. GE subsumes GT. 1521 if (P1 == PPC::PRED_LE && 1522 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) 1523 return true; 1524 if (P1 == PPC::PRED_GE && 1525 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) 1526 return true; 1527 1528 return false; 1529 } 1530 1531 bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI, 1532 std::vector<MachineOperand> &Pred) const { 1533 // Note: At the present time, the contents of Pred from this function is 1534 // unused by IfConversion. This implementation follows ARM by pushing the 1535 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of 1536 // predicate, instructions defining CTR or CTR8 are also included as 1537 // predicate-defining instructions. 1538 1539 const TargetRegisterClass *RCs[] = 1540 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, 1541 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; 1542 1543 bool Found = false; 1544 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1545 const MachineOperand &MO = MI.getOperand(i); 1546 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { 1547 const TargetRegisterClass *RC = RCs[c]; 1548 if (MO.isReg()) { 1549 if (MO.isDef() && RC->contains(MO.getReg())) { 1550 Pred.push_back(MO); 1551 Found = true; 1552 } 1553 } else if (MO.isRegMask()) { 1554 for (TargetRegisterClass::iterator I = RC->begin(), 1555 IE = RC->end(); I != IE; ++I) 1556 if (MO.clobbersPhysReg(*I)) { 1557 Pred.push_back(MO); 1558 Found = true; 1559 } 1560 } 1561 } 1562 } 1563 1564 return Found; 1565 } 1566 1567 bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const { 1568 unsigned OpC = MI.getOpcode(); 1569 switch (OpC) { 1570 default: 1571 return false; 1572 case PPC::B: 1573 case PPC::BLR: 1574 case PPC::BLR8: 1575 case PPC::BCTR: 1576 case PPC::BCTR8: 1577 case PPC::BCTRL: 1578 case PPC::BCTRL8: 1579 return true; 1580 } 1581 } 1582 1583 bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, 1584 unsigned &SrcReg2, int &Mask, 1585 int &Value) const { 1586 unsigned Opc = MI.getOpcode(); 1587 1588 switch (Opc) { 1589 default: return false; 1590 case PPC::CMPWI: 1591 case PPC::CMPLWI: 1592 case PPC::CMPDI: 1593 case PPC::CMPLDI: 1594 SrcReg = MI.getOperand(1).getReg(); 1595 SrcReg2 = 0; 1596 Value = MI.getOperand(2).getImm(); 1597 Mask = 0xFFFF; 1598 return true; 1599 case PPC::CMPW: 1600 case PPC::CMPLW: 1601 case PPC::CMPD: 1602 case PPC::CMPLD: 1603 case PPC::FCMPUS: 1604 case PPC::FCMPUD: 1605 SrcReg = MI.getOperand(1).getReg(); 1606 SrcReg2 = MI.getOperand(2).getReg(); 1607 Value = 0; 1608 Mask = 0; 1609 return true; 1610 } 1611 } 1612 1613 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg, 1614 unsigned SrcReg2, int Mask, int Value, 1615 const MachineRegisterInfo *MRI) const { 1616 if (DisableCmpOpt) 1617 return false; 1618 1619 int OpC = CmpInstr.getOpcode(); 1620 unsigned CRReg = CmpInstr.getOperand(0).getReg(); 1621 1622 // FP record forms set CR1 based on the execption status bits, not a 1623 // comparison with zero. 1624 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) 1625 return false; 1626 1627 // The record forms set the condition register based on a signed comparison 1628 // with zero (so says the ISA manual). This is not as straightforward as it 1629 // seems, however, because this is always a 64-bit comparison on PPC64, even 1630 // for instructions that are 32-bit in nature (like slw for example). 1631 // So, on PPC32, for unsigned comparisons, we can use the record forms only 1632 // for equality checks (as those don't depend on the sign). On PPC64, 1633 // we are restricted to equality for unsigned 64-bit comparisons and for 1634 // signed 32-bit comparisons the applicability is more restricted. 1635 bool isPPC64 = Subtarget.isPPC64(); 1636 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; 1637 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; 1638 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; 1639 1640 // Get the unique definition of SrcReg. 1641 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 1642 if (!MI) return false; 1643 1644 bool equalityOnly = false; 1645 bool noSub = false; 1646 if (isPPC64) { 1647 if (is32BitSignedCompare) { 1648 // We can perform this optimization only if MI is sign-extending. 1649 if (isSignExtended(*MI)) 1650 noSub = true; 1651 else 1652 return false; 1653 } else if (is32BitUnsignedCompare) { 1654 // We can perform this optimization, equality only, if MI is 1655 // zero-extending. 1656 if (isZeroExtended(*MI)) { 1657 noSub = true; 1658 equalityOnly = true; 1659 } else 1660 return false; 1661 } else 1662 equalityOnly = is64BitUnsignedCompare; 1663 } else 1664 equalityOnly = is32BitUnsignedCompare; 1665 1666 if (equalityOnly) { 1667 // We need to check the uses of the condition register in order to reject 1668 // non-equality comparisons. 1669 for (MachineRegisterInfo::use_instr_iterator 1670 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 1671 I != IE; ++I) { 1672 MachineInstr *UseMI = &*I; 1673 if (UseMI->getOpcode() == PPC::BCC) { 1674 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); 1675 unsigned PredCond = PPC::getPredicateCondition(Pred); 1676 // We ignore hint bits when checking for non-equality comparisons. 1677 if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE) 1678 return false; 1679 } else if (UseMI->getOpcode() == PPC::ISEL || 1680 UseMI->getOpcode() == PPC::ISEL8) { 1681 unsigned SubIdx = UseMI->getOperand(3).getSubReg(); 1682 if (SubIdx != PPC::sub_eq) 1683 return false; 1684 } else 1685 return false; 1686 } 1687 } 1688 1689 MachineBasicBlock::iterator I = CmpInstr; 1690 1691 // Scan forward to find the first use of the compare. 1692 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL; 1693 ++I) { 1694 bool FoundUse = false; 1695 for (MachineRegisterInfo::use_instr_iterator 1696 J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end(); 1697 J != JE; ++J) 1698 if (&*J == &*I) { 1699 FoundUse = true; 1700 break; 1701 } 1702 1703 if (FoundUse) 1704 break; 1705 } 1706 1707 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; 1708 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; 1709 1710 // There are two possible candidates which can be changed to set CR[01]. 1711 // One is MI, the other is a SUB instruction. 1712 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 1713 MachineInstr *Sub = nullptr; 1714 if (SrcReg2 != 0) 1715 // MI is not a candidate for CMPrr. 1716 MI = nullptr; 1717 // FIXME: Conservatively refuse to convert an instruction which isn't in the 1718 // same BB as the comparison. This is to allow the check below to avoid calls 1719 // (and other explicit clobbers); instead we should really check for these 1720 // more explicitly (in at least a few predecessors). 1721 else if (MI->getParent() != CmpInstr.getParent()) 1722 return false; 1723 else if (Value != 0) { 1724 // The record-form instructions set CR bit based on signed comparison 1725 // against 0. We try to convert a compare against 1 or -1 into a compare 1726 // against 0 to exploit record-form instructions. For example, we change 1727 // the condition "greater than -1" into "greater than or equal to 0" 1728 // and "less than 1" into "less than or equal to 0". 1729 1730 // Since we optimize comparison based on a specific branch condition, 1731 // we don't optimize if condition code is used by more than once. 1732 if (equalityOnly || !MRI->hasOneUse(CRReg)) 1733 return false; 1734 1735 MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg); 1736 if (UseMI->getOpcode() != PPC::BCC) 1737 return false; 1738 1739 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); 1740 PPC::Predicate NewPred = Pred; 1741 unsigned PredCond = PPC::getPredicateCondition(Pred); 1742 unsigned PredHint = PPC::getPredicateHint(Pred); 1743 int16_t Immed = (int16_t)Value; 1744 1745 // When modyfing the condition in the predicate, we propagate hint bits 1746 // from the original predicate to the new one. 1747 if (Immed == -1 && PredCond == PPC::PRED_GT) 1748 // We convert "greater than -1" into "greater than or equal to 0", 1749 // since we are assuming signed comparison by !equalityOnly 1750 NewPred = PPC::getPredicate(PPC::PRED_GE, PredHint); 1751 else if (Immed == -1 && PredCond == PPC::PRED_LE) 1752 // We convert "less than or equal to -1" into "less than 0". 1753 NewPred = PPC::getPredicate(PPC::PRED_LT, PredHint); 1754 else if (Immed == 1 && PredCond == PPC::PRED_LT) 1755 // We convert "less than 1" into "less than or equal to 0". 1756 NewPred = PPC::getPredicate(PPC::PRED_LE, PredHint); 1757 else if (Immed == 1 && PredCond == PPC::PRED_GE) 1758 // We convert "greater than or equal to 1" into "greater than 0". 1759 NewPred = PPC::getPredicate(PPC::PRED_GT, PredHint); 1760 else 1761 return false; 1762 1763 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), 1764 NewPred)); 1765 } 1766 1767 // Search for Sub. 1768 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1769 --I; 1770 1771 // Get ready to iterate backward from CmpInstr. 1772 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin(); 1773 1774 for (; I != E && !noSub; --I) { 1775 const MachineInstr &Instr = *I; 1776 unsigned IOpC = Instr.getOpcode(); 1777 1778 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) || 1779 Instr.readsRegister(PPC::CR0, TRI))) 1780 // This instruction modifies or uses the record condition register after 1781 // the one we want to change. While we could do this transformation, it 1782 // would likely not be profitable. This transformation removes one 1783 // instruction, and so even forcing RA to generate one move probably 1784 // makes it unprofitable. 1785 return false; 1786 1787 // Check whether CmpInstr can be made redundant by the current instruction. 1788 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || 1789 OpC == PPC::CMPD || OpC == PPC::CMPLD) && 1790 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && 1791 ((Instr.getOperand(1).getReg() == SrcReg && 1792 Instr.getOperand(2).getReg() == SrcReg2) || 1793 (Instr.getOperand(1).getReg() == SrcReg2 && 1794 Instr.getOperand(2).getReg() == SrcReg))) { 1795 Sub = &*I; 1796 break; 1797 } 1798 1799 if (I == B) 1800 // The 'and' is below the comparison instruction. 1801 return false; 1802 } 1803 1804 // Return false if no candidates exist. 1805 if (!MI && !Sub) 1806 return false; 1807 1808 // The single candidate is called MI. 1809 if (!MI) MI = Sub; 1810 1811 int NewOpC = -1; 1812 int MIOpC = MI->getOpcode(); 1813 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) 1814 NewOpC = MIOpC; 1815 else { 1816 NewOpC = PPC::getRecordFormOpcode(MIOpC); 1817 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) 1818 NewOpC = MIOpC; 1819 } 1820 1821 // FIXME: On the non-embedded POWER architectures, only some of the record 1822 // forms are fast, and we should use only the fast ones. 1823 1824 // The defining instruction has a record form (or is already a record 1825 // form). It is possible, however, that we'll need to reverse the condition 1826 // code of the users. 1827 if (NewOpC == -1) 1828 return false; 1829 1830 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP 1831 // needs to be updated to be based on SUB. Push the condition code 1832 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the 1833 // condition code of these operands will be modified. 1834 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to 1835 // comparison against 0, which may modify predicate. 1836 bool ShouldSwap = false; 1837 if (Sub && Value == 0) { 1838 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 1839 Sub->getOperand(2).getReg() == SrcReg; 1840 1841 // The operands to subf are the opposite of sub, so only in the fixed-point 1842 // case, invert the order. 1843 ShouldSwap = !ShouldSwap; 1844 } 1845 1846 if (ShouldSwap) 1847 for (MachineRegisterInfo::use_instr_iterator 1848 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 1849 I != IE; ++I) { 1850 MachineInstr *UseMI = &*I; 1851 if (UseMI->getOpcode() == PPC::BCC) { 1852 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); 1853 unsigned PredCond = PPC::getPredicateCondition(Pred); 1854 assert((!equalityOnly || 1855 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) && 1856 "Invalid predicate for equality-only optimization"); 1857 (void)PredCond; // To suppress warning in release build. 1858 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), 1859 PPC::getSwappedPredicate(Pred))); 1860 } else if (UseMI->getOpcode() == PPC::ISEL || 1861 UseMI->getOpcode() == PPC::ISEL8) { 1862 unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); 1863 assert((!equalityOnly || NewSubReg == PPC::sub_eq) && 1864 "Invalid CR bit for equality-only optimization"); 1865 1866 if (NewSubReg == PPC::sub_lt) 1867 NewSubReg = PPC::sub_gt; 1868 else if (NewSubReg == PPC::sub_gt) 1869 NewSubReg = PPC::sub_lt; 1870 1871 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), 1872 NewSubReg)); 1873 } else // We need to abort on a user we don't understand. 1874 return false; 1875 } 1876 assert(!(Value != 0 && ShouldSwap) && 1877 "Non-zero immediate support and ShouldSwap" 1878 "may conflict in updating predicate"); 1879 1880 // Create a new virtual register to hold the value of the CR set by the 1881 // record-form instruction. If the instruction was not previously in 1882 // record form, then set the kill flag on the CR. 1883 CmpInstr.eraseFromParent(); 1884 1885 MachineBasicBlock::iterator MII = MI; 1886 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), 1887 get(TargetOpcode::COPY), CRReg) 1888 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); 1889 1890 // Even if CR0 register were dead before, it is alive now since the 1891 // instruction we just built uses it. 1892 MI->clearRegisterDeads(PPC::CR0); 1893 1894 if (MIOpC != NewOpC) { 1895 // We need to be careful here: we're replacing one instruction with 1896 // another, and we need to make sure that we get all of the right 1897 // implicit uses and defs. On the other hand, the caller may be holding 1898 // an iterator to this instruction, and so we can't delete it (this is 1899 // specifically the case if this is the instruction directly after the 1900 // compare). 1901 1902 // Rotates are expensive instructions. If we're emitting a record-form 1903 // rotate that can just be an andi, we should just emit the andi. 1904 if ((MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINM8) && 1905 MI->getOperand(2).getImm() == 0) { 1906 int64_t MB = MI->getOperand(3).getImm(); 1907 int64_t ME = MI->getOperand(4).getImm(); 1908 if (MB < ME && MB >= 16) { 1909 uint64_t Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1); 1910 NewOpC = MIOpC == PPC::RLWINM ? PPC::ANDIo : PPC::ANDIo8; 1911 MI->RemoveOperand(4); 1912 MI->RemoveOperand(3); 1913 MI->getOperand(2).setImm(Mask); 1914 NumRcRotatesConvertedToRcAnd++; 1915 } 1916 } else if (MIOpC == PPC::RLDICL && MI->getOperand(2).getImm() == 0) { 1917 int64_t MB = MI->getOperand(3).getImm(); 1918 if (MB >= 48) { 1919 uint64_t Mask = (1LLU << (63 - MB + 1)) - 1; 1920 NewOpC = PPC::ANDIo8; 1921 MI->RemoveOperand(3); 1922 MI->getOperand(2).setImm(Mask); 1923 NumRcRotatesConvertedToRcAnd++; 1924 } 1925 } 1926 1927 const MCInstrDesc &NewDesc = get(NewOpC); 1928 MI->setDesc(NewDesc); 1929 1930 if (NewDesc.ImplicitDefs) 1931 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs(); 1932 *ImpDefs; ++ImpDefs) 1933 if (!MI->definesRegister(*ImpDefs)) 1934 MI->addOperand(*MI->getParent()->getParent(), 1935 MachineOperand::CreateReg(*ImpDefs, true, true)); 1936 if (NewDesc.ImplicitUses) 1937 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses(); 1938 *ImpUses; ++ImpUses) 1939 if (!MI->readsRegister(*ImpUses)) 1940 MI->addOperand(*MI->getParent()->getParent(), 1941 MachineOperand::CreateReg(*ImpUses, false, true)); 1942 } 1943 assert(MI->definesRegister(PPC::CR0) && 1944 "Record-form instruction does not define cr0?"); 1945 1946 // Modify the condition code of operands in OperandsToUpdate. 1947 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 1948 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 1949 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) 1950 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); 1951 1952 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) 1953 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); 1954 1955 return true; 1956 } 1957 1958 /// GetInstSize - Return the number of bytes of code the specified 1959 /// instruction may be. This returns the maximum number of bytes. 1960 /// 1961 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 1962 unsigned Opcode = MI.getOpcode(); 1963 1964 if (Opcode == PPC::INLINEASM) { 1965 const MachineFunction *MF = MI.getParent()->getParent(); 1966 const char *AsmStr = MI.getOperand(0).getSymbolName(); 1967 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 1968 } else if (Opcode == TargetOpcode::STACKMAP) { 1969 StackMapOpers Opers(&MI); 1970 return Opers.getNumPatchBytes(); 1971 } else if (Opcode == TargetOpcode::PATCHPOINT) { 1972 PatchPointOpers Opers(&MI); 1973 return Opers.getNumPatchBytes(); 1974 } else { 1975 return get(Opcode).getSize(); 1976 } 1977 } 1978 1979 std::pair<unsigned, unsigned> 1980 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 1981 const unsigned Mask = PPCII::MO_ACCESS_MASK; 1982 return std::make_pair(TF & Mask, TF & ~Mask); 1983 } 1984 1985 ArrayRef<std::pair<unsigned, const char *>> 1986 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 1987 using namespace PPCII; 1988 static const std::pair<unsigned, const char *> TargetFlags[] = { 1989 {MO_LO, "ppc-lo"}, 1990 {MO_HA, "ppc-ha"}, 1991 {MO_TPREL_LO, "ppc-tprel-lo"}, 1992 {MO_TPREL_HA, "ppc-tprel-ha"}, 1993 {MO_DTPREL_LO, "ppc-dtprel-lo"}, 1994 {MO_TLSLD_LO, "ppc-tlsld-lo"}, 1995 {MO_TOC_LO, "ppc-toc-lo"}, 1996 {MO_TLS, "ppc-tls"}}; 1997 return makeArrayRef(TargetFlags); 1998 } 1999 2000 ArrayRef<std::pair<unsigned, const char *>> 2001 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { 2002 using namespace PPCII; 2003 static const std::pair<unsigned, const char *> TargetFlags[] = { 2004 {MO_PLT, "ppc-plt"}, 2005 {MO_PIC_FLAG, "ppc-pic"}, 2006 {MO_NLP_FLAG, "ppc-nlp"}, 2007 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}}; 2008 return makeArrayRef(TargetFlags); 2009 } 2010 2011 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction. 2012 // The VSX versions have the advantage of a full 64-register target whereas 2013 // the FP ones have the advantage of lower latency and higher throughput. So 2014 // what we are after is using the faster instructions in low register pressure 2015 // situations and using the larger register file in high register pressure 2016 // situations. 2017 bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr &MI) const { 2018 unsigned UpperOpcode, LowerOpcode; 2019 switch (MI.getOpcode()) { 2020 case PPC::DFLOADf32: 2021 UpperOpcode = PPC::LXSSP; 2022 LowerOpcode = PPC::LFS; 2023 break; 2024 case PPC::DFLOADf64: 2025 UpperOpcode = PPC::LXSD; 2026 LowerOpcode = PPC::LFD; 2027 break; 2028 case PPC::DFSTOREf32: 2029 UpperOpcode = PPC::STXSSP; 2030 LowerOpcode = PPC::STFS; 2031 break; 2032 case PPC::DFSTOREf64: 2033 UpperOpcode = PPC::STXSD; 2034 LowerOpcode = PPC::STFD; 2035 break; 2036 case PPC::XFLOADf32: 2037 UpperOpcode = PPC::LXSSPX; 2038 LowerOpcode = PPC::LFSX; 2039 break; 2040 case PPC::XFLOADf64: 2041 UpperOpcode = PPC::LXSDX; 2042 LowerOpcode = PPC::LFDX; 2043 break; 2044 case PPC::XFSTOREf32: 2045 UpperOpcode = PPC::STXSSPX; 2046 LowerOpcode = PPC::STFSX; 2047 break; 2048 case PPC::XFSTOREf64: 2049 UpperOpcode = PPC::STXSDX; 2050 LowerOpcode = PPC::STFDX; 2051 break; 2052 case PPC::LIWAX: 2053 UpperOpcode = PPC::LXSIWAX; 2054 LowerOpcode = PPC::LFIWAX; 2055 break; 2056 case PPC::LIWZX: 2057 UpperOpcode = PPC::LXSIWZX; 2058 LowerOpcode = PPC::LFIWZX; 2059 break; 2060 case PPC::STIWX: 2061 UpperOpcode = PPC::STXSIWX; 2062 LowerOpcode = PPC::STFIWX; 2063 break; 2064 default: 2065 llvm_unreachable("Unknown Operation!"); 2066 } 2067 2068 unsigned TargetReg = MI.getOperand(0).getReg(); 2069 unsigned Opcode; 2070 if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) || 2071 (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31)) 2072 Opcode = LowerOpcode; 2073 else 2074 Opcode = UpperOpcode; 2075 MI.setDesc(get(Opcode)); 2076 return true; 2077 } 2078 2079 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 2080 auto &MBB = *MI.getParent(); 2081 auto DL = MI.getDebugLoc(); 2082 2083 switch (MI.getOpcode()) { 2084 case TargetOpcode::LOAD_STACK_GUARD: { 2085 assert(Subtarget.isTargetLinux() && 2086 "Only Linux target is expected to contain LOAD_STACK_GUARD"); 2087 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; 2088 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2; 2089 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ)); 2090 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2091 .addImm(Offset) 2092 .addReg(Reg); 2093 return true; 2094 } 2095 case PPC::DFLOADf32: 2096 case PPC::DFLOADf64: 2097 case PPC::DFSTOREf32: 2098 case PPC::DFSTOREf64: { 2099 assert(Subtarget.hasP9Vector() && 2100 "Invalid D-Form Pseudo-ops on Pre-P9 target."); 2101 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() && 2102 "D-form op must have register and immediate operands"); 2103 return expandVSXMemPseudo(MI); 2104 } 2105 case PPC::XFLOADf32: 2106 case PPC::XFSTOREf32: 2107 case PPC::LIWAX: 2108 case PPC::LIWZX: 2109 case PPC::STIWX: { 2110 assert(Subtarget.hasP8Vector() && 2111 "Invalid X-Form Pseudo-ops on Pre-P8 target."); 2112 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() && 2113 "X-form op must have register and register operands"); 2114 return expandVSXMemPseudo(MI); 2115 } 2116 case PPC::XFLOADf64: 2117 case PPC::XFSTOREf64: { 2118 assert(Subtarget.hasVSX() && 2119 "Invalid X-Form Pseudo-ops on target that has no VSX."); 2120 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() && 2121 "X-form op must have register and register operands"); 2122 return expandVSXMemPseudo(MI); 2123 } 2124 case PPC::SPILLTOVSR_LD: { 2125 unsigned TargetReg = MI.getOperand(0).getReg(); 2126 if (PPC::VSFRCRegClass.contains(TargetReg)) { 2127 MI.setDesc(get(PPC::DFLOADf64)); 2128 return expandPostRAPseudo(MI); 2129 } 2130 else 2131 MI.setDesc(get(PPC::LD)); 2132 return true; 2133 } 2134 case PPC::SPILLTOVSR_ST: { 2135 unsigned SrcReg = MI.getOperand(0).getReg(); 2136 if (PPC::VSFRCRegClass.contains(SrcReg)) { 2137 NumStoreSPILLVSRRCAsVec++; 2138 MI.setDesc(get(PPC::DFSTOREf64)); 2139 return expandPostRAPseudo(MI); 2140 } else { 2141 NumStoreSPILLVSRRCAsGpr++; 2142 MI.setDesc(get(PPC::STD)); 2143 } 2144 return true; 2145 } 2146 case PPC::SPILLTOVSR_LDX: { 2147 unsigned TargetReg = MI.getOperand(0).getReg(); 2148 if (PPC::VSFRCRegClass.contains(TargetReg)) 2149 MI.setDesc(get(PPC::LXSDX)); 2150 else 2151 MI.setDesc(get(PPC::LDX)); 2152 return true; 2153 } 2154 case PPC::SPILLTOVSR_STX: { 2155 unsigned SrcReg = MI.getOperand(0).getReg(); 2156 if (PPC::VSFRCRegClass.contains(SrcReg)) { 2157 NumStoreSPILLVSRRCAsVec++; 2158 MI.setDesc(get(PPC::STXSDX)); 2159 } else { 2160 NumStoreSPILLVSRRCAsGpr++; 2161 MI.setDesc(get(PPC::STDX)); 2162 } 2163 return true; 2164 } 2165 2166 case PPC::CFENCE8: { 2167 auto Val = MI.getOperand(0).getReg(); 2168 BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val); 2169 BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP)) 2170 .addImm(PPC::PRED_NE_MINUS) 2171 .addReg(PPC::CR7) 2172 .addImm(1); 2173 MI.setDesc(get(PPC::ISYNC)); 2174 MI.RemoveOperand(0); 2175 return true; 2176 } 2177 } 2178 return false; 2179 } 2180 2181 // Essentially a compile-time implementation of a compare->isel sequence. 2182 // It takes two constants to compare, along with the true/false registers 2183 // and the comparison type (as a subreg to a CR field) and returns one 2184 // of the true/false registers, depending on the comparison results. 2185 static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc, 2186 unsigned TrueReg, unsigned FalseReg, 2187 unsigned CRSubReg) { 2188 // Signed comparisons. The immediates are assumed to be sign-extended. 2189 if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) { 2190 switch (CRSubReg) { 2191 default: llvm_unreachable("Unknown integer comparison type."); 2192 case PPC::sub_lt: 2193 return Imm1 < Imm2 ? TrueReg : FalseReg; 2194 case PPC::sub_gt: 2195 return Imm1 > Imm2 ? TrueReg : FalseReg; 2196 case PPC::sub_eq: 2197 return Imm1 == Imm2 ? TrueReg : FalseReg; 2198 } 2199 } 2200 // Unsigned comparisons. 2201 else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) { 2202 switch (CRSubReg) { 2203 default: llvm_unreachable("Unknown integer comparison type."); 2204 case PPC::sub_lt: 2205 return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg; 2206 case PPC::sub_gt: 2207 return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg; 2208 case PPC::sub_eq: 2209 return Imm1 == Imm2 ? TrueReg : FalseReg; 2210 } 2211 } 2212 return PPC::NoRegister; 2213 } 2214 2215 // Replace an instruction with one that materializes a constant (and sets 2216 // CR0 if the original instruction was a record-form instruction). 2217 void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI, 2218 const LoadImmediateInfo &LII) const { 2219 // Remove existing operands. 2220 int OperandToKeep = LII.SetCR ? 1 : 0; 2221 for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--) 2222 MI.RemoveOperand(i); 2223 2224 // Replace the instruction. 2225 if (LII.SetCR) { 2226 MI.setDesc(get(LII.Is64Bit ? PPC::ANDIo8 : PPC::ANDIo)); 2227 // Set the immediate. 2228 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2229 .addImm(LII.Imm).addReg(PPC::CR0, RegState::ImplicitDefine); 2230 return; 2231 } 2232 else 2233 MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI)); 2234 2235 // Set the immediate. 2236 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2237 .addImm(LII.Imm); 2238 } 2239 2240 MachineInstr *PPCInstrInfo::getConstantDefMI(MachineInstr &MI, 2241 unsigned &ConstOp, 2242 bool &SeenIntermediateUse) const { 2243 ConstOp = ~0U; 2244 MachineInstr *DefMI = nullptr; 2245 MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo(); 2246 const TargetRegisterInfo *TRI = &getRegisterInfo(); 2247 // If we'ere in SSA, get the defs through the MRI. Otherwise, only look 2248 // within the basic block to see if the register is defined using an LI/LI8. 2249 if (MRI->isSSA()) { 2250 for (int i = 1, e = MI.getNumOperands(); i < e; i++) { 2251 if (!MI.getOperand(i).isReg()) 2252 continue; 2253 unsigned Reg = MI.getOperand(i).getReg(); 2254 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 2255 continue; 2256 unsigned TrueReg = TRI->lookThruCopyLike(Reg, MRI); 2257 if (TargetRegisterInfo::isVirtualRegister(TrueReg)) { 2258 DefMI = MRI->getVRegDef(TrueReg); 2259 if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8) { 2260 ConstOp = i; 2261 break; 2262 } 2263 } 2264 } 2265 } else { 2266 // Looking back through the definition for each operand could be expensive, 2267 // so exit early if this isn't an instruction that either has an immediate 2268 // form or is already an immediate form that we can handle. 2269 ImmInstrInfo III; 2270 unsigned Opc = MI.getOpcode(); 2271 bool ConvertibleImmForm = 2272 Opc == PPC::CMPWI || Opc == PPC::CMPLWI || 2273 Opc == PPC::CMPDI || Opc == PPC::CMPLDI || 2274 Opc == PPC::ADDI || Opc == PPC::ADDI8 || 2275 Opc == PPC::ORI || Opc == PPC::ORI8 || 2276 Opc == PPC::XORI || Opc == PPC::XORI8 || 2277 Opc == PPC::RLDICL || Opc == PPC::RLDICLo || 2278 Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 || 2279 Opc == PPC::RLWINM || Opc == PPC::RLWINMo || 2280 Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o; 2281 if (!instrHasImmForm(MI, III) && !ConvertibleImmForm) 2282 return nullptr; 2283 2284 // Don't convert or %X, %Y, %Y since that's just a register move. 2285 if ((Opc == PPC::OR || Opc == PPC::OR8) && 2286 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 2287 return nullptr; 2288 for (int i = 1, e = MI.getNumOperands(); i < e; i++) { 2289 MachineOperand &MO = MI.getOperand(i); 2290 SeenIntermediateUse = false; 2291 if (MO.isReg() && MO.isUse() && !MO.isImplicit()) { 2292 MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI; 2293 It++; 2294 unsigned Reg = MI.getOperand(i).getReg(); 2295 // MachineInstr::readsRegister only returns true if the machine 2296 // instruction reads the exact register or its super-register. It 2297 // does not consider uses of sub-registers which seems like strange 2298 // behaviour. Nonetheless, if we end up with a 64-bit register here, 2299 // get the corresponding 32-bit register to check. 2300 if (PPC::G8RCRegClass.contains(Reg)) 2301 Reg = Reg - PPC::X0 + PPC::R0; 2302 2303 // Is this register defined by a load-immediate in this block? 2304 for ( ; It != E; ++It) { 2305 if (It->modifiesRegister(Reg, &getRegisterInfo())) { 2306 if (It->getOpcode() == PPC::LI || It->getOpcode() == PPC::LI8) { 2307 ConstOp = i; 2308 return &*It; 2309 } else 2310 break; 2311 } else if (It->readsRegister(Reg, &getRegisterInfo())) 2312 // If we see another use of this reg between the def and the MI, 2313 // we want to flat it so the def isn't deleted. 2314 SeenIntermediateUse = true; 2315 } 2316 } 2317 } 2318 } 2319 return ConstOp == ~0U ? nullptr : DefMI; 2320 } 2321 2322 // If this instruction has an immediate form and one of its operands is a 2323 // result of a load-immediate, convert it to the immediate form if the constant 2324 // is in range. 2325 bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI, 2326 MachineInstr **KilledDef) const { 2327 MachineFunction *MF = MI.getParent()->getParent(); 2328 MachineRegisterInfo *MRI = &MF->getRegInfo(); 2329 bool PostRA = !MRI->isSSA(); 2330 bool SeenIntermediateUse = true; 2331 unsigned ConstantOperand = ~0U; 2332 MachineInstr *DefMI = getConstantDefMI(MI, ConstantOperand, 2333 SeenIntermediateUse); 2334 if (!DefMI || !DefMI->getOperand(1).isImm()) 2335 return false; 2336 assert(ConstantOperand < MI.getNumOperands() && 2337 "The constant operand needs to be valid at this point"); 2338 2339 int64_t Immediate = DefMI->getOperand(1).getImm(); 2340 // Sign-extend to 64-bits. 2341 int64_t SExtImm = ((uint64_t)Immediate & ~0x7FFFuLL) != 0 ? 2342 (Immediate | 0xFFFFFFFFFFFF0000) : Immediate; 2343 2344 if (KilledDef && MI.getOperand(ConstantOperand).isKill() && 2345 !SeenIntermediateUse) 2346 *KilledDef = DefMI; 2347 2348 // If this is a reg+reg instruction that has a reg+imm form, convert it now. 2349 ImmInstrInfo III; 2350 if (instrHasImmForm(MI, III)) 2351 return transformToImmForm(MI, III, ConstantOperand, SExtImm); 2352 2353 bool ReplaceWithLI = false; 2354 bool Is64BitLI = false; 2355 int64_t NewImm = 0; 2356 bool SetCR = false; 2357 unsigned Opc = MI.getOpcode(); 2358 switch (Opc) { 2359 default: return false; 2360 2361 // FIXME: Any branches conditional on such a comparison can be made 2362 // unconditional. At this time, this happens too infrequently to be worth 2363 // the implementation effort, but if that ever changes, we could convert 2364 // such a pattern here. 2365 case PPC::CMPWI: 2366 case PPC::CMPLWI: 2367 case PPC::CMPDI: 2368 case PPC::CMPLDI: { 2369 // Doing this post-RA would require dataflow analysis to reliably find uses 2370 // of the CR register set by the compare. 2371 if (PostRA) 2372 return false; 2373 // If a compare-immediate is fed by an immediate and is itself an input of 2374 // an ISEL (the most common case) into a COPY of the correct register. 2375 bool Changed = false; 2376 unsigned DefReg = MI.getOperand(0).getReg(); 2377 int64_t Comparand = MI.getOperand(2).getImm(); 2378 int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0 ? 2379 (Comparand | 0xFFFFFFFFFFFF0000) : Comparand; 2380 2381 for (auto &CompareUseMI : MRI->use_instructions(DefReg)) { 2382 unsigned UseOpc = CompareUseMI.getOpcode(); 2383 if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8) 2384 continue; 2385 unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg(); 2386 unsigned TrueReg = CompareUseMI.getOperand(1).getReg(); 2387 unsigned FalseReg = CompareUseMI.getOperand(2).getReg(); 2388 unsigned RegToCopy = selectReg(SExtImm, SExtComparand, Opc, TrueReg, 2389 FalseReg, CRSubReg); 2390 if (RegToCopy == PPC::NoRegister) 2391 continue; 2392 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0. 2393 if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) { 2394 CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI)); 2395 CompareUseMI.getOperand(1).ChangeToImmediate(0); 2396 CompareUseMI.RemoveOperand(3); 2397 CompareUseMI.RemoveOperand(2); 2398 continue; 2399 } 2400 DEBUG(dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n"); 2401 DEBUG(DefMI->dump(); MI.dump(); CompareUseMI.dump()); 2402 DEBUG(dbgs() << "Is converted to:\n"); 2403 // Convert to copy and remove unneeded operands. 2404 CompareUseMI.setDesc(get(PPC::COPY)); 2405 CompareUseMI.RemoveOperand(3); 2406 CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1); 2407 CmpIselsConverted++; 2408 Changed = true; 2409 DEBUG(CompareUseMI.dump()); 2410 } 2411 if (Changed) 2412 return true; 2413 // This may end up incremented multiple times since this function is called 2414 // during a fixed-point transformation, but it is only meant to indicate the 2415 // presence of this opportunity. 2416 MissedConvertibleImmediateInstrs++; 2417 return false; 2418 } 2419 2420 // Immediate forms - may simply be convertable to an LI. 2421 case PPC::ADDI: 2422 case PPC::ADDI8: { 2423 // Does the sum fit in a 16-bit signed field? 2424 int64_t Addend = MI.getOperand(2).getImm(); 2425 if (isInt<16>(Addend + SExtImm)) { 2426 ReplaceWithLI = true; 2427 Is64BitLI = Opc == PPC::ADDI8; 2428 NewImm = Addend + SExtImm; 2429 break; 2430 } 2431 return false; 2432 } 2433 case PPC::RLDICL: 2434 case PPC::RLDICLo: 2435 case PPC::RLDICL_32: 2436 case PPC::RLDICL_32_64: { 2437 // Use APInt's rotate function. 2438 int64_t SH = MI.getOperand(2).getImm(); 2439 int64_t MB = MI.getOperand(3).getImm(); 2440 APInt InVal(Opc == PPC::RLDICL ? 64 : 32, SExtImm, true); 2441 InVal = InVal.rotl(SH); 2442 uint64_t Mask = (1LLU << (63 - MB + 1)) - 1; 2443 InVal &= Mask; 2444 // Can't replace negative values with an LI as that will sign-extend 2445 // and not clear the left bits. If we're setting the CR bit, we will use 2446 // ANDIo which won't sign extend, so that's safe. 2447 if (isUInt<15>(InVal.getSExtValue()) || 2448 (Opc == PPC::RLDICLo && isUInt<16>(InVal.getSExtValue()))) { 2449 ReplaceWithLI = true; 2450 Is64BitLI = Opc != PPC::RLDICL_32; 2451 NewImm = InVal.getSExtValue(); 2452 SetCR = Opc == PPC::RLDICLo; 2453 if (SetCR && (SExtImm & NewImm) != NewImm) 2454 return false; 2455 break; 2456 } 2457 return false; 2458 } 2459 case PPC::RLWINM: 2460 case PPC::RLWINM8: 2461 case PPC::RLWINMo: 2462 case PPC::RLWINM8o: { 2463 int64_t SH = MI.getOperand(2).getImm(); 2464 int64_t MB = MI.getOperand(3).getImm(); 2465 int64_t ME = MI.getOperand(4).getImm(); 2466 APInt InVal(32, SExtImm, true); 2467 InVal = InVal.rotl(SH); 2468 // Set the bits ( MB + 32 ) to ( ME + 32 ). 2469 uint64_t Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1); 2470 InVal &= Mask; 2471 // Can't replace negative values with an LI as that will sign-extend 2472 // and not clear the left bits. If we're setting the CR bit, we will use 2473 // ANDIo which won't sign extend, so that's safe. 2474 bool ValueFits = isUInt<15>(InVal.getSExtValue()); 2475 ValueFits |= ((Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o) && 2476 isUInt<16>(InVal.getSExtValue())); 2477 if (ValueFits) { 2478 ReplaceWithLI = true; 2479 Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o; 2480 NewImm = InVal.getSExtValue(); 2481 SetCR = Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o; 2482 if (SetCR && (SExtImm & NewImm) != NewImm) 2483 return false; 2484 break; 2485 } 2486 return false; 2487 } 2488 case PPC::ORI: 2489 case PPC::ORI8: 2490 case PPC::XORI: 2491 case PPC::XORI8: { 2492 int64_t LogicalImm = MI.getOperand(2).getImm(); 2493 int64_t Result = 0; 2494 if (Opc == PPC::ORI || Opc == PPC::ORI8) 2495 Result = LogicalImm | SExtImm; 2496 else 2497 Result = LogicalImm ^ SExtImm; 2498 if (isInt<16>(Result)) { 2499 ReplaceWithLI = true; 2500 Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8; 2501 NewImm = Result; 2502 break; 2503 } 2504 return false; 2505 } 2506 } 2507 2508 if (ReplaceWithLI) { 2509 DEBUG(dbgs() << "Replacing instruction:\n"); 2510 DEBUG(MI.dump()); 2511 DEBUG(dbgs() << "Fed by:\n"); 2512 DEBUG(DefMI->dump()); 2513 LoadImmediateInfo LII; 2514 LII.Imm = NewImm; 2515 LII.Is64Bit = Is64BitLI; 2516 LII.SetCR = SetCR; 2517 // If we're setting the CR, the original load-immediate must be kept (as an 2518 // operand to ANDIo/ANDI8o). 2519 if (KilledDef && SetCR) 2520 *KilledDef = nullptr; 2521 replaceInstrWithLI(MI, LII); 2522 DEBUG(dbgs() << "With:\n"); 2523 DEBUG(MI.dump()); 2524 return true; 2525 } 2526 return false; 2527 } 2528 2529 bool PPCInstrInfo::instrHasImmForm(const MachineInstr &MI, 2530 ImmInstrInfo &III) const { 2531 unsigned Opc = MI.getOpcode(); 2532 // The vast majority of the instructions would need their operand 2 replaced 2533 // with an immediate when switching to the reg+imm form. A marked exception 2534 // are the update form loads/stores for which a constant operand 2 would need 2535 // to turn into a displacement and move operand 1 to the operand 2 position. 2536 III.ImmOpNo = 2; 2537 III.ConstantOpNo = 2; 2538 III.ImmWidth = 16; 2539 III.ImmMustBeMultipleOf = 1; 2540 III.TruncateImmTo = 0; 2541 switch (Opc) { 2542 default: return false; 2543 case PPC::ADD4: 2544 case PPC::ADD8: 2545 III.SignedImm = true; 2546 III.ZeroIsSpecialOrig = 0; 2547 III.ZeroIsSpecialNew = 1; 2548 III.IsCommutative = true; 2549 III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8; 2550 break; 2551 case PPC::ADDC: 2552 case PPC::ADDC8: 2553 III.SignedImm = true; 2554 III.ZeroIsSpecialOrig = 0; 2555 III.ZeroIsSpecialNew = 0; 2556 III.IsCommutative = true; 2557 III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8; 2558 break; 2559 case PPC::ADDCo: 2560 III.SignedImm = true; 2561 III.ZeroIsSpecialOrig = 0; 2562 III.ZeroIsSpecialNew = 0; 2563 III.IsCommutative = true; 2564 III.ImmOpcode = PPC::ADDICo; 2565 break; 2566 case PPC::SUBFC: 2567 case PPC::SUBFC8: 2568 III.SignedImm = true; 2569 III.ZeroIsSpecialOrig = 0; 2570 III.ZeroIsSpecialNew = 0; 2571 III.IsCommutative = false; 2572 III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8; 2573 break; 2574 case PPC::CMPW: 2575 case PPC::CMPD: 2576 III.SignedImm = true; 2577 III.ZeroIsSpecialOrig = 0; 2578 III.ZeroIsSpecialNew = 0; 2579 III.IsCommutative = false; 2580 III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI; 2581 break; 2582 case PPC::CMPLW: 2583 case PPC::CMPLD: 2584 III.SignedImm = false; 2585 III.ZeroIsSpecialOrig = 0; 2586 III.ZeroIsSpecialNew = 0; 2587 III.IsCommutative = false; 2588 III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI; 2589 break; 2590 case PPC::ANDo: 2591 case PPC::AND8o: 2592 case PPC::OR: 2593 case PPC::OR8: 2594 case PPC::XOR: 2595 case PPC::XOR8: 2596 III.SignedImm = false; 2597 III.ZeroIsSpecialOrig = 0; 2598 III.ZeroIsSpecialNew = 0; 2599 III.IsCommutative = true; 2600 switch(Opc) { 2601 default: llvm_unreachable("Unknown opcode"); 2602 case PPC::ANDo: III.ImmOpcode = PPC::ANDIo; break; 2603 case PPC::AND8o: III.ImmOpcode = PPC::ANDIo8; break; 2604 case PPC::OR: III.ImmOpcode = PPC::ORI; break; 2605 case PPC::OR8: III.ImmOpcode = PPC::ORI8; break; 2606 case PPC::XOR: III.ImmOpcode = PPC::XORI; break; 2607 case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break; 2608 } 2609 break; 2610 case PPC::RLWNM: 2611 case PPC::RLWNM8: 2612 case PPC::RLWNMo: 2613 case PPC::RLWNM8o: 2614 case PPC::SLW: 2615 case PPC::SLW8: 2616 case PPC::SLWo: 2617 case PPC::SLW8o: 2618 case PPC::SRW: 2619 case PPC::SRW8: 2620 case PPC::SRWo: 2621 case PPC::SRW8o: 2622 case PPC::SRAW: 2623 case PPC::SRAWo: 2624 III.SignedImm = false; 2625 III.ZeroIsSpecialOrig = 0; 2626 III.ZeroIsSpecialNew = 0; 2627 III.IsCommutative = false; 2628 // This isn't actually true, but the instructions ignore any of the 2629 // upper bits, so any immediate loaded with an LI is acceptable. 2630 // This does not apply to shift right algebraic because a value 2631 // out of range will produce a -1/0. 2632 III.ImmWidth = 16; 2633 if (Opc == PPC::RLWNM || Opc == PPC::RLWNM8 || 2634 Opc == PPC::RLWNMo || Opc == PPC::RLWNM8o) 2635 III.TruncateImmTo = 5; 2636 else 2637 III.TruncateImmTo = 6; 2638 switch(Opc) { 2639 default: llvm_unreachable("Unknown opcode"); 2640 case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break; 2641 case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break; 2642 case PPC::RLWNMo: III.ImmOpcode = PPC::RLWINMo; break; 2643 case PPC::RLWNM8o: III.ImmOpcode = PPC::RLWINM8o; break; 2644 case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break; 2645 case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break; 2646 case PPC::SLWo: III.ImmOpcode = PPC::RLWINMo; break; 2647 case PPC::SLW8o: III.ImmOpcode = PPC::RLWINM8o; break; 2648 case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break; 2649 case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break; 2650 case PPC::SRWo: III.ImmOpcode = PPC::RLWINMo; break; 2651 case PPC::SRW8o: III.ImmOpcode = PPC::RLWINM8o; break; 2652 case PPC::SRAW: 2653 III.ImmWidth = 5; 2654 III.TruncateImmTo = 0; 2655 III.ImmOpcode = PPC::SRAWI; 2656 break; 2657 case PPC::SRAWo: 2658 III.ImmWidth = 5; 2659 III.TruncateImmTo = 0; 2660 III.ImmOpcode = PPC::SRAWIo; 2661 break; 2662 } 2663 break; 2664 case PPC::RLDCL: 2665 case PPC::RLDCLo: 2666 case PPC::RLDCR: 2667 case PPC::RLDCRo: 2668 case PPC::SLD: 2669 case PPC::SLDo: 2670 case PPC::SRD: 2671 case PPC::SRDo: 2672 case PPC::SRAD: 2673 case PPC::SRADo: 2674 III.SignedImm = false; 2675 III.ZeroIsSpecialOrig = 0; 2676 III.ZeroIsSpecialNew = 0; 2677 III.IsCommutative = false; 2678 // This isn't actually true, but the instructions ignore any of the 2679 // upper bits, so any immediate loaded with an LI is acceptable. 2680 // This does not apply to shift right algebraic because a value 2681 // out of range will produce a -1/0. 2682 III.ImmWidth = 16; 2683 if (Opc == PPC::RLDCL || Opc == PPC::RLDCLo || 2684 Opc == PPC::RLDCR || Opc == PPC::RLDCRo) 2685 III.TruncateImmTo = 6; 2686 else 2687 III.TruncateImmTo = 7; 2688 switch(Opc) { 2689 default: llvm_unreachable("Unknown opcode"); 2690 case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break; 2691 case PPC::RLDCLo: III.ImmOpcode = PPC::RLDICLo; break; 2692 case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break; 2693 case PPC::RLDCRo: III.ImmOpcode = PPC::RLDICRo; break; 2694 case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break; 2695 case PPC::SLDo: III.ImmOpcode = PPC::RLDICRo; break; 2696 case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break; 2697 case PPC::SRDo: III.ImmOpcode = PPC::RLDICLo; break; 2698 case PPC::SRAD: 2699 III.ImmWidth = 6; 2700 III.TruncateImmTo = 0; 2701 III.ImmOpcode = PPC::SRADI; 2702 break; 2703 case PPC::SRADo: 2704 III.ImmWidth = 6; 2705 III.TruncateImmTo = 0; 2706 III.ImmOpcode = PPC::SRADIo; 2707 break; 2708 } 2709 break; 2710 // Loads and stores: 2711 case PPC::LBZX: 2712 case PPC::LBZX8: 2713 case PPC::LHZX: 2714 case PPC::LHZX8: 2715 case PPC::LHAX: 2716 case PPC::LHAX8: 2717 case PPC::LWZX: 2718 case PPC::LWZX8: 2719 case PPC::LWAX: 2720 case PPC::LDX: 2721 case PPC::LFSX: 2722 case PPC::LFDX: 2723 case PPC::STBX: 2724 case PPC::STBX8: 2725 case PPC::STHX: 2726 case PPC::STHX8: 2727 case PPC::STWX: 2728 case PPC::STWX8: 2729 case PPC::STDX: 2730 case PPC::STFSX: 2731 case PPC::STFDX: 2732 III.SignedImm = true; 2733 III.ZeroIsSpecialOrig = 1; 2734 III.ZeroIsSpecialNew = 2; 2735 III.IsCommutative = true; 2736 III.ImmOpNo = 1; 2737 III.ConstantOpNo = 2; 2738 switch(Opc) { 2739 default: llvm_unreachable("Unknown opcode"); 2740 case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break; 2741 case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break; 2742 case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break; 2743 case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break; 2744 case PPC::LHAX: III.ImmOpcode = PPC::LHA; break; 2745 case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break; 2746 case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break; 2747 case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break; 2748 case PPC::LWAX: 2749 III.ImmOpcode = PPC::LWA; 2750 III.ImmMustBeMultipleOf = 4; 2751 break; 2752 case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break; 2753 case PPC::LFSX: III.ImmOpcode = PPC::LFS; break; 2754 case PPC::LFDX: III.ImmOpcode = PPC::LFD; break; 2755 case PPC::STBX: III.ImmOpcode = PPC::STB; break; 2756 case PPC::STBX8: III.ImmOpcode = PPC::STB8; break; 2757 case PPC::STHX: III.ImmOpcode = PPC::STH; break; 2758 case PPC::STHX8: III.ImmOpcode = PPC::STH8; break; 2759 case PPC::STWX: III.ImmOpcode = PPC::STW; break; 2760 case PPC::STWX8: III.ImmOpcode = PPC::STW8; break; 2761 case PPC::STDX: 2762 III.ImmOpcode = PPC::STD; 2763 III.ImmMustBeMultipleOf = 4; 2764 break; 2765 case PPC::STFSX: III.ImmOpcode = PPC::STFS; break; 2766 case PPC::STFDX: III.ImmOpcode = PPC::STFD; break; 2767 } 2768 break; 2769 case PPC::LBZUX: 2770 case PPC::LBZUX8: 2771 case PPC::LHZUX: 2772 case PPC::LHZUX8: 2773 case PPC::LHAUX: 2774 case PPC::LHAUX8: 2775 case PPC::LWZUX: 2776 case PPC::LWZUX8: 2777 case PPC::LDUX: 2778 case PPC::LFSUX: 2779 case PPC::LFDUX: 2780 case PPC::STBUX: 2781 case PPC::STBUX8: 2782 case PPC::STHUX: 2783 case PPC::STHUX8: 2784 case PPC::STWUX: 2785 case PPC::STWUX8: 2786 case PPC::STDUX: 2787 case PPC::STFSUX: 2788 case PPC::STFDUX: 2789 III.SignedImm = true; 2790 III.ZeroIsSpecialOrig = 2; 2791 III.ZeroIsSpecialNew = 3; 2792 III.IsCommutative = false; 2793 III.ImmOpNo = 2; 2794 III.ConstantOpNo = 3; 2795 switch(Opc) { 2796 default: llvm_unreachable("Unknown opcode"); 2797 case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break; 2798 case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break; 2799 case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break; 2800 case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break; 2801 case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break; 2802 case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break; 2803 case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break; 2804 case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break; 2805 case PPC::LDUX: 2806 III.ImmOpcode = PPC::LDU; 2807 III.ImmMustBeMultipleOf = 4; 2808 break; 2809 case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break; 2810 case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break; 2811 case PPC::STBUX: III.ImmOpcode = PPC::STBU; break; 2812 case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break; 2813 case PPC::STHUX: III.ImmOpcode = PPC::STHU; break; 2814 case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break; 2815 case PPC::STWUX: III.ImmOpcode = PPC::STWU; break; 2816 case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break; 2817 case PPC::STDUX: 2818 III.ImmOpcode = PPC::STDU; 2819 III.ImmMustBeMultipleOf = 4; 2820 break; 2821 case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break; 2822 case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break; 2823 } 2824 break; 2825 // Power9 only. 2826 case PPC::LXVX: 2827 case PPC::LXSSPX: 2828 case PPC::LXSDX: 2829 case PPC::STXVX: 2830 case PPC::STXSSPX: 2831 case PPC::STXSDX: 2832 if (!Subtarget.hasP9Vector()) 2833 return false; 2834 III.SignedImm = true; 2835 III.ZeroIsSpecialOrig = 1; 2836 III.ZeroIsSpecialNew = 2; 2837 III.IsCommutative = true; 2838 III.ImmOpNo = 1; 2839 III.ConstantOpNo = 2; 2840 switch(Opc) { 2841 default: llvm_unreachable("Unknown opcode"); 2842 case PPC::LXVX: 2843 III.ImmOpcode = PPC::LXV; 2844 III.ImmMustBeMultipleOf = 16; 2845 break; 2846 case PPC::LXSSPX: 2847 III.ImmOpcode = PPC::LXSSP; 2848 III.ImmMustBeMultipleOf = 4; 2849 break; 2850 case PPC::LXSDX: 2851 III.ImmOpcode = PPC::LXSD; 2852 III.ImmMustBeMultipleOf = 4; 2853 break; 2854 case PPC::STXVX: 2855 III.ImmOpcode = PPC::STXV; 2856 III.ImmMustBeMultipleOf = 16; 2857 break; 2858 case PPC::STXSSPX: 2859 III.ImmOpcode = PPC::STXSSP; 2860 III.ImmMustBeMultipleOf = 4; 2861 break; 2862 case PPC::STXSDX: 2863 III.ImmOpcode = PPC::STXSD; 2864 III.ImmMustBeMultipleOf = 4; 2865 break; 2866 } 2867 break; 2868 } 2869 return true; 2870 } 2871 2872 // Utility function for swaping two arbitrary operands of an instruction. 2873 static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) { 2874 assert(Op1 != Op2 && "Cannot swap operand with itself."); 2875 2876 unsigned MaxOp = std::max(Op1, Op2); 2877 unsigned MinOp = std::min(Op1, Op2); 2878 MachineOperand MOp1 = MI.getOperand(MinOp); 2879 MachineOperand MOp2 = MI.getOperand(MaxOp); 2880 MI.RemoveOperand(std::max(Op1, Op2)); 2881 MI.RemoveOperand(std::min(Op1, Op2)); 2882 2883 // If the operands we are swapping are the two at the end (the common case) 2884 // we can just remove both and add them in the opposite order. 2885 if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) { 2886 MI.addOperand(MOp2); 2887 MI.addOperand(MOp1); 2888 } else { 2889 // Store all operands in a temporary vector, remove them and re-add in the 2890 // right order. 2891 SmallVector<MachineOperand, 2> MOps; 2892 unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops. 2893 for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) { 2894 MOps.push_back(MI.getOperand(i)); 2895 MI.RemoveOperand(i); 2896 } 2897 // MOp2 needs to be added next. 2898 MI.addOperand(MOp2); 2899 // Now add the rest. 2900 for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) { 2901 if (i == MaxOp) 2902 MI.addOperand(MOp1); 2903 else { 2904 MI.addOperand(MOps.back()); 2905 MOps.pop_back(); 2906 } 2907 } 2908 } 2909 } 2910 2911 bool PPCInstrInfo::transformToImmForm(MachineInstr &MI, const ImmInstrInfo &III, 2912 unsigned ConstantOpNo, 2913 int64_t Imm) const { 2914 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2915 bool PostRA = !MRI.isSSA(); 2916 // Exit early if we can't convert this. 2917 if ((ConstantOpNo != III.ConstantOpNo) && !III.IsCommutative) 2918 return false; 2919 if (Imm % III.ImmMustBeMultipleOf) 2920 return false; 2921 if (III.TruncateImmTo) 2922 Imm &= ((1 << III.TruncateImmTo) - 1); 2923 if (III.SignedImm) { 2924 APInt ActualValue(64, Imm, true); 2925 if (!ActualValue.isSignedIntN(III.ImmWidth)) 2926 return false; 2927 } else { 2928 uint64_t UnsignedMax = (1 << III.ImmWidth) - 1; 2929 if ((uint64_t)Imm > UnsignedMax) 2930 return false; 2931 } 2932 2933 // If we're post-RA, the instructions don't agree on whether register zero is 2934 // special, we can transform this as long as the register operand that will 2935 // end up in the location where zero is special isn't R0. 2936 if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { 2937 unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig : 2938 III.ZeroIsSpecialNew + 1; 2939 unsigned OrigZeroReg = MI.getOperand(PosForOrigZero).getReg(); 2940 unsigned NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg(); 2941 // If R0 is in the operand where zero is special for the new instruction, 2942 // it is unsafe to transform if the constant operand isn't that operand. 2943 if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) && 2944 ConstantOpNo != III.ZeroIsSpecialNew) 2945 return false; 2946 if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) && 2947 ConstantOpNo != PosForOrigZero) 2948 return false; 2949 } 2950 2951 unsigned Opc = MI.getOpcode(); 2952 bool SpecialShift32 = 2953 Opc == PPC::SLW || Opc == PPC::SLWo || Opc == PPC::SRW || Opc == PPC::SRWo; 2954 bool SpecialShift64 = 2955 Opc == PPC::SLD || Opc == PPC::SLDo || Opc == PPC::SRD || Opc == PPC::SRDo; 2956 bool SetCR = Opc == PPC::SLWo || Opc == PPC::SRWo || 2957 Opc == PPC::SLDo || Opc == PPC::SRDo; 2958 bool RightShift = 2959 Opc == PPC::SRW || Opc == PPC::SRWo || Opc == PPC::SRD || Opc == PPC::SRDo; 2960 2961 MI.setDesc(get(III.ImmOpcode)); 2962 if (ConstantOpNo == III.ConstantOpNo) { 2963 // Converting shifts to immediate form is a bit tricky since they may do 2964 // one of three things: 2965 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero 2966 // 2. If the shift amount is zero, the result is unchanged (save for maybe 2967 // setting CR0) 2968 // 3. If the shift amount is in [1, OpSize), it's just a shift 2969 if (SpecialShift32 || SpecialShift64) { 2970 LoadImmediateInfo LII; 2971 LII.Imm = 0; 2972 LII.SetCR = SetCR; 2973 LII.Is64Bit = SpecialShift64; 2974 uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F); 2975 if (Imm & (SpecialShift32 ? 0x20 : 0x40)) 2976 replaceInstrWithLI(MI, LII); 2977 // Shifts by zero don't change the value. If we don't need to set CR0, 2978 // just convert this to a COPY. Can't do this post-RA since we've already 2979 // cleaned up the copies. 2980 else if (!SetCR && ShAmt == 0 && !PostRA) { 2981 MI.RemoveOperand(2); 2982 MI.setDesc(get(PPC::COPY)); 2983 } else { 2984 // The 32 bit and 64 bit instructions are quite different. 2985 if (SpecialShift32) { 2986 // Left shifts use (N, 0, 31-N), right shifts use (32-N, N, 31). 2987 uint64_t SH = RightShift ? 32 - ShAmt : ShAmt; 2988 uint64_t MB = RightShift ? ShAmt : 0; 2989 uint64_t ME = RightShift ? 31 : 31 - ShAmt; 2990 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH); 2991 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB) 2992 .addImm(ME); 2993 } else { 2994 // Left shifts use (N, 63-N), right shifts use (64-N, N). 2995 uint64_t SH = RightShift ? 64 - ShAmt : ShAmt; 2996 uint64_t ME = RightShift ? ShAmt : 63 - ShAmt; 2997 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH); 2998 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME); 2999 } 3000 } 3001 } else 3002 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm); 3003 } 3004 // Convert commutative instructions (switch the operands and convert the 3005 // desired one to an immediate. 3006 else if (III.IsCommutative) { 3007 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm); 3008 swapMIOperands(MI, ConstantOpNo, III.ConstantOpNo); 3009 } else 3010 llvm_unreachable("Should have exited early!"); 3011 3012 // For instructions for which the constant register replaces a different 3013 // operand than where the immediate goes, we need to swap them. 3014 if (III.ConstantOpNo != III.ImmOpNo) 3015 swapMIOperands(MI, III.ConstantOpNo, III.ImmOpNo); 3016 3017 // If the R0/X0 register is special for the original instruction and not for 3018 // the new instruction (or vice versa), we need to fix up the register class. 3019 if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { 3020 if (!III.ZeroIsSpecialOrig) { 3021 unsigned RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg(); 3022 const TargetRegisterClass *NewRC = 3023 MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ? 3024 &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass; 3025 MRI.setRegClass(RegToModify, NewRC); 3026 } 3027 } 3028 return true; 3029 } 3030 3031 const TargetRegisterClass * 3032 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const { 3033 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) 3034 return &PPC::VSRCRegClass; 3035 return RC; 3036 } 3037 3038 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) { 3039 return PPC::getRecordFormOpcode(Opcode); 3040 } 3041 3042 // This function returns true if the machine instruction 3043 // always outputs a value by sign-extending a 32 bit value, 3044 // i.e. 0 to 31-th bits are same as 32-th bit. 3045 static bool isSignExtendingOp(const MachineInstr &MI) { 3046 int Opcode = MI.getOpcode(); 3047 if (Opcode == PPC::LI || Opcode == PPC::LI8 || 3048 Opcode == PPC::LIS || Opcode == PPC::LIS8 || 3049 Opcode == PPC::SRAW || Opcode == PPC::SRAWo || 3050 Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo || 3051 Opcode == PPC::LWA || Opcode == PPC::LWAX || 3052 Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 || 3053 Opcode == PPC::LHA || Opcode == PPC::LHAX || 3054 Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 || 3055 Opcode == PPC::LBZ || Opcode == PPC::LBZX || 3056 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || 3057 Opcode == PPC::LBZU || Opcode == PPC::LBZUX || 3058 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || 3059 Opcode == PPC::LHZ || Opcode == PPC::LHZX || 3060 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || 3061 Opcode == PPC::LHZU || Opcode == PPC::LHZUX || 3062 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || 3063 Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo || 3064 Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo || 3065 Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 || 3066 Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo || 3067 Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 || 3068 Opcode == PPC::EXTSB8_32_64) 3069 return true; 3070 3071 if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33) 3072 return true; 3073 3074 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || 3075 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) && 3076 MI.getOperand(3).getImm() > 0 && 3077 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) 3078 return true; 3079 3080 return false; 3081 } 3082 3083 // This function returns true if the machine instruction 3084 // always outputs zeros in higher 32 bits. 3085 static bool isZeroExtendingOp(const MachineInstr &MI) { 3086 int Opcode = MI.getOpcode(); 3087 // The 16-bit immediate is sign-extended in li/lis. 3088 // If the most significant bit is zero, all higher bits are zero. 3089 if (Opcode == PPC::LI || Opcode == PPC::LI8 || 3090 Opcode == PPC::LIS || Opcode == PPC::LIS8) { 3091 int64_t Imm = MI.getOperand(1).getImm(); 3092 if (((uint64_t)Imm & ~0x7FFFuLL) == 0) 3093 return true; 3094 } 3095 3096 // We have some variations of rotate-and-mask instructions 3097 // that clear higher 32-bits. 3098 if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo || 3099 Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo || 3100 Opcode == PPC::RLDICL_32_64) && 3101 MI.getOperand(3).getImm() >= 32) 3102 return true; 3103 3104 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) && 3105 MI.getOperand(3).getImm() >= 32 && 3106 MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm()) 3107 return true; 3108 3109 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || 3110 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo || 3111 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) && 3112 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) 3113 return true; 3114 3115 // There are other instructions that clear higher 32-bits. 3116 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo || 3117 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo || 3118 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 || 3119 Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo || 3120 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo || 3121 Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW || 3122 Opcode == PPC::SLW || Opcode == PPC::SLWo || 3123 Opcode == PPC::SRW || Opcode == PPC::SRWo || 3124 Opcode == PPC::SLW8 || Opcode == PPC::SRW8 || 3125 Opcode == PPC::SLWI || Opcode == PPC::SLWIo || 3126 Opcode == PPC::SRWI || Opcode == PPC::SRWIo || 3127 Opcode == PPC::LWZ || Opcode == PPC::LWZX || 3128 Opcode == PPC::LWZU || Opcode == PPC::LWZUX || 3129 Opcode == PPC::LWBRX || Opcode == PPC::LHBRX || 3130 Opcode == PPC::LHZ || Opcode == PPC::LHZX || 3131 Opcode == PPC::LHZU || Opcode == PPC::LHZUX || 3132 Opcode == PPC::LBZ || Opcode == PPC::LBZX || 3133 Opcode == PPC::LBZU || Opcode == PPC::LBZUX || 3134 Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 || 3135 Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 || 3136 Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 || 3137 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || 3138 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || 3139 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || 3140 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || 3141 Opcode == PPC::ANDIo || Opcode == PPC::ANDISo || 3142 Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo || 3143 Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo || 3144 Opcode == PPC::MFVSRWZ) 3145 return true; 3146 3147 return false; 3148 } 3149 3150 // This function returns true if the input MachineInstr is a TOC save 3151 // instruction. 3152 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const { 3153 if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg()) 3154 return false; 3155 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 3156 unsigned StackOffset = MI.getOperand(1).getImm(); 3157 unsigned StackReg = MI.getOperand(2).getReg(); 3158 if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset) 3159 return true; 3160 3161 return false; 3162 } 3163 3164 // We limit the max depth to track incoming values of PHIs or binary ops 3165 // (e.g. AND) to avoid exsessive cost. 3166 const unsigned MAX_DEPTH = 1; 3167 3168 bool 3169 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt, 3170 const unsigned Depth) const { 3171 const MachineFunction *MF = MI.getParent()->getParent(); 3172 const MachineRegisterInfo *MRI = &MF->getRegInfo(); 3173 3174 // If we know this instruction returns sign- or zero-extended result, 3175 // return true. 3176 if (SignExt ? isSignExtendingOp(MI): 3177 isZeroExtendingOp(MI)) 3178 return true; 3179 3180 switch (MI.getOpcode()) { 3181 case PPC::COPY: { 3182 unsigned SrcReg = MI.getOperand(1).getReg(); 3183 3184 // In both ELFv1 and v2 ABI, method parameters and the return value 3185 // are sign- or zero-extended. 3186 if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) { 3187 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); 3188 // We check the ZExt/SExt flags for a method parameter. 3189 if (MI.getParent()->getBasicBlock() == 3190 &MF->getFunction().getEntryBlock()) { 3191 unsigned VReg = MI.getOperand(0).getReg(); 3192 if (MF->getRegInfo().isLiveIn(VReg)) 3193 return SignExt ? FuncInfo->isLiveInSExt(VReg) : 3194 FuncInfo->isLiveInZExt(VReg); 3195 } 3196 3197 // For a method return value, we check the ZExt/SExt flags in attribute. 3198 // We assume the following code sequence for method call. 3199 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1 3200 // BL8_NOP @func,... 3201 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1 3202 // %5 = COPY %x3; G8RC:%5 3203 if (SrcReg == PPC::X3) { 3204 const MachineBasicBlock *MBB = MI.getParent(); 3205 MachineBasicBlock::const_instr_iterator II = 3206 MachineBasicBlock::const_instr_iterator(&MI); 3207 if (II != MBB->instr_begin() && 3208 (--II)->getOpcode() == PPC::ADJCALLSTACKUP) { 3209 const MachineInstr &CallMI = *(--II); 3210 if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) { 3211 const Function *CalleeFn = 3212 dyn_cast<Function>(CallMI.getOperand(0).getGlobal()); 3213 if (!CalleeFn) 3214 return false; 3215 const IntegerType *IntTy = 3216 dyn_cast<IntegerType>(CalleeFn->getReturnType()); 3217 const AttributeSet &Attrs = 3218 CalleeFn->getAttributes().getRetAttributes(); 3219 if (IntTy && IntTy->getBitWidth() <= 32) 3220 return Attrs.hasAttribute(SignExt ? Attribute::SExt : 3221 Attribute::ZExt); 3222 } 3223 } 3224 } 3225 } 3226 3227 // If this is a copy from another register, we recursively check source. 3228 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) 3229 return false; 3230 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 3231 if (SrcMI != NULL) 3232 return isSignOrZeroExtended(*SrcMI, SignExt, Depth); 3233 3234 return false; 3235 } 3236 3237 case PPC::ANDIo: 3238 case PPC::ANDISo: 3239 case PPC::ORI: 3240 case PPC::ORIS: 3241 case PPC::XORI: 3242 case PPC::XORIS: 3243 case PPC::ANDIo8: 3244 case PPC::ANDISo8: 3245 case PPC::ORI8: 3246 case PPC::ORIS8: 3247 case PPC::XORI8: 3248 case PPC::XORIS8: { 3249 // logical operation with 16-bit immediate does not change the upper bits. 3250 // So, we track the operand register as we do for register copy. 3251 unsigned SrcReg = MI.getOperand(1).getReg(); 3252 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) 3253 return false; 3254 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 3255 if (SrcMI != NULL) 3256 return isSignOrZeroExtended(*SrcMI, SignExt, Depth); 3257 3258 return false; 3259 } 3260 3261 // If all incoming values are sign-/zero-extended, 3262 // the output of OR, ISEL or PHI is also sign-/zero-extended. 3263 case PPC::OR: 3264 case PPC::OR8: 3265 case PPC::ISEL: 3266 case PPC::PHI: { 3267 if (Depth >= MAX_DEPTH) 3268 return false; 3269 3270 // The input registers for PHI are operand 1, 3, ... 3271 // The input registers for others are operand 1 and 2. 3272 unsigned E = 3, D = 1; 3273 if (MI.getOpcode() == PPC::PHI) { 3274 E = MI.getNumOperands(); 3275 D = 2; 3276 } 3277 3278 for (unsigned I = 1; I != E; I += D) { 3279 if (MI.getOperand(I).isReg()) { 3280 unsigned SrcReg = MI.getOperand(I).getReg(); 3281 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) 3282 return false; 3283 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 3284 if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1)) 3285 return false; 3286 } 3287 else 3288 return false; 3289 } 3290 return true; 3291 } 3292 3293 // If at least one of the incoming values of an AND is zero extended 3294 // then the output is also zero-extended. If both of the incoming values 3295 // are sign-extended then the output is also sign extended. 3296 case PPC::AND: 3297 case PPC::AND8: { 3298 if (Depth >= MAX_DEPTH) 3299 return false; 3300 3301 assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg()); 3302 3303 unsigned SrcReg1 = MI.getOperand(1).getReg(); 3304 unsigned SrcReg2 = MI.getOperand(2).getReg(); 3305 3306 if (!TargetRegisterInfo::isVirtualRegister(SrcReg1) || 3307 !TargetRegisterInfo::isVirtualRegister(SrcReg2)) 3308 return false; 3309 3310 const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1); 3311 const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2); 3312 if (!MISrc1 || !MISrc2) 3313 return false; 3314 3315 if(SignExt) 3316 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) && 3317 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); 3318 else 3319 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) || 3320 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); 3321 } 3322 3323 default: 3324 break; 3325 } 3326 return false; 3327 } 3328