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 "SPUFrameLowering.h"
19 #include "SPUTargetMachine.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFunction.h"
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/LLVMContext.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/raw_ostream.h"
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
42 isI32IntS10Immediate(ConstantSDNode *CN)
44 return isInt<10>(CN->getSExtValue());
47 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
49 isI32IntU10Immediate(ConstantSDNode *CN)
51 return isUInt<10>(CN->getSExtValue());
54 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
56 isI16IntS10Immediate(ConstantSDNode *CN)
58 return isInt<10>(CN->getSExtValue());
61 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
63 isI16IntU10Immediate(ConstantSDNode *CN)
65 return isUInt<10>((short) CN->getZExtValue());
68 //! ConstantSDNode predicate for signed 16-bit values
70 \param CN The constant SelectionDAG node holding the value
71 \param Imm The returned 16-bit value, if returning true
73 This predicate tests the value in \a CN to see whether it can be
74 represented as a 16-bit, sign-extended quantity. Returns true if
78 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
80 EVT vt = CN->getValueType(0);
81 Imm = (short) CN->getZExtValue();
82 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
84 } else if (vt == MVT::i32) {
85 int32_t i_val = (int32_t) CN->getZExtValue();
86 return i_val == SignExtend32<16>(i_val);
88 int64_t i_val = (int64_t) CN->getZExtValue();
89 return i_val == SignExtend64<16>(i_val);
93 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
95 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
97 EVT vt = FPN->getValueType(0);
99 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
100 if (val == SignExtend32<16>(val)) {
109 //! Generate the carry-generate shuffle mask.
110 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
111 SmallVector<SDValue, 16 > ShufBytes;
113 // Create the shuffle mask for "rotating" the borrow up one register slot
114 // once the borrow is generated.
115 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
116 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
117 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
118 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
120 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
121 &ShufBytes[0], ShufBytes.size());
124 //! Generate the borrow-generate shuffle mask
125 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
126 SmallVector<SDValue, 16 > ShufBytes;
128 // Create the shuffle mask for "rotating" the borrow up one register slot
129 // once the borrow is generated.
130 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
131 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
132 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
133 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
135 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
136 &ShufBytes[0], ShufBytes.size());
139 //===------------------------------------------------------------------===//
140 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
141 /// instructions for SelectionDAG operations.
143 class SPUDAGToDAGISel :
144 public SelectionDAGISel
146 const SPUTargetMachine &TM;
147 const SPUTargetLowering &SPUtli;
148 unsigned GlobalBaseReg;
151 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
152 SelectionDAGISel(tm),
154 SPUtli(*tm.getTargetLowering())
157 virtual bool runOnMachineFunction(MachineFunction &MF) {
158 // Make sure we re-emit a set of the global base reg if necessary
160 SelectionDAGISel::runOnMachineFunction(MF);
164 /// getI32Imm - Return a target constant with the specified value, of type
166 inline SDValue getI32Imm(uint32_t Imm) {
167 return CurDAG->getTargetConstant(Imm, MVT::i32);
170 /// getSmallIPtrImm - Return a target constant of pointer type.
171 inline SDValue getSmallIPtrImm(unsigned Imm) {
172 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
175 SDNode *emitBuildVector(SDNode *bvNode) {
176 EVT vecVT = bvNode->getValueType(0);
177 DebugLoc dl = bvNode->getDebugLoc();
179 // Check to see if this vector can be represented as a CellSPU immediate
180 // constant by invoking all of the instruction selection predicates:
181 if (((vecVT == MVT::v8i16) &&
182 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
183 ((vecVT == MVT::v4i32) &&
184 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
185 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
186 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
187 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
188 ((vecVT == MVT::v2i64) &&
189 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
190 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
191 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
192 HandleSDNode Dummy(SDValue(bvNode, 0));
193 if (SDNode *N = Select(bvNode))
195 return Dummy.getValue().getNode();
198 // No, need to emit a constant pool spill:
199 std::vector<Constant*> CV;
201 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
202 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
203 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
206 const Constant *CP = ConstantVector::get(CV);
207 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
208 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
209 SDValue CGPoolOffset =
210 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
212 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
213 CurDAG->getEntryNode(), CGPoolOffset,
214 MachinePointerInfo::getConstantPool(),
215 false, false, false, Alignment));
216 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
217 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
219 return Dummy.getValue().getNode();
222 /// Select - Convert the specified operand from a target-independent to a
223 /// target-specific node if it hasn't already been changed.
224 SDNode *Select(SDNode *N);
226 //! Emit the instruction sequence for i64 shl
227 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
229 //! Emit the instruction sequence for i64 srl
230 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
232 //! Emit the instruction sequence for i64 sra
233 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
235 //! Emit the necessary sequence for loading i64 constants:
236 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
238 //! Alternate instruction emit sequence for loading i64 constants
239 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
241 //! Returns true if the address N is an A-form (local store) address
242 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
245 //! D-form address predicate
246 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
249 /// Alternate D-form address using i7 offset predicate
250 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
253 /// D-form address selection workhorse
254 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
255 SDValue &Base, int minOffset, int maxOffset);
257 //! Address predicate if N can be expressed as an indexed [r+r] operation.
258 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
261 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
262 /// inline asm expressions.
263 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
265 std::vector<SDValue> &OutOps) {
267 switch (ConstraintCode) {
268 default: return true;
270 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
271 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
272 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
274 case 'o': // offsetable
275 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
276 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
278 Op1 = getSmallIPtrImm(0);
281 case 'v': // not offsetable
283 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
285 SelectAddrIdxOnly(Op, Op, Op0, Op1);
290 OutOps.push_back(Op0);
291 OutOps.push_back(Op1);
295 virtual const char *getPassName() const {
296 return "Cell SPU DAG->DAG Pattern Instruction Selection";
300 SDValue getRC( MVT );
302 // Include the pieces autogenerated from the target description.
303 #include "SPUGenDAGISel.inc"
308 \param Op The ISD instruction operand
309 \param N The address to be tested
310 \param Base The base address
311 \param Index The base address index
314 SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
316 // These match the addr256k operand type:
317 EVT OffsVT = MVT::i16;
318 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
321 switch (N.getOpcode()) {
323 val = dyn_cast<ConstantSDNode>(N.getNode())->getSExtValue();
324 Base = CurDAG->getTargetConstant( val , MVT::i32);
327 case ISD::ConstantPool:
328 case ISD::GlobalAddress:
329 report_fatal_error("SPU SelectAFormAddr: Pool/Global not lowered.");
332 case ISD::TargetConstant:
333 case ISD::TargetGlobalAddress:
334 case ISD::TargetJumpTable:
335 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
336 "not wrapped as A-form address.");
339 case SPUISD::AFormAddr:
340 // Just load from memory if there's only a single use of the location,
341 // otherwise, this will get handled below with D-form offset addresses
343 SDValue Op0 = N.getOperand(0);
344 switch (Op0.getOpcode()) {
345 case ISD::TargetConstantPool:
346 case ISD::TargetJumpTable:
351 case ISD::TargetGlobalAddress: {
352 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
353 const GlobalValue *GV = GSDN->getGlobal();
354 if (GV->getAlignment() == 16) {
369 SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
371 const int minDForm2Offset = -(1 << 7);
372 const int maxDForm2Offset = (1 << 7) - 1;
373 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
378 \param Op The ISD instruction (ignored)
379 \param N The address to be tested
380 \param Base Base address register/pointer
381 \param Index Base address index
383 Examine the input address by a base register plus a signed 10-bit
384 displacement, [r+I10] (D-form address).
386 \return true if \a N is a D-form address with \a Base and \a Index set
387 to non-empty SDValue instances.
390 SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
392 return DFormAddressPredicate(Op, N, Base, Index,
393 SPUFrameLowering::minFrameOffset(),
394 SPUFrameLowering::maxFrameOffset());
398 SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
399 SDValue &Index, int minOffset,
401 unsigned Opc = N.getOpcode();
402 EVT PtrTy = SPUtli.getPointerTy();
404 if (Opc == ISD::FrameIndex) {
405 // Stack frame index must be less than 512 (divided by 16):
406 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
407 int FI = int(FIN->getIndex());
408 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
410 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
411 Base = CurDAG->getTargetConstant(0, PtrTy);
412 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
415 } else if (Opc == ISD::ADD) {
416 // Generated by getelementptr
417 const SDValue Op0 = N.getOperand(0);
418 const SDValue Op1 = N.getOperand(1);
420 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
421 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
422 Base = CurDAG->getTargetConstant(0, PtrTy);
425 } else if (Op1.getOpcode() == ISD::Constant
426 || Op1.getOpcode() == ISD::TargetConstant) {
427 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
428 int32_t offset = int32_t(CN->getSExtValue());
430 if (Op0.getOpcode() == ISD::FrameIndex) {
431 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
432 int FI = int(FIN->getIndex());
433 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
434 << " frame index = " << FI << "\n");
436 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
437 Base = CurDAG->getTargetConstant(offset, PtrTy);
438 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
441 } else if (offset > minOffset && offset < maxOffset) {
442 Base = CurDAG->getTargetConstant(offset, PtrTy);
446 } else if (Op0.getOpcode() == ISD::Constant
447 || Op0.getOpcode() == ISD::TargetConstant) {
448 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
449 int32_t offset = int32_t(CN->getSExtValue());
451 if (Op1.getOpcode() == ISD::FrameIndex) {
452 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
453 int FI = int(FIN->getIndex());
454 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
455 << " frame index = " << FI << "\n");
457 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
458 Base = CurDAG->getTargetConstant(offset, PtrTy);
459 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
462 } else if (offset > minOffset && offset < maxOffset) {
463 Base = CurDAG->getTargetConstant(offset, PtrTy);
468 } else if (Opc == SPUISD::IndirectAddr) {
469 // Indirect with constant offset -> D-Form address
470 const SDValue Op0 = N.getOperand(0);
471 const SDValue Op1 = N.getOperand(1);
473 if (Op0.getOpcode() == SPUISD::Hi
474 && Op1.getOpcode() == SPUISD::Lo) {
475 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
476 Base = CurDAG->getTargetConstant(0, PtrTy);
479 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
483 if (isa<ConstantSDNode>(Op1)) {
484 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
485 offset = int32_t(CN->getSExtValue());
487 } else if (isa<ConstantSDNode>(Op0)) {
488 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
489 offset = int32_t(CN->getSExtValue());
493 if (offset >= minOffset && offset <= maxOffset) {
494 Base = CurDAG->getTargetConstant(offset, PtrTy);
499 } else if (Opc == SPUISD::AFormAddr) {
500 Base = CurDAG->getTargetConstant(0, N.getValueType());
503 } else if (Opc == SPUISD::LDRESULT) {
504 Base = CurDAG->getTargetConstant(0, N.getValueType());
507 } else if (Opc == ISD::Register
508 ||Opc == ISD::CopyFromReg
510 ||Opc == ISD::Constant) {
511 unsigned OpOpc = Op->getOpcode();
513 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
514 // Direct load/store without getelementptr
517 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
519 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
520 if (Offs.getOpcode() == ISD::UNDEF)
521 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
528 /* If otherwise unadorned, default to D-form address with 0 offset: */
529 if (Opc == ISD::CopyFromReg) {
530 Index = N.getOperand(1);
535 Base = CurDAG->getTargetConstant(0, Index.getValueType());
544 \param Op The ISD instruction operand
545 \param N The address operand
546 \param Base The base pointer operand
547 \param Index The offset/index operand
549 If the address \a N can be expressed as an A-form or D-form address, returns
550 false. Otherwise, creates two operands, Base and Index that will become the
551 (r)(r) X-form address.
554 SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
556 if (!SelectAFormAddr(Op, N, Base, Index)
557 && !SelectDFormAddr(Op, N, Base, Index)) {
558 // If the address is neither A-form or D-form, punt and use an X-form
560 Base = N.getOperand(1);
561 Index = N.getOperand(0);
569 Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
570 to be used as the last parameter of a
571 CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
572 \param VT the value type for which we want a register class
574 SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
575 switch( VT.SimpleTy ) {
577 return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
579 return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
581 return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
583 return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
585 return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
587 return CurDAG->getTargetConstant(SPU::GPRCRegClass.getID(), MVT::i32);
594 return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
596 assert( false && "add a new case here" );
601 //! Convert the operand from a target-independent to a target-specific node
605 SPUDAGToDAGISel::Select(SDNode *N) {
606 unsigned Opc = N->getOpcode();
609 EVT OpVT = N->getValueType(0);
611 DebugLoc dl = N->getDebugLoc();
613 if (N->isMachineOpcode())
614 return NULL; // Already selected.
616 if (Opc == ISD::FrameIndex) {
617 int FI = cast<FrameIndexSDNode>(N)->getIndex();
618 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
619 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
628 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
629 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
630 N->getValueType(0), TFI),
634 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
635 // Catch the i64 constants that end up here. Note: The backend doesn't
636 // attempt to legalize the constant (it's useless because DAGCombiner
637 // will insert 64-bit constants and we can't stop it).
638 return SelectI64Constant(N, OpVT, N->getDebugLoc());
639 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
640 && OpVT == MVT::i64) {
641 SDValue Op0 = N->getOperand(0);
642 EVT Op0VT = Op0.getValueType();
643 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
644 Op0VT, (128 / Op0VT.getSizeInBits()));
645 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
646 OpVT, (128 / OpVT.getSizeInBits()));
649 switch (Op0VT.getSimpleVT().SimpleTy) {
651 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
654 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
655 CurDAG->getConstant(0x80808080, MVT::i32),
656 CurDAG->getConstant(0x00010203, MVT::i32),
657 CurDAG->getConstant(0x80808080, MVT::i32),
658 CurDAG->getConstant(0x08090a0b, MVT::i32));
662 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
663 CurDAG->getConstant(0x80808080, MVT::i32),
664 CurDAG->getConstant(0x80800203, MVT::i32),
665 CurDAG->getConstant(0x80808080, MVT::i32),
666 CurDAG->getConstant(0x80800a0b, MVT::i32));
670 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
671 CurDAG->getConstant(0x80808080, MVT::i32),
672 CurDAG->getConstant(0x80808003, MVT::i32),
673 CurDAG->getConstant(0x80808080, MVT::i32),
674 CurDAG->getConstant(0x8080800b, MVT::i32));
678 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
680 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
684 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
685 PromScalar = SDValue(N, 0);
687 PromScalar = PromoteScalar.getValue();
689 SDValue zextShuffle =
690 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
691 PromScalar, PromScalar,
692 SDValue(shufMaskLoad, 0));
694 HandleSDNode Dummy2(zextShuffle);
695 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
696 zextShuffle = SDValue(N, 0);
698 zextShuffle = Dummy2.getValue();
699 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
702 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
703 SelectCode(Dummy.getValue().getNode());
704 return Dummy.getValue().getNode();
705 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
707 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
709 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
710 N->getOperand(0), N->getOperand(1),
711 SDValue(CGLoad, 0)));
713 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
714 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
716 return Dummy.getValue().getNode();
717 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
719 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
721 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
722 N->getOperand(0), N->getOperand(1),
723 SDValue(CGLoad, 0)));
725 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
726 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
728 return Dummy.getValue().getNode();
729 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
731 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
733 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
734 N->getOperand(0), N->getOperand(1),
735 SDValue(CGLoad, 0)));
736 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
737 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
739 return Dummy.getValue().getNode();
740 } else if (Opc == ISD::TRUNCATE) {
741 SDValue Op0 = N->getOperand(0);
742 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
744 && Op0.getValueType() == MVT::i64) {
745 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
747 // Take advantage of the fact that the upper 32 bits are in the
748 // i32 preferred slot and avoid shuffle gymnastics:
749 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
751 unsigned shift_amt = unsigned(CN->getZExtValue());
753 if (shift_amt >= 32) {
755 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
756 Op0.getOperand(0), getRC(MVT::i32));
760 // Take care of the additional shift, if present:
761 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
762 unsigned Opc = SPU::ROTMAIr32_i32;
764 if (Op0.getOpcode() == ISD::SRL)
767 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
775 } else if (Opc == ISD::SHL) {
776 if (OpVT == MVT::i64)
777 return SelectSHLi64(N, OpVT);
778 } else if (Opc == ISD::SRL) {
779 if (OpVT == MVT::i64)
780 return SelectSRLi64(N, OpVT);
781 } else if (Opc == ISD::SRA) {
782 if (OpVT == MVT::i64)
783 return SelectSRAi64(N, OpVT);
784 } else if (Opc == ISD::FNEG
785 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
786 DebugLoc dl = N->getDebugLoc();
787 // Check if the pattern is a special form of DFNMS:
788 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
789 SDValue Op0 = N->getOperand(0);
790 if (Op0.getOpcode() == ISD::FSUB) {
791 SDValue Op00 = Op0.getOperand(0);
792 if (Op00.getOpcode() == ISD::FMUL) {
793 unsigned Opc = SPU::DFNMSf64;
794 if (OpVT == MVT::v2f64)
795 Opc = SPU::DFNMSv2f64;
797 return CurDAG->getMachineNode(Opc, dl, OpVT,
804 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
805 SDNode *signMask = 0;
806 unsigned Opc = SPU::XORfneg64;
808 if (OpVT == MVT::f64) {
809 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
810 } else if (OpVT == MVT::v2f64) {
811 Opc = SPU::XORfnegvec;
812 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
814 negConst, negConst).getNode());
817 return CurDAG->getMachineNode(Opc, dl, OpVT,
818 N->getOperand(0), SDValue(signMask, 0));
819 } else if (Opc == ISD::FABS) {
820 if (OpVT == MVT::f64) {
821 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
822 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
823 N->getOperand(0), SDValue(signMask, 0));
824 } else if (OpVT == MVT::v2f64) {
825 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
826 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
828 SDNode *signMask = emitBuildVector(absVec.getNode());
829 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
830 N->getOperand(0), SDValue(signMask, 0));
832 } else if (Opc == SPUISD::LDRESULT) {
833 // Custom select instructions for LDRESULT
834 EVT VT = N->getValueType(0);
835 SDValue Arg = N->getOperand(0);
836 SDValue Chain = N->getOperand(1);
839 Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
841 getRC( VT.getSimpleVT()), Chain);
844 } else if (Opc == SPUISD::IndirectAddr) {
845 // Look at the operands: SelectCode() will catch the cases that aren't
846 // specifically handled here.
848 // SPUInstrInfo catches the following patterns:
849 // (SPUindirect (SPUhi ...), (SPUlo ...))
850 // (SPUindirect $sp, imm)
851 EVT VT = N->getValueType(0);
852 SDValue Op0 = N->getOperand(0);
853 SDValue Op1 = N->getOperand(1);
856 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
857 || (Op0.getOpcode() == ISD::Register
858 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
859 && RN->getReg() != SPU::R1))) {
862 if (Op1.getOpcode() == ISD::Constant) {
863 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
864 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
865 if (isInt<10>(CN->getSExtValue())) {
869 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
882 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
884 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
886 return SelectCode(N);
890 * Emit the instruction sequence for i64 left shifts. The basic algorithm
891 * is to fill the bottom two word slots with zeros so that zeros are shifted
892 * in as the entire quadword is shifted left.
894 * \note This code could also be used to implement v2i64 shl.
896 * @param Op The shl operand
897 * @param OpVT Op's machine value value type (doesn't need to be passed, but
898 * makes life easier.)
899 * @return The SDNode with the entire instruction sequence
902 SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
903 SDValue Op0 = N->getOperand(0);
904 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
905 OpVT, (128 / OpVT.getSizeInBits()));
906 SDValue ShiftAmt = N->getOperand(1);
907 EVT ShiftAmtVT = ShiftAmt.getValueType();
908 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
910 DebugLoc dl = N->getDebugLoc();
912 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
913 Op0, getRC(MVT::v2i64) );
914 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
915 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
916 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
917 CurDAG->getTargetConstant(0, OpVT));
918 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
919 SDValue(ZeroFill, 0),
921 SDValue(SelMask, 0));
923 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
924 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
925 unsigned bits = unsigned(CN->getZExtValue()) & 7;
929 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
931 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
936 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
937 SDValue((Shift != 0 ? Shift : VecOp0), 0),
938 CurDAG->getTargetConstant(bits, ShiftAmtVT));
942 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
944 CurDAG->getTargetConstant(3, ShiftAmtVT));
946 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
948 CurDAG->getTargetConstant(7, ShiftAmtVT));
950 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
951 SDValue(VecOp0, 0), SDValue(Bytes, 0));
953 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
954 SDValue(Shift, 0), SDValue(Bits, 0));
957 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
958 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
962 * Emit the instruction sequence for i64 logical right shifts.
964 * @param Op The shl operand
965 * @param OpVT Op's machine value value type (doesn't need to be passed, but
966 * makes life easier.)
967 * @return The SDNode with the entire instruction sequence
970 SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
971 SDValue Op0 = N->getOperand(0);
972 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
973 OpVT, (128 / OpVT.getSizeInBits()));
974 SDValue ShiftAmt = N->getOperand(1);
975 EVT ShiftAmtVT = ShiftAmt.getValueType();
976 SDNode *VecOp0, *Shift = 0;
977 DebugLoc dl = N->getDebugLoc();
979 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
980 Op0, getRC(MVT::v2i64) );
982 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
983 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
984 unsigned bits = unsigned(CN->getZExtValue()) & 7;
988 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
990 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
995 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
996 SDValue((Shift != 0 ? Shift : VecOp0), 0),
997 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1001 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1003 CurDAG->getTargetConstant(3, ShiftAmtVT));
1005 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1007 CurDAG->getTargetConstant(7, ShiftAmtVT));
1009 // Ensure that the shift amounts are negated!
1010 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1012 CurDAG->getTargetConstant(0, ShiftAmtVT));
1014 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1016 CurDAG->getTargetConstant(0, ShiftAmtVT));
1019 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1020 SDValue(VecOp0, 0), SDValue(Bytes, 0));
1022 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1023 SDValue(Shift, 0), SDValue(Bits, 0));
1026 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1027 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
1031 * Emit the instruction sequence for i64 arithmetic right shifts.
1033 * @param Op The shl operand
1034 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1035 * makes life easier.)
1036 * @return The SDNode with the entire instruction sequence
1039 SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
1040 // Promote Op0 to vector
1041 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1042 OpVT, (128 / OpVT.getSizeInBits()));
1043 SDValue ShiftAmt = N->getOperand(1);
1044 EVT ShiftAmtVT = ShiftAmt.getValueType();
1045 DebugLoc dl = N->getDebugLoc();
1048 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1049 VecVT, N->getOperand(0), getRC(MVT::v2i64));
1051 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1053 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1054 SDValue(VecOp0, 0), SignRotAmt);
1055 SDNode *UpperHalfSign =
1056 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1057 MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
1059 SDNode *UpperHalfSignMask =
1060 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
1061 SDNode *UpperLowerMask =
1062 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1063 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1064 SDNode *UpperLowerSelect =
1065 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1066 SDValue(UpperHalfSignMask, 0),
1068 SDValue(UpperLowerMask, 0));
1072 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1073 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1074 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1079 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1080 SDValue(UpperLowerSelect, 0),
1081 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1087 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1088 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1089 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1093 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1094 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1097 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1098 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1100 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1101 SDValue(Shift, 0), SDValue(NegShift, 0));
1104 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1105 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
1109 Do the necessary magic necessary to load a i64 constant
1111 SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
1113 ConstantSDNode *CN = cast<ConstantSDNode>(N);
1114 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1117 SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
1119 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
1121 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
1123 // Here's where it gets interesting, because we have to parse out the
1124 // subtree handed back in i64vec:
1126 if (i64vec.getOpcode() == ISD::BITCAST) {
1127 // The degenerate case where the upper and lower bits in the splat are
1129 SDValue Op0 = i64vec.getOperand(0);
1131 ReplaceUses(i64vec, Op0);
1132 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1133 SDValue(emitBuildVector(Op0.getNode()), 0),
1135 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1136 SDValue lhs = i64vec.getOperand(0);
1137 SDValue rhs = i64vec.getOperand(1);
1138 SDValue shufmask = i64vec.getOperand(2);
1140 if (lhs.getOpcode() == ISD::BITCAST) {
1141 ReplaceUses(lhs, lhs.getOperand(0));
1142 lhs = lhs.getOperand(0);
1145 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1147 : emitBuildVector(lhs.getNode()));
1149 if (rhs.getOpcode() == ISD::BITCAST) {
1150 ReplaceUses(rhs, rhs.getOperand(0));
1151 rhs = rhs.getOperand(0);
1154 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1156 : emitBuildVector(rhs.getNode()));
1158 if (shufmask.getOpcode() == ISD::BITCAST) {
1159 ReplaceUses(shufmask, shufmask.getOperand(0));
1160 shufmask = shufmask.getOperand(0);
1163 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1164 ? shufmask.getNode()
1165 : emitBuildVector(shufmask.getNode()));
1168 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
1169 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1170 SDValue(shufMaskNode, 0));
1171 HandleSDNode Dummy(shufNode);
1172 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1173 if (SN == 0) SN = Dummy.getValue().getNode();
1175 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1176 OpVT, SDValue(SN, 0), getRC(MVT::i64));
1177 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
1178 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1179 SDValue(emitBuildVector(i64vec.getNode()), 0),
1182 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
1187 /// createSPUISelDag - This pass converts a legalized DAG into a
1188 /// SPU-specific DAG, ready for instruction scheduling.
1190 FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1191 return new SPUDAGToDAGISel(TM);