blob: 6b67241d065e6b98a169ea51128869db960cd969 [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"
16#include "X86ISelLowering.h"
17#include "X86TargetMachine.h"
18#include "llvm/CallingConv.h"
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SSARegMap.h"
24#include "llvm/Target/TargetOptions.h"
25using namespace llvm;
26
27// FIXME: temporary.
28#include "llvm/Support/CommandLine.h"
29static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
30 cl::desc("Enable fastcc on X86"));
31
32X86TargetLowering::X86TargetLowering(TargetMachine &TM)
33 : TargetLowering(TM) {
Chris Lattner76ac0682005-11-15 00:40:23 +000034 // Set up the TargetLowering object.
35
36 // X86 is weird, it always uses i8 for shift amounts and setcc results.
37 setShiftAmountType(MVT::i8);
38 setSetCCResultType(MVT::i8);
39 setSetCCResultContents(ZeroOrOneSetCCResult);
40 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
41
42 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000043 addRegisterClass(MVT::i8, X86::R8RegisterClass);
44 addRegisterClass(MVT::i16, X86::R16RegisterClass);
45 addRegisterClass(MVT::i32, X86::R32RegisterClass);
46
47 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
48 // operation.
49 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
50 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
51 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
52 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
53
54 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
55 // this operation.
56 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
57 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
58
59 if (!X86ScalarSSE) {
60 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
61 // isn't legal.
62 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
63 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
64 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
65 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
66 }
67
68 // Handle FP_TO_UINT by promoting the destination to a larger signed
69 // conversion.
70 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
71 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
72 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
73
74 if (!X86ScalarSSE)
75 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
76
77 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
78 // this operation.
79 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
80 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
81 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
82
Chris Lattner30107e62005-12-23 05:15:23 +000083 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
84 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
85
Evan Cheng6fc31042005-12-19 23:12:38 +000086 if (X86DAGIsel) {
87 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
88 }
Chris Lattner76ac0682005-11-15 00:40:23 +000089 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
90 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
91 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
92 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +000093 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +000094 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
95 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
96 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
97 setOperationAction(ISD::FREM , MVT::f64 , Expand);
98 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
99 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
100 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
101 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
103 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
104 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
105 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
106 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000107 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000108
109 setOperationAction(ISD::READIO , MVT::i1 , Expand);
110 setOperationAction(ISD::READIO , MVT::i8 , Expand);
111 setOperationAction(ISD::READIO , MVT::i16 , Expand);
112 setOperationAction(ISD::READIO , MVT::i32 , Expand);
113 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
114 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
115 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
116 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
117
118 // These should be promoted to a larger select which is supported.
119 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
120 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng225a4d02005-12-17 01:21:05 +0000121 if (X86DAGIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000122 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000123 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
124 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000125 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
126 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000127 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
128 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
129 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000130 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
131 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
132 // X86 ret instruction may pop stack.
133 setOperationAction(ISD::RET , MVT::Other, Custom);
134 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000135 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000136 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
137 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
138 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
139 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
140 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
141 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000142 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000143
Chris Lattner9c415362005-11-29 06:16:21 +0000144 // We don't have line number support yet.
145 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000146 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
147 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000148
Chris Lattner76ac0682005-11-15 00:40:23 +0000149 if (X86ScalarSSE) {
150 // Set up the FP register classes.
151 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
152 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
153
154 // SSE has no load+extend ops
155 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
156 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
157
158 // SSE has no i16 to fp conversion, only i32
159 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
160 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
161
162 // Expand FP_TO_UINT into a select.
163 // FIXME: We would like to use a Custom expander here eventually to do
164 // the optimal thing for SSE vs. the default expansion in the legalizer.
165 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
166
167 // We don't support sin/cos/sqrt/fmod
168 setOperationAction(ISD::FSIN , MVT::f64, Expand);
169 setOperationAction(ISD::FCOS , MVT::f64, Expand);
170 setOperationAction(ISD::FABS , MVT::f64, Expand);
171 setOperationAction(ISD::FNEG , MVT::f64, Expand);
172 setOperationAction(ISD::FREM , MVT::f64, Expand);
173 setOperationAction(ISD::FSIN , MVT::f32, Expand);
174 setOperationAction(ISD::FCOS , MVT::f32, Expand);
175 setOperationAction(ISD::FABS , MVT::f32, Expand);
176 setOperationAction(ISD::FNEG , MVT::f32, Expand);
177 setOperationAction(ISD::FREM , MVT::f32, Expand);
178
179 addLegalFPImmediate(+0.0); // xorps / xorpd
180 } else {
181 // Set up the FP register classes.
182 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
183
184 if (!UnsafeFPMath) {
185 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
186 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
187 }
188
189 addLegalFPImmediate(+0.0); // FLD0
190 addLegalFPImmediate(+1.0); // FLD1
191 addLegalFPImmediate(-0.0); // FLD0/FCHS
192 addLegalFPImmediate(-1.0); // FLD1/FCHS
193 }
194 computeRegisterProperties();
195
196 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
197 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
198 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
199 allowUnalignedMemoryAccesses = true; // x86 supports it!
200}
201
202std::vector<SDOperand>
203X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
204 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
205 return LowerFastCCArguments(F, DAG);
206 return LowerCCCArguments(F, DAG);
207}
208
209std::pair<SDOperand, SDOperand>
210X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
211 bool isVarArg, unsigned CallingConv,
212 bool isTailCall,
213 SDOperand Callee, ArgListTy &Args,
214 SelectionDAG &DAG) {
215 assert((!isVarArg || CallingConv == CallingConv::C) &&
216 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000217
218 // If the callee is a GlobalAddress node (quite common, every direct call is)
219 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
220 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
221 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
222
Chris Lattner76ac0682005-11-15 00:40:23 +0000223 if (CallingConv == CallingConv::Fast && EnableFastCC)
224 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
225 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
226}
227
Evan Chenga74ce622005-12-21 02:39:21 +0000228SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
229 SelectionDAG &DAG) {
230 if (!X86DAGIsel)
231 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
232
233 SDOperand Copy;
234 MVT::ValueType OpVT = Op.getValueType();
235 switch (OpVT) {
236 default: assert(0 && "Unknown type to return!");
237 case MVT::i32:
238 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
239 break;
240 case MVT::i64: {
241 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
242 DAG.getConstant(1, MVT::i32));
243 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
244 DAG.getConstant(0, MVT::i32));
Evan Cheng172fce72006-01-06 00:43:03 +0000245 Copy = DAG.getCopyToReg(Chain, X86::EDX, Hi, SDOperand());
246 Copy = DAG.getCopyToReg(Copy, X86::EAX, Lo, Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000247 break;
248 }
249 case MVT::f32:
Evan Chenga74ce622005-12-21 02:39:21 +0000250 case MVT::f64:
251 if (!X86ScalarSSE) {
Evan Cheng9c249c32006-01-09 18:33:28 +0000252 if (OpVT == MVT::f32)
253 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chenga74ce622005-12-21 02:39:21 +0000254 std::vector<MVT::ValueType> Tys;
255 Tys.push_back(MVT::Other);
256 Tys.push_back(MVT::Flag);
257 std::vector<SDOperand> Ops;
258 Ops.push_back(Chain);
259 Ops.push_back(Op);
260 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
261 } else {
262 // Spill the value to memory and reload it into top of stack.
263 unsigned Size = MVT::getSizeInBits(OpVT)/8;
264 MachineFunction &MF = DAG.getMachineFunction();
265 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
266 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
267 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
268 StackSlot, DAG.getSrcValue(NULL));
269 std::vector<MVT::ValueType> Tys;
270 Tys.push_back(MVT::f64);
271 Tys.push_back(MVT::Other);
272 std::vector<SDOperand> Ops;
273 Ops.push_back(Chain);
274 Ops.push_back(StackSlot);
275 Ops.push_back(DAG.getValueType(OpVT));
276 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
277 Tys.clear();
278 Tys.push_back(MVT::Other);
279 Tys.push_back(MVT::Flag);
280 Ops.clear();
281 Ops.push_back(Copy.getValue(1));
282 Ops.push_back(Copy);
283 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
284 }
285 break;
286 }
Evan Chengc1583db2005-12-21 20:21:51 +0000287
288 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
289 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
290 Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000291}
292
Chris Lattner76ac0682005-11-15 00:40:23 +0000293//===----------------------------------------------------------------------===//
294// C Calling Convention implementation
295//===----------------------------------------------------------------------===//
296
297std::vector<SDOperand>
298X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
299 std::vector<SDOperand> ArgValues;
300
301 MachineFunction &MF = DAG.getMachineFunction();
302 MachineFrameInfo *MFI = MF.getFrameInfo();
303
304 // Add DAG nodes to load the arguments... On entry to a function on the X86,
305 // the stack frame looks like this:
306 //
307 // [ESP] -- return address
308 // [ESP + 4] -- first argument (leftmost lexically)
309 // [ESP + 8] -- second argument, if first argument is four bytes in size
310 // ...
311 //
312 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
313 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
314 MVT::ValueType ObjectVT = getValueType(I->getType());
315 unsigned ArgIncrement = 4;
316 unsigned ObjSize;
317 switch (ObjectVT) {
318 default: assert(0 && "Unhandled argument type!");
319 case MVT::i1:
320 case MVT::i8: ObjSize = 1; break;
321 case MVT::i16: ObjSize = 2; break;
322 case MVT::i32: ObjSize = 4; break;
323 case MVT::i64: ObjSize = ArgIncrement = 8; break;
324 case MVT::f32: ObjSize = 4; break;
325 case MVT::f64: ObjSize = ArgIncrement = 8; break;
326 }
327 // Create the frame index object for this incoming parameter...
328 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
329
330 // Create the SelectionDAG nodes corresponding to a load from this parameter
331 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
332
333 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
334 // dead loads.
335 SDOperand ArgValue;
336 if (!I->use_empty())
337 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
338 DAG.getSrcValue(NULL));
339 else {
340 if (MVT::isInteger(ObjectVT))
341 ArgValue = DAG.getConstant(0, ObjectVT);
342 else
343 ArgValue = DAG.getConstantFP(0, ObjectVT);
344 }
345 ArgValues.push_back(ArgValue);
346
347 ArgOffset += ArgIncrement; // Move on to the next argument...
348 }
349
350 // If the function takes variable number of arguments, make a frame index for
351 // the start of the first vararg value... for expansion of llvm.va_start.
352 if (F.isVarArg())
353 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
354 ReturnAddrIndex = 0; // No return address slot generated yet.
355 BytesToPopOnReturn = 0; // Callee pops nothing.
356 BytesCallerReserves = ArgOffset;
357
358 // Finally, inform the code generator which regs we return values in.
359 switch (getValueType(F.getReturnType())) {
360 default: assert(0 && "Unknown type!");
361 case MVT::isVoid: break;
362 case MVT::i1:
363 case MVT::i8:
364 case MVT::i16:
365 case MVT::i32:
366 MF.addLiveOut(X86::EAX);
367 break;
368 case MVT::i64:
369 MF.addLiveOut(X86::EAX);
370 MF.addLiveOut(X86::EDX);
371 break;
372 case MVT::f32:
373 case MVT::f64:
374 MF.addLiveOut(X86::ST0);
375 break;
376 }
377 return ArgValues;
378}
379
380std::pair<SDOperand, SDOperand>
381X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
382 bool isVarArg, bool isTailCall,
383 SDOperand Callee, ArgListTy &Args,
384 SelectionDAG &DAG) {
385 // Count how many bytes are to be pushed on the stack.
386 unsigned NumBytes = 0;
387
388 if (Args.empty()) {
389 // Save zero bytes.
390 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
391 DAG.getConstant(0, getPointerTy()));
392 } else {
393 for (unsigned i = 0, e = Args.size(); i != e; ++i)
394 switch (getValueType(Args[i].second)) {
395 default: assert(0 && "Unknown value type!");
396 case MVT::i1:
397 case MVT::i8:
398 case MVT::i16:
399 case MVT::i32:
400 case MVT::f32:
401 NumBytes += 4;
402 break;
403 case MVT::i64:
404 case MVT::f64:
405 NumBytes += 8;
406 break;
407 }
408
409 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
410 DAG.getConstant(NumBytes, getPointerTy()));
411
412 // Arguments go on the stack in reverse order, as specified by the ABI.
413 unsigned ArgOffset = 0;
414 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
415 X86::ESP, MVT::i32);
416 std::vector<SDOperand> Stores;
417
418 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
419 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
420 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
421
422 switch (getValueType(Args[i].second)) {
423 default: assert(0 && "Unexpected ValueType for argument!");
424 case MVT::i1:
425 case MVT::i8:
426 case MVT::i16:
427 // Promote the integer to 32 bits. If the input type is signed use a
428 // sign extend, otherwise use a zero extend.
429 if (Args[i].second->isSigned())
430 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
431 else
432 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
433
434 // FALL THROUGH
435 case MVT::i32:
436 case MVT::f32:
437 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
438 Args[i].first, PtrOff,
439 DAG.getSrcValue(NULL)));
440 ArgOffset += 4;
441 break;
442 case MVT::i64:
443 case MVT::f64:
444 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
445 Args[i].first, PtrOff,
446 DAG.getSrcValue(NULL)));
447 ArgOffset += 8;
448 break;
449 }
450 }
451 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
452 }
453
454 std::vector<MVT::ValueType> RetVals;
455 MVT::ValueType RetTyVT = getValueType(RetTy);
456 RetVals.push_back(MVT::Other);
457
458 // The result values produced have to be legal. Promote the result.
459 switch (RetTyVT) {
460 case MVT::isVoid: break;
461 default:
462 RetVals.push_back(RetTyVT);
463 break;
464 case MVT::i1:
465 case MVT::i8:
466 case MVT::i16:
467 RetVals.push_back(MVT::i32);
468 break;
469 case MVT::f32:
470 if (X86ScalarSSE)
471 RetVals.push_back(MVT::f32);
472 else
473 RetVals.push_back(MVT::f64);
474 break;
475 case MVT::i64:
476 RetVals.push_back(MVT::i32);
477 RetVals.push_back(MVT::i32);
478 break;
479 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000480
Evan Cheng45e190982006-01-05 00:27:02 +0000481 if (X86DAGIsel) {
482 std::vector<MVT::ValueType> NodeTys;
483 NodeTys.push_back(MVT::Other); // Returns a chain
484 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000485 std::vector<SDOperand> Ops;
486 Ops.push_back(Chain);
487 Ops.push_back(Callee);
488
Evan Cheng172fce72006-01-06 00:43:03 +0000489 // FIXME: Do not generate X86ISD::TAILCALL for now.
490 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000491 SDOperand InFlag = Chain.getValue(1);
492
493 SDOperand RetVal;
494 if (RetTyVT != MVT::isVoid) {
495 switch (RetTyVT) {
496 default: assert(0 && "Unknown value type to return!");
497 case MVT::i1:
498 case MVT::i8:
499 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
500 Chain = RetVal.getValue(1);
501 break;
502 case MVT::i16:
503 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
504 Chain = RetVal.getValue(1);
505 break;
506 case MVT::i32:
507 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
508 Chain = RetVal.getValue(1);
509 break;
510 case MVT::i64: {
511 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
512 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
513 Lo.getValue(2));
514 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
515 Chain = Hi.getValue(1);
516 break;
517 }
518 case MVT::f32:
519 case MVT::f64: {
520 std::vector<MVT::ValueType> Tys;
521 Tys.push_back(MVT::f64);
522 Tys.push_back(MVT::Other);
523 std::vector<SDOperand> Ops;
524 Ops.push_back(Chain);
525 Ops.push_back(InFlag);
526 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
527 Chain = RetVal.getValue(1);
528 if (X86ScalarSSE) {
529 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
530 MachineFunction &MF = DAG.getMachineFunction();
531 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
532 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
533 Tys.clear();
534 Tys.push_back(MVT::Other);
535 Ops.clear();
536 Ops.push_back(Chain);
537 Ops.push_back(RetVal);
538 Ops.push_back(StackSlot);
539 Ops.push_back(DAG.getValueType(RetTyVT));
540 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
541 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
542 DAG.getSrcValue(NULL));
543 Chain = RetVal.getValue(1);
544 } else if (RetTyVT == MVT::f32)
545 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
546 break;
547 }
548 }
549 }
550
551 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
552 DAG.getConstant(NumBytes, getPointerTy()),
553 DAG.getConstant(0, getPointerTy()));
554 return std::make_pair(RetVal, Chain);
555 } else {
556 std::vector<SDOperand> Ops;
557 Ops.push_back(Chain);
558 Ops.push_back(Callee);
559 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
560 Ops.push_back(DAG.getConstant(0, getPointerTy()));
561
562 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
563 RetVals, Ops);
564
565 SDOperand ResultVal;
566 switch (RetTyVT) {
567 case MVT::isVoid: break;
568 default:
569 ResultVal = TheCall.getValue(1);
570 break;
571 case MVT::i1:
572 case MVT::i8:
573 case MVT::i16:
574 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
575 break;
576 case MVT::f32:
577 // FIXME: we would really like to remember that this FP_ROUND operation is
578 // okay to eliminate if we allow excess FP precision.
579 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
580 break;
581 case MVT::i64:
582 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
583 TheCall.getValue(2));
584 break;
585 }
586
587 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
588 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000589 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000590}
591
592SDOperand
593X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
594 Value *VAListV, SelectionDAG &DAG) {
595 // vastart just stores the address of the VarArgsFrameIndex slot.
596 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
597 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
598 DAG.getSrcValue(VAListV));
599}
600
601
602std::pair<SDOperand,SDOperand>
603X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
604 Value *VAListV, const Type *ArgTy,
605 SelectionDAG &DAG) {
606 MVT::ValueType ArgVT = getValueType(ArgTy);
607 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
608 VAListP, DAG.getSrcValue(VAListV));
609 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
610 DAG.getSrcValue(NULL));
611 unsigned Amt;
612 if (ArgVT == MVT::i32)
613 Amt = 4;
614 else {
615 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
616 "Other types should have been promoted for varargs!");
617 Amt = 8;
618 }
619 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
620 DAG.getConstant(Amt, Val.getValueType()));
621 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
622 Val, VAListP, DAG.getSrcValue(VAListV));
623 return std::make_pair(Result, Chain);
624}
625
626//===----------------------------------------------------------------------===//
627// Fast Calling Convention implementation
628//===----------------------------------------------------------------------===//
629//
630// The X86 'fast' calling convention passes up to two integer arguments in
631// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
632// and requires that the callee pop its arguments off the stack (allowing proper
633// tail calls), and has the same return value conventions as C calling convs.
634//
635// This calling convention always arranges for the callee pop value to be 8n+4
636// bytes, which is needed for tail recursion elimination and stack alignment
637// reasons.
638//
639// Note that this can be enhanced in the future to pass fp vals in registers
640// (when we have a global fp allocator) and do other tricks.
641//
642
643/// AddLiveIn - This helper function adds the specified physical register to the
644/// MachineFunction as a live in value. It also creates a corresponding virtual
645/// register for it.
646static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
647 TargetRegisterClass *RC) {
648 assert(RC->contains(PReg) && "Not the correct regclass!");
649 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
650 MF.addLiveIn(PReg, VReg);
651 return VReg;
652}
653
654
655std::vector<SDOperand>
656X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
657 std::vector<SDOperand> ArgValues;
658
659 MachineFunction &MF = DAG.getMachineFunction();
660 MachineFrameInfo *MFI = MF.getFrameInfo();
661
662 // Add DAG nodes to load the arguments... On entry to a function the stack
663 // frame looks like this:
664 //
665 // [ESP] -- return address
666 // [ESP + 4] -- first nonreg argument (leftmost lexically)
667 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
668 // ...
669 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
670
671 // Keep track of the number of integer regs passed so far. This can be either
672 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
673 // used).
674 unsigned NumIntRegs = 0;
675
676 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
677 MVT::ValueType ObjectVT = getValueType(I->getType());
678 unsigned ArgIncrement = 4;
679 unsigned ObjSize = 0;
680 SDOperand ArgValue;
681
682 switch (ObjectVT) {
683 default: assert(0 && "Unhandled argument type!");
684 case MVT::i1:
685 case MVT::i8:
686 if (NumIntRegs < 2) {
687 if (!I->use_empty()) {
688 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
689 X86::R8RegisterClass);
690 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
691 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000692 if (ObjectVT == MVT::i1)
693 // FIXME: Should insert a assertzext here.
694 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000695 }
696 ++NumIntRegs;
697 break;
698 }
699
700 ObjSize = 1;
701 break;
702 case MVT::i16:
703 if (NumIntRegs < 2) {
704 if (!I->use_empty()) {
705 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
706 X86::R16RegisterClass);
707 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
708 DAG.setRoot(ArgValue.getValue(1));
709 }
710 ++NumIntRegs;
711 break;
712 }
713 ObjSize = 2;
714 break;
715 case MVT::i32:
716 if (NumIntRegs < 2) {
717 if (!I->use_empty()) {
718 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
719 X86::R32RegisterClass);
720 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
721 DAG.setRoot(ArgValue.getValue(1));
722 }
723 ++NumIntRegs;
724 break;
725 }
726 ObjSize = 4;
727 break;
728 case MVT::i64:
729 if (NumIntRegs == 0) {
730 if (!I->use_empty()) {
731 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
732 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
733
734 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
735 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
736 DAG.setRoot(Hi.getValue(1));
737
738 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
739 }
740 NumIntRegs = 2;
741 break;
742 } else if (NumIntRegs == 1) {
743 if (!I->use_empty()) {
744 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
745 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
746 DAG.setRoot(Low.getValue(1));
747
748 // Load the high part from memory.
749 // Create the frame index object for this incoming parameter...
750 int FI = MFI->CreateFixedObject(4, ArgOffset);
751 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
752 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
753 DAG.getSrcValue(NULL));
754 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
755 }
756 ArgOffset += 4;
757 NumIntRegs = 2;
758 break;
759 }
760 ObjSize = ArgIncrement = 8;
761 break;
762 case MVT::f32: ObjSize = 4; break;
763 case MVT::f64: ObjSize = ArgIncrement = 8; break;
764 }
765
766 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
767 // dead loads.
768 if (ObjSize && !I->use_empty()) {
769 // Create the frame index object for this incoming parameter...
770 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
771
772 // Create the SelectionDAG nodes corresponding to a load from this
773 // parameter.
774 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
775
776 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
777 DAG.getSrcValue(NULL));
778 } else if (ArgValue.Val == 0) {
779 if (MVT::isInteger(ObjectVT))
780 ArgValue = DAG.getConstant(0, ObjectVT);
781 else
782 ArgValue = DAG.getConstantFP(0, ObjectVT);
783 }
784 ArgValues.push_back(ArgValue);
785
786 if (ObjSize)
787 ArgOffset += ArgIncrement; // Move on to the next argument.
788 }
789
790 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
791 // arguments and the arguments after the retaddr has been pushed are aligned.
792 if ((ArgOffset & 7) == 0)
793 ArgOffset += 4;
794
795 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
796 ReturnAddrIndex = 0; // No return address slot generated yet.
797 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
798 BytesCallerReserves = 0;
799
800 // Finally, inform the code generator which regs we return values in.
801 switch (getValueType(F.getReturnType())) {
802 default: assert(0 && "Unknown type!");
803 case MVT::isVoid: break;
804 case MVT::i1:
805 case MVT::i8:
806 case MVT::i16:
807 case MVT::i32:
808 MF.addLiveOut(X86::EAX);
809 break;
810 case MVT::i64:
811 MF.addLiveOut(X86::EAX);
812 MF.addLiveOut(X86::EDX);
813 break;
814 case MVT::f32:
815 case MVT::f64:
816 MF.addLiveOut(X86::ST0);
817 break;
818 }
819 return ArgValues;
820}
821
822std::pair<SDOperand, SDOperand>
823X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
824 bool isTailCall, SDOperand Callee,
825 ArgListTy &Args, SelectionDAG &DAG) {
826 // Count how many bytes are to be pushed on the stack.
827 unsigned NumBytes = 0;
828
829 // Keep track of the number of integer regs passed so far. This can be either
830 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
831 // used).
832 unsigned NumIntRegs = 0;
833
834 for (unsigned i = 0, e = Args.size(); i != e; ++i)
835 switch (getValueType(Args[i].second)) {
836 default: assert(0 && "Unknown value type!");
837 case MVT::i1:
838 case MVT::i8:
839 case MVT::i16:
840 case MVT::i32:
841 if (NumIntRegs < 2) {
842 ++NumIntRegs;
843 break;
844 }
845 // fall through
846 case MVT::f32:
847 NumBytes += 4;
848 break;
849 case MVT::i64:
850 if (NumIntRegs == 0) {
851 NumIntRegs = 2;
852 break;
853 } else if (NumIntRegs == 1) {
854 NumIntRegs = 2;
855 NumBytes += 4;
856 break;
857 }
858
859 // fall through
860 case MVT::f64:
861 NumBytes += 8;
862 break;
863 }
864
865 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
866 // arguments and the arguments after the retaddr has been pushed are aligned.
867 if ((NumBytes & 7) == 0)
868 NumBytes += 4;
869
870 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
871 DAG.getConstant(NumBytes, getPointerTy()));
872
873 // Arguments go on the stack in reverse order, as specified by the ABI.
874 unsigned ArgOffset = 0;
875 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
876 X86::ESP, MVT::i32);
877 NumIntRegs = 0;
878 std::vector<SDOperand> Stores;
879 std::vector<SDOperand> RegValuesToPass;
880 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
881 switch (getValueType(Args[i].second)) {
882 default: assert(0 && "Unexpected ValueType for argument!");
883 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000884 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
885 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000886 case MVT::i8:
887 case MVT::i16:
888 case MVT::i32:
889 if (NumIntRegs < 2) {
890 RegValuesToPass.push_back(Args[i].first);
891 ++NumIntRegs;
892 break;
893 }
894 // Fall through
895 case MVT::f32: {
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 += 4;
902 break;
903 }
904 case MVT::i64:
905 if (NumIntRegs < 2) { // Can pass part of it in regs?
906 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
907 Args[i].first, DAG.getConstant(1, MVT::i32));
908 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
909 Args[i].first, DAG.getConstant(0, MVT::i32));
910 RegValuesToPass.push_back(Lo);
911 ++NumIntRegs;
912 if (NumIntRegs < 2) { // Pass both parts in regs?
913 RegValuesToPass.push_back(Hi);
914 ++NumIntRegs;
915 } else {
916 // Pass the high part in memory.
917 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
918 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
919 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
920 Hi, PtrOff, DAG.getSrcValue(NULL)));
921 ArgOffset += 4;
922 }
923 break;
924 }
925 // Fall through
926 case MVT::f64:
927 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
928 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
929 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
930 Args[i].first, PtrOff,
931 DAG.getSrcValue(NULL)));
932 ArgOffset += 8;
933 break;
934 }
935 }
936 if (!Stores.empty())
937 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
938
939 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
940 // arguments and the arguments after the retaddr has been pushed are aligned.
941 if ((ArgOffset & 7) == 0)
942 ArgOffset += 4;
943
944 std::vector<MVT::ValueType> RetVals;
945 MVT::ValueType RetTyVT = getValueType(RetTy);
946
947 RetVals.push_back(MVT::Other);
948
949 // The result values produced have to be legal. Promote the result.
950 switch (RetTyVT) {
951 case MVT::isVoid: break;
952 default:
953 RetVals.push_back(RetTyVT);
954 break;
955 case MVT::i1:
956 case MVT::i8:
957 case MVT::i16:
958 RetVals.push_back(MVT::i32);
959 break;
960 case MVT::f32:
961 if (X86ScalarSSE)
962 RetVals.push_back(MVT::f32);
963 else
964 RetVals.push_back(MVT::f64);
965 break;
966 case MVT::i64:
967 RetVals.push_back(MVT::i32);
968 RetVals.push_back(MVT::i32);
969 break;
970 }
971
Evan Cheng172fce72006-01-06 00:43:03 +0000972 if (X86DAGIsel) {
973 // Build a sequence of copy-to-reg nodes chained together with token chain
974 // and flag operands which copy the outgoing args into registers.
975 SDOperand InFlag;
976 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
977 unsigned CCReg;
978 SDOperand RegToPass = RegValuesToPass[i];
979 switch (RegToPass.getValueType()) {
980 default: assert(0 && "Bad thing to pass in regs");
981 case MVT::i8:
982 CCReg = (i == 0) ? X86::AL : X86::DL;
983 break;
984 case MVT::i16:
985 CCReg = (i == 0) ? X86::AX : X86::DX;
986 break;
987 case MVT::i32:
988 CCReg = (i == 0) ? X86::EAX : X86::EDX;
989 break;
990 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000991
Evan Cheng172fce72006-01-06 00:43:03 +0000992 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
993 InFlag = Chain.getValue(1);
994 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000995
Evan Cheng172fce72006-01-06 00:43:03 +0000996 std::vector<MVT::ValueType> NodeTys;
997 NodeTys.push_back(MVT::Other); // Returns a chain
998 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +0000999 std::vector<SDOperand> Ops;
1000 Ops.push_back(Chain);
1001 Ops.push_back(Callee);
1002 if (InFlag.Val)
1003 Ops.push_back(InFlag);
1004
1005 // FIXME: Do not generate X86ISD::TAILCALL for now.
1006 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1007 InFlag = Chain.getValue(1);
1008
1009 SDOperand RetVal;
1010 if (RetTyVT != MVT::isVoid) {
1011 switch (RetTyVT) {
1012 default: assert(0 && "Unknown value type to return!");
1013 case MVT::i1:
1014 case MVT::i8:
1015 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1016 Chain = RetVal.getValue(1);
1017 break;
1018 case MVT::i16:
1019 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1020 Chain = RetVal.getValue(1);
1021 break;
1022 case MVT::i32:
1023 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1024 Chain = RetVal.getValue(1);
1025 break;
1026 case MVT::i64: {
1027 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1028 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1029 Lo.getValue(2));
1030 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1031 Chain = Hi.getValue(1);
1032 break;
1033 }
1034 case MVT::f32:
1035 case MVT::f64: {
1036 std::vector<MVT::ValueType> Tys;
1037 Tys.push_back(MVT::f64);
1038 Tys.push_back(MVT::Other);
1039 std::vector<SDOperand> Ops;
1040 Ops.push_back(Chain);
1041 Ops.push_back(InFlag);
1042 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1043 Chain = RetVal.getValue(1);
1044 if (X86ScalarSSE) {
1045 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1046 MachineFunction &MF = DAG.getMachineFunction();
1047 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1048 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1049 Tys.clear();
1050 Tys.push_back(MVT::Other);
1051 Ops.clear();
1052 Ops.push_back(Chain);
1053 Ops.push_back(RetVal);
1054 Ops.push_back(StackSlot);
1055 Ops.push_back(DAG.getValueType(RetTyVT));
1056 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1057 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1058 DAG.getSrcValue(NULL));
1059 Chain = RetVal.getValue(1);
1060 } else if (RetTyVT == MVT::f32)
1061 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1062 break;
1063 }
1064 }
1065 }
1066
1067 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1068 DAG.getConstant(ArgOffset, getPointerTy()),
1069 DAG.getConstant(ArgOffset, getPointerTy()));
1070 return std::make_pair(RetVal, Chain);
1071 } else {
1072 std::vector<SDOperand> Ops;
1073 Ops.push_back(Chain);
1074 Ops.push_back(Callee);
1075 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1076 // Callee pops all arg values on the stack.
1077 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1078
1079 // Pass register arguments as needed.
1080 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1081
1082 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1083 RetVals, Ops);
1084 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1085
1086 SDOperand ResultVal;
1087 switch (RetTyVT) {
1088 case MVT::isVoid: break;
1089 default:
1090 ResultVal = TheCall.getValue(1);
1091 break;
1092 case MVT::i1:
1093 case MVT::i8:
1094 case MVT::i16:
1095 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1096 break;
1097 case MVT::f32:
1098 // FIXME: we would really like to remember that this FP_ROUND operation is
1099 // okay to eliminate if we allow excess FP precision.
1100 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1101 break;
1102 case MVT::i64:
1103 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1104 TheCall.getValue(2));
1105 break;
1106 }
1107
1108 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001109 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001110}
1111
1112SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1113 if (ReturnAddrIndex == 0) {
1114 // Set up a frame object for the return address.
1115 MachineFunction &MF = DAG.getMachineFunction();
1116 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1117 }
1118
1119 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1120}
1121
1122
1123
1124std::pair<SDOperand, SDOperand> X86TargetLowering::
1125LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1126 SelectionDAG &DAG) {
1127 SDOperand Result;
1128 if (Depth) // Depths > 0 not supported yet!
1129 Result = DAG.getConstant(0, getPointerTy());
1130 else {
1131 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1132 if (!isFrameAddress)
1133 // Just load the return address
1134 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1135 DAG.getSrcValue(NULL));
1136 else
1137 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1138 DAG.getConstant(4, MVT::i32));
1139 }
1140 return std::make_pair(Result, Chain);
1141}
1142
1143//===----------------------------------------------------------------------===//
1144// X86 Custom Lowering Hooks
1145//===----------------------------------------------------------------------===//
1146
Evan Cheng172fce72006-01-06 00:43:03 +00001147/// SetCCToX86CondCode - do a one to one translation of a ISD::CondCode to
1148/// X86 specific CondCode. It returns a X86ISD::COND_INVALID if it cannot
1149/// do a direct translation.
1150static unsigned CCToX86CondCode(SDOperand CC, bool isFP) {
1151 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1152 unsigned X86CC = X86ISD::COND_INVALID;
1153 if (!isFP) {
1154 switch (SetCCOpcode) {
1155 default: break;
1156 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1157 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1158 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1159 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1160 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1161 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1162 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1163 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1164 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1165 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1166 }
1167 } else {
1168 // On a floating point condition, the flags are set as follows:
1169 // ZF PF CF op
1170 // 0 | 0 | 0 | X > Y
1171 // 0 | 0 | 1 | X < Y
1172 // 1 | 0 | 0 | X == Y
1173 // 1 | 1 | 1 | unordered
1174 switch (SetCCOpcode) {
1175 default: break;
1176 case ISD::SETUEQ:
1177 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1178 case ISD::SETOGT:
1179 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1180 case ISD::SETOGE:
1181 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1182 case ISD::SETULT:
1183 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1184 case ISD::SETULE:
1185 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1186 case ISD::SETONE:
1187 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1188 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1189 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1190 }
1191 }
1192 return X86CC;
1193}
1194
Evan Cheng73a1ad92006-01-10 20:26:56 +00001195/// SupportedByFPCMOV - is there a floating point cmov for the specific
1196/// X86 condition code.
1197/// Current x86 isa includes the following FP cmov instructions:
1198/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1199static bool SupportedByFPCMOV(unsigned X86CC) {
1200 switch (X86CC) {
1201 default:
1202 return false;
1203 case X86ISD::COND_B:
1204 case X86ISD::COND_BE:
1205 case X86ISD::COND_E:
1206 case X86ISD::COND_P:
1207 case X86ISD::COND_A:
1208 case X86ISD::COND_AE:
1209 case X86ISD::COND_NE:
1210 case X86ISD::COND_NP:
1211 return true;
1212 }
1213}
1214
Chris Lattner76ac0682005-11-15 00:40:23 +00001215/// LowerOperation - Provide custom lowering hooks for some operations.
1216///
1217SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1218 switch (Op.getOpcode()) {
1219 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001220 case ISD::ADD_PARTS:
1221 case ISD::SUB_PARTS: {
1222 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1223 "Not an i64 add/sub!");
1224 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1225 std::vector<MVT::ValueType> Tys;
1226 Tys.push_back(MVT::i32);
1227 Tys.push_back(MVT::Flag);
1228 std::vector<SDOperand> Ops;
1229 Ops.push_back(Op.getOperand(0));
1230 Ops.push_back(Op.getOperand(2));
1231 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1232 Tys, Ops);
1233 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1234 Op.getOperand(1), Op.getOperand(3),
1235 Lo.getValue(1));
1236 Tys.clear();
1237 Tys.push_back(MVT::i32);
1238 Tys.push_back(MVT::i32);
1239 Ops.clear();
1240 Ops.push_back(Lo);
1241 Ops.push_back(Hi);
1242 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1243 }
1244 case ISD::SHL_PARTS:
1245 case ISD::SRA_PARTS:
1246 case ISD::SRL_PARTS: {
1247 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1248 "Not an i64 shift!");
1249 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1250 SDOperand ShOpLo = Op.getOperand(0);
1251 SDOperand ShOpHi = Op.getOperand(1);
1252 SDOperand ShAmt = Op.getOperand(2);
1253 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng12181af2006-01-09 22:29:54 +00001254 DAG.getConstant(31, MVT::i32))
Evan Cheng9c249c32006-01-09 18:33:28 +00001255 : DAG.getConstant(0, MVT::i32);
1256
1257 SDOperand Tmp2, Tmp3;
1258 if (Op.getOpcode() == ISD::SHL_PARTS) {
1259 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1260 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1261 } else {
1262 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1263 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SHL, MVT::i32, ShOpHi, ShAmt);
1264 }
1265
1266 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1267 ShAmt, DAG.getConstant(32, MVT::i8));
1268
1269 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001270 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001271
1272 std::vector<MVT::ValueType> Tys;
1273 Tys.push_back(MVT::i32);
1274 Tys.push_back(MVT::Flag);
1275 std::vector<SDOperand> Ops;
1276 if (Op.getOpcode() == ISD::SHL_PARTS) {
1277 Ops.push_back(Tmp2);
1278 Ops.push_back(Tmp3);
1279 Ops.push_back(CC);
1280 Ops.push_back(InFlag);
1281 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1282 InFlag = Hi.getValue(1);
1283
1284 Ops.clear();
1285 Ops.push_back(Tmp3);
1286 Ops.push_back(Tmp1);
1287 Ops.push_back(CC);
1288 Ops.push_back(InFlag);
1289 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1290 } else {
1291 Ops.push_back(Tmp2);
1292 Ops.push_back(Tmp3);
1293 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001294 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001295 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1296 InFlag = Lo.getValue(1);
1297
1298 Ops.clear();
1299 Ops.push_back(Tmp3);
1300 Ops.push_back(Tmp1);
1301 Ops.push_back(CC);
1302 Ops.push_back(InFlag);
1303 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1304 }
1305
1306 Tys.clear();
1307 Tys.push_back(MVT::i32);
1308 Tys.push_back(MVT::i32);
1309 Ops.clear();
1310 Ops.push_back(Lo);
1311 Ops.push_back(Hi);
1312 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1313 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001314 case ISD::SINT_TO_FP: {
1315 assert(Op.getValueType() == MVT::f64 &&
1316 Op.getOperand(0).getValueType() == MVT::i64 &&
1317 "Unknown SINT_TO_FP to lower!");
1318 // We lower sint64->FP into a store to a temporary stack slot, followed by a
1319 // FILD64m node.
1320 MachineFunction &MF = DAG.getMachineFunction();
1321 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1322 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1323 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1324 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
1325 std::vector<MVT::ValueType> RTs;
1326 RTs.push_back(MVT::f64);
1327 RTs.push_back(MVT::Other);
1328 std::vector<SDOperand> Ops;
1329 Ops.push_back(Store);
1330 Ops.push_back(StackSlot);
1331 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1332 }
1333 case ISD::FP_TO_SINT: {
1334 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1335 Op.getOperand(0).getValueType() == MVT::f64 &&
1336 "Unknown FP_TO_SINT to lower!");
1337 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1338 // stack slot.
1339 MachineFunction &MF = DAG.getMachineFunction();
1340 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1341 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1342 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1343
1344 unsigned Opc;
1345 switch (Op.getValueType()) {
1346 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1347 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1348 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1349 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1350 }
1351
1352 // Build the FP_TO_INT*_IN_MEM
1353 std::vector<SDOperand> Ops;
1354 Ops.push_back(DAG.getEntryNode());
1355 Ops.push_back(Op.getOperand(0));
1356 Ops.push_back(StackSlot);
1357 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1358
1359 // Load the result.
1360 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1361 DAG.getSrcValue(NULL));
1362 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001363 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001364 std::vector<MVT::ValueType> Tys;
1365 Tys.push_back(MVT::Other);
1366 Tys.push_back(MVT::Flag);
1367 std::vector<SDOperand> Ops;
1368 Ops.push_back(Op.getOperand(0));
1369 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001370 Ops.clear();
1371 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1372 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1373 MVT::i32, Ops[0].getValue(2)));
1374 Ops.push_back(Ops[1].getValue(1));
1375 Tys[0] = Tys[1] = MVT::i32;
1376 Tys.push_back(MVT::Other);
1377 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001378 }
Evan Chengc1583db2005-12-21 20:21:51 +00001379 case ISD::SETCC: {
1380 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1381 SDOperand CC = Op.getOperand(2);
1382 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1383 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001384 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1385 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1386 unsigned X86CC = CCToX86CondCode(CC, isFP);
1387 if (X86CC != X86ISD::COND_INVALID) {
1388 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1389 DAG.getConstant(X86CC, MVT::i8), Cond);
1390 } else {
1391 assert(isFP && "Illegal integer SetCC!");
1392
1393 std::vector<MVT::ValueType> Tys;
1394 std::vector<SDOperand> Ops;
1395 switch (SetCCOpcode) {
1396 default: assert(false && "Illegal floating point SetCC!");
1397 case ISD::SETOEQ: { // !PF & ZF
1398 Tys.push_back(MVT::i8);
1399 Tys.push_back(MVT::Flag);
1400 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1401 Ops.push_back(Cond);
1402 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1403 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1404 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1405 Tmp1.getValue(1));
1406 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1407 }
1408 case ISD::SETOLT: { // !PF & CF
1409 Tys.push_back(MVT::i8);
1410 Tys.push_back(MVT::Flag);
1411 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1412 Ops.push_back(Cond);
1413 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1414 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1415 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1416 Tmp1.getValue(1));
1417 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1418 }
1419 case ISD::SETOLE: { // !PF & (CF || ZF)
1420 Tys.push_back(MVT::i8);
1421 Tys.push_back(MVT::Flag);
1422 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1423 Ops.push_back(Cond);
1424 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1425 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1426 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1427 Tmp1.getValue(1));
1428 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1429 }
1430 case ISD::SETUGT: { // PF | (!ZF & !CF)
1431 Tys.push_back(MVT::i8);
1432 Tys.push_back(MVT::Flag);
1433 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1434 Ops.push_back(Cond);
1435 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1436 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1437 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1438 Tmp1.getValue(1));
1439 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1440 }
1441 case ISD::SETUGE: { // PF | !CF
1442 Tys.push_back(MVT::i8);
1443 Tys.push_back(MVT::Flag);
1444 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1445 Ops.push_back(Cond);
1446 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1447 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1448 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1449 Tmp1.getValue(1));
1450 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1451 }
1452 case ISD::SETUNE: { // PF | !ZF
1453 Tys.push_back(MVT::i8);
1454 Tys.push_back(MVT::Flag);
1455 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1456 Ops.push_back(Cond);
1457 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1458 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1459 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1460 Tmp1.getValue(1));
1461 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1462 }
1463 }
1464 }
Evan Chengc1583db2005-12-21 20:21:51 +00001465 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001466 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001467 MVT::ValueType VT = Op.getValueType();
1468 bool isFP = MVT::isFloatingPoint(VT);
1469 bool isFPStack = isFP && (X86Vector < SSE2);
1470 bool isFPSSE = isFP && (X86Vector >= SSE2);
1471 bool isValid = false;
1472 SDOperand Op0 = Op.getOperand(0);
1473 SDOperand Cond, CC;
1474 if (Op0.getOpcode() == X86ISD::SETCC) {
1475 CC = Op0.getOperand(0);
1476 Cond = Op0.getOperand(1);
1477 isValid =
1478 !(isFPStack &&
1479 !SupportedByFPCMOV(cast<ConstantSDNode>(CC)->getSignExtended()));
1480 } else if (Op0.getOpcode() == ISD::SETCC) {
1481 CC = Op0.getOperand(2);
1482 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng172fce72006-01-06 00:43:03 +00001483 unsigned X86CC = CCToX86CondCode(CC, isFP);
1484 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001485 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001486 Op0.getOperand(0), Op0.getOperand(1));
1487 isValid = true;
1488 }
1489
1490 if (!isValid) {
Evan Cheng172fce72006-01-06 00:43:03 +00001491 CC = DAG.getConstant(X86ISD::COND_E, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001492 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001493 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001494
1495 std::vector<MVT::ValueType> Tys;
1496 Tys.push_back(Op.getValueType());
1497 Tys.push_back(MVT::Flag);
1498 std::vector<SDOperand> Ops;
1499 Ops.push_back(Op.getOperand(1));
1500 Ops.push_back(Op.getOperand(2));
1501 Ops.push_back(CC);
1502 Ops.push_back(Cond);
1503 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001504 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001505 case ISD::BRCOND: {
Evan Cheng6fc31042005-12-19 23:12:38 +00001506 SDOperand Cond = Op.getOperand(1);
1507 SDOperand Dest = Op.getOperand(2);
1508 SDOperand CC;
1509 // TODO: handle Cond == OR / AND / XOR
Evan Chengc1583db2005-12-21 20:21:51 +00001510 if (Cond.getOpcode() == X86ISD::SETCC) {
1511 CC = Cond.getOperand(0);
1512 Cond = Cond.getOperand(1);
1513 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001514 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001515 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
1516 unsigned X86CC = CCToX86CondCode(CC, isFP);
1517 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001518 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1519 Cond.getOperand(0), Cond.getOperand(1));
1520 } else {
Evan Cheng172fce72006-01-06 00:43:03 +00001521 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001522 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1523 }
1524 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1525 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1526 }
Evan Cheng172fce72006-01-06 00:43:03 +00001527 case ISD::RET: {
1528 // Can only be return void.
Evan Cheng9c249c32006-01-09 18:33:28 +00001529 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng172fce72006-01-06 00:43:03 +00001530 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1531 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001532 case ISD::GlobalAddress: {
Evan Chenga74ce622005-12-21 02:39:21 +00001533 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001534 SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga74ce622005-12-21 02:39:21 +00001535 // For Darwin, external and weak symbols are indirect, so we want to load
1536 // the value at address GV, not the value of GV itself. This means that
1537 // the GlobalAddress must be in the base or index register of the address,
1538 // not the GV offset field.
1539 if (getTargetMachine().
1540 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1541 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001542 return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1543 GVOp, DAG.getSrcValue(NULL));
Evan Chenga74ce622005-12-21 02:39:21 +00001544 else
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001545 return GVOp;
Evan Chenga74ce622005-12-21 02:39:21 +00001546 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001547 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001548 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001549}
Evan Cheng6af02632005-12-20 06:22:03 +00001550
1551const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1552 switch (Opcode) {
1553 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001554 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1555 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1556 case X86ISD::ADC: return "X86ISD::ADC";
1557 case X86ISD::SBB: return "X86ISD::SBB";
1558 case X86ISD::SHLD: return "X86ISD::SHLD";
1559 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6af02632005-12-20 06:22:03 +00001560 case X86ISD::FILD64m: return "X86ISD::FILD64m";
1561 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1562 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1563 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001564 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001565 case X86ISD::FST: return "X86ISD::FST";
1566 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001567 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001568 case X86ISD::CALL: return "X86ISD::CALL";
1569 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1570 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1571 case X86ISD::CMP: return "X86ISD::CMP";
1572 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001573 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001574 case X86ISD::CMOV: return "X86ISD::CMOV";
1575 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001576 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00001577 }
1578}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001579
1580bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1581 uint64_t Mask) const {
1582
1583 unsigned Opc = Op.getOpcode();
1584
1585 switch (Opc) {
1586 default:
1587 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1588 break;
1589 case X86ISD::SETCC: return (Mask & 1) == 0;
1590 }
1591
1592 return false;
1593}