1 //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// 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 defines a pattern matching instruction selector for PowerPC, 11 // converting from a legalized dag to a PPC dag. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #define DEBUG_TYPE "ppc-codegen" 16 #include "PPC.h" 17 #include "PPCPredicates.h" 18 #include "PPCTargetMachine.h" 19 #include "PPCISelLowering.h" 20 #include "PPCHazardRecognizers.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineFunction.h" 23 #include "llvm/CodeGen/MachineFunctionAnalysis.h" 24 #include "llvm/CodeGen/MachineRegisterInfo.h" 25 #include "llvm/CodeGen/SelectionDAG.h" 26 #include "llvm/CodeGen/SelectionDAGISel.h" 27 #include "llvm/Target/TargetOptions.h" 28 #include "llvm/Constants.h" 29 #include "llvm/Function.h" 30 #include "llvm/GlobalValue.h" 31 #include "llvm/Intrinsics.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/MathExtras.h" 34 #include "llvm/Support/Compiler.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 using namespace llvm; 38 39 namespace { 40 //===--------------------------------------------------------------------===// 41 /// PPCDAGToDAGISel - PPC specific code to select PPC machine 42 /// instructions for SelectionDAG operations. 43 /// 44 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel { 45 PPCTargetMachine &TM; 46 PPCTargetLowering &PPCLowering; 47 const PPCSubtarget &PPCSubTarget; 48 unsigned GlobalBaseReg; 49 public: 50 explicit PPCDAGToDAGISel(PPCTargetMachine &tm) 51 : SelectionDAGISel(tm), TM(tm), 52 PPCLowering(*TM.getTargetLowering()), 53 PPCSubTarget(*TM.getSubtargetImpl()) {} 54 55 virtual bool runOnMachineFunction(MachineFunction &MF) { 56 // Make sure we re-emit a set of the global base reg if necessary 57 GlobalBaseReg = 0; 58 SelectionDAGISel::runOnMachineFunction(MF); 59 60 InsertVRSaveCode(MF); 61 return true; 62 } 63 64 /// getI32Imm - Return a target constant with the specified value, of type 65 /// i32. 66 inline SDValue getI32Imm(unsigned Imm) { 67 return CurDAG->getTargetConstant(Imm, MVT::i32); 68 } 69 70 /// getI64Imm - Return a target constant with the specified value, of type 71 /// i64. 72 inline SDValue getI64Imm(uint64_t Imm) { 73 return CurDAG->getTargetConstant(Imm, MVT::i64); 74 } 75 76 /// getSmallIPtrImm - Return a target constant of pointer type. 77 inline SDValue getSmallIPtrImm(unsigned Imm) { 78 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy()); 79 } 80 81 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s 82 /// with any number of 0s on either side. The 1s are allowed to wrap from 83 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 84 /// 0x0F0F0000 is not, since all 1s are not contiguous. 85 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME); 86 87 88 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a 89 /// rotate and mask opcode and mask operation. 90 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, 91 unsigned &SH, unsigned &MB, unsigned &ME); 92 93 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC 94 /// base register. Return the virtual register that holds this value. 95 SDNode *getGlobalBaseReg(); 96 97 // Select - Convert the specified operand from a target-independent to a 98 // target-specific node if it hasn't already been changed. 99 SDNode *Select(SDValue Op); 100 101 SDNode *SelectBitfieldInsert(SDNode *N); 102 103 /// SelectCC - Select a comparison of the specified values with the 104 /// specified condition code, returning the CR# of the expression. 105 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl); 106 107 /// SelectAddrImm - Returns true if the address N can be represented by 108 /// a base register plus a signed 16-bit displacement [r+imm]. 109 bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp, 110 SDValue &Base) { 111 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG); 112 } 113 114 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc 115 /// immediate field. Because preinc imms have already been validated, just 116 /// accept it. 117 bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const { 118 Out = N; 119 return true; 120 } 121 122 /// SelectAddrIdx - Given the specified addressed, check to see if it can be 123 /// represented as an indexed [r+r] operation. Returns false if it can 124 /// be represented by [r+imm], which are preferred. 125 bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base, 126 SDValue &Index) { 127 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG); 128 } 129 130 /// SelectAddrIdxOnly - Given the specified addressed, force it to be 131 /// represented as an indexed [r+r] operation. 132 bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base, 133 SDValue &Index) { 134 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG); 135 } 136 137 /// SelectAddrImmShift - Returns true if the address N can be represented by 138 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable 139 /// for use by STD and friends. 140 bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp, 141 SDValue &Base) { 142 return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG); 143 } 144 145 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for 146 /// inline asm expressions. 147 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, 148 char ConstraintCode, 149 std::vector<SDValue> &OutOps) { 150 SDValue Op0, Op1; 151 switch (ConstraintCode) { 152 default: return true; 153 case 'm': // memory 154 if (!SelectAddrIdx(Op, Op, Op0, Op1)) 155 SelectAddrImm(Op, Op, Op0, Op1); 156 break; 157 case 'o': // offsetable 158 if (!SelectAddrImm(Op, Op, Op0, Op1)) { 159 Op0 = Op; 160 Op1 = getSmallIPtrImm(0); 161 } 162 break; 163 case 'v': // not offsetable 164 SelectAddrIdxOnly(Op, Op, Op0, Op1); 165 break; 166 } 167 168 OutOps.push_back(Op0); 169 OutOps.push_back(Op1); 170 return false; 171 } 172 173 SDValue BuildSDIVSequence(SDNode *N); 174 SDValue BuildUDIVSequence(SDNode *N); 175 176 /// InstructionSelect - This callback is invoked by 177 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 178 virtual void InstructionSelect(); 179 180 void InsertVRSaveCode(MachineFunction &MF); 181 182 virtual const char *getPassName() const { 183 return "PowerPC DAG->DAG Pattern Instruction Selection"; 184 } 185 186 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for 187 /// this target when scheduling the DAG. 188 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() { 189 // Should use subtarget info to pick the right hazard recognizer. For 190 // now, always return a PPC970 recognizer. 191 const TargetInstrInfo *II = TM.getInstrInfo(); 192 assert(II && "No InstrInfo?"); 193 return new PPCHazardRecognizer970(*II); 194 } 195 196 // Include the pieces autogenerated from the target description. 197 #include "PPCGenDAGISel.inc" 198 199 private: 200 SDNode *SelectSETCC(SDValue Op); 201 }; 202 } 203 204 /// InstructionSelect - This callback is invoked by 205 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 206 void PPCDAGToDAGISel::InstructionSelect() { 207 DEBUG(BB->dump()); 208 209 // Select target instructions for the DAG. 210 SelectRoot(*CurDAG); 211 CurDAG->RemoveDeadNodes(); 212 } 213 214 /// InsertVRSaveCode - Once the entire function has been instruction selected, 215 /// all virtual registers are created and all machine instructions are built, 216 /// check to see if we need to save/restore VRSAVE. If so, do it. 217 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) { 218 // Check to see if this function uses vector registers, which means we have to 219 // save and restore the VRSAVE register and update it with the regs we use. 220 // 221 // In this case, there will be virtual registers of vector type type created 222 // by the scheduler. Detect them now. 223 bool HasVectorVReg = false; 224 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, 225 e = RegInfo->getLastVirtReg()+1; i != e; ++i) 226 if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) { 227 HasVectorVReg = true; 228 break; 229 } 230 if (!HasVectorVReg) return; // nothing to do. 231 232 // If we have a vector register, we want to emit code into the entry and exit 233 // blocks to save and restore the VRSAVE register. We do this here (instead 234 // of marking all vector instructions as clobbering VRSAVE) for two reasons: 235 // 236 // 1. This (trivially) reduces the load on the register allocator, by not 237 // having to represent the live range of the VRSAVE register. 238 // 2. This (more significantly) allows us to create a temporary virtual 239 // register to hold the saved VRSAVE value, allowing this temporary to be 240 // register allocated, instead of forcing it to be spilled to the stack. 241 242 // Create two vregs - one to hold the VRSAVE register that is live-in to the 243 // function and one for the value after having bits or'd into it. 244 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 245 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 246 247 const TargetInstrInfo &TII = *TM.getInstrInfo(); 248 MachineBasicBlock &EntryBB = *Fn.begin(); 249 DebugLoc dl = DebugLoc::getUnknownLoc(); 250 // Emit the following code into the entry block: 251 // InVRSAVE = MFVRSAVE 252 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE 253 // MTVRSAVE UpdatedVRSAVE 254 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point 255 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE); 256 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE), 257 UpdatedVRSAVE).addReg(InVRSAVE); 258 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); 259 260 // Find all return blocks, outputting a restore in each epilog. 261 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { 262 if (!BB->empty() && BB->back().getDesc().isReturn()) { 263 IP = BB->end(); --IP; 264 265 // Skip over all terminator instructions, which are part of the return 266 // sequence. 267 MachineBasicBlock::iterator I2 = IP; 268 while (I2 != BB->begin() && (--I2)->getDesc().isTerminator()) 269 IP = I2; 270 271 // Emit: MTVRSAVE InVRSave 272 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); 273 } 274 } 275 } 276 277 278 /// getGlobalBaseReg - Output the instructions required to put the 279 /// base address to use for accessing globals into a register. 280 /// 281 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { 282 if (!GlobalBaseReg) { 283 const TargetInstrInfo &TII = *TM.getInstrInfo(); 284 // Insert the set of GlobalBaseReg into the first MBB of the function 285 MachineBasicBlock &FirstMBB = BB->getParent()->front(); 286 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 287 DebugLoc dl = DebugLoc::getUnknownLoc(); 288 289 if (PPCLowering.getPointerTy() == MVT::i32) { 290 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass); 291 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR), PPC::LR); 292 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); 293 } else { 294 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass); 295 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8), PPC::LR8); 296 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); 297 } 298 } 299 return CurDAG->getRegister(GlobalBaseReg, 300 PPCLowering.getPointerTy()).getNode(); 301 } 302 303 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit 304 /// or 64-bit immediate, and if the value can be accurately represented as a 305 /// sign extension from a 16-bit value. If so, this returns true and the 306 /// immediate. 307 static bool isIntS16Immediate(SDNode *N, short &Imm) { 308 if (N->getOpcode() != ISD::Constant) 309 return false; 310 311 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); 312 if (N->getValueType(0) == MVT::i32) 313 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); 314 else 315 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); 316 } 317 318 static bool isIntS16Immediate(SDValue Op, short &Imm) { 319 return isIntS16Immediate(Op.getNode(), Imm); 320 } 321 322 323 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant 324 /// operand. If so Imm will receive the 32-bit value. 325 static bool isInt32Immediate(SDNode *N, unsigned &Imm) { 326 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { 327 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 328 return true; 329 } 330 return false; 331 } 332 333 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant 334 /// operand. If so Imm will receive the 64-bit value. 335 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { 336 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { 337 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 338 return true; 339 } 340 return false; 341 } 342 343 // isInt32Immediate - This method tests to see if a constant operand. 344 // If so Imm will receive the 32 bit value. 345 static bool isInt32Immediate(SDValue N, unsigned &Imm) { 346 return isInt32Immediate(N.getNode(), Imm); 347 } 348 349 350 // isOpcWithIntImmediate - This method tests to see if the node is a specific 351 // opcode and that it has a immediate integer right operand. 352 // If so Imm will receive the 32 bit value. 353 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { 354 return N->getOpcode() == Opc 355 && isInt32Immediate(N->getOperand(1).getNode(), Imm); 356 } 357 358 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { 359 if (isShiftedMask_32(Val)) { 360 // look for the first non-zero bit 361 MB = CountLeadingZeros_32(Val); 362 // look for the first zero bit after the run of ones 363 ME = CountLeadingZeros_32((Val - 1) ^ Val); 364 return true; 365 } else { 366 Val = ~Val; // invert mask 367 if (isShiftedMask_32(Val)) { 368 // effectively look for the first zero bit 369 ME = CountLeadingZeros_32(Val) - 1; 370 // effectively look for the first one bit after the run of zeros 371 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; 372 return true; 373 } 374 } 375 // no run present 376 return false; 377 } 378 379 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, 380 bool IsShiftMask, unsigned &SH, 381 unsigned &MB, unsigned &ME) { 382 // Don't even go down this path for i64, since different logic will be 383 // necessary for rldicl/rldicr/rldimi. 384 if (N->getValueType(0) != MVT::i32) 385 return false; 386 387 unsigned Shift = 32; 388 unsigned Indeterminant = ~0; // bit mask marking indeterminant results 389 unsigned Opcode = N->getOpcode(); 390 if (N->getNumOperands() != 2 || 391 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) 392 return false; 393 394 if (Opcode == ISD::SHL) { 395 // apply shift left to mask if it comes first 396 if (IsShiftMask) Mask = Mask << Shift; 397 // determine which bits are made indeterminant by shift 398 Indeterminant = ~(0xFFFFFFFFu << Shift); 399 } else if (Opcode == ISD::SRL) { 400 // apply shift right to mask if it comes first 401 if (IsShiftMask) Mask = Mask >> Shift; 402 // determine which bits are made indeterminant by shift 403 Indeterminant = ~(0xFFFFFFFFu >> Shift); 404 // adjust for the left rotate 405 Shift = 32 - Shift; 406 } else if (Opcode == ISD::ROTL) { 407 Indeterminant = 0; 408 } else { 409 return false; 410 } 411 412 // if the mask doesn't intersect any Indeterminant bits 413 if (Mask && !(Mask & Indeterminant)) { 414 SH = Shift & 31; 415 // make sure the mask is still a mask (wrap arounds may not be) 416 return isRunOfOnes(Mask, MB, ME); 417 } 418 return false; 419 } 420 421 /// SelectBitfieldInsert - turn an or of two masked values into 422 /// the rotate left word immediate then mask insert (rlwimi) instruction. 423 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) { 424 SDValue Op0 = N->getOperand(0); 425 SDValue Op1 = N->getOperand(1); 426 DebugLoc dl = N->getDebugLoc(); 427 428 APInt LKZ, LKO, RKZ, RKO; 429 CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO); 430 CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO); 431 432 unsigned TargetMask = LKZ.getZExtValue(); 433 unsigned InsertMask = RKZ.getZExtValue(); 434 435 if ((TargetMask | InsertMask) == 0xFFFFFFFF) { 436 unsigned Op0Opc = Op0.getOpcode(); 437 unsigned Op1Opc = Op1.getOpcode(); 438 unsigned Value, SH = 0; 439 TargetMask = ~TargetMask; 440 InsertMask = ~InsertMask; 441 442 // If the LHS has a foldable shift and the RHS does not, then swap it to the 443 // RHS so that we can fold the shift into the insert. 444 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { 445 if (Op0.getOperand(0).getOpcode() == ISD::SHL || 446 Op0.getOperand(0).getOpcode() == ISD::SRL) { 447 if (Op1.getOperand(0).getOpcode() != ISD::SHL && 448 Op1.getOperand(0).getOpcode() != ISD::SRL) { 449 std::swap(Op0, Op1); 450 std::swap(Op0Opc, Op1Opc); 451 std::swap(TargetMask, InsertMask); 452 } 453 } 454 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) { 455 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL && 456 Op1.getOperand(0).getOpcode() != ISD::SRL) { 457 std::swap(Op0, Op1); 458 std::swap(Op0Opc, Op1Opc); 459 std::swap(TargetMask, InsertMask); 460 } 461 } 462 463 unsigned MB, ME; 464 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) { 465 SDValue Tmp1, Tmp2, Tmp3; 466 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF; 467 468 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && 469 isInt32Immediate(Op1.getOperand(1), Value)) { 470 Op1 = Op1.getOperand(0); 471 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; 472 } 473 if (Op1Opc == ISD::AND) { 474 unsigned SHOpc = Op1.getOperand(0).getOpcode(); 475 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && 476 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { 477 Op1 = Op1.getOperand(0).getOperand(0); 478 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; 479 } else { 480 Op1 = Op1.getOperand(0); 481 } 482 } 483 484 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0; 485 SH &= 31; 486 SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB), 487 getI32Imm(ME) }; 488 return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5); 489 } 490 } 491 return 0; 492 } 493 494 /// SelectCC - Select a comparison of the specified values with the specified 495 /// condition code, returning the CR# of the expression. 496 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, 497 ISD::CondCode CC, DebugLoc dl) { 498 // Always select the LHS. 499 unsigned Opc; 500 501 if (LHS.getValueType() == MVT::i32) { 502 unsigned Imm; 503 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 504 if (isInt32Immediate(RHS, Imm)) { 505 // SETEQ/SETNE comparison with 16-bit immediate, fold it. 506 if (isUInt16(Imm)) 507 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS, 508 getI32Imm(Imm & 0xFFFF)), 0); 509 // If this is a 16-bit signed immediate, fold it. 510 if (isInt16((int)Imm)) 511 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS, 512 getI32Imm(Imm & 0xFFFF)), 0); 513 514 // For non-equality comparisons, the default code would materialize the 515 // constant, then compare against it, like this: 516 // lis r2, 4660 517 // ori r2, r2, 22136 518 // cmpw cr0, r3, r2 519 // Since we are just comparing for equality, we can emit this instead: 520 // xoris r0,r3,0x1234 521 // cmplwi cr0,r0,0x5678 522 // beq cr0,L6 523 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, dl, MVT::i32, LHS, 524 getI32Imm(Imm >> 16)), 0); 525 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, Xor, 526 getI32Imm(Imm & 0xFFFF)), 0); 527 } 528 Opc = PPC::CMPLW; 529 } else if (ISD::isUnsignedIntSetCC(CC)) { 530 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm)) 531 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS, 532 getI32Imm(Imm & 0xFFFF)), 0); 533 Opc = PPC::CMPLW; 534 } else { 535 short SImm; 536 if (isIntS16Immediate(RHS, SImm)) 537 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS, 538 getI32Imm((int)SImm & 0xFFFF)), 539 0); 540 Opc = PPC::CMPW; 541 } 542 } else if (LHS.getValueType() == MVT::i64) { 543 uint64_t Imm; 544 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 545 if (isInt64Immediate(RHS.getNode(), Imm)) { 546 // SETEQ/SETNE comparison with 16-bit immediate, fold it. 547 if (isUInt16(Imm)) 548 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS, 549 getI32Imm(Imm & 0xFFFF)), 0); 550 // If this is a 16-bit signed immediate, fold it. 551 if (isInt16(Imm)) 552 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS, 553 getI32Imm(Imm & 0xFFFF)), 0); 554 555 // For non-equality comparisons, the default code would materialize the 556 // constant, then compare against it, like this: 557 // lis r2, 4660 558 // ori r2, r2, 22136 559 // cmpd cr0, r3, r2 560 // Since we are just comparing for equality, we can emit this instead: 561 // xoris r0,r3,0x1234 562 // cmpldi cr0,r0,0x5678 563 // beq cr0,L6 564 if (isUInt32(Imm)) { 565 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, dl, MVT::i64, LHS, 566 getI64Imm(Imm >> 16)), 0); 567 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, Xor, 568 getI64Imm(Imm & 0xFFFF)), 0); 569 } 570 } 571 Opc = PPC::CMPLD; 572 } else if (ISD::isUnsignedIntSetCC(CC)) { 573 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm)) 574 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS, 575 getI64Imm(Imm & 0xFFFF)), 0); 576 Opc = PPC::CMPLD; 577 } else { 578 short SImm; 579 if (isIntS16Immediate(RHS, SImm)) 580 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS, 581 getI64Imm(SImm & 0xFFFF)), 582 0); 583 Opc = PPC::CMPD; 584 } 585 } else if (LHS.getValueType() == MVT::f32) { 586 Opc = PPC::FCMPUS; 587 } else { 588 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!"); 589 Opc = PPC::FCMPUD; 590 } 591 return SDValue(CurDAG->getTargetNode(Opc, dl, MVT::i32, LHS, RHS), 0); 592 } 593 594 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { 595 switch (CC) { 596 case ISD::SETUEQ: 597 case ISD::SETONE: 598 case ISD::SETOLE: 599 case ISD::SETOGE: 600 llvm_unreachable("Should be lowered by legalize!"); 601 default: llvm_unreachable("Unknown condition!"); 602 case ISD::SETOEQ: 603 case ISD::SETEQ: return PPC::PRED_EQ; 604 case ISD::SETUNE: 605 case ISD::SETNE: return PPC::PRED_NE; 606 case ISD::SETOLT: 607 case ISD::SETLT: return PPC::PRED_LT; 608 case ISD::SETULE: 609 case ISD::SETLE: return PPC::PRED_LE; 610 case ISD::SETOGT: 611 case ISD::SETGT: return PPC::PRED_GT; 612 case ISD::SETUGE: 613 case ISD::SETGE: return PPC::PRED_GE; 614 case ISD::SETO: return PPC::PRED_NU; 615 case ISD::SETUO: return PPC::PRED_UN; 616 // These two are invalid for floating point. Assume we have int. 617 case ISD::SETULT: return PPC::PRED_LT; 618 case ISD::SETUGT: return PPC::PRED_GT; 619 } 620 } 621 622 /// getCRIdxForSetCC - Return the index of the condition register field 623 /// associated with the SetCC condition, and whether or not the field is 624 /// treated as inverted. That is, lt = 0; ge = 0 inverted. 625 /// 626 /// If this returns with Other != -1, then the returned comparison is an or of 627 /// two simpler comparisons. In this case, Invert is guaranteed to be false. 628 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) { 629 Invert = false; 630 Other = -1; 631 switch (CC) { 632 default: llvm_unreachable("Unknown condition!"); 633 case ISD::SETOLT: 634 case ISD::SETLT: return 0; // Bit #0 = SETOLT 635 case ISD::SETOGT: 636 case ISD::SETGT: return 1; // Bit #1 = SETOGT 637 case ISD::SETOEQ: 638 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ 639 case ISD::SETUO: return 3; // Bit #3 = SETUO 640 case ISD::SETUGE: 641 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE 642 case ISD::SETULE: 643 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE 644 case ISD::SETUNE: 645 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE 646 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO 647 case ISD::SETUEQ: 648 case ISD::SETOGE: 649 case ISD::SETOLE: 650 case ISD::SETONE: 651 llvm_unreachable("Invalid branch code: should be expanded by legalize"); 652 // These are invalid for floating point. Assume integer. 653 case ISD::SETULT: return 0; 654 case ISD::SETUGT: return 1; 655 } 656 return 0; 657 } 658 659 SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) { 660 SDNode *N = Op.getNode(); 661 DebugLoc dl = N->getDebugLoc(); 662 unsigned Imm; 663 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 664 if (isInt32Immediate(N->getOperand(1), Imm)) { 665 // We can codegen setcc op, imm very efficiently compared to a brcond. 666 // Check for those cases here. 667 // setcc op, 0 668 if (Imm == 0) { 669 SDValue Op = N->getOperand(0); 670 switch (CC) { 671 default: break; 672 case ISD::SETEQ: { 673 Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); 674 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) }; 675 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 676 } 677 case ISD::SETNE: { 678 SDValue AD = 679 SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, 680 Op, getI32Imm(~0U)), 0); 681 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, 682 AD.getValue(1)); 683 } 684 case ISD::SETLT: { 685 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 686 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 687 } 688 case ISD::SETGT: { 689 SDValue T = 690 SDValue(CurDAG->getTargetNode(PPC::NEG, dl, MVT::i32, Op), 0); 691 T = SDValue(CurDAG->getTargetNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); 692 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 693 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 694 } 695 } 696 } else if (Imm == ~0U) { // setcc op, -1 697 SDValue Op = N->getOperand(0); 698 switch (CC) { 699 default: break; 700 case ISD::SETEQ: 701 Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, 702 Op, getI32Imm(1)), 0); 703 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 704 SDValue(CurDAG->getTargetNode(PPC::LI, dl, 705 MVT::i32, 706 getI32Imm(0)), 0), 707 Op.getValue(1)); 708 case ISD::SETNE: { 709 Op = SDValue(CurDAG->getTargetNode(PPC::NOR, dl, MVT::i32, Op, Op), 0); 710 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, 711 Op, getI32Imm(~0U)); 712 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), 713 Op, SDValue(AD, 1)); 714 } 715 case ISD::SETLT: { 716 SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, dl, MVT::i32, Op, 717 getI32Imm(1)), 0); 718 SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, dl, MVT::i32, AD, 719 Op), 0); 720 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 721 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 722 } 723 case ISD::SETGT: { 724 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 725 Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 726 0); 727 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, 728 getI32Imm(1)); 729 } 730 } 731 } 732 } 733 734 bool Inv; 735 int OtherCondIdx; 736 unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx); 737 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); 738 SDValue IntCR; 739 740 // Force the ccreg into CR7. 741 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); 742 743 SDValue InFlag(0, 0); // Null incoming flag value. 744 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, 745 InFlag).getValue(1); 746 747 if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1) 748 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, 749 CCReg), 0); 750 else 751 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, CCReg), 0); 752 753 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31), 754 getI32Imm(31), getI32Imm(31) }; 755 if (OtherCondIdx == -1 && !Inv) 756 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 757 758 // Get the specified bit. 759 SDValue Tmp = 760 SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0); 761 if (Inv) { 762 assert(OtherCondIdx == -1 && "Can't have split plus negation"); 763 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); 764 } 765 766 // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT. 767 // We already got the bit for the first part of the comparison (e.g. SETULE). 768 769 // Get the other bit of the comparison. 770 Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31); 771 SDValue OtherCond = 772 SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0); 773 774 return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond); 775 } 776 777 778 // Select - Convert the specified operand from a target-independent to a 779 // target-specific node if it hasn't already been changed. 780 SDNode *PPCDAGToDAGISel::Select(SDValue Op) { 781 SDNode *N = Op.getNode(); 782 DebugLoc dl = Op.getDebugLoc(); 783 if (N->isMachineOpcode()) 784 return NULL; // Already selected. 785 786 switch (N->getOpcode()) { 787 default: break; 788 789 case ISD::Constant: { 790 if (N->getValueType(0) == MVT::i64) { 791 // Get 64 bit value. 792 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); 793 // Assume no remaining bits. 794 unsigned Remainder = 0; 795 // Assume no shift required. 796 unsigned Shift = 0; 797 798 // If it can't be represented as a 32 bit value. 799 if (!isInt32(Imm)) { 800 Shift = CountTrailingZeros_64(Imm); 801 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; 802 803 // If the shifted value fits 32 bits. 804 if (isInt32(ImmSh)) { 805 // Go with the shifted value. 806 Imm = ImmSh; 807 } else { 808 // Still stuck with a 64 bit value. 809 Remainder = Imm; 810 Shift = 32; 811 Imm >>= 32; 812 } 813 } 814 815 // Intermediate operand. 816 SDNode *Result; 817 818 // Handle first 32 bits. 819 unsigned Lo = Imm & 0xFFFF; 820 unsigned Hi = (Imm >> 16) & 0xFFFF; 821 822 // Simple value. 823 if (isInt16(Imm)) { 824 // Just the Lo bits. 825 Result = CurDAG->getTargetNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo)); 826 } else if (Lo) { 827 // Handle the Hi bits. 828 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; 829 Result = CurDAG->getTargetNode(OpC, dl, MVT::i64, getI32Imm(Hi)); 830 // And Lo bits. 831 Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64, 832 SDValue(Result, 0), getI32Imm(Lo)); 833 } else { 834 // Just the Hi bits. 835 Result = CurDAG->getTargetNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi)); 836 } 837 838 // If no shift, we're done. 839 if (!Shift) return Result; 840 841 // Shift for next step if the upper 32-bits were not zero. 842 if (Imm) { 843 Result = CurDAG->getTargetNode(PPC::RLDICR, dl, MVT::i64, 844 SDValue(Result, 0), 845 getI32Imm(Shift), getI32Imm(63 - Shift)); 846 } 847 848 // Add in the last bits as required. 849 if ((Hi = (Remainder >> 16) & 0xFFFF)) { 850 Result = CurDAG->getTargetNode(PPC::ORIS8, dl, MVT::i64, 851 SDValue(Result, 0), getI32Imm(Hi)); 852 } 853 if ((Lo = Remainder & 0xFFFF)) { 854 Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64, 855 SDValue(Result, 0), getI32Imm(Lo)); 856 } 857 858 return Result; 859 } 860 break; 861 } 862 863 case ISD::SETCC: 864 return SelectSETCC(Op); 865 case PPCISD::GlobalBaseReg: 866 return getGlobalBaseReg(); 867 868 case ISD::FrameIndex: { 869 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 870 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType()); 871 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8; 872 if (N->hasOneUse()) 873 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI, 874 getSmallIPtrImm(0)); 875 return CurDAG->getTargetNode(Opc, dl, Op.getValueType(), TFI, 876 getSmallIPtrImm(0)); 877 } 878 879 case PPCISD::MFCR: { 880 SDValue InFlag = N->getOperand(1); 881 // Use MFOCRF if supported. 882 if (PPCSubTarget.isGigaProcessor()) 883 return CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, 884 N->getOperand(0), InFlag); 885 else 886 return CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, InFlag); 887 } 888 889 case ISD::SDIV: { 890 // FIXME: since this depends on the setting of the carry flag from the srawi 891 // we should really be making notes about that for the scheduler. 892 // FIXME: It sure would be nice if we could cheaply recognize the 893 // srl/add/sra pattern the dag combiner will generate for this as 894 // sra/addze rather than having to handle sdiv ourselves. oh well. 895 unsigned Imm; 896 if (isInt32Immediate(N->getOperand(1), Imm)) { 897 SDValue N0 = N->getOperand(0); 898 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { 899 SDNode *Op = 900 CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag, 901 N0, getI32Imm(Log2_32(Imm))); 902 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 903 SDValue(Op, 0), SDValue(Op, 1)); 904 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { 905 SDNode *Op = 906 CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag, 907 N0, getI32Imm(Log2_32(-Imm))); 908 SDValue PT = 909 SDValue(CurDAG->getTargetNode(PPC::ADDZE, dl, MVT::i32, 910 SDValue(Op, 0), SDValue(Op, 1)), 911 0); 912 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); 913 } 914 } 915 916 // Other cases are autogenerated. 917 break; 918 } 919 920 case ISD::LOAD: { 921 // Handle preincrement loads. 922 LoadSDNode *LD = cast<LoadSDNode>(Op); 923 MVT LoadedVT = LD->getMemoryVT(); 924 925 // Normal loads are handled by code generated from the .td file. 926 if (LD->getAddressingMode() != ISD::PRE_INC) 927 break; 928 929 SDValue Offset = LD->getOffset(); 930 if (isa<ConstantSDNode>(Offset) || 931 Offset.getOpcode() == ISD::TargetGlobalAddress) { 932 933 unsigned Opcode; 934 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; 935 if (LD->getValueType(0) != MVT::i64) { 936 // Handle PPC32 integer and normal FP loads. 937 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); 938 switch (LoadedVT.getSimpleVT()) { 939 default: llvm_unreachable("Invalid PPC load type!"); 940 case MVT::f64: Opcode = PPC::LFDU; break; 941 case MVT::f32: Opcode = PPC::LFSU; break; 942 case MVT::i32: Opcode = PPC::LWZU; break; 943 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break; 944 case MVT::i1: 945 case MVT::i8: Opcode = PPC::LBZU; break; 946 } 947 } else { 948 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); 949 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); 950 switch (LoadedVT.getSimpleVT()) { 951 default: llvm_unreachable("Invalid PPC load type!"); 952 case MVT::i64: Opcode = PPC::LDU; break; 953 case MVT::i32: Opcode = PPC::LWZU8; break; 954 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break; 955 case MVT::i1: 956 case MVT::i8: Opcode = PPC::LBZU8; break; 957 } 958 } 959 960 SDValue Chain = LD->getChain(); 961 SDValue Base = LD->getBasePtr(); 962 SDValue Ops[] = { Offset, Base, Chain }; 963 // FIXME: PPC64 964 return CurDAG->getTargetNode(Opcode, dl, LD->getValueType(0), 965 PPCLowering.getPointerTy(), 966 MVT::Other, Ops, 3); 967 } else { 968 llvm_unreachable("R+R preindex loads not supported yet!"); 969 } 970 } 971 972 case ISD::AND: { 973 unsigned Imm, Imm2, SH, MB, ME; 974 975 // If this is an and of a value rotated between 0 and 31 bits and then and'd 976 // with a mask, emit rlwinm 977 if (isInt32Immediate(N->getOperand(1), Imm) && 978 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) { 979 SDValue Val = N->getOperand(0).getOperand(0); 980 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; 981 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 982 } 983 // If this is just a masked value where the input is not handled above, and 984 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm 985 if (isInt32Immediate(N->getOperand(1), Imm) && 986 isRunOfOnes(Imm, MB, ME) && 987 N->getOperand(0).getOpcode() != ISD::ROTL) { 988 SDValue Val = N->getOperand(0); 989 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) }; 990 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 991 } 992 // AND X, 0 -> 0, not "rlwinm 32". 993 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) { 994 ReplaceUses(SDValue(N, 0), N->getOperand(1)); 995 return NULL; 996 } 997 // ISD::OR doesn't get all the bitfield insertion fun. 998 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert 999 if (isInt32Immediate(N->getOperand(1), Imm) && 1000 N->getOperand(0).getOpcode() == ISD::OR && 1001 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) { 1002 unsigned MB, ME; 1003 Imm = ~(Imm^Imm2); 1004 if (isRunOfOnes(Imm, MB, ME)) { 1005 SDValue Ops[] = { N->getOperand(0).getOperand(0), 1006 N->getOperand(0).getOperand(1), 1007 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) }; 1008 return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5); 1009 } 1010 } 1011 1012 // Other cases are autogenerated. 1013 break; 1014 } 1015 case ISD::OR: 1016 if (N->getValueType(0) == MVT::i32) 1017 if (SDNode *I = SelectBitfieldInsert(N)) 1018 return I; 1019 1020 // Other cases are autogenerated. 1021 break; 1022 case ISD::SHL: { 1023 unsigned Imm, SH, MB, ME; 1024 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && 1025 isRotateAndMask(N, Imm, true, SH, MB, ME)) { 1026 SDValue Ops[] = { N->getOperand(0).getOperand(0), 1027 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; 1028 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 1029 } 1030 1031 // Other cases are autogenerated. 1032 break; 1033 } 1034 case ISD::SRL: { 1035 unsigned Imm, SH, MB, ME; 1036 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && 1037 isRotateAndMask(N, Imm, true, SH, MB, ME)) { 1038 SDValue Ops[] = { N->getOperand(0).getOperand(0), 1039 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; 1040 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 1041 } 1042 1043 // Other cases are autogenerated. 1044 break; 1045 } 1046 case ISD::SELECT_CC: { 1047 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 1048 1049 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc 1050 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) 1051 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) 1052 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) 1053 if (N1C->isNullValue() && N3C->isNullValue() && 1054 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE && 1055 // FIXME: Implement this optzn for PPC64. 1056 N->getValueType(0) == MVT::i32) { 1057 SDNode *Tmp = 1058 CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, 1059 N->getOperand(0), getI32Imm(~0U)); 1060 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, 1061 SDValue(Tmp, 0), N->getOperand(0), 1062 SDValue(Tmp, 1)); 1063 } 1064 1065 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); 1066 unsigned BROpc = getPredicateForSetCC(CC); 1067 1068 unsigned SelectCCOp; 1069 if (N->getValueType(0) == MVT::i32) 1070 SelectCCOp = PPC::SELECT_CC_I4; 1071 else if (N->getValueType(0) == MVT::i64) 1072 SelectCCOp = PPC::SELECT_CC_I8; 1073 else if (N->getValueType(0) == MVT::f32) 1074 SelectCCOp = PPC::SELECT_CC_F4; 1075 else if (N->getValueType(0) == MVT::f64) 1076 SelectCCOp = PPC::SELECT_CC_F8; 1077 else 1078 SelectCCOp = PPC::SELECT_CC_VRRC; 1079 1080 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), 1081 getI32Imm(BROpc) }; 1082 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4); 1083 } 1084 case PPCISD::COND_BRANCH: { 1085 // Op #0 is the Chain. 1086 // Op #1 is the PPC::PRED_* number. 1087 // Op #2 is the CR# 1088 // Op #3 is the Dest MBB 1089 // Op #4 is the Flag. 1090 // Prevent PPC::PRED_* from being selected into LI. 1091 SDValue Pred = 1092 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()); 1093 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), 1094 N->getOperand(0), N->getOperand(4) }; 1095 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5); 1096 } 1097 case ISD::BR_CC: { 1098 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); 1099 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); 1100 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode, 1101 N->getOperand(4), N->getOperand(0) }; 1102 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4); 1103 } 1104 case ISD::BRIND: { 1105 // FIXME: Should custom lower this. 1106 SDValue Chain = N->getOperand(0); 1107 SDValue Target = N->getOperand(1); 1108 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; 1109 Chain = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Target, 1110 Chain), 0); 1111 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain); 1112 } 1113 case ISD::DECLARE: { 1114 SDValue Chain = N->getOperand(0); 1115 SDValue N1 = N->getOperand(1); 1116 SDValue N2 = N->getOperand(2); 1117 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1); 1118 1119 // FIXME: We need to handle this for VLAs. 1120 if (!FINode) { 1121 ReplaceUses(Op.getValue(0), Chain); 1122 return NULL; 1123 } 1124 1125 if (N2.getOpcode() == ISD::ADD) { 1126 if (N2.getOperand(0).getOpcode() == ISD::ADD && 1127 N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg && 1128 N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Hi && 1129 N2.getOperand(1).getOpcode() == PPCISD::Lo) 1130 N2 = N2.getOperand(0).getOperand(1).getOperand(0); 1131 else if (N2.getOperand(0).getOpcode() == ISD::ADD && 1132 N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg && 1133 N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Lo && 1134 N2.getOperand(1).getOpcode() == PPCISD::Hi) 1135 N2 = N2.getOperand(0).getOperand(1).getOperand(0); 1136 else if (N2.getOperand(0).getOpcode() == PPCISD::Hi && 1137 N2.getOperand(1).getOpcode() == PPCISD::Lo) 1138 N2 = N2.getOperand(0).getOperand(0); 1139 } 1140 1141 // If we don't have a global address here, the debug info is mangled, just 1142 // drop it. 1143 if (!isa<GlobalAddressSDNode>(N2)) { 1144 ReplaceUses(Op.getValue(0), Chain); 1145 return NULL; 1146 } 1147 int FI = cast<FrameIndexSDNode>(N1)->getIndex(); 1148 GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal(); 1149 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy()); 1150 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy()); 1151 return CurDAG->SelectNodeTo(N, TargetInstrInfo::DECLARE, 1152 MVT::Other, Tmp1, Tmp2, Chain); 1153 } 1154 } 1155 1156 return SelectCode(Op); 1157 } 1158 1159 1160 1161 /// createPPCISelDag - This pass converts a legalized DAG into a 1162 /// PowerPC-specific DAG, ready for instruction scheduling. 1163 /// 1164 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) { 1165 return new PPCDAGToDAGISel(TM); 1166 } 1167 1168