blob: 3905d55302996e2ddcab6c29bc02aa8a4be5c046 [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;
168 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
169 int prefslot_byte; /// Byte offset of the "preferred" slot
Scott Michel053c1da2008-01-29 02:16:57 +0000170 unsigned insmask_ins; /// Insert mask instruction for a-form
Scott Michel266bc8f2007-12-04 22:23:35 +0000171 };
172
173 const valtype_map_s valtype_map[] = {
Scott Michel053c1da2008-01-29 02:16:57 +0000174 { MVT::i1, 0, 3, 0 },
175 { MVT::i8, SPU::ORBIr8, 3, 0 },
176 { MVT::i16, SPU::ORHIr16, 2, 0 },
177 { MVT::i32, SPU::ORIr32, 0, 0 },
178 { MVT::i64, SPU::ORIr64, 0, 0 },
179 { MVT::f32, 0, 0, 0 },
180 { MVT::f64, 0, 0, 0 },
Scott Michel58c58182008-01-17 20:38:41 +0000181 // vector types... (sigh!)
Scott Michel053c1da2008-01-29 02:16:57 +0000182 { MVT::v16i8, 0, 0, SPU::CBD },
183 { MVT::v8i16, 0, 0, SPU::CHD },
184 { MVT::v4i32, 0, 0, SPU::CWD },
185 { MVT::v2i64, 0, 0, 0 },
186 { MVT::v4f32, 0, 0, SPU::CWD },
187 { MVT::v2f64, 0, 0, 0 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000188 };
189
190 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
191
192 const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT)
193 {
194 const valtype_map_s *retval = 0;
195 for (size_t i = 0; i < n_valtype_map; ++i) {
196 if (valtype_map[i].VT == VT) {
197 retval = valtype_map + i;
198 break;
199 }
200 }
201
202
203#ifndef NDEBUG
204 if (retval == 0) {
205 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
206 << MVT::getValueTypeString(VT)
207 << "\n";
208 abort();
209 }
210#endif
211
212 return retval;
213 }
214}
215
216//===--------------------------------------------------------------------===//
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:
228 SPUDAGToDAGISel(SPUTargetMachine &tm) :
229 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.
243 inline SDOperand getI32Imm(uint32_t Imm) {
244 return CurDAG->getTargetConstant(Imm, MVT::i32);
245 }
246
247 /// getI64Imm - Return a target constant with the specified value, of type
248 /// i64.
249 inline SDOperand getI64Imm(uint64_t Imm) {
250 return CurDAG->getTargetConstant(Imm, MVT::i64);
251 }
252
253 /// getSmallIPtrImm - Return a target constant of pointer type.
254 inline SDOperand getSmallIPtrImm(unsigned Imm) {
255 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.
260 SDNode *Select(SDOperand Op);
261
262 /// Return true if the address N is a RI7 format address [r+imm]
263 bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
264 SDOperand &Base);
265
266 //! Returns true if the address N is an A-form (local store) address
267 bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
268 SDOperand &Index);
269
270 //! D-form address predicate
271 bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
272 SDOperand &Index);
273
274 //! Address predicate if N can be expressed as an indexed [r+r] operation.
275 bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
276 SDOperand &Index);
277
278 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
279 /// inline asm expressions.
280 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
281 char ConstraintCode,
282 std::vector<SDOperand> &OutOps,
283 SelectionDAG &DAG) {
284 SDOperand Op0, Op1;
285 switch (ConstraintCode) {
286 default: return true;
287 case 'm': // memory
288 if (!SelectDFormAddr(Op, Op, Op0, Op1)
289 && !SelectAFormAddr(Op, Op, Op0, Op1))
290 SelectXFormAddr(Op, Op, Op0, Op1);
291 break;
292 case 'o': // offsetable
293 if (!SelectDFormAddr(Op, Op, Op0, Op1)
294 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
295 Op0 = Op;
296 AddToISelQueue(Op0); // r+0.
297 Op1 = getSmallIPtrImm(0);
298 }
299 break;
300 case 'v': // not offsetable
301#if 1
302 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
303#else
304 SelectAddrIdxOnly(Op, Op, Op0, Op1);
305#endif
306 break;
307 }
308
309 OutOps.push_back(Op0);
310 OutOps.push_back(Op1);
311 return false;
312 }
313
314 /// InstructionSelectBasicBlock - This callback is invoked by
315 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
316 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
317
318 virtual const char *getPassName() const {
319 return "Cell SPU DAG->DAG Pattern Instruction Selection";
320 }
321
322 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
323 /// this target when scheduling the DAG.
324 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
325 const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
326 assert(II && "No InstrInfo?");
327 return new SPUHazardRecognizer(*II);
328 }
329
330 // Include the pieces autogenerated from the target description.
331#include "SPUGenDAGISel.inc"
332};
333
334/// InstructionSelectBasicBlock - This callback is invoked by
335/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
336void
337SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG)
338{
339 DEBUG(BB->dump());
340
341 // Select target instructions for the DAG.
342 DAG.setRoot(SelectRoot(DAG.getRoot()));
343 DAG.RemoveDeadNodes();
344
345 // Emit machine code to BB.
346 ScheduleAndEmitDAG(DAG);
347}
348
349bool
350SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
351 SDOperand &Base) {
352 unsigned Opc = N.getOpcode();
353 unsigned VT = N.getValueType();
354 MVT::ValueType PtrVT = SPUtli.getPointerTy();
355 ConstantSDNode *CN = 0;
356 int Imm;
357
358 if (Opc == ISD::ADD) {
359 SDOperand Op0 = N.getOperand(0);
360 SDOperand Op1 = N.getOperand(1);
361 if (Op1.getOpcode() == ISD::Constant ||
362 Op1.getOpcode() == ISD::TargetConstant) {
363 CN = cast<ConstantSDNode>(Op1);
364 Imm = int(CN->getValue());
365 if (Imm <= 0xff) {
366 Disp = CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
367 Base = Op0;
368 return true;
369 }
370 }
371 } else if (Opc == ISD::GlobalAddress
372 || Opc == ISD::TargetGlobalAddress
373 || Opc == ISD::Register) {
374 // Plain old local store address:
375 Disp = CurDAG->getTargetConstant(0, VT);
376 Base = N;
377 return true;
Scott Michel053c1da2008-01-29 02:16:57 +0000378 } else if (Opc == SPUISD::IndirectAddr) {
379 SDOperand Op1 = N.getOperand(1);
380 if (Op1.getOpcode() == ISD::TargetConstant
381 || Op1.getOpcode() == ISD::Constant) {
382 CN = cast<ConstantSDNode>(N.getOperand(1));
383 assert(CN != 0 && "SelectIndirectAddr/SPUISD::DForm2Addr expecting constant");
384 Imm = unsigned(CN->getValue());
385 if (Imm < 0xff) {
386 Disp = CurDAG->getTargetConstant(CN->getValue(), PtrVT);
387 Base = N.getOperand(0);
388 return true;
389 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000390 }
391 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000392 return false;
393}
394
395/*!
396 \arg Op The ISD instructio operand
397 \arg N The address to be tested
398 \arg Base The base address
399 \arg Index The base address index
400 */
401bool
402SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
403 SDOperand &Index) {
404 // These match the addr256k operand type:
Scott Michel266bc8f2007-12-04 22:23:35 +0000405 MVT::ValueType OffsVT = MVT::i16;
Scott Michel053c1da2008-01-29 02:16:57 +0000406 SDOperand Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000407
408 switch (N.getOpcode()) {
409 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000410 case ISD::ConstantPool:
411 case ISD::GlobalAddress:
412 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
413 abort();
414 /*NOTREACHED*/
415
Scott Michel053c1da2008-01-29 02:16:57 +0000416 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000417 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000418 case ISD::TargetJumpTable:
419 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
420 << "A-form address.\n";
421 abort();
422 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000423
Scott Michel053c1da2008-01-29 02:16:57 +0000424 case SPUISD::AFormAddr:
425 // Just load from memory if there's only a single use of the location,
426 // otherwise, this will get handled below with D-form offset addresses
427 if (N.hasOneUse()) {
428 SDOperand Op0 = N.getOperand(0);
429 switch (Op0.getOpcode()) {
430 case ISD::TargetConstantPool:
431 case ISD::TargetJumpTable:
432 Base = Op0;
433 Index = Zero;
434 return true;
435
436 case ISD::TargetGlobalAddress: {
437 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
438 GlobalValue *GV = GSDN->getGlobal();
439 if (GV->getAlignment() == 16) {
440 Base = Op0;
441 Index = Zero;
442 return true;
443 }
444 break;
445 }
446 }
447 }
448 break;
449 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000450 return false;
451}
452
453/*!
454 \arg Op The ISD instruction (ignored)
455 \arg N The address to be tested
456 \arg Base Base address register/pointer
457 \arg Index Base address index
458
459 Examine the input address by a base register plus a signed 10-bit
460 displacement, [r+I10] (D-form address).
461
462 \return true if \a N is a D-form address with \a Base and \a Index set
463 to non-empty SDOperand instances.
464*/
465bool
466SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
467 SDOperand &Index) {
468 unsigned Opc = N.getOpcode();
469 unsigned PtrTy = SPUtli.getPointerTy();
470
Scott Michel053c1da2008-01-29 02:16:57 +0000471 if (Opc == ISD::FrameIndex) {
472 // Stack frame index must be less than 512 (divided by 16):
Scott Michel266bc8f2007-12-04 22:23:35 +0000473 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N);
474 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel053c1da2008-01-29 02:16:57 +0000475 << FI->getIndex() << "\n");
Scott Michel266bc8f2007-12-04 22:23:35 +0000476 if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) {
477 Base = CurDAG->getTargetConstant(0, PtrTy);
478 Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
479 return true;
480 }
481 } else if (Opc == ISD::ADD) {
482 // Generated by getelementptr
Scott Michel053c1da2008-01-29 02:16:57 +0000483 const SDOperand Op0 = N.getOperand(0);
484 const SDOperand Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000485
Scott Michel053c1da2008-01-29 02:16:57 +0000486 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
487 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
488 Base = CurDAG->getTargetConstant(0, PtrTy);
489 Index = N;
490 return true;
491 } else if (Op1.getOpcode() == ISD::Constant
492 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000493 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Scott Michel053c1da2008-01-29 02:16:57 +0000494 int32_t offset = int32_t(CN->getSignExtended());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000495
Scott Michel053c1da2008-01-29 02:16:57 +0000496 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000497 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op0);
498 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
499 << " frame index = " << FI->getIndex() << "\n");
500
501 if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) {
502 Base = CurDAG->getTargetConstant(offset, PtrTy);
503 Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
504 return true;
505 }
506 } else if (offset > SPUFrameInfo::minFrameOffset()
507 && offset < SPUFrameInfo::maxFrameOffset()) {
508 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000509 Index = Op0;
510 return true;
511 }
512 } else if (Op0.getOpcode() == ISD::Constant
513 || Op0.getOpcode() == ISD::TargetConstant) {
514 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
515 int32_t offset = int32_t(CN->getSignExtended());
516
517 if (Op1.getOpcode() == ISD::FrameIndex) {
518 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op1);
519 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
520 << " frame index = " << FI->getIndex() << "\n");
521
522 if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) {
523 Base = CurDAG->getTargetConstant(offset, PtrTy);
524 Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000525 return true;
526 }
Scott Michel053c1da2008-01-29 02:16:57 +0000527 } else if (offset > SPUFrameInfo::minFrameOffset()
528 && offset < SPUFrameInfo::maxFrameOffset()) {
529 Base = CurDAG->getTargetConstant(offset, PtrTy);
530 Index = Op1;
531 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000532 }
Scott Michel053c1da2008-01-29 02:16:57 +0000533 }
534 } else if (Opc == SPUISD::IndirectAddr) {
535 // Indirect with constant offset -> D-Form address
536 const SDOperand Op0 = N.getOperand(0);
537 const SDOperand Op1 = N.getOperand(1);
538 SDOperand Zero = CurDAG->getTargetConstant(0, N.getValueType());
Scott Michel497e8882008-01-11 21:01:19 +0000539
Scott Michel053c1da2008-01-29 02:16:57 +0000540 if (Op1.getOpcode() == ISD::Constant
541 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel497e8882008-01-11 21:01:19 +0000542 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michel053c1da2008-01-29 02:16:57 +0000543 int32_t offset = int32_t(CN->getSignExtended());
544 if (offset > SPUFrameInfo::minFrameOffset()
545 && offset < SPUFrameInfo::maxFrameOffset()) {
546 Base = CurDAG->getTargetConstant(CN->getValue(), PtrTy);
547 Index = Op0;
548 return true;
549 }
550 } else if (Op0.getOpcode() == ISD::Constant
551 || Op0.getOpcode() == ISD::TargetConstant) {
552 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
553 int32_t offset = int32_t(CN->getSignExtended());
554 if (offset > SPUFrameInfo::minFrameOffset()
555 && offset < SPUFrameInfo::maxFrameOffset()) {
556 Base = CurDAG->getTargetConstant(CN->getValue(), PtrTy);
557 Index = Op1;
558 return true;
559 }
560 } else if (Op0.getOpcode() == SPUISD::Hi
561 && Op1.getOpcode() == SPUISD::Lo) {
562 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000563 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000564 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000565 return true;
566 }
Scott Michel053c1da2008-01-29 02:16:57 +0000567 } else if (Opc == SPUISD::AFormAddr) {
568 Base = CurDAG->getTargetConstant(0, N.getValueType());
569 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000570 return true;
Scott Michel266bc8f2007-12-04 22:23:35 +0000571 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000572 return false;
573}
574
575/*!
576 \arg Op The ISD instruction operand
577 \arg N The address operand
578 \arg Base The base pointer operand
579 \arg Index The offset/index operand
580
581 If the address \a N can be expressed as a [r + s10imm] address, returns false.
582 Otherwise, creates two operands, Base and Index that will become the [r+r]
583 address.
584*/
585bool
586SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
587 SDOperand &Index) {
588 if (SelectAFormAddr(Op, N, Base, Index)
589 || SelectDFormAddr(Op, N, Base, Index))
590 return false;
591
Scott Michel053c1da2008-01-29 02:16:57 +0000592 // All else fails, punt and use an X-form address:
593 Base = N.getOperand(0);
594 Index = N.getOperand(1);
595 return true;
Scott Michel58c58182008-01-17 20:38:41 +0000596}
597
Scott Michel266bc8f2007-12-04 22:23:35 +0000598//! Convert the operand from a target-independent to a target-specific node
599/*!
600 */
601SDNode *
602SPUDAGToDAGISel::Select(SDOperand Op) {
603 SDNode *N = Op.Val;
604 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000605 int n_ops = -1;
606 unsigned NewOpc;
607 MVT::ValueType OpVT = Op.getValueType();
608 SDOperand Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000609
610 if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) {
611 return NULL; // Already selected.
612 } else if (Opc == ISD::FrameIndex) {
613 // Selects to AIr32 FI, 0 which in turn will become AIr32 SP, imm.
614 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Scott Michel9de5d0d2008-01-11 02:53:15 +0000615 MVT::ValueType PtrVT = SPUtli.getPointerTy();
616 SDOperand Zero = CurDAG->getTargetConstant(0, PtrVT);
617 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, PtrVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000618
Scott Michel9999e682007-12-19 07:35:06 +0000619 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AI32 <FI>, 0\n");
Scott Michel58c58182008-01-17 20:38:41 +0000620 NewOpc = SPU::AIr32;
621 Ops[0] = TFI;
622 Ops[1] = Zero;
623 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000624 } else if (Opc == ISD::ZERO_EXTEND) {
625 // (zero_extend:i16 (and:i8 <arg>, <const>))
626 const SDOperand &Op1 = N->getOperand(0);
627
628 if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
629 if (Op1.getOpcode() == ISD::AND) {
630 // Fold this into a single ANDHI. This is often seen in expansions of i1
631 // to i8, then i8 to i16 in logical/branching operations.
632 DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
633 "<arg>, <const>))\n");
634 NewOpc = SPU::ANDHI1To2;
635 Ops[0] = Op1.getOperand(0);
636 Ops[1] = Op1.getOperand(1);
637 n_ops = 2;
638 }
639 }
Scott Michel053c1da2008-01-29 02:16:57 +0000640 } else if (Opc == SPUISD::INSERT_MASK) {
641 SDOperand Op0 = Op.getOperand(0);
642 if (Op0.getOpcode() == SPUISD::AFormAddr) {
643 // (SPUvecinsmask (SPUaform <arg>, 0)) ->
644 // (CBD|CHD|CWD 0, arg)
645 const valtype_map_s *vtm = getValueTypeMapEntry(OpVT);
646 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getOperand(1));
647 assert(vtm->insmask_ins != 0 && "missing insert mask instruction");
648 NewOpc = vtm->insmask_ins;
649 Ops[0] = CurDAG->getTargetConstant(CN->getValue(), Op0.getValueType());
650 Ops[1] = Op0;
651 n_ops = 2;
652
653 AddToISelQueue(Op0);
654 } else if (Op0.getOpcode() == ISD::FrameIndex) {
655 // (SPUvecinsmask <fi>) ->
656 // (CBD|CHD|CWD 0, <fi>)
657 const valtype_map_s *vtm = getValueTypeMapEntry(OpVT);
658 NewOpc = vtm->insmask_ins;
659 Ops[0] = CurDAG->getTargetConstant(0, Op0.getValueType());
660 Ops[1] = Op0;
661 n_ops = 2;
662 } else if (isHighLow(Op0)) {
663 // (SPUvecinsmask (SPUindirect (SPUhi <arg>, 0), (SPUlow <arg>, 0))) ->
664 // (CBD|CHD|CWD 0, arg)
665 const valtype_map_s *vtm = getValueTypeMapEntry(OpVT);
666 NewOpc = vtm->insmask_ins;
667 Ops[0] = CurDAG->getTargetConstant(0, Op0.getValueType());
668 Ops[1] = Op0;
669 n_ops = 2;
670 AddToISelQueue(Op0);
671 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000672 } else if (Opc == SPUISD::LDRESULT) {
673 // Custom select instructions for LDRESULT
674 unsigned VT = N->getValueType(0);
675 SDOperand Arg = N->getOperand(0);
676 SDOperand Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000677 SDNode *Result;
Scott Michel266bc8f2007-12-04 22:23:35 +0000678
679 AddToISelQueue(Arg);
Scott Michel86c041f2007-12-20 00:44:13 +0000680 if (!MVT::isFloatingPoint(VT)) {
681 SDOperand Zero = CurDAG->getTargetConstant(0, VT);
682 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
683
684 if (vtm->ldresult_ins == 0) {
685 cerr << "LDRESULT for unsupported type: "
686 << MVT::getValueTypeString(VT)
687 << "\n";
688 abort();
689 } else
690 Opc = vtm->ldresult_ins;
691
692 AddToISelQueue(Zero);
Scott Michel58c58182008-01-17 20:38:41 +0000693 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000694 } else {
Scott Michel58c58182008-01-17 20:38:41 +0000695 Opc = (VT == MVT::f32 ? SPU::ORf32 : SPU::ORf64);
696 Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000697 }
698
Scott Michel266bc8f2007-12-04 22:23:35 +0000699 Chain = SDOperand(Result, 1);
Scott Michel86c041f2007-12-20 00:44:13 +0000700 AddToISelQueue(Chain);
701
Scott Michel266bc8f2007-12-04 22:23:35 +0000702 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000703 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michel58c58182008-01-17 20:38:41 +0000704 SDOperand Op0 = Op.getOperand(0);
705 if (Op0.getOpcode() == SPUISD::LDRESULT
706 || Op0.getOpcode() == SPUISD::AFormAddr) {
Scott Michel053c1da2008-01-29 02:16:57 +0000707 // (IndirectAddr (LDRESULT|AFormAddr, imm))
Scott Michel58c58182008-01-17 20:38:41 +0000708 SDOperand Op1 = Op.getOperand(1);
709 MVT::ValueType VT = Op.getValueType();
710
Scott Michel053c1da2008-01-29 02:16:57 +0000711 DEBUG(cerr << "CellSPU: IndirectAddr("
712 << (Op0.getOpcode() == SPUISD::LDRESULT
713 ? "LDRESULT"
714 : "AFormAddr")
715 << ", imm):\nOp0 = ");
Scott Michel58c58182008-01-17 20:38:41 +0000716 DEBUG(Op.getOperand(0).Val->dump(CurDAG));
717 DEBUG(cerr << "\nOp1 = ");
718 DEBUG(Op.getOperand(1).Val->dump(CurDAG));
719 DEBUG(cerr << "\n");
720
721 if (Op1.getOpcode() == ISD::Constant) {
722 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
723 Op1 = CurDAG->getTargetConstant(CN->getValue(), VT);
724 }
725 AddToISelQueue(Op0);
726 AddToISelQueue(Op1);
727 NewOpc = SPU::AIr32;
728 Ops[0] = Op0;
729 Ops[1] = Op1;
730 n_ops = 2;
731 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000732 }
733
Scott Michel58c58182008-01-17 20:38:41 +0000734 if (n_ops > 0) {
735 if (N->hasOneUse())
736 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
737 else
738 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
739 } else
740 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000741}
742
743/// createPPCISelDag - This pass converts a legalized DAG into a
744/// SPU-specific DAG, ready for instruction scheduling.
745///
746FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
747 return new SPUDAGToDAGISel(TM);
748}