blob: 5064b21ae6a4cbb435a5b0b3f8ded10315843614 [file] [log] [blame]
Chris Lattner76ac0682005-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 Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
20#include "llvm/Function.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetOptions.h"
27using namespace llvm;
28
29// FIXME: temporary.
30#include "llvm/Support/CommandLine.h"
31static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
32 cl::desc("Enable fastcc on X86"));
33
34X86TargetLowering::X86TargetLowering(TargetMachine &TM)
35 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000036 Subtarget = &TM.getSubtarget<X86Subtarget>();
37 X86ScalarSSE = Subtarget->hasSSE2();
38
Chris Lattner76ac0682005-11-15 00:40:23 +000039 // Set up the TargetLowering object.
40
41 // X86 is weird, it always uses i8 for shift amounts and setcc results.
42 setShiftAmountType(MVT::i8);
43 setSetCCResultType(MVT::i8);
44 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000045 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000047 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000048
Chris Lattner76ac0682005-11-15 00:40:23 +000049 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000050 addRegisterClass(MVT::i8, X86::R8RegisterClass);
51 addRegisterClass(MVT::i16, X86::R16RegisterClass);
52 addRegisterClass(MVT::i32, X86::R32RegisterClass);
53
54 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
55 // operation.
56 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
57 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
58 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000059
60 if (X86ScalarSSE)
61 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
62 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
63 else
64 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000065
66 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
67 // this operation.
68 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
69 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Evan Cheng08390f62006-01-30 22:13:22 +000070 if (X86ScalarSSE)
71 // SSE has no i16 to fp conversion, only i32
72 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
73 else if (!X86PatIsel) {
74 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
75 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
76 }
Chris Lattner76ac0682005-11-15 00:40:23 +000077
Evan Cheng5b97fcf2006-01-30 08:02:57 +000078 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
79 // isn't legal.
80 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
81 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
82
Evan Cheng08390f62006-01-30 22:13:22 +000083 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
84 // this operation.
85 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
86 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
87
88 if (X86ScalarSSE) {
89 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
90 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +000091 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +000092 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +000093 }
94
95 // Handle FP_TO_UINT by promoting the destination to a larger signed
96 // conversion.
97 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
98 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
99 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
100
Evan Cheng08390f62006-01-30 22:13:22 +0000101 if (X86ScalarSSE)
102 // Expand FP_TO_UINT into a select.
103 // FIXME: We would like to use a Custom expander here eventually to do
104 // the optimal thing for SSE vs. the default expansion in the legalizer.
105 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
106 else
Chris Lattner76ac0682005-11-15 00:40:23 +0000107 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
108
Evan Cheng08390f62006-01-30 22:13:22 +0000109 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
110 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000111
Evan Chenga814f0b32006-01-27 21:26:54 +0000112 if (!X86PatIsel) {
Evan Cheng6fc31042005-12-19 23:12:38 +0000113 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
114 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000115 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
116 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
117 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
121 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
122 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
123 setOperationAction(ISD::FREM , MVT::f64 , Expand);
124 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
125 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
126 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
127 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
128 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
129 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
130 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
131 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
132 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000133 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000134
Evan Chenga814f0b32006-01-27 21:26:54 +0000135 if (X86PatIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000136 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000137 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
138 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
139 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
140 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
141 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
142 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
143 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000144 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000145
Chris Lattner76ac0682005-11-15 00:40:23 +0000146 setOperationAction(ISD::READIO , MVT::i1 , Expand);
147 setOperationAction(ISD::READIO , MVT::i8 , Expand);
148 setOperationAction(ISD::READIO , MVT::i16 , Expand);
149 setOperationAction(ISD::READIO , MVT::i32 , Expand);
150 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
151 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
152 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
153 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
154
155 // These should be promoted to a larger select which is supported.
156 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
157 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Chenga814f0b32006-01-27 21:26:54 +0000158 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000159 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000160 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
161 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000162 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000164 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000167 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
169 // X86 ret instruction may pop stack.
170 setOperationAction(ISD::RET , MVT::Other, Custom);
171 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000172 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
174 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
175 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000179 // X86 wants to expand memset / memcpy itself.
180 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
181 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000182 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000183
Chris Lattner9c415362005-11-29 06:16:21 +0000184 // We don't have line number support yet.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
187 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000188
Nate Begemane74795c2006-01-25 18:21:52 +0000189 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
190 setOperationAction(ISD::VASTART , MVT::Other, Custom);
191
192 // Use the default implementation.
193 setOperationAction(ISD::VAARG , MVT::Other, Expand);
194 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
195 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000196 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
197 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
198 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000199
Chris Lattner76ac0682005-11-15 00:40:23 +0000200 if (X86ScalarSSE) {
201 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000202 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
203 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000204
205 // SSE has no load+extend ops
206 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
207 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
208
Chris Lattner76ac0682005-11-15 00:40:23 +0000209 // We don't support sin/cos/sqrt/fmod
210 setOperationAction(ISD::FSIN , MVT::f64, Expand);
211 setOperationAction(ISD::FCOS , MVT::f64, Expand);
212 setOperationAction(ISD::FABS , MVT::f64, Expand);
213 setOperationAction(ISD::FNEG , MVT::f64, Expand);
214 setOperationAction(ISD::FREM , MVT::f64, Expand);
215 setOperationAction(ISD::FSIN , MVT::f32, Expand);
216 setOperationAction(ISD::FCOS , MVT::f32, Expand);
217 setOperationAction(ISD::FABS , MVT::f32, Expand);
218 setOperationAction(ISD::FNEG , MVT::f32, Expand);
219 setOperationAction(ISD::FREM , MVT::f32, Expand);
220
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000221 // Expand FP immediates into loads from the stack, except for the special
222 // cases we handle.
223 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
224 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000225 addLegalFPImmediate(+0.0); // xorps / xorpd
226 } else {
227 // Set up the FP register classes.
228 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000229
230 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
231
Chris Lattner76ac0682005-11-15 00:40:23 +0000232 if (!UnsafeFPMath) {
233 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
234 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
235 }
236
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000237 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000238 addLegalFPImmediate(+0.0); // FLD0
239 addLegalFPImmediate(+1.0); // FLD1
240 addLegalFPImmediate(-0.0); // FLD0/FCHS
241 addLegalFPImmediate(-1.0); // FLD1/FCHS
242 }
243 computeRegisterProperties();
244
245 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
246 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
247 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
248 allowUnalignedMemoryAccesses = true; // x86 supports it!
249}
250
251std::vector<SDOperand>
252X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
253 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
254 return LowerFastCCArguments(F, DAG);
255 return LowerCCCArguments(F, DAG);
256}
257
258std::pair<SDOperand, SDOperand>
259X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
260 bool isVarArg, unsigned CallingConv,
261 bool isTailCall,
262 SDOperand Callee, ArgListTy &Args,
263 SelectionDAG &DAG) {
264 assert((!isVarArg || CallingConv == CallingConv::C) &&
265 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000266
267 // If the callee is a GlobalAddress node (quite common, every direct call is)
268 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
269 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
270 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000271 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
272 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000273
Chris Lattner76ac0682005-11-15 00:40:23 +0000274 if (CallingConv == CallingConv::Fast && EnableFastCC)
275 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
276 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
277}
278
279//===----------------------------------------------------------------------===//
280// C Calling Convention implementation
281//===----------------------------------------------------------------------===//
282
283std::vector<SDOperand>
284X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
285 std::vector<SDOperand> ArgValues;
286
287 MachineFunction &MF = DAG.getMachineFunction();
288 MachineFrameInfo *MFI = MF.getFrameInfo();
289
290 // Add DAG nodes to load the arguments... On entry to a function on the X86,
291 // the stack frame looks like this:
292 //
293 // [ESP] -- return address
294 // [ESP + 4] -- first argument (leftmost lexically)
295 // [ESP + 8] -- second argument, if first argument is four bytes in size
296 // ...
297 //
298 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
299 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
300 MVT::ValueType ObjectVT = getValueType(I->getType());
301 unsigned ArgIncrement = 4;
302 unsigned ObjSize;
303 switch (ObjectVT) {
304 default: assert(0 && "Unhandled argument type!");
305 case MVT::i1:
306 case MVT::i8: ObjSize = 1; break;
307 case MVT::i16: ObjSize = 2; break;
308 case MVT::i32: ObjSize = 4; break;
309 case MVT::i64: ObjSize = ArgIncrement = 8; break;
310 case MVT::f32: ObjSize = 4; break;
311 case MVT::f64: ObjSize = ArgIncrement = 8; break;
312 }
313 // Create the frame index object for this incoming parameter...
314 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
315
316 // Create the SelectionDAG nodes corresponding to a load from this parameter
317 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
318
319 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
320 // dead loads.
321 SDOperand ArgValue;
322 if (!I->use_empty())
323 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
324 DAG.getSrcValue(NULL));
325 else {
326 if (MVT::isInteger(ObjectVT))
327 ArgValue = DAG.getConstant(0, ObjectVT);
328 else
329 ArgValue = DAG.getConstantFP(0, ObjectVT);
330 }
331 ArgValues.push_back(ArgValue);
332
333 ArgOffset += ArgIncrement; // Move on to the next argument...
334 }
335
336 // If the function takes variable number of arguments, make a frame index for
337 // the start of the first vararg value... for expansion of llvm.va_start.
338 if (F.isVarArg())
339 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
340 ReturnAddrIndex = 0; // No return address slot generated yet.
341 BytesToPopOnReturn = 0; // Callee pops nothing.
342 BytesCallerReserves = ArgOffset;
343
344 // Finally, inform the code generator which regs we return values in.
345 switch (getValueType(F.getReturnType())) {
346 default: assert(0 && "Unknown type!");
347 case MVT::isVoid: break;
348 case MVT::i1:
349 case MVT::i8:
350 case MVT::i16:
351 case MVT::i32:
352 MF.addLiveOut(X86::EAX);
353 break;
354 case MVT::i64:
355 MF.addLiveOut(X86::EAX);
356 MF.addLiveOut(X86::EDX);
357 break;
358 case MVT::f32:
359 case MVT::f64:
360 MF.addLiveOut(X86::ST0);
361 break;
362 }
363 return ArgValues;
364}
365
366std::pair<SDOperand, SDOperand>
367X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
368 bool isVarArg, bool isTailCall,
369 SDOperand Callee, ArgListTy &Args,
370 SelectionDAG &DAG) {
371 // Count how many bytes are to be pushed on the stack.
372 unsigned NumBytes = 0;
373
374 if (Args.empty()) {
375 // Save zero bytes.
376 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
377 DAG.getConstant(0, getPointerTy()));
378 } else {
379 for (unsigned i = 0, e = Args.size(); i != e; ++i)
380 switch (getValueType(Args[i].second)) {
381 default: assert(0 && "Unknown value type!");
382 case MVT::i1:
383 case MVT::i8:
384 case MVT::i16:
385 case MVT::i32:
386 case MVT::f32:
387 NumBytes += 4;
388 break;
389 case MVT::i64:
390 case MVT::f64:
391 NumBytes += 8;
392 break;
393 }
394
395 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
396 DAG.getConstant(NumBytes, getPointerTy()));
397
398 // Arguments go on the stack in reverse order, as specified by the ABI.
399 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000400 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000401 std::vector<SDOperand> Stores;
402
403 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
404 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
405 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
406
407 switch (getValueType(Args[i].second)) {
408 default: assert(0 && "Unexpected ValueType for argument!");
409 case MVT::i1:
410 case MVT::i8:
411 case MVT::i16:
412 // Promote the integer to 32 bits. If the input type is signed use a
413 // sign extend, otherwise use a zero extend.
414 if (Args[i].second->isSigned())
415 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
416 else
417 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
418
419 // FALL THROUGH
420 case MVT::i32:
421 case MVT::f32:
422 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
423 Args[i].first, PtrOff,
424 DAG.getSrcValue(NULL)));
425 ArgOffset += 4;
426 break;
427 case MVT::i64:
428 case MVT::f64:
429 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
430 Args[i].first, PtrOff,
431 DAG.getSrcValue(NULL)));
432 ArgOffset += 8;
433 break;
434 }
435 }
436 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
437 }
438
439 std::vector<MVT::ValueType> RetVals;
440 MVT::ValueType RetTyVT = getValueType(RetTy);
441 RetVals.push_back(MVT::Other);
442
443 // The result values produced have to be legal. Promote the result.
444 switch (RetTyVT) {
445 case MVT::isVoid: break;
446 default:
447 RetVals.push_back(RetTyVT);
448 break;
449 case MVT::i1:
450 case MVT::i8:
451 case MVT::i16:
452 RetVals.push_back(MVT::i32);
453 break;
454 case MVT::f32:
455 if (X86ScalarSSE)
456 RetVals.push_back(MVT::f32);
457 else
458 RetVals.push_back(MVT::f64);
459 break;
460 case MVT::i64:
461 RetVals.push_back(MVT::i32);
462 RetVals.push_back(MVT::i32);
463 break;
464 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000465
Evan Chenga814f0b32006-01-27 21:26:54 +0000466 if (!X86PatIsel) {
Evan Cheng45e190982006-01-05 00:27:02 +0000467 std::vector<MVT::ValueType> NodeTys;
468 NodeTys.push_back(MVT::Other); // Returns a chain
469 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000470 std::vector<SDOperand> Ops;
471 Ops.push_back(Chain);
472 Ops.push_back(Callee);
473
Evan Cheng172fce72006-01-06 00:43:03 +0000474 // FIXME: Do not generate X86ISD::TAILCALL for now.
475 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000476 SDOperand InFlag = Chain.getValue(1);
477
Chris Lattner6f33eae2006-01-24 05:17:12 +0000478 NodeTys.clear();
479 NodeTys.push_back(MVT::Other); // Returns a chain
480 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
481 Ops.clear();
482 Ops.push_back(Chain);
483 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
484 Ops.push_back(DAG.getConstant(0, getPointerTy()));
485 Ops.push_back(InFlag);
486 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
487 InFlag = Chain.getValue(1);
488
Evan Cheng45e190982006-01-05 00:27:02 +0000489 SDOperand RetVal;
490 if (RetTyVT != MVT::isVoid) {
491 switch (RetTyVT) {
492 default: assert(0 && "Unknown value type to return!");
493 case MVT::i1:
494 case MVT::i8:
495 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
496 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000497 if (RetTyVT == MVT::i1)
498 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000499 break;
500 case MVT::i16:
501 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
502 Chain = RetVal.getValue(1);
503 break;
504 case MVT::i32:
505 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
506 Chain = RetVal.getValue(1);
507 break;
508 case MVT::i64: {
509 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
510 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
511 Lo.getValue(2));
512 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
513 Chain = Hi.getValue(1);
514 break;
515 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000516 case MVT::f32:
Evan Cheng45e190982006-01-05 00:27:02 +0000517 case MVT::f64: {
518 std::vector<MVT::ValueType> Tys;
519 Tys.push_back(MVT::f64);
520 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +0000521 Tys.push_back(MVT::Flag);
Evan Cheng45e190982006-01-05 00:27:02 +0000522 std::vector<SDOperand> Ops;
523 Ops.push_back(Chain);
524 Ops.push_back(InFlag);
525 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +0000526 Chain = RetVal.getValue(1);
527 InFlag = RetVal.getValue(2);
Evan Cheng45e190982006-01-05 00:27:02 +0000528 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000529 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
530 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +0000531 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng45e190982006-01-05 00:27:02 +0000532 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000533 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng45e190982006-01-05 00:27:02 +0000534 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
535 Tys.clear();
536 Tys.push_back(MVT::Other);
537 Ops.clear();
538 Ops.push_back(Chain);
539 Ops.push_back(RetVal);
540 Ops.push_back(StackSlot);
541 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +0000542 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000543 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
544 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
545 DAG.getSrcValue(NULL));
546 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +0000547 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000548
549 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
550 // FIXME: we would really like to remember that this FP_ROUND
551 // operation is okay to eliminate if we allow excess FP precision.
552 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000553 break;
554 }
555 }
556 }
557
Evan Cheng45e190982006-01-05 00:27:02 +0000558 return std::make_pair(RetVal, Chain);
559 } else {
560 std::vector<SDOperand> Ops;
561 Ops.push_back(Chain);
562 Ops.push_back(Callee);
563 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
564 Ops.push_back(DAG.getConstant(0, getPointerTy()));
565
566 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
567 RetVals, Ops);
568
569 SDOperand ResultVal;
570 switch (RetTyVT) {
571 case MVT::isVoid: break;
572 default:
573 ResultVal = TheCall.getValue(1);
574 break;
575 case MVT::i1:
576 case MVT::i8:
577 case MVT::i16:
578 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
579 break;
580 case MVT::f32:
581 // FIXME: we would really like to remember that this FP_ROUND operation is
582 // okay to eliminate if we allow excess FP precision.
583 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
584 break;
585 case MVT::i64:
586 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
587 TheCall.getValue(2));
588 break;
589 }
590
591 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
592 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000593 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000594}
595
Chris Lattner76ac0682005-11-15 00:40:23 +0000596//===----------------------------------------------------------------------===//
597// Fast Calling Convention implementation
598//===----------------------------------------------------------------------===//
599//
600// The X86 'fast' calling convention passes up to two integer arguments in
601// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
602// and requires that the callee pop its arguments off the stack (allowing proper
603// tail calls), and has the same return value conventions as C calling convs.
604//
605// This calling convention always arranges for the callee pop value to be 8n+4
606// bytes, which is needed for tail recursion elimination and stack alignment
607// reasons.
608//
609// Note that this can be enhanced in the future to pass fp vals in registers
610// (when we have a global fp allocator) and do other tricks.
611//
612
613/// AddLiveIn - This helper function adds the specified physical register to the
614/// MachineFunction as a live in value. It also creates a corresponding virtual
615/// register for it.
616static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
617 TargetRegisterClass *RC) {
618 assert(RC->contains(PReg) && "Not the correct regclass!");
619 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
620 MF.addLiveIn(PReg, VReg);
621 return VReg;
622}
623
624
625std::vector<SDOperand>
626X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
627 std::vector<SDOperand> ArgValues;
628
629 MachineFunction &MF = DAG.getMachineFunction();
630 MachineFrameInfo *MFI = MF.getFrameInfo();
631
632 // Add DAG nodes to load the arguments... On entry to a function the stack
633 // frame looks like this:
634 //
635 // [ESP] -- return address
636 // [ESP + 4] -- first nonreg argument (leftmost lexically)
637 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
638 // ...
639 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
640
641 // Keep track of the number of integer regs passed so far. This can be either
642 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
643 // used).
644 unsigned NumIntRegs = 0;
645
646 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
647 MVT::ValueType ObjectVT = getValueType(I->getType());
648 unsigned ArgIncrement = 4;
649 unsigned ObjSize = 0;
650 SDOperand ArgValue;
651
652 switch (ObjectVT) {
653 default: assert(0 && "Unhandled argument type!");
654 case MVT::i1:
655 case MVT::i8:
656 if (NumIntRegs < 2) {
657 if (!I->use_empty()) {
658 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
659 X86::R8RegisterClass);
660 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
661 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000662 if (ObjectVT == MVT::i1)
663 // FIXME: Should insert a assertzext here.
664 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000665 }
666 ++NumIntRegs;
667 break;
668 }
669
670 ObjSize = 1;
671 break;
672 case MVT::i16:
673 if (NumIntRegs < 2) {
674 if (!I->use_empty()) {
675 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
676 X86::R16RegisterClass);
677 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
678 DAG.setRoot(ArgValue.getValue(1));
679 }
680 ++NumIntRegs;
681 break;
682 }
683 ObjSize = 2;
684 break;
685 case MVT::i32:
686 if (NumIntRegs < 2) {
687 if (!I->use_empty()) {
688 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
689 X86::R32RegisterClass);
690 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
691 DAG.setRoot(ArgValue.getValue(1));
692 }
693 ++NumIntRegs;
694 break;
695 }
696 ObjSize = 4;
697 break;
698 case MVT::i64:
699 if (NumIntRegs == 0) {
700 if (!I->use_empty()) {
701 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
702 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
703
704 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
705 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
706 DAG.setRoot(Hi.getValue(1));
707
708 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
709 }
710 NumIntRegs = 2;
711 break;
712 } else if (NumIntRegs == 1) {
713 if (!I->use_empty()) {
714 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
715 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
716 DAG.setRoot(Low.getValue(1));
717
718 // Load the high part from memory.
719 // Create the frame index object for this incoming parameter...
720 int FI = MFI->CreateFixedObject(4, ArgOffset);
721 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
722 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
723 DAG.getSrcValue(NULL));
724 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
725 }
726 ArgOffset += 4;
727 NumIntRegs = 2;
728 break;
729 }
730 ObjSize = ArgIncrement = 8;
731 break;
732 case MVT::f32: ObjSize = 4; break;
733 case MVT::f64: ObjSize = ArgIncrement = 8; break;
734 }
735
736 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
737 // dead loads.
738 if (ObjSize && !I->use_empty()) {
739 // Create the frame index object for this incoming parameter...
740 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
741
742 // Create the SelectionDAG nodes corresponding to a load from this
743 // parameter.
744 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
745
746 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
747 DAG.getSrcValue(NULL));
748 } else if (ArgValue.Val == 0) {
749 if (MVT::isInteger(ObjectVT))
750 ArgValue = DAG.getConstant(0, ObjectVT);
751 else
752 ArgValue = DAG.getConstantFP(0, ObjectVT);
753 }
754 ArgValues.push_back(ArgValue);
755
756 if (ObjSize)
757 ArgOffset += ArgIncrement; // Move on to the next argument.
758 }
759
760 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
761 // arguments and the arguments after the retaddr has been pushed are aligned.
762 if ((ArgOffset & 7) == 0)
763 ArgOffset += 4;
764
765 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
766 ReturnAddrIndex = 0; // No return address slot generated yet.
767 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
768 BytesCallerReserves = 0;
769
770 // Finally, inform the code generator which regs we return values in.
771 switch (getValueType(F.getReturnType())) {
772 default: assert(0 && "Unknown type!");
773 case MVT::isVoid: break;
774 case MVT::i1:
775 case MVT::i8:
776 case MVT::i16:
777 case MVT::i32:
778 MF.addLiveOut(X86::EAX);
779 break;
780 case MVT::i64:
781 MF.addLiveOut(X86::EAX);
782 MF.addLiveOut(X86::EDX);
783 break;
784 case MVT::f32:
785 case MVT::f64:
786 MF.addLiveOut(X86::ST0);
787 break;
788 }
789 return ArgValues;
790}
791
792std::pair<SDOperand, SDOperand>
793X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
794 bool isTailCall, SDOperand Callee,
795 ArgListTy &Args, SelectionDAG &DAG) {
796 // Count how many bytes are to be pushed on the stack.
797 unsigned NumBytes = 0;
798
799 // Keep track of the number of integer regs passed so far. This can be either
800 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
801 // used).
802 unsigned NumIntRegs = 0;
803
804 for (unsigned i = 0, e = Args.size(); i != e; ++i)
805 switch (getValueType(Args[i].second)) {
806 default: assert(0 && "Unknown value type!");
807 case MVT::i1:
808 case MVT::i8:
809 case MVT::i16:
810 case MVT::i32:
811 if (NumIntRegs < 2) {
812 ++NumIntRegs;
813 break;
814 }
815 // fall through
816 case MVT::f32:
817 NumBytes += 4;
818 break;
819 case MVT::i64:
820 if (NumIntRegs == 0) {
821 NumIntRegs = 2;
822 break;
823 } else if (NumIntRegs == 1) {
824 NumIntRegs = 2;
825 NumBytes += 4;
826 break;
827 }
828
829 // fall through
830 case MVT::f64:
831 NumBytes += 8;
832 break;
833 }
834
835 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
836 // arguments and the arguments after the retaddr has been pushed are aligned.
837 if ((NumBytes & 7) == 0)
838 NumBytes += 4;
839
840 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
841 DAG.getConstant(NumBytes, getPointerTy()));
842
843 // Arguments go on the stack in reverse order, as specified by the ABI.
844 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000845 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000846 NumIntRegs = 0;
847 std::vector<SDOperand> Stores;
848 std::vector<SDOperand> RegValuesToPass;
849 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
850 switch (getValueType(Args[i].second)) {
851 default: assert(0 && "Unexpected ValueType for argument!");
852 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000853 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
854 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000855 case MVT::i8:
856 case MVT::i16:
857 case MVT::i32:
858 if (NumIntRegs < 2) {
859 RegValuesToPass.push_back(Args[i].first);
860 ++NumIntRegs;
861 break;
862 }
863 // Fall through
864 case MVT::f32: {
865 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
866 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
867 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
868 Args[i].first, PtrOff,
869 DAG.getSrcValue(NULL)));
870 ArgOffset += 4;
871 break;
872 }
873 case MVT::i64:
874 if (NumIntRegs < 2) { // Can pass part of it in regs?
875 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
876 Args[i].first, DAG.getConstant(1, MVT::i32));
877 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
878 Args[i].first, DAG.getConstant(0, MVT::i32));
879 RegValuesToPass.push_back(Lo);
880 ++NumIntRegs;
881 if (NumIntRegs < 2) { // Pass both parts in regs?
882 RegValuesToPass.push_back(Hi);
883 ++NumIntRegs;
884 } else {
885 // Pass the high part in memory.
886 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
887 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
888 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
889 Hi, PtrOff, DAG.getSrcValue(NULL)));
890 ArgOffset += 4;
891 }
892 break;
893 }
894 // Fall through
895 case MVT::f64:
896 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
897 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
898 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
899 Args[i].first, PtrOff,
900 DAG.getSrcValue(NULL)));
901 ArgOffset += 8;
902 break;
903 }
904 }
905 if (!Stores.empty())
906 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
907
908 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
909 // arguments and the arguments after the retaddr has been pushed are aligned.
910 if ((ArgOffset & 7) == 0)
911 ArgOffset += 4;
912
913 std::vector<MVT::ValueType> RetVals;
914 MVT::ValueType RetTyVT = getValueType(RetTy);
915
916 RetVals.push_back(MVT::Other);
917
918 // The result values produced have to be legal. Promote the result.
919 switch (RetTyVT) {
920 case MVT::isVoid: break;
921 default:
922 RetVals.push_back(RetTyVT);
923 break;
924 case MVT::i1:
925 case MVT::i8:
926 case MVT::i16:
927 RetVals.push_back(MVT::i32);
928 break;
929 case MVT::f32:
930 if (X86ScalarSSE)
931 RetVals.push_back(MVT::f32);
932 else
933 RetVals.push_back(MVT::f64);
934 break;
935 case MVT::i64:
936 RetVals.push_back(MVT::i32);
937 RetVals.push_back(MVT::i32);
938 break;
939 }
940
Evan Chenga814f0b32006-01-27 21:26:54 +0000941 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000942 // Build a sequence of copy-to-reg nodes chained together with token chain
943 // and flag operands which copy the outgoing args into registers.
944 SDOperand InFlag;
945 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
946 unsigned CCReg;
947 SDOperand RegToPass = RegValuesToPass[i];
948 switch (RegToPass.getValueType()) {
949 default: assert(0 && "Bad thing to pass in regs");
950 case MVT::i8:
951 CCReg = (i == 0) ? X86::AL : X86::DL;
952 break;
953 case MVT::i16:
954 CCReg = (i == 0) ? X86::AX : X86::DX;
955 break;
956 case MVT::i32:
957 CCReg = (i == 0) ? X86::EAX : X86::EDX;
958 break;
959 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000960
Evan Cheng172fce72006-01-06 00:43:03 +0000961 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
962 InFlag = Chain.getValue(1);
963 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000964
Evan Cheng172fce72006-01-06 00:43:03 +0000965 std::vector<MVT::ValueType> NodeTys;
966 NodeTys.push_back(MVT::Other); // Returns a chain
967 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +0000968 std::vector<SDOperand> Ops;
969 Ops.push_back(Chain);
970 Ops.push_back(Callee);
971 if (InFlag.Val)
972 Ops.push_back(InFlag);
973
974 // FIXME: Do not generate X86ISD::TAILCALL for now.
975 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
976 InFlag = Chain.getValue(1);
977
Chris Lattner6f33eae2006-01-24 05:17:12 +0000978 NodeTys.clear();
979 NodeTys.push_back(MVT::Other); // Returns a chain
980 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
981 Ops.clear();
982 Ops.push_back(Chain);
983 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
984 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
985 Ops.push_back(InFlag);
986 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
987 InFlag = Chain.getValue(1);
988
Evan Cheng172fce72006-01-06 00:43:03 +0000989 SDOperand RetVal;
990 if (RetTyVT != MVT::isVoid) {
991 switch (RetTyVT) {
992 default: assert(0 && "Unknown value type to return!");
993 case MVT::i1:
994 case MVT::i8:
995 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
996 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000997 if (RetTyVT == MVT::i1)
998 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +0000999 break;
1000 case MVT::i16:
1001 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1002 Chain = RetVal.getValue(1);
1003 break;
1004 case MVT::i32:
1005 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1006 Chain = RetVal.getValue(1);
1007 break;
1008 case MVT::i64: {
1009 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1010 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1011 Lo.getValue(2));
1012 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1013 Chain = Hi.getValue(1);
1014 break;
1015 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001016 case MVT::f32:
Evan Cheng172fce72006-01-06 00:43:03 +00001017 case MVT::f64: {
1018 std::vector<MVT::ValueType> Tys;
1019 Tys.push_back(MVT::f64);
1020 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +00001021 Tys.push_back(MVT::Flag);
Evan Cheng172fce72006-01-06 00:43:03 +00001022 std::vector<SDOperand> Ops;
1023 Ops.push_back(Chain);
1024 Ops.push_back(InFlag);
1025 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +00001026 Chain = RetVal.getValue(1);
1027 InFlag = RetVal.getValue(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001028 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001029 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1030 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +00001031 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng172fce72006-01-06 00:43:03 +00001032 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001033 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng172fce72006-01-06 00:43:03 +00001034 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1035 Tys.clear();
1036 Tys.push_back(MVT::Other);
1037 Ops.clear();
1038 Ops.push_back(Chain);
1039 Ops.push_back(RetVal);
1040 Ops.push_back(StackSlot);
1041 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +00001042 Ops.push_back(InFlag);
Evan Cheng172fce72006-01-06 00:43:03 +00001043 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1044 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1045 DAG.getSrcValue(NULL));
1046 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +00001047 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001048
1049 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1050 // FIXME: we would really like to remember that this FP_ROUND
1051 // operation is okay to eliminate if we allow excess FP precision.
1052 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001053 break;
1054 }
1055 }
1056 }
1057
Evan Cheng172fce72006-01-06 00:43:03 +00001058 return std::make_pair(RetVal, Chain);
1059 } else {
1060 std::vector<SDOperand> Ops;
1061 Ops.push_back(Chain);
1062 Ops.push_back(Callee);
1063 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1064 // Callee pops all arg values on the stack.
1065 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1066
1067 // Pass register arguments as needed.
1068 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1069
1070 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1071 RetVals, Ops);
1072 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1073
1074 SDOperand ResultVal;
1075 switch (RetTyVT) {
1076 case MVT::isVoid: break;
1077 default:
1078 ResultVal = TheCall.getValue(1);
1079 break;
1080 case MVT::i1:
1081 case MVT::i8:
1082 case MVT::i16:
1083 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1084 break;
1085 case MVT::f32:
1086 // FIXME: we would really like to remember that this FP_ROUND operation is
1087 // okay to eliminate if we allow excess FP precision.
1088 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1089 break;
1090 case MVT::i64:
1091 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1092 TheCall.getValue(2));
1093 break;
1094 }
1095
1096 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001097 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001098}
1099
1100SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1101 if (ReturnAddrIndex == 0) {
1102 // Set up a frame object for the return address.
1103 MachineFunction &MF = DAG.getMachineFunction();
1104 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1105 }
1106
1107 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1108}
1109
1110
1111
1112std::pair<SDOperand, SDOperand> X86TargetLowering::
1113LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1114 SelectionDAG &DAG) {
1115 SDOperand Result;
1116 if (Depth) // Depths > 0 not supported yet!
1117 Result = DAG.getConstant(0, getPointerTy());
1118 else {
1119 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1120 if (!isFrameAddress)
1121 // Just load the return address
1122 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1123 DAG.getSrcValue(NULL));
1124 else
1125 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1126 DAG.getConstant(4, MVT::i32));
1127 }
1128 return std::make_pair(Result, Chain);
1129}
1130
Evan Cheng339edad2006-01-11 00:33:36 +00001131/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1132/// which corresponds to the condition code.
1133static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1134 switch (X86CC) {
1135 default: assert(0 && "Unknown X86 conditional code!");
1136 case X86ISD::COND_A: return X86::JA;
1137 case X86ISD::COND_AE: return X86::JAE;
1138 case X86ISD::COND_B: return X86::JB;
1139 case X86ISD::COND_BE: return X86::JBE;
1140 case X86ISD::COND_E: return X86::JE;
1141 case X86ISD::COND_G: return X86::JG;
1142 case X86ISD::COND_GE: return X86::JGE;
1143 case X86ISD::COND_L: return X86::JL;
1144 case X86ISD::COND_LE: return X86::JLE;
1145 case X86ISD::COND_NE: return X86::JNE;
1146 case X86ISD::COND_NO: return X86::JNO;
1147 case X86ISD::COND_NP: return X86::JNP;
1148 case X86ISD::COND_NS: return X86::JNS;
1149 case X86ISD::COND_O: return X86::JO;
1150 case X86ISD::COND_P: return X86::JP;
1151 case X86ISD::COND_S: return X86::JS;
1152 }
1153}
Chris Lattner76ac0682005-11-15 00:40:23 +00001154
Evan Cheng45df7f82006-01-30 23:41:35 +00001155/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1156/// specific condition code. It returns a false if it cannot do a direct
1157/// translation. X86CC is the translated CondCode. Flip is set to true if the
1158/// the order of comparison operands should be flipped.
1159static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC, bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001160 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001161 Flip = false;
1162 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001163 if (!isFP) {
1164 switch (SetCCOpcode) {
1165 default: break;
1166 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1167 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1168 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1169 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1170 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1171 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1172 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1173 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1174 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1175 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1176 }
1177 } else {
1178 // On a floating point condition, the flags are set as follows:
1179 // ZF PF CF op
1180 // 0 | 0 | 0 | X > Y
1181 // 0 | 0 | 1 | X < Y
1182 // 1 | 0 | 0 | X == Y
1183 // 1 | 1 | 1 | unordered
1184 switch (SetCCOpcode) {
1185 default: break;
1186 case ISD::SETUEQ:
1187 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001188 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001189 case ISD::SETOGT:
1190 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001191 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001192 case ISD::SETOGE:
1193 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001194 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001195 case ISD::SETULT:
1196 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001197 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001198 case ISD::SETULE:
1199 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1200 case ISD::SETONE:
1201 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1202 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1203 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1204 }
1205 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001206
1207 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001208}
1209
Evan Cheng339edad2006-01-11 00:33:36 +00001210/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1211/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001212/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001213static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001214 switch (X86CC) {
1215 default:
1216 return false;
1217 case X86ISD::COND_B:
1218 case X86ISD::COND_BE:
1219 case X86ISD::COND_E:
1220 case X86ISD::COND_P:
1221 case X86ISD::COND_A:
1222 case X86ISD::COND_AE:
1223 case X86ISD::COND_NE:
1224 case X86ISD::COND_NP:
1225 return true;
1226 }
1227}
1228
Evan Cheng339edad2006-01-11 00:33:36 +00001229MachineBasicBlock *
1230X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1231 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001232 switch (MI->getOpcode()) {
1233 default: assert(false && "Unexpected instr type to insert");
1234 case X86::CMOV_FR32:
1235 case X86::CMOV_FR64: {
1236 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1237 // control-flow pattern. The incoming instruction knows the destination vreg
1238 // to set, the condition code register to branch on, the true/false values to
1239 // select between, and a branch opcode to use.
1240 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1241 ilist<MachineBasicBlock>::iterator It = BB;
1242 ++It;
1243
1244 // thisMBB:
1245 // ...
1246 // TrueVal = ...
1247 // cmpTY ccX, r1, r2
1248 // bCC copy1MBB
1249 // fallthrough --> copy0MBB
1250 MachineBasicBlock *thisMBB = BB;
1251 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1252 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1253 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1254 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1255 MachineFunction *F = BB->getParent();
1256 F->getBasicBlockList().insert(It, copy0MBB);
1257 F->getBasicBlockList().insert(It, sinkMBB);
1258 // Update machine-CFG edges
1259 BB->addSuccessor(copy0MBB);
1260 BB->addSuccessor(sinkMBB);
1261
1262 // copy0MBB:
1263 // %FalseValue = ...
1264 // # fallthrough to sinkMBB
1265 BB = copy0MBB;
1266
1267 // Update machine-CFG edges
1268 BB->addSuccessor(sinkMBB);
1269
1270 // sinkMBB:
1271 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1272 // ...
1273 BB = sinkMBB;
1274 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1275 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1276 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001277
Evan Cheng911c68d2006-01-16 21:21:29 +00001278 delete MI; // The pseudo instruction is gone now.
1279 return BB;
1280 }
Evan Cheng339edad2006-01-11 00:33:36 +00001281
Evan Cheng911c68d2006-01-16 21:21:29 +00001282 case X86::FP_TO_INT16_IN_MEM:
1283 case X86::FP_TO_INT32_IN_MEM:
1284 case X86::FP_TO_INT64_IN_MEM: {
1285 // Change the floating point control register to use "round towards zero"
1286 // mode when truncating to an integer value.
1287 MachineFunction *F = BB->getParent();
1288 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1289 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1290
1291 // Load the old value of the high byte of the control word...
1292 unsigned OldCW =
1293 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1294 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1295
1296 // Set the high part to be round to zero...
1297 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1298
1299 // Reload the modified control word now...
1300 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1301
1302 // Restore the memory image of control word to original value
1303 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1304
1305 // Get the X86 opcode to use.
1306 unsigned Opc;
1307 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001308 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001309 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1310 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1311 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1312 }
1313
1314 X86AddressMode AM;
1315 MachineOperand &Op = MI->getOperand(0);
1316 if (Op.isRegister()) {
1317 AM.BaseType = X86AddressMode::RegBase;
1318 AM.Base.Reg = Op.getReg();
1319 } else {
1320 AM.BaseType = X86AddressMode::FrameIndexBase;
1321 AM.Base.FrameIndex = Op.getFrameIndex();
1322 }
1323 Op = MI->getOperand(1);
1324 if (Op.isImmediate())
1325 AM.Scale = Op.getImmedValue();
1326 Op = MI->getOperand(2);
1327 if (Op.isImmediate())
1328 AM.IndexReg = Op.getImmedValue();
1329 Op = MI->getOperand(3);
1330 if (Op.isGlobalAddress()) {
1331 AM.GV = Op.getGlobal();
1332 } else {
1333 AM.Disp = Op.getImmedValue();
1334 }
1335 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1336
1337 // Reload the original control word now.
1338 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1339
1340 delete MI; // The pseudo instruction is gone now.
1341 return BB;
1342 }
1343 }
Evan Cheng339edad2006-01-11 00:33:36 +00001344}
1345
1346
1347//===----------------------------------------------------------------------===//
1348// X86 Custom Lowering Hooks
1349//===----------------------------------------------------------------------===//
1350
Chris Lattner76ac0682005-11-15 00:40:23 +00001351/// LowerOperation - Provide custom lowering hooks for some operations.
1352///
1353SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1354 switch (Op.getOpcode()) {
1355 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001356 case ISD::ADD_PARTS:
1357 case ISD::SUB_PARTS: {
1358 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1359 "Not an i64 add/sub!");
1360 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1361 std::vector<MVT::ValueType> Tys;
1362 Tys.push_back(MVT::i32);
1363 Tys.push_back(MVT::Flag);
1364 std::vector<SDOperand> Ops;
1365 Ops.push_back(Op.getOperand(0));
1366 Ops.push_back(Op.getOperand(2));
1367 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1368 Tys, Ops);
1369 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1370 Op.getOperand(1), Op.getOperand(3),
1371 Lo.getValue(1));
1372 Tys.clear();
1373 Tys.push_back(MVT::i32);
1374 Tys.push_back(MVT::i32);
1375 Ops.clear();
1376 Ops.push_back(Lo);
1377 Ops.push_back(Hi);
1378 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1379 }
1380 case ISD::SHL_PARTS:
1381 case ISD::SRA_PARTS:
1382 case ISD::SRL_PARTS: {
1383 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1384 "Not an i64 shift!");
1385 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1386 SDOperand ShOpLo = Op.getOperand(0);
1387 SDOperand ShOpHi = Op.getOperand(1);
1388 SDOperand ShAmt = Op.getOperand(2);
1389 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001390 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001391 : DAG.getConstant(0, MVT::i32);
1392
1393 SDOperand Tmp2, Tmp3;
1394 if (Op.getOpcode() == ISD::SHL_PARTS) {
1395 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1396 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1397 } else {
1398 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001399 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001400 }
1401
1402 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1403 ShAmt, DAG.getConstant(32, MVT::i8));
1404
1405 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001406 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001407
1408 std::vector<MVT::ValueType> Tys;
1409 Tys.push_back(MVT::i32);
1410 Tys.push_back(MVT::Flag);
1411 std::vector<SDOperand> Ops;
1412 if (Op.getOpcode() == ISD::SHL_PARTS) {
1413 Ops.push_back(Tmp2);
1414 Ops.push_back(Tmp3);
1415 Ops.push_back(CC);
1416 Ops.push_back(InFlag);
1417 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1418 InFlag = Hi.getValue(1);
1419
1420 Ops.clear();
1421 Ops.push_back(Tmp3);
1422 Ops.push_back(Tmp1);
1423 Ops.push_back(CC);
1424 Ops.push_back(InFlag);
1425 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1426 } else {
1427 Ops.push_back(Tmp2);
1428 Ops.push_back(Tmp3);
1429 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001430 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001431 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1432 InFlag = Lo.getValue(1);
1433
1434 Ops.clear();
1435 Ops.push_back(Tmp3);
1436 Ops.push_back(Tmp1);
1437 Ops.push_back(CC);
1438 Ops.push_back(InFlag);
1439 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1440 }
1441
1442 Tys.clear();
1443 Tys.push_back(MVT::i32);
1444 Tys.push_back(MVT::i32);
1445 Ops.clear();
1446 Ops.push_back(Lo);
1447 Ops.push_back(Hi);
1448 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1449 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001450 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001451 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001452 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001453 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001454
1455 SDOperand Result;
1456 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1457 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001458 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001459 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001460 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001461 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1462 DAG.getEntryNode(), Op.getOperand(0),
1463 StackSlot, DAG.getSrcValue(NULL));
1464
1465 // Build the FILD
1466 std::vector<MVT::ValueType> Tys;
1467 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001468 Tys.push_back(MVT::Other);
Evan Cheng6305e502006-01-12 22:54:21 +00001469 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001470 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001471 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001472 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001473 Ops.push_back(DAG.getValueType(SrcVT));
1474 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001475
1476 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001477 Chain = Result.getValue(1);
1478 SDOperand InFlag = Result.getValue(2);
1479
1480 // FIXME: Currently the FST is flagged to the FILD. This
1481 // shouldn't be necessary except that RFP cannot be live across
1482 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1483 MachineFunction &MF = DAG.getMachineFunction();
1484 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1485 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1486 std::vector<MVT::ValueType> Tys;
1487 Tys.push_back(MVT::Other);
1488 std::vector<SDOperand> Ops;
1489 Ops.push_back(Chain);
1490 Ops.push_back(Result);
1491 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001492 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001493 Ops.push_back(InFlag);
1494 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1495 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1496 DAG.getSrcValue(NULL));
1497 }
1498
Evan Cheng6305e502006-01-12 22:54:21 +00001499 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001500 }
1501 case ISD::FP_TO_SINT: {
1502 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001503 "Unknown FP_TO_SINT to lower!");
1504 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1505 // stack slot.
1506 MachineFunction &MF = DAG.getMachineFunction();
1507 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1508 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1509 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1510
1511 unsigned Opc;
1512 switch (Op.getValueType()) {
1513 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1514 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1515 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1516 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1517 }
1518
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001519 SDOperand Chain = DAG.getEntryNode();
1520 SDOperand Value = Op.getOperand(0);
1521 if (X86ScalarSSE) {
1522 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1523 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1524 DAG.getSrcValue(0));
1525 std::vector<MVT::ValueType> Tys;
1526 Tys.push_back(MVT::f64);
1527 Tys.push_back(MVT::Other);
1528 std::vector<SDOperand> Ops;
1529 Ops.push_back(Chain);
1530 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001531 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001532 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1533 Chain = Value.getValue(1);
1534 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1535 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1536 }
1537
Chris Lattner76ac0682005-11-15 00:40:23 +00001538 // Build the FP_TO_INT*_IN_MEM
1539 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001540 Ops.push_back(Chain);
1541 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001542 Ops.push_back(StackSlot);
1543 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1544
1545 // Load the result.
1546 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1547 DAG.getSrcValue(NULL));
1548 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001549 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001550 std::vector<MVT::ValueType> Tys;
1551 Tys.push_back(MVT::Other);
1552 Tys.push_back(MVT::Flag);
1553 std::vector<SDOperand> Ops;
1554 Ops.push_back(Op.getOperand(0));
1555 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001556 Ops.clear();
1557 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1558 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1559 MVT::i32, Ops[0].getValue(2)));
1560 Ops.push_back(Ops[1].getValue(1));
1561 Tys[0] = Tys[1] = MVT::i32;
1562 Tys.push_back(MVT::Other);
1563 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001564 }
Evan Chengc1583db2005-12-21 20:21:51 +00001565 case ISD::SETCC: {
1566 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001567 SDOperand Cond;
1568 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001569 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1570 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001571 bool Flip;
1572 unsigned X86CC;
1573 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1574 if (Flip)
1575 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1576 Op.getOperand(1), Op.getOperand(0));
1577 else
1578 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1579 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001580 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1581 DAG.getConstant(X86CC, MVT::i8), Cond);
1582 } else {
1583 assert(isFP && "Illegal integer SetCC!");
1584
Evan Cheng45df7f82006-01-30 23:41:35 +00001585 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1586 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001587 std::vector<MVT::ValueType> Tys;
1588 std::vector<SDOperand> Ops;
1589 switch (SetCCOpcode) {
1590 default: assert(false && "Illegal floating point SetCC!");
1591 case ISD::SETOEQ: { // !PF & ZF
1592 Tys.push_back(MVT::i8);
1593 Tys.push_back(MVT::Flag);
1594 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1595 Ops.push_back(Cond);
1596 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1597 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1598 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1599 Tmp1.getValue(1));
1600 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1601 }
Evan Cheng172fce72006-01-06 00:43:03 +00001602 case ISD::SETUNE: { // PF | !ZF
1603 Tys.push_back(MVT::i8);
1604 Tys.push_back(MVT::Flag);
1605 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1606 Ops.push_back(Cond);
1607 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1608 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1609 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1610 Tmp1.getValue(1));
1611 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1612 }
1613 }
1614 }
Evan Chengc1583db2005-12-21 20:21:51 +00001615 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001616 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001617 MVT::ValueType VT = Op.getValueType();
1618 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001619 bool isFPStack = isFP && !X86ScalarSSE;
1620 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001621 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001622 SDOperand Op0 = Op.getOperand(0);
1623 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001624 if (Op0.getOpcode() == ISD::SETCC)
1625 Op0 = LowerOperation(Op0, DAG);
1626
Evan Cheng73a1ad92006-01-10 20:26:56 +00001627 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001628 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1629 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1630 // have another use it will be eliminated.
1631 // If the X86ISD::SETCC has more than one use, then it's probably better
1632 // to use a test instead of duplicating the X86ISD::CMP (for register
1633 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001634 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1635 if (!Op0.hasOneUse()) {
1636 std::vector<MVT::ValueType> Tys;
1637 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1638 Tys.push_back(Op0.Val->getValueType(i));
1639 std::vector<SDOperand> Ops;
1640 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1641 Ops.push_back(Op0.getOperand(i));
1642 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1643 }
1644
Evan Chengfb22e862006-01-13 01:03:02 +00001645 CC = Op0.getOperand(0);
1646 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001647 // Make a copy as flag result cannot be used by more than one.
1648 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1649 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001650 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001651 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001652 } else
1653 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001654 } else
1655 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001656
Evan Cheng731423f2006-01-13 01:06:49 +00001657 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001658 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001659 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001660 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001661
1662 std::vector<MVT::ValueType> Tys;
1663 Tys.push_back(Op.getValueType());
1664 Tys.push_back(MVT::Flag);
1665 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001666 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1667 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001668 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001669 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001670 Ops.push_back(CC);
1671 Ops.push_back(Cond);
1672 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001673 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001674 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001675 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001676 SDOperand Cond = Op.getOperand(1);
1677 SDOperand Dest = Op.getOperand(2);
1678 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001679 if (Cond.getOpcode() == ISD::SETCC)
1680 Cond = LowerOperation(Cond, DAG);
1681
Evan Chengc1583db2005-12-21 20:21:51 +00001682 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001683 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1684 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1685 // have another use it will be eliminated.
1686 // If the X86ISD::SETCC has more than one use, then it's probably better
1687 // to use a test instead of duplicating the X86ISD::CMP (for register
1688 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001689 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1690 if (!Cond.hasOneUse()) {
1691 std::vector<MVT::ValueType> Tys;
1692 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1693 Tys.push_back(Cond.Val->getValueType(i));
1694 std::vector<SDOperand> Ops;
1695 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1696 Ops.push_back(Cond.getOperand(i));
1697 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1698 }
1699
Evan Chengfb22e862006-01-13 01:03:02 +00001700 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001701 Cond = Cond.getOperand(1);
1702 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001703 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001704 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001705 } else
1706 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001707 } else
1708 addTest = true;
1709
1710 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001711 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001712 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1713 }
1714 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1715 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1716 }
Evan Chengae986f12006-01-11 22:15:48 +00001717 case ISD::MEMSET: {
1718 SDOperand InFlag;
1719 SDOperand Chain = Op.getOperand(0);
1720 unsigned Align =
1721 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1722 if (Align == 0) Align = 1;
1723
1724 MVT::ValueType AVT;
1725 SDOperand Count;
1726 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1727 unsigned ValReg;
1728 unsigned Val = ValC->getValue() & 255;
1729
1730 // If the value is a constant, then we can potentially use larger sets.
1731 switch (Align & 3) {
1732 case 2: // WORD aligned
1733 AVT = MVT::i16;
1734 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1735 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1736 else
1737 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1738 DAG.getConstant(1, MVT::i8));
1739 Val = (Val << 8) | Val;
1740 ValReg = X86::AX;
1741 break;
1742 case 0: // DWORD aligned
1743 AVT = MVT::i32;
1744 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1745 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1746 else
1747 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1748 DAG.getConstant(2, MVT::i8));
1749 Val = (Val << 8) | Val;
1750 Val = (Val << 16) | Val;
1751 ValReg = X86::EAX;
1752 break;
1753 default: // Byte aligned
1754 AVT = MVT::i8;
1755 Count = Op.getOperand(3);
1756 ValReg = X86::AL;
1757 break;
1758 }
1759
1760 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1761 InFlag);
1762 InFlag = Chain.getValue(1);
1763 } else {
1764 AVT = MVT::i8;
1765 Count = Op.getOperand(3);
1766 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1767 InFlag = Chain.getValue(1);
1768 }
1769
1770 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1771 InFlag = Chain.getValue(1);
1772 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1773 InFlag = Chain.getValue(1);
1774
1775 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1776 DAG.getValueType(AVT), InFlag);
1777 }
1778 case ISD::MEMCPY: {
1779 SDOperand Chain = Op.getOperand(0);
1780 unsigned Align =
1781 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1782 if (Align == 0) Align = 1;
1783
1784 MVT::ValueType AVT;
1785 SDOperand Count;
1786 switch (Align & 3) {
1787 case 2: // WORD aligned
1788 AVT = MVT::i16;
1789 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1790 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1791 else
1792 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1793 DAG.getConstant(1, MVT::i8));
1794 break;
1795 case 0: // DWORD aligned
1796 AVT = MVT::i32;
1797 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1798 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1799 else
1800 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1801 DAG.getConstant(2, MVT::i8));
1802 break;
1803 default: // Byte aligned
1804 AVT = MVT::i8;
1805 Count = Op.getOperand(3);
1806 break;
1807 }
1808
1809 SDOperand InFlag;
1810 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1811 InFlag = Chain.getValue(1);
1812 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1813 InFlag = Chain.getValue(1);
1814 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1815 InFlag = Chain.getValue(1);
1816
1817 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1818 DAG.getValueType(AVT), InFlag);
1819 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001820 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001821 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001822 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1823 // For Darwin, external and weak symbols are indirect, so we want to load
1824 // the value at address GV, not the value of GV itself. This means that
1825 // the GlobalAddress must be in the base or index register of the address,
1826 // not the GV offset field.
1827 if (getTargetMachine().
1828 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1829 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001830 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1831 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1832 DAG.getSrcValue(NULL));
1833 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001834 }
Nate Begemane74795c2006-01-25 18:21:52 +00001835 case ISD::VASTART: {
1836 // vastart just stores the address of the VarArgsFrameIndex slot into the
1837 // memory location argument.
1838 // FIXME: Replace MVT::i32 with PointerTy
1839 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1840 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1841 Op.getOperand(1), Op.getOperand(2));
1842 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001843 case ISD::RET: {
1844 SDOperand Copy;
1845
1846 switch(Op.getNumOperands()) {
1847 default:
1848 assert(0 && "Do not know how to return this many arguments!");
1849 abort();
1850 case 1:
1851 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1852 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1853 case 2: {
1854 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1855 if (MVT::isInteger(ArgVT))
1856 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1857 SDOperand());
1858 else if (!X86ScalarSSE) {
1859 std::vector<MVT::ValueType> Tys;
1860 Tys.push_back(MVT::Other);
1861 Tys.push_back(MVT::Flag);
1862 std::vector<SDOperand> Ops;
1863 Ops.push_back(Op.getOperand(0));
1864 Ops.push_back(Op.getOperand(1));
1865 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1866 } else {
1867 // Spill the value to memory and reload it into top of stack.
1868 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1869 MachineFunction &MF = DAG.getMachineFunction();
1870 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1871 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1872 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1873 Op.getOperand(1), StackSlot,
1874 DAG.getSrcValue(0));
1875 std::vector<MVT::ValueType> Tys;
1876 Tys.push_back(MVT::f64);
1877 Tys.push_back(MVT::Other);
1878 std::vector<SDOperand> Ops;
1879 Ops.push_back(Chain);
1880 Ops.push_back(StackSlot);
1881 Ops.push_back(DAG.getValueType(ArgVT));
1882 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1883 Tys.clear();
1884 Tys.push_back(MVT::Other);
1885 Tys.push_back(MVT::Flag);
1886 Ops.clear();
1887 Ops.push_back(Copy.getValue(1));
1888 Ops.push_back(Copy);
1889 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1890 }
1891 break;
1892 }
1893 case 3:
1894 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1895 SDOperand());
1896 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1897 break;
1898 }
1899 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1900 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1901 Copy.getValue(1));
1902 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001903 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001904}
Evan Cheng6af02632005-12-20 06:22:03 +00001905
1906const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1907 switch (Opcode) {
1908 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001909 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1910 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1911 case X86ISD::ADC: return "X86ISD::ADC";
1912 case X86ISD::SBB: return "X86ISD::SBB";
1913 case X86ISD::SHLD: return "X86ISD::SHLD";
1914 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6305e502006-01-12 22:54:21 +00001915 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001916 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1917 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1918 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001919 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001920 case X86ISD::FST: return "X86ISD::FST";
1921 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001922 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001923 case X86ISD::CALL: return "X86ISD::CALL";
1924 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1925 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1926 case X86ISD::CMP: return "X86ISD::CMP";
1927 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001928 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001929 case X86ISD::CMOV: return "X86ISD::CMOV";
1930 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001931 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001932 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1933 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001934 }
1935}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001936
1937bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerf0b24d22006-01-30 04:09:27 +00001938 uint64_t Mask) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001939
1940 unsigned Opc = Op.getOpcode();
1941
1942 switch (Opc) {
1943 default:
1944 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1945 break;
1946 case X86ISD::SETCC: return (Mask & 1) == 0;
1947 }
1948
1949 return false;
1950}