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