blob: 7c84850683ed445e7cf100826f11b73b1239c4ac [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 }
239 computeRegisterProperties();
240
Evan Cheng87ed7162006-02-14 08:25:08 +0000241 // FIXME: These should be based on subtarget info. Plus, the values should
242 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000243 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
244 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
245 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 allowUnalignedMemoryAccesses = true; // x86 supports it!
247}
248
249std::vector<SDOperand>
250X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
251 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
252 return LowerFastCCArguments(F, DAG);
253 return LowerCCCArguments(F, DAG);
254}
255
256std::pair<SDOperand, SDOperand>
257X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
258 bool isVarArg, unsigned CallingConv,
259 bool isTailCall,
260 SDOperand Callee, ArgListTy &Args,
261 SelectionDAG &DAG) {
262 assert((!isVarArg || CallingConv == CallingConv::C) &&
263 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000264
265 // If the callee is a GlobalAddress node (quite common, every direct call is)
266 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
267 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
268 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000269 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
270 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000271
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000272 if (CallingConv == CallingConv::Fast && EnableFastCC)
273 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
274 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
275}
276
277//===----------------------------------------------------------------------===//
278// C Calling Convention implementation
279//===----------------------------------------------------------------------===//
280
281std::vector<SDOperand>
282X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
283 std::vector<SDOperand> ArgValues;
284
285 MachineFunction &MF = DAG.getMachineFunction();
286 MachineFrameInfo *MFI = MF.getFrameInfo();
287
288 // Add DAG nodes to load the arguments... On entry to a function on the X86,
289 // the stack frame looks like this:
290 //
291 // [ESP] -- return address
292 // [ESP + 4] -- first argument (leftmost lexically)
293 // [ESP + 8] -- second argument, if first argument is four bytes in size
294 // ...
295 //
296 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
297 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
298 MVT::ValueType ObjectVT = getValueType(I->getType());
299 unsigned ArgIncrement = 4;
300 unsigned ObjSize;
301 switch (ObjectVT) {
302 default: assert(0 && "Unhandled argument type!");
303 case MVT::i1:
304 case MVT::i8: ObjSize = 1; break;
305 case MVT::i16: ObjSize = 2; break;
306 case MVT::i32: ObjSize = 4; break;
307 case MVT::i64: ObjSize = ArgIncrement = 8; break;
308 case MVT::f32: ObjSize = 4; break;
309 case MVT::f64: ObjSize = ArgIncrement = 8; break;
310 }
311 // Create the frame index object for this incoming parameter...
312 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
313
314 // Create the SelectionDAG nodes corresponding to a load from this parameter
315 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
316
317 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
318 // dead loads.
319 SDOperand ArgValue;
320 if (!I->use_empty())
321 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
322 DAG.getSrcValue(NULL));
323 else {
324 if (MVT::isInteger(ObjectVT))
325 ArgValue = DAG.getConstant(0, ObjectVT);
326 else
327 ArgValue = DAG.getConstantFP(0, ObjectVT);
328 }
329 ArgValues.push_back(ArgValue);
330
331 ArgOffset += ArgIncrement; // Move on to the next argument...
332 }
333
334 // If the function takes variable number of arguments, make a frame index for
335 // the start of the first vararg value... for expansion of llvm.va_start.
336 if (F.isVarArg())
337 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
338 ReturnAddrIndex = 0; // No return address slot generated yet.
339 BytesToPopOnReturn = 0; // Callee pops nothing.
340 BytesCallerReserves = ArgOffset;
341
342 // Finally, inform the code generator which regs we return values in.
343 switch (getValueType(F.getReturnType())) {
344 default: assert(0 && "Unknown type!");
345 case MVT::isVoid: break;
346 case MVT::i1:
347 case MVT::i8:
348 case MVT::i16:
349 case MVT::i32:
350 MF.addLiveOut(X86::EAX);
351 break;
352 case MVT::i64:
353 MF.addLiveOut(X86::EAX);
354 MF.addLiveOut(X86::EDX);
355 break;
356 case MVT::f32:
357 case MVT::f64:
358 MF.addLiveOut(X86::ST0);
359 break;
360 }
361 return ArgValues;
362}
363
364std::pair<SDOperand, SDOperand>
365X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
366 bool isVarArg, bool isTailCall,
367 SDOperand Callee, ArgListTy &Args,
368 SelectionDAG &DAG) {
369 // Count how many bytes are to be pushed on the stack.
370 unsigned NumBytes = 0;
371
372 if (Args.empty()) {
373 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000374 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000375 } else {
376 for (unsigned i = 0, e = Args.size(); i != e; ++i)
377 switch (getValueType(Args[i].second)) {
378 default: assert(0 && "Unknown value type!");
379 case MVT::i1:
380 case MVT::i8:
381 case MVT::i16:
382 case MVT::i32:
383 case MVT::f32:
384 NumBytes += 4;
385 break;
386 case MVT::i64:
387 case MVT::f64:
388 NumBytes += 8;
389 break;
390 }
391
Chris Lattner94dd2922006-02-13 09:00:43 +0000392 Chain = DAG.getCALLSEQ_START(Chain,
393 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000394
395 // Arguments go on the stack in reverse order, as specified by the ABI.
396 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000397 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000398 std::vector<SDOperand> Stores;
399
400 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
401 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
402 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
403
404 switch (getValueType(Args[i].second)) {
405 default: assert(0 && "Unexpected ValueType for argument!");
406 case MVT::i1:
407 case MVT::i8:
408 case MVT::i16:
409 // Promote the integer to 32 bits. If the input type is signed use a
410 // sign extend, otherwise use a zero extend.
411 if (Args[i].second->isSigned())
412 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
413 else
414 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
415
416 // FALL THROUGH
417 case MVT::i32:
418 case MVT::f32:
419 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
420 Args[i].first, PtrOff,
421 DAG.getSrcValue(NULL)));
422 ArgOffset += 4;
423 break;
424 case MVT::i64:
425 case MVT::f64:
426 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
427 Args[i].first, PtrOff,
428 DAG.getSrcValue(NULL)));
429 ArgOffset += 8;
430 break;
431 }
432 }
433 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
434 }
435
436 std::vector<MVT::ValueType> RetVals;
437 MVT::ValueType RetTyVT = getValueType(RetTy);
438 RetVals.push_back(MVT::Other);
439
440 // The result values produced have to be legal. Promote the result.
441 switch (RetTyVT) {
442 case MVT::isVoid: break;
443 default:
444 RetVals.push_back(RetTyVT);
445 break;
446 case MVT::i1:
447 case MVT::i8:
448 case MVT::i16:
449 RetVals.push_back(MVT::i32);
450 break;
451 case MVT::f32:
452 if (X86ScalarSSE)
453 RetVals.push_back(MVT::f32);
454 else
455 RetVals.push_back(MVT::f64);
456 break;
457 case MVT::i64:
458 RetVals.push_back(MVT::i32);
459 RetVals.push_back(MVT::i32);
460 break;
461 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000462
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000463 std::vector<MVT::ValueType> NodeTys;
464 NodeTys.push_back(MVT::Other); // Returns a chain
465 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
466 std::vector<SDOperand> Ops;
467 Ops.push_back(Chain);
468 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000469
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000470 // FIXME: Do not generate X86ISD::TAILCALL for now.
471 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
472 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000473
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000474 NodeTys.clear();
475 NodeTys.push_back(MVT::Other); // Returns a chain
476 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
477 Ops.clear();
478 Ops.push_back(Chain);
479 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
480 Ops.push_back(DAG.getConstant(0, getPointerTy()));
481 Ops.push_back(InFlag);
482 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
483 InFlag = Chain.getValue(1);
484
485 SDOperand RetVal;
486 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000487 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000488 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000489 case MVT::i1:
490 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000491 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
492 Chain = RetVal.getValue(1);
493 if (RetTyVT == MVT::i1)
494 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
495 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000496 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000497 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
498 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000499 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000500 case MVT::i32:
501 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
502 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000503 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000504 case MVT::i64: {
505 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
506 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
507 Lo.getValue(2));
508 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
509 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000510 break;
511 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000512 case MVT::f32:
513 case MVT::f64: {
514 std::vector<MVT::ValueType> Tys;
515 Tys.push_back(MVT::f64);
516 Tys.push_back(MVT::Other);
517 Tys.push_back(MVT::Flag);
518 std::vector<SDOperand> Ops;
519 Ops.push_back(Chain);
520 Ops.push_back(InFlag);
521 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
522 Chain = RetVal.getValue(1);
523 InFlag = RetVal.getValue(2);
524 if (X86ScalarSSE) {
525 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
526 // shouldn't be necessary except that RFP cannot be live across
527 // multiple blocks. When stackifier is fixed, they can be uncoupled.
528 MachineFunction &MF = DAG.getMachineFunction();
529 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
530 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
531 Tys.clear();
532 Tys.push_back(MVT::Other);
533 Ops.clear();
534 Ops.push_back(Chain);
535 Ops.push_back(RetVal);
536 Ops.push_back(StackSlot);
537 Ops.push_back(DAG.getValueType(RetTyVT));
538 Ops.push_back(InFlag);
539 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
540 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
541 DAG.getSrcValue(NULL));
542 Chain = RetVal.getValue(1);
543 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000544
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000545 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
546 // FIXME: we would really like to remember that this FP_ROUND
547 // operation is okay to eliminate if we allow excess FP precision.
548 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
549 break;
550 }
551 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000553
554 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000555}
556
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000557//===----------------------------------------------------------------------===//
558// Fast Calling Convention implementation
559//===----------------------------------------------------------------------===//
560//
561// The X86 'fast' calling convention passes up to two integer arguments in
562// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
563// and requires that the callee pop its arguments off the stack (allowing proper
564// tail calls), and has the same return value conventions as C calling convs.
565//
566// This calling convention always arranges for the callee pop value to be 8n+4
567// bytes, which is needed for tail recursion elimination and stack alignment
568// reasons.
569//
570// Note that this can be enhanced in the future to pass fp vals in registers
571// (when we have a global fp allocator) and do other tricks.
572//
573
574/// AddLiveIn - This helper function adds the specified physical register to the
575/// MachineFunction as a live in value. It also creates a corresponding virtual
576/// register for it.
577static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
578 TargetRegisterClass *RC) {
579 assert(RC->contains(PReg) && "Not the correct regclass!");
580 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
581 MF.addLiveIn(PReg, VReg);
582 return VReg;
583}
584
585
586std::vector<SDOperand>
587X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
588 std::vector<SDOperand> ArgValues;
589
590 MachineFunction &MF = DAG.getMachineFunction();
591 MachineFrameInfo *MFI = MF.getFrameInfo();
592
593 // Add DAG nodes to load the arguments... On entry to a function the stack
594 // frame looks like this:
595 //
596 // [ESP] -- return address
597 // [ESP + 4] -- first nonreg argument (leftmost lexically)
598 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
599 // ...
600 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
601
602 // Keep track of the number of integer regs passed so far. This can be either
603 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
604 // used).
605 unsigned NumIntRegs = 0;
606
607 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
608 MVT::ValueType ObjectVT = getValueType(I->getType());
609 unsigned ArgIncrement = 4;
610 unsigned ObjSize = 0;
611 SDOperand ArgValue;
612
613 switch (ObjectVT) {
614 default: assert(0 && "Unhandled argument type!");
615 case MVT::i1:
616 case MVT::i8:
617 if (NumIntRegs < 2) {
618 if (!I->use_empty()) {
619 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
620 X86::R8RegisterClass);
621 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
622 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000623 if (ObjectVT == MVT::i1)
624 // FIXME: Should insert a assertzext here.
625 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
627 ++NumIntRegs;
628 break;
629 }
630
631 ObjSize = 1;
632 break;
633 case MVT::i16:
634 if (NumIntRegs < 2) {
635 if (!I->use_empty()) {
636 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
637 X86::R16RegisterClass);
638 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
639 DAG.setRoot(ArgValue.getValue(1));
640 }
641 ++NumIntRegs;
642 break;
643 }
644 ObjSize = 2;
645 break;
646 case MVT::i32:
647 if (NumIntRegs < 2) {
648 if (!I->use_empty()) {
649 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
650 X86::R32RegisterClass);
651 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
652 DAG.setRoot(ArgValue.getValue(1));
653 }
654 ++NumIntRegs;
655 break;
656 }
657 ObjSize = 4;
658 break;
659 case MVT::i64:
660 if (NumIntRegs == 0) {
661 if (!I->use_empty()) {
662 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
663 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
664
665 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
666 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
667 DAG.setRoot(Hi.getValue(1));
668
669 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
670 }
671 NumIntRegs = 2;
672 break;
673 } else if (NumIntRegs == 1) {
674 if (!I->use_empty()) {
675 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
676 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
677 DAG.setRoot(Low.getValue(1));
678
679 // Load the high part from memory.
680 // Create the frame index object for this incoming parameter...
681 int FI = MFI->CreateFixedObject(4, ArgOffset);
682 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
683 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
684 DAG.getSrcValue(NULL));
685 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
686 }
687 ArgOffset += 4;
688 NumIntRegs = 2;
689 break;
690 }
691 ObjSize = ArgIncrement = 8;
692 break;
693 case MVT::f32: ObjSize = 4; break;
694 case MVT::f64: ObjSize = ArgIncrement = 8; break;
695 }
696
697 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
698 // dead loads.
699 if (ObjSize && !I->use_empty()) {
700 // Create the frame index object for this incoming parameter...
701 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
702
703 // Create the SelectionDAG nodes corresponding to a load from this
704 // parameter.
705 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
706
707 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
708 DAG.getSrcValue(NULL));
709 } else if (ArgValue.Val == 0) {
710 if (MVT::isInteger(ObjectVT))
711 ArgValue = DAG.getConstant(0, ObjectVT);
712 else
713 ArgValue = DAG.getConstantFP(0, ObjectVT);
714 }
715 ArgValues.push_back(ArgValue);
716
717 if (ObjSize)
718 ArgOffset += ArgIncrement; // Move on to the next argument.
719 }
720
721 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
722 // arguments and the arguments after the retaddr has been pushed are aligned.
723 if ((ArgOffset & 7) == 0)
724 ArgOffset += 4;
725
726 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
727 ReturnAddrIndex = 0; // No return address slot generated yet.
728 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
729 BytesCallerReserves = 0;
730
731 // Finally, inform the code generator which regs we return values in.
732 switch (getValueType(F.getReturnType())) {
733 default: assert(0 && "Unknown type!");
734 case MVT::isVoid: break;
735 case MVT::i1:
736 case MVT::i8:
737 case MVT::i16:
738 case MVT::i32:
739 MF.addLiveOut(X86::EAX);
740 break;
741 case MVT::i64:
742 MF.addLiveOut(X86::EAX);
743 MF.addLiveOut(X86::EDX);
744 break;
745 case MVT::f32:
746 case MVT::f64:
747 MF.addLiveOut(X86::ST0);
748 break;
749 }
750 return ArgValues;
751}
752
753std::pair<SDOperand, SDOperand>
754X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
755 bool isTailCall, SDOperand Callee,
756 ArgListTy &Args, SelectionDAG &DAG) {
757 // Count how many bytes are to be pushed on the stack.
758 unsigned NumBytes = 0;
759
760 // Keep track of the number of integer regs passed so far. This can be either
761 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
762 // used).
763 unsigned NumIntRegs = 0;
764
765 for (unsigned i = 0, e = Args.size(); i != e; ++i)
766 switch (getValueType(Args[i].second)) {
767 default: assert(0 && "Unknown value type!");
768 case MVT::i1:
769 case MVT::i8:
770 case MVT::i16:
771 case MVT::i32:
772 if (NumIntRegs < 2) {
773 ++NumIntRegs;
774 break;
775 }
776 // fall through
777 case MVT::f32:
778 NumBytes += 4;
779 break;
780 case MVT::i64:
781 if (NumIntRegs == 0) {
782 NumIntRegs = 2;
783 break;
784 } else if (NumIntRegs == 1) {
785 NumIntRegs = 2;
786 NumBytes += 4;
787 break;
788 }
789
790 // fall through
791 case MVT::f64:
792 NumBytes += 8;
793 break;
794 }
795
796 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
797 // arguments and the arguments after the retaddr has been pushed are aligned.
798 if ((NumBytes & 7) == 0)
799 NumBytes += 4;
800
Chris Lattner94dd2922006-02-13 09:00:43 +0000801 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000802
803 // Arguments go on the stack in reverse order, as specified by the ABI.
804 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000805 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000806 NumIntRegs = 0;
807 std::vector<SDOperand> Stores;
808 std::vector<SDOperand> RegValuesToPass;
809 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
810 switch (getValueType(Args[i].second)) {
811 default: assert(0 && "Unexpected ValueType for argument!");
812 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000813 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
814 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000815 case MVT::i8:
816 case MVT::i16:
817 case MVT::i32:
818 if (NumIntRegs < 2) {
819 RegValuesToPass.push_back(Args[i].first);
820 ++NumIntRegs;
821 break;
822 }
823 // Fall through
824 case MVT::f32: {
825 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
826 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
827 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
828 Args[i].first, PtrOff,
829 DAG.getSrcValue(NULL)));
830 ArgOffset += 4;
831 break;
832 }
833 case MVT::i64:
834 if (NumIntRegs < 2) { // Can pass part of it in regs?
835 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
836 Args[i].first, DAG.getConstant(1, MVT::i32));
837 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
838 Args[i].first, DAG.getConstant(0, MVT::i32));
839 RegValuesToPass.push_back(Lo);
840 ++NumIntRegs;
841 if (NumIntRegs < 2) { // Pass both parts in regs?
842 RegValuesToPass.push_back(Hi);
843 ++NumIntRegs;
844 } else {
845 // Pass the high part in memory.
846 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
847 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
848 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
849 Hi, PtrOff, DAG.getSrcValue(NULL)));
850 ArgOffset += 4;
851 }
852 break;
853 }
854 // Fall through
855 case MVT::f64:
856 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
857 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
858 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
859 Args[i].first, PtrOff,
860 DAG.getSrcValue(NULL)));
861 ArgOffset += 8;
862 break;
863 }
864 }
865 if (!Stores.empty())
866 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
867
868 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
869 // arguments and the arguments after the retaddr has been pushed are aligned.
870 if ((ArgOffset & 7) == 0)
871 ArgOffset += 4;
872
873 std::vector<MVT::ValueType> RetVals;
874 MVT::ValueType RetTyVT = getValueType(RetTy);
875
876 RetVals.push_back(MVT::Other);
877
878 // The result values produced have to be legal. Promote the result.
879 switch (RetTyVT) {
880 case MVT::isVoid: break;
881 default:
882 RetVals.push_back(RetTyVT);
883 break;
884 case MVT::i1:
885 case MVT::i8:
886 case MVT::i16:
887 RetVals.push_back(MVT::i32);
888 break;
889 case MVT::f32:
890 if (X86ScalarSSE)
891 RetVals.push_back(MVT::f32);
892 else
893 RetVals.push_back(MVT::f64);
894 break;
895 case MVT::i64:
896 RetVals.push_back(MVT::i32);
897 RetVals.push_back(MVT::i32);
898 break;
899 }
900
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000901 // Build a sequence of copy-to-reg nodes chained together with token chain
902 // and flag operands which copy the outgoing args into registers.
903 SDOperand InFlag;
904 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
905 unsigned CCReg;
906 SDOperand RegToPass = RegValuesToPass[i];
907 switch (RegToPass.getValueType()) {
908 default: assert(0 && "Bad thing to pass in regs");
909 case MVT::i8:
910 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000911 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000912 case MVT::i16:
913 CCReg = (i == 0) ? X86::AX : X86::DX;
914 break;
915 case MVT::i32:
916 CCReg = (i == 0) ? X86::EAX : X86::EDX;
917 break;
918 }
919
920 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
921 InFlag = Chain.getValue(1);
922 }
923
924 std::vector<MVT::ValueType> NodeTys;
925 NodeTys.push_back(MVT::Other); // Returns a chain
926 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
927 std::vector<SDOperand> Ops;
928 Ops.push_back(Chain);
929 Ops.push_back(Callee);
930 if (InFlag.Val)
931 Ops.push_back(InFlag);
932
933 // FIXME: Do not generate X86ISD::TAILCALL for now.
934 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
935 InFlag = Chain.getValue(1);
936
937 NodeTys.clear();
938 NodeTys.push_back(MVT::Other); // Returns a chain
939 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
940 Ops.clear();
941 Ops.push_back(Chain);
942 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
943 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
944 Ops.push_back(InFlag);
945 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
946 InFlag = Chain.getValue(1);
947
948 SDOperand RetVal;
949 if (RetTyVT != MVT::isVoid) {
950 switch (RetTyVT) {
951 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +0000952 case MVT::i1:
953 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000954 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
955 Chain = RetVal.getValue(1);
956 if (RetTyVT == MVT::i1)
957 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
958 break;
Evan Chengd9558e02006-01-06 00:43:03 +0000959 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000960 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
961 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +0000962 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000963 case MVT::i32:
964 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
965 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +0000966 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000967 case MVT::i64: {
968 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
969 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
970 Lo.getValue(2));
971 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
972 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +0000973 break;
974 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000975 case MVT::f32:
976 case MVT::f64: {
977 std::vector<MVT::ValueType> Tys;
978 Tys.push_back(MVT::f64);
979 Tys.push_back(MVT::Other);
980 Tys.push_back(MVT::Flag);
981 std::vector<SDOperand> Ops;
982 Ops.push_back(Chain);
983 Ops.push_back(InFlag);
984 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
985 Chain = RetVal.getValue(1);
986 InFlag = RetVal.getValue(2);
987 if (X86ScalarSSE) {
988 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
989 // shouldn't be necessary except that RFP cannot be live across
990 // multiple blocks. When stackifier is fixed, they can be uncoupled.
991 MachineFunction &MF = DAG.getMachineFunction();
992 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
993 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
994 Tys.clear();
995 Tys.push_back(MVT::Other);
996 Ops.clear();
997 Ops.push_back(Chain);
998 Ops.push_back(RetVal);
999 Ops.push_back(StackSlot);
1000 Ops.push_back(DAG.getValueType(RetTyVT));
1001 Ops.push_back(InFlag);
1002 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1003 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1004 DAG.getSrcValue(NULL));
1005 Chain = RetVal.getValue(1);
1006 }
Evan Chengd9558e02006-01-06 00:43:03 +00001007
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001008 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1009 // FIXME: we would really like to remember that this FP_ROUND
1010 // operation is okay to eliminate if we allow excess FP precision.
1011 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1012 break;
1013 }
1014 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001015 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001016
1017 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001018}
1019
1020SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1021 if (ReturnAddrIndex == 0) {
1022 // Set up a frame object for the return address.
1023 MachineFunction &MF = DAG.getMachineFunction();
1024 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1025 }
1026
1027 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1028}
1029
1030
1031
1032std::pair<SDOperand, SDOperand> X86TargetLowering::
1033LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1034 SelectionDAG &DAG) {
1035 SDOperand Result;
1036 if (Depth) // Depths > 0 not supported yet!
1037 Result = DAG.getConstant(0, getPointerTy());
1038 else {
1039 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1040 if (!isFrameAddress)
1041 // Just load the return address
1042 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1043 DAG.getSrcValue(NULL));
1044 else
1045 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1046 DAG.getConstant(4, MVT::i32));
1047 }
1048 return std::make_pair(Result, Chain);
1049}
1050
Evan Cheng4a460802006-01-11 00:33:36 +00001051/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1052/// which corresponds to the condition code.
1053static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1054 switch (X86CC) {
1055 default: assert(0 && "Unknown X86 conditional code!");
1056 case X86ISD::COND_A: return X86::JA;
1057 case X86ISD::COND_AE: return X86::JAE;
1058 case X86ISD::COND_B: return X86::JB;
1059 case X86ISD::COND_BE: return X86::JBE;
1060 case X86ISD::COND_E: return X86::JE;
1061 case X86ISD::COND_G: return X86::JG;
1062 case X86ISD::COND_GE: return X86::JGE;
1063 case X86ISD::COND_L: return X86::JL;
1064 case X86ISD::COND_LE: return X86::JLE;
1065 case X86ISD::COND_NE: return X86::JNE;
1066 case X86ISD::COND_NO: return X86::JNO;
1067 case X86ISD::COND_NP: return X86::JNP;
1068 case X86ISD::COND_NS: return X86::JNS;
1069 case X86ISD::COND_O: return X86::JO;
1070 case X86ISD::COND_P: return X86::JP;
1071 case X86ISD::COND_S: return X86::JS;
1072 }
1073}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001074
Evan Cheng6dfa9992006-01-30 23:41:35 +00001075/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1076/// specific condition code. It returns a false if it cannot do a direct
1077/// translation. X86CC is the translated CondCode. Flip is set to true if the
1078/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001079static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1080 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001081 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001082 Flip = false;
1083 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001084 if (!isFP) {
1085 switch (SetCCOpcode) {
1086 default: break;
1087 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1088 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1089 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1090 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1091 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1092 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1093 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1094 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1095 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1096 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1097 }
1098 } else {
1099 // On a floating point condition, the flags are set as follows:
1100 // ZF PF CF op
1101 // 0 | 0 | 0 | X > Y
1102 // 0 | 0 | 1 | X < Y
1103 // 1 | 0 | 0 | X == Y
1104 // 1 | 1 | 1 | unordered
1105 switch (SetCCOpcode) {
1106 default: break;
1107 case ISD::SETUEQ:
1108 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001109 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001110 case ISD::SETOGT:
1111 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001112 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001113 case ISD::SETOGE:
1114 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001115 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001116 case ISD::SETULT:
1117 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001118 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001119 case ISD::SETULE:
1120 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1121 case ISD::SETONE:
1122 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1123 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1124 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1125 }
1126 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001127
1128 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001129}
1130
Evan Cheng4a460802006-01-11 00:33:36 +00001131/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1132/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001133/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001134static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001135 switch (X86CC) {
1136 default:
1137 return false;
1138 case X86ISD::COND_B:
1139 case X86ISD::COND_BE:
1140 case X86ISD::COND_E:
1141 case X86ISD::COND_P:
1142 case X86ISD::COND_A:
1143 case X86ISD::COND_AE:
1144 case X86ISD::COND_NE:
1145 case X86ISD::COND_NP:
1146 return true;
1147 }
1148}
1149
Evan Cheng4a460802006-01-11 00:33:36 +00001150MachineBasicBlock *
1151X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1152 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001153 switch (MI->getOpcode()) {
1154 default: assert(false && "Unexpected instr type to insert");
1155 case X86::CMOV_FR32:
1156 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001157 // To "insert" a SELECT_CC instruction, we actually have to insert the
1158 // diamond control-flow pattern. The incoming instruction knows the
1159 // destination vreg to set, the condition code register to branch on, the
1160 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001161 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1162 ilist<MachineBasicBlock>::iterator It = BB;
1163 ++It;
1164
1165 // thisMBB:
1166 // ...
1167 // TrueVal = ...
1168 // cmpTY ccX, r1, r2
1169 // bCC copy1MBB
1170 // fallthrough --> copy0MBB
1171 MachineBasicBlock *thisMBB = BB;
1172 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1173 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1174 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1175 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1176 MachineFunction *F = BB->getParent();
1177 F->getBasicBlockList().insert(It, copy0MBB);
1178 F->getBasicBlockList().insert(It, sinkMBB);
1179 // Update machine-CFG edges
1180 BB->addSuccessor(copy0MBB);
1181 BB->addSuccessor(sinkMBB);
1182
1183 // copy0MBB:
1184 // %FalseValue = ...
1185 // # fallthrough to sinkMBB
1186 BB = copy0MBB;
1187
1188 // Update machine-CFG edges
1189 BB->addSuccessor(sinkMBB);
1190
1191 // sinkMBB:
1192 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1193 // ...
1194 BB = sinkMBB;
1195 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1196 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1197 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001198
Evan Cheng0cc39452006-01-16 21:21:29 +00001199 delete MI; // The pseudo instruction is gone now.
1200 return BB;
1201 }
Evan Cheng4a460802006-01-11 00:33:36 +00001202
Evan Cheng0cc39452006-01-16 21:21:29 +00001203 case X86::FP_TO_INT16_IN_MEM:
1204 case X86::FP_TO_INT32_IN_MEM:
1205 case X86::FP_TO_INT64_IN_MEM: {
1206 // Change the floating point control register to use "round towards zero"
1207 // mode when truncating to an integer value.
1208 MachineFunction *F = BB->getParent();
1209 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1210 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1211
1212 // Load the old value of the high byte of the control word...
1213 unsigned OldCW =
1214 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1215 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1216
1217 // Set the high part to be round to zero...
1218 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1219
1220 // Reload the modified control word now...
1221 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1222
1223 // Restore the memory image of control word to original value
1224 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1225
1226 // Get the X86 opcode to use.
1227 unsigned Opc;
1228 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001229 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001230 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1231 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1232 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1233 }
1234
1235 X86AddressMode AM;
1236 MachineOperand &Op = MI->getOperand(0);
1237 if (Op.isRegister()) {
1238 AM.BaseType = X86AddressMode::RegBase;
1239 AM.Base.Reg = Op.getReg();
1240 } else {
1241 AM.BaseType = X86AddressMode::FrameIndexBase;
1242 AM.Base.FrameIndex = Op.getFrameIndex();
1243 }
1244 Op = MI->getOperand(1);
1245 if (Op.isImmediate())
1246 AM.Scale = Op.getImmedValue();
1247 Op = MI->getOperand(2);
1248 if (Op.isImmediate())
1249 AM.IndexReg = Op.getImmedValue();
1250 Op = MI->getOperand(3);
1251 if (Op.isGlobalAddress()) {
1252 AM.GV = Op.getGlobal();
1253 } else {
1254 AM.Disp = Op.getImmedValue();
1255 }
1256 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1257
1258 // Reload the original control word now.
1259 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1260
1261 delete MI; // The pseudo instruction is gone now.
1262 return BB;
1263 }
1264 }
Evan Cheng4a460802006-01-11 00:33:36 +00001265}
1266
1267
1268//===----------------------------------------------------------------------===//
1269// X86 Custom Lowering Hooks
1270//===----------------------------------------------------------------------===//
1271
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001272/// LowerOperation - Provide custom lowering hooks for some operations.
1273///
1274SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1275 switch (Op.getOpcode()) {
1276 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001277 case ISD::SHL_PARTS:
1278 case ISD::SRA_PARTS:
1279 case ISD::SRL_PARTS: {
1280 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1281 "Not an i64 shift!");
1282 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1283 SDOperand ShOpLo = Op.getOperand(0);
1284 SDOperand ShOpHi = Op.getOperand(1);
1285 SDOperand ShAmt = Op.getOperand(2);
1286 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001287 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001288 : DAG.getConstant(0, MVT::i32);
1289
1290 SDOperand Tmp2, Tmp3;
1291 if (Op.getOpcode() == ISD::SHL_PARTS) {
1292 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1293 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1294 } else {
1295 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001296 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001297 }
1298
1299 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1300 ShAmt, DAG.getConstant(32, MVT::i8));
1301
1302 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001303 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001304
1305 std::vector<MVT::ValueType> Tys;
1306 Tys.push_back(MVT::i32);
1307 Tys.push_back(MVT::Flag);
1308 std::vector<SDOperand> Ops;
1309 if (Op.getOpcode() == ISD::SHL_PARTS) {
1310 Ops.push_back(Tmp2);
1311 Ops.push_back(Tmp3);
1312 Ops.push_back(CC);
1313 Ops.push_back(InFlag);
1314 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1315 InFlag = Hi.getValue(1);
1316
1317 Ops.clear();
1318 Ops.push_back(Tmp3);
1319 Ops.push_back(Tmp1);
1320 Ops.push_back(CC);
1321 Ops.push_back(InFlag);
1322 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1323 } else {
1324 Ops.push_back(Tmp2);
1325 Ops.push_back(Tmp3);
1326 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001327 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001328 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1329 InFlag = Lo.getValue(1);
1330
1331 Ops.clear();
1332 Ops.push_back(Tmp3);
1333 Ops.push_back(Tmp1);
1334 Ops.push_back(CC);
1335 Ops.push_back(InFlag);
1336 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1337 }
1338
1339 Tys.clear();
1340 Tys.push_back(MVT::i32);
1341 Tys.push_back(MVT::i32);
1342 Ops.clear();
1343 Ops.push_back(Lo);
1344 Ops.push_back(Hi);
1345 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1346 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001347 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001348 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001349 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001350 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001351
1352 SDOperand Result;
1353 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1354 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001355 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001356 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001357 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001358 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1359 DAG.getEntryNode(), Op.getOperand(0),
1360 StackSlot, DAG.getSrcValue(NULL));
1361
1362 // Build the FILD
1363 std::vector<MVT::ValueType> Tys;
1364 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001365 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001366 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001367 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001368 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001369 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001370 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001371 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1372 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001373
1374 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001375 Chain = Result.getValue(1);
1376 SDOperand InFlag = Result.getValue(2);
1377
Evan Chenge3de85b2006-02-04 02:20:30 +00001378 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001379 // shouldn't be necessary except that RFP cannot be live across
1380 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1381 MachineFunction &MF = DAG.getMachineFunction();
1382 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1383 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1384 std::vector<MVT::ValueType> Tys;
1385 Tys.push_back(MVT::Other);
1386 std::vector<SDOperand> Ops;
1387 Ops.push_back(Chain);
1388 Ops.push_back(Result);
1389 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001390 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001391 Ops.push_back(InFlag);
1392 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1393 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1394 DAG.getSrcValue(NULL));
1395 }
1396
Evan Chenga3195e82006-01-12 22:54:21 +00001397 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001398 }
1399 case ISD::FP_TO_SINT: {
1400 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001401 "Unknown FP_TO_SINT to lower!");
1402 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1403 // stack slot.
1404 MachineFunction &MF = DAG.getMachineFunction();
1405 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1406 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1407 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1408
1409 unsigned Opc;
1410 switch (Op.getValueType()) {
1411 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1412 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1413 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1414 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1415 }
1416
Evan Cheng6dab0532006-01-30 08:02:57 +00001417 SDOperand Chain = DAG.getEntryNode();
1418 SDOperand Value = Op.getOperand(0);
1419 if (X86ScalarSSE) {
1420 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1421 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1422 DAG.getSrcValue(0));
1423 std::vector<MVT::ValueType> Tys;
1424 Tys.push_back(MVT::f64);
1425 Tys.push_back(MVT::Other);
1426 std::vector<SDOperand> Ops;
1427 Ops.push_back(Chain);
1428 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001429 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001430 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1431 Chain = Value.getValue(1);
1432 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1433 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1434 }
1435
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001436 // Build the FP_TO_INT*_IN_MEM
1437 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001438 Ops.push_back(Chain);
1439 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001440 Ops.push_back(StackSlot);
1441 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1442
1443 // Load the result.
1444 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1445 DAG.getSrcValue(NULL));
1446 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001447 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001448 std::vector<MVT::ValueType> Tys;
1449 Tys.push_back(MVT::Other);
1450 Tys.push_back(MVT::Flag);
1451 std::vector<SDOperand> Ops;
1452 Ops.push_back(Op.getOperand(0));
1453 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001454 Ops.clear();
1455 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1456 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1457 MVT::i32, Ops[0].getValue(2)));
1458 Ops.push_back(Ops[1].getValue(1));
1459 Tys[0] = Tys[1] = MVT::i32;
1460 Tys.push_back(MVT::Other);
1461 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001462 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001463 case ISD::FABS: {
1464 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001465 const Type *OpNTy = MVT::getTypeForValueType(VT);
1466 std::vector<Constant*> CV;
1467 if (VT == MVT::f64) {
1468 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1469 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1470 } else {
1471 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1472 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1473 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1474 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1475 }
1476 Constant *CS = ConstantStruct::get(CV);
1477 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1478 SDOperand Mask
1479 = DAG.getNode(X86ISD::LOAD_PACK,
1480 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001481 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1482 }
Evan Cheng223547a2006-01-31 22:28:30 +00001483 case ISD::FNEG: {
1484 MVT::ValueType VT = Op.getValueType();
1485 const Type *OpNTy = MVT::getTypeForValueType(VT);
1486 std::vector<Constant*> CV;
1487 if (VT == MVT::f64) {
1488 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1489 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1490 } else {
1491 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1492 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1493 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1494 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1495 }
1496 Constant *CS = ConstantStruct::get(CV);
1497 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1498 SDOperand Mask
1499 = DAG.getNode(X86ISD::LOAD_PACK,
1500 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1501 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1502 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001503 case ISD::SETCC: {
1504 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001505 SDOperand Cond;
1506 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001507 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1508 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001509 bool Flip;
1510 unsigned X86CC;
1511 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1512 if (Flip)
1513 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1514 Op.getOperand(1), Op.getOperand(0));
1515 else
1516 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1517 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001518 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1519 DAG.getConstant(X86CC, MVT::i8), Cond);
1520 } else {
1521 assert(isFP && "Illegal integer SetCC!");
1522
Evan Cheng6dfa9992006-01-30 23:41:35 +00001523 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1524 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001525 std::vector<MVT::ValueType> Tys;
1526 std::vector<SDOperand> Ops;
1527 switch (SetCCOpcode) {
1528 default: assert(false && "Illegal floating point SetCC!");
1529 case ISD::SETOEQ: { // !PF & ZF
1530 Tys.push_back(MVT::i8);
1531 Tys.push_back(MVT::Flag);
1532 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1533 Ops.push_back(Cond);
1534 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1535 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1536 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1537 Tmp1.getValue(1));
1538 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1539 }
Evan Chengd9558e02006-01-06 00:43:03 +00001540 case ISD::SETUNE: { // PF | !ZF
1541 Tys.push_back(MVT::i8);
1542 Tys.push_back(MVT::Flag);
1543 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1544 Ops.push_back(Cond);
1545 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1546 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1547 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1548 Tmp1.getValue(1));
1549 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1550 }
1551 }
1552 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001553 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001554 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001555 MVT::ValueType VT = Op.getValueType();
1556 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001557 bool isFPStack = isFP && !X86ScalarSSE;
1558 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001559 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001560 SDOperand Op0 = Op.getOperand(0);
1561 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001562 if (Op0.getOpcode() == ISD::SETCC)
1563 Op0 = LowerOperation(Op0, DAG);
1564
Evan Chengaaca22c2006-01-10 20:26:56 +00001565 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001566 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1567 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1568 // have another use it will be eliminated.
1569 // If the X86ISD::SETCC has more than one use, then it's probably better
1570 // to use a test instead of duplicating the X86ISD::CMP (for register
1571 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001572 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1573 if (!Op0.hasOneUse()) {
1574 std::vector<MVT::ValueType> Tys;
1575 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1576 Tys.push_back(Op0.Val->getValueType(i));
1577 std::vector<SDOperand> Ops;
1578 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1579 Ops.push_back(Op0.getOperand(i));
1580 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1581 }
1582
Evan Cheng1bcee362006-01-13 01:03:02 +00001583 CC = Op0.getOperand(0);
1584 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001585 // Make a copy as flag result cannot be used by more than one.
1586 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1587 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001588 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001589 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001590 } else
1591 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001592 } else
1593 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001594
Evan Cheng189d01e2006-01-13 01:06:49 +00001595 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001596 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001597 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001598 }
Evan Chenge3413162006-01-09 18:33:28 +00001599
1600 std::vector<MVT::ValueType> Tys;
1601 Tys.push_back(Op.getValueType());
1602 Tys.push_back(MVT::Flag);
1603 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001604 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1605 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001606 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001607 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001608 Ops.push_back(CC);
1609 Ops.push_back(Cond);
1610 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001611 }
Evan Cheng898101c2005-12-19 23:12:38 +00001612 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001613 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001614 SDOperand Cond = Op.getOperand(1);
1615 SDOperand Dest = Op.getOperand(2);
1616 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001617 if (Cond.getOpcode() == ISD::SETCC)
1618 Cond = LowerOperation(Cond, DAG);
1619
Evan Chengd5781fc2005-12-21 20:21:51 +00001620 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001621 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1622 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1623 // have another use it will be eliminated.
1624 // If the X86ISD::SETCC has more than one use, then it's probably better
1625 // to use a test instead of duplicating the X86ISD::CMP (for register
1626 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001627 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1628 if (!Cond.hasOneUse()) {
1629 std::vector<MVT::ValueType> Tys;
1630 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1631 Tys.push_back(Cond.Val->getValueType(i));
1632 std::vector<SDOperand> Ops;
1633 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1634 Ops.push_back(Cond.getOperand(i));
1635 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1636 }
1637
Evan Cheng1bcee362006-01-13 01:03:02 +00001638 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001639 Cond = Cond.getOperand(1);
1640 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001641 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001642 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001643 } else
1644 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001645 } else
1646 addTest = true;
1647
1648 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001649 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001650 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1651 }
1652 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1653 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1654 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001655 case ISD::MEMSET: {
1656 SDOperand InFlag;
1657 SDOperand Chain = Op.getOperand(0);
1658 unsigned Align =
1659 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1660 if (Align == 0) Align = 1;
1661
Evan Cheng18a84522006-02-16 00:21:07 +00001662 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1663 // If not DWORD aligned, call memset if size is less than the threshold.
1664 // It knows how to align to the right boundary first.
1665 if ((Align & 3) != 0 &&
1666 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1667 MVT::ValueType IntPtr = getPointerTy();
1668 const Type *IntPtrTy = getTargetData().getIntPtrType();
1669 std::vector<std::pair<SDOperand, const Type*> > Args;
1670 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1671 // Extend the ubyte argument to be an int value for the call.
1672 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1673 Args.push_back(std::make_pair(Val, IntPtrTy));
1674 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1675 std::pair<SDOperand,SDOperand> CallResult =
1676 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1677 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1678 return CallResult.second;
1679 }
1680
Evan Cheng67f92a72006-01-11 22:15:48 +00001681 MVT::ValueType AVT;
1682 SDOperand Count;
1683 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1684 unsigned ValReg;
1685 unsigned Val = ValC->getValue() & 255;
1686
1687 // If the value is a constant, then we can potentially use larger sets.
1688 switch (Align & 3) {
1689 case 2: // WORD aligned
1690 AVT = MVT::i16;
Evan Cheng18a84522006-02-16 00:21:07 +00001691 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001692 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1693 else
1694 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1695 DAG.getConstant(1, MVT::i8));
1696 Val = (Val << 8) | Val;
1697 ValReg = X86::AX;
1698 break;
1699 case 0: // DWORD aligned
1700 AVT = MVT::i32;
Evan Cheng18a84522006-02-16 00:21:07 +00001701 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001702 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1703 else
1704 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1705 DAG.getConstant(2, MVT::i8));
1706 Val = (Val << 8) | Val;
1707 Val = (Val << 16) | Val;
1708 ValReg = X86::EAX;
1709 break;
1710 default: // Byte aligned
1711 AVT = MVT::i8;
1712 Count = Op.getOperand(3);
1713 ValReg = X86::AL;
1714 break;
1715 }
1716
1717 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1718 InFlag);
1719 InFlag = Chain.getValue(1);
1720 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001721 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001722 Count = Op.getOperand(3);
1723 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1724 InFlag = Chain.getValue(1);
1725 }
1726
1727 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1728 InFlag = Chain.getValue(1);
1729 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1730 InFlag = Chain.getValue(1);
1731
1732 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1733 DAG.getValueType(AVT), InFlag);
1734 }
1735 case ISD::MEMCPY: {
1736 SDOperand Chain = Op.getOperand(0);
1737 unsigned Align =
1738 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1739 if (Align == 0) Align = 1;
1740
Evan Cheng18a84522006-02-16 00:21:07 +00001741 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1742 // If not DWORD aligned, call memcpy if size is less than the threshold.
1743 // It knows how to align to the right boundary first.
1744 if ((Align & 3) != 0 &&
1745 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1746 MVT::ValueType IntPtr = getPointerTy();
1747 const Type *IntPtrTy = getTargetData().getIntPtrType();
1748 std::vector<std::pair<SDOperand, const Type*> > Args;
1749 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1750 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1751 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1752 std::pair<SDOperand,SDOperand> CallResult =
1753 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1754 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1755 return CallResult.second;
1756 }
1757
Evan Cheng67f92a72006-01-11 22:15:48 +00001758 MVT::ValueType AVT;
1759 SDOperand Count;
1760 switch (Align & 3) {
1761 case 2: // WORD aligned
1762 AVT = MVT::i16;
Evan Cheng18a84522006-02-16 00:21:07 +00001763 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001764 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1765 else
Evan Cheng18a84522006-02-16 00:21:07 +00001766 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
Evan Cheng67f92a72006-01-11 22:15:48 +00001767 break;
1768 case 0: // DWORD aligned
1769 AVT = MVT::i32;
Evan Cheng18a84522006-02-16 00:21:07 +00001770 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001771 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1772 else
1773 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1774 DAG.getConstant(2, MVT::i8));
1775 break;
1776 default: // Byte aligned
1777 AVT = MVT::i8;
1778 Count = Op.getOperand(3);
1779 break;
1780 }
1781
1782 SDOperand InFlag;
1783 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1784 InFlag = Chain.getValue(1);
1785 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1786 InFlag = Chain.getValue(1);
1787 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1788 InFlag = Chain.getValue(1);
1789
1790 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1791 DAG.getValueType(AVT), InFlag);
1792 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001793 case ISD::ConstantPool: {
1794 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1795 SDOperand Result =
1796 DAG.getTargetConstantPool(CP->get(), getPointerTy(), CP->getAlignment());
1797 // Only lower ConstantPool on Darwin.
1798 if (getTargetMachine().
1799 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1800 // With PIC, the address is actually $g + Offset.
1801 if (PICEnabled)
1802 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1803 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1804 }
1805
1806 return Result;
1807 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001808 case ISD::GlobalAddress: {
Evan Cheng002fe9b2006-01-12 07:56:47 +00001809 SDOperand Result;
Evan Cheng7ccced62006-02-18 00:15:05 +00001810 // Only lower GlobalAddress on Darwin.
Evan Chengb077b842005-12-21 02:39:21 +00001811 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00001812 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1813 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1814 SDOperand Addr = DAG.getTargetGlobalAddress(GV, getPointerTy());
1815 // With PIC, the address is actually $g + Offset.
1816 if (PICEnabled)
1817 Addr = DAG.getNode(ISD::ADD, getPointerTy(),
1818 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Addr);
1819
1820 // For Darwin, external and weak symbols are indirect, so we want to load
1821 // the value at address GV, not the value of GV itself. This means that
1822 // the GlobalAddress must be in the base or index register of the address,
1823 // not the GV offset field.
Evan Cheng2338c5c2006-02-07 08:38:37 +00001824 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1825 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
1826 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng7ccced62006-02-18 00:15:05 +00001827 Addr, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00001828 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001829
Evan Cheng002fe9b2006-01-12 07:56:47 +00001830 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001831 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001832 case ISD::VASTART: {
1833 // vastart just stores the address of the VarArgsFrameIndex slot into the
1834 // memory location argument.
1835 // FIXME: Replace MVT::i32 with PointerTy
1836 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1837 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1838 Op.getOperand(1), Op.getOperand(2));
1839 }
Nate Begemanee625572006-01-27 21:09:22 +00001840 case ISD::RET: {
1841 SDOperand Copy;
1842
1843 switch(Op.getNumOperands()) {
1844 default:
1845 assert(0 && "Do not know how to return this many arguments!");
1846 abort();
1847 case 1:
1848 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1849 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1850 case 2: {
1851 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1852 if (MVT::isInteger(ArgVT))
1853 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1854 SDOperand());
1855 else if (!X86ScalarSSE) {
1856 std::vector<MVT::ValueType> Tys;
1857 Tys.push_back(MVT::Other);
1858 Tys.push_back(MVT::Flag);
1859 std::vector<SDOperand> Ops;
1860 Ops.push_back(Op.getOperand(0));
1861 Ops.push_back(Op.getOperand(1));
1862 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1863 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00001864 SDOperand MemLoc;
1865 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001866 SDOperand Value = Op.getOperand(1);
1867
Evan Cheng760df292006-02-01 01:19:32 +00001868 if (Value.getOpcode() == ISD::LOAD &&
1869 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00001870 Chain = Value.getOperand(0);
1871 MemLoc = Value.getOperand(1);
1872 } else {
1873 // Spill the value to memory and reload it into top of stack.
1874 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1875 MachineFunction &MF = DAG.getMachineFunction();
1876 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1877 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1878 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1879 Value, MemLoc, DAG.getSrcValue(0));
1880 }
Nate Begemanee625572006-01-27 21:09:22 +00001881 std::vector<MVT::ValueType> Tys;
1882 Tys.push_back(MVT::f64);
1883 Tys.push_back(MVT::Other);
1884 std::vector<SDOperand> Ops;
1885 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001886 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00001887 Ops.push_back(DAG.getValueType(ArgVT));
1888 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1889 Tys.clear();
1890 Tys.push_back(MVT::Other);
1891 Tys.push_back(MVT::Flag);
1892 Ops.clear();
1893 Ops.push_back(Copy.getValue(1));
1894 Ops.push_back(Copy);
1895 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1896 }
1897 break;
1898 }
1899 case 3:
1900 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1901 SDOperand());
1902 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1903 break;
1904 }
1905 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1906 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1907 Copy.getValue(1));
1908 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001909 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001910}
Evan Cheng72261582005-12-20 06:22:03 +00001911
1912const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1913 switch (Opcode) {
1914 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001915 case X86ISD::SHLD: return "X86ISD::SHLD";
1916 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00001917 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00001918 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00001919 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00001920 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00001921 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1922 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1923 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001924 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001925 case X86ISD::FST: return "X86ISD::FST";
1926 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001927 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001928 case X86ISD::CALL: return "X86ISD::CALL";
1929 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1930 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1931 case X86ISD::CMP: return "X86ISD::CMP";
1932 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001933 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001934 case X86ISD::CMOV: return "X86ISD::CMOV";
1935 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001936 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng67f92a72006-01-11 22:15:48 +00001937 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1938 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00001939 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00001940 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng72261582005-12-20 06:22:03 +00001941 }
1942}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001943
Nate Begeman368e18d2006-02-16 21:11:51 +00001944void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1945 uint64_t Mask,
1946 uint64_t &KnownZero,
1947 uint64_t &KnownOne,
1948 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001949
1950 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00001951 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001952
1953 switch (Opc) {
1954 default:
1955 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1956 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001957 case X86ISD::SETCC:
1958 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1959 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001960 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001961}
Chris Lattner259e97c2006-01-31 19:43:35 +00001962
1963std::vector<unsigned> X86TargetLowering::
1964getRegForInlineAsmConstraint(const std::string &Constraint) const {
1965 if (Constraint.size() == 1) {
1966 // FIXME: not handling fp-stack yet!
1967 // FIXME: not handling MMX registers yet ('y' constraint).
1968 switch (Constraint[0]) { // GCC X86 Constraint Letters
1969 default: break; // Unknown constriant letter
1970 case 'r': // GENERAL_REGS
1971 case 'R': // LEGACY_REGS
1972 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1973 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
1974 case 'l': // INDEX_REGS
1975 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1976 X86::ESI, X86::EDI, X86::EBP, 0);
1977 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
1978 case 'Q': // Q_REGS
1979 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
1980 case 'x': // SSE_REGS if SSE1 allowed
1981 if (Subtarget->hasSSE1())
1982 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1983 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1984 0);
1985 return std::vector<unsigned>();
1986 case 'Y': // SSE_REGS if SSE2 allowed
1987 if (Subtarget->hasSSE2())
1988 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1989 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1990 0);
1991 return std::vector<unsigned>();
1992 }
1993 }
1994
1995 // Handle explicit register names.
1996 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
1997}