blob: 564103058bc8251643ece89cf448fc65f1002955 [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//
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.
Chris Lattner6c18b102005-12-17 07:47:01 +00007//
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 Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000023#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Target/TargetLowering.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000027#include "llvm/Support/Debug.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000028#include <queue>
Evan Cheng900c8262006-02-05 06:51:51 +000029#include <set>
Chris Lattner6c18b102005-12-17 07:47:01 +000030using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// TargetLowering Implementation
34//===----------------------------------------------------------------------===//
35
Chris Lattner7c90f732006-02-05 05:50:24 +000036namespace SPISD {
Chris Lattner4d55aca2005-12-18 01:20:35 +000037 enum {
Chris Lattner7c90f732006-02-05 05:50:24 +000038 FIRST_NUMBER = ISD::BUILTIN_OP_END+SP::INSTRUCTION_LIST_END,
Chris Lattner9072c052006-01-30 06:14:02 +000039 CMPICC, // Compare two GPR operands, set icc.
40 CMPFCC, // Compare two FP operands, set fcc.
41 BRICC, // Branch to dest on icc condition
42 BRFCC, // Branch to dest on fcc condition
43 SELECT_ICC, // Select between two values using the current ICC flags.
44 SELECT_FCC, // Select between two values using the current FCC flags.
Chris Lattnere3572462005-12-18 02:10:39 +000045
Chris Lattner9072c052006-01-30 06:14:02 +000046 Hi, Lo, // Hi/Lo operations, typically on a global address.
Chris Lattner8fa54dc2005-12-18 06:59:57 +000047
Chris Lattner9072c052006-01-30 06:14:02 +000048 FTOI, // FP to Int within a FP register.
49 ITOF, // Int to FP within a FP register.
50
Chris Lattner7c90f732006-02-05 05:50:24 +000051 CALL, // A call instruction.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000052 RET_FLAG // Return with a flag operand.
Chris Lattner4d55aca2005-12-18 01:20:35 +000053 };
54}
55
Chris Lattner3772bcb2006-01-30 07:43:04 +000056/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
57/// condition.
Chris Lattner7c90f732006-02-05 05:50:24 +000058static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
Chris Lattner3772bcb2006-01-30 07:43:04 +000059 switch (CC) {
60 default: assert(0 && "Unknown integer condition code!");
Chris Lattner7c90f732006-02-05 05:50:24 +000061 case ISD::SETEQ: return SPCC::ICC_E;
62 case ISD::SETNE: return SPCC::ICC_NE;
63 case ISD::SETLT: return SPCC::ICC_L;
64 case ISD::SETGT: return SPCC::ICC_G;
65 case ISD::SETLE: return SPCC::ICC_LE;
66 case ISD::SETGE: return SPCC::ICC_GE;
67 case ISD::SETULT: return SPCC::ICC_CS;
68 case ISD::SETULE: return SPCC::ICC_LEU;
69 case ISD::SETUGT: return SPCC::ICC_GU;
70 case ISD::SETUGE: return SPCC::ICC_CC;
Chris Lattner3772bcb2006-01-30 07:43:04 +000071 }
72}
73
74/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
75/// FCC condition.
Chris Lattner7c90f732006-02-05 05:50:24 +000076static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
Chris Lattner3772bcb2006-01-30 07:43:04 +000077 switch (CC) {
78 default: assert(0 && "Unknown fp condition code!");
Chris Lattner8b5fbc52006-05-25 22:26:02 +000079 case ISD::SETEQ:
80 case ISD::SETOEQ: return SPCC::FCC_E;
81 case ISD::SETNE:
82 case ISD::SETUNE: return SPCC::FCC_NE;
83 case ISD::SETLT:
84 case ISD::SETOLT: return SPCC::FCC_L;
85 case ISD::SETGT:
86 case ISD::SETOGT: return SPCC::FCC_G;
87 case ISD::SETLE:
88 case ISD::SETOLE: return SPCC::FCC_LE;
89 case ISD::SETGE:
90 case ISD::SETOGE: return SPCC::FCC_GE;
Chris Lattner7c90f732006-02-05 05:50:24 +000091 case ISD::SETULT: return SPCC::FCC_UL;
92 case ISD::SETULE: return SPCC::FCC_ULE;
93 case ISD::SETUGT: return SPCC::FCC_UG;
94 case ISD::SETUGE: return SPCC::FCC_UGE;
95 case ISD::SETUO: return SPCC::FCC_U;
96 case ISD::SETO: return SPCC::FCC_O;
97 case ISD::SETONE: return SPCC::FCC_LG;
98 case ISD::SETUEQ: return SPCC::FCC_UE;
Chris Lattner3772bcb2006-01-30 07:43:04 +000099 }
100}
Chris Lattner3772bcb2006-01-30 07:43:04 +0000101
Chris Lattner6c18b102005-12-17 07:47:01 +0000102namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +0000103 class SparcTargetLowering : public TargetLowering {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000104 int VarArgsFrameOffset; // Frame offset to start of varargs area.
Chris Lattner6c18b102005-12-17 07:47:01 +0000105 public:
Chris Lattner7c90f732006-02-05 05:50:24 +0000106 SparcTargetLowering(TargetMachine &TM);
Chris Lattner4d55aca2005-12-18 01:20:35 +0000107 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Chris Lattner4a397e02006-01-30 03:51:45 +0000108
Chris Lattner601fe382008-03-17 02:52:07 +0000109 int getVarArgsFrameOffset() const { return VarArgsFrameOffset; }
110
Nate Begeman368e18d2006-02-16 21:11:51 +0000111 /// computeMaskedBitsForTargetNode - Determine which of the bits specified
112 /// in Mask are known to be either zero or one and return them in the
113 /// KnownZero/KnownOne bitsets.
114 virtual void computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +0000115 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +0000116 APInt &KnownZero,
117 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +0000118 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +0000119 unsigned Depth = 0) const;
Chris Lattner4a397e02006-01-30 03:51:45 +0000120
Chris Lattner6c18b102005-12-17 07:47:01 +0000121 virtual std::vector<SDOperand>
122 LowerArguments(Function &F, SelectionDAG &DAG);
123 virtual std::pair<SDOperand, SDOperand>
Duncan Sands00fee652008-02-14 17:28:50 +0000124 LowerCallTo(SDOperand Chain, const Type *RetTy,
125 bool RetSExt, bool RetZExt, bool isVarArg,
126 unsigned CC, bool isTailCall, SDOperand Callee,
Reid Spencer47857812006-12-31 05:55:36 +0000127 ArgListTy &Args, SelectionDAG &DAG);
Evan Chengff9b3732008-01-30 18:18:23 +0000128 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
129 MachineBasicBlock *MBB);
Chris Lattner72878a42006-01-12 07:31:15 +0000130
131 virtual const char *getTargetNodeName(unsigned Opcode) const;
Chris Lattner6c18b102005-12-17 07:47:01 +0000132 };
133}
134
Chris Lattner7c90f732006-02-05 05:50:24 +0000135SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner6c18b102005-12-17 07:47:01 +0000136 : TargetLowering(TM) {
137
138 // Set up the register classes.
Chris Lattner7c90f732006-02-05 05:50:24 +0000139 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
140 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
141 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattner9a60ff62005-12-17 20:50:42 +0000142
Evan Chengc5484282006-10-04 00:56:09 +0000143 // Turn FP extload into load/fextend
144 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000145 // Sparc doesn't have i1 sign extending load
146 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnera4b52152008-02-28 05:48:04 +0000147 // Turn FP truncstore into trunc + store.
148 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000149
Chris Lattnere3572462005-12-18 02:10:39 +0000150 // Custom legalize GlobalAddress nodes into LO/HI parts.
151 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000152 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Chris Lattner76acc872005-12-18 02:37:35 +0000153 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Chris Lattnere3572462005-12-18 02:10:39 +0000154
Chris Lattner9a60ff62005-12-17 20:50:42 +0000155 // Sparc doesn't have sext_inreg, replace them with shl/sra
Chris Lattner33084492005-12-18 08:13:54 +0000156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner7087e572005-12-17 22:39:19 +0000159
Chris Lattner85d0aaa2007-10-10 18:10:57 +0000160 // Sparc has no REM or DIVREM operations.
Chris Lattner7087e572005-12-17 22:39:19 +0000161 setOperationAction(ISD::UREM, MVT::i32, Expand);
162 setOperationAction(ISD::SREM, MVT::i32, Expand);
Chris Lattner85d0aaa2007-10-10 18:10:57 +0000163 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
164 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000165
166 // Custom expand fp<->sint
167 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
168 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
169
170 // Expand fp<->uint
171 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
172 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Chris Lattner6c18b102005-12-17 07:47:01 +0000173
Chris Lattner53e88452005-12-23 05:13:35 +0000174 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
175 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
176
Chris Lattner4d55aca2005-12-18 01:20:35 +0000177 // Sparc has no select or setcc: expand to SELECT_CC.
178 setOperationAction(ISD::SELECT, MVT::i32, Expand);
179 setOperationAction(ISD::SELECT, MVT::f32, Expand);
180 setOperationAction(ISD::SELECT, MVT::f64, Expand);
181 setOperationAction(ISD::SETCC, MVT::i32, Expand);
182 setOperationAction(ISD::SETCC, MVT::f32, Expand);
183 setOperationAction(ISD::SETCC, MVT::f64, Expand);
184
185 // Sparc doesn't have BRCOND either, it has BR_CC.
186 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000187 setOperationAction(ISD::BRIND, MVT::Other, Expand);
188 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Chris Lattner4d55aca2005-12-18 01:20:35 +0000189 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
190 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
191 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
192
Chris Lattner33084492005-12-18 08:13:54 +0000193 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
194 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
195 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
196
Chris Lattner7c90f732006-02-05 05:50:24 +0000197 // SPARC has no intrinsics for these particular operations.
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000198 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
199 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
200 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000201 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
202
Chris Lattner61772c22005-12-19 01:39:40 +0000203 setOperationAction(ISD::FSIN , MVT::f64, Expand);
204 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner8dc4b592007-07-13 16:24:10 +0000205 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000206 setOperationAction(ISD::FSIN , MVT::f32, Expand);
207 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner8dc4b592007-07-13 16:24:10 +0000208 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000209 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
210 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
211 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000212 setOperationAction(ISD::ROTL , MVT::i32, Expand);
213 setOperationAction(ISD::ROTR , MVT::i32, Expand);
Nate Begemand88fc032006-01-14 03:14:10 +0000214 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Chris Lattner9601a862006-03-05 05:08:37 +0000215 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
216 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000217 setOperationAction(ISD::FPOW , MVT::f64, Expand);
218 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000219
220 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
221 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
222 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000223
Chris Lattner61273d52008-02-28 05:44:20 +0000224 // FIXME: Sparc provides these multiplies, but we don't have them yet.
225 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
226
Jim Laskeye81aecb2005-12-21 20:51:37 +0000227 // We don't have line number support yet.
228 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000229 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskey1ee29252007-01-26 14:34:52 +0000230 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000231
Nate Begemanee625572006-01-27 21:09:22 +0000232 // RET must be custom lowered, to meet ABI requirements
233 setOperationAction(ISD::RET , MVT::Other, Custom);
Duncan Sands36397f52007-07-27 12:58:54 +0000234
Chris Lattnerc275dfa2006-02-04 08:31:30 +0000235 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Nate Begemanacc398c2006-01-25 18:21:52 +0000236 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerc275dfa2006-02-04 08:31:30 +0000237 // VAARG needs to be lowered to not do unaligned accesses for doubles.
238 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000239
240 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000241 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
242 setOperationAction(ISD::VAEND , MVT::Other, Expand);
243 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
244 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
Chris Lattner6fa1f572006-02-15 06:41:34 +0000245 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner934ea492006-01-15 08:55:25 +0000246
Chris Lattner6baaf912008-02-28 05:54:25 +0000247 // No debug info support yet.
248 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
249 setOperationAction(ISD::LABEL, MVT::Other, Expand);
250 setOperationAction(ISD::DECLARE, MVT::Other, Expand);
251
Chris Lattner7c90f732006-02-05 05:50:24 +0000252 setStackPointerRegisterToSaveRestore(SP::O6);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000253
Chris Lattner6baaf912008-02-28 05:54:25 +0000254 if (TM.getSubtarget<SparcSubtarget>().isV9())
Chris Lattner9072c052006-01-30 06:14:02 +0000255 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Chris Lattner9072c052006-01-30 06:14:02 +0000256
Chris Lattner6c18b102005-12-17 07:47:01 +0000257 computeRegisterProperties();
258}
259
Chris Lattner7c90f732006-02-05 05:50:24 +0000260const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Chris Lattner72878a42006-01-12 07:31:15 +0000261 switch (Opcode) {
Chris Lattner138d3222006-01-12 07:38:04 +0000262 default: return 0;
Chris Lattner7c90f732006-02-05 05:50:24 +0000263 case SPISD::CMPICC: return "SPISD::CMPICC";
264 case SPISD::CMPFCC: return "SPISD::CMPFCC";
265 case SPISD::BRICC: return "SPISD::BRICC";
266 case SPISD::BRFCC: return "SPISD::BRFCC";
267 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
268 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
269 case SPISD::Hi: return "SPISD::Hi";
270 case SPISD::Lo: return "SPISD::Lo";
271 case SPISD::FTOI: return "SPISD::FTOI";
272 case SPISD::ITOF: return "SPISD::ITOF";
273 case SPISD::CALL: return "SPISD::CALL";
274 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Chris Lattner72878a42006-01-12 07:31:15 +0000275 }
276}
277
Chris Lattner4a397e02006-01-30 03:51:45 +0000278/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
279/// be zero. Op is expected to be a target specific node. Used by DAG
280/// combiner.
Nate Begeman368e18d2006-02-16 21:11:51 +0000281void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +0000282 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +0000283 APInt &KnownZero,
284 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +0000285 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +0000286 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +0000287 APInt KnownZero2, KnownOne2;
288 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Nate Begeman368e18d2006-02-16 21:11:51 +0000289
Chris Lattner4a397e02006-01-30 03:51:45 +0000290 switch (Op.getOpcode()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000291 default: break;
Chris Lattner7c90f732006-02-05 05:50:24 +0000292 case SPISD::SELECT_ICC:
293 case SPISD::SELECT_FCC:
Dan Gohmanea859be2007-06-22 14:59:07 +0000294 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
295 Depth+1);
296 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
297 Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000298 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
299 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
300
301 // Only known if known in both the LHS and RHS.
302 KnownOne &= KnownOne2;
303 KnownZero &= KnownZero2;
304 break;
Chris Lattner4a397e02006-01-30 03:51:45 +0000305 }
306}
307
Chris Lattner384e5ef2005-12-18 13:33:06 +0000308/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
309/// either one or two GPRs, including FP values. TODO: we should pass FP values
310/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000311std::vector<SDOperand>
Chris Lattner7c90f732006-02-05 05:50:24 +0000312SparcTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000313 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +0000314 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Chris Lattnera01b7572005-12-17 08:03:24 +0000315 std::vector<SDOperand> ArgValues;
316
Chris Lattner384e5ef2005-12-18 13:33:06 +0000317 static const unsigned ArgRegs[] = {
Chris Lattner7c90f732006-02-05 05:50:24 +0000318 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
Chris Lattnera01b7572005-12-17 08:03:24 +0000319 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000320
321 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
322 unsigned ArgOffset = 68;
323
324 SDOperand Root = DAG.getRoot();
325 std::vector<SDOperand> OutChains;
326
Chris Lattnera01b7572005-12-17 08:03:24 +0000327 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
328 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000329
330 switch (ObjectVT) {
331 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000332 case MVT::i1:
333 case MVT::i8:
334 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000335 case MVT::i32:
336 if (I->use_empty()) { // Argument is dead.
337 if (CurArgReg < ArgRegEnd) ++CurArgReg;
338 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
339 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner84bc5422007-12-31 04:13:23 +0000340 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
341 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000342 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
343 if (ObjectVT != MVT::i32) {
Reid Spencer47857812006-12-31 05:55:36 +0000344 unsigned AssertOp = ISD::AssertSext;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000345 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
346 DAG.getValueType(ObjectVT));
347 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
348 }
349 ArgValues.push_back(Arg);
350 } else {
351 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
352 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
353 SDOperand Load;
354 if (ObjectVT == MVT::i32) {
Evan Cheng466685d2006-10-09 20:57:25 +0000355 Load = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000356 } else {
Reid Spencer47857812006-12-31 05:55:36 +0000357 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000358
Chris Lattner99cf5092006-01-16 01:40:00 +0000359 // Sparc is big endian, so add an offset based on the ObjectVT.
360 unsigned Offset = 4-std::max(1U, MVT::getSizeInBits(ObjectVT)/8);
361 FIPtr = DAG.getNode(ISD::ADD, MVT::i32, FIPtr,
362 DAG.getConstant(Offset, MVT::i32));
Chris Lattner384e5ef2005-12-18 13:33:06 +0000363 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
Evan Cheng466685d2006-10-09 20:57:25 +0000364 NULL, 0, ObjectVT);
Chris Lattnerf7511b42006-01-15 22:22:01 +0000365 Load = DAG.getNode(ISD::TRUNCATE, ObjectVT, Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000366 }
367 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000368 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000369
370 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000371 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000372 case MVT::f32:
373 if (I->use_empty()) { // Argument is dead.
374 if (CurArgReg < ArgRegEnd) ++CurArgReg;
375 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
376 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
377 // FP value is passed in an integer register.
Chris Lattner84bc5422007-12-31 04:13:23 +0000378 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
379 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000380 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
381
Chris Lattnera01874f2005-12-23 02:31:39 +0000382 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
383 ArgValues.push_back(Arg);
Chris Lattner46030a62006-01-19 07:22:29 +0000384 } else {
385 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
386 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Evan Cheng466685d2006-10-09 20:57:25 +0000387 SDOperand Load = DAG.getLoad(MVT::f32, Root, FIPtr, NULL, 0);
Chris Lattner46030a62006-01-19 07:22:29 +0000388 ArgValues.push_back(Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000389 }
390 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000391 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000392
393 case MVT::i64:
394 case MVT::f64:
395 if (I->use_empty()) { // Argument is dead.
396 if (CurArgReg < ArgRegEnd) ++CurArgReg;
397 if (CurArgReg < ArgRegEnd) ++CurArgReg;
398 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
Chris Lattnerb7163432006-01-31 02:45:52 +0000399 } else if (/* FIXME: Apparently this isn't safe?? */
400 0 && CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
Chris Lattner384e5ef2005-12-18 13:33:06 +0000401 ((CurArgReg-ArgRegs) & 1) == 0) {
402 // If this is a double argument and the whole thing lives on the stack,
403 // and the argument is aligned, load the double straight from the stack.
404 // We can't do a load in cases like void foo([6ints], int,double),
405 // because the double wouldn't be aligned!
406 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
407 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Evan Cheng466685d2006-10-09 20:57:25 +0000408 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr, NULL, 0));
Chris Lattner384e5ef2005-12-18 13:33:06 +0000409 } else {
410 SDOperand HiVal;
411 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner84bc5422007-12-31 04:13:23 +0000412 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
413 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000414 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
415 } else {
416 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
417 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Evan Cheng466685d2006-10-09 20:57:25 +0000418 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000419 }
420
421 SDOperand LoVal;
422 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner84bc5422007-12-31 04:13:23 +0000423 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
424 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000425 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
426 } else {
427 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
428 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Evan Cheng466685d2006-10-09 20:57:25 +0000429 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000430 }
431
432 // Compose the two halves together into an i64 unit.
433 SDOperand WholeValue =
434 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000435
436 // If we want a double, do a bit convert.
437 if (ObjectVT == MVT::f64)
438 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
439
440 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000441 }
442 ArgOffset += 8;
443 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000444 }
445 }
446
Chris Lattner384e5ef2005-12-18 13:33:06 +0000447 // Store remaining ArgRegs to the stack if this is a varargs function.
448 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000449 // Remember the vararg offset for the va_start implementation.
450 VarArgsFrameOffset = ArgOffset;
451
Chris Lattner384e5ef2005-12-18 13:33:06 +0000452 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000453 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
454 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000455 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
456
457 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
458 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
459
Evan Cheng8b2794a2006-10-13 21:14:26 +0000460 OutChains.push_back(DAG.getStore(DAG.getRoot(), Arg, FIPtr, NULL, 0));
Chris Lattner384e5ef2005-12-18 13:33:06 +0000461 ArgOffset += 4;
462 }
463 }
464
465 if (!OutChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000466 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other,
467 &OutChains[0], OutChains.size()));
Chris Lattnera01b7572005-12-17 08:03:24 +0000468
469 // Finally, inform the code generator which regs we return values in.
470 switch (getValueType(F.getReturnType())) {
471 default: assert(0 && "Unknown type!");
472 case MVT::isVoid: break;
473 case MVT::i1:
474 case MVT::i8:
475 case MVT::i16:
476 case MVT::i32:
Chris Lattner84bc5422007-12-31 04:13:23 +0000477 MF.getRegInfo().addLiveOut(SP::I0);
Chris Lattnera01b7572005-12-17 08:03:24 +0000478 break;
479 case MVT::i64:
Chris Lattner84bc5422007-12-31 04:13:23 +0000480 MF.getRegInfo().addLiveOut(SP::I0);
481 MF.getRegInfo().addLiveOut(SP::I1);
Chris Lattnera01b7572005-12-17 08:03:24 +0000482 break;
483 case MVT::f32:
Chris Lattner84bc5422007-12-31 04:13:23 +0000484 MF.getRegInfo().addLiveOut(SP::F0);
Chris Lattnera01b7572005-12-17 08:03:24 +0000485 break;
486 case MVT::f64:
Chris Lattner84bc5422007-12-31 04:13:23 +0000487 MF.getRegInfo().addLiveOut(SP::D0);
Chris Lattnera01b7572005-12-17 08:03:24 +0000488 break;
489 }
490
491 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000492}
493
494std::pair<SDOperand, SDOperand>
Chris Lattner7c90f732006-02-05 05:50:24 +0000495SparcTargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
Duncan Sands00fee652008-02-14 17:28:50 +0000496 bool RetSExt, bool RetZExt, bool isVarArg,
497 unsigned CC, bool isTailCall, SDOperand Callee,
Chris Lattner7c90f732006-02-05 05:50:24 +0000498 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000499 // Count the size of the outgoing arguments.
500 unsigned ArgsSize = 0;
501 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Reid Spencer47857812006-12-31 05:55:36 +0000502 switch (getValueType(Args[i].Ty)) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000503 default: assert(0 && "Unknown value type!");
504 case MVT::i1:
505 case MVT::i8:
506 case MVT::i16:
507 case MVT::i32:
508 case MVT::f32:
509 ArgsSize += 4;
510 break;
511 case MVT::i64:
512 case MVT::f64:
513 ArgsSize += 8;
514 break;
515 }
516 }
517 if (ArgsSize > 4*6)
518 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
519 else
520 ArgsSize = 0;
521
Chris Lattner6554bef2005-12-19 01:15:13 +0000522 // Keep stack frames 8-byte aligned.
523 ArgsSize = (ArgsSize+7) & ~7;
524
Chris Lattner94dd2922006-02-13 09:00:43 +0000525 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(ArgsSize, getPointerTy()));
Chris Lattner2db3ff62005-12-18 15:55:15 +0000526
Evan Cheng8b2794a2006-10-13 21:14:26 +0000527 SDOperand StackPtr;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000528 std::vector<SDOperand> Stores;
529 std::vector<SDOperand> RegValuesToPass;
530 unsigned ArgOffset = 68;
531 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Reid Spencer47857812006-12-31 05:55:36 +0000532 SDOperand Val = Args[i].Node;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000533 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000534 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000535 unsigned ObjSize;
536 switch (ObjectVT) {
537 default: assert(0 && "Unhandled argument type!");
538 case MVT::i1:
539 case MVT::i8:
Reid Spencer47857812006-12-31 05:55:36 +0000540 case MVT::i16: {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000541 // Promote the integer to 32-bits. If the input type is signed, use a
542 // sign extend, otherwise use a zero extend.
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000543 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
544 if (Args[i].isSExt)
Reid Spencer47857812006-12-31 05:55:36 +0000545 ExtendKind = ISD::SIGN_EXTEND;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000546 else if (Args[i].isZExt)
547 ExtendKind = ISD::ZERO_EXTEND;
Reid Spencer47857812006-12-31 05:55:36 +0000548 Val = DAG.getNode(ExtendKind, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000549 // FALL THROUGH
Reid Spencer47857812006-12-31 05:55:36 +0000550 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000551 case MVT::i32:
552 ObjSize = 4;
553
554 if (RegValuesToPass.size() >= 6) {
555 ValToStore = Val;
556 } else {
557 RegValuesToPass.push_back(Val);
558 }
559 break;
560 case MVT::f32:
561 ObjSize = 4;
562 if (RegValuesToPass.size() >= 6) {
563 ValToStore = Val;
564 } else {
565 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000566 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000567 RegValuesToPass.push_back(Val);
568 }
569 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000570 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000571 ObjSize = 8;
572 // If we can store this directly into the outgoing slot, do so. We can
573 // do this when all ArgRegs are used and if the outgoing slot is aligned.
Chris Lattner7f9975a2006-01-15 19:15:46 +0000574 // FIXME: McGill/misr fails with this.
575 if (0 && RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000576 ValToStore = Val;
577 break;
578 }
579
580 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000581 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000582 // FALL THROUGH
583 case MVT::i64:
584 ObjSize = 8;
585 if (RegValuesToPass.size() >= 6) {
586 ValToStore = Val; // Whole thing is passed in memory.
587 break;
588 }
589
590 // Split the value into top and bottom part. Top part goes in a reg.
Evan Chenga7dc4a52006-06-15 08:18:06 +0000591 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, getPointerTy(), Val,
Chris Lattner2db3ff62005-12-18 15:55:15 +0000592 DAG.getConstant(1, MVT::i32));
Evan Chenga7dc4a52006-06-15 08:18:06 +0000593 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, getPointerTy(), Val,
Chris Lattner2db3ff62005-12-18 15:55:15 +0000594 DAG.getConstant(0, MVT::i32));
595 RegValuesToPass.push_back(Hi);
596
597 if (RegValuesToPass.size() >= 6) {
598 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000599 ArgOffset += 4;
600 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000601 } else {
602 RegValuesToPass.push_back(Lo);
603 }
604 break;
605 }
606
607 if (ValToStore.Val) {
608 if (!StackPtr.Val) {
Chris Lattner7c90f732006-02-05 05:50:24 +0000609 StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000610 }
611 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
612 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000613 Stores.push_back(DAG.getStore(Chain, ValToStore, PtrOff, NULL, 0));
Chris Lattner2db3ff62005-12-18 15:55:15 +0000614 }
615 ArgOffset += ObjSize;
616 }
617
618 // Emit all stores, make sure the occur before any copies into physregs.
619 if (!Stores.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000620 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],Stores.size());
Chris Lattner2db3ff62005-12-18 15:55:15 +0000621
622 static const unsigned ArgRegs[] = {
Chris Lattner7c90f732006-02-05 05:50:24 +0000623 SP::O0, SP::O1, SP::O2, SP::O3, SP::O4, SP::O5
Chris Lattner2db3ff62005-12-18 15:55:15 +0000624 };
625
626 // Build a sequence of copy-to-reg nodes chained together with token chain
627 // and flag operands which copy the outgoing args into O[0-5].
628 SDOperand InFlag;
629 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
630 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
631 InFlag = Chain.getValue(1);
632 }
633
Chris Lattner2db3ff62005-12-18 15:55:15 +0000634 // If the callee is a GlobalAddress node (quite common, every direct call is)
635 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000636 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner2db3ff62005-12-18 15:55:15 +0000637 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
638 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000639 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
640 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000641
642 std::vector<MVT::ValueType> NodeTys;
643 NodeTys.push_back(MVT::Other); // Returns a chain
644 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000645 SDOperand Ops[] = { Chain, Callee, InFlag };
646 Chain = DAG.getNode(SPISD::CALL, NodeTys, Ops, InFlag.Val ? 3 : 2);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000647 InFlag = Chain.getValue(1);
648
649 MVT::ValueType RetTyVT = getValueType(RetTy);
650 SDOperand RetVal;
651 if (RetTyVT != MVT::isVoid) {
652 switch (RetTyVT) {
653 default: assert(0 && "Unknown value type to return!");
654 case MVT::i1:
655 case MVT::i8:
Reid Spencer47857812006-12-31 05:55:36 +0000656 case MVT::i16: {
Chris Lattner7c90f732006-02-05 05:50:24 +0000657 RetVal = DAG.getCopyFromReg(Chain, SP::O0, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000658 Chain = RetVal.getValue(1);
659
660 // Add a note to keep track of whether it is sign or zero extended.
Duncan Sands00fee652008-02-14 17:28:50 +0000661 ISD::NodeType AssertKind = ISD::DELETED_NODE;
662 if (RetSExt)
Reid Spencer47857812006-12-31 05:55:36 +0000663 AssertKind = ISD::AssertSext;
Duncan Sands00fee652008-02-14 17:28:50 +0000664 else if (RetZExt)
665 AssertKind = ISD::AssertZext;
666
667 if (AssertKind != ISD::DELETED_NODE)
668 RetVal = DAG.getNode(AssertKind, MVT::i32, RetVal,
669 DAG.getValueType(RetTyVT));
670
Chris Lattner2db3ff62005-12-18 15:55:15 +0000671 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
672 break;
Reid Spencer47857812006-12-31 05:55:36 +0000673 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000674 case MVT::i32:
Chris Lattner7c90f732006-02-05 05:50:24 +0000675 RetVal = DAG.getCopyFromReg(Chain, SP::O0, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000676 Chain = RetVal.getValue(1);
677 break;
678 case MVT::f32:
Chris Lattner7c90f732006-02-05 05:50:24 +0000679 RetVal = DAG.getCopyFromReg(Chain, SP::F0, MVT::f32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000680 Chain = RetVal.getValue(1);
681 break;
682 case MVT::f64:
Chris Lattner7c90f732006-02-05 05:50:24 +0000683 RetVal = DAG.getCopyFromReg(Chain, SP::D0, MVT::f64, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000684 Chain = RetVal.getValue(1);
685 break;
686 case MVT::i64:
Chris Lattner7c90f732006-02-05 05:50:24 +0000687 SDOperand Lo = DAG.getCopyFromReg(Chain, SP::O1, MVT::i32, InFlag);
688 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), SP::O0, MVT::i32,
Chris Lattner2db3ff62005-12-18 15:55:15 +0000689 Lo.getValue(2));
690 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
691 Chain = Hi.getValue(1);
692 break;
693 }
694 }
695
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000696 Chain = DAG.getCALLSEQ_END(Chain,
697 DAG.getConstant(ArgsSize, getPointerTy()),
698 DAG.getConstant(0, getPointerTy()),
699 SDOperand());
Chris Lattner2db3ff62005-12-18 15:55:15 +0000700 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000701}
702
Chris Lattner7c90f732006-02-05 05:50:24 +0000703// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
704// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Chris Lattner86638b92006-01-31 05:05:52 +0000705static void LookThroughSetCC(SDOperand &LHS, SDOperand &RHS,
Chris Lattner7c90f732006-02-05 05:50:24 +0000706 ISD::CondCode CC, unsigned &SPCC) {
Chris Lattner86638b92006-01-31 05:05:52 +0000707 if (isa<ConstantSDNode>(RHS) && cast<ConstantSDNode>(RHS)->getValue() == 0 &&
708 CC == ISD::SETNE &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000709 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
710 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
711 (LHS.getOpcode() == SPISD::SELECT_FCC &&
712 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Chris Lattner86638b92006-01-31 05:05:52 +0000713 isa<ConstantSDNode>(LHS.getOperand(0)) &&
714 isa<ConstantSDNode>(LHS.getOperand(1)) &&
715 cast<ConstantSDNode>(LHS.getOperand(0))->getValue() == 1 &&
716 cast<ConstantSDNode>(LHS.getOperand(1))->getValue() == 0) {
717 SDOperand CMPCC = LHS.getOperand(3);
Chris Lattner7c90f732006-02-05 05:50:24 +0000718 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getValue();
Chris Lattner86638b92006-01-31 05:05:52 +0000719 LHS = CMPCC.getOperand(0);
720 RHS = CMPCC.getOperand(1);
721 }
722}
723
Chris Lattner601fe382008-03-17 02:52:07 +0000724static SDOperand LowerGLOBALADDRESS(SDOperand Op, SelectionDAG &DAG) {
725 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
726 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
727 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, GA);
728 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, GA);
729 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
730}
731
732static SDOperand LowerCONSTANTPOOL(SDOperand Op, SelectionDAG &DAG) {
733 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
734 Constant *C = N->getConstVal();
735 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
736 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, CP);
737 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, CP);
738 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
739}
740
741static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
742 // Convert the fp value to integer in an FP register.
743 assert(Op.getValueType() == MVT::i32);
744 Op = DAG.getNode(SPISD::FTOI, MVT::f32, Op.getOperand(0));
745 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
746}
747
748static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
749 assert(Op.getOperand(0).getValueType() == MVT::i32);
750 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
751 // Convert the int value to FP in an FP register.
752 return DAG.getNode(SPISD::ITOF, Op.getValueType(), Tmp);
753}
754
755static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
756 SDOperand Chain = Op.getOperand(0);
757 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
758 SDOperand LHS = Op.getOperand(2);
759 SDOperand RHS = Op.getOperand(3);
760 SDOperand Dest = Op.getOperand(4);
761 unsigned Opc, SPCC = ~0U;
762
763 // If this is a br_cc of a "setcc", and if the setcc got lowered into
764 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
765 LookThroughSetCC(LHS, RHS, CC, SPCC);
766
767 // Get the condition flag.
768 SDOperand CompareFlag;
769 if (LHS.getValueType() == MVT::i32) {
770 std::vector<MVT::ValueType> VTs;
771 VTs.push_back(MVT::i32);
772 VTs.push_back(MVT::Flag);
773 SDOperand Ops[2] = { LHS, RHS };
774 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
775 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
776 Opc = SPISD::BRICC;
777 } else {
778 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
779 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
780 Opc = SPISD::BRFCC;
781 }
782 return DAG.getNode(Opc, MVT::Other, Chain, Dest,
783 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
784}
785
786static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
787 SDOperand LHS = Op.getOperand(0);
788 SDOperand RHS = Op.getOperand(1);
789 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
790 SDOperand TrueVal = Op.getOperand(2);
791 SDOperand FalseVal = Op.getOperand(3);
792 unsigned Opc, SPCC = ~0U;
793
794 // If this is a select_cc of a "setcc", and if the setcc got lowered into
795 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
796 LookThroughSetCC(LHS, RHS, CC, SPCC);
797
798 SDOperand CompareFlag;
799 if (LHS.getValueType() == MVT::i32) {
800 std::vector<MVT::ValueType> VTs;
801 VTs.push_back(LHS.getValueType()); // subcc returns a value
802 VTs.push_back(MVT::Flag);
803 SDOperand Ops[2] = { LHS, RHS };
804 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
805 Opc = SPISD::SELECT_ICC;
806 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
807 } else {
808 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
809 Opc = SPISD::SELECT_FCC;
810 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
811 }
812 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
813 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
814}
815
816static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
817 SparcTargetLowering &TLI) {
818 // vastart just stores the address of the VarArgsFrameIndex slot into the
819 // memory location argument.
820 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
821 DAG.getRegister(SP::I6, MVT::i32),
822 DAG.getConstant(TLI.getVarArgsFrameOffset(),
823 MVT::i32));
824 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
825 return DAG.getStore(Op.getOperand(0), Offset, Op.getOperand(1), SV, 0);
826}
827
828static SDOperand LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
829 SDNode *Node = Op.Val;
830 MVT::ValueType VT = Node->getValueType(0);
831 SDOperand InChain = Node->getOperand(0);
832 SDOperand VAListPtr = Node->getOperand(1);
833 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
834 SDOperand VAList = DAG.getLoad(MVT::i32, InChain, VAListPtr, SV, 0);
835 // Increment the pointer, VAList, to the next vaarg
836 SDOperand NextPtr = DAG.getNode(ISD::ADD, MVT::i32, VAList,
837 DAG.getConstant(MVT::getSizeInBits(VT)/8,
838 MVT::i32));
839 // Store the incremented VAList to the legalized pointer
840 InChain = DAG.getStore(VAList.getValue(1), NextPtr,
841 VAListPtr, SV, 0);
842 // Load the actual argument out of the pointer VAList, unless this is an
843 // f64 load.
844 if (VT != MVT::f64)
845 return DAG.getLoad(VT, InChain, VAList, NULL, 0);
846
847 // Otherwise, load it as i64, then do a bitconvert.
848 SDOperand V = DAG.getLoad(MVT::i64, InChain, VAList, NULL, 0);
849
850 // Bit-Convert the value to f64.
851 SDOperand Ops[2] = {
852 DAG.getNode(ISD::BIT_CONVERT, MVT::f64, V),
853 V.getValue(1)
854 };
855 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(MVT::f64, MVT::Other),
856 Ops, 2);
857}
858
859static SDOperand LowerDYNAMIC_STACKALLOC(SDOperand Op, SelectionDAG &DAG) {
860 SDOperand Chain = Op.getOperand(0); // Legalize the chain.
861 SDOperand Size = Op.getOperand(1); // Legalize the size.
862
863 unsigned SPReg = SP::O6;
864 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, MVT::i32);
865 SDOperand NewSP = DAG.getNode(ISD::SUB, MVT::i32, SP, Size); // Value
866 Chain = DAG.getCopyToReg(SP.getValue(1), SPReg, NewSP); // Output chain
867
868 // The resultant pointer is actually 16 words from the bottom of the stack,
869 // to provide a register spill area.
870 SDOperand NewVal = DAG.getNode(ISD::ADD, MVT::i32, NewSP,
871 DAG.getConstant(96, MVT::i32));
872 std::vector<MVT::ValueType> Tys;
873 Tys.push_back(MVT::i32);
874 Tys.push_back(MVT::Other);
875 SDOperand Ops[2] = { NewVal, Chain };
876 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
877}
878
879static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
880 SDOperand Copy;
881
882 switch(Op.getNumOperands()) {
883 default:
884 assert(0 && "Do not know how to return this many arguments!");
885 abort();
886 case 1:
887 return SDOperand(); // ret void is legal
888 case 3: {
889 unsigned ArgReg;
890 switch(Op.getOperand(1).getValueType()) {
891 default: assert(0 && "Unknown type to return!");
892 case MVT::i32: ArgReg = SP::I0; break;
893 case MVT::f32: ArgReg = SP::F0; break;
894 case MVT::f64: ArgReg = SP::D0; break;
895 }
896 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
897 SDOperand());
898 break;
899 }
900 case 5:
901 Copy = DAG.getCopyToReg(Op.getOperand(0), SP::I0, Op.getOperand(3),
902 SDOperand());
903 Copy = DAG.getCopyToReg(Copy, SP::I1, Op.getOperand(1), Copy.getValue(1));
904 break;
905 }
906 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
907}
908
Chris Lattner86638b92006-01-31 05:05:52 +0000909
Chris Lattner7c90f732006-02-05 05:50:24 +0000910SDOperand SparcTargetLowering::
Chris Lattner4d55aca2005-12-18 01:20:35 +0000911LowerOperation(SDOperand Op, SelectionDAG &DAG) {
912 switch (Op.getOpcode()) {
913 default: assert(0 && "Should not custom lower this!");
Chris Lattner601fe382008-03-17 02:52:07 +0000914 // Frame & Return address. Currently unimplemented
915 case ISD::RETURNADDR: return SDOperand();
916 case ISD::FRAMEADDR: return SDOperand();
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000917 case ISD::GlobalTLSAddress:
918 assert(0 && "TLS not implemented for Sparc.");
Chris Lattner601fe382008-03-17 02:52:07 +0000919 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
920 case ISD::ConstantPool: return LowerCONSTANTPOOL(Op, DAG);
921 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
922 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
923 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
924 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
925 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
926 case ISD::VAARG: return LowerVAARG(Op, DAG);
927 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
928 case ISD::RET: return LowerRET(Op, DAG);
Chris Lattnere3572462005-12-18 02:10:39 +0000929 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000930}
931
Chris Lattner33084492005-12-18 08:13:54 +0000932MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +0000933SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
934 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000935 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
Chris Lattner33084492005-12-18 08:13:54 +0000936 unsigned BROpcode;
Chris Lattner7a4d2912006-01-31 06:56:30 +0000937 unsigned CC;
Chris Lattner33084492005-12-18 08:13:54 +0000938 // Figure out the conditional branch opcode to use for this select_cc.
939 switch (MI->getOpcode()) {
940 default: assert(0 && "Unknown SELECT_CC!");
Chris Lattner7c90f732006-02-05 05:50:24 +0000941 case SP::SELECT_CC_Int_ICC:
942 case SP::SELECT_CC_FP_ICC:
943 case SP::SELECT_CC_DFP_ICC:
944 BROpcode = SP::BCOND;
Chris Lattnerc03468b2006-01-31 17:20:06 +0000945 break;
Chris Lattner7c90f732006-02-05 05:50:24 +0000946 case SP::SELECT_CC_Int_FCC:
947 case SP::SELECT_CC_FP_FCC:
948 case SP::SELECT_CC_DFP_FCC:
949 BROpcode = SP::FBCOND;
Chris Lattner33084492005-12-18 08:13:54 +0000950 break;
951 }
Chris Lattner7a4d2912006-01-31 06:56:30 +0000952
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000953 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Chris Lattner33084492005-12-18 08:13:54 +0000954
955 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
956 // control-flow pattern. The incoming instruction knows the destination vreg
957 // to set, the condition code register to branch on, the true/false values to
958 // select between, and a branch opcode to use.
959 const BasicBlock *LLVM_BB = BB->getBasicBlock();
960 ilist<MachineBasicBlock>::iterator It = BB;
961 ++It;
962
963 // thisMBB:
964 // ...
965 // TrueVal = ...
966 // [f]bCC copy1MBB
967 // fallthrough --> copy0MBB
968 MachineBasicBlock *thisMBB = BB;
969 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
970 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000971 BuildMI(BB, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Chris Lattner33084492005-12-18 08:13:54 +0000972 MachineFunction *F = BB->getParent();
973 F->getBasicBlockList().insert(It, copy0MBB);
974 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +0000975 // Update machine-CFG edges by first adding all successors of the current
976 // block to the new block which will contain the Phi node for the select.
977 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
978 e = BB->succ_end(); i != e; ++i)
979 sinkMBB->addSuccessor(*i);
980 // Next, remove all successors of the current block, and add the true
981 // and fallthrough blocks as its successors.
982 while(!BB->succ_empty())
983 BB->removeSuccessor(BB->succ_begin());
Chris Lattner33084492005-12-18 08:13:54 +0000984 BB->addSuccessor(copy0MBB);
985 BB->addSuccessor(sinkMBB);
986
987 // copy0MBB:
988 // %FalseValue = ...
989 // # fallthrough to sinkMBB
990 BB = copy0MBB;
991
992 // Update machine-CFG edges
993 BB->addSuccessor(sinkMBB);
994
995 // sinkMBB:
996 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
997 // ...
998 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +0000999 BuildMI(BB, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner33084492005-12-18 08:13:54 +00001000 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1001 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1002
1003 delete MI; // The pseudo instruction is gone now.
1004 return BB;
1005}
1006
Chris Lattner6c18b102005-12-17 07:47:01 +00001007//===----------------------------------------------------------------------===//
1008// Instruction Selector Implementation
1009//===----------------------------------------------------------------------===//
1010
1011//===--------------------------------------------------------------------===//
Chris Lattner7c90f732006-02-05 05:50:24 +00001012/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
Chris Lattner6c18b102005-12-17 07:47:01 +00001013/// instructions for SelectionDAG operations.
1014///
1015namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +00001016class SparcDAGToDAGISel : public SelectionDAGISel {
1017 SparcTargetLowering Lowering;
Chris Lattner76afdc92006-01-30 05:35:57 +00001018
1019 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
1020 /// make the right decision when generating code for different targets.
Chris Lattner7c90f732006-02-05 05:50:24 +00001021 const SparcSubtarget &Subtarget;
Chris Lattner6c18b102005-12-17 07:47:01 +00001022public:
Chris Lattner7c90f732006-02-05 05:50:24 +00001023 SparcDAGToDAGISel(TargetMachine &TM)
1024 : SelectionDAGISel(Lowering), Lowering(TM),
1025 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
Chris Lattner76afdc92006-01-30 05:35:57 +00001026 }
Chris Lattner6c18b102005-12-17 07:47:01 +00001027
Evan Cheng9ade2182006-08-26 05:34:46 +00001028 SDNode *Select(SDOperand Op);
Chris Lattner6c18b102005-12-17 07:47:01 +00001029
Chris Lattnerbc83fd92005-12-17 20:04:49 +00001030 // Complex Pattern Selectors.
Evan Cheng0d538262006-11-08 20:34:28 +00001031 bool SelectADDRrr(SDOperand Op, SDOperand N, SDOperand &R1, SDOperand &R2);
1032 bool SelectADDRri(SDOperand Op, SDOperand N, SDOperand &Base,
1033 SDOperand &Offset);
Chris Lattnerbc83fd92005-12-17 20:04:49 +00001034
Chris Lattner6c18b102005-12-17 07:47:01 +00001035 /// InstructionSelectBasicBlock - This callback is invoked by
1036 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
1037 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
1038
1039 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +00001040 return "SPARC DAG->DAG Pattern Instruction Selection";
Chris Lattner6c18b102005-12-17 07:47:01 +00001041 }
1042
1043 // Include the pieces autogenerated from the target description.
Chris Lattner7c90f732006-02-05 05:50:24 +00001044#include "SparcGenDAGISel.inc"
Chris Lattner6c18b102005-12-17 07:47:01 +00001045};
1046} // end anonymous namespace
1047
1048/// InstructionSelectBasicBlock - This callback is invoked by
1049/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7c90f732006-02-05 05:50:24 +00001050void SparcDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +00001051 DEBUG(BB->dump());
1052
1053 // Select target instructions for the DAG.
Evan Cheng900c8262006-02-05 06:51:51 +00001054 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattner6c18b102005-12-17 07:47:01 +00001055 DAG.RemoveDeadNodes();
1056
1057 // Emit machine code to BB.
1058 ScheduleAndEmitDAG(DAG);
1059}
1060
Evan Cheng0d538262006-11-08 20:34:28 +00001061bool SparcDAGToDAGISel::SelectADDRri(SDOperand Op, SDOperand Addr,
1062 SDOperand &Base, SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +00001063 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1064 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001065 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1066 return true;
1067 }
Chris Lattnerad7a3e62006-02-10 07:35:42 +00001068 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1069 Addr.getOpcode() == ISD::TargetGlobalAddress)
1070 return false; // direct calls.
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001071
1072 if (Addr.getOpcode() == ISD::ADD) {
1073 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
1074 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +00001075 if (FrameIndexSDNode *FIN =
1076 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001077 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +00001078 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001079 } else {
Chris Lattnerc26017a2006-02-05 08:35:50 +00001080 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001081 }
1082 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
1083 return true;
1084 }
1085 }
Chris Lattner7c90f732006-02-05 05:50:24 +00001086 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +00001087 Base = Addr.getOperand(1);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001088 Offset = Addr.getOperand(0).getOperand(0);
1089 return true;
1090 }
Chris Lattner7c90f732006-02-05 05:50:24 +00001091 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +00001092 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001093 Offset = Addr.getOperand(1).getOperand(0);
1094 return true;
1095 }
1096 }
Chris Lattnerc26017a2006-02-05 08:35:50 +00001097 Base = Addr;
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001098 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1099 return true;
1100}
1101
Evan Cheng0d538262006-11-08 20:34:28 +00001102bool SparcDAGToDAGISel::SelectADDRrr(SDOperand Op, SDOperand Addr,
1103 SDOperand &R1, SDOperand &R2) {
Chris Lattnerad7a3e62006-02-10 07:35:42 +00001104 if (Addr.getOpcode() == ISD::FrameIndex) return false;
1105 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1106 Addr.getOpcode() == ISD::TargetGlobalAddress)
1107 return false; // direct calls.
1108
Chris Lattner9034b882005-12-17 21:25:27 +00001109 if (Addr.getOpcode() == ISD::ADD) {
1110 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
1111 Predicate_simm13(Addr.getOperand(1).Val))
1112 return false; // Let the reg+imm pattern catch this!
Chris Lattner7c90f732006-02-05 05:50:24 +00001113 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
1114 Addr.getOperand(1).getOpcode() == SPISD::Lo)
Chris Lattnere1389ad2005-12-18 02:27:00 +00001115 return false; // Let the reg+imm pattern catch this!
Chris Lattnerc26017a2006-02-05 08:35:50 +00001116 R1 = Addr.getOperand(0);
1117 R2 = Addr.getOperand(1);
Chris Lattner9034b882005-12-17 21:25:27 +00001118 return true;
1119 }
1120
Chris Lattnerc26017a2006-02-05 08:35:50 +00001121 R1 = Addr;
Chris Lattner7c90f732006-02-05 05:50:24 +00001122 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattnerbc83fd92005-12-17 20:04:49 +00001123 return true;
1124}
1125
Evan Cheng9ade2182006-08-26 05:34:46 +00001126SDNode *SparcDAGToDAGISel::Select(SDOperand Op) {
Chris Lattner6c18b102005-12-17 07:47:01 +00001127 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +00001128 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng9ade2182006-08-26 05:34:46 +00001129 N->getOpcode() < SPISD::FIRST_NUMBER)
Evan Cheng64a752f2006-08-11 09:08:15 +00001130 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001131
Chris Lattner6c18b102005-12-17 07:47:01 +00001132 switch (N->getOpcode()) {
1133 default: break;
Chris Lattner7087e572005-12-17 22:39:19 +00001134 case ISD::SDIV:
1135 case ISD::UDIV: {
1136 // FIXME: should use a custom expander to expose the SRA to the dag.
Evan Cheng6da2f322006-08-26 01:07:58 +00001137 SDOperand DivLHS = N->getOperand(0);
1138 SDOperand DivRHS = N->getOperand(1);
1139 AddToISelQueue(DivLHS);
1140 AddToISelQueue(DivRHS);
Chris Lattner7087e572005-12-17 22:39:19 +00001141
1142 // Set the Y register to the high-part.
1143 SDOperand TopPart;
1144 if (N->getOpcode() == ISD::SDIV) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001145 TopPart = SDOperand(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS,
1146 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +00001147 } else {
Chris Lattner7c90f732006-02-05 05:50:24 +00001148 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner7087e572005-12-17 22:39:19 +00001149 }
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001150 TopPart = SDOperand(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart,
1151 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +00001152
1153 // FIXME: Handle div by immediate.
Chris Lattner7c90f732006-02-05 05:50:24 +00001154 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Evan Cheng23329f52006-08-16 07:30:09 +00001155 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng95514ba2006-08-26 08:00:10 +00001156 TopPart);
Chris Lattner7087e572005-12-17 22:39:19 +00001157 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001158 case ISD::MULHU:
1159 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +00001160 // FIXME: Handle mul by immediate.
Evan Cheng6da2f322006-08-26 01:07:58 +00001161 SDOperand MulLHS = N->getOperand(0);
1162 SDOperand MulRHS = N->getOperand(1);
1163 AddToISelQueue(MulLHS);
1164 AddToISelQueue(MulRHS);
Chris Lattner7c90f732006-02-05 05:50:24 +00001165 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001166 SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
Chris Lattnerad7a3e62006-02-10 07:35:42 +00001167 MulLHS, MulRHS);
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001168 // The high part is in the Y register.
Evan Cheng95514ba2006-08-26 08:00:10 +00001169 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDOperand(Mul, 1));
Evan Cheng64a752f2006-08-11 09:08:15 +00001170 return NULL;
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001171 }
Chris Lattner6c18b102005-12-17 07:47:01 +00001172 }
1173
Evan Cheng9ade2182006-08-26 05:34:46 +00001174 return SelectCode(Op);
Chris Lattner6c18b102005-12-17 07:47:01 +00001175}
1176
1177
Chris Lattner7c90f732006-02-05 05:50:24 +00001178/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner4dcfaac2006-01-26 07:22:22 +00001179/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +00001180///
Chris Lattner7c90f732006-02-05 05:50:24 +00001181FunctionPass *llvm::createSparcISelDag(TargetMachine &TM) {
1182 return new SparcDAGToDAGISel(TM);
Chris Lattner6c18b102005-12-17 07:47:01 +00001183}