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