blob: 9ac0e2e256c6ca83fa6a20549b456724ed82d1c3 [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 Michel266bc8f2007-12-04 22:23:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Constants.h"
29#include "llvm/GlobalValue.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/Compiler.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000034
35using namespace llvm;
36
37namespace {
38 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
39 bool
40 isI64IntS10Immediate(ConstantSDNode *CN)
41 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000042 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000043 }
44
45 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
46 bool
47 isI32IntS10Immediate(ConstantSDNode *CN)
48 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000049 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000050 }
51
52#if 0
53 //! SDNode predicate for sign-extended, 10-bit immediate values
54 bool
55 isI32IntS10Immediate(SDNode *N)
56 {
57 return (N->getOpcode() == ISD::Constant
58 && isI32IntS10Immediate(cast<ConstantSDNode>(N)));
59 }
60#endif
61
Scott Michel504c3692007-12-17 22:32:34 +000062 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
63 bool
64 isI32IntU10Immediate(ConstantSDNode *CN)
65 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000066 return isU10Constant(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000067 }
68
Scott Michel266bc8f2007-12-04 22:23:35 +000069 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
70 bool
71 isI16IntS10Immediate(ConstantSDNode *CN)
72 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000073 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000074 }
75
76 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
77 bool
78 isI16IntS10Immediate(SDNode *N)
79 {
80 return (N->getOpcode() == ISD::Constant
81 && isI16IntS10Immediate(cast<ConstantSDNode>(N)));
82 }
83
Scott Michelec2a08f2007-12-15 00:38:50 +000084 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
85 bool
86 isI16IntU10Immediate(ConstantSDNode *CN)
87 {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000088 return isU10Constant((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000089 }
90
91 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
92 bool
93 isI16IntU10Immediate(SDNode *N)
94 {
95 return (N->getOpcode() == ISD::Constant
96 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
97 }
98
Scott Michel266bc8f2007-12-04 22:23:35 +000099 //! ConstantSDNode predicate for signed 16-bit values
100 /*!
101 \arg CN The constant SelectionDAG node holding the value
102 \arg Imm The returned 16-bit value, if returning true
103
104 This predicate tests the value in \a CN to see whether it can be
105 represented as a 16-bit, sign-extended quantity. Returns true if
106 this is the case.
107 */
108 bool
109 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
110 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000111 MVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000112 Imm = (short) CN->getZExtValue();
Duncan Sands8e4eb092008-06-08 20:54:56 +0000113 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000114 return true;
115 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000116 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000117 short s_val = (short) i_val;
118 return i_val == s_val;
119 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000120 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000121 short s_val = (short) i_val;
122 return i_val == s_val;
123 }
124
125 return false;
126 }
127
128 //! SDNode predicate for signed 16-bit values.
129 bool
130 isIntS16Immediate(SDNode *N, short &Imm)
131 {
132 return (N->getOpcode() == ISD::Constant
133 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
134 }
135
136 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
137 static bool
138 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
139 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000140 MVT vt = FPN->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000141 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000142 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000143 int sval = (int) ((val << 16) >> 16);
144 Imm = (short) val;
145 return val == sval;
146 }
147
148 return false;
149 }
150
Scott Michel053c1da2008-01-29 02:16:57 +0000151 bool
Dan Gohman475871a2008-07-27 21:46:04 +0000152 isHighLow(const SDValue &Op)
Scott Michel053c1da2008-01-29 02:16:57 +0000153 {
154 return (Op.getOpcode() == SPUISD::IndirectAddr
155 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
156 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
157 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
158 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
159 }
160
Scott Michel266bc8f2007-12-04 22:23:35 +0000161 //===------------------------------------------------------------------===//
Duncan Sands83ec4b62008-06-06 12:08:01 +0000162 //! MVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000163
164 struct valtype_map_s {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000165 MVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000166 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000167 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000168 int prefslot_byte; /// Byte offset of the "preferred" slot
Scott Michel266bc8f2007-12-04 22:23:35 +0000169 };
170
171 const valtype_map_s valtype_map[] = {
Scott Michela59d4692008-02-23 18:41:37 +0000172 { MVT::i1, 0, false, 3 },
173 { MVT::i8, SPU::ORBIr8, true, 3 },
174 { MVT::i16, SPU::ORHIr16, true, 2 },
175 { MVT::i32, SPU::ORIr32, true, 0 },
176 { MVT::i64, SPU::ORr64, false, 0 },
177 { MVT::f32, SPU::ORf32, false, 0 },
178 { MVT::f64, SPU::ORf64, false, 0 },
Scott Michel58c58182008-01-17 20:38:41 +0000179 // vector types... (sigh!)
Scott Michela59d4692008-02-23 18:41:37 +0000180 { MVT::v16i8, 0, false, 0 },
181 { MVT::v8i16, 0, false, 0 },
182 { MVT::v4i32, 0, false, 0 },
183 { MVT::v2i64, 0, false, 0 },
184 { MVT::v4f32, 0, false, 0 },
185 { MVT::v2f64, 0, false, 0 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000186 };
187
188 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
189
Duncan Sands83ec4b62008-06-06 12:08:01 +0000190 const valtype_map_s *getValueTypeMapEntry(MVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000191 {
192 const valtype_map_s *retval = 0;
193 for (size_t i = 0; i < n_valtype_map; ++i) {
194 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000195 retval = valtype_map + i;
196 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000197 }
198 }
199
200
201#ifndef NDEBUG
202 if (retval == 0) {
203 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000204 << VT.getMVTString()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000205 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000206 abort();
207 }
208#endif
209
210 return retval;
211 }
212}
213
Dan Gohman844731a2008-05-13 00:00:25 +0000214namespace {
215
Scott Michel266bc8f2007-12-04 22:23:35 +0000216//===--------------------------------------------------------------------===//
217/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
218/// instructions for SelectionDAG operations.
219///
220class SPUDAGToDAGISel :
221 public SelectionDAGISel
222{
223 SPUTargetMachine &TM;
224 SPUTargetLowering &SPUtli;
225 unsigned GlobalBaseReg;
226
227public:
Dan Gohman1002c022008-07-07 18:00:37 +0000228 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
Scott Michel266bc8f2007-12-04 22:23:35 +0000229 SelectionDAGISel(*tm.getTargetLowering()),
230 TM(tm),
231 SPUtli(*tm.getTargetLowering())
232 {}
233
234 virtual bool runOnFunction(Function &Fn) {
235 // Make sure we re-emit a set of the global base reg if necessary
236 GlobalBaseReg = 0;
237 SelectionDAGISel::runOnFunction(Fn);
238 return true;
239 }
240
241 /// getI32Imm - Return a target constant with the specified value, of type
242 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000243 inline SDValue getI32Imm(uint32_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000244 return CurDAG->getTargetConstant(Imm, MVT::i32);
245 }
246
247 /// getI64Imm - Return a target constant with the specified value, of type
248 /// i64.
Dan Gohman475871a2008-07-27 21:46:04 +0000249 inline SDValue getI64Imm(uint64_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000250 return CurDAG->getTargetConstant(Imm, MVT::i64);
251 }
252
253 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman475871a2008-07-27 21:46:04 +0000254 inline SDValue getSmallIPtrImm(unsigned Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000255 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
256 }
257
258 /// Select - Convert the specified operand from a target-independent to a
259 /// target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +0000260 SDNode *Select(SDValue Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000261
Scott Michel266bc8f2007-12-04 22:23:35 +0000262 //! Returns true if the address N is an A-form (local store) address
Dan Gohman475871a2008-07-27 21:46:04 +0000263 bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
264 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000265
266 //! D-form address predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000267 bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
268 SDValue &Index);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000269
270 /// Alternate D-form address using i7 offset predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000271 bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
272 SDValue &Base);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000273
274 /// D-form address selection workhorse
Dan Gohman475871a2008-07-27 21:46:04 +0000275 bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
276 SDValue &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000277
278 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000279 bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
280 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000281
282 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
283 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000284 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000285 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000286 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +0000287 SDValue Op0, Op1;
Scott Michel266bc8f2007-12-04 22:23:35 +0000288 switch (ConstraintCode) {
289 default: return true;
290 case 'm': // memory
291 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000292 && !SelectAFormAddr(Op, Op, Op0, Op1))
293 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000294 break;
295 case 'o': // offsetable
296 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000297 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
298 Op0 = Op;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000299 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000300 }
301 break;
302 case 'v': // not offsetable
303#if 1
304 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
305#else
306 SelectAddrIdxOnly(Op, Op, Op0, Op1);
307#endif
308 break;
309 }
310
311 OutOps.push_back(Op0);
312 OutOps.push_back(Op1);
313 return false;
314 }
315
Evan Chengdb8d56b2008-06-30 20:45:06 +0000316 /// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000317 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000318 virtual void InstructionSelect();
Scott Michel266bc8f2007-12-04 22:23:35 +0000319
320 virtual const char *getPassName() const {
321 return "Cell SPU DAG->DAG Pattern Instruction Selection";
322 }
323
324 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
325 /// this target when scheduling the DAG.
326 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohman6448d912008-09-04 15:39:15 +0000327 const TargetInstrInfo *II = TM.getInstrInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +0000328 assert(II && "No InstrInfo?");
329 return new SPUHazardRecognizer(*II);
330 }
331
332 // Include the pieces autogenerated from the target description.
333#include "SPUGenDAGISel.inc"
334};
335
Dan Gohman844731a2008-05-13 00:00:25 +0000336}
337
Evan Chengdb8d56b2008-06-30 20:45:06 +0000338/// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000339/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
340void
Dan Gohmanf350b272008-08-23 02:25:05 +0000341SPUDAGToDAGISel::InstructionSelect()
Scott Michel266bc8f2007-12-04 22:23:35 +0000342{
343 DEBUG(BB->dump());
344
345 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000346 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +0000347 CurDAG->RemoveDeadNodes();
Scott Michel266bc8f2007-12-04 22:23:35 +0000348}
349
Scott Michel266bc8f2007-12-04 22:23:35 +0000350/*!
351 \arg Op The ISD instructio operand
352 \arg N The address to be tested
353 \arg Base The base address
354 \arg Index The base address index
355 */
356bool
Dan Gohman475871a2008-07-27 21:46:04 +0000357SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
358 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000359 // These match the addr256k operand type:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000360 MVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000361 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000362
363 switch (N.getOpcode()) {
364 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000365 case ISD::ConstantPool:
366 case ISD::GlobalAddress:
367 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
368 abort();
369 /*NOTREACHED*/
370
Scott Michel053c1da2008-01-29 02:16:57 +0000371 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000372 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000373 case ISD::TargetJumpTable:
374 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
375 << "A-form address.\n";
376 abort();
377 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000378
Scott Michel053c1da2008-01-29 02:16:57 +0000379 case SPUISD::AFormAddr:
380 // Just load from memory if there's only a single use of the location,
381 // otherwise, this will get handled below with D-form offset addresses
382 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000383 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000384 switch (Op0.getOpcode()) {
385 case ISD::TargetConstantPool:
386 case ISD::TargetJumpTable:
387 Base = Op0;
388 Index = Zero;
389 return true;
390
391 case ISD::TargetGlobalAddress: {
392 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
393 GlobalValue *GV = GSDN->getGlobal();
394 if (GV->getAlignment() == 16) {
395 Base = Op0;
396 Index = Zero;
397 return true;
398 }
399 break;
400 }
401 }
402 }
403 break;
404 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000405 return false;
406}
407
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000408bool
Dan Gohman475871a2008-07-27 21:46:04 +0000409SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
410 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000411 const int minDForm2Offset = -(1 << 7);
412 const int maxDForm2Offset = (1 << 7) - 1;
413 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
414 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000415}
416
Scott Michel266bc8f2007-12-04 22:23:35 +0000417/*!
418 \arg Op The ISD instruction (ignored)
419 \arg N The address to be tested
420 \arg Base Base address register/pointer
421 \arg Index Base address index
422
423 Examine the input address by a base register plus a signed 10-bit
424 displacement, [r+I10] (D-form address).
425
426 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000427 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000428*/
429bool
Dan Gohman475871a2008-07-27 21:46:04 +0000430SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
431 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000432 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000433 SPUFrameInfo::minFrameOffset(),
434 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000435}
436
437bool
Dan Gohman475871a2008-07-27 21:46:04 +0000438SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
439 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000440 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000441 unsigned Opc = N.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000442 MVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000443
Scott Michel053c1da2008-01-29 02:16:57 +0000444 if (Opc == ISD::FrameIndex) {
445 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000446 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
447 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000448 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000449 << FI << "\n");
450 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000451 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000452 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000453 return true;
454 }
455 } else if (Opc == ISD::ADD) {
456 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000457 const SDValue Op0 = N.getOperand(0);
458 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000459
Scott Michel053c1da2008-01-29 02:16:57 +0000460 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
461 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
462 Base = CurDAG->getTargetConstant(0, PtrTy);
463 Index = N;
464 return true;
465 } else if (Op1.getOpcode() == ISD::Constant
466 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000467 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000468 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000469
Scott Michel053c1da2008-01-29 02:16:57 +0000470 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000471 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
472 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000473 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000474 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000475
Scott Michel203b2d62008-04-30 00:30:08 +0000476 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000477 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000478 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000479 return true;
480 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000481 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000482 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000483 Index = Op0;
484 return true;
485 }
486 } else if (Op0.getOpcode() == ISD::Constant
487 || Op0.getOpcode() == ISD::TargetConstant) {
488 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000489 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000490
491 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000492 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
493 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000494 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000495 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000496
Scott Michel203b2d62008-04-30 00:30:08 +0000497 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000498 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000499 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000500 return true;
501 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000502 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000503 Base = CurDAG->getTargetConstant(offset, PtrTy);
504 Index = Op1;
505 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000506 }
Scott Michel053c1da2008-01-29 02:16:57 +0000507 }
508 } else if (Opc == SPUISD::IndirectAddr) {
509 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000510 const SDValue Op0 = N.getOperand(0);
511 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000512
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000513 if (Op0.getOpcode() == SPUISD::Hi
514 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000515 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000516 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000517 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000518 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000519 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
520 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000521 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000522
523 if (isa<ConstantSDNode>(Op1)) {
524 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000525 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000526 idxOp = Op0;
527 } else if (isa<ConstantSDNode>(Op0)) {
528 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000529 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000530 idxOp = Op1;
531 }
532
533 if (offset >= minOffset && offset <= maxOffset) {
534 Base = CurDAG->getTargetConstant(offset, PtrTy);
535 Index = idxOp;
536 return true;
537 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000538 }
Scott Michel053c1da2008-01-29 02:16:57 +0000539 } else if (Opc == SPUISD::AFormAddr) {
540 Base = CurDAG->getTargetConstant(0, N.getValueType());
541 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000542 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000543 } else if (Opc == SPUISD::LDRESULT) {
544 Base = CurDAG->getTargetConstant(0, N.getValueType());
545 Index = N;
546 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000547 } else if (Opc == ISD::Register || Opc == ISD::CopyFromReg) {
548 unsigned OpOpc = Op.getOpcode();
549
550 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
551 // Direct load/store without getelementptr
552 SDValue Addr, Offs;
553
554 // Get the register from CopyFromReg
555 if (Opc == ISD::CopyFromReg)
556 Addr = N.getOperand(1);
557 else
558 Addr = N; // Register
559
Scott Michelaedc6372008-12-10 00:15:19 +0000560 Offs = ((OpOpc == ISD::STORE) ? Op.getOperand(3) : Op.getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000561
562 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
563 if (Offs.getOpcode() == ISD::UNDEF)
564 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
565
566 Base = Offs;
567 Index = Addr;
568 return true;
569 }
Scott Michelaedc6372008-12-10 00:15:19 +0000570 } else {
571 /* If otherwise unadorned, default to D-form address with 0 offset: */
572 if (Opc == ISD::CopyFromReg) {
573 Index = N.getOperand(1);
574 } else {
575 Index = N;
576 }
577
578 Base = CurDAG->getTargetConstant(0, Index.getValueType());
579 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000580 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000581 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000582
Scott Michel266bc8f2007-12-04 22:23:35 +0000583 return false;
584}
585
586/*!
587 \arg Op The ISD instruction operand
588 \arg N The address operand
589 \arg Base The base pointer operand
590 \arg Index The offset/index operand
591
Scott Michel9c0c6b22008-11-21 02:56:16 +0000592 If the address \a N can be expressed as an A-form or D-form address, returns
593 false. Otherwise, creates two operands, Base and Index that will become the
594 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000595*/
596bool
Dan Gohman475871a2008-07-27 21:46:04 +0000597SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
598 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000599 if (!SelectAFormAddr(Op, N, Base, Index)
600 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000601 // If the address is neither A-form or D-form, punt and use an X-form
602 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000603 Base = N.getOperand(1);
604 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000605 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000606 }
607
608 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000609}
610
Scott Michel266bc8f2007-12-04 22:23:35 +0000611//! Convert the operand from a target-independent to a target-specific node
612/*!
613 */
614SDNode *
Dan Gohman475871a2008-07-27 21:46:04 +0000615SPUDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000616 SDNode *N = Op.getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000617 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000618 int n_ops = -1;
619 unsigned NewOpc;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000620 MVT OpVT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000621 SDValue Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000622
Dan Gohmane8be6c62008-07-17 19:10:17 +0000623 if (N->isMachineOpcode()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000624 return NULL; // Already selected.
625 } else if (Opc == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000626 // Selects to (add $sp, FI * stackSlotSize)
627 int FI =
628 SPUFrameInfo::FItoStackOffset(cast<FrameIndexSDNode>(N)->getIndex());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 MVT PtrVT = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000630
Scott Michel203b2d62008-04-30 00:30:08 +0000631 // Adjust stack slot to actual offset in frame:
632 if (isS10Constant(FI)) {
633 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AIr32 $sp, "
634 << FI
635 << "\n");
636 NewOpc = SPU::AIr32;
637 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
638 Ops[1] = CurDAG->getTargetConstant(FI, PtrVT);
639 n_ops = 2;
640 } else {
641 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with Ar32 $sp, "
642 << FI
643 << "\n");
644 NewOpc = SPU::Ar32;
645 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
646 Ops[1] = CurDAG->getConstant(FI, PtrVT);
647 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000648 }
Scott Michel58c58182008-01-17 20:38:41 +0000649 } else if (Opc == ISD::ZERO_EXTEND) {
650 // (zero_extend:i16 (and:i8 <arg>, <const>))
Dan Gohman475871a2008-07-27 21:46:04 +0000651 const SDValue &Op1 = N->getOperand(0);
Scott Michel58c58182008-01-17 20:38:41 +0000652
653 if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
654 if (Op1.getOpcode() == ISD::AND) {
655 // Fold this into a single ANDHI. This is often seen in expansions of i1
656 // to i8, then i8 to i16 in logical/branching operations.
657 DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
658 "<arg>, <const>))\n");
Scott Michela59d4692008-02-23 18:41:37 +0000659 NewOpc = SPU::ANDHIi8i16;
Scott Michel58c58182008-01-17 20:38:41 +0000660 Ops[0] = Op1.getOperand(0);
661 Ops[1] = Op1.getOperand(1);
662 n_ops = 2;
663 }
664 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000665 } else if (Opc == SPUISD::LDRESULT) {
666 // Custom select instructions for LDRESULT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000667 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000668 SDValue Arg = N->getOperand(0);
669 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000670 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000671 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
672
673 if (vtm->ldresult_ins == 0) {
674 cerr << "LDRESULT for unsupported type: "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000675 << VT.getMVTString()
Scott Michela59d4692008-02-23 18:41:37 +0000676 << "\n";
677 abort();
678 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000679
Scott Michela59d4692008-02-23 18:41:37 +0000680 Opc = vtm->ldresult_ins;
681 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000682 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000683
Scott Michel58c58182008-01-17 20:38:41 +0000684 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000685 } else {
Scott Michel30ee7df2008-12-04 03:02:42 +0000686 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000687 }
688
Dan Gohman475871a2008-07-27 21:46:04 +0000689 Chain = SDValue(Result, 1);
Scott Michel86c041f2007-12-20 00:44:13 +0000690
Scott Michel266bc8f2007-12-04 22:23:35 +0000691 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000692 } else if (Opc == SPUISD::IndirectAddr) {
Dan Gohman475871a2008-07-27 21:46:04 +0000693 SDValue Op0 = Op.getOperand(0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000694 if (Op0.getOpcode() == SPUISD::LDRESULT) {
695 /* || Op0.getOpcode() == SPUISD::AFormAddr) */
696 // (IndirectAddr (LDRESULT, imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000697 SDValue Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000698 MVT VT = Op.getValueType();
Scott Michel58c58182008-01-17 20:38:41 +0000699
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000700 DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = ");
Gabor Greifba36cb52008-08-28 21:40:38 +0000701 DEBUG(Op.getOperand(0).getNode()->dump(CurDAG));
Scott Michel58c58182008-01-17 20:38:41 +0000702 DEBUG(cerr << "\nOp1 = ");
Gabor Greifba36cb52008-08-28 21:40:38 +0000703 DEBUG(Op.getOperand(1).getNode()->dump(CurDAG));
Scott Michel58c58182008-01-17 20:38:41 +0000704 DEBUG(cerr << "\n");
705
706 if (Op1.getOpcode() == ISD::Constant) {
707 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000708 Op1 = CurDAG->getTargetConstant(CN->getZExtValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000709 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000710 Ops[0] = Op0;
711 Ops[1] = Op1;
712 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000713 }
Scott Michel58c58182008-01-17 20:38:41 +0000714 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000715 }
716
Scott Michel58c58182008-01-17 20:38:41 +0000717 if (n_ops > 0) {
718 if (N->hasOneUse())
719 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
720 else
721 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
722 } else
723 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000724}
725
726/// createPPCISelDag - This pass converts a legalized DAG into a
727/// SPU-specific DAG, ready for instruction scheduling.
728///
729FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
730 return new SPUDAGToDAGISel(TM);
731}