blob: 1015f5e7af66b6ca6b00288ce367ace32d92a910 [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);
Evan Cheng02568ff2006-01-30 22:13:22 +000073 if (X86ScalarSSE)
74 // SSE has no i16 to fp conversion, only i32
75 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
76 else if (!X86PatIsel) {
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 Cheng02568ff2006-01-30 22:13:22 +0000104 if (X86ScalarSSE)
105 // 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
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
111
Evan Cheng02568ff2006-01-30 22:13:22 +0000112 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
113 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000114
Evan Cheng8e44f072006-01-27 21:26:54 +0000115 if (!X86PatIsel) {
Evan Cheng898101c2005-12-19 23:12:38 +0000116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
117 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000118 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
119 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000120 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
121 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000122 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
126 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
127 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
128 setOperationAction(ISD::FREM , MVT::f64 , Expand);
129 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
130 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
132 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
133 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
135 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
136 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
137 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000138 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000139
Evan Cheng8e44f072006-01-27 21:26:54 +0000140 if (X86PatIsel) {
Nate Begemand88fc032006-01-14 03:14:10 +0000141 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Chengeb422a72006-01-11 23:20:05 +0000142 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
143 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
144 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
145 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
146 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
147 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
148 }
Nate Begemand88fc032006-01-14 03:14:10 +0000149 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000150
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151 setOperationAction(ISD::READIO , MVT::i1 , Expand);
152 setOperationAction(ISD::READIO , MVT::i8 , Expand);
153 setOperationAction(ISD::READIO , MVT::i16 , Expand);
154 setOperationAction(ISD::READIO , MVT::i32 , Expand);
155 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
156 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
157 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
158 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
159
160 // These should be promoted to a larger select which is supported.
161 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
162 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng8e44f072006-01-27 21:26:54 +0000163 if (!X86PatIsel) {
Evan Chengd9558e02006-01-06 00:43:03 +0000164 // X86 wants to expand cmov itself.
Evan Cheng7df96d62005-12-17 01:21:05 +0000165 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
166 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Chengd9558e02006-01-06 00:43:03 +0000167 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
168 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengd5781fc2005-12-21 20:21:51 +0000169 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
170 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
171 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Chengd9558e02006-01-06 00:43:03 +0000172 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
173 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
174 // X86 ret instruction may pop stack.
175 setOperationAction(ISD::RET , MVT::Other, Custom);
176 // Darwin ABI issue.
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000177 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge3413162006-01-09 18:33:28 +0000178 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
179 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
180 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
181 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
182 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
183 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Cheng67f92a72006-01-11 22:15:48 +0000184 // X86 wants to expand memset / memcpy itself.
185 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
186 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng7df96d62005-12-17 01:21:05 +0000187 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
Chris Lattnerf73bae12005-11-29 06:16:21 +0000189 // We don't have line number support yet.
190 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000191 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
192 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000193
Nate Begemanacc398c2006-01-25 18:21:52 +0000194 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
195 setOperationAction(ISD::VASTART , MVT::Other, Custom);
196
197 // Use the default implementation.
198 setOperationAction(ISD::VAARG , MVT::Other, Expand);
199 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
200 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000201 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
202 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
203 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000204
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205 if (X86ScalarSSE) {
206 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209
210 // SSE has no load+extend ops
211 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
212 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
213
Evan Cheng223547a2006-01-31 22:28:30 +0000214 // Use ANDPD to simulate FABS.
215 setOperationAction(ISD::FABS , MVT::f64, Custom);
216 setOperationAction(ISD::FABS , MVT::f32, Custom);
217
218 // Use XORP to simulate FNEG.
219 setOperationAction(ISD::FNEG , MVT::f64, Custom);
220 setOperationAction(ISD::FNEG , MVT::f32, Custom);
221
Evan Chengd25e9e82006-02-02 00:28:23 +0000222 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::FSIN , MVT::f64, Expand);
224 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f64, Expand);
226 setOperationAction(ISD::FSIN , MVT::f32, Expand);
227 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228 setOperationAction(ISD::FREM , MVT::f32, Expand);
229
Chris Lattnera54aa942006-01-29 06:26:08 +0000230 // Expand FP immediates into loads from the stack, except for the special
231 // cases we handle.
232 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
233 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000234 addLegalFPImmediate(+0.0); // xorps / xorpd
235 } else {
236 // Set up the FP register classes.
237 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000238
239 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
240
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000241 if (!UnsafeFPMath) {
242 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
243 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
244 }
245
Chris Lattnera54aa942006-01-29 06:26:08 +0000246 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 addLegalFPImmediate(+0.0); // FLD0
248 addLegalFPImmediate(+1.0); // FLD1
249 addLegalFPImmediate(-0.0); // FLD0/FCHS
250 addLegalFPImmediate(-1.0); // FLD1/FCHS
251 }
252 computeRegisterProperties();
253
254 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
255 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
256 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
257 allowUnalignedMemoryAccesses = true; // x86 supports it!
258}
259
260std::vector<SDOperand>
261X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
262 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
263 return LowerFastCCArguments(F, DAG);
264 return LowerCCCArguments(F, DAG);
265}
266
267std::pair<SDOperand, SDOperand>
268X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
269 bool isVarArg, unsigned CallingConv,
270 bool isTailCall,
271 SDOperand Callee, ArgListTy &Args,
272 SelectionDAG &DAG) {
273 assert((!isVarArg || CallingConv == CallingConv::C) &&
274 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000275
276 // If the callee is a GlobalAddress node (quite common, every direct call is)
277 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
278 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
279 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000280 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
281 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000282
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000283 if (CallingConv == CallingConv::Fast && EnableFastCC)
284 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
285 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
286}
287
288//===----------------------------------------------------------------------===//
289// C Calling Convention implementation
290//===----------------------------------------------------------------------===//
291
292std::vector<SDOperand>
293X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
294 std::vector<SDOperand> ArgValues;
295
296 MachineFunction &MF = DAG.getMachineFunction();
297 MachineFrameInfo *MFI = MF.getFrameInfo();
298
299 // Add DAG nodes to load the arguments... On entry to a function on the X86,
300 // the stack frame looks like this:
301 //
302 // [ESP] -- return address
303 // [ESP + 4] -- first argument (leftmost lexically)
304 // [ESP + 8] -- second argument, if first argument is four bytes in size
305 // ...
306 //
307 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
308 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
309 MVT::ValueType ObjectVT = getValueType(I->getType());
310 unsigned ArgIncrement = 4;
311 unsigned ObjSize;
312 switch (ObjectVT) {
313 default: assert(0 && "Unhandled argument type!");
314 case MVT::i1:
315 case MVT::i8: ObjSize = 1; break;
316 case MVT::i16: ObjSize = 2; break;
317 case MVT::i32: ObjSize = 4; break;
318 case MVT::i64: ObjSize = ArgIncrement = 8; break;
319 case MVT::f32: ObjSize = 4; break;
320 case MVT::f64: ObjSize = ArgIncrement = 8; break;
321 }
322 // Create the frame index object for this incoming parameter...
323 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
324
325 // Create the SelectionDAG nodes corresponding to a load from this parameter
326 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
327
328 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
329 // dead loads.
330 SDOperand ArgValue;
331 if (!I->use_empty())
332 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
333 DAG.getSrcValue(NULL));
334 else {
335 if (MVT::isInteger(ObjectVT))
336 ArgValue = DAG.getConstant(0, ObjectVT);
337 else
338 ArgValue = DAG.getConstantFP(0, ObjectVT);
339 }
340 ArgValues.push_back(ArgValue);
341
342 ArgOffset += ArgIncrement; // Move on to the next argument...
343 }
344
345 // If the function takes variable number of arguments, make a frame index for
346 // the start of the first vararg value... for expansion of llvm.va_start.
347 if (F.isVarArg())
348 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
349 ReturnAddrIndex = 0; // No return address slot generated yet.
350 BytesToPopOnReturn = 0; // Callee pops nothing.
351 BytesCallerReserves = ArgOffset;
352
353 // Finally, inform the code generator which regs we return values in.
354 switch (getValueType(F.getReturnType())) {
355 default: assert(0 && "Unknown type!");
356 case MVT::isVoid: break;
357 case MVT::i1:
358 case MVT::i8:
359 case MVT::i16:
360 case MVT::i32:
361 MF.addLiveOut(X86::EAX);
362 break;
363 case MVT::i64:
364 MF.addLiveOut(X86::EAX);
365 MF.addLiveOut(X86::EDX);
366 break;
367 case MVT::f32:
368 case MVT::f64:
369 MF.addLiveOut(X86::ST0);
370 break;
371 }
372 return ArgValues;
373}
374
375std::pair<SDOperand, SDOperand>
376X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
377 bool isVarArg, bool isTailCall,
378 SDOperand Callee, ArgListTy &Args,
379 SelectionDAG &DAG) {
380 // Count how many bytes are to be pushed on the stack.
381 unsigned NumBytes = 0;
382
383 if (Args.empty()) {
384 // Save zero bytes.
385 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
386 DAG.getConstant(0, getPointerTy()));
387 } else {
388 for (unsigned i = 0, e = Args.size(); i != e; ++i)
389 switch (getValueType(Args[i].second)) {
390 default: assert(0 && "Unknown value type!");
391 case MVT::i1:
392 case MVT::i8:
393 case MVT::i16:
394 case MVT::i32:
395 case MVT::f32:
396 NumBytes += 4;
397 break;
398 case MVT::i64:
399 case MVT::f64:
400 NumBytes += 8;
401 break;
402 }
403
404 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
405 DAG.getConstant(NumBytes, getPointerTy()));
406
407 // Arguments go on the stack in reverse order, as specified by the ABI.
408 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000409 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000410 std::vector<SDOperand> Stores;
411
412 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
413 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
414 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
415
416 switch (getValueType(Args[i].second)) {
417 default: assert(0 && "Unexpected ValueType for argument!");
418 case MVT::i1:
419 case MVT::i8:
420 case MVT::i16:
421 // Promote the integer to 32 bits. If the input type is signed use a
422 // sign extend, otherwise use a zero extend.
423 if (Args[i].second->isSigned())
424 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
425 else
426 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
427
428 // FALL THROUGH
429 case MVT::i32:
430 case MVT::f32:
431 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
432 Args[i].first, PtrOff,
433 DAG.getSrcValue(NULL)));
434 ArgOffset += 4;
435 break;
436 case MVT::i64:
437 case MVT::f64:
438 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
439 Args[i].first, PtrOff,
440 DAG.getSrcValue(NULL)));
441 ArgOffset += 8;
442 break;
443 }
444 }
445 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
446 }
447
448 std::vector<MVT::ValueType> RetVals;
449 MVT::ValueType RetTyVT = getValueType(RetTy);
450 RetVals.push_back(MVT::Other);
451
452 // The result values produced have to be legal. Promote the result.
453 switch (RetTyVT) {
454 case MVT::isVoid: break;
455 default:
456 RetVals.push_back(RetTyVT);
457 break;
458 case MVT::i1:
459 case MVT::i8:
460 case MVT::i16:
461 RetVals.push_back(MVT::i32);
462 break;
463 case MVT::f32:
464 if (X86ScalarSSE)
465 RetVals.push_back(MVT::f32);
466 else
467 RetVals.push_back(MVT::f64);
468 break;
469 case MVT::i64:
470 RetVals.push_back(MVT::i32);
471 RetVals.push_back(MVT::i32);
472 break;
473 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000474
Evan Cheng8e44f072006-01-27 21:26:54 +0000475 if (!X86PatIsel) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000476 std::vector<MVT::ValueType> NodeTys;
477 NodeTys.push_back(MVT::Other); // Returns a chain
478 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd90eb7f2006-01-05 00:27:02 +0000479 std::vector<SDOperand> Ops;
480 Ops.push_back(Chain);
481 Ops.push_back(Callee);
482
Evan Chengd9558e02006-01-06 00:43:03 +0000483 // FIXME: Do not generate X86ISD::TAILCALL for now.
484 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000485 SDOperand InFlag = Chain.getValue(1);
486
Chris Lattneraf63bb02006-01-24 05:17:12 +0000487 NodeTys.clear();
488 NodeTys.push_back(MVT::Other); // Returns a chain
489 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
490 Ops.clear();
491 Ops.push_back(Chain);
492 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
493 Ops.push_back(DAG.getConstant(0, getPointerTy()));
494 Ops.push_back(InFlag);
495 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
496 InFlag = Chain.getValue(1);
497
Evan Chengd90eb7f2006-01-05 00:27:02 +0000498 SDOperand RetVal;
499 if (RetTyVT != MVT::isVoid) {
500 switch (RetTyVT) {
501 default: assert(0 && "Unknown value type to return!");
502 case MVT::i1:
503 case MVT::i8:
504 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
505 Chain = RetVal.getValue(1);
Evan Cheng68e5d082006-01-18 08:08:38 +0000506 if (RetTyVT == MVT::i1)
507 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000508 break;
509 case MVT::i16:
510 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
511 Chain = RetVal.getValue(1);
512 break;
513 case MVT::i32:
514 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
515 Chain = RetVal.getValue(1);
516 break;
517 case MVT::i64: {
518 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
519 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
520 Lo.getValue(2));
521 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
522 Chain = Hi.getValue(1);
523 break;
524 }
Evan Cheng357c58e2006-01-17 21:58:21 +0000525 case MVT::f32:
Evan Chengd90eb7f2006-01-05 00:27:02 +0000526 case MVT::f64: {
527 std::vector<MVT::ValueType> Tys;
528 Tys.push_back(MVT::f64);
529 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000530 Tys.push_back(MVT::Flag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000531 std::vector<SDOperand> Ops;
532 Ops.push_back(Chain);
533 Ops.push_back(InFlag);
534 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000535 Chain = RetVal.getValue(1);
536 InFlag = RetVal.getValue(2);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000537 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +0000538 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
539 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng2059f882006-01-17 00:37:42 +0000540 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Chengd90eb7f2006-01-05 00:27:02 +0000541 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6dab0532006-01-30 08:02:57 +0000542 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000543 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
544 Tys.clear();
545 Tys.push_back(MVT::Other);
546 Ops.clear();
547 Ops.push_back(Chain);
548 Ops.push_back(RetVal);
549 Ops.push_back(StackSlot);
550 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000551 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000552 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
553 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
554 DAG.getSrcValue(NULL));
555 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000556 }
Evan Cheng357c58e2006-01-17 21:58:21 +0000557
558 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
559 // FIXME: we would really like to remember that this FP_ROUND
560 // operation is okay to eliminate if we allow excess FP precision.
561 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000562 break;
563 }
564 }
565 }
566
Evan Chengd90eb7f2006-01-05 00:27:02 +0000567 return std::make_pair(RetVal, Chain);
568 } else {
569 std::vector<SDOperand> Ops;
570 Ops.push_back(Chain);
571 Ops.push_back(Callee);
572 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
573 Ops.push_back(DAG.getConstant(0, getPointerTy()));
574
Chris Lattner259e97c2006-01-31 19:43:35 +0000575 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
Evan Chengd90eb7f2006-01-05 00:27:02 +0000576 RetVals, Ops);
577
578 SDOperand ResultVal;
579 switch (RetTyVT) {
580 case MVT::isVoid: break;
581 default:
582 ResultVal = TheCall.getValue(1);
583 break;
584 case MVT::i1:
585 case MVT::i8:
586 case MVT::i16:
587 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
588 break;
589 case MVT::f32:
590 // FIXME: we would really like to remember that this FP_ROUND operation is
591 // okay to eliminate if we allow excess FP precision.
592 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
593 break;
594 case MVT::i64:
595 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
596 TheCall.getValue(2));
597 break;
598 }
599
600 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
601 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000602 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000603}
604
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000605//===----------------------------------------------------------------------===//
606// Fast Calling Convention implementation
607//===----------------------------------------------------------------------===//
608//
609// The X86 'fast' calling convention passes up to two integer arguments in
610// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
611// and requires that the callee pop its arguments off the stack (allowing proper
612// tail calls), and has the same return value conventions as C calling convs.
613//
614// This calling convention always arranges for the callee pop value to be 8n+4
615// bytes, which is needed for tail recursion elimination and stack alignment
616// reasons.
617//
618// Note that this can be enhanced in the future to pass fp vals in registers
619// (when we have a global fp allocator) and do other tricks.
620//
621
622/// AddLiveIn - This helper function adds the specified physical register to the
623/// MachineFunction as a live in value. It also creates a corresponding virtual
624/// register for it.
625static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
626 TargetRegisterClass *RC) {
627 assert(RC->contains(PReg) && "Not the correct regclass!");
628 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
629 MF.addLiveIn(PReg, VReg);
630 return VReg;
631}
632
633
634std::vector<SDOperand>
635X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
636 std::vector<SDOperand> ArgValues;
637
638 MachineFunction &MF = DAG.getMachineFunction();
639 MachineFrameInfo *MFI = MF.getFrameInfo();
640
641 // Add DAG nodes to load the arguments... On entry to a function the stack
642 // frame looks like this:
643 //
644 // [ESP] -- return address
645 // [ESP + 4] -- first nonreg argument (leftmost lexically)
646 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
647 // ...
648 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
649
650 // Keep track of the number of integer regs passed so far. This can be either
651 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
652 // used).
653 unsigned NumIntRegs = 0;
654
655 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
656 MVT::ValueType ObjectVT = getValueType(I->getType());
657 unsigned ArgIncrement = 4;
658 unsigned ObjSize = 0;
659 SDOperand ArgValue;
660
661 switch (ObjectVT) {
662 default: assert(0 && "Unhandled argument type!");
663 case MVT::i1:
664 case MVT::i8:
665 if (NumIntRegs < 2) {
666 if (!I->use_empty()) {
667 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
668 X86::R8RegisterClass);
669 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
670 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000671 if (ObjectVT == MVT::i1)
672 // FIXME: Should insert a assertzext here.
673 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000674 }
675 ++NumIntRegs;
676 break;
677 }
678
679 ObjSize = 1;
680 break;
681 case MVT::i16:
682 if (NumIntRegs < 2) {
683 if (!I->use_empty()) {
684 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
685 X86::R16RegisterClass);
686 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
687 DAG.setRoot(ArgValue.getValue(1));
688 }
689 ++NumIntRegs;
690 break;
691 }
692 ObjSize = 2;
693 break;
694 case MVT::i32:
695 if (NumIntRegs < 2) {
696 if (!I->use_empty()) {
697 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
698 X86::R32RegisterClass);
699 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
700 DAG.setRoot(ArgValue.getValue(1));
701 }
702 ++NumIntRegs;
703 break;
704 }
705 ObjSize = 4;
706 break;
707 case MVT::i64:
708 if (NumIntRegs == 0) {
709 if (!I->use_empty()) {
710 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
711 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
712
713 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
714 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
715 DAG.setRoot(Hi.getValue(1));
716
717 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
718 }
719 NumIntRegs = 2;
720 break;
721 } else if (NumIntRegs == 1) {
722 if (!I->use_empty()) {
723 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
724 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
725 DAG.setRoot(Low.getValue(1));
726
727 // Load the high part from memory.
728 // Create the frame index object for this incoming parameter...
729 int FI = MFI->CreateFixedObject(4, ArgOffset);
730 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
731 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
732 DAG.getSrcValue(NULL));
733 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
734 }
735 ArgOffset += 4;
736 NumIntRegs = 2;
737 break;
738 }
739 ObjSize = ArgIncrement = 8;
740 break;
741 case MVT::f32: ObjSize = 4; break;
742 case MVT::f64: ObjSize = ArgIncrement = 8; break;
743 }
744
745 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
746 // dead loads.
747 if (ObjSize && !I->use_empty()) {
748 // Create the frame index object for this incoming parameter...
749 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
750
751 // Create the SelectionDAG nodes corresponding to a load from this
752 // parameter.
753 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
754
755 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
756 DAG.getSrcValue(NULL));
757 } else if (ArgValue.Val == 0) {
758 if (MVT::isInteger(ObjectVT))
759 ArgValue = DAG.getConstant(0, ObjectVT);
760 else
761 ArgValue = DAG.getConstantFP(0, ObjectVT);
762 }
763 ArgValues.push_back(ArgValue);
764
765 if (ObjSize)
766 ArgOffset += ArgIncrement; // Move on to the next argument.
767 }
768
769 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
770 // arguments and the arguments after the retaddr has been pushed are aligned.
771 if ((ArgOffset & 7) == 0)
772 ArgOffset += 4;
773
774 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
775 ReturnAddrIndex = 0; // No return address slot generated yet.
776 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
777 BytesCallerReserves = 0;
778
779 // Finally, inform the code generator which regs we return values in.
780 switch (getValueType(F.getReturnType())) {
781 default: assert(0 && "Unknown type!");
782 case MVT::isVoid: break;
783 case MVT::i1:
784 case MVT::i8:
785 case MVT::i16:
786 case MVT::i32:
787 MF.addLiveOut(X86::EAX);
788 break;
789 case MVT::i64:
790 MF.addLiveOut(X86::EAX);
791 MF.addLiveOut(X86::EDX);
792 break;
793 case MVT::f32:
794 case MVT::f64:
795 MF.addLiveOut(X86::ST0);
796 break;
797 }
798 return ArgValues;
799}
800
801std::pair<SDOperand, SDOperand>
802X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
803 bool isTailCall, SDOperand Callee,
804 ArgListTy &Args, SelectionDAG &DAG) {
805 // Count how many bytes are to be pushed on the stack.
806 unsigned NumBytes = 0;
807
808 // Keep track of the number of integer regs passed so far. This can be either
809 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
810 // used).
811 unsigned NumIntRegs = 0;
812
813 for (unsigned i = 0, e = Args.size(); i != e; ++i)
814 switch (getValueType(Args[i].second)) {
815 default: assert(0 && "Unknown value type!");
816 case MVT::i1:
817 case MVT::i8:
818 case MVT::i16:
819 case MVT::i32:
820 if (NumIntRegs < 2) {
821 ++NumIntRegs;
822 break;
823 }
824 // fall through
825 case MVT::f32:
826 NumBytes += 4;
827 break;
828 case MVT::i64:
829 if (NumIntRegs == 0) {
830 NumIntRegs = 2;
831 break;
832 } else if (NumIntRegs == 1) {
833 NumIntRegs = 2;
834 NumBytes += 4;
835 break;
836 }
837
838 // fall through
839 case MVT::f64:
840 NumBytes += 8;
841 break;
842 }
843
844 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
845 // arguments and the arguments after the retaddr has been pushed are aligned.
846 if ((NumBytes & 7) == 0)
847 NumBytes += 4;
848
849 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
850 DAG.getConstant(NumBytes, getPointerTy()));
851
852 // Arguments go on the stack in reverse order, as specified by the ABI.
853 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000854 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000855 NumIntRegs = 0;
856 std::vector<SDOperand> Stores;
857 std::vector<SDOperand> RegValuesToPass;
858 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
859 switch (getValueType(Args[i].second)) {
860 default: assert(0 && "Unexpected ValueType for argument!");
861 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000862 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
863 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000864 case MVT::i8:
865 case MVT::i16:
866 case MVT::i32:
867 if (NumIntRegs < 2) {
868 RegValuesToPass.push_back(Args[i].first);
869 ++NumIntRegs;
870 break;
871 }
872 // Fall through
873 case MVT::f32: {
874 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
875 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
876 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
877 Args[i].first, PtrOff,
878 DAG.getSrcValue(NULL)));
879 ArgOffset += 4;
880 break;
881 }
882 case MVT::i64:
883 if (NumIntRegs < 2) { // Can pass part of it in regs?
884 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
885 Args[i].first, DAG.getConstant(1, MVT::i32));
886 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
887 Args[i].first, DAG.getConstant(0, MVT::i32));
888 RegValuesToPass.push_back(Lo);
889 ++NumIntRegs;
890 if (NumIntRegs < 2) { // Pass both parts in regs?
891 RegValuesToPass.push_back(Hi);
892 ++NumIntRegs;
893 } else {
894 // Pass the high part in memory.
895 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
896 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
897 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
898 Hi, PtrOff, DAG.getSrcValue(NULL)));
899 ArgOffset += 4;
900 }
901 break;
902 }
903 // Fall through
904 case MVT::f64:
905 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
906 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
907 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
908 Args[i].first, PtrOff,
909 DAG.getSrcValue(NULL)));
910 ArgOffset += 8;
911 break;
912 }
913 }
914 if (!Stores.empty())
915 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
916
917 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
918 // arguments and the arguments after the retaddr has been pushed are aligned.
919 if ((ArgOffset & 7) == 0)
920 ArgOffset += 4;
921
922 std::vector<MVT::ValueType> RetVals;
923 MVT::ValueType RetTyVT = getValueType(RetTy);
924
925 RetVals.push_back(MVT::Other);
926
927 // The result values produced have to be legal. Promote the result.
928 switch (RetTyVT) {
929 case MVT::isVoid: break;
930 default:
931 RetVals.push_back(RetTyVT);
932 break;
933 case MVT::i1:
934 case MVT::i8:
935 case MVT::i16:
936 RetVals.push_back(MVT::i32);
937 break;
938 case MVT::f32:
939 if (X86ScalarSSE)
940 RetVals.push_back(MVT::f32);
941 else
942 RetVals.push_back(MVT::f64);
943 break;
944 case MVT::i64:
945 RetVals.push_back(MVT::i32);
946 RetVals.push_back(MVT::i32);
947 break;
948 }
949
Evan Cheng8e44f072006-01-27 21:26:54 +0000950 if (!X86PatIsel) {
Evan Chengd9558e02006-01-06 00:43:03 +0000951 // Build a sequence of copy-to-reg nodes chained together with token chain
952 // and flag operands which copy the outgoing args into registers.
953 SDOperand InFlag;
954 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
955 unsigned CCReg;
956 SDOperand RegToPass = RegValuesToPass[i];
957 switch (RegToPass.getValueType()) {
958 default: assert(0 && "Bad thing to pass in regs");
959 case MVT::i8:
960 CCReg = (i == 0) ? X86::AL : X86::DL;
961 break;
962 case MVT::i16:
963 CCReg = (i == 0) ? X86::AX : X86::DX;
964 break;
965 case MVT::i32:
966 CCReg = (i == 0) ? X86::EAX : X86::EDX;
967 break;
968 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000969
Evan Chengd9558e02006-01-06 00:43:03 +0000970 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
971 InFlag = Chain.getValue(1);
972 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000973
Evan Chengd9558e02006-01-06 00:43:03 +0000974 std::vector<MVT::ValueType> NodeTys;
975 NodeTys.push_back(MVT::Other); // Returns a chain
976 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd9558e02006-01-06 00:43:03 +0000977 std::vector<SDOperand> Ops;
978 Ops.push_back(Chain);
979 Ops.push_back(Callee);
980 if (InFlag.Val)
981 Ops.push_back(InFlag);
982
983 // FIXME: Do not generate X86ISD::TAILCALL for now.
984 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
985 InFlag = Chain.getValue(1);
986
Chris Lattneraf63bb02006-01-24 05:17:12 +0000987 NodeTys.clear();
988 NodeTys.push_back(MVT::Other); // Returns a chain
989 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
990 Ops.clear();
991 Ops.push_back(Chain);
992 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
993 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
994 Ops.push_back(InFlag);
995 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
996 InFlag = Chain.getValue(1);
997
Evan Chengd9558e02006-01-06 00:43:03 +0000998 SDOperand RetVal;
999 if (RetTyVT != MVT::isVoid) {
1000 switch (RetTyVT) {
1001 default: assert(0 && "Unknown value type to return!");
1002 case MVT::i1:
1003 case MVT::i8:
1004 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1005 Chain = RetVal.getValue(1);
Evan Cheng68e5d082006-01-18 08:08:38 +00001006 if (RetTyVT == MVT::i1)
1007 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Chengd9558e02006-01-06 00:43:03 +00001008 break;
1009 case MVT::i16:
1010 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1011 Chain = RetVal.getValue(1);
1012 break;
1013 case MVT::i32:
1014 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1015 Chain = RetVal.getValue(1);
1016 break;
1017 case MVT::i64: {
1018 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1019 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1020 Lo.getValue(2));
1021 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1022 Chain = Hi.getValue(1);
1023 break;
1024 }
Evan Cheng357c58e2006-01-17 21:58:21 +00001025 case MVT::f32:
Evan Chengd9558e02006-01-06 00:43:03 +00001026 case MVT::f64: {
1027 std::vector<MVT::ValueType> Tys;
1028 Tys.push_back(MVT::f64);
1029 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001030 Tys.push_back(MVT::Flag);
Evan Chengd9558e02006-01-06 00:43:03 +00001031 std::vector<SDOperand> Ops;
1032 Ops.push_back(Chain);
1033 Ops.push_back(InFlag);
1034 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001035 Chain = RetVal.getValue(1);
1036 InFlag = RetVal.getValue(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001037 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001038 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1039 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng2059f882006-01-17 00:37:42 +00001040 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Chengd9558e02006-01-06 00:43:03 +00001041 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6dab0532006-01-30 08:02:57 +00001042 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Chengd9558e02006-01-06 00:43:03 +00001043 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1044 Tys.clear();
1045 Tys.push_back(MVT::Other);
1046 Ops.clear();
1047 Ops.push_back(Chain);
1048 Ops.push_back(RetVal);
1049 Ops.push_back(StackSlot);
1050 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001051 Ops.push_back(InFlag);
Evan Chengd9558e02006-01-06 00:43:03 +00001052 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1053 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1054 DAG.getSrcValue(NULL));
1055 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001056 }
Evan Cheng357c58e2006-01-17 21:58:21 +00001057
1058 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1059 // FIXME: we would really like to remember that this FP_ROUND
1060 // operation is okay to eliminate if we allow excess FP precision.
1061 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Chengd9558e02006-01-06 00:43:03 +00001062 break;
1063 }
1064 }
1065 }
1066
Evan Chengd9558e02006-01-06 00:43:03 +00001067 return std::make_pair(RetVal, Chain);
1068 } else {
1069 std::vector<SDOperand> Ops;
1070 Ops.push_back(Chain);
1071 Ops.push_back(Callee);
1072 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1073 // Callee pops all arg values on the stack.
1074 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1075
1076 // Pass register arguments as needed.
1077 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1078
Chris Lattner259e97c2006-01-31 19:43:35 +00001079 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
Evan Chengd9558e02006-01-06 00:43:03 +00001080 RetVals, Ops);
1081 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1082
1083 SDOperand ResultVal;
1084 switch (RetTyVT) {
1085 case MVT::isVoid: break;
1086 default:
1087 ResultVal = TheCall.getValue(1);
1088 break;
1089 case MVT::i1:
1090 case MVT::i8:
1091 case MVT::i16:
1092 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1093 break;
1094 case MVT::f32:
1095 // FIXME: we would really like to remember that this FP_ROUND operation is
1096 // okay to eliminate if we allow excess FP precision.
1097 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1098 break;
1099 case MVT::i64:
1100 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1101 TheCall.getValue(2));
1102 break;
1103 }
1104
1105 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001106 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001107}
1108
1109SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1110 if (ReturnAddrIndex == 0) {
1111 // Set up a frame object for the return address.
1112 MachineFunction &MF = DAG.getMachineFunction();
1113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1114 }
1115
1116 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1117}
1118
1119
1120
1121std::pair<SDOperand, SDOperand> X86TargetLowering::
1122LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1123 SelectionDAG &DAG) {
1124 SDOperand Result;
1125 if (Depth) // Depths > 0 not supported yet!
1126 Result = DAG.getConstant(0, getPointerTy());
1127 else {
1128 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1129 if (!isFrameAddress)
1130 // Just load the return address
1131 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1132 DAG.getSrcValue(NULL));
1133 else
1134 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1135 DAG.getConstant(4, MVT::i32));
1136 }
1137 return std::make_pair(Result, Chain);
1138}
1139
Evan Cheng4a460802006-01-11 00:33:36 +00001140/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1141/// which corresponds to the condition code.
1142static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1143 switch (X86CC) {
1144 default: assert(0 && "Unknown X86 conditional code!");
1145 case X86ISD::COND_A: return X86::JA;
1146 case X86ISD::COND_AE: return X86::JAE;
1147 case X86ISD::COND_B: return X86::JB;
1148 case X86ISD::COND_BE: return X86::JBE;
1149 case X86ISD::COND_E: return X86::JE;
1150 case X86ISD::COND_G: return X86::JG;
1151 case X86ISD::COND_GE: return X86::JGE;
1152 case X86ISD::COND_L: return X86::JL;
1153 case X86ISD::COND_LE: return X86::JLE;
1154 case X86ISD::COND_NE: return X86::JNE;
1155 case X86ISD::COND_NO: return X86::JNO;
1156 case X86ISD::COND_NP: return X86::JNP;
1157 case X86ISD::COND_NS: return X86::JNS;
1158 case X86ISD::COND_O: return X86::JO;
1159 case X86ISD::COND_P: return X86::JP;
1160 case X86ISD::COND_S: return X86::JS;
1161 }
1162}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001163
Evan Cheng6dfa9992006-01-30 23:41:35 +00001164/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1165/// specific condition code. It returns a false if it cannot do a direct
1166/// translation. X86CC is the translated CondCode. Flip is set to true if the
1167/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001168static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1169 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001170 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001171 Flip = false;
1172 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001173 if (!isFP) {
1174 switch (SetCCOpcode) {
1175 default: break;
1176 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1177 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1178 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1179 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1180 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1181 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1182 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1183 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1184 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1185 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1186 }
1187 } else {
1188 // On a floating point condition, the flags are set as follows:
1189 // ZF PF CF op
1190 // 0 | 0 | 0 | X > Y
1191 // 0 | 0 | 1 | X < Y
1192 // 1 | 0 | 0 | X == Y
1193 // 1 | 1 | 1 | unordered
1194 switch (SetCCOpcode) {
1195 default: break;
1196 case ISD::SETUEQ:
1197 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001198 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001199 case ISD::SETOGT:
1200 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001201 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001202 case ISD::SETOGE:
1203 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001204 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001205 case ISD::SETULT:
1206 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001207 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001208 case ISD::SETULE:
1209 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1210 case ISD::SETONE:
1211 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1212 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1213 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1214 }
1215 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001216
1217 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001218}
1219
Evan Cheng4a460802006-01-11 00:33:36 +00001220/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1221/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001222/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001223static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001224 switch (X86CC) {
1225 default:
1226 return false;
1227 case X86ISD::COND_B:
1228 case X86ISD::COND_BE:
1229 case X86ISD::COND_E:
1230 case X86ISD::COND_P:
1231 case X86ISD::COND_A:
1232 case X86ISD::COND_AE:
1233 case X86ISD::COND_NE:
1234 case X86ISD::COND_NP:
1235 return true;
1236 }
1237}
1238
Evan Cheng4a460802006-01-11 00:33:36 +00001239MachineBasicBlock *
1240X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1241 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001242 switch (MI->getOpcode()) {
1243 default: assert(false && "Unexpected instr type to insert");
1244 case X86::CMOV_FR32:
1245 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001246 // To "insert" a SELECT_CC instruction, we actually have to insert the
1247 // diamond control-flow pattern. The incoming instruction knows the
1248 // destination vreg to set, the condition code register to branch on, the
1249 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 ilist<MachineBasicBlock>::iterator It = BB;
1252 ++It;
1253
1254 // thisMBB:
1255 // ...
1256 // TrueVal = ...
1257 // cmpTY ccX, r1, r2
1258 // bCC copy1MBB
1259 // fallthrough --> copy0MBB
1260 MachineBasicBlock *thisMBB = BB;
1261 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1262 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1263 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1264 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1265 MachineFunction *F = BB->getParent();
1266 F->getBasicBlockList().insert(It, copy0MBB);
1267 F->getBasicBlockList().insert(It, sinkMBB);
1268 // Update machine-CFG edges
1269 BB->addSuccessor(copy0MBB);
1270 BB->addSuccessor(sinkMBB);
1271
1272 // copy0MBB:
1273 // %FalseValue = ...
1274 // # fallthrough to sinkMBB
1275 BB = copy0MBB;
1276
1277 // Update machine-CFG edges
1278 BB->addSuccessor(sinkMBB);
1279
1280 // sinkMBB:
1281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1282 // ...
1283 BB = sinkMBB;
1284 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001287
Evan Cheng0cc39452006-01-16 21:21:29 +00001288 delete MI; // The pseudo instruction is gone now.
1289 return BB;
1290 }
Evan Cheng4a460802006-01-11 00:33:36 +00001291
Evan Cheng0cc39452006-01-16 21:21:29 +00001292 case X86::FP_TO_INT16_IN_MEM:
1293 case X86::FP_TO_INT32_IN_MEM:
1294 case X86::FP_TO_INT64_IN_MEM: {
1295 // Change the floating point control register to use "round towards zero"
1296 // mode when truncating to an integer value.
1297 MachineFunction *F = BB->getParent();
1298 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1299 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1300
1301 // Load the old value of the high byte of the control word...
1302 unsigned OldCW =
1303 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1304 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1305
1306 // Set the high part to be round to zero...
1307 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1308
1309 // Reload the modified control word now...
1310 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1311
1312 // Restore the memory image of control word to original value
1313 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1314
1315 // Get the X86 opcode to use.
1316 unsigned Opc;
1317 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001318 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001319 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1320 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1321 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1322 }
1323
1324 X86AddressMode AM;
1325 MachineOperand &Op = MI->getOperand(0);
1326 if (Op.isRegister()) {
1327 AM.BaseType = X86AddressMode::RegBase;
1328 AM.Base.Reg = Op.getReg();
1329 } else {
1330 AM.BaseType = X86AddressMode::FrameIndexBase;
1331 AM.Base.FrameIndex = Op.getFrameIndex();
1332 }
1333 Op = MI->getOperand(1);
1334 if (Op.isImmediate())
1335 AM.Scale = Op.getImmedValue();
1336 Op = MI->getOperand(2);
1337 if (Op.isImmediate())
1338 AM.IndexReg = Op.getImmedValue();
1339 Op = MI->getOperand(3);
1340 if (Op.isGlobalAddress()) {
1341 AM.GV = Op.getGlobal();
1342 } else {
1343 AM.Disp = Op.getImmedValue();
1344 }
1345 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1346
1347 // Reload the original control word now.
1348 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1349
1350 delete MI; // The pseudo instruction is gone now.
1351 return BB;
1352 }
1353 }
Evan Cheng4a460802006-01-11 00:33:36 +00001354}
1355
1356
1357//===----------------------------------------------------------------------===//
1358// X86 Custom Lowering Hooks
1359//===----------------------------------------------------------------------===//
1360
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001361/// LowerOperation - Provide custom lowering hooks for some operations.
1362///
1363SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1364 switch (Op.getOpcode()) {
1365 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001366 case ISD::ADD_PARTS:
1367 case ISD::SUB_PARTS: {
1368 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1369 "Not an i64 add/sub!");
1370 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1371 std::vector<MVT::ValueType> Tys;
1372 Tys.push_back(MVT::i32);
1373 Tys.push_back(MVT::Flag);
1374 std::vector<SDOperand> Ops;
1375 Ops.push_back(Op.getOperand(0));
1376 Ops.push_back(Op.getOperand(2));
1377 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1378 Tys, Ops);
1379 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1380 Op.getOperand(1), Op.getOperand(3),
1381 Lo.getValue(1));
1382 Tys.clear();
1383 Tys.push_back(MVT::i32);
1384 Tys.push_back(MVT::i32);
1385 Ops.clear();
1386 Ops.push_back(Lo);
1387 Ops.push_back(Hi);
1388 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1389 }
1390 case ISD::SHL_PARTS:
1391 case ISD::SRA_PARTS:
1392 case ISD::SRL_PARTS: {
1393 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1394 "Not an i64 shift!");
1395 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1396 SDOperand ShOpLo = Op.getOperand(0);
1397 SDOperand ShOpHi = Op.getOperand(1);
1398 SDOperand ShAmt = Op.getOperand(2);
1399 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001400 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001401 : DAG.getConstant(0, MVT::i32);
1402
1403 SDOperand Tmp2, Tmp3;
1404 if (Op.getOpcode() == ISD::SHL_PARTS) {
1405 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1406 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1407 } else {
1408 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001409 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001410 }
1411
1412 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1413 ShAmt, DAG.getConstant(32, MVT::i8));
1414
1415 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001416 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001417
1418 std::vector<MVT::ValueType> Tys;
1419 Tys.push_back(MVT::i32);
1420 Tys.push_back(MVT::Flag);
1421 std::vector<SDOperand> Ops;
1422 if (Op.getOpcode() == ISD::SHL_PARTS) {
1423 Ops.push_back(Tmp2);
1424 Ops.push_back(Tmp3);
1425 Ops.push_back(CC);
1426 Ops.push_back(InFlag);
1427 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1428 InFlag = Hi.getValue(1);
1429
1430 Ops.clear();
1431 Ops.push_back(Tmp3);
1432 Ops.push_back(Tmp1);
1433 Ops.push_back(CC);
1434 Ops.push_back(InFlag);
1435 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1436 } else {
1437 Ops.push_back(Tmp2);
1438 Ops.push_back(Tmp3);
1439 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001440 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001441 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1442 InFlag = Lo.getValue(1);
1443
1444 Ops.clear();
1445 Ops.push_back(Tmp3);
1446 Ops.push_back(Tmp1);
1447 Ops.push_back(CC);
1448 Ops.push_back(InFlag);
1449 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1450 }
1451
1452 Tys.clear();
1453 Tys.push_back(MVT::i32);
1454 Tys.push_back(MVT::i32);
1455 Ops.clear();
1456 Ops.push_back(Lo);
1457 Ops.push_back(Hi);
1458 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001460 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001461 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001462 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001463 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001464
1465 SDOperand Result;
1466 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1467 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001468 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001469 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001470 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001471 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1472 DAG.getEntryNode(), Op.getOperand(0),
1473 StackSlot, DAG.getSrcValue(NULL));
1474
1475 // Build the FILD
1476 std::vector<MVT::ValueType> Tys;
1477 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001478 Tys.push_back(MVT::Other);
Evan Chenga3195e82006-01-12 22:54:21 +00001479 Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001480 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001481 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001482 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001483 Ops.push_back(DAG.getValueType(SrcVT));
1484 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001485
1486 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001487 Chain = Result.getValue(1);
1488 SDOperand InFlag = Result.getValue(2);
1489
1490 // FIXME: Currently the FST is flagged to the FILD. This
1491 // shouldn't be necessary except that RFP cannot be live across
1492 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1493 MachineFunction &MF = DAG.getMachineFunction();
1494 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1495 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1496 std::vector<MVT::ValueType> Tys;
1497 Tys.push_back(MVT::Other);
1498 std::vector<SDOperand> Ops;
1499 Ops.push_back(Chain);
1500 Ops.push_back(Result);
1501 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001502 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001503 Ops.push_back(InFlag);
1504 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1505 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1506 DAG.getSrcValue(NULL));
1507 }
1508
Evan Chenga3195e82006-01-12 22:54:21 +00001509 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001510 }
1511 case ISD::FP_TO_SINT: {
1512 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001513 "Unknown FP_TO_SINT to lower!");
1514 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1515 // stack slot.
1516 MachineFunction &MF = DAG.getMachineFunction();
1517 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1518 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1519 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1520
1521 unsigned Opc;
1522 switch (Op.getValueType()) {
1523 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1524 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1525 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1526 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1527 }
1528
Evan Cheng6dab0532006-01-30 08:02:57 +00001529 SDOperand Chain = DAG.getEntryNode();
1530 SDOperand Value = Op.getOperand(0);
1531 if (X86ScalarSSE) {
1532 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1533 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1534 DAG.getSrcValue(0));
1535 std::vector<MVT::ValueType> Tys;
1536 Tys.push_back(MVT::f64);
1537 Tys.push_back(MVT::Other);
1538 std::vector<SDOperand> Ops;
1539 Ops.push_back(Chain);
1540 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001541 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001542 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1543 Chain = Value.getValue(1);
1544 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1545 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1546 }
1547
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001548 // Build the FP_TO_INT*_IN_MEM
1549 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001550 Ops.push_back(Chain);
1551 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001552 Ops.push_back(StackSlot);
1553 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1554
1555 // Load the result.
1556 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1557 DAG.getSrcValue(NULL));
1558 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001559 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001560 std::vector<MVT::ValueType> Tys;
1561 Tys.push_back(MVT::Other);
1562 Tys.push_back(MVT::Flag);
1563 std::vector<SDOperand> Ops;
1564 Ops.push_back(Op.getOperand(0));
1565 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001566 Ops.clear();
1567 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1568 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1569 MVT::i32, Ops[0].getValue(2)));
1570 Ops.push_back(Ops[1].getValue(1));
1571 Tys[0] = Tys[1] = MVT::i32;
1572 Tys.push_back(MVT::Other);
1573 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001574 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001575 case ISD::FABS: {
1576 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001577 const Type *OpNTy = MVT::getTypeForValueType(VT);
1578 std::vector<Constant*> CV;
1579 if (VT == MVT::f64) {
1580 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1581 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1582 } else {
1583 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1584 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1585 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1586 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 }
1588 Constant *CS = ConstantStruct::get(CV);
1589 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1590 SDOperand Mask
1591 = DAG.getNode(X86ISD::LOAD_PACK,
1592 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001593 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1594 }
Evan Cheng223547a2006-01-31 22:28:30 +00001595 case ISD::FNEG: {
1596 MVT::ValueType VT = Op.getValueType();
1597 const Type *OpNTy = MVT::getTypeForValueType(VT);
1598 std::vector<Constant*> CV;
1599 if (VT == MVT::f64) {
1600 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1601 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1602 } else {
1603 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1604 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1605 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1606 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1607 }
1608 Constant *CS = ConstantStruct::get(CV);
1609 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1610 SDOperand Mask
1611 = DAG.getNode(X86ISD::LOAD_PACK,
1612 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1613 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1614 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001615 case ISD::SETCC: {
1616 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001617 SDOperand Cond;
1618 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001619 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1620 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001621 bool Flip;
1622 unsigned X86CC;
1623 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1624 if (Flip)
1625 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1626 Op.getOperand(1), Op.getOperand(0));
1627 else
1628 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1629 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001630 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1631 DAG.getConstant(X86CC, MVT::i8), Cond);
1632 } else {
1633 assert(isFP && "Illegal integer SetCC!");
1634
Evan Cheng6dfa9992006-01-30 23:41:35 +00001635 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1636 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001637 std::vector<MVT::ValueType> Tys;
1638 std::vector<SDOperand> Ops;
1639 switch (SetCCOpcode) {
1640 default: assert(false && "Illegal floating point SetCC!");
1641 case ISD::SETOEQ: { // !PF & ZF
1642 Tys.push_back(MVT::i8);
1643 Tys.push_back(MVT::Flag);
1644 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1645 Ops.push_back(Cond);
1646 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1647 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1648 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1649 Tmp1.getValue(1));
1650 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1651 }
Evan Chengd9558e02006-01-06 00:43:03 +00001652 case ISD::SETUNE: { // PF | !ZF
1653 Tys.push_back(MVT::i8);
1654 Tys.push_back(MVT::Flag);
1655 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1656 Ops.push_back(Cond);
1657 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1658 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1659 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1660 Tmp1.getValue(1));
1661 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1662 }
1663 }
1664 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001665 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001666 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001667 MVT::ValueType VT = Op.getValueType();
1668 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001669 bool isFPStack = isFP && !X86ScalarSSE;
1670 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001671 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001672 SDOperand Op0 = Op.getOperand(0);
1673 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001674 if (Op0.getOpcode() == ISD::SETCC)
1675 Op0 = LowerOperation(Op0, DAG);
1676
Evan Chengaaca22c2006-01-10 20:26:56 +00001677 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001678 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1679 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1680 // have another use it will be eliminated.
1681 // If the X86ISD::SETCC has more than one use, then it's probably better
1682 // to use a test instead of duplicating the X86ISD::CMP (for register
1683 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001684 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1685 if (!Op0.hasOneUse()) {
1686 std::vector<MVT::ValueType> Tys;
1687 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1688 Tys.push_back(Op0.Val->getValueType(i));
1689 std::vector<SDOperand> Ops;
1690 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1691 Ops.push_back(Op0.getOperand(i));
1692 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1693 }
1694
Evan Cheng1bcee362006-01-13 01:03:02 +00001695 CC = Op0.getOperand(0);
1696 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001697 // Make a copy as flag result cannot be used by more than one.
1698 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1699 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001700 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001701 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001702 } else
1703 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001704 } else
1705 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001706
Evan Cheng189d01e2006-01-13 01:06:49 +00001707 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001708 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001709 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001710 }
Evan Chenge3413162006-01-09 18:33:28 +00001711
1712 std::vector<MVT::ValueType> Tys;
1713 Tys.push_back(Op.getValueType());
1714 Tys.push_back(MVT::Flag);
1715 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001716 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1717 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001718 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001719 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001720 Ops.push_back(CC);
1721 Ops.push_back(Cond);
1722 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001723 }
Evan Cheng898101c2005-12-19 23:12:38 +00001724 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001725 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001726 SDOperand Cond = Op.getOperand(1);
1727 SDOperand Dest = Op.getOperand(2);
1728 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001729 if (Cond.getOpcode() == ISD::SETCC)
1730 Cond = LowerOperation(Cond, DAG);
1731
Evan Chengd5781fc2005-12-21 20:21:51 +00001732 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001733 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1734 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1735 // have another use it will be eliminated.
1736 // If the X86ISD::SETCC has more than one use, then it's probably better
1737 // to use a test instead of duplicating the X86ISD::CMP (for register
1738 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001739 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1740 if (!Cond.hasOneUse()) {
1741 std::vector<MVT::ValueType> Tys;
1742 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1743 Tys.push_back(Cond.Val->getValueType(i));
1744 std::vector<SDOperand> Ops;
1745 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1746 Ops.push_back(Cond.getOperand(i));
1747 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1748 }
1749
Evan Cheng1bcee362006-01-13 01:03:02 +00001750 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001751 Cond = Cond.getOperand(1);
1752 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001753 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001754 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001755 } else
1756 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001757 } else
1758 addTest = true;
1759
1760 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001761 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001762 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1763 }
1764 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1765 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1766 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001767 case ISD::MEMSET: {
1768 SDOperand InFlag;
1769 SDOperand Chain = Op.getOperand(0);
1770 unsigned Align =
1771 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1772 if (Align == 0) Align = 1;
1773
1774 MVT::ValueType AVT;
1775 SDOperand Count;
1776 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1777 unsigned ValReg;
1778 unsigned Val = ValC->getValue() & 255;
1779
1780 // If the value is a constant, then we can potentially use larger sets.
1781 switch (Align & 3) {
1782 case 2: // WORD aligned
1783 AVT = MVT::i16;
1784 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1785 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1786 else
1787 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1788 DAG.getConstant(1, MVT::i8));
1789 Val = (Val << 8) | Val;
1790 ValReg = X86::AX;
1791 break;
1792 case 0: // DWORD aligned
1793 AVT = MVT::i32;
1794 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1795 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1796 else
1797 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1798 DAG.getConstant(2, MVT::i8));
1799 Val = (Val << 8) | Val;
1800 Val = (Val << 16) | Val;
1801 ValReg = X86::EAX;
1802 break;
1803 default: // Byte aligned
1804 AVT = MVT::i8;
1805 Count = Op.getOperand(3);
1806 ValReg = X86::AL;
1807 break;
1808 }
1809
1810 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1811 InFlag);
1812 InFlag = Chain.getValue(1);
1813 } else {
1814 AVT = MVT::i8;
1815 Count = Op.getOperand(3);
1816 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1817 InFlag = Chain.getValue(1);
1818 }
1819
1820 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1821 InFlag = Chain.getValue(1);
1822 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1823 InFlag = Chain.getValue(1);
1824
1825 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1826 DAG.getValueType(AVT), InFlag);
1827 }
1828 case ISD::MEMCPY: {
1829 SDOperand Chain = Op.getOperand(0);
1830 unsigned Align =
1831 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1832 if (Align == 0) Align = 1;
1833
1834 MVT::ValueType AVT;
1835 SDOperand Count;
1836 switch (Align & 3) {
1837 case 2: // WORD aligned
1838 AVT = MVT::i16;
1839 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1840 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1841 else
1842 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1843 DAG.getConstant(1, MVT::i8));
1844 break;
1845 case 0: // DWORD aligned
1846 AVT = MVT::i32;
1847 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1848 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1849 else
1850 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1851 DAG.getConstant(2, MVT::i8));
1852 break;
1853 default: // Byte aligned
1854 AVT = MVT::i8;
1855 Count = Op.getOperand(3);
1856 break;
1857 }
1858
1859 SDOperand InFlag;
1860 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1861 InFlag = Chain.getValue(1);
1862 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1863 InFlag = Chain.getValue(1);
1864 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1865 InFlag = Chain.getValue(1);
1866
1867 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1868 DAG.getValueType(AVT), InFlag);
1869 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001870 case ISD::GlobalAddress: {
Evan Cheng002fe9b2006-01-12 07:56:47 +00001871 SDOperand Result;
Evan Chengb077b842005-12-21 02:39:21 +00001872 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1873 // For Darwin, external and weak symbols are indirect, so we want to load
1874 // the value at address GV, not the value of GV itself. This means that
1875 // the GlobalAddress must be in the base or index register of the address,
1876 // not the GV offset field.
1877 if (getTargetMachine().
1878 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1879 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng002fe9b2006-01-12 07:56:47 +00001880 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1881 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1882 DAG.getSrcValue(NULL));
1883 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001884 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001885 case ISD::VASTART: {
1886 // vastart just stores the address of the VarArgsFrameIndex slot into the
1887 // memory location argument.
1888 // FIXME: Replace MVT::i32 with PointerTy
1889 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1890 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1891 Op.getOperand(1), Op.getOperand(2));
1892 }
Nate Begemanee625572006-01-27 21:09:22 +00001893 case ISD::RET: {
1894 SDOperand Copy;
1895
1896 switch(Op.getNumOperands()) {
1897 default:
1898 assert(0 && "Do not know how to return this many arguments!");
1899 abort();
1900 case 1:
1901 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1902 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1903 case 2: {
1904 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1905 if (MVT::isInteger(ArgVT))
1906 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1907 SDOperand());
1908 else if (!X86ScalarSSE) {
1909 std::vector<MVT::ValueType> Tys;
1910 Tys.push_back(MVT::Other);
1911 Tys.push_back(MVT::Flag);
1912 std::vector<SDOperand> Ops;
1913 Ops.push_back(Op.getOperand(0));
1914 Ops.push_back(Op.getOperand(1));
1915 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1916 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00001917 SDOperand MemLoc;
1918 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001919 SDOperand Value = Op.getOperand(1);
1920
Evan Cheng760df292006-02-01 01:19:32 +00001921 if (Value.getOpcode() == ISD::LOAD &&
1922 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00001923 Chain = Value.getOperand(0);
1924 MemLoc = Value.getOperand(1);
1925 } else {
1926 // Spill the value to memory and reload it into top of stack.
1927 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1928 MachineFunction &MF = DAG.getMachineFunction();
1929 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1930 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1931 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1932 Value, MemLoc, DAG.getSrcValue(0));
1933 }
Nate Begemanee625572006-01-27 21:09:22 +00001934 std::vector<MVT::ValueType> Tys;
1935 Tys.push_back(MVT::f64);
1936 Tys.push_back(MVT::Other);
1937 std::vector<SDOperand> Ops;
1938 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001939 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00001940 Ops.push_back(DAG.getValueType(ArgVT));
1941 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1942 Tys.clear();
1943 Tys.push_back(MVT::Other);
1944 Tys.push_back(MVT::Flag);
1945 Ops.clear();
1946 Ops.push_back(Copy.getValue(1));
1947 Ops.push_back(Copy);
1948 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1949 }
1950 break;
1951 }
1952 case 3:
1953 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1954 SDOperand());
1955 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1956 break;
1957 }
1958 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1959 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1960 Copy.getValue(1));
1961 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001962 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001963}
Evan Cheng72261582005-12-20 06:22:03 +00001964
1965const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1966 switch (Opcode) {
1967 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001968 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1969 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1970 case X86ISD::ADC: return "X86ISD::ADC";
1971 case X86ISD::SBB: return "X86ISD::SBB";
1972 case X86ISD::SHLD: return "X86ISD::SHLD";
1973 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00001974 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00001975 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00001976 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng72261582005-12-20 06:22:03 +00001977 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1978 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1979 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001980 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001981 case X86ISD::FST: return "X86ISD::FST";
1982 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001983 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001984 case X86ISD::CALL: return "X86ISD::CALL";
1985 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1986 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1987 case X86ISD::CMP: return "X86ISD::CMP";
1988 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001989 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001990 case X86ISD::CMOV: return "X86ISD::CMOV";
1991 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001992 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng67f92a72006-01-11 22:15:48 +00001993 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1994 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00001995 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng72261582005-12-20 06:22:03 +00001996 }
1997}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001998
1999bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002000 uint64_t Mask) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002001
2002 unsigned Opc = Op.getOpcode();
2003
2004 switch (Opc) {
2005 default:
2006 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2007 break;
2008 case X86ISD::SETCC: return (Mask & 1) == 0;
2009 }
2010
2011 return false;
2012}
Chris Lattner259e97c2006-01-31 19:43:35 +00002013
2014std::vector<unsigned> X86TargetLowering::
2015getRegForInlineAsmConstraint(const std::string &Constraint) const {
2016 if (Constraint.size() == 1) {
2017 // FIXME: not handling fp-stack yet!
2018 // FIXME: not handling MMX registers yet ('y' constraint).
2019 switch (Constraint[0]) { // GCC X86 Constraint Letters
2020 default: break; // Unknown constriant letter
2021 case 'r': // GENERAL_REGS
2022 case 'R': // LEGACY_REGS
2023 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2024 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2025 case 'l': // INDEX_REGS
2026 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2027 X86::ESI, X86::EDI, X86::EBP, 0);
2028 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2029 case 'Q': // Q_REGS
2030 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2031 case 'x': // SSE_REGS if SSE1 allowed
2032 if (Subtarget->hasSSE1())
2033 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2034 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2035 0);
2036 return std::vector<unsigned>();
2037 case 'Y': // SSE_REGS if SSE2 allowed
2038 if (Subtarget->hasSSE2())
2039 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2040 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2041 0);
2042 return std::vector<unsigned>();
2043 }
2044 }
2045
2046 // Handle explicit register names.
2047 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
2048}