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 "X86InstrBuilder.h"
17 #include "X86ISelLowering.h"
18 #include "X86TargetMachine.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/SSARegMap.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/ADT/VectorExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
37 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
39 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
42 // Set up the TargetLowering object.
44 // X86 is weird, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
47 setSetCCResultContents(ZeroOrOneSetCCResult);
48 setSchedulingPreference(SchedulingForRegPressure);
49 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
50 setStackPointerRegisterToSaveRestore(X86::ESP);
52 // Set up the register classes.
53 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
59 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
61 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
74 // SSE has no i16 to fp conversion, only i32
75 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
76 else if (!X86PatIsel) {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
81 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
86 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
94 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
95 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
100 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
102 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
105 // Expand FP_TO_UINT into a select.
106 // FIXME: We would like to use a Custom expander here eventually to do
107 // the optimal thing for SSE vs. the default expansion in the legalizer.
108 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
110 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
112 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
113 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
118 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
119 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
120 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
121 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
122 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
126 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
127 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
128 setOperationAction(ISD::FREM , MVT::f64 , Expand);
129 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
130 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
132 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
133 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
135 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
136 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
137 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
138 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
141 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
142 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
143 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
144 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
145 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
146 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
147 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
149 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
151 setOperationAction(ISD::READIO , MVT::i1 , Expand);
152 setOperationAction(ISD::READIO , MVT::i8 , Expand);
153 setOperationAction(ISD::READIO , MVT::i16 , Expand);
154 setOperationAction(ISD::READIO , MVT::i32 , Expand);
155 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
156 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
157 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
158 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
160 // These should be promoted to a larger select which is supported.
161 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
162 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
164 // X86 wants to expand cmov itself.
165 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
166 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
167 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
168 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
169 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
170 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
171 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
172 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
173 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
174 // X86 ret instruction may pop stack.
175 setOperationAction(ISD::RET , MVT::Other, Custom);
177 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
178 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
179 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
180 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
181 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
182 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
183 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
184 // X86 wants to expand memset / memcpy itself.
185 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
186 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
189 // We don't have line number support yet.
190 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
191 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
192 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
194 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
195 setOperationAction(ISD::VASTART , MVT::Other, Custom);
197 // Use the default implementation.
198 setOperationAction(ISD::VAARG , MVT::Other, Expand);
199 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
200 setOperationAction(ISD::VAEND , MVT::Other, Expand);
201 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
202 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
203 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
206 // Set up the FP register classes.
207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
210 // SSE has no load+extend ops
211 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
212 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
214 // Use ANDPD to simulate FABS.
215 setOperationAction(ISD::FABS , MVT::f64, Custom);
216 setOperationAction(ISD::FABS , MVT::f32, Custom);
218 // Use XORP to simulate FNEG.
219 setOperationAction(ISD::FNEG , MVT::f64, Custom);
220 setOperationAction(ISD::FNEG , MVT::f32, Custom);
222 // We don't support sin/cos/fmod
223 setOperationAction(ISD::FSIN , MVT::f64, Expand);
224 setOperationAction(ISD::FCOS , MVT::f64, Expand);
225 setOperationAction(ISD::FREM , MVT::f64, Expand);
226 setOperationAction(ISD::FSIN , MVT::f32, Expand);
227 setOperationAction(ISD::FCOS , MVT::f32, Expand);
228 setOperationAction(ISD::FREM , MVT::f32, Expand);
230 // Expand FP immediates into loads from the stack, except for the special
232 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
233 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
234 addLegalFPImmediate(+0.0); // xorps / xorpd
236 // Set up the FP register classes.
237 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
239 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
242 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
243 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
246 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
247 addLegalFPImmediate(+0.0); // FLD0
248 addLegalFPImmediate(+1.0); // FLD1
249 addLegalFPImmediate(-0.0); // FLD0/FCHS
250 addLegalFPImmediate(-1.0); // FLD1/FCHS
252 computeRegisterProperties();
254 // FIXME: These should be based on subtarget info. Plus, the values should
255 // be smaller when we are in optimizing for size mode.
256 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
257 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
258 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
259 allowUnalignedMemoryAccesses = true; // x86 supports it!
262 std::vector<SDOperand>
263 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
264 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
265 return LowerFastCCArguments(F, DAG);
266 return LowerCCCArguments(F, DAG);
269 std::pair<SDOperand, SDOperand>
270 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
271 bool isVarArg, unsigned CallingConv,
273 SDOperand Callee, ArgListTy &Args,
275 assert((!isVarArg || CallingConv == CallingConv::C) &&
276 "Only C takes varargs!");
278 // If the callee is a GlobalAddress node (quite common, every direct call is)
279 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
280 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
281 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
282 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
283 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
285 if (CallingConv == CallingConv::Fast && EnableFastCC)
286 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
287 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
290 //===----------------------------------------------------------------------===//
291 // C Calling Convention implementation
292 //===----------------------------------------------------------------------===//
294 std::vector<SDOperand>
295 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
296 std::vector<SDOperand> ArgValues;
298 MachineFunction &MF = DAG.getMachineFunction();
299 MachineFrameInfo *MFI = MF.getFrameInfo();
301 // Add DAG nodes to load the arguments... On entry to a function on the X86,
302 // the stack frame looks like this:
304 // [ESP] -- return address
305 // [ESP + 4] -- first argument (leftmost lexically)
306 // [ESP + 8] -- second argument, if first argument is four bytes in size
309 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
310 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
311 MVT::ValueType ObjectVT = getValueType(I->getType());
312 unsigned ArgIncrement = 4;
315 default: assert(0 && "Unhandled argument type!");
317 case MVT::i8: ObjSize = 1; break;
318 case MVT::i16: ObjSize = 2; break;
319 case MVT::i32: ObjSize = 4; break;
320 case MVT::i64: ObjSize = ArgIncrement = 8; break;
321 case MVT::f32: ObjSize = 4; break;
322 case MVT::f64: ObjSize = ArgIncrement = 8; break;
324 // Create the frame index object for this incoming parameter...
325 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
327 // Create the SelectionDAG nodes corresponding to a load from this parameter
328 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
330 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
334 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
335 DAG.getSrcValue(NULL));
337 if (MVT::isInteger(ObjectVT))
338 ArgValue = DAG.getConstant(0, ObjectVT);
340 ArgValue = DAG.getConstantFP(0, ObjectVT);
342 ArgValues.push_back(ArgValue);
344 ArgOffset += ArgIncrement; // Move on to the next argument...
347 // If the function takes variable number of arguments, make a frame index for
348 // the start of the first vararg value... for expansion of llvm.va_start.
350 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
351 ReturnAddrIndex = 0; // No return address slot generated yet.
352 BytesToPopOnReturn = 0; // Callee pops nothing.
353 BytesCallerReserves = ArgOffset;
355 // Finally, inform the code generator which regs we return values in.
356 switch (getValueType(F.getReturnType())) {
357 default: assert(0 && "Unknown type!");
358 case MVT::isVoid: break;
363 MF.addLiveOut(X86::EAX);
366 MF.addLiveOut(X86::EAX);
367 MF.addLiveOut(X86::EDX);
371 MF.addLiveOut(X86::ST0);
377 std::pair<SDOperand, SDOperand>
378 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
379 bool isVarArg, bool isTailCall,
380 SDOperand Callee, ArgListTy &Args,
382 // Count how many bytes are to be pushed on the stack.
383 unsigned NumBytes = 0;
387 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
389 for (unsigned i = 0, e = Args.size(); i != e; ++i)
390 switch (getValueType(Args[i].second)) {
391 default: assert(0 && "Unknown value type!");
405 Chain = DAG.getCALLSEQ_START(Chain,
406 DAG.getConstant(NumBytes, getPointerTy()));
408 // Arguments go on the stack in reverse order, as specified by the ABI.
409 unsigned ArgOffset = 0;
410 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
411 std::vector<SDOperand> Stores;
413 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
414 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
415 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
417 switch (getValueType(Args[i].second)) {
418 default: assert(0 && "Unexpected ValueType for argument!");
422 // Promote the integer to 32 bits. If the input type is signed use a
423 // sign extend, otherwise use a zero extend.
424 if (Args[i].second->isSigned())
425 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
427 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
432 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
433 Args[i].first, PtrOff,
434 DAG.getSrcValue(NULL)));
439 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
440 Args[i].first, PtrOff,
441 DAG.getSrcValue(NULL)));
446 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
449 std::vector<MVT::ValueType> RetVals;
450 MVT::ValueType RetTyVT = getValueType(RetTy);
451 RetVals.push_back(MVT::Other);
453 // The result values produced have to be legal. Promote the result.
455 case MVT::isVoid: break;
457 RetVals.push_back(RetTyVT);
462 RetVals.push_back(MVT::i32);
466 RetVals.push_back(MVT::f32);
468 RetVals.push_back(MVT::f64);
471 RetVals.push_back(MVT::i32);
472 RetVals.push_back(MVT::i32);
477 std::vector<MVT::ValueType> NodeTys;
478 NodeTys.push_back(MVT::Other); // Returns a chain
479 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
480 std::vector<SDOperand> Ops;
481 Ops.push_back(Chain);
482 Ops.push_back(Callee);
484 // FIXME: Do not generate X86ISD::TAILCALL for now.
485 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
486 SDOperand InFlag = Chain.getValue(1);
489 NodeTys.push_back(MVT::Other); // Returns a chain
490 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
492 Ops.push_back(Chain);
493 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
494 Ops.push_back(DAG.getConstant(0, getPointerTy()));
495 Ops.push_back(InFlag);
496 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
497 InFlag = Chain.getValue(1);
500 if (RetTyVT != MVT::isVoid) {
502 default: assert(0 && "Unknown value type to return!");
505 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
506 Chain = RetVal.getValue(1);
507 if (RetTyVT == MVT::i1)
508 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
511 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
512 Chain = RetVal.getValue(1);
515 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
516 Chain = RetVal.getValue(1);
519 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
520 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
522 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
523 Chain = Hi.getValue(1);
528 std::vector<MVT::ValueType> Tys;
529 Tys.push_back(MVT::f64);
530 Tys.push_back(MVT::Other);
531 Tys.push_back(MVT::Flag);
532 std::vector<SDOperand> Ops;
533 Ops.push_back(Chain);
534 Ops.push_back(InFlag);
535 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
536 Chain = RetVal.getValue(1);
537 InFlag = RetVal.getValue(2);
539 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
540 // shouldn't be necessary except that RFP cannot be live across
541 // multiple blocks. When stackifier is fixed, they can be uncoupled.
542 MachineFunction &MF = DAG.getMachineFunction();
543 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
544 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
546 Tys.push_back(MVT::Other);
548 Ops.push_back(Chain);
549 Ops.push_back(RetVal);
550 Ops.push_back(StackSlot);
551 Ops.push_back(DAG.getValueType(RetTyVT));
552 Ops.push_back(InFlag);
553 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
554 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
555 DAG.getSrcValue(NULL));
556 Chain = RetVal.getValue(1);
559 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
560 // FIXME: we would really like to remember that this FP_ROUND
561 // operation is okay to eliminate if we allow excess FP precision.
562 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
568 return std::make_pair(RetVal, Chain);
570 std::vector<SDOperand> Ops;
571 Ops.push_back(Chain);
572 Ops.push_back(Callee);
573 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
574 Ops.push_back(DAG.getConstant(0, getPointerTy()));
576 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
581 case MVT::isVoid: break;
583 ResultVal = TheCall.getValue(1);
588 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
591 // FIXME: we would really like to remember that this FP_ROUND operation is
592 // okay to eliminate if we allow excess FP precision.
593 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
596 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
597 TheCall.getValue(2));
601 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
602 return std::make_pair(ResultVal, Chain);
606 //===----------------------------------------------------------------------===//
607 // Fast Calling Convention implementation
608 //===----------------------------------------------------------------------===//
610 // The X86 'fast' calling convention passes up to two integer arguments in
611 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
612 // and requires that the callee pop its arguments off the stack (allowing proper
613 // tail calls), and has the same return value conventions as C calling convs.
615 // This calling convention always arranges for the callee pop value to be 8n+4
616 // bytes, which is needed for tail recursion elimination and stack alignment
619 // Note that this can be enhanced in the future to pass fp vals in registers
620 // (when we have a global fp allocator) and do other tricks.
623 /// AddLiveIn - This helper function adds the specified physical register to the
624 /// MachineFunction as a live in value. It also creates a corresponding virtual
626 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
627 TargetRegisterClass *RC) {
628 assert(RC->contains(PReg) && "Not the correct regclass!");
629 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
630 MF.addLiveIn(PReg, VReg);
635 std::vector<SDOperand>
636 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
637 std::vector<SDOperand> ArgValues;
639 MachineFunction &MF = DAG.getMachineFunction();
640 MachineFrameInfo *MFI = MF.getFrameInfo();
642 // Add DAG nodes to load the arguments... On entry to a function the stack
643 // frame looks like this:
645 // [ESP] -- return address
646 // [ESP + 4] -- first nonreg argument (leftmost lexically)
647 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
649 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
651 // Keep track of the number of integer regs passed so far. This can be either
652 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
654 unsigned NumIntRegs = 0;
656 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
657 MVT::ValueType ObjectVT = getValueType(I->getType());
658 unsigned ArgIncrement = 4;
659 unsigned ObjSize = 0;
663 default: assert(0 && "Unhandled argument type!");
666 if (NumIntRegs < 2) {
667 if (!I->use_empty()) {
668 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
669 X86::R8RegisterClass);
670 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
671 DAG.setRoot(ArgValue.getValue(1));
672 if (ObjectVT == MVT::i1)
673 // FIXME: Should insert a assertzext here.
674 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
683 if (NumIntRegs < 2) {
684 if (!I->use_empty()) {
685 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
686 X86::R16RegisterClass);
687 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
688 DAG.setRoot(ArgValue.getValue(1));
696 if (NumIntRegs < 2) {
697 if (!I->use_empty()) {
698 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
699 X86::R32RegisterClass);
700 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
701 DAG.setRoot(ArgValue.getValue(1));
709 if (NumIntRegs == 0) {
710 if (!I->use_empty()) {
711 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
712 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
714 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
715 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
716 DAG.setRoot(Hi.getValue(1));
718 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
722 } else if (NumIntRegs == 1) {
723 if (!I->use_empty()) {
724 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
725 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
726 DAG.setRoot(Low.getValue(1));
728 // Load the high part from memory.
729 // Create the frame index object for this incoming parameter...
730 int FI = MFI->CreateFixedObject(4, ArgOffset);
731 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
732 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
733 DAG.getSrcValue(NULL));
734 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
740 ObjSize = ArgIncrement = 8;
742 case MVT::f32: ObjSize = 4; break;
743 case MVT::f64: ObjSize = ArgIncrement = 8; break;
746 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
748 if (ObjSize && !I->use_empty()) {
749 // Create the frame index object for this incoming parameter...
750 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
752 // Create the SelectionDAG nodes corresponding to a load from this
754 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
756 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
757 DAG.getSrcValue(NULL));
758 } else if (ArgValue.Val == 0) {
759 if (MVT::isInteger(ObjectVT))
760 ArgValue = DAG.getConstant(0, ObjectVT);
762 ArgValue = DAG.getConstantFP(0, ObjectVT);
764 ArgValues.push_back(ArgValue);
767 ArgOffset += ArgIncrement; // Move on to the next argument.
770 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
771 // arguments and the arguments after the retaddr has been pushed are aligned.
772 if ((ArgOffset & 7) == 0)
775 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
776 ReturnAddrIndex = 0; // No return address slot generated yet.
777 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
778 BytesCallerReserves = 0;
780 // Finally, inform the code generator which regs we return values in.
781 switch (getValueType(F.getReturnType())) {
782 default: assert(0 && "Unknown type!");
783 case MVT::isVoid: break;
788 MF.addLiveOut(X86::EAX);
791 MF.addLiveOut(X86::EAX);
792 MF.addLiveOut(X86::EDX);
796 MF.addLiveOut(X86::ST0);
802 std::pair<SDOperand, SDOperand>
803 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
804 bool isTailCall, SDOperand Callee,
805 ArgListTy &Args, SelectionDAG &DAG) {
806 // Count how many bytes are to be pushed on the stack.
807 unsigned NumBytes = 0;
809 // Keep track of the number of integer regs passed so far. This can be either
810 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
812 unsigned NumIntRegs = 0;
814 for (unsigned i = 0, e = Args.size(); i != e; ++i)
815 switch (getValueType(Args[i].second)) {
816 default: assert(0 && "Unknown value type!");
821 if (NumIntRegs < 2) {
830 if (NumIntRegs == 0) {
833 } else if (NumIntRegs == 1) {
845 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
846 // arguments and the arguments after the retaddr has been pushed are aligned.
847 if ((NumBytes & 7) == 0)
850 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
852 // Arguments go on the stack in reverse order, as specified by the ABI.
853 unsigned ArgOffset = 0;
854 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
856 std::vector<SDOperand> Stores;
857 std::vector<SDOperand> RegValuesToPass;
858 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
859 switch (getValueType(Args[i].second)) {
860 default: assert(0 && "Unexpected ValueType for argument!");
862 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
867 if (NumIntRegs < 2) {
868 RegValuesToPass.push_back(Args[i].first);
874 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
875 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
876 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
877 Args[i].first, PtrOff,
878 DAG.getSrcValue(NULL)));
883 if (NumIntRegs < 2) { // Can pass part of it in regs?
884 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
885 Args[i].first, DAG.getConstant(1, MVT::i32));
886 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
887 Args[i].first, DAG.getConstant(0, MVT::i32));
888 RegValuesToPass.push_back(Lo);
890 if (NumIntRegs < 2) { // Pass both parts in regs?
891 RegValuesToPass.push_back(Hi);
894 // Pass the high part in memory.
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 Hi, PtrOff, DAG.getSrcValue(NULL)));
905 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
906 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
907 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
908 Args[i].first, PtrOff,
909 DAG.getSrcValue(NULL)));
915 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
917 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
918 // arguments and the arguments after the retaddr has been pushed are aligned.
919 if ((ArgOffset & 7) == 0)
922 std::vector<MVT::ValueType> RetVals;
923 MVT::ValueType RetTyVT = getValueType(RetTy);
925 RetVals.push_back(MVT::Other);
927 // The result values produced have to be legal. Promote the result.
929 case MVT::isVoid: break;
931 RetVals.push_back(RetTyVT);
936 RetVals.push_back(MVT::i32);
940 RetVals.push_back(MVT::f32);
942 RetVals.push_back(MVT::f64);
945 RetVals.push_back(MVT::i32);
946 RetVals.push_back(MVT::i32);
951 // Build a sequence of copy-to-reg nodes chained together with token chain
952 // and flag operands which copy the outgoing args into registers.
954 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
956 SDOperand RegToPass = RegValuesToPass[i];
957 switch (RegToPass.getValueType()) {
958 default: assert(0 && "Bad thing to pass in regs");
960 CCReg = (i == 0) ? X86::AL : X86::DL;
963 CCReg = (i == 0) ? X86::AX : X86::DX;
966 CCReg = (i == 0) ? X86::EAX : X86::EDX;
970 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
971 InFlag = Chain.getValue(1);
974 std::vector<MVT::ValueType> NodeTys;
975 NodeTys.push_back(MVT::Other); // Returns a chain
976 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
977 std::vector<SDOperand> Ops;
978 Ops.push_back(Chain);
979 Ops.push_back(Callee);
981 Ops.push_back(InFlag);
983 // FIXME: Do not generate X86ISD::TAILCALL for now.
984 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
985 InFlag = Chain.getValue(1);
988 NodeTys.push_back(MVT::Other); // Returns a chain
989 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
991 Ops.push_back(Chain);
992 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
993 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
994 Ops.push_back(InFlag);
995 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
996 InFlag = Chain.getValue(1);
999 if (RetTyVT != MVT::isVoid) {
1001 default: assert(0 && "Unknown value type to return!");
1004 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1005 Chain = RetVal.getValue(1);
1006 if (RetTyVT == MVT::i1)
1007 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1010 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1011 Chain = RetVal.getValue(1);
1014 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1015 Chain = RetVal.getValue(1);
1018 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1019 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1021 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1022 Chain = Hi.getValue(1);
1027 std::vector<MVT::ValueType> Tys;
1028 Tys.push_back(MVT::f64);
1029 Tys.push_back(MVT::Other);
1030 Tys.push_back(MVT::Flag);
1031 std::vector<SDOperand> Ops;
1032 Ops.push_back(Chain);
1033 Ops.push_back(InFlag);
1034 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1035 Chain = RetVal.getValue(1);
1036 InFlag = RetVal.getValue(2);
1038 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1039 // shouldn't be necessary except that RFP cannot be live across
1040 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1041 MachineFunction &MF = DAG.getMachineFunction();
1042 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1043 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1045 Tys.push_back(MVT::Other);
1047 Ops.push_back(Chain);
1048 Ops.push_back(RetVal);
1049 Ops.push_back(StackSlot);
1050 Ops.push_back(DAG.getValueType(RetTyVT));
1051 Ops.push_back(InFlag);
1052 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1053 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1054 DAG.getSrcValue(NULL));
1055 Chain = RetVal.getValue(1);
1058 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1059 // FIXME: we would really like to remember that this FP_ROUND
1060 // operation is okay to eliminate if we allow excess FP precision.
1061 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1067 return std::make_pair(RetVal, Chain);
1069 std::vector<SDOperand> Ops;
1070 Ops.push_back(Chain);
1071 Ops.push_back(Callee);
1072 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1073 // Callee pops all arg values on the stack.
1074 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1076 // Pass register arguments as needed.
1077 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1079 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
1081 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1083 SDOperand ResultVal;
1085 case MVT::isVoid: break;
1087 ResultVal = TheCall.getValue(1);
1092 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1095 // FIXME: we would really like to remember that this FP_ROUND operation is
1096 // okay to eliminate if we allow excess FP precision.
1097 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1100 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1101 TheCall.getValue(2));
1105 return std::make_pair(ResultVal, Chain);
1109 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1110 if (ReturnAddrIndex == 0) {
1111 // Set up a frame object for the return address.
1112 MachineFunction &MF = DAG.getMachineFunction();
1113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1116 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1121 std::pair<SDOperand, SDOperand> X86TargetLowering::
1122 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1123 SelectionDAG &DAG) {
1125 if (Depth) // Depths > 0 not supported yet!
1126 Result = DAG.getConstant(0, getPointerTy());
1128 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1129 if (!isFrameAddress)
1130 // Just load the return address
1131 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1132 DAG.getSrcValue(NULL));
1134 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1135 DAG.getConstant(4, MVT::i32));
1137 return std::make_pair(Result, Chain);
1140 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1141 /// which corresponds to the condition code.
1142 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1144 default: assert(0 && "Unknown X86 conditional code!");
1145 case X86ISD::COND_A: return X86::JA;
1146 case X86ISD::COND_AE: return X86::JAE;
1147 case X86ISD::COND_B: return X86::JB;
1148 case X86ISD::COND_BE: return X86::JBE;
1149 case X86ISD::COND_E: return X86::JE;
1150 case X86ISD::COND_G: return X86::JG;
1151 case X86ISD::COND_GE: return X86::JGE;
1152 case X86ISD::COND_L: return X86::JL;
1153 case X86ISD::COND_LE: return X86::JLE;
1154 case X86ISD::COND_NE: return X86::JNE;
1155 case X86ISD::COND_NO: return X86::JNO;
1156 case X86ISD::COND_NP: return X86::JNP;
1157 case X86ISD::COND_NS: return X86::JNS;
1158 case X86ISD::COND_O: return X86::JO;
1159 case X86ISD::COND_P: return X86::JP;
1160 case X86ISD::COND_S: return X86::JS;
1164 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1165 /// specific condition code. It returns a false if it cannot do a direct
1166 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1167 /// the order of comparison operands should be flipped.
1168 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1170 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1172 X86CC = X86ISD::COND_INVALID;
1174 switch (SetCCOpcode) {
1176 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1177 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1178 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1179 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1180 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1181 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1182 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1183 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1184 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1185 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1188 // On a floating point condition, the flags are set as follows:
1190 // 0 | 0 | 0 | X > Y
1191 // 0 | 0 | 1 | X < Y
1192 // 1 | 0 | 0 | X == Y
1193 // 1 | 1 | 1 | unordered
1194 switch (SetCCOpcode) {
1197 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1198 case ISD::SETOLE: Flip = true; // Fallthrough
1200 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1201 case ISD::SETOLT: Flip = true; // Fallthrough
1203 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1204 case ISD::SETUGE: Flip = true; // Fallthrough
1206 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1207 case ISD::SETUGT: Flip = true; // Fallthrough
1209 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1211 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1212 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1213 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1217 return X86CC != X86ISD::COND_INVALID;
1220 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1221 /// code. Current x86 isa includes the following FP cmov instructions:
1222 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1223 static bool hasFPCMov(unsigned X86CC) {
1227 case X86ISD::COND_B:
1228 case X86ISD::COND_BE:
1229 case X86ISD::COND_E:
1230 case X86ISD::COND_P:
1231 case X86ISD::COND_A:
1232 case X86ISD::COND_AE:
1233 case X86ISD::COND_NE:
1234 case X86ISD::COND_NP:
1240 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1241 MachineBasicBlock *BB) {
1242 switch (MI->getOpcode()) {
1243 default: assert(false && "Unexpected instr type to insert");
1244 case X86::CMOV_FR32:
1245 case X86::CMOV_FR64: {
1246 // To "insert" a SELECT_CC instruction, we actually have to insert the
1247 // diamond control-flow pattern. The incoming instruction knows the
1248 // destination vreg to set, the condition code register to branch on, the
1249 // true/false values to select between, and a branch opcode to use.
1250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 ilist<MachineBasicBlock>::iterator It = BB;
1257 // cmpTY ccX, r1, r2
1259 // fallthrough --> copy0MBB
1260 MachineBasicBlock *thisMBB = BB;
1261 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1262 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1263 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1264 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1265 MachineFunction *F = BB->getParent();
1266 F->getBasicBlockList().insert(It, copy0MBB);
1267 F->getBasicBlockList().insert(It, sinkMBB);
1268 // Update machine-CFG edges
1269 BB->addSuccessor(copy0MBB);
1270 BB->addSuccessor(sinkMBB);
1273 // %FalseValue = ...
1274 // # fallthrough to sinkMBB
1277 // Update machine-CFG edges
1278 BB->addSuccessor(sinkMBB);
1281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1284 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1288 delete MI; // The pseudo instruction is gone now.
1292 case X86::FP_TO_INT16_IN_MEM:
1293 case X86::FP_TO_INT32_IN_MEM:
1294 case X86::FP_TO_INT64_IN_MEM: {
1295 // Change the floating point control register to use "round towards zero"
1296 // mode when truncating to an integer value.
1297 MachineFunction *F = BB->getParent();
1298 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1299 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1301 // Load the old value of the high byte of the control word...
1303 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1304 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1306 // Set the high part to be round to zero...
1307 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1309 // Reload the modified control word now...
1310 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1312 // Restore the memory image of control word to original value
1313 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1315 // Get the X86 opcode to use.
1317 switch (MI->getOpcode()) {
1318 default: assert(0 && "illegal opcode!");
1319 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1320 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1321 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1325 MachineOperand &Op = MI->getOperand(0);
1326 if (Op.isRegister()) {
1327 AM.BaseType = X86AddressMode::RegBase;
1328 AM.Base.Reg = Op.getReg();
1330 AM.BaseType = X86AddressMode::FrameIndexBase;
1331 AM.Base.FrameIndex = Op.getFrameIndex();
1333 Op = MI->getOperand(1);
1334 if (Op.isImmediate())
1335 AM.Scale = Op.getImmedValue();
1336 Op = MI->getOperand(2);
1337 if (Op.isImmediate())
1338 AM.IndexReg = Op.getImmedValue();
1339 Op = MI->getOperand(3);
1340 if (Op.isGlobalAddress()) {
1341 AM.GV = Op.getGlobal();
1343 AM.Disp = Op.getImmedValue();
1345 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1347 // Reload the original control word now.
1348 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1350 delete MI; // The pseudo instruction is gone now.
1357 //===----------------------------------------------------------------------===//
1358 // X86 Custom Lowering Hooks
1359 //===----------------------------------------------------------------------===//
1361 /// LowerOperation - Provide custom lowering hooks for some operations.
1363 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1364 switch (Op.getOpcode()) {
1365 default: assert(0 && "Should not custom lower this!");
1366 case ISD::ADD_PARTS:
1367 case ISD::SUB_PARTS: {
1368 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1369 "Not an i64 add/sub!");
1370 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1371 std::vector<MVT::ValueType> Tys;
1372 Tys.push_back(MVT::i32);
1373 Tys.push_back(MVT::Flag);
1374 std::vector<SDOperand> Ops;
1375 Ops.push_back(Op.getOperand(0));
1376 Ops.push_back(Op.getOperand(2));
1377 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1379 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1380 Op.getOperand(1), Op.getOperand(3),
1383 Tys.push_back(MVT::i32);
1384 Tys.push_back(MVT::i32);
1388 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1390 case ISD::SHL_PARTS:
1391 case ISD::SRA_PARTS:
1392 case ISD::SRL_PARTS: {
1393 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1394 "Not an i64 shift!");
1395 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1396 SDOperand ShOpLo = Op.getOperand(0);
1397 SDOperand ShOpHi = Op.getOperand(1);
1398 SDOperand ShAmt = Op.getOperand(2);
1399 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1400 DAG.getConstant(31, MVT::i8))
1401 : DAG.getConstant(0, MVT::i32);
1403 SDOperand Tmp2, Tmp3;
1404 if (Op.getOpcode() == ISD::SHL_PARTS) {
1405 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1406 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1408 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1409 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1412 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1413 ShAmt, DAG.getConstant(32, MVT::i8));
1416 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1418 std::vector<MVT::ValueType> Tys;
1419 Tys.push_back(MVT::i32);
1420 Tys.push_back(MVT::Flag);
1421 std::vector<SDOperand> Ops;
1422 if (Op.getOpcode() == ISD::SHL_PARTS) {
1423 Ops.push_back(Tmp2);
1424 Ops.push_back(Tmp3);
1426 Ops.push_back(InFlag);
1427 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1428 InFlag = Hi.getValue(1);
1431 Ops.push_back(Tmp3);
1432 Ops.push_back(Tmp1);
1434 Ops.push_back(InFlag);
1435 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1437 Ops.push_back(Tmp2);
1438 Ops.push_back(Tmp3);
1440 Ops.push_back(InFlag);
1441 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1442 InFlag = Lo.getValue(1);
1445 Ops.push_back(Tmp3);
1446 Ops.push_back(Tmp1);
1448 Ops.push_back(InFlag);
1449 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1453 Tys.push_back(MVT::i32);
1454 Tys.push_back(MVT::i32);
1458 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1460 case ISD::SINT_TO_FP: {
1461 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
1462 Op.getOperand(0).getValueType() >= MVT::i16 &&
1463 "Unknown SINT_TO_FP to lower!");
1466 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1467 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1468 MachineFunction &MF = DAG.getMachineFunction();
1469 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1470 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1471 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1472 DAG.getEntryNode(), Op.getOperand(0),
1473 StackSlot, DAG.getSrcValue(NULL));
1476 std::vector<MVT::ValueType> Tys;
1477 Tys.push_back(MVT::f64);
1478 Tys.push_back(MVT::Other);
1479 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
1480 std::vector<SDOperand> Ops;
1481 Ops.push_back(Chain);
1482 Ops.push_back(StackSlot);
1483 Ops.push_back(DAG.getValueType(SrcVT));
1484 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1488 Chain = Result.getValue(1);
1489 SDOperand InFlag = Result.getValue(2);
1491 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
1492 // shouldn't be necessary except that RFP cannot be live across
1493 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1494 MachineFunction &MF = DAG.getMachineFunction();
1495 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1496 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1497 std::vector<MVT::ValueType> Tys;
1498 Tys.push_back(MVT::Other);
1499 std::vector<SDOperand> Ops;
1500 Ops.push_back(Chain);
1501 Ops.push_back(Result);
1502 Ops.push_back(StackSlot);
1503 Ops.push_back(DAG.getValueType(Op.getValueType()));
1504 Ops.push_back(InFlag);
1505 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1506 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1507 DAG.getSrcValue(NULL));
1512 case ISD::FP_TO_SINT: {
1513 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1514 "Unknown FP_TO_SINT to lower!");
1515 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1517 MachineFunction &MF = DAG.getMachineFunction();
1518 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1519 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1520 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1523 switch (Op.getValueType()) {
1524 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1525 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1526 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1527 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1530 SDOperand Chain = DAG.getEntryNode();
1531 SDOperand Value = Op.getOperand(0);
1533 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1534 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1535 DAG.getSrcValue(0));
1536 std::vector<MVT::ValueType> Tys;
1537 Tys.push_back(MVT::f64);
1538 Tys.push_back(MVT::Other);
1539 std::vector<SDOperand> Ops;
1540 Ops.push_back(Chain);
1541 Ops.push_back(StackSlot);
1542 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
1543 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1544 Chain = Value.getValue(1);
1545 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1546 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1549 // Build the FP_TO_INT*_IN_MEM
1550 std::vector<SDOperand> Ops;
1551 Ops.push_back(Chain);
1552 Ops.push_back(Value);
1553 Ops.push_back(StackSlot);
1554 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1557 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1558 DAG.getSrcValue(NULL));
1560 case ISD::READCYCLECOUNTER: {
1561 std::vector<MVT::ValueType> Tys;
1562 Tys.push_back(MVT::Other);
1563 Tys.push_back(MVT::Flag);
1564 std::vector<SDOperand> Ops;
1565 Ops.push_back(Op.getOperand(0));
1566 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1568 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1569 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1570 MVT::i32, Ops[0].getValue(2)));
1571 Ops.push_back(Ops[1].getValue(1));
1572 Tys[0] = Tys[1] = MVT::i32;
1573 Tys.push_back(MVT::Other);
1574 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1577 MVT::ValueType VT = Op.getValueType();
1578 const Type *OpNTy = MVT::getTypeForValueType(VT);
1579 std::vector<Constant*> CV;
1580 if (VT == MVT::f64) {
1581 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1582 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1584 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1585 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1586 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1589 Constant *CS = ConstantStruct::get(CV);
1590 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1592 = DAG.getNode(X86ISD::LOAD_PACK,
1593 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1594 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1597 MVT::ValueType VT = Op.getValueType();
1598 const Type *OpNTy = MVT::getTypeForValueType(VT);
1599 std::vector<Constant*> CV;
1600 if (VT == MVT::f64) {
1601 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1602 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1604 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1605 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1606 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1607 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1609 Constant *CS = ConstantStruct::get(CV);
1610 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1612 = DAG.getNode(X86ISD::LOAD_PACK,
1613 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1614 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1617 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1619 SDOperand CC = Op.getOperand(2);
1620 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1621 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1624 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1626 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1627 Op.getOperand(1), Op.getOperand(0));
1629 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1630 Op.getOperand(0), Op.getOperand(1));
1631 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1632 DAG.getConstant(X86CC, MVT::i8), Cond);
1634 assert(isFP && "Illegal integer SetCC!");
1636 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1637 Op.getOperand(0), Op.getOperand(1));
1638 std::vector<MVT::ValueType> Tys;
1639 std::vector<SDOperand> Ops;
1640 switch (SetCCOpcode) {
1641 default: assert(false && "Illegal floating point SetCC!");
1642 case ISD::SETOEQ: { // !PF & ZF
1643 Tys.push_back(MVT::i8);
1644 Tys.push_back(MVT::Flag);
1645 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1646 Ops.push_back(Cond);
1647 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1648 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1649 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1651 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1653 case ISD::SETUNE: { // PF | !ZF
1654 Tys.push_back(MVT::i8);
1655 Tys.push_back(MVT::Flag);
1656 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1657 Ops.push_back(Cond);
1658 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1659 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1660 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1662 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1668 MVT::ValueType VT = Op.getValueType();
1669 bool isFP = MVT::isFloatingPoint(VT);
1670 bool isFPStack = isFP && !X86ScalarSSE;
1671 bool isFPSSE = isFP && X86ScalarSSE;
1672 bool addTest = false;
1673 SDOperand Op0 = Op.getOperand(0);
1675 if (Op0.getOpcode() == ISD::SETCC)
1676 Op0 = LowerOperation(Op0, DAG);
1678 if (Op0.getOpcode() == X86ISD::SETCC) {
1679 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1680 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1681 // have another use it will be eliminated.
1682 // If the X86ISD::SETCC has more than one use, then it's probably better
1683 // to use a test instead of duplicating the X86ISD::CMP (for register
1684 // pressure reason).
1685 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1686 if (!Op0.hasOneUse()) {
1687 std::vector<MVT::ValueType> Tys;
1688 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1689 Tys.push_back(Op0.Val->getValueType(i));
1690 std::vector<SDOperand> Ops;
1691 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1692 Ops.push_back(Op0.getOperand(i));
1693 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1696 CC = Op0.getOperand(0);
1697 Cond = Op0.getOperand(1);
1698 // Make a copy as flag result cannot be used by more than one.
1699 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1700 Cond.getOperand(0), Cond.getOperand(1));
1702 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
1709 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1710 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
1713 std::vector<MVT::ValueType> Tys;
1714 Tys.push_back(Op.getValueType());
1715 Tys.push_back(MVT::Flag);
1716 std::vector<SDOperand> Ops;
1717 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1718 // condition is true.
1719 Ops.push_back(Op.getOperand(2));
1720 Ops.push_back(Op.getOperand(1));
1722 Ops.push_back(Cond);
1723 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
1726 bool addTest = false;
1727 SDOperand Cond = Op.getOperand(1);
1728 SDOperand Dest = Op.getOperand(2);
1730 if (Cond.getOpcode() == ISD::SETCC)
1731 Cond = LowerOperation(Cond, DAG);
1733 if (Cond.getOpcode() == X86ISD::SETCC) {
1734 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1735 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1736 // have another use it will be eliminated.
1737 // If the X86ISD::SETCC has more than one use, then it's probably better
1738 // to use a test instead of duplicating the X86ISD::CMP (for register
1739 // pressure reason).
1740 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1741 if (!Cond.hasOneUse()) {
1742 std::vector<MVT::ValueType> Tys;
1743 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1744 Tys.push_back(Cond.Val->getValueType(i));
1745 std::vector<SDOperand> Ops;
1746 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1747 Ops.push_back(Cond.getOperand(i));
1748 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1751 CC = Cond.getOperand(0);
1752 Cond = Cond.getOperand(1);
1753 // Make a copy as flag result cannot be used by more than one.
1754 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1755 Cond.getOperand(0), Cond.getOperand(1));
1762 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1763 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1765 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1766 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1770 SDOperand Chain = Op.getOperand(0);
1772 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1773 if (Align == 0) Align = 1;
1777 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1779 unsigned Val = ValC->getValue() & 255;
1781 // If the value is a constant, then we can potentially use larger sets.
1782 switch (Align & 3) {
1783 case 2: // WORD aligned
1785 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1786 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1788 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1789 DAG.getConstant(1, MVT::i8));
1790 Val = (Val << 8) | Val;
1793 case 0: // DWORD aligned
1795 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1796 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1798 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1799 DAG.getConstant(2, MVT::i8));
1800 Val = (Val << 8) | Val;
1801 Val = (Val << 16) | Val;
1804 default: // Byte aligned
1806 Count = Op.getOperand(3);
1811 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1813 InFlag = Chain.getValue(1);
1816 Count = Op.getOperand(3);
1817 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1818 InFlag = Chain.getValue(1);
1821 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1822 InFlag = Chain.getValue(1);
1823 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1824 InFlag = Chain.getValue(1);
1826 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1827 DAG.getValueType(AVT), InFlag);
1830 SDOperand Chain = Op.getOperand(0);
1832 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1833 if (Align == 0) Align = 1;
1837 switch (Align & 3) {
1838 case 2: // WORD aligned
1840 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1841 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1843 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1844 DAG.getConstant(1, MVT::i8));
1846 case 0: // DWORD aligned
1848 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1849 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1851 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1852 DAG.getConstant(2, MVT::i8));
1854 default: // Byte aligned
1856 Count = Op.getOperand(3);
1861 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1862 InFlag = Chain.getValue(1);
1863 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1864 InFlag = Chain.getValue(1);
1865 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1866 InFlag = Chain.getValue(1);
1868 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1869 DAG.getValueType(AVT), InFlag);
1871 case ISD::GlobalAddress: {
1873 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1874 // For Darwin, external and weak symbols are indirect, so we want to load
1875 // the value at address GV, not the value of GV itself. This means that
1876 // the GlobalAddress must be in the base or index register of the address,
1877 // not the GV offset field.
1878 if (getTargetMachine().
1879 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals()) {
1880 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1881 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
1882 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1883 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1884 DAG.getSrcValue(NULL));
1888 case ISD::VASTART: {
1889 // vastart just stores the address of the VarArgsFrameIndex slot into the
1890 // memory location argument.
1891 // FIXME: Replace MVT::i32 with PointerTy
1892 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1893 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1894 Op.getOperand(1), Op.getOperand(2));
1899 switch(Op.getNumOperands()) {
1901 assert(0 && "Do not know how to return this many arguments!");
1904 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1905 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1907 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1908 if (MVT::isInteger(ArgVT))
1909 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1911 else if (!X86ScalarSSE) {
1912 std::vector<MVT::ValueType> Tys;
1913 Tys.push_back(MVT::Other);
1914 Tys.push_back(MVT::Flag);
1915 std::vector<SDOperand> Ops;
1916 Ops.push_back(Op.getOperand(0));
1917 Ops.push_back(Op.getOperand(1));
1918 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1921 SDOperand Chain = Op.getOperand(0);
1922 SDOperand Value = Op.getOperand(1);
1924 if (Value.getOpcode() == ISD::LOAD &&
1925 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
1926 Chain = Value.getOperand(0);
1927 MemLoc = Value.getOperand(1);
1929 // Spill the value to memory and reload it into top of stack.
1930 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1931 MachineFunction &MF = DAG.getMachineFunction();
1932 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1933 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1934 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1935 Value, MemLoc, DAG.getSrcValue(0));
1937 std::vector<MVT::ValueType> Tys;
1938 Tys.push_back(MVT::f64);
1939 Tys.push_back(MVT::Other);
1940 std::vector<SDOperand> Ops;
1941 Ops.push_back(Chain);
1942 Ops.push_back(MemLoc);
1943 Ops.push_back(DAG.getValueType(ArgVT));
1944 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1946 Tys.push_back(MVT::Other);
1947 Tys.push_back(MVT::Flag);
1949 Ops.push_back(Copy.getValue(1));
1950 Ops.push_back(Copy);
1951 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1956 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1958 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1961 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1962 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1968 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1970 default: return NULL;
1971 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1972 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1973 case X86ISD::ADC: return "X86ISD::ADC";
1974 case X86ISD::SBB: return "X86ISD::SBB";
1975 case X86ISD::SHLD: return "X86ISD::SHLD";
1976 case X86ISD::SHRD: return "X86ISD::SHRD";
1977 case X86ISD::FAND: return "X86ISD::FAND";
1978 case X86ISD::FXOR: return "X86ISD::FXOR";
1979 case X86ISD::FILD: return "X86ISD::FILD";
1980 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
1981 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1982 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1983 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
1984 case X86ISD::FLD: return "X86ISD::FLD";
1985 case X86ISD::FST: return "X86ISD::FST";
1986 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
1987 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
1988 case X86ISD::CALL: return "X86ISD::CALL";
1989 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1990 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1991 case X86ISD::CMP: return "X86ISD::CMP";
1992 case X86ISD::TEST: return "X86ISD::TEST";
1993 case X86ISD::SETCC: return "X86ISD::SETCC";
1994 case X86ISD::CMOV: return "X86ISD::CMOV";
1995 case X86ISD::BRCOND: return "X86ISD::BRCOND";
1996 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
1997 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1998 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
1999 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
2003 bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
2004 uint64_t Mask) const {
2006 unsigned Opc = Op.getOpcode();
2010 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2012 case X86ISD::SETCC: return (Mask & 1) == 0;
2018 std::vector<unsigned> X86TargetLowering::
2019 getRegForInlineAsmConstraint(const std::string &Constraint) const {
2020 if (Constraint.size() == 1) {
2021 // FIXME: not handling fp-stack yet!
2022 // FIXME: not handling MMX registers yet ('y' constraint).
2023 switch (Constraint[0]) { // GCC X86 Constraint Letters
2024 default: break; // Unknown constriant letter
2025 case 'r': // GENERAL_REGS
2026 case 'R': // LEGACY_REGS
2027 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2028 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2029 case 'l': // INDEX_REGS
2030 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2031 X86::ESI, X86::EDI, X86::EBP, 0);
2032 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2034 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2035 case 'x': // SSE_REGS if SSE1 allowed
2036 if (Subtarget->hasSSE1())
2037 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2038 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2040 return std::vector<unsigned>();
2041 case 'Y': // SSE_REGS if SSE2 allowed
2042 if (Subtarget->hasSSE2())
2043 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2044 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2046 return std::vector<unsigned>();
2050 // Handle explicit register names.
2051 return TargetLowering::getRegForInlineAsmConstraint(Constraint);