LLVM API Documentation
00001 //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a pattern matching instruction selector for PowerPC, 00011 // converting from a legalized dag to a PPC dag. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "ppc-codegen" 00016 #include "PPC.h" 00017 #include "PPCPredicates.h" 00018 #include "PPCTargetMachine.h" 00019 #include "PPCISelLowering.h" 00020 #include "PPCHazardRecognizers.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/CodeGen/MachineFunction.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/CodeGen/SelectionDAG.h" 00025 #include "llvm/CodeGen/SelectionDAGISel.h" 00026 #include "llvm/Target/TargetOptions.h" 00027 #include "llvm/Constants.h" 00028 #include "llvm/GlobalValue.h" 00029 #include "llvm/Intrinsics.h" 00030 #include "llvm/Support/Debug.h" 00031 #include "llvm/Support/MathExtras.h" 00032 #include "llvm/Support/Compiler.h" 00033 using namespace llvm; 00034 00035 namespace { 00036 //===--------------------------------------------------------------------===// 00037 /// PPCDAGToDAGISel - PPC specific code to select PPC machine 00038 /// instructions for SelectionDAG operations. 00039 /// 00040 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel { 00041 PPCTargetMachine &TM; 00042 PPCTargetLowering &PPCLowering; 00043 const PPCSubtarget &PPCSubTarget; 00044 unsigned GlobalBaseReg; 00045 public: 00046 explicit PPCDAGToDAGISel(PPCTargetMachine &tm) 00047 : SelectionDAGISel(*tm.getTargetLowering()), TM(tm), 00048 PPCLowering(*TM.getTargetLowering()), 00049 PPCSubTarget(*TM.getSubtargetImpl()) {} 00050 00051 virtual bool runOnFunction(Function &Fn) { 00052 // Make sure we re-emit a set of the global base reg if necessary 00053 GlobalBaseReg = 0; 00054 SelectionDAGISel::runOnFunction(Fn); 00055 00056 InsertVRSaveCode(Fn); 00057 return true; 00058 } 00059 00060 /// getI32Imm - Return a target constant with the specified value, of type 00061 /// i32. 00062 inline SDValue getI32Imm(unsigned Imm) { 00063 return CurDAG->getTargetConstant(Imm, MVT::i32); 00064 } 00065 00066 /// getI64Imm - Return a target constant with the specified value, of type 00067 /// i64. 00068 inline SDValue getI64Imm(uint64_t Imm) { 00069 return CurDAG->getTargetConstant(Imm, MVT::i64); 00070 } 00071 00072 /// getSmallIPtrImm - Return a target constant of pointer type. 00073 inline SDValue getSmallIPtrImm(unsigned Imm) { 00074 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy()); 00075 } 00076 00077 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s 00078 /// with any number of 0s on either side. The 1s are allowed to wrap from 00079 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 00080 /// 0x0F0F0000 is not, since all 1s are not contiguous. 00081 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME); 00082 00083 00084 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a 00085 /// rotate and mask opcode and mask operation. 00086 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, 00087 unsigned &SH, unsigned &MB, unsigned &ME); 00088 00089 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC 00090 /// base register. Return the virtual register that holds this value. 00091 SDNode *getGlobalBaseReg(); 00092 00093 // Select - Convert the specified operand from a target-independent to a 00094 // target-specific node if it hasn't already been changed. 00095 SDNode *Select(SDValue Op); 00096 00097 SDNode *SelectBitfieldInsert(SDNode *N); 00098 00099 /// SelectCC - Select a comparison of the specified values with the 00100 /// specified condition code, returning the CR# of the expression. 00101 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC); 00102 00103 /// SelectAddrImm - Returns true if the address N can be represented by 00104 /// a base register plus a signed 16-bit displacement [r+imm]. 00105 bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp, 00106 SDValue &Base) { 00107 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG); 00108 } 00109 00110 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc 00111 /// immediate field. Because preinc imms have already been validated, just 00112 /// accept it. 00113 bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const { 00114 Out = N; 00115 return true; 00116 } 00117 00118 /// SelectAddrIdx - Given the specified addressed, check to see if it can be 00119 /// represented as an indexed [r+r] operation. Returns false if it can 00120 /// be represented by [r+imm], which are preferred. 00121 bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base, 00122 SDValue &Index) { 00123 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG); 00124 } 00125 00126 /// SelectAddrIdxOnly - Given the specified addressed, force it to be 00127 /// represented as an indexed [r+r] operation. 00128 bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base, 00129 SDValue &Index) { 00130 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG); 00131 } 00132 00133 /// SelectAddrImmShift - Returns true if the address N can be represented by 00134 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable 00135 /// for use by STD and friends. 00136 bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp, 00137 SDValue &Base) { 00138 return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG); 00139 } 00140 00141 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for 00142 /// inline asm expressions. 00143 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, 00144 char ConstraintCode, 00145 std::vector<SDValue> &OutOps) { 00146 SDValue Op0, Op1; 00147 switch (ConstraintCode) { 00148 default: return true; 00149 case 'm': // memory 00150 if (!SelectAddrIdx(Op, Op, Op0, Op1)) 00151 SelectAddrImm(Op, Op, Op0, Op1); 00152 break; 00153 case 'o': // offsetable 00154 if (!SelectAddrImm(Op, Op, Op0, Op1)) { 00155 Op0 = Op; 00156 Op1 = getSmallIPtrImm(0); 00157 } 00158 break; 00159 case 'v': // not offsetable 00160 SelectAddrIdxOnly(Op, Op, Op0, Op1); 00161 break; 00162 } 00163 00164 OutOps.push_back(Op0); 00165 OutOps.push_back(Op1); 00166 return false; 00167 } 00168 00169 SDValue BuildSDIVSequence(SDNode *N); 00170 SDValue BuildUDIVSequence(SDNode *N); 00171 00172 /// InstructionSelect - This callback is invoked by 00173 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 00174 virtual void InstructionSelect(); 00175 00176 void InsertVRSaveCode(Function &Fn); 00177 00178 virtual const char *getPassName() const { 00179 return "PowerPC DAG->DAG Pattern Instruction Selection"; 00180 } 00181 00182 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for 00183 /// this target when scheduling the DAG. 00184 virtual HazardRecognizer *CreateTargetHazardRecognizer() { 00185 // Should use subtarget info to pick the right hazard recognizer. For 00186 // now, always return a PPC970 recognizer. 00187 const TargetInstrInfo *II = TM.getInstrInfo(); 00188 assert(II && "No InstrInfo?"); 00189 return new PPCHazardRecognizer970(*II); 00190 } 00191 00192 // Include the pieces autogenerated from the target description. 00193 #include "PPCGenDAGISel.inc" 00194 00195 private: 00196 SDNode *SelectSETCC(SDValue Op); 00197 }; 00198 } 00199 00200 /// InstructionSelect - This callback is invoked by 00201 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 00202 void PPCDAGToDAGISel::InstructionSelect() { 00203 DEBUG(BB->dump()); 00204 00205 // Select target instructions for the DAG. 00206 SelectRoot(*CurDAG); 00207 CurDAG->RemoveDeadNodes(); 00208 } 00209 00210 /// InsertVRSaveCode - Once the entire function has been instruction selected, 00211 /// all virtual registers are created and all machine instructions are built, 00212 /// check to see if we need to save/restore VRSAVE. If so, do it. 00213 void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) { 00214 // Check to see if this function uses vector registers, which means we have to 00215 // save and restore the VRSAVE register and update it with the regs we use. 00216 // 00217 // In this case, there will be virtual registers of vector type type created 00218 // by the scheduler. Detect them now. 00219 MachineFunction &Fn = MachineFunction::get(&F); 00220 bool HasVectorVReg = false; 00221 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, 00222 e = RegInfo->getLastVirtReg()+1; i != e; ++i) 00223 if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) { 00224 HasVectorVReg = true; 00225 break; 00226 } 00227 if (!HasVectorVReg) return; // nothing to do. 00228 00229 // If we have a vector register, we want to emit code into the entry and exit 00230 // blocks to save and restore the VRSAVE register. We do this here (instead 00231 // of marking all vector instructions as clobbering VRSAVE) for two reasons: 00232 // 00233 // 1. This (trivially) reduces the load on the register allocator, by not 00234 // having to represent the live range of the VRSAVE register. 00235 // 2. This (more significantly) allows us to create a temporary virtual 00236 // register to hold the saved VRSAVE value, allowing this temporary to be 00237 // register allocated, instead of forcing it to be spilled to the stack. 00238 00239 // Create two vregs - one to hold the VRSAVE register that is live-in to the 00240 // function and one for the value after having bits or'd into it. 00241 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 00242 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 00243 00244 const TargetInstrInfo &TII = *TM.getInstrInfo(); 00245 MachineBasicBlock &EntryBB = *Fn.begin(); 00246 // Emit the following code into the entry block: 00247 // InVRSAVE = MFVRSAVE 00248 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE 00249 // MTVRSAVE UpdatedVRSAVE 00250 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point 00251 BuildMI(EntryBB, IP, TII.get(PPC::MFVRSAVE), InVRSAVE); 00252 BuildMI(EntryBB, IP, TII.get(PPC::UPDATE_VRSAVE), 00253 UpdatedVRSAVE).addReg(InVRSAVE); 00254 BuildMI(EntryBB, IP, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); 00255 00256 // Find all return blocks, outputting a restore in each epilog. 00257 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { 00258 if (!BB->empty() && BB->back().getDesc().isReturn()) { 00259 IP = BB->end(); --IP; 00260 00261 // Skip over all terminator instructions, which are part of the return 00262 // sequence. 00263 MachineBasicBlock::iterator I2 = IP; 00264 while (I2 != BB->begin() && (--I2)->getDesc().isTerminator()) 00265 IP = I2; 00266 00267 // Emit: MTVRSAVE InVRSave 00268 BuildMI(*BB, IP, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); 00269 } 00270 } 00271 } 00272 00273 00274 /// getGlobalBaseReg - Output the instructions required to put the 00275 /// base address to use for accessing globals into a register. 00276 /// 00277 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { 00278 if (!GlobalBaseReg) { 00279 const TargetInstrInfo &TII = *TM.getInstrInfo(); 00280 // Insert the set of GlobalBaseReg into the first MBB of the function 00281 MachineBasicBlock &FirstMBB = BB->getParent()->front(); 00282 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 00283 00284 if (PPCLowering.getPointerTy() == MVT::i32) { 00285 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass); 00286 BuildMI(FirstMBB, MBBI, TII.get(PPC::MovePCtoLR), PPC::LR); 00287 BuildMI(FirstMBB, MBBI, TII.get(PPC::MFLR), GlobalBaseReg); 00288 } else { 00289 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass); 00290 BuildMI(FirstMBB, MBBI, TII.get(PPC::MovePCtoLR8), PPC::LR8); 00291 BuildMI(FirstMBB, MBBI, TII.get(PPC::MFLR8), GlobalBaseReg); 00292 } 00293 } 00294 return CurDAG->getRegister(GlobalBaseReg, 00295 PPCLowering.getPointerTy()).getNode(); 00296 } 00297 00298 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit 00299 /// or 64-bit immediate, and if the value can be accurately represented as a 00300 /// sign extension from a 16-bit value. If so, this returns true and the 00301 /// immediate. 00302 static bool isIntS16Immediate(SDNode *N, short &Imm) { 00303 if (N->getOpcode() != ISD::Constant) 00304 return false; 00305 00306 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); 00307 if (N->getValueType(0) == MVT::i32) 00308 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); 00309 else 00310 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); 00311 } 00312 00313 static bool isIntS16Immediate(SDValue Op, short &Imm) { 00314 return isIntS16Immediate(Op.getNode(), Imm); 00315 } 00316 00317 00318 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant 00319 /// operand. If so Imm will receive the 32-bit value. 00320 static bool isInt32Immediate(SDNode *N, unsigned &Imm) { 00321 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { 00322 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 00323 return true; 00324 } 00325 return false; 00326 } 00327 00328 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant 00329 /// operand. If so Imm will receive the 64-bit value. 00330 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { 00331 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { 00332 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 00333 return true; 00334 } 00335 return false; 00336 } 00337 00338 // isInt32Immediate - This method tests to see if a constant operand. 00339 // If so Imm will receive the 32 bit value. 00340 static bool isInt32Immediate(SDValue N, unsigned &Imm) { 00341 return isInt32Immediate(N.getNode(), Imm); 00342 } 00343 00344 00345 // isOpcWithIntImmediate - This method tests to see if the node is a specific 00346 // opcode and that it has a immediate integer right operand. 00347 // If so Imm will receive the 32 bit value. 00348 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { 00349 return N->getOpcode() == Opc 00350 && isInt32Immediate(N->getOperand(1).getNode(), Imm); 00351 } 00352 00353 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { 00354 if (isShiftedMask_32(Val)) { 00355 // look for the first non-zero bit 00356 MB = CountLeadingZeros_32(Val); 00357 // look for the first zero bit after the run of ones 00358 ME = CountLeadingZeros_32((Val - 1) ^ Val); 00359 return true; 00360 } else { 00361 Val = ~Val; // invert mask 00362 if (isShiftedMask_32(Val)) { 00363 // effectively look for the first zero bit 00364 ME = CountLeadingZeros_32(Val) - 1; 00365 // effectively look for the first one bit after the run of zeros 00366 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; 00367 return true; 00368 } 00369 } 00370 // no run present 00371 return false; 00372 } 00373 00374 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, 00375 bool IsShiftMask, unsigned &SH, 00376 unsigned &MB, unsigned &ME) { 00377 // Don't even go down this path for i64, since different logic will be 00378 // necessary for rldicl/rldicr/rldimi. 00379 if (N->getValueType(0) != MVT::i32) 00380 return false; 00381 00382 unsigned Shift = 32; 00383 unsigned Indeterminant = ~0; // bit mask marking indeterminant results 00384 unsigned Opcode = N->getOpcode(); 00385 if (N->getNumOperands() != 2 || 00386 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) 00387 return false; 00388 00389 if (Opcode == ISD::SHL) { 00390 // apply shift left to mask if it comes first 00391 if (IsShiftMask) Mask = Mask << Shift; 00392 // determine which bits are made indeterminant by shift 00393 Indeterminant = ~(0xFFFFFFFFu << Shift); 00394 } else if (Opcode == ISD::SRL) { 00395 // apply shift right to mask if it comes first 00396 if (IsShiftMask) Mask = Mask >> Shift; 00397 // determine which bits are made indeterminant by shift 00398 Indeterminant = ~(0xFFFFFFFFu >> Shift); 00399 // adjust for the left rotate 00400 Shift = 32 - Shift; 00401 } else if (Opcode == ISD::ROTL) { 00402 Indeterminant = 0; 00403 } else { 00404 return false; 00405 } 00406 00407 // if the mask doesn't intersect any Indeterminant bits 00408 if (Mask && !(Mask & Indeterminant)) { 00409 SH = Shift & 31; 00410 // make sure the mask is still a mask (wrap arounds may not be) 00411 return isRunOfOnes(Mask, MB, ME); 00412 } 00413 return false; 00414 } 00415 00416 /// SelectBitfieldInsert - turn an or of two masked values into 00417 /// the rotate left word immediate then mask insert (rlwimi) instruction. 00418 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) { 00419 SDValue Op0 = N->getOperand(0); 00420 SDValue Op1 = N->getOperand(1); 00421 00422 APInt LKZ, LKO, RKZ, RKO; 00423 CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO); 00424 CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO); 00425 00426 unsigned TargetMask = LKZ.getZExtValue(); 00427 unsigned InsertMask = RKZ.getZExtValue(); 00428 00429 if ((TargetMask | InsertMask) == 0xFFFFFFFF) { 00430 unsigned Op0Opc = Op0.getOpcode(); 00431 unsigned Op1Opc = Op1.getOpcode(); 00432 unsigned Value, SH = 0; 00433 TargetMask = ~TargetMask; 00434 InsertMask = ~InsertMask; 00435 00436 // If the LHS has a foldable shift and the RHS does not, then swap it to the 00437 // RHS so that we can fold the shift into the insert. 00438 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { 00439 if (Op0.getOperand(0).getOpcode() == ISD::SHL || 00440 Op0.getOperand(0).getOpcode() == ISD::SRL) { 00441 if (Op1.getOperand(0).getOpcode() != ISD::SHL && 00442 Op1.getOperand(0).getOpcode() != ISD::SRL) { 00443 std::swap(Op0, Op1); 00444 std::swap(Op0Opc, Op1Opc); 00445 std::swap(TargetMask, InsertMask); 00446 } 00447 } 00448 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) { 00449 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL && 00450 Op1.getOperand(0).getOpcode() != ISD::SRL) { 00451 std::swap(Op0, Op1); 00452 std::swap(Op0Opc, Op1Opc); 00453 std::swap(TargetMask, InsertMask); 00454 } 00455 } 00456 00457 unsigned MB, ME; 00458 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) { 00459 SDValue Tmp1, Tmp2, Tmp3; 00460 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF; 00461 00462 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && 00463 isInt32Immediate(Op1.getOperand(1), Value)) { 00464 Op1 = Op1.getOperand(0); 00465 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; 00466 } 00467 if (Op1Opc == ISD::AND) { 00468 unsigned SHOpc = Op1.getOperand(0).getOpcode(); 00469 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && 00470 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { 00471 Op1 = Op1.getOperand(0).getOperand(0); 00472 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; 00473 } else { 00474 Op1 = Op1.getOperand(0); 00475 } 00476 } 00477 00478 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0; 00479 SH &= 31; 00480 SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB), 00481 getI32Imm(ME) }; 00482 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5); 00483 } 00484 } 00485 return 0; 00486 } 00487 00488 /// SelectCC - Select a comparison of the specified values with the specified 00489 /// condition code, returning the CR# of the expression. 00490 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, 00491 ISD::CondCode CC) { 00492 // Always select the LHS. 00493 unsigned Opc; 00494 00495 if (LHS.getValueType() == MVT::i32) { 00496 unsigned Imm; 00497 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 00498 if (isInt32Immediate(RHS, Imm)) { 00499 // SETEQ/SETNE comparison with 16-bit immediate, fold it. 00500 if (isUInt16(Imm)) 00501 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS, 00502 getI32Imm(Imm & 0xFFFF)), 0); 00503 // If this is a 16-bit signed immediate, fold it. 00504 if (isInt16((int)Imm)) 00505 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS, 00506 getI32Imm(Imm & 0xFFFF)), 0); 00507 00508 // For non-equality comparisons, the default code would materialize the 00509 // constant, then compare against it, like this: 00510 // lis r2, 4660 00511 // ori r2, r2, 22136 00512 // cmpw cr0, r3, r2 00513 // Since we are just comparing for equality, we can emit this instead: 00514 // xoris r0,r3,0x1234 00515 // cmplwi cr0,r0,0x5678 00516 // beq cr0,L6 00517 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, MVT::i32, LHS, 00518 getI32Imm(Imm >> 16)), 0); 00519 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, Xor, 00520 getI32Imm(Imm & 0xFFFF)), 0); 00521 } 00522 Opc = PPC::CMPLW; 00523 } else if (ISD::isUnsignedIntSetCC(CC)) { 00524 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm)) 00525 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS, 00526 getI32Imm(Imm & 0xFFFF)), 0); 00527 Opc = PPC::CMPLW; 00528 } else { 00529 short SImm; 00530 if (isIntS16Immediate(RHS, SImm)) 00531 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS, 00532 getI32Imm((int)SImm & 0xFFFF)), 00533 0); 00534 Opc = PPC::CMPW; 00535 } 00536 } else if (LHS.getValueType() == MVT::i64) { 00537 uint64_t Imm; 00538 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 00539 if (isInt64Immediate(RHS.getNode(), Imm)) { 00540 // SETEQ/SETNE comparison with 16-bit immediate, fold it. 00541 if (isUInt16(Imm)) 00542 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS, 00543 getI32Imm(Imm & 0xFFFF)), 0); 00544 // If this is a 16-bit signed immediate, fold it. 00545 if (isInt16(Imm)) 00546 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS, 00547 getI32Imm(Imm & 0xFFFF)), 0); 00548 00549 // For non-equality comparisons, the default code would materialize the 00550 // constant, then compare against it, like this: 00551 // lis r2, 4660 00552 // ori r2, r2, 22136 00553 // cmpd cr0, r3, r2 00554 // Since we are just comparing for equality, we can emit this instead: 00555 // xoris r0,r3,0x1234 00556 // cmpldi cr0,r0,0x5678 00557 // beq cr0,L6 00558 if (isUInt32(Imm)) { 00559 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, MVT::i64, LHS, 00560 getI64Imm(Imm >> 16)), 0); 00561 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, Xor, 00562 getI64Imm(Imm & 0xFFFF)), 0); 00563 } 00564 } 00565 Opc = PPC::CMPLD; 00566 } else if (ISD::isUnsignedIntSetCC(CC)) { 00567 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm)) 00568 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS, 00569 getI64Imm(Imm & 0xFFFF)), 0); 00570 Opc = PPC::CMPLD; 00571 } else { 00572 short SImm; 00573 if (isIntS16Immediate(RHS, SImm)) 00574 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS, 00575 getI64Imm(SImm & 0xFFFF)), 00576 0); 00577 Opc = PPC::CMPD; 00578 } 00579 } else if (LHS.getValueType() == MVT::f32) { 00580 Opc = PPC::FCMPUS; 00581 } else { 00582 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!"); 00583 Opc = PPC::FCMPUD; 00584 } 00585 return SDValue(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0); 00586 } 00587 00588 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { 00589 switch (CC) { 00590 case ISD::SETUEQ: 00591 case ISD::SETONE: 00592 case ISD::SETOLE: 00593 case ISD::SETOGE: 00594 assert(0 && "Should be lowered by legalize!"); 00595 default: assert(0 && "Unknown condition!"); abort(); 00596 case ISD::SETOEQ: 00597 case ISD::SETEQ: return PPC::PRED_EQ; 00598 case ISD::SETUNE: 00599 case ISD::SETNE: return PPC::PRED_NE; 00600 case ISD::SETOLT: 00601 case ISD::SETLT: return PPC::PRED_LT; 00602 case ISD::SETULE: 00603 case ISD::SETLE: return PPC::PRED_LE; 00604 case ISD::SETOGT: 00605 case ISD::SETGT: return PPC::PRED_GT; 00606 case ISD::SETUGE: 00607 case ISD::SETGE: return PPC::PRED_GE; 00608 case ISD::SETO: return PPC::PRED_NU; 00609 case ISD::SETUO: return PPC::PRED_UN; 00610 // These two are invalid for floating point. Assume we have int. 00611 case ISD::SETULT: return PPC::PRED_LT; 00612 case ISD::SETUGT: return PPC::PRED_GT; 00613 } 00614 } 00615 00616 /// getCRIdxForSetCC - Return the index of the condition register field 00617 /// associated with the SetCC condition, and whether or not the field is 00618 /// treated as inverted. That is, lt = 0; ge = 0 inverted. 00619 /// 00620 /// If this returns with Other != -1, then the returned comparison is an or of 00621 /// two simpler comparisons. In this case, Invert is guaranteed to be false. 00622 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) { 00623 Invert = false; 00624 Other = -1; 00625 switch (CC) { 00626 default: assert(0 && "Unknown condition!"); abort(); 00627 case ISD::SETOLT: 00628 case ISD::SETLT: return 0; // Bit #0 = SETOLT 00629 case ISD::SETOGT: 00630 case ISD::SETGT: return 1; // Bit #1 = SETOGT 00631 case ISD::SETOEQ: 00632 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ 00633 case ISD::SETUO: return 3; // Bit #3 = SETUO 00634 case ISD::SETUGE: 00635 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE 00636 case ISD::SETULE: 00637 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE 00638 case ISD::SETUNE: 00639 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE 00640 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO 00641 case ISD::SETUEQ: 00642 case ISD::SETOGE: 00643 case ISD::SETOLE: 00644 case ISD::SETONE: 00645 assert(0 && "Invalid branch code: should be expanded by legalize"); 00646 // These are invalid for floating point. Assume integer. 00647 case ISD::SETULT: return 0; 00648 case ISD::SETUGT: return 1; 00649 } 00650 return 0; 00651 } 00652 00653 SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) { 00654 SDNode *N = Op.getNode(); 00655 unsigned Imm; 00656 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 00657 if (isInt32Immediate(N->getOperand(1), Imm)) { 00658 // We can codegen setcc op, imm very efficiently compared to a brcond. 00659 // Check for those cases here. 00660 // setcc op, 0 00661 if (Imm == 0) { 00662 SDValue Op = N->getOperand(0); 00663 switch (CC) { 00664 default: break; 00665 case ISD::SETEQ: { 00666 Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0); 00667 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) }; 00668 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 00669 } 00670 case ISD::SETNE: { 00671 SDValue AD = 00672 SDValue(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 00673 Op, getI32Imm(~0U)), 0); 00674 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, 00675 AD.getValue(1)); 00676 } 00677 case ISD::SETLT: { 00678 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 00679 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 00680 } 00681 case ISD::SETGT: { 00682 SDValue T = 00683 SDValue(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0); 00684 T = SDValue(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0); 00685 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 00686 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 00687 } 00688 } 00689 } else if (Imm == ~0U) { // setcc op, -1 00690 SDValue Op = N->getOperand(0); 00691 switch (CC) { 00692 default: break; 00693 case ISD::SETEQ: 00694 Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 00695 Op, getI32Imm(1)), 0); 00696 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 00697 SDValue(CurDAG->getTargetNode(PPC::LI, MVT::i32, 00698 getI32Imm(0)), 0), 00699 Op.getValue(1)); 00700 case ISD::SETNE: { 00701 Op = SDValue(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0); 00702 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 00703 Op, getI32Imm(~0U)); 00704 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), 00705 Op, SDValue(AD, 1)); 00706 } 00707 case ISD::SETLT: { 00708 SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op, 00709 getI32Imm(1)), 0); 00710 SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, 00711 Op), 0); 00712 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 00713 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 00714 } 00715 case ISD::SETGT: { 00716 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; 00717 Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0); 00718 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, 00719 getI32Imm(1)); 00720 } 00721 } 00722 } 00723 } 00724 00725 bool Inv; 00726 int OtherCondIdx; 00727 unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx); 00728 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); 00729 SDValue IntCR; 00730 00731 // Force the ccreg into CR7. 00732 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); 00733 00734 SDValue InFlag(0, 0); // Null incoming flag value. 00735 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg, 00736 InFlag).getValue(1); 00737 00738 if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1) 00739 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, 00740 CCReg), 0); 00741 else 00742 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0); 00743 00744 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31), 00745 getI32Imm(31), getI32Imm(31) }; 00746 if (OtherCondIdx == -1 && !Inv) 00747 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); 00748 00749 // Get the specified bit. 00750 SDValue Tmp = 00751 SDValue(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0); 00752 if (Inv) { 00753 assert(OtherCondIdx == -1 && "Can't have split plus negation"); 00754 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); 00755 } 00756 00757 // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT. 00758 // We already got the bit for the first part of the comparison (e.g. SETULE). 00759 00760 // Get the other bit of the comparison. 00761 Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31); 00762 SDValue OtherCond = 00763 SDValue(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0); 00764 00765 return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond); 00766 } 00767 00768 00769 // Select - Convert the specified operand from a target-independent to a 00770 // target-specific node if it hasn't already been changed. 00771 SDNode *PPCDAGToDAGISel::Select(SDValue Op) { 00772 SDNode *N = Op.getNode(); 00773 if (N->isMachineOpcode()) 00774 return NULL; // Already selected. 00775 00776 switch (N->getOpcode()) { 00777 default: break; 00778 00779 case ISD::Constant: { 00780 if (N->getValueType(0) == MVT::i64) { 00781 // Get 64 bit value. 00782 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); 00783 // Assume no remaining bits. 00784 unsigned Remainder = 0; 00785 // Assume no shift required. 00786 unsigned Shift = 0; 00787 00788 // If it can't be represented as a 32 bit value. 00789 if (!isInt32(Imm)) { 00790 Shift = CountTrailingZeros_64(Imm); 00791 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; 00792 00793 // If the shifted value fits 32 bits. 00794 if (isInt32(ImmSh)) { 00795 // Go with the shifted value. 00796 Imm = ImmSh; 00797 } else { 00798 // Still stuck with a 64 bit value. 00799 Remainder = Imm; 00800 Shift = 32; 00801 Imm >>= 32; 00802 } 00803 } 00804 00805 // Intermediate operand. 00806 SDNode *Result; 00807 00808 // Handle first 32 bits. 00809 unsigned Lo = Imm & 0xFFFF; 00810 unsigned Hi = (Imm >> 16) & 0xFFFF; 00811 00812 // Simple value. 00813 if (isInt16(Imm)) { 00814 // Just the Lo bits. 00815 Result = CurDAG->getTargetNode(PPC::LI8, MVT::i64, getI32Imm(Lo)); 00816 } else if (Lo) { 00817 // Handle the Hi bits. 00818 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; 00819 Result = CurDAG->getTargetNode(OpC, MVT::i64, getI32Imm(Hi)); 00820 // And Lo bits. 00821 Result = CurDAG->getTargetNode(PPC::ORI8, MVT::i64, 00822 SDValue(Result, 0), getI32Imm(Lo)); 00823 } else { 00824 // Just the Hi bits. 00825 Result = CurDAG->getTargetNode(PPC::LIS8, MVT::i64, getI32Imm(Hi)); 00826 } 00827 00828 // If no shift, we're done. 00829 if (!Shift) return Result; 00830 00831 // Shift for next step if the upper 32-bits were not zero. 00832 if (Imm) { 00833 Result = CurDAG->getTargetNode(PPC::RLDICR, MVT::i64, 00834 SDValue(Result, 0), 00835 getI32Imm(Shift), getI32Imm(63 - Shift)); 00836 } 00837 00838 // Add in the last bits as required. 00839 if ((Hi = (Remainder >> 16) & 0xFFFF)) { 00840 Result = CurDAG->getTargetNode(PPC::ORIS8, MVT::i64, 00841 SDValue(Result, 0), getI32Imm(Hi)); 00842 } 00843 if ((Lo = Remainder & 0xFFFF)) { 00844 Result = CurDAG->getTargetNode(PPC::ORI8, MVT::i64, 00845 SDValue(Result, 0), getI32Imm(Lo)); 00846 } 00847 00848 return Result; 00849 } 00850 break; 00851 } 00852 00853 case ISD::SETCC: 00854 return SelectSETCC(Op); 00855 case PPCISD::GlobalBaseReg: 00856 return getGlobalBaseReg(); 00857 00858 case ISD::FrameIndex: { 00859 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 00860 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType()); 00861 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8; 00862 if (N->hasOneUse()) 00863 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI, 00864 getSmallIPtrImm(0)); 00865 return CurDAG->getTargetNode(Opc, Op.getValueType(), TFI, 00866 getSmallIPtrImm(0)); 00867 } 00868 00869 case PPCISD::MFCR: { 00870 SDValue InFlag = N->getOperand(1); 00871 // Use MFOCRF if supported. 00872 if (PPCSubTarget.isGigaProcessor()) 00873 return CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, 00874 N->getOperand(0), InFlag); 00875 else 00876 return CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag); 00877 } 00878 00879 case ISD::SDIV: { 00880 // FIXME: since this depends on the setting of the carry flag from the srawi 00881 // we should really be making notes about that for the scheduler. 00882 // FIXME: It sure would be nice if we could cheaply recognize the 00883 // srl/add/sra pattern the dag combiner will generate for this as 00884 // sra/addze rather than having to handle sdiv ourselves. oh well. 00885 unsigned Imm; 00886 if (isInt32Immediate(N->getOperand(1), Imm)) { 00887 SDValue N0 = N->getOperand(0); 00888 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { 00889 SDNode *Op = 00890 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, 00891 N0, getI32Imm(Log2_32(Imm))); 00892 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 00893 SDValue(Op, 0), SDValue(Op, 1)); 00894 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { 00895 SDNode *Op = 00896 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, 00897 N0, getI32Imm(Log2_32(-Imm))); 00898 SDValue PT = 00899 SDValue(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, 00900 SDValue(Op, 0), SDValue(Op, 1)), 00901 0); 00902 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); 00903 } 00904 } 00905 00906 // Other cases are autogenerated. 00907 break; 00908 } 00909 00910 case ISD::LOAD: { 00911 // Handle preincrement loads. 00912 LoadSDNode *LD = cast<LoadSDNode>(Op); 00913 MVT LoadedVT = LD->getMemoryVT(); 00914 00915 // Normal loads are handled by code generated from the .td file. 00916 if (LD->getAddressingMode() != ISD::PRE_INC) 00917 break; 00918 00919