blob: b2f3f18ec2f9f725d43fcbbaab9b46741087ad70 [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
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//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/Function.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000027#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner259e97c2006-01-31 19:43:35 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030using namespace llvm;
31
32// FIXME: temporary.
33#include "llvm/Support/CommandLine.h"
34static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
36
37X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000039 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
41
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042 // Set up the TargetLowering object.
43
44 // X86 is weird, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
47 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000048 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000050 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattnera54aa942006-01-29 06:26:08 +000051
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58 // operation.
59 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
61 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000062
63 if (X86ScalarSSE)
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
66 else
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70 // this operation.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000073 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000074 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000075 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000076 else {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
79 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080
Evan Cheng6dab0532006-01-30 08:02:57 +000081 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82 // isn't legal.
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
85
Evan Cheng02568ff2006-01-30 22:13:22 +000086 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
90
91 if (X86ScalarSSE) {
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
93 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +000095 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096 }
97
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
99 // conversion.
100 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
102 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
103
Evan Cheng45af8fd2006-02-18 07:26:17 +0000104 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000105 // Expand FP_TO_UINT into a select.
106 // FIXME: We would like to use a Custom expander here eventually to do
107 // the optimal thing for SSE vs. the default expansion in the legalizer.
108 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
109 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000110 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
112
Evan Cheng02568ff2006-01-30 22:13:22 +0000113 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
114 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000115
Evan Cheng5298bcc2006-02-17 07:01:52 +0000116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000117 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
118 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000119 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
120 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
125 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
126 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
127 setOperationAction(ISD::FREM , MVT::f64 , Expand);
128 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
129 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
130 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
132 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
133 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
136 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000137 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000138 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000139
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000140 // These should be promoted to a larger select which is supported.
141 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
142 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000143
144 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000145 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
146 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
147 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
148 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
149 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
150 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
151 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
152 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
153 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000154 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000155 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000156 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000157 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000158 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000159 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000160 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000161 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
162 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
163 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000164 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000165 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
166 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
Chris Lattnerf73bae12005-11-29 06:16:21 +0000168 // We don't have line number support yet.
169 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000170 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000171 // FIXME - use subtarget debug flags
172 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
173 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000174
Nate Begemanacc398c2006-01-25 18:21:52 +0000175 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
176 setOperationAction(ISD::VASTART , MVT::Other, Custom);
177
178 // Use the default implementation.
179 setOperationAction(ISD::VAARG , MVT::Other, Expand);
180 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
181 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000182 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
183 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
184 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000185
Chris Lattner9601a862006-03-05 05:08:37 +0000186 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
187 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
188
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189 if (X86ScalarSSE) {
190 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000191 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
192 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000193
194 // SSE has no load+extend ops
195 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
196 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
197
Evan Cheng223547a2006-01-31 22:28:30 +0000198 // Use ANDPD to simulate FABS.
199 setOperationAction(ISD::FABS , MVT::f64, Custom);
200 setOperationAction(ISD::FABS , MVT::f32, Custom);
201
202 // Use XORP to simulate FNEG.
203 setOperationAction(ISD::FNEG , MVT::f64, Custom);
204 setOperationAction(ISD::FNEG , MVT::f32, Custom);
205
Evan Chengd25e9e82006-02-02 00:28:23 +0000206 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 setOperationAction(ISD::FSIN , MVT::f64, Expand);
208 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209 setOperationAction(ISD::FREM , MVT::f64, Expand);
210 setOperationAction(ISD::FSIN , MVT::f32, Expand);
211 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000212 setOperationAction(ISD::FREM , MVT::f32, Expand);
213
Chris Lattnera54aa942006-01-29 06:26:08 +0000214 // Expand FP immediates into loads from the stack, except for the special
215 // cases we handle.
216 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
217 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 addLegalFPImmediate(+0.0); // xorps / xorpd
219 } else {
220 // Set up the FP register classes.
221 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000222
223 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 if (!UnsafeFPMath) {
226 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
227 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
228 }
229
Chris Lattnera54aa942006-01-29 06:26:08 +0000230 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // FLD0
232 addLegalFPImmediate(+1.0); // FLD1
233 addLegalFPImmediate(-0.0); // FLD0/FCHS
234 addLegalFPImmediate(-1.0); // FLD1/FCHS
235 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000236
Evan Chengd30bf012006-03-01 01:11:20 +0000237 // First set operation action for all vector types to expand. Then we
238 // will selectively turn on ones that can be effectively codegen'd.
239 for (unsigned VT = (unsigned)MVT::Vector + 1;
240 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
241 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
242 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
243 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
244 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
245 }
246
Evan Cheng470a6ad2006-02-22 02:26:30 +0000247 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
248 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
249 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
250 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
251
Evan Chengd30bf012006-03-01 01:11:20 +0000252 // FIXME: add MMX packed arithmetics
Evan Cheng470a6ad2006-02-22 02:26:30 +0000253 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
254 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
255 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
256 }
257
258 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
259 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
260
Evan Chengd30bf012006-03-01 01:11:20 +0000261 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
262 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
263 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
264 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000265 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
266 }
267
268 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
269 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
270 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
271 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
272 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
273 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
274
275
Evan Chengd30bf012006-03-01 01:11:20 +0000276 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
277 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
278 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
279 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000280 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
281 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
282 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
283 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
284 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
285 }
286
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287 computeRegisterProperties();
288
Evan Cheng87ed7162006-02-14 08:25:08 +0000289 // FIXME: These should be based on subtarget info. Plus, the values should
290 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000291 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
292 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
293 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294 allowUnalignedMemoryAccesses = true; // x86 supports it!
295}
296
297std::vector<SDOperand>
298X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
299 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
300 return LowerFastCCArguments(F, DAG);
301 return LowerCCCArguments(F, DAG);
302}
303
304std::pair<SDOperand, SDOperand>
305X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
306 bool isVarArg, unsigned CallingConv,
307 bool isTailCall,
308 SDOperand Callee, ArgListTy &Args,
309 SelectionDAG &DAG) {
310 assert((!isVarArg || CallingConv == CallingConv::C) &&
311 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000312
313 // If the callee is a GlobalAddress node (quite common, every direct call is)
314 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
315 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
316 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000317 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
318 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000319
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320 if (CallingConv == CallingConv::Fast && EnableFastCC)
321 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
322 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
323}
324
325//===----------------------------------------------------------------------===//
326// C Calling Convention implementation
327//===----------------------------------------------------------------------===//
328
329std::vector<SDOperand>
330X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
331 std::vector<SDOperand> ArgValues;
332
333 MachineFunction &MF = DAG.getMachineFunction();
334 MachineFrameInfo *MFI = MF.getFrameInfo();
335
336 // Add DAG nodes to load the arguments... On entry to a function on the X86,
337 // the stack frame looks like this:
338 //
339 // [ESP] -- return address
340 // [ESP + 4] -- first argument (leftmost lexically)
341 // [ESP + 8] -- second argument, if first argument is four bytes in size
342 // ...
343 //
344 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
345 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
346 MVT::ValueType ObjectVT = getValueType(I->getType());
347 unsigned ArgIncrement = 4;
348 unsigned ObjSize;
349 switch (ObjectVT) {
350 default: assert(0 && "Unhandled argument type!");
351 case MVT::i1:
352 case MVT::i8: ObjSize = 1; break;
353 case MVT::i16: ObjSize = 2; break;
354 case MVT::i32: ObjSize = 4; break;
355 case MVT::i64: ObjSize = ArgIncrement = 8; break;
356 case MVT::f32: ObjSize = 4; break;
357 case MVT::f64: ObjSize = ArgIncrement = 8; break;
358 }
359 // Create the frame index object for this incoming parameter...
360 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
361
362 // Create the SelectionDAG nodes corresponding to a load from this parameter
363 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
364
365 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
366 // dead loads.
367 SDOperand ArgValue;
368 if (!I->use_empty())
369 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
370 DAG.getSrcValue(NULL));
371 else {
372 if (MVT::isInteger(ObjectVT))
373 ArgValue = DAG.getConstant(0, ObjectVT);
374 else
375 ArgValue = DAG.getConstantFP(0, ObjectVT);
376 }
377 ArgValues.push_back(ArgValue);
378
379 ArgOffset += ArgIncrement; // Move on to the next argument...
380 }
381
382 // If the function takes variable number of arguments, make a frame index for
383 // the start of the first vararg value... for expansion of llvm.va_start.
384 if (F.isVarArg())
385 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
386 ReturnAddrIndex = 0; // No return address slot generated yet.
387 BytesToPopOnReturn = 0; // Callee pops nothing.
388 BytesCallerReserves = ArgOffset;
389
390 // Finally, inform the code generator which regs we return values in.
391 switch (getValueType(F.getReturnType())) {
392 default: assert(0 && "Unknown type!");
393 case MVT::isVoid: break;
394 case MVT::i1:
395 case MVT::i8:
396 case MVT::i16:
397 case MVT::i32:
398 MF.addLiveOut(X86::EAX);
399 break;
400 case MVT::i64:
401 MF.addLiveOut(X86::EAX);
402 MF.addLiveOut(X86::EDX);
403 break;
404 case MVT::f32:
405 case MVT::f64:
406 MF.addLiveOut(X86::ST0);
407 break;
408 }
409 return ArgValues;
410}
411
412std::pair<SDOperand, SDOperand>
413X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
414 bool isVarArg, bool isTailCall,
415 SDOperand Callee, ArgListTy &Args,
416 SelectionDAG &DAG) {
417 // Count how many bytes are to be pushed on the stack.
418 unsigned NumBytes = 0;
419
420 if (Args.empty()) {
421 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000422 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000423 } else {
424 for (unsigned i = 0, e = Args.size(); i != e; ++i)
425 switch (getValueType(Args[i].second)) {
426 default: assert(0 && "Unknown value type!");
427 case MVT::i1:
428 case MVT::i8:
429 case MVT::i16:
430 case MVT::i32:
431 case MVT::f32:
432 NumBytes += 4;
433 break;
434 case MVT::i64:
435 case MVT::f64:
436 NumBytes += 8;
437 break;
438 }
439
Chris Lattner94dd2922006-02-13 09:00:43 +0000440 Chain = DAG.getCALLSEQ_START(Chain,
441 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000442
443 // Arguments go on the stack in reverse order, as specified by the ABI.
444 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000445 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000446 std::vector<SDOperand> Stores;
447
448 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
449 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
450 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
451
452 switch (getValueType(Args[i].second)) {
453 default: assert(0 && "Unexpected ValueType for argument!");
454 case MVT::i1:
455 case MVT::i8:
456 case MVT::i16:
457 // Promote the integer to 32 bits. If the input type is signed use a
458 // sign extend, otherwise use a zero extend.
459 if (Args[i].second->isSigned())
460 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
461 else
462 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
463
464 // FALL THROUGH
465 case MVT::i32:
466 case MVT::f32:
467 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
468 Args[i].first, PtrOff,
469 DAG.getSrcValue(NULL)));
470 ArgOffset += 4;
471 break;
472 case MVT::i64:
473 case MVT::f64:
474 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
475 Args[i].first, PtrOff,
476 DAG.getSrcValue(NULL)));
477 ArgOffset += 8;
478 break;
479 }
480 }
481 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
482 }
483
484 std::vector<MVT::ValueType> RetVals;
485 MVT::ValueType RetTyVT = getValueType(RetTy);
486 RetVals.push_back(MVT::Other);
487
488 // The result values produced have to be legal. Promote the result.
489 switch (RetTyVT) {
490 case MVT::isVoid: break;
491 default:
492 RetVals.push_back(RetTyVT);
493 break;
494 case MVT::i1:
495 case MVT::i8:
496 case MVT::i16:
497 RetVals.push_back(MVT::i32);
498 break;
499 case MVT::f32:
500 if (X86ScalarSSE)
501 RetVals.push_back(MVT::f32);
502 else
503 RetVals.push_back(MVT::f64);
504 break;
505 case MVT::i64:
506 RetVals.push_back(MVT::i32);
507 RetVals.push_back(MVT::i32);
508 break;
509 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000510
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000511 std::vector<MVT::ValueType> NodeTys;
512 NodeTys.push_back(MVT::Other); // Returns a chain
513 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
514 std::vector<SDOperand> Ops;
515 Ops.push_back(Chain);
516 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000517
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000518 // FIXME: Do not generate X86ISD::TAILCALL for now.
519 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
520 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000521
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000522 NodeTys.clear();
523 NodeTys.push_back(MVT::Other); // Returns a chain
524 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
525 Ops.clear();
526 Ops.push_back(Chain);
527 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
528 Ops.push_back(DAG.getConstant(0, getPointerTy()));
529 Ops.push_back(InFlag);
530 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
531 InFlag = Chain.getValue(1);
532
533 SDOperand RetVal;
534 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000535 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000536 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000537 case MVT::i1:
538 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000539 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
540 Chain = RetVal.getValue(1);
541 if (RetTyVT == MVT::i1)
542 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
543 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000544 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000545 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
546 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000547 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000548 case MVT::i32:
549 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
550 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000551 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000552 case MVT::i64: {
553 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
554 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
555 Lo.getValue(2));
556 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
557 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000558 break;
559 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000560 case MVT::f32:
561 case MVT::f64: {
562 std::vector<MVT::ValueType> Tys;
563 Tys.push_back(MVT::f64);
564 Tys.push_back(MVT::Other);
565 Tys.push_back(MVT::Flag);
566 std::vector<SDOperand> Ops;
567 Ops.push_back(Chain);
568 Ops.push_back(InFlag);
569 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
570 Chain = RetVal.getValue(1);
571 InFlag = RetVal.getValue(2);
572 if (X86ScalarSSE) {
573 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
574 // shouldn't be necessary except that RFP cannot be live across
575 // multiple blocks. When stackifier is fixed, they can be uncoupled.
576 MachineFunction &MF = DAG.getMachineFunction();
577 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
578 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
579 Tys.clear();
580 Tys.push_back(MVT::Other);
581 Ops.clear();
582 Ops.push_back(Chain);
583 Ops.push_back(RetVal);
584 Ops.push_back(StackSlot);
585 Ops.push_back(DAG.getValueType(RetTyVT));
586 Ops.push_back(InFlag);
587 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
588 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
589 DAG.getSrcValue(NULL));
590 Chain = RetVal.getValue(1);
591 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000592
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000593 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
594 // FIXME: we would really like to remember that this FP_ROUND
595 // operation is okay to eliminate if we allow excess FP precision.
596 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
597 break;
598 }
599 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000600 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000601
602 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000603}
604
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000605//===----------------------------------------------------------------------===//
606// Fast Calling Convention implementation
607//===----------------------------------------------------------------------===//
608//
609// The X86 'fast' calling convention passes up to two integer arguments in
610// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
611// and requires that the callee pop its arguments off the stack (allowing proper
612// tail calls), and has the same return value conventions as C calling convs.
613//
614// This calling convention always arranges for the callee pop value to be 8n+4
615// bytes, which is needed for tail recursion elimination and stack alignment
616// reasons.
617//
618// Note that this can be enhanced in the future to pass fp vals in registers
619// (when we have a global fp allocator) and do other tricks.
620//
621
622/// AddLiveIn - This helper function adds the specified physical register to the
623/// MachineFunction as a live in value. It also creates a corresponding virtual
624/// register for it.
625static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
626 TargetRegisterClass *RC) {
627 assert(RC->contains(PReg) && "Not the correct regclass!");
628 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
629 MF.addLiveIn(PReg, VReg);
630 return VReg;
631}
632
633
634std::vector<SDOperand>
635X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
636 std::vector<SDOperand> ArgValues;
637
638 MachineFunction &MF = DAG.getMachineFunction();
639 MachineFrameInfo *MFI = MF.getFrameInfo();
640
641 // Add DAG nodes to load the arguments... On entry to a function the stack
642 // frame looks like this:
643 //
644 // [ESP] -- return address
645 // [ESP + 4] -- first nonreg argument (leftmost lexically)
646 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
647 // ...
648 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
649
650 // Keep track of the number of integer regs passed so far. This can be either
651 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
652 // used).
653 unsigned NumIntRegs = 0;
654
655 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
656 MVT::ValueType ObjectVT = getValueType(I->getType());
657 unsigned ArgIncrement = 4;
658 unsigned ObjSize = 0;
659 SDOperand ArgValue;
660
661 switch (ObjectVT) {
662 default: assert(0 && "Unhandled argument type!");
663 case MVT::i1:
664 case MVT::i8:
665 if (NumIntRegs < 2) {
666 if (!I->use_empty()) {
667 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
668 X86::R8RegisterClass);
669 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
670 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000671 if (ObjectVT == MVT::i1)
672 // FIXME: Should insert a assertzext here.
673 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000674 }
675 ++NumIntRegs;
676 break;
677 }
678
679 ObjSize = 1;
680 break;
681 case MVT::i16:
682 if (NumIntRegs < 2) {
683 if (!I->use_empty()) {
684 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
685 X86::R16RegisterClass);
686 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
687 DAG.setRoot(ArgValue.getValue(1));
688 }
689 ++NumIntRegs;
690 break;
691 }
692 ObjSize = 2;
693 break;
694 case MVT::i32:
695 if (NumIntRegs < 2) {
696 if (!I->use_empty()) {
697 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
698 X86::R32RegisterClass);
699 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
700 DAG.setRoot(ArgValue.getValue(1));
701 }
702 ++NumIntRegs;
703 break;
704 }
705 ObjSize = 4;
706 break;
707 case MVT::i64:
708 if (NumIntRegs == 0) {
709 if (!I->use_empty()) {
710 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
711 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
712
713 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
714 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
715 DAG.setRoot(Hi.getValue(1));
716
717 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
718 }
719 NumIntRegs = 2;
720 break;
721 } else if (NumIntRegs == 1) {
722 if (!I->use_empty()) {
723 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
724 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
725 DAG.setRoot(Low.getValue(1));
726
727 // Load the high part from memory.
728 // Create the frame index object for this incoming parameter...
729 int FI = MFI->CreateFixedObject(4, ArgOffset);
730 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
731 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
732 DAG.getSrcValue(NULL));
733 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
734 }
735 ArgOffset += 4;
736 NumIntRegs = 2;
737 break;
738 }
739 ObjSize = ArgIncrement = 8;
740 break;
741 case MVT::f32: ObjSize = 4; break;
742 case MVT::f64: ObjSize = ArgIncrement = 8; break;
743 }
744
745 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
746 // dead loads.
747 if (ObjSize && !I->use_empty()) {
748 // Create the frame index object for this incoming parameter...
749 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
750
751 // Create the SelectionDAG nodes corresponding to a load from this
752 // parameter.
753 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
754
755 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
756 DAG.getSrcValue(NULL));
757 } else if (ArgValue.Val == 0) {
758 if (MVT::isInteger(ObjectVT))
759 ArgValue = DAG.getConstant(0, ObjectVT);
760 else
761 ArgValue = DAG.getConstantFP(0, ObjectVT);
762 }
763 ArgValues.push_back(ArgValue);
764
765 if (ObjSize)
766 ArgOffset += ArgIncrement; // Move on to the next argument.
767 }
768
769 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
770 // arguments and the arguments after the retaddr has been pushed are aligned.
771 if ((ArgOffset & 7) == 0)
772 ArgOffset += 4;
773
774 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
775 ReturnAddrIndex = 0; // No return address slot generated yet.
776 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
777 BytesCallerReserves = 0;
778
779 // Finally, inform the code generator which regs we return values in.
780 switch (getValueType(F.getReturnType())) {
781 default: assert(0 && "Unknown type!");
782 case MVT::isVoid: break;
783 case MVT::i1:
784 case MVT::i8:
785 case MVT::i16:
786 case MVT::i32:
787 MF.addLiveOut(X86::EAX);
788 break;
789 case MVT::i64:
790 MF.addLiveOut(X86::EAX);
791 MF.addLiveOut(X86::EDX);
792 break;
793 case MVT::f32:
794 case MVT::f64:
795 MF.addLiveOut(X86::ST0);
796 break;
797 }
798 return ArgValues;
799}
800
801std::pair<SDOperand, SDOperand>
802X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
803 bool isTailCall, SDOperand Callee,
804 ArgListTy &Args, SelectionDAG &DAG) {
805 // Count how many bytes are to be pushed on the stack.
806 unsigned NumBytes = 0;
807
808 // Keep track of the number of integer regs passed so far. This can be either
809 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
810 // used).
811 unsigned NumIntRegs = 0;
812
813 for (unsigned i = 0, e = Args.size(); i != e; ++i)
814 switch (getValueType(Args[i].second)) {
815 default: assert(0 && "Unknown value type!");
816 case MVT::i1:
817 case MVT::i8:
818 case MVT::i16:
819 case MVT::i32:
820 if (NumIntRegs < 2) {
821 ++NumIntRegs;
822 break;
823 }
824 // fall through
825 case MVT::f32:
826 NumBytes += 4;
827 break;
828 case MVT::i64:
829 if (NumIntRegs == 0) {
830 NumIntRegs = 2;
831 break;
832 } else if (NumIntRegs == 1) {
833 NumIntRegs = 2;
834 NumBytes += 4;
835 break;
836 }
837
838 // fall through
839 case MVT::f64:
840 NumBytes += 8;
841 break;
842 }
843
844 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
845 // arguments and the arguments after the retaddr has been pushed are aligned.
846 if ((NumBytes & 7) == 0)
847 NumBytes += 4;
848
Chris Lattner94dd2922006-02-13 09:00:43 +0000849 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000850
851 // Arguments go on the stack in reverse order, as specified by the ABI.
852 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000853 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000854 NumIntRegs = 0;
855 std::vector<SDOperand> Stores;
856 std::vector<SDOperand> RegValuesToPass;
857 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
858 switch (getValueType(Args[i].second)) {
859 default: assert(0 && "Unexpected ValueType for argument!");
860 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000861 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
862 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000863 case MVT::i8:
864 case MVT::i16:
865 case MVT::i32:
866 if (NumIntRegs < 2) {
867 RegValuesToPass.push_back(Args[i].first);
868 ++NumIntRegs;
869 break;
870 }
871 // Fall through
872 case MVT::f32: {
873 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
874 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
875 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
876 Args[i].first, PtrOff,
877 DAG.getSrcValue(NULL)));
878 ArgOffset += 4;
879 break;
880 }
881 case MVT::i64:
882 if (NumIntRegs < 2) { // Can pass part of it in regs?
883 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
884 Args[i].first, DAG.getConstant(1, MVT::i32));
885 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
886 Args[i].first, DAG.getConstant(0, MVT::i32));
887 RegValuesToPass.push_back(Lo);
888 ++NumIntRegs;
889 if (NumIntRegs < 2) { // Pass both parts in regs?
890 RegValuesToPass.push_back(Hi);
891 ++NumIntRegs;
892 } else {
893 // Pass the high part in memory.
894 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
895 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
896 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
897 Hi, PtrOff, DAG.getSrcValue(NULL)));
898 ArgOffset += 4;
899 }
900 break;
901 }
902 // Fall through
903 case MVT::f64:
904 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
905 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
906 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
907 Args[i].first, PtrOff,
908 DAG.getSrcValue(NULL)));
909 ArgOffset += 8;
910 break;
911 }
912 }
913 if (!Stores.empty())
914 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
915
916 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
917 // arguments and the arguments after the retaddr has been pushed are aligned.
918 if ((ArgOffset & 7) == 0)
919 ArgOffset += 4;
920
921 std::vector<MVT::ValueType> RetVals;
922 MVT::ValueType RetTyVT = getValueType(RetTy);
923
924 RetVals.push_back(MVT::Other);
925
926 // The result values produced have to be legal. Promote the result.
927 switch (RetTyVT) {
928 case MVT::isVoid: break;
929 default:
930 RetVals.push_back(RetTyVT);
931 break;
932 case MVT::i1:
933 case MVT::i8:
934 case MVT::i16:
935 RetVals.push_back(MVT::i32);
936 break;
937 case MVT::f32:
938 if (X86ScalarSSE)
939 RetVals.push_back(MVT::f32);
940 else
941 RetVals.push_back(MVT::f64);
942 break;
943 case MVT::i64:
944 RetVals.push_back(MVT::i32);
945 RetVals.push_back(MVT::i32);
946 break;
947 }
948
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000949 // Build a sequence of copy-to-reg nodes chained together with token chain
950 // and flag operands which copy the outgoing args into registers.
951 SDOperand InFlag;
952 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
953 unsigned CCReg;
954 SDOperand RegToPass = RegValuesToPass[i];
955 switch (RegToPass.getValueType()) {
956 default: assert(0 && "Bad thing to pass in regs");
957 case MVT::i8:
958 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000959 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000960 case MVT::i16:
961 CCReg = (i == 0) ? X86::AX : X86::DX;
962 break;
963 case MVT::i32:
964 CCReg = (i == 0) ? X86::EAX : X86::EDX;
965 break;
966 }
967
968 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
969 InFlag = Chain.getValue(1);
970 }
971
972 std::vector<MVT::ValueType> NodeTys;
973 NodeTys.push_back(MVT::Other); // Returns a chain
974 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
975 std::vector<SDOperand> Ops;
976 Ops.push_back(Chain);
977 Ops.push_back(Callee);
978 if (InFlag.Val)
979 Ops.push_back(InFlag);
980
981 // FIXME: Do not generate X86ISD::TAILCALL for now.
982 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
983 InFlag = Chain.getValue(1);
984
985 NodeTys.clear();
986 NodeTys.push_back(MVT::Other); // Returns a chain
987 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
988 Ops.clear();
989 Ops.push_back(Chain);
990 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
991 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
992 Ops.push_back(InFlag);
993 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
994 InFlag = Chain.getValue(1);
995
996 SDOperand RetVal;
997 if (RetTyVT != MVT::isVoid) {
998 switch (RetTyVT) {
999 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001000 case MVT::i1:
1001 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001002 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1003 Chain = RetVal.getValue(1);
1004 if (RetTyVT == MVT::i1)
1005 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1006 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001007 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001008 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1009 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001010 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001011 case MVT::i32:
1012 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1013 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001014 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001015 case MVT::i64: {
1016 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1017 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1018 Lo.getValue(2));
1019 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1020 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001021 break;
1022 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001023 case MVT::f32:
1024 case MVT::f64: {
1025 std::vector<MVT::ValueType> Tys;
1026 Tys.push_back(MVT::f64);
1027 Tys.push_back(MVT::Other);
1028 Tys.push_back(MVT::Flag);
1029 std::vector<SDOperand> Ops;
1030 Ops.push_back(Chain);
1031 Ops.push_back(InFlag);
1032 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1033 Chain = RetVal.getValue(1);
1034 InFlag = RetVal.getValue(2);
1035 if (X86ScalarSSE) {
1036 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1037 // shouldn't be necessary except that RFP cannot be live across
1038 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1039 MachineFunction &MF = DAG.getMachineFunction();
1040 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1041 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1042 Tys.clear();
1043 Tys.push_back(MVT::Other);
1044 Ops.clear();
1045 Ops.push_back(Chain);
1046 Ops.push_back(RetVal);
1047 Ops.push_back(StackSlot);
1048 Ops.push_back(DAG.getValueType(RetTyVT));
1049 Ops.push_back(InFlag);
1050 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1051 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1052 DAG.getSrcValue(NULL));
1053 Chain = RetVal.getValue(1);
1054 }
Evan Chengd9558e02006-01-06 00:43:03 +00001055
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001056 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1057 // FIXME: we would really like to remember that this FP_ROUND
1058 // operation is okay to eliminate if we allow excess FP precision.
1059 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1060 break;
1061 }
1062 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001063 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001064
1065 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001066}
1067
1068SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1069 if (ReturnAddrIndex == 0) {
1070 // Set up a frame object for the return address.
1071 MachineFunction &MF = DAG.getMachineFunction();
1072 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1073 }
1074
1075 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1076}
1077
1078
1079
1080std::pair<SDOperand, SDOperand> X86TargetLowering::
1081LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1082 SelectionDAG &DAG) {
1083 SDOperand Result;
1084 if (Depth) // Depths > 0 not supported yet!
1085 Result = DAG.getConstant(0, getPointerTy());
1086 else {
1087 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1088 if (!isFrameAddress)
1089 // Just load the return address
1090 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1091 DAG.getSrcValue(NULL));
1092 else
1093 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1094 DAG.getConstant(4, MVT::i32));
1095 }
1096 return std::make_pair(Result, Chain);
1097}
1098
Evan Cheng4a460802006-01-11 00:33:36 +00001099/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1100/// which corresponds to the condition code.
1101static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1102 switch (X86CC) {
1103 default: assert(0 && "Unknown X86 conditional code!");
1104 case X86ISD::COND_A: return X86::JA;
1105 case X86ISD::COND_AE: return X86::JAE;
1106 case X86ISD::COND_B: return X86::JB;
1107 case X86ISD::COND_BE: return X86::JBE;
1108 case X86ISD::COND_E: return X86::JE;
1109 case X86ISD::COND_G: return X86::JG;
1110 case X86ISD::COND_GE: return X86::JGE;
1111 case X86ISD::COND_L: return X86::JL;
1112 case X86ISD::COND_LE: return X86::JLE;
1113 case X86ISD::COND_NE: return X86::JNE;
1114 case X86ISD::COND_NO: return X86::JNO;
1115 case X86ISD::COND_NP: return X86::JNP;
1116 case X86ISD::COND_NS: return X86::JNS;
1117 case X86ISD::COND_O: return X86::JO;
1118 case X86ISD::COND_P: return X86::JP;
1119 case X86ISD::COND_S: return X86::JS;
1120 }
1121}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001122
Evan Cheng6dfa9992006-01-30 23:41:35 +00001123/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1124/// specific condition code. It returns a false if it cannot do a direct
1125/// translation. X86CC is the translated CondCode. Flip is set to true if the
1126/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001127static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1128 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001129 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001130 Flip = false;
1131 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001132 if (!isFP) {
1133 switch (SetCCOpcode) {
1134 default: break;
1135 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1136 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1137 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1138 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1139 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1140 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1141 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1142 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1143 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1144 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1145 }
1146 } else {
1147 // On a floating point condition, the flags are set as follows:
1148 // ZF PF CF op
1149 // 0 | 0 | 0 | X > Y
1150 // 0 | 0 | 1 | X < Y
1151 // 1 | 0 | 0 | X == Y
1152 // 1 | 1 | 1 | unordered
1153 switch (SetCCOpcode) {
1154 default: break;
1155 case ISD::SETUEQ:
1156 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001157 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001158 case ISD::SETOGT:
1159 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001160 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001161 case ISD::SETOGE:
1162 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001163 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001164 case ISD::SETULT:
1165 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001166 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001167 case ISD::SETULE:
1168 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1169 case ISD::SETONE:
1170 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1171 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1172 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1173 }
1174 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001175
1176 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001177}
1178
Evan Cheng4a460802006-01-11 00:33:36 +00001179/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1180/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001181/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001182static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001183 switch (X86CC) {
1184 default:
1185 return false;
1186 case X86ISD::COND_B:
1187 case X86ISD::COND_BE:
1188 case X86ISD::COND_E:
1189 case X86ISD::COND_P:
1190 case X86ISD::COND_A:
1191 case X86ISD::COND_AE:
1192 case X86ISD::COND_NE:
1193 case X86ISD::COND_NP:
1194 return true;
1195 }
1196}
1197
Evan Cheng4a460802006-01-11 00:33:36 +00001198MachineBasicBlock *
1199X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1200 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001201 switch (MI->getOpcode()) {
1202 default: assert(false && "Unexpected instr type to insert");
1203 case X86::CMOV_FR32:
1204 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001205 // To "insert" a SELECT_CC instruction, we actually have to insert the
1206 // diamond control-flow pattern. The incoming instruction knows the
1207 // destination vreg to set, the condition code register to branch on, the
1208 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001209 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1210 ilist<MachineBasicBlock>::iterator It = BB;
1211 ++It;
1212
1213 // thisMBB:
1214 // ...
1215 // TrueVal = ...
1216 // cmpTY ccX, r1, r2
1217 // bCC copy1MBB
1218 // fallthrough --> copy0MBB
1219 MachineBasicBlock *thisMBB = BB;
1220 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1221 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1222 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1223 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1224 MachineFunction *F = BB->getParent();
1225 F->getBasicBlockList().insert(It, copy0MBB);
1226 F->getBasicBlockList().insert(It, sinkMBB);
1227 // Update machine-CFG edges
1228 BB->addSuccessor(copy0MBB);
1229 BB->addSuccessor(sinkMBB);
1230
1231 // copy0MBB:
1232 // %FalseValue = ...
1233 // # fallthrough to sinkMBB
1234 BB = copy0MBB;
1235
1236 // Update machine-CFG edges
1237 BB->addSuccessor(sinkMBB);
1238
1239 // sinkMBB:
1240 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1241 // ...
1242 BB = sinkMBB;
1243 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1244 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1245 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001246
Evan Cheng0cc39452006-01-16 21:21:29 +00001247 delete MI; // The pseudo instruction is gone now.
1248 return BB;
1249 }
Evan Cheng4a460802006-01-11 00:33:36 +00001250
Evan Cheng0cc39452006-01-16 21:21:29 +00001251 case X86::FP_TO_INT16_IN_MEM:
1252 case X86::FP_TO_INT32_IN_MEM:
1253 case X86::FP_TO_INT64_IN_MEM: {
1254 // Change the floating point control register to use "round towards zero"
1255 // mode when truncating to an integer value.
1256 MachineFunction *F = BB->getParent();
1257 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1258 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1259
1260 // Load the old value of the high byte of the control word...
1261 unsigned OldCW =
1262 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1263 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1264
1265 // Set the high part to be round to zero...
1266 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1267
1268 // Reload the modified control word now...
1269 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1270
1271 // Restore the memory image of control word to original value
1272 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1273
1274 // Get the X86 opcode to use.
1275 unsigned Opc;
1276 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001277 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001278 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1279 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1280 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1281 }
1282
1283 X86AddressMode AM;
1284 MachineOperand &Op = MI->getOperand(0);
1285 if (Op.isRegister()) {
1286 AM.BaseType = X86AddressMode::RegBase;
1287 AM.Base.Reg = Op.getReg();
1288 } else {
1289 AM.BaseType = X86AddressMode::FrameIndexBase;
1290 AM.Base.FrameIndex = Op.getFrameIndex();
1291 }
1292 Op = MI->getOperand(1);
1293 if (Op.isImmediate())
1294 AM.Scale = Op.getImmedValue();
1295 Op = MI->getOperand(2);
1296 if (Op.isImmediate())
1297 AM.IndexReg = Op.getImmedValue();
1298 Op = MI->getOperand(3);
1299 if (Op.isGlobalAddress()) {
1300 AM.GV = Op.getGlobal();
1301 } else {
1302 AM.Disp = Op.getImmedValue();
1303 }
1304 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1305
1306 // Reload the original control word now.
1307 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1308
1309 delete MI; // The pseudo instruction is gone now.
1310 return BB;
1311 }
1312 }
Evan Cheng4a460802006-01-11 00:33:36 +00001313}
1314
1315
1316//===----------------------------------------------------------------------===//
1317// X86 Custom Lowering Hooks
1318//===----------------------------------------------------------------------===//
1319
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001320/// LowerOperation - Provide custom lowering hooks for some operations.
1321///
1322SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1323 switch (Op.getOpcode()) {
1324 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001325 case ISD::SHL_PARTS:
1326 case ISD::SRA_PARTS:
1327 case ISD::SRL_PARTS: {
1328 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1329 "Not an i64 shift!");
1330 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1331 SDOperand ShOpLo = Op.getOperand(0);
1332 SDOperand ShOpHi = Op.getOperand(1);
1333 SDOperand ShAmt = Op.getOperand(2);
1334 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001335 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001336 : DAG.getConstant(0, MVT::i32);
1337
1338 SDOperand Tmp2, Tmp3;
1339 if (Op.getOpcode() == ISD::SHL_PARTS) {
1340 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1341 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1342 } else {
1343 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001344 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001345 }
1346
1347 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1348 ShAmt, DAG.getConstant(32, MVT::i8));
1349
1350 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001351 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001352
1353 std::vector<MVT::ValueType> Tys;
1354 Tys.push_back(MVT::i32);
1355 Tys.push_back(MVT::Flag);
1356 std::vector<SDOperand> Ops;
1357 if (Op.getOpcode() == ISD::SHL_PARTS) {
1358 Ops.push_back(Tmp2);
1359 Ops.push_back(Tmp3);
1360 Ops.push_back(CC);
1361 Ops.push_back(InFlag);
1362 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1363 InFlag = Hi.getValue(1);
1364
1365 Ops.clear();
1366 Ops.push_back(Tmp3);
1367 Ops.push_back(Tmp1);
1368 Ops.push_back(CC);
1369 Ops.push_back(InFlag);
1370 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1371 } else {
1372 Ops.push_back(Tmp2);
1373 Ops.push_back(Tmp3);
1374 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001375 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001376 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1377 InFlag = Lo.getValue(1);
1378
1379 Ops.clear();
1380 Ops.push_back(Tmp3);
1381 Ops.push_back(Tmp1);
1382 Ops.push_back(CC);
1383 Ops.push_back(InFlag);
1384 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1385 }
1386
1387 Tys.clear();
1388 Tys.push_back(MVT::i32);
1389 Tys.push_back(MVT::i32);
1390 Ops.clear();
1391 Ops.push_back(Lo);
1392 Ops.push_back(Hi);
1393 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1394 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001395 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001396 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001397 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001398 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001399
1400 SDOperand Result;
1401 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1402 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001403 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001404 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001405 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001406 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1407 DAG.getEntryNode(), Op.getOperand(0),
1408 StackSlot, DAG.getSrcValue(NULL));
1409
1410 // Build the FILD
1411 std::vector<MVT::ValueType> Tys;
1412 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001413 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001414 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001415 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001416 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001417 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001418 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001419 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1420 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001421
1422 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001423 Chain = Result.getValue(1);
1424 SDOperand InFlag = Result.getValue(2);
1425
Evan Chenge3de85b2006-02-04 02:20:30 +00001426 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001427 // shouldn't be necessary except that RFP cannot be live across
1428 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1429 MachineFunction &MF = DAG.getMachineFunction();
1430 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1431 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1432 std::vector<MVT::ValueType> Tys;
1433 Tys.push_back(MVT::Other);
1434 std::vector<SDOperand> Ops;
1435 Ops.push_back(Chain);
1436 Ops.push_back(Result);
1437 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001438 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001439 Ops.push_back(InFlag);
1440 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1441 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1442 DAG.getSrcValue(NULL));
1443 }
1444
Evan Chenga3195e82006-01-12 22:54:21 +00001445 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001446 }
1447 case ISD::FP_TO_SINT: {
1448 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001449 "Unknown FP_TO_SINT to lower!");
1450 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1451 // stack slot.
1452 MachineFunction &MF = DAG.getMachineFunction();
1453 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1454 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1455 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1456
1457 unsigned Opc;
1458 switch (Op.getValueType()) {
1459 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1460 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1461 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1462 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1463 }
1464
Evan Cheng6dab0532006-01-30 08:02:57 +00001465 SDOperand Chain = DAG.getEntryNode();
1466 SDOperand Value = Op.getOperand(0);
1467 if (X86ScalarSSE) {
1468 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1469 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1470 DAG.getSrcValue(0));
1471 std::vector<MVT::ValueType> Tys;
1472 Tys.push_back(MVT::f64);
1473 Tys.push_back(MVT::Other);
1474 std::vector<SDOperand> Ops;
1475 Ops.push_back(Chain);
1476 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001477 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001478 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1479 Chain = Value.getValue(1);
1480 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1481 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1482 }
1483
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001484 // Build the FP_TO_INT*_IN_MEM
1485 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001486 Ops.push_back(Chain);
1487 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001488 Ops.push_back(StackSlot);
1489 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1490
1491 // Load the result.
1492 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1493 DAG.getSrcValue(NULL));
1494 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001495 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001496 std::vector<MVT::ValueType> Tys;
1497 Tys.push_back(MVT::Other);
1498 Tys.push_back(MVT::Flag);
1499 std::vector<SDOperand> Ops;
1500 Ops.push_back(Op.getOperand(0));
1501 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001502 Ops.clear();
1503 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1504 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1505 MVT::i32, Ops[0].getValue(2)));
1506 Ops.push_back(Ops[1].getValue(1));
1507 Tys[0] = Tys[1] = MVT::i32;
1508 Tys.push_back(MVT::Other);
1509 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001510 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001511 case ISD::FABS: {
1512 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001513 const Type *OpNTy = MVT::getTypeForValueType(VT);
1514 std::vector<Constant*> CV;
1515 if (VT == MVT::f64) {
1516 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1517 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1518 } else {
1519 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1520 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1521 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1522 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1523 }
1524 Constant *CS = ConstantStruct::get(CV);
1525 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1526 SDOperand Mask
1527 = DAG.getNode(X86ISD::LOAD_PACK,
1528 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001529 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1530 }
Evan Cheng223547a2006-01-31 22:28:30 +00001531 case ISD::FNEG: {
1532 MVT::ValueType VT = Op.getValueType();
1533 const Type *OpNTy = MVT::getTypeForValueType(VT);
1534 std::vector<Constant*> CV;
1535 if (VT == MVT::f64) {
1536 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1537 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1538 } else {
1539 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1540 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1541 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1542 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1543 }
1544 Constant *CS = ConstantStruct::get(CV);
1545 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1546 SDOperand Mask
1547 = DAG.getNode(X86ISD::LOAD_PACK,
1548 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1549 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1550 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001551 case ISD::SETCC: {
1552 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001553 SDOperand Cond;
1554 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001555 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1556 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001557 bool Flip;
1558 unsigned X86CC;
1559 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1560 if (Flip)
1561 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1562 Op.getOperand(1), Op.getOperand(0));
1563 else
1564 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1565 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001566 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1567 DAG.getConstant(X86CC, MVT::i8), Cond);
1568 } else {
1569 assert(isFP && "Illegal integer SetCC!");
1570
Evan Cheng6dfa9992006-01-30 23:41:35 +00001571 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1572 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001573 std::vector<MVT::ValueType> Tys;
1574 std::vector<SDOperand> Ops;
1575 switch (SetCCOpcode) {
1576 default: assert(false && "Illegal floating point SetCC!");
1577 case ISD::SETOEQ: { // !PF & ZF
1578 Tys.push_back(MVT::i8);
1579 Tys.push_back(MVT::Flag);
1580 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1581 Ops.push_back(Cond);
1582 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1583 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1584 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1585 Tmp1.getValue(1));
1586 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1587 }
Evan Chengd9558e02006-01-06 00:43:03 +00001588 case ISD::SETUNE: { // PF | !ZF
1589 Tys.push_back(MVT::i8);
1590 Tys.push_back(MVT::Flag);
1591 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1592 Ops.push_back(Cond);
1593 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1594 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1595 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1596 Tmp1.getValue(1));
1597 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1598 }
1599 }
1600 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001601 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001602 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001603 MVT::ValueType VT = Op.getValueType();
1604 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001605 bool isFPStack = isFP && !X86ScalarSSE;
1606 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001607 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001608 SDOperand Op0 = Op.getOperand(0);
1609 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001610 if (Op0.getOpcode() == ISD::SETCC)
1611 Op0 = LowerOperation(Op0, DAG);
1612
Evan Chengaaca22c2006-01-10 20:26:56 +00001613 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001614 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1615 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1616 // have another use it will be eliminated.
1617 // If the X86ISD::SETCC has more than one use, then it's probably better
1618 // to use a test instead of duplicating the X86ISD::CMP (for register
1619 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001620 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1621 if (!Op0.hasOneUse()) {
1622 std::vector<MVT::ValueType> Tys;
1623 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1624 Tys.push_back(Op0.Val->getValueType(i));
1625 std::vector<SDOperand> Ops;
1626 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1627 Ops.push_back(Op0.getOperand(i));
1628 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1629 }
1630
Evan Cheng1bcee362006-01-13 01:03:02 +00001631 CC = Op0.getOperand(0);
1632 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001633 // Make a copy as flag result cannot be used by more than one.
1634 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1635 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001636 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001637 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001638 } else
1639 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001640 } else
1641 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001642
Evan Cheng189d01e2006-01-13 01:06:49 +00001643 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001644 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001645 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001646 }
Evan Chenge3413162006-01-09 18:33:28 +00001647
1648 std::vector<MVT::ValueType> Tys;
1649 Tys.push_back(Op.getValueType());
1650 Tys.push_back(MVT::Flag);
1651 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001652 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1653 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001654 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001655 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001656 Ops.push_back(CC);
1657 Ops.push_back(Cond);
1658 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001659 }
Evan Cheng898101c2005-12-19 23:12:38 +00001660 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001661 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001662 SDOperand Cond = Op.getOperand(1);
1663 SDOperand Dest = Op.getOperand(2);
1664 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001665 if (Cond.getOpcode() == ISD::SETCC)
1666 Cond = LowerOperation(Cond, DAG);
1667
Evan Chengd5781fc2005-12-21 20:21:51 +00001668 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001669 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1670 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1671 // have another use it will be eliminated.
1672 // If the X86ISD::SETCC has more than one use, then it's probably better
1673 // to use a test instead of duplicating the X86ISD::CMP (for register
1674 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001675 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1676 if (!Cond.hasOneUse()) {
1677 std::vector<MVT::ValueType> Tys;
1678 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1679 Tys.push_back(Cond.Val->getValueType(i));
1680 std::vector<SDOperand> Ops;
1681 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1682 Ops.push_back(Cond.getOperand(i));
1683 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1684 }
1685
Evan Cheng1bcee362006-01-13 01:03:02 +00001686 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001687 Cond = Cond.getOperand(1);
1688 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001689 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001690 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001691 } else
1692 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001693 } else
1694 addTest = true;
1695
1696 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001697 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001698 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1699 }
1700 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1701 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1702 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001703 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001704 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001705 SDOperand Chain = Op.getOperand(0);
1706 unsigned Align =
1707 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1708 if (Align == 0) Align = 1;
1709
Evan Cheng18a84522006-02-16 00:21:07 +00001710 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1711 // If not DWORD aligned, call memset if size is less than the threshold.
1712 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001713 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001714 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001715 MVT::ValueType IntPtr = getPointerTy();
1716 const Type *IntPtrTy = getTargetData().getIntPtrType();
1717 std::vector<std::pair<SDOperand, const Type*> > Args;
1718 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1719 // Extend the ubyte argument to be an int value for the call.
1720 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1721 Args.push_back(std::make_pair(Val, IntPtrTy));
1722 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1723 std::pair<SDOperand,SDOperand> CallResult =
1724 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1725 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1726 return CallResult.second;
1727 }
1728
Evan Cheng67f92a72006-01-11 22:15:48 +00001729 MVT::ValueType AVT;
1730 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001731 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1732 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001733 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001734 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001735 unsigned ValReg;
1736 unsigned Val = ValC->getValue() & 255;
1737
1738 // If the value is a constant, then we can potentially use larger sets.
1739 switch (Align & 3) {
1740 case 2: // WORD aligned
1741 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001742 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1743 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001744 Val = (Val << 8) | Val;
1745 ValReg = X86::AX;
1746 break;
1747 case 0: // DWORD aligned
1748 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001749 if (I) {
1750 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1751 BytesLeft = I->getValue() % 4;
1752 } else {
1753 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1754 DAG.getConstant(2, MVT::i8));
1755 TwoRepStos = true;
1756 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001757 Val = (Val << 8) | Val;
1758 Val = (Val << 16) | Val;
1759 ValReg = X86::EAX;
1760 break;
1761 default: // Byte aligned
1762 AVT = MVT::i8;
1763 Count = Op.getOperand(3);
1764 ValReg = X86::AL;
1765 break;
1766 }
1767
1768 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1769 InFlag);
1770 InFlag = Chain.getValue(1);
1771 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001772 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001773 Count = Op.getOperand(3);
1774 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1775 InFlag = Chain.getValue(1);
1776 }
1777
1778 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1779 InFlag = Chain.getValue(1);
1780 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1781 InFlag = Chain.getValue(1);
1782
Evan Chengff909922006-03-07 23:29:39 +00001783 std::vector<MVT::ValueType> Tys;
1784 Tys.push_back(MVT::Other);
1785 Tys.push_back(MVT::Flag);
1786 std::vector<SDOperand> Ops;
1787 Ops.push_back(Chain);
1788 Ops.push_back(DAG.getValueType(AVT));
1789 Ops.push_back(InFlag);
1790 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1791
1792 if (TwoRepStos) {
1793 InFlag = Chain.getValue(1);
1794 Count = Op.getOperand(3);
1795 MVT::ValueType CVT = Count.getValueType();
1796 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1797 DAG.getConstant(3, CVT));
1798 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1799 InFlag = Chain.getValue(1);
1800 Tys.clear();
1801 Tys.push_back(MVT::Other);
1802 Tys.push_back(MVT::Flag);
1803 Ops.clear();
1804 Ops.push_back(Chain);
1805 Ops.push_back(DAG.getValueType(MVT::i8));
1806 Ops.push_back(InFlag);
1807 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1808 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001809 // Issue stores for the last 1 - 3 bytes.
1810 SDOperand Value;
1811 unsigned Val = ValC->getValue() & 255;
1812 unsigned Offset = I->getValue() - BytesLeft;
1813 SDOperand DstAddr = Op.getOperand(1);
1814 MVT::ValueType AddrVT = DstAddr.getValueType();
1815 if (BytesLeft >= 2) {
1816 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1817 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1818 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1819 DAG.getConstant(Offset, AddrVT)),
1820 DAG.getSrcValue(NULL));
1821 BytesLeft -= 2;
1822 Offset += 2;
1823 }
1824
1825 if (BytesLeft == 1) {
1826 Value = DAG.getConstant(Val, MVT::i8);
1827 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1828 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1829 DAG.getConstant(Offset, AddrVT)),
1830 DAG.getSrcValue(NULL));
1831 }
1832 }
1833
1834 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001835 }
1836 case ISD::MEMCPY: {
1837 SDOperand Chain = Op.getOperand(0);
1838 unsigned Align =
1839 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1840 if (Align == 0) Align = 1;
1841
Evan Cheng18a84522006-02-16 00:21:07 +00001842 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1843 // If not DWORD aligned, call memcpy if size is less than the threshold.
1844 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001845 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001846 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001847 MVT::ValueType IntPtr = getPointerTy();
1848 const Type *IntPtrTy = getTargetData().getIntPtrType();
1849 std::vector<std::pair<SDOperand, const Type*> > Args;
1850 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1851 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1852 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1853 std::pair<SDOperand,SDOperand> CallResult =
1854 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1855 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1856 return CallResult.second;
1857 }
1858
Evan Cheng67f92a72006-01-11 22:15:48 +00001859 MVT::ValueType AVT;
1860 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001861 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001862 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001863 switch (Align & 3) {
1864 case 2: // WORD aligned
1865 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001866 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1867 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001868 break;
1869 case 0: // DWORD aligned
1870 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001871 if (I) {
1872 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1873 BytesLeft = I->getValue() % 4;
1874 } else {
1875 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1876 DAG.getConstant(2, MVT::i8));
1877 TwoRepMovs = true;
1878 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001879 break;
1880 default: // Byte aligned
1881 AVT = MVT::i8;
1882 Count = Op.getOperand(3);
1883 break;
1884 }
1885
Evan Cheng62bec2c2006-03-04 02:48:56 +00001886 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001887 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1888 InFlag = Chain.getValue(1);
1889 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1890 InFlag = Chain.getValue(1);
1891 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1892 InFlag = Chain.getValue(1);
1893
Evan Chengff909922006-03-07 23:29:39 +00001894 std::vector<MVT::ValueType> Tys;
1895 Tys.push_back(MVT::Other);
1896 Tys.push_back(MVT::Flag);
1897 std::vector<SDOperand> Ops;
1898 Ops.push_back(Chain);
1899 Ops.push_back(DAG.getValueType(AVT));
1900 Ops.push_back(InFlag);
1901 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1902
1903 if (TwoRepMovs) {
1904 InFlag = Chain.getValue(1);
1905 Count = Op.getOperand(3);
1906 MVT::ValueType CVT = Count.getValueType();
1907 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1908 DAG.getConstant(3, CVT));
1909 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1910 InFlag = Chain.getValue(1);
1911 Tys.clear();
1912 Tys.push_back(MVT::Other);
1913 Tys.push_back(MVT::Flag);
1914 Ops.clear();
1915 Ops.push_back(Chain);
1916 Ops.push_back(DAG.getValueType(MVT::i8));
1917 Ops.push_back(InFlag);
1918 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1919 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001920 // Issue loads and stores for the last 1 - 3 bytes.
1921 unsigned Offset = I->getValue() - BytesLeft;
1922 SDOperand DstAddr = Op.getOperand(1);
1923 MVT::ValueType DstVT = DstAddr.getValueType();
1924 SDOperand SrcAddr = Op.getOperand(2);
1925 MVT::ValueType SrcVT = SrcAddr.getValueType();
1926 SDOperand Value;
1927 if (BytesLeft >= 2) {
1928 Value = DAG.getLoad(MVT::i16, Chain,
1929 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1930 DAG.getConstant(Offset, SrcVT)),
1931 DAG.getSrcValue(NULL));
1932 Chain = Value.getValue(1);
1933 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1934 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1935 DAG.getConstant(Offset, DstVT)),
1936 DAG.getSrcValue(NULL));
1937 BytesLeft -= 2;
1938 Offset += 2;
1939 }
1940
1941 if (BytesLeft == 1) {
1942 Value = DAG.getLoad(MVT::i8, Chain,
1943 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1944 DAG.getConstant(Offset, SrcVT)),
1945 DAG.getSrcValue(NULL));
1946 Chain = Value.getValue(1);
1947 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1948 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1949 DAG.getConstant(Offset, DstVT)),
1950 DAG.getSrcValue(NULL));
1951 }
1952 }
1953
1954 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001955 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00001956
1957 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1958 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1959 // one of the above mentioned nodes. It has to be wrapped because otherwise
1960 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1961 // be used to form addressing mode. These wrapped nodes will be selected
1962 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00001963 case ISD::ConstantPool: {
1964 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00001965 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1966 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1967 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00001968 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00001969 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001970 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00001971 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1972 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1973 }
1974
1975 return Result;
1976 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001977 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00001978 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1979 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1980 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00001981 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00001982 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00001983 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001984 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00001985 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1986 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00001987
1988 // For Darwin, external and weak symbols are indirect, so we want to load
1989 // the value at address GV, not the value of GV itself. This means that
1990 // the GlobalAddress must be in the base or index register of the address,
1991 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001992 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
1993 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1994 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
Evan Cheng2338c5c2006-02-07 08:38:37 +00001995 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00001996 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00001997 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001998
Evan Cheng002fe9b2006-01-12 07:56:47 +00001999 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002000 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002001 case ISD::ExternalSymbol: {
2002 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2003 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2004 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2005 if (getTargetMachine().
2006 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2007 // With PIC, the address is actually $g + Offset.
2008 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2009 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2010 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2011 }
2012
2013 return Result;
2014 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002015 case ISD::VASTART: {
2016 // vastart just stores the address of the VarArgsFrameIndex slot into the
2017 // memory location argument.
2018 // FIXME: Replace MVT::i32 with PointerTy
2019 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2020 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2021 Op.getOperand(1), Op.getOperand(2));
2022 }
Nate Begemanee625572006-01-27 21:09:22 +00002023 case ISD::RET: {
2024 SDOperand Copy;
2025
2026 switch(Op.getNumOperands()) {
2027 default:
2028 assert(0 && "Do not know how to return this many arguments!");
2029 abort();
2030 case 1:
2031 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2032 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2033 case 2: {
2034 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2035 if (MVT::isInteger(ArgVT))
2036 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2037 SDOperand());
2038 else if (!X86ScalarSSE) {
2039 std::vector<MVT::ValueType> Tys;
2040 Tys.push_back(MVT::Other);
2041 Tys.push_back(MVT::Flag);
2042 std::vector<SDOperand> Ops;
2043 Ops.push_back(Op.getOperand(0));
2044 Ops.push_back(Op.getOperand(1));
2045 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2046 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002047 SDOperand MemLoc;
2048 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002049 SDOperand Value = Op.getOperand(1);
2050
Evan Cheng760df292006-02-01 01:19:32 +00002051 if (Value.getOpcode() == ISD::LOAD &&
2052 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002053 Chain = Value.getOperand(0);
2054 MemLoc = Value.getOperand(1);
2055 } else {
2056 // Spill the value to memory and reload it into top of stack.
2057 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2058 MachineFunction &MF = DAG.getMachineFunction();
2059 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2060 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2061 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2062 Value, MemLoc, DAG.getSrcValue(0));
2063 }
Nate Begemanee625572006-01-27 21:09:22 +00002064 std::vector<MVT::ValueType> Tys;
2065 Tys.push_back(MVT::f64);
2066 Tys.push_back(MVT::Other);
2067 std::vector<SDOperand> Ops;
2068 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002069 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002070 Ops.push_back(DAG.getValueType(ArgVT));
2071 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2072 Tys.clear();
2073 Tys.push_back(MVT::Other);
2074 Tys.push_back(MVT::Flag);
2075 Ops.clear();
2076 Ops.push_back(Copy.getValue(1));
2077 Ops.push_back(Copy);
2078 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2079 }
2080 break;
2081 }
2082 case 3:
2083 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2084 SDOperand());
2085 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2086 break;
2087 }
2088 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2089 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2090 Copy.getValue(1));
2091 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002092 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002093}
Evan Cheng72261582005-12-20 06:22:03 +00002094
2095const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2096 switch (Opcode) {
2097 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002098 case X86ISD::SHLD: return "X86ISD::SHLD";
2099 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002100 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002101 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002102 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002103 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002104 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2105 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2106 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002107 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002108 case X86ISD::FST: return "X86ISD::FST";
2109 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002110 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002111 case X86ISD::CALL: return "X86ISD::CALL";
2112 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2113 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2114 case X86ISD::CMP: return "X86ISD::CMP";
2115 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002116 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002117 case X86ISD::CMOV: return "X86ISD::CMOV";
2118 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002119 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002120 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2121 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002122 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002123 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002124 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002125 }
2126}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002127
Nate Begeman368e18d2006-02-16 21:11:51 +00002128void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2129 uint64_t Mask,
2130 uint64_t &KnownZero,
2131 uint64_t &KnownOne,
2132 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002133
2134 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002135 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002136
2137 switch (Opc) {
2138 default:
2139 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2140 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002141 case X86ISD::SETCC:
2142 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2143 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002144 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002145}
Chris Lattner259e97c2006-01-31 19:43:35 +00002146
2147std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002148getRegClassForInlineAsmConstraint(const std::string &Constraint,
2149 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002150 if (Constraint.size() == 1) {
2151 // FIXME: not handling fp-stack yet!
2152 // FIXME: not handling MMX registers yet ('y' constraint).
2153 switch (Constraint[0]) { // GCC X86 Constraint Letters
2154 default: break; // Unknown constriant letter
2155 case 'r': // GENERAL_REGS
2156 case 'R': // LEGACY_REGS
2157 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2158 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2159 case 'l': // INDEX_REGS
2160 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2161 X86::ESI, X86::EDI, X86::EBP, 0);
2162 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2163 case 'Q': // Q_REGS
2164 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2165 case 'x': // SSE_REGS if SSE1 allowed
2166 if (Subtarget->hasSSE1())
2167 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2168 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2169 0);
2170 return std::vector<unsigned>();
2171 case 'Y': // SSE_REGS if SSE2 allowed
2172 if (Subtarget->hasSSE2())
2173 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2174 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2175 0);
2176 return std::vector<unsigned>();
2177 }
2178 }
2179
Chris Lattner1efa40f2006-02-22 00:56:39 +00002180 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002181}