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