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" |
| 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/SelectionDAG.h" |
| 24 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 25 | #include "llvm/Target/TargetOptions.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/GlobalValue.h" |
| 29 | #include "llvm/Intrinsics.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/MathExtras.h" |
| 32 | #include "llvm/Support/Compiler.h" |
| 33 | #include <iostream> |
| 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 | { |
| 44 | return isS10Constant(CN->getValue()); |
| 45 | } |
| 46 | |
| 47 | //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates |
| 48 | bool |
| 49 | isI32IntS10Immediate(ConstantSDNode *CN) |
| 50 | { |
| 51 | return isS10Constant((int) CN->getValue()); |
| 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 | { |
| 68 | return isU10Constant((int) CN->getValue()); |
| 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 | { |
| 75 | return isS10Constant((short) CN->getValue()); |
| 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 | { |
| 113 | MVT::ValueType vt = CN->getValueType(0); |
| 114 | Imm = (short) CN->getValue(); |
| 115 | if (vt >= MVT::i1 && vt <= MVT::i16) { |
| 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 | { |
| 142 | MVT::ValueType vt = FPN->getValueType(0); |
| 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 |
| 154 | isHighLow(const SDOperand &Op) |
| 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 | //===------------------------------------------------------------------===// |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 164 | //! MVT::ValueType to "useful stuff" mapping structure: |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 165 | |
| 166 | struct valtype_map_s { |
| 167 | MVT::ValueType 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 | |
| 192 | const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT) |
| 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 " |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 206 | << MVT::getValueTypeString(VT) |
| 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 | |
| 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: |
| 228 | SPUDAGToDAGISel(SPUTargetMachine &tm) : |
| 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. |
| 243 | inline SDOperand getI32Imm(uint32_t Imm) { |
| 244 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 245 | } |
| 246 | |
| 247 | /// getI64Imm - Return a target constant with the specified value, of type |
| 248 | /// i64. |
| 249 | inline SDOperand getI64Imm(uint64_t Imm) { |
| 250 | return CurDAG->getTargetConstant(Imm, MVT::i64); |
| 251 | } |
| 252 | |
| 253 | /// getSmallIPtrImm - Return a target constant of pointer type. |
| 254 | inline SDOperand getSmallIPtrImm(unsigned Imm) { |
| 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. |
| 260 | SDNode *Select(SDOperand Op); |
| 261 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 262 | //! Returns true if the address N is an A-form (local store) address |
| 263 | bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 264 | SDOperand &Index); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 265 | |
| 266 | //! D-form address predicate |
| 267 | bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 268 | SDOperand &Index); |
| 269 | |
| 270 | /// Alternate D-form address using i7 offset predicate |
| 271 | bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp, |
| 272 | SDOperand &Base); |
| 273 | |
| 274 | /// D-form address selection workhorse |
| 275 | bool DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Disp, |
| 276 | SDOperand &Base, int minOffset, int maxOffset); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 277 | |
| 278 | //! Address predicate if N can be expressed as an indexed [r+r] operation. |
| 279 | bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 280 | SDOperand &Index); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 281 | |
| 282 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 283 | /// inline asm expressions. |
| 284 | virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 285 | char ConstraintCode, |
| 286 | std::vector<SDOperand> &OutOps, |
| 287 | SelectionDAG &DAG) { |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 288 | SDOperand Op0, Op1; |
| 289 | switch (ConstraintCode) { |
| 290 | default: return true; |
| 291 | case 'm': // memory |
| 292 | if (!SelectDFormAddr(Op, Op, Op0, Op1) |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 293 | && !SelectAFormAddr(Op, Op, Op0, Op1)) |
| 294 | SelectXFormAddr(Op, Op, Op0, Op1); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 295 | break; |
| 296 | case 'o': // offsetable |
| 297 | if (!SelectDFormAddr(Op, Op, Op0, Op1) |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 298 | && !SelectAFormAddr(Op, Op, Op0, Op1)) { |
| 299 | Op0 = Op; |
| 300 | AddToISelQueue(Op0); // r+0. |
| 301 | Op1 = getSmallIPtrImm(0); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 302 | } |
| 303 | break; |
| 304 | case 'v': // not offsetable |
| 305 | #if 1 |
| 306 | assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled."); |
| 307 | #else |
| 308 | SelectAddrIdxOnly(Op, Op, Op0, Op1); |
| 309 | #endif |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | OutOps.push_back(Op0); |
| 314 | OutOps.push_back(Op1); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 319 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 320 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 321 | |
| 322 | virtual const char *getPassName() const { |
| 323 | return "Cell SPU DAG->DAG Pattern Instruction Selection"; |
| 324 | } |
| 325 | |
| 326 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 327 | /// this target when scheduling the DAG. |
| 328 | virtual HazardRecognizer *CreateTargetHazardRecognizer() { |
| 329 | const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo(); |
| 330 | assert(II && "No InstrInfo?"); |
| 331 | return new SPUHazardRecognizer(*II); |
| 332 | } |
| 333 | |
| 334 | // Include the pieces autogenerated from the target description. |
| 335 | #include "SPUGenDAGISel.inc" |
| 336 | }; |
| 337 | |
| 338 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 339 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 340 | void |
| 341 | SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) |
| 342 | { |
| 343 | DEBUG(BB->dump()); |
| 344 | |
| 345 | // Select target instructions for the DAG. |
| 346 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
| 347 | DAG.RemoveDeadNodes(); |
| 348 | |
| 349 | // Emit machine code to BB. |
| 350 | ScheduleAndEmitDAG(DAG); |
| 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 |
| 360 | SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 361 | SDOperand &Index) { |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 362 | // These match the addr256k operand type: |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 363 | MVT::ValueType OffsVT = MVT::i16; |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 364 | SDOperand 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()) { |
| 386 | SDOperand Op0 = N.getOperand(0); |
| 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 |
| 412 | SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp, |
| 413 | SDOperand &Base) { |
| 414 | return DFormAddressPredicate(Op, N, Disp, Base, -(1 << 7), (1 << 7) - 1); |
| 415 | } |
| 416 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 417 | /*! |
| 418 | \arg Op The ISD instruction (ignored) |
| 419 | \arg N The address to be tested |
| 420 | \arg Base Base address register/pointer |
| 421 | \arg Index Base address index |
| 422 | |
| 423 | Examine the input address by a base register plus a signed 10-bit |
| 424 | displacement, [r+I10] (D-form address). |
| 425 | |
| 426 | \return true if \a N is a D-form address with \a Base and \a Index set |
| 427 | to non-empty SDOperand instances. |
| 428 | */ |
| 429 | bool |
| 430 | SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 431 | SDOperand &Index) { |
| 432 | return DFormAddressPredicate(Op, N, Base, Index, |
| 433 | SPUFrameInfo::minFrameOffset(), |
| 434 | SPUFrameInfo::maxFrameOffset()); |
| 435 | } |
| 436 | |
| 437 | bool |
| 438 | SPUDAGToDAGISel::DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Base, |
| 439 | SDOperand &Index, int minOffset, |
| 440 | int maxOffset) { |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 441 | unsigned Opc = N.getOpcode(); |
| 442 | unsigned PtrTy = SPUtli.getPointerTy(); |
| 443 | |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 444 | if (Opc == ISD::FrameIndex) { |
| 445 | // Stack frame index must be less than 512 (divided by 16): |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 446 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N); |
| 447 | DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = " |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 448 | << FI->getIndex() << "\n"); |
| 449 | if (FI->getIndex() < maxOffset) { |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 450 | Base = CurDAG->getTargetConstant(0, PtrTy); |
| 451 | Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy); |
| 452 | return true; |
| 453 | } |
| 454 | } else if (Opc == ISD::ADD) { |
| 455 | // Generated by getelementptr |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 456 | const SDOperand Op0 = N.getOperand(0); |
| 457 | const SDOperand 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); |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 467 | int32_t offset = int32_t(CN->getSignExtended()); |
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 | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 470 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op0); |
| 471 | DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset |
| 472 | << " frame index = " << FI->getIndex() << "\n"); |
| 473 | |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 474 | if (FI->getIndex() < maxOffset) { |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 475 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
| 476 | Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy); |
| 477 | return true; |
| 478 | } |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 479 | } else if (offset > minOffset && offset < maxOffset) { |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 480 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 481 | Index = Op0; |
| 482 | return true; |
| 483 | } |
| 484 | } else if (Op0.getOpcode() == ISD::Constant |
| 485 | || Op0.getOpcode() == ISD::TargetConstant) { |
| 486 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0); |
| 487 | int32_t offset = int32_t(CN->getSignExtended()); |
| 488 | |
| 489 | if (Op1.getOpcode() == ISD::FrameIndex) { |
| 490 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op1); |
| 491 | DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset |
| 492 | << " frame index = " << FI->getIndex() << "\n"); |
| 493 | |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 494 | if (FI->getIndex() < maxOffset) { |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 495 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
| 496 | Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy); |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 497 | return true; |
| 498 | } |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 499 | } else if (offset > minOffset && offset < maxOffset) { |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 500 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
| 501 | Index = Op1; |
| 502 | return true; |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 503 | } |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 504 | } |
| 505 | } else if (Opc == SPUISD::IndirectAddr) { |
| 506 | // Indirect with constant offset -> D-Form address |
| 507 | const SDOperand Op0 = N.getOperand(0); |
| 508 | const SDOperand Op1 = N.getOperand(1); |
Scott Michel | 497e888 | 2008-01-11 21:01:19 +0000 | [diff] [blame] | 509 | |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 510 | if (Op0.getOpcode() == SPUISD::Hi |
| 511 | && Op1.getOpcode() == SPUISD::Lo) { |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 512 | // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0)) |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 513 | Base = CurDAG->getTargetConstant(0, PtrTy); |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 514 | Index = N; |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 515 | return true; |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 516 | } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) { |
| 517 | int32_t offset = 0; |
| 518 | SDOperand idxOp; |
| 519 | |
| 520 | if (isa<ConstantSDNode>(Op1)) { |
| 521 | ConstantSDNode *CN = cast<ConstantSDNode>(Op1); |
| 522 | offset = int32_t(CN->getSignExtended()); |
| 523 | idxOp = Op0; |
| 524 | } else if (isa<ConstantSDNode>(Op0)) { |
| 525 | ConstantSDNode *CN = cast<ConstantSDNode>(Op0); |
| 526 | offset = int32_t(CN->getSignExtended()); |
| 527 | idxOp = Op1; |
| 528 | } |
| 529 | |
| 530 | if (offset >= minOffset && offset <= maxOffset) { |
| 531 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
| 532 | Index = idxOp; |
| 533 | return true; |
| 534 | } |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 535 | } |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 536 | } else if (Opc == SPUISD::AFormAddr) { |
| 537 | Base = CurDAG->getTargetConstant(0, N.getValueType()); |
| 538 | Index = N; |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 539 | return true; |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 540 | } else if (Opc == SPUISD::LDRESULT) { |
| 541 | Base = CurDAG->getTargetConstant(0, N.getValueType()); |
| 542 | Index = N; |
| 543 | return true; |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 544 | } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 545 | return false; |
| 546 | } |
| 547 | |
| 548 | /*! |
| 549 | \arg Op The ISD instruction operand |
| 550 | \arg N The address operand |
| 551 | \arg Base The base pointer operand |
| 552 | \arg Index The offset/index operand |
| 553 | |
| 554 | If the address \a N can be expressed as a [r + s10imm] address, returns false. |
| 555 | Otherwise, creates two operands, Base and Index that will become the [r+r] |
| 556 | address. |
| 557 | */ |
| 558 | bool |
| 559 | SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 560 | SDOperand &Index) { |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 561 | if (SelectAFormAddr(Op, N, Base, Index) |
| 562 | || SelectDFormAddr(Op, N, Base, Index)) |
| 563 | return false; |
| 564 | |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 565 | // All else fails, punt and use an X-form address: |
| 566 | Base = N.getOperand(0); |
| 567 | Index = N.getOperand(1); |
| 568 | return true; |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 571 | //! Convert the operand from a target-independent to a target-specific node |
| 572 | /*! |
| 573 | */ |
| 574 | SDNode * |
| 575 | SPUDAGToDAGISel::Select(SDOperand Op) { |
| 576 | SDNode *N = Op.Val; |
| 577 | unsigned Opc = N->getOpcode(); |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 578 | int n_ops = -1; |
| 579 | unsigned NewOpc; |
| 580 | MVT::ValueType OpVT = Op.getValueType(); |
| 581 | SDOperand Ops[8]; |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 582 | |
| 583 | if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) { |
| 584 | return NULL; // Already selected. |
| 585 | } else if (Opc == ISD::FrameIndex) { |
| 586 | // Selects to AIr32 FI, 0 which in turn will become AIr32 SP, imm. |
| 587 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 588 | MVT::ValueType PtrVT = SPUtli.getPointerTy(); |
| 589 | SDOperand Zero = CurDAG->getTargetConstant(0, PtrVT); |
| 590 | SDOperand TFI = CurDAG->getTargetFrameIndex(FI, PtrVT); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 591 | |
Scott Michel | 9999e68 | 2007-12-19 07:35:06 +0000 | [diff] [blame] | 592 | DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AI32 <FI>, 0\n"); |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 593 | NewOpc = SPU::AIr32; |
| 594 | Ops[0] = TFI; |
| 595 | Ops[1] = Zero; |
| 596 | n_ops = 2; |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 597 | } else if (Opc == ISD::ZERO_EXTEND) { |
| 598 | // (zero_extend:i16 (and:i8 <arg>, <const>)) |
| 599 | const SDOperand &Op1 = N->getOperand(0); |
| 600 | |
| 601 | if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) { |
| 602 | if (Op1.getOpcode() == ISD::AND) { |
| 603 | // Fold this into a single ANDHI. This is often seen in expansions of i1 |
| 604 | // to i8, then i8 to i16 in logical/branching operations. |
| 605 | DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 " |
| 606 | "<arg>, <const>))\n"); |
Scott Michel | a59d469 | 2008-02-23 18:41:37 +0000 | [diff] [blame^] | 607 | NewOpc = SPU::ANDHIi8i16; |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 608 | Ops[0] = Op1.getOperand(0); |
| 609 | Ops[1] = Op1.getOperand(1); |
| 610 | n_ops = 2; |
| 611 | } |
| 612 | } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 613 | } else if (Opc == SPUISD::LDRESULT) { |
| 614 | // Custom select instructions for LDRESULT |
| 615 | unsigned VT = N->getValueType(0); |
| 616 | SDOperand Arg = N->getOperand(0); |
| 617 | SDOperand Chain = N->getOperand(1); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 618 | SDNode *Result; |
Scott Michel | a59d469 | 2008-02-23 18:41:37 +0000 | [diff] [blame^] | 619 | const valtype_map_s *vtm = getValueTypeMapEntry(VT); |
| 620 | |
| 621 | if (vtm->ldresult_ins == 0) { |
| 622 | cerr << "LDRESULT for unsupported type: " |
| 623 | << MVT::getValueTypeString(VT) |
| 624 | << "\n"; |
| 625 | abort(); |
| 626 | } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 627 | |
| 628 | AddToISelQueue(Arg); |
Scott Michel | a59d469 | 2008-02-23 18:41:37 +0000 | [diff] [blame^] | 629 | Opc = vtm->ldresult_ins; |
| 630 | if (vtm->ldresult_imm) { |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 631 | SDOperand Zero = CurDAG->getTargetConstant(0, VT); |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 632 | |
| 633 | AddToISelQueue(Zero); |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 634 | Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain); |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 635 | } else { |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 636 | Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain); |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 639 | Chain = SDOperand(Result, 1); |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 640 | AddToISelQueue(Chain); |
| 641 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 642 | return Result; |
Scott Michel | 053c1da | 2008-01-29 02:16:57 +0000 | [diff] [blame] | 643 | } else if (Opc == SPUISD::IndirectAddr) { |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 644 | SDOperand Op0 = Op.getOperand(0); |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 645 | if (Op0.getOpcode() == SPUISD::LDRESULT) { |
| 646 | /* || Op0.getOpcode() == SPUISD::AFormAddr) */ |
| 647 | // (IndirectAddr (LDRESULT, imm)) |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 648 | SDOperand Op1 = Op.getOperand(1); |
| 649 | MVT::ValueType VT = Op.getValueType(); |
| 650 | |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 651 | DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = "); |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 652 | DEBUG(Op.getOperand(0).Val->dump(CurDAG)); |
| 653 | DEBUG(cerr << "\nOp1 = "); |
| 654 | DEBUG(Op.getOperand(1).Val->dump(CurDAG)); |
| 655 | DEBUG(cerr << "\n"); |
| 656 | |
| 657 | if (Op1.getOpcode() == ISD::Constant) { |
| 658 | ConstantSDNode *CN = cast<ConstantSDNode>(Op1); |
| 659 | Op1 = CurDAG->getTargetConstant(CN->getValue(), VT); |
Scott Michel | 7f9ba9b | 2008-01-30 02:55:46 +0000 | [diff] [blame] | 660 | NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32); |
| 661 | AddToISelQueue(Op0); |
| 662 | AddToISelQueue(Op1); |
| 663 | Ops[0] = Op0; |
| 664 | Ops[1] = Op1; |
| 665 | n_ops = 2; |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 666 | } |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 667 | } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Scott Michel | 58c5818 | 2008-01-17 20:38:41 +0000 | [diff] [blame] | 670 | if (n_ops > 0) { |
| 671 | if (N->hasOneUse()) |
| 672 | return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops); |
| 673 | else |
| 674 | return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops); |
| 675 | } else |
| 676 | return SelectCode(Op); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /// createPPCISelDag - This pass converts a legalized DAG into a |
| 680 | /// SPU-specific DAG, ready for instruction scheduling. |
| 681 | /// |
| 682 | FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) { |
| 683 | return new SPUDAGToDAGISel(TM); |
| 684 | } |