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 | |
| 153 | //===------------------------------------------------------------------===// |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 154 | //! MVT::ValueType to "useful stuff" mapping structure: |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 155 | |
| 156 | struct valtype_map_s { |
| 157 | MVT::ValueType VT; |
| 158 | unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined) |
| 159 | int prefslot_byte; /// Byte offset of the "preferred" slot |
| 160 | unsigned brcc_eq_ins; /// br_cc equal instruction |
| 161 | unsigned brcc_neq_ins; /// br_cc not equal instruction |
| 162 | }; |
| 163 | |
| 164 | const valtype_map_s valtype_map[] = { |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 165 | { MVT::i1, 0, 3, 0, 0 }, |
| 166 | { MVT::i8, 0, 3, 0, 0 }, |
| 167 | { MVT::i16, SPU::ORHIr16, 2, SPU::BRHZ, SPU::BRHNZ }, |
| 168 | { MVT::i32, SPU::ORIr32, 0, SPU::BRZ, SPU::BRNZ }, |
| 169 | { MVT::i64, SPU::ORIr64, 0, 0, 0 }, |
| 170 | { MVT::f32, 0, 0, 0, 0 }, |
| 171 | { MVT::f64, 0, 0, 0, 0 } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]); |
| 175 | |
| 176 | const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT) |
| 177 | { |
| 178 | const valtype_map_s *retval = 0; |
| 179 | for (size_t i = 0; i < n_valtype_map; ++i) { |
| 180 | if (valtype_map[i].VT == VT) { |
| 181 | retval = valtype_map + i; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | #ifndef NDEBUG |
| 188 | if (retval == 0) { |
| 189 | cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for " |
| 190 | << MVT::getValueTypeString(VT) |
| 191 | << "\n"; |
| 192 | abort(); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | return retval; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | //===--------------------------------------------------------------------===// |
| 201 | /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine |
| 202 | /// instructions for SelectionDAG operations. |
| 203 | /// |
| 204 | class SPUDAGToDAGISel : |
| 205 | public SelectionDAGISel |
| 206 | { |
| 207 | SPUTargetMachine &TM; |
| 208 | SPUTargetLowering &SPUtli; |
| 209 | unsigned GlobalBaseReg; |
| 210 | |
| 211 | public: |
| 212 | SPUDAGToDAGISel(SPUTargetMachine &tm) : |
| 213 | SelectionDAGISel(*tm.getTargetLowering()), |
| 214 | TM(tm), |
| 215 | SPUtli(*tm.getTargetLowering()) |
| 216 | {} |
| 217 | |
| 218 | virtual bool runOnFunction(Function &Fn) { |
| 219 | // Make sure we re-emit a set of the global base reg if necessary |
| 220 | GlobalBaseReg = 0; |
| 221 | SelectionDAGISel::runOnFunction(Fn); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | /// getI32Imm - Return a target constant with the specified value, of type |
| 226 | /// i32. |
| 227 | inline SDOperand getI32Imm(uint32_t Imm) { |
| 228 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 229 | } |
| 230 | |
| 231 | /// getI64Imm - Return a target constant with the specified value, of type |
| 232 | /// i64. |
| 233 | inline SDOperand getI64Imm(uint64_t Imm) { |
| 234 | return CurDAG->getTargetConstant(Imm, MVT::i64); |
| 235 | } |
| 236 | |
| 237 | /// getSmallIPtrImm - Return a target constant of pointer type. |
| 238 | inline SDOperand getSmallIPtrImm(unsigned Imm) { |
| 239 | return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy()); |
| 240 | } |
| 241 | |
| 242 | /// Select - Convert the specified operand from a target-independent to a |
| 243 | /// target-specific node if it hasn't already been changed. |
| 244 | SDNode *Select(SDOperand Op); |
| 245 | |
| 246 | /// Return true if the address N is a RI7 format address [r+imm] |
| 247 | bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp, |
| 248 | SDOperand &Base); |
| 249 | |
| 250 | //! Returns true if the address N is an A-form (local store) address |
| 251 | bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 252 | SDOperand &Index); |
| 253 | |
| 254 | //! D-form address predicate |
| 255 | bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 256 | SDOperand &Index); |
| 257 | |
| 258 | //! Address predicate if N can be expressed as an indexed [r+r] operation. |
| 259 | bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 260 | SDOperand &Index); |
| 261 | |
| 262 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 263 | /// inline asm expressions. |
| 264 | virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op, |
| 265 | char ConstraintCode, |
| 266 | std::vector<SDOperand> &OutOps, |
| 267 | SelectionDAG &DAG) { |
| 268 | SDOperand Op0, Op1; |
| 269 | switch (ConstraintCode) { |
| 270 | default: return true; |
| 271 | case 'm': // memory |
| 272 | if (!SelectDFormAddr(Op, Op, Op0, Op1) |
| 273 | && !SelectAFormAddr(Op, Op, Op0, Op1)) |
| 274 | SelectXFormAddr(Op, Op, Op0, Op1); |
| 275 | break; |
| 276 | case 'o': // offsetable |
| 277 | if (!SelectDFormAddr(Op, Op, Op0, Op1) |
| 278 | && !SelectAFormAddr(Op, Op, Op0, Op1)) { |
| 279 | Op0 = Op; |
| 280 | AddToISelQueue(Op0); // r+0. |
| 281 | Op1 = getSmallIPtrImm(0); |
| 282 | } |
| 283 | break; |
| 284 | case 'v': // not offsetable |
| 285 | #if 1 |
| 286 | assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled."); |
| 287 | #else |
| 288 | SelectAddrIdxOnly(Op, Op, Op0, Op1); |
| 289 | #endif |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | OutOps.push_back(Op0); |
| 294 | OutOps.push_back(Op1); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 299 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 300 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 301 | |
| 302 | virtual const char *getPassName() const { |
| 303 | return "Cell SPU DAG->DAG Pattern Instruction Selection"; |
| 304 | } |
| 305 | |
| 306 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 307 | /// this target when scheduling the DAG. |
| 308 | virtual HazardRecognizer *CreateTargetHazardRecognizer() { |
| 309 | const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo(); |
| 310 | assert(II && "No InstrInfo?"); |
| 311 | return new SPUHazardRecognizer(*II); |
| 312 | } |
| 313 | |
| 314 | // Include the pieces autogenerated from the target description. |
| 315 | #include "SPUGenDAGISel.inc" |
| 316 | }; |
| 317 | |
| 318 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 319 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 320 | void |
| 321 | SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) |
| 322 | { |
| 323 | DEBUG(BB->dump()); |
| 324 | |
| 325 | // Select target instructions for the DAG. |
| 326 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
| 327 | DAG.RemoveDeadNodes(); |
| 328 | |
| 329 | // Emit machine code to BB. |
| 330 | ScheduleAndEmitDAG(DAG); |
| 331 | } |
| 332 | |
| 333 | bool |
| 334 | SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp, |
| 335 | SDOperand &Base) { |
| 336 | unsigned Opc = N.getOpcode(); |
| 337 | unsigned VT = N.getValueType(); |
| 338 | MVT::ValueType PtrVT = SPUtli.getPointerTy(); |
| 339 | ConstantSDNode *CN = 0; |
| 340 | int Imm; |
| 341 | |
| 342 | if (Opc == ISD::ADD) { |
| 343 | SDOperand Op0 = N.getOperand(0); |
| 344 | SDOperand Op1 = N.getOperand(1); |
| 345 | if (Op1.getOpcode() == ISD::Constant || |
| 346 | Op1.getOpcode() == ISD::TargetConstant) { |
| 347 | CN = cast<ConstantSDNode>(Op1); |
| 348 | Imm = int(CN->getValue()); |
| 349 | if (Imm <= 0xff) { |
| 350 | Disp = CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy()); |
| 351 | Base = Op0; |
| 352 | return true; |
| 353 | } |
| 354 | } |
| 355 | } else if (Opc == ISD::GlobalAddress |
| 356 | || Opc == ISD::TargetGlobalAddress |
| 357 | || Opc == ISD::Register) { |
| 358 | // Plain old local store address: |
| 359 | Disp = CurDAG->getTargetConstant(0, VT); |
| 360 | Base = N; |
| 361 | return true; |
| 362 | } else if (Opc == SPUISD::DFormAddr) { |
| 363 | // D-Form address: This is pretty straightforward, naturally... |
| 364 | CN = cast<ConstantSDNode>(N.getOperand(1)); |
| 365 | assert(CN != 0 && "SelectDFormAddr/SPUISD::DForm2Addr expecting constant"); |
| 366 | Imm = unsigned(CN->getValue()); |
| 367 | if (Imm < 0xff) { |
| 368 | Disp = CurDAG->getTargetConstant(CN->getValue(), PtrVT); |
| 369 | Base = N.getOperand(0); |
| 370 | return true; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | /*! |
| 378 | \arg Op The ISD instructio operand |
| 379 | \arg N The address to be tested |
| 380 | \arg Base The base address |
| 381 | \arg Index The base address index |
| 382 | */ |
| 383 | bool |
| 384 | SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 385 | SDOperand &Index) { |
| 386 | // These match the addr256k operand type: |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 387 | MVT::ValueType OffsVT = MVT::i16; |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 388 | MVT::ValueType PtrVT = SPUtli.getPointerTy(); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 389 | |
| 390 | switch (N.getOpcode()) { |
| 391 | case ISD::Constant: |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 392 | case ISD::ConstantPool: |
| 393 | case ISD::GlobalAddress: |
| 394 | cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n"; |
| 395 | abort(); |
| 396 | /*NOTREACHED*/ |
| 397 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 398 | case ISD::TargetConstant: { |
| 399 | // Loading from a constant address. |
| 400 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N); |
| 401 | int Imm = (int)CN->getValue(); |
| 402 | if (Imm < 0x3ffff && (Imm & 0x3) == 0) { |
| 403 | Base = CurDAG->getTargetConstant(Imm, PtrVT); |
| 404 | // Note that this operand will be ignored by the assembly printer... |
| 405 | Index = CurDAG->getTargetConstant(0, OffsVT); |
| 406 | return true; |
| 407 | } |
| 408 | } |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 409 | case ISD::TargetGlobalAddress: |
| 410 | case ISD::TargetConstantPool: |
| 411 | case SPUISD::AFormAddr: { |
| 412 | // The address is in Base. N is a dummy that will be ignored by |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 413 | // the assembly printer. |
| 414 | Base = N; |
| 415 | Index = CurDAG->getTargetConstant(0, OffsVT); |
| 416 | return true; |
| 417 | } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | /*! |
| 424 | \arg Op The ISD instruction (ignored) |
| 425 | \arg N The address to be tested |
| 426 | \arg Base Base address register/pointer |
| 427 | \arg Index Base address index |
| 428 | |
| 429 | Examine the input address by a base register plus a signed 10-bit |
| 430 | displacement, [r+I10] (D-form address). |
| 431 | |
| 432 | \return true if \a N is a D-form address with \a Base and \a Index set |
| 433 | to non-empty SDOperand instances. |
| 434 | */ |
| 435 | bool |
| 436 | SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 437 | SDOperand &Index) { |
| 438 | unsigned Opc = N.getOpcode(); |
| 439 | unsigned PtrTy = SPUtli.getPointerTy(); |
| 440 | |
| 441 | if (Opc == ISD::Register) { |
| 442 | Base = N; |
| 443 | Index = CurDAG->getTargetConstant(0, PtrTy); |
| 444 | return true; |
| 445 | } else if (Opc == ISD::FrameIndex) { |
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 | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 448 | << FI->getIndex() << "\n"); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 449 | if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) { |
| 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 |
| 456 | const SDOperand Op0 = N.getOperand(0); // Frame index/base |
| 457 | const SDOperand Op1 = N.getOperand(1); // Offset within base |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 458 | |
Scott Michel | 497e888 | 2008-01-11 21:01:19 +0000 | [diff] [blame^] | 459 | if ((Op1.getOpcode() == ISD::Constant |
| 460 | || Op1.getOpcode() == ISD::TargetConstant) |
| 461 | && Op0.getOpcode() != SPUISD::XFormAddr) { |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 462 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1); |
| 463 | assert(CN != 0 && "SelectDFormAddr: Expected a constant"); |
| 464 | |
| 465 | int32_t offset = (int32_t) CN->getSignExtended(); |
| 466 | unsigned Opc0 = Op0.getOpcode(); |
| 467 | |
| 468 | if ((offset & 0xf) != 0) { |
| 469 | // Unaligned offset: punt and let X-form address handle it. |
| 470 | // NOTE: This really doesn't have to be strictly 16-byte aligned, |
| 471 | // since the load/store quadword instructions will implicitly |
| 472 | // zero the lower 4 bits of the resulting address. |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | if (Opc0 == ISD::FrameIndex) { |
| 477 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op0); |
| 478 | DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset |
| 479 | << " frame index = " << FI->getIndex() << "\n"); |
| 480 | |
| 481 | if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) { |
| 482 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
| 483 | Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy); |
| 484 | return true; |
| 485 | } |
| 486 | } else if (offset > SPUFrameInfo::minFrameOffset() |
| 487 | && offset < SPUFrameInfo::maxFrameOffset()) { |
| 488 | Base = CurDAG->getTargetConstant(offset, PtrTy); |
| 489 | if (Opc0 == ISD::GlobalAddress) { |
| 490 | // Convert global address to target global address |
| 491 | GlobalAddressSDNode *GV = dyn_cast<GlobalAddressSDNode>(Op0); |
| 492 | Index = CurDAG->getTargetGlobalAddress(GV->getGlobal(), PtrTy); |
| 493 | return true; |
| 494 | } else { |
| 495 | // Otherwise, just take operand 0 |
| 496 | Index = Op0; |
| 497 | return true; |
| 498 | } |
| 499 | } |
| 500 | } else |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 501 | return false; |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 502 | } else if (Opc == SPUISD::DFormAddr) { |
Scott Michel | 497e888 | 2008-01-11 21:01:19 +0000 | [diff] [blame^] | 503 | // D-Form address: This is pretty straightforward, |
| 504 | // naturally... but make sure that this isn't a D-form address |
| 505 | // with a X-form address embedded within: |
| 506 | const SDOperand Op0 = N.getOperand(0); // Frame index/base |
| 507 | const SDOperand Op1 = N.getOperand(1); // Offset within base |
| 508 | |
| 509 | if (Op0.getOpcode() != SPUISD::XFormAddr) { |
| 510 | ConstantSDNode *CN = cast<ConstantSDNode>(Op1); |
| 511 | assert(CN != 0 && "SelectDFormAddr/SPUISD::DFormAddr expecting constant"); |
| 512 | Base = CurDAG->getTargetConstant(CN->getValue(), PtrTy); |
| 513 | Index = Op0; |
| 514 | return true; |
| 515 | } |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 516 | } else if (Opc == ISD::FrameIndex) { |
| 517 | // Stack frame index must be less than 512 (divided by 16): |
| 518 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N); |
| 519 | DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = " |
| 520 | << FI->getIndex() << "\n"); |
| 521 | if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) { |
| 522 | Base = CurDAG->getTargetConstant(0, PtrTy); |
| 523 | Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy); |
| 524 | return true; |
| 525 | } |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | /*! |
| 532 | \arg Op The ISD instruction operand |
| 533 | \arg N The address operand |
| 534 | \arg Base The base pointer operand |
| 535 | \arg Index The offset/index operand |
| 536 | |
| 537 | If the address \a N can be expressed as a [r + s10imm] address, returns false. |
| 538 | Otherwise, creates two operands, Base and Index that will become the [r+r] |
| 539 | address. |
| 540 | */ |
| 541 | bool |
| 542 | SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 543 | SDOperand &Index) { |
| 544 | if (SelectAFormAddr(Op, N, Base, Index) |
| 545 | || SelectDFormAddr(Op, N, Base, Index)) |
| 546 | return false; |
| 547 | |
| 548 | unsigned Opc = N.getOpcode(); |
| 549 | |
| 550 | if (Opc == ISD::ADD) { |
| 551 | SDOperand N1 = N.getOperand(0); |
| 552 | SDOperand N2 = N.getOperand(1); |
| 553 | unsigned N1Opc = N1.getOpcode(); |
| 554 | unsigned N2Opc = N2.getOpcode(); |
| 555 | |
| 556 | if ((N1Opc == SPUISD::Hi && N2Opc == SPUISD::Lo) |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 557 | || (N1Opc == SPUISD::Lo && N2Opc == SPUISD::Hi) |
| 558 | || (N1Opc == SPUISD::XFormAddr)) { |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 559 | Base = N.getOperand(0); |
| 560 | Index = N.getOperand(1); |
| 561 | return true; |
| 562 | } else { |
| 563 | cerr << "SelectXFormAddr: Unhandled ADD operands:\n"; |
| 564 | N1.Val->dump(); |
| 565 | cerr << "\n"; |
| 566 | N2.Val->dump(); |
| 567 | cerr << "\n"; |
| 568 | abort(); |
| 569 | /*UNREACHED*/ |
| 570 | } |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 571 | } else if (Opc == SPUISD::XFormAddr) { |
| 572 | Base = N; |
| 573 | Index = N.getOperand(1); |
| 574 | return true; |
Scott Michel | 497e888 | 2008-01-11 21:01:19 +0000 | [diff] [blame^] | 575 | } else if (Opc == SPUISD::DFormAddr) { |
| 576 | // Must be a D-form address with an X-form address embedded |
| 577 | // within: |
| 578 | Base = N.getOperand(0); |
| 579 | Index = N.getOperand(1); |
| 580 | return true; |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 581 | } else if (N.getNumOperands() == 2) { |
| 582 | SDOperand N1 = N.getOperand(0); |
| 583 | SDOperand N2 = N.getOperand(1); |
| 584 | unsigned N1Opc = N1.getOpcode(); |
| 585 | unsigned N2Opc = N2.getOpcode(); |
| 586 | |
| 587 | if ((N1Opc == ISD::CopyToReg || N1Opc == ISD::Register) |
| 588 | && (N2Opc == ISD::CopyToReg || N2Opc == ISD::Register)) { |
| 589 | Base = N.getOperand(0); |
| 590 | Index = N.getOperand(1); |
| 591 | return true; |
| 592 | /*UNREACHED*/ |
| 593 | } else { |
| 594 | cerr << "SelectXFormAddr: 2-operand unhandled operand:\n"; |
Scott Michel | 497e888 | 2008-01-11 21:01:19 +0000 | [diff] [blame^] | 595 | N.Val->dump(CurDAG); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 596 | cerr << "\n"; |
| 597 | abort(); |
| 598 | /*UNREACHED*/ |
| 599 | } |
| 600 | } else { |
| 601 | cerr << "SelectXFormAddr: Unhandled operand type:\n"; |
Scott Michel | 497e888 | 2008-01-11 21:01:19 +0000 | [diff] [blame^] | 602 | N.Val->dump(CurDAG); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 603 | cerr << "\n"; |
| 604 | abort(); |
| 605 | /*UNREACHED*/ |
| 606 | } |
| 607 | |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | //! Convert the operand from a target-independent to a target-specific node |
| 612 | /*! |
| 613 | */ |
| 614 | SDNode * |
| 615 | SPUDAGToDAGISel::Select(SDOperand Op) { |
| 616 | SDNode *N = Op.Val; |
| 617 | unsigned Opc = N->getOpcode(); |
| 618 | |
| 619 | if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) { |
| 620 | return NULL; // Already selected. |
| 621 | } else if (Opc == ISD::FrameIndex) { |
| 622 | // Selects to AIr32 FI, 0 which in turn will become AIr32 SP, imm. |
| 623 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 624 | MVT::ValueType PtrVT = SPUtli.getPointerTy(); |
| 625 | SDOperand Zero = CurDAG->getTargetConstant(0, PtrVT); |
| 626 | SDOperand TFI = CurDAG->getTargetFrameIndex(FI, PtrVT); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 627 | |
Scott Michel | 9999e68 | 2007-12-19 07:35:06 +0000 | [diff] [blame] | 628 | DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AI32 <FI>, 0\n"); |
Scott Michel | 9de5d0d | 2008-01-11 02:53:15 +0000 | [diff] [blame] | 629 | if (N->hasOneUse()) |
| 630 | return CurDAG->SelectNodeTo(N, SPU::AIr32, Op.getValueType(), TFI, Zero); |
| 631 | CurDAG->getTargetNode(SPU::AIr32, Op.getValueType(), TFI, Zero); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 632 | } else if (Opc == SPUISD::LDRESULT) { |
| 633 | // Custom select instructions for LDRESULT |
| 634 | unsigned VT = N->getValueType(0); |
| 635 | SDOperand Arg = N->getOperand(0); |
| 636 | SDOperand Chain = N->getOperand(1); |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 637 | SDNode *Result; |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 638 | |
| 639 | AddToISelQueue(Arg); |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 640 | if (!MVT::isFloatingPoint(VT)) { |
| 641 | SDOperand Zero = CurDAG->getTargetConstant(0, VT); |
| 642 | const valtype_map_s *vtm = getValueTypeMapEntry(VT); |
| 643 | |
| 644 | if (vtm->ldresult_ins == 0) { |
| 645 | cerr << "LDRESULT for unsupported type: " |
| 646 | << MVT::getValueTypeString(VT) |
| 647 | << "\n"; |
| 648 | abort(); |
| 649 | } else |
| 650 | Opc = vtm->ldresult_ins; |
| 651 | |
| 652 | AddToISelQueue(Zero); |
| 653 | Result = CurDAG->SelectNodeTo(N, Opc, VT, MVT::Other, Arg, Zero, Chain); |
| 654 | } else { |
| 655 | Result = |
| 656 | CurDAG->SelectNodeTo(N, (VT == MVT::f32 ? SPU::ORf32 : SPU::ORf64), |
| 657 | MVT::Other, Arg, Arg, Chain); |
| 658 | } |
| 659 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 660 | Chain = SDOperand(Result, 1); |
Scott Michel | 86c041f | 2007-12-20 00:44:13 +0000 | [diff] [blame] | 661 | AddToISelQueue(Chain); |
| 662 | |
Scott Michel | 266bc8f | 2007-12-04 22:23:35 +0000 | [diff] [blame] | 663 | return Result; |
| 664 | } |
| 665 | |
| 666 | return SelectCode(Op); |
| 667 | } |
| 668 | |
| 669 | /// createPPCISelDag - This pass converts a legalized DAG into a |
| 670 | /// SPU-specific DAG, ready for instruction scheduling. |
| 671 | /// |
| 672 | FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) { |
| 673 | return new SPUDAGToDAGISel(TM); |
| 674 | } |