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