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 unsigned PPCInstrInfo::lookThruCopyLike(unsigned SrcReg, 2182 const MachineRegisterInfo *MRI) { 2183 while (true) { 2184 MachineInstr *MI = MRI->getVRegDef(SrcReg); 2185 if (!MI->isCopyLike()) 2186 return SrcReg; 2187 2188 unsigned CopySrcReg; 2189 if (MI->isCopy()) 2190 CopySrcReg = MI->getOperand(1).getReg(); 2191 else { 2192 assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike"); 2193 CopySrcReg = MI->getOperand(2).getReg(); 2194 } 2195 2196 if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg)) 2197 return CopySrcReg; 2198 2199 SrcReg = CopySrcReg; 2200 } 2201 } 2202 2203 // Essentially a compile-time implementation of a compare->isel sequence. 2204 // It takes two constants to compare, along with the true/false registers 2205 // and the comparison type (as a subreg to a CR field) and returns one 2206 // of the true/false registers, depending on the comparison results. 2207 static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc, 2208 unsigned TrueReg, unsigned FalseReg, 2209 unsigned CRSubReg) { 2210 // Signed comparisons. The immediates are assumed to be sign-extended. 2211 if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) { 2212 switch (CRSubReg) { 2213 default: llvm_unreachable("Unknown integer comparison type."); 2214 case PPC::sub_lt: 2215 return Imm1 < Imm2 ? TrueReg : FalseReg; 2216 case PPC::sub_gt: 2217 return Imm1 > Imm2 ? TrueReg : FalseReg; 2218 case PPC::sub_eq: 2219 return Imm1 == Imm2 ? TrueReg : FalseReg; 2220 } 2221 } 2222 // Unsigned comparisons. 2223 else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) { 2224 switch (CRSubReg) { 2225 default: llvm_unreachable("Unknown integer comparison type."); 2226 case PPC::sub_lt: 2227 return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg; 2228 case PPC::sub_gt: 2229 return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg; 2230 case PPC::sub_eq: 2231 return Imm1 == Imm2 ? TrueReg : FalseReg; 2232 } 2233 } 2234 return PPC::NoRegister; 2235 } 2236 2237 // Replace an instruction with one that materializes a constant (and sets 2238 // CR0 if the original instruction was a record-form instruction). 2239 void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI, 2240 const LoadImmediateInfo &LII) const { 2241 // Remove existing operands. 2242 int OperandToKeep = LII.SetCR ? 1 : 0; 2243 for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--) 2244 MI.RemoveOperand(i); 2245 2246 // Replace the instruction. 2247 if (LII.SetCR) { 2248 MI.setDesc(get(LII.Is64Bit ? PPC::ANDIo8 : PPC::ANDIo)); 2249 // Set the immediate. 2250 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2251 .addImm(LII.Imm).addReg(PPC::CR0, RegState::ImplicitDefine); 2252 return; 2253 } 2254 else 2255 MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI)); 2256 2257 // Set the immediate. 2258 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 2259 .addImm(LII.Imm); 2260 } 2261 2262 MachineInstr *PPCInstrInfo::getConstantDefMI(MachineInstr &MI, 2263 unsigned &ConstOp, 2264 bool &SeenIntermediateUse) const { 2265 ConstOp = ~0U; 2266 MachineInstr *DefMI = nullptr; 2267 MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo(); 2268 // If we'ere in SSA, get the defs through the MRI. Otherwise, only look 2269 // within the basic block to see if the register is defined using an LI/LI8. 2270 if (MRI->isSSA()) { 2271 for (int i = 1, e = MI.getNumOperands(); i < e; i++) { 2272 if (!MI.getOperand(i).isReg()) 2273 continue; 2274 unsigned Reg = MI.getOperand(i).getReg(); 2275 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 2276 continue; 2277 unsigned TrueReg = lookThruCopyLike(Reg, MRI); 2278 if (TargetRegisterInfo::isVirtualRegister(TrueReg)) { 2279 DefMI = MRI->getVRegDef(TrueReg); 2280 if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8) { 2281 ConstOp = i; 2282 break; 2283 } 2284 } 2285 } 2286 } else { 2287 // Looking back through the definition for each operand could be expensive, 2288 // so exit early if this isn't an instruction that either has an immediate 2289 // form or is already an immediate form that we can handle. 2290 ImmInstrInfo III; 2291 unsigned Opc = MI.getOpcode(); 2292 bool ConvertibleImmForm = 2293 Opc == PPC::CMPWI || Opc == PPC::CMPLWI || 2294 Opc == PPC::CMPDI || Opc == PPC::CMPLDI || 2295 Opc == PPC::ADDI || Opc == PPC::ADDI8 || 2296 Opc == PPC::ORI || Opc == PPC::ORI8 || 2297 Opc == PPC::XORI || Opc == PPC::XORI8 || 2298 Opc == PPC::RLDICL || Opc == PPC::RLDICLo || 2299 Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 || 2300 Opc == PPC::RLWINM || Opc == PPC::RLWINMo || 2301 Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o; 2302 if (!instrHasImmForm(MI, III) && !ConvertibleImmForm) 2303 return nullptr; 2304 2305 // Don't convert or %X, %Y, %Y since that's just a register move. 2306 if ((Opc == PPC::OR || Opc == PPC::OR8) && 2307 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 2308 return nullptr; 2309 for (int i = 1, e = MI.getNumOperands(); i < e; i++) { 2310 MachineOperand &MO = MI.getOperand(i); 2311 SeenIntermediateUse = false; 2312 if (MO.isReg() && MO.isUse() && !MO.isImplicit()) { 2313 MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI; 2314 It++; 2315 unsigned Reg = MI.getOperand(i).getReg(); 2316 // MachineInstr::readsRegister only returns true if the machine 2317 // instruction reads the exact register or its super-register. It 2318 // does not consider uses of sub-registers which seems like strange 2319 // behaviour. Nonetheless, if we end up with a 64-bit register here, 2320 // get the corresponding 32-bit register to check. 2321 if (PPC::G8RCRegClass.contains(Reg)) 2322 Reg = Reg - PPC::X0 + PPC::R0; 2323 2324 // Is this register defined by a load-immediate in this block? 2325 for ( ; It != E; ++It) { 2326 if (It->modifiesRegister(Reg, &getRegisterInfo())) { 2327 if (It->getOpcode() == PPC::LI || It->getOpcode() == PPC::LI8) { 2328 ConstOp = i; 2329 return &*It; 2330 } else 2331 break; 2332 } else if (It->readsRegister(Reg, &getRegisterInfo())) 2333 // If we see another use of this reg between the def and the MI, 2334 // we want to flat it so the def isn't deleted. 2335 SeenIntermediateUse = true; 2336 } 2337 } 2338 } 2339 } 2340 return ConstOp == ~0U ? nullptr : DefMI; 2341 } 2342 2343 // If this instruction has an immediate form and one of its operands is a 2344 // result of a load-immediate, convert it to the immediate form if the constant 2345 // is in range. 2346 bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI, 2347 MachineInstr **KilledDef) const { 2348 MachineFunction *MF = MI.getParent()->getParent(); 2349 MachineRegisterInfo *MRI = &MF->getRegInfo(); 2350 bool PostRA = !MRI->isSSA(); 2351 bool SeenIntermediateUse = true; 2352 unsigned ConstantOperand = ~0U; 2353 MachineInstr *DefMI = getConstantDefMI(MI, ConstantOperand, 2354 SeenIntermediateUse); 2355 if (!DefMI || !DefMI->getOperand(1).isImm()) 2356 return false; 2357 assert(ConstantOperand < MI.getNumOperands() && 2358 "The constant operand needs to be valid at this point"); 2359 2360 int64_t Immediate = DefMI->getOperand(1).getImm(); 2361 // Sign-extend to 64-bits. 2362 int64_t SExtImm = ((uint64_t)Immediate & ~0x7FFFuLL) != 0 ? 2363 (Immediate | 0xFFFFFFFFFFFF0000) : Immediate; 2364 2365 if (KilledDef && MI.getOperand(ConstantOperand).isKill() && 2366 !SeenIntermediateUse) 2367 *KilledDef = DefMI; 2368 2369 // If this is a reg+reg instruction that has a reg+imm form, convert it now. 2370 ImmInstrInfo III; 2371 if (instrHasImmForm(MI, III)) 2372 return transformToImmForm(MI, III, ConstantOperand, SExtImm); 2373 2374 bool ReplaceWithLI = false; 2375 bool Is64BitLI = false; 2376 int64_t NewImm = 0; 2377 bool SetCR = false; 2378 unsigned Opc = MI.getOpcode(); 2379 switch (Opc) { 2380 default: return false; 2381 2382 // FIXME: Any branches conditional on such a comparison can be made 2383 // unconditional. At this time, this happens too infrequently to be worth 2384 // the implementation effort, but if that ever changes, we could convert 2385 // such a pattern here. 2386 case PPC::CMPWI: 2387 case PPC::CMPLWI: 2388 case PPC::CMPDI: 2389 case PPC::CMPLDI: { 2390 // Doing this post-RA would require dataflow analysis to reliably find uses 2391 // of the CR register set by the compare. 2392 if (PostRA) 2393 return false; 2394 // If a compare-immediate is fed by an immediate and is itself an input of 2395 // an ISEL (the most common case) into a COPY of the correct register. 2396 bool Changed = false; 2397 unsigned DefReg = MI.getOperand(0).getReg(); 2398 int64_t Comparand = MI.getOperand(2).getImm(); 2399 int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0 ? 2400 (Comparand | 0xFFFFFFFFFFFF0000) : Comparand; 2401 2402 for (auto &CompareUseMI : MRI->use_instructions(DefReg)) { 2403 unsigned UseOpc = CompareUseMI.getOpcode(); 2404 if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8) 2405 continue; 2406 unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg(); 2407 unsigned TrueReg = CompareUseMI.getOperand(1).getReg(); 2408 unsigned FalseReg = CompareUseMI.getOperand(2).getReg(); 2409 unsigned RegToCopy = selectReg(SExtImm, SExtComparand, Opc, TrueReg, 2410 FalseReg, CRSubReg); 2411 if (RegToCopy == PPC::NoRegister) 2412 continue; 2413 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0. 2414 if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) { 2415 CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI)); 2416 CompareUseMI.getOperand(1).ChangeToImmediate(0); 2417 CompareUseMI.RemoveOperand(3); 2418 CompareUseMI.RemoveOperand(2); 2419 continue; 2420 } 2421 DEBUG(dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n"); 2422 DEBUG(DefMI->dump(); MI.dump(); CompareUseMI.dump()); 2423 DEBUG(dbgs() << "Is converted to:\n"); 2424 // Convert to copy and remove unneeded operands. 2425 CompareUseMI.setDesc(get(PPC::COPY)); 2426 CompareUseMI.RemoveOperand(3); 2427 CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1); 2428 CmpIselsConverted++; 2429 Changed = true; 2430 DEBUG(CompareUseMI.dump()); 2431 } 2432 if (Changed) 2433 return true; 2434 // This may end up incremented multiple times since this function is called 2435 // during a fixed-point transformation, but it is only meant to indicate the 2436 // presence of this opportunity. 2437 MissedConvertibleImmediateInstrs++; 2438 return false; 2439 } 2440 2441 // Immediate forms - may simply be convertable to an LI. 2442 case PPC::ADDI: 2443 case PPC::ADDI8: { 2444 // Does the sum fit in a 16-bit signed field? 2445 int64_t Addend = MI.getOperand(2).getImm(); 2446 if (isInt<16>(Addend + SExtImm)) { 2447 ReplaceWithLI = true; 2448 Is64BitLI = Opc == PPC::ADDI8; 2449 NewImm = Addend + SExtImm; 2450 break; 2451 } 2452 return false; 2453 } 2454 case PPC::RLDICL: 2455 case PPC::RLDICLo: 2456 case PPC::RLDICL_32: 2457 case PPC::RLDICL_32_64: { 2458 // Use APInt's rotate function. 2459 int64_t SH = MI.getOperand(2).getImm(); 2460 int64_t MB = MI.getOperand(3).getImm(); 2461 APInt InVal(Opc == PPC::RLDICL ? 64 : 32, SExtImm, true); 2462 InVal = InVal.rotl(SH); 2463 uint64_t Mask = (1LLU << (63 - MB + 1)) - 1; 2464 InVal &= Mask; 2465 // Can't replace negative values with an LI as that will sign-extend 2466 // and not clear the left bits. If we're setting the CR bit, we will use 2467 // ANDIo which won't sign extend, so that's safe. 2468 if (isUInt<15>(InVal.getSExtValue()) || 2469 (Opc == PPC::RLDICLo && isUInt<16>(InVal.getSExtValue()))) { 2470 ReplaceWithLI = true; 2471 Is64BitLI = Opc != PPC::RLDICL_32; 2472 NewImm = InVal.getSExtValue(); 2473 SetCR = Opc == PPC::RLDICLo; 2474 if (SetCR && (SExtImm & NewImm) != NewImm) 2475 return false; 2476 break; 2477 } 2478 return false; 2479 } 2480 case PPC::RLWINM: 2481 case PPC::RLWINM8: 2482 case PPC::RLWINMo: 2483 case PPC::RLWINM8o: { 2484 int64_t SH = MI.getOperand(2).getImm(); 2485 int64_t MB = MI.getOperand(3).getImm(); 2486 int64_t ME = MI.getOperand(4).getImm(); 2487 APInt InVal(32, SExtImm, true); 2488 InVal = InVal.rotl(SH); 2489 // Set the bits ( MB + 32 ) to ( ME + 32 ). 2490 uint64_t Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1); 2491 InVal &= Mask; 2492 // Can't replace negative values with an LI as that will sign-extend 2493 // and not clear the left bits. If we're setting the CR bit, we will use 2494 // ANDIo which won't sign extend, so that's safe. 2495 bool ValueFits = isUInt<15>(InVal.getSExtValue()); 2496 ValueFits |= ((Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o) && 2497 isUInt<16>(InVal.getSExtValue())); 2498 if (ValueFits) { 2499 ReplaceWithLI = true; 2500 Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o; 2501 NewImm = InVal.getSExtValue(); 2502 SetCR = Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o; 2503 if (SetCR && (SExtImm & NewImm) != NewImm) 2504 return false; 2505 break; 2506 } 2507 return false; 2508 } 2509 case PPC::ORI: 2510 case PPC::ORI8: 2511 case PPC::XORI: 2512 case PPC::XORI8: { 2513 int64_t LogicalImm = MI.getOperand(2).getImm(); 2514 int64_t Result = 0; 2515 if (Opc == PPC::ORI || Opc == PPC::ORI8) 2516 Result = LogicalImm | SExtImm; 2517 else 2518 Result = LogicalImm ^ SExtImm; 2519 if (isInt<16>(Result)) { 2520 ReplaceWithLI = true; 2521 Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8; 2522 NewImm = Result; 2523 break; 2524 } 2525 return false; 2526 } 2527 } 2528 2529 if (ReplaceWithLI) { 2530 DEBUG(dbgs() << "Replacing instruction:\n"); 2531 DEBUG(MI.dump()); 2532 DEBUG(dbgs() << "Fed by:\n"); 2533 DEBUG(DefMI->dump()); 2534 LoadImmediateInfo LII; 2535 LII.Imm = NewImm; 2536 LII.Is64Bit = Is64BitLI; 2537 LII.SetCR = SetCR; 2538 // If we're setting the CR, the original load-immediate must be kept (as an 2539 // operand to ANDIo/ANDI8o). 2540 if (KilledDef && SetCR) 2541 *KilledDef = nullptr; 2542 replaceInstrWithLI(MI, LII); 2543 DEBUG(dbgs() << "With:\n"); 2544 DEBUG(MI.dump()); 2545 return true; 2546 } 2547 return false; 2548 } 2549 2550 bool PPCInstrInfo::instrHasImmForm(const MachineInstr &MI, 2551 ImmInstrInfo &III) const { 2552 unsigned Opc = MI.getOpcode(); 2553 // The vast majority of the instructions would need their operand 2 replaced 2554 // with an immediate when switching to the reg+imm form. A marked exception 2555 // are the update form loads/stores for which a constant operand 2 would need 2556 // to turn into a displacement and move operand 1 to the operand 2 position. 2557 III.ImmOpNo = 2; 2558 III.ConstantOpNo = 2; 2559 III.ImmWidth = 16; 2560 III.ImmMustBeMultipleOf = 1; 2561 III.TruncateImmTo = 0; 2562 switch (Opc) { 2563 default: return false; 2564 case PPC::ADD4: 2565 case PPC::ADD8: 2566 III.SignedImm = true; 2567 III.ZeroIsSpecialOrig = 0; 2568 III.ZeroIsSpecialNew = 1; 2569 III.IsCommutative = true; 2570 III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8; 2571 break; 2572 case PPC::ADDC: 2573 case PPC::ADDC8: 2574 III.SignedImm = true; 2575 III.ZeroIsSpecialOrig = 0; 2576 III.ZeroIsSpecialNew = 0; 2577 III.IsCommutative = true; 2578 III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8; 2579 break; 2580 case PPC::ADDCo: 2581 III.SignedImm = true; 2582 III.ZeroIsSpecialOrig = 0; 2583 III.ZeroIsSpecialNew = 0; 2584 III.IsCommutative = true; 2585 III.ImmOpcode = PPC::ADDICo; 2586 break; 2587 case PPC::SUBFC: 2588 case PPC::SUBFC8: 2589 III.SignedImm = true; 2590 III.ZeroIsSpecialOrig = 0; 2591 III.ZeroIsSpecialNew = 0; 2592 III.IsCommutative = false; 2593 III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8; 2594 break; 2595 case PPC::CMPW: 2596 case PPC::CMPD: 2597 III.SignedImm = true; 2598 III.ZeroIsSpecialOrig = 0; 2599 III.ZeroIsSpecialNew = 0; 2600 III.IsCommutative = false; 2601 III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI; 2602 break; 2603 case PPC::CMPLW: 2604 case PPC::CMPLD: 2605 III.SignedImm = false; 2606 III.ZeroIsSpecialOrig = 0; 2607 III.ZeroIsSpecialNew = 0; 2608 III.IsCommutative = false; 2609 III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI; 2610 break; 2611 case PPC::ANDo: 2612 case PPC::AND8o: 2613 case PPC::OR: 2614 case PPC::OR8: 2615 case PPC::XOR: 2616 case PPC::XOR8: 2617 III.SignedImm = false; 2618 III.ZeroIsSpecialOrig = 0; 2619 III.ZeroIsSpecialNew = 0; 2620 III.IsCommutative = true; 2621 switch(Opc) { 2622 default: llvm_unreachable("Unknown opcode"); 2623 case PPC::ANDo: III.ImmOpcode = PPC::ANDIo; break; 2624 case PPC::AND8o: III.ImmOpcode = PPC::ANDIo8; break; 2625 case PPC::OR: III.ImmOpcode = PPC::ORI; break; 2626 case PPC::OR8: III.ImmOpcode = PPC::ORI8; break; 2627 case PPC::XOR: III.ImmOpcode = PPC::XORI; break; 2628 case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break; 2629 } 2630 break; 2631 case PPC::RLWNM: 2632 case PPC::RLWNM8: 2633 case PPC::RLWNMo: 2634 case PPC::RLWNM8o: 2635 case PPC::SLW: 2636 case PPC::SLW8: 2637 case PPC::SLWo: 2638 case PPC::SLW8o: 2639 case PPC::SRW: 2640 case PPC::SRW8: 2641 case PPC::SRWo: 2642 case PPC::SRW8o: 2643 case PPC::SRAW: 2644 case PPC::SRAWo: 2645 III.SignedImm = false; 2646 III.ZeroIsSpecialOrig = 0; 2647 III.ZeroIsSpecialNew = 0; 2648 III.IsCommutative = false; 2649 // This isn't actually true, but the instructions ignore any of the 2650 // upper bits, so any immediate loaded with an LI is acceptable. 2651 // This does not apply to shift right algebraic because a value 2652 // out of range will produce a -1/0. 2653 III.ImmWidth = 16; 2654 if (Opc == PPC::RLWNM || Opc == PPC::RLWNM8 || 2655 Opc == PPC::RLWNMo || Opc == PPC::RLWNM8o) 2656 III.TruncateImmTo = 5; 2657 else 2658 III.TruncateImmTo = 6; 2659 switch(Opc) { 2660 default: llvm_unreachable("Unknown opcode"); 2661 case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break; 2662 case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break; 2663 case PPC::RLWNMo: III.ImmOpcode = PPC::RLWINMo; break; 2664 case PPC::RLWNM8o: III.ImmOpcode = PPC::RLWINM8o; break; 2665 case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break; 2666 case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break; 2667 case PPC::SLWo: III.ImmOpcode = PPC::RLWINMo; break; 2668 case PPC::SLW8o: III.ImmOpcode = PPC::RLWINM8o; break; 2669 case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break; 2670 case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break; 2671 case PPC::SRWo: III.ImmOpcode = PPC::RLWINMo; break; 2672 case PPC::SRW8o: III.ImmOpcode = PPC::RLWINM8o; break; 2673 case PPC::SRAW: 2674 III.ImmWidth = 5; 2675 III.TruncateImmTo = 0; 2676 III.ImmOpcode = PPC::SRAWI; 2677 break; 2678 case PPC::SRAWo: 2679 III.ImmWidth = 5; 2680 III.TruncateImmTo = 0; 2681 III.ImmOpcode = PPC::SRAWIo; 2682 break; 2683 } 2684 break; 2685 case PPC::RLDCL: 2686 case PPC::RLDCLo: 2687 case PPC::RLDCR: 2688 case PPC::RLDCRo: 2689 case PPC::SLD: 2690 case PPC::SLDo: 2691 case PPC::SRD: 2692 case PPC::SRDo: 2693 case PPC::SRAD: 2694 case PPC::SRADo: 2695 III.SignedImm = false; 2696 III.ZeroIsSpecialOrig = 0; 2697 III.ZeroIsSpecialNew = 0; 2698 III.IsCommutative = false; 2699 // This isn't actually true, but the instructions ignore any of the 2700 // upper bits, so any immediate loaded with an LI is acceptable. 2701 // This does not apply to shift right algebraic because a value 2702 // out of range will produce a -1/0. 2703 III.ImmWidth = 16; 2704 if (Opc == PPC::RLDCL || Opc == PPC::RLDCLo || 2705 Opc == PPC::RLDCR || Opc == PPC::RLDCRo) 2706 III.TruncateImmTo = 6; 2707 else 2708 III.TruncateImmTo = 7; 2709 switch(Opc) { 2710 default: llvm_unreachable("Unknown opcode"); 2711 case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break; 2712 case PPC::RLDCLo: III.ImmOpcode = PPC::RLDICLo; break; 2713 case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break; 2714 case PPC::RLDCRo: III.ImmOpcode = PPC::RLDICRo; break; 2715 case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break; 2716 case PPC::SLDo: III.ImmOpcode = PPC::RLDICRo; break; 2717 case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break; 2718 case PPC::SRDo: III.ImmOpcode = PPC::RLDICLo; break; 2719 case PPC::SRAD: 2720 III.ImmWidth = 6; 2721 III.TruncateImmTo = 0; 2722 III.ImmOpcode = PPC::SRADI; 2723 break; 2724 case PPC::SRADo: 2725 III.ImmWidth = 6; 2726 III.TruncateImmTo = 0; 2727 III.ImmOpcode = PPC::SRADIo; 2728 break; 2729 } 2730 break; 2731 // Loads and stores: 2732 case PPC::LBZX: 2733 case PPC::LBZX8: 2734 case PPC::LHZX: 2735 case PPC::LHZX8: 2736 case PPC::LHAX: 2737 case PPC::LHAX8: 2738 case PPC::LWZX: 2739 case PPC::LWZX8: 2740 case PPC::LWAX: 2741 case PPC::LDX: 2742 case PPC::LFSX: 2743 case PPC::LFDX: 2744 case PPC::STBX: 2745 case PPC::STBX8: 2746 case PPC::STHX: 2747 case PPC::STHX8: 2748 case PPC::STWX: 2749 case PPC::STWX8: 2750 case PPC::STDX: 2751 case PPC::STFSX: 2752 case PPC::STFDX: 2753 III.SignedImm = true; 2754 III.ZeroIsSpecialOrig = 1; 2755 III.ZeroIsSpecialNew = 2; 2756 III.IsCommutative = true; 2757 III.ImmOpNo = 1; 2758 III.ConstantOpNo = 2; 2759 switch(Opc) { 2760 default: llvm_unreachable("Unknown opcode"); 2761 case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break; 2762 case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break; 2763 case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break; 2764 case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break; 2765 case PPC::LHAX: III.ImmOpcode = PPC::LHA; break; 2766 case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break; 2767 case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break; 2768 case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break; 2769 case PPC::LWAX: 2770 III.ImmOpcode = PPC::LWA; 2771 III.ImmMustBeMultipleOf = 4; 2772 break; 2773 case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break; 2774 case PPC::LFSX: III.ImmOpcode = PPC::LFS; break; 2775 case PPC::LFDX: III.ImmOpcode = PPC::LFD; break; 2776 case PPC::STBX: III.ImmOpcode = PPC::STB; break; 2777 case PPC::STBX8: III.ImmOpcode = PPC::STB8; break; 2778 case PPC::STHX: III.ImmOpcode = PPC::STH; break; 2779 case PPC::STHX8: III.ImmOpcode = PPC::STH8; break; 2780 case PPC::STWX: III.ImmOpcode = PPC::STW; break; 2781 case PPC::STWX8: III.ImmOpcode = PPC::STW8; break; 2782 case PPC::STDX: 2783 III.ImmOpcode = PPC::STD; 2784 III.ImmMustBeMultipleOf = 4; 2785 break; 2786 case PPC::STFSX: III.ImmOpcode = PPC::STFS; break; 2787 case PPC::STFDX: III.ImmOpcode = PPC::STFD; break; 2788 } 2789 break; 2790 case PPC::LBZUX: 2791 case PPC::LBZUX8: 2792 case PPC::LHZUX: 2793 case PPC::LHZUX8: 2794 case PPC::LHAUX: 2795 case PPC::LHAUX8: 2796 case PPC::LWZUX: 2797 case PPC::LWZUX8: 2798 case PPC::LDUX: 2799 case PPC::LFSUX: 2800 case PPC::LFDUX: 2801 case PPC::STBUX: 2802 case PPC::STBUX8: 2803 case PPC::STHUX: 2804 case PPC::STHUX8: 2805 case PPC::STWUX: 2806 case PPC::STWUX8: 2807 case PPC::STDUX: 2808 case PPC::STFSUX: 2809 case PPC::STFDUX: 2810 III.SignedImm = true; 2811 III.ZeroIsSpecialOrig = 2; 2812 III.ZeroIsSpecialNew = 3; 2813 III.IsCommutative = false; 2814 III.ImmOpNo = 2; 2815 III.ConstantOpNo = 3; 2816 switch(Opc) { 2817 default: llvm_unreachable("Unknown opcode"); 2818 case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break; 2819 case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break; 2820 case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break; 2821 case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break; 2822 case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break; 2823 case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break; 2824 case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break; 2825 case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break; 2826 case PPC::LDUX: 2827 III.ImmOpcode = PPC::LDU; 2828 III.ImmMustBeMultipleOf = 4; 2829 break; 2830 case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break; 2831 case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break; 2832 case PPC::STBUX: III.ImmOpcode = PPC::STBU; break; 2833 case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break; 2834 case PPC::STHUX: III.ImmOpcode = PPC::STHU; break; 2835 case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break; 2836 case PPC::STWUX: III.ImmOpcode = PPC::STWU; break; 2837 case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break; 2838 case PPC::STDUX: 2839 III.ImmOpcode = PPC::STDU; 2840 III.ImmMustBeMultipleOf = 4; 2841 break; 2842 case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break; 2843 case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break; 2844 } 2845 break; 2846 // Power9 only. 2847 case PPC::LXVX: 2848 case PPC::LXSSPX: 2849 case PPC::LXSDX: 2850 case PPC::STXVX: 2851 case PPC::STXSSPX: 2852 case PPC::STXSDX: 2853 if (!Subtarget.hasP9Vector()) 2854 return false; 2855 III.SignedImm = true; 2856 III.ZeroIsSpecialOrig = 1; 2857 III.ZeroIsSpecialNew = 2; 2858 III.IsCommutative = true; 2859 III.ImmOpNo = 1; 2860 III.ConstantOpNo = 2; 2861 switch(Opc) { 2862 default: llvm_unreachable("Unknown opcode"); 2863 case PPC::LXVX: 2864 III.ImmOpcode = PPC::LXV; 2865 III.ImmMustBeMultipleOf = 16; 2866 break; 2867 case PPC::LXSSPX: 2868 III.ImmOpcode = PPC::LXSSP; 2869 III.ImmMustBeMultipleOf = 4; 2870 break; 2871 case PPC::LXSDX: 2872 III.ImmOpcode = PPC::LXSD; 2873 III.ImmMustBeMultipleOf = 4; 2874 break; 2875 case PPC::STXVX: 2876 III.ImmOpcode = PPC::STXV; 2877 III.ImmMustBeMultipleOf = 16; 2878 break; 2879 case PPC::STXSSPX: 2880 III.ImmOpcode = PPC::STXSSP; 2881 III.ImmMustBeMultipleOf = 4; 2882 break; 2883 case PPC::STXSDX: 2884 III.ImmOpcode = PPC::STXSD; 2885 III.ImmMustBeMultipleOf = 4; 2886 break; 2887 } 2888 break; 2889 } 2890 return true; 2891 } 2892 2893 // Utility function for swaping two arbitrary operands of an instruction. 2894 static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) { 2895 assert(Op1 != Op2 && "Cannot swap operand with itself."); 2896 2897 unsigned MaxOp = std::max(Op1, Op2); 2898 unsigned MinOp = std::min(Op1, Op2); 2899 MachineOperand MOp1 = MI.getOperand(MinOp); 2900 MachineOperand MOp2 = MI.getOperand(MaxOp); 2901 MI.RemoveOperand(std::max(Op1, Op2)); 2902 MI.RemoveOperand(std::min(Op1, Op2)); 2903 2904 // If the operands we are swapping are the two at the end (the common case) 2905 // we can just remove both and add them in the opposite order. 2906 if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) { 2907 MI.addOperand(MOp2); 2908 MI.addOperand(MOp1); 2909 } else { 2910 // Store all operands in a temporary vector, remove them and re-add in the 2911 // right order. 2912 SmallVector<MachineOperand, 2> MOps; 2913 unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops. 2914 for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) { 2915 MOps.push_back(MI.getOperand(i)); 2916 MI.RemoveOperand(i); 2917 } 2918 // MOp2 needs to be added next. 2919 MI.addOperand(MOp2); 2920 // Now add the rest. 2921 for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) { 2922 if (i == MaxOp) 2923 MI.addOperand(MOp1); 2924 else { 2925 MI.addOperand(MOps.back()); 2926 MOps.pop_back(); 2927 } 2928 } 2929 } 2930 } 2931 2932 bool PPCInstrInfo::transformToImmForm(MachineInstr &MI, const ImmInstrInfo &III, 2933 unsigned ConstantOpNo, 2934 int64_t Imm) const { 2935 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2936 bool PostRA = !MRI.isSSA(); 2937 // Exit early if we can't convert this. 2938 if ((ConstantOpNo != III.ConstantOpNo) && !III.IsCommutative) 2939 return false; 2940 if (Imm % III.ImmMustBeMultipleOf) 2941 return false; 2942 if (III.TruncateImmTo) 2943 Imm &= ((1 << III.TruncateImmTo) - 1); 2944 if (III.SignedImm) { 2945 APInt ActualValue(64, Imm, true); 2946 if (!ActualValue.isSignedIntN(III.ImmWidth)) 2947 return false; 2948 } else { 2949 uint64_t UnsignedMax = (1 << III.ImmWidth) - 1; 2950 if ((uint64_t)Imm > UnsignedMax) 2951 return false; 2952 } 2953 2954 // If we're post-RA, the instructions don't agree on whether register zero is 2955 // special, we can transform this as long as the register operand that will 2956 // end up in the location where zero is special isn't R0. 2957 if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { 2958 unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig : 2959 III.ZeroIsSpecialNew + 1; 2960 unsigned OrigZeroReg = MI.getOperand(PosForOrigZero).getReg(); 2961 unsigned NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg(); 2962 // If R0 is in the operand where zero is special for the new instruction, 2963 // it is unsafe to transform if the constant operand isn't that operand. 2964 if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) && 2965 ConstantOpNo != III.ZeroIsSpecialNew) 2966 return false; 2967 if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) && 2968 ConstantOpNo != PosForOrigZero) 2969 return false; 2970 } 2971 2972 unsigned Opc = MI.getOpcode(); 2973 bool SpecialShift32 = 2974 Opc == PPC::SLW || Opc == PPC::SLWo || Opc == PPC::SRW || Opc == PPC::SRWo; 2975 bool SpecialShift64 = 2976 Opc == PPC::SLD || Opc == PPC::SLDo || Opc == PPC::SRD || Opc == PPC::SRDo; 2977 bool SetCR = Opc == PPC::SLWo || Opc == PPC::SRWo || 2978 Opc == PPC::SLDo || Opc == PPC::SRDo; 2979 bool RightShift = 2980 Opc == PPC::SRW || Opc == PPC::SRWo || Opc == PPC::SRD || Opc == PPC::SRDo; 2981 2982 MI.setDesc(get(III.ImmOpcode)); 2983 if (ConstantOpNo == III.ConstantOpNo) { 2984 // Converting shifts to immediate form is a bit tricky since they may do 2985 // one of three things: 2986 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero 2987 // 2. If the shift amount is zero, the result is unchanged (save for maybe 2988 // setting CR0) 2989 // 3. If the shift amount is in [1, OpSize), it's just a shift 2990 if (SpecialShift32 || SpecialShift64) { 2991 LoadImmediateInfo LII; 2992 LII.Imm = 0; 2993 LII.SetCR = SetCR; 2994 LII.Is64Bit = SpecialShift64; 2995 uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F); 2996 if (Imm & (SpecialShift32 ? 0x20 : 0x40)) 2997 replaceInstrWithLI(MI, LII); 2998 // Shifts by zero don't change the value. If we don't need to set CR0, 2999 // just convert this to a COPY. Can't do this post-RA since we've already 3000 // cleaned up the copies. 3001 else if (!SetCR && ShAmt == 0 && !PostRA) { 3002 MI.RemoveOperand(2); 3003 MI.setDesc(get(PPC::COPY)); 3004 } else { 3005 // The 32 bit and 64 bit instructions are quite different. 3006 if (SpecialShift32) { 3007 // Left shifts use (N, 0, 31-N), right shifts use (32-N, N, 31). 3008 uint64_t SH = RightShift ? 32 - ShAmt : ShAmt; 3009 uint64_t MB = RightShift ? ShAmt : 0; 3010 uint64_t ME = RightShift ? 31 : 31 - ShAmt; 3011 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH); 3012 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB) 3013 .addImm(ME); 3014 } else { 3015 // Left shifts use (N, 63-N), right shifts use (64-N, N). 3016 uint64_t SH = RightShift ? 64 - ShAmt : ShAmt; 3017 uint64_t ME = RightShift ? ShAmt : 63 - ShAmt; 3018 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH); 3019 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME); 3020 } 3021 } 3022 } else 3023 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm); 3024 } 3025 // Convert commutative instructions (switch the operands and convert the 3026 // desired one to an immediate. 3027 else if (III.IsCommutative) { 3028 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm); 3029 swapMIOperands(MI, ConstantOpNo, III.ConstantOpNo); 3030 } else 3031 llvm_unreachable("Should have exited early!"); 3032 3033 // For instructions for which the constant register replaces a different 3034 // operand than where the immediate goes, we need to swap them. 3035 if (III.ConstantOpNo != III.ImmOpNo) 3036 swapMIOperands(MI, III.ConstantOpNo, III.ImmOpNo); 3037 3038 // If the R0/X0 register is special for the original instruction and not for 3039 // the new instruction (or vice versa), we need to fix up the register class. 3040 if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { 3041 if (!III.ZeroIsSpecialOrig) { 3042 unsigned RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg(); 3043 const TargetRegisterClass *NewRC = 3044 MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ? 3045 &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass; 3046 MRI.setRegClass(RegToModify, NewRC); 3047 } 3048 } 3049 return true; 3050 } 3051 3052 const TargetRegisterClass * 3053 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const { 3054 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) 3055 return &PPC::VSRCRegClass; 3056 return RC; 3057 } 3058 3059 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) { 3060 return PPC::getRecordFormOpcode(Opcode); 3061 } 3062 3063 // This function returns true if the machine instruction 3064 // always outputs a value by sign-extending a 32 bit value, 3065 // i.e. 0 to 31-th bits are same as 32-th bit. 3066 static bool isSignExtendingOp(const MachineInstr &MI) { 3067 int Opcode = MI.getOpcode(); 3068 if (Opcode == PPC::LI || Opcode == PPC::LI8 || 3069 Opcode == PPC::LIS || Opcode == PPC::LIS8 || 3070 Opcode == PPC::SRAW || Opcode == PPC::SRAWo || 3071 Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo || 3072 Opcode == PPC::LWA || Opcode == PPC::LWAX || 3073 Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 || 3074 Opcode == PPC::LHA || Opcode == PPC::LHAX || 3075 Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 || 3076 Opcode == PPC::LBZ || Opcode == PPC::LBZX || 3077 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || 3078 Opcode == PPC::LBZU || Opcode == PPC::LBZUX || 3079 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || 3080 Opcode == PPC::LHZ || Opcode == PPC::LHZX || 3081 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || 3082 Opcode == PPC::LHZU || Opcode == PPC::LHZUX || 3083 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || 3084 Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo || 3085 Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo || 3086 Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 || 3087 Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo || 3088 Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 || 3089 Opcode == PPC::EXTSB8_32_64) 3090 return true; 3091 3092 if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33) 3093 return true; 3094 3095 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || 3096 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) && 3097 MI.getOperand(3).getImm() > 0 && 3098 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) 3099 return true; 3100 3101 return false; 3102 } 3103 3104 // This function returns true if the machine instruction 3105 // always outputs zeros in higher 32 bits. 3106 static bool isZeroExtendingOp(const MachineInstr &MI) { 3107 int Opcode = MI.getOpcode(); 3108 // The 16-bit immediate is sign-extended in li/lis. 3109 // If the most significant bit is zero, all higher bits are zero. 3110 if (Opcode == PPC::LI || Opcode == PPC::LI8 || 3111 Opcode == PPC::LIS || Opcode == PPC::LIS8) { 3112 int64_t Imm = MI.getOperand(1).getImm(); 3113 if (((uint64_t)Imm & ~0x7FFFuLL) == 0) 3114 return true; 3115 } 3116 3117 // We have some variations of rotate-and-mask instructions 3118 // that clear higher 32-bits. 3119 if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo || 3120 Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo || 3121 Opcode == PPC::RLDICL_32_64) && 3122 MI.getOperand(3).getImm() >= 32) 3123 return true; 3124 3125 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) && 3126 MI.getOperand(3).getImm() >= 32 && 3127 MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm()) 3128 return true; 3129 3130 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || 3131 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo || 3132 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) && 3133 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) 3134 return true; 3135 3136 // There are other instructions that clear higher 32-bits. 3137 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo || 3138 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo || 3139 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 || 3140 Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo || 3141 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo || 3142 Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW || 3143 Opcode == PPC::SLW || Opcode == PPC::SLWo || 3144 Opcode == PPC::SRW || Opcode == PPC::SRWo || 3145 Opcode == PPC::SLW8 || Opcode == PPC::SRW8 || 3146 Opcode == PPC::SLWI || Opcode == PPC::SLWIo || 3147 Opcode == PPC::SRWI || Opcode == PPC::SRWIo || 3148 Opcode == PPC::LWZ || Opcode == PPC::LWZX || 3149 Opcode == PPC::LWZU || Opcode == PPC::LWZUX || 3150 Opcode == PPC::LWBRX || Opcode == PPC::LHBRX || 3151 Opcode == PPC::LHZ || Opcode == PPC::LHZX || 3152 Opcode == PPC::LHZU || Opcode == PPC::LHZUX || 3153 Opcode == PPC::LBZ || Opcode == PPC::LBZX || 3154 Opcode == PPC::LBZU || Opcode == PPC::LBZUX || 3155 Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 || 3156 Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 || 3157 Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 || 3158 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || 3159 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || 3160 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || 3161 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || 3162 Opcode == PPC::ANDIo || Opcode == PPC::ANDISo || 3163 Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo || 3164 Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo || 3165 Opcode == PPC::MFVSRWZ) 3166 return true; 3167 3168 return false; 3169 } 3170 3171 // This function returns true if the input MachineInstr is a TOC save 3172 // instruction. 3173 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const { 3174 if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg()) 3175 return false; 3176 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 3177 unsigned StackOffset = MI.getOperand(1).getImm(); 3178 unsigned StackReg = MI.getOperand(2).getReg(); 3179 if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset) 3180 return true; 3181 3182 return false; 3183 } 3184 3185 // We limit the max depth to track incoming values of PHIs or binary ops 3186 // (e.g. AND) to avoid exsessive cost. 3187 const unsigned MAX_DEPTH = 1; 3188 3189 bool 3190 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt, 3191 const unsigned Depth) const { 3192 const MachineFunction *MF = MI.getParent()->getParent(); 3193 const MachineRegisterInfo *MRI = &MF->getRegInfo(); 3194 3195 // If we know this instruction returns sign- or zero-extended result, 3196 // return true. 3197 if (SignExt ? isSignExtendingOp(MI): 3198 isZeroExtendingOp(MI)) 3199 return true; 3200 3201 switch (MI.getOpcode()) { 3202 case PPC::COPY: { 3203 unsigned SrcReg = MI.getOperand(1).getReg(); 3204 3205 // In both ELFv1 and v2 ABI, method parameters and the return value 3206 // are sign- or zero-extended. 3207 if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) { 3208 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); 3209 // We check the ZExt/SExt flags for a method parameter. 3210 if (MI.getParent()->getBasicBlock() == 3211 &MF->getFunction().getEntryBlock()) { 3212 unsigned VReg = MI.getOperand(0).getReg(); 3213 if (MF->getRegInfo().isLiveIn(VReg)) 3214 return SignExt ? FuncInfo->isLiveInSExt(VReg) : 3215 FuncInfo->isLiveInZExt(VReg); 3216 } 3217 3218 // For a method return value, we check the ZExt/SExt flags in attribute. 3219 // We assume the following code sequence for method call. 3220 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1 3221 // BL8_NOP @func,... 3222 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1 3223 // %5 = COPY %x3; G8RC:%5 3224 if (SrcReg == PPC::X3) { 3225 const MachineBasicBlock *MBB = MI.getParent(); 3226 MachineBasicBlock::const_instr_iterator II = 3227 MachineBasicBlock::const_instr_iterator(&MI); 3228 if (II != MBB->instr_begin() && 3229 (--II)->getOpcode() == PPC::ADJCALLSTACKUP) { 3230 const MachineInstr &CallMI = *(--II); 3231 if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) { 3232 const Function *CalleeFn = 3233 dyn_cast<Function>(CallMI.getOperand(0).getGlobal()); 3234 if (!CalleeFn) 3235 return false; 3236 const IntegerType *IntTy = 3237 dyn_cast<IntegerType>(CalleeFn->getReturnType()); 3238 const AttributeSet &Attrs = 3239 CalleeFn->getAttributes().getRetAttributes(); 3240 if (IntTy && IntTy->getBitWidth() <= 32) 3241 return Attrs.hasAttribute(SignExt ? Attribute::SExt : 3242 Attribute::ZExt); 3243 } 3244 } 3245 } 3246 } 3247 3248 // If this is a copy from another register, we recursively check source. 3249 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) 3250 return false; 3251 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 3252 if (SrcMI != NULL) 3253 return isSignOrZeroExtended(*SrcMI, SignExt, Depth); 3254 3255 return false; 3256 } 3257 3258 case PPC::ANDIo: 3259 case PPC::ANDISo: 3260 case PPC::ORI: 3261 case PPC::ORIS: 3262 case PPC::XORI: 3263 case PPC::XORIS: 3264 case PPC::ANDIo8: 3265 case PPC::ANDISo8: 3266 case PPC::ORI8: 3267 case PPC::ORIS8: 3268 case PPC::XORI8: 3269 case PPC::XORIS8: { 3270 // logical operation with 16-bit immediate does not change the upper bits. 3271 // So, we track the operand register as we do for register copy. 3272 unsigned SrcReg = MI.getOperand(1).getReg(); 3273 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) 3274 return false; 3275 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 3276 if (SrcMI != NULL) 3277 return isSignOrZeroExtended(*SrcMI, SignExt, Depth); 3278 3279 return false; 3280 } 3281 3282 // If all incoming values are sign-/zero-extended, 3283 // the output of OR, ISEL or PHI is also sign-/zero-extended. 3284 case PPC::OR: 3285 case PPC::OR8: 3286 case PPC::ISEL: 3287 case PPC::PHI: { 3288 if (Depth >= MAX_DEPTH) 3289 return false; 3290 3291 // The input registers for PHI are operand 1, 3, ... 3292 // The input registers for others are operand 1 and 2. 3293 unsigned E = 3, D = 1; 3294 if (MI.getOpcode() == PPC::PHI) { 3295 E = MI.getNumOperands(); 3296 D = 2; 3297 } 3298 3299 for (unsigned I = 1; I != E; I += D) { 3300 if (MI.getOperand(I).isReg()) { 3301 unsigned SrcReg = MI.getOperand(I).getReg(); 3302 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) 3303 return false; 3304 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 3305 if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1)) 3306 return false; 3307 } 3308 else 3309 return false; 3310 } 3311 return true; 3312 } 3313 3314 // If at least one of the incoming values of an AND is zero extended 3315 // then the output is also zero-extended. If both of the incoming values 3316 // are sign-extended then the output is also sign extended. 3317 case PPC::AND: 3318 case PPC::AND8: { 3319 if (Depth >= MAX_DEPTH) 3320 return false; 3321 3322 assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg()); 3323 3324 unsigned SrcReg1 = MI.getOperand(1).getReg(); 3325 unsigned SrcReg2 = MI.getOperand(2).getReg(); 3326 3327 if (!TargetRegisterInfo::isVirtualRegister(SrcReg1) || 3328 !TargetRegisterInfo::isVirtualRegister(SrcReg2)) 3329 return false; 3330 3331 const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1); 3332 const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2); 3333 if (!MISrc1 || !MISrc2) 3334 return false; 3335 3336 if(SignExt) 3337 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) && 3338 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); 3339 else 3340 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) || 3341 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); 3342 } 3343 3344 default: 3345 break; 3346 } 3347 return false; 3348 } 3349