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 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
255 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
256 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
257 allowUnalignedMemoryAccesses = true; // x86 supports it!
260 std::vector<SDOperand>
261 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
262 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
263 return LowerFastCCArguments(F, DAG);
264 return LowerCCCArguments(F, DAG);
267 std::pair<SDOperand, SDOperand>
268 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
269 bool isVarArg, unsigned CallingConv,
271 SDOperand Callee, ArgListTy &Args,
273 assert((!isVarArg || CallingConv == CallingConv::C) &&
274 "Only C takes varargs!");
276 // If the callee is a GlobalAddress node (quite common, every direct call is)
277 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
278 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
279 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
280 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
281 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
283 if (CallingConv == CallingConv::Fast && EnableFastCC)
284 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
285 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
288 //===----------------------------------------------------------------------===//
289 // C Calling Convention implementation
290 //===----------------------------------------------------------------------===//
292 std::vector<SDOperand>
293 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
294 std::vector<SDOperand> ArgValues;
296 MachineFunction &MF = DAG.getMachineFunction();
297 MachineFrameInfo *MFI = MF.getFrameInfo();
299 // Add DAG nodes to load the arguments... On entry to a function on the X86,
300 // the stack frame looks like this:
302 // [ESP] -- return address
303 // [ESP + 4] -- first argument (leftmost lexically)
304 // [ESP + 8] -- second argument, if first argument is four bytes in size
307 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
308 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
309 MVT::ValueType ObjectVT = getValueType(I->getType());
310 unsigned ArgIncrement = 4;
313 default: assert(0 && "Unhandled argument type!");
315 case MVT::i8: ObjSize = 1; break;
316 case MVT::i16: ObjSize = 2; break;
317 case MVT::i32: ObjSize = 4; break;
318 case MVT::i64: ObjSize = ArgIncrement = 8; break;
319 case MVT::f32: ObjSize = 4; break;
320 case MVT::f64: ObjSize = ArgIncrement = 8; break;
322 // Create the frame index object for this incoming parameter...
323 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
325 // Create the SelectionDAG nodes corresponding to a load from this parameter
326 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
328 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
332 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
333 DAG.getSrcValue(NULL));
335 if (MVT::isInteger(ObjectVT))
336 ArgValue = DAG.getConstant(0, ObjectVT);
338 ArgValue = DAG.getConstantFP(0, ObjectVT);
340 ArgValues.push_back(ArgValue);
342 ArgOffset += ArgIncrement; // Move on to the next argument...
345 // If the function takes variable number of arguments, make a frame index for
346 // the start of the first vararg value... for expansion of llvm.va_start.
348 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
349 ReturnAddrIndex = 0; // No return address slot generated yet.
350 BytesToPopOnReturn = 0; // Callee pops nothing.
351 BytesCallerReserves = ArgOffset;
353 // Finally, inform the code generator which regs we return values in.
354 switch (getValueType(F.getReturnType())) {
355 default: assert(0 && "Unknown type!");
356 case MVT::isVoid: break;
361 MF.addLiveOut(X86::EAX);
364 MF.addLiveOut(X86::EAX);
365 MF.addLiveOut(X86::EDX);
369 MF.addLiveOut(X86::ST0);
375 std::pair<SDOperand, SDOperand>
376 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
377 bool isVarArg, bool isTailCall,
378 SDOperand Callee, ArgListTy &Args,
380 // Count how many bytes are to be pushed on the stack.
381 unsigned NumBytes = 0;
385 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
387 for (unsigned i = 0, e = Args.size(); i != e; ++i)
388 switch (getValueType(Args[i].second)) {
389 default: assert(0 && "Unknown value type!");
403 Chain = DAG.getCALLSEQ_START(Chain,
404 DAG.getConstant(NumBytes, getPointerTy()));
406 // Arguments go on the stack in reverse order, as specified by the ABI.
407 unsigned ArgOffset = 0;
408 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
409 std::vector<SDOperand> Stores;
411 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
412 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
413 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
415 switch (getValueType(Args[i].second)) {
416 default: assert(0 && "Unexpected ValueType for argument!");
420 // Promote the integer to 32 bits. If the input type is signed use a
421 // sign extend, otherwise use a zero extend.
422 if (Args[i].second->isSigned())
423 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
425 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
430 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
431 Args[i].first, PtrOff,
432 DAG.getSrcValue(NULL)));
437 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
438 Args[i].first, PtrOff,
439 DAG.getSrcValue(NULL)));
444 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
447 std::vector<MVT::ValueType> RetVals;
448 MVT::ValueType RetTyVT = getValueType(RetTy);
449 RetVals.push_back(MVT::Other);
451 // The result values produced have to be legal. Promote the result.
453 case MVT::isVoid: break;
455 RetVals.push_back(RetTyVT);
460 RetVals.push_back(MVT::i32);
464 RetVals.push_back(MVT::f32);
466 RetVals.push_back(MVT::f64);
469 RetVals.push_back(MVT::i32);
470 RetVals.push_back(MVT::i32);
475 std::vector<MVT::ValueType> NodeTys;
476 NodeTys.push_back(MVT::Other); // Returns a chain
477 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
478 std::vector<SDOperand> Ops;
479 Ops.push_back(Chain);
480 Ops.push_back(Callee);
482 // FIXME: Do not generate X86ISD::TAILCALL for now.
483 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
484 SDOperand InFlag = Chain.getValue(1);
487 NodeTys.push_back(MVT::Other); // Returns a chain
488 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
490 Ops.push_back(Chain);
491 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
492 Ops.push_back(DAG.getConstant(0, getPointerTy()));
493 Ops.push_back(InFlag);
494 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
495 InFlag = Chain.getValue(1);
498 if (RetTyVT != MVT::isVoid) {
500 default: assert(0 && "Unknown value type to return!");
503 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
504 Chain = RetVal.getValue(1);
505 if (RetTyVT == MVT::i1)
506 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
509 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
510 Chain = RetVal.getValue(1);
513 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
514 Chain = RetVal.getValue(1);
517 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
518 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
520 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
521 Chain = Hi.getValue(1);
526 std::vector<MVT::ValueType> Tys;
527 Tys.push_back(MVT::f64);
528 Tys.push_back(MVT::Other);
529 Tys.push_back(MVT::Flag);
530 std::vector<SDOperand> Ops;
531 Ops.push_back(Chain);
532 Ops.push_back(InFlag);
533 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
534 Chain = RetVal.getValue(1);
535 InFlag = RetVal.getValue(2);
537 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
538 // shouldn't be necessary except that RFP cannot be live across
539 // multiple blocks. When stackifier is fixed, they can be uncoupled.
540 MachineFunction &MF = DAG.getMachineFunction();
541 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
542 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
544 Tys.push_back(MVT::Other);
546 Ops.push_back(Chain);
547 Ops.push_back(RetVal);
548 Ops.push_back(StackSlot);
549 Ops.push_back(DAG.getValueType(RetTyVT));
550 Ops.push_back(InFlag);
551 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
552 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
553 DAG.getSrcValue(NULL));
554 Chain = RetVal.getValue(1);
557 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
558 // FIXME: we would really like to remember that this FP_ROUND
559 // operation is okay to eliminate if we allow excess FP precision.
560 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
566 return std::make_pair(RetVal, Chain);
568 std::vector<SDOperand> Ops;
569 Ops.push_back(Chain);
570 Ops.push_back(Callee);
571 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
572 Ops.push_back(DAG.getConstant(0, getPointerTy()));
574 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
579 case MVT::isVoid: break;
581 ResultVal = TheCall.getValue(1);
586 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
589 // FIXME: we would really like to remember that this FP_ROUND operation is
590 // okay to eliminate if we allow excess FP precision.
591 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
594 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
595 TheCall.getValue(2));
599 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
600 return std::make_pair(ResultVal, Chain);
604 //===----------------------------------------------------------------------===//
605 // Fast Calling Convention implementation
606 //===----------------------------------------------------------------------===//
608 // The X86 'fast' calling convention passes up to two integer arguments in
609 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
610 // and requires that the callee pop its arguments off the stack (allowing proper
611 // tail calls), and has the same return value conventions as C calling convs.
613 // This calling convention always arranges for the callee pop value to be 8n+4
614 // bytes, which is needed for tail recursion elimination and stack alignment
617 // Note that this can be enhanced in the future to pass fp vals in registers
618 // (when we have a global fp allocator) and do other tricks.
621 /// AddLiveIn - This helper function adds the specified physical register to the
622 /// MachineFunction as a live in value. It also creates a corresponding virtual
624 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
625 TargetRegisterClass *RC) {
626 assert(RC->contains(PReg) && "Not the correct regclass!");
627 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
628 MF.addLiveIn(PReg, VReg);
633 std::vector<SDOperand>
634 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
635 std::vector<SDOperand> ArgValues;
637 MachineFunction &MF = DAG.getMachineFunction();
638 MachineFrameInfo *MFI = MF.getFrameInfo();
640 // Add DAG nodes to load the arguments... On entry to a function the stack
641 // frame looks like this:
643 // [ESP] -- return address
644 // [ESP + 4] -- first nonreg argument (leftmost lexically)
645 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
647 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
649 // Keep track of the number of integer regs passed so far. This can be either
650 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
652 unsigned NumIntRegs = 0;
654 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
655 MVT::ValueType ObjectVT = getValueType(I->getType());
656 unsigned ArgIncrement = 4;
657 unsigned ObjSize = 0;
661 default: assert(0 && "Unhandled argument type!");
664 if (NumIntRegs < 2) {
665 if (!I->use_empty()) {
666 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
667 X86::R8RegisterClass);
668 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
669 DAG.setRoot(ArgValue.getValue(1));
670 if (ObjectVT == MVT::i1)
671 // FIXME: Should insert a assertzext here.
672 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
681 if (NumIntRegs < 2) {
682 if (!I->use_empty()) {
683 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
684 X86::R16RegisterClass);
685 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
686 DAG.setRoot(ArgValue.getValue(1));
694 if (NumIntRegs < 2) {
695 if (!I->use_empty()) {
696 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
697 X86::R32RegisterClass);
698 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
699 DAG.setRoot(ArgValue.getValue(1));
707 if (NumIntRegs == 0) {
708 if (!I->use_empty()) {
709 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
710 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
712 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
713 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
714 DAG.setRoot(Hi.getValue(1));
716 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
720 } else if (NumIntRegs == 1) {
721 if (!I->use_empty()) {
722 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
723 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
724 DAG.setRoot(Low.getValue(1));
726 // Load the high part from memory.
727 // Create the frame index object for this incoming parameter...
728 int FI = MFI->CreateFixedObject(4, ArgOffset);
729 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
730 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
731 DAG.getSrcValue(NULL));
732 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
738 ObjSize = ArgIncrement = 8;
740 case MVT::f32: ObjSize = 4; break;
741 case MVT::f64: ObjSize = ArgIncrement = 8; break;
744 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
746 if (ObjSize && !I->use_empty()) {
747 // Create the frame index object for this incoming parameter...
748 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
750 // Create the SelectionDAG nodes corresponding to a load from this
752 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
754 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
755 DAG.getSrcValue(NULL));
756 } else if (ArgValue.Val == 0) {
757 if (MVT::isInteger(ObjectVT))
758 ArgValue = DAG.getConstant(0, ObjectVT);
760 ArgValue = DAG.getConstantFP(0, ObjectVT);
762 ArgValues.push_back(ArgValue);
765 ArgOffset += ArgIncrement; // Move on to the next argument.
768 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
769 // arguments and the arguments after the retaddr has been pushed are aligned.
770 if ((ArgOffset & 7) == 0)
773 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
774 ReturnAddrIndex = 0; // No return address slot generated yet.
775 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
776 BytesCallerReserves = 0;
778 // Finally, inform the code generator which regs we return values in.
779 switch (getValueType(F.getReturnType())) {
780 default: assert(0 && "Unknown type!");
781 case MVT::isVoid: break;
786 MF.addLiveOut(X86::EAX);
789 MF.addLiveOut(X86::EAX);
790 MF.addLiveOut(X86::EDX);
794 MF.addLiveOut(X86::ST0);
800 std::pair<SDOperand, SDOperand>
801 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
802 bool isTailCall, SDOperand Callee,
803 ArgListTy &Args, SelectionDAG &DAG) {
804 // Count how many bytes are to be pushed on the stack.
805 unsigned NumBytes = 0;
807 // Keep track of the number of integer regs passed so far. This can be either
808 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
810 unsigned NumIntRegs = 0;
812 for (unsigned i = 0, e = Args.size(); i != e; ++i)
813 switch (getValueType(Args[i].second)) {
814 default: assert(0 && "Unknown value type!");
819 if (NumIntRegs < 2) {
828 if (NumIntRegs == 0) {
831 } else if (NumIntRegs == 1) {
843 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
844 // arguments and the arguments after the retaddr has been pushed are aligned.
845 if ((NumBytes & 7) == 0)
848 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
850 // Arguments go on the stack in reverse order, as specified by the ABI.
851 unsigned ArgOffset = 0;
852 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
854 std::vector<SDOperand> Stores;
855 std::vector<SDOperand> RegValuesToPass;
856 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
857 switch (getValueType(Args[i].second)) {
858 default: assert(0 && "Unexpected ValueType for argument!");
860 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
865 if (NumIntRegs < 2) {
866 RegValuesToPass.push_back(Args[i].first);
872 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
873 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
874 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
875 Args[i].first, PtrOff,
876 DAG.getSrcValue(NULL)));
881 if (NumIntRegs < 2) { // Can pass part of it in regs?
882 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
883 Args[i].first, DAG.getConstant(1, MVT::i32));
884 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
885 Args[i].first, DAG.getConstant(0, MVT::i32));
886 RegValuesToPass.push_back(Lo);
888 if (NumIntRegs < 2) { // Pass both parts in regs?
889 RegValuesToPass.push_back(Hi);
892 // Pass the high part in memory.
893 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
894 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
895 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
896 Hi, PtrOff, DAG.getSrcValue(NULL)));
903 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
904 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
905 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
906 Args[i].first, PtrOff,
907 DAG.getSrcValue(NULL)));
913 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
915 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
916 // arguments and the arguments after the retaddr has been pushed are aligned.
917 if ((ArgOffset & 7) == 0)
920 std::vector<MVT::ValueType> RetVals;
921 MVT::ValueType RetTyVT = getValueType(RetTy);
923 RetVals.push_back(MVT::Other);
925 // The result values produced have to be legal. Promote the result.
927 case MVT::isVoid: break;
929 RetVals.push_back(RetTyVT);
934 RetVals.push_back(MVT::i32);
938 RetVals.push_back(MVT::f32);
940 RetVals.push_back(MVT::f64);
943 RetVals.push_back(MVT::i32);
944 RetVals.push_back(MVT::i32);
949 // Build a sequence of copy-to-reg nodes chained together with token chain
950 // and flag operands which copy the outgoing args into registers.
952 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
954 SDOperand RegToPass = RegValuesToPass[i];
955 switch (RegToPass.getValueType()) {
956 default: assert(0 && "Bad thing to pass in regs");
958 CCReg = (i == 0) ? X86::AL : X86::DL;
961 CCReg = (i == 0) ? X86::AX : X86::DX;
964 CCReg = (i == 0) ? X86::EAX : X86::EDX;
968 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
969 InFlag = Chain.getValue(1);
972 std::vector<MVT::ValueType> NodeTys;
973 NodeTys.push_back(MVT::Other); // Returns a chain
974 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
975 std::vector<SDOperand> Ops;
976 Ops.push_back(Chain);
977 Ops.push_back(Callee);
979 Ops.push_back(InFlag);
981 // FIXME: Do not generate X86ISD::TAILCALL for now.
982 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
983 InFlag = Chain.getValue(1);
986 NodeTys.push_back(MVT::Other); // Returns a chain
987 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
989 Ops.push_back(Chain);
990 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
991 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
992 Ops.push_back(InFlag);
993 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
994 InFlag = Chain.getValue(1);
997 if (RetTyVT != MVT::isVoid) {
999 default: assert(0 && "Unknown value type to return!");
1002 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1003 Chain = RetVal.getValue(1);
1004 if (RetTyVT == MVT::i1)
1005 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1008 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1009 Chain = RetVal.getValue(1);
1012 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1013 Chain = RetVal.getValue(1);
1016 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1017 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1019 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1020 Chain = Hi.getValue(1);
1025 std::vector<MVT::ValueType> Tys;
1026 Tys.push_back(MVT::f64);
1027 Tys.push_back(MVT::Other);
1028 Tys.push_back(MVT::Flag);
1029 std::vector<SDOperand> Ops;
1030 Ops.push_back(Chain);
1031 Ops.push_back(InFlag);
1032 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1033 Chain = RetVal.getValue(1);
1034 InFlag = RetVal.getValue(2);
1036 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1037 // shouldn't be necessary except that RFP cannot be live across
1038 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1039 MachineFunction &MF = DAG.getMachineFunction();
1040 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1041 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1043 Tys.push_back(MVT::Other);
1045 Ops.push_back(Chain);
1046 Ops.push_back(RetVal);
1047 Ops.push_back(StackSlot);
1048 Ops.push_back(DAG.getValueType(RetTyVT));
1049 Ops.push_back(InFlag);
1050 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1051 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1052 DAG.getSrcValue(NULL));
1053 Chain = RetVal.getValue(1);
1056 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1057 // FIXME: we would really like to remember that this FP_ROUND
1058 // operation is okay to eliminate if we allow excess FP precision.
1059 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1065 return std::make_pair(RetVal, Chain);
1067 std::vector<SDOperand> Ops;
1068 Ops.push_back(Chain);
1069 Ops.push_back(Callee);
1070 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1071 // Callee pops all arg values on the stack.
1072 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1074 // Pass register arguments as needed.
1075 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1077 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
1079 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1081 SDOperand ResultVal;
1083 case MVT::isVoid: break;
1085 ResultVal = TheCall.getValue(1);
1090 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1093 // FIXME: we would really like to remember that this FP_ROUND operation is
1094 // okay to eliminate if we allow excess FP precision.
1095 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1098 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1099 TheCall.getValue(2));
1103 return std::make_pair(ResultVal, Chain);
1107 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1108 if (ReturnAddrIndex == 0) {
1109 // Set up a frame object for the return address.
1110 MachineFunction &MF = DAG.getMachineFunction();
1111 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1114 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1119 std::pair<SDOperand, SDOperand> X86TargetLowering::
1120 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1121 SelectionDAG &DAG) {
1123 if (Depth) // Depths > 0 not supported yet!
1124 Result = DAG.getConstant(0, getPointerTy());
1126 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1127 if (!isFrameAddress)
1128 // Just load the return address
1129 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1130 DAG.getSrcValue(NULL));
1132 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1133 DAG.getConstant(4, MVT::i32));
1135 return std::make_pair(Result, Chain);
1138 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1139 /// which corresponds to the condition code.
1140 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1142 default: assert(0 && "Unknown X86 conditional code!");
1143 case X86ISD::COND_A: return X86::JA;
1144 case X86ISD::COND_AE: return X86::JAE;
1145 case X86ISD::COND_B: return X86::JB;
1146 case X86ISD::COND_BE: return X86::JBE;
1147 case X86ISD::COND_E: return X86::JE;
1148 case X86ISD::COND_G: return X86::JG;
1149 case X86ISD::COND_GE: return X86::JGE;
1150 case X86ISD::COND_L: return X86::JL;
1151 case X86ISD::COND_LE: return X86::JLE;
1152 case X86ISD::COND_NE: return X86::JNE;
1153 case X86ISD::COND_NO: return X86::JNO;
1154 case X86ISD::COND_NP: return X86::JNP;
1155 case X86ISD::COND_NS: return X86::JNS;
1156 case X86ISD::COND_O: return X86::JO;
1157 case X86ISD::COND_P: return X86::JP;
1158 case X86ISD::COND_S: return X86::JS;
1162 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1163 /// specific condition code. It returns a false if it cannot do a direct
1164 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1165 /// the order of comparison operands should be flipped.
1166 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1168 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1170 X86CC = X86ISD::COND_INVALID;
1172 switch (SetCCOpcode) {
1174 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1175 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1176 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1177 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1178 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1179 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1180 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1181 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1182 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1183 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1186 // On a floating point condition, the flags are set as follows:
1188 // 0 | 0 | 0 | X > Y
1189 // 0 | 0 | 1 | X < Y
1190 // 1 | 0 | 0 | X == Y
1191 // 1 | 1 | 1 | unordered
1192 switch (SetCCOpcode) {
1195 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1196 case ISD::SETOLE: Flip = true; // Fallthrough
1198 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1199 case ISD::SETOLT: Flip = true; // Fallthrough
1201 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1202 case ISD::SETUGE: Flip = true; // Fallthrough
1204 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1205 case ISD::SETUGT: Flip = true; // Fallthrough
1207 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1209 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1210 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1211 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1215 return X86CC != X86ISD::COND_INVALID;
1218 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1219 /// code. Current x86 isa includes the following FP cmov instructions:
1220 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1221 static bool hasFPCMov(unsigned X86CC) {
1225 case X86ISD::COND_B:
1226 case X86ISD::COND_BE:
1227 case X86ISD::COND_E:
1228 case X86ISD::COND_P:
1229 case X86ISD::COND_A:
1230 case X86ISD::COND_AE:
1231 case X86ISD::COND_NE:
1232 case X86ISD::COND_NP:
1238 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1239 MachineBasicBlock *BB) {
1240 switch (MI->getOpcode()) {
1241 default: assert(false && "Unexpected instr type to insert");
1242 case X86::CMOV_FR32:
1243 case X86::CMOV_FR64: {
1244 // To "insert" a SELECT_CC instruction, we actually have to insert the
1245 // diamond control-flow pattern. The incoming instruction knows the
1246 // destination vreg to set, the condition code register to branch on, the
1247 // true/false values to select between, and a branch opcode to use.
1248 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1249 ilist<MachineBasicBlock>::iterator It = BB;
1255 // cmpTY ccX, r1, r2
1257 // fallthrough --> copy0MBB
1258 MachineBasicBlock *thisMBB = BB;
1259 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1260 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1261 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1262 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1263 MachineFunction *F = BB->getParent();
1264 F->getBasicBlockList().insert(It, copy0MBB);
1265 F->getBasicBlockList().insert(It, sinkMBB);
1266 // Update machine-CFG edges
1267 BB->addSuccessor(copy0MBB);
1268 BB->addSuccessor(sinkMBB);
1271 // %FalseValue = ...
1272 // # fallthrough to sinkMBB
1275 // Update machine-CFG edges
1276 BB->addSuccessor(sinkMBB);
1279 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1282 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1283 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1284 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1286 delete MI; // The pseudo instruction is gone now.
1290 case X86::FP_TO_INT16_IN_MEM:
1291 case X86::FP_TO_INT32_IN_MEM:
1292 case X86::FP_TO_INT64_IN_MEM: {
1293 // Change the floating point control register to use "round towards zero"
1294 // mode when truncating to an integer value.
1295 MachineFunction *F = BB->getParent();
1296 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1297 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1299 // Load the old value of the high byte of the control word...
1301 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1302 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1304 // Set the high part to be round to zero...
1305 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1307 // Reload the modified control word now...
1308 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1310 // Restore the memory image of control word to original value
1311 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1313 // Get the X86 opcode to use.
1315 switch (MI->getOpcode()) {
1316 default: assert(0 && "illegal opcode!");
1317 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1318 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1319 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1323 MachineOperand &Op = MI->getOperand(0);
1324 if (Op.isRegister()) {
1325 AM.BaseType = X86AddressMode::RegBase;
1326 AM.Base.Reg = Op.getReg();
1328 AM.BaseType = X86AddressMode::FrameIndexBase;
1329 AM.Base.FrameIndex = Op.getFrameIndex();
1331 Op = MI->getOperand(1);
1332 if (Op.isImmediate())
1333 AM.Scale = Op.getImmedValue();
1334 Op = MI->getOperand(2);
1335 if (Op.isImmediate())
1336 AM.IndexReg = Op.getImmedValue();
1337 Op = MI->getOperand(3);
1338 if (Op.isGlobalAddress()) {
1339 AM.GV = Op.getGlobal();
1341 AM.Disp = Op.getImmedValue();
1343 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1345 // Reload the original control word now.
1346 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1348 delete MI; // The pseudo instruction is gone now.
1355 //===----------------------------------------------------------------------===//
1356 // X86 Custom Lowering Hooks
1357 //===----------------------------------------------------------------------===//
1359 /// LowerOperation - Provide custom lowering hooks for some operations.
1361 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1362 switch (Op.getOpcode()) {
1363 default: assert(0 && "Should not custom lower this!");
1364 case ISD::ADD_PARTS:
1365 case ISD::SUB_PARTS: {
1366 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1367 "Not an i64 add/sub!");
1368 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1369 std::vector<MVT::ValueType> Tys;
1370 Tys.push_back(MVT::i32);
1371 Tys.push_back(MVT::Flag);
1372 std::vector<SDOperand> Ops;
1373 Ops.push_back(Op.getOperand(0));
1374 Ops.push_back(Op.getOperand(2));
1375 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1377 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1378 Op.getOperand(1), Op.getOperand(3),
1381 Tys.push_back(MVT::i32);
1382 Tys.push_back(MVT::i32);
1386 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1388 case ISD::SHL_PARTS:
1389 case ISD::SRA_PARTS:
1390 case ISD::SRL_PARTS: {
1391 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1392 "Not an i64 shift!");
1393 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1394 SDOperand ShOpLo = Op.getOperand(0);
1395 SDOperand ShOpHi = Op.getOperand(1);
1396 SDOperand ShAmt = Op.getOperand(2);
1397 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1398 DAG.getConstant(31, MVT::i8))
1399 : DAG.getConstant(0, MVT::i32);
1401 SDOperand Tmp2, Tmp3;
1402 if (Op.getOpcode() == ISD::SHL_PARTS) {
1403 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1404 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1406 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1407 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1410 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1411 ShAmt, DAG.getConstant(32, MVT::i8));
1414 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1416 std::vector<MVT::ValueType> Tys;
1417 Tys.push_back(MVT::i32);
1418 Tys.push_back(MVT::Flag);
1419 std::vector<SDOperand> Ops;
1420 if (Op.getOpcode() == ISD::SHL_PARTS) {
1421 Ops.push_back(Tmp2);
1422 Ops.push_back(Tmp3);
1424 Ops.push_back(InFlag);
1425 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1426 InFlag = Hi.getValue(1);
1429 Ops.push_back(Tmp3);
1430 Ops.push_back(Tmp1);
1432 Ops.push_back(InFlag);
1433 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1435 Ops.push_back(Tmp2);
1436 Ops.push_back(Tmp3);
1438 Ops.push_back(InFlag);
1439 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1440 InFlag = Lo.getValue(1);
1443 Ops.push_back(Tmp3);
1444 Ops.push_back(Tmp1);
1446 Ops.push_back(InFlag);
1447 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1451 Tys.push_back(MVT::i32);
1452 Tys.push_back(MVT::i32);
1456 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1458 case ISD::SINT_TO_FP: {
1459 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
1460 Op.getOperand(0).getValueType() >= MVT::i16 &&
1461 "Unknown SINT_TO_FP to lower!");
1464 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1465 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1466 MachineFunction &MF = DAG.getMachineFunction();
1467 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1468 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1469 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1470 DAG.getEntryNode(), Op.getOperand(0),
1471 StackSlot, DAG.getSrcValue(NULL));
1474 std::vector<MVT::ValueType> Tys;
1475 Tys.push_back(MVT::f64);
1476 Tys.push_back(MVT::Other);
1477 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
1478 std::vector<SDOperand> Ops;
1479 Ops.push_back(Chain);
1480 Ops.push_back(StackSlot);
1481 Ops.push_back(DAG.getValueType(SrcVT));
1482 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1486 Chain = Result.getValue(1);
1487 SDOperand InFlag = Result.getValue(2);
1489 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
1490 // shouldn't be necessary except that RFP cannot be live across
1491 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1492 MachineFunction &MF = DAG.getMachineFunction();
1493 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1494 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1495 std::vector<MVT::ValueType> Tys;
1496 Tys.push_back(MVT::Other);
1497 std::vector<SDOperand> Ops;
1498 Ops.push_back(Chain);
1499 Ops.push_back(Result);
1500 Ops.push_back(StackSlot);
1501 Ops.push_back(DAG.getValueType(Op.getValueType()));
1502 Ops.push_back(InFlag);
1503 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1504 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1505 DAG.getSrcValue(NULL));
1510 case ISD::FP_TO_SINT: {
1511 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1512 "Unknown FP_TO_SINT to lower!");
1513 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1515 MachineFunction &MF = DAG.getMachineFunction();
1516 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1517 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1518 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1521 switch (Op.getValueType()) {
1522 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1523 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1524 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1525 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1528 SDOperand Chain = DAG.getEntryNode();
1529 SDOperand Value = Op.getOperand(0);
1531 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1532 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1533 DAG.getSrcValue(0));
1534 std::vector<MVT::ValueType> Tys;
1535 Tys.push_back(MVT::f64);
1536 Tys.push_back(MVT::Other);
1537 std::vector<SDOperand> Ops;
1538 Ops.push_back(Chain);
1539 Ops.push_back(StackSlot);
1540 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
1541 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1542 Chain = Value.getValue(1);
1543 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1544 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1547 // Build the FP_TO_INT*_IN_MEM
1548 std::vector<SDOperand> Ops;
1549 Ops.push_back(Chain);
1550 Ops.push_back(Value);
1551 Ops.push_back(StackSlot);
1552 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1555 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1556 DAG.getSrcValue(NULL));
1558 case ISD::READCYCLECOUNTER: {
1559 std::vector<MVT::ValueType> Tys;
1560 Tys.push_back(MVT::Other);
1561 Tys.push_back(MVT::Flag);
1562 std::vector<SDOperand> Ops;
1563 Ops.push_back(Op.getOperand(0));
1564 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1566 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1567 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1568 MVT::i32, Ops[0].getValue(2)));
1569 Ops.push_back(Ops[1].getValue(1));
1570 Tys[0] = Tys[1] = MVT::i32;
1571 Tys.push_back(MVT::Other);
1572 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1575 MVT::ValueType VT = Op.getValueType();
1576 const Type *OpNTy = MVT::getTypeForValueType(VT);
1577 std::vector<Constant*> CV;
1578 if (VT == MVT::f64) {
1579 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1580 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1582 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1583 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1584 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1585 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 Constant *CS = ConstantStruct::get(CV);
1588 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1590 = DAG.getNode(X86ISD::LOAD_PACK,
1591 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1592 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1595 MVT::ValueType VT = Op.getValueType();
1596 const Type *OpNTy = MVT::getTypeForValueType(VT);
1597 std::vector<Constant*> CV;
1598 if (VT == MVT::f64) {
1599 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1600 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1602 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1603 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1604 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1605 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1607 Constant *CS = ConstantStruct::get(CV);
1608 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1610 = DAG.getNode(X86ISD::LOAD_PACK,
1611 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1612 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1615 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1617 SDOperand CC = Op.getOperand(2);
1618 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1619 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1622 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1624 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1625 Op.getOperand(1), Op.getOperand(0));
1627 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1628 Op.getOperand(0), Op.getOperand(1));
1629 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1630 DAG.getConstant(X86CC, MVT::i8), Cond);
1632 assert(isFP && "Illegal integer SetCC!");
1634 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1635 Op.getOperand(0), Op.getOperand(1));
1636 std::vector<MVT::ValueType> Tys;
1637 std::vector<SDOperand> Ops;
1638 switch (SetCCOpcode) {
1639 default: assert(false && "Illegal floating point SetCC!");
1640 case ISD::SETOEQ: { // !PF & ZF
1641 Tys.push_back(MVT::i8);
1642 Tys.push_back(MVT::Flag);
1643 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1644 Ops.push_back(Cond);
1645 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1646 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1647 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1649 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1651 case ISD::SETUNE: { // PF | !ZF
1652 Tys.push_back(MVT::i8);
1653 Tys.push_back(MVT::Flag);
1654 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1655 Ops.push_back(Cond);
1656 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1657 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1658 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1660 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1666 MVT::ValueType VT = Op.getValueType();
1667 bool isFP = MVT::isFloatingPoint(VT);
1668 bool isFPStack = isFP && !X86ScalarSSE;
1669 bool isFPSSE = isFP && X86ScalarSSE;
1670 bool addTest = false;
1671 SDOperand Op0 = Op.getOperand(0);
1673 if (Op0.getOpcode() == ISD::SETCC)
1674 Op0 = LowerOperation(Op0, DAG);
1676 if (Op0.getOpcode() == X86ISD::SETCC) {
1677 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1678 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1679 // have another use it will be eliminated.
1680 // If the X86ISD::SETCC has more than one use, then it's probably better
1681 // to use a test instead of duplicating the X86ISD::CMP (for register
1682 // pressure reason).
1683 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1684 if (!Op0.hasOneUse()) {
1685 std::vector<MVT::ValueType> Tys;
1686 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1687 Tys.push_back(Op0.Val->getValueType(i));
1688 std::vector<SDOperand> Ops;
1689 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1690 Ops.push_back(Op0.getOperand(i));
1691 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1694 CC = Op0.getOperand(0);
1695 Cond = Op0.getOperand(1);
1696 // Make a copy as flag result cannot be used by more than one.
1697 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1698 Cond.getOperand(0), Cond.getOperand(1));
1700 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
1707 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1708 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
1711 std::vector<MVT::ValueType> Tys;
1712 Tys.push_back(Op.getValueType());
1713 Tys.push_back(MVT::Flag);
1714 std::vector<SDOperand> Ops;
1715 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1716 // condition is true.
1717 Ops.push_back(Op.getOperand(2));
1718 Ops.push_back(Op.getOperand(1));
1720 Ops.push_back(Cond);
1721 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
1724 bool addTest = false;
1725 SDOperand Cond = Op.getOperand(1);
1726 SDOperand Dest = Op.getOperand(2);
1728 if (Cond.getOpcode() == ISD::SETCC)
1729 Cond = LowerOperation(Cond, DAG);
1731 if (Cond.getOpcode() == X86ISD::SETCC) {
1732 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1733 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1734 // have another use it will be eliminated.
1735 // If the X86ISD::SETCC has more than one use, then it's probably better
1736 // to use a test instead of duplicating the X86ISD::CMP (for register
1737 // pressure reason).
1738 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1739 if (!Cond.hasOneUse()) {
1740 std::vector<MVT::ValueType> Tys;
1741 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1742 Tys.push_back(Cond.Val->getValueType(i));
1743 std::vector<SDOperand> Ops;
1744 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1745 Ops.push_back(Cond.getOperand(i));
1746 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1749 CC = Cond.getOperand(0);
1750 Cond = Cond.getOperand(1);
1751 // Make a copy as flag result cannot be used by more than one.
1752 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1753 Cond.getOperand(0), Cond.getOperand(1));
1760 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1761 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1763 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1764 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1768 SDOperand Chain = Op.getOperand(0);
1770 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1771 if (Align == 0) Align = 1;
1775 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1777 unsigned Val = ValC->getValue() & 255;
1779 // If the value is a constant, then we can potentially use larger sets.
1780 switch (Align & 3) {
1781 case 2: // WORD aligned
1783 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1784 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1786 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1787 DAG.getConstant(1, MVT::i8));
1788 Val = (Val << 8) | Val;
1791 case 0: // DWORD aligned
1793 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1794 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1796 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1797 DAG.getConstant(2, MVT::i8));
1798 Val = (Val << 8) | Val;
1799 Val = (Val << 16) | Val;
1802 default: // Byte aligned
1804 Count = Op.getOperand(3);
1809 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1811 InFlag = Chain.getValue(1);
1814 Count = Op.getOperand(3);
1815 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1816 InFlag = Chain.getValue(1);
1819 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1820 InFlag = Chain.getValue(1);
1821 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1822 InFlag = Chain.getValue(1);
1824 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1825 DAG.getValueType(AVT), InFlag);
1828 SDOperand Chain = Op.getOperand(0);
1830 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1831 if (Align == 0) Align = 1;
1835 switch (Align & 3) {
1836 case 2: // WORD aligned
1838 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1839 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1841 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1842 DAG.getConstant(1, MVT::i8));
1844 case 0: // DWORD aligned
1846 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1847 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1849 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1850 DAG.getConstant(2, MVT::i8));
1852 default: // Byte aligned
1854 Count = Op.getOperand(3);
1859 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1860 InFlag = Chain.getValue(1);
1861 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1862 InFlag = Chain.getValue(1);
1863 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1864 InFlag = Chain.getValue(1);
1866 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1867 DAG.getValueType(AVT), InFlag);
1869 case ISD::GlobalAddress: {
1871 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1872 // For Darwin, external and weak symbols are indirect, so we want to load
1873 // the value at address GV, not the value of GV itself. This means that
1874 // the GlobalAddress must be in the base or index register of the address,
1875 // not the GV offset field.
1876 if (getTargetMachine().
1877 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals()) {
1878 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1879 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
1880 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1881 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1882 DAG.getSrcValue(NULL));
1886 case ISD::VASTART: {
1887 // vastart just stores the address of the VarArgsFrameIndex slot into the
1888 // memory location argument.
1889 // FIXME: Replace MVT::i32 with PointerTy
1890 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1891 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1892 Op.getOperand(1), Op.getOperand(2));
1897 switch(Op.getNumOperands()) {
1899 assert(0 && "Do not know how to return this many arguments!");
1902 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1903 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1905 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1906 if (MVT::isInteger(ArgVT))
1907 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1909 else if (!X86ScalarSSE) {
1910 std::vector<MVT::ValueType> Tys;
1911 Tys.push_back(MVT::Other);
1912 Tys.push_back(MVT::Flag);
1913 std::vector<SDOperand> Ops;
1914 Ops.push_back(Op.getOperand(0));
1915 Ops.push_back(Op.getOperand(1));
1916 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1919 SDOperand Chain = Op.getOperand(0);
1920 SDOperand Value = Op.getOperand(1);
1922 if (Value.getOpcode() == ISD::LOAD &&
1923 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
1924 Chain = Value.getOperand(0);
1925 MemLoc = Value.getOperand(1);
1927 // Spill the value to memory and reload it into top of stack.
1928 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1929 MachineFunction &MF = DAG.getMachineFunction();
1930 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1931 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1932 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1933 Value, MemLoc, DAG.getSrcValue(0));
1935 std::vector<MVT::ValueType> Tys;
1936 Tys.push_back(MVT::f64);
1937 Tys.push_back(MVT::Other);
1938 std::vector<SDOperand> Ops;
1939 Ops.push_back(Chain);
1940 Ops.push_back(MemLoc);
1941 Ops.push_back(DAG.getValueType(ArgVT));
1942 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1944 Tys.push_back(MVT::Other);
1945 Tys.push_back(MVT::Flag);
1947 Ops.push_back(Copy.getValue(1));
1948 Ops.push_back(Copy);
1949 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1954 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1956 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1959 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1960 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1966 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1968 default: return NULL;
1969 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1970 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1971 case X86ISD::ADC: return "X86ISD::ADC";
1972 case X86ISD::SBB: return "X86ISD::SBB";
1973 case X86ISD::SHLD: return "X86ISD::SHLD";
1974 case X86ISD::SHRD: return "X86ISD::SHRD";
1975 case X86ISD::FAND: return "X86ISD::FAND";
1976 case X86ISD::FXOR: return "X86ISD::FXOR";
1977 case X86ISD::FILD: return "X86ISD::FILD";
1978 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
1979 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1980 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1981 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
1982 case X86ISD::FLD: return "X86ISD::FLD";
1983 case X86ISD::FST: return "X86ISD::FST";
1984 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
1985 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
1986 case X86ISD::CALL: return "X86ISD::CALL";
1987 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1988 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1989 case X86ISD::CMP: return "X86ISD::CMP";
1990 case X86ISD::TEST: return "X86ISD::TEST";
1991 case X86ISD::SETCC: return "X86ISD::SETCC";
1992 case X86ISD::CMOV: return "X86ISD::CMOV";
1993 case X86ISD::BRCOND: return "X86ISD::BRCOND";
1994 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
1995 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1996 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
1997 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
2001 bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
2002 uint64_t Mask) const {
2004 unsigned Opc = Op.getOpcode();
2008 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2010 case X86ISD::SETCC: return (Mask & 1) == 0;
2016 std::vector<unsigned> X86TargetLowering::
2017 getRegForInlineAsmConstraint(const std::string &Constraint) const {
2018 if (Constraint.size() == 1) {
2019 // FIXME: not handling fp-stack yet!
2020 // FIXME: not handling MMX registers yet ('y' constraint).
2021 switch (Constraint[0]) { // GCC X86 Constraint Letters
2022 default: break; // Unknown constriant letter
2023 case 'r': // GENERAL_REGS
2024 case 'R': // LEGACY_REGS
2025 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2026 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2027 case 'l': // INDEX_REGS
2028 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2029 X86::ESI, X86::EDI, X86::EBP, 0);
2030 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2032 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2033 case 'x': // SSE_REGS if SSE1 allowed
2034 if (Subtarget->hasSSE1())
2035 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2036 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2038 return std::vector<unsigned>();
2039 case 'Y': // SSE_REGS if SSE2 allowed
2040 if (Subtarget->hasSSE2())
2041 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2042 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2044 return std::vector<unsigned>();
2048 // Handle explicit register names.
2049 return TargetLowering::getRegForInlineAsmConstraint(Constraint);