blob: 105e6cebcb687f80e975987ce8587c63c8d1213f [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/Function.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000027#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner259e97c2006-01-31 19:43:35 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030using namespace llvm;
31
32// FIXME: temporary.
33#include "llvm/Support/CommandLine.h"
34static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
36
37X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000039 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
41
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042 // Set up the TargetLowering object.
43
44 // X86 is weird, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
47 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000048 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000050 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattnera54aa942006-01-29 06:26:08 +000051
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58 // operation.
59 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
61 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000062
63 if (X86ScalarSSE)
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
66 else
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70 // this operation.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000073 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000074 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000075 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000076 else {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
79 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080
Evan Cheng6dab0532006-01-30 08:02:57 +000081 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82 // isn't legal.
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
85
Evan Cheng02568ff2006-01-30 22:13:22 +000086 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
90
91 if (X86ScalarSSE) {
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
93 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +000095 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096 }
97
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
99 // conversion.
100 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
102 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
103
Evan Cheng45af8fd2006-02-18 07:26:17 +0000104 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000105 // Expand FP_TO_UINT into a select.
106 // FIXME: We would like to use a Custom expander here eventually to do
107 // the optimal thing for SSE vs. the default expansion in the legalizer.
108 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
109 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000110 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
112
Evan Cheng02568ff2006-01-30 22:13:22 +0000113 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
114 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000115
Evan Cheng5298bcc2006-02-17 07:01:52 +0000116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000117 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
118 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000119 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
120 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
125 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
126 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
127 setOperationAction(ISD::FREM , MVT::f64 , Expand);
128 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
129 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
130 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
132 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
133 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
136 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000137 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000138 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000139
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000140 setOperationAction(ISD::READIO , MVT::i1 , Expand);
141 setOperationAction(ISD::READIO , MVT::i8 , Expand);
142 setOperationAction(ISD::READIO , MVT::i16 , Expand);
143 setOperationAction(ISD::READIO , MVT::i32 , Expand);
144 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
145 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
146 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
147 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
148
149 // These should be promoted to a larger select which is supported.
150 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
151 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000152
153 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000154 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
155 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
156 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
157 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
158 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
159 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
160 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
161 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
162 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000163 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000164 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000165 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000166 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000167 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000168 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000169 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
170 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
171 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000172 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000173 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
174 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000175
Chris Lattnerf73bae12005-11-29 06:16:21 +0000176 // We don't have line number support yet.
177 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000178 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
179 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000180
Nate Begemanacc398c2006-01-25 18:21:52 +0000181 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
182 setOperationAction(ISD::VASTART , MVT::Other, Custom);
183
184 // Use the default implementation.
185 setOperationAction(ISD::VAARG , MVT::Other, Expand);
186 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
187 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000188 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
189 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
190 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000191
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000192 if (X86ScalarSSE) {
193 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000194 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
195 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000196
197 // SSE has no load+extend ops
198 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
199 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
200
Evan Cheng223547a2006-01-31 22:28:30 +0000201 // Use ANDPD to simulate FABS.
202 setOperationAction(ISD::FABS , MVT::f64, Custom);
203 setOperationAction(ISD::FABS , MVT::f32, Custom);
204
205 // Use XORP to simulate FNEG.
206 setOperationAction(ISD::FNEG , MVT::f64, Custom);
207 setOperationAction(ISD::FNEG , MVT::f32, Custom);
208
Evan Chengd25e9e82006-02-02 00:28:23 +0000209 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000210 setOperationAction(ISD::FSIN , MVT::f64, Expand);
211 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000212 setOperationAction(ISD::FREM , MVT::f64, Expand);
213 setOperationAction(ISD::FSIN , MVT::f32, Expand);
214 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215 setOperationAction(ISD::FREM , MVT::f32, Expand);
216
Chris Lattnera54aa942006-01-29 06:26:08 +0000217 // Expand FP immediates into loads from the stack, except for the special
218 // cases we handle.
219 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
220 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221 addLegalFPImmediate(+0.0); // xorps / xorpd
222 } else {
223 // Set up the FP register classes.
224 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000225
226 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
227
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228 if (!UnsafeFPMath) {
229 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
230 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
231 }
232
Chris Lattnera54aa942006-01-29 06:26:08 +0000233 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000234 addLegalFPImmediate(+0.0); // FLD0
235 addLegalFPImmediate(+1.0); // FLD1
236 addLegalFPImmediate(-0.0); // FLD0/FCHS
237 addLegalFPImmediate(-1.0); // FLD1/FCHS
238 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000239
240 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
241 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
242 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
243 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
244
245 // FIXME: We don't support any ConstantVec's yet. We should custom expand
246 // the ones we do!
247 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
248 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
249 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
250 }
251
252 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
253 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
254
255 // FIXME: We don't support any ConstantVec's yet. We should custom expand
256 // the ones we do!
257 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
258 }
259
260 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
261 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
262 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
263 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
264 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
265 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
266
267
268 // FIXME: We don't support any ConstantVec's yet. We should custom expand
269 // the ones we do!
270 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
271 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
272 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
273 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
274 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
275 }
276
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000277 computeRegisterProperties();
278
Evan Cheng87ed7162006-02-14 08:25:08 +0000279 // FIXME: These should be based on subtarget info. Plus, the values should
280 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000281 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
282 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
283 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284 allowUnalignedMemoryAccesses = true; // x86 supports it!
285}
286
287std::vector<SDOperand>
288X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
289 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
290 return LowerFastCCArguments(F, DAG);
291 return LowerCCCArguments(F, DAG);
292}
293
294std::pair<SDOperand, SDOperand>
295X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
296 bool isVarArg, unsigned CallingConv,
297 bool isTailCall,
298 SDOperand Callee, ArgListTy &Args,
299 SelectionDAG &DAG) {
300 assert((!isVarArg || CallingConv == CallingConv::C) &&
301 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000302
303 // If the callee is a GlobalAddress node (quite common, every direct call is)
304 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
305 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
306 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000307 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
308 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000309
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310 if (CallingConv == CallingConv::Fast && EnableFastCC)
311 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
312 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
313}
314
315//===----------------------------------------------------------------------===//
316// C Calling Convention implementation
317//===----------------------------------------------------------------------===//
318
319std::vector<SDOperand>
320X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
321 std::vector<SDOperand> ArgValues;
322
323 MachineFunction &MF = DAG.getMachineFunction();
324 MachineFrameInfo *MFI = MF.getFrameInfo();
325
326 // Add DAG nodes to load the arguments... On entry to a function on the X86,
327 // the stack frame looks like this:
328 //
329 // [ESP] -- return address
330 // [ESP + 4] -- first argument (leftmost lexically)
331 // [ESP + 8] -- second argument, if first argument is four bytes in size
332 // ...
333 //
334 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
335 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
336 MVT::ValueType ObjectVT = getValueType(I->getType());
337 unsigned ArgIncrement = 4;
338 unsigned ObjSize;
339 switch (ObjectVT) {
340 default: assert(0 && "Unhandled argument type!");
341 case MVT::i1:
342 case MVT::i8: ObjSize = 1; break;
343 case MVT::i16: ObjSize = 2; break;
344 case MVT::i32: ObjSize = 4; break;
345 case MVT::i64: ObjSize = ArgIncrement = 8; break;
346 case MVT::f32: ObjSize = 4; break;
347 case MVT::f64: ObjSize = ArgIncrement = 8; break;
348 }
349 // Create the frame index object for this incoming parameter...
350 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
351
352 // Create the SelectionDAG nodes corresponding to a load from this parameter
353 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
354
355 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
356 // dead loads.
357 SDOperand ArgValue;
358 if (!I->use_empty())
359 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
360 DAG.getSrcValue(NULL));
361 else {
362 if (MVT::isInteger(ObjectVT))
363 ArgValue = DAG.getConstant(0, ObjectVT);
364 else
365 ArgValue = DAG.getConstantFP(0, ObjectVT);
366 }
367 ArgValues.push_back(ArgValue);
368
369 ArgOffset += ArgIncrement; // Move on to the next argument...
370 }
371
372 // If the function takes variable number of arguments, make a frame index for
373 // the start of the first vararg value... for expansion of llvm.va_start.
374 if (F.isVarArg())
375 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
376 ReturnAddrIndex = 0; // No return address slot generated yet.
377 BytesToPopOnReturn = 0; // Callee pops nothing.
378 BytesCallerReserves = ArgOffset;
379
380 // Finally, inform the code generator which regs we return values in.
381 switch (getValueType(F.getReturnType())) {
382 default: assert(0 && "Unknown type!");
383 case MVT::isVoid: break;
384 case MVT::i1:
385 case MVT::i8:
386 case MVT::i16:
387 case MVT::i32:
388 MF.addLiveOut(X86::EAX);
389 break;
390 case MVT::i64:
391 MF.addLiveOut(X86::EAX);
392 MF.addLiveOut(X86::EDX);
393 break;
394 case MVT::f32:
395 case MVT::f64:
396 MF.addLiveOut(X86::ST0);
397 break;
398 }
399 return ArgValues;
400}
401
402std::pair<SDOperand, SDOperand>
403X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
404 bool isVarArg, bool isTailCall,
405 SDOperand Callee, ArgListTy &Args,
406 SelectionDAG &DAG) {
407 // Count how many bytes are to be pushed on the stack.
408 unsigned NumBytes = 0;
409
410 if (Args.empty()) {
411 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000412 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000413 } else {
414 for (unsigned i = 0, e = Args.size(); i != e; ++i)
415 switch (getValueType(Args[i].second)) {
416 default: assert(0 && "Unknown value type!");
417 case MVT::i1:
418 case MVT::i8:
419 case MVT::i16:
420 case MVT::i32:
421 case MVT::f32:
422 NumBytes += 4;
423 break;
424 case MVT::i64:
425 case MVT::f64:
426 NumBytes += 8;
427 break;
428 }
429
Chris Lattner94dd2922006-02-13 09:00:43 +0000430 Chain = DAG.getCALLSEQ_START(Chain,
431 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432
433 // Arguments go on the stack in reverse order, as specified by the ABI.
434 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000435 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000436 std::vector<SDOperand> Stores;
437
438 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
439 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
440 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
441
442 switch (getValueType(Args[i].second)) {
443 default: assert(0 && "Unexpected ValueType for argument!");
444 case MVT::i1:
445 case MVT::i8:
446 case MVT::i16:
447 // Promote the integer to 32 bits. If the input type is signed use a
448 // sign extend, otherwise use a zero extend.
449 if (Args[i].second->isSigned())
450 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
451 else
452 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
453
454 // FALL THROUGH
455 case MVT::i32:
456 case MVT::f32:
457 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
458 Args[i].first, PtrOff,
459 DAG.getSrcValue(NULL)));
460 ArgOffset += 4;
461 break;
462 case MVT::i64:
463 case MVT::f64:
464 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
465 Args[i].first, PtrOff,
466 DAG.getSrcValue(NULL)));
467 ArgOffset += 8;
468 break;
469 }
470 }
471 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
472 }
473
474 std::vector<MVT::ValueType> RetVals;
475 MVT::ValueType RetTyVT = getValueType(RetTy);
476 RetVals.push_back(MVT::Other);
477
478 // The result values produced have to be legal. Promote the result.
479 switch (RetTyVT) {
480 case MVT::isVoid: break;
481 default:
482 RetVals.push_back(RetTyVT);
483 break;
484 case MVT::i1:
485 case MVT::i8:
486 case MVT::i16:
487 RetVals.push_back(MVT::i32);
488 break;
489 case MVT::f32:
490 if (X86ScalarSSE)
491 RetVals.push_back(MVT::f32);
492 else
493 RetVals.push_back(MVT::f64);
494 break;
495 case MVT::i64:
496 RetVals.push_back(MVT::i32);
497 RetVals.push_back(MVT::i32);
498 break;
499 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000501 std::vector<MVT::ValueType> NodeTys;
502 NodeTys.push_back(MVT::Other); // Returns a chain
503 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
504 std::vector<SDOperand> Ops;
505 Ops.push_back(Chain);
506 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000507
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000508 // FIXME: Do not generate X86ISD::TAILCALL for now.
509 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
510 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000511
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000512 NodeTys.clear();
513 NodeTys.push_back(MVT::Other); // Returns a chain
514 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
515 Ops.clear();
516 Ops.push_back(Chain);
517 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
518 Ops.push_back(DAG.getConstant(0, getPointerTy()));
519 Ops.push_back(InFlag);
520 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
521 InFlag = Chain.getValue(1);
522
523 SDOperand RetVal;
524 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000525 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000526 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000527 case MVT::i1:
528 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000529 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
530 Chain = RetVal.getValue(1);
531 if (RetTyVT == MVT::i1)
532 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
533 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000534 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000535 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
536 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000537 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000538 case MVT::i32:
539 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
540 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000541 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000542 case MVT::i64: {
543 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
544 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
545 Lo.getValue(2));
546 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
547 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000548 break;
549 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000550 case MVT::f32:
551 case MVT::f64: {
552 std::vector<MVT::ValueType> Tys;
553 Tys.push_back(MVT::f64);
554 Tys.push_back(MVT::Other);
555 Tys.push_back(MVT::Flag);
556 std::vector<SDOperand> Ops;
557 Ops.push_back(Chain);
558 Ops.push_back(InFlag);
559 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
560 Chain = RetVal.getValue(1);
561 InFlag = RetVal.getValue(2);
562 if (X86ScalarSSE) {
563 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
564 // shouldn't be necessary except that RFP cannot be live across
565 // multiple blocks. When stackifier is fixed, they can be uncoupled.
566 MachineFunction &MF = DAG.getMachineFunction();
567 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
568 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
569 Tys.clear();
570 Tys.push_back(MVT::Other);
571 Ops.clear();
572 Ops.push_back(Chain);
573 Ops.push_back(RetVal);
574 Ops.push_back(StackSlot);
575 Ops.push_back(DAG.getValueType(RetTyVT));
576 Ops.push_back(InFlag);
577 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
578 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
579 DAG.getSrcValue(NULL));
580 Chain = RetVal.getValue(1);
581 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000582
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000583 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
584 // FIXME: we would really like to remember that this FP_ROUND
585 // operation is okay to eliminate if we allow excess FP precision.
586 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
587 break;
588 }
589 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000591
592 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000593}
594
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000595//===----------------------------------------------------------------------===//
596// Fast Calling Convention implementation
597//===----------------------------------------------------------------------===//
598//
599// The X86 'fast' calling convention passes up to two integer arguments in
600// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
601// and requires that the callee pop its arguments off the stack (allowing proper
602// tail calls), and has the same return value conventions as C calling convs.
603//
604// This calling convention always arranges for the callee pop value to be 8n+4
605// bytes, which is needed for tail recursion elimination and stack alignment
606// reasons.
607//
608// Note that this can be enhanced in the future to pass fp vals in registers
609// (when we have a global fp allocator) and do other tricks.
610//
611
612/// AddLiveIn - This helper function adds the specified physical register to the
613/// MachineFunction as a live in value. It also creates a corresponding virtual
614/// register for it.
615static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
616 TargetRegisterClass *RC) {
617 assert(RC->contains(PReg) && "Not the correct regclass!");
618 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
619 MF.addLiveIn(PReg, VReg);
620 return VReg;
621}
622
623
624std::vector<SDOperand>
625X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
626 std::vector<SDOperand> ArgValues;
627
628 MachineFunction &MF = DAG.getMachineFunction();
629 MachineFrameInfo *MFI = MF.getFrameInfo();
630
631 // Add DAG nodes to load the arguments... On entry to a function the stack
632 // frame looks like this:
633 //
634 // [ESP] -- return address
635 // [ESP + 4] -- first nonreg argument (leftmost lexically)
636 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
637 // ...
638 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
639
640 // Keep track of the number of integer regs passed so far. This can be either
641 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
642 // used).
643 unsigned NumIntRegs = 0;
644
645 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
646 MVT::ValueType ObjectVT = getValueType(I->getType());
647 unsigned ArgIncrement = 4;
648 unsigned ObjSize = 0;
649 SDOperand ArgValue;
650
651 switch (ObjectVT) {
652 default: assert(0 && "Unhandled argument type!");
653 case MVT::i1:
654 case MVT::i8:
655 if (NumIntRegs < 2) {
656 if (!I->use_empty()) {
657 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
658 X86::R8RegisterClass);
659 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
660 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000661 if (ObjectVT == MVT::i1)
662 // FIXME: Should insert a assertzext here.
663 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000664 }
665 ++NumIntRegs;
666 break;
667 }
668
669 ObjSize = 1;
670 break;
671 case MVT::i16:
672 if (NumIntRegs < 2) {
673 if (!I->use_empty()) {
674 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
675 X86::R16RegisterClass);
676 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
677 DAG.setRoot(ArgValue.getValue(1));
678 }
679 ++NumIntRegs;
680 break;
681 }
682 ObjSize = 2;
683 break;
684 case MVT::i32:
685 if (NumIntRegs < 2) {
686 if (!I->use_empty()) {
687 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
688 X86::R32RegisterClass);
689 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
690 DAG.setRoot(ArgValue.getValue(1));
691 }
692 ++NumIntRegs;
693 break;
694 }
695 ObjSize = 4;
696 break;
697 case MVT::i64:
698 if (NumIntRegs == 0) {
699 if (!I->use_empty()) {
700 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
701 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
702
703 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
704 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
705 DAG.setRoot(Hi.getValue(1));
706
707 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
708 }
709 NumIntRegs = 2;
710 break;
711 } else if (NumIntRegs == 1) {
712 if (!I->use_empty()) {
713 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
714 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
715 DAG.setRoot(Low.getValue(1));
716
717 // Load the high part from memory.
718 // Create the frame index object for this incoming parameter...
719 int FI = MFI->CreateFixedObject(4, ArgOffset);
720 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
721 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
722 DAG.getSrcValue(NULL));
723 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
724 }
725 ArgOffset += 4;
726 NumIntRegs = 2;
727 break;
728 }
729 ObjSize = ArgIncrement = 8;
730 break;
731 case MVT::f32: ObjSize = 4; break;
732 case MVT::f64: ObjSize = ArgIncrement = 8; break;
733 }
734
735 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
736 // dead loads.
737 if (ObjSize && !I->use_empty()) {
738 // Create the frame index object for this incoming parameter...
739 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
740
741 // Create the SelectionDAG nodes corresponding to a load from this
742 // parameter.
743 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
744
745 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
746 DAG.getSrcValue(NULL));
747 } else if (ArgValue.Val == 0) {
748 if (MVT::isInteger(ObjectVT))
749 ArgValue = DAG.getConstant(0, ObjectVT);
750 else
751 ArgValue = DAG.getConstantFP(0, ObjectVT);
752 }
753 ArgValues.push_back(ArgValue);
754
755 if (ObjSize)
756 ArgOffset += ArgIncrement; // Move on to the next argument.
757 }
758
759 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
760 // arguments and the arguments after the retaddr has been pushed are aligned.
761 if ((ArgOffset & 7) == 0)
762 ArgOffset += 4;
763
764 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
765 ReturnAddrIndex = 0; // No return address slot generated yet.
766 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
767 BytesCallerReserves = 0;
768
769 // Finally, inform the code generator which regs we return values in.
770 switch (getValueType(F.getReturnType())) {
771 default: assert(0 && "Unknown type!");
772 case MVT::isVoid: break;
773 case MVT::i1:
774 case MVT::i8:
775 case MVT::i16:
776 case MVT::i32:
777 MF.addLiveOut(X86::EAX);
778 break;
779 case MVT::i64:
780 MF.addLiveOut(X86::EAX);
781 MF.addLiveOut(X86::EDX);
782 break;
783 case MVT::f32:
784 case MVT::f64:
785 MF.addLiveOut(X86::ST0);
786 break;
787 }
788 return ArgValues;
789}
790
791std::pair<SDOperand, SDOperand>
792X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
793 bool isTailCall, SDOperand Callee,
794 ArgListTy &Args, SelectionDAG &DAG) {
795 // Count how many bytes are to be pushed on the stack.
796 unsigned NumBytes = 0;
797
798 // Keep track of the number of integer regs passed so far. This can be either
799 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
800 // used).
801 unsigned NumIntRegs = 0;
802
803 for (unsigned i = 0, e = Args.size(); i != e; ++i)
804 switch (getValueType(Args[i].second)) {
805 default: assert(0 && "Unknown value type!");
806 case MVT::i1:
807 case MVT::i8:
808 case MVT::i16:
809 case MVT::i32:
810 if (NumIntRegs < 2) {
811 ++NumIntRegs;
812 break;
813 }
814 // fall through
815 case MVT::f32:
816 NumBytes += 4;
817 break;
818 case MVT::i64:
819 if (NumIntRegs == 0) {
820 NumIntRegs = 2;
821 break;
822 } else if (NumIntRegs == 1) {
823 NumIntRegs = 2;
824 NumBytes += 4;
825 break;
826 }
827
828 // fall through
829 case MVT::f64:
830 NumBytes += 8;
831 break;
832 }
833
834 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
835 // arguments and the arguments after the retaddr has been pushed are aligned.
836 if ((NumBytes & 7) == 0)
837 NumBytes += 4;
838
Chris Lattner94dd2922006-02-13 09:00:43 +0000839 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000840
841 // Arguments go on the stack in reverse order, as specified by the ABI.
842 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000843 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000844 NumIntRegs = 0;
845 std::vector<SDOperand> Stores;
846 std::vector<SDOperand> RegValuesToPass;
847 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
848 switch (getValueType(Args[i].second)) {
849 default: assert(0 && "Unexpected ValueType for argument!");
850 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000851 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
852 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000853 case MVT::i8:
854 case MVT::i16:
855 case MVT::i32:
856 if (NumIntRegs < 2) {
857 RegValuesToPass.push_back(Args[i].first);
858 ++NumIntRegs;
859 break;
860 }
861 // Fall through
862 case MVT::f32: {
863 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
864 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
865 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
866 Args[i].first, PtrOff,
867 DAG.getSrcValue(NULL)));
868 ArgOffset += 4;
869 break;
870 }
871 case MVT::i64:
872 if (NumIntRegs < 2) { // Can pass part of it in regs?
873 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
874 Args[i].first, DAG.getConstant(1, MVT::i32));
875 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
876 Args[i].first, DAG.getConstant(0, MVT::i32));
877 RegValuesToPass.push_back(Lo);
878 ++NumIntRegs;
879 if (NumIntRegs < 2) { // Pass both parts in regs?
880 RegValuesToPass.push_back(Hi);
881 ++NumIntRegs;
882 } else {
883 // Pass the high part in memory.
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 Hi, PtrOff, DAG.getSrcValue(NULL)));
888 ArgOffset += 4;
889 }
890 break;
891 }
892 // Fall through
893 case MVT::f64:
894 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
895 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
896 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
897 Args[i].first, PtrOff,
898 DAG.getSrcValue(NULL)));
899 ArgOffset += 8;
900 break;
901 }
902 }
903 if (!Stores.empty())
904 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
905
906 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
907 // arguments and the arguments after the retaddr has been pushed are aligned.
908 if ((ArgOffset & 7) == 0)
909 ArgOffset += 4;
910
911 std::vector<MVT::ValueType> RetVals;
912 MVT::ValueType RetTyVT = getValueType(RetTy);
913
914 RetVals.push_back(MVT::Other);
915
916 // The result values produced have to be legal. Promote the result.
917 switch (RetTyVT) {
918 case MVT::isVoid: break;
919 default:
920 RetVals.push_back(RetTyVT);
921 break;
922 case MVT::i1:
923 case MVT::i8:
924 case MVT::i16:
925 RetVals.push_back(MVT::i32);
926 break;
927 case MVT::f32:
928 if (X86ScalarSSE)
929 RetVals.push_back(MVT::f32);
930 else
931 RetVals.push_back(MVT::f64);
932 break;
933 case MVT::i64:
934 RetVals.push_back(MVT::i32);
935 RetVals.push_back(MVT::i32);
936 break;
937 }
938
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000939 // Build a sequence of copy-to-reg nodes chained together with token chain
940 // and flag operands which copy the outgoing args into registers.
941 SDOperand InFlag;
942 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
943 unsigned CCReg;
944 SDOperand RegToPass = RegValuesToPass[i];
945 switch (RegToPass.getValueType()) {
946 default: assert(0 && "Bad thing to pass in regs");
947 case MVT::i8:
948 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000949 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000950 case MVT::i16:
951 CCReg = (i == 0) ? X86::AX : X86::DX;
952 break;
953 case MVT::i32:
954 CCReg = (i == 0) ? X86::EAX : X86::EDX;
955 break;
956 }
957
958 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
959 InFlag = Chain.getValue(1);
960 }
961
962 std::vector<MVT::ValueType> NodeTys;
963 NodeTys.push_back(MVT::Other); // Returns a chain
964 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
965 std::vector<SDOperand> Ops;
966 Ops.push_back(Chain);
967 Ops.push_back(Callee);
968 if (InFlag.Val)
969 Ops.push_back(InFlag);
970
971 // FIXME: Do not generate X86ISD::TAILCALL for now.
972 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
973 InFlag = Chain.getValue(1);
974
975 NodeTys.clear();
976 NodeTys.push_back(MVT::Other); // Returns a chain
977 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
978 Ops.clear();
979 Ops.push_back(Chain);
980 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
981 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
982 Ops.push_back(InFlag);
983 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
984 InFlag = Chain.getValue(1);
985
986 SDOperand RetVal;
987 if (RetTyVT != MVT::isVoid) {
988 switch (RetTyVT) {
989 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +0000990 case MVT::i1:
991 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000992 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
993 Chain = RetVal.getValue(1);
994 if (RetTyVT == MVT::i1)
995 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
996 break;
Evan Chengd9558e02006-01-06 00:43:03 +0000997 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000998 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
999 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001000 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001001 case MVT::i32:
1002 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1003 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001004 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001005 case MVT::i64: {
1006 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1007 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1008 Lo.getValue(2));
1009 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1010 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001011 break;
1012 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001013 case MVT::f32:
1014 case MVT::f64: {
1015 std::vector<MVT::ValueType> Tys;
1016 Tys.push_back(MVT::f64);
1017 Tys.push_back(MVT::Other);
1018 Tys.push_back(MVT::Flag);
1019 std::vector<SDOperand> Ops;
1020 Ops.push_back(Chain);
1021 Ops.push_back(InFlag);
1022 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1023 Chain = RetVal.getValue(1);
1024 InFlag = RetVal.getValue(2);
1025 if (X86ScalarSSE) {
1026 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1027 // shouldn't be necessary except that RFP cannot be live across
1028 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1029 MachineFunction &MF = DAG.getMachineFunction();
1030 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1031 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1032 Tys.clear();
1033 Tys.push_back(MVT::Other);
1034 Ops.clear();
1035 Ops.push_back(Chain);
1036 Ops.push_back(RetVal);
1037 Ops.push_back(StackSlot);
1038 Ops.push_back(DAG.getValueType(RetTyVT));
1039 Ops.push_back(InFlag);
1040 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1041 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1042 DAG.getSrcValue(NULL));
1043 Chain = RetVal.getValue(1);
1044 }
Evan Chengd9558e02006-01-06 00:43:03 +00001045
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001046 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1047 // FIXME: we would really like to remember that this FP_ROUND
1048 // operation is okay to eliminate if we allow excess FP precision.
1049 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1050 break;
1051 }
1052 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001053 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001054
1055 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001056}
1057
1058SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1059 if (ReturnAddrIndex == 0) {
1060 // Set up a frame object for the return address.
1061 MachineFunction &MF = DAG.getMachineFunction();
1062 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1063 }
1064
1065 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1066}
1067
1068
1069
1070std::pair<SDOperand, SDOperand> X86TargetLowering::
1071LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1072 SelectionDAG &DAG) {
1073 SDOperand Result;
1074 if (Depth) // Depths > 0 not supported yet!
1075 Result = DAG.getConstant(0, getPointerTy());
1076 else {
1077 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1078 if (!isFrameAddress)
1079 // Just load the return address
1080 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1081 DAG.getSrcValue(NULL));
1082 else
1083 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1084 DAG.getConstant(4, MVT::i32));
1085 }
1086 return std::make_pair(Result, Chain);
1087}
1088
Evan Cheng4a460802006-01-11 00:33:36 +00001089/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1090/// which corresponds to the condition code.
1091static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1092 switch (X86CC) {
1093 default: assert(0 && "Unknown X86 conditional code!");
1094 case X86ISD::COND_A: return X86::JA;
1095 case X86ISD::COND_AE: return X86::JAE;
1096 case X86ISD::COND_B: return X86::JB;
1097 case X86ISD::COND_BE: return X86::JBE;
1098 case X86ISD::COND_E: return X86::JE;
1099 case X86ISD::COND_G: return X86::JG;
1100 case X86ISD::COND_GE: return X86::JGE;
1101 case X86ISD::COND_L: return X86::JL;
1102 case X86ISD::COND_LE: return X86::JLE;
1103 case X86ISD::COND_NE: return X86::JNE;
1104 case X86ISD::COND_NO: return X86::JNO;
1105 case X86ISD::COND_NP: return X86::JNP;
1106 case X86ISD::COND_NS: return X86::JNS;
1107 case X86ISD::COND_O: return X86::JO;
1108 case X86ISD::COND_P: return X86::JP;
1109 case X86ISD::COND_S: return X86::JS;
1110 }
1111}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001112
Evan Cheng6dfa9992006-01-30 23:41:35 +00001113/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1114/// specific condition code. It returns a false if it cannot do a direct
1115/// translation. X86CC is the translated CondCode. Flip is set to true if the
1116/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001117static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1118 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001119 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001120 Flip = false;
1121 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001122 if (!isFP) {
1123 switch (SetCCOpcode) {
1124 default: break;
1125 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1126 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1127 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1128 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1129 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1130 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1131 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1132 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1133 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1134 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1135 }
1136 } else {
1137 // On a floating point condition, the flags are set as follows:
1138 // ZF PF CF op
1139 // 0 | 0 | 0 | X > Y
1140 // 0 | 0 | 1 | X < Y
1141 // 1 | 0 | 0 | X == Y
1142 // 1 | 1 | 1 | unordered
1143 switch (SetCCOpcode) {
1144 default: break;
1145 case ISD::SETUEQ:
1146 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001147 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001148 case ISD::SETOGT:
1149 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001150 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001151 case ISD::SETOGE:
1152 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001153 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001154 case ISD::SETULT:
1155 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001156 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001157 case ISD::SETULE:
1158 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1159 case ISD::SETONE:
1160 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1161 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1162 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1163 }
1164 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001165
1166 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001167}
1168
Evan Cheng4a460802006-01-11 00:33:36 +00001169/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1170/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001171/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001172static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001173 switch (X86CC) {
1174 default:
1175 return false;
1176 case X86ISD::COND_B:
1177 case X86ISD::COND_BE:
1178 case X86ISD::COND_E:
1179 case X86ISD::COND_P:
1180 case X86ISD::COND_A:
1181 case X86ISD::COND_AE:
1182 case X86ISD::COND_NE:
1183 case X86ISD::COND_NP:
1184 return true;
1185 }
1186}
1187
Evan Cheng4a460802006-01-11 00:33:36 +00001188MachineBasicBlock *
1189X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1190 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001191 switch (MI->getOpcode()) {
1192 default: assert(false && "Unexpected instr type to insert");
1193 case X86::CMOV_FR32:
1194 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001195 // To "insert" a SELECT_CC instruction, we actually have to insert the
1196 // diamond control-flow pattern. The incoming instruction knows the
1197 // destination vreg to set, the condition code register to branch on, the
1198 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001199 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1200 ilist<MachineBasicBlock>::iterator It = BB;
1201 ++It;
1202
1203 // thisMBB:
1204 // ...
1205 // TrueVal = ...
1206 // cmpTY ccX, r1, r2
1207 // bCC copy1MBB
1208 // fallthrough --> copy0MBB
1209 MachineBasicBlock *thisMBB = BB;
1210 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1211 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1212 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1213 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1214 MachineFunction *F = BB->getParent();
1215 F->getBasicBlockList().insert(It, copy0MBB);
1216 F->getBasicBlockList().insert(It, sinkMBB);
1217 // Update machine-CFG edges
1218 BB->addSuccessor(copy0MBB);
1219 BB->addSuccessor(sinkMBB);
1220
1221 // copy0MBB:
1222 // %FalseValue = ...
1223 // # fallthrough to sinkMBB
1224 BB = copy0MBB;
1225
1226 // Update machine-CFG edges
1227 BB->addSuccessor(sinkMBB);
1228
1229 // sinkMBB:
1230 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1231 // ...
1232 BB = sinkMBB;
1233 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1234 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1235 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001236
Evan Cheng0cc39452006-01-16 21:21:29 +00001237 delete MI; // The pseudo instruction is gone now.
1238 return BB;
1239 }
Evan Cheng4a460802006-01-11 00:33:36 +00001240
Evan Cheng0cc39452006-01-16 21:21:29 +00001241 case X86::FP_TO_INT16_IN_MEM:
1242 case X86::FP_TO_INT32_IN_MEM:
1243 case X86::FP_TO_INT64_IN_MEM: {
1244 // Change the floating point control register to use "round towards zero"
1245 // mode when truncating to an integer value.
1246 MachineFunction *F = BB->getParent();
1247 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1248 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1249
1250 // Load the old value of the high byte of the control word...
1251 unsigned OldCW =
1252 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1253 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1254
1255 // Set the high part to be round to zero...
1256 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1257
1258 // Reload the modified control word now...
1259 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1260
1261 // Restore the memory image of control word to original value
1262 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1263
1264 // Get the X86 opcode to use.
1265 unsigned Opc;
1266 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001267 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001268 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1269 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1270 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1271 }
1272
1273 X86AddressMode AM;
1274 MachineOperand &Op = MI->getOperand(0);
1275 if (Op.isRegister()) {
1276 AM.BaseType = X86AddressMode::RegBase;
1277 AM.Base.Reg = Op.getReg();
1278 } else {
1279 AM.BaseType = X86AddressMode::FrameIndexBase;
1280 AM.Base.FrameIndex = Op.getFrameIndex();
1281 }
1282 Op = MI->getOperand(1);
1283 if (Op.isImmediate())
1284 AM.Scale = Op.getImmedValue();
1285 Op = MI->getOperand(2);
1286 if (Op.isImmediate())
1287 AM.IndexReg = Op.getImmedValue();
1288 Op = MI->getOperand(3);
1289 if (Op.isGlobalAddress()) {
1290 AM.GV = Op.getGlobal();
1291 } else {
1292 AM.Disp = Op.getImmedValue();
1293 }
1294 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1295
1296 // Reload the original control word now.
1297 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1298
1299 delete MI; // The pseudo instruction is gone now.
1300 return BB;
1301 }
1302 }
Evan Cheng4a460802006-01-11 00:33:36 +00001303}
1304
1305
1306//===----------------------------------------------------------------------===//
1307// X86 Custom Lowering Hooks
1308//===----------------------------------------------------------------------===//
1309
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001310/// LowerOperation - Provide custom lowering hooks for some operations.
1311///
1312SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1313 switch (Op.getOpcode()) {
1314 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001315 case ISD::SHL_PARTS:
1316 case ISD::SRA_PARTS:
1317 case ISD::SRL_PARTS: {
1318 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1319 "Not an i64 shift!");
1320 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1321 SDOperand ShOpLo = Op.getOperand(0);
1322 SDOperand ShOpHi = Op.getOperand(1);
1323 SDOperand ShAmt = Op.getOperand(2);
1324 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001325 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001326 : DAG.getConstant(0, MVT::i32);
1327
1328 SDOperand Tmp2, Tmp3;
1329 if (Op.getOpcode() == ISD::SHL_PARTS) {
1330 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1331 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1332 } else {
1333 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001334 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001335 }
1336
1337 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1338 ShAmt, DAG.getConstant(32, MVT::i8));
1339
1340 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001341 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001342
1343 std::vector<MVT::ValueType> Tys;
1344 Tys.push_back(MVT::i32);
1345 Tys.push_back(MVT::Flag);
1346 std::vector<SDOperand> Ops;
1347 if (Op.getOpcode() == ISD::SHL_PARTS) {
1348 Ops.push_back(Tmp2);
1349 Ops.push_back(Tmp3);
1350 Ops.push_back(CC);
1351 Ops.push_back(InFlag);
1352 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1353 InFlag = Hi.getValue(1);
1354
1355 Ops.clear();
1356 Ops.push_back(Tmp3);
1357 Ops.push_back(Tmp1);
1358 Ops.push_back(CC);
1359 Ops.push_back(InFlag);
1360 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1361 } else {
1362 Ops.push_back(Tmp2);
1363 Ops.push_back(Tmp3);
1364 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001365 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001366 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1367 InFlag = Lo.getValue(1);
1368
1369 Ops.clear();
1370 Ops.push_back(Tmp3);
1371 Ops.push_back(Tmp1);
1372 Ops.push_back(CC);
1373 Ops.push_back(InFlag);
1374 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1375 }
1376
1377 Tys.clear();
1378 Tys.push_back(MVT::i32);
1379 Tys.push_back(MVT::i32);
1380 Ops.clear();
1381 Ops.push_back(Lo);
1382 Ops.push_back(Hi);
1383 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1384 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001385 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001386 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001387 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001388 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001389
1390 SDOperand Result;
1391 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1392 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001393 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001394 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001395 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001396 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1397 DAG.getEntryNode(), Op.getOperand(0),
1398 StackSlot, DAG.getSrcValue(NULL));
1399
1400 // Build the FILD
1401 std::vector<MVT::ValueType> Tys;
1402 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001403 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001404 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001405 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001406 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001407 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001408 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001409 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1410 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001411
1412 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001413 Chain = Result.getValue(1);
1414 SDOperand InFlag = Result.getValue(2);
1415
Evan Chenge3de85b2006-02-04 02:20:30 +00001416 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001417 // shouldn't be necessary except that RFP cannot be live across
1418 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1419 MachineFunction &MF = DAG.getMachineFunction();
1420 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1421 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1422 std::vector<MVT::ValueType> Tys;
1423 Tys.push_back(MVT::Other);
1424 std::vector<SDOperand> Ops;
1425 Ops.push_back(Chain);
1426 Ops.push_back(Result);
1427 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001428 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001429 Ops.push_back(InFlag);
1430 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1431 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1432 DAG.getSrcValue(NULL));
1433 }
1434
Evan Chenga3195e82006-01-12 22:54:21 +00001435 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001436 }
1437 case ISD::FP_TO_SINT: {
1438 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001439 "Unknown FP_TO_SINT to lower!");
1440 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1441 // stack slot.
1442 MachineFunction &MF = DAG.getMachineFunction();
1443 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1444 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1445 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1446
1447 unsigned Opc;
1448 switch (Op.getValueType()) {
1449 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1450 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1451 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1452 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1453 }
1454
Evan Cheng6dab0532006-01-30 08:02:57 +00001455 SDOperand Chain = DAG.getEntryNode();
1456 SDOperand Value = Op.getOperand(0);
1457 if (X86ScalarSSE) {
1458 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1459 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1460 DAG.getSrcValue(0));
1461 std::vector<MVT::ValueType> Tys;
1462 Tys.push_back(MVT::f64);
1463 Tys.push_back(MVT::Other);
1464 std::vector<SDOperand> Ops;
1465 Ops.push_back(Chain);
1466 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001467 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001468 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1469 Chain = Value.getValue(1);
1470 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1471 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1472 }
1473
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001474 // Build the FP_TO_INT*_IN_MEM
1475 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001476 Ops.push_back(Chain);
1477 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001478 Ops.push_back(StackSlot);
1479 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1480
1481 // Load the result.
1482 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1483 DAG.getSrcValue(NULL));
1484 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001485 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001486 std::vector<MVT::ValueType> Tys;
1487 Tys.push_back(MVT::Other);
1488 Tys.push_back(MVT::Flag);
1489 std::vector<SDOperand> Ops;
1490 Ops.push_back(Op.getOperand(0));
1491 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001492 Ops.clear();
1493 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1494 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1495 MVT::i32, Ops[0].getValue(2)));
1496 Ops.push_back(Ops[1].getValue(1));
1497 Tys[0] = Tys[1] = MVT::i32;
1498 Tys.push_back(MVT::Other);
1499 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001500 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001501 case ISD::FABS: {
1502 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001503 const Type *OpNTy = MVT::getTypeForValueType(VT);
1504 std::vector<Constant*> CV;
1505 if (VT == MVT::f64) {
1506 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1507 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1508 } else {
1509 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1510 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1511 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1512 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1513 }
1514 Constant *CS = ConstantStruct::get(CV);
1515 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1516 SDOperand Mask
1517 = DAG.getNode(X86ISD::LOAD_PACK,
1518 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001519 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1520 }
Evan Cheng223547a2006-01-31 22:28:30 +00001521 case ISD::FNEG: {
1522 MVT::ValueType VT = Op.getValueType();
1523 const Type *OpNTy = MVT::getTypeForValueType(VT);
1524 std::vector<Constant*> CV;
1525 if (VT == MVT::f64) {
1526 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1527 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1528 } else {
1529 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1530 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1531 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1532 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1533 }
1534 Constant *CS = ConstantStruct::get(CV);
1535 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1536 SDOperand Mask
1537 = DAG.getNode(X86ISD::LOAD_PACK,
1538 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1539 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1540 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001541 case ISD::SETCC: {
1542 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001543 SDOperand Cond;
1544 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001545 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1546 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001547 bool Flip;
1548 unsigned X86CC;
1549 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1550 if (Flip)
1551 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1552 Op.getOperand(1), Op.getOperand(0));
1553 else
1554 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1555 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001556 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1557 DAG.getConstant(X86CC, MVT::i8), Cond);
1558 } else {
1559 assert(isFP && "Illegal integer SetCC!");
1560
Evan Cheng6dfa9992006-01-30 23:41:35 +00001561 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1562 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001563 std::vector<MVT::ValueType> Tys;
1564 std::vector<SDOperand> Ops;
1565 switch (SetCCOpcode) {
1566 default: assert(false && "Illegal floating point SetCC!");
1567 case ISD::SETOEQ: { // !PF & ZF
1568 Tys.push_back(MVT::i8);
1569 Tys.push_back(MVT::Flag);
1570 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1571 Ops.push_back(Cond);
1572 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1573 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1574 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1575 Tmp1.getValue(1));
1576 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1577 }
Evan Chengd9558e02006-01-06 00:43:03 +00001578 case ISD::SETUNE: { // PF | !ZF
1579 Tys.push_back(MVT::i8);
1580 Tys.push_back(MVT::Flag);
1581 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1582 Ops.push_back(Cond);
1583 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1584 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1585 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1586 Tmp1.getValue(1));
1587 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1588 }
1589 }
1590 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001591 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001592 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001593 MVT::ValueType VT = Op.getValueType();
1594 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001595 bool isFPStack = isFP && !X86ScalarSSE;
1596 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001597 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001598 SDOperand Op0 = Op.getOperand(0);
1599 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001600 if (Op0.getOpcode() == ISD::SETCC)
1601 Op0 = LowerOperation(Op0, DAG);
1602
Evan Chengaaca22c2006-01-10 20:26:56 +00001603 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001604 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1605 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1606 // have another use it will be eliminated.
1607 // If the X86ISD::SETCC has more than one use, then it's probably better
1608 // to use a test instead of duplicating the X86ISD::CMP (for register
1609 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001610 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1611 if (!Op0.hasOneUse()) {
1612 std::vector<MVT::ValueType> Tys;
1613 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1614 Tys.push_back(Op0.Val->getValueType(i));
1615 std::vector<SDOperand> Ops;
1616 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1617 Ops.push_back(Op0.getOperand(i));
1618 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1619 }
1620
Evan Cheng1bcee362006-01-13 01:03:02 +00001621 CC = Op0.getOperand(0);
1622 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001623 // Make a copy as flag result cannot be used by more than one.
1624 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1625 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001626 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001627 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001628 } else
1629 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001630 } else
1631 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001632
Evan Cheng189d01e2006-01-13 01:06:49 +00001633 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001634 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001635 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001636 }
Evan Chenge3413162006-01-09 18:33:28 +00001637
1638 std::vector<MVT::ValueType> Tys;
1639 Tys.push_back(Op.getValueType());
1640 Tys.push_back(MVT::Flag);
1641 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001642 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1643 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001644 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001645 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001646 Ops.push_back(CC);
1647 Ops.push_back(Cond);
1648 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001649 }
Evan Cheng898101c2005-12-19 23:12:38 +00001650 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001651 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001652 SDOperand Cond = Op.getOperand(1);
1653 SDOperand Dest = Op.getOperand(2);
1654 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001655 if (Cond.getOpcode() == ISD::SETCC)
1656 Cond = LowerOperation(Cond, DAG);
1657
Evan Chengd5781fc2005-12-21 20:21:51 +00001658 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001659 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1660 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1661 // have another use it will be eliminated.
1662 // If the X86ISD::SETCC has more than one use, then it's probably better
1663 // to use a test instead of duplicating the X86ISD::CMP (for register
1664 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001665 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1666 if (!Cond.hasOneUse()) {
1667 std::vector<MVT::ValueType> Tys;
1668 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1669 Tys.push_back(Cond.Val->getValueType(i));
1670 std::vector<SDOperand> Ops;
1671 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1672 Ops.push_back(Cond.getOperand(i));
1673 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1674 }
1675
Evan Cheng1bcee362006-01-13 01:03:02 +00001676 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001677 Cond = Cond.getOperand(1);
1678 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001679 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001680 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001681 } else
1682 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001683 } else
1684 addTest = true;
1685
1686 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001687 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001688 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1689 }
1690 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1691 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1692 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001693 case ISD::MEMSET: {
1694 SDOperand InFlag;
1695 SDOperand Chain = Op.getOperand(0);
1696 unsigned Align =
1697 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1698 if (Align == 0) Align = 1;
1699
Evan Cheng18a84522006-02-16 00:21:07 +00001700 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1701 // If not DWORD aligned, call memset if size is less than the threshold.
1702 // It knows how to align to the right boundary first.
1703 if ((Align & 3) != 0 &&
1704 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1705 MVT::ValueType IntPtr = getPointerTy();
1706 const Type *IntPtrTy = getTargetData().getIntPtrType();
1707 std::vector<std::pair<SDOperand, const Type*> > Args;
1708 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1709 // Extend the ubyte argument to be an int value for the call.
1710 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1711 Args.push_back(std::make_pair(Val, IntPtrTy));
1712 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1713 std::pair<SDOperand,SDOperand> CallResult =
1714 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1715 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1716 return CallResult.second;
1717 }
1718
Evan Cheng67f92a72006-01-11 22:15:48 +00001719 MVT::ValueType AVT;
1720 SDOperand Count;
1721 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1722 unsigned ValReg;
1723 unsigned Val = ValC->getValue() & 255;
1724
1725 // If the value is a constant, then we can potentially use larger sets.
1726 switch (Align & 3) {
1727 case 2: // WORD aligned
1728 AVT = MVT::i16;
Evan Cheng18a84522006-02-16 00:21:07 +00001729 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001730 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1731 else
1732 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1733 DAG.getConstant(1, MVT::i8));
1734 Val = (Val << 8) | Val;
1735 ValReg = X86::AX;
1736 break;
1737 case 0: // DWORD aligned
1738 AVT = MVT::i32;
Evan Cheng18a84522006-02-16 00:21:07 +00001739 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001740 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1741 else
1742 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1743 DAG.getConstant(2, MVT::i8));
1744 Val = (Val << 8) | Val;
1745 Val = (Val << 16) | Val;
1746 ValReg = X86::EAX;
1747 break;
1748 default: // Byte aligned
1749 AVT = MVT::i8;
1750 Count = Op.getOperand(3);
1751 ValReg = X86::AL;
1752 break;
1753 }
1754
1755 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1756 InFlag);
1757 InFlag = Chain.getValue(1);
1758 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001759 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001760 Count = Op.getOperand(3);
1761 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1762 InFlag = Chain.getValue(1);
1763 }
1764
1765 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1766 InFlag = Chain.getValue(1);
1767 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1768 InFlag = Chain.getValue(1);
1769
1770 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1771 DAG.getValueType(AVT), InFlag);
1772 }
1773 case ISD::MEMCPY: {
1774 SDOperand Chain = Op.getOperand(0);
1775 unsigned Align =
1776 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1777 if (Align == 0) Align = 1;
1778
Evan Cheng18a84522006-02-16 00:21:07 +00001779 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1780 // If not DWORD aligned, call memcpy if size is less than the threshold.
1781 // It knows how to align to the right boundary first.
1782 if ((Align & 3) != 0 &&
1783 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1784 MVT::ValueType IntPtr = getPointerTy();
1785 const Type *IntPtrTy = getTargetData().getIntPtrType();
1786 std::vector<std::pair<SDOperand, const Type*> > Args;
1787 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1788 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1789 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1790 std::pair<SDOperand,SDOperand> CallResult =
1791 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1792 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1793 return CallResult.second;
1794 }
1795
Evan Cheng67f92a72006-01-11 22:15:48 +00001796 MVT::ValueType AVT;
1797 SDOperand Count;
1798 switch (Align & 3) {
1799 case 2: // WORD aligned
1800 AVT = MVT::i16;
Evan Cheng18a84522006-02-16 00:21:07 +00001801 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001802 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1803 else
Evan Cheng18a84522006-02-16 00:21:07 +00001804 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
Evan Cheng67f92a72006-01-11 22:15:48 +00001805 break;
1806 case 0: // DWORD aligned
1807 AVT = MVT::i32;
Evan Cheng18a84522006-02-16 00:21:07 +00001808 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001809 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1810 else
1811 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1812 DAG.getConstant(2, MVT::i8));
1813 break;
1814 default: // Byte aligned
1815 AVT = MVT::i8;
1816 Count = Op.getOperand(3);
1817 break;
1818 }
1819
1820 SDOperand InFlag;
1821 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1822 InFlag = Chain.getValue(1);
1823 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1824 InFlag = Chain.getValue(1);
1825 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1826 InFlag = Chain.getValue(1);
1827
1828 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1829 DAG.getValueType(AVT), InFlag);
1830 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001831 case ISD::ConstantPool: {
1832 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1833 SDOperand Result =
1834 DAG.getTargetConstantPool(CP->get(), getPointerTy(), CP->getAlignment());
1835 // Only lower ConstantPool on Darwin.
1836 if (getTargetMachine().
1837 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1838 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001839 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00001840 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1841 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1842 }
1843
1844 return Result;
1845 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001846 case ISD::GlobalAddress: {
Evan Cheng002fe9b2006-01-12 07:56:47 +00001847 SDOperand Result;
Evan Cheng7ccced62006-02-18 00:15:05 +00001848 // Only lower GlobalAddress on Darwin.
Evan Chengb077b842005-12-21 02:39:21 +00001849 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00001850 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1851 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1852 SDOperand Addr = DAG.getTargetGlobalAddress(GV, getPointerTy());
1853 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001854 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00001855 Addr = DAG.getNode(ISD::ADD, getPointerTy(),
1856 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Addr);
1857
1858 // For Darwin, external and weak symbols are indirect, so we want to load
1859 // the value at address GV, not the value of GV itself. This means that
1860 // the GlobalAddress must be in the base or index register of the address,
1861 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001862 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
1863 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1864 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
Evan Cheng2338c5c2006-02-07 08:38:37 +00001865 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng7ccced62006-02-18 00:15:05 +00001866 Addr, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00001867 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001868
Evan Cheng002fe9b2006-01-12 07:56:47 +00001869 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001870 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001871 case ISD::VASTART: {
1872 // vastart just stores the address of the VarArgsFrameIndex slot into the
1873 // memory location argument.
1874 // FIXME: Replace MVT::i32 with PointerTy
1875 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1876 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1877 Op.getOperand(1), Op.getOperand(2));
1878 }
Nate Begemanee625572006-01-27 21:09:22 +00001879 case ISD::RET: {
1880 SDOperand Copy;
1881
1882 switch(Op.getNumOperands()) {
1883 default:
1884 assert(0 && "Do not know how to return this many arguments!");
1885 abort();
1886 case 1:
1887 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1888 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1889 case 2: {
1890 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1891 if (MVT::isInteger(ArgVT))
1892 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1893 SDOperand());
1894 else if (!X86ScalarSSE) {
1895 std::vector<MVT::ValueType> Tys;
1896 Tys.push_back(MVT::Other);
1897 Tys.push_back(MVT::Flag);
1898 std::vector<SDOperand> Ops;
1899 Ops.push_back(Op.getOperand(0));
1900 Ops.push_back(Op.getOperand(1));
1901 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1902 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00001903 SDOperand MemLoc;
1904 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001905 SDOperand Value = Op.getOperand(1);
1906
Evan Cheng760df292006-02-01 01:19:32 +00001907 if (Value.getOpcode() == ISD::LOAD &&
1908 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00001909 Chain = Value.getOperand(0);
1910 MemLoc = Value.getOperand(1);
1911 } else {
1912 // Spill the value to memory and reload it into top of stack.
1913 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1914 MachineFunction &MF = DAG.getMachineFunction();
1915 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1916 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1917 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1918 Value, MemLoc, DAG.getSrcValue(0));
1919 }
Nate Begemanee625572006-01-27 21:09:22 +00001920 std::vector<MVT::ValueType> Tys;
1921 Tys.push_back(MVT::f64);
1922 Tys.push_back(MVT::Other);
1923 std::vector<SDOperand> Ops;
1924 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001925 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00001926 Ops.push_back(DAG.getValueType(ArgVT));
1927 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1928 Tys.clear();
1929 Tys.push_back(MVT::Other);
1930 Tys.push_back(MVT::Flag);
1931 Ops.clear();
1932 Ops.push_back(Copy.getValue(1));
1933 Ops.push_back(Copy);
1934 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1935 }
1936 break;
1937 }
1938 case 3:
1939 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1940 SDOperand());
1941 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1942 break;
1943 }
1944 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1945 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1946 Copy.getValue(1));
1947 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001948 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001949}
Evan Cheng72261582005-12-20 06:22:03 +00001950
1951const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1952 switch (Opcode) {
1953 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001954 case X86ISD::SHLD: return "X86ISD::SHLD";
1955 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00001956 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00001957 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00001958 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00001959 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00001960 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1961 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1962 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001963 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001964 case X86ISD::FST: return "X86ISD::FST";
1965 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001966 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001967 case X86ISD::CALL: return "X86ISD::CALL";
1968 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1969 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1970 case X86ISD::CMP: return "X86ISD::CMP";
1971 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001972 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001973 case X86ISD::CMOV: return "X86ISD::CMOV";
1974 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001975 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng67f92a72006-01-11 22:15:48 +00001976 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1977 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00001978 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00001979 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng72261582005-12-20 06:22:03 +00001980 }
1981}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001982
Nate Begeman368e18d2006-02-16 21:11:51 +00001983void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1984 uint64_t Mask,
1985 uint64_t &KnownZero,
1986 uint64_t &KnownOne,
1987 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001988
1989 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00001990 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001991
1992 switch (Opc) {
1993 default:
1994 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1995 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001996 case X86ISD::SETCC:
1997 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1998 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001999 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002000}
Chris Lattner259e97c2006-01-31 19:43:35 +00002001
2002std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002003getRegClassForInlineAsmConstraint(const std::string &Constraint,
2004 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002005 if (Constraint.size() == 1) {
2006 // FIXME: not handling fp-stack yet!
2007 // FIXME: not handling MMX registers yet ('y' constraint).
2008 switch (Constraint[0]) { // GCC X86 Constraint Letters
2009 default: break; // Unknown constriant letter
2010 case 'r': // GENERAL_REGS
2011 case 'R': // LEGACY_REGS
2012 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2013 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2014 case 'l': // INDEX_REGS
2015 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2016 X86::ESI, X86::EDI, X86::EBP, 0);
2017 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2018 case 'Q': // Q_REGS
2019 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2020 case 'x': // SSE_REGS if SSE1 allowed
2021 if (Subtarget->hasSSE1())
2022 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2023 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2024 0);
2025 return std::vector<unsigned>();
2026 case 'Y': // SSE_REGS if SSE2 allowed
2027 if (Subtarget->hasSSE2())
2028 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2029 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2030 0);
2031 return std::vector<unsigned>();
2032 }
2033 }
2034
Chris Lattner1efa40f2006-02-22 00:56:39 +00002035 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002036}