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::QVLFDX: 277 case PPC::QVLFSXs: 278 case PPC::QVLFDXb: 279 case PPC::RESTORE_VRSAVE: 280 // Check for the operands added by addFrameReference (the immediate is the 281 // offset which defaults to 0). 282 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && 283 MI->getOperand(2).isFI()) { 284 FrameIndex = MI->getOperand(2).getIndex(); 285 return MI->getOperand(0).getReg(); 286 } 287 break; 288 } 289 return 0; 290 } 291 292 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 293 int &FrameIndex) const { 294 // Note: This list must be kept consistent with StoreRegToStackSlot. 295 switch (MI->getOpcode()) { 296 default: break; 297 case PPC::STD: 298 case PPC::STW: 299 case PPC::STFS: 300 case PPC::STFD: 301 case PPC::SPILL_CR: 302 case PPC::SPILL_CRBIT: 303 case PPC::STVX: 304 case PPC::STXVD2X: 305 case PPC::QVSTFDX: 306 case PPC::QVSTFSXs: 307 case PPC::QVSTFDXb: 308 case PPC::SPILL_VRSAVE: 309 // Check for the operands added by addFrameReference (the immediate is the 310 // offset which defaults to 0). 311 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && 312 MI->getOperand(2).isFI()) { 313 FrameIndex = MI->getOperand(2).getIndex(); 314 return MI->getOperand(0).getReg(); 315 } 316 break; 317 } 318 return 0; 319 } 320 321 MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr *MI, 322 bool NewMI, 323 unsigned OpIdx1, 324 unsigned OpIdx2) const { 325 MachineFunction &MF = *MI->getParent()->getParent(); 326 327 // Normal instructions can be commuted the obvious way. 328 if (MI->getOpcode() != PPC::RLWIMI && 329 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,MachineBasicBlock *&TBB, 450 MachineBasicBlock *&FBB, 451 SmallVectorImpl<MachineOperand> &Cond, 452 bool AllowModify) const { 453 bool isPPC64 = Subtarget.isPPC64(); 454 455 // If the block has no terminators, it just falls into the block after it. 456 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 457 if (I == MBB.end()) 458 return false; 459 460 if (!isUnpredicatedTerminator(*I)) 461 return false; 462 463 // Get the last instruction in the block. 464 MachineInstr *LastInst = I; 465 466 // If there is only one terminator instruction, process it. 467 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { 468 if (LastInst->getOpcode() == PPC::B) { 469 if (!LastInst->getOperand(0).isMBB()) 470 return true; 471 TBB = LastInst->getOperand(0).getMBB(); 472 return false; 473 } else if (LastInst->getOpcode() == PPC::BCC) { 474 if (!LastInst->getOperand(2).isMBB()) 475 return true; 476 // Block ends with fall-through condbranch. 477 TBB = LastInst->getOperand(2).getMBB(); 478 Cond.push_back(LastInst->getOperand(0)); 479 Cond.push_back(LastInst->getOperand(1)); 480 return false; 481 } else if (LastInst->getOpcode() == PPC::BC) { 482 if (!LastInst->getOperand(1).isMBB()) 483 return true; 484 // Block ends with fall-through condbranch. 485 TBB = LastInst->getOperand(1).getMBB(); 486 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 487 Cond.push_back(LastInst->getOperand(0)); 488 return false; 489 } else if (LastInst->getOpcode() == PPC::BCn) { 490 if (!LastInst->getOperand(1).isMBB()) 491 return true; 492 // Block ends with fall-through condbranch. 493 TBB = LastInst->getOperand(1).getMBB(); 494 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 495 Cond.push_back(LastInst->getOperand(0)); 496 return false; 497 } else if (LastInst->getOpcode() == PPC::BDNZ8 || 498 LastInst->getOpcode() == PPC::BDNZ) { 499 if (!LastInst->getOperand(0).isMBB()) 500 return true; 501 if (DisableCTRLoopAnal) 502 return true; 503 TBB = LastInst->getOperand(0).getMBB(); 504 Cond.push_back(MachineOperand::CreateImm(1)); 505 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 506 true)); 507 return false; 508 } else if (LastInst->getOpcode() == PPC::BDZ8 || 509 LastInst->getOpcode() == PPC::BDZ) { 510 if (!LastInst->getOperand(0).isMBB()) 511 return true; 512 if (DisableCTRLoopAnal) 513 return true; 514 TBB = LastInst->getOperand(0).getMBB(); 515 Cond.push_back(MachineOperand::CreateImm(0)); 516 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 517 true)); 518 return false; 519 } 520 521 // Otherwise, don't know what this is. 522 return true; 523 } 524 525 // Get the instruction before it if it's a terminator. 526 MachineInstr *SecondLastInst = I; 527 528 // If there are three terminators, we don't know what sort of block this is. 529 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I)) 530 return true; 531 532 // If the block ends with PPC::B and PPC:BCC, handle it. 533 if (SecondLastInst->getOpcode() == PPC::BCC && 534 LastInst->getOpcode() == PPC::B) { 535 if (!SecondLastInst->getOperand(2).isMBB() || 536 !LastInst->getOperand(0).isMBB()) 537 return true; 538 TBB = SecondLastInst->getOperand(2).getMBB(); 539 Cond.push_back(SecondLastInst->getOperand(0)); 540 Cond.push_back(SecondLastInst->getOperand(1)); 541 FBB = LastInst->getOperand(0).getMBB(); 542 return false; 543 } else if (SecondLastInst->getOpcode() == PPC::BC && 544 LastInst->getOpcode() == PPC::B) { 545 if (!SecondLastInst->getOperand(1).isMBB() || 546 !LastInst->getOperand(0).isMBB()) 547 return true; 548 TBB = SecondLastInst->getOperand(1).getMBB(); 549 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 550 Cond.push_back(SecondLastInst->getOperand(0)); 551 FBB = LastInst->getOperand(0).getMBB(); 552 return false; 553 } else if (SecondLastInst->getOpcode() == PPC::BCn && 554 LastInst->getOpcode() == PPC::B) { 555 if (!SecondLastInst->getOperand(1).isMBB() || 556 !LastInst->getOperand(0).isMBB()) 557 return true; 558 TBB = SecondLastInst->getOperand(1).getMBB(); 559 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); 560 Cond.push_back(SecondLastInst->getOperand(0)); 561 FBB = LastInst->getOperand(0).getMBB(); 562 return false; 563 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 || 564 SecondLastInst->getOpcode() == PPC::BDNZ) && 565 LastInst->getOpcode() == PPC::B) { 566 if (!SecondLastInst->getOperand(0).isMBB() || 567 !LastInst->getOperand(0).isMBB()) 568 return true; 569 if (DisableCTRLoopAnal) 570 return true; 571 TBB = SecondLastInst->getOperand(0).getMBB(); 572 Cond.push_back(MachineOperand::CreateImm(1)); 573 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 574 true)); 575 FBB = LastInst->getOperand(0).getMBB(); 576 return false; 577 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 || 578 SecondLastInst->getOpcode() == PPC::BDZ) && 579 LastInst->getOpcode() == PPC::B) { 580 if (!SecondLastInst->getOperand(0).isMBB() || 581 !LastInst->getOperand(0).isMBB()) 582 return true; 583 if (DisableCTRLoopAnal) 584 return true; 585 TBB = SecondLastInst->getOperand(0).getMBB(); 586 Cond.push_back(MachineOperand::CreateImm(0)); 587 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, 588 true)); 589 FBB = LastInst->getOperand(0).getMBB(); 590 return false; 591 } 592 593 // If the block ends with two PPC:Bs, handle it. The second one is not 594 // executed, so remove it. 595 if (SecondLastInst->getOpcode() == PPC::B && 596 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) const { 611 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 612 if (I == MBB.end()) 613 return 0; 614 615 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && 616 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 617 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 618 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 619 return 0; 620 621 // Remove the branch. 622 I->eraseFromParent(); 623 624 I = MBB.end(); 625 626 if (I == MBB.begin()) return 1; 627 --I; 628 if (I->getOpcode() != PPC::BCC && 629 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && 630 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && 631 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) 632 return 1; 633 634 // Remove the branch. 635 I->eraseFromParent(); 636 return 2; 637 } 638 639 unsigned 640 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 641 MachineBasicBlock *FBB, 642 ArrayRef<MachineOperand> Cond, 643 DebugLoc DL) const { 644 // Shouldn't be a fall through. 645 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 646 assert((Cond.size() == 2 || Cond.size() == 0) && 647 "PPC branch conditions have two components!"); 648 649 bool isPPC64 = Subtarget.isPPC64(); 650 651 // One-way branch. 652 if (!FBB) { 653 if (Cond.empty()) // Unconditional branch 654 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); 655 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 656 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 657 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 658 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 659 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 660 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); 661 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 662 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); 663 else // Conditional branch 664 BuildMI(&MBB, DL, get(PPC::BCC)) 665 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); 666 return 1; 667 } 668 669 // Two-way Conditional Branch. 670 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 671 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 672 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 673 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 674 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 675 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); 676 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 677 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); 678 else 679 BuildMI(&MBB, DL, get(PPC::BCC)) 680 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); 681 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); 682 return 2; 683 } 684 685 // Select analysis. 686 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 687 ArrayRef<MachineOperand> Cond, 688 unsigned TrueReg, unsigned FalseReg, 689 int &CondCycles, int &TrueCycles, int &FalseCycles) const { 690 if (!Subtarget.hasISEL()) 691 return false; 692 693 if (Cond.size() != 2) 694 return false; 695 696 // If this is really a bdnz-like condition, then it cannot be turned into a 697 // select. 698 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 699 return false; 700 701 // Check register classes. 702 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 703 const TargetRegisterClass *RC = 704 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 705 if (!RC) 706 return false; 707 708 // isel is for regular integer GPRs only. 709 if (!PPC::GPRCRegClass.hasSubClassEq(RC) && 710 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && 711 !PPC::G8RCRegClass.hasSubClassEq(RC) && 712 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) 713 return false; 714 715 // FIXME: These numbers are for the A2, how well they work for other cores is 716 // an open question. On the A2, the isel instruction has a 2-cycle latency 717 // but single-cycle throughput. These numbers are used in combination with 718 // the MispredictPenalty setting from the active SchedMachineModel. 719 CondCycles = 1; 720 TrueCycles = 1; 721 FalseCycles = 1; 722 723 return true; 724 } 725 726 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, 727 MachineBasicBlock::iterator MI, DebugLoc dl, 728 unsigned DestReg, ArrayRef<MachineOperand> Cond, 729 unsigned TrueReg, unsigned FalseReg) const { 730 assert(Cond.size() == 2 && 731 "PPC branch conditions have two components!"); 732 733 assert(Subtarget.hasISEL() && 734 "Cannot insert select on target without ISEL support"); 735 736 // Get the register classes. 737 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 738 const TargetRegisterClass *RC = 739 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 740 assert(RC && "TrueReg and FalseReg must have overlapping register classes"); 741 742 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || 743 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); 744 assert((Is64Bit || 745 PPC::GPRCRegClass.hasSubClassEq(RC) || 746 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && 747 "isel is for regular integer GPRs only"); 748 749 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; 750 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm()); 751 752 unsigned SubIdx = 0; 753 bool SwapOps = false; 754 switch (SelectPred) { 755 case PPC::PRED_EQ: 756 case PPC::PRED_EQ_MINUS: 757 case PPC::PRED_EQ_PLUS: 758 SubIdx = PPC::sub_eq; SwapOps = false; break; 759 case PPC::PRED_NE: 760 case PPC::PRED_NE_MINUS: 761 case PPC::PRED_NE_PLUS: 762 SubIdx = PPC::sub_eq; SwapOps = true; break; 763 case PPC::PRED_LT: 764 case PPC::PRED_LT_MINUS: 765 case PPC::PRED_LT_PLUS: 766 SubIdx = PPC::sub_lt; SwapOps = false; break; 767 case PPC::PRED_GE: 768 case PPC::PRED_GE_MINUS: 769 case PPC::PRED_GE_PLUS: 770 SubIdx = PPC::sub_lt; SwapOps = true; break; 771 case PPC::PRED_GT: 772 case PPC::PRED_GT_MINUS: 773 case PPC::PRED_GT_PLUS: 774 SubIdx = PPC::sub_gt; SwapOps = false; break; 775 case PPC::PRED_LE: 776 case PPC::PRED_LE_MINUS: 777 case PPC::PRED_LE_PLUS: 778 SubIdx = PPC::sub_gt; SwapOps = true; break; 779 case PPC::PRED_UN: 780 case PPC::PRED_UN_MINUS: 781 case PPC::PRED_UN_PLUS: 782 SubIdx = PPC::sub_un; SwapOps = false; break; 783 case PPC::PRED_NU: 784 case PPC::PRED_NU_MINUS: 785 case PPC::PRED_NU_PLUS: 786 SubIdx = PPC::sub_un; SwapOps = true; break; 787 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; 788 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; 789 } 790 791 unsigned FirstReg = SwapOps ? FalseReg : TrueReg, 792 SecondReg = SwapOps ? TrueReg : FalseReg; 793 794 // The first input register of isel cannot be r0. If it is a member 795 // of a register class that can be r0, then copy it first (the 796 // register allocator should eliminate the copy). 797 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || 798 MRI.getRegClass(FirstReg)->contains(PPC::X0)) { 799 const TargetRegisterClass *FirstRC = 800 MRI.getRegClass(FirstReg)->contains(PPC::X0) ? 801 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; 802 unsigned OldFirstReg = FirstReg; 803 FirstReg = MRI.createVirtualRegister(FirstRC); 804 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) 805 .addReg(OldFirstReg); 806 } 807 808 BuildMI(MBB, MI, dl, get(OpCode), DestReg) 809 .addReg(FirstReg).addReg(SecondReg) 810 .addReg(Cond[1].getReg(), 0, SubIdx); 811 } 812 813 static unsigned getCRBitValue(unsigned CRBit) { 814 unsigned Ret = 4; 815 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || 816 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || 817 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || 818 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) 819 Ret = 3; 820 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || 821 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || 822 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || 823 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) 824 Ret = 2; 825 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || 826 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || 827 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || 828 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) 829 Ret = 1; 830 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || 831 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || 832 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || 833 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) 834 Ret = 0; 835 836 assert(Ret != 4 && "Invalid CR bit register"); 837 return Ret; 838 } 839 840 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 841 MachineBasicBlock::iterator I, DebugLoc DL, 842 unsigned DestReg, unsigned SrcReg, 843 bool KillSrc) const { 844 // We can end up with self copies and similar things as a result of VSX copy 845 // legalization. Promote them here. 846 const TargetRegisterInfo *TRI = &getRegisterInfo(); 847 if (PPC::F8RCRegClass.contains(DestReg) && 848 PPC::VSRCRegClass.contains(SrcReg)) { 849 unsigned SuperReg = 850 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); 851 852 if (VSXSelfCopyCrash && SrcReg == SuperReg) 853 llvm_unreachable("nop VSX copy"); 854 855 DestReg = SuperReg; 856 } else if (PPC::VRRCRegClass.contains(DestReg) && 857 PPC::VSRCRegClass.contains(SrcReg)) { 858 unsigned SuperReg = 859 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass); 860 861 if (VSXSelfCopyCrash && SrcReg == SuperReg) 862 llvm_unreachable("nop VSX copy"); 863 864 DestReg = SuperReg; 865 } else if (PPC::F8RCRegClass.contains(SrcReg) && 866 PPC::VSRCRegClass.contains(DestReg)) { 867 unsigned SuperReg = 868 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); 869 870 if (VSXSelfCopyCrash && DestReg == SuperReg) 871 llvm_unreachable("nop VSX copy"); 872 873 SrcReg = SuperReg; 874 } else if (PPC::VRRCRegClass.contains(SrcReg) && 875 PPC::VSRCRegClass.contains(DestReg)) { 876 unsigned SuperReg = 877 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass); 878 879 if (VSXSelfCopyCrash && DestReg == SuperReg) 880 llvm_unreachable("nop VSX copy"); 881 882 SrcReg = SuperReg; 883 } 884 885 // Different class register copy 886 if (PPC::CRBITRCRegClass.contains(SrcReg) && 887 PPC::GPRCRegClass.contains(DestReg)) { 888 unsigned CRReg = getCRFromCRBit(SrcReg); 889 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg); 890 getKillRegState(KillSrc); 891 // Rotate the CR bit in the CR fields to be the least significant bit and 892 // then mask with 0x1 (MB = ME = 31). 893 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) 894 .addReg(DestReg, RegState::Kill) 895 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) 896 .addImm(31) 897 .addImm(31); 898 return; 899 } else if (PPC::CRRCRegClass.contains(SrcReg) && 900 PPC::G8RCRegClass.contains(DestReg)) { 901 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg); 902 getKillRegState(KillSrc); 903 return; 904 } else if (PPC::CRRCRegClass.contains(SrcReg) && 905 PPC::GPRCRegClass.contains(DestReg)) { 906 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); 907 getKillRegState(KillSrc); 908 return; 909 } 910 911 unsigned Opc; 912 if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) 913 Opc = PPC::OR; 914 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) 915 Opc = PPC::OR8; 916 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) 917 Opc = PPC::FMR; 918 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) 919 Opc = PPC::MCRF; 920 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) 921 Opc = PPC::VOR; 922 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) 923 // There are two different ways this can be done: 924 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only 925 // issue in VSU pipeline 0. 926 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but 927 // can go to either pipeline. 928 // We'll always use xxlor here, because in practically all cases where 929 // copies are generated, they are close enough to some use that the 930 // lower-latency form is preferable. 931 Opc = PPC::XXLOR; 932 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || 933 PPC::VSSRCRegClass.contains(DestReg, SrcReg)) 934 Opc = PPC::XXLORf; 935 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) 936 Opc = PPC::QVFMR; 937 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) 938 Opc = PPC::QVFMRs; 939 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) 940 Opc = PPC::QVFMRb; 941 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) 942 Opc = PPC::CROR; 943 else 944 llvm_unreachable("Impossible reg-to-reg copy"); 945 946 const MCInstrDesc &MCID = get(Opc); 947 if (MCID.getNumOperands() == 3) 948 BuildMI(MBB, I, DL, MCID, DestReg) 949 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); 950 else 951 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); 952 } 953 954 // This function returns true if a CR spill is necessary and false otherwise. 955 bool 956 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, 957 unsigned SrcReg, bool isKill, 958 int FrameIdx, 959 const TargetRegisterClass *RC, 960 SmallVectorImpl<MachineInstr*> &NewMIs, 961 bool &NonRI, bool &SpillsVRS) const{ 962 // Note: If additional store instructions are added here, 963 // update isStoreToStackSlot. 964 965 DebugLoc DL; 966 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 967 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 968 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) 969 .addReg(SrcReg, 970 getKillRegState(isKill)), 971 FrameIdx)); 972 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 973 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 974 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) 975 .addReg(SrcReg, 976 getKillRegState(isKill)), 977 FrameIdx)); 978 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 979 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) 980 .addReg(SrcReg, 981 getKillRegState(isKill)), 982 FrameIdx)); 983 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 984 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) 985 .addReg(SrcReg, 986 getKillRegState(isKill)), 987 FrameIdx)); 988 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 989 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) 990 .addReg(SrcReg, 991 getKillRegState(isKill)), 992 FrameIdx)); 993 return true; 994 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 995 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) 996 .addReg(SrcReg, 997 getKillRegState(isKill)), 998 FrameIdx)); 999 return true; 1000 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1001 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) 1002 .addReg(SrcReg, 1003 getKillRegState(isKill)), 1004 FrameIdx)); 1005 NonRI = true; 1006 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1007 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X)) 1008 .addReg(SrcReg, 1009 getKillRegState(isKill)), 1010 FrameIdx)); 1011 NonRI = true; 1012 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1013 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX)) 1014 .addReg(SrcReg, 1015 getKillRegState(isKill)), 1016 FrameIdx)); 1017 NonRI = true; 1018 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1019 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX)) 1020 .addReg(SrcReg, 1021 getKillRegState(isKill)), 1022 FrameIdx)); 1023 NonRI = true; 1024 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1025 assert(Subtarget.isDarwin() && 1026 "VRSAVE only needs spill/restore on Darwin"); 1027 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) 1028 .addReg(SrcReg, 1029 getKillRegState(isKill)), 1030 FrameIdx)); 1031 SpillsVRS = true; 1032 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1033 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX)) 1034 .addReg(SrcReg, 1035 getKillRegState(isKill)), 1036 FrameIdx)); 1037 NonRI = true; 1038 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1039 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs)) 1040 .addReg(SrcReg, 1041 getKillRegState(isKill)), 1042 FrameIdx)); 1043 NonRI = true; 1044 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1045 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) 1046 .addReg(SrcReg, 1047 getKillRegState(isKill)), 1048 FrameIdx)); 1049 NonRI = true; 1050 } else { 1051 llvm_unreachable("Unknown regclass!"); 1052 } 1053 1054 return false; 1055 } 1056 1057 void 1058 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 1059 MachineBasicBlock::iterator MI, 1060 unsigned SrcReg, bool isKill, int FrameIdx, 1061 const TargetRegisterClass *RC, 1062 const TargetRegisterInfo *TRI) const { 1063 MachineFunction &MF = *MBB.getParent(); 1064 SmallVector<MachineInstr*, 4> NewMIs; 1065 1066 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1067 FuncInfo->setHasSpills(); 1068 1069 bool NonRI = false, SpillsVRS = false; 1070 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, 1071 NonRI, SpillsVRS)) 1072 FuncInfo->setSpillsCR(); 1073 1074 if (SpillsVRS) 1075 FuncInfo->setSpillsVRSAVE(); 1076 1077 if (NonRI) 1078 FuncInfo->setHasNonRISpills(); 1079 1080 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1081 MBB.insert(MI, NewMIs[i]); 1082 1083 const MachineFrameInfo &MFI = *MF.getFrameInfo(); 1084 MachineMemOperand *MMO = MF.getMachineMemOperand( 1085 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1086 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx), 1087 MFI.getObjectAlignment(FrameIdx)); 1088 NewMIs.back()->addMemOperand(MF, MMO); 1089 } 1090 1091 bool 1092 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL, 1093 unsigned DestReg, int FrameIdx, 1094 const TargetRegisterClass *RC, 1095 SmallVectorImpl<MachineInstr*> &NewMIs, 1096 bool &NonRI, bool &SpillsVRS) const{ 1097 // Note: If additional load instructions are added here, 1098 // update isLoadFromStackSlot. 1099 1100 if (PPC::GPRCRegClass.hasSubClassEq(RC) || 1101 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { 1102 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), 1103 DestReg), FrameIdx)); 1104 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || 1105 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { 1106 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), 1107 FrameIdx)); 1108 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { 1109 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), 1110 FrameIdx)); 1111 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { 1112 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), 1113 FrameIdx)); 1114 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { 1115 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1116 get(PPC::RESTORE_CR), DestReg), 1117 FrameIdx)); 1118 return true; 1119 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { 1120 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1121 get(PPC::RESTORE_CRBIT), DestReg), 1122 FrameIdx)); 1123 return true; 1124 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1125 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), 1126 FrameIdx)); 1127 NonRI = true; 1128 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1129 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg), 1130 FrameIdx)); 1131 NonRI = true; 1132 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1133 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg), 1134 FrameIdx)); 1135 NonRI = true; 1136 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { 1137 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg), 1138 FrameIdx)); 1139 NonRI = true; 1140 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { 1141 assert(Subtarget.isDarwin() && 1142 "VRSAVE only needs spill/restore on Darwin"); 1143 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, 1144 get(PPC::RESTORE_VRSAVE), 1145 DestReg), 1146 FrameIdx)); 1147 SpillsVRS = true; 1148 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { 1149 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg), 1150 FrameIdx)); 1151 NonRI = true; 1152 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { 1153 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg), 1154 FrameIdx)); 1155 NonRI = true; 1156 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1157 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), 1158 FrameIdx)); 1159 NonRI = true; 1160 } else { 1161 llvm_unreachable("Unknown regclass!"); 1162 } 1163 1164 return false; 1165 } 1166 1167 void 1168 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 1169 MachineBasicBlock::iterator MI, 1170 unsigned DestReg, int FrameIdx, 1171 const TargetRegisterClass *RC, 1172 const TargetRegisterInfo *TRI) const { 1173 MachineFunction &MF = *MBB.getParent(); 1174 SmallVector<MachineInstr*, 4> NewMIs; 1175 DebugLoc DL; 1176 if (MI != MBB.end()) DL = MI->getDebugLoc(); 1177 1178 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1179 FuncInfo->setHasSpills(); 1180 1181 bool NonRI = false, SpillsVRS = false; 1182 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, 1183 NonRI, SpillsVRS)) 1184 FuncInfo->setSpillsCR(); 1185 1186 if (SpillsVRS) 1187 FuncInfo->setSpillsVRSAVE(); 1188 1189 if (NonRI) 1190 FuncInfo->setHasNonRISpills(); 1191 1192 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 1193 MBB.insert(MI, NewMIs[i]); 1194 1195 const MachineFrameInfo &MFI = *MF.getFrameInfo(); 1196 MachineMemOperand *MMO = MF.getMachineMemOperand( 1197 MachinePointerInfo::getFixedStack(MF, FrameIdx), 1198 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx), 1199 MFI.getObjectAlignment(FrameIdx)); 1200 NewMIs.back()->addMemOperand(MF, MMO); 1201 } 1202 1203 bool PPCInstrInfo:: 1204 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 1205 assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); 1206 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) 1207 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); 1208 else 1209 // Leave the CR# the same, but invert the condition. 1210 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); 1211 return false; 1212 } 1213 1214 bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI, 1215 unsigned Reg, MachineRegisterInfo *MRI) const { 1216 // For some instructions, it is legal to fold ZERO into the RA register field. 1217 // A zero immediate should always be loaded with a single li. 1218 unsigned DefOpc = DefMI->getOpcode(); 1219 if (DefOpc != PPC::LI && DefOpc != PPC::LI8) 1220 return false; 1221 if (!DefMI->getOperand(1).isImm()) 1222 return false; 1223 if (DefMI->getOperand(1).getImm() != 0) 1224 return false; 1225 1226 // Note that we cannot here invert the arguments of an isel in order to fold 1227 // a ZERO into what is presented as the second argument. All we have here 1228 // is the condition bit, and that might come from a CR-logical bit operation. 1229 1230 const MCInstrDesc &UseMCID = UseMI->getDesc(); 1231 1232 // Only fold into real machine instructions. 1233 if (UseMCID.isPseudo()) 1234 return false; 1235 1236 unsigned UseIdx; 1237 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx) 1238 if (UseMI->getOperand(UseIdx).isReg() && 1239 UseMI->getOperand(UseIdx).getReg() == Reg) 1240 break; 1241 1242 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI"); 1243 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); 1244 1245 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; 1246 1247 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 1248 // register (which might also be specified as a pointer class kind). 1249 if (UseInfo->isLookupPtrRegClass()) { 1250 if (UseInfo->RegClass /* Kind */ != 1) 1251 return false; 1252 } else { 1253 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && 1254 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) 1255 return false; 1256 } 1257 1258 // Make sure this is not tied to an output register (or otherwise 1259 // constrained). This is true for ST?UX registers, for example, which 1260 // are tied to their output registers. 1261 if (UseInfo->Constraints != 0) 1262 return false; 1263 1264 unsigned ZeroReg; 1265 if (UseInfo->isLookupPtrRegClass()) { 1266 bool isPPC64 = Subtarget.isPPC64(); 1267 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; 1268 } else { 1269 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? 1270 PPC::ZERO8 : PPC::ZERO; 1271 } 1272 1273 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 1274 UseMI->getOperand(UseIdx).setReg(ZeroReg); 1275 1276 if (DeleteDef) 1277 DefMI->eraseFromParent(); 1278 1279 return true; 1280 } 1281 1282 static bool MBBDefinesCTR(MachineBasicBlock &MBB) { 1283 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 1284 I != IE; ++I) 1285 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) 1286 return true; 1287 return false; 1288 } 1289 1290 // We should make sure that, if we're going to predicate both sides of a 1291 // condition (a diamond), that both sides don't define the counter register. We 1292 // can predicate counter-decrement-based branches, but while that predicates 1293 // the branching, it does not predicate the counter decrement. If we tried to 1294 // merge the triangle into one predicated block, we'd decrement the counter 1295 // twice. 1296 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, 1297 unsigned NumT, unsigned ExtraT, 1298 MachineBasicBlock &FMBB, 1299 unsigned NumF, unsigned ExtraF, 1300 BranchProbability Probability) const { 1301 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); 1302 } 1303 1304 1305 bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const { 1306 // The predicated branches are identified by their type, not really by the 1307 // explicit presence of a predicate. Furthermore, some of them can be 1308 // predicated more than once. Because if conversion won't try to predicate 1309 // any instruction which already claims to be predicated (by returning true 1310 // here), always return false. In doing so, we let isPredicable() be the 1311 // final word on whether not the instruction can be (further) predicated. 1312 1313 return false; 1314 } 1315 1316 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const { 1317 if (!MI.isTerminator()) 1318 return false; 1319 1320 // Conditional branch is a special case. 1321 if (MI.isBranch() && !MI.isBarrier()) 1322 return true; 1323 1324 return !isPredicated(MI); 1325 } 1326 1327 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI, 1328 ArrayRef<MachineOperand> Pred) const { 1329 unsigned OpC = MI.getOpcode(); 1330 if (OpC == PPC::BLR || OpC == PPC::BLR8) { 1331 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1332 bool isPPC64 = Subtarget.isPPC64(); 1333 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) 1334 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); 1335 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1336 MI.setDesc(get(PPC::BCLR)); 1337 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1338 .addReg(Pred[1].getReg()); 1339 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1340 MI.setDesc(get(PPC::BCLRn)); 1341 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1342 .addReg(Pred[1].getReg()); 1343 } else { 1344 MI.setDesc(get(PPC::BCCLR)); 1345 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1346 .addImm(Pred[0].getImm()) 1347 .addReg(Pred[1].getReg()); 1348 } 1349 1350 return true; 1351 } else if (OpC == PPC::B) { 1352 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { 1353 bool isPPC64 = Subtarget.isPPC64(); 1354 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) 1355 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); 1356 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1357 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1358 MI.RemoveOperand(0); 1359 1360 MI.setDesc(get(PPC::BC)); 1361 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1362 .addReg(Pred[1].getReg()) 1363 .addMBB(MBB); 1364 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1365 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1366 MI.RemoveOperand(0); 1367 1368 MI.setDesc(get(PPC::BCn)); 1369 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1370 .addReg(Pred[1].getReg()) 1371 .addMBB(MBB); 1372 } else { 1373 MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); 1374 MI.RemoveOperand(0); 1375 1376 MI.setDesc(get(PPC::BCC)); 1377 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1378 .addImm(Pred[0].getImm()) 1379 .addReg(Pred[1].getReg()) 1380 .addMBB(MBB); 1381 } 1382 1383 return true; 1384 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || 1385 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { 1386 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) 1387 llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); 1388 1389 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; 1390 bool isPPC64 = Subtarget.isPPC64(); 1391 1392 if (Pred[0].getImm() == PPC::PRED_BIT_SET) { 1393 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) 1394 : (setLR ? PPC::BCCTRL : PPC::BCCTR))); 1395 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1396 .addReg(Pred[1].getReg()); 1397 return true; 1398 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { 1399 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) 1400 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); 1401 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1402 .addReg(Pred[1].getReg()); 1403 return true; 1404 } 1405 1406 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) 1407 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); 1408 MachineInstrBuilder(*MI.getParent()->getParent(), MI) 1409 .addImm(Pred[0].getImm()) 1410 .addReg(Pred[1].getReg()); 1411 return true; 1412 } 1413 1414 return false; 1415 } 1416 1417 bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, 1418 ArrayRef<MachineOperand> Pred2) const { 1419 assert(Pred1.size() == 2 && "Invalid PPC first predicate"); 1420 assert(Pred2.size() == 2 && "Invalid PPC second predicate"); 1421 1422 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) 1423 return false; 1424 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) 1425 return false; 1426 1427 // P1 can only subsume P2 if they test the same condition register. 1428 if (Pred1[1].getReg() != Pred2[1].getReg()) 1429 return false; 1430 1431 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); 1432 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); 1433 1434 if (P1 == P2) 1435 return true; 1436 1437 // Does P1 subsume P2, e.g. GE subsumes GT. 1438 if (P1 == PPC::PRED_LE && 1439 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) 1440 return true; 1441 if (P1 == PPC::PRED_GE && 1442 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) 1443 return true; 1444 1445 return false; 1446 } 1447 1448 bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI, 1449 std::vector<MachineOperand> &Pred) const { 1450 // Note: At the present time, the contents of Pred from this function is 1451 // unused by IfConversion. This implementation follows ARM by pushing the 1452 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of 1453 // predicate, instructions defining CTR or CTR8 are also included as 1454 // predicate-defining instructions. 1455 1456 const TargetRegisterClass *RCs[] = 1457 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, 1458 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; 1459 1460 bool Found = false; 1461 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1462 const MachineOperand &MO = MI.getOperand(i); 1463 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { 1464 const TargetRegisterClass *RC = RCs[c]; 1465 if (MO.isReg()) { 1466 if (MO.isDef() && RC->contains(MO.getReg())) { 1467 Pred.push_back(MO); 1468 Found = true; 1469 } 1470 } else if (MO.isRegMask()) { 1471 for (TargetRegisterClass::iterator I = RC->begin(), 1472 IE = RC->end(); I != IE; ++I) 1473 if (MO.clobbersPhysReg(*I)) { 1474 Pred.push_back(MO); 1475 Found = true; 1476 } 1477 } 1478 } 1479 } 1480 1481 return Found; 1482 } 1483 1484 bool PPCInstrInfo::isPredicable(MachineInstr &MI) const { 1485 unsigned OpC = MI.getOpcode(); 1486 switch (OpC) { 1487 default: 1488 return false; 1489 case PPC::B: 1490 case PPC::BLR: 1491 case PPC::BLR8: 1492 case PPC::BCTR: 1493 case PPC::BCTR8: 1494 case PPC::BCTRL: 1495 case PPC::BCTRL8: 1496 return true; 1497 } 1498 } 1499 1500 bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI, 1501 unsigned &SrcReg, unsigned &SrcReg2, 1502 int &Mask, int &Value) const { 1503 unsigned Opc = MI->getOpcode(); 1504 1505 switch (Opc) { 1506 default: return false; 1507 case PPC::CMPWI: 1508 case PPC::CMPLWI: 1509 case PPC::CMPDI: 1510 case PPC::CMPLDI: 1511 SrcReg = MI->getOperand(1).getReg(); 1512 SrcReg2 = 0; 1513 Value = MI->getOperand(2).getImm(); 1514 Mask = 0xFFFF; 1515 return true; 1516 case PPC::CMPW: 1517 case PPC::CMPLW: 1518 case PPC::CMPD: 1519 case PPC::CMPLD: 1520 case PPC::FCMPUS: 1521 case PPC::FCMPUD: 1522 SrcReg = MI->getOperand(1).getReg(); 1523 SrcReg2 = MI->getOperand(2).getReg(); 1524 return true; 1525 } 1526 } 1527 1528 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr, 1529 unsigned SrcReg, unsigned SrcReg2, 1530 int Mask, int Value, 1531 const MachineRegisterInfo *MRI) const { 1532 if (DisableCmpOpt) 1533 return false; 1534 1535 int OpC = CmpInstr->getOpcode(); 1536 unsigned CRReg = CmpInstr->getOperand(0).getReg(); 1537 1538 // FP record forms set CR1 based on the execption status bits, not a 1539 // comparison with zero. 1540 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) 1541 return false; 1542 1543 // The record forms set the condition register based on a signed comparison 1544 // with zero (so says the ISA manual). This is not as straightforward as it 1545 // seems, however, because this is always a 64-bit comparison on PPC64, even 1546 // for instructions that are 32-bit in nature (like slw for example). 1547 // So, on PPC32, for unsigned comparisons, we can use the record forms only 1548 // for equality checks (as those don't depend on the sign). On PPC64, 1549 // we are restricted to equality for unsigned 64-bit comparisons and for 1550 // signed 32-bit comparisons the applicability is more restricted. 1551 bool isPPC64 = Subtarget.isPPC64(); 1552 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; 1553 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; 1554 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; 1555 1556 // Get the unique definition of SrcReg. 1557 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 1558 if (!MI) return false; 1559 int MIOpC = MI->getOpcode(); 1560 1561 bool equalityOnly = false; 1562 bool noSub = false; 1563 if (isPPC64) { 1564 if (is32BitSignedCompare) { 1565 // We can perform this optimization only if MI is sign-extending. 1566 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || 1567 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || 1568 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || 1569 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || 1570 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { 1571 noSub = true; 1572 } else 1573 return false; 1574 } else if (is32BitUnsignedCompare) { 1575 // 32-bit rotate and mask instructions are zero extending only if MB <= ME 1576 bool isZeroExtendingRotate = 1577 (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo || 1578 MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo) 1579 && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm(); 1580 1581 // We can perform this optimization, equality only, if MI is 1582 // zero-extending. 1583 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || 1584 MIOpC == PPC::SLW || MIOpC == PPC::SLWo || 1585 MIOpC == PPC::SRW || MIOpC == PPC::SRWo || 1586 isZeroExtendingRotate) { 1587 noSub = true; 1588 equalityOnly = true; 1589 } else 1590 return false; 1591 } else 1592 equalityOnly = is64BitUnsignedCompare; 1593 } else 1594 equalityOnly = is32BitUnsignedCompare; 1595 1596 if (equalityOnly) { 1597 // We need to check the uses of the condition register in order to reject 1598 // non-equality comparisons. 1599 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg), 1600 IE = MRI->use_instr_end(); I != IE; ++I) { 1601 MachineInstr *UseMI = &*I; 1602 if (UseMI->getOpcode() == PPC::BCC) { 1603 unsigned Pred = UseMI->getOperand(0).getImm(); 1604 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE) 1605 return false; 1606 } else if (UseMI->getOpcode() == PPC::ISEL || 1607 UseMI->getOpcode() == PPC::ISEL8) { 1608 unsigned SubIdx = UseMI->getOperand(3).getSubReg(); 1609 if (SubIdx != PPC::sub_eq) 1610 return false; 1611 } else 1612 return false; 1613 } 1614 } 1615 1616 MachineBasicBlock::iterator I = CmpInstr; 1617 1618 // Scan forward to find the first use of the compare. 1619 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end(); 1620 I != EL; ++I) { 1621 bool FoundUse = false; 1622 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg), 1623 JE = MRI->use_instr_end(); J != JE; ++J) 1624 if (&*J == &*I) { 1625 FoundUse = true; 1626 break; 1627 } 1628 1629 if (FoundUse) 1630 break; 1631 } 1632 1633 // There are two possible candidates which can be changed to set CR[01]. 1634 // One is MI, the other is a SUB instruction. 1635 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 1636 MachineInstr *Sub = nullptr; 1637 if (SrcReg2 != 0) 1638 // MI is not a candidate for CMPrr. 1639 MI = nullptr; 1640 // FIXME: Conservatively refuse to convert an instruction which isn't in the 1641 // same BB as the comparison. This is to allow the check below to avoid calls 1642 // (and other explicit clobbers); instead we should really check for these 1643 // more explicitly (in at least a few predecessors). 1644 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) { 1645 // PPC does not have a record-form SUBri. 1646 return false; 1647 } 1648 1649 // Search for Sub. 1650 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1651 --I; 1652 1653 // Get ready to iterate backward from CmpInstr. 1654 MachineBasicBlock::iterator E = MI, 1655 B = CmpInstr->getParent()->begin(); 1656 1657 for (; I != E && !noSub; --I) { 1658 const MachineInstr &Instr = *I; 1659 unsigned IOpC = Instr.getOpcode(); 1660 1661 if (&*I != CmpInstr && ( 1662 Instr.modifiesRegister(PPC::CR0, TRI) || 1663 Instr.readsRegister(PPC::CR0, TRI))) 1664 // This instruction modifies or uses the record condition register after 1665 // the one we want to change. While we could do this transformation, it 1666 // would likely not be profitable. This transformation removes one 1667 // instruction, and so even forcing RA to generate one move probably 1668 // makes it unprofitable. 1669 return false; 1670 1671 // Check whether CmpInstr can be made redundant by the current instruction. 1672 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || 1673 OpC == PPC::CMPD || OpC == PPC::CMPLD) && 1674 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && 1675 ((Instr.getOperand(1).getReg() == SrcReg && 1676 Instr.getOperand(2).getReg() == SrcReg2) || 1677 (Instr.getOperand(1).getReg() == SrcReg2 && 1678 Instr.getOperand(2).getReg() == SrcReg))) { 1679 Sub = &*I; 1680 break; 1681 } 1682 1683 if (I == B) 1684 // The 'and' is below the comparison instruction. 1685 return false; 1686 } 1687 1688 // Return false if no candidates exist. 1689 if (!MI && !Sub) 1690 return false; 1691 1692 // The single candidate is called MI. 1693 if (!MI) MI = Sub; 1694 1695 int NewOpC = -1; 1696 MIOpC = MI->getOpcode(); 1697 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) 1698 NewOpC = MIOpC; 1699 else { 1700 NewOpC = PPC::getRecordFormOpcode(MIOpC); 1701 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) 1702 NewOpC = MIOpC; 1703 } 1704 1705 // FIXME: On the non-embedded POWER architectures, only some of the record 1706 // forms are fast, and we should use only the fast ones. 1707 1708 // The defining instruction has a record form (or is already a record 1709 // form). It is possible, however, that we'll need to reverse the condition 1710 // code of the users. 1711 if (NewOpC == -1) 1712 return false; 1713 1714 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; 1715 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; 1716 1717 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP 1718 // needs to be updated to be based on SUB. Push the condition code 1719 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the 1720 // condition code of these operands will be modified. 1721 bool ShouldSwap = false; 1722 if (Sub) { 1723 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 1724 Sub->getOperand(2).getReg() == SrcReg; 1725 1726 // The operands to subf are the opposite of sub, so only in the fixed-point 1727 // case, invert the order. 1728 ShouldSwap = !ShouldSwap; 1729 } 1730 1731 if (ShouldSwap) 1732 for (MachineRegisterInfo::use_instr_iterator 1733 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 1734 I != IE; ++I) { 1735 MachineInstr *UseMI = &*I; 1736 if (UseMI->getOpcode() == PPC::BCC) { 1737 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); 1738 assert((!equalityOnly || 1739 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) && 1740 "Invalid predicate for equality-only optimization"); 1741 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), 1742 PPC::getSwappedPredicate(Pred))); 1743 } else if (UseMI->getOpcode() == PPC::ISEL || 1744 UseMI->getOpcode() == PPC::ISEL8) { 1745 unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); 1746 assert((!equalityOnly || NewSubReg == PPC::sub_eq) && 1747 "Invalid CR bit for equality-only optimization"); 1748 1749 if (NewSubReg == PPC::sub_lt) 1750 NewSubReg = PPC::sub_gt; 1751 else if (NewSubReg == PPC::sub_gt) 1752 NewSubReg = PPC::sub_lt; 1753 1754 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), 1755 NewSubReg)); 1756 } else // We need to abort on a user we don't understand. 1757 return false; 1758 } 1759 1760 // Create a new virtual register to hold the value of the CR set by the 1761 // record-form instruction. If the instruction was not previously in 1762 // record form, then set the kill flag on the CR. 1763 CmpInstr->eraseFromParent(); 1764 1765 MachineBasicBlock::iterator MII = MI; 1766 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), 1767 get(TargetOpcode::COPY), CRReg) 1768 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); 1769 1770 // Even if CR0 register were dead before, it is alive now since the 1771 // instruction we just built uses it. 1772 MI->clearRegisterDeads(PPC::CR0); 1773 1774 if (MIOpC != NewOpC) { 1775 // We need to be careful here: we're replacing one instruction with 1776 // another, and we need to make sure that we get all of the right 1777 // implicit uses and defs. On the other hand, the caller may be holding 1778 // an iterator to this instruction, and so we can't delete it (this is 1779 // specifically the case if this is the instruction directly after the 1780 // compare). 1781 1782 const MCInstrDesc &NewDesc = get(NewOpC); 1783 MI->setDesc(NewDesc); 1784 1785 if (NewDesc.ImplicitDefs) 1786 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs(); 1787 *ImpDefs; ++ImpDefs) 1788 if (!MI->definesRegister(*ImpDefs)) 1789 MI->addOperand(*MI->getParent()->getParent(), 1790 MachineOperand::CreateReg(*ImpDefs, true, true)); 1791 if (NewDesc.ImplicitUses) 1792 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses(); 1793 *ImpUses; ++ImpUses) 1794 if (!MI->readsRegister(*ImpUses)) 1795 MI->addOperand(*MI->getParent()->getParent(), 1796 MachineOperand::CreateReg(*ImpUses, false, true)); 1797 } 1798 assert(MI->definesRegister(PPC::CR0) && 1799 "Record-form instruction does not define cr0?"); 1800 1801 // Modify the condition code of operands in OperandsToUpdate. 1802 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to 1803 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 1804 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) 1805 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); 1806 1807 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) 1808 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); 1809 1810 return true; 1811 } 1812 1813 /// GetInstSize - Return the number of bytes of code the specified 1814 /// instruction may be. This returns the maximum number of bytes. 1815 /// 1816 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { 1817 unsigned Opcode = MI->getOpcode(); 1818 1819 if (Opcode == PPC::INLINEASM) { 1820 const MachineFunction *MF = MI->getParent()->getParent(); 1821 const char *AsmStr = MI->getOperand(0).getSymbolName(); 1822 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 1823 } else if (Opcode == TargetOpcode::STACKMAP) { 1824 return MI->getOperand(1).getImm(); 1825 } else if (Opcode == TargetOpcode::PATCHPOINT) { 1826 PatchPointOpers Opers(MI); 1827 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm(); 1828 } else { 1829 const MCInstrDesc &Desc = get(Opcode); 1830 return Desc.getSize(); 1831 } 1832 } 1833 1834 std::pair<unsigned, unsigned> 1835 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 1836 const unsigned Mask = PPCII::MO_ACCESS_MASK; 1837 return std::make_pair(TF & Mask, TF & ~Mask); 1838 } 1839 1840 ArrayRef<std::pair<unsigned, const char *>> 1841 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 1842 using namespace PPCII; 1843 static const std::pair<unsigned, const char *> TargetFlags[] = { 1844 {MO_LO, "ppc-lo"}, 1845 {MO_HA, "ppc-ha"}, 1846 {MO_TPREL_LO, "ppc-tprel-lo"}, 1847 {MO_TPREL_HA, "ppc-tprel-ha"}, 1848 {MO_DTPREL_LO, "ppc-dtprel-lo"}, 1849 {MO_TLSLD_LO, "ppc-tlsld-lo"}, 1850 {MO_TOC_LO, "ppc-toc-lo"}, 1851 {MO_TLS, "ppc-tls"}}; 1852 return makeArrayRef(TargetFlags); 1853 } 1854 1855 ArrayRef<std::pair<unsigned, const char *>> 1856 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { 1857 using namespace PPCII; 1858 static const std::pair<unsigned, const char *> TargetFlags[] = { 1859 {MO_PLT_OR_STUB, "ppc-plt-or-stub"}, 1860 {MO_PIC_FLAG, "ppc-pic"}, 1861 {MO_NLP_FLAG, "ppc-nlp"}, 1862 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}}; 1863 return makeArrayRef(TargetFlags); 1864 } 1865 1866 bool PPCInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const { 1867 switch (MI->getOpcode()) { 1868 case TargetOpcode::LOAD_STACK_GUARD: { 1869 assert(Subtarget.isTargetLinux() && 1870 "Only Linux target is expected to contain LOAD_STACK_GUARD"); 1871 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; 1872 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2; 1873 MI->setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ)); 1874 MachineInstrBuilder(*MI->getParent()->getParent(), MI) 1875 .addImm(Offset) 1876 .addReg(Reg); 1877 return true; 1878 } 1879 } 1880 return false; 1881 } 1882