blob: c181f3ce483dd6f8aa086984b98910c021cd48c8 [file] [log] [blame]
Chris Lattner4ee451d2007-12-29 20:36:04 +00001//===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel266bc8f2007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for the Cell SPU,
11// converting from a legalized dag to a SPU-target dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SPU.h"
16#include "SPUTargetMachine.h"
17#include "SPUISelLowering.h"
18#include "SPUHazardRecognizers.h"
19#include "SPUFrameInfo.h"
Scott Michel203b2d62008-04-30 00:30:08 +000020#include "SPURegisterNames.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Constants.h"
29#include "llvm/GlobalValue.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/Compiler.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000034#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 {
Scott Michel78c47fa2008-03-10 16:58:52 +000044 return isS10Constant(CN->getSignExtended());
Scott Michel266bc8f2007-12-04 22:23:35 +000045 }
46
47 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
48 bool
49 isI32IntS10Immediate(ConstantSDNode *CN)
50 {
Scott Michel78c47fa2008-03-10 16:58:52 +000051 return isS10Constant(CN->getSignExtended());
Scott Michel266bc8f2007-12-04 22:23:35 +000052 }
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 {
Scott Michel79698f62008-03-20 00:51:36 +000068 return isU10Constant(CN->getSignExtended());
Scott Michel504c3692007-12-17 22:32:34 +000069 }
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 {
Scott Michel79698f62008-03-20 00:51:36 +000075 return isS10Constant(CN->getSignExtended());
Scott Michel266bc8f2007-12-04 22:23:35 +000076 }
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 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000113 MVT vt = CN->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000114 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 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000142 MVT vt = FPN->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000143 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 //===------------------------------------------------------------------===//
Duncan Sands83ec4b62008-06-06 12:08:01 +0000164 //! MVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000165
166 struct valtype_map_s {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000167 MVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000168 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000169 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000170 int prefslot_byte; /// Byte offset of the "preferred" slot
Scott Michel266bc8f2007-12-04 22:23:35 +0000171 };
172
173 const valtype_map_s valtype_map[] = {
Scott Michela59d4692008-02-23 18:41:37 +0000174 { MVT::i1, 0, false, 3 },
175 { MVT::i8, SPU::ORBIr8, true, 3 },
176 { MVT::i16, SPU::ORHIr16, true, 2 },
177 { MVT::i32, SPU::ORIr32, true, 0 },
178 { MVT::i64, SPU::ORr64, false, 0 },
179 { MVT::f32, SPU::ORf32, false, 0 },
180 { MVT::f64, SPU::ORf64, false, 0 },
Scott Michel58c58182008-01-17 20:38:41 +0000181 // vector types... (sigh!)
Scott Michela59d4692008-02-23 18:41:37 +0000182 { MVT::v16i8, 0, false, 0 },
183 { MVT::v8i16, 0, false, 0 },
184 { MVT::v4i32, 0, false, 0 },
185 { MVT::v2i64, 0, false, 0 },
186 { MVT::v4f32, 0, false, 0 },
187 { MVT::v2f64, 0, false, 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
Duncan Sands83ec4b62008-06-06 12:08:01 +0000192 const valtype_map_s *getValueTypeMapEntry(MVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000193 {
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) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000197 retval = valtype_map + i;
198 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000199 }
200 }
201
202
203#ifndef NDEBUG
204 if (retval == 0) {
205 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000206 << VT.getMVTString()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000207 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000208 abort();
209 }
210#endif
211
212 return retval;
213 }
214}
215
Dan Gohman844731a2008-05-13 00:00:25 +0000216namespace {
217
Scott Michel266bc8f2007-12-04 22:23:35 +0000218//===--------------------------------------------------------------------===//
219/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
220/// instructions for SelectionDAG operations.
221///
222class SPUDAGToDAGISel :
223 public SelectionDAGISel
224{
225 SPUTargetMachine &TM;
226 SPUTargetLowering &SPUtli;
227 unsigned GlobalBaseReg;
228
229public:
230 SPUDAGToDAGISel(SPUTargetMachine &tm) :
231 SelectionDAGISel(*tm.getTargetLowering()),
232 TM(tm),
233 SPUtli(*tm.getTargetLowering())
234 {}
235
236 virtual bool runOnFunction(Function &Fn) {
237 // Make sure we re-emit a set of the global base reg if necessary
238 GlobalBaseReg = 0;
239 SelectionDAGISel::runOnFunction(Fn);
240 return true;
241 }
242
243 /// getI32Imm - Return a target constant with the specified value, of type
244 /// i32.
245 inline SDOperand getI32Imm(uint32_t Imm) {
246 return CurDAG->getTargetConstant(Imm, MVT::i32);
247 }
248
249 /// getI64Imm - Return a target constant with the specified value, of type
250 /// i64.
251 inline SDOperand getI64Imm(uint64_t Imm) {
252 return CurDAG->getTargetConstant(Imm, MVT::i64);
253 }
254
255 /// getSmallIPtrImm - Return a target constant of pointer type.
256 inline SDOperand getSmallIPtrImm(unsigned Imm) {
257 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
258 }
259
260 /// Select - Convert the specified operand from a target-independent to a
261 /// target-specific node if it hasn't already been changed.
262 SDNode *Select(SDOperand Op);
263
Scott Michel266bc8f2007-12-04 22:23:35 +0000264 //! Returns true if the address N is an A-form (local store) address
265 bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000266 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000267
268 //! D-form address predicate
269 bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000270 SDOperand &Index);
271
272 /// Alternate D-form address using i7 offset predicate
273 bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
274 SDOperand &Base);
275
276 /// D-form address selection workhorse
277 bool DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Disp,
278 SDOperand &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000279
280 //! Address predicate if N can be expressed as an indexed [r+r] operation.
281 bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000282 SDOperand &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000283
284 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
285 /// inline asm expressions.
286 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000287 char ConstraintCode,
288 std::vector<SDOperand> &OutOps,
289 SelectionDAG &DAG) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000290 SDOperand Op0, Op1;
291 switch (ConstraintCode) {
292 default: return true;
293 case 'm': // memory
294 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000295 && !SelectAFormAddr(Op, Op, Op0, Op1))
296 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000297 break;
298 case 'o': // offsetable
299 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000300 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
301 Op0 = Op;
302 AddToISelQueue(Op0); // r+0.
303 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000304 }
305 break;
306 case 'v': // not offsetable
307#if 1
308 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
309#else
310 SelectAddrIdxOnly(Op, Op, Op0, Op1);
311#endif
312 break;
313 }
314
315 OutOps.push_back(Op0);
316 OutOps.push_back(Op1);
317 return false;
318 }
319
320 /// InstructionSelectBasicBlock - This callback is invoked by
321 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
322 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
323
324 virtual const char *getPassName() const {
325 return "Cell SPU DAG->DAG Pattern Instruction Selection";
326 }
327
328 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
329 /// this target when scheduling the DAG.
330 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
331 const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
332 assert(II && "No InstrInfo?");
333 return new SPUHazardRecognizer(*II);
334 }
335
336 // Include the pieces autogenerated from the target description.
337#include "SPUGenDAGISel.inc"
338};
339
Dan Gohman844731a2008-05-13 00:00:25 +0000340}
341
Scott Michel266bc8f2007-12-04 22:23:35 +0000342/// InstructionSelectBasicBlock - This callback is invoked by
343/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
344void
345SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG)
346{
347 DEBUG(BB->dump());
348
349 // Select target instructions for the DAG.
350 DAG.setRoot(SelectRoot(DAG.getRoot()));
351 DAG.RemoveDeadNodes();
352
353 // Emit machine code to BB.
354 ScheduleAndEmitDAG(DAG);
355}
356
Scott Michel266bc8f2007-12-04 22:23:35 +0000357/*!
358 \arg Op The ISD instructio operand
359 \arg N The address to be tested
360 \arg Base The base address
361 \arg Index The base address index
362 */
363bool
364SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000365 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000366 // These match the addr256k operand type:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000367 MVT OffsVT = MVT::i16;
Scott Michel053c1da2008-01-29 02:16:57 +0000368 SDOperand Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000369
370 switch (N.getOpcode()) {
371 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000372 case ISD::ConstantPool:
373 case ISD::GlobalAddress:
374 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
375 abort();
376 /*NOTREACHED*/
377
Scott Michel053c1da2008-01-29 02:16:57 +0000378 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000379 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000380 case ISD::TargetJumpTable:
381 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
382 << "A-form address.\n";
383 abort();
384 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000385
Scott Michel053c1da2008-01-29 02:16:57 +0000386 case SPUISD::AFormAddr:
387 // Just load from memory if there's only a single use of the location,
388 // otherwise, this will get handled below with D-form offset addresses
389 if (N.hasOneUse()) {
390 SDOperand Op0 = N.getOperand(0);
391 switch (Op0.getOpcode()) {
392 case ISD::TargetConstantPool:
393 case ISD::TargetJumpTable:
394 Base = Op0;
395 Index = Zero;
396 return true;
397
398 case ISD::TargetGlobalAddress: {
399 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
400 GlobalValue *GV = GSDN->getGlobal();
401 if (GV->getAlignment() == 16) {
402 Base = Op0;
403 Index = Zero;
404 return true;
405 }
406 break;
407 }
408 }
409 }
410 break;
411 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000412 return false;
413}
414
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000415bool
416SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
417 SDOperand &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000418 const int minDForm2Offset = -(1 << 7);
419 const int maxDForm2Offset = (1 << 7) - 1;
420 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
421 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000422}
423
Scott Michel266bc8f2007-12-04 22:23:35 +0000424/*!
425 \arg Op The ISD instruction (ignored)
426 \arg N The address to be tested
427 \arg Base Base address register/pointer
428 \arg Index Base address index
429
430 Examine the input address by a base register plus a signed 10-bit
431 displacement, [r+I10] (D-form address).
432
433 \return true if \a N is a D-form address with \a Base and \a Index set
434 to non-empty SDOperand instances.
435*/
436bool
437SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000438 SDOperand &Index) {
439 return DFormAddressPredicate(Op, N, Base, Index,
440 SPUFrameInfo::minFrameOffset(),
441 SPUFrameInfo::maxFrameOffset());
442}
443
444bool
445SPUDAGToDAGISel::DFormAddressPredicate(SDOperand Op, SDOperand N, SDOperand &Base,
446 SDOperand &Index, int minOffset,
447 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000448 unsigned Opc = N.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000449 MVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000450
Scott Michel053c1da2008-01-29 02:16:57 +0000451 if (Opc == ISD::FrameIndex) {
452 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000453 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
454 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000455 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000456 << FI << "\n");
457 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000458 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000459 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000460 return true;
461 }
462 } else if (Opc == ISD::ADD) {
463 // Generated by getelementptr
Scott Michel053c1da2008-01-29 02:16:57 +0000464 const SDOperand Op0 = N.getOperand(0);
465 const SDOperand Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000466
Scott Michel053c1da2008-01-29 02:16:57 +0000467 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
468 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
469 Base = CurDAG->getTargetConstant(0, PtrTy);
470 Index = N;
471 return true;
472 } else if (Op1.getOpcode() == ISD::Constant
473 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000474 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Scott Michel053c1da2008-01-29 02:16:57 +0000475 int32_t offset = int32_t(CN->getSignExtended());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000476
Scott Michel053c1da2008-01-29 02:16:57 +0000477 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000478 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
479 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000480 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000481 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000482
Scott Michel203b2d62008-04-30 00:30:08 +0000483 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000484 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000485 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000486 return true;
487 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000488 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000489 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000490 Index = Op0;
491 return true;
492 }
493 } else if (Op0.getOpcode() == ISD::Constant
494 || Op0.getOpcode() == ISD::TargetConstant) {
495 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
496 int32_t offset = int32_t(CN->getSignExtended());
497
498 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000499 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
500 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000501 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000502 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000503
Scott Michel203b2d62008-04-30 00:30:08 +0000504 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000505 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000506 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000507 return true;
508 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000509 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000510 Base = CurDAG->getTargetConstant(offset, PtrTy);
511 Index = Op1;
512 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000513 }
Scott Michel053c1da2008-01-29 02:16:57 +0000514 }
515 } else if (Opc == SPUISD::IndirectAddr) {
516 // Indirect with constant offset -> D-Form address
517 const SDOperand Op0 = N.getOperand(0);
518 const SDOperand Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000519
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000520 if (Op0.getOpcode() == SPUISD::Hi
521 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000522 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000523 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000524 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000525 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000526 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
527 int32_t offset = 0;
528 SDOperand idxOp;
529
530 if (isa<ConstantSDNode>(Op1)) {
531 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
532 offset = int32_t(CN->getSignExtended());
533 idxOp = Op0;
534 } else if (isa<ConstantSDNode>(Op0)) {
535 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
536 offset = int32_t(CN->getSignExtended());
537 idxOp = Op1;
538 }
539
540 if (offset >= minOffset && offset <= maxOffset) {
541 Base = CurDAG->getTargetConstant(offset, PtrTy);
542 Index = idxOp;
543 return true;
544 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000545 }
Scott Michel053c1da2008-01-29 02:16:57 +0000546 } else if (Opc == SPUISD::AFormAddr) {
547 Base = CurDAG->getTargetConstant(0, N.getValueType());
548 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000549 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000550 } else if (Opc == SPUISD::LDRESULT) {
551 Base = CurDAG->getTargetConstant(0, N.getValueType());
552 Index = N;
553 return true;
Scott Michel266bc8f2007-12-04 22:23:35 +0000554 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000555 return false;
556}
557
558/*!
559 \arg Op The ISD instruction operand
560 \arg N The address operand
561 \arg Base The base pointer operand
562 \arg Index The offset/index operand
563
564 If the address \a N can be expressed as a [r + s10imm] address, returns false.
565 Otherwise, creates two operands, Base and Index that will become the [r+r]
566 address.
567*/
568bool
569SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000570 SDOperand &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000571 if (SelectAFormAddr(Op, N, Base, Index)
572 || SelectDFormAddr(Op, N, Base, Index))
573 return false;
574
Scott Michel053c1da2008-01-29 02:16:57 +0000575 // All else fails, punt and use an X-form address:
576 Base = N.getOperand(0);
577 Index = N.getOperand(1);
578 return true;
Scott Michel58c58182008-01-17 20:38:41 +0000579}
580
Scott Michel266bc8f2007-12-04 22:23:35 +0000581//! Convert the operand from a target-independent to a target-specific node
582/*!
583 */
584SDNode *
585SPUDAGToDAGISel::Select(SDOperand Op) {
586 SDNode *N = Op.Val;
587 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000588 int n_ops = -1;
589 unsigned NewOpc;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000590 MVT OpVT = Op.getValueType();
Scott Michel58c58182008-01-17 20:38:41 +0000591 SDOperand Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000592
593 if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) {
594 return NULL; // Already selected.
595 } else if (Opc == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000596 // Selects to (add $sp, FI * stackSlotSize)
597 int FI =
598 SPUFrameInfo::FItoStackOffset(cast<FrameIndexSDNode>(N)->getIndex());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000599 MVT PtrVT = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000600
Scott Michel203b2d62008-04-30 00:30:08 +0000601 // Adjust stack slot to actual offset in frame:
602 if (isS10Constant(FI)) {
603 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AIr32 $sp, "
604 << FI
605 << "\n");
606 NewOpc = SPU::AIr32;
607 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
608 Ops[1] = CurDAG->getTargetConstant(FI, PtrVT);
609 n_ops = 2;
610 } else {
611 DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with Ar32 $sp, "
612 << FI
613 << "\n");
614 NewOpc = SPU::Ar32;
615 Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
616 Ops[1] = CurDAG->getConstant(FI, PtrVT);
617 n_ops = 2;
618
619 AddToISelQueue(Ops[1]);
620 }
Scott Michel58c58182008-01-17 20:38:41 +0000621 } else if (Opc == ISD::ZERO_EXTEND) {
622 // (zero_extend:i16 (and:i8 <arg>, <const>))
623 const SDOperand &Op1 = N->getOperand(0);
624
625 if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
626 if (Op1.getOpcode() == ISD::AND) {
627 // Fold this into a single ANDHI. This is often seen in expansions of i1
628 // to i8, then i8 to i16 in logical/branching operations.
629 DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
630 "<arg>, <const>))\n");
Scott Michela59d4692008-02-23 18:41:37 +0000631 NewOpc = SPU::ANDHIi8i16;
Scott Michel58c58182008-01-17 20:38:41 +0000632 Ops[0] = Op1.getOperand(0);
633 Ops[1] = Op1.getOperand(1);
634 n_ops = 2;
635 }
636 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000637 } else if (Opc == SPUISD::LDRESULT) {
638 // Custom select instructions for LDRESULT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000639 MVT VT = N->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000640 SDOperand Arg = N->getOperand(0);
641 SDOperand Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000642 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000643 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
644
645 if (vtm->ldresult_ins == 0) {
646 cerr << "LDRESULT for unsupported type: "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000647 << VT.getMVTString()
Scott Michela59d4692008-02-23 18:41:37 +0000648 << "\n";
649 abort();
650 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000651
652 AddToISelQueue(Arg);
Scott Michela59d4692008-02-23 18:41:37 +0000653 Opc = vtm->ldresult_ins;
654 if (vtm->ldresult_imm) {
Scott Michel86c041f2007-12-20 00:44:13 +0000655 SDOperand Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000656
657 AddToISelQueue(Zero);
Scott Michel58c58182008-01-17 20:38:41 +0000658 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000659 } else {
Scott Michel58c58182008-01-17 20:38:41 +0000660 Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000661 }
662
Scott Michel266bc8f2007-12-04 22:23:35 +0000663 Chain = SDOperand(Result, 1);
Scott Michel86c041f2007-12-20 00:44:13 +0000664 AddToISelQueue(Chain);
665
Scott Michel266bc8f2007-12-04 22:23:35 +0000666 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000667 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michel58c58182008-01-17 20:38:41 +0000668 SDOperand Op0 = Op.getOperand(0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000669 if (Op0.getOpcode() == SPUISD::LDRESULT) {
670 /* || Op0.getOpcode() == SPUISD::AFormAddr) */
671 // (IndirectAddr (LDRESULT, imm))
Scott Michel58c58182008-01-17 20:38:41 +0000672 SDOperand Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000673 MVT VT = Op.getValueType();
Scott Michel58c58182008-01-17 20:38:41 +0000674
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000675 DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = ");
Scott Michel58c58182008-01-17 20:38:41 +0000676 DEBUG(Op.getOperand(0).Val->dump(CurDAG));
677 DEBUG(cerr << "\nOp1 = ");
678 DEBUG(Op.getOperand(1).Val->dump(CurDAG));
679 DEBUG(cerr << "\n");
680
681 if (Op1.getOpcode() == ISD::Constant) {
682 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
683 Op1 = CurDAG->getTargetConstant(CN->getValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000684 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
685 AddToISelQueue(Op0);
686 AddToISelQueue(Op1);
687 Ops[0] = Op0;
688 Ops[1] = Op1;
689 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000690 }
Scott Michel58c58182008-01-17 20:38:41 +0000691 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000692 }
693
Scott Michel58c58182008-01-17 20:38:41 +0000694 if (n_ops > 0) {
695 if (N->hasOneUse())
696 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
697 else
698 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
699 } else
700 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000701}
702
703/// createPPCISelDag - This pass converts a legalized DAG into a
704/// SPU-specific DAG, ready for instruction scheduling.
705///
706FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
707 return new SPUDAGToDAGISel(TM);
708}