blob: 8bde66300a190e7f424ef0e0fddea470f094d114 [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"
20#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000023#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Target/TargetOptions.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/Constants.h"
28#include "llvm/GlobalValue.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/Support/Compiler.h"
33#include <iostream>
34#include <queue>
35#include <set>
36
37using namespace llvm;
38
39namespace {
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
41 bool
42 isI64IntS10Immediate(ConstantSDNode *CN)
43 {
44 return isS10Constant(CN->getValue());
45 }
46
47 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
48 bool
49 isI32IntS10Immediate(ConstantSDNode *CN)
50 {
51 return isS10Constant((int) CN->getValue());
52 }
53
54#if 0
55 //! SDNode predicate for sign-extended, 10-bit immediate values
56 bool
57 isI32IntS10Immediate(SDNode *N)
58 {
59 return (N->getOpcode() == ISD::Constant
60 && isI32IntS10Immediate(cast<ConstantSDNode>(N)));
61 }
62#endif
63
Scott Michel504c3692007-12-17 22:32:34 +000064 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
65 bool
66 isI32IntU10Immediate(ConstantSDNode *CN)
67 {
68 return isU10Constant((int) CN->getValue());
69 }
70
Scott Michel266bc8f2007-12-04 22:23:35 +000071 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
72 bool
73 isI16IntS10Immediate(ConstantSDNode *CN)
74 {
75 return isS10Constant((short) CN->getValue());
76 }
77
78 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
79 bool
80 isI16IntS10Immediate(SDNode *N)
81 {
82 return (N->getOpcode() == ISD::Constant
83 && isI16IntS10Immediate(cast<ConstantSDNode>(N)));
84 }
85
Scott Michelec2a08f2007-12-15 00:38:50 +000086 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
87 bool
88 isI16IntU10Immediate(ConstantSDNode *CN)
89 {
90 return isU10Constant((short) CN->getValue());
91 }
92
93 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
94 bool
95 isI16IntU10Immediate(SDNode *N)
96 {
97 return (N->getOpcode() == ISD::Constant
98 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
99 }
100
Scott Michel266bc8f2007-12-04 22:23:35 +0000101 //! ConstantSDNode predicate for signed 16-bit values
102 /*!
103 \arg CN The constant SelectionDAG node holding the value
104 \arg Imm The returned 16-bit value, if returning true
105
106 This predicate tests the value in \a CN to see whether it can be
107 represented as a 16-bit, sign-extended quantity. Returns true if
108 this is the case.
109 */
110 bool
111 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
112 {
113 MVT::ValueType vt = CN->getValueType(0);
114 Imm = (short) CN->getValue();
115 if (vt >= MVT::i1 && vt <= MVT::i16) {
116 return true;
117 } else if (vt == MVT::i32) {
118 int32_t i_val = (int32_t) CN->getValue();
119 short s_val = (short) i_val;
120 return i_val == s_val;
121 } else {
122 int64_t i_val = (int64_t) CN->getValue();
123 short s_val = (short) i_val;
124 return i_val == s_val;
125 }
126
127 return false;
128 }
129
130 //! SDNode predicate for signed 16-bit values.
131 bool
132 isIntS16Immediate(SDNode *N, short &Imm)
133 {
134 return (N->getOpcode() == ISD::Constant
135 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
136 }
137
138 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
139 static bool
140 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
141 {
142 MVT::ValueType vt = FPN->getValueType(0);
143 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000144 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000145 int sval = (int) ((val << 16) >> 16);
146 Imm = (short) val;
147 return val == sval;
148 }
149
150 return false;
151 }
152
Scott Michel053c1da2008-01-29 02:16:57 +0000153 bool
154 isHighLow(const SDOperand &Op)
155 {
156 return (Op.getOpcode() == SPUISD::IndirectAddr
157 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
158 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
159 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
160 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
161 }
162
Scott Michel266bc8f2007-12-04 22:23:35 +0000163 //===------------------------------------------------------------------===//
Scott Michel86c041f2007-12-20 00:44:13 +0000164 //! MVT::ValueType to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000165
166 struct valtype_map_s {
167 MVT::ValueType VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000168 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
169 int prefslot_byte; /// Byte offset of the "preferred" slot
Scott Michel266bc8f2007-12-04 22:23:35 +0000170 };
171
172 const valtype_map_s valtype_map[] = {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000173 { MVT::i1, 0, 3 },
174 { MVT::i8, SPU::ORBIr8, 3 },
175 { MVT::i16, SPU::ORHIr16, 2 },
176 { MVT::i32, SPU::ORIr32, 0 },
177 { MVT::i64, SPU::ORIr64, 0 },
178 { MVT::f32, 0, 0 },
179 { MVT::f64, 0, 0 },
Scott Michel58c58182008-01-17 20:38:41 +0000180 // vector types... (sigh!)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000181 { MVT::v16i8, 0, 0 },
182 { MVT::v8i16, 0, 0 },
183 { MVT::v4i32, 0, 0 },
184 { MVT::v2i64, 0, 0 },
185 { MVT::v4f32, 0, 0 },
186 { MVT::v2f64, 0, 0 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000187 };
188
189 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
190
191 const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT)
192 {
193 const valtype_map_s *retval = 0;
194 for (size_t i = 0; i < n_valtype_map; ++i) {
195 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000196 retval = valtype_map + i;
197 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000198 }
199 }
200
201
202#ifndef NDEBUG
203 if (retval == 0) {
204 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000205 << MVT::getValueTypeString(VT)
206 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000207 abort();
208 }
209#endif
210
211 return retval;
212 }
213}
214
215//===--------------------------------------------------------------------===//
216/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
217/// instructions for SelectionDAG operations.
218///
219class SPUDAGToDAGISel :
220 public SelectionDAGISel
221{
222 SPUTargetMachine &TM;
223 SPUTargetLowering &SPUtli;
224 unsigned GlobalBaseReg;
225
226public:
227 SPUDAGToDAGISel(SPUTargetMachine &tm) :
228 SelectionDAGISel(*tm.getTargetLowering()),
229 TM(tm),
230 SPUtli(*tm.getTargetLowering())
231 {}
232
233 virtual bool runOnFunction(Function &Fn) {
234 // Make sure we re-emit a set of the global base reg if necessary
235 GlobalBaseReg = 0;
236 SelectionDAGISel::runOnFunction(Fn);
237 return true;
238 }
239
240 /// getI32Imm - Return a target constant with the specified value, of type
241 /// i32.
242 inline SDOperand getI32Imm(uint32_t Imm) {
243 return CurDAG->getTargetConstant(Imm, MVT::i32);
244 }
245
246 /// getI64Imm - Return a target constant with the specified value, of type
247 /// i64.
248 inline SDOperand getI64Imm(uint64_t Imm) {
249 return CurDAG->getTargetConstant(Imm, MVT::i64);
250 }
251
252 /// getSmallIPtrImm - Return a target constant of pointer type.
253 inline SDOperand getSmallIPtrImm(unsigned Imm) {
254 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
255 }
256
257 /// Select - Convert the specified operand from a target-independent to a
258 /// target-specific node if it hasn't already been changed.
259 SDNode *Select(SDOperand Op);
260
Scott Michel266bc8f2007-12-04 22:23:35 +0000261 //! Returns true if the address N is an A-form (local store) address
262 bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000263 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000264
265 //! D-form address predicate
266 bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000267 SDOperand &Index);
268
269 /// Alternate D-form address using i7 offset predicate
270 bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
271 SDOperand &Base);
272
273 /// D-form address selection workhorse
274 bool DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Disp,
275 SDOperand &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000276
277 //! Address predicate if N can be expressed as an indexed [r+r] operation.
278 bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000279 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000280
281 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
282 /// inline asm expressions.
283 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000284 char ConstraintCode,
285 std::vector<SDOperand> &OutOps,
286 SelectionDAG &DAG) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000287 SDOperand Op0, Op1;
288 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;
299 AddToISelQueue(Op0); // r+0.
300 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000301 }
302 break;
303 case 'v': // not offsetable
304#if 1
305 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
306#else
307 SelectAddrIdxOnly(Op, Op, Op0, Op1);
308#endif
309 break;
310 }
311
312 OutOps.push_back(Op0);
313 OutOps.push_back(Op1);
314 return false;
315 }
316
317 /// InstructionSelectBasicBlock - This callback is invoked by
318 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
319 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
320
321 virtual const char *getPassName() const {
322 return "Cell SPU DAG->DAG Pattern Instruction Selection";
323 }
324
325 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
326 /// this target when scheduling the DAG.
327 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
328 const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
329 assert(II && "No InstrInfo?");
330 return new SPUHazardRecognizer(*II);
331 }
332
333 // Include the pieces autogenerated from the target description.
334#include "SPUGenDAGISel.inc"
335};
336
337/// InstructionSelectBasicBlock - This callback is invoked by
338/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
339void
340SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG)
341{
342 DEBUG(BB->dump());
343
344 // Select target instructions for the DAG.
345 DAG.setRoot(SelectRoot(DAG.getRoot()));
346 DAG.RemoveDeadNodes();
347
348 // Emit machine code to BB.
349 ScheduleAndEmitDAG(DAG);
350}
351
Scott Michel266bc8f2007-12-04 22:23:35 +0000352/*!
353 \arg Op The ISD instructio operand
354 \arg N The address to be tested
355 \arg Base The base address
356 \arg Index The base address index
357 */
358bool
359SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000360 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000361 // These match the addr256k operand type:
Scott Michel266bc8f2007-12-04 22:23:35 +0000362 MVT::ValueType OffsVT = MVT::i16;
Scott Michel053c1da2008-01-29 02:16:57 +0000363 SDOperand Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000364
365 switch (N.getOpcode()) {
366 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000367 case ISD::ConstantPool:
368 case ISD::GlobalAddress:
369 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
370 abort();
371 /*NOTREACHED*/
372
Scott Michel053c1da2008-01-29 02:16:57 +0000373 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000374 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000375 case ISD::TargetJumpTable:
376 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
377 << "A-form address.\n";
378 abort();
379 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000380
Scott Michel053c1da2008-01-29 02:16:57 +0000381 case SPUISD::AFormAddr:
382 // Just load from memory if there's only a single use of the location,
383 // otherwise, this will get handled below with D-form offset addresses
384 if (N.hasOneUse()) {
385 SDOperand Op0 = N.getOperand(0);
386 switch (Op0.getOpcode()) {
387 case ISD::TargetConstantPool:
388 case ISD::TargetJumpTable:
389 Base = Op0;
390 Index = Zero;
391 return true;
392
393 case ISD::TargetGlobalAddress: {
394 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
395 GlobalValue *GV = GSDN->getGlobal();
396 if (GV->getAlignment() == 16) {
397 Base = Op0;
398 Index = Zero;
399 return true;
400 }
401 break;
402 }
403 }
404 }
405 break;
406 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000407 return false;
408}
409
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000410bool
411SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
412 SDOperand &Base) {
413 return DFormAddressPredicate(Op, N, Disp, Base, -(1 << 7), (1 << 7) - 1);
414}
415
Scott Michel266bc8f2007-12-04 22:23:35 +0000416/*!
417 \arg Op The ISD instruction (ignored)
418 \arg N The address to be tested
419 \arg Base Base address register/pointer
420 \arg Index Base address index
421
422 Examine the input address by a base register plus a signed 10-bit
423 displacement, [r+I10] (D-form address).
424
425 \return true if \a N is a D-form address with \a Base and \a Index set
426 to non-empty SDOperand instances.
427*/
428bool
429SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000430 SDOperand &Index) {
431 return DFormAddressPredicate(Op, N, Base, Index,
432 SPUFrameInfo::minFrameOffset(),
433 SPUFrameInfo::maxFrameOffset());
434}
435
436bool
437SPUDAGToDAGISel::DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Base,
438 SDOperand &Index, int minOffset,
439 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000440 unsigned Opc = N.getOpcode();
441 unsigned PtrTy = SPUtli.getPointerTy();
442
Scott Michel053c1da2008-01-29 02:16:57 +0000443 if (Opc == ISD::FrameIndex) {
444 // Stack frame index must be less than 512 (divided by 16):
Scott Michel266bc8f2007-12-04 22:23:35 +0000445 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N);
446 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000447 << FI->getIndex() << "\n");
448 if (FI->getIndex() < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000449 Base = CurDAG->getTargetConstant(0, PtrTy);
450 Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
451 return true;
452 }
453 } else if (Opc == ISD::ADD) {
454 // Generated by getelementptr
Scott Michel053c1da2008-01-29 02:16:57 +0000455 const SDOperand Op0 = N.getOperand(0);
456 const SDOperand Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000457
Scott Michel053c1da2008-01-29 02:16:57 +0000458 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
459 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
460 Base = CurDAG->getTargetConstant(0, PtrTy);
461 Index = N;
462 return true;
463 } else if (Op1.getOpcode() == ISD::Constant
464 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000465 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Scott Michel053c1da2008-01-29 02:16:57 +0000466 int32_t offset = int32_t(CN->getSignExtended());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000467
Scott Michel053c1da2008-01-29 02:16:57 +0000468 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000469 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op0);
470 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
471 << " frame index = " << FI->getIndex() << "\n");
472
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000473 if (FI->getIndex() < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000474 Base = CurDAG->getTargetConstant(offset, PtrTy);
475 Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
476 return true;
477 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000478 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000479 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000480 Index = Op0;
481 return true;
482 }
483 } else if (Op0.getOpcode() == ISD::Constant
484 || Op0.getOpcode() == ISD::TargetConstant) {
485 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
486 int32_t offset = int32_t(CN->getSignExtended());
487
488 if (Op1.getOpcode() == ISD::FrameIndex) {
489 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op1);
490 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
491 << " frame index = " << FI->getIndex() << "\n");
492
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000493 if (FI->getIndex() < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000494 Base = CurDAG->getTargetConstant(offset, PtrTy);
495 Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000496 return true;
497 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000498 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000499 Base = CurDAG->getTargetConstant(offset, PtrTy);
500 Index = Op1;
501 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000502 }
Scott Michel053c1da2008-01-29 02:16:57 +0000503 }
504 } else if (Opc == SPUISD::IndirectAddr) {
505 // Indirect with constant offset -> D-Form address
506 const SDOperand Op0 = N.getOperand(0);
507 const SDOperand Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000508
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000509 if (Op0.getOpcode() == SPUISD::Hi
510 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000511 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000512 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000513 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000514 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000515 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
516 int32_t offset = 0;
517 SDOperand idxOp;
518
519 if (isa<ConstantSDNode>(Op1)) {
520 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
521 offset = int32_t(CN->getSignExtended());
522 idxOp = Op0;
523 } else if (isa<ConstantSDNode>(Op0)) {
524 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
525 offset = int32_t(CN->getSignExtended());
526 idxOp = Op1;
527 }
528
529 if (offset >= minOffset && offset <= maxOffset) {
530 Base = CurDAG->getTargetConstant(offset, PtrTy);
531 Index = idxOp;
532 return true;
533 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000534 }
Scott Michel053c1da2008-01-29 02:16:57 +0000535 } else if (Opc == SPUISD::AFormAddr) {
536 Base = CurDAG->getTargetConstant(0, N.getValueType());
537 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000538 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000539 } else if (Opc == SPUISD::LDRESULT) {
540 Base = CurDAG->getTargetConstant(0, N.getValueType());
541 Index = N;
542 return true;
Scott Michel266bc8f2007-12-04 22:23:35 +0000543 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000544 return false;
545}
546
547/*!
548 \arg Op The ISD instruction operand
549 \arg N The address operand
550 \arg Base The base pointer operand
551 \arg Index The offset/index operand
552
553 If the address \a N can be expressed as a [r + s10imm] address, returns false.
554 Otherwise, creates two operands, Base and Index that will become the [r+r]
555 address.
556*/
557bool
558SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000559 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000560 if (SelectAFormAddr(Op, N, Base, Index)
561 || SelectDFormAddr(Op, N, Base, Index))
562 return false;
563
Scott Michel053c1da2008-01-29 02:16:57 +0000564 // All else fails, punt and use an X-form address:
565 Base = N.getOperand(0);
566 Index = N.getOperand(1);
567 return true;
Scott Michel58c58182008-01-17 20:38:41 +0000568}
569
Scott Michel266bc8f2007-12-04 22:23:35 +0000570//! Convert the operand from a target-independent to a target-specific node
571/*!
572 */
573SDNode *
574SPUDAGToDAGISel::Select(SDOperand Op) {
575 SDNode *N = Op.Val;
576 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000577 int n_ops = -1;
578 unsigned NewOpc;
579 MVT::ValueType OpVT = Op.getValueType();
580 SDOperand Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000581
582 if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) {
583 return NULL; // Already selected.
584 } else if (Opc == ISD::FrameIndex) {
585 // Selects to AIr32 FI, 0 which in turn will become AIr32 SP, imm.
586 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Scott Michel9de5d0d2008-01-11 02:53:15 +0000587 MVT::ValueType PtrVT = SPUtli.getPointerTy();
588 SDOperand Zero = CurDAG->getTargetConstant(0, PtrVT);
589 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, PtrVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000590
Scott Michel9999e682007-12-19 07:35:06 +0000591 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AI32 <FI>, 0\n");
Scott Michel58c58182008-01-17 20:38:41 +0000592 NewOpc = SPU::AIr32;
593 Ops[0] = TFI;
594 Ops[1] = Zero;
595 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000596 } else if (Opc == ISD::ZERO_EXTEND) {
597 // (zero_extend:i16 (and:i8 <arg>, <const>))
598 const SDOperand &Op1 = N->getOperand(0);
599
600 if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
601 if (Op1.getOpcode() == ISD::AND) {
602 // Fold this into a single ANDHI. This is often seen in expansions of i1
603 // to i8, then i8 to i16 in logical/branching operations.
604 DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
605 "<arg>, <const>))\n");
606 NewOpc = SPU::ANDHI1To2;
607 Ops[0] = Op1.getOperand(0);
608 Ops[1] = Op1.getOperand(1);
609 n_ops = 2;
610 }
611 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000612 } else if (Opc == SPUISD::LDRESULT) {
613 // Custom select instructions for LDRESULT
614 unsigned VT = N->getValueType(0);
615 SDOperand Arg = N->getOperand(0);
616 SDOperand Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000617 SDNode *Result;
Scott Michel266bc8f2007-12-04 22:23:35 +0000618
619 AddToISelQueue(Arg);
Scott Michel86c041f2007-12-20 00:44:13 +0000620 if (!MVT::isFloatingPoint(VT)) {
621 SDOperand Zero = CurDAG->getTargetConstant(0, VT);
622 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
623
624 if (vtm->ldresult_ins == 0) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000625 cerr << "LDRESULT for unsupported type: "
626 << MVT::getValueTypeString(VT)
627 << "\n";
628 abort();
Scott Michel86c041f2007-12-20 00:44:13 +0000629 } else
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000630 Opc = vtm->ldresult_ins;
Scott Michel86c041f2007-12-20 00:44:13 +0000631
632 AddToISelQueue(Zero);
Scott Michel58c58182008-01-17 20:38:41 +0000633 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000634 } else {
Scott Michel58c58182008-01-17 20:38:41 +0000635 Opc = (VT == MVT::f32 ? SPU::ORf32 : SPU::ORf64);
636 Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000637 }
638
Scott Michel266bc8f2007-12-04 22:23:35 +0000639 Chain = SDOperand(Result, 1);
Scott Michel86c041f2007-12-20 00:44:13 +0000640 AddToISelQueue(Chain);
641
Scott Michel266bc8f2007-12-04 22:23:35 +0000642 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000643 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michel58c58182008-01-17 20:38:41 +0000644 SDOperand Op0 = Op.getOperand(0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000645 if (Op0.getOpcode() == SPUISD::LDRESULT) {
646 /* || Op0.getOpcode() == SPUISD::AFormAddr) */
647 // (IndirectAddr (LDRESULT, imm))
Scott Michel58c58182008-01-17 20:38:41 +0000648 SDOperand Op1 = Op.getOperand(1);
649 MVT::ValueType VT = Op.getValueType();
650
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000651 DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = ");
Scott Michel58c58182008-01-17 20:38:41 +0000652 DEBUG(Op.getOperand(0).Val->dump(CurDAG));
653 DEBUG(cerr << "\nOp1 = ");
654 DEBUG(Op.getOperand(1).Val->dump(CurDAG));
655 DEBUG(cerr << "\n");
656
657 if (Op1.getOpcode() == ISD::Constant) {
658 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
659 Op1 = CurDAG->getTargetConstant(CN->getValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000660 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
661 AddToISelQueue(Op0);
662 AddToISelQueue(Op1);
663 Ops[0] = Op0;
664 Ops[1] = Op1;
665 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000666 }
Scott Michel58c58182008-01-17 20:38:41 +0000667 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000668 }
669
Scott Michel58c58182008-01-17 20:38:41 +0000670 if (n_ops > 0) {
671 if (N->hasOneUse())
672 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
673 else
674 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
675 } else
676 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000677}
678
679/// createPPCISelDag - This pass converts a legalized DAG into a
680/// SPU-specific DAG, ready for instruction scheduling.
681///
682FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
683 return new SPUDAGToDAGISel(TM);
684}