blob: fee12eff00335671827f933c82b301ed412bb309 [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());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000223 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
224 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000225
Chris Lattner76ac0682005-11-15 00:40:23 +0000226 if (CallingConv == CallingConv::Fast && EnableFastCC)
227 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
228 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
229}
230
Evan Chenga74ce622005-12-21 02:39:21 +0000231SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
232 SelectionDAG &DAG) {
233 if (!X86DAGIsel)
234 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
235
236 SDOperand Copy;
237 MVT::ValueType OpVT = Op.getValueType();
238 switch (OpVT) {
239 default: assert(0 && "Unknown type to return!");
240 case MVT::i32:
241 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
242 break;
243 case MVT::i64: {
244 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
245 DAG.getConstant(1, MVT::i32));
246 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
247 DAG.getConstant(0, MVT::i32));
Evan Cheng172fce72006-01-06 00:43:03 +0000248 Copy = DAG.getCopyToReg(Chain, X86::EDX, Hi, SDOperand());
249 Copy = DAG.getCopyToReg(Copy, X86::EAX, Lo, Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000250 break;
251 }
252 case MVT::f32:
Evan Chenga74ce622005-12-21 02:39:21 +0000253 case MVT::f64:
254 if (!X86ScalarSSE) {
Evan Cheng9c249c32006-01-09 18:33:28 +0000255 if (OpVT == MVT::f32)
256 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chenga74ce622005-12-21 02:39:21 +0000257 std::vector<MVT::ValueType> Tys;
258 Tys.push_back(MVT::Other);
259 Tys.push_back(MVT::Flag);
260 std::vector<SDOperand> Ops;
261 Ops.push_back(Chain);
262 Ops.push_back(Op);
263 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
264 } else {
265 // Spill the value to memory and reload it into top of stack.
266 unsigned Size = MVT::getSizeInBits(OpVT)/8;
267 MachineFunction &MF = DAG.getMachineFunction();
268 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
269 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
270 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
271 StackSlot, DAG.getSrcValue(NULL));
272 std::vector<MVT::ValueType> Tys;
273 Tys.push_back(MVT::f64);
274 Tys.push_back(MVT::Other);
275 std::vector<SDOperand> Ops;
276 Ops.push_back(Chain);
277 Ops.push_back(StackSlot);
278 Ops.push_back(DAG.getValueType(OpVT));
279 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
280 Tys.clear();
281 Tys.push_back(MVT::Other);
282 Tys.push_back(MVT::Flag);
283 Ops.clear();
284 Ops.push_back(Copy.getValue(1));
285 Ops.push_back(Copy);
286 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
287 }
288 break;
289 }
Evan Chengc1583db2005-12-21 20:21:51 +0000290
291 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
292 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
293 Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000294}
295
Chris Lattner76ac0682005-11-15 00:40:23 +0000296//===----------------------------------------------------------------------===//
297// C Calling Convention implementation
298//===----------------------------------------------------------------------===//
299
300std::vector<SDOperand>
301X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
302 std::vector<SDOperand> ArgValues;
303
304 MachineFunction &MF = DAG.getMachineFunction();
305 MachineFrameInfo *MFI = MF.getFrameInfo();
306
307 // Add DAG nodes to load the arguments... On entry to a function on the X86,
308 // the stack frame looks like this:
309 //
310 // [ESP] -- return address
311 // [ESP + 4] -- first argument (leftmost lexically)
312 // [ESP + 8] -- second argument, if first argument is four bytes in size
313 // ...
314 //
315 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
316 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
317 MVT::ValueType ObjectVT = getValueType(I->getType());
318 unsigned ArgIncrement = 4;
319 unsigned ObjSize;
320 switch (ObjectVT) {
321 default: assert(0 && "Unhandled argument type!");
322 case MVT::i1:
323 case MVT::i8: ObjSize = 1; break;
324 case MVT::i16: ObjSize = 2; break;
325 case MVT::i32: ObjSize = 4; break;
326 case MVT::i64: ObjSize = ArgIncrement = 8; break;
327 case MVT::f32: ObjSize = 4; break;
328 case MVT::f64: ObjSize = ArgIncrement = 8; break;
329 }
330 // Create the frame index object for this incoming parameter...
331 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
332
333 // Create the SelectionDAG nodes corresponding to a load from this parameter
334 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
335
336 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
337 // dead loads.
338 SDOperand ArgValue;
339 if (!I->use_empty())
340 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
341 DAG.getSrcValue(NULL));
342 else {
343 if (MVT::isInteger(ObjectVT))
344 ArgValue = DAG.getConstant(0, ObjectVT);
345 else
346 ArgValue = DAG.getConstantFP(0, ObjectVT);
347 }
348 ArgValues.push_back(ArgValue);
349
350 ArgOffset += ArgIncrement; // Move on to the next argument...
351 }
352
353 // If the function takes variable number of arguments, make a frame index for
354 // the start of the first vararg value... for expansion of llvm.va_start.
355 if (F.isVarArg())
356 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
357 ReturnAddrIndex = 0; // No return address slot generated yet.
358 BytesToPopOnReturn = 0; // Callee pops nothing.
359 BytesCallerReserves = ArgOffset;
360
361 // Finally, inform the code generator which regs we return values in.
362 switch (getValueType(F.getReturnType())) {
363 default: assert(0 && "Unknown type!");
364 case MVT::isVoid: break;
365 case MVT::i1:
366 case MVT::i8:
367 case MVT::i16:
368 case MVT::i32:
369 MF.addLiveOut(X86::EAX);
370 break;
371 case MVT::i64:
372 MF.addLiveOut(X86::EAX);
373 MF.addLiveOut(X86::EDX);
374 break;
375 case MVT::f32:
376 case MVT::f64:
377 MF.addLiveOut(X86::ST0);
378 break;
379 }
380 return ArgValues;
381}
382
383std::pair<SDOperand, SDOperand>
384X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
385 bool isVarArg, bool isTailCall,
386 SDOperand Callee, ArgListTy &Args,
387 SelectionDAG &DAG) {
388 // Count how many bytes are to be pushed on the stack.
389 unsigned NumBytes = 0;
390
391 if (Args.empty()) {
392 // Save zero bytes.
393 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
394 DAG.getConstant(0, getPointerTy()));
395 } else {
396 for (unsigned i = 0, e = Args.size(); i != e; ++i)
397 switch (getValueType(Args[i].second)) {
398 default: assert(0 && "Unknown value type!");
399 case MVT::i1:
400 case MVT::i8:
401 case MVT::i16:
402 case MVT::i32:
403 case MVT::f32:
404 NumBytes += 4;
405 break;
406 case MVT::i64:
407 case MVT::f64:
408 NumBytes += 8;
409 break;
410 }
411
412 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
413 DAG.getConstant(NumBytes, getPointerTy()));
414
415 // Arguments go on the stack in reverse order, as specified by the ABI.
416 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000417 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000418 std::vector<SDOperand> Stores;
419
420 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
421 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
422 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
423
424 switch (getValueType(Args[i].second)) {
425 default: assert(0 && "Unexpected ValueType for argument!");
426 case MVT::i1:
427 case MVT::i8:
428 case MVT::i16:
429 // Promote the integer to 32 bits. If the input type is signed use a
430 // sign extend, otherwise use a zero extend.
431 if (Args[i].second->isSigned())
432 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
433 else
434 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
435
436 // FALL THROUGH
437 case MVT::i32:
438 case MVT::f32:
439 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
440 Args[i].first, PtrOff,
441 DAG.getSrcValue(NULL)));
442 ArgOffset += 4;
443 break;
444 case MVT::i64:
445 case MVT::f64:
446 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
447 Args[i].first, PtrOff,
448 DAG.getSrcValue(NULL)));
449 ArgOffset += 8;
450 break;
451 }
452 }
453 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
454 }
455
456 std::vector<MVT::ValueType> RetVals;
457 MVT::ValueType RetTyVT = getValueType(RetTy);
458 RetVals.push_back(MVT::Other);
459
460 // The result values produced have to be legal. Promote the result.
461 switch (RetTyVT) {
462 case MVT::isVoid: break;
463 default:
464 RetVals.push_back(RetTyVT);
465 break;
466 case MVT::i1:
467 case MVT::i8:
468 case MVT::i16:
469 RetVals.push_back(MVT::i32);
470 break;
471 case MVT::f32:
472 if (X86ScalarSSE)
473 RetVals.push_back(MVT::f32);
474 else
475 RetVals.push_back(MVT::f64);
476 break;
477 case MVT::i64:
478 RetVals.push_back(MVT::i32);
479 RetVals.push_back(MVT::i32);
480 break;
481 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000482
Evan Cheng45e190982006-01-05 00:27:02 +0000483 if (X86DAGIsel) {
484 std::vector<MVT::ValueType> NodeTys;
485 NodeTys.push_back(MVT::Other); // Returns a chain
486 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000487 std::vector<SDOperand> Ops;
488 Ops.push_back(Chain);
489 Ops.push_back(Callee);
490
Evan Cheng172fce72006-01-06 00:43:03 +0000491 // FIXME: Do not generate X86ISD::TAILCALL for now.
492 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000493 SDOperand InFlag = Chain.getValue(1);
494
495 SDOperand RetVal;
496 if (RetTyVT != MVT::isVoid) {
497 switch (RetTyVT) {
498 default: assert(0 && "Unknown value type to return!");
499 case MVT::i1:
500 case MVT::i8:
501 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
502 Chain = RetVal.getValue(1);
503 break;
504 case MVT::i16:
505 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
506 Chain = RetVal.getValue(1);
507 break;
508 case MVT::i32:
509 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
510 Chain = RetVal.getValue(1);
511 break;
512 case MVT::i64: {
513 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
514 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
515 Lo.getValue(2));
516 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
517 Chain = Hi.getValue(1);
518 break;
519 }
520 case MVT::f32:
521 case MVT::f64: {
522 std::vector<MVT::ValueType> Tys;
523 Tys.push_back(MVT::f64);
524 Tys.push_back(MVT::Other);
525 std::vector<SDOperand> Ops;
526 Ops.push_back(Chain);
527 Ops.push_back(InFlag);
528 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
529 Chain = RetVal.getValue(1);
530 if (X86ScalarSSE) {
531 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
532 MachineFunction &MF = DAG.getMachineFunction();
533 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
534 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
535 Tys.clear();
536 Tys.push_back(MVT::Other);
537 Ops.clear();
538 Ops.push_back(Chain);
539 Ops.push_back(RetVal);
540 Ops.push_back(StackSlot);
541 Ops.push_back(DAG.getValueType(RetTyVT));
542 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
543 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
544 DAG.getSrcValue(NULL));
545 Chain = RetVal.getValue(1);
546 } else if (RetTyVT == MVT::f32)
547 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
548 break;
549 }
550 }
551 }
552
553 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
554 DAG.getConstant(NumBytes, getPointerTy()),
555 DAG.getConstant(0, getPointerTy()));
556 return std::make_pair(RetVal, Chain);
557 } else {
558 std::vector<SDOperand> Ops;
559 Ops.push_back(Chain);
560 Ops.push_back(Callee);
561 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
562 Ops.push_back(DAG.getConstant(0, getPointerTy()));
563
564 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
565 RetVals, Ops);
566
567 SDOperand ResultVal;
568 switch (RetTyVT) {
569 case MVT::isVoid: break;
570 default:
571 ResultVal = TheCall.getValue(1);
572 break;
573 case MVT::i1:
574 case MVT::i8:
575 case MVT::i16:
576 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
577 break;
578 case MVT::f32:
579 // FIXME: we would really like to remember that this FP_ROUND operation is
580 // okay to eliminate if we allow excess FP precision.
581 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
582 break;
583 case MVT::i64:
584 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
585 TheCall.getValue(2));
586 break;
587 }
588
589 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
590 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000591 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000592}
593
594SDOperand
595X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
596 Value *VAListV, SelectionDAG &DAG) {
597 // vastart just stores the address of the VarArgsFrameIndex slot.
598 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
599 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
600 DAG.getSrcValue(VAListV));
601}
602
603
604std::pair<SDOperand,SDOperand>
605X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
606 Value *VAListV, const Type *ArgTy,
607 SelectionDAG &DAG) {
608 MVT::ValueType ArgVT = getValueType(ArgTy);
609 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
610 VAListP, DAG.getSrcValue(VAListV));
611 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
612 DAG.getSrcValue(NULL));
613 unsigned Amt;
614 if (ArgVT == MVT::i32)
615 Amt = 4;
616 else {
617 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
618 "Other types should have been promoted for varargs!");
619 Amt = 8;
620 }
621 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
622 DAG.getConstant(Amt, Val.getValueType()));
623 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
624 Val, VAListP, DAG.getSrcValue(VAListV));
625 return std::make_pair(Result, Chain);
626}
627
628//===----------------------------------------------------------------------===//
629// Fast Calling Convention implementation
630//===----------------------------------------------------------------------===//
631//
632// The X86 'fast' calling convention passes up to two integer arguments in
633// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
634// and requires that the callee pop its arguments off the stack (allowing proper
635// tail calls), and has the same return value conventions as C calling convs.
636//
637// This calling convention always arranges for the callee pop value to be 8n+4
638// bytes, which is needed for tail recursion elimination and stack alignment
639// reasons.
640//
641// Note that this can be enhanced in the future to pass fp vals in registers
642// (when we have a global fp allocator) and do other tricks.
643//
644
645/// AddLiveIn - This helper function adds the specified physical register to the
646/// MachineFunction as a live in value. It also creates a corresponding virtual
647/// register for it.
648static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
649 TargetRegisterClass *RC) {
650 assert(RC->contains(PReg) && "Not the correct regclass!");
651 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
652 MF.addLiveIn(PReg, VReg);
653 return VReg;
654}
655
656
657std::vector<SDOperand>
658X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
659 std::vector<SDOperand> ArgValues;
660
661 MachineFunction &MF = DAG.getMachineFunction();
662 MachineFrameInfo *MFI = MF.getFrameInfo();
663
664 // Add DAG nodes to load the arguments... On entry to a function the stack
665 // frame looks like this:
666 //
667 // [ESP] -- return address
668 // [ESP + 4] -- first nonreg argument (leftmost lexically)
669 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
670 // ...
671 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
672
673 // Keep track of the number of integer regs passed so far. This can be either
674 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
675 // used).
676 unsigned NumIntRegs = 0;
677
678 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
679 MVT::ValueType ObjectVT = getValueType(I->getType());
680 unsigned ArgIncrement = 4;
681 unsigned ObjSize = 0;
682 SDOperand ArgValue;
683
684 switch (ObjectVT) {
685 default: assert(0 && "Unhandled argument type!");
686 case MVT::i1:
687 case MVT::i8:
688 if (NumIntRegs < 2) {
689 if (!I->use_empty()) {
690 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
691 X86::R8RegisterClass);
692 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
693 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000694 if (ObjectVT == MVT::i1)
695 // FIXME: Should insert a assertzext here.
696 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000697 }
698 ++NumIntRegs;
699 break;
700 }
701
702 ObjSize = 1;
703 break;
704 case MVT::i16:
705 if (NumIntRegs < 2) {
706 if (!I->use_empty()) {
707 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
708 X86::R16RegisterClass);
709 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
710 DAG.setRoot(ArgValue.getValue(1));
711 }
712 ++NumIntRegs;
713 break;
714 }
715 ObjSize = 2;
716 break;
717 case MVT::i32:
718 if (NumIntRegs < 2) {
719 if (!I->use_empty()) {
720 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
721 X86::R32RegisterClass);
722 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
723 DAG.setRoot(ArgValue.getValue(1));
724 }
725 ++NumIntRegs;
726 break;
727 }
728 ObjSize = 4;
729 break;
730 case MVT::i64:
731 if (NumIntRegs == 0) {
732 if (!I->use_empty()) {
733 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
734 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
735
736 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
737 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
738 DAG.setRoot(Hi.getValue(1));
739
740 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
741 }
742 NumIntRegs = 2;
743 break;
744 } else if (NumIntRegs == 1) {
745 if (!I->use_empty()) {
746 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
747 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
748 DAG.setRoot(Low.getValue(1));
749
750 // Load the high part from memory.
751 // Create the frame index object for this incoming parameter...
752 int FI = MFI->CreateFixedObject(4, ArgOffset);
753 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
754 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
755 DAG.getSrcValue(NULL));
756 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
757 }
758 ArgOffset += 4;
759 NumIntRegs = 2;
760 break;
761 }
762 ObjSize = ArgIncrement = 8;
763 break;
764 case MVT::f32: ObjSize = 4; break;
765 case MVT::f64: ObjSize = ArgIncrement = 8; break;
766 }
767
768 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
769 // dead loads.
770 if (ObjSize && !I->use_empty()) {
771 // Create the frame index object for this incoming parameter...
772 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
773
774 // Create the SelectionDAG nodes corresponding to a load from this
775 // parameter.
776 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
777
778 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
779 DAG.getSrcValue(NULL));
780 } else if (ArgValue.Val == 0) {
781 if (MVT::isInteger(ObjectVT))
782 ArgValue = DAG.getConstant(0, ObjectVT);
783 else
784 ArgValue = DAG.getConstantFP(0, ObjectVT);
785 }
786 ArgValues.push_back(ArgValue);
787
788 if (ObjSize)
789 ArgOffset += ArgIncrement; // Move on to the next argument.
790 }
791
792 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
793 // arguments and the arguments after the retaddr has been pushed are aligned.
794 if ((ArgOffset & 7) == 0)
795 ArgOffset += 4;
796
797 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
798 ReturnAddrIndex = 0; // No return address slot generated yet.
799 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
800 BytesCallerReserves = 0;
801
802 // Finally, inform the code generator which regs we return values in.
803 switch (getValueType(F.getReturnType())) {
804 default: assert(0 && "Unknown type!");
805 case MVT::isVoid: break;
806 case MVT::i1:
807 case MVT::i8:
808 case MVT::i16:
809 case MVT::i32:
810 MF.addLiveOut(X86::EAX);
811 break;
812 case MVT::i64:
813 MF.addLiveOut(X86::EAX);
814 MF.addLiveOut(X86::EDX);
815 break;
816 case MVT::f32:
817 case MVT::f64:
818 MF.addLiveOut(X86::ST0);
819 break;
820 }
821 return ArgValues;
822}
823
824std::pair<SDOperand, SDOperand>
825X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
826 bool isTailCall, SDOperand Callee,
827 ArgListTy &Args, SelectionDAG &DAG) {
828 // Count how many bytes are to be pushed on the stack.
829 unsigned NumBytes = 0;
830
831 // Keep track of the number of integer regs passed so far. This can be either
832 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
833 // used).
834 unsigned NumIntRegs = 0;
835
836 for (unsigned i = 0, e = Args.size(); i != e; ++i)
837 switch (getValueType(Args[i].second)) {
838 default: assert(0 && "Unknown value type!");
839 case MVT::i1:
840 case MVT::i8:
841 case MVT::i16:
842 case MVT::i32:
843 if (NumIntRegs < 2) {
844 ++NumIntRegs;
845 break;
846 }
847 // fall through
848 case MVT::f32:
849 NumBytes += 4;
850 break;
851 case MVT::i64:
852 if (NumIntRegs == 0) {
853 NumIntRegs = 2;
854 break;
855 } else if (NumIntRegs == 1) {
856 NumIntRegs = 2;
857 NumBytes += 4;
858 break;
859 }
860
861 // fall through
862 case MVT::f64:
863 NumBytes += 8;
864 break;
865 }
866
867 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
868 // arguments and the arguments after the retaddr has been pushed are aligned.
869 if ((NumBytes & 7) == 0)
870 NumBytes += 4;
871
872 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
873 DAG.getConstant(NumBytes, getPointerTy()));
874
875 // Arguments go on the stack in reverse order, as specified by the ABI.
876 unsigned ArgOffset = 0;
877 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
878 X86::ESP, MVT::i32);
879 NumIntRegs = 0;
880 std::vector<SDOperand> Stores;
881 std::vector<SDOperand> RegValuesToPass;
882 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
883 switch (getValueType(Args[i].second)) {
884 default: assert(0 && "Unexpected ValueType for argument!");
885 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000886 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
887 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000888 case MVT::i8:
889 case MVT::i16:
890 case MVT::i32:
891 if (NumIntRegs < 2) {
892 RegValuesToPass.push_back(Args[i].first);
893 ++NumIntRegs;
894 break;
895 }
896 // Fall through
897 case MVT::f32: {
898 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
899 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
900 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
901 Args[i].first, PtrOff,
902 DAG.getSrcValue(NULL)));
903 ArgOffset += 4;
904 break;
905 }
906 case MVT::i64:
907 if (NumIntRegs < 2) { // Can pass part of it in regs?
908 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
909 Args[i].first, DAG.getConstant(1, MVT::i32));
910 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
911 Args[i].first, DAG.getConstant(0, MVT::i32));
912 RegValuesToPass.push_back(Lo);
913 ++NumIntRegs;
914 if (NumIntRegs < 2) { // Pass both parts in regs?
915 RegValuesToPass.push_back(Hi);
916 ++NumIntRegs;
917 } else {
918 // Pass the high part in memory.
919 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
920 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
921 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
922 Hi, PtrOff, DAG.getSrcValue(NULL)));
923 ArgOffset += 4;
924 }
925 break;
926 }
927 // Fall through
928 case MVT::f64:
929 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
930 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
931 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
932 Args[i].first, PtrOff,
933 DAG.getSrcValue(NULL)));
934 ArgOffset += 8;
935 break;
936 }
937 }
938 if (!Stores.empty())
939 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
940
941 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
942 // arguments and the arguments after the retaddr has been pushed are aligned.
943 if ((ArgOffset & 7) == 0)
944 ArgOffset += 4;
945
946 std::vector<MVT::ValueType> RetVals;
947 MVT::ValueType RetTyVT = getValueType(RetTy);
948
949 RetVals.push_back(MVT::Other);
950
951 // The result values produced have to be legal. Promote the result.
952 switch (RetTyVT) {
953 case MVT::isVoid: break;
954 default:
955 RetVals.push_back(RetTyVT);
956 break;
957 case MVT::i1:
958 case MVT::i8:
959 case MVT::i16:
960 RetVals.push_back(MVT::i32);
961 break;
962 case MVT::f32:
963 if (X86ScalarSSE)
964 RetVals.push_back(MVT::f32);
965 else
966 RetVals.push_back(MVT::f64);
967 break;
968 case MVT::i64:
969 RetVals.push_back(MVT::i32);
970 RetVals.push_back(MVT::i32);
971 break;
972 }
973
Evan Cheng172fce72006-01-06 00:43:03 +0000974 if (X86DAGIsel) {
975 // Build a sequence of copy-to-reg nodes chained together with token chain
976 // and flag operands which copy the outgoing args into registers.
977 SDOperand InFlag;
978 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
979 unsigned CCReg;
980 SDOperand RegToPass = RegValuesToPass[i];
981 switch (RegToPass.getValueType()) {
982 default: assert(0 && "Bad thing to pass in regs");
983 case MVT::i8:
984 CCReg = (i == 0) ? X86::AL : X86::DL;
985 break;
986 case MVT::i16:
987 CCReg = (i == 0) ? X86::AX : X86::DX;
988 break;
989 case MVT::i32:
990 CCReg = (i == 0) ? X86::EAX : X86::EDX;
991 break;
992 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000993
Evan Cheng172fce72006-01-06 00:43:03 +0000994 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
995 InFlag = Chain.getValue(1);
996 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000997
Evan Cheng172fce72006-01-06 00:43:03 +0000998 std::vector<MVT::ValueType> NodeTys;
999 NodeTys.push_back(MVT::Other); // Returns a chain
1000 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +00001001 std::vector<SDOperand> Ops;
1002 Ops.push_back(Chain);
1003 Ops.push_back(Callee);
1004 if (InFlag.Val)
1005 Ops.push_back(InFlag);
1006
1007 // FIXME: Do not generate X86ISD::TAILCALL for now.
1008 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1009 InFlag = Chain.getValue(1);
1010
1011 SDOperand RetVal;
1012 if (RetTyVT != MVT::isVoid) {
1013 switch (RetTyVT) {
1014 default: assert(0 && "Unknown value type to return!");
1015 case MVT::i1:
1016 case MVT::i8:
1017 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1018 Chain = RetVal.getValue(1);
1019 break;
1020 case MVT::i16:
1021 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1022 Chain = RetVal.getValue(1);
1023 break;
1024 case MVT::i32:
1025 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1026 Chain = RetVal.getValue(1);
1027 break;
1028 case MVT::i64: {
1029 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1030 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1031 Lo.getValue(2));
1032 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1033 Chain = Hi.getValue(1);
1034 break;
1035 }
1036 case MVT::f32:
1037 case MVT::f64: {
1038 std::vector<MVT::ValueType> Tys;
1039 Tys.push_back(MVT::f64);
1040 Tys.push_back(MVT::Other);
1041 std::vector<SDOperand> Ops;
1042 Ops.push_back(Chain);
1043 Ops.push_back(InFlag);
1044 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1045 Chain = RetVal.getValue(1);
1046 if (X86ScalarSSE) {
1047 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1048 MachineFunction &MF = DAG.getMachineFunction();
1049 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1050 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1051 Tys.clear();
1052 Tys.push_back(MVT::Other);
1053 Ops.clear();
1054 Ops.push_back(Chain);
1055 Ops.push_back(RetVal);
1056 Ops.push_back(StackSlot);
1057 Ops.push_back(DAG.getValueType(RetTyVT));
1058 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1059 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1060 DAG.getSrcValue(NULL));
1061 Chain = RetVal.getValue(1);
1062 } else if (RetTyVT == MVT::f32)
1063 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1064 break;
1065 }
1066 }
1067 }
1068
1069 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1070 DAG.getConstant(ArgOffset, getPointerTy()),
1071 DAG.getConstant(ArgOffset, getPointerTy()));
1072 return std::make_pair(RetVal, Chain);
1073 } else {
1074 std::vector<SDOperand> Ops;
1075 Ops.push_back(Chain);
1076 Ops.push_back(Callee);
1077 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1078 // Callee pops all arg values on the stack.
1079 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1080
1081 // Pass register arguments as needed.
1082 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1083
1084 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1085 RetVals, Ops);
1086 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1087
1088 SDOperand ResultVal;
1089 switch (RetTyVT) {
1090 case MVT::isVoid: break;
1091 default:
1092 ResultVal = TheCall.getValue(1);
1093 break;
1094 case MVT::i1:
1095 case MVT::i8:
1096 case MVT::i16:
1097 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1098 break;
1099 case MVT::f32:
1100 // FIXME: we would really like to remember that this FP_ROUND operation is
1101 // okay to eliminate if we allow excess FP precision.
1102 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1103 break;
1104 case MVT::i64:
1105 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1106 TheCall.getValue(2));
1107 break;
1108 }
1109
1110 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001111 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001112}
1113
1114SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1115 if (ReturnAddrIndex == 0) {
1116 // Set up a frame object for the return address.
1117 MachineFunction &MF = DAG.getMachineFunction();
1118 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1119 }
1120
1121 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1122}
1123
1124
1125
1126std::pair<SDOperand, SDOperand> X86TargetLowering::
1127LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1128 SelectionDAG &DAG) {
1129 SDOperand Result;
1130 if (Depth) // Depths > 0 not supported yet!
1131 Result = DAG.getConstant(0, getPointerTy());
1132 else {
1133 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1134 if (!isFrameAddress)
1135 // Just load the return address
1136 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1137 DAG.getSrcValue(NULL));
1138 else
1139 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1140 DAG.getConstant(4, MVT::i32));
1141 }
1142 return std::make_pair(Result, Chain);
1143}
1144
Evan Cheng339edad2006-01-11 00:33:36 +00001145/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1146/// which corresponds to the condition code.
1147static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1148 switch (X86CC) {
1149 default: assert(0 && "Unknown X86 conditional code!");
1150 case X86ISD::COND_A: return X86::JA;
1151 case X86ISD::COND_AE: return X86::JAE;
1152 case X86ISD::COND_B: return X86::JB;
1153 case X86ISD::COND_BE: return X86::JBE;
1154 case X86ISD::COND_E: return X86::JE;
1155 case X86ISD::COND_G: return X86::JG;
1156 case X86ISD::COND_GE: return X86::JGE;
1157 case X86ISD::COND_L: return X86::JL;
1158 case X86ISD::COND_LE: return X86::JLE;
1159 case X86ISD::COND_NE: return X86::JNE;
1160 case X86ISD::COND_NO: return X86::JNO;
1161 case X86ISD::COND_NP: return X86::JNP;
1162 case X86ISD::COND_NS: return X86::JNS;
1163 case X86ISD::COND_O: return X86::JO;
1164 case X86ISD::COND_P: return X86::JP;
1165 case X86ISD::COND_S: return X86::JS;
1166 }
1167}
Chris Lattner76ac0682005-11-15 00:40:23 +00001168
Evan Cheng339edad2006-01-11 00:33:36 +00001169/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1170/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Cheng172fce72006-01-06 00:43:03 +00001171/// do a direct translation.
Evan Cheng339edad2006-01-11 00:33:36 +00001172static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Cheng172fce72006-01-06 00:43:03 +00001173 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1174 unsigned X86CC = X86ISD::COND_INVALID;
1175 if (!isFP) {
1176 switch (SetCCOpcode) {
1177 default: break;
1178 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1179 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1180 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1181 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1182 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1183 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1184 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1185 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1186 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1187 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1188 }
1189 } else {
1190 // On a floating point condition, the flags are set as follows:
1191 // ZF PF CF op
1192 // 0 | 0 | 0 | X > Y
1193 // 0 | 0 | 1 | X < Y
1194 // 1 | 0 | 0 | X == Y
1195 // 1 | 1 | 1 | unordered
1196 switch (SetCCOpcode) {
1197 default: break;
1198 case ISD::SETUEQ:
1199 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1200 case ISD::SETOGT:
1201 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1202 case ISD::SETOGE:
1203 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1204 case ISD::SETULT:
1205 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1206 case ISD::SETULE:
1207 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1208 case ISD::SETONE:
1209 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1210 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1211 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1212 }
1213 }
1214 return X86CC;
1215}
1216
Evan Cheng339edad2006-01-11 00:33:36 +00001217/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1218/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001219/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001220static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001221 switch (X86CC) {
1222 default:
1223 return false;
1224 case X86ISD::COND_B:
1225 case X86ISD::COND_BE:
1226 case X86ISD::COND_E:
1227 case X86ISD::COND_P:
1228 case X86ISD::COND_A:
1229 case X86ISD::COND_AE:
1230 case X86ISD::COND_NE:
1231 case X86ISD::COND_NP:
1232 return true;
1233 }
1234}
1235
Evan Cheng339edad2006-01-11 00:33:36 +00001236MachineBasicBlock *
1237X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1238 MachineBasicBlock *BB) {
1239 assert((MI->getOpcode() == X86::CMOV_FR32 ||
1240 MI->getOpcode() == X86::CMOV_FR64) &&
1241 "Unexpected instr type to insert");
1242
1243 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1244 // control-flow pattern. The incoming instruction knows the destination vreg
1245 // to set, the condition code register to branch on, the true/false values to
1246 // select between, and a branch opcode to use.
1247 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1248 ilist<MachineBasicBlock>::iterator It = BB;
1249 ++It;
1250
1251 // thisMBB:
1252 // ...
1253 // TrueVal = ...
1254 // cmpTY ccX, r1, r2
1255 // bCC copy1MBB
1256 // fallthrough --> copy0MBB
1257 MachineBasicBlock *thisMBB = BB;
1258 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1259 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1260 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1261 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1262 MachineFunction *F = BB->getParent();
1263 F->getBasicBlockList().insert(It, copy0MBB);
1264 F->getBasicBlockList().insert(It, sinkMBB);
1265 // Update machine-CFG edges
1266 BB->addSuccessor(copy0MBB);
1267 BB->addSuccessor(sinkMBB);
1268
1269 // copy0MBB:
1270 // %FalseValue = ...
1271 // # fallthrough to sinkMBB
1272 BB = copy0MBB;
1273
1274 // Update machine-CFG edges
1275 BB->addSuccessor(sinkMBB);
1276
1277 // sinkMBB:
1278 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1279 // ...
1280 BB = sinkMBB;
1281 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1282 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1283 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1284
1285 delete MI; // The pseudo instruction is gone now.
1286 return BB;
1287}
1288
1289
1290//===----------------------------------------------------------------------===//
1291// X86 Custom Lowering Hooks
1292//===----------------------------------------------------------------------===//
1293
Chris Lattner76ac0682005-11-15 00:40:23 +00001294/// LowerOperation - Provide custom lowering hooks for some operations.
1295///
1296SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1297 switch (Op.getOpcode()) {
1298 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001299 case ISD::ADD_PARTS:
1300 case ISD::SUB_PARTS: {
1301 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1302 "Not an i64 add/sub!");
1303 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1304 std::vector<MVT::ValueType> Tys;
1305 Tys.push_back(MVT::i32);
1306 Tys.push_back(MVT::Flag);
1307 std::vector<SDOperand> Ops;
1308 Ops.push_back(Op.getOperand(0));
1309 Ops.push_back(Op.getOperand(2));
1310 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1311 Tys, Ops);
1312 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1313 Op.getOperand(1), Op.getOperand(3),
1314 Lo.getValue(1));
1315 Tys.clear();
1316 Tys.push_back(MVT::i32);
1317 Tys.push_back(MVT::i32);
1318 Ops.clear();
1319 Ops.push_back(Lo);
1320 Ops.push_back(Hi);
1321 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1322 }
1323 case ISD::SHL_PARTS:
1324 case ISD::SRA_PARTS:
1325 case ISD::SRL_PARTS: {
1326 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1327 "Not an i64 shift!");
1328 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1329 SDOperand ShOpLo = Op.getOperand(0);
1330 SDOperand ShOpHi = Op.getOperand(1);
1331 SDOperand ShAmt = Op.getOperand(2);
1332 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng12181af2006-01-09 22:29:54 +00001333 DAG.getConstant(31, MVT::i32))
Evan Cheng9c249c32006-01-09 18:33:28 +00001334 : DAG.getConstant(0, MVT::i32);
1335
1336 SDOperand Tmp2, Tmp3;
1337 if (Op.getOpcode() == ISD::SHL_PARTS) {
1338 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1339 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1340 } else {
1341 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1342 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SHL, MVT::i32, ShOpHi, ShAmt);
1343 }
1344
1345 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1346 ShAmt, DAG.getConstant(32, MVT::i8));
1347
1348 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001349 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001350
1351 std::vector<MVT::ValueType> Tys;
1352 Tys.push_back(MVT::i32);
1353 Tys.push_back(MVT::Flag);
1354 std::vector<SDOperand> Ops;
1355 if (Op.getOpcode() == ISD::SHL_PARTS) {
1356 Ops.push_back(Tmp2);
1357 Ops.push_back(Tmp3);
1358 Ops.push_back(CC);
1359 Ops.push_back(InFlag);
1360 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1361 InFlag = Hi.getValue(1);
1362
1363 Ops.clear();
1364 Ops.push_back(Tmp3);
1365 Ops.push_back(Tmp1);
1366 Ops.push_back(CC);
1367 Ops.push_back(InFlag);
1368 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1369 } else {
1370 Ops.push_back(Tmp2);
1371 Ops.push_back(Tmp3);
1372 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001373 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001374 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1375 InFlag = Lo.getValue(1);
1376
1377 Ops.clear();
1378 Ops.push_back(Tmp3);
1379 Ops.push_back(Tmp1);
1380 Ops.push_back(CC);
1381 Ops.push_back(InFlag);
1382 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1383 }
1384
1385 Tys.clear();
1386 Tys.push_back(MVT::i32);
1387 Tys.push_back(MVT::i32);
1388 Ops.clear();
1389 Ops.push_back(Lo);
1390 Ops.push_back(Hi);
1391 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1392 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001393 case ISD::SINT_TO_FP: {
1394 assert(Op.getValueType() == MVT::f64 &&
1395 Op.getOperand(0).getValueType() == MVT::i64 &&
1396 "Unknown SINT_TO_FP to lower!");
1397 // We lower sint64->FP into a store to a temporary stack slot, followed by a
1398 // FILD64m node.
1399 MachineFunction &MF = DAG.getMachineFunction();
1400 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1401 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1402 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1403 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
1404 std::vector<MVT::ValueType> RTs;
1405 RTs.push_back(MVT::f64);
1406 RTs.push_back(MVT::Other);
1407 std::vector<SDOperand> Ops;
1408 Ops.push_back(Store);
1409 Ops.push_back(StackSlot);
1410 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1411 }
1412 case ISD::FP_TO_SINT: {
1413 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1414 Op.getOperand(0).getValueType() == MVT::f64 &&
1415 "Unknown FP_TO_SINT to lower!");
1416 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1417 // stack slot.
1418 MachineFunction &MF = DAG.getMachineFunction();
1419 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1420 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1421 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1422
1423 unsigned Opc;
1424 switch (Op.getValueType()) {
1425 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1426 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1427 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1428 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1429 }
1430
1431 // Build the FP_TO_INT*_IN_MEM
1432 std::vector<SDOperand> Ops;
1433 Ops.push_back(DAG.getEntryNode());
1434 Ops.push_back(Op.getOperand(0));
1435 Ops.push_back(StackSlot);
1436 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1437
1438 // Load the result.
1439 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1440 DAG.getSrcValue(NULL));
1441 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001442 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001443 std::vector<MVT::ValueType> Tys;
1444 Tys.push_back(MVT::Other);
1445 Tys.push_back(MVT::Flag);
1446 std::vector<SDOperand> Ops;
1447 Ops.push_back(Op.getOperand(0));
1448 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001449 Ops.clear();
1450 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1451 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1452 MVT::i32, Ops[0].getValue(2)));
1453 Ops.push_back(Ops[1].getValue(1));
1454 Tys[0] = Tys[1] = MVT::i32;
1455 Tys.push_back(MVT::Other);
1456 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001457 }
Evan Chengc1583db2005-12-21 20:21:51 +00001458 case ISD::SETCC: {
1459 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1460 SDOperand CC = Op.getOperand(2);
1461 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1462 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001463 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1464 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001465 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001466 if (X86CC != X86ISD::COND_INVALID) {
1467 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1468 DAG.getConstant(X86CC, MVT::i8), Cond);
1469 } else {
1470 assert(isFP && "Illegal integer SetCC!");
1471
1472 std::vector<MVT::ValueType> Tys;
1473 std::vector<SDOperand> Ops;
1474 switch (SetCCOpcode) {
1475 default: assert(false && "Illegal floating point SetCC!");
1476 case ISD::SETOEQ: { // !PF & ZF
1477 Tys.push_back(MVT::i8);
1478 Tys.push_back(MVT::Flag);
1479 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1480 Ops.push_back(Cond);
1481 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1482 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1483 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1484 Tmp1.getValue(1));
1485 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1486 }
1487 case ISD::SETOLT: { // !PF & CF
1488 Tys.push_back(MVT::i8);
1489 Tys.push_back(MVT::Flag);
1490 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1491 Ops.push_back(Cond);
1492 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1493 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1494 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1495 Tmp1.getValue(1));
1496 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1497 }
1498 case ISD::SETOLE: { // !PF & (CF || ZF)
1499 Tys.push_back(MVT::i8);
1500 Tys.push_back(MVT::Flag);
1501 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1502 Ops.push_back(Cond);
1503 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1504 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1505 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1506 Tmp1.getValue(1));
1507 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1508 }
1509 case ISD::SETUGT: { // PF | (!ZF & !CF)
1510 Tys.push_back(MVT::i8);
1511 Tys.push_back(MVT::Flag);
1512 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1513 Ops.push_back(Cond);
1514 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1515 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1516 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1517 Tmp1.getValue(1));
1518 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1519 }
1520 case ISD::SETUGE: { // PF | !CF
1521 Tys.push_back(MVT::i8);
1522 Tys.push_back(MVT::Flag);
1523 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1524 Ops.push_back(Cond);
1525 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1526 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1527 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1528 Tmp1.getValue(1));
1529 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1530 }
1531 case ISD::SETUNE: { // PF | !ZF
1532 Tys.push_back(MVT::i8);
1533 Tys.push_back(MVT::Flag);
1534 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1535 Ops.push_back(Cond);
1536 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1537 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1538 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1539 Tmp1.getValue(1));
1540 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1541 }
1542 }
1543 }
Evan Chengc1583db2005-12-21 20:21:51 +00001544 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001545 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001546 MVT::ValueType VT = Op.getValueType();
1547 bool isFP = MVT::isFloatingPoint(VT);
1548 bool isFPStack = isFP && (X86Vector < SSE2);
1549 bool isFPSSE = isFP && (X86Vector >= SSE2);
1550 bool isValid = false;
1551 SDOperand Op0 = Op.getOperand(0);
1552 SDOperand Cond, CC;
1553 if (Op0.getOpcode() == X86ISD::SETCC) {
1554 CC = Op0.getOperand(0);
1555 Cond = Op0.getOperand(1);
1556 isValid =
Evan Cheng339edad2006-01-11 00:33:36 +00001557 !(isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()));
Evan Cheng73a1ad92006-01-10 20:26:56 +00001558 } else if (Op0.getOpcode() == ISD::SETCC) {
1559 CC = Op0.getOperand(2);
1560 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001561 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001562 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001563 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001564 Op0.getOperand(0), Op0.getOperand(1));
1565 isValid = true;
1566 }
1567
1568 if (!isValid) {
Evan Cheng172fce72006-01-06 00:43:03 +00001569 CC = DAG.getConstant(X86ISD::COND_E, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001570 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001571 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001572
1573 std::vector<MVT::ValueType> Tys;
1574 Tys.push_back(Op.getValueType());
1575 Tys.push_back(MVT::Flag);
1576 std::vector<SDOperand> Ops;
1577 Ops.push_back(Op.getOperand(1));
1578 Ops.push_back(Op.getOperand(2));
1579 Ops.push_back(CC);
1580 Ops.push_back(Cond);
1581 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001582 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001583 case ISD::BRCOND: {
Evan Cheng6fc31042005-12-19 23:12:38 +00001584 SDOperand Cond = Op.getOperand(1);
1585 SDOperand Dest = Op.getOperand(2);
1586 SDOperand CC;
1587 // TODO: handle Cond == OR / AND / XOR
Evan Chengc1583db2005-12-21 20:21:51 +00001588 if (Cond.getOpcode() == X86ISD::SETCC) {
1589 CC = Cond.getOperand(0);
1590 Cond = Cond.getOperand(1);
1591 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001592 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001593 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001594 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001595 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001596 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1597 Cond.getOperand(0), Cond.getOperand(1));
1598 } else {
Evan Cheng172fce72006-01-06 00:43:03 +00001599 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001600 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1601 }
1602 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1603 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1604 }
Evan Cheng172fce72006-01-06 00:43:03 +00001605 case ISD::RET: {
1606 // Can only be return void.
Evan Cheng9c249c32006-01-09 18:33:28 +00001607 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng172fce72006-01-06 00:43:03 +00001608 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1609 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001610 case ISD::GlobalAddress: {
Evan Chenga74ce622005-12-21 02:39:21 +00001611 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001612 SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga74ce622005-12-21 02:39:21 +00001613 // For Darwin, external and weak symbols are indirect, so we want to load
1614 // the value at address GV, not the value of GV itself. This means that
1615 // the GlobalAddress must be in the base or index register of the address,
1616 // not the GV offset field.
1617 if (getTargetMachine().
1618 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1619 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001620 return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1621 GVOp, DAG.getSrcValue(NULL));
Evan Chenga74ce622005-12-21 02:39:21 +00001622 else
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001623 return GVOp;
Evan Chenga74ce622005-12-21 02:39:21 +00001624 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001625 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001626 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001627}
Evan Cheng6af02632005-12-20 06:22:03 +00001628
1629const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1630 switch (Opcode) {
1631 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001632 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1633 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1634 case X86ISD::ADC: return "X86ISD::ADC";
1635 case X86ISD::SBB: return "X86ISD::SBB";
1636 case X86ISD::SHLD: return "X86ISD::SHLD";
1637 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6af02632005-12-20 06:22:03 +00001638 case X86ISD::FILD64m: return "X86ISD::FILD64m";
1639 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1640 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1641 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001642 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001643 case X86ISD::FST: return "X86ISD::FST";
1644 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001645 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001646 case X86ISD::CALL: return "X86ISD::CALL";
1647 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1648 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1649 case X86ISD::CMP: return "X86ISD::CMP";
1650 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001651 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001652 case X86ISD::CMOV: return "X86ISD::CMOV";
1653 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001654 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00001655 }
1656}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001657
1658bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1659 uint64_t Mask) const {
1660
1661 unsigned Opc = Op.getOpcode();
1662
1663 switch (Opc) {
1664 default:
1665 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1666 break;
1667 case X86ISD::SETCC: return (Mask & 1) == 0;
1668 }
1669
1670 return false;
1671}