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/LiveIntervalAnalysis.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 static cl:: 50 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, 51 cl::desc("Disable analysis for CTR loops")); 52 53 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", 54 cl::desc("Disable compare instruction optimization"), cl::Hidden); 55 56 static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", 57 cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), 58 cl::Hidden); 59 60 static cl::opt<bool> 61 UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden, 62 cl::desc("Use the old (incorrect) instruction latency calculation")); 63 64 // Pin the vtable to this file. 65 void PPCInstrInfo::anchor() {} 66 67 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) 68 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP), 69 Subtarget(STI), RI(STI.getTargetMachine()) {} 70 71 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for 72 /// this target when scheduling the DAG. 73 ScheduleHazardRecognizer * 74 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, 75 const ScheduleDAG *DAG) const { 76 unsigned Directive = 77 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective(); 78 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || 79 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { 80 const InstrItineraryData *II = 81 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); 82 return new ScoreboardHazardRecognizer(II, DAG); 83 } 84 85 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); 86 } 87 88 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer 89 /// to use for this target when scheduling the DAG. 90 ScheduleHazardRecognizer * 91 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 92 const ScheduleDAG *DAG) const { 93 unsigned Directive = 94 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective(); 95 96 // FIXME: Leaving this as-is until we have POWER9 scheduling info 97 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) 98 return new PPCDispatchGroupSBHazardRecognizer(II, DAG); 99 100 // Most subtargets use a PPC970 recognizer. 101 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && 102 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { 103 assert(DAG->TII && "No InstrInfo?"); 104 105 return new PPCHazardRecognizer970(*DAG); 106 } 107 108 return new ScoreboardHazardRecognizer(II, DAG); 109 } 110 111 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 112 const MachineInstr &MI, 113 unsigned *PredCost) const { 114 if (!ItinData || UseOldLatencyCalc) 115 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost); 116 117 // The default implementation of getInstrLatency calls getStageLatency, but 118 // getStageLatency does not do the right thing for us. While we have 119 // itinerary, most cores are fully pipelined, and so the itineraries only 120 // express the first part of the pipeline, not every stage. Instead, we need 121 // to use the listed output operand cycle number (using operand 0 here, which 122 // is an output). 123 124 unsigned Latency = 1; 125 unsigned DefClass = MI.getDesc().getSchedClass(); 126 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 127 const MachineOperand &MO = MI.getOperand(i); 128 if (!MO.isReg() || !MO.isDef() || MO.isImplicit()) 129 continue; 130 131 int Cycle = ItinData->getOperandCycle(DefClass, i); 132 if (Cycle < 0) 133 continue; 134 135 Latency = std::max(Latency, (unsigned) Cycle); 136 } 137 138 return Latency; 139 } 140 141 int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 142 const MachineInstr &DefMI, unsigned DefIdx, 143 const MachineInstr &UseMI, 144 unsigned UseIdx) const { 145 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, 146 UseMI, UseIdx); 147 148 if (!DefMI.getParent()) 149 return Latency; 150 151 const MachineOperand &DefMO = DefMI.getOperand(DefIdx); 152 unsigned Reg = DefMO.getReg(); 153 154 bool IsRegCR; 155 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 156 const MachineRegisterInfo *MRI = 157 &DefMI.getParent()->getParent()->getRegInfo(); 158 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || 159 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); 160 } else { 161 IsRegCR = PPC::CRRCRegClass.contains(Reg) || 162 PPC::CRBITRCRegClass.contains(Reg); 163 } 164 165 if (UseMI.isBranch() && IsRegCR) { 166 if (Latency < 0) 167 Latency = getInstrLatency(ItinData, DefMI); 168 169 // On some cores, there is an additional delay between writing to a condition 170 // register, and using it from a branch. 171 unsigned Directive = Subtarget.getDarwinDirective(); 172 switch (Directive) { 173 default: break; 174 case PPC::DIR_7400: 175 case PPC::DIR_750: 176 case PPC::DIR_970: 177 case PPC::DIR_E5500: 178 case PPC::DIR_PWR4: 179 case PPC::DIR_PWR5: 180 case PPC::DIR_PWR5X: 181 case PPC::DIR_PWR6: 182 case PPC::DIR_PWR6X: 183 case PPC::DIR_PWR7: 184 case PPC::DIR_PWR8: 185 // FIXME: Is this needed for POWER9? 186 Latency += 2; 187 break; 188 } 189 } 190 191 return Latency; 192 } 193 194 // This function does not list all associative and commutative operations, but 195 // only those worth feeding through the machine combiner in an attempt to 196 // reduce the critical path. Mostly, this means floating-point operations, 197 // because they have high latencies (compared to other operations, such and 198 // and/or, which are also associative and commutative, but have low latencies). 199 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const { 200 switch (Inst.getOpcode()) { 201 // FP Add: 202 case PPC::FADD: 203 case PPC::FADDS: 204 // FP Multiply: 205 case PPC::FMUL: 206 case PPC::FMULS: 207 // Altivec Add: 208 case PPC::VADDFP: 209 // VSX Add: 210 case PPC::XSADDDP: 211 case PPC::XVADDDP: 212 case PPC::XVADDSP: 213 case PPC::XSADDSP: 214 // VSX Multiply: 215 case PPC::XSMULDP: 216 case PPC::XVMULDP: 217 case PPC::XVMULSP: 218 case PPC::XSMULSP: 219 // QPX Add: 220 case PPC::QVFADD: 221 case PPC::QVFADDS: 222 case PPC::QVFADDSs: 223 // QPX Multiply: 224 case PPC::QVFMUL: 225 case PPC::QVFMULS: 226 case PPC::QVFMULSs: 227 return true; 228 default: 229 return false; 230 } 231 } 232 233 bool PPCInstrInfo::getMachineCombinerPatterns( 234 MachineInstr &Root, 235 SmallVectorImpl<MachineCombinerPattern> &Patterns) const { 236 // Using the machine combiner in this way is potentially expensive, so 237 // restrict to when aggressive optimizations are desired. 238 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive) 239 return false; 240 241 // FP reassociation is only legal when we don't need strict IEEE semantics. 242 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath) 243 return false; 244 245 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns); 246 } 247 248 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. 249 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, 250 unsigned &SrcReg, unsigned &DstReg, 251 unsigned &SubIdx) const { 252 switch (MI.getOpcode()) { 253 default: return false; 254 case PPC::EXTSW: 255 case PPC::EXTSW_32_64: 256 SrcReg = MI.getOperand(1).getReg(); 257 DstReg = MI.getOperand(0).getReg(); 258 SubIdx = PPC::sub_32; 259 return true; 260 } 261 } 262 263 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 264 int &FrameIndex) const { 265 // Note: This list must be kept consistent with LoadRegFromStackSlot. 266 switch (MI.getOpcode()) { 267 default: break; 268 case PPC::LD: 269 case PPC::LWZ: 270 case PPC::LFS: 271 case PPC::LFD: 272 case PPC::RESTORE_CR: 273 case PPC::RESTORE_CRBIT: 274 case PPC::LVX: 275 case PPC::LXVD2X: 276 case PPC::LXVX: 277 case PPC::QVLFDX: 278 case PPC::QVLFSXs: 279 case PPC::QVLFDXb: 280 case PPC::RESTORE_VRSAVE: 281 // Check for the operands added by addFrameReference (the immediate is the 282 // offset which defaults to 0). 283 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 284 MI.getOperand(2).isFI()) { 285 FrameIndex = MI.getOperand(2).getIndex(); 286 return MI.getOperand(0).getReg(); 287 } 288 break; 289 } 290 return 0; 291 } 292 293 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 294 int &FrameIndex) const { 295 // Note: This list must be kept consistent with StoreRegToStackSlot. 296 switch (MI.getOpcode()) { 297 default: break; 298 case PPC::STD: 299 case PPC::STW: 300 case PPC::STFS: 301 case PPC::STFD: 302 case PPC::SPILL_CR: 303 case PPC::SPILL_CRBIT: 304 case PPC::STVX: 305 case PPC::STXVD2X: 306 case PPC::STXVX: 307 case PPC::QVSTFDX: 308 case PPC::QVSTFSXs: 309 case PPC::QVSTFDXb: 310 case PPC::SPILL_VRSAVE: 311 // Check for the operands added by addFrameReference (the immediate is the 312 // offset which defaults to 0). 313 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 314 MI.getOperand(2).isFI()) { 315 FrameIndex = MI.getOperand(2).getIndex(); 316 return MI.getOperand(0).getReg(); 317 } 318 break; 319 } 320 return 0; 321 } 322 323 MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 324 unsigned OpIdx1, 325 unsigned OpIdx2) const { 326 MachineFunction &MF = *MI.getParent()->getParent(); 327 328 // Normal instructions can be commuted the obvious way. 329 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo) 330 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 331 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a 332 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because 333 // changing the relative order of the mask operands might change what happens 334 // to the high-bits of the mask (and, thus, the result). 335 336 // Cannot commute if it has a non-zero rotate count. 337 if (MI.getOperand(3).getImm() != 0) 338 return nullptr; 339 340 // If we have a zero rotate count, we have: 341 // M = mask(MB,ME) 342 // Op0 = (Op1 & ~M) | (Op2 & M) 343 // Change this to: 344 // M = mask((ME+1)&31, (MB-1)&31) 345 // Op0 = (Op2 & ~M) | (Op1 & M) 346 347 // Swap op1/op2 348 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) && 349 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo."); 350 unsigned Reg0 = MI.getOperand(0).getReg(); 351 unsigned Reg1 = MI.getOperand(1).getReg(); 352 unsigned Reg2 = MI.getOperand(2).getReg(); 353 unsigned SubReg1 = MI.getOperand(1).getSubReg(); 354 unsigned SubReg2 = MI.getOperand(2).getSubReg(); 355 bool Reg1IsKill = MI.getOperand(1).isKill(); 356 bool Reg2IsKill = MI.getOperand(2).isKill(); 357 bool ChangeReg0 = false; 358 // If machine instrs are no longer in two-address forms, update 359 // destination register as well. 360 if (Reg0 == Reg1) { 361 // Must be two address instruction! 362 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) && 363 "Expecting a two-address instruction!"); 364 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); 365 Reg2IsKill = false; 366 ChangeReg0 = true; 367 } 368 369 // Masks. 370 unsigned MB = MI.getOperand(4).getImm(); 371 unsigned ME = MI.getOperand(5).getImm(); 372 373 // We can't commute a trivial mask (there is no way to represent an all-zero 374 // mask). 375 if (MB == 0 && ME == 31) 376 return nullptr; 377 378 if (NewMI) { 379 // Create a new instruction. 380 unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg(); 381 bool Reg0IsDead = MI.getOperand(0).isDead(); 382 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc()) 383 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) 384 .addReg(Reg2, getKillRegState(Reg2IsKill)) 385 .addReg(Reg1, getKillRegState(Reg1IsKill)) 386 .addImm((ME + 1) & 31) 387 .addImm((MB - 1) & 31); 388 } 389 390 if (ChangeReg0) { 391 MI.getOperand(0).setReg(Reg2); 392 MI.getOperand(0).setSubReg(SubReg2); 393 } 394 MI.getOperand(2).setReg(Reg1); 395 MI.getOperand(1).setReg(Reg2); 396 MI.getOperand(2).setSubReg(SubReg1); 397 MI.getOperand(1).setSubReg(SubReg2); 398 MI.getOperand(2).setIsKill(Reg1IsKill); 399 MI.getOperand(1).setIsKill(Reg2IsKill); 400 401 // Swap the mask around. 402 MI.getOperand(4).setImm((ME + 1) & 31); 403 MI.getOperand(5).setImm((MB - 1) & 31); 404 return &MI; 405 } 406 407 bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1, 408 unsigned &SrcOpIdx2) const { 409 // For VSX A-Type FMA instructions, it is the first two operands that can be 410 // commuted, however, because the non-encoded tied input operand is listed 411 // first, the operands to swap are actually the second and third. 412 413 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode()); 414 if (AltOpc == -1) 415 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 416 417 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1 418 // and SrcOpIdx2. 419 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3); 420 } 421 422 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, 423 MachineBasicBlock::iterator MI) const { 424 // This function is used for scheduling, and the nop wanted here is the type 425 // that terminates dispatch groups on the POWER cores. 426 unsigned Directive = Subtarget.getDarwinDirective(); 427 unsigned Opcode; 428 switch (Directive) { 429 default: Opcode = PPC::NOP; break; 430 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; 431 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; 432 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */ 433 // FIXME: Update when POWER9 scheduling model is ready. 434 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break; 435 } 436 437 DebugLoc DL; 438 BuildMI(MBB, MI, DL, get(Opcode)); 439 } 440 441 /// getNoopForMachoTarget - Return the noop instruction to use for a noop. 442 void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const { 443 NopInst.setOpcode(PPC::NOP); 444 } 445 446 // Branch analysis. 447 // Note: If the condition register is set to CTR or CTR8 then this is a 448 // BDNZ (imm == 1) or BDZ (imm == 0) branch. 449 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 450 MachineBasicBlock *&TBB, 451 MachineBasicBlock *&FBB, 452 SmallVectorImpl<MachineOperand> &Cond, 453 bool AllowModify) const { 454 bool isPPC64 = Subtarget.isPPC64(); 455 456 // If the block has no terminators, it just falls into the block after it. 457 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 458 if (I == MBB.end()) 459 return false; 460 461 if (!isUnpredicatedTerminator(*I)) 462 return false; 463 464 // Get the last instruction in the block. 465 MachineInstr &LastInst = *I; 466 467 // If there is only one terminator instruction, process it. 468 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { 469 if (LastInst.getOpcode() == PPC::B) { 470 if (!LastInst.getOperand(0).isMBB()) 471 return true; 472 TBB = LastInst.getOperand(0).getMBB(); 473 return false; 474 } else if (LastInst.getOpcode() == PPC::BCC) { 475 if (!LastInst.getOperand(2).isMBB()) 476 return true; 477 // Block ends with fall-through condbranch. 478 TBB = LastInst.getOperand(2).getMBB(); 479 Cond.push_back(LastInst.getOperand(0)); 480 Cond.push_back(LastInst.getOperand(1)); 481 return false; 482 } else if (LastInst.getOpcode() == PPC::BC) { 483 if (!LastInst.getOperand(1).isMBB()) 484 return true; 485 // Block ends with fall-through condbranch. 486 TBB = LastInst.getOperand(1).getMBB(); 487 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 488 Cond.push_back(LastInst.getOperand(0)); 489 return false; 490 } else if (LastInst.getOpcode() == PPC::BCn) { 491 if (!LastInst.getOperand(1).isMBB()) 492 return true; 493 // Block ends with fall-through condbranch. 494 TBB = LastInst.getOperand(1).getMBB(); 495 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 496 Cond.push_back(LastInst.getOperand(0)); 497 return false; 498 } else if (LastInst.getOpcode() == PPC::BDNZ8 || 499 LastInst.getOpcode() == PPC::BDNZ) { 500 if (!LastInst.getOperand(0).isMBB()) 501 return true; 502 if (DisableCTRLoopAnal) 503 return true; 504 TBB = LastInst.getOperand(0).getMBB(); 505 Cond.push_back(MachineOperand::CreateImm(1)); 506 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 507 true)); 508 return false; 509 } else if (LastInst.getOpcode() == PPC::BDZ8 || 510 LastInst.getOpcode() == PPC::BDZ) { 511 if (!LastInst.getOperand(0).isMBB()) 512 return true; 513 if (DisableCTRLoopAnal) 514 return true; 515 TBB = LastInst.getOperand(0).getMBB(); 516 Cond.push_back(MachineOperand::CreateImm(0)); 517 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 518 true)); 519 return false; 520 } 521 522 // Otherwise, don't know what this is. 523 return true; 524 } 525 526 // Get the instruction before it if it's a terminator. 527 MachineInstr &SecondLastInst = *I; 528 529 // If there are three terminators, we don't know what sort of block this is. 530 if (I != MBB.begin() && isUnpredicatedTerminator(*--I)) 531 return true; 532 533 // If the block ends with PPC::B and PPC:BCC, handle it. 534 if (SecondLastInst.getOpcode() == PPC::BCC && 535 LastInst.getOpcode() == PPC::B) { 536 if (!SecondLastInst.getOperand(2).isMBB() || 537 !LastInst.getOperand(0).isMBB()) 538 return true; 539 TBB = SecondLastInst.getOperand(2).getMBB(); 540 Cond.push_back(SecondLastInst.getOperand(0)); 541 Cond.push_back(SecondLastInst.getOperand(1)); 542 FBB = LastInst.getOperand(0).getMBB(); 543 return false; 544 } else if (SecondLastInst.getOpcode() == PPC::BC && 545 LastInst.getOpcode() == PPC::B) { 546 if (!SecondLastInst.getOperand(1).isMBB() || 547 !LastInst.getOperand(0).isMBB()) 548 return true; 549 TBB = SecondLastInst.getOperand(1).getMBB(); 550 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 551 Cond.push_back(SecondLastInst.getOperand(0)); 552 FBB = LastInst.getOperand(0).getMBB(); 553 return false; 554 } else if (SecondLastInst.getOpcode() == PPC::BCn && 555 LastInst.getOpcode() == PPC::B) { 556 if (!SecondLastInst.getOperand(1).isMBB() || 557 !LastInst.getOperand(0).isMBB()) 558 return true; 559 TBB = SecondLastInst.getOperand(1).getMBB(); 560 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 561 Cond.push_back(SecondLastInst.getOperand(0)); 562 FBB = LastInst.getOperand(0).getMBB(); 563 return false; 564 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 || 565 SecondLastInst.getOpcode() == PPC::BDNZ) && 566 LastInst.getOpcode() == PPC::B) { 567 if (!SecondLastInst.getOperand(0).isMBB() || 568 !LastInst.getOperand(0).isMBB()) 569 return true; 570 if (DisableCTRLoopAnal) 571 return true; 572 TBB = SecondLastInst.getOperand(0).getMBB(); 573 Cond.push_back(MachineOperand::CreateImm(1)); 574 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 575 true)); 576 FBB = LastInst.getOperand(0).getMBB(); 577 return false; 578 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 || 579 SecondLastInst.getOpcode() == PPC::BDZ) && 580 LastInst.getOpcode() == PPC::B) { 581 if (!SecondLastInst.getOperand(0).isMBB() || 582 !LastInst.getOperand(0).isMBB()) 583 return true; 584 if (DisableCTRLoopAnal) 585 return true; 586 TBB = SecondLastInst.getOperand(0).getMBB(); 587 Cond.push_back(MachineOperand::CreateImm(0)); 588 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 589 true)); 590 FBB = LastInst.getOperand(0).getMBB(); 591 return false; 592 } 593 594 // If the block ends with two PPC:Bs, handle it. The second one is not 595 // executed, so remove it. 596 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) { 597 if (!SecondLastInst.getOperand(0).isMBB()) 598 return true; 599 TBB = SecondLastInst.getOperand(0).getMBB(); 600 I = LastInst; 601 if (AllowModify) 602 I->eraseFromParent(); 603 return false; 604 } 605 606 // Otherwise, can't handle this. 607 return true; 608 } 609 610 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB, 611 int *BytesRemoved) const { 612 assert(!BytesRemoved && "code size not handled"); 613 614 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 615 if (I == MBB.end()) 616 return 0; 617 618 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && 619 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 620 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 621 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 622 return 0; 623 624 // Remove the branch. 625 I->eraseFromParent(); 626 627 I = MBB.end(); 628 629 if (I == MBB.begin()) return 1; 630 --I; 631 if (I->getOpcode() != PPC::BCC && 632 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 633 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 634 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 635 return 1; 636 637 // Remove the branch. 638 I->eraseFromParent(); 639 return 2; 640 } 641 642 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB, 643 MachineBasicBlock *TBB, 644 MachineBasicBlock *FBB, 645 ArrayRef<MachineOperand> Cond, 646 const DebugLoc &DL, 647 int *BytesAdded) const { 648 // Shouldn't be a fall through. 649 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 650 assert((Cond.size() == 2 || Cond.size() == 0) && 651 "PPC branch conditions have two components!"); 652 assert(!BytesAdded && "code size not handled"); 653 654 bool isPPC64 = Subtarget.isPPC64(); 655 656 // One-way branch. 657 if (!FBB) { 658 if (Cond.empty()) // Unconditional branch 659 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); 660 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 661 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 662 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 663 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 664 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 665 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); 666 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 667 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); 668 else // Conditional branch 669 BuildMI(&MBB, DL, get(PPC::BCC)) 670 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); 671 return 1; 672 } 673 674 // Two-way Conditional Branch. 675 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 676 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 677 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 678 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 679 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 680 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); 681 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 682 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); 683 else 684 BuildMI(&MBB, DL, get(PPC::BCC)) 685 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); 686 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); 687 return 2; 688 } 689 690 // Select analysis. 691 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 692 ArrayRef<MachineOperand> Cond, 693 unsigned TrueReg, unsigned FalseReg, 694 int &CondCycles, int &TrueCycles, int &FalseCycles) const { 695 if (!Subtarget.hasISEL()) 696 return false; 697 698 if (Cond.size() != 2) 699 return false; 700 701 // If this is really a bdnz-like condition, then it cannot be turned into a 702 // select. 703 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 704 return false; 705 706 // Check register classes. 707 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 708 const TargetRegisterClass *RC = 709 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 710 if (!RC) 711 return false; 712 713 // isel is for regular integer GPRs only. 714 if (!PPC::GPRCRegClass.hasSubClassEq(RC) && 715 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && 716 !PPC::G8RCRegClass.hasSubClassEq(RC) && 717 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) 718 return false; 719 720 // FIXME: These numbers are for the A2, how well they work for other cores is 721 // an open question. On the A2, the isel instruction has a 2-cycle latency 722 // but single-cycle throughput. These numbers are used in combination with 723 // the MispredictPenalty setting from the active SchedMachineModel. 724 CondCycles = 1; 725 TrueCycles = 1; 726 FalseCycles = 1; 727 728 return true; 729 } 730 731 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, 732 MachineBasicBlock::iterator MI, 733 const DebugLoc &dl, unsigned DestReg, 734 ArrayRef<MachineOperand> Cond, unsigned TrueReg, 735 unsigned FalseReg) const { 736 assert(Cond.size() == 2 && 737 "PPC branch conditions have two components!"); 738 739 assert(Subtarget.hasISEL() && 740 "Cannot insert select on target without ISEL support"); 741 742 // Get the register classes. 743 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 744 const TargetRegisterClass *RC = 745 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 746 assert(RC && "TrueReg and FalseReg must have overlapping register classes"); 747 748 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || 749 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); 750 assert((Is64Bit || 751 PPC::GPRCRegClass.hasSubClassEq(RC) || 752 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && 753 "isel is for regular integer GPRs only"); 754 755 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; 756 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm()); 757 758 unsigned SubIdx = 0; 759 bool SwapOps = false; 760 switch (SelectPred) { 761 case PPC::PRED_EQ: 762 case PPC::PRED_EQ_MINUS: 763 case PPC::PRED_EQ_PLUS: 764 SubIdx = PPC::sub_eq; SwapOps = false; break; 765 case PPC::PRED_NE: 766 case PPC::PRED_NE_MINUS: 767 case PPC::PRED_NE_PLUS: 768 SubIdx = PPC::sub_eq; SwapOps = true; break; 769 case PPC::PRED_LT: 770 case PPC::PRED_LT_MINUS: 771 case PPC::PRED_LT_PLUS: 772 SubIdx = PPC::sub_lt; SwapOps = false; break; 773 case PPC::PRED_GE: 774 case PPC::PRED_GE_MINUS: 775 case PPC::PRED_GE_PLUS: 776 SubIdx = PPC::sub_lt; SwapOps = true; break; 777 case PPC::PRED_GT: 778 case PPC::PRED_GT_MINUS: 779 case PPC::PRED_GT_PLUS: 780 SubIdx = PPC::sub_gt; SwapOps = false; break; 781 case PPC::PRED_LE: 782 case PPC::PRED_LE_MINUS: 783 case PPC::PRED_LE_PLUS: 784 SubIdx = PPC::sub_gt; SwapOps = true; break; 785 case PPC::PRED_UN: 786 case PPC::PRED_UN_MINUS: 787 case PPC::PRED_UN_PLUS: 788 SubIdx = PPC::sub_un; SwapOps = false; break; 789 case PPC::PRED_NU: 790 case PPC::PRED_NU_MINUS: 791 case PPC::PRED_NU_PLUS: 792 SubIdx = PPC::sub_un; SwapOps = true; break; 793 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; 794 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; 795 } 796 797 unsigned FirstReg = SwapOps ? FalseReg : TrueReg, 798 SecondReg = SwapOps ? TrueReg : FalseReg; 799 800 // The first input register of isel cannot be r0. If it is a member 801 // of a register class that can be r0, then copy it first (the 802 // register allocator should eliminate the copy). 803 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || 804 MRI.getRegClass(FirstReg)->contains(PPC::X0)) { 805 const TargetRegisterClass *FirstRC = 806 MRI.getRegClass(FirstReg)->contains(PPC::X0) ? 807 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; 808 unsigned OldFirstReg = FirstReg; 809 FirstReg = MRI.createVirtualRegister(FirstRC); 810 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) 811 .addReg(OldFirstReg); 812 } 813 814 BuildMI(MBB, MI, dl, get(OpCode), DestReg) 815 .addReg(FirstReg).addReg(SecondReg) 816 .addReg(Cond[1].getReg(), 0, SubIdx); 817 } 818 819 static unsigned getCRBitValue(unsigned CRBit) { 820 unsigned Ret = 4; 821 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || 822 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || 823 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || 824 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) 825 Ret = 3; 826 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || 827 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || 828 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || 829 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) 830 Ret = 2; 831 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || 832 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || 833 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || 834 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) 835 Ret = 1; 836 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || 837 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || 838 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || 839 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) 840 Ret = 0; 841 842 assert(Ret != 4 && "Invalid CR bit register"); 843 return Ret; 844 } 845 846 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 847 MachineBasicBlock::iterator I, 848 const DebugLoc &DL, unsigned DestReg, 849 unsigned SrcReg, bool KillSrc) const { 850 // We can end up with self copies and similar things as a result of VSX copy 851 // legalization. Promote them here. 852 const TargetRegisterInfo *TRI = &getRegisterInfo(); 853 if (PPC::F8RCRegClass.contains(DestReg) && 854 PPC::VSRCRegClass.contains(SrcReg)) { 855 unsigned SuperReg = 856 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); 857 858 if (VSXSelfCopyCrash && SrcReg == SuperReg) 859 llvm_unreachable("nop VSX copy"); 860 861 DestReg = SuperReg; 862 } else if (PPC::VRRCRegClass.contains(DestReg) && 863 PPC::VSRCRegClass.contains(SrcReg)) { 864 unsigned SuperReg = 865 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass); 866 867 if (VSXSelfCopyCrash && SrcReg == SuperReg) 868 llvm_unreachable("nop VSX copy"); 869 870 DestReg = SuperReg; 871 } else if (PPC::F8RCRegClass.contains(SrcReg) && 872 PPC::VSRCRegClass.contains(DestReg)) { 873 unsigned SuperReg = 874 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); 875 876 if (VSXSelfCopyCrash && DestReg == SuperReg) 877 llvm_unreachable("nop VSX copy"); 878 879 SrcReg = SuperReg; 880 } else if (PPC::VRRCRegClass.contains(SrcReg) && 881 PPC::VSRCRegClass.contains(DestReg)) { 882 unsigned SuperReg = 883 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass); 884 885 if (VSXSelfCopyCrash && DestReg == SuperReg) 886 llvm_unreachable("nop VSX copy"); 887 888 SrcReg = SuperReg; 889 } 890 891 // Different class register copy 892 if (PPC::CRBITRCRegClass.contains(SrcReg) && 893 PPC::GPRCRegClass.contains(DestReg)) { 894 unsigned CRReg = getCRFromCRBit(SrcReg); 895 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg); 896 getKillRegState(KillSrc); 897 // Rotate the CR bit in the CR fields to be the least significant bit and 898 // then mask with 0x1 (MB = ME = 31). 899 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) 900 .addReg(DestReg, RegState::Kill) 901 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) 902 .addImm(31) 903 .addImm(31); 904 return; 905 } else if (PPC::CRRCRegClass.contains(SrcReg) && 906 PPC::G8RCRegClass.contains(DestReg)) { 907 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg); 908 getKillRegState(KillSrc); 909 return; 910 } else if (PPC::CRRCRegClass.contains(SrcReg) && 911 PPC::GPRCRegClass.contains(DestReg)) { 912 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); 913 getKillRegState(KillSrc); 914 return; 915 } 916 917 unsigned Opc; 918 if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) 919 Opc = PPC::OR; 920 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) 921 Opc = PPC::OR8; 922 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) 923 Opc = PPC::FMR; 924 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) 925 Opc = PPC::MCRF; 926 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) 927 Opc = PPC::VOR; 928 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) 929 // There are two different ways this can be done: 930 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only 931 // issue in VSU pipeline 0. 932 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but 933 // can go to either pipeline. 934 // We'll always use xxlor here, because in practically all cases where 935 // copies are generated, they are close enough to some use that the 936 // lower-latency form is preferable. 937 Opc = PPC::XXLOR; 938 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || 939 PPC::VSSRCRegClass.contains(DestReg, SrcReg)) 940 Opc = PPC::XXLORf; 941 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) 942 Opc = PPC::QVFMR; 943 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) 944 Opc = PPC::QVFMRs; 945 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) 946 Opc = PPC::QVFMRb; 947 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) 948 Opc = PPC::CROR; 949 else 950 llvm_unreachable("Impossible reg-to-reg copy"); 951 952 const MCInstrDesc &MCID = get(Opc); 953 if (MCID.getNumOperands() == 3) 954 BuildMI(MBB, I, DL, MCID, DestReg) 955 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); 956 else 957 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); 958 } 959 960 // This function returns true if a CR spill is necessary and false otherwise. 961 bool 962 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, 963 unsigned SrcReg, bool isKill, 964 int FrameIdx, 965 const TargetRegisterClass *RC, 966 SmallVectorImpl<MachineInstr*> &NewMIs, 967 bool &NonRI, bool &SpillsVRS) const{ 968 // Note: If additional store instructions are added here, 969 // update isStoreToStackSlot. 970 971 DebugLoc DL; 972 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 973 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 974 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) 975 .addReg(SrcReg, 976 getKillRegState(isKill)), 977 FrameIdx)); 978 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 979 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 980 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) 981 .addReg(SrcReg, 982 getKillRegState(isKill)), 983 FrameIdx)); 984 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 985 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) 986 .addReg(SrcReg, 987 getKillRegState(isKill)), 988 FrameIdx)); 989 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 990 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) 991 .addReg(SrcReg, 992 getKillRegState(isKill)), 993 FrameIdx)); 994 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 995 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) 996 .addReg(SrcReg, 997 getKillRegState(isKill)), 998 FrameIdx)); 999 return true; 1000 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 1001 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) 1002 .addReg(SrcReg, 1003 getKillRegState(isKill)), 1004 FrameIdx)); 1005 return true; 1006 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1007 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) 1008 .addReg(SrcReg, 1009 getKillRegState(isKill)), 1010 FrameIdx)); 1011 NonRI = true; 1012 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1013 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXVX : PPC::STXVD2X; 1014 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op)) 1015 .addReg(SrcReg, 1016 getKillRegState(isKill)), 1017 FrameIdx)); 1018 NonRI = true; 1019 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1020 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX)) 1021 .addReg(SrcReg, 1022 getKillRegState(isKill)), 1023 FrameIdx)); 1024 NonRI = true; 1025 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1026 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX)) 1027 .addReg(SrcReg, 1028 getKillRegState(isKill)), 1029 FrameIdx)); 1030 NonRI = true; 1031 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1032 assert(Subtarget.isDarwin() && 1033 "VRSAVE only needs spill/restore on Darwin"); 1034 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) 1035 .addReg(SrcReg, 1036 getKillRegState(isKill)), 1037 FrameIdx)); 1038 SpillsVRS = true; 1039 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1040 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX)) 1041 .addReg(SrcReg, 1042 getKillRegState(isKill)), 1043 FrameIdx)); 1044 NonRI = true; 1045 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1046 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs)) 1047 .addReg(SrcReg, 1048 getKillRegState(isKill)), 1049 FrameIdx)); 1050 NonRI = true; 1051 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1052 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) 1053 .addReg(SrcReg, 1054 getKillRegState(isKill)), 1055 FrameIdx)); 1056 NonRI = true; 1057 } else { 1058 llvm_unreachable("Unknown regclass!"); 1059 } 1060 1061 return false; 1062 } 1063 1064 void 1065 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 1066 MachineBasicBlock::iterator MI, 1067 unsigned SrcReg, bool isKill, int FrameIdx, 1068 const TargetRegisterClass *RC, 1069 const TargetRegisterInfo *TRI) const { 1070 MachineFunction &MF = *MBB.getParent(); 1071 SmallVector<MachineInstr*, 4> NewMIs; 1072 1073 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1074 FuncInfo->setHasSpills(); 1075 1076 bool NonRI = false, SpillsVRS = false; 1077 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, 1078 NonRI, SpillsVRS)) 1079 FuncInfo->setSpillsCR(); 1080 1081 if (SpillsVRS) 1082 FuncInfo->setSpillsVRSAVE(); 1083 1084 if (NonRI) 1085 FuncInfo->setHasNonRISpills(); 1086 1087 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1088 MBB.insert(MI, NewMIs[i]); 1089 1090 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1091 MachineMemOperand *MMO = MF.getMachineMemOperand( 1092 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1093 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx), 1094 MFI.getObjectAlignment(FrameIdx)); 1095 NewMIs.back()->addMemOperand(MF, MMO); 1096 } 1097 1098 bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL, 1099 unsigned DestReg, int FrameIdx, 1100 const TargetRegisterClass *RC, 1101 SmallVectorImpl<MachineInstr *> &NewMIs, 1102 bool &NonRI, bool &SpillsVRS) const { 1103 // Note: If additional load instructions are added here, 1104 // update isLoadFromStackSlot. 1105 1106 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 1107 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 1108 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), 1109 DestReg), FrameIdx)); 1110 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 1111 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 1112 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), 1113 FrameIdx)); 1114 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 1115 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), 1116 FrameIdx)); 1117 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 1118 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), 1119 FrameIdx)); 1120 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 1121 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1122 get(PPC::RESTORE_CR), DestReg), 1123 FrameIdx)); 1124 return true; 1125 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 1126 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1127 get(PPC::RESTORE_CRBIT), DestReg), 1128 FrameIdx)); 1129 return true; 1130 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1131 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), 1132 FrameIdx)); 1133 NonRI = true; 1134 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1135 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXVX : PPC::LXVD2X; 1136 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg), 1137 FrameIdx)); 1138 NonRI = true; 1139 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1140 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg), 1141 FrameIdx)); 1142 NonRI = true; 1143 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1144 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg), 1145 FrameIdx)); 1146 NonRI = true; 1147 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1148 assert(Subtarget.isDarwin() && 1149 "VRSAVE only needs spill/restore on Darwin"); 1150 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1151 get(PPC::RESTORE_VRSAVE), 1152 DestReg), 1153 FrameIdx)); 1154 SpillsVRS = true; 1155 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1156 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg), 1157 FrameIdx)); 1158 NonRI = true; 1159 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1160 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg), 1161 FrameIdx)); 1162 NonRI = true; 1163 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1164 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), 1165 FrameIdx)); 1166 NonRI = true; 1167 } else { 1168 llvm_unreachable("Unknown regclass!"); 1169 } 1170 1171 return false; 1172 } 1173 1174 void 1175 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 1176 MachineBasicBlock::iterator MI, 1177 unsigned DestReg, int FrameIdx, 1178 const TargetRegisterClass *RC, 1179 const TargetRegisterInfo *TRI) const { 1180 MachineFunction &MF = *MBB.getParent(); 1181 SmallVector<MachineInstr*, 4> NewMIs; 1182 DebugLoc DL; 1183 if (MI != MBB.end()) DL = MI->getDebugLoc(); 1184 1185 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1186 FuncInfo->setHasSpills(); 1187 1188 bool NonRI = false, SpillsVRS = false; 1189 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, 1190 NonRI, SpillsVRS)) 1191 FuncInfo->setSpillsCR(); 1192 1193 if (SpillsVRS) 1194 FuncInfo->setSpillsVRSAVE(); 1195 1196 if (NonRI) 1197 FuncInfo->setHasNonRISpills(); 1198 1199 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1200 MBB.insert(MI, NewMIs[i]); 1201 1202 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1203 MachineMemOperand *MMO = MF.getMachineMemOperand( 1204 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1205 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx), 1206 MFI.getObjectAlignment(FrameIdx)); 1207 NewMIs.back()->addMemOperand(MF, MMO); 1208 } 1209 1210 bool PPCInstrInfo:: 1211 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 1212 assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); 1213 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) 1214 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); 1215 else 1216 // Leave the CR# the same, but invert the condition. 1217 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); 1218 return false; 1219 } 1220 1221 bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 1222 unsigned Reg, MachineRegisterInfo *MRI) const { 1223 // For some instructions, it is legal to fold ZERO into the RA register field. 1224 // A zero immediate should always be loaded with a single li. 1225 unsigned DefOpc = DefMI.getOpcode(); 1226 if (DefOpc != PPC::LI && DefOpc != PPC::LI8) 1227 return false; 1228 if (!DefMI.getOperand(1).isImm()) 1229 return false; 1230 if (DefMI.getOperand(1).getImm() != 0) 1231 return false; 1232 1233 // Note that we cannot here invert the arguments of an isel in order to fold 1234 // a ZERO into what is presented as the second argument. All we have here 1235 // is the condition bit, and that might come from a CR-logical bit operation. 1236 1237 const MCInstrDesc &UseMCID = UseMI.getDesc(); 1238 1239 // Only fold into real machine instructions. 1240 if (UseMCID.isPseudo()) 1241 return false; 1242 1243 unsigned UseIdx; 1244 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx) 1245 if (UseMI.getOperand(UseIdx).isReg() && 1246 UseMI.getOperand(UseIdx).getReg() == Reg) 1247 break; 1248 1249 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI"); 1250 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); 1251 1252 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; 1253 1254 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 1255 // register (which might also be specified as a pointer class kind). 1256 if (UseInfo->isLookupPtrRegClass()) { 1257 if (UseInfo->RegClass /* Kind */ != 1) 1258 return false; 1259 } else { 1260 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && 1261 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) 1262 return false; 1263 } 1264 1265 // Make sure this is not tied to an output register (or otherwise 1266 // constrained). This is true for ST?UX registers, for example, which 1267 // are tied to their output registers. 1268 if (UseInfo->Constraints != 0) 1269 return false; 1270 1271 unsigned ZeroReg; 1272 if (UseInfo->isLookupPtrRegClass()) { 1273 bool isPPC64 = Subtarget.isPPC64(); 1274 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; 1275 } else { 1276 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? 1277 PPC::ZERO8 : PPC::ZERO; 1278 } 1279 1280 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 1281 UseMI.getOperand(UseIdx).setReg(ZeroReg); 1282 1283 if (DeleteDef) 1284 DefMI.eraseFromParent(); 1285 1286 return true; 1287 } 1288 1289 static bool MBBDefinesCTR(MachineBasicBlock &MBB) { 1290 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 1291 I != IE; ++I) 1292 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) 1293 return true; 1294 return false; 1295 } 1296 1297 // We should make sure that, if we're going to predicate both sides of a 1298 // condition (a diamond), that both sides don't define the counter register. We 1299 // can predicate counter-decrement-based branches, but while that predicates 1300 // the branching, it does not predicate the counter decrement. If we tried to 1301 // merge the triangle into one predicated block, we'd decrement the counter 1302 // twice. 1303 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, 1304 unsigned NumT, unsigned ExtraT, 1305 MachineBasicBlock &FMBB, 1306 unsigned NumF, unsigned ExtraF, 1307 BranchProbability Probability) const { 1308 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); 1309 } 1310 1311 1312 bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const { 1313 // The predicated branches are identified by their type, not really by the 1314 // explicit presence of a predicate. Furthermore, some of them can be 1315 // predicated more than once. Because if conversion won't try to predicate 1316 // any instruction which already claims to be predicated (by returning true 1317 // here), always return false. In doing so, we let isPredicable() be the 1318 // final word on whether not the instruction can be (further) predicated. 1319 1320 return false; 1321 } 1322 1323 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const { 1324 if (!MI.isTerminator()) 1325 return false; 1326 1327 // Conditional branch is a special case. 1328 if (MI.isBranch() && !MI.isBarrier()) 1329 return true; 1330 1331 return !isPredicated(MI); 1332 } 1333 1334 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI, 1335 ArrayRef<MachineOperand> Pred) const { 1336 unsigned OpC = MI.getOpcode(); 1337 if (OpC == PPC::BLR || OpC == PPC::BLR8) { 1338 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1339 bool isPPC64 = Subtarget.isPPC64(); 1340 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) 1341 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); 1342 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1343 MI.setDesc(get(PPC::BCLR)); 1344 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1345 .addReg(Pred[1].getReg()); 1346 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1347 MI.setDesc(get(PPC::BCLRn)); 1348 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1349 .addReg(Pred[1].getReg()); 1350 } else { 1351 MI.setDesc(get(PPC::BCCLR)); 1352 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1353 .addImm(Pred[0].getImm()) 1354 .addReg(Pred[1].getReg()); 1355 } 1356 1357 return true; 1358 } else if (OpC == PPC::B) { 1359 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1360 bool isPPC64 = Subtarget.isPPC64(); 1361 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) 1362 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); 1363 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1364 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1365 MI.RemoveOperand(0); 1366 1367 MI.setDesc(get(PPC::BC)); 1368 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1369 .addReg(Pred[1].getReg()) 1370 .addMBB(MBB); 1371 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1372 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1373 MI.RemoveOperand(0); 1374 1375 MI.setDesc(get(PPC::BCn)); 1376 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1377 .addReg(Pred[1].getReg()) 1378 .addMBB(MBB); 1379 } else { 1380 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1381 MI.RemoveOperand(0); 1382 1383 MI.setDesc(get(PPC::BCC)); 1384 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1385 .addImm(Pred[0].getImm()) 1386 .addReg(Pred[1].getReg()) 1387 .addMBB(MBB); 1388 } 1389 1390 return true; 1391 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || 1392 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { 1393 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) 1394 llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); 1395 1396 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; 1397 bool isPPC64 = Subtarget.isPPC64(); 1398 1399 if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1400 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) 1401 : (setLR ? PPC::BCCTRL : PPC::BCCTR))); 1402 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1403 .addReg(Pred[1].getReg()); 1404 return true; 1405 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1406 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) 1407 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); 1408 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1409 .addReg(Pred[1].getReg()); 1410 return true; 1411 } 1412 1413 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) 1414 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); 1415 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1416 .addImm(Pred[0].getImm()) 1417 .addReg(Pred[1].getReg()); 1418 return true; 1419 } 1420 1421 return false; 1422 } 1423 1424 bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, 1425 ArrayRef<MachineOperand> Pred2) const { 1426 assert(Pred1.size() == 2 && "Invalid PPC first predicate"); 1427 assert(Pred2.size() == 2 && "Invalid PPC second predicate"); 1428 1429 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) 1430 return false; 1431 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) 1432 return false; 1433 1434 // P1 can only subsume P2 if they test the same condition register. 1435 if (Pred1[1].getReg() != Pred2[1].getReg()) 1436 return false; 1437 1438 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); 1439 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); 1440 1441 if (P1 == P2) 1442 return true; 1443 1444 // Does P1 subsume P2, e.g. GE subsumes GT. 1445 if (P1 == PPC::PRED_LE && 1446 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) 1447 return true; 1448 if (P1 == PPC::PRED_GE && 1449 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) 1450 return true; 1451 1452 return false; 1453 } 1454 1455 bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI, 1456 std::vector<MachineOperand> &Pred) const { 1457 // Note: At the present time, the contents of Pred from this function is 1458 // unused by IfConversion. This implementation follows ARM by pushing the 1459 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of 1460 // predicate, instructions defining CTR or CTR8 are also included as 1461 // predicate-defining instructions. 1462 1463 const TargetRegisterClass *RCs[] = 1464 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, 1465 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; 1466 1467 bool Found = false; 1468 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1469 const MachineOperand &MO = MI.getOperand(i); 1470 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { 1471 const TargetRegisterClass *RC = RCs[c]; 1472 if (MO.isReg()) { 1473 if (MO.isDef() && RC->contains(MO.getReg())) { 1474 Pred.push_back(MO); 1475 Found = true; 1476 } 1477 } else if (MO.isRegMask()) { 1478 for (TargetRegisterClass::iterator I = RC->begin(), 1479 IE = RC->end(); I != IE; ++I) 1480 if (MO.clobbersPhysReg(*I)) { 1481 Pred.push_back(MO); 1482 Found = true; 1483 } 1484 } 1485 } 1486 } 1487 1488 return Found; 1489 } 1490 1491 bool PPCInstrInfo::isPredicable(MachineInstr &MI) const { 1492 unsigned OpC = MI.getOpcode(); 1493 switch (OpC) { 1494 default: 1495 return false; 1496 case PPC::B: 1497 case PPC::BLR: 1498 case PPC::BLR8: 1499 case PPC::BCTR: 1500 case PPC::BCTR8: 1501 case PPC::BCTRL: 1502 case PPC::BCTRL8: 1503 return true; 1504 } 1505 } 1506 1507 bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, 1508 unsigned &SrcReg2, int &Mask, 1509 int &Value) const { 1510 unsigned Opc = MI.getOpcode(); 1511 1512 switch (Opc) { 1513 default: return false; 1514 case PPC::CMPWI: 1515 case PPC::CMPLWI: 1516 case PPC::CMPDI: 1517 case PPC::CMPLDI: 1518 SrcReg = MI.getOperand(1).getReg(); 1519 SrcReg2 = 0; 1520 Value = MI.getOperand(2).getImm(); 1521 Mask = 0xFFFF; 1522 return true; 1523 case PPC::CMPW: 1524 case PPC::CMPLW: 1525 case PPC::CMPD: 1526 case PPC::CMPLD: 1527 case PPC::FCMPUS: 1528 case PPC::FCMPUD: 1529 SrcReg = MI.getOperand(1).getReg(); 1530 SrcReg2 = MI.getOperand(2).getReg(); 1531 return true; 1532 } 1533 } 1534 1535 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg, 1536 unsigned SrcReg2, int Mask, int Value, 1537 const MachineRegisterInfo *MRI) const { 1538 if (DisableCmpOpt) 1539 return false; 1540 1541 int OpC = CmpInstr.getOpcode(); 1542 unsigned CRReg = CmpInstr.getOperand(0).getReg(); 1543 1544 // FP record forms set CR1 based on the execption status bits, not a 1545 // comparison with zero. 1546 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) 1547 return false; 1548 1549 // The record forms set the condition register based on a signed comparison 1550 // with zero (so says the ISA manual). This is not as straightforward as it 1551 // seems, however, because this is always a 64-bit comparison on PPC64, even 1552 // for instructions that are 32-bit in nature (like slw for example). 1553 // So, on PPC32, for unsigned comparisons, we can use the record forms only 1554 // for equality checks (as those don't depend on the sign). On PPC64, 1555 // we are restricted to equality for unsigned 64-bit comparisons and for 1556 // signed 32-bit comparisons the applicability is more restricted. 1557 bool isPPC64 = Subtarget.isPPC64(); 1558 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; 1559 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; 1560 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; 1561 1562 // Get the unique definition of SrcReg. 1563 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 1564 if (!MI) return false; 1565 int MIOpC = MI->getOpcode(); 1566 1567 bool equalityOnly = false; 1568 bool noSub = false; 1569 if (isPPC64) { 1570 if (is32BitSignedCompare) { 1571 // We can perform this optimization only if MI is sign-extending. 1572 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || 1573 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || 1574 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || 1575 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || 1576 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { 1577 noSub = true; 1578 } else 1579 return false; 1580 } else if (is32BitUnsignedCompare) { 1581 // 32-bit rotate and mask instructions are zero extending only if MB <= ME 1582 bool isZeroExtendingRotate = 1583 (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo || 1584 MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo) 1585 && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm(); 1586 1587 // We can perform this optimization, equality only, if MI is 1588 // zero-extending. 1589 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || 1590 MIOpC == PPC::SLW || MIOpC == PPC::SLWo || 1591 MIOpC == PPC::SRW || MIOpC == PPC::SRWo || 1592 isZeroExtendingRotate) { 1593 noSub = true; 1594 equalityOnly = true; 1595 } else 1596 return false; 1597 } else 1598 equalityOnly = is64BitUnsignedCompare; 1599 } else 1600 equalityOnly = is32BitUnsignedCompare; 1601 1602 if (equalityOnly) { 1603 // We need to check the uses of the condition register in order to reject 1604 // non-equality comparisons. 1605 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg), 1606 IE = MRI->use_instr_end(); I != IE; ++I) { 1607 MachineInstr *UseMI = &*I; 1608 if (UseMI->getOpcode() == PPC::BCC) { 1609 unsigned Pred = UseMI->getOperand(0).getImm(); 1610 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE) 1611 return false; 1612 } else if (UseMI->getOpcode() == PPC::ISEL || 1613 UseMI->getOpcode() == PPC::ISEL8) { 1614 unsigned SubIdx = UseMI->getOperand(3).getSubReg(); 1615 if (SubIdx != PPC::sub_eq) 1616 return false; 1617 } else 1618 return false; 1619 } 1620 } 1621 1622 MachineBasicBlock::iterator I = CmpInstr; 1623 1624 // Scan forward to find the first use of the compare. 1625 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL; 1626 ++I) { 1627 bool FoundUse = false; 1628 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg), 1629 JE = MRI->use_instr_end(); J != JE; ++J) 1630 if (&*J == &*I) { 1631 FoundUse = true; 1632 break; 1633 } 1634 1635 if (FoundUse) 1636 break; 1637 } 1638 1639 // There are two possible candidates which can be changed to set CR[01]. 1640 // One is MI, the other is a SUB instruction. 1641 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 1642 MachineInstr *Sub = nullptr; 1643 if (SrcReg2 != 0) 1644 // MI is not a candidate for CMPrr. 1645 MI = nullptr; 1646 // FIXME: Conservatively refuse to convert an instruction which isn't in the 1647 // same BB as the comparison. This is to allow the check below to avoid calls 1648 // (and other explicit clobbers); instead we should really check for these 1649 // more explicitly (in at least a few predecessors). 1650 else if (MI->getParent() != CmpInstr.getParent() || Value != 0) { 1651 // PPC does not have a record-form SUBri. 1652 return false; 1653 } 1654 1655 // Search for Sub. 1656 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1657 --I; 1658 1659 // Get ready to iterate backward from CmpInstr. 1660 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin(); 1661 1662 for (; I != E && !noSub; --I) { 1663 const MachineInstr &Instr = *I; 1664 unsigned IOpC = Instr.getOpcode(); 1665 1666 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) || 1667 Instr.readsRegister(PPC::CR0, TRI))) 1668 // This instruction modifies or uses the record condition register after 1669 // the one we want to change. While we could do this transformation, it 1670 // would likely not be profitable. This transformation removes one 1671 // instruction, and so even forcing RA to generate one move probably 1672 // makes it unprofitable. 1673 return false; 1674 1675 // Check whether CmpInstr can be made redundant by the current instruction. 1676 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || 1677 OpC == PPC::CMPD || OpC == PPC::CMPLD) && 1678 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && 1679 ((Instr.getOperand(1).getReg() == SrcReg && 1680 Instr.getOperand(2).getReg() == SrcReg2) || 1681 (Instr.getOperand(1).getReg() == SrcReg2 && 1682 Instr.getOperand(2).getReg() == SrcReg))) { 1683 Sub = &*I; 1684 break; 1685 } 1686 1687 if (I == B) 1688 // The 'and' is below the comparison instruction. 1689 return false; 1690 } 1691 1692 // Return false if no candidates exist. 1693 if (!MI && !Sub) 1694 return false; 1695 1696 // The single candidate is called MI. 1697 if (!MI) MI = Sub; 1698 1699 int NewOpC = -1; 1700 MIOpC = MI->getOpcode(); 1701 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) 1702 NewOpC = MIOpC; 1703 else { 1704 NewOpC = PPC::getRecordFormOpcode(MIOpC); 1705 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) 1706 NewOpC = MIOpC; 1707 } 1708 1709 // FIXME: On the non-embedded POWER architectures, only some of the record 1710 // forms are fast, and we should use only the fast ones. 1711 1712 // The defining instruction has a record form (or is already a record 1713 // form). It is possible, however, that we'll need to reverse the condition 1714 // code of the users. 1715 if (NewOpC == -1) 1716 return false; 1717 1718 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; 1719 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; 1720 1721 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP 1722 // needs to be updated to be based on SUB. Push the condition code 1723 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the 1724 // condition code of these operands will be modified. 1725 bool ShouldSwap = false; 1726 if (Sub) { 1727 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 1728 Sub->getOperand(2).getReg() == SrcReg; 1729 1730 // The operands to subf are the opposite of sub, so only in the fixed-point 1731 // case, invert the order. 1732 ShouldSwap = !ShouldSwap; 1733 } 1734 1735 if (ShouldSwap) 1736 for (MachineRegisterInfo::use_instr_iterator 1737 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 1738 I != IE; ++I) { 1739 MachineInstr *UseMI = &*I; 1740 if (UseMI->getOpcode() == PPC::BCC) { 1741 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); 1742 assert((!equalityOnly || 1743 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) && 1744 "Invalid predicate for equality-only optimization"); 1745 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), 1746 PPC::getSwappedPredicate(Pred))); 1747 } else if (UseMI->getOpcode() == PPC::ISEL || 1748 UseMI->getOpcode() == PPC::ISEL8) { 1749 unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); 1750 assert((!equalityOnly || NewSubReg == PPC::sub_eq) && 1751 "Invalid CR bit for equality-only optimization"); 1752 1753 if (NewSubReg == PPC::sub_lt) 1754 NewSubReg = PPC::sub_gt; 1755 else if (NewSubReg == PPC::sub_gt) 1756 NewSubReg = PPC::sub_lt; 1757 1758 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), 1759 NewSubReg)); 1760 } else // We need to abort on a user we don't understand. 1761 return false; 1762 } 1763 1764 // Create a new virtual register to hold the value of the CR set by the 1765 // record-form instruction. If the instruction was not previously in 1766 // record form, then set the kill flag on the CR. 1767 CmpInstr.eraseFromParent(); 1768 1769 MachineBasicBlock::iterator MII = MI; 1770 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), 1771 get(TargetOpcode::COPY), CRReg) 1772 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); 1773 1774 // Even if CR0 register were dead before, it is alive now since the 1775 // instruction we just built uses it. 1776 MI->clearRegisterDeads(PPC::CR0); 1777 1778 if (MIOpC != NewOpC) { 1779 // We need to be careful here: we're replacing one instruction with 1780 // another, and we need to make sure that we get all of the right 1781 // implicit uses and defs. On the other hand, the caller may be holding 1782 // an iterator to this instruction, and so we can't delete it (this is 1783 // specifically the case if this is the instruction directly after the 1784 // compare). 1785 1786 const MCInstrDesc &NewDesc = get(NewOpC); 1787 MI->setDesc(NewDesc); 1788 1789 if (NewDesc.ImplicitDefs) 1790 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs(); 1791 *ImpDefs; ++ImpDefs) 1792 if (!MI->definesRegister(*ImpDefs)) 1793 MI->addOperand(*MI->getParent()->getParent(), 1794 MachineOperand::CreateReg(*ImpDefs, true, true)); 1795 if (NewDesc.ImplicitUses) 1796 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses(); 1797 *ImpUses; ++ImpUses) 1798 if (!MI->readsRegister(*ImpUses)) 1799 MI->addOperand(*MI->getParent()->getParent(), 1800 MachineOperand::CreateReg(*ImpUses, false, true)); 1801 } 1802 assert(MI->definesRegister(PPC::CR0) && 1803 "Record-form instruction does not define cr0?"); 1804 1805 // Modify the condition code of operands in OperandsToUpdate. 1806 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 1807 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 1808 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) 1809 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); 1810 1811 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) 1812 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); 1813 1814 return true; 1815 } 1816 1817 /// GetInstSize - Return the number of bytes of code the specified 1818 /// instruction may be. This returns the maximum number of bytes. 1819 /// 1820 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 1821 unsigned Opcode = MI.getOpcode(); 1822 1823 if (Opcode == PPC::INLINEASM) { 1824 const MachineFunction *MF = MI.getParent()->getParent(); 1825 const char *AsmStr = MI.getOperand(0).getSymbolName(); 1826 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 1827 } else if (Opcode == TargetOpcode::STACKMAP) { 1828 StackMapOpers Opers(&MI); 1829 return Opers.getNumPatchBytes(); 1830 } else if (Opcode == TargetOpcode::PATCHPOINT) { 1831 PatchPointOpers Opers(&MI); 1832 return Opers.getNumPatchBytes(); 1833 } else { 1834 const MCInstrDesc &Desc = get(Opcode); 1835 return Desc.getSize(); 1836 } 1837 } 1838 1839 std::pair<unsigned, unsigned> 1840 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 1841 const unsigned Mask = PPCII::MO_ACCESS_MASK; 1842 return std::make_pair(TF & Mask, TF & ~Mask); 1843 } 1844 1845 ArrayRef<std::pair<unsigned, const char *>> 1846 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 1847 using namespace PPCII; 1848 static const std::pair<unsigned, const char *> TargetFlags[] = { 1849 {MO_LO, "ppc-lo"}, 1850 {MO_HA, "ppc-ha"}, 1851 {MO_TPREL_LO, "ppc-tprel-lo"}, 1852 {MO_TPREL_HA, "ppc-tprel-ha"}, 1853 {MO_DTPREL_LO, "ppc-dtprel-lo"}, 1854 {MO_TLSLD_LO, "ppc-tlsld-lo"}, 1855 {MO_TOC_LO, "ppc-toc-lo"}, 1856 {MO_TLS, "ppc-tls"}}; 1857 return makeArrayRef(TargetFlags); 1858 } 1859 1860 ArrayRef<std::pair<unsigned, const char *>> 1861 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { 1862 using namespace PPCII; 1863 static const std::pair<unsigned, const char *> TargetFlags[] = { 1864 {MO_PLT, "ppc-plt"}, 1865 {MO_PIC_FLAG, "ppc-pic"}, 1866 {MO_NLP_FLAG, "ppc-nlp"}, 1867 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}}; 1868 return makeArrayRef(TargetFlags); 1869 } 1870 1871 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1872 switch (MI.getOpcode()) { 1873 case TargetOpcode::LOAD_STACK_GUARD: { 1874 assert(Subtarget.isTargetLinux() && 1875 "Only Linux target is expected to contain LOAD_STACK_GUARD"); 1876 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; 1877 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2; 1878 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ)); 1879 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1880 .addImm(Offset) 1881 .addReg(Reg); 1882 return true; 1883 } 1884 } 1885 return false; 1886 } 1887