blob: ea767586b06036ce9d2c9dfb7541710e673190a6 [file] [log] [blame]
Chris Lattner4ee451d2007-12-29 20:36:04 +00001//===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel266bc8f2007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for the Cell SPU,
11// converting from a legalized dag to a SPU-target dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SPU.h"
16#include "SPUTargetMachine.h"
17#include "SPUISelLowering.h"
18#include "SPUHazardRecognizers.h"
19#include "SPUFrameInfo.h"
Scott Michel203b2d62008-04-30 00:30:08 +000020#include "SPURegisterNames.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000021#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000028#include "llvm/Target/TargetOptions.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Constants.h"
31#include "llvm/GlobalValue.h"
32#include "llvm/Intrinsics.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/Compiler.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000036
37using namespace llvm;
38
39namespace {
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
41 bool
42 isI64IntS10Immediate(ConstantSDNode *CN)
43 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000044 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000045 }
46
47 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
48 bool
49 isI32IntS10Immediate(ConstantSDNode *CN)
50 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000051 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000052 }
53
Scott Michel504c3692007-12-17 22:32:34 +000054 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
55 bool
56 isI32IntU10Immediate(ConstantSDNode *CN)
57 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000058 return isU10Constant(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000059 }
60
Scott Michel266bc8f2007-12-04 22:23:35 +000061 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
62 bool
63 isI16IntS10Immediate(ConstantSDNode *CN)
64 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000065 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000066 }
67
68 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
69 bool
70 isI16IntS10Immediate(SDNode *N)
71 {
Scott Michel9de57a92009-01-26 22:33:37 +000072 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
73 return (CN != 0 && isI16IntS10Immediate(CN));
Scott Michel266bc8f2007-12-04 22:23:35 +000074 }
75
Scott Michelec2a08f2007-12-15 00:38:50 +000076 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
77 bool
78 isI16IntU10Immediate(ConstantSDNode *CN)
79 {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000080 return isU10Constant((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000081 }
82
83 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
84 bool
85 isI16IntU10Immediate(SDNode *N)
86 {
87 return (N->getOpcode() == ISD::Constant
88 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
89 }
90
Scott Michel266bc8f2007-12-04 22:23:35 +000091 //! ConstantSDNode predicate for signed 16-bit values
92 /*!
93 \arg CN The constant SelectionDAG node holding the value
94 \arg Imm The returned 16-bit value, if returning true
95
96 This predicate tests the value in \a CN to see whether it can be
97 represented as a 16-bit, sign-extended quantity. Returns true if
98 this is the case.
99 */
100 bool
101 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
102 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000103 MVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000104 Imm = (short) CN->getZExtValue();
Duncan Sands8e4eb092008-06-08 20:54:56 +0000105 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000106 return true;
107 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000108 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000109 short s_val = (short) i_val;
110 return i_val == s_val;
111 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000112 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000113 short s_val = (short) i_val;
114 return i_val == s_val;
115 }
116
117 return false;
118 }
119
120 //! SDNode predicate for signed 16-bit values.
121 bool
122 isIntS16Immediate(SDNode *N, short &Imm)
123 {
124 return (N->getOpcode() == ISD::Constant
125 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
126 }
127
128 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
129 static bool
130 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
131 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000132 MVT vt = FPN->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000133 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000134 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000135 int sval = (int) ((val << 16) >> 16);
136 Imm = (short) val;
137 return val == sval;
138 }
139
140 return false;
141 }
142
Scott Michel053c1da2008-01-29 02:16:57 +0000143 bool
Scott Michel02d711b2008-12-30 23:28:25 +0000144 isHighLow(const SDValue &Op)
Scott Michel053c1da2008-01-29 02:16:57 +0000145 {
146 return (Op.getOpcode() == SPUISD::IndirectAddr
147 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
148 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
149 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
150 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
151 }
152
Scott Michel266bc8f2007-12-04 22:23:35 +0000153 //===------------------------------------------------------------------===//
Duncan Sands83ec4b62008-06-06 12:08:01 +0000154 //! MVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000155
156 struct valtype_map_s {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000157 MVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000158 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000159 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michelf0569be2008-12-27 04:51:36 +0000160 unsigned lrinst; /// LR instruction
Scott Michel266bc8f2007-12-04 22:23:35 +0000161 };
162
163 const valtype_map_s valtype_map[] = {
Scott Michelf0569be2008-12-27 04:51:36 +0000164 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
165 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
166 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
167 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
168 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
169 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
Scott Michel58c58182008-01-17 20:38:41 +0000170 // vector types... (sigh!)
Scott Michelf0569be2008-12-27 04:51:36 +0000171 { MVT::v16i8, 0, false, SPU::LRv16i8 },
172 { MVT::v8i16, 0, false, SPU::LRv8i16 },
173 { MVT::v4i32, 0, false, SPU::LRv4i32 },
174 { MVT::v2i64, 0, false, SPU::LRv2i64 },
175 { MVT::v4f32, 0, false, SPU::LRv4f32 },
176 { MVT::v2f64, 0, false, SPU::LRv2f64 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000177 };
178
179 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
180
Duncan Sands83ec4b62008-06-06 12:08:01 +0000181 const valtype_map_s *getValueTypeMapEntry(MVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000182 {
183 const valtype_map_s *retval = 0;
184 for (size_t i = 0; i < n_valtype_map; ++i) {
185 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000186 retval = valtype_map + i;
187 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000188 }
189 }
190
191
192#ifndef NDEBUG
193 if (retval == 0) {
194 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000195 << VT.getMVTString()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000196 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000197 abort();
198 }
199#endif
200
201 return retval;
202 }
203}
204
Dan Gohman844731a2008-05-13 00:00:25 +0000205namespace {
206
Scott Michel266bc8f2007-12-04 22:23:35 +0000207//===--------------------------------------------------------------------===//
208/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
209/// instructions for SelectionDAG operations.
210///
211class SPUDAGToDAGISel :
212 public SelectionDAGISel
213{
214 SPUTargetMachine &TM;
215 SPUTargetLowering &SPUtli;
216 unsigned GlobalBaseReg;
217
218public:
Dan Gohman1002c022008-07-07 18:00:37 +0000219 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +0000220 SelectionDAGISel(tm),
Scott Michel266bc8f2007-12-04 22:23:35 +0000221 TM(tm),
222 SPUtli(*tm.getTargetLowering())
Scott Michel9de57a92009-01-26 22:33:37 +0000223 { }
Scott Michel02d711b2008-12-30 23:28:25 +0000224
Scott Michel266bc8f2007-12-04 22:23:35 +0000225 virtual bool runOnFunction(Function &Fn) {
226 // Make sure we re-emit a set of the global base reg if necessary
227 GlobalBaseReg = 0;
228 SelectionDAGISel::runOnFunction(Fn);
229 return true;
230 }
Scott Michel02d711b2008-12-30 23:28:25 +0000231
Scott Michel266bc8f2007-12-04 22:23:35 +0000232 /// getI32Imm - Return a target constant with the specified value, of type
233 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 inline SDValue getI32Imm(uint32_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000235 return CurDAG->getTargetConstant(Imm, MVT::i32);
236 }
237
238 /// getI64Imm - Return a target constant with the specified value, of type
239 /// i64.
Dan Gohman475871a2008-07-27 21:46:04 +0000240 inline SDValue getI64Imm(uint64_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000241 return CurDAG->getTargetConstant(Imm, MVT::i64);
242 }
Scott Michel02d711b2008-12-30 23:28:25 +0000243
Scott Michel266bc8f2007-12-04 22:23:35 +0000244 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman475871a2008-07-27 21:46:04 +0000245 inline SDValue getSmallIPtrImm(unsigned Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000246 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000247 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000248
Scott Michel94bd57e2009-01-15 04:41:47 +0000249 SDNode *emitBuildVector(SDValue build_vec) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000250 MVT vecVT = build_vec.getValueType();
251 SDNode *bvNode = build_vec.getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000252
253 // Check to see if this vector can be represented as a CellSPU immediate
Scott Michel9de57a92009-01-26 22:33:37 +0000254 // constant by invoking all of the instruction selection predicates:
255 if (((vecVT == MVT::v8i16) &&
256 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
257 ((vecVT == MVT::v4i32) &&
258 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
259 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
260 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
261 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
262 ((vecVT == MVT::v2i64) &&
263 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
264 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
265 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0))))
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000266 return Select(build_vec);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000267
268 // No, need to emit a constant pool spill:
Scott Michel94bd57e2009-01-15 04:41:47 +0000269 std::vector<Constant*> CV;
270
271 for (size_t i = 0; i < build_vec.getNumOperands(); ++i) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000272 ConstantSDNode *V = dyn_cast<ConstantSDNode > (build_vec.getOperand(i));
273 CV.push_back(const_cast<ConstantInt *> (V->getConstantIntValue()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000274 }
275
276 Constant *CP = ConstantVector::get(CV);
277 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000278 unsigned Alignment = 1 << cast<ConstantPoolSDNode > (CPIdx)->getAlignment();
Scott Michel94bd57e2009-01-15 04:41:47 +0000279 SDValue CGPoolOffset =
280 SPU::LowerConstantPool(CPIdx, *CurDAG,
281 SPUtli.getSPUTargetMachine());
282 return SelectCode(CurDAG->getLoad(build_vec.getValueType(),
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000283 CurDAG->getEntryNode(), CGPoolOffset,
284 PseudoSourceValue::getConstantPool(), 0,
285 false, Alignment));
Scott Michel94bd57e2009-01-15 04:41:47 +0000286 }
287
Scott Michel266bc8f2007-12-04 22:23:35 +0000288 /// Select - Convert the specified operand from a target-independent to a
289 /// target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDNode *Select(SDValue Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000291
Scott Michel02d711b2008-12-30 23:28:25 +0000292 //! Emit the instruction sequence for i64 shl
293 SDNode *SelectSHLi64(SDValue &Op, MVT OpVT);
294
295 //! Emit the instruction sequence for i64 srl
296 SDNode *SelectSRLi64(SDValue &Op, MVT OpVT);
297
298 //! Emit the instruction sequence for i64 sra
299 SDNode *SelectSRAi64(SDValue &Op, MVT OpVT);
300
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000301 //! Emit the necessary sequence for loading i64 constants:
302 SDNode *SelectI64Constant(SDValue &Op, MVT OpVT);
303
Scott Michel266bc8f2007-12-04 22:23:35 +0000304 //! Returns true if the address N is an A-form (local store) address
Dan Gohman475871a2008-07-27 21:46:04 +0000305 bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
306 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000307
308 //! D-form address predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000309 bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
310 SDValue &Index);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000311
312 /// Alternate D-form address using i7 offset predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000313 bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
314 SDValue &Base);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000315
316 /// D-form address selection workhorse
Dan Gohman475871a2008-07-27 21:46:04 +0000317 bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
318 SDValue &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000319
320 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000321 bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
322 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000323
324 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
325 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000326 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000327 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000328 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +0000329 SDValue Op0, Op1;
Scott Michel266bc8f2007-12-04 22:23:35 +0000330 switch (ConstraintCode) {
331 default: return true;
332 case 'm': // memory
Scott Michel02d711b2008-12-30 23:28:25 +0000333 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000334 && !SelectAFormAddr(Op, Op, Op0, Op1))
335 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000336 break;
337 case 'o': // offsetable
338 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000339 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
340 Op0 = Op;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000341 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000342 }
343 break;
344 case 'v': // not offsetable
345#if 1
346 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
347#else
348 SelectAddrIdxOnly(Op, Op, Op0, Op1);
349#endif
350 break;
351 }
Scott Michel02d711b2008-12-30 23:28:25 +0000352
Scott Michel266bc8f2007-12-04 22:23:35 +0000353 OutOps.push_back(Op0);
354 OutOps.push_back(Op1);
355 return false;
356 }
357
Evan Chengdb8d56b2008-06-30 20:45:06 +0000358 /// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000359 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000360 virtual void InstructionSelect();
Scott Michel266bc8f2007-12-04 22:23:35 +0000361
362 virtual const char *getPassName() const {
363 return "Cell SPU DAG->DAG Pattern Instruction Selection";
Scott Michel02d711b2008-12-30 23:28:25 +0000364 }
365
Scott Michel266bc8f2007-12-04 22:23:35 +0000366 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
367 /// this target when scheduling the DAG.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000368 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohman6448d912008-09-04 15:39:15 +0000369 const TargetInstrInfo *II = TM.getInstrInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +0000370 assert(II && "No InstrInfo?");
Scott Michel02d711b2008-12-30 23:28:25 +0000371 return new SPUHazardRecognizer(*II);
Scott Michel266bc8f2007-12-04 22:23:35 +0000372 }
373
374 // Include the pieces autogenerated from the target description.
375#include "SPUGenDAGISel.inc"
376};
377
Dan Gohman844731a2008-05-13 00:00:25 +0000378}
379
Evan Chengdb8d56b2008-06-30 20:45:06 +0000380/// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000381/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
382void
Dan Gohmanf350b272008-08-23 02:25:05 +0000383SPUDAGToDAGISel::InstructionSelect()
Scott Michel266bc8f2007-12-04 22:23:35 +0000384{
385 DEBUG(BB->dump());
386
387 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000388 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +0000389 CurDAG->RemoveDeadNodes();
Scott Michel266bc8f2007-12-04 22:23:35 +0000390}
391
Scott Michel266bc8f2007-12-04 22:23:35 +0000392/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000393 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000394 \arg N The address to be tested
395 \arg Base The base address
396 \arg Index The base address index
397 */
398bool
Dan Gohman475871a2008-07-27 21:46:04 +0000399SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
400 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000401 // These match the addr256k operand type:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000402 MVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000404
405 switch (N.getOpcode()) {
406 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000407 case ISD::ConstantPool:
408 case ISD::GlobalAddress:
409 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
410 abort();
411 /*NOTREACHED*/
412
Scott Michel053c1da2008-01-29 02:16:57 +0000413 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000414 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000415 case ISD::TargetJumpTable:
416 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
417 << "A-form address.\n";
418 abort();
419 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000420
Scott Michel02d711b2008-12-30 23:28:25 +0000421 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000422 // Just load from memory if there's only a single use of the location,
423 // otherwise, this will get handled below with D-form offset addresses
424 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000425 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000426 switch (Op0.getOpcode()) {
427 case ISD::TargetConstantPool:
428 case ISD::TargetJumpTable:
429 Base = Op0;
430 Index = Zero;
431 return true;
432
433 case ISD::TargetGlobalAddress: {
434 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
435 GlobalValue *GV = GSDN->getGlobal();
436 if (GV->getAlignment() == 16) {
437 Base = Op0;
438 Index = Zero;
439 return true;
440 }
441 break;
442 }
443 }
444 }
445 break;
446 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000447 return false;
448}
449
Scott Michel02d711b2008-12-30 23:28:25 +0000450bool
Dan Gohman475871a2008-07-27 21:46:04 +0000451SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
452 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000453 const int minDForm2Offset = -(1 << 7);
454 const int maxDForm2Offset = (1 << 7) - 1;
455 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
456 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000457}
458
Scott Michel266bc8f2007-12-04 22:23:35 +0000459/*!
460 \arg Op The ISD instruction (ignored)
461 \arg N The address to be tested
462 \arg Base Base address register/pointer
463 \arg Index Base address index
464
465 Examine the input address by a base register plus a signed 10-bit
466 displacement, [r+I10] (D-form address).
467
468 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000469 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000470*/
471bool
Dan Gohman475871a2008-07-27 21:46:04 +0000472SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
473 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000474 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000475 SPUFrameInfo::minFrameOffset(),
476 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000477}
478
479bool
Dan Gohman475871a2008-07-27 21:46:04 +0000480SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
481 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000482 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000483 unsigned Opc = N.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000484 MVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000485
Scott Michel053c1da2008-01-29 02:16:57 +0000486 if (Opc == ISD::FrameIndex) {
487 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000488 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
489 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000490 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000491 << FI << "\n");
492 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000493 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000494 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000495 return true;
496 }
497 } else if (Opc == ISD::ADD) {
498 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000499 const SDValue Op0 = N.getOperand(0);
500 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000501
Scott Michel053c1da2008-01-29 02:16:57 +0000502 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
503 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
504 Base = CurDAG->getTargetConstant(0, PtrTy);
505 Index = N;
506 return true;
507 } else if (Op1.getOpcode() == ISD::Constant
508 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000509 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000510 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000511
Scott Michel053c1da2008-01-29 02:16:57 +0000512 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000513 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
514 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000515 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000516 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000517
Scott Michel203b2d62008-04-30 00:30:08 +0000518 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000519 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000520 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000521 return true;
522 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000523 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000524 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000525 Index = Op0;
526 return true;
527 }
528 } else if (Op0.getOpcode() == ISD::Constant
529 || Op0.getOpcode() == ISD::TargetConstant) {
530 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000531 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000532
533 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000534 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
535 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000536 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000537 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000538
Scott Michel203b2d62008-04-30 00:30:08 +0000539 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000540 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000541 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000542 return true;
543 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000544 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000545 Base = CurDAG->getTargetConstant(offset, PtrTy);
546 Index = Op1;
547 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000548 }
Scott Michel053c1da2008-01-29 02:16:57 +0000549 }
550 } else if (Opc == SPUISD::IndirectAddr) {
551 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000552 const SDValue Op0 = N.getOperand(0);
553 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000554
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000555 if (Op0.getOpcode() == SPUISD::Hi
556 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000557 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000558 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000559 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000560 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000561 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
562 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000563 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000564
565 if (isa<ConstantSDNode>(Op1)) {
566 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000567 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000568 idxOp = Op0;
569 } else if (isa<ConstantSDNode>(Op0)) {
570 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000571 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000572 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000573 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000574
575 if (offset >= minOffset && offset <= maxOffset) {
576 Base = CurDAG->getTargetConstant(offset, PtrTy);
577 Index = idxOp;
578 return true;
579 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000580 }
Scott Michel053c1da2008-01-29 02:16:57 +0000581 } else if (Opc == SPUISD::AFormAddr) {
582 Base = CurDAG->getTargetConstant(0, N.getValueType());
583 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000584 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000585 } else if (Opc == SPUISD::LDRESULT) {
586 Base = CurDAG->getTargetConstant(0, N.getValueType());
587 Index = N;
588 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000589 } else if (Opc == ISD::Register || Opc == ISD::CopyFromReg) {
590 unsigned OpOpc = Op.getOpcode();
591
592 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
593 // Direct load/store without getelementptr
594 SDValue Addr, Offs;
595
596 // Get the register from CopyFromReg
597 if (Opc == ISD::CopyFromReg)
598 Addr = N.getOperand(1);
599 else
600 Addr = N; // Register
601
Scott Michelaedc6372008-12-10 00:15:19 +0000602 Offs = ((OpOpc == ISD::STORE) ? Op.getOperand(3) : Op.getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000603
604 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
605 if (Offs.getOpcode() == ISD::UNDEF)
606 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
607
608 Base = Offs;
609 Index = Addr;
610 return true;
611 }
Scott Michelaedc6372008-12-10 00:15:19 +0000612 } else {
613 /* If otherwise unadorned, default to D-form address with 0 offset: */
614 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000615 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000616 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000617 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000618 }
619
620 Base = CurDAG->getTargetConstant(0, Index.getValueType());
621 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000622 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000623 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000624
Scott Michel266bc8f2007-12-04 22:23:35 +0000625 return false;
626}
627
628/*!
629 \arg Op The ISD instruction operand
630 \arg N The address operand
631 \arg Base The base pointer operand
632 \arg Index The offset/index operand
633
Scott Michel9c0c6b22008-11-21 02:56:16 +0000634 If the address \a N can be expressed as an A-form or D-form address, returns
635 false. Otherwise, creates two operands, Base and Index that will become the
636 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000637*/
638bool
Dan Gohman475871a2008-07-27 21:46:04 +0000639SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
640 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000641 if (!SelectAFormAddr(Op, N, Base, Index)
642 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000643 // If the address is neither A-form or D-form, punt and use an X-form
644 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000645 Base = N.getOperand(1);
646 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000647 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000648 }
649
650 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000651}
652
Scott Michel266bc8f2007-12-04 22:23:35 +0000653//! Convert the operand from a target-independent to a target-specific node
654/*!
655 */
656SDNode *
Dan Gohman475871a2008-07-27 21:46:04 +0000657SPUDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000658 SDNode *N = Op.getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000659 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000660 int n_ops = -1;
661 unsigned NewOpc;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000662 MVT OpVT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000663 SDValue Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000664
Dan Gohmane8be6c62008-07-17 19:10:17 +0000665 if (N->isMachineOpcode()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000666 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000667 }
668
669 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000670 int FI = cast<FrameIndexSDNode>(N)->getIndex();
671 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
672 SDValue Imm0 = CurDAG->getTargetConstant(0, Op.getValueType());
Scott Michel266bc8f2007-12-04 22:23:35 +0000673
Scott Michel02d711b2008-12-30 23:28:25 +0000674 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000675 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000676 Ops[0] = TFI;
677 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000678 n_ops = 2;
679 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000680 NewOpc = SPU::Ar32;
Scott Michel02d711b2008-12-30 23:28:25 +0000681 Ops[0] = CurDAG->getRegister(SPU::R1, Op.getValueType());
682 Ops[1] = SDValue(CurDAG->getTargetNode(SPU::ILAr32, Op.getValueType(),
683 TFI, Imm0), 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000684 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000685 }
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000686 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
687 // Catch the i64 constants that end up here. Note: The backend doesn't
688 // attempt to legalize the constant (it's useless because DAGCombiner
689 // will insert 64-bit constants and we can't stop it).
690 return SelectI64Constant(Op, OpVT);
Scott Michel94bd57e2009-01-15 04:41:47 +0000691 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
692 && OpVT == MVT::i64) {
693 SDValue Op0 = Op.getOperand(0);
694 MVT Op0VT = Op0.getValueType();
695 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
696 MVT OpVecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
697 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000698
Scott Michel94bd57e2009-01-15 04:41:47 +0000699 switch (Op0VT.getSimpleVT()) {
700 default:
701 cerr << "CellSPU Select: Unhandled zero/any extend MVT\n";
702 abort();
703 /*NOTREACHED*/
704 break;
705 case MVT::i32:
706 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000707 CurDAG->getConstant(0x80808080, MVT::i32),
708 CurDAG->getConstant(0x00010203, MVT::i32),
709 CurDAG->getConstant(0x80808080, MVT::i32),
710 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000711 break;
712
713 case MVT::i16:
714 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000715 CurDAG->getConstant(0x80808080, MVT::i32),
716 CurDAG->getConstant(0x80800203, MVT::i32),
717 CurDAG->getConstant(0x80808080, MVT::i32),
718 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000719 break;
720
721 case MVT::i8:
722 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000723 CurDAG->getConstant(0x80808080, MVT::i32),
724 CurDAG->getConstant(0x80808003, MVT::i32),
725 CurDAG->getConstant(0x80808080, MVT::i32),
726 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000727 break;
Scott Michel58c58182008-01-17 20:38:41 +0000728 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000729
730 SDNode *shufMaskLoad = emitBuildVector(shufMask);
731 SDNode *PromoteScalar =
732 SelectCode(CurDAG->getNode(SPUISD::PREFSLOT2VEC, Op0VecVT, Op0));
733
734 SDValue zextShuffle =
735 CurDAG->getNode(SPUISD::SHUFB, OpVecVT,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000736 SDValue(PromoteScalar, 0),
737 SDValue(PromoteScalar, 0),
738 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000739
740 // N.B.: BIT_CONVERT replaces and updates the zextShuffle node, so we
741 // re-use it in the VEC2PREFSLOT selection without needing to explicitly
742 // call SelectCode (it's already done for us.)
743 SelectCode(CurDAG->getNode(ISD::BIT_CONVERT, OpVecVT, zextShuffle));
744 return SelectCode(CurDAG->getNode(SPUISD::VEC2PREFSLOT, OpVT,
745 zextShuffle));
746 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
747 SDNode *CGLoad =
748 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG));
749
750 return SelectCode(CurDAG->getNode(SPUISD::ADD64_MARKER, OpVT,
751 Op.getOperand(0), Op.getOperand(1),
752 SDValue(CGLoad, 0)));
753 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
754 SDNode *CGLoad =
755 emitBuildVector(SPU::getBorrowGenerateShufMask(*CurDAG));
756
757 return SelectCode(CurDAG->getNode(SPUISD::SUB64_MARKER, OpVT,
758 Op.getOperand(0), Op.getOperand(1),
759 SDValue(CGLoad, 0)));
760 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
761 SDNode *CGLoad =
762 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG));
763
764 return SelectCode(CurDAG->getNode(SPUISD::MUL64_MARKER, OpVT,
765 Op.getOperand(0), Op.getOperand(1),
766 SDValue(CGLoad, 0)));
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000767 } else if (Opc == ISD::TRUNCATE) {
768 SDValue Op0 = Op.getOperand(0);
769 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
770 && OpVT == MVT::i32
771 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000772 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
773 //
774 // Take advantage of the fact that the upper 32 bits are in the
775 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000776 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
777 if (CN != 0) {
778 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000779
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000780 if (shift_amt >= 32) {
781 SDNode *hi32 =
782 CurDAG->getTargetNode(SPU::ORr32_r64, OpVT, Op0.getOperand(0));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000783
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000784 shift_amt -= 32;
785 if (shift_amt > 0) {
786 // Take care of the additional shift, if present:
787 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
788 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000789
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000790 if (Op0.getOpcode() == ISD::SRL)
791 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000792
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000793 hi32 = CurDAG->getTargetNode(Opc, OpVT, SDValue(hi32, 0), shift);
794 }
795
796 return hi32;
797 }
798 }
799 }
Scott Michel02d711b2008-12-30 23:28:25 +0000800 } else if (Opc == ISD::SHL) {
801 if (OpVT == MVT::i64) {
802 return SelectSHLi64(Op, OpVT);
803 }
804 } else if (Opc == ISD::SRL) {
805 if (OpVT == MVT::i64) {
806 return SelectSRLi64(Op, OpVT);
807 }
808 } else if (Opc == ISD::SRA) {
809 if (OpVT == MVT::i64) {
810 return SelectSRAi64(Op, OpVT);
811 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000812 } else if (Opc == SPUISD::LDRESULT) {
813 // Custom select instructions for LDRESULT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000815 SDValue Arg = N->getOperand(0);
816 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000817 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000818 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
819
820 if (vtm->ldresult_ins == 0) {
821 cerr << "LDRESULT for unsupported type: "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000822 << VT.getMVTString()
Scott Michela59d4692008-02-23 18:41:37 +0000823 << "\n";
824 abort();
825 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000826
Scott Michela59d4692008-02-23 18:41:37 +0000827 Opc = vtm->ldresult_ins;
828 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000829 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000830
Scott Michel58c58182008-01-17 20:38:41 +0000831 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000832 } else {
Scott Michel30ee7df2008-12-04 03:02:42 +0000833 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000834 }
835
Scott Michel266bc8f2007-12-04 22:23:35 +0000836 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000837 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000838 // Look at the operands: SelectCode() will catch the cases that aren't
839 // specifically handled here.
840 //
841 // SPUInstrInfo catches the following patterns:
842 // (SPUindirect (SPUhi ...), (SPUlo ...))
843 // (SPUindirect $sp, imm)
844 MVT VT = Op.getValueType();
845 SDValue Op0 = N->getOperand(0);
846 SDValue Op1 = N->getOperand(1);
847 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000848
Scott Michelf0569be2008-12-27 04:51:36 +0000849 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
850 || (Op0.getOpcode() == ISD::Register
851 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
852 && RN->getReg() != SPU::R1))) {
853 NewOpc = SPU::Ar32;
Scott Michel58c58182008-01-17 20:38:41 +0000854 if (Op1.getOpcode() == ISD::Constant) {
855 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000856 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000857 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
Scott Michel58c58182008-01-17 20:38:41 +0000858 }
Scott Michelf0569be2008-12-27 04:51:36 +0000859 Ops[0] = Op0;
860 Ops[1] = Op1;
861 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000862 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000863 }
Scott Michel02d711b2008-12-30 23:28:25 +0000864
Scott Michel58c58182008-01-17 20:38:41 +0000865 if (n_ops > 0) {
866 if (N->hasOneUse())
867 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
868 else
869 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
870 } else
871 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000872}
873
Scott Michel02d711b2008-12-30 23:28:25 +0000874/*!
875 * Emit the instruction sequence for i64 left shifts. The basic algorithm
876 * is to fill the bottom two word slots with zeros so that zeros are shifted
877 * in as the entire quadword is shifted left.
878 *
879 * \note This code could also be used to implement v2i64 shl.
880 *
881 * @param Op The shl operand
882 * @param OpVT Op's machine value value type (doesn't need to be passed, but
883 * makes life easier.)
884 * @return The SDNode with the entire instruction sequence
885 */
886SDNode *
887SPUDAGToDAGISel::SelectSHLi64(SDValue &Op, MVT OpVT) {
888 SDValue Op0 = Op.getOperand(0);
889 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
890 SDValue ShiftAmt = Op.getOperand(1);
891 MVT ShiftAmtVT = ShiftAmt.getValueType();
892 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
893 SDValue SelMaskVal;
894
895 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op0);
896 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
897 SelMask = CurDAG->getTargetNode(SPU::FSMBIv2i64, VecVT, SelMaskVal);
898 ZeroFill = CurDAG->getTargetNode(SPU::ILv2i64, VecVT,
899 CurDAG->getTargetConstant(0, OpVT));
900 VecOp0 = CurDAG->getTargetNode(SPU::SELBv2i64, VecVT,
901 SDValue(ZeroFill, 0),
902 SDValue(VecOp0, 0),
903 SDValue(SelMask, 0));
904
905 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
906 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
907 unsigned bits = unsigned(CN->getZExtValue()) & 7;
908
909 if (bytes > 0) {
910 Shift =
911 CurDAG->getTargetNode(SPU::SHLQBYIv2i64, VecVT,
912 SDValue(VecOp0, 0),
913 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
914 }
915
916 if (bits > 0) {
917 Shift =
918 CurDAG->getTargetNode(SPU::SHLQBIIv2i64, VecVT,
919 SDValue((Shift != 0 ? Shift : VecOp0), 0),
920 CurDAG->getTargetConstant(bits, ShiftAmtVT));
921 }
922 } else {
923 SDNode *Bytes =
924 CurDAG->getTargetNode(SPU::ROTMIr32, ShiftAmtVT,
925 ShiftAmt,
926 CurDAG->getTargetConstant(3, ShiftAmtVT));
927 SDNode *Bits =
928 CurDAG->getTargetNode(SPU::ANDIr32, ShiftAmtVT,
929 ShiftAmt,
930 CurDAG->getTargetConstant(7, ShiftAmtVT));
931 Shift =
932 CurDAG->getTargetNode(SPU::SHLQBYv2i64, VecVT,
933 SDValue(VecOp0, 0), SDValue(Bytes, 0));
934 Shift =
935 CurDAG->getTargetNode(SPU::SHLQBIv2i64, VecVT,
936 SDValue(Shift, 0), SDValue(Bits, 0));
937 }
938
939 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
940}
941
942/*!
943 * Emit the instruction sequence for i64 logical right shifts.
944 *
945 * @param Op The shl operand
946 * @param OpVT Op's machine value value type (doesn't need to be passed, but
947 * makes life easier.)
948 * @return The SDNode with the entire instruction sequence
949 */
950SDNode *
951SPUDAGToDAGISel::SelectSRLi64(SDValue &Op, MVT OpVT) {
952 SDValue Op0 = Op.getOperand(0);
953 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
954 SDValue ShiftAmt = Op.getOperand(1);
955 MVT ShiftAmtVT = ShiftAmt.getValueType();
956 SDNode *VecOp0, *Shift = 0;
957
958 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op0);
959
960 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
961 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
962 unsigned bits = unsigned(CN->getZExtValue()) & 7;
963
964 if (bytes > 0) {
965 Shift =
966 CurDAG->getTargetNode(SPU::ROTQMBYIv2i64, VecVT,
967 SDValue(VecOp0, 0),
968 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
969 }
970
971 if (bits > 0) {
972 Shift =
973 CurDAG->getTargetNode(SPU::ROTQMBIIv2i64, VecVT,
974 SDValue((Shift != 0 ? Shift : VecOp0), 0),
975 CurDAG->getTargetConstant(bits, ShiftAmtVT));
976 }
977 } else {
978 SDNode *Bytes =
979 CurDAG->getTargetNode(SPU::ROTMIr32, ShiftAmtVT,
980 ShiftAmt,
981 CurDAG->getTargetConstant(3, ShiftAmtVT));
982 SDNode *Bits =
983 CurDAG->getTargetNode(SPU::ANDIr32, ShiftAmtVT,
984 ShiftAmt,
985 CurDAG->getTargetConstant(7, ShiftAmtVT));
986
987 // Ensure that the shift amounts are negated!
988 Bytes = CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
989 SDValue(Bytes, 0),
990 CurDAG->getTargetConstant(0, ShiftAmtVT));
991
992 Bits = CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
993 SDValue(Bits, 0),
994 CurDAG->getTargetConstant(0, ShiftAmtVT));
995
996 Shift =
997 CurDAG->getTargetNode(SPU::ROTQMBYv2i64, VecVT,
998 SDValue(VecOp0, 0), SDValue(Bytes, 0));
999 Shift =
1000 CurDAG->getTargetNode(SPU::ROTQMBIv2i64, VecVT,
1001 SDValue(Shift, 0), SDValue(Bits, 0));
1002 }
1003
1004 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
1005}
1006
1007/*!
1008 * Emit the instruction sequence for i64 arithmetic right shifts.
1009 *
1010 * @param Op The shl operand
1011 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1012 * makes life easier.)
1013 * @return The SDNode with the entire instruction sequence
1014 */
1015SDNode *
1016SPUDAGToDAGISel::SelectSRAi64(SDValue &Op, MVT OpVT) {
1017 // Promote Op0 to vector
1018 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
1019 SDValue ShiftAmt = Op.getOperand(1);
1020 MVT ShiftAmtVT = ShiftAmt.getValueType();
1021
1022 SDNode *VecOp0 =
1023 CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op.getOperand(0));
1024
1025 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1026 SDNode *SignRot =
1027 CurDAG->getTargetNode(SPU::ROTMAIv2i64_i32, MVT::v2i64,
1028 SDValue(VecOp0, 0), SignRotAmt);
1029 SDNode *UpperHalfSign =
1030 CurDAG->getTargetNode(SPU::ORi32_v4i32, MVT::i32, SDValue(SignRot, 0));
1031
1032 SDNode *UpperHalfSignMask =
1033 CurDAG->getTargetNode(SPU::FSM64r32, VecVT, SDValue(UpperHalfSign, 0));
1034 SDNode *UpperLowerMask =
1035 CurDAG->getTargetNode(SPU::FSMBIv2i64, VecVT,
1036 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1037 SDNode *UpperLowerSelect =
1038 CurDAG->getTargetNode(SPU::SELBv2i64, VecVT,
1039 SDValue(UpperHalfSignMask, 0),
1040 SDValue(VecOp0, 0),
1041 SDValue(UpperLowerMask, 0));
1042
1043 SDNode *Shift = 0;
1044
1045 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1046 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1047 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1048
1049 if (bytes > 0) {
1050 bytes = 31 - bytes;
1051 Shift =
1052 CurDAG->getTargetNode(SPU::ROTQBYIv2i64, VecVT,
1053 SDValue(UpperLowerSelect, 0),
1054 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1055 }
1056
1057 if (bits > 0) {
1058 bits = 8 - bits;
1059 Shift =
1060 CurDAG->getTargetNode(SPU::ROTQBIIv2i64, VecVT,
1061 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1062 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1063 }
1064 } else {
1065 SDNode *NegShift =
1066 CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
1067 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1068
1069 Shift =
1070 CurDAG->getTargetNode(SPU::ROTQBYBIv2i64_r32, VecVT,
1071 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1072 Shift =
1073 CurDAG->getTargetNode(SPU::ROTQBIv2i64, VecVT,
1074 SDValue(Shift, 0), SDValue(NegShift, 0));
1075 }
1076
1077 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
1078}
1079
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001080/*!
1081 Do the necessary magic necessary to load a i64 constant
1082 */
1083SDNode *SPUDAGToDAGISel::SelectI64Constant(SDValue& Op, MVT OpVT) {
1084 ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
1085 MVT OpVecVT = MVT::getVectorVT(OpVT, 2);
1086 SDValue i64vec =
1087 SPU::LowerSplat_v2i64(OpVecVT, *CurDAG, CN->getZExtValue());
1088
1089 // Here's where it gets interesting, because we have to parse out the
1090 // subtree handed back in i64vec:
1091
1092 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1093 // The degenerate case where the upper and lower bits in the splat are
1094 // identical:
1095 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001096
Scott Michel9de57a92009-01-26 22:33:37 +00001097 ReplaceUses(i64vec, Op0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001098 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT,
1099 SDValue(emitBuildVector(Op0), 0));
1100 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1101 SDValue lhs = i64vec.getOperand(0);
1102 SDValue rhs = i64vec.getOperand(1);
1103 SDValue shufmask = i64vec.getOperand(2);
1104
1105 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1106 ReplaceUses(lhs, lhs.getOperand(0));
1107 lhs = lhs.getOperand(0);
1108 }
1109
1110 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1111 ? lhs.getNode()
1112 : emitBuildVector(lhs));
1113
1114 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1115 ReplaceUses(rhs, rhs.getOperand(0));
1116 rhs = rhs.getOperand(0);
1117 }
1118
1119 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1120 ? rhs.getNode()
1121 : emitBuildVector(rhs));
Scott Michel9de57a92009-01-26 22:33:37 +00001122
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001123 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1124 ReplaceUses(shufmask, shufmask.getOperand(0));
1125 shufmask = shufmask.getOperand(0);
1126 }
1127
1128 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1129 ? shufmask.getNode()
1130 : emitBuildVector(shufmask));
1131
1132 SDNode *shufNode =
1133 Select(CurDAG->getNode(SPUISD::SHUFB, OpVecVT,
1134 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1135 SDValue(shufMaskNode, 0)));
1136
1137 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(shufNode, 0));
1138 } else {
1139 cerr << "SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec condition\n";
1140 abort();
1141 }
1142}
1143
Scott Michel02d711b2008-12-30 23:28:25 +00001144/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001145/// SPU-specific DAG, ready for instruction scheduling.
1146///
1147FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1148 return new SPUDAGToDAGISel(TM);
1149}