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