1 //===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for the Cell SPU,
11 // converting from a legalized dag to a SPU-target dag.
13 //===----------------------------------------------------------------------===//
16 #include "SPUTargetMachine.h"
17 #include "SPUHazardRecognizers.h"
18 #include "SPUFrameInfo.h"
19 #include "SPURegisterNames.h"
20 #include "SPUTargetMachine.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/CodeGen/PseudoSourceValue.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Constants.h"
30 #include "llvm/GlobalValue.h"
31 #include "llvm/Intrinsics.h"
32 #include "llvm/LLVMContext.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/raw_ostream.h"
42 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
44 isI64IntS10Immediate(ConstantSDNode *CN)
46 return isInt<10>(CN->getSExtValue());
49 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
51 isI32IntS10Immediate(ConstantSDNode *CN)
53 return isInt<10>(CN->getSExtValue());
56 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
58 isI32IntU10Immediate(ConstantSDNode *CN)
60 return isUInt<10>(CN->getSExtValue());
63 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
65 isI16IntS10Immediate(ConstantSDNode *CN)
67 return isInt<10>(CN->getSExtValue());
70 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
72 isI16IntU10Immediate(ConstantSDNode *CN)
74 return isUInt<10>((short) CN->getZExtValue());
77 //! ConstantSDNode predicate for signed 16-bit values
79 \arg CN The constant SelectionDAG node holding the value
80 \arg Imm The returned 16-bit value, if returning true
82 This predicate tests the value in \a CN to see whether it can be
83 represented as a 16-bit, sign-extended quantity. Returns true if
87 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
89 EVT vt = CN->getValueType(0);
90 Imm = (short) CN->getZExtValue();
91 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
93 } else if (vt == MVT::i32) {
94 int32_t i_val = (int32_t) CN->getZExtValue();
95 short s_val = (short) i_val;
96 return i_val == s_val;
98 int64_t i_val = (int64_t) CN->getZExtValue();
99 short s_val = (short) i_val;
100 return i_val == s_val;
106 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
108 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
110 EVT vt = FPN->getValueType(0);
111 if (vt == MVT::f32) {
112 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
113 int sval = (int) ((val << 16) >> 16);
121 //===------------------------------------------------------------------===//
122 //! EVT to "useful stuff" mapping structure:
124 struct valtype_map_s {
126 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
127 bool ldresult_imm; /// LDRESULT instruction requires immediate?
128 unsigned lrinst; /// LR instruction
131 const valtype_map_s valtype_map[] = {
132 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
133 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
134 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
135 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
136 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
137 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
138 // vector types... (sigh!)
139 { MVT::v16i8, 0, false, SPU::LRv16i8 },
140 { MVT::v8i16, 0, false, SPU::LRv8i16 },
141 { MVT::v4i32, 0, false, SPU::LRv4i32 },
142 { MVT::v2i64, 0, false, SPU::LRv2i64 },
143 { MVT::v4f32, 0, false, SPU::LRv4f32 },
144 { MVT::v2f64, 0, false, SPU::LRv2f64 }
147 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
149 const valtype_map_s *getValueTypeMapEntry(EVT VT)
151 const valtype_map_s *retval = 0;
152 for (size_t i = 0; i < n_valtype_map; ++i) {
153 if (valtype_map[i].VT == VT) {
154 retval = valtype_map + i;
162 report_fatal_error("SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns"
163 "NULL for " + Twine(VT.getEVTString()));
170 //! Generate the carry-generate shuffle mask.
171 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
172 SmallVector<SDValue, 16 > ShufBytes;
174 // Create the shuffle mask for "rotating" the borrow up one register slot
175 // once the borrow is generated.
176 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
177 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
178 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
179 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
181 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
182 &ShufBytes[0], ShufBytes.size());
185 //! Generate the borrow-generate shuffle mask
186 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
187 SmallVector<SDValue, 16 > ShufBytes;
189 // Create the shuffle mask for "rotating" the borrow up one register slot
190 // once the borrow is generated.
191 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
192 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
193 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
194 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
196 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
197 &ShufBytes[0], ShufBytes.size());
200 //===------------------------------------------------------------------===//
201 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
202 /// instructions for SelectionDAG operations.
204 class SPUDAGToDAGISel :
205 public SelectionDAGISel
207 const SPUTargetMachine &TM;
208 const SPUTargetLowering &SPUtli;
209 unsigned GlobalBaseReg;
212 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
213 SelectionDAGISel(tm),
215 SPUtli(*tm.getTargetLowering())
218 virtual bool runOnMachineFunction(MachineFunction &MF) {
219 // Make sure we re-emit a set of the global base reg if necessary
221 SelectionDAGISel::runOnMachineFunction(MF);
225 /// getI32Imm - Return a target constant with the specified value, of type
227 inline SDValue getI32Imm(uint32_t Imm) {
228 return CurDAG->getTargetConstant(Imm, MVT::i32);
231 /// getI64Imm - Return a target constant with the specified value, of type
233 inline SDValue getI64Imm(uint64_t Imm) {
234 return CurDAG->getTargetConstant(Imm, MVT::i64);
237 /// getSmallIPtrImm - Return a target constant of pointer type.
238 inline SDValue getSmallIPtrImm(unsigned Imm) {
239 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
242 SDNode *emitBuildVector(SDNode *bvNode) {
243 EVT vecVT = bvNode->getValueType(0);
244 DebugLoc dl = bvNode->getDebugLoc();
246 // Check to see if this vector can be represented as a CellSPU immediate
247 // constant by invoking all of the instruction selection predicates:
248 if (((vecVT == MVT::v8i16) &&
249 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
250 ((vecVT == MVT::v4i32) &&
251 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
252 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
253 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
254 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
255 ((vecVT == MVT::v2i64) &&
256 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
257 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
258 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
259 HandleSDNode Dummy(SDValue(bvNode, 0));
260 if (SDNode *N = Select(bvNode))
262 return Dummy.getValue().getNode();
265 // No, need to emit a constant pool spill:
266 std::vector<Constant*> CV;
268 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
269 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
270 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
273 const Constant *CP = ConstantVector::get(CV);
274 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
275 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
276 SDValue CGPoolOffset =
277 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
279 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
280 CurDAG->getEntryNode(), CGPoolOffset,
281 PseudoSourceValue::getConstantPool(),0,
282 false, false, Alignment));
283 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
284 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
286 return Dummy.getValue().getNode();
289 /// Select - Convert the specified operand from a target-independent to a
290 /// target-specific node if it hasn't already been changed.
291 SDNode *Select(SDNode *N);
293 //! Emit the instruction sequence for i64 shl
294 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
296 //! Emit the instruction sequence for i64 srl
297 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
299 //! Emit the instruction sequence for i64 sra
300 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
302 //! Emit the necessary sequence for loading i64 constants:
303 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
305 //! Alternate instruction emit sequence for loading i64 constants
306 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
308 //! Returns true if the address N is an A-form (local store) address
309 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
312 //! D-form address predicate
313 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
316 /// Alternate D-form address using i7 offset predicate
317 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
320 /// D-form address selection workhorse
321 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
322 SDValue &Base, int minOffset, int maxOffset);
324 //! Address predicate if N can be expressed as an indexed [r+r] operation.
325 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
328 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
329 /// inline asm expressions.
330 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
332 std::vector<SDValue> &OutOps) {
334 switch (ConstraintCode) {
335 default: return true;
337 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
338 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
339 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
341 case 'o': // offsetable
342 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
343 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
345 Op1 = getSmallIPtrImm(0);
348 case 'v': // not offsetable
350 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
352 SelectAddrIdxOnly(Op, Op, Op0, Op1);
357 OutOps.push_back(Op0);
358 OutOps.push_back(Op1);
362 virtual const char *getPassName() const {
363 return "Cell SPU DAG->DAG Pattern Instruction Selection";
366 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
367 /// this target when scheduling the DAG.
368 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
369 const TargetInstrInfo *II = TM.getInstrInfo();
370 assert(II && "No InstrInfo?");
371 return new SPUHazardRecognizer(*II);
374 // Include the pieces autogenerated from the target description.
375 #include "SPUGenDAGISel.inc"
380 \arg Op The ISD instruction operand
381 \arg N The address to be tested
382 \arg Base The base address
383 \arg Index The base address index
386 SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
388 // These match the addr256k operand type:
389 EVT OffsVT = MVT::i16;
390 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
392 switch (N.getOpcode()) {
394 case ISD::ConstantPool:
395 case ISD::GlobalAddress:
396 report_fatal_error("SPU SelectAFormAddr: Constant/Pool/Global not lowered.");
399 case ISD::TargetConstant:
400 case ISD::TargetGlobalAddress:
401 case ISD::TargetJumpTable:
402 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
403 "not wrapped as A-form address.");
406 case SPUISD::AFormAddr:
407 // Just load from memory if there's only a single use of the location,
408 // otherwise, this will get handled below with D-form offset addresses
410 SDValue Op0 = N.getOperand(0);
411 switch (Op0.getOpcode()) {
412 case ISD::TargetConstantPool:
413 case ISD::TargetJumpTable:
418 case ISD::TargetGlobalAddress: {
419 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
420 const GlobalValue *GV = GSDN->getGlobal();
421 if (GV->getAlignment() == 16) {
436 SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
438 const int minDForm2Offset = -(1 << 7);
439 const int maxDForm2Offset = (1 << 7) - 1;
440 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
445 \arg Op The ISD instruction (ignored)
446 \arg N The address to be tested
447 \arg Base Base address register/pointer
448 \arg Index Base address index
450 Examine the input address by a base register plus a signed 10-bit
451 displacement, [r+I10] (D-form address).
453 \return true if \a N is a D-form address with \a Base and \a Index set
454 to non-empty SDValue instances.
457 SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
459 return DFormAddressPredicate(Op, N, Base, Index,
460 SPUFrameInfo::minFrameOffset(),
461 SPUFrameInfo::maxFrameOffset());
465 SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
466 SDValue &Index, int minOffset,
468 unsigned Opc = N.getOpcode();
469 EVT PtrTy = SPUtli.getPointerTy();
471 if (Opc == ISD::FrameIndex) {
472 // Stack frame index must be less than 512 (divided by 16):
473 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
474 int FI = int(FIN->getIndex());
475 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
477 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
478 Base = CurDAG->getTargetConstant(0, PtrTy);
479 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
482 } else if (Opc == ISD::ADD) {
483 // Generated by getelementptr
484 const SDValue Op0 = N.getOperand(0);
485 const SDValue Op1 = N.getOperand(1);
487 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
488 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
489 Base = CurDAG->getTargetConstant(0, PtrTy);
492 } else if (Op1.getOpcode() == ISD::Constant
493 || Op1.getOpcode() == ISD::TargetConstant) {
494 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
495 int32_t offset = int32_t(CN->getSExtValue());
497 if (Op0.getOpcode() == ISD::FrameIndex) {
498 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
499 int FI = int(FIN->getIndex());
500 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
501 << " frame index = " << FI << "\n");
503 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
504 Base = CurDAG->getTargetConstant(offset, PtrTy);
505 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
508 } else if (offset > minOffset && offset < maxOffset) {
509 Base = CurDAG->getTargetConstant(offset, PtrTy);
513 } else if (Op0.getOpcode() == ISD::Constant
514 || Op0.getOpcode() == ISD::TargetConstant) {
515 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
516 int32_t offset = int32_t(CN->getSExtValue());
518 if (Op1.getOpcode() == ISD::FrameIndex) {
519 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
520 int FI = int(FIN->getIndex());
521 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
522 << " frame index = " << FI << "\n");
524 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
525 Base = CurDAG->getTargetConstant(offset, PtrTy);
526 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
529 } else if (offset > minOffset && offset < maxOffset) {
530 Base = CurDAG->getTargetConstant(offset, PtrTy);
535 } else if (Opc == SPUISD::IndirectAddr) {
536 // Indirect with constant offset -> D-Form address
537 const SDValue Op0 = N.getOperand(0);
538 const SDValue Op1 = N.getOperand(1);
540 if (Op0.getOpcode() == SPUISD::Hi
541 && Op1.getOpcode() == SPUISD::Lo) {
542 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
543 Base = CurDAG->getTargetConstant(0, PtrTy);
546 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
550 if (isa<ConstantSDNode>(Op1)) {
551 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
552 offset = int32_t(CN->getSExtValue());
554 } else if (isa<ConstantSDNode>(Op0)) {
555 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
556 offset = int32_t(CN->getSExtValue());
560 if (offset >= minOffset && offset <= maxOffset) {
561 Base = CurDAG->getTargetConstant(offset, PtrTy);
566 } else if (Opc == SPUISD::AFormAddr) {
567 Base = CurDAG->getTargetConstant(0, N.getValueType());
570 } else if (Opc == SPUISD::LDRESULT) {
571 Base = CurDAG->getTargetConstant(0, N.getValueType());
574 } else if (Opc == ISD::Register
575 ||Opc == ISD::CopyFromReg
577 ||Opc == ISD::Constant) {
578 unsigned OpOpc = Op->getOpcode();
580 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
581 // Direct load/store without getelementptr
584 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
586 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
587 if (Offs.getOpcode() == ISD::UNDEF)
588 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
595 /* If otherwise unadorned, default to D-form address with 0 offset: */
596 if (Opc == ISD::CopyFromReg) {
597 Index = N.getOperand(1);
602 Base = CurDAG->getTargetConstant(0, Index.getValueType());
611 \arg Op The ISD instruction operand
612 \arg N The address operand
613 \arg Base The base pointer operand
614 \arg Index The offset/index operand
616 If the address \a N can be expressed as an A-form or D-form address, returns
617 false. Otherwise, creates two operands, Base and Index that will become the
618 (r)(r) X-form address.
621 SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
623 if (!SelectAFormAddr(Op, N, Base, Index)
624 && !SelectDFormAddr(Op, N, Base, Index)) {
625 // If the address is neither A-form or D-form, punt and use an X-form
627 Base = N.getOperand(1);
628 Index = N.getOperand(0);
635 //! Convert the operand from a target-independent to a target-specific node
639 SPUDAGToDAGISel::Select(SDNode *N) {
640 unsigned Opc = N->getOpcode();
643 EVT OpVT = N->getValueType(0);
645 DebugLoc dl = N->getDebugLoc();
647 if (N->isMachineOpcode())
648 return NULL; // Already selected.
650 if (Opc == ISD::FrameIndex) {
651 int FI = cast<FrameIndexSDNode>(N)->getIndex();
652 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
653 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
662 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
663 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
664 N->getValueType(0), TFI, Imm0),
668 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
669 // Catch the i64 constants that end up here. Note: The backend doesn't
670 // attempt to legalize the constant (it's useless because DAGCombiner
671 // will insert 64-bit constants and we can't stop it).
672 return SelectI64Constant(N, OpVT, N->getDebugLoc());
673 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
674 && OpVT == MVT::i64) {
675 SDValue Op0 = N->getOperand(0);
676 EVT Op0VT = Op0.getValueType();
677 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
678 Op0VT, (128 / Op0VT.getSizeInBits()));
679 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
680 OpVT, (128 / OpVT.getSizeInBits()));
683 switch (Op0VT.getSimpleVT().SimpleTy) {
685 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
688 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
689 CurDAG->getConstant(0x80808080, MVT::i32),
690 CurDAG->getConstant(0x00010203, MVT::i32),
691 CurDAG->getConstant(0x80808080, MVT::i32),
692 CurDAG->getConstant(0x08090a0b, MVT::i32));
696 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
697 CurDAG->getConstant(0x80808080, MVT::i32),
698 CurDAG->getConstant(0x80800203, MVT::i32),
699 CurDAG->getConstant(0x80808080, MVT::i32),
700 CurDAG->getConstant(0x80800a0b, MVT::i32));
704 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
705 CurDAG->getConstant(0x80808080, MVT::i32),
706 CurDAG->getConstant(0x80808003, MVT::i32),
707 CurDAG->getConstant(0x80808080, MVT::i32),
708 CurDAG->getConstant(0x8080800b, MVT::i32));
712 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
714 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
718 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
719 PromScalar = SDValue(N, 0);
721 PromScalar = PromoteScalar.getValue();
723 SDValue zextShuffle =
724 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
725 PromScalar, PromScalar,
726 SDValue(shufMaskLoad, 0));
728 HandleSDNode Dummy2(zextShuffle);
729 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
730 zextShuffle = SDValue(N, 0);
732 zextShuffle = Dummy2.getValue();
733 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
736 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
737 SelectCode(Dummy.getValue().getNode());
738 return Dummy.getValue().getNode();
739 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
741 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
743 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
744 N->getOperand(0), N->getOperand(1),
745 SDValue(CGLoad, 0)));
747 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
748 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
750 return Dummy.getValue().getNode();
751 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
753 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
755 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
756 N->getOperand(0), N->getOperand(1),
757 SDValue(CGLoad, 0)));
759 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
760 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
762 return Dummy.getValue().getNode();
763 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
765 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
767 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
768 N->getOperand(0), N->getOperand(1),
769 SDValue(CGLoad, 0)));
770 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
771 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
773 return Dummy.getValue().getNode();
774 } else if (Opc == ISD::TRUNCATE) {
775 SDValue Op0 = N->getOperand(0);
776 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
778 && Op0.getValueType() == MVT::i64) {
779 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
781 // Take advantage of the fact that the upper 32 bits are in the
782 // i32 preferred slot and avoid shuffle gymnastics:
783 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
785 unsigned shift_amt = unsigned(CN->getZExtValue());
787 if (shift_amt >= 32) {
789 CurDAG->getMachineNode(SPU::ORr32_r64, dl, OpVT,
794 // Take care of the additional shift, if present:
795 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
796 unsigned Opc = SPU::ROTMAIr32_i32;
798 if (Op0.getOpcode() == ISD::SRL)
801 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
809 } else if (Opc == ISD::SHL) {
810 if (OpVT == MVT::i64)
811 return SelectSHLi64(N, OpVT);
812 } else if (Opc == ISD::SRL) {
813 if (OpVT == MVT::i64)
814 return SelectSRLi64(N, OpVT);
815 } else if (Opc == ISD::SRA) {
816 if (OpVT == MVT::i64)
817 return SelectSRAi64(N, OpVT);
818 } else if (Opc == ISD::FNEG
819 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
820 DebugLoc dl = N->getDebugLoc();
821 // Check if the pattern is a special form of DFNMS:
822 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
823 SDValue Op0 = N->getOperand(0);
824 if (Op0.getOpcode() == ISD::FSUB) {
825 SDValue Op00 = Op0.getOperand(0);
826 if (Op00.getOpcode() == ISD::FMUL) {
827 unsigned Opc = SPU::DFNMSf64;
828 if (OpVT == MVT::v2f64)
829 Opc = SPU::DFNMSv2f64;
831 return CurDAG->getMachineNode(Opc, dl, OpVT,
838 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
839 SDNode *signMask = 0;
840 unsigned Opc = SPU::XORfneg64;
842 if (OpVT == MVT::f64) {
843 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
844 } else if (OpVT == MVT::v2f64) {
845 Opc = SPU::XORfnegvec;
846 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
848 negConst, negConst).getNode());
851 return CurDAG->getMachineNode(Opc, dl, OpVT,
852 N->getOperand(0), SDValue(signMask, 0));
853 } else if (Opc == ISD::FABS) {
854 if (OpVT == MVT::f64) {
855 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
856 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
857 N->getOperand(0), SDValue(signMask, 0));
858 } else if (OpVT == MVT::v2f64) {
859 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
860 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
862 SDNode *signMask = emitBuildVector(absVec.getNode());
863 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
864 N->getOperand(0), SDValue(signMask, 0));
866 } else if (Opc == SPUISD::LDRESULT) {
867 // Custom select instructions for LDRESULT
868 EVT VT = N->getValueType(0);
869 SDValue Arg = N->getOperand(0);
870 SDValue Chain = N->getOperand(1);
872 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
874 if (vtm->ldresult_ins == 0) {
875 report_fatal_error("LDRESULT for unsupported type: " +
876 Twine(VT.getEVTString()));
879 Opc = vtm->ldresult_ins;
880 if (vtm->ldresult_imm) {
881 SDValue Zero = CurDAG->getTargetConstant(0, VT);
883 Result = CurDAG->getMachineNode(Opc, dl, VT, MVT::Other, Arg, Zero, Chain);
885 Result = CurDAG->getMachineNode(Opc, dl, VT, MVT::Other, Arg, Arg, Chain);
889 } else if (Opc == SPUISD::IndirectAddr) {
890 // Look at the operands: SelectCode() will catch the cases that aren't
891 // specifically handled here.
893 // SPUInstrInfo catches the following patterns:
894 // (SPUindirect (SPUhi ...), (SPUlo ...))
895 // (SPUindirect $sp, imm)
896 EVT VT = N->getValueType(0);
897 SDValue Op0 = N->getOperand(0);
898 SDValue Op1 = N->getOperand(1);
901 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
902 || (Op0.getOpcode() == ISD::Register
903 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
904 && RN->getReg() != SPU::R1))) {
907 if (Op1.getOpcode() == ISD::Constant) {
908 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
909 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
910 if (isInt<10>(CN->getSExtValue())) {
914 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
927 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
929 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
931 return SelectCode(N);
935 * Emit the instruction sequence for i64 left shifts. The basic algorithm
936 * is to fill the bottom two word slots with zeros so that zeros are shifted
937 * in as the entire quadword is shifted left.
939 * \note This code could also be used to implement v2i64 shl.
941 * @param Op The shl operand
942 * @param OpVT Op's machine value value type (doesn't need to be passed, but
943 * makes life easier.)
944 * @return The SDNode with the entire instruction sequence
947 SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
948 SDValue Op0 = N->getOperand(0);
949 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
950 OpVT, (128 / OpVT.getSizeInBits()));
951 SDValue ShiftAmt = N->getOperand(1);
952 EVT ShiftAmtVT = ShiftAmt.getValueType();
953 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
955 DebugLoc dl = N->getDebugLoc();
957 VecOp0 = CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
958 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
959 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
960 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
961 CurDAG->getTargetConstant(0, OpVT));
962 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
963 SDValue(ZeroFill, 0),
965 SDValue(SelMask, 0));
967 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
968 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
969 unsigned bits = unsigned(CN->getZExtValue()) & 7;
973 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
975 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
980 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
981 SDValue((Shift != 0 ? Shift : VecOp0), 0),
982 CurDAG->getTargetConstant(bits, ShiftAmtVT));
986 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
988 CurDAG->getTargetConstant(3, ShiftAmtVT));
990 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
992 CurDAG->getTargetConstant(7, ShiftAmtVT));
994 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
995 SDValue(VecOp0, 0), SDValue(Bytes, 0));
997 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
998 SDValue(Shift, 0), SDValue(Bits, 0));
1001 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
1005 * Emit the instruction sequence for i64 logical right shifts.
1007 * @param Op The shl operand
1008 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1009 * makes life easier.)
1010 * @return The SDNode with the entire instruction sequence
1013 SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
1014 SDValue Op0 = N->getOperand(0);
1015 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1016 OpVT, (128 / OpVT.getSizeInBits()));
1017 SDValue ShiftAmt = N->getOperand(1);
1018 EVT ShiftAmtVT = ShiftAmt.getValueType();
1019 SDNode *VecOp0, *Shift = 0;
1020 DebugLoc dl = N->getDebugLoc();
1022 VecOp0 = CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
1024 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1025 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1026 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1030 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
1032 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1037 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1038 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1039 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1043 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1045 CurDAG->getTargetConstant(3, ShiftAmtVT));
1047 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1049 CurDAG->getTargetConstant(7, ShiftAmtVT));
1051 // Ensure that the shift amounts are negated!
1052 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1054 CurDAG->getTargetConstant(0, ShiftAmtVT));
1056 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1058 CurDAG->getTargetConstant(0, ShiftAmtVT));
1061 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1062 SDValue(VecOp0, 0), SDValue(Bytes, 0));
1064 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1065 SDValue(Shift, 0), SDValue(Bits, 0));
1068 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
1072 * Emit the instruction sequence for i64 arithmetic right shifts.
1074 * @param Op The shl operand
1075 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1076 * makes life easier.)
1077 * @return The SDNode with the entire instruction sequence
1080 SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
1081 // Promote Op0 to vector
1082 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1083 OpVT, (128 / OpVT.getSizeInBits()));
1084 SDValue ShiftAmt = N->getOperand(1);
1085 EVT ShiftAmtVT = ShiftAmt.getValueType();
1086 DebugLoc dl = N->getDebugLoc();
1089 CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, N->getOperand(0));
1091 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1093 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1094 SDValue(VecOp0, 0), SignRotAmt);
1095 SDNode *UpperHalfSign =
1096 CurDAG->getMachineNode(SPU::ORi32_v4i32, dl, MVT::i32, SDValue(SignRot, 0));
1098 SDNode *UpperHalfSignMask =
1099 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
1100 SDNode *UpperLowerMask =
1101 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1102 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1103 SDNode *UpperLowerSelect =
1104 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1105 SDValue(UpperHalfSignMask, 0),
1107 SDValue(UpperLowerMask, 0));
1111 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1112 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1113 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1118 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1119 SDValue(UpperLowerSelect, 0),
1120 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1126 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1127 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1128 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1132 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1133 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1136 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1137 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1139 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1140 SDValue(Shift, 0), SDValue(NegShift, 0));
1143 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
1147 Do the necessary magic necessary to load a i64 constant
1149 SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
1151 ConstantSDNode *CN = cast<ConstantSDNode>(N);
1152 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1155 SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
1157 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
1159 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
1161 // Here's where it gets interesting, because we have to parse out the
1162 // subtree handed back in i64vec:
1164 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1165 // The degenerate case where the upper and lower bits in the splat are
1167 SDValue Op0 = i64vec.getOperand(0);
1169 ReplaceUses(i64vec, Op0);
1170 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT,
1171 SDValue(emitBuildVector(Op0.getNode()), 0));
1172 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1173 SDValue lhs = i64vec.getOperand(0);
1174 SDValue rhs = i64vec.getOperand(1);
1175 SDValue shufmask = i64vec.getOperand(2);
1177 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1178 ReplaceUses(lhs, lhs.getOperand(0));
1179 lhs = lhs.getOperand(0);
1182 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1184 : emitBuildVector(lhs.getNode()));
1186 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1187 ReplaceUses(rhs, rhs.getOperand(0));
1188 rhs = rhs.getOperand(0);
1191 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1193 : emitBuildVector(rhs.getNode()));
1195 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1196 ReplaceUses(shufmask, shufmask.getOperand(0));
1197 shufmask = shufmask.getOperand(0);
1200 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1201 ? shufmask.getNode()
1202 : emitBuildVector(shufmask.getNode()));
1205 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
1206 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1207 SDValue(shufMaskNode, 0));
1208 HandleSDNode Dummy(shufNode);
1209 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1210 if (SN == 0) SN = Dummy.getValue().getNode();
1212 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(SN, 0));
1213 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
1214 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT,
1215 SDValue(emitBuildVector(i64vec.getNode()), 0));
1217 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
1222 /// createSPUISelDag - This pass converts a legalized DAG into a
1223 /// SPU-specific DAG, ready for instruction scheduling.
1225 FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1226 return new SPUDAGToDAGISel(TM);