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