blob: 25f6e234a2f3f6b686e97ef05928332a6ffb4a52 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
Chris Lattner6c18b102005-12-17 07:47:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner7c90f732006-02-05 05:50:24 +000010// This file defines an instruction selector for the SPARC target.
Chris Lattner6c18b102005-12-17 07:47:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7c90f732006-02-05 05:50:24 +000014#include "Sparc.h"
15#include "SparcTargetMachine.h"
Chris Lattner384e5ef2005-12-18 13:33:06 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera01b7572005-12-17 08:03:24 +000017#include "llvm/Function.h"
Chris Lattner420736d2006-03-25 06:47:10 +000018#include "llvm/Intrinsics.h"
Chris Lattner8fa54dc2005-12-18 06:59:57 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnera01b7572005-12-17 08:03:24 +000020#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner33084492005-12-18 08:13:54 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattnera01b7572005-12-17 08:03:24 +000024#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000025#include "llvm/Target/TargetLowering.h"
26#include "llvm/Support/Debug.h"
27#include <iostream>
Evan Cheng900c8262006-02-05 06:51:51 +000028#include <set>
Chris Lattner6c18b102005-12-17 07:47:01 +000029using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// TargetLowering Implementation
33//===----------------------------------------------------------------------===//
34
Chris Lattner7c90f732006-02-05 05:50:24 +000035namespace SPISD {
Chris Lattner4d55aca2005-12-18 01:20:35 +000036 enum {
Chris Lattner7c90f732006-02-05 05:50:24 +000037 FIRST_NUMBER = ISD::BUILTIN_OP_END+SP::INSTRUCTION_LIST_END,
Chris Lattner9072c052006-01-30 06:14:02 +000038 CMPICC, // Compare two GPR operands, set icc.
39 CMPFCC, // Compare two FP operands, set fcc.
40 BRICC, // Branch to dest on icc condition
41 BRFCC, // Branch to dest on fcc condition
42 SELECT_ICC, // Select between two values using the current ICC flags.
43 SELECT_FCC, // Select between two values using the current FCC flags.
Chris Lattnere3572462005-12-18 02:10:39 +000044
Chris Lattner9072c052006-01-30 06:14:02 +000045 Hi, Lo, // Hi/Lo operations, typically on a global address.
Chris Lattner8fa54dc2005-12-18 06:59:57 +000046
Chris Lattner9072c052006-01-30 06:14:02 +000047 FTOI, // FP to Int within a FP register.
48 ITOF, // Int to FP within a FP register.
49
Chris Lattner7c90f732006-02-05 05:50:24 +000050 CALL, // A call instruction.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000051 RET_FLAG // Return with a flag operand.
Chris Lattner4d55aca2005-12-18 01:20:35 +000052 };
53}
54
Chris Lattner3772bcb2006-01-30 07:43:04 +000055/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
56/// condition.
Chris Lattner7c90f732006-02-05 05:50:24 +000057static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
Chris Lattner3772bcb2006-01-30 07:43:04 +000058 switch (CC) {
59 default: assert(0 && "Unknown integer condition code!");
Chris Lattner7c90f732006-02-05 05:50:24 +000060 case ISD::SETEQ: return SPCC::ICC_E;
61 case ISD::SETNE: return SPCC::ICC_NE;
62 case ISD::SETLT: return SPCC::ICC_L;
63 case ISD::SETGT: return SPCC::ICC_G;
64 case ISD::SETLE: return SPCC::ICC_LE;
65 case ISD::SETGE: return SPCC::ICC_GE;
66 case ISD::SETULT: return SPCC::ICC_CS;
67 case ISD::SETULE: return SPCC::ICC_LEU;
68 case ISD::SETUGT: return SPCC::ICC_GU;
69 case ISD::SETUGE: return SPCC::ICC_CC;
Chris Lattner3772bcb2006-01-30 07:43:04 +000070 }
71}
72
73/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
74/// FCC condition.
Chris Lattner7c90f732006-02-05 05:50:24 +000075static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
Chris Lattner3772bcb2006-01-30 07:43:04 +000076 switch (CC) {
77 default: assert(0 && "Unknown fp condition code!");
Chris Lattner7c90f732006-02-05 05:50:24 +000078 case ISD::SETEQ: return SPCC::FCC_E;
79 case ISD::SETNE: return SPCC::FCC_NE;
80 case ISD::SETLT: return SPCC::FCC_L;
81 case ISD::SETGT: return SPCC::FCC_G;
82 case ISD::SETLE: return SPCC::FCC_LE;
83 case ISD::SETGE: return SPCC::FCC_GE;
84 case ISD::SETULT: return SPCC::FCC_UL;
85 case ISD::SETULE: return SPCC::FCC_ULE;
86 case ISD::SETUGT: return SPCC::FCC_UG;
87 case ISD::SETUGE: return SPCC::FCC_UGE;
88 case ISD::SETUO: return SPCC::FCC_U;
89 case ISD::SETO: return SPCC::FCC_O;
90 case ISD::SETONE: return SPCC::FCC_LG;
91 case ISD::SETUEQ: return SPCC::FCC_UE;
Chris Lattner3772bcb2006-01-30 07:43:04 +000092 }
93}
Chris Lattner3772bcb2006-01-30 07:43:04 +000094
Chris Lattner6c18b102005-12-17 07:47:01 +000095namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +000096 class SparcTargetLowering : public TargetLowering {
Chris Lattner2db3ff62005-12-18 15:55:15 +000097 int VarArgsFrameOffset; // Frame offset to start of varargs area.
Chris Lattner6c18b102005-12-17 07:47:01 +000098 public:
Chris Lattner7c90f732006-02-05 05:50:24 +000099 SparcTargetLowering(TargetMachine &TM);
Chris Lattner4d55aca2005-12-18 01:20:35 +0000100 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Chris Lattner4a397e02006-01-30 03:51:45 +0000101
Nate Begeman368e18d2006-02-16 21:11:51 +0000102 /// computeMaskedBitsForTargetNode - Determine which of the bits specified
103 /// in Mask are known to be either zero or one and return them in the
104 /// KnownZero/KnownOne bitsets.
105 virtual void computeMaskedBitsForTargetNode(const SDOperand Op,
106 uint64_t Mask,
107 uint64_t &KnownZero,
108 uint64_t &KnownOne,
109 unsigned Depth = 0) const;
Chris Lattner4a397e02006-01-30 03:51:45 +0000110
Chris Lattner6c18b102005-12-17 07:47:01 +0000111 virtual std::vector<SDOperand>
112 LowerArguments(Function &F, SelectionDAG &DAG);
113 virtual std::pair<SDOperand, SDOperand>
114 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
115 unsigned CC,
116 bool isTailCall, SDOperand Callee, ArgListTy &Args,
117 SelectionDAG &DAG);
Chris Lattner33084492005-12-18 08:13:54 +0000118 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
119 MachineBasicBlock *MBB);
Chris Lattner72878a42006-01-12 07:31:15 +0000120
121 virtual const char *getTargetNodeName(unsigned Opcode) const;
Chris Lattner6c18b102005-12-17 07:47:01 +0000122 };
123}
124
Chris Lattner7c90f732006-02-05 05:50:24 +0000125SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner6c18b102005-12-17 07:47:01 +0000126 : TargetLowering(TM) {
127
128 // Set up the register classes.
Chris Lattner7c90f732006-02-05 05:50:24 +0000129 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
130 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
131 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattner9a60ff62005-12-17 20:50:42 +0000132
Chris Lattnere3572462005-12-18 02:10:39 +0000133 // Custom legalize GlobalAddress nodes into LO/HI parts.
134 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Chris Lattner76acc872005-12-18 02:37:35 +0000135 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Chris Lattnere3572462005-12-18 02:10:39 +0000136
Chris Lattner9a60ff62005-12-17 20:50:42 +0000137 // Sparc doesn't have sext_inreg, replace them with shl/sra
Chris Lattner33084492005-12-18 08:13:54 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner7087e572005-12-17 22:39:19 +0000141
142 // Sparc has no REM operation.
143 setOperationAction(ISD::UREM, MVT::i32, Expand);
144 setOperationAction(ISD::SREM, MVT::i32, Expand);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000145
146 // Custom expand fp<->sint
147 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
148 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
149
150 // Expand fp<->uint
151 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
152 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Chris Lattner6c18b102005-12-17 07:47:01 +0000153
Chris Lattner53e88452005-12-23 05:13:35 +0000154 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
155 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
156
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000157 // Turn FP extload into load/fextend
Chris Lattner065c8962005-12-18 07:13:32 +0000158 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
159
Chris Lattner4d55aca2005-12-18 01:20:35 +0000160 // Sparc has no select or setcc: expand to SELECT_CC.
161 setOperationAction(ISD::SELECT, MVT::i32, Expand);
162 setOperationAction(ISD::SELECT, MVT::f32, Expand);
163 setOperationAction(ISD::SELECT, MVT::f64, Expand);
164 setOperationAction(ISD::SETCC, MVT::i32, Expand);
165 setOperationAction(ISD::SETCC, MVT::f32, Expand);
166 setOperationAction(ISD::SETCC, MVT::f64, Expand);
167
168 // Sparc doesn't have BRCOND either, it has BR_CC.
169 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Nate Begeman37efe672006-04-22 18:53:45 +0000170 setOperationAction(ISD::BRIND, MVT::i32, Expand);
Chris Lattner4d55aca2005-12-18 01:20:35 +0000171 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
172 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
173 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
174
Chris Lattner33084492005-12-18 08:13:54 +0000175 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
176 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
177 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
178
Chris Lattner7c90f732006-02-05 05:50:24 +0000179 // SPARC has no intrinsics for these particular operations.
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000180 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
181 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
182 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
183
Chris Lattner61772c22005-12-19 01:39:40 +0000184 setOperationAction(ISD::FSIN , MVT::f64, Expand);
185 setOperationAction(ISD::FCOS , MVT::f64, Expand);
186 setOperationAction(ISD::FSIN , MVT::f32, Expand);
187 setOperationAction(ISD::FCOS , MVT::f32, Expand);
188 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
189 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
190 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000191 setOperationAction(ISD::ROTL , MVT::i32, Expand);
192 setOperationAction(ISD::ROTR , MVT::i32, Expand);
Nate Begemand88fc032006-01-14 03:14:10 +0000193 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Chris Lattner9601a862006-03-05 05:08:37 +0000194 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
195 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000196
197 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
198 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
199 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000200
201 // We don't have line number support yet.
202 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000203 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
204 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000205
Nate Begemanee625572006-01-27 21:09:22 +0000206 // RET must be custom lowered, to meet ABI requirements
207 setOperationAction(ISD::RET , MVT::Other, Custom);
208
Chris Lattnerc275dfa2006-02-04 08:31:30 +0000209 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Nate Begemanacc398c2006-01-25 18:21:52 +0000210 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerc275dfa2006-02-04 08:31:30 +0000211 // VAARG needs to be lowered to not do unaligned accesses for doubles.
212 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000213
214 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000215 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
216 setOperationAction(ISD::VAEND , MVT::Other, Expand);
217 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
218 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
Chris Lattner6fa1f572006-02-15 06:41:34 +0000219 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner934ea492006-01-15 08:55:25 +0000220
Chris Lattner2adc05c2006-01-30 22:20:49 +0000221 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
222 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
223
Chris Lattner7c90f732006-02-05 05:50:24 +0000224 setStackPointerRegisterToSaveRestore(SP::O6);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000225
Chris Lattner7c90f732006-02-05 05:50:24 +0000226 if (TM.getSubtarget<SparcSubtarget>().isV9()) {
Chris Lattner9072c052006-01-30 06:14:02 +0000227 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
228 }
229
Chris Lattner6c18b102005-12-17 07:47:01 +0000230 computeRegisterProperties();
231}
232
Chris Lattner7c90f732006-02-05 05:50:24 +0000233const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Chris Lattner72878a42006-01-12 07:31:15 +0000234 switch (Opcode) {
Chris Lattner138d3222006-01-12 07:38:04 +0000235 default: return 0;
Chris Lattner7c90f732006-02-05 05:50:24 +0000236 case SPISD::CMPICC: return "SPISD::CMPICC";
237 case SPISD::CMPFCC: return "SPISD::CMPFCC";
238 case SPISD::BRICC: return "SPISD::BRICC";
239 case SPISD::BRFCC: return "SPISD::BRFCC";
240 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
241 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
242 case SPISD::Hi: return "SPISD::Hi";
243 case SPISD::Lo: return "SPISD::Lo";
244 case SPISD::FTOI: return "SPISD::FTOI";
245 case SPISD::ITOF: return "SPISD::ITOF";
246 case SPISD::CALL: return "SPISD::CALL";
247 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Chris Lattner72878a42006-01-12 07:31:15 +0000248 }
249}
250
Chris Lattner4a397e02006-01-30 03:51:45 +0000251/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
252/// be zero. Op is expected to be a target specific node. Used by DAG
253/// combiner.
Nate Begeman368e18d2006-02-16 21:11:51 +0000254void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
255 uint64_t Mask,
256 uint64_t &KnownZero,
257 uint64_t &KnownOne,
258 unsigned Depth) const {
259 uint64_t KnownZero2, KnownOne2;
260 KnownZero = KnownOne = 0; // Don't know anything.
261
Chris Lattner4a397e02006-01-30 03:51:45 +0000262 switch (Op.getOpcode()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000263 default: break;
Chris Lattner7c90f732006-02-05 05:50:24 +0000264 case SPISD::SELECT_ICC:
265 case SPISD::SELECT_FCC:
Nate Begeman368e18d2006-02-16 21:11:51 +0000266 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
267 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
268 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
269 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
270
271 // Only known if known in both the LHS and RHS.
272 KnownOne &= KnownOne2;
273 KnownZero &= KnownZero2;
274 break;
Chris Lattner4a397e02006-01-30 03:51:45 +0000275 }
276}
277
Chris Lattner384e5ef2005-12-18 13:33:06 +0000278/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
279/// either one or two GPRs, including FP values. TODO: we should pass FP values
280/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000281std::vector<SDOperand>
Chris Lattner7c90f732006-02-05 05:50:24 +0000282SparcTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000283 MachineFunction &MF = DAG.getMachineFunction();
284 SSARegMap *RegMap = MF.getSSARegMap();
285 std::vector<SDOperand> ArgValues;
286
Chris Lattner384e5ef2005-12-18 13:33:06 +0000287 static const unsigned ArgRegs[] = {
Chris Lattner7c90f732006-02-05 05:50:24 +0000288 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
Chris Lattnera01b7572005-12-17 08:03:24 +0000289 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000290
291 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
292 unsigned ArgOffset = 68;
293
294 SDOperand Root = DAG.getRoot();
295 std::vector<SDOperand> OutChains;
296
Chris Lattnera01b7572005-12-17 08:03:24 +0000297 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
298 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000299
300 switch (ObjectVT) {
301 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000302 case MVT::i1:
303 case MVT::i8:
304 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000305 case MVT::i32:
306 if (I->use_empty()) { // Argument is dead.
307 if (CurArgReg < ArgRegEnd) ++CurArgReg;
308 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
309 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner7c90f732006-02-05 05:50:24 +0000310 unsigned VReg = RegMap->createVirtualRegister(&SP::IntRegsRegClass);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000311 MF.addLiveIn(*CurArgReg++, VReg);
312 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
313 if (ObjectVT != MVT::i32) {
314 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
315 : ISD::AssertZext;
316 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
317 DAG.getValueType(ObjectVT));
318 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
319 }
320 ArgValues.push_back(Arg);
321 } else {
322 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
323 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
324 SDOperand Load;
325 if (ObjectVT == MVT::i32) {
326 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
327 } else {
328 unsigned LoadOp =
329 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
330
Chris Lattner99cf5092006-01-16 01:40:00 +0000331 // Sparc is big endian, so add an offset based on the ObjectVT.
332 unsigned Offset = 4-std::max(1U, MVT::getSizeInBits(ObjectVT)/8);
333 FIPtr = DAG.getNode(ISD::ADD, MVT::i32, FIPtr,
334 DAG.getConstant(Offset, MVT::i32));
Chris Lattner384e5ef2005-12-18 13:33:06 +0000335 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
336 DAG.getSrcValue(0), ObjectVT);
Chris Lattnerf7511b42006-01-15 22:22:01 +0000337 Load = DAG.getNode(ISD::TRUNCATE, ObjectVT, Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000338 }
339 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000340 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000341
342 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000343 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000344 case MVT::f32:
345 if (I->use_empty()) { // Argument is dead.
346 if (CurArgReg < ArgRegEnd) ++CurArgReg;
347 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
348 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
349 // FP value is passed in an integer register.
Chris Lattner7c90f732006-02-05 05:50:24 +0000350 unsigned VReg = RegMap->createVirtualRegister(&SP::IntRegsRegClass);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000351 MF.addLiveIn(*CurArgReg++, VReg);
352 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
353
Chris Lattnera01874f2005-12-23 02:31:39 +0000354 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
355 ArgValues.push_back(Arg);
Chris Lattner46030a62006-01-19 07:22:29 +0000356 } else {
357 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
358 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
359 SDOperand Load = DAG.getLoad(MVT::f32, Root, FIPtr, DAG.getSrcValue(0));
360 ArgValues.push_back(Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000361 }
362 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000363 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000364
365 case MVT::i64:
366 case MVT::f64:
367 if (I->use_empty()) { // Argument is dead.
368 if (CurArgReg < ArgRegEnd) ++CurArgReg;
369 if (CurArgReg < ArgRegEnd) ++CurArgReg;
370 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
Chris Lattnerb7163432006-01-31 02:45:52 +0000371 } else if (/* FIXME: Apparently this isn't safe?? */
372 0 && CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
Chris Lattner384e5ef2005-12-18 13:33:06 +0000373 ((CurArgReg-ArgRegs) & 1) == 0) {
374 // If this is a double argument and the whole thing lives on the stack,
375 // and the argument is aligned, load the double straight from the stack.
376 // We can't do a load in cases like void foo([6ints], int,double),
377 // because the double wouldn't be aligned!
378 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
379 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
380 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
381 DAG.getSrcValue(0)));
382 } else {
383 SDOperand HiVal;
384 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner7c90f732006-02-05 05:50:24 +0000385 unsigned VRegHi = RegMap->createVirtualRegister(&SP::IntRegsRegClass);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000386 MF.addLiveIn(*CurArgReg++, VRegHi);
387 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
388 } else {
389 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
390 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
391 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
392 }
393
394 SDOperand LoVal;
395 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner7c90f732006-02-05 05:50:24 +0000396 unsigned VRegLo = RegMap->createVirtualRegister(&SP::IntRegsRegClass);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000397 MF.addLiveIn(*CurArgReg++, VRegLo);
398 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
399 } else {
400 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
401 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
402 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
403 }
404
405 // Compose the two halves together into an i64 unit.
406 SDOperand WholeValue =
407 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000408
409 // If we want a double, do a bit convert.
410 if (ObjectVT == MVT::f64)
411 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
412
413 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000414 }
415 ArgOffset += 8;
416 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000417 }
418 }
419
Chris Lattner384e5ef2005-12-18 13:33:06 +0000420 // Store remaining ArgRegs to the stack if this is a varargs function.
421 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000422 // Remember the vararg offset for the va_start implementation.
423 VarArgsFrameOffset = ArgOffset;
424
Chris Lattner384e5ef2005-12-18 13:33:06 +0000425 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
Chris Lattner7c90f732006-02-05 05:50:24 +0000426 unsigned VReg = RegMap->createVirtualRegister(&SP::IntRegsRegClass);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000427 MF.addLiveIn(*CurArgReg, VReg);
428 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
429
430 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
431 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
432
433 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
434 Arg, FIPtr, DAG.getSrcValue(0)));
435 ArgOffset += 4;
436 }
437 }
438
439 if (!OutChains.empty())
440 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000441
442 // Finally, inform the code generator which regs we return values in.
443 switch (getValueType(F.getReturnType())) {
444 default: assert(0 && "Unknown type!");
445 case MVT::isVoid: break;
446 case MVT::i1:
447 case MVT::i8:
448 case MVT::i16:
449 case MVT::i32:
Chris Lattner7c90f732006-02-05 05:50:24 +0000450 MF.addLiveOut(SP::I0);
Chris Lattnera01b7572005-12-17 08:03:24 +0000451 break;
452 case MVT::i64:
Chris Lattner7c90f732006-02-05 05:50:24 +0000453 MF.addLiveOut(SP::I0);
454 MF.addLiveOut(SP::I1);
Chris Lattnera01b7572005-12-17 08:03:24 +0000455 break;
456 case MVT::f32:
Chris Lattner7c90f732006-02-05 05:50:24 +0000457 MF.addLiveOut(SP::F0);
Chris Lattnera01b7572005-12-17 08:03:24 +0000458 break;
459 case MVT::f64:
Chris Lattner7c90f732006-02-05 05:50:24 +0000460 MF.addLiveOut(SP::D0);
Chris Lattnera01b7572005-12-17 08:03:24 +0000461 break;
462 }
463
464 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000465}
466
467std::pair<SDOperand, SDOperand>
Chris Lattner7c90f732006-02-05 05:50:24 +0000468SparcTargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
469 bool isVarArg, unsigned CC,
470 bool isTailCall, SDOperand Callee,
471 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000472 // Count the size of the outgoing arguments.
473 unsigned ArgsSize = 0;
474 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
475 switch (getValueType(Args[i].second)) {
476 default: assert(0 && "Unknown value type!");
477 case MVT::i1:
478 case MVT::i8:
479 case MVT::i16:
480 case MVT::i32:
481 case MVT::f32:
482 ArgsSize += 4;
483 break;
484 case MVT::i64:
485 case MVT::f64:
486 ArgsSize += 8;
487 break;
488 }
489 }
490 if (ArgsSize > 4*6)
491 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
492 else
493 ArgsSize = 0;
494
Chris Lattner6554bef2005-12-19 01:15:13 +0000495 // Keep stack frames 8-byte aligned.
496 ArgsSize = (ArgsSize+7) & ~7;
497
Chris Lattner94dd2922006-02-13 09:00:43 +0000498 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(ArgsSize, getPointerTy()));
Chris Lattner2db3ff62005-12-18 15:55:15 +0000499
500 SDOperand StackPtr, NullSV;
501 std::vector<SDOperand> Stores;
502 std::vector<SDOperand> RegValuesToPass;
503 unsigned ArgOffset = 68;
504 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
505 SDOperand Val = Args[i].first;
506 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000507 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000508 unsigned ObjSize;
509 switch (ObjectVT) {
510 default: assert(0 && "Unhandled argument type!");
511 case MVT::i1:
512 case MVT::i8:
513 case MVT::i16:
514 // Promote the integer to 32-bits. If the input type is signed, use a
515 // sign extend, otherwise use a zero extend.
516 if (Args[i].second->isSigned())
517 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
518 else
519 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
520 // FALL THROUGH
521 case MVT::i32:
522 ObjSize = 4;
523
524 if (RegValuesToPass.size() >= 6) {
525 ValToStore = Val;
526 } else {
527 RegValuesToPass.push_back(Val);
528 }
529 break;
530 case MVT::f32:
531 ObjSize = 4;
532 if (RegValuesToPass.size() >= 6) {
533 ValToStore = Val;
534 } else {
535 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000536 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000537 RegValuesToPass.push_back(Val);
538 }
539 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000540 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000541 ObjSize = 8;
542 // If we can store this directly into the outgoing slot, do so. We can
543 // do this when all ArgRegs are used and if the outgoing slot is aligned.
Chris Lattner7f9975a2006-01-15 19:15:46 +0000544 // FIXME: McGill/misr fails with this.
545 if (0 && RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000546 ValToStore = Val;
547 break;
548 }
549
550 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000551 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000552 // FALL THROUGH
553 case MVT::i64:
554 ObjSize = 8;
555 if (RegValuesToPass.size() >= 6) {
556 ValToStore = Val; // Whole thing is passed in memory.
557 break;
558 }
559
560 // Split the value into top and bottom part. Top part goes in a reg.
561 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
562 DAG.getConstant(1, MVT::i32));
563 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
564 DAG.getConstant(0, MVT::i32));
565 RegValuesToPass.push_back(Hi);
566
567 if (RegValuesToPass.size() >= 6) {
568 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000569 ArgOffset += 4;
570 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000571 } else {
572 RegValuesToPass.push_back(Lo);
573 }
574 break;
575 }
576
577 if (ValToStore.Val) {
578 if (!StackPtr.Val) {
Chris Lattner7c90f732006-02-05 05:50:24 +0000579 StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000580 NullSV = DAG.getSrcValue(NULL);
581 }
582 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
583 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
584 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
585 ValToStore, PtrOff, NullSV));
586 }
587 ArgOffset += ObjSize;
588 }
589
590 // Emit all stores, make sure the occur before any copies into physregs.
591 if (!Stores.empty())
592 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
593
594 static const unsigned ArgRegs[] = {
Chris Lattner7c90f732006-02-05 05:50:24 +0000595 SP::O0, SP::O1, SP::O2, SP::O3, SP::O4, SP::O5
Chris Lattner2db3ff62005-12-18 15:55:15 +0000596 };
597
598 // Build a sequence of copy-to-reg nodes chained together with token chain
599 // and flag operands which copy the outgoing args into O[0-5].
600 SDOperand InFlag;
601 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
602 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
603 InFlag = Chain.getValue(1);
604 }
605
Chris Lattner2db3ff62005-12-18 15:55:15 +0000606 // If the callee is a GlobalAddress node (quite common, every direct call is)
607 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000608 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner2db3ff62005-12-18 15:55:15 +0000609 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
610 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000611 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
612 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000613
614 std::vector<MVT::ValueType> NodeTys;
615 NodeTys.push_back(MVT::Other); // Returns a chain
616 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattner44ea7b12006-01-27 23:30:03 +0000617 std::vector<SDOperand> Ops;
618 Ops.push_back(Chain);
619 Ops.push_back(Callee);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000620 if (InFlag.Val)
Chris Lattner44ea7b12006-01-27 23:30:03 +0000621 Ops.push_back(InFlag);
Chris Lattner7c90f732006-02-05 05:50:24 +0000622 Chain = DAG.getNode(SPISD::CALL, NodeTys, Ops);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000623 InFlag = Chain.getValue(1);
624
625 MVT::ValueType RetTyVT = getValueType(RetTy);
626 SDOperand RetVal;
627 if (RetTyVT != MVT::isVoid) {
628 switch (RetTyVT) {
629 default: assert(0 && "Unknown value type to return!");
630 case MVT::i1:
631 case MVT::i8:
632 case MVT::i16:
Chris Lattner7c90f732006-02-05 05:50:24 +0000633 RetVal = DAG.getCopyFromReg(Chain, SP::O0, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000634 Chain = RetVal.getValue(1);
635
636 // Add a note to keep track of whether it is sign or zero extended.
637 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
638 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
639 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
640 break;
641 case MVT::i32:
Chris Lattner7c90f732006-02-05 05:50:24 +0000642 RetVal = DAG.getCopyFromReg(Chain, SP::O0, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000643 Chain = RetVal.getValue(1);
644 break;
645 case MVT::f32:
Chris Lattner7c90f732006-02-05 05:50:24 +0000646 RetVal = DAG.getCopyFromReg(Chain, SP::F0, MVT::f32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000647 Chain = RetVal.getValue(1);
648 break;
649 case MVT::f64:
Chris Lattner7c90f732006-02-05 05:50:24 +0000650 RetVal = DAG.getCopyFromReg(Chain, SP::D0, MVT::f64, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000651 Chain = RetVal.getValue(1);
652 break;
653 case MVT::i64:
Chris Lattner7c90f732006-02-05 05:50:24 +0000654 SDOperand Lo = DAG.getCopyFromReg(Chain, SP::O1, MVT::i32, InFlag);
655 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), SP::O0, MVT::i32,
Chris Lattner2db3ff62005-12-18 15:55:15 +0000656 Lo.getValue(2));
657 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
658 Chain = Hi.getValue(1);
659 break;
660 }
661 }
662
663 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
664 DAG.getConstant(ArgsSize, getPointerTy()));
665
Chris Lattner2db3ff62005-12-18 15:55:15 +0000666 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000667}
668
Chris Lattner7c90f732006-02-05 05:50:24 +0000669// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
670// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Chris Lattner86638b92006-01-31 05:05:52 +0000671static void LookThroughSetCC(SDOperand &LHS, SDOperand &RHS,
Chris Lattner7c90f732006-02-05 05:50:24 +0000672 ISD::CondCode CC, unsigned &SPCC) {
Chris Lattner86638b92006-01-31 05:05:52 +0000673 if (isa<ConstantSDNode>(RHS) && cast<ConstantSDNode>(RHS)->getValue() == 0 &&
674 CC == ISD::SETNE &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000675 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
676 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
677 (LHS.getOpcode() == SPISD::SELECT_FCC &&
678 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Chris Lattner86638b92006-01-31 05:05:52 +0000679 isa<ConstantSDNode>(LHS.getOperand(0)) &&
680 isa<ConstantSDNode>(LHS.getOperand(1)) &&
681 cast<ConstantSDNode>(LHS.getOperand(0))->getValue() == 1 &&
682 cast<ConstantSDNode>(LHS.getOperand(1))->getValue() == 0) {
683 SDOperand CMPCC = LHS.getOperand(3);
Chris Lattner7c90f732006-02-05 05:50:24 +0000684 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getValue();
Chris Lattner86638b92006-01-31 05:05:52 +0000685 LHS = CMPCC.getOperand(0);
686 RHS = CMPCC.getOperand(1);
687 }
688}
689
690
Chris Lattner7c90f732006-02-05 05:50:24 +0000691SDOperand SparcTargetLowering::
Chris Lattner4d55aca2005-12-18 01:20:35 +0000692LowerOperation(SDOperand Op, SelectionDAG &DAG) {
693 switch (Op.getOpcode()) {
694 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000695 case ISD::GlobalAddress: {
696 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
697 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner7c90f732006-02-05 05:50:24 +0000698 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, GA);
699 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, GA);
Chris Lattnere3572462005-12-18 02:10:39 +0000700 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
701 }
Chris Lattner76acc872005-12-18 02:37:35 +0000702 case ISD::ConstantPool: {
703 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
Evan Chengb8973bd2006-01-31 22:23:14 +0000704 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32,
705 cast<ConstantPoolSDNode>(Op)->getAlignment());
Chris Lattner7c90f732006-02-05 05:50:24 +0000706 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, CP);
707 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, CP);
Chris Lattner76acc872005-12-18 02:37:35 +0000708 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
709 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000710 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000711 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000712 assert(Op.getValueType() == MVT::i32);
Chris Lattner7c90f732006-02-05 05:50:24 +0000713 Op = DAG.getNode(SPISD::FTOI, MVT::f32, Op.getOperand(0));
Chris Lattner3cb71872005-12-23 05:00:16 +0000714 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000715 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000716 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner3fbb7262006-01-11 07:27:40 +0000717 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000718 // Convert the int value to FP in an FP register.
Chris Lattner7c90f732006-02-05 05:50:24 +0000719 return DAG.getNode(SPISD::ITOF, Op.getValueType(), Tmp);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000720 }
Chris Lattner33084492005-12-18 08:13:54 +0000721 case ISD::BR_CC: {
722 SDOperand Chain = Op.getOperand(0);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000723 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Chris Lattner33084492005-12-18 08:13:54 +0000724 SDOperand LHS = Op.getOperand(2);
725 SDOperand RHS = Op.getOperand(3);
726 SDOperand Dest = Op.getOperand(4);
Chris Lattner7c90f732006-02-05 05:50:24 +0000727 unsigned Opc, SPCC = ~0U;
Chris Lattner86638b92006-01-31 05:05:52 +0000728
729 // If this is a br_cc of a "setcc", and if the setcc got lowered into
730 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
Chris Lattner7c90f732006-02-05 05:50:24 +0000731 LookThroughSetCC(LHS, RHS, CC, SPCC);
Chris Lattner33084492005-12-18 08:13:54 +0000732
733 // Get the condition flag.
Chris Lattner86638b92006-01-31 05:05:52 +0000734 SDOperand CompareFlag;
Chris Lattner33084492005-12-18 08:13:54 +0000735 if (LHS.getValueType() == MVT::i32) {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000736 std::vector<MVT::ValueType> VTs;
737 VTs.push_back(MVT::i32);
738 VTs.push_back(MVT::Flag);
739 std::vector<SDOperand> Ops;
740 Ops.push_back(LHS);
741 Ops.push_back(RHS);
Chris Lattner7c90f732006-02-05 05:50:24 +0000742 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops).getValue(1);
743 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
744 Opc = SPISD::BRICC;
Chris Lattner33084492005-12-18 08:13:54 +0000745 } else {
Chris Lattner7c90f732006-02-05 05:50:24 +0000746 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
747 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
748 Opc = SPISD::BRFCC;
Chris Lattner33084492005-12-18 08:13:54 +0000749 }
Chris Lattner86638b92006-01-31 05:05:52 +0000750 return DAG.getNode(Opc, MVT::Other, Chain, Dest,
Chris Lattner7c90f732006-02-05 05:50:24 +0000751 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner33084492005-12-18 08:13:54 +0000752 }
753 case ISD::SELECT_CC: {
754 SDOperand LHS = Op.getOperand(0);
755 SDOperand RHS = Op.getOperand(1);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000756 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Chris Lattner33084492005-12-18 08:13:54 +0000757 SDOperand TrueVal = Op.getOperand(2);
758 SDOperand FalseVal = Op.getOperand(3);
Chris Lattner7c90f732006-02-05 05:50:24 +0000759 unsigned Opc, SPCC = ~0U;
Chris Lattner3772bcb2006-01-30 07:43:04 +0000760
Chris Lattnerdea95282006-01-30 04:34:44 +0000761 // If this is a select_cc of a "setcc", and if the setcc got lowered into
762 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
Chris Lattner7c90f732006-02-05 05:50:24 +0000763 LookThroughSetCC(LHS, RHS, CC, SPCC);
Chris Lattnerdea95282006-01-30 04:34:44 +0000764
Chris Lattner4bb91022006-01-12 17:05:32 +0000765 SDOperand CompareFlag;
Chris Lattner4bb91022006-01-12 17:05:32 +0000766 if (LHS.getValueType() == MVT::i32) {
767 std::vector<MVT::ValueType> VTs;
768 VTs.push_back(LHS.getValueType()); // subcc returns a value
769 VTs.push_back(MVT::Flag);
770 std::vector<SDOperand> Ops;
771 Ops.push_back(LHS);
772 Ops.push_back(RHS);
Chris Lattner7c90f732006-02-05 05:50:24 +0000773 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops).getValue(1);
774 Opc = SPISD::SELECT_ICC;
775 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Chris Lattner4bb91022006-01-12 17:05:32 +0000776 } else {
Chris Lattner7c90f732006-02-05 05:50:24 +0000777 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
778 Opc = SPISD::SELECT_FCC;
779 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
Chris Lattner4bb91022006-01-12 17:05:32 +0000780 }
Chris Lattner33084492005-12-18 08:13:54 +0000781 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
Chris Lattner7c90f732006-02-05 05:50:24 +0000782 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner33084492005-12-18 08:13:54 +0000783 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000784 case ISD::VASTART: {
785 // vastart just stores the address of the VarArgsFrameIndex slot into the
786 // memory location argument.
787 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner7c90f732006-02-05 05:50:24 +0000788 DAG.getRegister(SP::I6, MVT::i32),
Chris Lattnerc275dfa2006-02-04 08:31:30 +0000789 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
Nate Begemanacc398c2006-01-25 18:21:52 +0000790 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), Offset,
791 Op.getOperand(1), Op.getOperand(2));
792 }
Chris Lattnerc275dfa2006-02-04 08:31:30 +0000793 case ISD::VAARG: {
794 SDNode *Node = Op.Val;
795 MVT::ValueType VT = Node->getValueType(0);
796 SDOperand InChain = Node->getOperand(0);
797 SDOperand VAListPtr = Node->getOperand(1);
798 SDOperand VAList = DAG.getLoad(getPointerTy(), InChain, VAListPtr,
799 Node->getOperand(2));
800 // Increment the pointer, VAList, to the next vaarg
801 SDOperand NextPtr = DAG.getNode(ISD::ADD, getPointerTy(), VAList,
802 DAG.getConstant(MVT::getSizeInBits(VT)/8,
803 getPointerTy()));
804 // Store the incremented VAList to the legalized pointer
805 InChain = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), NextPtr,
806 VAListPtr, Node->getOperand(2));
807 // Load the actual argument out of the pointer VAList, unless this is an
808 // f64 load.
809 if (VT != MVT::f64) {
810 return DAG.getLoad(VT, InChain, VAList, DAG.getSrcValue(0));
811 } else {
812 // Otherwise, load it as i64, then do a bitconvert.
813 SDOperand V = DAG.getLoad(MVT::i64, InChain, VAList, DAG.getSrcValue(0));
814 std::vector<MVT::ValueType> Tys;
815 Tys.push_back(MVT::f64);
816 Tys.push_back(MVT::Other);
817 std::vector<SDOperand> Ops;
818 // Bit-Convert the value to f64.
819 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f64, V));
820 Ops.push_back(V.getValue(1));
821 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
822 }
823 }
Chris Lattner6fa1f572006-02-15 06:41:34 +0000824 case ISD::DYNAMIC_STACKALLOC: {
825 SDOperand Chain = Op.getOperand(0); // Legalize the chain.
826 SDOperand Size = Op.getOperand(1); // Legalize the size.
827
828 unsigned SPReg = SP::O6;
829 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, MVT::i32);
830 SDOperand NewSP = DAG.getNode(ISD::SUB, MVT::i32, SP, Size); // Value
831 Chain = DAG.getCopyToReg(SP.getValue(1), SPReg, NewSP); // Output chain
832
833 // The resultant pointer is actually 16 words from the bottom of the stack,
834 // to provide a register spill area.
835 SDOperand NewVal = DAG.getNode(ISD::ADD, MVT::i32, NewSP,
836 DAG.getConstant(96, MVT::i32));
837 std::vector<MVT::ValueType> Tys;
838 Tys.push_back(MVT::i32);
839 Tys.push_back(MVT::Other);
840 std::vector<SDOperand> Ops;
841 Ops.push_back(NewVal);
842 Ops.push_back(Chain);
843 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
844 }
Nate Begemanee625572006-01-27 21:09:22 +0000845 case ISD::RET: {
846 SDOperand Copy;
847
848 switch(Op.getNumOperands()) {
849 default:
850 assert(0 && "Do not know how to return this many arguments!");
851 abort();
852 case 1:
853 return SDOperand(); // ret void is legal
854 case 2: {
855 unsigned ArgReg;
856 switch(Op.getOperand(1).getValueType()) {
857 default: assert(0 && "Unknown type to return!");
Chris Lattner7c90f732006-02-05 05:50:24 +0000858 case MVT::i32: ArgReg = SP::I0; break;
859 case MVT::f32: ArgReg = SP::F0; break;
860 case MVT::f64: ArgReg = SP::D0; break;
Nate Begemanee625572006-01-27 21:09:22 +0000861 }
862 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
863 SDOperand());
864 break;
865 }
866 case 3:
Chris Lattner7c90f732006-02-05 05:50:24 +0000867 Copy = DAG.getCopyToReg(Op.getOperand(0), SP::I0, Op.getOperand(2),
Nate Begemanee625572006-01-27 21:09:22 +0000868 SDOperand());
Chris Lattner7c90f732006-02-05 05:50:24 +0000869 Copy = DAG.getCopyToReg(Copy, SP::I1, Op.getOperand(1), Copy.getValue(1));
Nate Begemanee625572006-01-27 21:09:22 +0000870 break;
871 }
Chris Lattner7c90f732006-02-05 05:50:24 +0000872 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begemanee625572006-01-27 21:09:22 +0000873 }
Chris Lattnerbce88872006-01-15 08:43:57 +0000874 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000875}
876
Chris Lattner33084492005-12-18 08:13:54 +0000877MachineBasicBlock *
Chris Lattner7c90f732006-02-05 05:50:24 +0000878SparcTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
879 MachineBasicBlock *BB) {
Chris Lattner33084492005-12-18 08:13:54 +0000880 unsigned BROpcode;
Chris Lattner7a4d2912006-01-31 06:56:30 +0000881 unsigned CC;
Chris Lattner33084492005-12-18 08:13:54 +0000882 // Figure out the conditional branch opcode to use for this select_cc.
883 switch (MI->getOpcode()) {
884 default: assert(0 && "Unknown SELECT_CC!");
Chris Lattner7c90f732006-02-05 05:50:24 +0000885 case SP::SELECT_CC_Int_ICC:
886 case SP::SELECT_CC_FP_ICC:
887 case SP::SELECT_CC_DFP_ICC:
888 BROpcode = SP::BCOND;
Chris Lattnerc03468b2006-01-31 17:20:06 +0000889 break;
Chris Lattner7c90f732006-02-05 05:50:24 +0000890 case SP::SELECT_CC_Int_FCC:
891 case SP::SELECT_CC_FP_FCC:
892 case SP::SELECT_CC_DFP_FCC:
893 BROpcode = SP::FBCOND;
Chris Lattner33084492005-12-18 08:13:54 +0000894 break;
895 }
Chris Lattner7a4d2912006-01-31 06:56:30 +0000896
Chris Lattner7c90f732006-02-05 05:50:24 +0000897 CC = (SPCC::CondCodes)MI->getOperand(3).getImmedValue();
Chris Lattner33084492005-12-18 08:13:54 +0000898
899 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
900 // control-flow pattern. The incoming instruction knows the destination vreg
901 // to set, the condition code register to branch on, the true/false values to
902 // select between, and a branch opcode to use.
903 const BasicBlock *LLVM_BB = BB->getBasicBlock();
904 ilist<MachineBasicBlock>::iterator It = BB;
905 ++It;
906
907 // thisMBB:
908 // ...
909 // TrueVal = ...
910 // [f]bCC copy1MBB
911 // fallthrough --> copy0MBB
912 MachineBasicBlock *thisMBB = BB;
913 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
914 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Chris Lattner7a4d2912006-01-31 06:56:30 +0000915 BuildMI(BB, BROpcode, 2).addMBB(sinkMBB).addImm(CC);
Chris Lattner33084492005-12-18 08:13:54 +0000916 MachineFunction *F = BB->getParent();
917 F->getBasicBlockList().insert(It, copy0MBB);
918 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +0000919 // Update machine-CFG edges by first adding all successors of the current
920 // block to the new block which will contain the Phi node for the select.
921 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
922 e = BB->succ_end(); i != e; ++i)
923 sinkMBB->addSuccessor(*i);
924 // Next, remove all successors of the current block, and add the true
925 // and fallthrough blocks as its successors.
926 while(!BB->succ_empty())
927 BB->removeSuccessor(BB->succ_begin());
Chris Lattner33084492005-12-18 08:13:54 +0000928 BB->addSuccessor(copy0MBB);
929 BB->addSuccessor(sinkMBB);
930
931 // copy0MBB:
932 // %FalseValue = ...
933 // # fallthrough to sinkMBB
934 BB = copy0MBB;
935
936 // Update machine-CFG edges
937 BB->addSuccessor(sinkMBB);
938
939 // sinkMBB:
940 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
941 // ...
942 BB = sinkMBB;
Chris Lattner7c90f732006-02-05 05:50:24 +0000943 BuildMI(BB, SP::PHI, 4, MI->getOperand(0).getReg())
Chris Lattner33084492005-12-18 08:13:54 +0000944 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
945 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
946
947 delete MI; // The pseudo instruction is gone now.
948 return BB;
949}
950
Chris Lattner6c18b102005-12-17 07:47:01 +0000951//===----------------------------------------------------------------------===//
952// Instruction Selector Implementation
953//===----------------------------------------------------------------------===//
954
955//===--------------------------------------------------------------------===//
Chris Lattner7c90f732006-02-05 05:50:24 +0000956/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
Chris Lattner6c18b102005-12-17 07:47:01 +0000957/// instructions for SelectionDAG operations.
958///
959namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +0000960class SparcDAGToDAGISel : public SelectionDAGISel {
961 SparcTargetLowering Lowering;
Chris Lattner76afdc92006-01-30 05:35:57 +0000962
963 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
964 /// make the right decision when generating code for different targets.
Chris Lattner7c90f732006-02-05 05:50:24 +0000965 const SparcSubtarget &Subtarget;
Chris Lattner6c18b102005-12-17 07:47:01 +0000966public:
Chris Lattner7c90f732006-02-05 05:50:24 +0000967 SparcDAGToDAGISel(TargetMachine &TM)
968 : SelectionDAGISel(Lowering), Lowering(TM),
969 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
Chris Lattner76afdc92006-01-30 05:35:57 +0000970 }
Chris Lattner6c18b102005-12-17 07:47:01 +0000971
Evan Cheng34167212006-02-09 00:37:58 +0000972 void Select(SDOperand &Result, SDOperand Op);
Chris Lattner6c18b102005-12-17 07:47:01 +0000973
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000974 // Complex Pattern Selectors.
975 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
976 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
977
Chris Lattner6c18b102005-12-17 07:47:01 +0000978 /// InstructionSelectBasicBlock - This callback is invoked by
979 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
980 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
981
982 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +0000983 return "SPARC DAG->DAG Pattern Instruction Selection";
Chris Lattner6c18b102005-12-17 07:47:01 +0000984 }
985
986 // Include the pieces autogenerated from the target description.
Chris Lattner7c90f732006-02-05 05:50:24 +0000987#include "SparcGenDAGISel.inc"
Chris Lattner6c18b102005-12-17 07:47:01 +0000988};
989} // end anonymous namespace
990
991/// InstructionSelectBasicBlock - This callback is invoked by
992/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7c90f732006-02-05 05:50:24 +0000993void SparcDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000994 DEBUG(BB->dump());
995
996 // Select target instructions for the DAG.
Evan Cheng900c8262006-02-05 06:51:51 +0000997 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattner6c18b102005-12-17 07:47:01 +0000998 CodeGenMap.clear();
999 DAG.RemoveDeadNodes();
1000
1001 // Emit machine code to BB.
1002 ScheduleAndEmitDAG(DAG);
1003}
1004
Chris Lattner7c90f732006-02-05 05:50:24 +00001005bool SparcDAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
Chris Lattner3029f922006-02-09 04:46:04 +00001006 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +00001007 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1008 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001009 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1010 return true;
1011 }
Chris Lattnerad7a3e62006-02-10 07:35:42 +00001012 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1013 Addr.getOpcode() == ISD::TargetGlobalAddress)
1014 return false; // direct calls.
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001015
1016 if (Addr.getOpcode() == ISD::ADD) {
1017 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
1018 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +00001019 if (FrameIndexSDNode *FIN =
1020 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001021 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +00001022 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001023 } else {
Chris Lattnerc26017a2006-02-05 08:35:50 +00001024 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001025 }
1026 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
1027 return true;
1028 }
1029 }
Chris Lattner7c90f732006-02-05 05:50:24 +00001030 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +00001031 Base = Addr.getOperand(1);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001032 Offset = Addr.getOperand(0).getOperand(0);
1033 return true;
1034 }
Chris Lattner7c90f732006-02-05 05:50:24 +00001035 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +00001036 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001037 Offset = Addr.getOperand(1).getOperand(0);
1038 return true;
1039 }
1040 }
Chris Lattnerc26017a2006-02-05 08:35:50 +00001041 Base = Addr;
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001042 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1043 return true;
1044}
1045
Chris Lattner7c90f732006-02-05 05:50:24 +00001046bool SparcDAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
1047 SDOperand &R2) {
Chris Lattnerad7a3e62006-02-10 07:35:42 +00001048 if (Addr.getOpcode() == ISD::FrameIndex) return false;
1049 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1050 Addr.getOpcode() == ISD::TargetGlobalAddress)
1051 return false; // direct calls.
1052
Chris Lattner9034b882005-12-17 21:25:27 +00001053 if (Addr.getOpcode() == ISD::ADD) {
1054 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
1055 Predicate_simm13(Addr.getOperand(1).Val))
1056 return false; // Let the reg+imm pattern catch this!
Chris Lattner7c90f732006-02-05 05:50:24 +00001057 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
1058 Addr.getOperand(1).getOpcode() == SPISD::Lo)
Chris Lattnere1389ad2005-12-18 02:27:00 +00001059 return false; // Let the reg+imm pattern catch this!
Chris Lattnerc26017a2006-02-05 08:35:50 +00001060 R1 = Addr.getOperand(0);
1061 R2 = Addr.getOperand(1);
Chris Lattner9034b882005-12-17 21:25:27 +00001062 return true;
1063 }
1064
Chris Lattnerc26017a2006-02-05 08:35:50 +00001065 R1 = Addr;
Chris Lattner7c90f732006-02-05 05:50:24 +00001066 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattnerbc83fd92005-12-17 20:04:49 +00001067 return true;
1068}
1069
Evan Cheng34167212006-02-09 00:37:58 +00001070void SparcDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattner6c18b102005-12-17 07:47:01 +00001071 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +00001072 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +00001073 N->getOpcode() < SPISD::FIRST_NUMBER) {
1074 Result = Op;
1075 return; // Already selected.
1076 }
1077
Chris Lattner6c18b102005-12-17 07:47:01 +00001078 // If this has already been converted, use it.
1079 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +00001080 if (CGMI != CodeGenMap.end()) {
1081 Result = CGMI->second;
1082 return;
1083 }
Chris Lattner6c18b102005-12-17 07:47:01 +00001084
1085 switch (N->getOpcode()) {
1086 default: break;
Chris Lattner7087e572005-12-17 22:39:19 +00001087 case ISD::SDIV:
1088 case ISD::UDIV: {
1089 // FIXME: should use a custom expander to expose the SRA to the dag.
Evan Cheng34167212006-02-09 00:37:58 +00001090 SDOperand DivLHS, DivRHS;
1091 Select(DivLHS, N->getOperand(0));
1092 Select(DivRHS, N->getOperand(1));
Chris Lattner7087e572005-12-17 22:39:19 +00001093
1094 // Set the Y register to the high-part.
1095 SDOperand TopPart;
1096 if (N->getOpcode() == ISD::SDIV) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001097 TopPart = SDOperand(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS,
1098 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +00001099 } else {
Chris Lattner7c90f732006-02-05 05:50:24 +00001100 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner7087e572005-12-17 22:39:19 +00001101 }
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001102 TopPart = SDOperand(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart,
1103 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +00001104
1105 // FIXME: Handle div by immediate.
Chris Lattner7c90f732006-02-05 05:50:24 +00001106 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Evan Cheng34167212006-02-09 00:37:58 +00001107 Result = CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
1108 return;
Chris Lattner7087e572005-12-17 22:39:19 +00001109 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001110 case ISD::MULHU:
1111 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +00001112 // FIXME: Handle mul by immediate.
Evan Cheng34167212006-02-09 00:37:58 +00001113 SDOperand MulLHS, MulRHS;
1114 Select(MulLHS, N->getOperand(0));
1115 Select(MulRHS, N->getOperand(1));
Chris Lattner7c90f732006-02-05 05:50:24 +00001116 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001117 SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
Chris Lattnerad7a3e62006-02-10 07:35:42 +00001118 MulLHS, MulRHS);
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001119 // The high part is in the Y register.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001120 Result = CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDOperand(Mul, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001121 return;
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001122 }
Chris Lattner6c18b102005-12-17 07:47:01 +00001123 }
1124
Evan Cheng34167212006-02-09 00:37:58 +00001125 SelectCode(Result, Op);
Chris Lattner6c18b102005-12-17 07:47:01 +00001126}
1127
1128
Chris Lattner7c90f732006-02-05 05:50:24 +00001129/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner4dcfaac2006-01-26 07:22:22 +00001130/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +00001131///
Chris Lattner7c90f732006-02-05 05:50:24 +00001132FunctionPass *llvm::createSparcISelDag(TargetMachine &TM) {
1133 return new SparcDAGToDAGISel(TM);
Chris Lattner6c18b102005-12-17 07:47:01 +00001134}