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