blob: fe6b9d00d95f5a5ec8a225d1b0f92394d2a16b7c [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
Chris Lattner76ac0682005-11-15 00:40:23 +00001195/// LowerOperation - Provide custom lowering hooks for some operations.
1196///
1197SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1198 switch (Op.getOpcode()) {
1199 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001200 case ISD::ADD_PARTS:
1201 case ISD::SUB_PARTS: {
1202 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1203 "Not an i64 add/sub!");
1204 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1205 std::vector<MVT::ValueType> Tys;
1206 Tys.push_back(MVT::i32);
1207 Tys.push_back(MVT::Flag);
1208 std::vector<SDOperand> Ops;
1209 Ops.push_back(Op.getOperand(0));
1210 Ops.push_back(Op.getOperand(2));
1211 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1212 Tys, Ops);
1213 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1214 Op.getOperand(1), Op.getOperand(3),
1215 Lo.getValue(1));
1216 Tys.clear();
1217 Tys.push_back(MVT::i32);
1218 Tys.push_back(MVT::i32);
1219 Ops.clear();
1220 Ops.push_back(Lo);
1221 Ops.push_back(Hi);
1222 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1223 }
1224 case ISD::SHL_PARTS:
1225 case ISD::SRA_PARTS:
1226 case ISD::SRL_PARTS: {
1227 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1228 "Not an i64 shift!");
1229 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1230 SDOperand ShOpLo = Op.getOperand(0);
1231 SDOperand ShOpHi = Op.getOperand(1);
1232 SDOperand ShAmt = Op.getOperand(2);
1233 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1234 DAG.getConstant(32, MVT::i32))
1235 : DAG.getConstant(0, MVT::i32);
1236
1237 SDOperand Tmp2, Tmp3;
1238 if (Op.getOpcode() == ISD::SHL_PARTS) {
1239 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1240 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1241 } else {
1242 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1243 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SHL, MVT::i32, ShOpHi, ShAmt);
1244 }
1245
1246 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1247 ShAmt, DAG.getConstant(32, MVT::i8));
1248
1249 SDOperand Hi, Lo;
1250 SDOperand CC = DAG.getConstant(X86ISD::COND_E, MVT::i8);
1251
1252 std::vector<MVT::ValueType> Tys;
1253 Tys.push_back(MVT::i32);
1254 Tys.push_back(MVT::Flag);
1255 std::vector<SDOperand> Ops;
1256 if (Op.getOpcode() == ISD::SHL_PARTS) {
1257 Ops.push_back(Tmp2);
1258 Ops.push_back(Tmp3);
1259 Ops.push_back(CC);
1260 Ops.push_back(InFlag);
1261 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1262 InFlag = Hi.getValue(1);
1263
1264 Ops.clear();
1265 Ops.push_back(Tmp3);
1266 Ops.push_back(Tmp1);
1267 Ops.push_back(CC);
1268 Ops.push_back(InFlag);
1269 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1270 } else {
1271 Ops.push_back(Tmp2);
1272 Ops.push_back(Tmp3);
1273 Ops.push_back(CC);
1274 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1275 InFlag = Lo.getValue(1);
1276
1277 Ops.clear();
1278 Ops.push_back(Tmp3);
1279 Ops.push_back(Tmp1);
1280 Ops.push_back(CC);
1281 Ops.push_back(InFlag);
1282 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1283 }
1284
1285 Tys.clear();
1286 Tys.push_back(MVT::i32);
1287 Tys.push_back(MVT::i32);
1288 Ops.clear();
1289 Ops.push_back(Lo);
1290 Ops.push_back(Hi);
1291 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1292 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001293 case ISD::SINT_TO_FP: {
1294 assert(Op.getValueType() == MVT::f64 &&
1295 Op.getOperand(0).getValueType() == MVT::i64 &&
1296 "Unknown SINT_TO_FP to lower!");
1297 // We lower sint64->FP into a store to a temporary stack slot, followed by a
1298 // FILD64m node.
1299 MachineFunction &MF = DAG.getMachineFunction();
1300 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1301 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1302 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1303 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
1304 std::vector<MVT::ValueType> RTs;
1305 RTs.push_back(MVT::f64);
1306 RTs.push_back(MVT::Other);
1307 std::vector<SDOperand> Ops;
1308 Ops.push_back(Store);
1309 Ops.push_back(StackSlot);
1310 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1311 }
1312 case ISD::FP_TO_SINT: {
1313 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1314 Op.getOperand(0).getValueType() == MVT::f64 &&
1315 "Unknown FP_TO_SINT to lower!");
1316 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1317 // stack slot.
1318 MachineFunction &MF = DAG.getMachineFunction();
1319 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1320 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1321 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1322
1323 unsigned Opc;
1324 switch (Op.getValueType()) {
1325 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1326 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1327 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1328 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1329 }
1330
1331 // Build the FP_TO_INT*_IN_MEM
1332 std::vector<SDOperand> Ops;
1333 Ops.push_back(DAG.getEntryNode());
1334 Ops.push_back(Op.getOperand(0));
1335 Ops.push_back(StackSlot);
1336 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1337
1338 // Load the result.
1339 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1340 DAG.getSrcValue(NULL));
1341 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001342 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001343 std::vector<MVT::ValueType> Tys;
1344 Tys.push_back(MVT::Other);
1345 Tys.push_back(MVT::Flag);
1346 std::vector<SDOperand> Ops;
1347 Ops.push_back(Op.getOperand(0));
1348 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001349 Ops.clear();
1350 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1351 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1352 MVT::i32, Ops[0].getValue(2)));
1353 Ops.push_back(Ops[1].getValue(1));
1354 Tys[0] = Tys[1] = MVT::i32;
1355 Tys.push_back(MVT::Other);
1356 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001357 }
Evan Chengc1583db2005-12-21 20:21:51 +00001358 case ISD::SETCC: {
1359 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1360 SDOperand CC = Op.getOperand(2);
1361 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1362 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001363 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1364 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1365 unsigned X86CC = CCToX86CondCode(CC, isFP);
1366 if (X86CC != X86ISD::COND_INVALID) {
1367 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1368 DAG.getConstant(X86CC, MVT::i8), Cond);
1369 } else {
1370 assert(isFP && "Illegal integer SetCC!");
1371
1372 std::vector<MVT::ValueType> Tys;
1373 std::vector<SDOperand> Ops;
1374 switch (SetCCOpcode) {
1375 default: assert(false && "Illegal floating point SetCC!");
1376 case ISD::SETOEQ: { // !PF & ZF
1377 Tys.push_back(MVT::i8);
1378 Tys.push_back(MVT::Flag);
1379 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1380 Ops.push_back(Cond);
1381 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1382 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1383 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1384 Tmp1.getValue(1));
1385 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1386 }
1387 case ISD::SETOLT: { // !PF & CF
1388 Tys.push_back(MVT::i8);
1389 Tys.push_back(MVT::Flag);
1390 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1391 Ops.push_back(Cond);
1392 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1393 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1394 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1395 Tmp1.getValue(1));
1396 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1397 }
1398 case ISD::SETOLE: { // !PF & (CF || ZF)
1399 Tys.push_back(MVT::i8);
1400 Tys.push_back(MVT::Flag);
1401 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1402 Ops.push_back(Cond);
1403 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1404 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1405 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1406 Tmp1.getValue(1));
1407 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1408 }
1409 case ISD::SETUGT: { // PF | (!ZF & !CF)
1410 Tys.push_back(MVT::i8);
1411 Tys.push_back(MVT::Flag);
1412 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1413 Ops.push_back(Cond);
1414 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1415 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1416 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1417 Tmp1.getValue(1));
1418 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1419 }
1420 case ISD::SETUGE: { // PF | !CF
1421 Tys.push_back(MVT::i8);
1422 Tys.push_back(MVT::Flag);
1423 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1424 Ops.push_back(Cond);
1425 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1426 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1427 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1428 Tmp1.getValue(1));
1429 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1430 }
1431 case ISD::SETUNE: { // PF | !ZF
1432 Tys.push_back(MVT::i8);
1433 Tys.push_back(MVT::Flag);
1434 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1435 Ops.push_back(Cond);
1436 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1437 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1438 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1439 Tmp1.getValue(1));
1440 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1441 }
1442 }
1443 }
Evan Chengc1583db2005-12-21 20:21:51 +00001444 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001445 case ISD::SELECT: {
Evan Cheng225a4d02005-12-17 01:21:05 +00001446 SDOperand Cond = Op.getOperand(0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001447 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001448 if (Cond.getOpcode() == X86ISD::SETCC) {
1449 CC = Cond.getOperand(0);
1450 Cond = Cond.getOperand(1);
1451 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng225a4d02005-12-17 01:21:05 +00001452 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001453 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
1454 unsigned X86CC = CCToX86CondCode(CC, isFP);
1455 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001456 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1457 Cond.getOperand(0), Cond.getOperand(1));
1458 } else {
Evan Cheng172fce72006-01-06 00:43:03 +00001459 CC = DAG.getConstant(X86ISD::COND_E, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001460 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1461 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001462
1463 std::vector<MVT::ValueType> Tys;
1464 Tys.push_back(Op.getValueType());
1465 Tys.push_back(MVT::Flag);
1466 std::vector<SDOperand> Ops;
1467 Ops.push_back(Op.getOperand(1));
1468 Ops.push_back(Op.getOperand(2));
1469 Ops.push_back(CC);
1470 Ops.push_back(Cond);
1471 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001472 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001473 case ISD::BRCOND: {
Evan Cheng6fc31042005-12-19 23:12:38 +00001474 SDOperand Cond = Op.getOperand(1);
1475 SDOperand Dest = Op.getOperand(2);
1476 SDOperand CC;
1477 // TODO: handle Cond == OR / AND / XOR
Evan Chengc1583db2005-12-21 20:21:51 +00001478 if (Cond.getOpcode() == X86ISD::SETCC) {
1479 CC = Cond.getOperand(0);
1480 Cond = Cond.getOperand(1);
1481 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001482 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001483 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
1484 unsigned X86CC = CCToX86CondCode(CC, isFP);
1485 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001486 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1487 Cond.getOperand(0), Cond.getOperand(1));
1488 } else {
Evan Cheng172fce72006-01-06 00:43:03 +00001489 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001490 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1491 }
1492 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1493 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1494 }
Evan Cheng172fce72006-01-06 00:43:03 +00001495 case ISD::RET: {
1496 // Can only be return void.
Evan Cheng9c249c32006-01-09 18:33:28 +00001497 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng172fce72006-01-06 00:43:03 +00001498 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1499 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001500 case ISD::GlobalAddress: {
Evan Chenga74ce622005-12-21 02:39:21 +00001501 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001502 SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga74ce622005-12-21 02:39:21 +00001503 // For Darwin, external and weak symbols are indirect, so we want to load
1504 // the value at address GV, not the value of GV itself. This means that
1505 // the GlobalAddress must be in the base or index register of the address,
1506 // not the GV offset field.
1507 if (getTargetMachine().
1508 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1509 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001510 return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1511 GVOp, DAG.getSrcValue(NULL));
Evan Chenga74ce622005-12-21 02:39:21 +00001512 else
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001513 return GVOp;
Evan Chenga74ce622005-12-21 02:39:21 +00001514 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001515 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001516 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001517}
Evan Cheng6af02632005-12-20 06:22:03 +00001518
1519const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1520 switch (Opcode) {
1521 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001522 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1523 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1524 case X86ISD::ADC: return "X86ISD::ADC";
1525 case X86ISD::SBB: return "X86ISD::SBB";
1526 case X86ISD::SHLD: return "X86ISD::SHLD";
1527 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6af02632005-12-20 06:22:03 +00001528 case X86ISD::FILD64m: return "X86ISD::FILD64m";
1529 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1530 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1531 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001532 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001533 case X86ISD::FST: return "X86ISD::FST";
1534 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001535 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001536 case X86ISD::CALL: return "X86ISD::CALL";
1537 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1538 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1539 case X86ISD::CMP: return "X86ISD::CMP";
1540 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001541 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001542 case X86ISD::CMOV: return "X86ISD::CMOV";
1543 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001544 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00001545 }
1546}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001547
1548bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1549 uint64_t Mask) const {
1550
1551 unsigned Opc = Op.getOpcode();
1552
1553 switch (Opc) {
1554 default:
1555 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1556 break;
1557 case X86ISD::SETCC: return (Mask & 1) == 0;
1558 }
1559
1560 return false;
1561}