Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Dan Gohman | fdf9ee2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 27 | #include "llvm/Target/TargetOptions.h" |
| 28 | #include "llvm/Constants.h" |
Chris Lattner | 3ed055f | 2009-04-17 00:26:12 +0000 | [diff] [blame] | 29 | #include "llvm/Function.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Edwin Török | 4d9756a | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Dan Gohman | f2b2957 | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 46 | PPCTargetLowering &PPCLowering; |
Evan Cheng | 9d99c5e | 2007-10-23 06:42:42 +0000 | [diff] [blame] | 47 | const PPCSubtarget &PPCSubTarget; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | unsigned GlobalBaseReg; |
| 49 | public: |
Dan Gohman | e887fdf | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 50 | explicit PPCDAGToDAGISel(PPCTargetMachine &tm) |
Dan Gohman | 96eb47a | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 51 | : SelectionDAGISel(tm), TM(tm), |
Evan Cheng | 9d99c5e | 2007-10-23 06:42:42 +0000 | [diff] [blame] | 52 | PPCLowering(*TM.getTargetLowering()), |
| 53 | PPCSubTarget(*TM.getSubtargetImpl()) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | |
Dan Gohman | fdf9ee2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 55 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | // Make sure we re-emit a set of the global base reg if necessary |
| 57 | GlobalBaseReg = 0; |
Dan Gohman | fdf9ee2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 58 | SelectionDAGISel::runOnMachineFunction(MF); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | |
Dan Gohman | fdf9ee2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 60 | InsertVRSaveCode(MF); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /// getI32Imm - Return a target constant with the specified value, of type |
| 65 | /// i32. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 66 | inline SDValue getI32Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 67 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 68 | } |
| 69 | |
| 70 | /// getI64Imm - Return a target constant with the specified value, of type |
| 71 | /// i64. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 72 | inline SDValue getI64Imm(uint64_t Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | return CurDAG->getTargetConstant(Imm, MVT::i64); |
| 74 | } |
| 75 | |
| 76 | /// getSmallIPtrImm - Return a target constant of pointer type. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 77 | inline SDValue getSmallIPtrImm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 99 | SDNode *Select(SDValue Op); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 105 | SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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]. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 109 | bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp, |
| 110 | SDValue &Base) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 117 | bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 125 | bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base, |
| 126 | SDValue &Index) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 132 | bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base, |
| 133 | SDValue &Index) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 140 | bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp, |
| 141 | SDValue &Base) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG); |
| 143 | } |
| 144 | |
| 145 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 146 | /// inline asm expressions. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 147 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | char ConstraintCode, |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 149 | std::vector<SDValue> &OutOps) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 150 | SDValue Op0, Op1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 173 | SDValue BuildSDIVSequence(SDNode *N); |
| 174 | SDValue BuildUDIVSequence(SDNode *N); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 176 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 177 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 178 | virtual void InstructionSelect(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 179 | |
Dan Gohman | fdf9ee2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 180 | void InsertVRSaveCode(MachineFunction &MF); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | dd6547d | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 188 | virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | // Should use subtarget info to pick the right hazard recognizer. For |
| 190 | // now, always return a PPC970 recognizer. |
Dan Gohman | 404e854 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 191 | const TargetInstrInfo *II = TM.getInstrInfo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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: |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 200 | SDNode *SelectSETCC(SDValue Op); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | }; |
| 202 | } |
| 203 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 204 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 205 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 206 | void PPCDAGToDAGISel::InstructionSelect() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 207 | DEBUG(BB->dump()); |
| 208 | |
| 209 | // Select target instructions for the DAG. |
David Greene | 932618b | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 210 | SelectRoot(*CurDAG); |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 211 | CurDAG->RemoveDeadNodes(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | fdf9ee2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 217 | void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 223 | bool HasVectorVReg = false; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 224 | for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 225 | e = RegInfo->getLastVirtReg()+1; i != e; ++i) |
| 226 | if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 244 | unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
| 245 | unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | |
| 247 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 248 | MachineBasicBlock &EntryBB = *Fn.begin(); |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 249 | DebugLoc dl = DebugLoc::getUnknownLoc(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 255 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE); |
| 256 | BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE), |
Chris Lattner | 6232760 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 257 | UpdatedVRSAVE).addReg(InVRSAVE); |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 258 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 5b93037 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 262 | if (!BB->empty() && BB->back().getDesc().isReturn()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Chris Lattner | 5b93037 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 268 | while (I2 != BB->begin() && (--I2)->getDesc().isTerminator()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 269 | IP = I2; |
| 270 | |
| 271 | // Emit: MTVRSAVE InVRSave |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 272 | BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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(); |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 287 | DebugLoc dl = DebugLoc::getUnknownLoc(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 288 | |
| 289 | if (PPCLowering.getPointerTy() == MVT::i32) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 290 | GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass); |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 291 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR), PPC::LR); |
| 292 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 293 | } else { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 294 | GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass); |
Dale Johannesen | f1cac0f | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 295 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8), PPC::LR8); |
| 296 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 299 | return CurDAG->getRegister(GlobalBaseReg, |
| 300 | PPCLowering.getPointerTy()).getNode(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 311 | Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 312 | if (N->getValueType(0) == MVT::i32) |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 313 | return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 314 | else |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 315 | return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 318 | static bool isIntS16Immediate(SDValue Op, short &Imm) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 319 | return isIntS16Immediate(Op.getNode(), Imm); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 327 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 337 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 345 | static bool isInt32Immediate(SDValue N, unsigned &Imm) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 346 | return isInt32Immediate(N.getNode(), Imm); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 354 | return N->getOpcode() == Opc |
| 355 | && isInt32Immediate(N->getOperand(1).getNode(), Imm); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 || |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 391 | !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 424 | SDValue Op0 = N->getOperand(0); |
| 425 | SDValue Op1 = N->getOperand(1); |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 426 | DebugLoc dl = N->getDebugLoc(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 427 | |
Dan Gohman | 63f4e46 | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 428 | APInt LKZ, LKO, RKZ, RKO; |
| 429 | CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO); |
| 430 | CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 431 | |
Dan Gohman | 63f4e46 | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 432 | unsigned TargetMask = LKZ.getZExtValue(); |
| 433 | unsigned InsertMask = RKZ.getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 465 | SDValue Tmp1, Tmp2, Tmp3; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 485 | SH &= 31; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 486 | SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 487 | getI32Imm(ME) }; |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 488 | return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 496 | SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 497 | ISD::CondCode CC, DebugLoc dl) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 498 | // Always select the LHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 507 | return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 508 | getI32Imm(Imm & 0xFFFF)), 0); |
| 509 | // If this is a 16-bit signed immediate, fold it. |
| 510 | if (isInt16((int)Imm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 511 | return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 523 | SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, dl, MVT::i32, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 524 | getI32Imm(Imm >> 16)), 0); |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 525 | return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, Xor, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 526 | getI32Imm(Imm & 0xFFFF)), 0); |
| 527 | } |
| 528 | Opc = PPC::CMPLW; |
| 529 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
| 530 | if (isInt32Immediate(RHS, Imm) && isUInt16(Imm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 531 | return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 532 | getI32Imm(Imm & 0xFFFF)), 0); |
| 533 | Opc = PPC::CMPLW; |
| 534 | } else { |
| 535 | short SImm; |
| 536 | if (isIntS16Immediate(RHS, SImm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 537 | return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 545 | if (isInt64Immediate(RHS.getNode(), Imm)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 546 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
| 547 | if (isUInt16(Imm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 548 | return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 549 | getI32Imm(Imm & 0xFFFF)), 0); |
| 550 | // If this is a 16-bit signed immediate, fold it. |
| 551 | if (isInt16(Imm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 552 | return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)) { |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 565 | SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, dl, MVT::i64, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 566 | getI64Imm(Imm >> 16)), 0); |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 567 | return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, Xor, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 568 | getI64Imm(Imm & 0xFFFF)), 0); |
| 569 | } |
| 570 | } |
| 571 | Opc = PPC::CMPLD; |
| 572 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 573 | if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 574 | return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 575 | getI64Imm(Imm & 0xFFFF)), 0); |
| 576 | Opc = PPC::CMPLD; |
| 577 | } else { |
| 578 | short SImm; |
| 579 | if (isIntS16Immediate(RHS, SImm)) |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 580 | return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | } |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 591 | return SDValue(CurDAG->getTargetNode(Opc, dl, MVT::i32, LHS, RHS), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { |
| 595 | switch (CC) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 596 | case ISD::SETUEQ: |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 597 | case ISD::SETONE: |
| 598 | case ISD::SETOLE: |
| 599 | case ISD::SETOGE: |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 600 | llvm_unreachable("Should be lowered by legalize!"); |
| 601 | default: llvm_unreachable("Unknown condition!"); |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 602 | case ISD::SETOEQ: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 603 | case ISD::SETEQ: return PPC::PRED_EQ; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 604 | case ISD::SETUNE: |
| 605 | case ISD::SETNE: return PPC::PRED_NE; |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 606 | case ISD::SETOLT: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 607 | case ISD::SETLT: return PPC::PRED_LT; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 608 | case ISD::SETULE: |
| 609 | case ISD::SETLE: return PPC::PRED_LE; |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 610 | case ISD::SETOGT: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 611 | case ISD::SETGT: return PPC::PRED_GT; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 612 | case ISD::SETUGE: |
| 613 | case ISD::SETGE: return PPC::PRED_GE; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 614 | case ISD::SETO: return PPC::PRED_NU; |
| 615 | case ISD::SETUO: return PPC::PRED_UN; |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 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; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 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; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 631 | switch (CC) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 632 | default: llvm_unreachable("Unknown condition!"); |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 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 |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 640 | case ISD::SETUGE: |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 641 | case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 642 | case ISD::SETULE: |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 643 | case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 644 | case ISD::SETUNE: |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 645 | case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE |
| 646 | case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 647 | case ISD::SETUEQ: |
| 648 | case ISD::SETOGE: |
| 649 | case ISD::SETOLE: |
| 650 | case ISD::SETONE: |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 651 | llvm_unreachable("Invalid branch code: should be expanded by legalize"); |
Dale Johannesen | 32100b2 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 652 | // These are invalid for floating point. Assume integer. |
| 653 | case ISD::SETULT: return 0; |
| 654 | case ISD::SETUGT: return 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 655 | } |
| 656 | return 0; |
| 657 | } |
| 658 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 659 | SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 660 | SDNode *N = Op.getNode(); |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 661 | DebugLoc dl = N->getDebugLoc(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 669 | SDValue Op = N->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 670 | switch (CC) { |
| 671 | default: break; |
| 672 | case ISD::SETEQ: { |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 673 | Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 674 | SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 675 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
| 676 | } |
| 677 | case ISD::SETNE: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 678 | SDValue AD = |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 679 | SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 685 | SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 686 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
| 687 | } |
| 688 | case ISD::SETGT: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 689 | SDValue T = |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 690 | SDValue(CurDAG->getTargetNode(PPC::NEG, dl, MVT::i32, Op), 0); |
| 691 | T = SDValue(CurDAG->getTargetNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 692 | SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 693 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
| 694 | } |
| 695 | } |
| 696 | } else if (Imm == ~0U) { // setcc op, -1 |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 697 | SDValue Op = N->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 698 | switch (CC) { |
| 699 | default: break; |
| 700 | case ISD::SETEQ: |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 701 | Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 702 | Op, getI32Imm(1)), 0); |
| 703 | return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 704 | SDValue(CurDAG->getTargetNode(PPC::LI, dl, |
| 705 | MVT::i32, |
| 706 | getI32Imm(0)), 0), |
| 707 | Op.getValue(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 708 | case ISD::SETNE: { |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 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, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 711 | Op, getI32Imm(~0U)); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 712 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), |
| 713 | Op, SDValue(AD, 1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 714 | } |
| 715 | case ISD::SETLT: { |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 716 | SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, dl, MVT::i32, Op, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 717 | getI32Imm(1)), 0); |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 718 | SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, dl, MVT::i32, AD, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 719 | Op), 0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 720 | SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 721 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
| 722 | } |
| 723 | case ISD::SETGT: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 724 | SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 725 | Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), |
| 726 | 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 727 | return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, |
| 728 | getI32Imm(1)); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | bool Inv; |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 735 | int OtherCondIdx; |
| 736 | unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx); |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 737 | SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 738 | SDValue IntCR; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 739 | |
| 740 | // Force the ccreg into CR7. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 741 | SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 742 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 743 | SDValue InFlag(0, 0); // Null incoming flag value. |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 744 | CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 745 | InFlag).getValue(1); |
| 746 | |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 747 | if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1) |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 748 | IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 749 | CCReg), 0); |
| 750 | else |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 751 | IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, CCReg), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 752 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 753 | SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 754 | getI32Imm(31), getI32Imm(31) }; |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 755 | if (OtherCondIdx == -1 && !Inv) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 756 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 757 | |
| 758 | // Get the specified bit. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 759 | SDValue Tmp = |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 760 | SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0); |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 761 | if (Inv) { |
| 762 | assert(OtherCondIdx == -1 && "Can't have split plus negation"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 763 | return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); |
| 764 | } |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 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); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 771 | SDValue OtherCond = |
Dale Johannesen | b03cc3f | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 772 | SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0); |
Chris Lattner | 6c36fb5 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 773 | |
| 774 | return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 780 | SDNode *PPCDAGToDAGISel::Select(SDValue Op) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 781 | SDNode *N = Op.getNode(); |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 782 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | bd68c79 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 783 | if (N->isMachineOpcode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 792 | int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 825 | Result = CurDAG->getTargetNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 826 | } else if (Lo) { |
| 827 | // Handle the Hi bits. |
| 828 | unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 829 | Result = CurDAG->getTargetNode(OpC, dl, MVT::i64, getI32Imm(Hi)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 830 | // And Lo bits. |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 831 | Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 832 | SDValue(Result, 0), getI32Imm(Lo)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 833 | } else { |
| 834 | // Just the Hi bits. |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 835 | Result = CurDAG->getTargetNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 843 | Result = CurDAG->getTargetNode(PPC::RLDICR, dl, MVT::i64, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 844 | SDValue(Result, 0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 845 | getI32Imm(Shift), getI32Imm(63 - Shift)); |
| 846 | } |
| 847 | |
| 848 | // Add in the last bits as required. |
| 849 | if ((Hi = (Remainder >> 16) & 0xFFFF)) { |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 850 | Result = CurDAG->getTargetNode(PPC::ORIS8, dl, MVT::i64, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 851 | SDValue(Result, 0), getI32Imm(Hi)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 852 | } |
| 853 | if ((Lo = Remainder & 0xFFFF)) { |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 854 | Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 855 | SDValue(Result, 0), getI32Imm(Lo)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 870 | SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)); |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 875 | return CurDAG->getTargetNode(Opc, dl, Op.getValueType(), TFI, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 876 | getSmallIPtrImm(0)); |
| 877 | } |
| 878 | |
| 879 | case PPCISD::MFCR: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 880 | SDValue InFlag = N->getOperand(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 881 | // Use MFOCRF if supported. |
Evan Cheng | 9d99c5e | 2007-10-23 06:42:42 +0000 | [diff] [blame] | 882 | if (PPCSubTarget.isGigaProcessor()) |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 883 | return CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 884 | N->getOperand(0), InFlag); |
| 885 | else |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 886 | return CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, InFlag); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 897 | SDValue N0 = N->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 898 | if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { |
| 899 | SDNode *Op = |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 900 | CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 901 | N0, getI32Imm(Log2_32(Imm))); |
| 902 | return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 903 | SDValue(Op, 0), SDValue(Op, 1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 904 | } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { |
| 905 | SDNode *Op = |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 906 | CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 907 | N0, getI32Imm(Log2_32(-Imm))); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 908 | SDValue PT = |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 909 | SDValue(CurDAG->getTargetNode(PPC::ADDZE, dl, MVT::i32, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 910 | SDValue(Op, 0), SDValue(Op, 1)), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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); |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 923 | MVT LoadedVT = LD->getMemoryVT(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 924 | |
| 925 | // Normal loads are handled by code generated from the .td file. |
| 926 | if (LD->getAddressingMode() != ISD::PRE_INC) |
| 927 | break; |
| 928 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 929 | SDValue Offset = LD->getOffset(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 937 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 938 | switch (LoadedVT.getSimpleVT()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 939 | default: llvm_unreachable("Invalid PPC load type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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!"); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 949 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 950 | switch (LoadedVT.getSimpleVT()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 951 | default: llvm_unreachable("Invalid PPC load type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 960 | SDValue Chain = LD->getChain(); |
| 961 | SDValue Base = LD->getBasePtr(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 962 | SDValue Ops[] = { Offset, Base, Chain }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 963 | // FIXME: PPC64 |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 964 | return CurDAG->getTargetNode(Opcode, dl, LD->getValueType(0), |
Dan Gohman | bd68c79 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 965 | PPCLowering.getPointerTy(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 966 | MVT::Other, Ops, 3); |
| 967 | } else { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 968 | llvm_unreachable("R+R preindex loads not supported yet!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 978 | isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 979 | SDValue Val = N->getOperand(0).getOperand(0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 980 | SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 988 | SDValue Val = N->getOperand(0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 989 | SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 994 | ReplaceUses(SDValue(N, 0), N->getOperand(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1005 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1006 | N->getOperand(0).getOperand(1), |
| 1007 | getI32Imm(0), getI32Imm(MB),getI32Imm(ME) }; |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 1008 | return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1024 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1025 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1026 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1036 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1037 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1038 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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() && |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1054 | N2C->getZExtValue() == 1ULL && CC == ISD::SETNE && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1055 | // FIXME: Implement this optzn for PPC64. |
| 1056 | N->getValueType(0) == MVT::i32) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1057 | SDNode *Tmp = |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 1058 | CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1059 | N->getOperand(0), getI32Imm(~0U)); |
| 1060 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1061 | SDValue(Tmp, 0), N->getOperand(0), |
| 1062 | SDValue(Tmp, 1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1065 | SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1080 | SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1081 | getI32Imm(BROpc) }; |
| 1082 | return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4); |
| 1083 | } |
| 1084 | case PPCISD::COND_BRANCH: { |
Dan Gohman | a1fb67a | 2008-11-05 17:16:24 +0000 | [diff] [blame] | 1085 | // Op #0 is the Chain. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1086 | // Op #1 is the PPC::PRED_* number. |
| 1087 | // Op #2 is the CR# |
| 1088 | // Op #3 is the Dest MBB |
Dan Gohman | cc3df85 | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1089 | // Op #4 is the Flag. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1090 | // Prevent PPC::PRED_* from being selected into LI. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1091 | SDValue Pred = |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1092 | getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1093 | SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1094 | N->getOperand(0), N->getOperand(4) }; |
| 1095 | return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5); |
| 1096 | } |
| 1097 | case ISD::BR_CC: { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1098 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
Dale Johannesen | 5d398a3 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1099 | SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1100 | SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1106 | SDValue Chain = N->getOperand(0); |
| 1107 | SDValue Target = N->getOperand(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1108 | unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; |
Dale Johannesen | 913ba76 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 1109 | Chain = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Target, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1110 | Chain), 0); |
| 1111 | return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain); |
| 1112 | } |
Evan Cheng | b6facc4 | 2009-01-16 22:57:32 +0000 | [diff] [blame] | 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); |
Chris Lattner | ebf925e | 2009-02-12 17:37:15 +0000 | [diff] [blame] | 1118 | |
| 1119 | // FIXME: We need to handle this for VLAs. |
| 1120 | if (!FINode) { |
| 1121 | ReplaceUses(Op.getValue(0), Chain); |
| 1122 | return NULL; |
| 1123 | } |
| 1124 | |
Evan Cheng | 27cec74 | 2009-01-19 18:31:51 +0000 | [diff] [blame] | 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 && |
Evan Cheng | a7482db | 2009-01-19 18:57:29 +0000 | [diff] [blame] | 1132 | N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg && |
| 1133 | N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Lo && |
Evan Cheng | 27cec74 | 2009-01-19 18:31:51 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | ebf925e | 2009-02-12 17:37:15 +0000 | [diff] [blame] | 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 | } |
Evan Cheng | b6facc4 | 2009-01-16 22:57:32 +0000 | [diff] [blame] | 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 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |