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