| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 1 | //===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===// | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 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. | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This file defines a pattern matching instruction selector for the Cell SPU, | 
|  | 11 | // converting from a legalized dag to a SPU-target dag. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
|  | 15 | #include "SPU.h" | 
|  | 16 | #include "SPUTargetMachine.h" | 
|  | 17 | #include "SPUISelLowering.h" | 
|  | 18 | #include "SPUHazardRecognizers.h" | 
|  | 19 | #include "SPUFrameInfo.h" | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 20 | #include "SPURegisterNames.h" | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineConstantPool.h" | 
|  | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" | 
|  | 23 | #include "llvm/CodeGen/MachineFunction.h" | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SelectionDAG.h" | 
|  | 25 | #include "llvm/CodeGen/SelectionDAGISel.h" | 
|  | 26 | #include "llvm/Target/TargetOptions.h" | 
|  | 27 | #include "llvm/ADT/Statistic.h" | 
|  | 28 | #include "llvm/Constants.h" | 
|  | 29 | #include "llvm/GlobalValue.h" | 
|  | 30 | #include "llvm/Intrinsics.h" | 
|  | 31 | #include "llvm/Support/Debug.h" | 
|  | 32 | #include "llvm/Support/MathExtras.h" | 
|  | 33 | #include "llvm/Support/Compiler.h" | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | using namespace llvm; | 
|  | 36 |  | 
|  | 37 | namespace { | 
|  | 38 | //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates | 
|  | 39 | bool | 
|  | 40 | isI64IntS10Immediate(ConstantSDNode *CN) | 
|  | 41 | { | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 42 | return isS10Constant(CN->getSExtValue()); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates | 
|  | 46 | bool | 
|  | 47 | isI32IntS10Immediate(ConstantSDNode *CN) | 
|  | 48 | { | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 49 | return isS10Constant(CN->getSExtValue()); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | #if 0 | 
|  | 53 | //! SDNode predicate for sign-extended, 10-bit immediate values | 
|  | 54 | bool | 
|  | 55 | isI32IntS10Immediate(SDNode *N) | 
|  | 56 | { | 
|  | 57 | return (N->getOpcode() == ISD::Constant | 
|  | 58 | && isI32IntS10Immediate(cast<ConstantSDNode>(N))); | 
|  | 59 | } | 
|  | 60 | #endif | 
|  | 61 |  | 
| Scott Michel | c5cccb9 | 2007-12-17 22:32:34 +0000 | [diff] [blame] | 62 | //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values | 
|  | 63 | bool | 
|  | 64 | isI32IntU10Immediate(ConstantSDNode *CN) | 
|  | 65 | { | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 66 | return isU10Constant(CN->getSExtValue()); | 
| Scott Michel | c5cccb9 | 2007-12-17 22:32:34 +0000 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 69 | //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values | 
|  | 70 | bool | 
|  | 71 | isI16IntS10Immediate(ConstantSDNode *CN) | 
|  | 72 | { | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 73 | return isS10Constant(CN->getSExtValue()); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
|  | 76 | //! SDNode predicate for i16 sign-extended, 10-bit immediate values | 
|  | 77 | bool | 
|  | 78 | isI16IntS10Immediate(SDNode *N) | 
|  | 79 | { | 
|  | 80 | return (N->getOpcode() == ISD::Constant | 
|  | 81 | && isI16IntS10Immediate(cast<ConstantSDNode>(N))); | 
|  | 82 | } | 
|  | 83 |  | 
| Scott Michel | 0aa7133 | 2007-12-15 00:38:50 +0000 | [diff] [blame] | 84 | //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values | 
|  | 85 | bool | 
|  | 86 | isI16IntU10Immediate(ConstantSDNode *CN) | 
|  | 87 | { | 
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 88 | return isU10Constant((short) CN->getZExtValue()); | 
| Scott Michel | 0aa7133 | 2007-12-15 00:38:50 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | //! SDNode predicate for i16 sign-extended, 10-bit immediate values | 
|  | 92 | bool | 
|  | 93 | isI16IntU10Immediate(SDNode *N) | 
|  | 94 | { | 
|  | 95 | return (N->getOpcode() == ISD::Constant | 
|  | 96 | && isI16IntU10Immediate(cast<ConstantSDNode>(N))); | 
|  | 97 | } | 
|  | 98 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 99 | //! ConstantSDNode predicate for signed 16-bit values | 
|  | 100 | /*! | 
|  | 101 | \arg CN The constant SelectionDAG node holding the value | 
|  | 102 | \arg Imm The returned 16-bit value, if returning true | 
|  | 103 |  | 
|  | 104 | This predicate tests the value in \a CN to see whether it can be | 
|  | 105 | represented as a 16-bit, sign-extended quantity. Returns true if | 
|  | 106 | this is the case. | 
|  | 107 | */ | 
|  | 108 | bool | 
|  | 109 | isIntS16Immediate(ConstantSDNode *CN, short &Imm) | 
|  | 110 | { | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 111 | MVT vt = CN->getValueType(0); | 
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 112 | Imm = (short) CN->getZExtValue(); | 
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 113 | if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 114 | return true; | 
|  | 115 | } else if (vt == MVT::i32) { | 
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 116 | int32_t i_val = (int32_t) CN->getZExtValue(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 117 | short s_val = (short) i_val; | 
|  | 118 | return i_val == s_val; | 
|  | 119 | } else { | 
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 120 | int64_t i_val = (int64_t) CN->getZExtValue(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 121 | short s_val = (short) i_val; | 
|  | 122 | return i_val == s_val; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | return false; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | //! SDNode predicate for signed 16-bit values. | 
|  | 129 | bool | 
|  | 130 | isIntS16Immediate(SDNode *N, short &Imm) | 
|  | 131 | { | 
|  | 132 | return (N->getOpcode() == ISD::Constant | 
|  | 133 | && isIntS16Immediate(cast<ConstantSDNode>(N), Imm)); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext. | 
|  | 137 | static bool | 
|  | 138 | isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm) | 
|  | 139 | { | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 140 | MVT vt = FPN->getValueType(0); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 141 | if (vt == MVT::f32) { | 
| Chris Lattner | 91f3379 | 2007-12-22 22:45:38 +0000 | [diff] [blame] | 142 | int val = FloatToBits(FPN->getValueAPF().convertToFloat()); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 143 | int sval = (int) ((val << 16) >> 16); | 
|  | 144 | Imm = (short) val; | 
|  | 145 | return val == sval; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | return false; | 
|  | 149 | } | 
|  | 150 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 151 | bool | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 152 | isHighLow(const SDValue &Op) | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 153 | { | 
|  | 154 | return (Op.getOpcode() == SPUISD::IndirectAddr | 
|  | 155 | && ((Op.getOperand(0).getOpcode() == SPUISD::Hi | 
|  | 156 | && Op.getOperand(1).getOpcode() == SPUISD::Lo) | 
|  | 157 | || (Op.getOperand(0).getOpcode() == SPUISD::Lo | 
|  | 158 | && Op.getOperand(1).getOpcode() == SPUISD::Hi))); | 
|  | 159 | } | 
|  | 160 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 161 | //===------------------------------------------------------------------===// | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 162 | //! MVT to "useful stuff" mapping structure: | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 163 |  | 
|  | 164 | struct valtype_map_s { | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 165 | MVT VT; | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 166 | unsigned ldresult_ins;      /// LDRESULT instruction (0 = undefined) | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 167 | bool ldresult_imm;          /// LDRESULT instruction requires immediate? | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 168 | int prefslot_byte;          /// Byte offset of the "preferred" slot | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 169 | }; | 
|  | 170 |  | 
|  | 171 | const valtype_map_s valtype_map[] = { | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 172 | { MVT::i1,    0,            false, 3 }, | 
|  | 173 | { MVT::i8,    SPU::ORBIr8,  true,  3 }, | 
|  | 174 | { MVT::i16,   SPU::ORHIr16, true,  2 }, | 
|  | 175 | { MVT::i32,   SPU::ORIr32,  true,  0 }, | 
|  | 176 | { MVT::i64,   SPU::ORr64,   false, 0 }, | 
|  | 177 | { MVT::f32,   SPU::ORf32,   false, 0 }, | 
|  | 178 | { MVT::f64,   SPU::ORf64,   false, 0 }, | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 179 | // vector types... (sigh!) | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 180 | { MVT::v16i8, 0,            false, 0 }, | 
|  | 181 | { MVT::v8i16, 0,            false, 0 }, | 
|  | 182 | { MVT::v4i32, 0,            false, 0 }, | 
|  | 183 | { MVT::v2i64, 0,            false, 0 }, | 
|  | 184 | { MVT::v4f32, 0,            false, 0 }, | 
|  | 185 | { MVT::v2f64, 0,            false, 0 } | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 186 | }; | 
|  | 187 |  | 
|  | 188 | const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]); | 
|  | 189 |  | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 190 | const valtype_map_s *getValueTypeMapEntry(MVT VT) | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 191 | { | 
|  | 192 | const valtype_map_s *retval = 0; | 
|  | 193 | for (size_t i = 0; i < n_valtype_map; ++i) { | 
|  | 194 | if (valtype_map[i].VT == VT) { | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 195 | retval = valtype_map + i; | 
|  | 196 | break; | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 197 | } | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 |  | 
|  | 201 | #ifndef NDEBUG | 
|  | 202 | if (retval == 0) { | 
|  | 203 | cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for " | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 204 | << VT.getMVTString() | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 205 | << "\n"; | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 206 | abort(); | 
|  | 207 | } | 
|  | 208 | #endif | 
|  | 209 |  | 
|  | 210 | return retval; | 
|  | 211 | } | 
|  | 212 | } | 
|  | 213 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 214 | namespace { | 
|  | 215 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 216 | //===--------------------------------------------------------------------===// | 
|  | 217 | /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine | 
|  | 218 | /// instructions for SelectionDAG operations. | 
|  | 219 | /// | 
|  | 220 | class SPUDAGToDAGISel : | 
|  | 221 | public SelectionDAGISel | 
|  | 222 | { | 
|  | 223 | SPUTargetMachine &TM; | 
|  | 224 | SPUTargetLowering &SPUtli; | 
|  | 225 | unsigned GlobalBaseReg; | 
|  | 226 |  | 
|  | 227 | public: | 
| Dan Gohman | 56e3f63 | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 228 | explicit SPUDAGToDAGISel(SPUTargetMachine &tm) : | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 229 | SelectionDAGISel(*tm.getTargetLowering()), | 
|  | 230 | TM(tm), | 
|  | 231 | SPUtli(*tm.getTargetLowering()) | 
|  | 232 | {} | 
|  | 233 |  | 
|  | 234 | virtual bool runOnFunction(Function &Fn) { | 
|  | 235 | // Make sure we re-emit a set of the global base reg if necessary | 
|  | 236 | GlobalBaseReg = 0; | 
|  | 237 | SelectionDAGISel::runOnFunction(Fn); | 
|  | 238 | return true; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | /// getI32Imm - Return a target constant with the specified value, of type | 
|  | 242 | /// i32. | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 243 | inline SDValue getI32Imm(uint32_t Imm) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 244 | return CurDAG->getTargetConstant(Imm, MVT::i32); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | /// getI64Imm - Return a target constant with the specified value, of type | 
|  | 248 | /// i64. | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 249 | inline SDValue getI64Imm(uint64_t Imm) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 250 | return CurDAG->getTargetConstant(Imm, MVT::i64); | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | /// getSmallIPtrImm - Return a target constant of pointer type. | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 254 | inline SDValue getSmallIPtrImm(unsigned Imm) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 255 | return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy()); | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | /// Select - Convert the specified operand from a target-independent to a | 
|  | 259 | /// target-specific node if it hasn't already been changed. | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 260 | SDNode *Select(SDValue Op); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 261 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 262 | //! Returns true if the address N is an A-form (local store) address | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 263 | bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base, | 
|  | 264 | SDValue &Index); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 265 |  | 
|  | 266 | //! D-form address predicate | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 267 | bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base, | 
|  | 268 | SDValue &Index); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 269 |  | 
|  | 270 | /// Alternate D-form address using i7 offset predicate | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 271 | bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp, | 
|  | 272 | SDValue &Base); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 273 |  | 
|  | 274 | /// D-form address selection workhorse | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 275 | bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp, | 
|  | 276 | SDValue &Base, int minOffset, int maxOffset); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 277 |  | 
|  | 278 | //! Address predicate if N can be expressed as an indexed [r+r] operation. | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 279 | bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base, | 
|  | 280 | SDValue &Index); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 281 |  | 
|  | 282 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for | 
|  | 283 | /// inline asm expressions. | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 284 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 285 | char ConstraintCode, | 
| Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 286 | std::vector<SDValue> &OutOps) { | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 287 | SDValue Op0, Op1; | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 288 | switch (ConstraintCode) { | 
|  | 289 | default: return true; | 
|  | 290 | case 'm':   // memory | 
|  | 291 | if (!SelectDFormAddr(Op, Op, Op0, Op1) | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 292 | && !SelectAFormAddr(Op, Op, Op0, Op1)) | 
|  | 293 | SelectXFormAddr(Op, Op, Op0, Op1); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 294 | break; | 
|  | 295 | case 'o':   // offsetable | 
|  | 296 | if (!SelectDFormAddr(Op, Op, Op0, Op1) | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 297 | && !SelectAFormAddr(Op, Op, Op0, Op1)) { | 
|  | 298 | Op0 = Op; | 
|  | 299 | AddToISelQueue(Op0);     // r+0. | 
|  | 300 | Op1 = getSmallIPtrImm(0); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 301 | } | 
|  | 302 | break; | 
|  | 303 | case 'v':   // not offsetable | 
|  | 304 | #if 1 | 
|  | 305 | assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled."); | 
|  | 306 | #else | 
|  | 307 | SelectAddrIdxOnly(Op, Op, Op0, Op1); | 
|  | 308 | #endif | 
|  | 309 | break; | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | OutOps.push_back(Op0); | 
|  | 313 | OutOps.push_back(Op1); | 
|  | 314 | return false; | 
|  | 315 | } | 
|  | 316 |  | 
| Evan Cheng | 0711d68 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 317 | /// InstructionSelect - This callback is invoked by | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 318 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. | 
| Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 319 | virtual void InstructionSelect(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 320 |  | 
|  | 321 | virtual const char *getPassName() const { | 
|  | 322 | return "Cell SPU DAG->DAG Pattern Instruction Selection"; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for | 
|  | 326 | /// this target when scheduling the DAG. | 
|  | 327 | virtual HazardRecognizer *CreateTargetHazardRecognizer() { | 
| Dan Gohman | 634412f | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 328 | const TargetInstrInfo *II = TM.getInstrInfo(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 329 | assert(II && "No InstrInfo?"); | 
|  | 330 | return new SPUHazardRecognizer(*II); | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | // Include the pieces autogenerated from the target description. | 
|  | 334 | #include "SPUGenDAGISel.inc" | 
|  | 335 | }; | 
|  | 336 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Evan Cheng | 0711d68 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 339 | /// InstructionSelect - This callback is invoked by | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 340 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. | 
|  | 341 | void | 
| Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 342 | SPUDAGToDAGISel::InstructionSelect() | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 343 | { | 
|  | 344 | DEBUG(BB->dump()); | 
|  | 345 |  | 
|  | 346 | // Select target instructions for the DAG. | 
| Dan Gohman | d3582c9 | 2008-08-21 16:36:34 +0000 | [diff] [blame] | 347 | SelectRoot(); | 
| Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 348 | CurDAG->RemoveDeadNodes(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 351 | /*! | 
|  | 352 | \arg Op The ISD instructio operand | 
|  | 353 | \arg N The address to be tested | 
|  | 354 | \arg Base The base address | 
|  | 355 | \arg Index The base address index | 
|  | 356 | */ | 
|  | 357 | bool | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 358 | SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base, | 
|  | 359 | SDValue &Index) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 360 | // These match the addr256k operand type: | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 361 | MVT OffsVT = MVT::i16; | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 362 | SDValue Zero = CurDAG->getTargetConstant(0, OffsVT); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 363 |  | 
|  | 364 | switch (N.getOpcode()) { | 
|  | 365 | case ISD::Constant: | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 366 | case ISD::ConstantPool: | 
|  | 367 | case ISD::GlobalAddress: | 
|  | 368 | cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n"; | 
|  | 369 | abort(); | 
|  | 370 | /*NOTREACHED*/ | 
|  | 371 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 372 | case ISD::TargetConstant: | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 373 | case ISD::TargetGlobalAddress: | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 374 | case ISD::TargetJumpTable: | 
|  | 375 | cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as " | 
|  | 376 | << "A-form address.\n"; | 
|  | 377 | abort(); | 
|  | 378 | /*NOTREACHED*/ | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 379 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 380 | case SPUISD::AFormAddr: | 
|  | 381 | // Just load from memory if there's only a single use of the location, | 
|  | 382 | // otherwise, this will get handled below with D-form offset addresses | 
|  | 383 | if (N.hasOneUse()) { | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 384 | SDValue Op0 = N.getOperand(0); | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 385 | switch (Op0.getOpcode()) { | 
|  | 386 | case ISD::TargetConstantPool: | 
|  | 387 | case ISD::TargetJumpTable: | 
|  | 388 | Base = Op0; | 
|  | 389 | Index = Zero; | 
|  | 390 | return true; | 
|  | 391 |  | 
|  | 392 | case ISD::TargetGlobalAddress: { | 
|  | 393 | GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0); | 
|  | 394 | GlobalValue *GV = GSDN->getGlobal(); | 
|  | 395 | if (GV->getAlignment() == 16) { | 
|  | 396 | Base = Op0; | 
|  | 397 | Index = Zero; | 
|  | 398 | return true; | 
|  | 399 | } | 
|  | 400 | break; | 
|  | 401 | } | 
|  | 402 | } | 
|  | 403 | } | 
|  | 404 | break; | 
|  | 405 | } | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 406 | return false; | 
|  | 407 | } | 
|  | 408 |  | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 409 | bool | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 410 | SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp, | 
|  | 411 | SDValue &Base) { | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 412 | const int minDForm2Offset = -(1 << 7); | 
|  | 413 | const int maxDForm2Offset = (1 << 7) - 1; | 
|  | 414 | return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset, | 
|  | 415 | maxDForm2Offset); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 418 | /*! | 
|  | 419 | \arg Op The ISD instruction (ignored) | 
|  | 420 | \arg N The address to be tested | 
|  | 421 | \arg Base Base address register/pointer | 
|  | 422 | \arg Index Base address index | 
|  | 423 |  | 
|  | 424 | Examine the input address by a base register plus a signed 10-bit | 
|  | 425 | displacement, [r+I10] (D-form address). | 
|  | 426 |  | 
|  | 427 | \return true if \a N is a D-form address with \a Base and \a Index set | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 428 | to non-empty SDValue instances. | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 429 | */ | 
|  | 430 | bool | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 431 | SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base, | 
|  | 432 | SDValue &Index) { | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 433 | return DFormAddressPredicate(Op, N, Base, Index, | 
|  | 434 | SPUFrameInfo::minFrameOffset(), | 
|  | 435 | SPUFrameInfo::maxFrameOffset()); | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | bool | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 439 | SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base, | 
|  | 440 | SDValue &Index, int minOffset, | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 441 | int maxOffset) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 442 | unsigned Opc = N.getOpcode(); | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 443 | MVT PtrTy = SPUtli.getPointerTy(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 444 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 445 | if (Opc == ISD::FrameIndex) { | 
|  | 446 | // Stack frame index must be less than 512 (divided by 16): | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 447 | FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N); | 
|  | 448 | int FI = int(FIN->getIndex()); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 449 | DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = " | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 450 | << FI << "\n"); | 
|  | 451 | if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 452 | Base = CurDAG->getTargetConstant(0, PtrTy); | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 453 | Index = CurDAG->getTargetFrameIndex(FI, PtrTy); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 454 | return true; | 
|  | 455 | } | 
|  | 456 | } else if (Opc == ISD::ADD) { | 
|  | 457 | // Generated by getelementptr | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 458 | const SDValue Op0 = N.getOperand(0); | 
|  | 459 | const SDValue Op1 = N.getOperand(1); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 460 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 461 | if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo) | 
|  | 462 | || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) { | 
|  | 463 | Base = CurDAG->getTargetConstant(0, PtrTy); | 
|  | 464 | Index = N; | 
|  | 465 | return true; | 
|  | 466 | } else if (Op1.getOpcode() == ISD::Constant | 
|  | 467 | || Op1.getOpcode() == ISD::TargetConstant) { | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 468 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1); | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 469 | int32_t offset = int32_t(CN->getSExtValue()); | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 470 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 471 | if (Op0.getOpcode() == ISD::FrameIndex) { | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 472 | FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0); | 
|  | 473 | int FI = int(FIN->getIndex()); | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 474 | DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 475 | << " frame index = " << FI << "\n"); | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 476 |  | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 477 | if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) { | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 478 | Base = CurDAG->getTargetConstant(offset, PtrTy); | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 479 | Index = CurDAG->getTargetFrameIndex(FI, PtrTy); | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 480 | return true; | 
|  | 481 | } | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 482 | } else if (offset > minOffset && offset < maxOffset) { | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 483 | Base = CurDAG->getTargetConstant(offset, PtrTy); | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 484 | Index = Op0; | 
|  | 485 | return true; | 
|  | 486 | } | 
|  | 487 | } else if (Op0.getOpcode() == ISD::Constant | 
|  | 488 | || Op0.getOpcode() == ISD::TargetConstant) { | 
|  | 489 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0); | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 490 | int32_t offset = int32_t(CN->getSExtValue()); | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 491 |  | 
|  | 492 | if (Op1.getOpcode() == ISD::FrameIndex) { | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 493 | FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1); | 
|  | 494 | int FI = int(FIN->getIndex()); | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 495 | DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 496 | << " frame index = " << FI << "\n"); | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 497 |  | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 498 | if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) { | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 499 | Base = CurDAG->getTargetConstant(offset, PtrTy); | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 500 | Index = CurDAG->getTargetFrameIndex(FI, PtrTy); | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 501 | return true; | 
|  | 502 | } | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 503 | } else if (offset > minOffset && offset < maxOffset) { | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 504 | Base = CurDAG->getTargetConstant(offset, PtrTy); | 
|  | 505 | Index = Op1; | 
|  | 506 | return true; | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 507 | } | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 508 | } | 
|  | 509 | } else if (Opc == SPUISD::IndirectAddr) { | 
|  | 510 | // Indirect with constant offset -> D-Form address | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 511 | const SDValue Op0 = N.getOperand(0); | 
|  | 512 | const SDValue Op1 = N.getOperand(1); | 
| Scott Michel | a8f67e0 | 2008-01-11 21:01:19 +0000 | [diff] [blame] | 513 |  | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 514 | if (Op0.getOpcode() == SPUISD::Hi | 
|  | 515 | && Op1.getOpcode() == SPUISD::Lo) { | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 516 | // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0)) | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 517 | Base = CurDAG->getTargetConstant(0, PtrTy); | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 518 | Index = N; | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 519 | return true; | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 520 | } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) { | 
|  | 521 | int32_t offset = 0; | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 522 | SDValue idxOp; | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 523 |  | 
|  | 524 | if (isa<ConstantSDNode>(Op1)) { | 
|  | 525 | ConstantSDNode *CN = cast<ConstantSDNode>(Op1); | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 526 | offset = int32_t(CN->getSExtValue()); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 527 | idxOp = Op0; | 
|  | 528 | } else if (isa<ConstantSDNode>(Op0)) { | 
|  | 529 | ConstantSDNode *CN = cast<ConstantSDNode>(Op0); | 
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 530 | offset = int32_t(CN->getSExtValue()); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 531 | idxOp = Op1; | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | if (offset >= minOffset && offset <= maxOffset) { | 
|  | 535 | Base = CurDAG->getTargetConstant(offset, PtrTy); | 
|  | 536 | Index = idxOp; | 
|  | 537 | return true; | 
|  | 538 | } | 
| Scott Michel | 8d5841a | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 539 | } | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 540 | } else if (Opc == SPUISD::AFormAddr) { | 
|  | 541 | Base = CurDAG->getTargetConstant(0, N.getValueType()); | 
|  | 542 | Index = N; | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 543 | return true; | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 544 | } else if (Opc == SPUISD::LDRESULT) { | 
|  | 545 | Base = CurDAG->getTargetConstant(0, N.getValueType()); | 
|  | 546 | Index = N; | 
|  | 547 | return true; | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 548 | } | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 549 | return false; | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | /*! | 
|  | 553 | \arg Op The ISD instruction operand | 
|  | 554 | \arg N The address operand | 
|  | 555 | \arg Base The base pointer operand | 
|  | 556 | \arg Index The offset/index operand | 
|  | 557 |  | 
|  | 558 | If the address \a N can be expressed as a [r + s10imm] address, returns false. | 
|  | 559 | Otherwise, creates two operands, Base and Index that will become the [r+r] | 
|  | 560 | address. | 
|  | 561 | */ | 
|  | 562 | bool | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 563 | SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base, | 
|  | 564 | SDValue &Index) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 565 | if (SelectAFormAddr(Op, N, Base, Index) | 
|  | 566 | || SelectDFormAddr(Op, N, Base, Index)) | 
|  | 567 | return false; | 
|  | 568 |  | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 569 | // All else fails, punt and use an X-form address: | 
|  | 570 | Base = N.getOperand(0); | 
|  | 571 | Index = N.getOperand(1); | 
|  | 572 | return true; | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 573 | } | 
|  | 574 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 575 | //! Convert the operand from a target-independent to a target-specific node | 
|  | 576 | /*! | 
|  | 577 | */ | 
|  | 578 | SDNode * | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 579 | SPUDAGToDAGISel::Select(SDValue Op) { | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 580 | SDNode *N = Op.getNode(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 581 | unsigned Opc = N->getOpcode(); | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 582 | int n_ops = -1; | 
|  | 583 | unsigned NewOpc; | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 584 | MVT OpVT = Op.getValueType(); | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 585 | SDValue Ops[8]; | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 586 |  | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 587 | if (N->isMachineOpcode()) { | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 588 | return NULL;   // Already selected. | 
|  | 589 | } else if (Opc == ISD::FrameIndex) { | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 590 | // Selects to (add $sp, FI * stackSlotSize) | 
|  | 591 | int FI = | 
|  | 592 | SPUFrameInfo::FItoStackOffset(cast<FrameIndexSDNode>(N)->getIndex()); | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 593 | MVT PtrVT = SPUtli.getPointerTy(); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 594 |  | 
| Scott Michel | c3a1910 | 2008-04-30 00:30:08 +0000 | [diff] [blame] | 595 | // Adjust stack slot to actual offset in frame: | 
|  | 596 | if (isS10Constant(FI)) { | 
|  | 597 | DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AIr32 $sp, " | 
|  | 598 | << FI | 
|  | 599 | << "\n"); | 
|  | 600 | NewOpc = SPU::AIr32; | 
|  | 601 | Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT); | 
|  | 602 | Ops[1] = CurDAG->getTargetConstant(FI, PtrVT); | 
|  | 603 | n_ops = 2; | 
|  | 604 | } else { | 
|  | 605 | DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with Ar32 $sp, " | 
|  | 606 | << FI | 
|  | 607 | << "\n"); | 
|  | 608 | NewOpc = SPU::Ar32; | 
|  | 609 | Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT); | 
|  | 610 | Ops[1] = CurDAG->getConstant(FI, PtrVT); | 
|  | 611 | n_ops = 2; | 
|  | 612 |  | 
|  | 613 | AddToISelQueue(Ops[1]); | 
|  | 614 | } | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 615 | } else if (Opc == ISD::ZERO_EXTEND) { | 
|  | 616 | // (zero_extend:i16 (and:i8 <arg>, <const>)) | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 617 | const SDValue &Op1 = N->getOperand(0); | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 618 |  | 
|  | 619 | if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) { | 
|  | 620 | if (Op1.getOpcode() == ISD::AND) { | 
|  | 621 | // Fold this into a single ANDHI. This is often seen in expansions of i1 | 
|  | 622 | // to i8, then i8 to i16 in logical/branching operations. | 
|  | 623 | DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 " | 
|  | 624 | "<arg>, <const>))\n"); | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 625 | NewOpc = SPU::ANDHIi8i16; | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 626 | Ops[0] = Op1.getOperand(0); | 
|  | 627 | Ops[1] = Op1.getOperand(1); | 
|  | 628 | n_ops = 2; | 
|  | 629 | } | 
|  | 630 | } | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 631 | } else if (Opc == SPUISD::LDRESULT) { | 
|  | 632 | // Custom select instructions for LDRESULT | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 633 | MVT VT = N->getValueType(0); | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 634 | SDValue Arg = N->getOperand(0); | 
|  | 635 | SDValue Chain = N->getOperand(1); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 636 | SDNode *Result; | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 637 | const valtype_map_s *vtm = getValueTypeMapEntry(VT); | 
|  | 638 |  | 
|  | 639 | if (vtm->ldresult_ins == 0) { | 
|  | 640 | cerr << "LDRESULT for unsupported type: " | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 641 | << VT.getMVTString() | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 642 | << "\n"; | 
|  | 643 | abort(); | 
|  | 644 | } | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 645 |  | 
|  | 646 | AddToISelQueue(Arg); | 
| Scott Michel | 7d5eaec | 2008-02-23 18:41:37 +0000 | [diff] [blame] | 647 | Opc = vtm->ldresult_ins; | 
|  | 648 | if (vtm->ldresult_imm) { | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 649 | SDValue Zero = CurDAG->getTargetConstant(0, VT); | 
| Scott Michel | 5f1470f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 650 |  | 
|  | 651 | AddToISelQueue(Zero); | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 652 | Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain); | 
| Scott Michel | 5f1470f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 653 | } else { | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 654 | Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain); | 
| Scott Michel | 5f1470f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 655 | } | 
|  | 656 |  | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 657 | Chain = SDValue(Result, 1); | 
| Scott Michel | 5f1470f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 658 | AddToISelQueue(Chain); | 
|  | 659 |  | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 660 | return Result; | 
| Scott Michel | ceae3bb | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 661 | } else if (Opc == SPUISD::IndirectAddr) { | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 662 | SDValue Op0 = Op.getOperand(0); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 663 | if (Op0.getOpcode() == SPUISD::LDRESULT) { | 
|  | 664 | /* || Op0.getOpcode() == SPUISD::AFormAddr) */ | 
|  | 665 | // (IndirectAddr (LDRESULT, imm)) | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 666 | SDValue Op1 = Op.getOperand(1); | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 667 | MVT VT = Op.getValueType(); | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 668 |  | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 669 | DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = "); | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 670 | DEBUG(Op.getOperand(0).getNode()->dump(CurDAG)); | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 671 | DEBUG(cerr << "\nOp1 = "); | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 672 | DEBUG(Op.getOperand(1).getNode()->dump(CurDAG)); | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 673 | DEBUG(cerr << "\n"); | 
|  | 674 |  | 
|  | 675 | if (Op1.getOpcode() == ISD::Constant) { | 
|  | 676 | ConstantSDNode *CN = cast<ConstantSDNode>(Op1); | 
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 677 | Op1 = CurDAG->getTargetConstant(CN->getZExtValue(), VT); | 
| Scott Michel | bb713ae | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 678 | NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32); | 
|  | 679 | AddToISelQueue(Op0); | 
|  | 680 | AddToISelQueue(Op1); | 
|  | 681 | Ops[0] = Op0; | 
|  | 682 | Ops[1] = Op1; | 
|  | 683 | n_ops = 2; | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 684 | } | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 685 | } | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 686 | } | 
|  | 687 |  | 
| Scott Michel | e4d3e3c | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 688 | if (n_ops > 0) { | 
|  | 689 | if (N->hasOneUse()) | 
|  | 690 | return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops); | 
|  | 691 | else | 
|  | 692 | return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops); | 
|  | 693 | } else | 
|  | 694 | return SelectCode(Op); | 
| Scott Michel | 6e22c65 | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 695 | } | 
|  | 696 |  | 
|  | 697 | /// createPPCISelDag - This pass converts a legalized DAG into a | 
|  | 698 | /// SPU-specific DAG, ready for instruction scheduling. | 
|  | 699 | /// | 
|  | 700 | FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) { | 
|  | 701 | return new SPUDAGToDAGISel(TM); | 
|  | 702 | } |