blob: 08cc8d0d057ca5ca7961417009ee8217e5865476 [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
Scott Michel504c3692007-12-17 22:32:34 +000054 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
55 bool
56 isI32IntU10Immediate(ConstantSDNode *CN)
57 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000058 return isU10Constant(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000059 }
60
Scott Michel266bc8f2007-12-04 22:23:35 +000061 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
62 bool
63 isI16IntS10Immediate(ConstantSDNode *CN)
64 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000065 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000066 }
67
68 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
69 bool
70 isI16IntS10Immediate(SDNode *N)
71 {
Scott Michel9de57a92009-01-26 22:33:37 +000072 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
73 return (CN != 0 && isI16IntS10Immediate(CN));
Scott Michel266bc8f2007-12-04 22:23:35 +000074 }
75
Scott Michelec2a08f2007-12-15 00:38:50 +000076 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
77 bool
78 isI16IntU10Immediate(ConstantSDNode *CN)
79 {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000080 return isU10Constant((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000081 }
82
83 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
84 bool
85 isI16IntU10Immediate(SDNode *N)
86 {
87 return (N->getOpcode() == ISD::Constant
88 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
89 }
90
Scott Michel266bc8f2007-12-04 22:23:35 +000091 //! ConstantSDNode predicate for signed 16-bit values
92 /*!
93 \arg CN The constant SelectionDAG node holding the value
94 \arg Imm The returned 16-bit value, if returning true
95
96 This predicate tests the value in \a CN to see whether it can be
97 represented as a 16-bit, sign-extended quantity. Returns true if
98 this is the case.
99 */
100 bool
101 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
102 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000103 MVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000104 Imm = (short) CN->getZExtValue();
Duncan Sands8e4eb092008-06-08 20:54:56 +0000105 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000106 return true;
107 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000108 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000109 short s_val = (short) i_val;
110 return i_val == s_val;
111 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000112 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000113 short s_val = (short) i_val;
114 return i_val == s_val;
115 }
116
117 return false;
118 }
119
120 //! SDNode predicate for signed 16-bit values.
121 bool
122 isIntS16Immediate(SDNode *N, short &Imm)
123 {
124 return (N->getOpcode() == ISD::Constant
125 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
126 }
127
128 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
129 static bool
130 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
131 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000132 MVT vt = FPN->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000133 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000134 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000135 int sval = (int) ((val << 16) >> 16);
136 Imm = (short) val;
137 return val == sval;
138 }
139
140 return false;
141 }
142
Scott Michel053c1da2008-01-29 02:16:57 +0000143 bool
Scott Michel02d711b2008-12-30 23:28:25 +0000144 isHighLow(const SDValue &Op)
Scott Michel053c1da2008-01-29 02:16:57 +0000145 {
146 return (Op.getOpcode() == SPUISD::IndirectAddr
147 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
148 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
149 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
150 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
151 }
152
Scott Michel266bc8f2007-12-04 22:23:35 +0000153 //===------------------------------------------------------------------===//
Duncan Sands83ec4b62008-06-06 12:08:01 +0000154 //! MVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000155
156 struct valtype_map_s {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000157 MVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000158 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000159 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michelf0569be2008-12-27 04:51:36 +0000160 unsigned lrinst; /// LR instruction
Scott Michel266bc8f2007-12-04 22:23:35 +0000161 };
162
163 const valtype_map_s valtype_map[] = {
Scott Michelf0569be2008-12-27 04:51:36 +0000164 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
165 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
166 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
167 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
168 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
169 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
Scott Michel58c58182008-01-17 20:38:41 +0000170 // vector types... (sigh!)
Scott Michelf0569be2008-12-27 04:51:36 +0000171 { MVT::v16i8, 0, false, SPU::LRv16i8 },
172 { MVT::v8i16, 0, false, SPU::LRv8i16 },
173 { MVT::v4i32, 0, false, SPU::LRv4i32 },
174 { MVT::v2i64, 0, false, SPU::LRv2i64 },
175 { MVT::v4f32, 0, false, SPU::LRv4f32 },
176 { MVT::v2f64, 0, false, SPU::LRv2f64 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000177 };
178
179 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
180
Duncan Sands83ec4b62008-06-06 12:08:01 +0000181 const valtype_map_s *getValueTypeMapEntry(MVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000182 {
183 const valtype_map_s *retval = 0;
184 for (size_t i = 0; i < n_valtype_map; ++i) {
185 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000186 retval = valtype_map + i;
187 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000188 }
189 }
190
191
192#ifndef NDEBUG
193 if (retval == 0) {
194 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000195 << VT.getMVTString()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000196 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000197 abort();
198 }
199#endif
200
201 return retval;
202 }
203}
204
Dan Gohman844731a2008-05-13 00:00:25 +0000205namespace {
206
Scott Michel266bc8f2007-12-04 22:23:35 +0000207//===--------------------------------------------------------------------===//
208/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
209/// instructions for SelectionDAG operations.
210///
211class SPUDAGToDAGISel :
212 public SelectionDAGISel
213{
214 SPUTargetMachine &TM;
215 SPUTargetLowering &SPUtli;
216 unsigned GlobalBaseReg;
217
218public:
Dan Gohman1002c022008-07-07 18:00:37 +0000219 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +0000220 SelectionDAGISel(tm),
Scott Michel266bc8f2007-12-04 22:23:35 +0000221 TM(tm),
222 SPUtli(*tm.getTargetLowering())
Scott Michel9de57a92009-01-26 22:33:37 +0000223 { }
Scott Michel02d711b2008-12-30 23:28:25 +0000224
Scott Michel266bc8f2007-12-04 22:23:35 +0000225 virtual bool runOnFunction(Function &Fn) {
226 // Make sure we re-emit a set of the global base reg if necessary
227 GlobalBaseReg = 0;
228 SelectionDAGISel::runOnFunction(Fn);
229 return true;
230 }
Scott Michel02d711b2008-12-30 23:28:25 +0000231
Scott Michel266bc8f2007-12-04 22:23:35 +0000232 /// getI32Imm - Return a target constant with the specified value, of type
233 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 inline SDValue getI32Imm(uint32_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000235 return CurDAG->getTargetConstant(Imm, MVT::i32);
236 }
237
238 /// getI64Imm - Return a target constant with the specified value, of type
239 /// i64.
Dan Gohman475871a2008-07-27 21:46:04 +0000240 inline SDValue getI64Imm(uint64_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000241 return CurDAG->getTargetConstant(Imm, MVT::i64);
242 }
Scott Michel02d711b2008-12-30 23:28:25 +0000243
Scott Michel266bc8f2007-12-04 22:23:35 +0000244 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman475871a2008-07-27 21:46:04 +0000245 inline SDValue getSmallIPtrImm(unsigned Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000246 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000247 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000248
Scott Michel94bd57e2009-01-15 04:41:47 +0000249 SDNode *emitBuildVector(SDValue build_vec) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000250 MVT vecVT = build_vec.getValueType();
251 SDNode *bvNode = build_vec.getNode();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000252 DebugLoc dl = bvNode->getDebugLoc();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000253
254 // Check to see if this vector can be represented as a CellSPU immediate
Scott Michel9de57a92009-01-26 22:33:37 +0000255 // constant by invoking all of the instruction selection predicates:
256 if (((vecVT == MVT::v8i16) &&
257 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
258 ((vecVT == MVT::v4i32) &&
259 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
260 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
261 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
262 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
263 ((vecVT == MVT::v2i64) &&
264 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
265 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
266 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0))))
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000267 return Select(build_vec);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000268
269 // No, need to emit a constant pool spill:
Scott Michel94bd57e2009-01-15 04:41:47 +0000270 std::vector<Constant*> CV;
271
272 for (size_t i = 0; i < build_vec.getNumOperands(); ++i) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000273 ConstantSDNode *V = dyn_cast<ConstantSDNode > (build_vec.getOperand(i));
274 CV.push_back(const_cast<ConstantInt *> (V->getConstantIntValue()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000275 }
276
277 Constant *CP = ConstantVector::get(CV);
278 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000279 unsigned Alignment = 1 << cast<ConstantPoolSDNode > (CPIdx)->getAlignment();
Scott Michel94bd57e2009-01-15 04:41:47 +0000280 SDValue CGPoolOffset =
281 SPU::LowerConstantPool(CPIdx, *CurDAG,
282 SPUtli.getSPUTargetMachine());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000283 return SelectCode(CurDAG->getLoad(build_vec.getValueType(), dl,
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000284 CurDAG->getEntryNode(), CGPoolOffset,
285 PseudoSourceValue::getConstantPool(), 0,
286 false, Alignment));
Scott Michel94bd57e2009-01-15 04:41:47 +0000287 }
288
Scott Michel266bc8f2007-12-04 22:23:35 +0000289 /// Select - Convert the specified operand from a target-independent to a
290 /// target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDNode *Select(SDValue Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000292
Scott Michel02d711b2008-12-30 23:28:25 +0000293 //! Emit the instruction sequence for i64 shl
294 SDNode *SelectSHLi64(SDValue &Op, MVT OpVT);
295
296 //! Emit the instruction sequence for i64 srl
297 SDNode *SelectSRLi64(SDValue &Op, MVT OpVT);
298
299 //! Emit the instruction sequence for i64 sra
300 SDNode *SelectSRAi64(SDValue &Op, MVT OpVT);
301
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000302 //! Emit the necessary sequence for loading i64 constants:
303 SDNode *SelectI64Constant(SDValue &Op, MVT OpVT);
304
Scott Michel266bc8f2007-12-04 22:23:35 +0000305 //! Returns true if the address N is an A-form (local store) address
Dan Gohman475871a2008-07-27 21:46:04 +0000306 bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
307 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000308
309 //! D-form address predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000310 bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
311 SDValue &Index);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000312
313 /// Alternate D-form address using i7 offset predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000314 bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
315 SDValue &Base);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000316
317 /// D-form address selection workhorse
Dan Gohman475871a2008-07-27 21:46:04 +0000318 bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
319 SDValue &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000320
321 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000322 bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
323 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000324
325 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
326 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000327 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000328 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000329 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +0000330 SDValue Op0, Op1;
Scott Michel266bc8f2007-12-04 22:23:35 +0000331 switch (ConstraintCode) {
332 default: return true;
333 case 'm': // memory
Scott Michel02d711b2008-12-30 23:28:25 +0000334 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000335 && !SelectAFormAddr(Op, Op, Op0, Op1))
336 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000337 break;
338 case 'o': // offsetable
339 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000340 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
341 Op0 = Op;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000342 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000343 }
344 break;
345 case 'v': // not offsetable
346#if 1
347 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
348#else
349 SelectAddrIdxOnly(Op, Op, Op0, Op1);
350#endif
351 break;
352 }
Scott Michel02d711b2008-12-30 23:28:25 +0000353
Scott Michel266bc8f2007-12-04 22:23:35 +0000354 OutOps.push_back(Op0);
355 OutOps.push_back(Op1);
356 return false;
357 }
358
Evan Chengdb8d56b2008-06-30 20:45:06 +0000359 /// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000360 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000361 virtual void InstructionSelect();
Scott Michel266bc8f2007-12-04 22:23:35 +0000362
363 virtual const char *getPassName() const {
364 return "Cell SPU DAG->DAG Pattern Instruction Selection";
Scott Michel02d711b2008-12-30 23:28:25 +0000365 }
366
Scott Michel266bc8f2007-12-04 22:23:35 +0000367 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
368 /// this target when scheduling the DAG.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000369 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohman6448d912008-09-04 15:39:15 +0000370 const TargetInstrInfo *II = TM.getInstrInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +0000371 assert(II && "No InstrInfo?");
Scott Michel02d711b2008-12-30 23:28:25 +0000372 return new SPUHazardRecognizer(*II);
Scott Michel266bc8f2007-12-04 22:23:35 +0000373 }
374
375 // Include the pieces autogenerated from the target description.
376#include "SPUGenDAGISel.inc"
377};
378
Dan Gohman844731a2008-05-13 00:00:25 +0000379}
380
Evan Chengdb8d56b2008-06-30 20:45:06 +0000381/// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000382/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
383void
Dan Gohmanf350b272008-08-23 02:25:05 +0000384SPUDAGToDAGISel::InstructionSelect()
Scott Michel266bc8f2007-12-04 22:23:35 +0000385{
386 DEBUG(BB->dump());
387
388 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000389 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +0000390 CurDAG->RemoveDeadNodes();
Scott Michel266bc8f2007-12-04 22:23:35 +0000391}
392
Scott Michel266bc8f2007-12-04 22:23:35 +0000393/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000394 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000395 \arg N The address to be tested
396 \arg Base The base address
397 \arg Index The base address index
398 */
399bool
Dan Gohman475871a2008-07-27 21:46:04 +0000400SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
401 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000402 // These match the addr256k operand type:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000403 MVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000404 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000405
406 switch (N.getOpcode()) {
407 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000408 case ISD::ConstantPool:
409 case ISD::GlobalAddress:
410 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
411 abort();
412 /*NOTREACHED*/
413
Scott Michel053c1da2008-01-29 02:16:57 +0000414 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000415 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000416 case ISD::TargetJumpTable:
417 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
418 << "A-form address.\n";
419 abort();
420 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000421
Scott Michel02d711b2008-12-30 23:28:25 +0000422 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000423 // Just load from memory if there's only a single use of the location,
424 // otherwise, this will get handled below with D-form offset addresses
425 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000426 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000427 switch (Op0.getOpcode()) {
428 case ISD::TargetConstantPool:
429 case ISD::TargetJumpTable:
430 Base = Op0;
431 Index = Zero;
432 return true;
433
434 case ISD::TargetGlobalAddress: {
435 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
436 GlobalValue *GV = GSDN->getGlobal();
437 if (GV->getAlignment() == 16) {
438 Base = Op0;
439 Index = Zero;
440 return true;
441 }
442 break;
443 }
444 }
445 }
446 break;
447 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000448 return false;
449}
450
Scott Michel02d711b2008-12-30 23:28:25 +0000451bool
Dan Gohman475871a2008-07-27 21:46:04 +0000452SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
453 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000454 const int minDForm2Offset = -(1 << 7);
455 const int maxDForm2Offset = (1 << 7) - 1;
456 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
457 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000458}
459
Scott Michel266bc8f2007-12-04 22:23:35 +0000460/*!
461 \arg Op The ISD instruction (ignored)
462 \arg N The address to be tested
463 \arg Base Base address register/pointer
464 \arg Index Base address index
465
466 Examine the input address by a base register plus a signed 10-bit
467 displacement, [r+I10] (D-form address).
468
469 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000470 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000471*/
472bool
Dan Gohman475871a2008-07-27 21:46:04 +0000473SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
474 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000475 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000476 SPUFrameInfo::minFrameOffset(),
477 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000478}
479
480bool
Dan Gohman475871a2008-07-27 21:46:04 +0000481SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
482 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000483 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000484 unsigned Opc = N.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000485 MVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000486
Scott Michel053c1da2008-01-29 02:16:57 +0000487 if (Opc == ISD::FrameIndex) {
488 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000489 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
490 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000491 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000492 << FI << "\n");
493 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000494 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000495 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000496 return true;
497 }
498 } else if (Opc == ISD::ADD) {
499 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000500 const SDValue Op0 = N.getOperand(0);
501 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000502
Scott Michel053c1da2008-01-29 02:16:57 +0000503 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
504 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
505 Base = CurDAG->getTargetConstant(0, PtrTy);
506 Index = N;
507 return true;
508 } else if (Op1.getOpcode() == ISD::Constant
509 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000510 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000511 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000512
Scott Michel053c1da2008-01-29 02:16:57 +0000513 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000514 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
515 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000516 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000517 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000518
Scott Michel203b2d62008-04-30 00:30:08 +0000519 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000520 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000521 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000522 return true;
523 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000524 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000525 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000526 Index = Op0;
527 return true;
528 }
529 } else if (Op0.getOpcode() == ISD::Constant
530 || Op0.getOpcode() == ISD::TargetConstant) {
531 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000532 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000533
534 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000535 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
536 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000537 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000538 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000539
Scott Michel203b2d62008-04-30 00:30:08 +0000540 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000541 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000542 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000543 return true;
544 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000545 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000546 Base = CurDAG->getTargetConstant(offset, PtrTy);
547 Index = Op1;
548 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000549 }
Scott Michel053c1da2008-01-29 02:16:57 +0000550 }
551 } else if (Opc == SPUISD::IndirectAddr) {
552 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000553 const SDValue Op0 = N.getOperand(0);
554 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000555
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000556 if (Op0.getOpcode() == SPUISD::Hi
557 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000558 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000559 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000560 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000561 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000562 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
563 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000564 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000565
566 if (isa<ConstantSDNode>(Op1)) {
567 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000568 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000569 idxOp = Op0;
570 } else if (isa<ConstantSDNode>(Op0)) {
571 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000572 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000573 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000574 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000575
576 if (offset >= minOffset && offset <= maxOffset) {
577 Base = CurDAG->getTargetConstant(offset, PtrTy);
578 Index = idxOp;
579 return true;
580 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000581 }
Scott Michel053c1da2008-01-29 02:16:57 +0000582 } else if (Opc == SPUISD::AFormAddr) {
583 Base = CurDAG->getTargetConstant(0, N.getValueType());
584 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000585 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000586 } else if (Opc == SPUISD::LDRESULT) {
587 Base = CurDAG->getTargetConstant(0, N.getValueType());
588 Index = N;
589 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000590 } else if (Opc == ISD::Register || Opc == ISD::CopyFromReg) {
591 unsigned OpOpc = Op.getOpcode();
592
593 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
594 // Direct load/store without getelementptr
595 SDValue Addr, Offs;
596
597 // Get the register from CopyFromReg
598 if (Opc == ISD::CopyFromReg)
599 Addr = N.getOperand(1);
600 else
601 Addr = N; // Register
602
Scott Michelaedc6372008-12-10 00:15:19 +0000603 Offs = ((OpOpc == ISD::STORE) ? Op.getOperand(3) : Op.getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000604
605 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
606 if (Offs.getOpcode() == ISD::UNDEF)
607 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
608
609 Base = Offs;
610 Index = Addr;
611 return true;
612 }
Scott Michelaedc6372008-12-10 00:15:19 +0000613 } else {
614 /* If otherwise unadorned, default to D-form address with 0 offset: */
615 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000616 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000617 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000618 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000619 }
620
621 Base = CurDAG->getTargetConstant(0, Index.getValueType());
622 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000623 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000624 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000625
Scott Michel266bc8f2007-12-04 22:23:35 +0000626 return false;
627}
628
629/*!
630 \arg Op The ISD instruction operand
631 \arg N The address operand
632 \arg Base The base pointer operand
633 \arg Index The offset/index operand
634
Scott Michel9c0c6b22008-11-21 02:56:16 +0000635 If the address \a N can be expressed as an A-form or D-form address, returns
636 false. Otherwise, creates two operands, Base and Index that will become the
637 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000638*/
639bool
Dan Gohman475871a2008-07-27 21:46:04 +0000640SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
641 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000642 if (!SelectAFormAddr(Op, N, Base, Index)
643 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000644 // If the address is neither A-form or D-form, punt and use an X-form
645 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000646 Base = N.getOperand(1);
647 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000648 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000649 }
650
651 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000652}
653
Scott Michel266bc8f2007-12-04 22:23:35 +0000654//! Convert the operand from a target-independent to a target-specific node
655/*!
656 */
657SDNode *
Dan Gohman475871a2008-07-27 21:46:04 +0000658SPUDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000659 SDNode *N = Op.getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000660 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000661 int n_ops = -1;
662 unsigned NewOpc;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000663 MVT OpVT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000664 SDValue Ops[8];
Scott Michel266bc8f2007-12-04 22:23:35 +0000665
Dan Gohmane8be6c62008-07-17 19:10:17 +0000666 if (N->isMachineOpcode()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000667 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000668 }
669
670 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000671 int FI = cast<FrameIndexSDNode>(N)->getIndex();
672 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
673 SDValue Imm0 = CurDAG->getTargetConstant(0, Op.getValueType());
Scott Michel266bc8f2007-12-04 22:23:35 +0000674
Scott Michel02d711b2008-12-30 23:28:25 +0000675 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000676 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000677 Ops[0] = TFI;
678 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000679 n_ops = 2;
680 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000681 NewOpc = SPU::Ar32;
Scott Michel02d711b2008-12-30 23:28:25 +0000682 Ops[0] = CurDAG->getRegister(SPU::R1, Op.getValueType());
683 Ops[1] = SDValue(CurDAG->getTargetNode(SPU::ILAr32, Op.getValueType(),
684 TFI, Imm0), 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000685 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000686 }
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000687 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
688 // Catch the i64 constants that end up here. Note: The backend doesn't
689 // attempt to legalize the constant (it's useless because DAGCombiner
690 // will insert 64-bit constants and we can't stop it).
691 return SelectI64Constant(Op, OpVT);
Scott Michel94bd57e2009-01-15 04:41:47 +0000692 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
693 && OpVT == MVT::i64) {
694 SDValue Op0 = Op.getOperand(0);
695 MVT Op0VT = Op0.getValueType();
696 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
697 MVT OpVecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
698 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000699
Scott Michel94bd57e2009-01-15 04:41:47 +0000700 switch (Op0VT.getSimpleVT()) {
701 default:
702 cerr << "CellSPU Select: Unhandled zero/any extend MVT\n";
703 abort();
704 /*NOTREACHED*/
705 break;
706 case MVT::i32:
707 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000708 CurDAG->getConstant(0x80808080, MVT::i32),
709 CurDAG->getConstant(0x00010203, MVT::i32),
710 CurDAG->getConstant(0x80808080, MVT::i32),
711 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000712 break;
713
714 case MVT::i16:
715 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000716 CurDAG->getConstant(0x80808080, MVT::i32),
717 CurDAG->getConstant(0x80800203, MVT::i32),
718 CurDAG->getConstant(0x80808080, MVT::i32),
719 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000720 break;
721
722 case MVT::i8:
723 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000724 CurDAG->getConstant(0x80808080, MVT::i32),
725 CurDAG->getConstant(0x80808003, MVT::i32),
726 CurDAG->getConstant(0x80808080, MVT::i32),
727 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000728 break;
Scott Michel58c58182008-01-17 20:38:41 +0000729 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000730
731 SDNode *shufMaskLoad = emitBuildVector(shufMask);
732 SDNode *PromoteScalar =
733 SelectCode(CurDAG->getNode(SPUISD::PREFSLOT2VEC, Op0VecVT, Op0));
734
735 SDValue zextShuffle =
736 CurDAG->getNode(SPUISD::SHUFB, OpVecVT,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000737 SDValue(PromoteScalar, 0),
738 SDValue(PromoteScalar, 0),
739 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000740
741 // N.B.: BIT_CONVERT replaces and updates the zextShuffle node, so we
742 // re-use it in the VEC2PREFSLOT selection without needing to explicitly
743 // call SelectCode (it's already done for us.)
744 SelectCode(CurDAG->getNode(ISD::BIT_CONVERT, OpVecVT, zextShuffle));
745 return SelectCode(CurDAG->getNode(SPUISD::VEC2PREFSLOT, OpVT,
746 zextShuffle));
747 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
748 SDNode *CGLoad =
749 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG));
750
751 return SelectCode(CurDAG->getNode(SPUISD::ADD64_MARKER, OpVT,
752 Op.getOperand(0), Op.getOperand(1),
753 SDValue(CGLoad, 0)));
754 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
755 SDNode *CGLoad =
756 emitBuildVector(SPU::getBorrowGenerateShufMask(*CurDAG));
757
758 return SelectCode(CurDAG->getNode(SPUISD::SUB64_MARKER, OpVT,
759 Op.getOperand(0), Op.getOperand(1),
760 SDValue(CGLoad, 0)));
761 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
762 SDNode *CGLoad =
763 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG));
764
765 return SelectCode(CurDAG->getNode(SPUISD::MUL64_MARKER, OpVT,
766 Op.getOperand(0), Op.getOperand(1),
767 SDValue(CGLoad, 0)));
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000768 } else if (Opc == ISD::TRUNCATE) {
769 SDValue Op0 = Op.getOperand(0);
770 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
771 && OpVT == MVT::i32
772 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000773 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
774 //
775 // Take advantage of the fact that the upper 32 bits are in the
776 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000777 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
778 if (CN != 0) {
779 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000780
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000781 if (shift_amt >= 32) {
782 SDNode *hi32 =
783 CurDAG->getTargetNode(SPU::ORr32_r64, OpVT, Op0.getOperand(0));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000784
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000785 shift_amt -= 32;
786 if (shift_amt > 0) {
787 // Take care of the additional shift, if present:
788 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
789 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000790
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000791 if (Op0.getOpcode() == ISD::SRL)
792 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000793
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000794 hi32 = CurDAG->getTargetNode(Opc, OpVT, SDValue(hi32, 0), shift);
795 }
796
797 return hi32;
798 }
799 }
800 }
Scott Michel02d711b2008-12-30 23:28:25 +0000801 } else if (Opc == ISD::SHL) {
802 if (OpVT == MVT::i64) {
803 return SelectSHLi64(Op, OpVT);
804 }
805 } else if (Opc == ISD::SRL) {
806 if (OpVT == MVT::i64) {
807 return SelectSRLi64(Op, OpVT);
808 }
809 } else if (Opc == ISD::SRA) {
810 if (OpVT == MVT::i64) {
811 return SelectSRAi64(Op, OpVT);
812 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000813 } else if (Opc == SPUISD::LDRESULT) {
814 // Custom select instructions for LDRESULT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000815 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue Arg = N->getOperand(0);
817 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000818 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000819 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
820
821 if (vtm->ldresult_ins == 0) {
822 cerr << "LDRESULT for unsupported type: "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000823 << VT.getMVTString()
Scott Michela59d4692008-02-23 18:41:37 +0000824 << "\n";
825 abort();
826 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000827
Scott Michela59d4692008-02-23 18:41:37 +0000828 Opc = vtm->ldresult_ins;
829 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000830 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000831
Scott Michel58c58182008-01-17 20:38:41 +0000832 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000833 } else {
Scott Michel30ee7df2008-12-04 03:02:42 +0000834 Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000835 }
836
Scott Michel266bc8f2007-12-04 22:23:35 +0000837 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000838 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000839 // Look at the operands: SelectCode() will catch the cases that aren't
840 // specifically handled here.
841 //
842 // SPUInstrInfo catches the following patterns:
843 // (SPUindirect (SPUhi ...), (SPUlo ...))
844 // (SPUindirect $sp, imm)
845 MVT VT = Op.getValueType();
846 SDValue Op0 = N->getOperand(0);
847 SDValue Op1 = N->getOperand(1);
848 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000849
Scott Michelf0569be2008-12-27 04:51:36 +0000850 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
851 || (Op0.getOpcode() == ISD::Register
852 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
853 && RN->getReg() != SPU::R1))) {
854 NewOpc = SPU::Ar32;
Scott Michel58c58182008-01-17 20:38:41 +0000855 if (Op1.getOpcode() == ISD::Constant) {
856 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000857 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000858 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
Scott Michel58c58182008-01-17 20:38:41 +0000859 }
Scott Michelf0569be2008-12-27 04:51:36 +0000860 Ops[0] = Op0;
861 Ops[1] = Op1;
862 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000863 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000864 }
Scott Michel02d711b2008-12-30 23:28:25 +0000865
Scott Michel58c58182008-01-17 20:38:41 +0000866 if (n_ops > 0) {
867 if (N->hasOneUse())
868 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
869 else
870 return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
871 } else
872 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000873}
874
Scott Michel02d711b2008-12-30 23:28:25 +0000875/*!
876 * Emit the instruction sequence for i64 left shifts. The basic algorithm
877 * is to fill the bottom two word slots with zeros so that zeros are shifted
878 * in as the entire quadword is shifted left.
879 *
880 * \note This code could also be used to implement v2i64 shl.
881 *
882 * @param Op The shl operand
883 * @param OpVT Op's machine value value type (doesn't need to be passed, but
884 * makes life easier.)
885 * @return The SDNode with the entire instruction sequence
886 */
887SDNode *
888SPUDAGToDAGISel::SelectSHLi64(SDValue &Op, MVT OpVT) {
889 SDValue Op0 = Op.getOperand(0);
890 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
891 SDValue ShiftAmt = Op.getOperand(1);
892 MVT ShiftAmtVT = ShiftAmt.getValueType();
893 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
894 SDValue SelMaskVal;
895
896 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op0);
897 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
898 SelMask = CurDAG->getTargetNode(SPU::FSMBIv2i64, VecVT, SelMaskVal);
899 ZeroFill = CurDAG->getTargetNode(SPU::ILv2i64, VecVT,
900 CurDAG->getTargetConstant(0, OpVT));
901 VecOp0 = CurDAG->getTargetNode(SPU::SELBv2i64, VecVT,
902 SDValue(ZeroFill, 0),
903 SDValue(VecOp0, 0),
904 SDValue(SelMask, 0));
905
906 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
907 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
908 unsigned bits = unsigned(CN->getZExtValue()) & 7;
909
910 if (bytes > 0) {
911 Shift =
912 CurDAG->getTargetNode(SPU::SHLQBYIv2i64, VecVT,
913 SDValue(VecOp0, 0),
914 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
915 }
916
917 if (bits > 0) {
918 Shift =
919 CurDAG->getTargetNode(SPU::SHLQBIIv2i64, VecVT,
920 SDValue((Shift != 0 ? Shift : VecOp0), 0),
921 CurDAG->getTargetConstant(bits, ShiftAmtVT));
922 }
923 } else {
924 SDNode *Bytes =
925 CurDAG->getTargetNode(SPU::ROTMIr32, ShiftAmtVT,
926 ShiftAmt,
927 CurDAG->getTargetConstant(3, ShiftAmtVT));
928 SDNode *Bits =
929 CurDAG->getTargetNode(SPU::ANDIr32, ShiftAmtVT,
930 ShiftAmt,
931 CurDAG->getTargetConstant(7, ShiftAmtVT));
932 Shift =
933 CurDAG->getTargetNode(SPU::SHLQBYv2i64, VecVT,
934 SDValue(VecOp0, 0), SDValue(Bytes, 0));
935 Shift =
936 CurDAG->getTargetNode(SPU::SHLQBIv2i64, VecVT,
937 SDValue(Shift, 0), SDValue(Bits, 0));
938 }
939
940 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
941}
942
943/*!
944 * Emit the instruction sequence for i64 logical right shifts.
945 *
946 * @param Op The shl operand
947 * @param OpVT Op's machine value value type (doesn't need to be passed, but
948 * makes life easier.)
949 * @return The SDNode with the entire instruction sequence
950 */
951SDNode *
952SPUDAGToDAGISel::SelectSRLi64(SDValue &Op, MVT OpVT) {
953 SDValue Op0 = Op.getOperand(0);
954 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
955 SDValue ShiftAmt = Op.getOperand(1);
956 MVT ShiftAmtVT = ShiftAmt.getValueType();
957 SDNode *VecOp0, *Shift = 0;
958
959 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op0);
960
961 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
962 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
963 unsigned bits = unsigned(CN->getZExtValue()) & 7;
964
965 if (bytes > 0) {
966 Shift =
967 CurDAG->getTargetNode(SPU::ROTQMBYIv2i64, VecVT,
968 SDValue(VecOp0, 0),
969 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
970 }
971
972 if (bits > 0) {
973 Shift =
974 CurDAG->getTargetNode(SPU::ROTQMBIIv2i64, VecVT,
975 SDValue((Shift != 0 ? Shift : VecOp0), 0),
976 CurDAG->getTargetConstant(bits, ShiftAmtVT));
977 }
978 } else {
979 SDNode *Bytes =
980 CurDAG->getTargetNode(SPU::ROTMIr32, ShiftAmtVT,
981 ShiftAmt,
982 CurDAG->getTargetConstant(3, ShiftAmtVT));
983 SDNode *Bits =
984 CurDAG->getTargetNode(SPU::ANDIr32, ShiftAmtVT,
985 ShiftAmt,
986 CurDAG->getTargetConstant(7, ShiftAmtVT));
987
988 // Ensure that the shift amounts are negated!
989 Bytes = CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
990 SDValue(Bytes, 0),
991 CurDAG->getTargetConstant(0, ShiftAmtVT));
992
993 Bits = CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
994 SDValue(Bits, 0),
995 CurDAG->getTargetConstant(0, ShiftAmtVT));
996
997 Shift =
998 CurDAG->getTargetNode(SPU::ROTQMBYv2i64, VecVT,
999 SDValue(VecOp0, 0), SDValue(Bytes, 0));
1000 Shift =
1001 CurDAG->getTargetNode(SPU::ROTQMBIv2i64, VecVT,
1002 SDValue(Shift, 0), SDValue(Bits, 0));
1003 }
1004
1005 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
1006}
1007
1008/*!
1009 * Emit the instruction sequence for i64 arithmetic right shifts.
1010 *
1011 * @param Op The shl operand
1012 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1013 * makes life easier.)
1014 * @return The SDNode with the entire instruction sequence
1015 */
1016SDNode *
1017SPUDAGToDAGISel::SelectSRAi64(SDValue &Op, MVT OpVT) {
1018 // Promote Op0 to vector
1019 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
1020 SDValue ShiftAmt = Op.getOperand(1);
1021 MVT ShiftAmtVT = ShiftAmt.getValueType();
1022
1023 SDNode *VecOp0 =
1024 CurDAG->getTargetNode(SPU::ORv2i64_i64, VecVT, Op.getOperand(0));
1025
1026 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1027 SDNode *SignRot =
1028 CurDAG->getTargetNode(SPU::ROTMAIv2i64_i32, MVT::v2i64,
1029 SDValue(VecOp0, 0), SignRotAmt);
1030 SDNode *UpperHalfSign =
1031 CurDAG->getTargetNode(SPU::ORi32_v4i32, MVT::i32, SDValue(SignRot, 0));
1032
1033 SDNode *UpperHalfSignMask =
1034 CurDAG->getTargetNode(SPU::FSM64r32, VecVT, SDValue(UpperHalfSign, 0));
1035 SDNode *UpperLowerMask =
1036 CurDAG->getTargetNode(SPU::FSMBIv2i64, VecVT,
1037 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1038 SDNode *UpperLowerSelect =
1039 CurDAG->getTargetNode(SPU::SELBv2i64, VecVT,
1040 SDValue(UpperHalfSignMask, 0),
1041 SDValue(VecOp0, 0),
1042 SDValue(UpperLowerMask, 0));
1043
1044 SDNode *Shift = 0;
1045
1046 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1047 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1048 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1049
1050 if (bytes > 0) {
1051 bytes = 31 - bytes;
1052 Shift =
1053 CurDAG->getTargetNode(SPU::ROTQBYIv2i64, VecVT,
1054 SDValue(UpperLowerSelect, 0),
1055 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1056 }
1057
1058 if (bits > 0) {
1059 bits = 8 - bits;
1060 Shift =
1061 CurDAG->getTargetNode(SPU::ROTQBIIv2i64, VecVT,
1062 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1063 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1064 }
1065 } else {
1066 SDNode *NegShift =
1067 CurDAG->getTargetNode(SPU::SFIr32, ShiftAmtVT,
1068 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1069
1070 Shift =
1071 CurDAG->getTargetNode(SPU::ROTQBYBIv2i64_r32, VecVT,
1072 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1073 Shift =
1074 CurDAG->getTargetNode(SPU::ROTQBIv2i64, VecVT,
1075 SDValue(Shift, 0), SDValue(NegShift, 0));
1076 }
1077
1078 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(Shift, 0));
1079}
1080
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001081/*!
1082 Do the necessary magic necessary to load a i64 constant
1083 */
1084SDNode *SPUDAGToDAGISel::SelectI64Constant(SDValue& Op, MVT OpVT) {
1085 ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
1086 MVT OpVecVT = MVT::getVectorVT(OpVT, 2);
1087 SDValue i64vec =
1088 SPU::LowerSplat_v2i64(OpVecVT, *CurDAG, CN->getZExtValue());
1089
1090 // Here's where it gets interesting, because we have to parse out the
1091 // subtree handed back in i64vec:
1092
1093 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1094 // The degenerate case where the upper and lower bits in the splat are
1095 // identical:
1096 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001097
Scott Michel9de57a92009-01-26 22:33:37 +00001098 ReplaceUses(i64vec, Op0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001099 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT,
1100 SDValue(emitBuildVector(Op0), 0));
1101 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1102 SDValue lhs = i64vec.getOperand(0);
1103 SDValue rhs = i64vec.getOperand(1);
1104 SDValue shufmask = i64vec.getOperand(2);
1105
1106 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1107 ReplaceUses(lhs, lhs.getOperand(0));
1108 lhs = lhs.getOperand(0);
1109 }
1110
1111 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1112 ? lhs.getNode()
1113 : emitBuildVector(lhs));
1114
1115 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1116 ReplaceUses(rhs, rhs.getOperand(0));
1117 rhs = rhs.getOperand(0);
1118 }
1119
1120 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1121 ? rhs.getNode()
1122 : emitBuildVector(rhs));
Scott Michel9de57a92009-01-26 22:33:37 +00001123
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001124 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1125 ReplaceUses(shufmask, shufmask.getOperand(0));
1126 shufmask = shufmask.getOperand(0);
1127 }
1128
1129 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1130 ? shufmask.getNode()
1131 : emitBuildVector(shufmask));
1132
1133 SDNode *shufNode =
1134 Select(CurDAG->getNode(SPUISD::SHUFB, OpVecVT,
1135 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1136 SDValue(shufMaskNode, 0)));
1137
1138 return CurDAG->getTargetNode(SPU::ORi64_v2i64, OpVT, SDValue(shufNode, 0));
1139 } else {
1140 cerr << "SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec condition\n";
1141 abort();
1142 }
1143}
1144
Scott Michel02d711b2008-12-30 23:28:25 +00001145/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001146/// SPU-specific DAG, ready for instruction scheduling.
1147///
1148FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1149 return new SPUDAGToDAGISel(TM);
1150}