blob: 49a5e864d3e0ab832862c3af9bc539193fd15c54 [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"
34#include <iostream>
35#include <queue>
36#include <set>
37
38using namespace llvm;
39
40namespace {
41 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
42 bool
43 isI64IntS10Immediate(ConstantSDNode *CN)
44 {
Scott Michel78c47fa2008-03-10 16:58:52 +000045 return isS10Constant(CN->getSignExtended());
Scott Michel266bc8f2007-12-04 22:23:35 +000046 }
47
48 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
49 bool
50 isI32IntS10Immediate(ConstantSDNode *CN)
51 {
Scott Michel78c47fa2008-03-10 16:58:52 +000052 return isS10Constant(CN->getSignExtended());
Scott Michel266bc8f2007-12-04 22:23:35 +000053 }
54
55#if 0
56 //! SDNode predicate for sign-extended, 10-bit immediate values
57 bool
58 isI32IntS10Immediate(SDNode *N)
59 {
60 return (N->getOpcode() == ISD::Constant
61 && isI32IntS10Immediate(cast<ConstantSDNode>(N)));
62 }
63#endif
64
Scott Michel504c3692007-12-17 22:32:34 +000065 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
66 bool
67 isI32IntU10Immediate(ConstantSDNode *CN)
68 {
Scott Michel79698f62008-03-20 00:51:36 +000069 return isU10Constant(CN->getSignExtended());
Scott Michel504c3692007-12-17 22:32:34 +000070 }
71
Scott Michel266bc8f2007-12-04 22:23:35 +000072 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
73 bool
74 isI16IntS10Immediate(ConstantSDNode *CN)
75 {
Scott Michel79698f62008-03-20 00:51:36 +000076 return isS10Constant(CN->getSignExtended());
Scott Michel266bc8f2007-12-04 22:23:35 +000077 }
78
79 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
80 bool
81 isI16IntS10Immediate(SDNode *N)
82 {
83 return (N->getOpcode() == ISD::Constant
84 && isI16IntS10Immediate(cast<ConstantSDNode>(N)));
85 }
86
Scott Michelec2a08f2007-12-15 00:38:50 +000087 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
88 bool
89 isI16IntU10Immediate(ConstantSDNode *CN)
90 {
91 return isU10Constant((short) CN->getValue());
92 }
93
94 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
95 bool
96 isI16IntU10Immediate(SDNode *N)
97 {
98 return (N->getOpcode() == ISD::Constant
99 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
100 }
101
Scott Michel266bc8f2007-12-04 22:23:35 +0000102 //! ConstantSDNode predicate for signed 16-bit values
103 /*!
104 \arg CN The constant SelectionDAG node holding the value
105 \arg Imm The returned 16-bit value, if returning true
106
107 This predicate tests the value in \a CN to see whether it can be
108 represented as a 16-bit, sign-extended quantity. Returns true if
109 this is the case.
110 */
111 bool
112 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
113 {
114 MVT::ValueType vt = CN->getValueType(0);
115 Imm = (short) CN->getValue();
116 if (vt >= MVT::i1 && vt <= MVT::i16) {
117 return true;
118 } else if (vt == MVT::i32) {
119 int32_t i_val = (int32_t) CN->getValue();
120 short s_val = (short) i_val;
121 return i_val == s_val;
122 } else {
123 int64_t i_val = (int64_t) CN->getValue();
124 short s_val = (short) i_val;
125 return i_val == s_val;
126 }
127
128 return false;
129 }
130
131 //! SDNode predicate for signed 16-bit values.
132 bool
133 isIntS16Immediate(SDNode *N, short &Imm)
134 {
135 return (N->getOpcode() == ISD::Constant
136 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
137 }
138
139 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
140 static bool
141 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
142 {
143 MVT::ValueType vt = FPN->getValueType(0);
144 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000145 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000146 int sval = (int) ((val << 16) >> 16);
147 Imm = (short) val;
148 return val == sval;
149 }
150
151 return false;
152 }
153
Scott Michel053c1da2008-01-29 02:16:57 +0000154 bool
155 isHighLow(const SDOperand &Op)
156 {
157 return (Op.getOpcode() == SPUISD::IndirectAddr
158 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
159 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
160 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
161 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
162 }
163
Scott Michel266bc8f2007-12-04 22:23:35 +0000164 //===------------------------------------------------------------------===//
Scott Michel86c041f2007-12-20 00:44:13 +0000165 //! MVT::ValueType to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000166
167 struct valtype_map_s {
168 MVT::ValueType VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000169 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000170 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000171 int prefslot_byte; /// Byte offset of the "preferred" slot
Scott Michel266bc8f2007-12-04 22:23:35 +0000172 };
173
174 const valtype_map_s valtype_map[] = {
Scott Michela59d4692008-02-23 18:41:37 +0000175 { MVT::i1, 0, false, 3 },
176 { MVT::i8, SPU::ORBIr8, true, 3 },
177 { MVT::i16, SPU::ORHIr16, true, 2 },
178 { MVT::i32, SPU::ORIr32, true, 0 },
179 { MVT::i64, SPU::ORr64, false, 0 },
180 { MVT::f32, SPU::ORf32, false, 0 },
181 { MVT::f64, SPU::ORf64, false, 0 },
Scott Michel58c58182008-01-17 20:38:41 +0000182 // vector types... (sigh!)
Scott Michela59d4692008-02-23 18:41:37 +0000183 { MVT::v16i8, 0, false, 0 },
184 { MVT::v8i16, 0, false, 0 },
185 { MVT::v4i32, 0, false, 0 },
186 { MVT::v2i64, 0, false, 0 },
187 { MVT::v4f32, 0, false, 0 },
188 { MVT::v2f64, 0, false, 0 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000189 };
190
191 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
192
193 const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT)
194 {
195 const valtype_map_s *retval = 0;
196 for (size_t i = 0; i < n_valtype_map; ++i) {
197 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000198 retval = valtype_map + i;
199 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000200 }
201 }
202
203
204#ifndef NDEBUG
205 if (retval == 0) {
206 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000207 << MVT::getValueTypeString(VT)
208 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000209 abort();
210 }
211#endif
212
213 return retval;
214 }
215}
216
217//===--------------------------------------------------------------------===//
218/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
219/// instructions for SelectionDAG operations.
220///
221class SPUDAGToDAGISel :
222 public SelectionDAGISel
223{
224 SPUTargetMachine &TM;
225 SPUTargetLowering &SPUtli;
226 unsigned GlobalBaseReg;
227
228public:
229 SPUDAGToDAGISel(SPUTargetMachine &tm) :
230 SelectionDAGISel(*tm.getTargetLowering()),
231 TM(tm),
232 SPUtli(*tm.getTargetLowering())
233 {}
234
235 virtual bool runOnFunction(Function &Fn) {
236 // Make sure we re-emit a set of the global base reg if necessary
237 GlobalBaseReg = 0;
238 SelectionDAGISel::runOnFunction(Fn);
239 return true;
240 }
241
242 /// getI32Imm - Return a target constant with the specified value, of type
243 /// i32.
244 inline SDOperand getI32Imm(uint32_t Imm) {
245 return CurDAG->getTargetConstant(Imm, MVT::i32);
246 }
247
248 /// getI64Imm - Return a target constant with the specified value, of type
249 /// i64.
250 inline SDOperand getI64Imm(uint64_t Imm) {
251 return CurDAG->getTargetConstant(Imm, MVT::i64);
252 }
253
254 /// getSmallIPtrImm - Return a target constant of pointer type.
255 inline SDOperand getSmallIPtrImm(unsigned Imm) {
256 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
257 }
258
259 /// Select - Convert the specified operand from a target-independent to a
260 /// target-specific node if it hasn't already been changed.
261 SDNode *Select(SDOperand Op);
262
Scott Michel266bc8f2007-12-04 22:23:35 +0000263 //! Returns true if the address N is an A-form (local store) address
264 bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000265 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000266
267 //! D-form address predicate
268 bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000269 SDOperand &Index);
270
271 /// Alternate D-form address using i7 offset predicate
272 bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
273 SDOperand &Base);
274
275 /// D-form address selection workhorse
276 bool DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Disp,
277 SDOperand &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000278
279 //! Address predicate if N can be expressed as an indexed [r+r] operation.
280 bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000281 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000282
283 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
284 /// inline asm expressions.
285 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000286 char ConstraintCode,
287 std::vector<SDOperand> &OutOps,
288 SelectionDAG &DAG) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000289 SDOperand Op0, Op1;
290 switch (ConstraintCode) {
291 default: return true;
292 case 'm': // memory
293 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000294 && !SelectAFormAddr(Op, Op, Op0, Op1))
295 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000296 break;
297 case 'o': // offsetable
298 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000299 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
300 Op0 = Op;
301 AddToISelQueue(Op0); // r+0.
302 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000303 }
304 break;
305 case 'v': // not offsetable
306#if 1
307 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
308#else
309 SelectAddrIdxOnly(Op, Op, Op0, Op1);
310#endif
311 break;
312 }
313
314 OutOps.push_back(Op0);
315 OutOps.push_back(Op1);
316 return false;
317 }
318
319 /// InstructionSelectBasicBlock - This callback is invoked by
320 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
321 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
322
323 virtual const char *getPassName() const {
324 return "Cell SPU DAG->DAG Pattern Instruction Selection";
325 }
326
327 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
328 /// this target when scheduling the DAG.
329 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
330 const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
331 assert(II && "No InstrInfo?");
332 return new SPUHazardRecognizer(*II);
333 }
334
335 // Include the pieces autogenerated from the target description.
336#include "SPUGenDAGISel.inc"
337};
338
339/// InstructionSelectBasicBlock - This callback is invoked by
340/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
341void
342SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG)
343{
344 DEBUG(BB->dump());
345
346 // Select target instructions for the DAG.
347 DAG.setRoot(SelectRoot(DAG.getRoot()));
348 DAG.RemoveDeadNodes();
349
350 // Emit machine code to BB.
351 ScheduleAndEmitDAG(DAG);
352}
353
Scott Michel266bc8f2007-12-04 22:23:35 +0000354/*!
355 \arg Op The ISD instructio operand
356 \arg N The address to be tested
357 \arg Base The base address
358 \arg Index The base address index
359 */
360bool
361SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000362 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000363 // These match the addr256k operand type:
Scott Michel266bc8f2007-12-04 22:23:35 +0000364 MVT::ValueType OffsVT = MVT::i16;
Scott Michel053c1da2008-01-29 02:16:57 +0000365 SDOperand Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000366
367 switch (N.getOpcode()) {
368 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000369 case ISD::ConstantPool:
370 case ISD::GlobalAddress:
371 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
372 abort();
373 /*NOTREACHED*/
374
Scott Michel053c1da2008-01-29 02:16:57 +0000375 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000376 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000377 case ISD::TargetJumpTable:
378 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
379 << "A-form address.\n";
380 abort();
381 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000382
Scott Michel053c1da2008-01-29 02:16:57 +0000383 case SPUISD::AFormAddr:
384 // Just load from memory if there's only a single use of the location,
385 // otherwise, this will get handled below with D-form offset addresses
386 if (N.hasOneUse()) {
387 SDOperand Op0 = N.getOperand(0);
388 switch (Op0.getOpcode()) {
389 case ISD::TargetConstantPool:
390 case ISD::TargetJumpTable:
391 Base = Op0;
392 Index = Zero;
393 return true;
394
395 case ISD::TargetGlobalAddress: {
396 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
397 GlobalValue *GV = GSDN->getGlobal();
398 if (GV->getAlignment() == 16) {
399 Base = Op0;
400 Index = Zero;
401 return true;
402 }
403 break;
404 }
405 }
406 }
407 break;
408 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000409 return false;
410}
411
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000412bool
413SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
414 SDOperand &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000415 const int minDForm2Offset = -(1 << 7);
416 const int maxDForm2Offset = (1 << 7) - 1;
417 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
418 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000419}
420
Scott Michel266bc8f2007-12-04 22:23:35 +0000421/*!
422 \arg Op The ISD instruction (ignored)
423 \arg N The address to be tested
424 \arg Base Base address register/pointer
425 \arg Index Base address index
426
427 Examine the input address by a base register plus a signed 10-bit
428 displacement, [r+I10] (D-form address).
429
430 \return true if \a N is a D-form address with \a Base and \a Index set
431 to non-empty SDOperand instances.
432*/
433bool
434SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000435 SDOperand &Index) {
436 return DFormAddressPredicate(Op, N, Base, Index,
437 SPUFrameInfo::minFrameOffset(),
438 SPUFrameInfo::maxFrameOffset());
439}
440
441bool
442SPUDAGToDAGISel::DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Base,
443 SDOperand &Index, int minOffset,
444 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000445 unsigned Opc = N.getOpcode();
446 unsigned PtrTy = SPUtli.getPointerTy();
447
Scott Michel053c1da2008-01-29 02:16:57 +0000448 if (Opc == ISD::FrameIndex) {
449 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000450 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
451 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000452 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000453 << FI << "\n");
454 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000455 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000456 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000457 return true;
458 }
459 } else if (Opc == ISD::ADD) {
460 // Generated by getelementptr
Scott Michel053c1da2008-01-29 02:16:57 +0000461 const SDOperand Op0 = N.getOperand(0);
462 const SDOperand Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000463
Scott Michel053c1da2008-01-29 02:16:57 +0000464 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
465 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
466 Base = CurDAG->getTargetConstant(0, PtrTy);
467 Index = N;
468 return true;
469 } else if (Op1.getOpcode() == ISD::Constant
470 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000471 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Scott Michel053c1da2008-01-29 02:16:57 +0000472 int32_t offset = int32_t(CN->getSignExtended());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000473
Scott Michel053c1da2008-01-29 02:16:57 +0000474 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000475 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
476 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000477 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000478 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000479
Scott Michel203b2d62008-04-30 00:30:08 +0000480 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000481 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000482 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000483 return true;
484 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000485 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000486 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000487 Index = Op0;
488 return true;
489 }
490 } else if (Op0.getOpcode() == ISD::Constant
491 || Op0.getOpcode() == ISD::TargetConstant) {
492 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
493 int32_t offset = int32_t(CN->getSignExtended());
494
495 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000496 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
497 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000498 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000499 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000500
Scott Michel203b2d62008-04-30 00:30:08 +0000501 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000502 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000503 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000504 return true;
505 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000506 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000507 Base = CurDAG->getTargetConstant(offset, PtrTy);
508 Index = Op1;
509 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000510 }
Scott Michel053c1da2008-01-29 02:16:57 +0000511 }
512 } else if (Opc == SPUISD::IndirectAddr) {
513 // Indirect with constant offset -> D-Form address
514 const SDOperand Op0 = N.getOperand(0);
515 const SDOperand Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000516
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000517 if (Op0.getOpcode() == SPUISD::Hi
518 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000519 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000520 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000521 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000522 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000523 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
524 int32_t offset = 0;
525 SDOperand idxOp;
526
527 if (isa<ConstantSDNode>(Op1)) {
528 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
529 offset = int32_t(CN->getSignExtended());
530 idxOp = Op0;
531 } else if (isa<ConstantSDNode>(Op0)) {
532 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
533 offset = int32_t(CN->getSignExtended());
534 idxOp = Op1;
535 }
536
537 if (offset >= minOffset && offset <= maxOffset) {
538 Base = CurDAG->getTargetConstant(offset, PtrTy);
539 Index = idxOp;
540 return true;
541 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000542 }
Scott Michel053c1da2008-01-29 02:16:57 +0000543 } else if (Opc == SPUISD::AFormAddr) {
544 Base = CurDAG->getTargetConstant(0, N.getValueType());
545 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000546 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000547 } else if (Opc == SPUISD::LDRESULT) {
548 Base = CurDAG->getTargetConstant(0, N.getValueType());
549 Index = N;
550 return true;
Scott Michel266bc8f2007-12-04 22:23:35 +0000551 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000552 return false;
553}
554
555/*!
556 \arg Op The ISD instruction operand
557 \arg N The address operand
558 \arg Base The base pointer operand
559 \arg Index The offset/index operand
560
561 If the address \a N can be expressed as a [r + s10imm] address, returns false.
562 Otherwise, creates two operands, Base and Index that will become the [r+r]
563 address.
564*/
565bool
566SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000567 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000568 if (SelectAFormAddr(Op, N, Base, Index)
569 || SelectDFormAddr(Op, N, Base, Index))
570 return false;
571
Scott Michel053c1da2008-01-29 02:16:57 +0000572 // All else fails, punt and use an X-form address:
573 Base = N.getOperand(0);
574 Index = N.getOperand(1);
575 return true;
Scott Michel58c58182008-01-17 20:38:41 +0000576}
577
Scott Michel266bc8f2007-12-04 22:23:35 +0000578//! Convert the operand from a target-independent to a target-specific node
579/*!
580 */
581SDNode *
582SPUDAGToDAGISel::Select(SDOperand Op) {
583 SDNode *N = Op.Val;
584 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000585 int n_ops = -1;
586 unsigned NewOpc;
587 MVT::ValueType OpVT = Op.getValueType();
588 SDOperand Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000589
590 if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) {
591 return NULL; // Already selected.
592 } else if (Opc == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000593 // Selects to (add $sp, FI * stackSlotSize)
594 int FI =
595 SPUFrameInfo::FItoStackOffset(cast<FrameIndexSDNode>(N)->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000596 MVT::ValueType PtrVT = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000597
Scott Michel203b2d62008-04-30 00:30:08 +0000598 // Adjust stack slot to actual offset in frame:
599 if (isS10Constant(FI)) {
600 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AIr32 $sp, "
601 << FI
602 << "\n");
603 NewOpc = SPU::AIr32;
604 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
605 Ops[1] = CurDAG->getTargetConstant(FI, PtrVT);
606 n_ops = 2;
607 } else {
608 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with Ar32 $sp, "
609 << FI
610 << "\n");
611 NewOpc = SPU::Ar32;
612 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
613 Ops[1] = CurDAG->getConstant(FI, PtrVT);
614 n_ops = 2;
615
616 AddToISelQueue(Ops[1]);
617 }
Scott Michel58c58182008-01-17 20:38:41 +0000618 } else if (Opc == ISD::ZERO_EXTEND) {
619 // (zero_extend:i16 (and:i8 <arg>, <const>))
620 const SDOperand &Op1 = N->getOperand(0);
621
622 if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
623 if (Op1.getOpcode() == ISD::AND) {
624 // Fold this into a single ANDHI. This is often seen in expansions of i1
625 // to i8, then i8 to i16 in logical/branching operations.
626 DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
627 "<arg>, <const>))\n");
Scott Michela59d4692008-02-23 18:41:37 +0000628 NewOpc = SPU::ANDHIi8i16;
Scott Michel58c58182008-01-17 20:38:41 +0000629 Ops[0] = Op1.getOperand(0);
630 Ops[1] = Op1.getOperand(1);
631 n_ops = 2;
632 }
633 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000634 } else if (Opc == SPUISD::LDRESULT) {
635 // Custom select instructions for LDRESULT
636 unsigned VT = N->getValueType(0);
637 SDOperand Arg = N->getOperand(0);
638 SDOperand Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000639 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000640 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
641
642 if (vtm->ldresult_ins == 0) {
643 cerr << "LDRESULT for unsupported type: "
644 << MVT::getValueTypeString(VT)
645 << "\n";
646 abort();
647 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000648
649 AddToISelQueue(Arg);
Scott Michela59d4692008-02-23 18:41:37 +0000650 Opc = vtm->ldresult_ins;
651 if (vtm->ldresult_imm) {
Scott Michel86c041f2007-12-20 00:44:13 +0000652 SDOperand Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000653
654 AddToISelQueue(Zero);
Scott Michel58c58182008-01-17 20:38:41 +0000655 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000656 } else {
Scott Michel58c58182008-01-17 20:38:41 +0000657 Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000658 }
659
Scott Michel266bc8f2007-12-04 22:23:35 +0000660 Chain = SDOperand(Result, 1);
Scott Michel86c041f2007-12-20 00:44:13 +0000661 AddToISelQueue(Chain);
662
Scott Michel266bc8f2007-12-04 22:23:35 +0000663 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000664 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michel58c58182008-01-17 20:38:41 +0000665 SDOperand Op0 = Op.getOperand(0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000666 if (Op0.getOpcode() == SPUISD::LDRESULT) {
667 /* || Op0.getOpcode() == SPUISD::AFormAddr) */
668 // (IndirectAddr (LDRESULT, imm))
Scott Michel58c58182008-01-17 20:38:41 +0000669 SDOperand Op1 = Op.getOperand(1);
670 MVT::ValueType VT = Op.getValueType();
671
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000672 DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = ");
Scott Michel58c58182008-01-17 20:38:41 +0000673 DEBUG(Op.getOperand(0).Val->dump(CurDAG));
674 DEBUG(cerr << "\nOp1 = ");
675 DEBUG(Op.getOperand(1).Val->dump(CurDAG));
676 DEBUG(cerr << "\n");
677
678 if (Op1.getOpcode() == ISD::Constant) {
679 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
680 Op1 = CurDAG->getTargetConstant(CN->getValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000681 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
682 AddToISelQueue(Op0);
683 AddToISelQueue(Op1);
684 Ops[0] = Op0;
685 Ops[1] = Op1;
686 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000687 }
Scott Michel58c58182008-01-17 20:38:41 +0000688 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000689 }
690
Scott Michel58c58182008-01-17 20:38:41 +0000691 if (n_ops > 0) {
692 if (N->hasOneUse())
693 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
694 else
695 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
696 } else
697 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000698}
699
700/// createPPCISelDag - This pass converts a legalized DAG into a
701/// SPU-specific DAG, ready for instruction scheduling.
702///
703FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
704 return new SPUDAGToDAGISel(TM);
705}