1 //===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "X86ISelLowering.h"
17 #include "X86TargetMachine.h"
18 #include "llvm/CallingConv.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SSARegMap.h"
25 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/Support/CommandLine.h"
30 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
31 cl::desc("Enable fastcc on X86"));
33 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
34 : TargetLowering(TM) {
35 // Set up the TargetLowering object.
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
43 // Set up the register classes.
44 addRegisterClass(MVT::i8, X86::R8RegisterClass);
45 addRegisterClass(MVT::i16, X86::R16RegisterClass);
46 addRegisterClass(MVT::i32, X86::R32RegisterClass);
48 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
50 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
51 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
52 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
53 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
55 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
57 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
58 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
61 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
63 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
64 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
65 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
66 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
69 // Handle FP_TO_UINT by promoting the destination to a larger signed
71 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
72 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
73 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
76 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
78 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
80 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
81 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
82 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
84 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
85 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
88 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
90 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
91 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
92 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
93 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
94 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
95 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
96 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
97 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
98 setOperationAction(ISD::FREM , MVT::f64 , Expand);
99 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
100 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
101 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
102 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
103 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
104 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
105 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
106 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
107 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
108 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
110 setOperationAction(ISD::READIO , MVT::i1 , Expand);
111 setOperationAction(ISD::READIO , MVT::i8 , Expand);
112 setOperationAction(ISD::READIO , MVT::i16 , Expand);
113 setOperationAction(ISD::READIO , MVT::i32 , Expand);
114 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
115 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
116 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
117 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
119 // These should be promoted to a larger select which is supported.
120 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
121 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
123 // X86 wants to expand cmov itself.
124 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
125 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
126 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
127 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
128 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
129 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
130 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
131 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
132 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
133 // X86 ret instruction may pop stack.
134 setOperationAction(ISD::RET , MVT::Other, Custom);
136 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
137 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
138 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
139 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
140 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
141 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
142 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
145 // We don't have line number support yet.
146 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
147 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
148 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
151 // Set up the FP register classes.
152 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
153 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
155 // SSE has no load+extend ops
156 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
157 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
159 // SSE has no i16 to fp conversion, only i32
160 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
161 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
163 // Expand FP_TO_UINT into a select.
164 // FIXME: We would like to use a Custom expander here eventually to do
165 // the optimal thing for SSE vs. the default expansion in the legalizer.
166 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
168 // We don't support sin/cos/sqrt/fmod
169 setOperationAction(ISD::FSIN , MVT::f64, Expand);
170 setOperationAction(ISD::FCOS , MVT::f64, Expand);
171 setOperationAction(ISD::FABS , MVT::f64, Expand);
172 setOperationAction(ISD::FNEG , MVT::f64, Expand);
173 setOperationAction(ISD::FREM , MVT::f64, Expand);
174 setOperationAction(ISD::FSIN , MVT::f32, Expand);
175 setOperationAction(ISD::FCOS , MVT::f32, Expand);
176 setOperationAction(ISD::FABS , MVT::f32, Expand);
177 setOperationAction(ISD::FNEG , MVT::f32, Expand);
178 setOperationAction(ISD::FREM , MVT::f32, Expand);
180 addLegalFPImmediate(+0.0); // xorps / xorpd
182 // Set up the FP register classes.
183 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
186 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
187 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
190 addLegalFPImmediate(+0.0); // FLD0
191 addLegalFPImmediate(+1.0); // FLD1
192 addLegalFPImmediate(-0.0); // FLD0/FCHS
193 addLegalFPImmediate(-1.0); // FLD1/FCHS
195 computeRegisterProperties();
197 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
198 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
199 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
200 allowUnalignedMemoryAccesses = true; // x86 supports it!
203 std::vector<SDOperand>
204 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
205 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
206 return LowerFastCCArguments(F, DAG);
207 return LowerCCCArguments(F, DAG);
210 std::pair<SDOperand, SDOperand>
211 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
212 bool isVarArg, unsigned CallingConv,
214 SDOperand Callee, ArgListTy &Args,
216 assert((!isVarArg || CallingConv == CallingConv::C) &&
217 "Only C takes varargs!");
219 // If the callee is a GlobalAddress node (quite common, every direct call is)
220 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
221 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
222 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
224 if (CallingConv == CallingConv::Fast && EnableFastCC)
225 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
226 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
229 SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
232 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
235 MVT::ValueType OpVT = Op.getValueType();
237 default: assert(0 && "Unknown type to return!");
239 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
242 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
243 DAG.getConstant(1, MVT::i32));
244 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
245 DAG.getConstant(0, MVT::i32));
246 Copy = DAG.getCopyToReg(Chain, X86::EDX, Hi, SDOperand());
247 Copy = DAG.getCopyToReg(Copy, X86::EAX, Lo, Copy.getValue(1));
253 if (OpVT == MVT::f32)
254 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
255 std::vector<MVT::ValueType> Tys;
256 Tys.push_back(MVT::Other);
257 Tys.push_back(MVT::Flag);
258 std::vector<SDOperand> Ops;
259 Ops.push_back(Chain);
261 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
263 // Spill the value to memory and reload it into top of stack.
264 unsigned Size = MVT::getSizeInBits(OpVT)/8;
265 MachineFunction &MF = DAG.getMachineFunction();
266 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
267 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
268 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
269 StackSlot, DAG.getSrcValue(NULL));
270 std::vector<MVT::ValueType> Tys;
271 Tys.push_back(MVT::f64);
272 Tys.push_back(MVT::Other);
273 std::vector<SDOperand> Ops;
274 Ops.push_back(Chain);
275 Ops.push_back(StackSlot);
276 Ops.push_back(DAG.getValueType(OpVT));
277 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
279 Tys.push_back(MVT::Other);
280 Tys.push_back(MVT::Flag);
282 Ops.push_back(Copy.getValue(1));
284 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
289 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
290 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
294 //===----------------------------------------------------------------------===//
295 // C Calling Convention implementation
296 //===----------------------------------------------------------------------===//
298 std::vector<SDOperand>
299 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
300 std::vector<SDOperand> ArgValues;
302 MachineFunction &MF = DAG.getMachineFunction();
303 MachineFrameInfo *MFI = MF.getFrameInfo();
305 // Add DAG nodes to load the arguments... On entry to a function on the X86,
306 // the stack frame looks like this:
308 // [ESP] -- return address
309 // [ESP + 4] -- first argument (leftmost lexically)
310 // [ESP + 8] -- second argument, if first argument is four bytes in size
313 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
314 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
315 MVT::ValueType ObjectVT = getValueType(I->getType());
316 unsigned ArgIncrement = 4;
319 default: assert(0 && "Unhandled argument type!");
321 case MVT::i8: ObjSize = 1; break;
322 case MVT::i16: ObjSize = 2; break;
323 case MVT::i32: ObjSize = 4; break;
324 case MVT::i64: ObjSize = ArgIncrement = 8; break;
325 case MVT::f32: ObjSize = 4; break;
326 case MVT::f64: ObjSize = ArgIncrement = 8; break;
328 // Create the frame index object for this incoming parameter...
329 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
331 // Create the SelectionDAG nodes corresponding to a load from this parameter
332 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
334 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
338 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
339 DAG.getSrcValue(NULL));
341 if (MVT::isInteger(ObjectVT))
342 ArgValue = DAG.getConstant(0, ObjectVT);
344 ArgValue = DAG.getConstantFP(0, ObjectVT);
346 ArgValues.push_back(ArgValue);
348 ArgOffset += ArgIncrement; // Move on to the next argument...
351 // If the function takes variable number of arguments, make a frame index for
352 // the start of the first vararg value... for expansion of llvm.va_start.
354 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
355 ReturnAddrIndex = 0; // No return address slot generated yet.
356 BytesToPopOnReturn = 0; // Callee pops nothing.
357 BytesCallerReserves = ArgOffset;
359 // Finally, inform the code generator which regs we return values in.
360 switch (getValueType(F.getReturnType())) {
361 default: assert(0 && "Unknown type!");
362 case MVT::isVoid: break;
367 MF.addLiveOut(X86::EAX);
370 MF.addLiveOut(X86::EAX);
371 MF.addLiveOut(X86::EDX);
375 MF.addLiveOut(X86::ST0);
381 std::pair<SDOperand, SDOperand>
382 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
383 bool isVarArg, bool isTailCall,
384 SDOperand Callee, ArgListTy &Args,
386 // Count how many bytes are to be pushed on the stack.
387 unsigned NumBytes = 0;
391 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
392 DAG.getConstant(0, getPointerTy()));
394 for (unsigned i = 0, e = Args.size(); i != e; ++i)
395 switch (getValueType(Args[i].second)) {
396 default: assert(0 && "Unknown value type!");
410 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
411 DAG.getConstant(NumBytes, getPointerTy()));
413 // Arguments go on the stack in reverse order, as specified by the ABI.
414 unsigned ArgOffset = 0;
415 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
417 std::vector<SDOperand> Stores;
419 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
420 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
421 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
423 switch (getValueType(Args[i].second)) {
424 default: assert(0 && "Unexpected ValueType for argument!");
428 // Promote the integer to 32 bits. If the input type is signed use a
429 // sign extend, otherwise use a zero extend.
430 if (Args[i].second->isSigned())
431 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
433 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
438 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
439 Args[i].first, PtrOff,
440 DAG.getSrcValue(NULL)));
445 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
446 Args[i].first, PtrOff,
447 DAG.getSrcValue(NULL)));
452 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
455 std::vector<MVT::ValueType> RetVals;
456 MVT::ValueType RetTyVT = getValueType(RetTy);
457 RetVals.push_back(MVT::Other);
459 // The result values produced have to be legal. Promote the result.
461 case MVT::isVoid: break;
463 RetVals.push_back(RetTyVT);
468 RetVals.push_back(MVT::i32);
472 RetVals.push_back(MVT::f32);
474 RetVals.push_back(MVT::f64);
477 RetVals.push_back(MVT::i32);
478 RetVals.push_back(MVT::i32);
483 std::vector<MVT::ValueType> NodeTys;
484 NodeTys.push_back(MVT::Other); // Returns a chain
485 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
486 std::vector<SDOperand> Ops;
487 Ops.push_back(Chain);
488 Ops.push_back(Callee);
490 // FIXME: Do not generate X86ISD::TAILCALL for now.
491 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
492 SDOperand InFlag = Chain.getValue(1);
495 if (RetTyVT != MVT::isVoid) {
497 default: assert(0 && "Unknown value type to return!");
500 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
501 Chain = RetVal.getValue(1);
504 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
505 Chain = RetVal.getValue(1);
508 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
509 Chain = RetVal.getValue(1);
512 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
513 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
515 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
516 Chain = Hi.getValue(1);
521 std::vector<MVT::ValueType> Tys;
522 Tys.push_back(MVT::f64);
523 Tys.push_back(MVT::Other);
524 std::vector<SDOperand> Ops;
525 Ops.push_back(Chain);
526 Ops.push_back(InFlag);
527 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
528 Chain = RetVal.getValue(1);
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());
535 Tys.push_back(MVT::Other);
537 Ops.push_back(Chain);
538 Ops.push_back(RetVal);
539 Ops.push_back(StackSlot);
540 Ops.push_back(DAG.getValueType(RetTyVT));
541 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
542 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
543 DAG.getSrcValue(NULL));
544 Chain = RetVal.getValue(1);
545 } else if (RetTyVT == MVT::f32)
546 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
552 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
553 DAG.getConstant(NumBytes, getPointerTy()),
554 DAG.getConstant(0, getPointerTy()));
555 return std::make_pair(RetVal, Chain);
557 std::vector<SDOperand> Ops;
558 Ops.push_back(Chain);
559 Ops.push_back(Callee);
560 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
561 Ops.push_back(DAG.getConstant(0, getPointerTy()));
563 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
568 case MVT::isVoid: break;
570 ResultVal = TheCall.getValue(1);
575 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
578 // FIXME: we would really like to remember that this FP_ROUND operation is
579 // okay to eliminate if we allow excess FP precision.
580 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
583 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
584 TheCall.getValue(2));
588 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
589 return std::make_pair(ResultVal, Chain);
594 X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
595 Value *VAListV, SelectionDAG &DAG) {
596 // vastart just stores the address of the VarArgsFrameIndex slot.
597 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
598 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
599 DAG.getSrcValue(VAListV));
603 std::pair<SDOperand,SDOperand>
604 X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
605 Value *VAListV, const Type *ArgTy,
607 MVT::ValueType ArgVT = getValueType(ArgTy);
608 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
609 VAListP, DAG.getSrcValue(VAListV));
610 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
611 DAG.getSrcValue(NULL));
613 if (ArgVT == MVT::i32)
616 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
617 "Other types should have been promoted for varargs!");
620 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
621 DAG.getConstant(Amt, Val.getValueType()));
622 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
623 Val, VAListP, DAG.getSrcValue(VAListV));
624 return std::make_pair(Result, Chain);
627 //===----------------------------------------------------------------------===//
628 // Fast Calling Convention implementation
629 //===----------------------------------------------------------------------===//
631 // The X86 'fast' calling convention passes up to two integer arguments in
632 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
633 // and requires that the callee pop its arguments off the stack (allowing proper
634 // tail calls), and has the same return value conventions as C calling convs.
636 // This calling convention always arranges for the callee pop value to be 8n+4
637 // bytes, which is needed for tail recursion elimination and stack alignment
640 // Note that this can be enhanced in the future to pass fp vals in registers
641 // (when we have a global fp allocator) and do other tricks.
644 /// AddLiveIn - This helper function adds the specified physical register to the
645 /// MachineFunction as a live in value. It also creates a corresponding virtual
647 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
648 TargetRegisterClass *RC) {
649 assert(RC->contains(PReg) && "Not the correct regclass!");
650 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
651 MF.addLiveIn(PReg, VReg);
656 std::vector<SDOperand>
657 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
658 std::vector<SDOperand> ArgValues;
660 MachineFunction &MF = DAG.getMachineFunction();
661 MachineFrameInfo *MFI = MF.getFrameInfo();
663 // Add DAG nodes to load the arguments... On entry to a function the stack
664 // frame looks like this:
666 // [ESP] -- return address
667 // [ESP + 4] -- first nonreg argument (leftmost lexically)
668 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
670 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
672 // Keep track of the number of integer regs passed so far. This can be either
673 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
675 unsigned NumIntRegs = 0;
677 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
678 MVT::ValueType ObjectVT = getValueType(I->getType());
679 unsigned ArgIncrement = 4;
680 unsigned ObjSize = 0;
684 default: assert(0 && "Unhandled argument type!");
687 if (NumIntRegs < 2) {
688 if (!I->use_empty()) {
689 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
690 X86::R8RegisterClass);
691 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
692 DAG.setRoot(ArgValue.getValue(1));
693 if (ObjectVT == MVT::i1)
694 // FIXME: Should insert a assertzext here.
695 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
704 if (NumIntRegs < 2) {
705 if (!I->use_empty()) {
706 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
707 X86::R16RegisterClass);
708 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
709 DAG.setRoot(ArgValue.getValue(1));
717 if (NumIntRegs < 2) {
718 if (!I->use_empty()) {
719 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
720 X86::R32RegisterClass);
721 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
722 DAG.setRoot(ArgValue.getValue(1));
730 if (NumIntRegs == 0) {
731 if (!I->use_empty()) {
732 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
733 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
735 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
736 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
737 DAG.setRoot(Hi.getValue(1));
739 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
743 } else if (NumIntRegs == 1) {
744 if (!I->use_empty()) {
745 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
746 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
747 DAG.setRoot(Low.getValue(1));
749 // Load the high part from memory.
750 // Create the frame index object for this incoming parameter...
751 int FI = MFI->CreateFixedObject(4, ArgOffset);
752 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
753 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
754 DAG.getSrcValue(NULL));
755 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
761 ObjSize = ArgIncrement = 8;
763 case MVT::f32: ObjSize = 4; break;
764 case MVT::f64: ObjSize = ArgIncrement = 8; break;
767 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
769 if (ObjSize && !I->use_empty()) {
770 // Create the frame index object for this incoming parameter...
771 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
773 // Create the SelectionDAG nodes corresponding to a load from this
775 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
777 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
778 DAG.getSrcValue(NULL));
779 } else if (ArgValue.Val == 0) {
780 if (MVT::isInteger(ObjectVT))
781 ArgValue = DAG.getConstant(0, ObjectVT);
783 ArgValue = DAG.getConstantFP(0, ObjectVT);
785 ArgValues.push_back(ArgValue);
788 ArgOffset += ArgIncrement; // Move on to the next argument.
791 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
792 // arguments and the arguments after the retaddr has been pushed are aligned.
793 if ((ArgOffset & 7) == 0)
796 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
797 ReturnAddrIndex = 0; // No return address slot generated yet.
798 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
799 BytesCallerReserves = 0;
801 // Finally, inform the code generator which regs we return values in.
802 switch (getValueType(F.getReturnType())) {
803 default: assert(0 && "Unknown type!");
804 case MVT::isVoid: break;
809 MF.addLiveOut(X86::EAX);
812 MF.addLiveOut(X86::EAX);
813 MF.addLiveOut(X86::EDX);
817 MF.addLiveOut(X86::ST0);
823 std::pair<SDOperand, SDOperand>
824 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
825 bool isTailCall, SDOperand Callee,
826 ArgListTy &Args, SelectionDAG &DAG) {
827 // Count how many bytes are to be pushed on the stack.
828 unsigned NumBytes = 0;
830 // Keep track of the number of integer regs passed so far. This can be either
831 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
833 unsigned NumIntRegs = 0;
835 for (unsigned i = 0, e = Args.size(); i != e; ++i)
836 switch (getValueType(Args[i].second)) {
837 default: assert(0 && "Unknown value type!");
842 if (NumIntRegs < 2) {
851 if (NumIntRegs == 0) {
854 } else if (NumIntRegs == 1) {
866 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
867 // arguments and the arguments after the retaddr has been pushed are aligned.
868 if ((NumBytes & 7) == 0)
871 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
872 DAG.getConstant(NumBytes, getPointerTy()));
874 // Arguments go on the stack in reverse order, as specified by the ABI.
875 unsigned ArgOffset = 0;
876 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
879 std::vector<SDOperand> Stores;
880 std::vector<SDOperand> RegValuesToPass;
881 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
882 switch (getValueType(Args[i].second)) {
883 default: assert(0 && "Unexpected ValueType for argument!");
885 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
890 if (NumIntRegs < 2) {
891 RegValuesToPass.push_back(Args[i].first);
897 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
898 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
899 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
900 Args[i].first, PtrOff,
901 DAG.getSrcValue(NULL)));
906 if (NumIntRegs < 2) { // Can pass part of it in regs?
907 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
908 Args[i].first, DAG.getConstant(1, MVT::i32));
909 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
910 Args[i].first, DAG.getConstant(0, MVT::i32));
911 RegValuesToPass.push_back(Lo);
913 if (NumIntRegs < 2) { // Pass both parts in regs?
914 RegValuesToPass.push_back(Hi);
917 // Pass the high part in memory.
918 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
919 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
920 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
921 Hi, PtrOff, DAG.getSrcValue(NULL)));
928 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
929 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
930 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
931 Args[i].first, PtrOff,
932 DAG.getSrcValue(NULL)));
938 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
940 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
941 // arguments and the arguments after the retaddr has been pushed are aligned.
942 if ((ArgOffset & 7) == 0)
945 std::vector<MVT::ValueType> RetVals;
946 MVT::ValueType RetTyVT = getValueType(RetTy);
948 RetVals.push_back(MVT::Other);
950 // The result values produced have to be legal. Promote the result.
952 case MVT::isVoid: break;
954 RetVals.push_back(RetTyVT);
959 RetVals.push_back(MVT::i32);
963 RetVals.push_back(MVT::f32);
965 RetVals.push_back(MVT::f64);
968 RetVals.push_back(MVT::i32);
969 RetVals.push_back(MVT::i32);
974 // Build a sequence of copy-to-reg nodes chained together with token chain
975 // and flag operands which copy the outgoing args into registers.
977 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
979 SDOperand RegToPass = RegValuesToPass[i];
980 switch (RegToPass.getValueType()) {
981 default: assert(0 && "Bad thing to pass in regs");
983 CCReg = (i == 0) ? X86::AL : X86::DL;
986 CCReg = (i == 0) ? X86::AX : X86::DX;
989 CCReg = (i == 0) ? X86::EAX : X86::EDX;
993 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
994 InFlag = Chain.getValue(1);
997 std::vector<MVT::ValueType> NodeTys;
998 NodeTys.push_back(MVT::Other); // Returns a chain
999 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1000 std::vector<SDOperand> Ops;
1001 Ops.push_back(Chain);
1002 Ops.push_back(Callee);
1004 Ops.push_back(InFlag);
1006 // FIXME: Do not generate X86ISD::TAILCALL for now.
1007 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1008 InFlag = Chain.getValue(1);
1011 if (RetTyVT != MVT::isVoid) {
1013 default: assert(0 && "Unknown value type to return!");
1016 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1017 Chain = RetVal.getValue(1);
1020 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1021 Chain = RetVal.getValue(1);
1024 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1025 Chain = RetVal.getValue(1);
1028 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1029 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1031 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1032 Chain = Hi.getValue(1);
1037 std::vector<MVT::ValueType> Tys;
1038 Tys.push_back(MVT::f64);
1039 Tys.push_back(MVT::Other);
1040 std::vector<SDOperand> Ops;
1041 Ops.push_back(Chain);
1042 Ops.push_back(InFlag);
1043 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1044 Chain = RetVal.getValue(1);
1046 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1047 MachineFunction &MF = DAG.getMachineFunction();
1048 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1049 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1051 Tys.push_back(MVT::Other);
1053 Ops.push_back(Chain);
1054 Ops.push_back(RetVal);
1055 Ops.push_back(StackSlot);
1056 Ops.push_back(DAG.getValueType(RetTyVT));
1057 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1058 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1059 DAG.getSrcValue(NULL));
1060 Chain = RetVal.getValue(1);
1061 } else if (RetTyVT == MVT::f32)
1062 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1068 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1069 DAG.getConstant(ArgOffset, getPointerTy()),
1070 DAG.getConstant(ArgOffset, getPointerTy()));
1071 return std::make_pair(RetVal, Chain);
1073 std::vector<SDOperand> Ops;
1074 Ops.push_back(Chain);
1075 Ops.push_back(Callee);
1076 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1077 // Callee pops all arg values on the stack.
1078 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1080 // Pass register arguments as needed.
1081 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1083 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1085 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1087 SDOperand ResultVal;
1089 case MVT::isVoid: break;
1091 ResultVal = TheCall.getValue(1);
1096 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1099 // FIXME: we would really like to remember that this FP_ROUND operation is
1100 // okay to eliminate if we allow excess FP precision.
1101 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1104 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1105 TheCall.getValue(2));
1109 return std::make_pair(ResultVal, Chain);
1113 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1114 if (ReturnAddrIndex == 0) {
1115 // Set up a frame object for the return address.
1116 MachineFunction &MF = DAG.getMachineFunction();
1117 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1120 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1125 std::pair<SDOperand, SDOperand> X86TargetLowering::
1126 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1127 SelectionDAG &DAG) {
1129 if (Depth) // Depths > 0 not supported yet!
1130 Result = DAG.getConstant(0, getPointerTy());
1132 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1133 if (!isFrameAddress)
1134 // Just load the return address
1135 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1136 DAG.getSrcValue(NULL));
1138 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1139 DAG.getConstant(4, MVT::i32));
1141 return std::make_pair(Result, Chain);
1144 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1145 /// which corresponds to the condition code.
1146 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1148 default: assert(0 && "Unknown X86 conditional code!");
1149 case X86ISD::COND_A: return X86::JA;
1150 case X86ISD::COND_AE: return X86::JAE;
1151 case X86ISD::COND_B: return X86::JB;
1152 case X86ISD::COND_BE: return X86::JBE;
1153 case X86ISD::COND_E: return X86::JE;
1154 case X86ISD::COND_G: return X86::JG;
1155 case X86ISD::COND_GE: return X86::JGE;
1156 case X86ISD::COND_L: return X86::JL;
1157 case X86ISD::COND_LE: return X86::JLE;
1158 case X86ISD::COND_NE: return X86::JNE;
1159 case X86ISD::COND_NO: return X86::JNO;
1160 case X86ISD::COND_NP: return X86::JNP;
1161 case X86ISD::COND_NS: return X86::JNS;
1162 case X86ISD::COND_O: return X86::JO;
1163 case X86ISD::COND_P: return X86::JP;
1164 case X86ISD::COND_S: return X86::JS;
1168 /// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1169 /// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
1170 /// do a direct translation.
1171 static unsigned getX86CC(SDOperand CC, bool isFP) {
1172 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1173 unsigned X86CC = X86ISD::COND_INVALID;
1175 switch (SetCCOpcode) {
1177 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1178 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1179 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1180 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1181 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1182 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1183 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1184 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1185 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1186 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1189 // On a floating point condition, the flags are set as follows:
1191 // 0 | 0 | 0 | X > Y
1192 // 0 | 0 | 1 | X < Y
1193 // 1 | 0 | 0 | X == Y
1194 // 1 | 1 | 1 | unordered
1195 switch (SetCCOpcode) {
1198 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1200 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1202 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1204 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1206 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1208 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1209 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1210 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1216 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1217 /// code. Current x86 isa includes the following FP cmov instructions:
1218 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1219 static bool hasFPCMov(unsigned X86CC) {
1223 case X86ISD::COND_B:
1224 case X86ISD::COND_BE:
1225 case X86ISD::COND_E:
1226 case X86ISD::COND_P:
1227 case X86ISD::COND_A:
1228 case X86ISD::COND_AE:
1229 case X86ISD::COND_NE:
1230 case X86ISD::COND_NP:
1236 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1237 MachineBasicBlock *BB) {
1238 assert((MI->getOpcode() == X86::CMOV_FR32 ||
1239 MI->getOpcode() == X86::CMOV_FR64) &&
1240 "Unexpected instr type to insert");
1242 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1243 // control-flow pattern. The incoming instruction knows the destination vreg
1244 // to set, the condition code register to branch on, the true/false values to
1245 // select between, and a branch opcode to use.
1246 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1247 ilist<MachineBasicBlock>::iterator It = BB;
1253 // cmpTY ccX, r1, r2
1255 // fallthrough --> copy0MBB
1256 MachineBasicBlock *thisMBB = BB;
1257 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1258 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1259 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1260 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1261 MachineFunction *F = BB->getParent();
1262 F->getBasicBlockList().insert(It, copy0MBB);
1263 F->getBasicBlockList().insert(It, sinkMBB);
1264 // Update machine-CFG edges
1265 BB->addSuccessor(copy0MBB);
1266 BB->addSuccessor(sinkMBB);
1269 // %FalseValue = ...
1270 // # fallthrough to sinkMBB
1273 // Update machine-CFG edges
1274 BB->addSuccessor(sinkMBB);
1277 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1280 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1281 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1282 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1284 delete MI; // The pseudo instruction is gone now.
1289 //===----------------------------------------------------------------------===//
1290 // X86 Custom Lowering Hooks
1291 //===----------------------------------------------------------------------===//
1293 /// LowerOperation - Provide custom lowering hooks for some operations.
1295 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1296 switch (Op.getOpcode()) {
1297 default: assert(0 && "Should not custom lower this!");
1298 case ISD::ADD_PARTS:
1299 case ISD::SUB_PARTS: {
1300 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1301 "Not an i64 add/sub!");
1302 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1303 std::vector<MVT::ValueType> Tys;
1304 Tys.push_back(MVT::i32);
1305 Tys.push_back(MVT::Flag);
1306 std::vector<SDOperand> Ops;
1307 Ops.push_back(Op.getOperand(0));
1308 Ops.push_back(Op.getOperand(2));
1309 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1311 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1312 Op.getOperand(1), Op.getOperand(3),
1315 Tys.push_back(MVT::i32);
1316 Tys.push_back(MVT::i32);
1320 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1322 case ISD::SHL_PARTS:
1323 case ISD::SRA_PARTS:
1324 case ISD::SRL_PARTS: {
1325 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1326 "Not an i64 shift!");
1327 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1328 SDOperand ShOpLo = Op.getOperand(0);
1329 SDOperand ShOpHi = Op.getOperand(1);
1330 SDOperand ShAmt = Op.getOperand(2);
1331 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1332 DAG.getConstant(31, MVT::i32))
1333 : DAG.getConstant(0, MVT::i32);
1335 SDOperand Tmp2, Tmp3;
1336 if (Op.getOpcode() == ISD::SHL_PARTS) {
1337 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1338 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1340 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1341 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SHL, MVT::i32, ShOpHi, ShAmt);
1344 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1345 ShAmt, DAG.getConstant(32, MVT::i8));
1348 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1350 std::vector<MVT::ValueType> Tys;
1351 Tys.push_back(MVT::i32);
1352 Tys.push_back(MVT::Flag);
1353 std::vector<SDOperand> Ops;
1354 if (Op.getOpcode() == ISD::SHL_PARTS) {
1355 Ops.push_back(Tmp2);
1356 Ops.push_back(Tmp3);
1358 Ops.push_back(InFlag);
1359 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1360 InFlag = Hi.getValue(1);
1363 Ops.push_back(Tmp3);
1364 Ops.push_back(Tmp1);
1366 Ops.push_back(InFlag);
1367 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1369 Ops.push_back(Tmp2);
1370 Ops.push_back(Tmp3);
1372 Ops.push_back(InFlag);
1373 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1374 InFlag = Lo.getValue(1);
1377 Ops.push_back(Tmp3);
1378 Ops.push_back(Tmp1);
1380 Ops.push_back(InFlag);
1381 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1385 Tys.push_back(MVT::i32);
1386 Tys.push_back(MVT::i32);
1390 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1392 case ISD::SINT_TO_FP: {
1393 assert(Op.getValueType() == MVT::f64 &&
1394 Op.getOperand(0).getValueType() == MVT::i64 &&
1395 "Unknown SINT_TO_FP to lower!");
1396 // We lower sint64->FP into a store to a temporary stack slot, followed by a
1398 MachineFunction &MF = DAG.getMachineFunction();
1399 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1400 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1401 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1402 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
1403 std::vector<MVT::ValueType> RTs;
1404 RTs.push_back(MVT::f64);
1405 RTs.push_back(MVT::Other);
1406 std::vector<SDOperand> Ops;
1407 Ops.push_back(Store);
1408 Ops.push_back(StackSlot);
1409 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1411 case ISD::FP_TO_SINT: {
1412 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1413 Op.getOperand(0).getValueType() == MVT::f64 &&
1414 "Unknown FP_TO_SINT to lower!");
1415 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1417 MachineFunction &MF = DAG.getMachineFunction();
1418 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1419 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1420 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1423 switch (Op.getValueType()) {
1424 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1425 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1426 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1427 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1430 // Build the FP_TO_INT*_IN_MEM
1431 std::vector<SDOperand> Ops;
1432 Ops.push_back(DAG.getEntryNode());
1433 Ops.push_back(Op.getOperand(0));
1434 Ops.push_back(StackSlot);
1435 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1438 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1439 DAG.getSrcValue(NULL));
1441 case ISD::READCYCLECOUNTER: {
1442 std::vector<MVT::ValueType> Tys;
1443 Tys.push_back(MVT::Other);
1444 Tys.push_back(MVT::Flag);
1445 std::vector<SDOperand> Ops;
1446 Ops.push_back(Op.getOperand(0));
1447 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1449 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1450 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1451 MVT::i32, Ops[0].getValue(2)));
1452 Ops.push_back(Ops[1].getValue(1));
1453 Tys[0] = Tys[1] = MVT::i32;
1454 Tys.push_back(MVT::Other);
1455 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1458 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1459 SDOperand CC = Op.getOperand(2);
1460 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1461 Op.getOperand(0), Op.getOperand(1));
1462 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1463 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1464 unsigned X86CC = getX86CC(CC, isFP);
1465 if (X86CC != X86ISD::COND_INVALID) {
1466 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1467 DAG.getConstant(X86CC, MVT::i8), Cond);
1469 assert(isFP && "Illegal integer SetCC!");
1471 std::vector<MVT::ValueType> Tys;
1472 std::vector<SDOperand> Ops;
1473 switch (SetCCOpcode) {
1474 default: assert(false && "Illegal floating point SetCC!");
1475 case ISD::SETOEQ: { // !PF & ZF
1476 Tys.push_back(MVT::i8);
1477 Tys.push_back(MVT::Flag);
1478 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1479 Ops.push_back(Cond);
1480 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1481 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1482 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1484 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1486 case ISD::SETOLT: { // !PF & CF
1487 Tys.push_back(MVT::i8);
1488 Tys.push_back(MVT::Flag);
1489 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1490 Ops.push_back(Cond);
1491 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1492 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1493 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1495 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1497 case ISD::SETOLE: { // !PF & (CF || ZF)
1498 Tys.push_back(MVT::i8);
1499 Tys.push_back(MVT::Flag);
1500 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1501 Ops.push_back(Cond);
1502 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1503 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1504 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1506 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1508 case ISD::SETUGT: { // PF | (!ZF & !CF)
1509 Tys.push_back(MVT::i8);
1510 Tys.push_back(MVT::Flag);
1511 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1512 Ops.push_back(Cond);
1513 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1514 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1515 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1517 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1519 case ISD::SETUGE: { // PF | !CF
1520 Tys.push_back(MVT::i8);
1521 Tys.push_back(MVT::Flag);
1522 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1523 Ops.push_back(Cond);
1524 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1525 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1526 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1528 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1530 case ISD::SETUNE: { // PF | !ZF
1531 Tys.push_back(MVT::i8);
1532 Tys.push_back(MVT::Flag);
1533 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1534 Ops.push_back(Cond);
1535 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1536 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1537 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1539 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1545 MVT::ValueType VT = Op.getValueType();
1546 bool isFP = MVT::isFloatingPoint(VT);
1547 bool isFPStack = isFP && (X86Vector < SSE2);
1548 bool isFPSSE = isFP && (X86Vector >= SSE2);
1549 bool isValid = false;
1550 SDOperand Op0 = Op.getOperand(0);
1552 if (Op0.getOpcode() == X86ISD::SETCC) {
1553 CC = Op0.getOperand(0);
1554 Cond = Op0.getOperand(1);
1556 !(isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()));
1557 } else if (Op0.getOpcode() == ISD::SETCC) {
1558 CC = Op0.getOperand(2);
1559 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
1560 unsigned X86CC = getX86CC(CC, isFP);
1561 CC = DAG.getConstant(X86CC, MVT::i8);
1562 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1563 Op0.getOperand(0), Op0.getOperand(1));
1568 CC = DAG.getConstant(X86ISD::COND_E, MVT::i8);
1569 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
1572 std::vector<MVT::ValueType> Tys;
1573 Tys.push_back(Op.getValueType());
1574 Tys.push_back(MVT::Flag);
1575 std::vector<SDOperand> Ops;
1576 Ops.push_back(Op.getOperand(1));
1577 Ops.push_back(Op.getOperand(2));
1579 Ops.push_back(Cond);
1580 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
1583 SDOperand Cond = Op.getOperand(1);
1584 SDOperand Dest = Op.getOperand(2);
1586 // TODO: handle Cond == OR / AND / XOR
1587 if (Cond.getOpcode() == X86ISD::SETCC) {
1588 CC = Cond.getOperand(0);
1589 Cond = Cond.getOperand(1);
1590 } else if (Cond.getOpcode() == ISD::SETCC) {
1591 CC = Cond.getOperand(2);
1592 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
1593 unsigned X86CC = getX86CC(CC, isFP);
1594 CC = DAG.getConstant(X86CC, MVT::i8);
1595 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1596 Cond.getOperand(0), Cond.getOperand(1));
1598 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1599 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1601 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1602 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1605 // Can only be return void.
1606 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1607 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1609 case ISD::GlobalAddress: {
1610 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1611 SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
1612 // For Darwin, external and weak symbols are indirect, so we want to load
1613 // the value at address GV, not the value of GV itself. This means that
1614 // the GlobalAddress must be in the base or index register of the address,
1615 // not the GV offset field.
1616 if (getTargetMachine().
1617 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1618 (GV->hasWeakLinkage() || GV->isExternal()))
1619 return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1620 GVOp, DAG.getSrcValue(NULL));
1628 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1630 default: return NULL;
1631 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1632 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1633 case X86ISD::ADC: return "X86ISD::ADC";
1634 case X86ISD::SBB: return "X86ISD::SBB";
1635 case X86ISD::SHLD: return "X86ISD::SHLD";
1636 case X86ISD::SHRD: return "X86ISD::SHRD";
1637 case X86ISD::FILD64m: return "X86ISD::FILD64m";
1638 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1639 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1640 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
1641 case X86ISD::FLD: return "X86ISD::FLD";
1642 case X86ISD::FST: return "X86ISD::FST";
1643 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
1644 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
1645 case X86ISD::CALL: return "X86ISD::CALL";
1646 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1647 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1648 case X86ISD::CMP: return "X86ISD::CMP";
1649 case X86ISD::TEST: return "X86ISD::TEST";
1650 case X86ISD::SETCC: return "X86ISD::SETCC";
1651 case X86ISD::CMOV: return "X86ISD::CMOV";
1652 case X86ISD::BRCOND: return "X86ISD::BRCOND";
1653 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
1657 bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1658 uint64_t Mask) const {
1660 unsigned Opc = Op.getOpcode();
1664 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1666 case X86ISD::SETCC: return (Mask & 1) == 0;