blob: c6628aca63a9a59c13e9e3f85dbfe3a0d5cd7e4f [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
Dan Gohman844731a2008-05-13 00:00:25 +0000217namespace {
218
Scott Michel266bc8f2007-12-04 22:23:35 +0000219//===--------------------------------------------------------------------===//
220/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
221/// instructions for SelectionDAG operations.
222///
223class SPUDAGToDAGISel :
224 public SelectionDAGISel
225{
226 SPUTargetMachine &TM;
227 SPUTargetLowering &SPUtli;
228 unsigned GlobalBaseReg;
229
230public:
231 SPUDAGToDAGISel(SPUTargetMachine &tm) :
232 SelectionDAGISel(*tm.getTargetLowering()),
233 TM(tm),
234 SPUtli(*tm.getTargetLowering())
235 {}
236
237 virtual bool runOnFunction(Function &Fn) {
238 // Make sure we re-emit a set of the global base reg if necessary
239 GlobalBaseReg = 0;
240 SelectionDAGISel::runOnFunction(Fn);
241 return true;
242 }
243
244 /// getI32Imm - Return a target constant with the specified value, of type
245 /// i32.
246 inline SDOperand getI32Imm(uint32_t Imm) {
247 return CurDAG->getTargetConstant(Imm, MVT::i32);
248 }
249
250 /// getI64Imm - Return a target constant with the specified value, of type
251 /// i64.
252 inline SDOperand getI64Imm(uint64_t Imm) {
253 return CurDAG->getTargetConstant(Imm, MVT::i64);
254 }
255
256 /// getSmallIPtrImm - Return a target constant of pointer type.
257 inline SDOperand getSmallIPtrImm(unsigned Imm) {
258 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
259 }
260
261 /// Select - Convert the specified operand from a target-independent to a
262 /// target-specific node if it hasn't already been changed.
263 SDNode *Select(SDOperand Op);
264
Scott Michel266bc8f2007-12-04 22:23:35 +0000265 //! Returns true if the address N is an A-form (local store) address
266 bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000267 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000268
269 //! D-form address predicate
270 bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000271 SDOperand &Index);
272
273 /// Alternate D-form address using i7 offset predicate
274 bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
275 SDOperand &Base);
276
277 /// D-form address selection workhorse
278 bool DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Disp,
279 SDOperand &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000280
281 //! Address predicate if N can be expressed as an indexed [r+r] operation.
282 bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000283 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000284
285 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
286 /// inline asm expressions.
287 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000288 char ConstraintCode,
289 std::vector<SDOperand> &OutOps,
290 SelectionDAG &DAG) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000291 SDOperand Op0, Op1;
292 switch (ConstraintCode) {
293 default: return true;
294 case 'm': // memory
295 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000296 && !SelectAFormAddr(Op, Op, Op0, Op1))
297 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000298 break;
299 case 'o': // offsetable
300 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000301 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
302 Op0 = Op;
303 AddToISelQueue(Op0); // r+0.
304 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000305 }
306 break;
307 case 'v': // not offsetable
308#if 1
309 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
310#else
311 SelectAddrIdxOnly(Op, Op, Op0, Op1);
312#endif
313 break;
314 }
315
316 OutOps.push_back(Op0);
317 OutOps.push_back(Op1);
318 return false;
319 }
320
321 /// InstructionSelectBasicBlock - This callback is invoked by
322 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
323 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
324
325 virtual const char *getPassName() const {
326 return "Cell SPU DAG->DAG Pattern Instruction Selection";
327 }
328
329 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
330 /// this target when scheduling the DAG.
331 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
332 const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
333 assert(II && "No InstrInfo?");
334 return new SPUHazardRecognizer(*II);
335 }
336
337 // Include the pieces autogenerated from the target description.
338#include "SPUGenDAGISel.inc"
339};
340
Dan Gohman844731a2008-05-13 00:00:25 +0000341}
342
Scott Michel266bc8f2007-12-04 22:23:35 +0000343/// InstructionSelectBasicBlock - This callback is invoked by
344/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
345void
346SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG)
347{
348 DEBUG(BB->dump());
349
350 // Select target instructions for the DAG.
351 DAG.setRoot(SelectRoot(DAG.getRoot()));
352 DAG.RemoveDeadNodes();
353
354 // Emit machine code to BB.
355 ScheduleAndEmitDAG(DAG);
356}
357
Scott Michel266bc8f2007-12-04 22:23:35 +0000358/*!
359 \arg Op The ISD instructio operand
360 \arg N The address to be tested
361 \arg Base The base address
362 \arg Index The base address index
363 */
364bool
365SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000366 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000367 // These match the addr256k operand type:
Scott Michel266bc8f2007-12-04 22:23:35 +0000368 MVT::ValueType OffsVT = MVT::i16;
Scott Michel053c1da2008-01-29 02:16:57 +0000369 SDOperand Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000370
371 switch (N.getOpcode()) {
372 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000373 case ISD::ConstantPool:
374 case ISD::GlobalAddress:
375 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
376 abort();
377 /*NOTREACHED*/
378
Scott Michel053c1da2008-01-29 02:16:57 +0000379 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000380 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000381 case ISD::TargetJumpTable:
382 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
383 << "A-form address.\n";
384 abort();
385 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000386
Scott Michel053c1da2008-01-29 02:16:57 +0000387 case SPUISD::AFormAddr:
388 // Just load from memory if there's only a single use of the location,
389 // otherwise, this will get handled below with D-form offset addresses
390 if (N.hasOneUse()) {
391 SDOperand Op0 = N.getOperand(0);
392 switch (Op0.getOpcode()) {
393 case ISD::TargetConstantPool:
394 case ISD::TargetJumpTable:
395 Base = Op0;
396 Index = Zero;
397 return true;
398
399 case ISD::TargetGlobalAddress: {
400 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
401 GlobalValue *GV = GSDN->getGlobal();
402 if (GV->getAlignment() == 16) {
403 Base = Op0;
404 Index = Zero;
405 return true;
406 }
407 break;
408 }
409 }
410 }
411 break;
412 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000413 return false;
414}
415
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000416bool
417SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
418 SDOperand &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000419 const int minDForm2Offset = -(1 << 7);
420 const int maxDForm2Offset = (1 << 7) - 1;
421 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
422 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000423}
424
Scott Michel266bc8f2007-12-04 22:23:35 +0000425/*!
426 \arg Op The ISD instruction (ignored)
427 \arg N The address to be tested
428 \arg Base Base address register/pointer
429 \arg Index Base address index
430
431 Examine the input address by a base register plus a signed 10-bit
432 displacement, [r+I10] (D-form address).
433
434 \return true if \a N is a D-form address with \a Base and \a Index set
435 to non-empty SDOperand instances.
436*/
437bool
438SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000439 SDOperand &Index) {
440 return DFormAddressPredicate(Op, N, Base, Index,
441 SPUFrameInfo::minFrameOffset(),
442 SPUFrameInfo::maxFrameOffset());
443}
444
445bool
446SPUDAGToDAGISel::DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Base,
447 SDOperand &Index, int minOffset,
448 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000449 unsigned Opc = N.getOpcode();
450 unsigned PtrTy = SPUtli.getPointerTy();
451
Scott Michel053c1da2008-01-29 02:16:57 +0000452 if (Opc == ISD::FrameIndex) {
453 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000454 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
455 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000456 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000457 << FI << "\n");
458 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000459 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000460 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000461 return true;
462 }
463 } else if (Opc == ISD::ADD) {
464 // Generated by getelementptr
Scott Michel053c1da2008-01-29 02:16:57 +0000465 const SDOperand Op0 = N.getOperand(0);
466 const SDOperand Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000467
Scott Michel053c1da2008-01-29 02:16:57 +0000468 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
469 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
470 Base = CurDAG->getTargetConstant(0, PtrTy);
471 Index = N;
472 return true;
473 } else if (Op1.getOpcode() == ISD::Constant
474 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000475 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Scott Michel053c1da2008-01-29 02:16:57 +0000476 int32_t offset = int32_t(CN->getSignExtended());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000477
Scott Michel053c1da2008-01-29 02:16:57 +0000478 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000479 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
480 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000481 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000482 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000483
Scott Michel203b2d62008-04-30 00:30:08 +0000484 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000485 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000486 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000487 return true;
488 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000489 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000490 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000491 Index = Op0;
492 return true;
493 }
494 } else if (Op0.getOpcode() == ISD::Constant
495 || Op0.getOpcode() == ISD::TargetConstant) {
496 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
497 int32_t offset = int32_t(CN->getSignExtended());
498
499 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000500 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
501 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000502 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000503 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000504
Scott Michel203b2d62008-04-30 00:30:08 +0000505 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000506 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000507 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000508 return true;
509 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000510 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000511 Base = CurDAG->getTargetConstant(offset, PtrTy);
512 Index = Op1;
513 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000514 }
Scott Michel053c1da2008-01-29 02:16:57 +0000515 }
516 } else if (Opc == SPUISD::IndirectAddr) {
517 // Indirect with constant offset -> D-Form address
518 const SDOperand Op0 = N.getOperand(0);
519 const SDOperand Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000520
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000521 if (Op0.getOpcode() == SPUISD::Hi
522 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000523 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000524 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000525 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000526 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000527 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
528 int32_t offset = 0;
529 SDOperand idxOp;
530
531 if (isa<ConstantSDNode>(Op1)) {
532 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
533 offset = int32_t(CN->getSignExtended());
534 idxOp = Op0;
535 } else if (isa<ConstantSDNode>(Op0)) {
536 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
537 offset = int32_t(CN->getSignExtended());
538 idxOp = Op1;
539 }
540
541 if (offset >= minOffset && offset <= maxOffset) {
542 Base = CurDAG->getTargetConstant(offset, PtrTy);
543 Index = idxOp;
544 return true;
545 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000546 }
Scott Michel053c1da2008-01-29 02:16:57 +0000547 } else if (Opc == SPUISD::AFormAddr) {
548 Base = CurDAG->getTargetConstant(0, N.getValueType());
549 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000550 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000551 } else if (Opc == SPUISD::LDRESULT) {
552 Base = CurDAG->getTargetConstant(0, N.getValueType());
553 Index = N;
554 return true;
Scott Michel266bc8f2007-12-04 22:23:35 +0000555 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000556 return false;
557}
558
559/*!
560 \arg Op The ISD instruction operand
561 \arg N The address operand
562 \arg Base The base pointer operand
563 \arg Index The offset/index operand
564
565 If the address \a N can be expressed as a [r + s10imm] address, returns false.
566 Otherwise, creates two operands, Base and Index that will become the [r+r]
567 address.
568*/
569bool
570SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000571 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000572 if (SelectAFormAddr(Op, N, Base, Index)
573 || SelectDFormAddr(Op, N, Base, Index))
574 return false;
575
Scott Michel053c1da2008-01-29 02:16:57 +0000576 // All else fails, punt and use an X-form address:
577 Base = N.getOperand(0);
578 Index = N.getOperand(1);
579 return true;
Scott Michel58c58182008-01-17 20:38:41 +0000580}
581
Scott Michel266bc8f2007-12-04 22:23:35 +0000582//! Convert the operand from a target-independent to a target-specific node
583/*!
584 */
585SDNode *
586SPUDAGToDAGISel::Select(SDOperand Op) {
587 SDNode *N = Op.Val;
588 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000589 int n_ops = -1;
590 unsigned NewOpc;
591 MVT::ValueType OpVT = Op.getValueType();
592 SDOperand Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000593
594 if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) {
595 return NULL; // Already selected.
596 } else if (Opc == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000597 // Selects to (add $sp, FI * stackSlotSize)
598 int FI =
599 SPUFrameInfo::FItoStackOffset(cast<FrameIndexSDNode>(N)->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000600 MVT::ValueType PtrVT = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000601
Scott Michel203b2d62008-04-30 00:30:08 +0000602 // Adjust stack slot to actual offset in frame:
603 if (isS10Constant(FI)) {
604 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AIr32 $sp, "
605 << FI
606 << "\n");
607 NewOpc = SPU::AIr32;
608 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
609 Ops[1] = CurDAG->getTargetConstant(FI, PtrVT);
610 n_ops = 2;
611 } else {
612 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with Ar32 $sp, "
613 << FI
614 << "\n");
615 NewOpc = SPU::Ar32;
616 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
617 Ops[1] = CurDAG->getConstant(FI, PtrVT);
618 n_ops = 2;
619
620 AddToISelQueue(Ops[1]);
621 }
Scott Michel58c58182008-01-17 20:38:41 +0000622 } else if (Opc == ISD::ZERO_EXTEND) {
623 // (zero_extend:i16 (and:i8 <arg>, <const>))
624 const SDOperand &Op1 = N->getOperand(0);
625
626 if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
627 if (Op1.getOpcode() == ISD::AND) {
628 // Fold this into a single ANDHI. This is often seen in expansions of i1
629 // to i8, then i8 to i16 in logical/branching operations.
630 DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
631 "<arg>, <const>))\n");
Scott Michela59d4692008-02-23 18:41:37 +0000632 NewOpc = SPU::ANDHIi8i16;
Scott Michel58c58182008-01-17 20:38:41 +0000633 Ops[0] = Op1.getOperand(0);
634 Ops[1] = Op1.getOperand(1);
635 n_ops = 2;
636 }
637 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000638 } else if (Opc == SPUISD::LDRESULT) {
639 // Custom select instructions for LDRESULT
640 unsigned VT = N->getValueType(0);
641 SDOperand Arg = N->getOperand(0);
642 SDOperand Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000643 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000644 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
645
646 if (vtm->ldresult_ins == 0) {
647 cerr << "LDRESULT for unsupported type: "
648 << MVT::getValueTypeString(VT)
649 << "\n";
650 abort();
651 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000652
653 AddToISelQueue(Arg);
Scott Michela59d4692008-02-23 18:41:37 +0000654 Opc = vtm->ldresult_ins;
655 if (vtm->ldresult_imm) {
Scott Michel86c041f2007-12-20 00:44:13 +0000656 SDOperand Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000657
658 AddToISelQueue(Zero);
Scott Michel58c58182008-01-17 20:38:41 +0000659 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000660 } else {
Scott Michel58c58182008-01-17 20:38:41 +0000661 Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000662 }
663
Scott Michel266bc8f2007-12-04 22:23:35 +0000664 Chain = SDOperand(Result, 1);
Scott Michel86c041f2007-12-20 00:44:13 +0000665 AddToISelQueue(Chain);
666
Scott Michel266bc8f2007-12-04 22:23:35 +0000667 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000668 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michel58c58182008-01-17 20:38:41 +0000669 SDOperand Op0 = Op.getOperand(0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000670 if (Op0.getOpcode() == SPUISD::LDRESULT) {
671 /* || Op0.getOpcode() == SPUISD::AFormAddr) */
672 // (IndirectAddr (LDRESULT, imm))
Scott Michel58c58182008-01-17 20:38:41 +0000673 SDOperand Op1 = Op.getOperand(1);
674 MVT::ValueType VT = Op.getValueType();
675
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000676 DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = ");
Scott Michel58c58182008-01-17 20:38:41 +0000677 DEBUG(Op.getOperand(0).Val->dump(CurDAG));
678 DEBUG(cerr << "\nOp1 = ");
679 DEBUG(Op.getOperand(1).Val->dump(CurDAG));
680 DEBUG(cerr << "\n");
681
682 if (Op1.getOpcode() == ISD::Constant) {
683 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
684 Op1 = CurDAG->getTargetConstant(CN->getValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000685 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
686 AddToISelQueue(Op0);
687 AddToISelQueue(Op1);
688 Ops[0] = Op0;
689 Ops[1] = Op1;
690 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000691 }
Scott Michel58c58182008-01-17 20:38:41 +0000692 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000693 }
694
Scott Michel58c58182008-01-17 20:38:41 +0000695 if (n_ops > 0) {
696 if (N->hasOneUse())
697 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
698 else
699 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
700 } else
701 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000702}
703
704/// createPPCISelDag - This pass converts a legalized DAG into a
705/// SPU-specific DAG, ready for instruction scheduling.
706///
707FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
708 return new SPUDAGToDAGISel(TM);
709}