blob: 6d7f40d5d7515475601223b183ecca8998aea573 [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 Michel94bd57e2009-01-15 04:41:47 +000021#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000028#include "llvm/Target/TargetOptions.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Constants.h"
31#include "llvm/GlobalValue.h"
32#include "llvm/Intrinsics.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/Compiler.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000036
37using namespace llvm;
38
39namespace {
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
41 bool
42 isI64IntS10Immediate(ConstantSDNode *CN)
43 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000044 return isS10Constant(CN->getSExtValue());
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 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000051 return isS10Constant(CN->getSExtValue());
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 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000068 return isU10Constant(CN->getSExtValue());
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 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000075 return isS10Constant(CN->getSExtValue());
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 {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000090 return isU10Constant((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000091 }
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);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000114 Imm = (short) CN->getZExtValue();
Duncan Sands8e4eb092008-06-08 20:54:56 +0000115 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000116 return true;
117 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000118 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000119 short s_val = (short) i_val;
120 return i_val == s_val;
121 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000122 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000123 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
Scott Michel02d711b2008-12-30 23:28:25 +0000154 isHighLow(const SDValue &Op)
Scott Michel053c1da2008-01-29 02:16:57 +0000155 {
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 Michelf0569be2008-12-27 04:51:36 +0000170 unsigned lrinst; /// LR instruction
Scott Michel266bc8f2007-12-04 22:23:35 +0000171 };
172
173 const valtype_map_s valtype_map[] = {
Scott Michelf0569be2008-12-27 04:51:36 +0000174 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
175 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
176 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
177 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
178 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
179 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
Scott Michel58c58182008-01-17 20:38:41 +0000180 // vector types... (sigh!)
Scott Michelf0569be2008-12-27 04:51:36 +0000181 { MVT::v16i8, 0, false, SPU::LRv16i8 },
182 { MVT::v8i16, 0, false, SPU::LRv8i16 },
183 { MVT::v4i32, 0, false, SPU::LRv4i32 },
184 { MVT::v2i64, 0, false, SPU::LRv2i64 },
185 { MVT::v4f32, 0, false, SPU::LRv4f32 },
186 { MVT::v2f64, 0, false, SPU::LRv2f64 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000187 };
188
189 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
190
Duncan Sands83ec4b62008-06-06 12:08:01 +0000191 const valtype_map_s *getValueTypeMapEntry(MVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000192 {
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 "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000205 << VT.getMVTString()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000206 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000207 abort();
208 }
209#endif
210
211 return retval;
212 }
213}
214
Dan Gohman844731a2008-05-13 00:00:25 +0000215namespace {
216
Scott Michel266bc8f2007-12-04 22:23:35 +0000217//===--------------------------------------------------------------------===//
218/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
219/// instructions for SelectionDAG operations.
220///
221class SPUDAGToDAGISel :
222 public SelectionDAGISel
223{
224 SPUTargetMachine &TM;
225 SPUTargetLowering &SPUtli;
226 unsigned GlobalBaseReg;
227
228public:
Dan Gohman1002c022008-07-07 18:00:37 +0000229 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +0000230 SelectionDAGISel(tm),
Scott Michel266bc8f2007-12-04 22:23:35 +0000231 TM(tm),
232 SPUtli(*tm.getTargetLowering())
233 {}
Scott Michel02d711b2008-12-30 23:28:25 +0000234
Scott Michel266bc8f2007-12-04 22:23:35 +0000235 virtual bool runOnFunction(Function &Fn) {
236 // Make sure we re-emit a set of the global base reg if necessary
237 GlobalBaseReg = 0;
238 SelectionDAGISel::runOnFunction(Fn);
239 return true;
240 }
Scott Michel02d711b2008-12-30 23:28:25 +0000241
Scott Michel266bc8f2007-12-04 22:23:35 +0000242 /// getI32Imm - Return a target constant with the specified value, of type
243 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000244 inline SDValue getI32Imm(uint32_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000245 return CurDAG->getTargetConstant(Imm, MVT::i32);
246 }
247
248 /// getI64Imm - Return a target constant with the specified value, of type
249 /// i64.
Dan Gohman475871a2008-07-27 21:46:04 +0000250 inline SDValue getI64Imm(uint64_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000251 return CurDAG->getTargetConstant(Imm, MVT::i64);
252 }
Scott Michel02d711b2008-12-30 23:28:25 +0000253
Scott Michel266bc8f2007-12-04 22:23:35 +0000254 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman475871a2008-07-27 21:46:04 +0000255 inline SDValue getSmallIPtrImm(unsigned Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000256 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000257 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000258
Scott Michel94bd57e2009-01-15 04:41:47 +0000259 SDNode *emitBuildVector(SDValue build_vec) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000260 MVT vecVT = build_vec.getValueType();
261 SDNode *bvNode = build_vec.getNode();
262 bool canBeSelected = false;
263
264 // Check to see if this vector can be represented as a CellSPU immediate
265 // constant.
266 if (vecVT == MVT::v8i16) {
267 if (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0) {
268 canBeSelected = true;
269 }
270 } else if (vecVT == MVT::v4i32) {
271 if ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0)
272 || (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0)
273 || (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0)
274 || (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0)) {
275 canBeSelected = true;
276 }
277 } else if (vecVT == MVT::v2i64) {
278 if ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)
279 || (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)
280 || (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)) {
281 canBeSelected = true;
282 }
283 }
284
285 if (canBeSelected) {
286 return Select(build_vec);
287 }
288
289 // No, need to emit a constant pool spill:
Scott Michel94bd57e2009-01-15 04:41:47 +0000290 std::vector<Constant*> CV;
291
292 for (size_t i = 0; i < build_vec.getNumOperands(); ++i) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000293 ConstantSDNode *V = dyn_cast<ConstantSDNode > (build_vec.getOperand(i));
294 CV.push_back(const_cast<ConstantInt *> (V->getConstantIntValue()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000295 }
296
297 Constant *CP = ConstantVector::get(CV);
298 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000299 unsigned Alignment = 1 << cast<ConstantPoolSDNode > (CPIdx)->getAlignment();
Scott Michel94bd57e2009-01-15 04:41:47 +0000300 SDValue CGPoolOffset =
301 SPU::LowerConstantPool(CPIdx, *CurDAG,
302 SPUtli.getSPUTargetMachine());
303 return SelectCode(CurDAG->getLoad(build_vec.getValueType(),
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000304 CurDAG->getEntryNode(), CGPoolOffset,
305 PseudoSourceValue::getConstantPool(), 0,
306 false, Alignment));
Scott Michel94bd57e2009-01-15 04:41:47 +0000307 }
308
Scott Michel266bc8f2007-12-04 22:23:35 +0000309 /// Select - Convert the specified operand from a target-independent to a
310 /// target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +0000311 SDNode *Select(SDValue Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000312
Scott Michel02d711b2008-12-30 23:28:25 +0000313 //! Emit the instruction sequence for i64 shl
314 SDNode *SelectSHLi64(SDValue &Op, MVT OpVT);
315
316 //! Emit the instruction sequence for i64 srl
317 SDNode *SelectSRLi64(SDValue &Op, MVT OpVT);
318
319 //! Emit the instruction sequence for i64 sra
320 SDNode *SelectSRAi64(SDValue &Op, MVT OpVT);
321
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000322 //! Emit the necessary sequence for loading i64 constants:
323 SDNode *SelectI64Constant(SDValue &Op, MVT OpVT);
324
Scott Michel266bc8f2007-12-04 22:23:35 +0000325 //! Returns true if the address N is an A-form (local store) address
Dan Gohman475871a2008-07-27 21:46:04 +0000326 bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
327 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000328
329 //! D-form address predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000330 bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
331 SDValue &Index);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000332
333 /// Alternate D-form address using i7 offset predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000334 bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
335 SDValue &Base);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000336
337 /// D-form address selection workhorse
Dan Gohman475871a2008-07-27 21:46:04 +0000338 bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
339 SDValue &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000340
341 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000342 bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
343 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000344
345 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
346 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000347 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000348 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000349 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +0000350 SDValue Op0, Op1;
Scott Michel266bc8f2007-12-04 22:23:35 +0000351 switch (ConstraintCode) {
352 default: return true;
353 case 'm': // memory
Scott Michel02d711b2008-12-30 23:28:25 +0000354 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000355 && !SelectAFormAddr(Op, Op, Op0, Op1))
356 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000357 break;
358 case 'o': // offsetable
359 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000360 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
361 Op0 = Op;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000362 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000363 }
364 break;
365 case 'v': // not offsetable
366#if 1
367 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
368#else
369 SelectAddrIdxOnly(Op, Op, Op0, Op1);
370#endif
371 break;
372 }
Scott Michel02d711b2008-12-30 23:28:25 +0000373
Scott Michel266bc8f2007-12-04 22:23:35 +0000374 OutOps.push_back(Op0);
375 OutOps.push_back(Op1);
376 return false;
377 }
378
Evan Chengdb8d56b2008-06-30 20:45:06 +0000379 /// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000380 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000381 virtual void InstructionSelect();
Scott Michel266bc8f2007-12-04 22:23:35 +0000382
383 virtual const char *getPassName() const {
384 return "Cell SPU DAG->DAG Pattern Instruction Selection";
Scott Michel02d711b2008-12-30 23:28:25 +0000385 }
386
Scott Michel266bc8f2007-12-04 22:23:35 +0000387 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
388 /// this target when scheduling the DAG.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000389 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohman6448d912008-09-04 15:39:15 +0000390 const TargetInstrInfo *II = TM.getInstrInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +0000391 assert(II && "No InstrInfo?");
Scott Michel02d711b2008-12-30 23:28:25 +0000392 return new SPUHazardRecognizer(*II);
Scott Michel266bc8f2007-12-04 22:23:35 +0000393 }
394
395 // Include the pieces autogenerated from the target description.
396#include "SPUGenDAGISel.inc"
397};
398
Dan Gohman844731a2008-05-13 00:00:25 +0000399}
400
Evan Chengdb8d56b2008-06-30 20:45:06 +0000401/// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000402/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
403void
Dan Gohmanf350b272008-08-23 02:25:05 +0000404SPUDAGToDAGISel::InstructionSelect()
Scott Michel266bc8f2007-12-04 22:23:35 +0000405{
406 DEBUG(BB->dump());
407
408 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000409 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +0000410 CurDAG->RemoveDeadNodes();
Scott Michel266bc8f2007-12-04 22:23:35 +0000411}
412
Scott Michel266bc8f2007-12-04 22:23:35 +0000413/*!
414 \arg Op The ISD instructio operand
415 \arg N The address to be tested
416 \arg Base The base address
417 \arg Index The base address index
418 */
419bool
Dan Gohman475871a2008-07-27 21:46:04 +0000420SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
421 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000422 // These match the addr256k operand type:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000423 MVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000424 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000425
426 switch (N.getOpcode()) {
427 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000428 case ISD::ConstantPool:
429 case ISD::GlobalAddress:
430 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
431 abort();
432 /*NOTREACHED*/
433
Scott Michel053c1da2008-01-29 02:16:57 +0000434 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000435 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000436 case ISD::TargetJumpTable:
437 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
438 << "A-form address.\n";
439 abort();
440 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000441
Scott Michel02d711b2008-12-30 23:28:25 +0000442 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000443 // Just load from memory if there's only a single use of the location,
444 // otherwise, this will get handled below with D-form offset addresses
445 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000446 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000447 switch (Op0.getOpcode()) {
448 case ISD::TargetConstantPool:
449 case ISD::TargetJumpTable:
450 Base = Op0;
451 Index = Zero;
452 return true;
453
454 case ISD::TargetGlobalAddress: {
455 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
456 GlobalValue *GV = GSDN->getGlobal();
457 if (GV->getAlignment() == 16) {
458 Base = Op0;
459 Index = Zero;
460 return true;
461 }
462 break;
463 }
464 }
465 }
466 break;
467 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000468 return false;
469}
470
Scott Michel02d711b2008-12-30 23:28:25 +0000471bool
Dan Gohman475871a2008-07-27 21:46:04 +0000472SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
473 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000474 const int minDForm2Offset = -(1 << 7);
475 const int maxDForm2Offset = (1 << 7) - 1;
476 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
477 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000478}
479
Scott Michel266bc8f2007-12-04 22:23:35 +0000480/*!
481 \arg Op The ISD instruction (ignored)
482 \arg N The address to be tested
483 \arg Base Base address register/pointer
484 \arg Index Base address index
485
486 Examine the input address by a base register plus a signed 10-bit
487 displacement, [r+I10] (D-form address).
488
489 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000490 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000491*/
492bool
Dan Gohman475871a2008-07-27 21:46:04 +0000493SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
494 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000495 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000496 SPUFrameInfo::minFrameOffset(),
497 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000498}
499
500bool
Dan Gohman475871a2008-07-27 21:46:04 +0000501SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
502 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000503 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000504 unsigned Opc = N.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000505 MVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000506
Scott Michel053c1da2008-01-29 02:16:57 +0000507 if (Opc == ISD::FrameIndex) {
508 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000509 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
510 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000511 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000512 << FI << "\n");
513 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000514 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000515 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000516 return true;
517 }
518 } else if (Opc == ISD::ADD) {
519 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000520 const SDValue Op0 = N.getOperand(0);
521 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000522
Scott Michel053c1da2008-01-29 02:16:57 +0000523 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
524 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
525 Base = CurDAG->getTargetConstant(0, PtrTy);
526 Index = N;
527 return true;
528 } else if (Op1.getOpcode() == ISD::Constant
529 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000530 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000531 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000532
Scott Michel053c1da2008-01-29 02:16:57 +0000533 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000534 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
535 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000536 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000537 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000538
Scott Michel203b2d62008-04-30 00:30:08 +0000539 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000540 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000541 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000542 return true;
543 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000544 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000545 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000546 Index = Op0;
547 return true;
548 }
549 } else if (Op0.getOpcode() == ISD::Constant
550 || Op0.getOpcode() == ISD::TargetConstant) {
551 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000552 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000553
554 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000555 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
556 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000557 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000558 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000559
Scott Michel203b2d62008-04-30 00:30:08 +0000560 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000561 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000562 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000563 return true;
564 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000565 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000566 Base = CurDAG->getTargetConstant(offset, PtrTy);
567 Index = Op1;
568 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000569 }
Scott Michel053c1da2008-01-29 02:16:57 +0000570 }
571 } else if (Opc == SPUISD::IndirectAddr) {
572 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000573 const SDValue Op0 = N.getOperand(0);
574 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000575
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000576 if (Op0.getOpcode() == SPUISD::Hi
577 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000578 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000579 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000580 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000581 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000582 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
583 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000584 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000585
586 if (isa<ConstantSDNode>(Op1)) {
587 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000588 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000589 idxOp = Op0;
590 } else if (isa<ConstantSDNode>(Op0)) {
591 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000592 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000593 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000594 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000595
596 if (offset >= minOffset && offset <= maxOffset) {
597 Base = CurDAG->getTargetConstant(offset, PtrTy);
598 Index = idxOp;
599 return true;
600 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000601 }
Scott Michel053c1da2008-01-29 02:16:57 +0000602 } else if (Opc == SPUISD::AFormAddr) {
603 Base = CurDAG->getTargetConstant(0, N.getValueType());
604 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000605 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000606 } else if (Opc == SPUISD::LDRESULT) {
607 Base = CurDAG->getTargetConstant(0, N.getValueType());
608 Index = N;
609 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000610 } else if (Opc == ISD::Register || Opc == ISD::CopyFromReg) {
611 unsigned OpOpc = Op.getOpcode();
612
613 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
614 // Direct load/store without getelementptr
615 SDValue Addr, Offs;
616
617 // Get the register from CopyFromReg
618 if (Opc == ISD::CopyFromReg)
619 Addr = N.getOperand(1);
620 else
621 Addr = N; // Register
622
Scott Michelaedc6372008-12-10 00:15:19 +0000623 Offs = ((OpOpc == ISD::STORE) ? Op.getOperand(3) : Op.getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000624
625 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
626 if (Offs.getOpcode() == ISD::UNDEF)
627 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
628
629 Base = Offs;
630 Index = Addr;
631 return true;
632 }
Scott Michelaedc6372008-12-10 00:15:19 +0000633 } else {
634 /* If otherwise unadorned, default to D-form address with 0 offset: */
635 if (Opc == ISD::CopyFromReg) {
636 Index = N.getOperand(1);
637 } else {
638 Index = N;
639 }
640
641 Base = CurDAG->getTargetConstant(0, Index.getValueType());
642 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000643 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000644 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000645
Scott Michel266bc8f2007-12-04 22:23:35 +0000646 return false;
647}
648
649/*!
650 \arg Op The ISD instruction operand
651 \arg N The address operand
652 \arg Base The base pointer operand
653 \arg Index The offset/index operand
654
Scott Michel9c0c6b22008-11-21 02:56:16 +0000655 If the address \a N can be expressed as an A-form or D-form address, returns
656 false. Otherwise, creates two operands, Base and Index that will become the
657 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000658*/
659bool
Dan Gohman475871a2008-07-27 21:46:04 +0000660SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
661 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000662 if (!SelectAFormAddr(Op, N, Base, Index)
663 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000664 // If the address is neither A-form or D-form, punt and use an X-form
665 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000666 Base = N.getOperand(1);
667 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000668 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000669 }
670
671 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000672}
673
Scott Michel266bc8f2007-12-04 22:23:35 +0000674//! Convert the operand from a target-independent to a target-specific node
675/*!
676 */
677SDNode *
Dan Gohman475871a2008-07-27 21:46:04 +0000678SPUDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000679 SDNode *N = Op.getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000680 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000681 int n_ops = -1;
682 unsigned NewOpc;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000683 MVT OpVT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000684 SDValue Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000685
Dan Gohmane8be6c62008-07-17 19:10:17 +0000686 if (N->isMachineOpcode()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000687 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000688 }
689
690 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000691 int FI = cast<FrameIndexSDNode>(N)->getIndex();
692 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
693 SDValue Imm0 = CurDAG->getTargetConstant(0, Op.getValueType());
Scott Michel266bc8f2007-12-04 22:23:35 +0000694
Scott Michel02d711b2008-12-30 23:28:25 +0000695 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000696 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000697 Ops[0] = TFI;
698 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000699 n_ops = 2;
700 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000701 NewOpc = SPU::Ar32;
Scott Michel02d711b2008-12-30 23:28:25 +0000702 Ops[0] = CurDAG->getRegister(SPU::R1, Op.getValueType());
703 Ops[1] = SDValue(CurDAG->getTargetNode(SPU::ILAr32, Op.getValueType(),
704 TFI, Imm0), 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000705 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000706 }
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000707 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
708 // Catch the i64 constants that end up here. Note: The backend doesn't
709 // attempt to legalize the constant (it's useless because DAGCombiner
710 // will insert 64-bit constants and we can't stop it).
711 return SelectI64Constant(Op, OpVT);
Scott Michel94bd57e2009-01-15 04:41:47 +0000712 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
713 && OpVT == MVT::i64) {
714 SDValue Op0 = Op.getOperand(0);
715 MVT Op0VT = Op0.getValueType();
716 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
717 MVT OpVecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
718 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000719
Scott Michel94bd57e2009-01-15 04:41:47 +0000720 switch (Op0VT.getSimpleVT()) {
721 default:
722 cerr << "CellSPU Select: Unhandled zero/any extend MVT\n";
723 abort();
724 /*NOTREACHED*/
725 break;
726 case MVT::i32:
727 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000728 CurDAG->getConstant(0x80808080, MVT::i32),
729 CurDAG->getConstant(0x00010203, MVT::i32),
730 CurDAG->getConstant(0x80808080, MVT::i32),
731 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000732 break;
733
734 case MVT::i16:
735 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000736 CurDAG->getConstant(0x80808080, MVT::i32),
737 CurDAG->getConstant(0x80800203, MVT::i32),
738 CurDAG->getConstant(0x80808080, MVT::i32),
739 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000740 break;
741
742 case MVT::i8:
743 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000744 CurDAG->getConstant(0x80808080, MVT::i32),
745 CurDAG->getConstant(0x80808003, MVT::i32),
746 CurDAG->getConstant(0x80808080, MVT::i32),
747 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000748 break;
Scott Michel58c58182008-01-17 20:38:41 +0000749 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000750
751 SDNode *shufMaskLoad = emitBuildVector(shufMask);
752 SDNode *PromoteScalar =
753 SelectCode(CurDAG->getNode(SPUISD::PREFSLOT2VEC, Op0VecVT, Op0));
754
755 SDValue zextShuffle =
756 CurDAG->getNode(SPUISD::SHUFB, OpVecVT,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000757 SDValue(PromoteScalar, 0),
758 SDValue(PromoteScalar, 0),
759 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000760
761 // N.B.: BIT_CONVERT replaces and updates the zextShuffle node, so we
762 // re-use it in the VEC2PREFSLOT selection without needing to explicitly
763 // call SelectCode (it's already done for us.)
764 SelectCode(CurDAG->getNode(ISD::BIT_CONVERT, OpVecVT, zextShuffle));
765 return SelectCode(CurDAG->getNode(SPUISD::VEC2PREFSLOT, OpVT,
766 zextShuffle));
767 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
768 SDNode *CGLoad =
769 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG));
770
771 return SelectCode(CurDAG->getNode(SPUISD::ADD64_MARKER, OpVT,
772 Op.getOperand(0), Op.getOperand(1),
773 SDValue(CGLoad, 0)));
774 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
775 SDNode *CGLoad =
776 emitBuildVector(SPU::getBorrowGenerateShufMask(*CurDAG));
777
778 return SelectCode(CurDAG->getNode(SPUISD::SUB64_MARKER, OpVT,
779 Op.getOperand(0), Op.getOperand(1),
780 SDValue(CGLoad, 0)));
781 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
782 SDNode *CGLoad =
783 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG));
784
785 return SelectCode(CurDAG->getNode(SPUISD::MUL64_MARKER, OpVT,
786 Op.getOperand(0), Op.getOperand(1),
787 SDValue(CGLoad, 0)));
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000788 } else if (Opc == ISD::TRUNCATE) {
789 SDValue Op0 = Op.getOperand(0);
790 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
791 && OpVT == MVT::i32
792 && Op0.getValueType() == MVT::i64) {
793 // Catch the (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32 to
794 // take advantage of the fact that the upper 32 bits are in the
795 // i32 preferred slot and avoid all kinds of other shuffle gymnastics:
796 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
797 if (CN != 0) {
798 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000799
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000800 if (shift_amt >= 32) {
801 SDNode *hi32 =
802 CurDAG->getTargetNode(SPU::ORr32_r64, OpVT, Op0.getOperand(0));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000803
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000804 shift_amt -= 32;
805 if (shift_amt > 0) {
806 // Take care of the additional shift, if present:
807 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
808 unsigned Opc = SPU::ROTMAIr32_i32;
809
810 if (Op0.getOpcode() == ISD::SRL)
811 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000812
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000813 hi32 = CurDAG->getTargetNode(Opc, OpVT, SDValue(hi32, 0), shift);
814 }
815
816 return hi32;
817 }
818 }
819 }
Scott Michel02d711b2008-12-30 23:28:25 +0000820 } else if (Opc == ISD::SHL) {
821 if (OpVT == MVT::i64) {
822 return SelectSHLi64(Op, OpVT);
823 }
824 } else if (Opc == ISD::SRL) {
825 if (OpVT == MVT::i64) {
826 return SelectSRLi64(Op, OpVT);
827 }
828 } else if (Opc == ISD::SRA) {
829 if (OpVT == MVT::i64) {
830 return SelectSRAi64(Op, OpVT);
831 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000832 } else if (Opc == SPUISD::LDRESULT) {
833 // Custom select instructions for LDRESULT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000834 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue Arg = N->getOperand(0);
836 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000837 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000838 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
839
840 if (vtm->ldresult_ins == 0) {
841 cerr << "LDRESULT for unsupported type: "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000842 << VT.getMVTString()
Scott Michela59d4692008-02-23 18:41:37 +0000843 << "\n";
844 abort();
845 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000846
Scott Michela59d4692008-02-23 18:41:37 +0000847 Opc = vtm->ldresult_ins;
848 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000849 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000850
Scott Michel58c58182008-01-17 20:38:41 +0000851 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000852 } else {
Scott Michel30ee7df2008-12-04 03:02:42 +0000853 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000854 }
855
Scott Michel266bc8f2007-12-04 22:23:35 +0000856 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000857 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000858 // Look at the operands: SelectCode() will catch the cases that aren't
859 // specifically handled here.
860 //
861 // SPUInstrInfo catches the following patterns:
862 // (SPUindirect (SPUhi ...), (SPUlo ...))
863 // (SPUindirect $sp, imm)
864 MVT VT = Op.getValueType();
865 SDValue Op0 = N->getOperand(0);
866 SDValue Op1 = N->getOperand(1);
867 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000868
Scott Michelf0569be2008-12-27 04:51:36 +0000869 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
870 || (Op0.getOpcode() == ISD::Register
871 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
872 && RN->getReg() != SPU::R1))) {
873 NewOpc = SPU::Ar32;
Scott Michel58c58182008-01-17 20:38:41 +0000874 if (Op1.getOpcode() == ISD::Constant) {
875 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000876 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000877 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
Scott Michel58c58182008-01-17 20:38:41 +0000878 }
Scott Michelf0569be2008-12-27 04:51:36 +0000879 Ops[0] = Op0;
880 Ops[1] = Op1;
881 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000882 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000883 }
Scott Michel02d711b2008-12-30 23:28:25 +0000884
Scott Michel58c58182008-01-17 20:38:41 +0000885 if (n_ops > 0) {
886 if (N->hasOneUse())
887 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
888 else
889 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
890 } else
891 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000892}
893
Scott Michel02d711b2008-12-30 23:28:25 +0000894/*!
895 * Emit the instruction sequence for i64 left shifts. The basic algorithm
896 * is to fill the bottom two word slots with zeros so that zeros are shifted
897 * in as the entire quadword is shifted left.
898 *
899 * \note This code could also be used to implement v2i64 shl.
900 *
901 * @param Op The shl operand
902 * @param OpVT Op's machine value value type (doesn't need to be passed, but
903 * makes life easier.)
904 * @return The SDNode with the entire instruction sequence
905 */
906SDNode *
907SPUDAGToDAGISel::SelectSHLi64(SDValue &Op, MVT OpVT) {
908 SDValue Op0 = Op.getOperand(0);
909 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
910 SDValue ShiftAmt = Op.getOperand(1);
911 MVT ShiftAmtVT = ShiftAmt.getValueType();
912 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
913 SDValue SelMaskVal;
914
915 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op0);
916 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
917 SelMask = CurDAG->getTargetNode(SPU::FSMBIv2i64, VecVT, SelMaskVal);
918 ZeroFill = CurDAG->getTargetNode(SPU::ILv2i64, VecVT,
919 CurDAG->getTargetConstant(0, OpVT));
920 VecOp0 = CurDAG->getTargetNode(SPU::SELBv2i64, VecVT,
921 SDValue(ZeroFill, 0),
922 SDValue(VecOp0, 0),
923 SDValue(SelMask, 0));
924
925 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
926 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
927 unsigned bits = unsigned(CN->getZExtValue()) & 7;
928
929 if (bytes > 0) {
930 Shift =
931 CurDAG->getTargetNode(SPU::SHLQBYIv2i64, VecVT,
932 SDValue(VecOp0, 0),
933 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
934 }
935
936 if (bits > 0) {
937 Shift =
938 CurDAG->getTargetNode(SPU::SHLQBIIv2i64, VecVT,
939 SDValue((Shift != 0 ? Shift : VecOp0), 0),
940 CurDAG->getTargetConstant(bits, ShiftAmtVT));
941 }
942 } else {
943 SDNode *Bytes =
944 CurDAG->getTargetNode(SPU::ROTMIr32, ShiftAmtVT,
945 ShiftAmt,
946 CurDAG->getTargetConstant(3, ShiftAmtVT));
947 SDNode *Bits =
948 CurDAG->getTargetNode(SPU::ANDIr32, ShiftAmtVT,
949 ShiftAmt,
950 CurDAG->getTargetConstant(7, ShiftAmtVT));
951 Shift =
952 CurDAG->getTargetNode(SPU::SHLQBYv2i64, VecVT,
953 SDValue(VecOp0, 0), SDValue(Bytes, 0));
954 Shift =
955 CurDAG->getTargetNode(SPU::SHLQBIv2i64, VecVT,
956 SDValue(Shift, 0), SDValue(Bits, 0));
957 }
958
959 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
960}
961
962/*!
963 * Emit the instruction sequence for i64 logical right shifts.
964 *
965 * @param Op The shl operand
966 * @param OpVT Op's machine value value type (doesn't need to be passed, but
967 * makes life easier.)
968 * @return The SDNode with the entire instruction sequence
969 */
970SDNode *
971SPUDAGToDAGISel::SelectSRLi64(SDValue &Op, MVT OpVT) {
972 SDValue Op0 = Op.getOperand(0);
973 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
974 SDValue ShiftAmt = Op.getOperand(1);
975 MVT ShiftAmtVT = ShiftAmt.getValueType();
976 SDNode *VecOp0, *Shift = 0;
977
978 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op0);
979
980 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
981 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
982 unsigned bits = unsigned(CN->getZExtValue()) & 7;
983
984 if (bytes > 0) {
985 Shift =
986 CurDAG->getTargetNode(SPU::ROTQMBYIv2i64, VecVT,
987 SDValue(VecOp0, 0),
988 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
989 }
990
991 if (bits > 0) {
992 Shift =
993 CurDAG->getTargetNode(SPU::ROTQMBIIv2i64, VecVT,
994 SDValue((Shift != 0 ? Shift : VecOp0), 0),
995 CurDAG->getTargetConstant(bits, ShiftAmtVT));
996 }
997 } else {
998 SDNode *Bytes =
999 CurDAG->getTargetNode(SPU::ROTMIr32, ShiftAmtVT,
1000 ShiftAmt,
1001 CurDAG->getTargetConstant(3, ShiftAmtVT));
1002 SDNode *Bits =
1003 CurDAG->getTargetNode(SPU::ANDIr32, ShiftAmtVT,
1004 ShiftAmt,
1005 CurDAG->getTargetConstant(7, ShiftAmtVT));
1006
1007 // Ensure that the shift amounts are negated!
1008 Bytes = CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
1009 SDValue(Bytes, 0),
1010 CurDAG->getTargetConstant(0, ShiftAmtVT));
1011
1012 Bits = CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
1013 SDValue(Bits, 0),
1014 CurDAG->getTargetConstant(0, ShiftAmtVT));
1015
1016 Shift =
1017 CurDAG->getTargetNode(SPU::ROTQMBYv2i64, VecVT,
1018 SDValue(VecOp0, 0), SDValue(Bytes, 0));
1019 Shift =
1020 CurDAG->getTargetNode(SPU::ROTQMBIv2i64, VecVT,
1021 SDValue(Shift, 0), SDValue(Bits, 0));
1022 }
1023
1024 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
1025}
1026
1027/*!
1028 * Emit the instruction sequence for i64 arithmetic right shifts.
1029 *
1030 * @param Op The shl operand
1031 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1032 * makes life easier.)
1033 * @return The SDNode with the entire instruction sequence
1034 */
1035SDNode *
1036SPUDAGToDAGISel::SelectSRAi64(SDValue &Op, MVT OpVT) {
1037 // Promote Op0 to vector
1038 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
1039 SDValue ShiftAmt = Op.getOperand(1);
1040 MVT ShiftAmtVT = ShiftAmt.getValueType();
1041
1042 SDNode *VecOp0 =
1043 CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op.getOperand(0));
1044
1045 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1046 SDNode *SignRot =
1047 CurDAG->getTargetNode(SPU::ROTMAIv2i64_i32, MVT::v2i64,
1048 SDValue(VecOp0, 0), SignRotAmt);
1049 SDNode *UpperHalfSign =
1050 CurDAG->getTargetNode(SPU::ORi32_v4i32, MVT::i32, SDValue(SignRot, 0));
1051
1052 SDNode *UpperHalfSignMask =
1053 CurDAG->getTargetNode(SPU::FSM64r32, VecVT, SDValue(UpperHalfSign, 0));
1054 SDNode *UpperLowerMask =
1055 CurDAG->getTargetNode(SPU::FSMBIv2i64, VecVT,
1056 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1057 SDNode *UpperLowerSelect =
1058 CurDAG->getTargetNode(SPU::SELBv2i64, VecVT,
1059 SDValue(UpperHalfSignMask, 0),
1060 SDValue(VecOp0, 0),
1061 SDValue(UpperLowerMask, 0));
1062
1063 SDNode *Shift = 0;
1064
1065 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1066 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1067 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1068
1069 if (bytes > 0) {
1070 bytes = 31 - bytes;
1071 Shift =
1072 CurDAG->getTargetNode(SPU::ROTQBYIv2i64, VecVT,
1073 SDValue(UpperLowerSelect, 0),
1074 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1075 }
1076
1077 if (bits > 0) {
1078 bits = 8 - bits;
1079 Shift =
1080 CurDAG->getTargetNode(SPU::ROTQBIIv2i64, VecVT,
1081 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1082 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1083 }
1084 } else {
1085 SDNode *NegShift =
1086 CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
1087 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1088
1089 Shift =
1090 CurDAG->getTargetNode(SPU::ROTQBYBIv2i64_r32, VecVT,
1091 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1092 Shift =
1093 CurDAG->getTargetNode(SPU::ROTQBIv2i64, VecVT,
1094 SDValue(Shift, 0), SDValue(NegShift, 0));
1095 }
1096
1097 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
1098}
1099
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001100/*!
1101 Do the necessary magic necessary to load a i64 constant
1102 */
1103SDNode *SPUDAGToDAGISel::SelectI64Constant(SDValue& Op, MVT OpVT) {
1104 ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
1105 MVT OpVecVT = MVT::getVectorVT(OpVT, 2);
1106 SDValue i64vec =
1107 SPU::LowerSplat_v2i64(OpVecVT, *CurDAG, CN->getZExtValue());
1108
1109 // Here's where it gets interesting, because we have to parse out the
1110 // subtree handed back in i64vec:
1111
1112 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1113 // The degenerate case where the upper and lower bits in the splat are
1114 // identical:
1115 SDValue Op0 = i64vec.getOperand(0);
1116 ReplaceUses(i64vec, Op0);
1117
1118 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT,
1119 SDValue(emitBuildVector(Op0), 0));
1120 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1121 SDValue lhs = i64vec.getOperand(0);
1122 SDValue rhs = i64vec.getOperand(1);
1123 SDValue shufmask = i64vec.getOperand(2);
1124
1125 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1126 ReplaceUses(lhs, lhs.getOperand(0));
1127 lhs = lhs.getOperand(0);
1128 }
1129
1130 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1131 ? lhs.getNode()
1132 : emitBuildVector(lhs));
1133
1134 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1135 ReplaceUses(rhs, rhs.getOperand(0));
1136 rhs = rhs.getOperand(0);
1137 }
1138
1139 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1140 ? rhs.getNode()
1141 : emitBuildVector(rhs));
1142
1143 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1144 ReplaceUses(shufmask, shufmask.getOperand(0));
1145 shufmask = shufmask.getOperand(0);
1146 }
1147
1148 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1149 ? shufmask.getNode()
1150 : emitBuildVector(shufmask));
1151
1152 SDNode *shufNode =
1153 Select(CurDAG->getNode(SPUISD::SHUFB, OpVecVT,
1154 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1155 SDValue(shufMaskNode, 0)));
1156
1157 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(shufNode, 0));
1158 } else {
1159 cerr << "SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec condition\n";
1160 abort();
1161 }
1162}
1163
Scott Michel02d711b2008-12-30 23:28:25 +00001164/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001165/// SPU-specific DAG, ready for instruction scheduling.
1166///
1167FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1168 return new SPUDAGToDAGISel(TM);
1169}