blob: a5b261c83741d35530b9542b85f79e419f5ad170 [file] [log] [blame]
Chris Lattner76ac0682005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86ISelLowering.h"
17#include "X86TargetMachine.h"
18#include "llvm/CallingConv.h"
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SSARegMap.h"
24#include "llvm/Target/TargetOptions.h"
25using namespace llvm;
26
27// FIXME: temporary.
28#include "llvm/Support/CommandLine.h"
29static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
30 cl::desc("Enable fastcc on X86"));
31
32X86TargetLowering::X86TargetLowering(TargetMachine &TM)
33 : TargetLowering(TM) {
Chris Lattner76ac0682005-11-15 00:40:23 +000034 // Set up the TargetLowering object.
35
36 // X86 is weird, it always uses i8 for shift amounts and setcc results.
37 setShiftAmountType(MVT::i8);
38 setSetCCResultType(MVT::i8);
39 setSetCCResultContents(ZeroOrOneSetCCResult);
40 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
41
42 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000043 addRegisterClass(MVT::i8, X86::R8RegisterClass);
44 addRegisterClass(MVT::i16, X86::R16RegisterClass);
45 addRegisterClass(MVT::i32, X86::R32RegisterClass);
46
47 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
48 // operation.
49 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
50 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
51 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
52 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
53
54 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
55 // this operation.
56 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
57 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
58
59 if (!X86ScalarSSE) {
60 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
61 // isn't legal.
62 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
63 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
64 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
65 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
66 }
67
68 // Handle FP_TO_UINT by promoting the destination to a larger signed
69 // conversion.
70 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
71 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
72 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
73
74 if (!X86ScalarSSE)
75 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
76
77 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
78 // this operation.
79 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
80 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
81 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
82
Chris Lattner30107e62005-12-23 05:15:23 +000083 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
84 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
85
Evan Cheng6fc31042005-12-19 23:12:38 +000086 if (X86DAGIsel) {
87 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
88 }
Chris Lattner76ac0682005-11-15 00:40:23 +000089 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
90 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
91 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
92 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +000093 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +000094 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
95 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
96 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
97 setOperationAction(ISD::FREM , MVT::f64 , Expand);
98 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
99 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
100 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
101 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
103 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
104 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
105 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
106 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000107 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000108
109 setOperationAction(ISD::READIO , MVT::i1 , Expand);
110 setOperationAction(ISD::READIO , MVT::i8 , Expand);
111 setOperationAction(ISD::READIO , MVT::i16 , Expand);
112 setOperationAction(ISD::READIO , MVT::i32 , Expand);
113 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
114 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
115 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
116 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
117
118 // These should be promoted to a larger select which is supported.
119 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
120 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng225a4d02005-12-17 01:21:05 +0000121 // X86 wants to expand cmov itself.
122 if (X86DAGIsel) {
123 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
124 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000125 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
126 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
127 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000128 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000129 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000130
Chris Lattner9c415362005-11-29 06:16:21 +0000131 // We don't have line number support yet.
132 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskey9e296be2005-12-21 20:51:37 +0000133 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000134
Chris Lattner76ac0682005-11-15 00:40:23 +0000135 if (X86ScalarSSE) {
136 // Set up the FP register classes.
137 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
138 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
139
140 // SSE has no load+extend ops
141 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
142 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
143
144 // SSE has no i16 to fp conversion, only i32
145 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
146 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
147
148 // Expand FP_TO_UINT into a select.
149 // FIXME: We would like to use a Custom expander here eventually to do
150 // the optimal thing for SSE vs. the default expansion in the legalizer.
151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
152
153 // We don't support sin/cos/sqrt/fmod
154 setOperationAction(ISD::FSIN , MVT::f64, Expand);
155 setOperationAction(ISD::FCOS , MVT::f64, Expand);
156 setOperationAction(ISD::FABS , MVT::f64, Expand);
157 setOperationAction(ISD::FNEG , MVT::f64, Expand);
158 setOperationAction(ISD::FREM , MVT::f64, Expand);
159 setOperationAction(ISD::FSIN , MVT::f32, Expand);
160 setOperationAction(ISD::FCOS , MVT::f32, Expand);
161 setOperationAction(ISD::FABS , MVT::f32, Expand);
162 setOperationAction(ISD::FNEG , MVT::f32, Expand);
163 setOperationAction(ISD::FREM , MVT::f32, Expand);
164
165 addLegalFPImmediate(+0.0); // xorps / xorpd
166 } else {
167 // Set up the FP register classes.
168 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
169
170 if (!UnsafeFPMath) {
171 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
172 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
173 }
174
175 addLegalFPImmediate(+0.0); // FLD0
176 addLegalFPImmediate(+1.0); // FLD1
177 addLegalFPImmediate(-0.0); // FLD0/FCHS
178 addLegalFPImmediate(-1.0); // FLD1/FCHS
179 }
180 computeRegisterProperties();
181
182 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
183 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
184 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
185 allowUnalignedMemoryAccesses = true; // x86 supports it!
186}
187
188std::vector<SDOperand>
189X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
190 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
191 return LowerFastCCArguments(F, DAG);
192 return LowerCCCArguments(F, DAG);
193}
194
195std::pair<SDOperand, SDOperand>
196X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
197 bool isVarArg, unsigned CallingConv,
198 bool isTailCall,
199 SDOperand Callee, ArgListTy &Args,
200 SelectionDAG &DAG) {
201 assert((!isVarArg || CallingConv == CallingConv::C) &&
202 "Only C takes varargs!");
203 if (CallingConv == CallingConv::Fast && EnableFastCC)
204 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
205 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
206}
207
Evan Chenga74ce622005-12-21 02:39:21 +0000208SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
209 SelectionDAG &DAG) {
210 if (!X86DAGIsel)
211 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
212
213 SDOperand Copy;
214 MVT::ValueType OpVT = Op.getValueType();
215 switch (OpVT) {
216 default: assert(0 && "Unknown type to return!");
217 case MVT::i32:
218 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
219 break;
220 case MVT::i64: {
221 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
222 DAG.getConstant(1, MVT::i32));
223 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
224 DAG.getConstant(0, MVT::i32));
225 Copy = DAG.getCopyToReg(Chain, X86::EAX, Hi, SDOperand());
226 Copy = DAG.getCopyToReg(Copy, X86::EDX, Lo, Copy.getValue(1));
227 break;
228 }
229 case MVT::f32:
Evan Chenga74ce622005-12-21 02:39:21 +0000230 case MVT::f64:
231 if (!X86ScalarSSE) {
232 std::vector<MVT::ValueType> Tys;
233 Tys.push_back(MVT::Other);
234 Tys.push_back(MVT::Flag);
235 std::vector<SDOperand> Ops;
236 Ops.push_back(Chain);
Evan Cheng5c59d492005-12-23 07:31:11 +0000237 if (OpVT == MVT::f32)
238 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chenga74ce622005-12-21 02:39:21 +0000239 Ops.push_back(Op);
240 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
241 } else {
242 // Spill the value to memory and reload it into top of stack.
243 unsigned Size = MVT::getSizeInBits(OpVT)/8;
244 MachineFunction &MF = DAG.getMachineFunction();
245 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
246 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
247 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
248 StackSlot, DAG.getSrcValue(NULL));
249 std::vector<MVT::ValueType> Tys;
250 Tys.push_back(MVT::f64);
251 Tys.push_back(MVT::Other);
252 std::vector<SDOperand> Ops;
253 Ops.push_back(Chain);
254 Ops.push_back(StackSlot);
255 Ops.push_back(DAG.getValueType(OpVT));
256 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
257 Tys.clear();
258 Tys.push_back(MVT::Other);
259 Tys.push_back(MVT::Flag);
260 Ops.clear();
261 Ops.push_back(Copy.getValue(1));
262 Ops.push_back(Copy);
263 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
264 }
265 break;
266 }
Evan Chengc1583db2005-12-21 20:21:51 +0000267
268 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
269 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
270 Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000271}
272
Chris Lattner76ac0682005-11-15 00:40:23 +0000273//===----------------------------------------------------------------------===//
274// C Calling Convention implementation
275//===----------------------------------------------------------------------===//
276
277std::vector<SDOperand>
278X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
279 std::vector<SDOperand> ArgValues;
280
281 MachineFunction &MF = DAG.getMachineFunction();
282 MachineFrameInfo *MFI = MF.getFrameInfo();
283
284 // Add DAG nodes to load the arguments... On entry to a function on the X86,
285 // the stack frame looks like this:
286 //
287 // [ESP] -- return address
288 // [ESP + 4] -- first argument (leftmost lexically)
289 // [ESP + 8] -- second argument, if first argument is four bytes in size
290 // ...
291 //
292 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
293 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
294 MVT::ValueType ObjectVT = getValueType(I->getType());
295 unsigned ArgIncrement = 4;
296 unsigned ObjSize;
297 switch (ObjectVT) {
298 default: assert(0 && "Unhandled argument type!");
299 case MVT::i1:
300 case MVT::i8: ObjSize = 1; break;
301 case MVT::i16: ObjSize = 2; break;
302 case MVT::i32: ObjSize = 4; break;
303 case MVT::i64: ObjSize = ArgIncrement = 8; break;
304 case MVT::f32: ObjSize = 4; break;
305 case MVT::f64: ObjSize = ArgIncrement = 8; break;
306 }
307 // Create the frame index object for this incoming parameter...
308 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
309
310 // Create the SelectionDAG nodes corresponding to a load from this parameter
311 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
312
313 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
314 // dead loads.
315 SDOperand ArgValue;
316 if (!I->use_empty())
317 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
318 DAG.getSrcValue(NULL));
319 else {
320 if (MVT::isInteger(ObjectVT))
321 ArgValue = DAG.getConstant(0, ObjectVT);
322 else
323 ArgValue = DAG.getConstantFP(0, ObjectVT);
324 }
325 ArgValues.push_back(ArgValue);
326
327 ArgOffset += ArgIncrement; // Move on to the next argument...
328 }
329
330 // If the function takes variable number of arguments, make a frame index for
331 // the start of the first vararg value... for expansion of llvm.va_start.
332 if (F.isVarArg())
333 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
334 ReturnAddrIndex = 0; // No return address slot generated yet.
335 BytesToPopOnReturn = 0; // Callee pops nothing.
336 BytesCallerReserves = ArgOffset;
337
338 // Finally, inform the code generator which regs we return values in.
339 switch (getValueType(F.getReturnType())) {
340 default: assert(0 && "Unknown type!");
341 case MVT::isVoid: break;
342 case MVT::i1:
343 case MVT::i8:
344 case MVT::i16:
345 case MVT::i32:
346 MF.addLiveOut(X86::EAX);
347 break;
348 case MVT::i64:
349 MF.addLiveOut(X86::EAX);
350 MF.addLiveOut(X86::EDX);
351 break;
352 case MVT::f32:
353 case MVT::f64:
354 MF.addLiveOut(X86::ST0);
355 break;
356 }
357 return ArgValues;
358}
359
360std::pair<SDOperand, SDOperand>
361X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
362 bool isVarArg, bool isTailCall,
363 SDOperand Callee, ArgListTy &Args,
364 SelectionDAG &DAG) {
365 // Count how many bytes are to be pushed on the stack.
366 unsigned NumBytes = 0;
367
368 if (Args.empty()) {
369 // Save zero bytes.
370 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
371 DAG.getConstant(0, getPointerTy()));
372 } else {
373 for (unsigned i = 0, e = Args.size(); i != e; ++i)
374 switch (getValueType(Args[i].second)) {
375 default: assert(0 && "Unknown value type!");
376 case MVT::i1:
377 case MVT::i8:
378 case MVT::i16:
379 case MVT::i32:
380 case MVT::f32:
381 NumBytes += 4;
382 break;
383 case MVT::i64:
384 case MVT::f64:
385 NumBytes += 8;
386 break;
387 }
388
389 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
390 DAG.getConstant(NumBytes, getPointerTy()));
391
392 // Arguments go on the stack in reverse order, as specified by the ABI.
393 unsigned ArgOffset = 0;
394 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
395 X86::ESP, MVT::i32);
396 std::vector<SDOperand> Stores;
397
398 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
399 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
400 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
401
402 switch (getValueType(Args[i].second)) {
403 default: assert(0 && "Unexpected ValueType for argument!");
404 case MVT::i1:
405 case MVT::i8:
406 case MVT::i16:
407 // Promote the integer to 32 bits. If the input type is signed use a
408 // sign extend, otherwise use a zero extend.
409 if (Args[i].second->isSigned())
410 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
411 else
412 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
413
414 // FALL THROUGH
415 case MVT::i32:
416 case MVT::f32:
417 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
418 Args[i].first, PtrOff,
419 DAG.getSrcValue(NULL)));
420 ArgOffset += 4;
421 break;
422 case MVT::i64:
423 case MVT::f64:
424 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
425 Args[i].first, PtrOff,
426 DAG.getSrcValue(NULL)));
427 ArgOffset += 8;
428 break;
429 }
430 }
431 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
432 }
433
434 std::vector<MVT::ValueType> RetVals;
435 MVT::ValueType RetTyVT = getValueType(RetTy);
436 RetVals.push_back(MVT::Other);
437
438 // The result values produced have to be legal. Promote the result.
439 switch (RetTyVT) {
440 case MVT::isVoid: break;
441 default:
442 RetVals.push_back(RetTyVT);
443 break;
444 case MVT::i1:
445 case MVT::i8:
446 case MVT::i16:
447 RetVals.push_back(MVT::i32);
448 break;
449 case MVT::f32:
450 if (X86ScalarSSE)
451 RetVals.push_back(MVT::f32);
452 else
453 RetVals.push_back(MVT::f64);
454 break;
455 case MVT::i64:
456 RetVals.push_back(MVT::i32);
457 RetVals.push_back(MVT::i32);
458 break;
459 }
460 std::vector<SDOperand> Ops;
461 Ops.push_back(Chain);
462 Ops.push_back(Callee);
463 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
464 Ops.push_back(DAG.getConstant(0, getPointerTy()));
465 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
466 RetVals, Ops);
467 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
468
469 SDOperand ResultVal;
470 switch (RetTyVT) {
471 case MVT::isVoid: break;
472 default:
473 ResultVal = TheCall.getValue(1);
474 break;
475 case MVT::i1:
476 case MVT::i8:
477 case MVT::i16:
478 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
479 break;
480 case MVT::f32:
481 // FIXME: we would really like to remember that this FP_ROUND operation is
482 // okay to eliminate if we allow excess FP precision.
483 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
484 break;
485 case MVT::i64:
486 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
487 TheCall.getValue(2));
488 break;
489 }
490
491 return std::make_pair(ResultVal, Chain);
492}
493
494SDOperand
495X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
496 Value *VAListV, SelectionDAG &DAG) {
497 // vastart just stores the address of the VarArgsFrameIndex slot.
498 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
499 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
500 DAG.getSrcValue(VAListV));
501}
502
503
504std::pair<SDOperand,SDOperand>
505X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
506 Value *VAListV, const Type *ArgTy,
507 SelectionDAG &DAG) {
508 MVT::ValueType ArgVT = getValueType(ArgTy);
509 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
510 VAListP, DAG.getSrcValue(VAListV));
511 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
512 DAG.getSrcValue(NULL));
513 unsigned Amt;
514 if (ArgVT == MVT::i32)
515 Amt = 4;
516 else {
517 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
518 "Other types should have been promoted for varargs!");
519 Amt = 8;
520 }
521 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
522 DAG.getConstant(Amt, Val.getValueType()));
523 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
524 Val, VAListP, DAG.getSrcValue(VAListV));
525 return std::make_pair(Result, Chain);
526}
527
528//===----------------------------------------------------------------------===//
529// Fast Calling Convention implementation
530//===----------------------------------------------------------------------===//
531//
532// The X86 'fast' calling convention passes up to two integer arguments in
533// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
534// and requires that the callee pop its arguments off the stack (allowing proper
535// tail calls), and has the same return value conventions as C calling convs.
536//
537// This calling convention always arranges for the callee pop value to be 8n+4
538// bytes, which is needed for tail recursion elimination and stack alignment
539// reasons.
540//
541// Note that this can be enhanced in the future to pass fp vals in registers
542// (when we have a global fp allocator) and do other tricks.
543//
544
545/// AddLiveIn - This helper function adds the specified physical register to the
546/// MachineFunction as a live in value. It also creates a corresponding virtual
547/// register for it.
548static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
549 TargetRegisterClass *RC) {
550 assert(RC->contains(PReg) && "Not the correct regclass!");
551 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
552 MF.addLiveIn(PReg, VReg);
553 return VReg;
554}
555
556
557std::vector<SDOperand>
558X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
559 std::vector<SDOperand> ArgValues;
560
561 MachineFunction &MF = DAG.getMachineFunction();
562 MachineFrameInfo *MFI = MF.getFrameInfo();
563
564 // Add DAG nodes to load the arguments... On entry to a function the stack
565 // frame looks like this:
566 //
567 // [ESP] -- return address
568 // [ESP + 4] -- first nonreg argument (leftmost lexically)
569 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
570 // ...
571 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
572
573 // Keep track of the number of integer regs passed so far. This can be either
574 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
575 // used).
576 unsigned NumIntRegs = 0;
577
578 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
579 MVT::ValueType ObjectVT = getValueType(I->getType());
580 unsigned ArgIncrement = 4;
581 unsigned ObjSize = 0;
582 SDOperand ArgValue;
583
584 switch (ObjectVT) {
585 default: assert(0 && "Unhandled argument type!");
586 case MVT::i1:
587 case MVT::i8:
588 if (NumIntRegs < 2) {
589 if (!I->use_empty()) {
590 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
591 X86::R8RegisterClass);
592 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
593 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000594 if (ObjectVT == MVT::i1)
595 // FIXME: Should insert a assertzext here.
596 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000597 }
598 ++NumIntRegs;
599 break;
600 }
601
602 ObjSize = 1;
603 break;
604 case MVT::i16:
605 if (NumIntRegs < 2) {
606 if (!I->use_empty()) {
607 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
608 X86::R16RegisterClass);
609 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
610 DAG.setRoot(ArgValue.getValue(1));
611 }
612 ++NumIntRegs;
613 break;
614 }
615 ObjSize = 2;
616 break;
617 case MVT::i32:
618 if (NumIntRegs < 2) {
619 if (!I->use_empty()) {
620 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
621 X86::R32RegisterClass);
622 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
623 DAG.setRoot(ArgValue.getValue(1));
624 }
625 ++NumIntRegs;
626 break;
627 }
628 ObjSize = 4;
629 break;
630 case MVT::i64:
631 if (NumIntRegs == 0) {
632 if (!I->use_empty()) {
633 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
634 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
635
636 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
637 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
638 DAG.setRoot(Hi.getValue(1));
639
640 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
641 }
642 NumIntRegs = 2;
643 break;
644 } else if (NumIntRegs == 1) {
645 if (!I->use_empty()) {
646 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
647 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
648 DAG.setRoot(Low.getValue(1));
649
650 // Load the high part from memory.
651 // Create the frame index object for this incoming parameter...
652 int FI = MFI->CreateFixedObject(4, ArgOffset);
653 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
654 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
655 DAG.getSrcValue(NULL));
656 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
657 }
658 ArgOffset += 4;
659 NumIntRegs = 2;
660 break;
661 }
662 ObjSize = ArgIncrement = 8;
663 break;
664 case MVT::f32: ObjSize = 4; break;
665 case MVT::f64: ObjSize = ArgIncrement = 8; break;
666 }
667
668 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
669 // dead loads.
670 if (ObjSize && !I->use_empty()) {
671 // Create the frame index object for this incoming parameter...
672 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
673
674 // Create the SelectionDAG nodes corresponding to a load from this
675 // parameter.
676 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
677
678 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
679 DAG.getSrcValue(NULL));
680 } else if (ArgValue.Val == 0) {
681 if (MVT::isInteger(ObjectVT))
682 ArgValue = DAG.getConstant(0, ObjectVT);
683 else
684 ArgValue = DAG.getConstantFP(0, ObjectVT);
685 }
686 ArgValues.push_back(ArgValue);
687
688 if (ObjSize)
689 ArgOffset += ArgIncrement; // Move on to the next argument.
690 }
691
692 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
693 // arguments and the arguments after the retaddr has been pushed are aligned.
694 if ((ArgOffset & 7) == 0)
695 ArgOffset += 4;
696
697 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
698 ReturnAddrIndex = 0; // No return address slot generated yet.
699 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
700 BytesCallerReserves = 0;
701
702 // Finally, inform the code generator which regs we return values in.
703 switch (getValueType(F.getReturnType())) {
704 default: assert(0 && "Unknown type!");
705 case MVT::isVoid: break;
706 case MVT::i1:
707 case MVT::i8:
708 case MVT::i16:
709 case MVT::i32:
710 MF.addLiveOut(X86::EAX);
711 break;
712 case MVT::i64:
713 MF.addLiveOut(X86::EAX);
714 MF.addLiveOut(X86::EDX);
715 break;
716 case MVT::f32:
717 case MVT::f64:
718 MF.addLiveOut(X86::ST0);
719 break;
720 }
721 return ArgValues;
722}
723
724std::pair<SDOperand, SDOperand>
725X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
726 bool isTailCall, SDOperand Callee,
727 ArgListTy &Args, SelectionDAG &DAG) {
728 // Count how many bytes are to be pushed on the stack.
729 unsigned NumBytes = 0;
730
731 // Keep track of the number of integer regs passed so far. This can be either
732 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
733 // used).
734 unsigned NumIntRegs = 0;
735
736 for (unsigned i = 0, e = Args.size(); i != e; ++i)
737 switch (getValueType(Args[i].second)) {
738 default: assert(0 && "Unknown value type!");
739 case MVT::i1:
740 case MVT::i8:
741 case MVT::i16:
742 case MVT::i32:
743 if (NumIntRegs < 2) {
744 ++NumIntRegs;
745 break;
746 }
747 // fall through
748 case MVT::f32:
749 NumBytes += 4;
750 break;
751 case MVT::i64:
752 if (NumIntRegs == 0) {
753 NumIntRegs = 2;
754 break;
755 } else if (NumIntRegs == 1) {
756 NumIntRegs = 2;
757 NumBytes += 4;
758 break;
759 }
760
761 // fall through
762 case MVT::f64:
763 NumBytes += 8;
764 break;
765 }
766
767 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
768 // arguments and the arguments after the retaddr has been pushed are aligned.
769 if ((NumBytes & 7) == 0)
770 NumBytes += 4;
771
772 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
773 DAG.getConstant(NumBytes, getPointerTy()));
774
775 // Arguments go on the stack in reverse order, as specified by the ABI.
776 unsigned ArgOffset = 0;
777 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
778 X86::ESP, MVT::i32);
779 NumIntRegs = 0;
780 std::vector<SDOperand> Stores;
781 std::vector<SDOperand> RegValuesToPass;
782 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
783 switch (getValueType(Args[i].second)) {
784 default: assert(0 && "Unexpected ValueType for argument!");
785 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000786 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
787 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000788 case MVT::i8:
789 case MVT::i16:
790 case MVT::i32:
791 if (NumIntRegs < 2) {
792 RegValuesToPass.push_back(Args[i].first);
793 ++NumIntRegs;
794 break;
795 }
796 // Fall through
797 case MVT::f32: {
798 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
799 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
800 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
801 Args[i].first, PtrOff,
802 DAG.getSrcValue(NULL)));
803 ArgOffset += 4;
804 break;
805 }
806 case MVT::i64:
807 if (NumIntRegs < 2) { // Can pass part of it in regs?
808 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
809 Args[i].first, DAG.getConstant(1, MVT::i32));
810 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
811 Args[i].first, DAG.getConstant(0, MVT::i32));
812 RegValuesToPass.push_back(Lo);
813 ++NumIntRegs;
814 if (NumIntRegs < 2) { // Pass both parts in regs?
815 RegValuesToPass.push_back(Hi);
816 ++NumIntRegs;
817 } else {
818 // Pass the high part in memory.
819 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
820 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
821 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
822 Hi, PtrOff, DAG.getSrcValue(NULL)));
823 ArgOffset += 4;
824 }
825 break;
826 }
827 // Fall through
828 case MVT::f64:
829 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
830 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
831 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
832 Args[i].first, PtrOff,
833 DAG.getSrcValue(NULL)));
834 ArgOffset += 8;
835 break;
836 }
837 }
838 if (!Stores.empty())
839 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
840
841 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
842 // arguments and the arguments after the retaddr has been pushed are aligned.
843 if ((ArgOffset & 7) == 0)
844 ArgOffset += 4;
845
846 std::vector<MVT::ValueType> RetVals;
847 MVT::ValueType RetTyVT = getValueType(RetTy);
848
849 RetVals.push_back(MVT::Other);
850
851 // The result values produced have to be legal. Promote the result.
852 switch (RetTyVT) {
853 case MVT::isVoid: break;
854 default:
855 RetVals.push_back(RetTyVT);
856 break;
857 case MVT::i1:
858 case MVT::i8:
859 case MVT::i16:
860 RetVals.push_back(MVT::i32);
861 break;
862 case MVT::f32:
863 if (X86ScalarSSE)
864 RetVals.push_back(MVT::f32);
865 else
866 RetVals.push_back(MVT::f64);
867 break;
868 case MVT::i64:
869 RetVals.push_back(MVT::i32);
870 RetVals.push_back(MVT::i32);
871 break;
872 }
873
874 std::vector<SDOperand> Ops;
875 Ops.push_back(Chain);
876 Ops.push_back(Callee);
877 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
878 // Callee pops all arg values on the stack.
879 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
880
881 // Pass register arguments as needed.
882 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
883
884 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
885 RetVals, Ops);
886 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
887
888 SDOperand ResultVal;
889 switch (RetTyVT) {
890 case MVT::isVoid: break;
891 default:
892 ResultVal = TheCall.getValue(1);
893 break;
894 case MVT::i1:
895 case MVT::i8:
896 case MVT::i16:
897 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
898 break;
899 case MVT::f32:
900 // FIXME: we would really like to remember that this FP_ROUND operation is
901 // okay to eliminate if we allow excess FP precision.
902 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
903 break;
904 case MVT::i64:
905 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
906 TheCall.getValue(2));
907 break;
908 }
909
910 return std::make_pair(ResultVal, Chain);
911}
912
913SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
914 if (ReturnAddrIndex == 0) {
915 // Set up a frame object for the return address.
916 MachineFunction &MF = DAG.getMachineFunction();
917 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
918 }
919
920 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
921}
922
923
924
925std::pair<SDOperand, SDOperand> X86TargetLowering::
926LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
927 SelectionDAG &DAG) {
928 SDOperand Result;
929 if (Depth) // Depths > 0 not supported yet!
930 Result = DAG.getConstant(0, getPointerTy());
931 else {
932 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
933 if (!isFrameAddress)
934 // Just load the return address
935 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
936 DAG.getSrcValue(NULL));
937 else
938 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
939 DAG.getConstant(4, MVT::i32));
940 }
941 return std::make_pair(Result, Chain);
942}
943
944//===----------------------------------------------------------------------===//
945// X86 Custom Lowering Hooks
946//===----------------------------------------------------------------------===//
947
948/// LowerOperation - Provide custom lowering hooks for some operations.
949///
950SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
951 switch (Op.getOpcode()) {
952 default: assert(0 && "Should not custom lower this!");
953 case ISD::SINT_TO_FP: {
954 assert(Op.getValueType() == MVT::f64 &&
955 Op.getOperand(0).getValueType() == MVT::i64 &&
956 "Unknown SINT_TO_FP to lower!");
957 // We lower sint64->FP into a store to a temporary stack slot, followed by a
958 // FILD64m node.
959 MachineFunction &MF = DAG.getMachineFunction();
960 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
961 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
962 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
963 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
964 std::vector<MVT::ValueType> RTs;
965 RTs.push_back(MVT::f64);
966 RTs.push_back(MVT::Other);
967 std::vector<SDOperand> Ops;
968 Ops.push_back(Store);
969 Ops.push_back(StackSlot);
970 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
971 }
972 case ISD::FP_TO_SINT: {
973 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
974 Op.getOperand(0).getValueType() == MVT::f64 &&
975 "Unknown FP_TO_SINT to lower!");
976 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
977 // stack slot.
978 MachineFunction &MF = DAG.getMachineFunction();
979 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
980 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
981 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
982
983 unsigned Opc;
984 switch (Op.getValueType()) {
985 default: assert(0 && "Invalid FP_TO_SINT to lower!");
986 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
987 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
988 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
989 }
990
991 // Build the FP_TO_INT*_IN_MEM
992 std::vector<SDOperand> Ops;
993 Ops.push_back(DAG.getEntryNode());
994 Ops.push_back(Op.getOperand(0));
995 Ops.push_back(StackSlot);
996 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
997
998 // Load the result.
999 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1000 DAG.getSrcValue(NULL));
1001 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001002 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001003 std::vector<MVT::ValueType> Tys;
1004 Tys.push_back(MVT::Other);
1005 Tys.push_back(MVT::Flag);
1006 std::vector<SDOperand> Ops;
1007 Ops.push_back(Op.getOperand(0));
1008 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001009 Ops.clear();
1010 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1011 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1012 MVT::i32, Ops[0].getValue(2)));
1013 Ops.push_back(Ops[1].getValue(1));
1014 Tys[0] = Tys[1] = MVT::i32;
1015 Tys.push_back(MVT::Other);
1016 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001017 }
Evan Chengc1583db2005-12-21 20:21:51 +00001018 case ISD::SETCC: {
1019 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1020 SDOperand CC = Op.getOperand(2);
1021 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1022 Op.getOperand(0), Op.getOperand(1));
1023 return DAG.getNode(X86ISD::SETCC, MVT::i8, CC, Cond);
1024 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001025 case ISD::SELECT: {
Evan Cheng225a4d02005-12-17 01:21:05 +00001026 SDOperand Cond = Op.getOperand(0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001027 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001028 if (Cond.getOpcode() == X86ISD::SETCC) {
1029 CC = Cond.getOperand(0);
1030 Cond = Cond.getOperand(1);
1031 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng225a4d02005-12-17 01:21:05 +00001032 CC = Cond.getOperand(2);
1033 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1034 Cond.getOperand(0), Cond.getOperand(1));
1035 } else {
1036 CC = DAG.getCondCode(ISD::SETEQ);
1037 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1038 }
1039 return DAG.getNode(X86ISD::CMOV, Op.getValueType(),
1040 Op.getOperand(1), Op.getOperand(2), CC, Cond);
1041 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001042 case ISD::BRCOND: {
Evan Cheng6fc31042005-12-19 23:12:38 +00001043 SDOperand Cond = Op.getOperand(1);
1044 SDOperand Dest = Op.getOperand(2);
1045 SDOperand CC;
1046 // TODO: handle Cond == OR / AND / XOR
Evan Chengc1583db2005-12-21 20:21:51 +00001047 if (Cond.getOpcode() == X86ISD::SETCC) {
1048 CC = Cond.getOperand(0);
1049 Cond = Cond.getOperand(1);
1050 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001051 CC = Cond.getOperand(2);
1052 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1053 Cond.getOperand(0), Cond.getOperand(1));
1054 } else {
1055 CC = DAG.getCondCode(ISD::SETNE);
1056 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1057 }
1058 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1059 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1060 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001061 case ISD::GlobalAddress: {
Evan Chenga74ce622005-12-21 02:39:21 +00001062 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001063 SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga74ce622005-12-21 02:39:21 +00001064 // For Darwin, external and weak symbols are indirect, so we want to load
1065 // the value at address GV, not the value of GV itself. This means that
1066 // the GlobalAddress must be in the base or index register of the address,
1067 // not the GV offset field.
1068 if (getTargetMachine().
1069 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1070 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001071 return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1072 GVOp, DAG.getSrcValue(NULL));
Evan Chenga74ce622005-12-21 02:39:21 +00001073 else
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001074 return GVOp;
Evan Chenga74ce622005-12-21 02:39:21 +00001075 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001076 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001077 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001078}
Evan Cheng6af02632005-12-20 06:22:03 +00001079
1080const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1081 switch (Opcode) {
1082 default: return NULL;
1083 case X86ISD::FILD64m: return "X86ISD::FILD64m";
1084 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1085 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1086 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001087 case X86ISD::FLD: return "X86ISD::FLD";
1088 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001089 case X86ISD::CALL: return "X86ISD::CALL";
1090 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1091 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1092 case X86ISD::CMP: return "X86ISD::CMP";
1093 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001094 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001095 case X86ISD::CMOV: return "X86ISD::CMOV";
1096 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001097 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00001098 }
1099}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001100
1101bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1102 uint64_t Mask) const {
1103
1104 unsigned Opc = Op.getOpcode();
1105
1106 switch (Opc) {
1107 default:
1108 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1109 break;
1110 case X86ISD::SETCC: return (Mask & 1) == 0;
1111 }
1112
1113 return false;
1114}