blob: 148c7f8af9fced246fb99c6cd21edac11c079b03 [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"
Evan Cheng30b37b52006-03-13 23:18:16 +000022#include "llvm/ADT/VectorExtras.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000029#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33// FIXME: temporary.
34#include "llvm/Support/CommandLine.h"
35static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
36 cl::desc("Enable fastcc on X86"));
37
38X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043 // Set up the TargetLowering object.
44
45 // X86 is weird, it always uses i8 for shift amounts and setcc results.
46 setShiftAmountType(MVT::i8);
47 setSetCCResultType(MVT::i8);
48 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000049 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000051 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000052
53 // Add legal addressing mode scale values.
54 addLegalAddressScale(8);
55 addLegalAddressScale(4);
56 addLegalAddressScale(2);
57 // Enter the ones which require both scale + index last. These are more
58 // expensive.
59 addLegalAddressScale(9);
60 addLegalAddressScale(5);
61 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000062
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000063 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 addRegisterClass(MVT::i8, X86::R8RegisterClass);
65 addRegisterClass(MVT::i16, X86::R16RegisterClass);
66 addRegisterClass(MVT::i32, X86::R32RegisterClass);
67
68 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
69 // operation.
70 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
71 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
72 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000073
74 if (X86ScalarSSE)
75 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
76 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
77 else
78 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079
80 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
81 // this operation.
82 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
83 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000084 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000085 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000086 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000087 else {
88 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
89 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
90 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091
Evan Cheng6dab0532006-01-30 08:02:57 +000092 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
93 // isn't legal.
94 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
95 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
96
Evan Cheng02568ff2006-01-30 22:13:22 +000097 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
98 // this operation.
99 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
101
102 if (X86ScalarSSE) {
103 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
104 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000106 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000107 }
108
109 // Handle FP_TO_UINT by promoting the destination to a larger signed
110 // conversion.
111 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
112 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
113 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
114
Evan Cheng45af8fd2006-02-18 07:26:17 +0000115 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000116 // Expand FP_TO_UINT into a select.
117 // FIXME: We would like to use a Custom expander here eventually to do
118 // the optimal thing for SSE vs. the default expansion in the legalizer.
119 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
120 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000121 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000122 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
123
Evan Cheng02568ff2006-01-30 22:13:22 +0000124 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
125 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000126
Evan Cheng5298bcc2006-02-17 07:01:52 +0000127 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
129 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000130 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
131 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
133 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000134 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000135 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
136 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
137 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
138 setOperationAction(ISD::FREM , MVT::f64 , Expand);
139 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
140 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
141 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
142 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
144 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
145 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
147 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000148 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000149 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000150
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151 // These should be promoted to a larger select which is supported.
152 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
153 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000154
155 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000156 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
157 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
158 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
159 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
160 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
161 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
162 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
163 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
164 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000165 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000166 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000167 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000168 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000169 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000170 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000171 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000172 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
173 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
174 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000175 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000176 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
177 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178
Chris Lattnerf73bae12005-11-29 06:16:21 +0000179 // We don't have line number support yet.
180 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000181 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000182 // FIXME - use subtarget debug flags
183 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
184 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000185
Nate Begemanacc398c2006-01-25 18:21:52 +0000186 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
187 setOperationAction(ISD::VASTART , MVT::Other, Custom);
188
189 // Use the default implementation.
190 setOperationAction(ISD::VAARG , MVT::Other, Expand);
191 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
192 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000193 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
194 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
195 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000196
Chris Lattner9601a862006-03-05 05:08:37 +0000197 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
198 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
199
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000200 if (X86ScalarSSE) {
201 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000202 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
203 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000204
205 // SSE has no load+extend ops
206 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
207 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
208
Evan Cheng223547a2006-01-31 22:28:30 +0000209 // Use ANDPD to simulate FABS.
210 setOperationAction(ISD::FABS , MVT::f64, Custom);
211 setOperationAction(ISD::FABS , MVT::f32, Custom);
212
213 // Use XORP to simulate FNEG.
214 setOperationAction(ISD::FNEG , MVT::f64, Custom);
215 setOperationAction(ISD::FNEG , MVT::f32, Custom);
216
Evan Chengd25e9e82006-02-02 00:28:23 +0000217 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 setOperationAction(ISD::FSIN , MVT::f64, Expand);
219 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 setOperationAction(ISD::FREM , MVT::f64, Expand);
221 setOperationAction(ISD::FSIN , MVT::f32, Expand);
222 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::FREM , MVT::f32, Expand);
224
Chris Lattnera54aa942006-01-29 06:26:08 +0000225 // Expand FP immediates into loads from the stack, except for the special
226 // cases we handle.
227 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
228 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229 addLegalFPImmediate(+0.0); // xorps / xorpd
230 } else {
231 // Set up the FP register classes.
232 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000233
234 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
235
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236 if (!UnsafeFPMath) {
237 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
238 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
239 }
240
Chris Lattnera54aa942006-01-29 06:26:08 +0000241 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000242 addLegalFPImmediate(+0.0); // FLD0
243 addLegalFPImmediate(+1.0); // FLD1
244 addLegalFPImmediate(-0.0); // FLD0/FCHS
245 addLegalFPImmediate(-1.0); // FLD1/FCHS
246 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000247
Evan Chengd30bf012006-03-01 01:11:20 +0000248 // First set operation action for all vector types to expand. Then we
249 // will selectively turn on ones that can be effectively codegen'd.
250 for (unsigned VT = (unsigned)MVT::Vector + 1;
251 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
252 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
253 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
254 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
256 }
257
Evan Cheng470a6ad2006-02-22 02:26:30 +0000258 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
259 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
260 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
261 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
262
Evan Chengd30bf012006-03-01 01:11:20 +0000263 // FIXME: add MMX packed arithmetics
Evan Cheng470a6ad2006-02-22 02:26:30 +0000264 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
265 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
266 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
267 }
268
269 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
270 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
271
Evan Chengd30bf012006-03-01 01:11:20 +0000272 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
273 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
274 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
275 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000276 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
277 }
278
279 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
280 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
281 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
282 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
283 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
284 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
285
286
Evan Chengd30bf012006-03-01 01:11:20 +0000287 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
288 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
289 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
290 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000291 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
292 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
293 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
294 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
295 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
296 }
297
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000298 computeRegisterProperties();
299
Evan Cheng87ed7162006-02-14 08:25:08 +0000300 // FIXME: These should be based on subtarget info. Plus, the values should
301 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000302 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
303 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
304 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 allowUnalignedMemoryAccesses = true; // x86 supports it!
306}
307
308std::vector<SDOperand>
309X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
310 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
311 return LowerFastCCArguments(F, DAG);
312 return LowerCCCArguments(F, DAG);
313}
314
315std::pair<SDOperand, SDOperand>
316X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
317 bool isVarArg, unsigned CallingConv,
318 bool isTailCall,
319 SDOperand Callee, ArgListTy &Args,
320 SelectionDAG &DAG) {
321 assert((!isVarArg || CallingConv == CallingConv::C) &&
322 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000323
324 // If the callee is a GlobalAddress node (quite common, every direct call is)
325 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
326 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
327 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000328 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
329 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000330
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000331 if (CallingConv == CallingConv::Fast && EnableFastCC)
332 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
333 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
334}
335
336//===----------------------------------------------------------------------===//
337// C Calling Convention implementation
338//===----------------------------------------------------------------------===//
339
340std::vector<SDOperand>
341X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
342 std::vector<SDOperand> ArgValues;
343
344 MachineFunction &MF = DAG.getMachineFunction();
345 MachineFrameInfo *MFI = MF.getFrameInfo();
346
347 // Add DAG nodes to load the arguments... On entry to a function on the X86,
348 // the stack frame looks like this:
349 //
350 // [ESP] -- return address
351 // [ESP + 4] -- first argument (leftmost lexically)
352 // [ESP + 8] -- second argument, if first argument is four bytes in size
353 // ...
354 //
355 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
356 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
357 MVT::ValueType ObjectVT = getValueType(I->getType());
358 unsigned ArgIncrement = 4;
359 unsigned ObjSize;
360 switch (ObjectVT) {
361 default: assert(0 && "Unhandled argument type!");
362 case MVT::i1:
363 case MVT::i8: ObjSize = 1; break;
364 case MVT::i16: ObjSize = 2; break;
365 case MVT::i32: ObjSize = 4; break;
366 case MVT::i64: ObjSize = ArgIncrement = 8; break;
367 case MVT::f32: ObjSize = 4; break;
368 case MVT::f64: ObjSize = ArgIncrement = 8; break;
369 }
370 // Create the frame index object for this incoming parameter...
371 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
372
373 // Create the SelectionDAG nodes corresponding to a load from this parameter
374 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
375
376 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
377 // dead loads.
378 SDOperand ArgValue;
379 if (!I->use_empty())
380 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
381 DAG.getSrcValue(NULL));
382 else {
383 if (MVT::isInteger(ObjectVT))
384 ArgValue = DAG.getConstant(0, ObjectVT);
385 else
386 ArgValue = DAG.getConstantFP(0, ObjectVT);
387 }
388 ArgValues.push_back(ArgValue);
389
390 ArgOffset += ArgIncrement; // Move on to the next argument...
391 }
392
393 // If the function takes variable number of arguments, make a frame index for
394 // the start of the first vararg value... for expansion of llvm.va_start.
395 if (F.isVarArg())
396 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
397 ReturnAddrIndex = 0; // No return address slot generated yet.
398 BytesToPopOnReturn = 0; // Callee pops nothing.
399 BytesCallerReserves = ArgOffset;
400
401 // Finally, inform the code generator which regs we return values in.
402 switch (getValueType(F.getReturnType())) {
403 default: assert(0 && "Unknown type!");
404 case MVT::isVoid: break;
405 case MVT::i1:
406 case MVT::i8:
407 case MVT::i16:
408 case MVT::i32:
409 MF.addLiveOut(X86::EAX);
410 break;
411 case MVT::i64:
412 MF.addLiveOut(X86::EAX);
413 MF.addLiveOut(X86::EDX);
414 break;
415 case MVT::f32:
416 case MVT::f64:
417 MF.addLiveOut(X86::ST0);
418 break;
419 }
420 return ArgValues;
421}
422
423std::pair<SDOperand, SDOperand>
424X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
425 bool isVarArg, bool isTailCall,
426 SDOperand Callee, ArgListTy &Args,
427 SelectionDAG &DAG) {
428 // Count how many bytes are to be pushed on the stack.
429 unsigned NumBytes = 0;
430
431 if (Args.empty()) {
432 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000433 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 } else {
435 for (unsigned i = 0, e = Args.size(); i != e; ++i)
436 switch (getValueType(Args[i].second)) {
437 default: assert(0 && "Unknown value type!");
438 case MVT::i1:
439 case MVT::i8:
440 case MVT::i16:
441 case MVT::i32:
442 case MVT::f32:
443 NumBytes += 4;
444 break;
445 case MVT::i64:
446 case MVT::f64:
447 NumBytes += 8;
448 break;
449 }
450
Chris Lattner94dd2922006-02-13 09:00:43 +0000451 Chain = DAG.getCALLSEQ_START(Chain,
452 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000453
454 // Arguments go on the stack in reverse order, as specified by the ABI.
455 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000456 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000457 std::vector<SDOperand> Stores;
458
459 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
460 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
461 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
462
463 switch (getValueType(Args[i].second)) {
464 default: assert(0 && "Unexpected ValueType for argument!");
465 case MVT::i1:
466 case MVT::i8:
467 case MVT::i16:
468 // Promote the integer to 32 bits. If the input type is signed use a
469 // sign extend, otherwise use a zero extend.
470 if (Args[i].second->isSigned())
471 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
472 else
473 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
474
475 // FALL THROUGH
476 case MVT::i32:
477 case MVT::f32:
478 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
479 Args[i].first, PtrOff,
480 DAG.getSrcValue(NULL)));
481 ArgOffset += 4;
482 break;
483 case MVT::i64:
484 case MVT::f64:
485 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
486 Args[i].first, PtrOff,
487 DAG.getSrcValue(NULL)));
488 ArgOffset += 8;
489 break;
490 }
491 }
492 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
493 }
494
495 std::vector<MVT::ValueType> RetVals;
496 MVT::ValueType RetTyVT = getValueType(RetTy);
497 RetVals.push_back(MVT::Other);
498
499 // The result values produced have to be legal. Promote the result.
500 switch (RetTyVT) {
501 case MVT::isVoid: break;
502 default:
503 RetVals.push_back(RetTyVT);
504 break;
505 case MVT::i1:
506 case MVT::i8:
507 case MVT::i16:
508 RetVals.push_back(MVT::i32);
509 break;
510 case MVT::f32:
511 if (X86ScalarSSE)
512 RetVals.push_back(MVT::f32);
513 else
514 RetVals.push_back(MVT::f64);
515 break;
516 case MVT::i64:
517 RetVals.push_back(MVT::i32);
518 RetVals.push_back(MVT::i32);
519 break;
520 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000521
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000522 std::vector<MVT::ValueType> NodeTys;
523 NodeTys.push_back(MVT::Other); // Returns a chain
524 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
525 std::vector<SDOperand> Ops;
526 Ops.push_back(Chain);
527 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000528
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000529 // FIXME: Do not generate X86ISD::TAILCALL for now.
530 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
531 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000532
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000533 NodeTys.clear();
534 NodeTys.push_back(MVT::Other); // Returns a chain
535 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
536 Ops.clear();
537 Ops.push_back(Chain);
538 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
539 Ops.push_back(DAG.getConstant(0, getPointerTy()));
540 Ops.push_back(InFlag);
541 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
542 InFlag = Chain.getValue(1);
543
544 SDOperand RetVal;
545 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000546 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000547 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000548 case MVT::i1:
549 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000550 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
551 Chain = RetVal.getValue(1);
552 if (RetTyVT == MVT::i1)
553 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
554 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000555 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000556 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
557 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000558 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000559 case MVT::i32:
560 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
561 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000562 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000563 case MVT::i64: {
564 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
565 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
566 Lo.getValue(2));
567 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
568 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000569 break;
570 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000571 case MVT::f32:
572 case MVT::f64: {
573 std::vector<MVT::ValueType> Tys;
574 Tys.push_back(MVT::f64);
575 Tys.push_back(MVT::Other);
576 Tys.push_back(MVT::Flag);
577 std::vector<SDOperand> Ops;
578 Ops.push_back(Chain);
579 Ops.push_back(InFlag);
580 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
581 Chain = RetVal.getValue(1);
582 InFlag = RetVal.getValue(2);
583 if (X86ScalarSSE) {
584 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
585 // shouldn't be necessary except that RFP cannot be live across
586 // multiple blocks. When stackifier is fixed, they can be uncoupled.
587 MachineFunction &MF = DAG.getMachineFunction();
588 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
589 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
590 Tys.clear();
591 Tys.push_back(MVT::Other);
592 Ops.clear();
593 Ops.push_back(Chain);
594 Ops.push_back(RetVal);
595 Ops.push_back(StackSlot);
596 Ops.push_back(DAG.getValueType(RetTyVT));
597 Ops.push_back(InFlag);
598 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
599 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
600 DAG.getSrcValue(NULL));
601 Chain = RetVal.getValue(1);
602 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000603
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000604 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
605 // FIXME: we would really like to remember that this FP_ROUND
606 // operation is okay to eliminate if we allow excess FP precision.
607 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
608 break;
609 }
610 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000611 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000612
613 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614}
615
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000616//===----------------------------------------------------------------------===//
617// Fast Calling Convention implementation
618//===----------------------------------------------------------------------===//
619//
620// The X86 'fast' calling convention passes up to two integer arguments in
621// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
622// and requires that the callee pop its arguments off the stack (allowing proper
623// tail calls), and has the same return value conventions as C calling convs.
624//
625// This calling convention always arranges for the callee pop value to be 8n+4
626// bytes, which is needed for tail recursion elimination and stack alignment
627// reasons.
628//
629// Note that this can be enhanced in the future to pass fp vals in registers
630// (when we have a global fp allocator) and do other tricks.
631//
632
633/// AddLiveIn - This helper function adds the specified physical register to the
634/// MachineFunction as a live in value. It also creates a corresponding virtual
635/// register for it.
636static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
637 TargetRegisterClass *RC) {
638 assert(RC->contains(PReg) && "Not the correct regclass!");
639 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
640 MF.addLiveIn(PReg, VReg);
641 return VReg;
642}
643
644
645std::vector<SDOperand>
646X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
647 std::vector<SDOperand> ArgValues;
648
649 MachineFunction &MF = DAG.getMachineFunction();
650 MachineFrameInfo *MFI = MF.getFrameInfo();
651
652 // Add DAG nodes to load the arguments... On entry to a function the stack
653 // frame looks like this:
654 //
655 // [ESP] -- return address
656 // [ESP + 4] -- first nonreg argument (leftmost lexically)
657 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
658 // ...
659 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
660
661 // Keep track of the number of integer regs passed so far. This can be either
662 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
663 // used).
664 unsigned NumIntRegs = 0;
665
666 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
667 MVT::ValueType ObjectVT = getValueType(I->getType());
668 unsigned ArgIncrement = 4;
669 unsigned ObjSize = 0;
670 SDOperand ArgValue;
671
672 switch (ObjectVT) {
673 default: assert(0 && "Unhandled argument type!");
674 case MVT::i1:
675 case MVT::i8:
676 if (NumIntRegs < 2) {
677 if (!I->use_empty()) {
678 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
679 X86::R8RegisterClass);
680 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
681 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000682 if (ObjectVT == MVT::i1)
683 // FIXME: Should insert a assertzext here.
684 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000685 }
686 ++NumIntRegs;
687 break;
688 }
689
690 ObjSize = 1;
691 break;
692 case MVT::i16:
693 if (NumIntRegs < 2) {
694 if (!I->use_empty()) {
695 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
696 X86::R16RegisterClass);
697 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
698 DAG.setRoot(ArgValue.getValue(1));
699 }
700 ++NumIntRegs;
701 break;
702 }
703 ObjSize = 2;
704 break;
705 case MVT::i32:
706 if (NumIntRegs < 2) {
707 if (!I->use_empty()) {
708 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
709 X86::R32RegisterClass);
710 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
711 DAG.setRoot(ArgValue.getValue(1));
712 }
713 ++NumIntRegs;
714 break;
715 }
716 ObjSize = 4;
717 break;
718 case MVT::i64:
719 if (NumIntRegs == 0) {
720 if (!I->use_empty()) {
721 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
722 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
723
724 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
725 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
726 DAG.setRoot(Hi.getValue(1));
727
728 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
729 }
730 NumIntRegs = 2;
731 break;
732 } else if (NumIntRegs == 1) {
733 if (!I->use_empty()) {
734 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
735 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
736 DAG.setRoot(Low.getValue(1));
737
738 // Load the high part from memory.
739 // Create the frame index object for this incoming parameter...
740 int FI = MFI->CreateFixedObject(4, ArgOffset);
741 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
742 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
743 DAG.getSrcValue(NULL));
744 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
745 }
746 ArgOffset += 4;
747 NumIntRegs = 2;
748 break;
749 }
750 ObjSize = ArgIncrement = 8;
751 break;
752 case MVT::f32: ObjSize = 4; break;
753 case MVT::f64: ObjSize = ArgIncrement = 8; break;
754 }
755
756 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
757 // dead loads.
758 if (ObjSize && !I->use_empty()) {
759 // Create the frame index object for this incoming parameter...
760 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
761
762 // Create the SelectionDAG nodes corresponding to a load from this
763 // parameter.
764 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
765
766 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
767 DAG.getSrcValue(NULL));
768 } else if (ArgValue.Val == 0) {
769 if (MVT::isInteger(ObjectVT))
770 ArgValue = DAG.getConstant(0, ObjectVT);
771 else
772 ArgValue = DAG.getConstantFP(0, ObjectVT);
773 }
774 ArgValues.push_back(ArgValue);
775
776 if (ObjSize)
777 ArgOffset += ArgIncrement; // Move on to the next argument.
778 }
779
780 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
781 // arguments and the arguments after the retaddr has been pushed are aligned.
782 if ((ArgOffset & 7) == 0)
783 ArgOffset += 4;
784
785 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
786 ReturnAddrIndex = 0; // No return address slot generated yet.
787 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
788 BytesCallerReserves = 0;
789
790 // Finally, inform the code generator which regs we return values in.
791 switch (getValueType(F.getReturnType())) {
792 default: assert(0 && "Unknown type!");
793 case MVT::isVoid: break;
794 case MVT::i1:
795 case MVT::i8:
796 case MVT::i16:
797 case MVT::i32:
798 MF.addLiveOut(X86::EAX);
799 break;
800 case MVT::i64:
801 MF.addLiveOut(X86::EAX);
802 MF.addLiveOut(X86::EDX);
803 break;
804 case MVT::f32:
805 case MVT::f64:
806 MF.addLiveOut(X86::ST0);
807 break;
808 }
809 return ArgValues;
810}
811
812std::pair<SDOperand, SDOperand>
813X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
814 bool isTailCall, SDOperand Callee,
815 ArgListTy &Args, SelectionDAG &DAG) {
816 // Count how many bytes are to be pushed on the stack.
817 unsigned NumBytes = 0;
818
819 // Keep track of the number of integer regs passed so far. This can be either
820 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
821 // used).
822 unsigned NumIntRegs = 0;
823
824 for (unsigned i = 0, e = Args.size(); i != e; ++i)
825 switch (getValueType(Args[i].second)) {
826 default: assert(0 && "Unknown value type!");
827 case MVT::i1:
828 case MVT::i8:
829 case MVT::i16:
830 case MVT::i32:
831 if (NumIntRegs < 2) {
832 ++NumIntRegs;
833 break;
834 }
835 // fall through
836 case MVT::f32:
837 NumBytes += 4;
838 break;
839 case MVT::i64:
840 if (NumIntRegs == 0) {
841 NumIntRegs = 2;
842 break;
843 } else if (NumIntRegs == 1) {
844 NumIntRegs = 2;
845 NumBytes += 4;
846 break;
847 }
848
849 // fall through
850 case MVT::f64:
851 NumBytes += 8;
852 break;
853 }
854
855 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
856 // arguments and the arguments after the retaddr has been pushed are aligned.
857 if ((NumBytes & 7) == 0)
858 NumBytes += 4;
859
Chris Lattner94dd2922006-02-13 09:00:43 +0000860 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000861
862 // Arguments go on the stack in reverse order, as specified by the ABI.
863 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000864 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000865 NumIntRegs = 0;
866 std::vector<SDOperand> Stores;
867 std::vector<SDOperand> RegValuesToPass;
868 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
869 switch (getValueType(Args[i].second)) {
870 default: assert(0 && "Unexpected ValueType for argument!");
871 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000872 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
873 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000874 case MVT::i8:
875 case MVT::i16:
876 case MVT::i32:
877 if (NumIntRegs < 2) {
878 RegValuesToPass.push_back(Args[i].first);
879 ++NumIntRegs;
880 break;
881 }
882 // Fall through
883 case MVT::f32: {
884 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
885 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
886 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
887 Args[i].first, PtrOff,
888 DAG.getSrcValue(NULL)));
889 ArgOffset += 4;
890 break;
891 }
892 case MVT::i64:
893 if (NumIntRegs < 2) { // Can pass part of it in regs?
894 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
895 Args[i].first, DAG.getConstant(1, MVT::i32));
896 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
897 Args[i].first, DAG.getConstant(0, MVT::i32));
898 RegValuesToPass.push_back(Lo);
899 ++NumIntRegs;
900 if (NumIntRegs < 2) { // Pass both parts in regs?
901 RegValuesToPass.push_back(Hi);
902 ++NumIntRegs;
903 } else {
904 // Pass the high part in memory.
905 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
906 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
907 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
908 Hi, PtrOff, DAG.getSrcValue(NULL)));
909 ArgOffset += 4;
910 }
911 break;
912 }
913 // Fall through
914 case MVT::f64:
915 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
916 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
917 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
918 Args[i].first, PtrOff,
919 DAG.getSrcValue(NULL)));
920 ArgOffset += 8;
921 break;
922 }
923 }
924 if (!Stores.empty())
925 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
926
927 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
928 // arguments and the arguments after the retaddr has been pushed are aligned.
929 if ((ArgOffset & 7) == 0)
930 ArgOffset += 4;
931
932 std::vector<MVT::ValueType> RetVals;
933 MVT::ValueType RetTyVT = getValueType(RetTy);
934
935 RetVals.push_back(MVT::Other);
936
937 // The result values produced have to be legal. Promote the result.
938 switch (RetTyVT) {
939 case MVT::isVoid: break;
940 default:
941 RetVals.push_back(RetTyVT);
942 break;
943 case MVT::i1:
944 case MVT::i8:
945 case MVT::i16:
946 RetVals.push_back(MVT::i32);
947 break;
948 case MVT::f32:
949 if (X86ScalarSSE)
950 RetVals.push_back(MVT::f32);
951 else
952 RetVals.push_back(MVT::f64);
953 break;
954 case MVT::i64:
955 RetVals.push_back(MVT::i32);
956 RetVals.push_back(MVT::i32);
957 break;
958 }
959
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000960 // Build a sequence of copy-to-reg nodes chained together with token chain
961 // and flag operands which copy the outgoing args into registers.
962 SDOperand InFlag;
963 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
964 unsigned CCReg;
965 SDOperand RegToPass = RegValuesToPass[i];
966 switch (RegToPass.getValueType()) {
967 default: assert(0 && "Bad thing to pass in regs");
968 case MVT::i8:
969 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000970 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000971 case MVT::i16:
972 CCReg = (i == 0) ? X86::AX : X86::DX;
973 break;
974 case MVT::i32:
975 CCReg = (i == 0) ? X86::EAX : X86::EDX;
976 break;
977 }
978
979 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
980 InFlag = Chain.getValue(1);
981 }
982
983 std::vector<MVT::ValueType> NodeTys;
984 NodeTys.push_back(MVT::Other); // Returns a chain
985 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
986 std::vector<SDOperand> Ops;
987 Ops.push_back(Chain);
988 Ops.push_back(Callee);
989 if (InFlag.Val)
990 Ops.push_back(InFlag);
991
992 // FIXME: Do not generate X86ISD::TAILCALL for now.
993 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
994 InFlag = Chain.getValue(1);
995
996 NodeTys.clear();
997 NodeTys.push_back(MVT::Other); // Returns a chain
998 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
999 Ops.clear();
1000 Ops.push_back(Chain);
1001 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1002 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1003 Ops.push_back(InFlag);
1004 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1005 InFlag = Chain.getValue(1);
1006
1007 SDOperand RetVal;
1008 if (RetTyVT != MVT::isVoid) {
1009 switch (RetTyVT) {
1010 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001011 case MVT::i1:
1012 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001013 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1014 Chain = RetVal.getValue(1);
1015 if (RetTyVT == MVT::i1)
1016 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1017 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001018 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001019 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1020 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001021 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001022 case MVT::i32:
1023 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1024 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001025 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001026 case MVT::i64: {
1027 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1028 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1029 Lo.getValue(2));
1030 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1031 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001032 break;
1033 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001034 case MVT::f32:
1035 case MVT::f64: {
1036 std::vector<MVT::ValueType> Tys;
1037 Tys.push_back(MVT::f64);
1038 Tys.push_back(MVT::Other);
1039 Tys.push_back(MVT::Flag);
1040 std::vector<SDOperand> Ops;
1041 Ops.push_back(Chain);
1042 Ops.push_back(InFlag);
1043 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1044 Chain = RetVal.getValue(1);
1045 InFlag = RetVal.getValue(2);
1046 if (X86ScalarSSE) {
1047 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1048 // shouldn't be necessary except that RFP cannot be live across
1049 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1050 MachineFunction &MF = DAG.getMachineFunction();
1051 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1052 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1053 Tys.clear();
1054 Tys.push_back(MVT::Other);
1055 Ops.clear();
1056 Ops.push_back(Chain);
1057 Ops.push_back(RetVal);
1058 Ops.push_back(StackSlot);
1059 Ops.push_back(DAG.getValueType(RetTyVT));
1060 Ops.push_back(InFlag);
1061 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1062 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1063 DAG.getSrcValue(NULL));
1064 Chain = RetVal.getValue(1);
1065 }
Evan Chengd9558e02006-01-06 00:43:03 +00001066
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001067 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1068 // FIXME: we would really like to remember that this FP_ROUND
1069 // operation is okay to eliminate if we allow excess FP precision.
1070 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1071 break;
1072 }
1073 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001074 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001075
1076 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001077}
1078
1079SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1080 if (ReturnAddrIndex == 0) {
1081 // Set up a frame object for the return address.
1082 MachineFunction &MF = DAG.getMachineFunction();
1083 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1084 }
1085
1086 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1087}
1088
1089
1090
1091std::pair<SDOperand, SDOperand> X86TargetLowering::
1092LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1093 SelectionDAG &DAG) {
1094 SDOperand Result;
1095 if (Depth) // Depths > 0 not supported yet!
1096 Result = DAG.getConstant(0, getPointerTy());
1097 else {
1098 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1099 if (!isFrameAddress)
1100 // Just load the return address
1101 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1102 DAG.getSrcValue(NULL));
1103 else
1104 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1105 DAG.getConstant(4, MVT::i32));
1106 }
1107 return std::make_pair(Result, Chain);
1108}
1109
Evan Cheng4a460802006-01-11 00:33:36 +00001110/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1111/// which corresponds to the condition code.
1112static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1113 switch (X86CC) {
1114 default: assert(0 && "Unknown X86 conditional code!");
1115 case X86ISD::COND_A: return X86::JA;
1116 case X86ISD::COND_AE: return X86::JAE;
1117 case X86ISD::COND_B: return X86::JB;
1118 case X86ISD::COND_BE: return X86::JBE;
1119 case X86ISD::COND_E: return X86::JE;
1120 case X86ISD::COND_G: return X86::JG;
1121 case X86ISD::COND_GE: return X86::JGE;
1122 case X86ISD::COND_L: return X86::JL;
1123 case X86ISD::COND_LE: return X86::JLE;
1124 case X86ISD::COND_NE: return X86::JNE;
1125 case X86ISD::COND_NO: return X86::JNO;
1126 case X86ISD::COND_NP: return X86::JNP;
1127 case X86ISD::COND_NS: return X86::JNS;
1128 case X86ISD::COND_O: return X86::JO;
1129 case X86ISD::COND_P: return X86::JP;
1130 case X86ISD::COND_S: return X86::JS;
1131 }
1132}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001133
Evan Cheng6dfa9992006-01-30 23:41:35 +00001134/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1135/// specific condition code. It returns a false if it cannot do a direct
1136/// translation. X86CC is the translated CondCode. Flip is set to true if the
1137/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001138static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1139 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001140 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001141 Flip = false;
1142 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001143 if (!isFP) {
1144 switch (SetCCOpcode) {
1145 default: break;
1146 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1147 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1148 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1149 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1150 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1151 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1152 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1153 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1154 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1155 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1156 }
1157 } else {
1158 // On a floating point condition, the flags are set as follows:
1159 // ZF PF CF op
1160 // 0 | 0 | 0 | X > Y
1161 // 0 | 0 | 1 | X < Y
1162 // 1 | 0 | 0 | X == Y
1163 // 1 | 1 | 1 | unordered
1164 switch (SetCCOpcode) {
1165 default: break;
1166 case ISD::SETUEQ:
1167 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001168 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001169 case ISD::SETOGT:
1170 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001171 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001172 case ISD::SETOGE:
1173 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001174 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001175 case ISD::SETULT:
1176 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001177 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001178 case ISD::SETULE:
1179 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1180 case ISD::SETONE:
1181 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1182 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1183 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1184 }
1185 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001186
1187 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001188}
1189
Evan Cheng4a460802006-01-11 00:33:36 +00001190/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1191/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001192/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001193static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001194 switch (X86CC) {
1195 default:
1196 return false;
1197 case X86ISD::COND_B:
1198 case X86ISD::COND_BE:
1199 case X86ISD::COND_E:
1200 case X86ISD::COND_P:
1201 case X86ISD::COND_A:
1202 case X86ISD::COND_AE:
1203 case X86ISD::COND_NE:
1204 case X86ISD::COND_NP:
1205 return true;
1206 }
1207}
1208
Evan Cheng4a460802006-01-11 00:33:36 +00001209MachineBasicBlock *
1210X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1211 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001212 switch (MI->getOpcode()) {
1213 default: assert(false && "Unexpected instr type to insert");
1214 case X86::CMOV_FR32:
1215 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001216 // To "insert" a SELECT_CC instruction, we actually have to insert the
1217 // diamond control-flow pattern. The incoming instruction knows the
1218 // destination vreg to set, the condition code register to branch on, the
1219 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001220 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1221 ilist<MachineBasicBlock>::iterator It = BB;
1222 ++It;
1223
1224 // thisMBB:
1225 // ...
1226 // TrueVal = ...
1227 // cmpTY ccX, r1, r2
1228 // bCC copy1MBB
1229 // fallthrough --> copy0MBB
1230 MachineBasicBlock *thisMBB = BB;
1231 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1232 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1233 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1234 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1235 MachineFunction *F = BB->getParent();
1236 F->getBasicBlockList().insert(It, copy0MBB);
1237 F->getBasicBlockList().insert(It, sinkMBB);
1238 // Update machine-CFG edges
1239 BB->addSuccessor(copy0MBB);
1240 BB->addSuccessor(sinkMBB);
1241
1242 // copy0MBB:
1243 // %FalseValue = ...
1244 // # fallthrough to sinkMBB
1245 BB = copy0MBB;
1246
1247 // Update machine-CFG edges
1248 BB->addSuccessor(sinkMBB);
1249
1250 // sinkMBB:
1251 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1252 // ...
1253 BB = sinkMBB;
1254 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1255 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1256 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001257
Evan Cheng0cc39452006-01-16 21:21:29 +00001258 delete MI; // The pseudo instruction is gone now.
1259 return BB;
1260 }
Evan Cheng4a460802006-01-11 00:33:36 +00001261
Evan Cheng0cc39452006-01-16 21:21:29 +00001262 case X86::FP_TO_INT16_IN_MEM:
1263 case X86::FP_TO_INT32_IN_MEM:
1264 case X86::FP_TO_INT64_IN_MEM: {
1265 // Change the floating point control register to use "round towards zero"
1266 // mode when truncating to an integer value.
1267 MachineFunction *F = BB->getParent();
1268 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1269 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1270
1271 // Load the old value of the high byte of the control word...
1272 unsigned OldCW =
1273 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1274 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1275
1276 // Set the high part to be round to zero...
1277 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1278
1279 // Reload the modified control word now...
1280 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1281
1282 // Restore the memory image of control word to original value
1283 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1284
1285 // Get the X86 opcode to use.
1286 unsigned Opc;
1287 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001288 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001289 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1290 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1291 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1292 }
1293
1294 X86AddressMode AM;
1295 MachineOperand &Op = MI->getOperand(0);
1296 if (Op.isRegister()) {
1297 AM.BaseType = X86AddressMode::RegBase;
1298 AM.Base.Reg = Op.getReg();
1299 } else {
1300 AM.BaseType = X86AddressMode::FrameIndexBase;
1301 AM.Base.FrameIndex = Op.getFrameIndex();
1302 }
1303 Op = MI->getOperand(1);
1304 if (Op.isImmediate())
1305 AM.Scale = Op.getImmedValue();
1306 Op = MI->getOperand(2);
1307 if (Op.isImmediate())
1308 AM.IndexReg = Op.getImmedValue();
1309 Op = MI->getOperand(3);
1310 if (Op.isGlobalAddress()) {
1311 AM.GV = Op.getGlobal();
1312 } else {
1313 AM.Disp = Op.getImmedValue();
1314 }
1315 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1316
1317 // Reload the original control word now.
1318 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1319
1320 delete MI; // The pseudo instruction is gone now.
1321 return BB;
1322 }
1323 }
Evan Cheng4a460802006-01-11 00:33:36 +00001324}
1325
1326
1327//===----------------------------------------------------------------------===//
1328// X86 Custom Lowering Hooks
1329//===----------------------------------------------------------------------===//
1330
Evan Cheng30b37b52006-03-13 23:18:16 +00001331/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1332/// load. For Darwin, external and weak symbols are indirect, loading the value
1333/// at address GV rather then the value of GV itself. This means that the
1334/// GlobalAddress must be in the base or index register of the address, not the
1335/// GV offset field.
1336static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1337 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1338 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1339}
1340
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001341/// LowerOperation - Provide custom lowering hooks for some operations.
1342///
1343SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1344 switch (Op.getOpcode()) {
1345 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001346 case ISD::SHL_PARTS:
1347 case ISD::SRA_PARTS:
1348 case ISD::SRL_PARTS: {
1349 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1350 "Not an i64 shift!");
1351 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1352 SDOperand ShOpLo = Op.getOperand(0);
1353 SDOperand ShOpHi = Op.getOperand(1);
1354 SDOperand ShAmt = Op.getOperand(2);
1355 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001356 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001357 : DAG.getConstant(0, MVT::i32);
1358
1359 SDOperand Tmp2, Tmp3;
1360 if (Op.getOpcode() == ISD::SHL_PARTS) {
1361 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1362 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1363 } else {
1364 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001365 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001366 }
1367
1368 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1369 ShAmt, DAG.getConstant(32, MVT::i8));
1370
1371 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001372 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001373
1374 std::vector<MVT::ValueType> Tys;
1375 Tys.push_back(MVT::i32);
1376 Tys.push_back(MVT::Flag);
1377 std::vector<SDOperand> Ops;
1378 if (Op.getOpcode() == ISD::SHL_PARTS) {
1379 Ops.push_back(Tmp2);
1380 Ops.push_back(Tmp3);
1381 Ops.push_back(CC);
1382 Ops.push_back(InFlag);
1383 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1384 InFlag = Hi.getValue(1);
1385
1386 Ops.clear();
1387 Ops.push_back(Tmp3);
1388 Ops.push_back(Tmp1);
1389 Ops.push_back(CC);
1390 Ops.push_back(InFlag);
1391 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1392 } else {
1393 Ops.push_back(Tmp2);
1394 Ops.push_back(Tmp3);
1395 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001396 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001397 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1398 InFlag = Lo.getValue(1);
1399
1400 Ops.clear();
1401 Ops.push_back(Tmp3);
1402 Ops.push_back(Tmp1);
1403 Ops.push_back(CC);
1404 Ops.push_back(InFlag);
1405 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1406 }
1407
1408 Tys.clear();
1409 Tys.push_back(MVT::i32);
1410 Tys.push_back(MVT::i32);
1411 Ops.clear();
1412 Ops.push_back(Lo);
1413 Ops.push_back(Hi);
1414 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1415 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001416 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001417 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001418 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001419 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001420
1421 SDOperand Result;
1422 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1423 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001424 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001425 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001426 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001427 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1428 DAG.getEntryNode(), Op.getOperand(0),
1429 StackSlot, DAG.getSrcValue(NULL));
1430
1431 // Build the FILD
1432 std::vector<MVT::ValueType> Tys;
1433 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001434 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001435 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001436 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001437 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001438 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001439 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001440 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1441 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001442
1443 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001444 Chain = Result.getValue(1);
1445 SDOperand InFlag = Result.getValue(2);
1446
Evan Chenge3de85b2006-02-04 02:20:30 +00001447 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001448 // shouldn't be necessary except that RFP cannot be live across
1449 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1450 MachineFunction &MF = DAG.getMachineFunction();
1451 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1452 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1453 std::vector<MVT::ValueType> Tys;
1454 Tys.push_back(MVT::Other);
1455 std::vector<SDOperand> Ops;
1456 Ops.push_back(Chain);
1457 Ops.push_back(Result);
1458 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001459 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001460 Ops.push_back(InFlag);
1461 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1462 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1463 DAG.getSrcValue(NULL));
1464 }
1465
Evan Chenga3195e82006-01-12 22:54:21 +00001466 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001467 }
1468 case ISD::FP_TO_SINT: {
1469 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001470 "Unknown FP_TO_SINT to lower!");
1471 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1472 // stack slot.
1473 MachineFunction &MF = DAG.getMachineFunction();
1474 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1475 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1476 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1477
1478 unsigned Opc;
1479 switch (Op.getValueType()) {
1480 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1481 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1482 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1483 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1484 }
1485
Evan Cheng6dab0532006-01-30 08:02:57 +00001486 SDOperand Chain = DAG.getEntryNode();
1487 SDOperand Value = Op.getOperand(0);
1488 if (X86ScalarSSE) {
1489 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1490 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1491 DAG.getSrcValue(0));
1492 std::vector<MVT::ValueType> Tys;
1493 Tys.push_back(MVT::f64);
1494 Tys.push_back(MVT::Other);
1495 std::vector<SDOperand> Ops;
1496 Ops.push_back(Chain);
1497 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001498 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001499 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1500 Chain = Value.getValue(1);
1501 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1502 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1503 }
1504
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001505 // Build the FP_TO_INT*_IN_MEM
1506 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001507 Ops.push_back(Chain);
1508 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001509 Ops.push_back(StackSlot);
1510 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1511
1512 // Load the result.
1513 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1514 DAG.getSrcValue(NULL));
1515 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001516 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001517 std::vector<MVT::ValueType> Tys;
1518 Tys.push_back(MVT::Other);
1519 Tys.push_back(MVT::Flag);
1520 std::vector<SDOperand> Ops;
1521 Ops.push_back(Op.getOperand(0));
1522 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001523 Ops.clear();
1524 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1525 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1526 MVT::i32, Ops[0].getValue(2)));
1527 Ops.push_back(Ops[1].getValue(1));
1528 Tys[0] = Tys[1] = MVT::i32;
1529 Tys.push_back(MVT::Other);
1530 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001531 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001532 case ISD::FABS: {
1533 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001534 const Type *OpNTy = MVT::getTypeForValueType(VT);
1535 std::vector<Constant*> CV;
1536 if (VT == MVT::f64) {
1537 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1538 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1539 } else {
1540 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1541 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1542 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1543 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1544 }
1545 Constant *CS = ConstantStruct::get(CV);
1546 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1547 SDOperand Mask
1548 = DAG.getNode(X86ISD::LOAD_PACK,
1549 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001550 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1551 }
Evan Cheng223547a2006-01-31 22:28:30 +00001552 case ISD::FNEG: {
1553 MVT::ValueType VT = Op.getValueType();
1554 const Type *OpNTy = MVT::getTypeForValueType(VT);
1555 std::vector<Constant*> CV;
1556 if (VT == MVT::f64) {
1557 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1558 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1559 } else {
1560 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1561 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1562 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1563 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1564 }
1565 Constant *CS = ConstantStruct::get(CV);
1566 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1567 SDOperand Mask
1568 = DAG.getNode(X86ISD::LOAD_PACK,
1569 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1570 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1571 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001572 case ISD::SETCC: {
1573 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001574 SDOperand Cond;
1575 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001576 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1577 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001578 bool Flip;
1579 unsigned X86CC;
1580 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1581 if (Flip)
1582 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1583 Op.getOperand(1), Op.getOperand(0));
1584 else
1585 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1586 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001587 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1588 DAG.getConstant(X86CC, MVT::i8), Cond);
1589 } else {
1590 assert(isFP && "Illegal integer SetCC!");
1591
Evan Cheng6dfa9992006-01-30 23:41:35 +00001592 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1593 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001594 std::vector<MVT::ValueType> Tys;
1595 std::vector<SDOperand> Ops;
1596 switch (SetCCOpcode) {
1597 default: assert(false && "Illegal floating point SetCC!");
1598 case ISD::SETOEQ: { // !PF & ZF
1599 Tys.push_back(MVT::i8);
1600 Tys.push_back(MVT::Flag);
1601 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1602 Ops.push_back(Cond);
1603 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1604 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1605 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1606 Tmp1.getValue(1));
1607 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1608 }
Evan Chengd9558e02006-01-06 00:43:03 +00001609 case ISD::SETUNE: { // PF | !ZF
1610 Tys.push_back(MVT::i8);
1611 Tys.push_back(MVT::Flag);
1612 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1613 Ops.push_back(Cond);
1614 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1615 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1616 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1617 Tmp1.getValue(1));
1618 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1619 }
1620 }
1621 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001622 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001623 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001624 MVT::ValueType VT = Op.getValueType();
1625 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001626 bool isFPStack = isFP && !X86ScalarSSE;
1627 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001628 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001629 SDOperand Op0 = Op.getOperand(0);
1630 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001631 if (Op0.getOpcode() == ISD::SETCC)
1632 Op0 = LowerOperation(Op0, DAG);
1633
Evan Chengaaca22c2006-01-10 20:26:56 +00001634 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001635 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1636 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1637 // have another use it will be eliminated.
1638 // If the X86ISD::SETCC has more than one use, then it's probably better
1639 // to use a test instead of duplicating the X86ISD::CMP (for register
1640 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001641 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1642 if (!Op0.hasOneUse()) {
1643 std::vector<MVT::ValueType> Tys;
1644 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1645 Tys.push_back(Op0.Val->getValueType(i));
1646 std::vector<SDOperand> Ops;
1647 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1648 Ops.push_back(Op0.getOperand(i));
1649 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1650 }
1651
Evan Cheng1bcee362006-01-13 01:03:02 +00001652 CC = Op0.getOperand(0);
1653 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001654 // Make a copy as flag result cannot be used by more than one.
1655 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1656 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001657 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001658 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001659 } else
1660 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001661 } else
1662 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001663
Evan Cheng189d01e2006-01-13 01:06:49 +00001664 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001665 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001666 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001667 }
Evan Chenge3413162006-01-09 18:33:28 +00001668
1669 std::vector<MVT::ValueType> Tys;
1670 Tys.push_back(Op.getValueType());
1671 Tys.push_back(MVT::Flag);
1672 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001673 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1674 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001675 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001676 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001677 Ops.push_back(CC);
1678 Ops.push_back(Cond);
1679 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001680 }
Evan Cheng898101c2005-12-19 23:12:38 +00001681 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001682 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001683 SDOperand Cond = Op.getOperand(1);
1684 SDOperand Dest = Op.getOperand(2);
1685 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001686 if (Cond.getOpcode() == ISD::SETCC)
1687 Cond = LowerOperation(Cond, DAG);
1688
Evan Chengd5781fc2005-12-21 20:21:51 +00001689 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001690 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1691 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1692 // have another use it will be eliminated.
1693 // If the X86ISD::SETCC has more than one use, then it's probably better
1694 // to use a test instead of duplicating the X86ISD::CMP (for register
1695 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001696 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1697 if (!Cond.hasOneUse()) {
1698 std::vector<MVT::ValueType> Tys;
1699 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1700 Tys.push_back(Cond.Val->getValueType(i));
1701 std::vector<SDOperand> Ops;
1702 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1703 Ops.push_back(Cond.getOperand(i));
1704 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1705 }
1706
Evan Cheng1bcee362006-01-13 01:03:02 +00001707 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001708 Cond = Cond.getOperand(1);
1709 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001710 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001711 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001712 } else
1713 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001714 } else
1715 addTest = true;
1716
1717 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001718 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001719 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1720 }
1721 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1722 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1723 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001724 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001725 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001726 SDOperand Chain = Op.getOperand(0);
1727 unsigned Align =
1728 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1729 if (Align == 0) Align = 1;
1730
Evan Cheng18a84522006-02-16 00:21:07 +00001731 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1732 // If not DWORD aligned, call memset if size is less than the threshold.
1733 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001734 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001735 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001736 MVT::ValueType IntPtr = getPointerTy();
1737 const Type *IntPtrTy = getTargetData().getIntPtrType();
1738 std::vector<std::pair<SDOperand, const Type*> > Args;
1739 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1740 // Extend the ubyte argument to be an int value for the call.
1741 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1742 Args.push_back(std::make_pair(Val, IntPtrTy));
1743 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1744 std::pair<SDOperand,SDOperand> CallResult =
1745 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1746 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1747 return CallResult.second;
1748 }
1749
Evan Cheng67f92a72006-01-11 22:15:48 +00001750 MVT::ValueType AVT;
1751 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001752 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1753 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001754 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001755 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001756 unsigned ValReg;
1757 unsigned Val = ValC->getValue() & 255;
1758
1759 // If the value is a constant, then we can potentially use larger sets.
1760 switch (Align & 3) {
1761 case 2: // WORD aligned
1762 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001763 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1764 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001765 Val = (Val << 8) | Val;
1766 ValReg = X86::AX;
1767 break;
1768 case 0: // DWORD aligned
1769 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001770 if (I) {
1771 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1772 BytesLeft = I->getValue() % 4;
1773 } else {
1774 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1775 DAG.getConstant(2, MVT::i8));
1776 TwoRepStos = true;
1777 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001778 Val = (Val << 8) | Val;
1779 Val = (Val << 16) | Val;
1780 ValReg = X86::EAX;
1781 break;
1782 default: // Byte aligned
1783 AVT = MVT::i8;
1784 Count = Op.getOperand(3);
1785 ValReg = X86::AL;
1786 break;
1787 }
1788
1789 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1790 InFlag);
1791 InFlag = Chain.getValue(1);
1792 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001793 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001794 Count = Op.getOperand(3);
1795 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1796 InFlag = Chain.getValue(1);
1797 }
1798
1799 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1800 InFlag = Chain.getValue(1);
1801 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1802 InFlag = Chain.getValue(1);
1803
Evan Chengff909922006-03-07 23:29:39 +00001804 std::vector<MVT::ValueType> Tys;
1805 Tys.push_back(MVT::Other);
1806 Tys.push_back(MVT::Flag);
1807 std::vector<SDOperand> Ops;
1808 Ops.push_back(Chain);
1809 Ops.push_back(DAG.getValueType(AVT));
1810 Ops.push_back(InFlag);
1811 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1812
1813 if (TwoRepStos) {
1814 InFlag = Chain.getValue(1);
1815 Count = Op.getOperand(3);
1816 MVT::ValueType CVT = Count.getValueType();
1817 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1818 DAG.getConstant(3, CVT));
1819 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1820 InFlag = Chain.getValue(1);
1821 Tys.clear();
1822 Tys.push_back(MVT::Other);
1823 Tys.push_back(MVT::Flag);
1824 Ops.clear();
1825 Ops.push_back(Chain);
1826 Ops.push_back(DAG.getValueType(MVT::i8));
1827 Ops.push_back(InFlag);
1828 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1829 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001830 // Issue stores for the last 1 - 3 bytes.
1831 SDOperand Value;
1832 unsigned Val = ValC->getValue() & 255;
1833 unsigned Offset = I->getValue() - BytesLeft;
1834 SDOperand DstAddr = Op.getOperand(1);
1835 MVT::ValueType AddrVT = DstAddr.getValueType();
1836 if (BytesLeft >= 2) {
1837 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1838 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1839 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1840 DAG.getConstant(Offset, AddrVT)),
1841 DAG.getSrcValue(NULL));
1842 BytesLeft -= 2;
1843 Offset += 2;
1844 }
1845
1846 if (BytesLeft == 1) {
1847 Value = DAG.getConstant(Val, MVT::i8);
1848 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1849 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1850 DAG.getConstant(Offset, AddrVT)),
1851 DAG.getSrcValue(NULL));
1852 }
1853 }
1854
1855 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001856 }
1857 case ISD::MEMCPY: {
1858 SDOperand Chain = Op.getOperand(0);
1859 unsigned Align =
1860 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1861 if (Align == 0) Align = 1;
1862
Evan Cheng18a84522006-02-16 00:21:07 +00001863 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1864 // If not DWORD aligned, call memcpy if size is less than the threshold.
1865 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001866 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001867 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001868 MVT::ValueType IntPtr = getPointerTy();
1869 const Type *IntPtrTy = getTargetData().getIntPtrType();
1870 std::vector<std::pair<SDOperand, const Type*> > Args;
1871 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1872 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1873 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1874 std::pair<SDOperand,SDOperand> CallResult =
1875 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1876 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1877 return CallResult.second;
1878 }
1879
Evan Cheng67f92a72006-01-11 22:15:48 +00001880 MVT::ValueType AVT;
1881 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001882 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001883 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001884 switch (Align & 3) {
1885 case 2: // WORD aligned
1886 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001887 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1888 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001889 break;
1890 case 0: // DWORD aligned
1891 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001892 if (I) {
1893 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1894 BytesLeft = I->getValue() % 4;
1895 } else {
1896 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1897 DAG.getConstant(2, MVT::i8));
1898 TwoRepMovs = true;
1899 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001900 break;
1901 default: // Byte aligned
1902 AVT = MVT::i8;
1903 Count = Op.getOperand(3);
1904 break;
1905 }
1906
Evan Cheng62bec2c2006-03-04 02:48:56 +00001907 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001908 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1909 InFlag = Chain.getValue(1);
1910 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1911 InFlag = Chain.getValue(1);
1912 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1913 InFlag = Chain.getValue(1);
1914
Evan Chengff909922006-03-07 23:29:39 +00001915 std::vector<MVT::ValueType> Tys;
1916 Tys.push_back(MVT::Other);
1917 Tys.push_back(MVT::Flag);
1918 std::vector<SDOperand> Ops;
1919 Ops.push_back(Chain);
1920 Ops.push_back(DAG.getValueType(AVT));
1921 Ops.push_back(InFlag);
1922 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1923
1924 if (TwoRepMovs) {
1925 InFlag = Chain.getValue(1);
1926 Count = Op.getOperand(3);
1927 MVT::ValueType CVT = Count.getValueType();
1928 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1929 DAG.getConstant(3, CVT));
1930 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1931 InFlag = Chain.getValue(1);
1932 Tys.clear();
1933 Tys.push_back(MVT::Other);
1934 Tys.push_back(MVT::Flag);
1935 Ops.clear();
1936 Ops.push_back(Chain);
1937 Ops.push_back(DAG.getValueType(MVT::i8));
1938 Ops.push_back(InFlag);
1939 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1940 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001941 // Issue loads and stores for the last 1 - 3 bytes.
1942 unsigned Offset = I->getValue() - BytesLeft;
1943 SDOperand DstAddr = Op.getOperand(1);
1944 MVT::ValueType DstVT = DstAddr.getValueType();
1945 SDOperand SrcAddr = Op.getOperand(2);
1946 MVT::ValueType SrcVT = SrcAddr.getValueType();
1947 SDOperand Value;
1948 if (BytesLeft >= 2) {
1949 Value = DAG.getLoad(MVT::i16, Chain,
1950 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1951 DAG.getConstant(Offset, SrcVT)),
1952 DAG.getSrcValue(NULL));
1953 Chain = Value.getValue(1);
1954 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1955 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1956 DAG.getConstant(Offset, DstVT)),
1957 DAG.getSrcValue(NULL));
1958 BytesLeft -= 2;
1959 Offset += 2;
1960 }
1961
1962 if (BytesLeft == 1) {
1963 Value = DAG.getLoad(MVT::i8, Chain,
1964 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1965 DAG.getConstant(Offset, SrcVT)),
1966 DAG.getSrcValue(NULL));
1967 Chain = Value.getValue(1);
1968 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1969 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1970 DAG.getConstant(Offset, DstVT)),
1971 DAG.getSrcValue(NULL));
1972 }
1973 }
1974
1975 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001976 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00001977
1978 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1979 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1980 // one of the above mentioned nodes. It has to be wrapped because otherwise
1981 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1982 // be used to form addressing mode. These wrapped nodes will be selected
1983 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00001984 case ISD::ConstantPool: {
1985 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00001986 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1987 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1988 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00001989 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00001990 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001991 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00001992 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1993 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1994 }
1995
1996 return Result;
1997 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001998 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00001999 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2000 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2001 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002002 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002003 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002004 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002005 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002006 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2007 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002008
2009 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002010 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002011 // the GlobalAddress must be in the base or index register of the address,
2012 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002013 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002014 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002015 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002016 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002017 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002018
Evan Cheng002fe9b2006-01-12 07:56:47 +00002019 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002020 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002021 case ISD::ExternalSymbol: {
2022 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2023 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2024 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2025 if (getTargetMachine().
2026 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2027 // With PIC, the address is actually $g + Offset.
2028 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2029 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2030 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2031 }
2032
2033 return Result;
2034 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002035 case ISD::VASTART: {
2036 // vastart just stores the address of the VarArgsFrameIndex slot into the
2037 // memory location argument.
2038 // FIXME: Replace MVT::i32 with PointerTy
2039 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2040 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2041 Op.getOperand(1), Op.getOperand(2));
2042 }
Nate Begemanee625572006-01-27 21:09:22 +00002043 case ISD::RET: {
2044 SDOperand Copy;
2045
2046 switch(Op.getNumOperands()) {
2047 default:
2048 assert(0 && "Do not know how to return this many arguments!");
2049 abort();
2050 case 1:
2051 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2052 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2053 case 2: {
2054 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2055 if (MVT::isInteger(ArgVT))
2056 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2057 SDOperand());
2058 else if (!X86ScalarSSE) {
2059 std::vector<MVT::ValueType> Tys;
2060 Tys.push_back(MVT::Other);
2061 Tys.push_back(MVT::Flag);
2062 std::vector<SDOperand> Ops;
2063 Ops.push_back(Op.getOperand(0));
2064 Ops.push_back(Op.getOperand(1));
2065 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2066 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002067 SDOperand MemLoc;
2068 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002069 SDOperand Value = Op.getOperand(1);
2070
Evan Cheng760df292006-02-01 01:19:32 +00002071 if (Value.getOpcode() == ISD::LOAD &&
2072 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002073 Chain = Value.getOperand(0);
2074 MemLoc = Value.getOperand(1);
2075 } else {
2076 // Spill the value to memory and reload it into top of stack.
2077 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2078 MachineFunction &MF = DAG.getMachineFunction();
2079 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2080 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2081 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2082 Value, MemLoc, DAG.getSrcValue(0));
2083 }
Nate Begemanee625572006-01-27 21:09:22 +00002084 std::vector<MVT::ValueType> Tys;
2085 Tys.push_back(MVT::f64);
2086 Tys.push_back(MVT::Other);
2087 std::vector<SDOperand> Ops;
2088 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002089 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002090 Ops.push_back(DAG.getValueType(ArgVT));
2091 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2092 Tys.clear();
2093 Tys.push_back(MVT::Other);
2094 Tys.push_back(MVT::Flag);
2095 Ops.clear();
2096 Ops.push_back(Copy.getValue(1));
2097 Ops.push_back(Copy);
2098 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2099 }
2100 break;
2101 }
2102 case 3:
2103 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2104 SDOperand());
2105 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2106 break;
2107 }
2108 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2109 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2110 Copy.getValue(1));
2111 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002112 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002113}
Evan Cheng72261582005-12-20 06:22:03 +00002114
2115const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2116 switch (Opcode) {
2117 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002118 case X86ISD::SHLD: return "X86ISD::SHLD";
2119 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002120 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002121 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002122 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002123 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002124 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2125 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2126 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002127 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002128 case X86ISD::FST: return "X86ISD::FST";
2129 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002130 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002131 case X86ISD::CALL: return "X86ISD::CALL";
2132 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2133 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2134 case X86ISD::CMP: return "X86ISD::CMP";
2135 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002136 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002137 case X86ISD::CMOV: return "X86ISD::CMOV";
2138 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002139 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002140 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2141 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002142 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002143 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002144 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002145 }
2146}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002147
Nate Begeman368e18d2006-02-16 21:11:51 +00002148void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2149 uint64_t Mask,
2150 uint64_t &KnownZero,
2151 uint64_t &KnownOne,
2152 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002153
2154 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002155 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002156
2157 switch (Opc) {
2158 default:
2159 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2160 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002161 case X86ISD::SETCC:
2162 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2163 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002164 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002165}
Chris Lattner259e97c2006-01-31 19:43:35 +00002166
2167std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002168getRegClassForInlineAsmConstraint(const std::string &Constraint,
2169 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002170 if (Constraint.size() == 1) {
2171 // FIXME: not handling fp-stack yet!
2172 // FIXME: not handling MMX registers yet ('y' constraint).
2173 switch (Constraint[0]) { // GCC X86 Constraint Letters
2174 default: break; // Unknown constriant letter
2175 case 'r': // GENERAL_REGS
2176 case 'R': // LEGACY_REGS
2177 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2178 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2179 case 'l': // INDEX_REGS
2180 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2181 X86::ESI, X86::EDI, X86::EBP, 0);
2182 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2183 case 'Q': // Q_REGS
2184 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2185 case 'x': // SSE_REGS if SSE1 allowed
2186 if (Subtarget->hasSSE1())
2187 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2188 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2189 0);
2190 return std::vector<unsigned>();
2191 case 'Y': // SSE_REGS if SSE2 allowed
2192 if (Subtarget->hasSSE2())
2193 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2194 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2195 0);
2196 return std::vector<unsigned>();
2197 }
2198 }
2199
Chris Lattner1efa40f2006-02-22 00:56:39 +00002200 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002201}
Evan Cheng30b37b52006-03-13 23:18:16 +00002202
2203/// isLegalAddressImmediate - Return true if the integer value or
2204/// GlobalValue can be used as the offset of the target addressing mode.
2205bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2206 // X86 allows a sign-extended 32-bit immediate field.
2207 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2208}
2209
2210bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2211 if (getTargetMachine().
2212 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2213 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2214 if (RModel == Reloc::Static)
2215 return true;
2216 else if (RModel == Reloc::DynamicNoPIC)
2217 return DarwinGVRequiresExtraLoad(GV);
2218 else
2219 return false;
2220 } else
2221 return true;
2222}