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