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