X86ISelLowering.cpp revision a54aa94197d5af9a72c0de422a58e938da80b2a4
1//===-- 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 "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
20#include "llvm/Function.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetOptions.h"
27using namespace llvm;
28
29// FIXME: temporary.
30#include "llvm/Support/CommandLine.h"
31static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
32                                  cl::desc("Enable fastcc on X86"));
33
34X86TargetLowering::X86TargetLowering(TargetMachine &TM)
35  : TargetLowering(TM) {
36  Subtarget = &TM.getSubtarget<X86Subtarget>();
37  X86ScalarSSE = Subtarget->hasSSE2();
38
39  // Set up the TargetLowering object.
40
41  // X86 is weird, it always uses i8 for shift amounts and setcc results.
42  setShiftAmountType(MVT::i8);
43  setSetCCResultType(MVT::i8);
44  setSetCCResultContents(ZeroOrOneSetCCResult);
45  setSchedulingPreference(SchedulingForRegPressure);
46  setShiftAmountFlavor(Mask);   // shl X, 32 == shl X, 0
47  setStackPointerRegisterToSaveRestore(X86::ESP);
48
49  // Set up the register classes.
50  addRegisterClass(MVT::i8, X86::R8RegisterClass);
51  addRegisterClass(MVT::i16, X86::R16RegisterClass);
52  addRegisterClass(MVT::i32, X86::R32RegisterClass);
53
54  // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
55  // operation.
56  setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
57  setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
58  setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
59
60  if (X86ScalarSSE)
61    // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
62    setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Expand);
63  else
64    setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Promote);
65
66  // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
67  // this operation.
68  setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
69  setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
70
71  if (!X86ScalarSSE) {
72    // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
73    // isn't legal.
74    setOperationAction(ISD::SINT_TO_FP     , MVT::i64  , Custom);
75    setOperationAction(ISD::FP_TO_SINT     , MVT::i64  , Custom);
76    setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
77    setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
78  }
79
80  // Handle FP_TO_UINT by promoting the destination to a larger signed
81  // conversion.
82  setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
83  setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
84  setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
85
86  if (!X86ScalarSSE)
87    setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Promote);
88
89  // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
90  // this operation.
91  setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
92  setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
93  setOperationAction(ISD::FP_TO_SINT       , MVT::i16  , Promote);
94
95  setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
96  setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
97
98  if (!X86PatIsel) {
99    setOperationAction(ISD::BRCOND         , MVT::Other, Custom);
100  }
101  setOperationAction(ISD::BRCONDTWOWAY     , MVT::Other, Expand);
102  setOperationAction(ISD::BRTWOWAY_CC      , MVT::Other, Expand);
103  setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
104  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Expand);
105  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Expand);
106  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
107  setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
108  setOperationAction(ISD::SEXTLOAD         , MVT::i1   , Expand);
109  setOperationAction(ISD::FREM             , MVT::f64  , Expand);
110  setOperationAction(ISD::CTPOP            , MVT::i8   , Expand);
111  setOperationAction(ISD::CTTZ             , MVT::i8   , Expand);
112  setOperationAction(ISD::CTLZ             , MVT::i8   , Expand);
113  setOperationAction(ISD::CTPOP            , MVT::i16  , Expand);
114  setOperationAction(ISD::CTTZ             , MVT::i16  , Expand);
115  setOperationAction(ISD::CTLZ             , MVT::i16  , Expand);
116  setOperationAction(ISD::CTPOP            , MVT::i32  , Expand);
117  setOperationAction(ISD::CTTZ             , MVT::i32  , Expand);
118  setOperationAction(ISD::CTLZ             , MVT::i32  , Expand);
119  setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
120
121  if (X86PatIsel) {
122    setOperationAction(ISD::BSWAP          , MVT::i32  , Expand);
123    setOperationAction(ISD::ROTL           , MVT::i8   , Expand);
124    setOperationAction(ISD::ROTR           , MVT::i8   , Expand);
125    setOperationAction(ISD::ROTL           , MVT::i16  , Expand);
126    setOperationAction(ISD::ROTR           , MVT::i16  , Expand);
127    setOperationAction(ISD::ROTL           , MVT::i32  , Expand);
128    setOperationAction(ISD::ROTR           , MVT::i32  , Expand);
129  }
130  setOperationAction(ISD::BSWAP            , MVT::i16  , Expand);
131
132  setOperationAction(ISD::READIO           , MVT::i1   , Expand);
133  setOperationAction(ISD::READIO           , MVT::i8   , Expand);
134  setOperationAction(ISD::READIO           , MVT::i16  , Expand);
135  setOperationAction(ISD::READIO           , MVT::i32  , Expand);
136  setOperationAction(ISD::WRITEIO          , MVT::i1   , Expand);
137  setOperationAction(ISD::WRITEIO          , MVT::i8   , Expand);
138  setOperationAction(ISD::WRITEIO          , MVT::i16  , Expand);
139  setOperationAction(ISD::WRITEIO          , MVT::i32  , Expand);
140
141  // These should be promoted to a larger select which is supported.
142  setOperationAction(ISD::SELECT           , MVT::i1   , Promote);
143  setOperationAction(ISD::SELECT           , MVT::i8   , Promote);
144  if (!X86PatIsel) {
145    // X86 wants to expand cmov itself.
146    setOperationAction(ISD::SELECT         , MVT::i16  , Custom);
147    setOperationAction(ISD::SELECT         , MVT::i32  , Custom);
148    setOperationAction(ISD::SELECT         , MVT::f32  , Custom);
149    setOperationAction(ISD::SELECT         , MVT::f64  , Custom);
150    setOperationAction(ISD::SETCC          , MVT::i8   , Custom);
151    setOperationAction(ISD::SETCC          , MVT::i16  , Custom);
152    setOperationAction(ISD::SETCC          , MVT::i32  , Custom);
153    setOperationAction(ISD::SETCC          , MVT::f32  , Custom);
154    setOperationAction(ISD::SETCC          , MVT::f64  , Custom);
155    // X86 ret instruction may pop stack.
156    setOperationAction(ISD::RET            , MVT::Other, Custom);
157    // Darwin ABI issue.
158    setOperationAction(ISD::GlobalAddress  , MVT::i32  , Custom);
159    // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
160    setOperationAction(ISD::ADD_PARTS      , MVT::i32  , Custom);
161    setOperationAction(ISD::SUB_PARTS      , MVT::i32  , Custom);
162    setOperationAction(ISD::SHL_PARTS      , MVT::i32  , Custom);
163    setOperationAction(ISD::SRA_PARTS      , MVT::i32  , Custom);
164    setOperationAction(ISD::SRL_PARTS      , MVT::i32  , Custom);
165    // X86 wants to expand memset / memcpy itself.
166    setOperationAction(ISD::MEMSET         , MVT::Other, Custom);
167    setOperationAction(ISD::MEMCPY         , MVT::Other, Custom);
168  }
169
170  // We don't have line number support yet.
171  setOperationAction(ISD::LOCATION, MVT::Other, Expand);
172  setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
173  setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
174
175  // VASTART needs to be custom lowered to use the VarArgsFrameIndex
176  setOperationAction(ISD::VASTART           , MVT::Other, Custom);
177
178  // Use the default implementation.
179  setOperationAction(ISD::VAARG             , MVT::Other, Expand);
180  setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
181  setOperationAction(ISD::VAEND             , MVT::Other, Expand);
182  setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
183  setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
184  setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Expand);
185
186  if (X86ScalarSSE) {
187    // Set up the FP register classes.
188    addRegisterClass(MVT::f32, X86::FR32RegisterClass);
189    addRegisterClass(MVT::f64, X86::FR64RegisterClass);
190
191    // SSE has no load+extend ops
192    setOperationAction(ISD::EXTLOAD,  MVT::f32, Expand);
193    setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
194
195    // SSE has no i16 to fp conversion, only i32
196    setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
197    setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
198
199    // Expand FP_TO_UINT into a select.
200    // FIXME: We would like to use a Custom expander here eventually to do
201    // the optimal thing for SSE vs. the default expansion in the legalizer.
202    setOperationAction(ISD::FP_TO_UINT       , MVT::i32  , Expand);
203
204    // We don't support sin/cos/sqrt/fmod
205    setOperationAction(ISD::FSIN , MVT::f64, Expand);
206    setOperationAction(ISD::FCOS , MVT::f64, Expand);
207    setOperationAction(ISD::FABS , MVT::f64, Expand);
208    setOperationAction(ISD::FNEG , MVT::f64, Expand);
209    setOperationAction(ISD::FREM , MVT::f64, Expand);
210    setOperationAction(ISD::FSIN , MVT::f32, Expand);
211    setOperationAction(ISD::FCOS , MVT::f32, Expand);
212    setOperationAction(ISD::FABS , MVT::f32, Expand);
213    setOperationAction(ISD::FNEG , MVT::f32, Expand);
214    setOperationAction(ISD::FREM , MVT::f32, Expand);
215
216    // Expand FP immediates into loads from the stack, except for the special
217    // cases we handle.
218    setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
219    setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
220    addLegalFPImmediate(+0.0); // xorps / xorpd
221  } else {
222    // Set up the FP register classes.
223    addRegisterClass(MVT::f64, X86::RFPRegisterClass);
224
225    if (!X86PatIsel) {
226      setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
227      setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
228    }
229
230    if (!UnsafeFPMath) {
231      setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
232      setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
233    }
234
235    setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
236    addLegalFPImmediate(+0.0); // FLD0
237    addLegalFPImmediate(+1.0); // FLD1
238    addLegalFPImmediate(-0.0); // FLD0/FCHS
239    addLegalFPImmediate(-1.0); // FLD1/FCHS
240  }
241  computeRegisterProperties();
242
243  maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
244  maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
245  maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
246  allowUnalignedMemoryAccesses = true; // x86 supports it!
247}
248
249std::vector<SDOperand>
250X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
251  if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
252    return LowerFastCCArguments(F, DAG);
253  return LowerCCCArguments(F, DAG);
254}
255
256std::pair<SDOperand, SDOperand>
257X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
258                               bool isVarArg, unsigned CallingConv,
259                               bool isTailCall,
260                               SDOperand Callee, ArgListTy &Args,
261                               SelectionDAG &DAG) {
262  assert((!isVarArg || CallingConv == CallingConv::C) &&
263         "Only C takes varargs!");
264
265  // If the callee is a GlobalAddress node (quite common, every direct call is)
266  // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
267  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
268    Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
269  else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
270    Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
271
272  if (CallingConv == CallingConv::Fast && EnableFastCC)
273    return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
274  return  LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
275}
276
277//===----------------------------------------------------------------------===//
278//                    C Calling Convention implementation
279//===----------------------------------------------------------------------===//
280
281std::vector<SDOperand>
282X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
283  std::vector<SDOperand> ArgValues;
284
285  MachineFunction &MF = DAG.getMachineFunction();
286  MachineFrameInfo *MFI = MF.getFrameInfo();
287
288  // Add DAG nodes to load the arguments...  On entry to a function on the X86,
289  // the stack frame looks like this:
290  //
291  // [ESP] -- return address
292  // [ESP + 4] -- first argument (leftmost lexically)
293  // [ESP + 8] -- second argument, if first argument is four bytes in size
294  //    ...
295  //
296  unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
297  for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
298    MVT::ValueType ObjectVT = getValueType(I->getType());
299    unsigned ArgIncrement = 4;
300    unsigned ObjSize;
301    switch (ObjectVT) {
302    default: assert(0 && "Unhandled argument type!");
303    case MVT::i1:
304    case MVT::i8:  ObjSize = 1;                break;
305    case MVT::i16: ObjSize = 2;                break;
306    case MVT::i32: ObjSize = 4;                break;
307    case MVT::i64: ObjSize = ArgIncrement = 8; break;
308    case MVT::f32: ObjSize = 4;                break;
309    case MVT::f64: ObjSize = ArgIncrement = 8; break;
310    }
311    // Create the frame index object for this incoming parameter...
312    int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
313
314    // Create the SelectionDAG nodes corresponding to a load from this parameter
315    SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
316
317    // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
318    // dead loads.
319    SDOperand ArgValue;
320    if (!I->use_empty())
321      ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
322                             DAG.getSrcValue(NULL));
323    else {
324      if (MVT::isInteger(ObjectVT))
325        ArgValue = DAG.getConstant(0, ObjectVT);
326      else
327        ArgValue = DAG.getConstantFP(0, ObjectVT);
328    }
329    ArgValues.push_back(ArgValue);
330
331    ArgOffset += ArgIncrement;   // Move on to the next argument...
332  }
333
334  // If the function takes variable number of arguments, make a frame index for
335  // the start of the first vararg value... for expansion of llvm.va_start.
336  if (F.isVarArg())
337    VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
338  ReturnAddrIndex = 0;     // No return address slot generated yet.
339  BytesToPopOnReturn = 0;  // Callee pops nothing.
340  BytesCallerReserves = ArgOffset;
341
342  // Finally, inform the code generator which regs we return values in.
343  switch (getValueType(F.getReturnType())) {
344  default: assert(0 && "Unknown type!");
345  case MVT::isVoid: break;
346  case MVT::i1:
347  case MVT::i8:
348  case MVT::i16:
349  case MVT::i32:
350    MF.addLiveOut(X86::EAX);
351    break;
352  case MVT::i64:
353    MF.addLiveOut(X86::EAX);
354    MF.addLiveOut(X86::EDX);
355    break;
356  case MVT::f32:
357  case MVT::f64:
358    MF.addLiveOut(X86::ST0);
359    break;
360  }
361  return ArgValues;
362}
363
364std::pair<SDOperand, SDOperand>
365X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
366                                  bool isVarArg, bool isTailCall,
367                                  SDOperand Callee, ArgListTy &Args,
368                                  SelectionDAG &DAG) {
369  // Count how many bytes are to be pushed on the stack.
370  unsigned NumBytes = 0;
371
372  if (Args.empty()) {
373    // Save zero bytes.
374    Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
375                        DAG.getConstant(0, getPointerTy()));
376  } else {
377    for (unsigned i = 0, e = Args.size(); i != e; ++i)
378      switch (getValueType(Args[i].second)) {
379      default: assert(0 && "Unknown value type!");
380      case MVT::i1:
381      case MVT::i8:
382      case MVT::i16:
383      case MVT::i32:
384      case MVT::f32:
385        NumBytes += 4;
386        break;
387      case MVT::i64:
388      case MVT::f64:
389        NumBytes += 8;
390        break;
391      }
392
393    Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
394                        DAG.getConstant(NumBytes, getPointerTy()));
395
396    // Arguments go on the stack in reverse order, as specified by the ABI.
397    unsigned ArgOffset = 0;
398    SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
399    std::vector<SDOperand> Stores;
400
401    for (unsigned i = 0, e = Args.size(); i != e; ++i) {
402      SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
403      PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
404
405      switch (getValueType(Args[i].second)) {
406      default: assert(0 && "Unexpected ValueType for argument!");
407      case MVT::i1:
408      case MVT::i8:
409      case MVT::i16:
410        // Promote the integer to 32 bits.  If the input type is signed use a
411        // sign extend, otherwise use a zero extend.
412        if (Args[i].second->isSigned())
413          Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
414        else
415          Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
416
417        // FALL THROUGH
418      case MVT::i32:
419      case MVT::f32:
420        Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
421                                     Args[i].first, PtrOff,
422                                     DAG.getSrcValue(NULL)));
423        ArgOffset += 4;
424        break;
425      case MVT::i64:
426      case MVT::f64:
427        Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
428                                     Args[i].first, PtrOff,
429                                     DAG.getSrcValue(NULL)));
430        ArgOffset += 8;
431        break;
432      }
433    }
434    Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
435  }
436
437  std::vector<MVT::ValueType> RetVals;
438  MVT::ValueType RetTyVT = getValueType(RetTy);
439  RetVals.push_back(MVT::Other);
440
441  // The result values produced have to be legal.  Promote the result.
442  switch (RetTyVT) {
443  case MVT::isVoid: break;
444  default:
445    RetVals.push_back(RetTyVT);
446    break;
447  case MVT::i1:
448  case MVT::i8:
449  case MVT::i16:
450    RetVals.push_back(MVT::i32);
451    break;
452  case MVT::f32:
453    if (X86ScalarSSE)
454      RetVals.push_back(MVT::f32);
455    else
456      RetVals.push_back(MVT::f64);
457    break;
458  case MVT::i64:
459    RetVals.push_back(MVT::i32);
460    RetVals.push_back(MVT::i32);
461    break;
462  }
463
464  if (!X86PatIsel) {
465    std::vector<MVT::ValueType> NodeTys;
466    NodeTys.push_back(MVT::Other);   // Returns a chain
467    NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
468    std::vector<SDOperand> Ops;
469    Ops.push_back(Chain);
470    Ops.push_back(Callee);
471
472    // FIXME: Do not generate X86ISD::TAILCALL for now.
473    Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
474    SDOperand InFlag = Chain.getValue(1);
475
476    NodeTys.clear();
477    NodeTys.push_back(MVT::Other);   // Returns a chain
478    NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
479    Ops.clear();
480    Ops.push_back(Chain);
481    Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
482    Ops.push_back(DAG.getConstant(0, getPointerTy()));
483    Ops.push_back(InFlag);
484    Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
485    InFlag = Chain.getValue(1);
486
487    SDOperand RetVal;
488    if (RetTyVT != MVT::isVoid) {
489      switch (RetTyVT) {
490      default: assert(0 && "Unknown value type to return!");
491      case MVT::i1:
492      case MVT::i8:
493        RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
494        Chain = RetVal.getValue(1);
495        if (RetTyVT == MVT::i1)
496          RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
497        break;
498      case MVT::i16:
499        RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
500        Chain = RetVal.getValue(1);
501        break;
502      case MVT::i32:
503        RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
504        Chain = RetVal.getValue(1);
505        break;
506      case MVT::i64: {
507        SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
508        SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
509                                          Lo.getValue(2));
510        RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
511        Chain = Hi.getValue(1);
512        break;
513      }
514      case MVT::f32:
515      case MVT::f64: {
516        std::vector<MVT::ValueType> Tys;
517        Tys.push_back(MVT::f64);
518        Tys.push_back(MVT::Other);
519        Tys.push_back(MVT::Flag);
520        std::vector<SDOperand> Ops;
521        Ops.push_back(Chain);
522        Ops.push_back(InFlag);
523        RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
524        Chain  = RetVal.getValue(1);
525        InFlag = RetVal.getValue(2);
526        if (X86ScalarSSE) {
527          // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
528          // shouldn't be necessary except for RFP cannot be live across
529          // multiple blocks. When stackifier is fixed, they can be uncoupled.
530          unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
531          MachineFunction &MF = DAG.getMachineFunction();
532          int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
533          SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
534          Tys.clear();
535          Tys.push_back(MVT::Other);
536          Ops.clear();
537          Ops.push_back(Chain);
538          Ops.push_back(RetVal);
539          Ops.push_back(StackSlot);
540          Ops.push_back(DAG.getValueType(RetTyVT));
541          Ops.push_back(InFlag);
542          Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
543          RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
544                               DAG.getSrcValue(NULL));
545          Chain = RetVal.getValue(1);
546        }
547
548        if (RetTyVT == MVT::f32 && !X86ScalarSSE)
549          // FIXME: we would really like to remember that this FP_ROUND
550          // operation is okay to eliminate if we allow excess FP precision.
551          RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
552        break;
553      }
554      }
555    }
556
557    return std::make_pair(RetVal, Chain);
558  } else {
559    std::vector<SDOperand> Ops;
560    Ops.push_back(Chain);
561    Ops.push_back(Callee);
562    Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
563    Ops.push_back(DAG.getConstant(0, getPointerTy()));
564
565    SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
566                                    RetVals, Ops);
567
568    SDOperand ResultVal;
569    switch (RetTyVT) {
570    case MVT::isVoid: break;
571    default:
572      ResultVal = TheCall.getValue(1);
573      break;
574    case MVT::i1:
575    case MVT::i8:
576    case MVT::i16:
577      ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
578      break;
579    case MVT::f32:
580      // FIXME: we would really like to remember that this FP_ROUND operation is
581      // okay to eliminate if we allow excess FP precision.
582      ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
583      break;
584    case MVT::i64:
585      ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
586                              TheCall.getValue(2));
587      break;
588    }
589
590    Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
591    return std::make_pair(ResultVal, Chain);
592  }
593}
594
595//===----------------------------------------------------------------------===//
596//                    Fast Calling Convention implementation
597//===----------------------------------------------------------------------===//
598//
599// The X86 'fast' calling convention passes up to two integer arguments in
600// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
601// and requires that the callee pop its arguments off the stack (allowing proper
602// tail calls), and has the same return value conventions as C calling convs.
603//
604// This calling convention always arranges for the callee pop value to be 8n+4
605// bytes, which is needed for tail recursion elimination and stack alignment
606// reasons.
607//
608// Note that this can be enhanced in the future to pass fp vals in registers
609// (when we have a global fp allocator) and do other tricks.
610//
611
612/// AddLiveIn - This helper function adds the specified physical register to the
613/// MachineFunction as a live in value.  It also creates a corresponding virtual
614/// register for it.
615static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
616                          TargetRegisterClass *RC) {
617  assert(RC->contains(PReg) && "Not the correct regclass!");
618  unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
619  MF.addLiveIn(PReg, VReg);
620  return VReg;
621}
622
623
624std::vector<SDOperand>
625X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
626  std::vector<SDOperand> ArgValues;
627
628  MachineFunction &MF = DAG.getMachineFunction();
629  MachineFrameInfo *MFI = MF.getFrameInfo();
630
631  // Add DAG nodes to load the arguments...  On entry to a function the stack
632  // frame looks like this:
633  //
634  // [ESP] -- return address
635  // [ESP + 4] -- first nonreg argument (leftmost lexically)
636  // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
637  //    ...
638  unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
639
640  // Keep track of the number of integer regs passed so far.  This can be either
641  // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
642  // used).
643  unsigned NumIntRegs = 0;
644
645  for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
646    MVT::ValueType ObjectVT = getValueType(I->getType());
647    unsigned ArgIncrement = 4;
648    unsigned ObjSize = 0;
649    SDOperand ArgValue;
650
651    switch (ObjectVT) {
652    default: assert(0 && "Unhandled argument type!");
653    case MVT::i1:
654    case MVT::i8:
655      if (NumIntRegs < 2) {
656        if (!I->use_empty()) {
657          unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
658                                    X86::R8RegisterClass);
659          ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
660          DAG.setRoot(ArgValue.getValue(1));
661          if (ObjectVT == MVT::i1)
662            // FIXME: Should insert a assertzext here.
663            ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
664        }
665        ++NumIntRegs;
666        break;
667      }
668
669      ObjSize = 1;
670      break;
671    case MVT::i16:
672      if (NumIntRegs < 2) {
673        if (!I->use_empty()) {
674          unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
675                                    X86::R16RegisterClass);
676          ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
677          DAG.setRoot(ArgValue.getValue(1));
678        }
679        ++NumIntRegs;
680        break;
681      }
682      ObjSize = 2;
683      break;
684    case MVT::i32:
685      if (NumIntRegs < 2) {
686        if (!I->use_empty()) {
687          unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
688                                    X86::R32RegisterClass);
689          ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
690          DAG.setRoot(ArgValue.getValue(1));
691        }
692        ++NumIntRegs;
693        break;
694      }
695      ObjSize = 4;
696      break;
697    case MVT::i64:
698      if (NumIntRegs == 0) {
699        if (!I->use_empty()) {
700          unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
701          unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
702
703          SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
704          SDOperand Hi  = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
705          DAG.setRoot(Hi.getValue(1));
706
707          ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
708        }
709        NumIntRegs = 2;
710        break;
711      } else if (NumIntRegs == 1) {
712        if (!I->use_empty()) {
713          unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
714          SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
715          DAG.setRoot(Low.getValue(1));
716
717          // Load the high part from memory.
718          // Create the frame index object for this incoming parameter...
719          int FI = MFI->CreateFixedObject(4, ArgOffset);
720          SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
721          SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
722                                     DAG.getSrcValue(NULL));
723          ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
724        }
725        ArgOffset += 4;
726        NumIntRegs = 2;
727        break;
728      }
729      ObjSize = ArgIncrement = 8;
730      break;
731    case MVT::f32: ObjSize = 4;                break;
732    case MVT::f64: ObjSize = ArgIncrement = 8; break;
733    }
734
735    // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
736    // dead loads.
737    if (ObjSize && !I->use_empty()) {
738      // Create the frame index object for this incoming parameter...
739      int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
740
741      // Create the SelectionDAG nodes corresponding to a load from this
742      // parameter.
743      SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
744
745      ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
746                             DAG.getSrcValue(NULL));
747    } else if (ArgValue.Val == 0) {
748      if (MVT::isInteger(ObjectVT))
749        ArgValue = DAG.getConstant(0, ObjectVT);
750      else
751        ArgValue = DAG.getConstantFP(0, ObjectVT);
752    }
753    ArgValues.push_back(ArgValue);
754
755    if (ObjSize)
756      ArgOffset += ArgIncrement;   // Move on to the next argument.
757  }
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  VarArgsFrameIndex = 0xAAAAAAA;   // fastcc functions can't have varargs.
765  ReturnAddrIndex = 0;             // No return address slot generated yet.
766  BytesToPopOnReturn = ArgOffset;  // Callee pops all stack arguments.
767  BytesCallerReserves = 0;
768
769  // Finally, inform the code generator which regs we return values in.
770  switch (getValueType(F.getReturnType())) {
771  default: assert(0 && "Unknown type!");
772  case MVT::isVoid: break;
773  case MVT::i1:
774  case MVT::i8:
775  case MVT::i16:
776  case MVT::i32:
777    MF.addLiveOut(X86::EAX);
778    break;
779  case MVT::i64:
780    MF.addLiveOut(X86::EAX);
781    MF.addLiveOut(X86::EDX);
782    break;
783  case MVT::f32:
784  case MVT::f64:
785    MF.addLiveOut(X86::ST0);
786    break;
787  }
788  return ArgValues;
789}
790
791std::pair<SDOperand, SDOperand>
792X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
793                                     bool isTailCall, SDOperand Callee,
794                                     ArgListTy &Args, SelectionDAG &DAG) {
795  // Count how many bytes are to be pushed on the stack.
796  unsigned NumBytes = 0;
797
798  // Keep track of the number of integer regs passed so far.  This can be either
799  // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
800  // used).
801  unsigned NumIntRegs = 0;
802
803  for (unsigned i = 0, e = Args.size(); i != e; ++i)
804    switch (getValueType(Args[i].second)) {
805    default: assert(0 && "Unknown value type!");
806    case MVT::i1:
807    case MVT::i8:
808    case MVT::i16:
809    case MVT::i32:
810      if (NumIntRegs < 2) {
811        ++NumIntRegs;
812        break;
813      }
814      // fall through
815    case MVT::f32:
816      NumBytes += 4;
817      break;
818    case MVT::i64:
819      if (NumIntRegs == 0) {
820        NumIntRegs = 2;
821        break;
822      } else if (NumIntRegs == 1) {
823        NumIntRegs = 2;
824        NumBytes += 4;
825        break;
826      }
827
828      // fall through
829    case MVT::f64:
830      NumBytes += 8;
831      break;
832    }
833
834  // Make sure the instruction takes 8n+4 bytes to make sure the start of the
835  // arguments and the arguments after the retaddr has been pushed are aligned.
836  if ((NumBytes & 7) == 0)
837    NumBytes += 4;
838
839  Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
840                      DAG.getConstant(NumBytes, getPointerTy()));
841
842  // Arguments go on the stack in reverse order, as specified by the ABI.
843  unsigned ArgOffset = 0;
844  SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
845  NumIntRegs = 0;
846  std::vector<SDOperand> Stores;
847  std::vector<SDOperand> RegValuesToPass;
848  for (unsigned i = 0, e = Args.size(); i != e; ++i) {
849    switch (getValueType(Args[i].second)) {
850    default: assert(0 && "Unexpected ValueType for argument!");
851    case MVT::i1:
852      Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
853      // Fall through.
854    case MVT::i8:
855    case MVT::i16:
856    case MVT::i32:
857      if (NumIntRegs < 2) {
858        RegValuesToPass.push_back(Args[i].first);
859        ++NumIntRegs;
860        break;
861      }
862      // Fall through
863    case MVT::f32: {
864      SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
865      PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
866      Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
867                                   Args[i].first, PtrOff,
868                                   DAG.getSrcValue(NULL)));
869      ArgOffset += 4;
870      break;
871    }
872    case MVT::i64:
873      if (NumIntRegs < 2) {    // Can pass part of it in regs?
874        SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
875                                   Args[i].first, DAG.getConstant(1, MVT::i32));
876        SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
877                                   Args[i].first, DAG.getConstant(0, MVT::i32));
878        RegValuesToPass.push_back(Lo);
879        ++NumIntRegs;
880        if (NumIntRegs < 2) {   // Pass both parts in regs?
881          RegValuesToPass.push_back(Hi);
882          ++NumIntRegs;
883        } else {
884          // Pass the high part in memory.
885          SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
886          PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
887          Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
888                                       Hi, PtrOff, DAG.getSrcValue(NULL)));
889          ArgOffset += 4;
890        }
891        break;
892      }
893      // Fall through
894    case MVT::f64:
895      SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
896      PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
897      Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
898                                   Args[i].first, PtrOff,
899                                   DAG.getSrcValue(NULL)));
900      ArgOffset += 8;
901      break;
902    }
903  }
904  if (!Stores.empty())
905    Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
906
907  // Make sure the instruction takes 8n+4 bytes to make sure the start of the
908  // arguments and the arguments after the retaddr has been pushed are aligned.
909  if ((ArgOffset & 7) == 0)
910    ArgOffset += 4;
911
912  std::vector<MVT::ValueType> RetVals;
913  MVT::ValueType RetTyVT = getValueType(RetTy);
914
915  RetVals.push_back(MVT::Other);
916
917  // The result values produced have to be legal.  Promote the result.
918  switch (RetTyVT) {
919  case MVT::isVoid: break;
920  default:
921    RetVals.push_back(RetTyVT);
922    break;
923  case MVT::i1:
924  case MVT::i8:
925  case MVT::i16:
926    RetVals.push_back(MVT::i32);
927    break;
928  case MVT::f32:
929    if (X86ScalarSSE)
930      RetVals.push_back(MVT::f32);
931    else
932      RetVals.push_back(MVT::f64);
933    break;
934  case MVT::i64:
935    RetVals.push_back(MVT::i32);
936    RetVals.push_back(MVT::i32);
937    break;
938  }
939
940  if (!X86PatIsel) {
941    // Build a sequence of copy-to-reg nodes chained together with token chain
942    // and flag operands which copy the outgoing args into registers.
943    SDOperand InFlag;
944    for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
945      unsigned CCReg;
946      SDOperand RegToPass = RegValuesToPass[i];
947      switch (RegToPass.getValueType()) {
948      default: assert(0 && "Bad thing to pass in regs");
949      case MVT::i8:
950        CCReg = (i == 0) ? X86::AL  : X86::DL;
951        break;
952      case MVT::i16:
953        CCReg = (i == 0) ? X86::AX  : X86::DX;
954        break;
955      case MVT::i32:
956        CCReg = (i == 0) ? X86::EAX : X86::EDX;
957        break;
958      }
959
960      Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
961      InFlag = Chain.getValue(1);
962    }
963
964    std::vector<MVT::ValueType> NodeTys;
965    NodeTys.push_back(MVT::Other);   // Returns a chain
966    NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
967    std::vector<SDOperand> Ops;
968    Ops.push_back(Chain);
969    Ops.push_back(Callee);
970    if (InFlag.Val)
971      Ops.push_back(InFlag);
972
973    // FIXME: Do not generate X86ISD::TAILCALL for now.
974    Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
975    InFlag = Chain.getValue(1);
976
977    NodeTys.clear();
978    NodeTys.push_back(MVT::Other);   // Returns a chain
979    NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
980    Ops.clear();
981    Ops.push_back(Chain);
982    Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
983    Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
984    Ops.push_back(InFlag);
985    Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
986    InFlag = Chain.getValue(1);
987
988    SDOperand RetVal;
989    if (RetTyVT != MVT::isVoid) {
990      switch (RetTyVT) {
991      default: assert(0 && "Unknown value type to return!");
992      case MVT::i1:
993      case MVT::i8:
994        RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
995        Chain = RetVal.getValue(1);
996        if (RetTyVT == MVT::i1)
997          RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
998        break;
999      case MVT::i16:
1000        RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1001        Chain = RetVal.getValue(1);
1002        break;
1003      case MVT::i32:
1004        RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1005        Chain = RetVal.getValue(1);
1006        break;
1007      case MVT::i64: {
1008        SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1009        SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1010                                          Lo.getValue(2));
1011        RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1012        Chain = Hi.getValue(1);
1013        break;
1014      }
1015      case MVT::f32:
1016      case MVT::f64: {
1017        std::vector<MVT::ValueType> Tys;
1018        Tys.push_back(MVT::f64);
1019        Tys.push_back(MVT::Other);
1020        Tys.push_back(MVT::Flag);
1021        std::vector<SDOperand> Ops;
1022        Ops.push_back(Chain);
1023        Ops.push_back(InFlag);
1024        RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1025        Chain  = RetVal.getValue(1);
1026        InFlag = RetVal.getValue(2);
1027        if (X86ScalarSSE) {
1028          // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
1029          // shouldn't be necessary except for RFP cannot be live across
1030          // multiple blocks. When stackifier is fixed, they can be uncoupled.
1031          unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1032          MachineFunction &MF = DAG.getMachineFunction();
1033          int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1034          SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1035          Tys.clear();
1036          Tys.push_back(MVT::Other);
1037          Ops.clear();
1038          Ops.push_back(Chain);
1039          Ops.push_back(RetVal);
1040          Ops.push_back(StackSlot);
1041          Ops.push_back(DAG.getValueType(RetTyVT));
1042          Ops.push_back(InFlag);
1043          Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1044          RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1045                               DAG.getSrcValue(NULL));
1046          Chain = RetVal.getValue(1);
1047        }
1048
1049        if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1050          // FIXME: we would really like to remember that this FP_ROUND
1051          // operation is okay to eliminate if we allow excess FP precision.
1052          RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1053        break;
1054      }
1055      }
1056    }
1057
1058    return std::make_pair(RetVal, Chain);
1059  } else {
1060    std::vector<SDOperand> Ops;
1061    Ops.push_back(Chain);
1062    Ops.push_back(Callee);
1063    Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1064    // Callee pops all arg values on the stack.
1065    Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1066
1067    // Pass register arguments as needed.
1068    Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1069
1070    SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1071                                    RetVals, Ops);
1072    Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1073
1074    SDOperand ResultVal;
1075    switch (RetTyVT) {
1076    case MVT::isVoid: break;
1077    default:
1078      ResultVal = TheCall.getValue(1);
1079      break;
1080    case MVT::i1:
1081    case MVT::i8:
1082    case MVT::i16:
1083      ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1084      break;
1085    case MVT::f32:
1086      // FIXME: we would really like to remember that this FP_ROUND operation is
1087      // okay to eliminate if we allow excess FP precision.
1088      ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1089      break;
1090    case MVT::i64:
1091      ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1092                              TheCall.getValue(2));
1093      break;
1094    }
1095
1096    return std::make_pair(ResultVal, Chain);
1097  }
1098}
1099
1100SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1101  if (ReturnAddrIndex == 0) {
1102    // Set up a frame object for the return address.
1103    MachineFunction &MF = DAG.getMachineFunction();
1104    ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1105  }
1106
1107  return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1108}
1109
1110
1111
1112std::pair<SDOperand, SDOperand> X86TargetLowering::
1113LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1114                        SelectionDAG &DAG) {
1115  SDOperand Result;
1116  if (Depth)        // Depths > 0 not supported yet!
1117    Result = DAG.getConstant(0, getPointerTy());
1118  else {
1119    SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1120    if (!isFrameAddress)
1121      // Just load the return address
1122      Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1123                           DAG.getSrcValue(NULL));
1124    else
1125      Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1126                           DAG.getConstant(4, MVT::i32));
1127  }
1128  return std::make_pair(Result, Chain);
1129}
1130
1131/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1132/// which corresponds to the condition code.
1133static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1134  switch (X86CC) {
1135  default: assert(0 && "Unknown X86 conditional code!");
1136  case X86ISD::COND_A:  return X86::JA;
1137  case X86ISD::COND_AE: return X86::JAE;
1138  case X86ISD::COND_B:  return X86::JB;
1139  case X86ISD::COND_BE: return X86::JBE;
1140  case X86ISD::COND_E:  return X86::JE;
1141  case X86ISD::COND_G:  return X86::JG;
1142  case X86ISD::COND_GE: return X86::JGE;
1143  case X86ISD::COND_L:  return X86::JL;
1144  case X86ISD::COND_LE: return X86::JLE;
1145  case X86ISD::COND_NE: return X86::JNE;
1146  case X86ISD::COND_NO: return X86::JNO;
1147  case X86ISD::COND_NP: return X86::JNP;
1148  case X86ISD::COND_NS: return X86::JNS;
1149  case X86ISD::COND_O:  return X86::JO;
1150  case X86ISD::COND_P:  return X86::JP;
1151  case X86ISD::COND_S:  return X86::JS;
1152  }
1153}
1154
1155/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1156/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
1157/// do a direct translation.
1158static unsigned getX86CC(SDOperand CC, bool isFP) {
1159  ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1160  unsigned X86CC = X86ISD::COND_INVALID;
1161  if (!isFP) {
1162    switch (SetCCOpcode) {
1163    default: break;
1164    case ISD::SETEQ:  X86CC = X86ISD::COND_E;  break;
1165    case ISD::SETGT:  X86CC = X86ISD::COND_G;  break;
1166    case ISD::SETGE:  X86CC = X86ISD::COND_GE; break;
1167    case ISD::SETLT:  X86CC = X86ISD::COND_L;  break;
1168    case ISD::SETLE:  X86CC = X86ISD::COND_LE; break;
1169    case ISD::SETNE:  X86CC = X86ISD::COND_NE; break;
1170    case ISD::SETULT: X86CC = X86ISD::COND_B;  break;
1171    case ISD::SETUGT: X86CC = X86ISD::COND_A;  break;
1172    case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1173    case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1174    }
1175  } else {
1176    // On a floating point condition, the flags are set as follows:
1177    // ZF  PF  CF   op
1178    //  0 | 0 | 0 | X > Y
1179    //  0 | 0 | 1 | X < Y
1180    //  1 | 0 | 0 | X == Y
1181    //  1 | 1 | 1 | unordered
1182    switch (SetCCOpcode) {
1183    default: break;
1184    case ISD::SETUEQ:
1185    case ISD::SETEQ: X86CC = X86ISD::COND_E;  break;
1186    case ISD::SETOGT:
1187    case ISD::SETGT: X86CC = X86ISD::COND_A;  break;
1188    case ISD::SETOGE:
1189    case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1190    case ISD::SETULT:
1191    case ISD::SETLT: X86CC = X86ISD::COND_B;  break;
1192    case ISD::SETULE:
1193    case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1194    case ISD::SETONE:
1195    case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1196    case ISD::SETUO: X86CC = X86ISD::COND_P;  break;
1197    case ISD::SETO:  X86CC = X86ISD::COND_NP; break;
1198    }
1199  }
1200  return X86CC;
1201}
1202
1203/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1204/// code. Current x86 isa includes the following FP cmov instructions:
1205/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1206static bool hasFPCMov(unsigned X86CC) {
1207  switch (X86CC) {
1208  default:
1209    return false;
1210  case X86ISD::COND_B:
1211  case X86ISD::COND_BE:
1212  case X86ISD::COND_E:
1213  case X86ISD::COND_P:
1214  case X86ISD::COND_A:
1215  case X86ISD::COND_AE:
1216  case X86ISD::COND_NE:
1217  case X86ISD::COND_NP:
1218    return true;
1219  }
1220}
1221
1222MachineBasicBlock *
1223X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1224                                           MachineBasicBlock *BB) {
1225  switch (MI->getOpcode()) {
1226  default: assert(false && "Unexpected instr type to insert");
1227  case X86::CMOV_FR32:
1228  case X86::CMOV_FR64: {
1229    // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1230    // control-flow pattern.  The incoming instruction knows the destination vreg
1231    // to set, the condition code register to branch on, the true/false values to
1232    // select between, and a branch opcode to use.
1233    const BasicBlock *LLVM_BB = BB->getBasicBlock();
1234    ilist<MachineBasicBlock>::iterator It = BB;
1235    ++It;
1236
1237    //  thisMBB:
1238    //  ...
1239    //   TrueVal = ...
1240    //   cmpTY ccX, r1, r2
1241    //   bCC copy1MBB
1242    //   fallthrough --> copy0MBB
1243    MachineBasicBlock *thisMBB = BB;
1244    MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1245    MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1246    unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1247    BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1248    MachineFunction *F = BB->getParent();
1249    F->getBasicBlockList().insert(It, copy0MBB);
1250    F->getBasicBlockList().insert(It, sinkMBB);
1251    // Update machine-CFG edges
1252    BB->addSuccessor(copy0MBB);
1253    BB->addSuccessor(sinkMBB);
1254
1255    //  copy0MBB:
1256    //   %FalseValue = ...
1257    //   # fallthrough to sinkMBB
1258    BB = copy0MBB;
1259
1260    // Update machine-CFG edges
1261    BB->addSuccessor(sinkMBB);
1262
1263    //  sinkMBB:
1264    //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1265    //  ...
1266    BB = sinkMBB;
1267    BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1268      .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1269      .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1270
1271    delete MI;   // The pseudo instruction is gone now.
1272    return BB;
1273  }
1274
1275  case X86::FP_TO_INT16_IN_MEM:
1276  case X86::FP_TO_INT32_IN_MEM:
1277  case X86::FP_TO_INT64_IN_MEM: {
1278    // Change the floating point control register to use "round towards zero"
1279    // mode when truncating to an integer value.
1280    MachineFunction *F = BB->getParent();
1281    int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1282    addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1283
1284    // Load the old value of the high byte of the control word...
1285    unsigned OldCW =
1286      F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1287    addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1288
1289    // Set the high part to be round to zero...
1290    addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1291
1292    // Reload the modified control word now...
1293    addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1294
1295    // Restore the memory image of control word to original value
1296    addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1297
1298    // Get the X86 opcode to use.
1299    unsigned Opc;
1300    switch (MI->getOpcode()) {
1301    default: assert(0 && "illegal opcode!");
1302    case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1303    case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1304    case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1305    }
1306
1307    X86AddressMode AM;
1308    MachineOperand &Op = MI->getOperand(0);
1309    if (Op.isRegister()) {
1310      AM.BaseType = X86AddressMode::RegBase;
1311      AM.Base.Reg = Op.getReg();
1312    } else {
1313      AM.BaseType = X86AddressMode::FrameIndexBase;
1314      AM.Base.FrameIndex = Op.getFrameIndex();
1315    }
1316    Op = MI->getOperand(1);
1317    if (Op.isImmediate())
1318      AM.Scale = Op.getImmedValue();
1319    Op = MI->getOperand(2);
1320    if (Op.isImmediate())
1321      AM.IndexReg = Op.getImmedValue();
1322    Op = MI->getOperand(3);
1323    if (Op.isGlobalAddress()) {
1324      AM.GV = Op.getGlobal();
1325    } else {
1326      AM.Disp = Op.getImmedValue();
1327    }
1328    addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1329
1330    // Reload the original control word now.
1331    addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1332
1333    delete MI;   // The pseudo instruction is gone now.
1334    return BB;
1335  }
1336  }
1337}
1338
1339
1340//===----------------------------------------------------------------------===//
1341//                           X86 Custom Lowering Hooks
1342//===----------------------------------------------------------------------===//
1343
1344/// LowerOperation - Provide custom lowering hooks for some operations.
1345///
1346SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1347  switch (Op.getOpcode()) {
1348  default: assert(0 && "Should not custom lower this!");
1349  case ISD::ADD_PARTS:
1350  case ISD::SUB_PARTS: {
1351    assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1352           "Not an i64 add/sub!");
1353    bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1354    std::vector<MVT::ValueType> Tys;
1355    Tys.push_back(MVT::i32);
1356    Tys.push_back(MVT::Flag);
1357    std::vector<SDOperand> Ops;
1358    Ops.push_back(Op.getOperand(0));
1359    Ops.push_back(Op.getOperand(2));
1360    SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1361                               Tys, Ops);
1362    SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1363                               Op.getOperand(1), Op.getOperand(3),
1364                               Lo.getValue(1));
1365    Tys.clear();
1366    Tys.push_back(MVT::i32);
1367    Tys.push_back(MVT::i32);
1368    Ops.clear();
1369    Ops.push_back(Lo);
1370    Ops.push_back(Hi);
1371    return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1372  }
1373  case ISD::SHL_PARTS:
1374  case ISD::SRA_PARTS:
1375  case ISD::SRL_PARTS: {
1376    assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1377           "Not an i64 shift!");
1378    bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1379    SDOperand ShOpLo = Op.getOperand(0);
1380    SDOperand ShOpHi = Op.getOperand(1);
1381    SDOperand ShAmt  = Op.getOperand(2);
1382    SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1383                                         DAG.getConstant(31, MVT::i8))
1384                           : DAG.getConstant(0, MVT::i32);
1385
1386    SDOperand Tmp2, Tmp3;
1387    if (Op.getOpcode() == ISD::SHL_PARTS) {
1388      Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1389      Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1390    } else {
1391      Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1392      Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1393    }
1394
1395    SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1396                                   ShAmt, DAG.getConstant(32, MVT::i8));
1397
1398    SDOperand Hi, Lo;
1399    SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1400
1401    std::vector<MVT::ValueType> Tys;
1402    Tys.push_back(MVT::i32);
1403    Tys.push_back(MVT::Flag);
1404    std::vector<SDOperand> Ops;
1405    if (Op.getOpcode() == ISD::SHL_PARTS) {
1406      Ops.push_back(Tmp2);
1407      Ops.push_back(Tmp3);
1408      Ops.push_back(CC);
1409      Ops.push_back(InFlag);
1410      Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1411      InFlag = Hi.getValue(1);
1412
1413      Ops.clear();
1414      Ops.push_back(Tmp3);
1415      Ops.push_back(Tmp1);
1416      Ops.push_back(CC);
1417      Ops.push_back(InFlag);
1418      Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1419    } else {
1420      Ops.push_back(Tmp2);
1421      Ops.push_back(Tmp3);
1422      Ops.push_back(CC);
1423      Ops.push_back(InFlag);
1424      Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1425      InFlag = Lo.getValue(1);
1426
1427      Ops.clear();
1428      Ops.push_back(Tmp3);
1429      Ops.push_back(Tmp1);
1430      Ops.push_back(CC);
1431      Ops.push_back(InFlag);
1432      Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1433    }
1434
1435    Tys.clear();
1436    Tys.push_back(MVT::i32);
1437    Tys.push_back(MVT::i32);
1438    Ops.clear();
1439    Ops.push_back(Lo);
1440    Ops.push_back(Hi);
1441    return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1442  }
1443  case ISD::SINT_TO_FP: {
1444    assert(Op.getValueType() == MVT::f64 &&
1445           Op.getOperand(0).getValueType() <= MVT::i64 &&
1446           Op.getOperand(0).getValueType() >= MVT::i16 &&
1447           "Unknown SINT_TO_FP to lower!");
1448
1449    SDOperand Result;
1450    MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1451    unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1452    MachineFunction &MF = DAG.getMachineFunction();
1453    int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1454    SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1455    SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1456                                  DAG.getEntryNode(), Op.getOperand(0),
1457                                  StackSlot, DAG.getSrcValue(NULL));
1458
1459    // Build the FILD
1460    std::vector<MVT::ValueType> Tys;
1461    Tys.push_back(MVT::f64);
1462    Tys.push_back(MVT::Flag);
1463    std::vector<SDOperand> Ops;
1464    Ops.push_back(Chain);
1465    Ops.push_back(StackSlot);
1466    Ops.push_back(DAG.getValueType(SrcVT));
1467    Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
1468    return Result;
1469  }
1470  case ISD::FP_TO_SINT: {
1471    assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1472           Op.getOperand(0).getValueType() == MVT::f64 &&
1473           "Unknown FP_TO_SINT to lower!");
1474    // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1475    // stack slot.
1476    MachineFunction &MF = DAG.getMachineFunction();
1477    unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1478    int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1479    SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1480
1481    unsigned Opc;
1482    switch (Op.getValueType()) {
1483    default: assert(0 && "Invalid FP_TO_SINT to lower!");
1484    case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1485    case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1486    case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1487    }
1488
1489    // Build the FP_TO_INT*_IN_MEM
1490    std::vector<SDOperand> Ops;
1491    Ops.push_back(DAG.getEntryNode());
1492    Ops.push_back(Op.getOperand(0));
1493    Ops.push_back(StackSlot);
1494    SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1495
1496    // Load the result.
1497    return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1498                       DAG.getSrcValue(NULL));
1499  }
1500  case ISD::READCYCLECOUNTER: {
1501    std::vector<MVT::ValueType> Tys;
1502    Tys.push_back(MVT::Other);
1503    Tys.push_back(MVT::Flag);
1504    std::vector<SDOperand> Ops;
1505    Ops.push_back(Op.getOperand(0));
1506    SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1507    Ops.clear();
1508    Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1509    Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1510                                     MVT::i32, Ops[0].getValue(2)));
1511    Ops.push_back(Ops[1].getValue(1));
1512    Tys[0] = Tys[1] = MVT::i32;
1513    Tys.push_back(MVT::Other);
1514    return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1515  }
1516  case ISD::SETCC: {
1517    assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1518    SDOperand CC   = Op.getOperand(2);
1519    SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1520                                 Op.getOperand(0), Op.getOperand(1));
1521    ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1522    bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1523    unsigned X86CC = getX86CC(CC, isFP);
1524    if (X86CC != X86ISD::COND_INVALID) {
1525      return DAG.getNode(X86ISD::SETCC, MVT::i8,
1526                         DAG.getConstant(X86CC, MVT::i8), Cond);
1527    } else {
1528      assert(isFP && "Illegal integer SetCC!");
1529
1530      std::vector<MVT::ValueType> Tys;
1531      std::vector<SDOperand> Ops;
1532      switch (SetCCOpcode) {
1533      default: assert(false && "Illegal floating point SetCC!");
1534      case ISD::SETOEQ: {  // !PF & ZF
1535        Tys.push_back(MVT::i8);
1536        Tys.push_back(MVT::Flag);
1537        Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1538        Ops.push_back(Cond);
1539        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1540        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1541                                     DAG.getConstant(X86ISD::COND_E, MVT::i8),
1542                                     Tmp1.getValue(1));
1543        return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1544      }
1545      case ISD::SETOLT: {  // !PF & CF
1546        Tys.push_back(MVT::i8);
1547        Tys.push_back(MVT::Flag);
1548        Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1549        Ops.push_back(Cond);
1550        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1551        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1552                                     DAG.getConstant(X86ISD::COND_B, MVT::i8),
1553                                     Tmp1.getValue(1));
1554        return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1555      }
1556      case ISD::SETOLE: {  // !PF & (CF || ZF)
1557        Tys.push_back(MVT::i8);
1558        Tys.push_back(MVT::Flag);
1559        Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1560        Ops.push_back(Cond);
1561        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1562        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1563                                     DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1564                                     Tmp1.getValue(1));
1565        return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1566      }
1567      case ISD::SETUGT: {  // PF | (!ZF & !CF)
1568        Tys.push_back(MVT::i8);
1569        Tys.push_back(MVT::Flag);
1570        Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1571        Ops.push_back(Cond);
1572        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1573        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1574                                     DAG.getConstant(X86ISD::COND_A, MVT::i8),
1575                                     Tmp1.getValue(1));
1576        return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1577      }
1578      case ISD::SETUGE: {  // PF | !CF
1579        Tys.push_back(MVT::i8);
1580        Tys.push_back(MVT::Flag);
1581        Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1582        Ops.push_back(Cond);
1583        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1584        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1585                                     DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1586                                     Tmp1.getValue(1));
1587        return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1588      }
1589      case ISD::SETUNE: {  // PF | !ZF
1590        Tys.push_back(MVT::i8);
1591        Tys.push_back(MVT::Flag);
1592        Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1593        Ops.push_back(Cond);
1594        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1595        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1596                                     DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1597                                     Tmp1.getValue(1));
1598        return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1599      }
1600      }
1601    }
1602  }
1603  case ISD::SELECT: {
1604    MVT::ValueType VT = Op.getValueType();
1605    bool isFP      = MVT::isFloatingPoint(VT);
1606    bool isFPStack = isFP && !X86ScalarSSE;
1607    bool isFPSSE   = isFP && X86ScalarSSE;
1608    bool addTest   = false;
1609    SDOperand Op0 = Op.getOperand(0);
1610    SDOperand Cond, CC;
1611    if (Op0.getOpcode() == X86ISD::SETCC) {
1612      // If condition flag is set by a X86ISD::CMP, then make a copy of it
1613      // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1614      // have another use it will be eliminated.
1615      // If the X86ISD::SETCC has more than one use, then it's probably better
1616      // to use a test instead of duplicating the X86ISD::CMP (for register
1617      // pressure reason).
1618      if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1619        if (!Op0.hasOneUse()) {
1620          std::vector<MVT::ValueType> Tys;
1621          for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1622            Tys.push_back(Op0.Val->getValueType(i));
1623          std::vector<SDOperand> Ops;
1624          for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1625            Ops.push_back(Op0.getOperand(i));
1626          Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1627        }
1628
1629        CC   = Op0.getOperand(0);
1630        Cond = Op0.getOperand(1);
1631        // Make a copy as flag result cannot be used by more than one.
1632        Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1633                           Cond.getOperand(0), Cond.getOperand(1));
1634        addTest =
1635          isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
1636      } else
1637        addTest = true;
1638    } else if (Op0.getOpcode() == ISD::SETCC) {
1639      CC = Op0.getOperand(2);
1640      bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
1641      unsigned X86CC = getX86CC(CC, isFP);
1642      CC = DAG.getConstant(X86CC, MVT::i8);
1643      Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1644                         Op0.getOperand(0), Op0.getOperand(1));
1645    } else
1646      addTest = true;
1647
1648    if (addTest) {
1649      CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1650      Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
1651    }
1652
1653    std::vector<MVT::ValueType> Tys;
1654    Tys.push_back(Op.getValueType());
1655    Tys.push_back(MVT::Flag);
1656    std::vector<SDOperand> Ops;
1657    // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1658    // condition is true.
1659    Ops.push_back(Op.getOperand(2));
1660    Ops.push_back(Op.getOperand(1));
1661    Ops.push_back(CC);
1662    Ops.push_back(Cond);
1663    return DAG.getNode(X86ISD::CMOV, Tys, Ops);
1664  }
1665  case ISD::BRCOND: {
1666    bool addTest = false;
1667    SDOperand Cond  = Op.getOperand(1);
1668    SDOperand Dest  = Op.getOperand(2);
1669    SDOperand CC;
1670    if (Cond.getOpcode() == X86ISD::SETCC) {
1671      // If condition flag is set by a X86ISD::CMP, then make a copy of it
1672      // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1673      // have another use it will be eliminated.
1674      // If the X86ISD::SETCC has more than one use, then it's probably better
1675      // to use a test instead of duplicating the X86ISD::CMP (for register
1676      // pressure reason).
1677      if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1678        if (!Cond.hasOneUse()) {
1679          std::vector<MVT::ValueType> Tys;
1680          for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1681            Tys.push_back(Cond.Val->getValueType(i));
1682          std::vector<SDOperand> Ops;
1683          for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1684            Ops.push_back(Cond.getOperand(i));
1685          Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1686        }
1687
1688        CC   = Cond.getOperand(0);
1689        Cond = Cond.getOperand(1);
1690        // Make a copy as flag result cannot be used by more than one.
1691        Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1692                           Cond.getOperand(0), Cond.getOperand(1));
1693      } else
1694        addTest = true;
1695    } else if (Cond.getOpcode() == ISD::SETCC) {
1696      CC = Cond.getOperand(2);
1697      bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
1698      unsigned X86CC = getX86CC(CC, isFP);
1699      CC = DAG.getConstant(X86CC, MVT::i8);
1700      Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1701                         Cond.getOperand(0), Cond.getOperand(1));
1702    } else
1703      addTest = true;
1704
1705    if (addTest) {
1706      CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1707      Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1708    }
1709    return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1710                       Op.getOperand(0), Op.getOperand(2), CC, Cond);
1711  }
1712  case ISD::MEMSET: {
1713    SDOperand InFlag;
1714    SDOperand Chain = Op.getOperand(0);
1715    unsigned Align =
1716      (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1717    if (Align == 0) Align = 1;
1718
1719    MVT::ValueType AVT;
1720    SDOperand Count;
1721    if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1722      unsigned ValReg;
1723      unsigned Val = ValC->getValue() & 255;
1724
1725      // If the value is a constant, then we can potentially use larger sets.
1726      switch (Align & 3) {
1727      case 2:   // WORD aligned
1728        AVT = MVT::i16;
1729        if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1730          Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1731        else
1732          Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1733                              DAG.getConstant(1, MVT::i8));
1734        Val    = (Val << 8) | Val;
1735        ValReg = X86::AX;
1736        break;
1737      case 0:   // DWORD aligned
1738        AVT = MVT::i32;
1739        if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1740          Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1741        else
1742          Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1743                              DAG.getConstant(2, MVT::i8));
1744        Val = (Val << 8)  | Val;
1745        Val = (Val << 16) | Val;
1746        ValReg = X86::EAX;
1747        break;
1748      default:  // Byte aligned
1749        AVT = MVT::i8;
1750        Count = Op.getOperand(3);
1751        ValReg = X86::AL;
1752        break;
1753      }
1754
1755      Chain  = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1756                                InFlag);
1757      InFlag = Chain.getValue(1);
1758    } else {
1759      AVT    = MVT::i8;
1760      Count  = Op.getOperand(3);
1761      Chain  = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1762      InFlag = Chain.getValue(1);
1763    }
1764
1765    Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1766    InFlag = Chain.getValue(1);
1767    Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1768    InFlag = Chain.getValue(1);
1769
1770    return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1771                       DAG.getValueType(AVT), InFlag);
1772  }
1773  case ISD::MEMCPY: {
1774    SDOperand Chain = Op.getOperand(0);
1775    unsigned Align =
1776      (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1777    if (Align == 0) Align = 1;
1778
1779    MVT::ValueType AVT;
1780    SDOperand Count;
1781    switch (Align & 3) {
1782    case 2:   // WORD aligned
1783      AVT = MVT::i16;
1784      if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1785        Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1786      else
1787        Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1788                            DAG.getConstant(1, MVT::i8));
1789      break;
1790    case 0:   // DWORD aligned
1791      AVT = MVT::i32;
1792      if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1793        Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1794      else
1795        Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1796                            DAG.getConstant(2, MVT::i8));
1797      break;
1798    default:  // Byte aligned
1799      AVT = MVT::i8;
1800      Count = Op.getOperand(3);
1801      break;
1802    }
1803
1804    SDOperand InFlag;
1805    Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1806    InFlag = Chain.getValue(1);
1807    Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1808    InFlag = Chain.getValue(1);
1809    Chain  = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1810    InFlag = Chain.getValue(1);
1811
1812    return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1813                       DAG.getValueType(AVT), InFlag);
1814  }
1815  case ISD::GlobalAddress: {
1816    SDOperand Result;
1817    GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1818    // For Darwin, external and weak symbols are indirect, so we want to load
1819    // the value at address GV, not the value of GV itself.  This means that
1820    // the GlobalAddress must be in the base or index register of the address,
1821    // not the GV offset field.
1822    if (getTargetMachine().
1823        getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1824        (GV->hasWeakLinkage() || GV->isExternal()))
1825      Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1826                           DAG.getTargetGlobalAddress(GV, getPointerTy()),
1827                           DAG.getSrcValue(NULL));
1828    return Result;
1829  }
1830  case ISD::VASTART: {
1831    // vastart just stores the address of the VarArgsFrameIndex slot into the
1832    // memory location argument.
1833    // FIXME: Replace MVT::i32 with PointerTy
1834    SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1835    return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1836                       Op.getOperand(1), Op.getOperand(2));
1837  }
1838  case ISD::RET: {
1839    SDOperand Copy;
1840
1841    switch(Op.getNumOperands()) {
1842    default:
1843      assert(0 && "Do not know how to return this many arguments!");
1844      abort();
1845    case 1:
1846      return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1847                         DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1848    case 2: {
1849      MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1850      if (MVT::isInteger(ArgVT))
1851        Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1852                                SDOperand());
1853      else if (!X86ScalarSSE) {
1854        std::vector<MVT::ValueType> Tys;
1855        Tys.push_back(MVT::Other);
1856        Tys.push_back(MVT::Flag);
1857        std::vector<SDOperand> Ops;
1858        Ops.push_back(Op.getOperand(0));
1859        Ops.push_back(Op.getOperand(1));
1860        Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1861      } else {
1862        // Spill the value to memory and reload it into top of stack.
1863        unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1864        MachineFunction &MF = DAG.getMachineFunction();
1865        int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1866        SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1867        SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1868                                      Op.getOperand(1), StackSlot,
1869                                      DAG.getSrcValue(0));
1870        std::vector<MVT::ValueType> Tys;
1871        Tys.push_back(MVT::f64);
1872        Tys.push_back(MVT::Other);
1873        std::vector<SDOperand> Ops;
1874        Ops.push_back(Chain);
1875        Ops.push_back(StackSlot);
1876        Ops.push_back(DAG.getValueType(ArgVT));
1877        Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1878        Tys.clear();
1879        Tys.push_back(MVT::Other);
1880        Tys.push_back(MVT::Flag);
1881        Ops.clear();
1882        Ops.push_back(Copy.getValue(1));
1883        Ops.push_back(Copy);
1884        Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1885      }
1886      break;
1887    }
1888    case 3:
1889      Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1890                              SDOperand());
1891      Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1892      break;
1893    }
1894    return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1895                       Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1896                       Copy.getValue(1));
1897  }
1898  }
1899}
1900
1901const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1902  switch (Opcode) {
1903  default: return NULL;
1904  case X86ISD::ADD_FLAG:           return "X86ISD::ADD_FLAG";
1905  case X86ISD::SUB_FLAG:           return "X86ISD::SUB_FLAG";
1906  case X86ISD::ADC:                return "X86ISD::ADC";
1907  case X86ISD::SBB:                return "X86ISD::SBB";
1908  case X86ISD::SHLD:               return "X86ISD::SHLD";
1909  case X86ISD::SHRD:               return "X86ISD::SHRD";
1910  case X86ISD::FILD:               return "X86ISD::FILD";
1911  case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1912  case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1913  case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
1914  case X86ISD::FLD:                return "X86ISD::FLD";
1915  case X86ISD::FST:                return "X86ISD::FST";
1916  case X86ISD::FP_GET_RESULT:      return "X86ISD::FP_GET_RESULT";
1917  case X86ISD::FP_SET_RESULT:      return "X86ISD::FP_SET_RESULT";
1918  case X86ISD::CALL:               return "X86ISD::CALL";
1919  case X86ISD::TAILCALL:           return "X86ISD::TAILCALL";
1920  case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
1921  case X86ISD::CMP:                return "X86ISD::CMP";
1922  case X86ISD::TEST:               return "X86ISD::TEST";
1923  case X86ISD::SETCC:              return "X86ISD::SETCC";
1924  case X86ISD::CMOV:               return "X86ISD::CMOV";
1925  case X86ISD::BRCOND:             return "X86ISD::BRCOND";
1926  case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
1927  case X86ISD::REP_STOS:           return "X86ISD::RET_STOS";
1928  case X86ISD::REP_MOVS:           return "X86ISD::RET_MOVS";
1929  }
1930}
1931
1932bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1933                                                       uint64_t Mask) const {
1934
1935  unsigned Opc = Op.getOpcode();
1936
1937  switch (Opc) {
1938  default:
1939    assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1940    break;
1941  case X86ISD::SETCC: return (Mask & 1) == 0;
1942  }
1943
1944  return false;
1945}
1946