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