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);
73 // SSE has no i16 to fp conversion, only i32
75 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
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);
104 if (X86ScalarSSE && !Subtarget->hasSSE3())
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 // With SSE3 we can use fisttpll to convert to a signed i64.
111 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
113 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
114 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
117 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
118 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
119 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
120 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
121 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
125 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
126 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
127 setOperationAction(ISD::FREM , MVT::f64 , Expand);
128 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
129 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
130 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
132 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
133 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
136 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
137 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
138 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
140 // These should be promoted to a larger select which is supported.
141 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
142 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
144 // X86 wants to expand cmov itself.
145 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
146 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
147 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
148 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
149 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
150 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
151 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
152 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
153 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
154 // X86 ret instruction may pop stack.
155 setOperationAction(ISD::RET , MVT::Other, Custom);
157 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
158 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
159 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
160 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
161 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
162 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
163 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
164 // X86 wants to expand memset / memcpy itself.
165 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
166 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
168 // We don't have line number support yet.
169 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
170 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
171 // FIXME - use subtarget debug flags
172 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
173 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
175 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
176 setOperationAction(ISD::VASTART , MVT::Other, Custom);
178 // Use the default implementation.
179 setOperationAction(ISD::VAARG , MVT::Other, Expand);
180 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
181 setOperationAction(ISD::VAEND , MVT::Other, Expand);
182 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
183 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
184 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
186 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
187 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
190 // Set up the FP register classes.
191 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
192 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
194 // SSE has no load+extend ops
195 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
196 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
198 // Use ANDPD to simulate FABS.
199 setOperationAction(ISD::FABS , MVT::f64, Custom);
200 setOperationAction(ISD::FABS , MVT::f32, Custom);
202 // Use XORP to simulate FNEG.
203 setOperationAction(ISD::FNEG , MVT::f64, Custom);
204 setOperationAction(ISD::FNEG , MVT::f32, Custom);
206 // We don't support sin/cos/fmod
207 setOperationAction(ISD::FSIN , MVT::f64, Expand);
208 setOperationAction(ISD::FCOS , MVT::f64, Expand);
209 setOperationAction(ISD::FREM , MVT::f64, Expand);
210 setOperationAction(ISD::FSIN , MVT::f32, Expand);
211 setOperationAction(ISD::FCOS , MVT::f32, Expand);
212 setOperationAction(ISD::FREM , MVT::f32, Expand);
214 // Expand FP immediates into loads from the stack, except for the special
216 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
217 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
218 addLegalFPImmediate(+0.0); // xorps / xorpd
220 // Set up the FP register classes.
221 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
223 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
226 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
227 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
231 addLegalFPImmediate(+0.0); // FLD0
232 addLegalFPImmediate(+1.0); // FLD1
233 addLegalFPImmediate(-0.0); // FLD0/FCHS
234 addLegalFPImmediate(-1.0); // FLD1/FCHS
237 // First set operation action for all vector types to expand. Then we
238 // will selectively turn on ones that can be effectively codegen'd.
239 for (unsigned VT = (unsigned)MVT::Vector + 1;
240 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
241 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
242 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
243 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
244 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
247 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
248 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
249 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
250 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
252 // FIXME: add MMX packed arithmetics
253 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
254 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
255 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
258 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
259 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
261 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
262 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
263 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
264 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
265 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
268 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
269 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
270 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
271 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
272 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
273 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
276 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
277 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
278 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
279 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
280 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
281 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
282 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
283 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
284 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
287 computeRegisterProperties();
289 // FIXME: These should be based on subtarget info. Plus, the values should
290 // be smaller when we are in optimizing for size mode.
291 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
292 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
293 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
294 allowUnalignedMemoryAccesses = true; // x86 supports it!
297 std::vector<SDOperand>
298 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
299 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
300 return LowerFastCCArguments(F, DAG);
301 return LowerCCCArguments(F, DAG);
304 std::pair<SDOperand, SDOperand>
305 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
306 bool isVarArg, unsigned CallingConv,
308 SDOperand Callee, ArgListTy &Args,
310 assert((!isVarArg || CallingConv == CallingConv::C) &&
311 "Only C takes varargs!");
313 // If the callee is a GlobalAddress node (quite common, every direct call is)
314 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
315 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
316 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
317 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
318 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
320 if (CallingConv == CallingConv::Fast && EnableFastCC)
321 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
322 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
325 //===----------------------------------------------------------------------===//
326 // C Calling Convention implementation
327 //===----------------------------------------------------------------------===//
329 std::vector<SDOperand>
330 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
331 std::vector<SDOperand> ArgValues;
333 MachineFunction &MF = DAG.getMachineFunction();
334 MachineFrameInfo *MFI = MF.getFrameInfo();
336 // Add DAG nodes to load the arguments... On entry to a function on the X86,
337 // the stack frame looks like this:
339 // [ESP] -- return address
340 // [ESP + 4] -- first argument (leftmost lexically)
341 // [ESP + 8] -- second argument, if first argument is four bytes in size
344 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
345 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
346 MVT::ValueType ObjectVT = getValueType(I->getType());
347 unsigned ArgIncrement = 4;
350 default: assert(0 && "Unhandled argument type!");
352 case MVT::i8: ObjSize = 1; break;
353 case MVT::i16: ObjSize = 2; break;
354 case MVT::i32: ObjSize = 4; break;
355 case MVT::i64: ObjSize = ArgIncrement = 8; break;
356 case MVT::f32: ObjSize = 4; break;
357 case MVT::f64: ObjSize = ArgIncrement = 8; break;
359 // Create the frame index object for this incoming parameter...
360 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
362 // Create the SelectionDAG nodes corresponding to a load from this parameter
363 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
365 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
369 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
370 DAG.getSrcValue(NULL));
372 if (MVT::isInteger(ObjectVT))
373 ArgValue = DAG.getConstant(0, ObjectVT);
375 ArgValue = DAG.getConstantFP(0, ObjectVT);
377 ArgValues.push_back(ArgValue);
379 ArgOffset += ArgIncrement; // Move on to the next argument...
382 // If the function takes variable number of arguments, make a frame index for
383 // the start of the first vararg value... for expansion of llvm.va_start.
385 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
386 ReturnAddrIndex = 0; // No return address slot generated yet.
387 BytesToPopOnReturn = 0; // Callee pops nothing.
388 BytesCallerReserves = ArgOffset;
390 // Finally, inform the code generator which regs we return values in.
391 switch (getValueType(F.getReturnType())) {
392 default: assert(0 && "Unknown type!");
393 case MVT::isVoid: break;
398 MF.addLiveOut(X86::EAX);
401 MF.addLiveOut(X86::EAX);
402 MF.addLiveOut(X86::EDX);
406 MF.addLiveOut(X86::ST0);
412 std::pair<SDOperand, SDOperand>
413 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
414 bool isVarArg, bool isTailCall,
415 SDOperand Callee, ArgListTy &Args,
417 // Count how many bytes are to be pushed on the stack.
418 unsigned NumBytes = 0;
422 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
424 for (unsigned i = 0, e = Args.size(); i != e; ++i)
425 switch (getValueType(Args[i].second)) {
426 default: assert(0 && "Unknown value type!");
440 Chain = DAG.getCALLSEQ_START(Chain,
441 DAG.getConstant(NumBytes, getPointerTy()));
443 // Arguments go on the stack in reverse order, as specified by the ABI.
444 unsigned ArgOffset = 0;
445 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
446 std::vector<SDOperand> Stores;
448 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
449 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
450 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
452 switch (getValueType(Args[i].second)) {
453 default: assert(0 && "Unexpected ValueType for argument!");
457 // Promote the integer to 32 bits. If the input type is signed use a
458 // sign extend, otherwise use a zero extend.
459 if (Args[i].second->isSigned())
460 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
462 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
467 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
468 Args[i].first, PtrOff,
469 DAG.getSrcValue(NULL)));
474 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
475 Args[i].first, PtrOff,
476 DAG.getSrcValue(NULL)));
481 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
484 std::vector<MVT::ValueType> RetVals;
485 MVT::ValueType RetTyVT = getValueType(RetTy);
486 RetVals.push_back(MVT::Other);
488 // The result values produced have to be legal. Promote the result.
490 case MVT::isVoid: break;
492 RetVals.push_back(RetTyVT);
497 RetVals.push_back(MVT::i32);
501 RetVals.push_back(MVT::f32);
503 RetVals.push_back(MVT::f64);
506 RetVals.push_back(MVT::i32);
507 RetVals.push_back(MVT::i32);
511 std::vector<MVT::ValueType> NodeTys;
512 NodeTys.push_back(MVT::Other); // Returns a chain
513 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
514 std::vector<SDOperand> Ops;
515 Ops.push_back(Chain);
516 Ops.push_back(Callee);
518 // FIXME: Do not generate X86ISD::TAILCALL for now.
519 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
520 SDOperand InFlag = Chain.getValue(1);
523 NodeTys.push_back(MVT::Other); // Returns a chain
524 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
526 Ops.push_back(Chain);
527 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
528 Ops.push_back(DAG.getConstant(0, getPointerTy()));
529 Ops.push_back(InFlag);
530 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
531 InFlag = Chain.getValue(1);
534 if (RetTyVT != MVT::isVoid) {
536 default: assert(0 && "Unknown value type to return!");
539 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
540 Chain = RetVal.getValue(1);
541 if (RetTyVT == MVT::i1)
542 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
545 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
546 Chain = RetVal.getValue(1);
549 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
550 Chain = RetVal.getValue(1);
553 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
554 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
556 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
557 Chain = Hi.getValue(1);
562 std::vector<MVT::ValueType> Tys;
563 Tys.push_back(MVT::f64);
564 Tys.push_back(MVT::Other);
565 Tys.push_back(MVT::Flag);
566 std::vector<SDOperand> Ops;
567 Ops.push_back(Chain);
568 Ops.push_back(InFlag);
569 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
570 Chain = RetVal.getValue(1);
571 InFlag = RetVal.getValue(2);
573 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
574 // shouldn't be necessary except that RFP cannot be live across
575 // multiple blocks. When stackifier is fixed, they can be uncoupled.
576 MachineFunction &MF = DAG.getMachineFunction();
577 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
578 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
580 Tys.push_back(MVT::Other);
582 Ops.push_back(Chain);
583 Ops.push_back(RetVal);
584 Ops.push_back(StackSlot);
585 Ops.push_back(DAG.getValueType(RetTyVT));
586 Ops.push_back(InFlag);
587 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
588 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
589 DAG.getSrcValue(NULL));
590 Chain = RetVal.getValue(1);
593 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
594 // FIXME: we would really like to remember that this FP_ROUND
595 // operation is okay to eliminate if we allow excess FP precision.
596 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
602 return std::make_pair(RetVal, Chain);
605 //===----------------------------------------------------------------------===//
606 // Fast Calling Convention implementation
607 //===----------------------------------------------------------------------===//
609 // The X86 'fast' calling convention passes up to two integer arguments in
610 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
611 // and requires that the callee pop its arguments off the stack (allowing proper
612 // tail calls), and has the same return value conventions as C calling convs.
614 // This calling convention always arranges for the callee pop value to be 8n+4
615 // bytes, which is needed for tail recursion elimination and stack alignment
618 // Note that this can be enhanced in the future to pass fp vals in registers
619 // (when we have a global fp allocator) and do other tricks.
622 /// AddLiveIn - This helper function adds the specified physical register to the
623 /// MachineFunction as a live in value. It also creates a corresponding virtual
625 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
626 TargetRegisterClass *RC) {
627 assert(RC->contains(PReg) && "Not the correct regclass!");
628 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
629 MF.addLiveIn(PReg, VReg);
634 std::vector<SDOperand>
635 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
636 std::vector<SDOperand> ArgValues;
638 MachineFunction &MF = DAG.getMachineFunction();
639 MachineFrameInfo *MFI = MF.getFrameInfo();
641 // Add DAG nodes to load the arguments... On entry to a function the stack
642 // frame looks like this:
644 // [ESP] -- return address
645 // [ESP + 4] -- first nonreg argument (leftmost lexically)
646 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
648 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
650 // Keep track of the number of integer regs passed so far. This can be either
651 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
653 unsigned NumIntRegs = 0;
655 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
656 MVT::ValueType ObjectVT = getValueType(I->getType());
657 unsigned ArgIncrement = 4;
658 unsigned ObjSize = 0;
662 default: assert(0 && "Unhandled argument type!");
665 if (NumIntRegs < 2) {
666 if (!I->use_empty()) {
667 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
668 X86::R8RegisterClass);
669 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
670 DAG.setRoot(ArgValue.getValue(1));
671 if (ObjectVT == MVT::i1)
672 // FIXME: Should insert a assertzext here.
673 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
682 if (NumIntRegs < 2) {
683 if (!I->use_empty()) {
684 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
685 X86::R16RegisterClass);
686 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
687 DAG.setRoot(ArgValue.getValue(1));
695 if (NumIntRegs < 2) {
696 if (!I->use_empty()) {
697 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
698 X86::R32RegisterClass);
699 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
700 DAG.setRoot(ArgValue.getValue(1));
708 if (NumIntRegs == 0) {
709 if (!I->use_empty()) {
710 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
711 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
713 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
714 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
715 DAG.setRoot(Hi.getValue(1));
717 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
721 } else if (NumIntRegs == 1) {
722 if (!I->use_empty()) {
723 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
724 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
725 DAG.setRoot(Low.getValue(1));
727 // Load the high part from memory.
728 // Create the frame index object for this incoming parameter...
729 int FI = MFI->CreateFixedObject(4, ArgOffset);
730 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
731 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
732 DAG.getSrcValue(NULL));
733 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
739 ObjSize = ArgIncrement = 8;
741 case MVT::f32: ObjSize = 4; break;
742 case MVT::f64: ObjSize = ArgIncrement = 8; break;
745 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
747 if (ObjSize && !I->use_empty()) {
748 // Create the frame index object for this incoming parameter...
749 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
751 // Create the SelectionDAG nodes corresponding to a load from this
753 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
755 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
756 DAG.getSrcValue(NULL));
757 } else if (ArgValue.Val == 0) {
758 if (MVT::isInteger(ObjectVT))
759 ArgValue = DAG.getConstant(0, ObjectVT);
761 ArgValue = DAG.getConstantFP(0, ObjectVT);
763 ArgValues.push_back(ArgValue);
766 ArgOffset += ArgIncrement; // Move on to the next argument.
769 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
770 // arguments and the arguments after the retaddr has been pushed are aligned.
771 if ((ArgOffset & 7) == 0)
774 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
775 ReturnAddrIndex = 0; // No return address slot generated yet.
776 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
777 BytesCallerReserves = 0;
779 // Finally, inform the code generator which regs we return values in.
780 switch (getValueType(F.getReturnType())) {
781 default: assert(0 && "Unknown type!");
782 case MVT::isVoid: break;
787 MF.addLiveOut(X86::EAX);
790 MF.addLiveOut(X86::EAX);
791 MF.addLiveOut(X86::EDX);
795 MF.addLiveOut(X86::ST0);
801 std::pair<SDOperand, SDOperand>
802 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
803 bool isTailCall, SDOperand Callee,
804 ArgListTy &Args, SelectionDAG &DAG) {
805 // Count how many bytes are to be pushed on the stack.
806 unsigned NumBytes = 0;
808 // Keep track of the number of integer regs passed so far. This can be either
809 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
811 unsigned NumIntRegs = 0;
813 for (unsigned i = 0, e = Args.size(); i != e; ++i)
814 switch (getValueType(Args[i].second)) {
815 default: assert(0 && "Unknown value type!");
820 if (NumIntRegs < 2) {
829 if (NumIntRegs == 0) {
832 } else if (NumIntRegs == 1) {
844 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
845 // arguments and the arguments after the retaddr has been pushed are aligned.
846 if ((NumBytes & 7) == 0)
849 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
851 // Arguments go on the stack in reverse order, as specified by the ABI.
852 unsigned ArgOffset = 0;
853 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
855 std::vector<SDOperand> Stores;
856 std::vector<SDOperand> RegValuesToPass;
857 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
858 switch (getValueType(Args[i].second)) {
859 default: assert(0 && "Unexpected ValueType for argument!");
861 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
866 if (NumIntRegs < 2) {
867 RegValuesToPass.push_back(Args[i].first);
873 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
874 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
875 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
876 Args[i].first, PtrOff,
877 DAG.getSrcValue(NULL)));
882 if (NumIntRegs < 2) { // Can pass part of it in regs?
883 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
884 Args[i].first, DAG.getConstant(1, MVT::i32));
885 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
886 Args[i].first, DAG.getConstant(0, MVT::i32));
887 RegValuesToPass.push_back(Lo);
889 if (NumIntRegs < 2) { // Pass both parts in regs?
890 RegValuesToPass.push_back(Hi);
893 // Pass the high part in memory.
894 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
895 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
896 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
897 Hi, PtrOff, DAG.getSrcValue(NULL)));
904 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
905 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
906 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
907 Args[i].first, PtrOff,
908 DAG.getSrcValue(NULL)));
914 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
916 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
917 // arguments and the arguments after the retaddr has been pushed are aligned.
918 if ((ArgOffset & 7) == 0)
921 std::vector<MVT::ValueType> RetVals;
922 MVT::ValueType RetTyVT = getValueType(RetTy);
924 RetVals.push_back(MVT::Other);
926 // The result values produced have to be legal. Promote the result.
928 case MVT::isVoid: break;
930 RetVals.push_back(RetTyVT);
935 RetVals.push_back(MVT::i32);
939 RetVals.push_back(MVT::f32);
941 RetVals.push_back(MVT::f64);
944 RetVals.push_back(MVT::i32);
945 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);
1068 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1069 if (ReturnAddrIndex == 0) {
1070 // Set up a frame object for the return address.
1071 MachineFunction &MF = DAG.getMachineFunction();
1072 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1075 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1080 std::pair<SDOperand, SDOperand> X86TargetLowering::
1081 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1082 SelectionDAG &DAG) {
1084 if (Depth) // Depths > 0 not supported yet!
1085 Result = DAG.getConstant(0, getPointerTy());
1087 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1088 if (!isFrameAddress)
1089 // Just load the return address
1090 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1091 DAG.getSrcValue(NULL));
1093 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1094 DAG.getConstant(4, MVT::i32));
1096 return std::make_pair(Result, Chain);
1099 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1100 /// which corresponds to the condition code.
1101 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1103 default: assert(0 && "Unknown X86 conditional code!");
1104 case X86ISD::COND_A: return X86::JA;
1105 case X86ISD::COND_AE: return X86::JAE;
1106 case X86ISD::COND_B: return X86::JB;
1107 case X86ISD::COND_BE: return X86::JBE;
1108 case X86ISD::COND_E: return X86::JE;
1109 case X86ISD::COND_G: return X86::JG;
1110 case X86ISD::COND_GE: return X86::JGE;
1111 case X86ISD::COND_L: return X86::JL;
1112 case X86ISD::COND_LE: return X86::JLE;
1113 case X86ISD::COND_NE: return X86::JNE;
1114 case X86ISD::COND_NO: return X86::JNO;
1115 case X86ISD::COND_NP: return X86::JNP;
1116 case X86ISD::COND_NS: return X86::JNS;
1117 case X86ISD::COND_O: return X86::JO;
1118 case X86ISD::COND_P: return X86::JP;
1119 case X86ISD::COND_S: return X86::JS;
1123 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1124 /// specific condition code. It returns a false if it cannot do a direct
1125 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1126 /// the order of comparison operands should be flipped.
1127 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1129 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1131 X86CC = X86ISD::COND_INVALID;
1133 switch (SetCCOpcode) {
1135 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1136 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1137 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1138 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1139 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1140 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1141 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1142 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1143 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1144 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1147 // On a floating point condition, the flags are set as follows:
1149 // 0 | 0 | 0 | X > Y
1150 // 0 | 0 | 1 | X < Y
1151 // 1 | 0 | 0 | X == Y
1152 // 1 | 1 | 1 | unordered
1153 switch (SetCCOpcode) {
1156 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1157 case ISD::SETOLE: Flip = true; // Fallthrough
1159 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1160 case ISD::SETOLT: Flip = true; // Fallthrough
1162 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1163 case ISD::SETUGE: Flip = true; // Fallthrough
1165 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1166 case ISD::SETUGT: Flip = true; // Fallthrough
1168 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1170 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1171 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1172 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1176 return X86CC != X86ISD::COND_INVALID;
1179 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1180 /// code. Current x86 isa includes the following FP cmov instructions:
1181 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1182 static bool hasFPCMov(unsigned X86CC) {
1186 case X86ISD::COND_B:
1187 case X86ISD::COND_BE:
1188 case X86ISD::COND_E:
1189 case X86ISD::COND_P:
1190 case X86ISD::COND_A:
1191 case X86ISD::COND_AE:
1192 case X86ISD::COND_NE:
1193 case X86ISD::COND_NP:
1199 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1200 MachineBasicBlock *BB) {
1201 switch (MI->getOpcode()) {
1202 default: assert(false && "Unexpected instr type to insert");
1203 case X86::CMOV_FR32:
1204 case X86::CMOV_FR64: {
1205 // To "insert" a SELECT_CC instruction, we actually have to insert the
1206 // diamond control-flow pattern. The incoming instruction knows the
1207 // destination vreg to set, the condition code register to branch on, the
1208 // true/false values to select between, and a branch opcode to use.
1209 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1210 ilist<MachineBasicBlock>::iterator It = BB;
1216 // cmpTY ccX, r1, r2
1218 // fallthrough --> copy0MBB
1219 MachineBasicBlock *thisMBB = BB;
1220 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1221 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1222 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1223 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1224 MachineFunction *F = BB->getParent();
1225 F->getBasicBlockList().insert(It, copy0MBB);
1226 F->getBasicBlockList().insert(It, sinkMBB);
1227 // Update machine-CFG edges
1228 BB->addSuccessor(copy0MBB);
1229 BB->addSuccessor(sinkMBB);
1232 // %FalseValue = ...
1233 // # fallthrough to sinkMBB
1236 // Update machine-CFG edges
1237 BB->addSuccessor(sinkMBB);
1240 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1243 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1244 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1245 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1247 delete MI; // The pseudo instruction is gone now.
1251 case X86::FP_TO_INT16_IN_MEM:
1252 case X86::FP_TO_INT32_IN_MEM:
1253 case X86::FP_TO_INT64_IN_MEM: {
1254 // Change the floating point control register to use "round towards zero"
1255 // mode when truncating to an integer value.
1256 MachineFunction *F = BB->getParent();
1257 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1258 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1260 // Load the old value of the high byte of the control word...
1262 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1263 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1265 // Set the high part to be round to zero...
1266 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1268 // Reload the modified control word now...
1269 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1271 // Restore the memory image of control word to original value
1272 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1274 // Get the X86 opcode to use.
1276 switch (MI->getOpcode()) {
1277 default: assert(0 && "illegal opcode!");
1278 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1279 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1280 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1284 MachineOperand &Op = MI->getOperand(0);
1285 if (Op.isRegister()) {
1286 AM.BaseType = X86AddressMode::RegBase;
1287 AM.Base.Reg = Op.getReg();
1289 AM.BaseType = X86AddressMode::FrameIndexBase;
1290 AM.Base.FrameIndex = Op.getFrameIndex();
1292 Op = MI->getOperand(1);
1293 if (Op.isImmediate())
1294 AM.Scale = Op.getImmedValue();
1295 Op = MI->getOperand(2);
1296 if (Op.isImmediate())
1297 AM.IndexReg = Op.getImmedValue();
1298 Op = MI->getOperand(3);
1299 if (Op.isGlobalAddress()) {
1300 AM.GV = Op.getGlobal();
1302 AM.Disp = Op.getImmedValue();
1304 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1306 // Reload the original control word now.
1307 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1309 delete MI; // The pseudo instruction is gone now.
1316 //===----------------------------------------------------------------------===//
1317 // X86 Custom Lowering Hooks
1318 //===----------------------------------------------------------------------===//
1320 /// LowerOperation - Provide custom lowering hooks for some operations.
1322 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1323 switch (Op.getOpcode()) {
1324 default: assert(0 && "Should not custom lower this!");
1325 case ISD::SHL_PARTS:
1326 case ISD::SRA_PARTS:
1327 case ISD::SRL_PARTS: {
1328 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1329 "Not an i64 shift!");
1330 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1331 SDOperand ShOpLo = Op.getOperand(0);
1332 SDOperand ShOpHi = Op.getOperand(1);
1333 SDOperand ShAmt = Op.getOperand(2);
1334 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1335 DAG.getConstant(31, MVT::i8))
1336 : DAG.getConstant(0, MVT::i32);
1338 SDOperand Tmp2, Tmp3;
1339 if (Op.getOpcode() == ISD::SHL_PARTS) {
1340 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1341 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1343 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1344 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1347 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1348 ShAmt, DAG.getConstant(32, MVT::i8));
1351 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1353 std::vector<MVT::ValueType> Tys;
1354 Tys.push_back(MVT::i32);
1355 Tys.push_back(MVT::Flag);
1356 std::vector<SDOperand> Ops;
1357 if (Op.getOpcode() == ISD::SHL_PARTS) {
1358 Ops.push_back(Tmp2);
1359 Ops.push_back(Tmp3);
1361 Ops.push_back(InFlag);
1362 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1363 InFlag = Hi.getValue(1);
1366 Ops.push_back(Tmp3);
1367 Ops.push_back(Tmp1);
1369 Ops.push_back(InFlag);
1370 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1372 Ops.push_back(Tmp2);
1373 Ops.push_back(Tmp3);
1375 Ops.push_back(InFlag);
1376 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1377 InFlag = Lo.getValue(1);
1380 Ops.push_back(Tmp3);
1381 Ops.push_back(Tmp1);
1383 Ops.push_back(InFlag);
1384 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1388 Tys.push_back(MVT::i32);
1389 Tys.push_back(MVT::i32);
1393 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1395 case ISD::SINT_TO_FP: {
1396 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
1397 Op.getOperand(0).getValueType() >= MVT::i16 &&
1398 "Unknown SINT_TO_FP to lower!");
1401 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1402 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1403 MachineFunction &MF = DAG.getMachineFunction();
1404 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1405 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1406 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1407 DAG.getEntryNode(), Op.getOperand(0),
1408 StackSlot, DAG.getSrcValue(NULL));
1411 std::vector<MVT::ValueType> Tys;
1412 Tys.push_back(MVT::f64);
1413 Tys.push_back(MVT::Other);
1414 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
1415 std::vector<SDOperand> Ops;
1416 Ops.push_back(Chain);
1417 Ops.push_back(StackSlot);
1418 Ops.push_back(DAG.getValueType(SrcVT));
1419 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1423 Chain = Result.getValue(1);
1424 SDOperand InFlag = Result.getValue(2);
1426 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
1427 // shouldn't be necessary except that RFP cannot be live across
1428 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1429 MachineFunction &MF = DAG.getMachineFunction();
1430 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1431 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1432 std::vector<MVT::ValueType> Tys;
1433 Tys.push_back(MVT::Other);
1434 std::vector<SDOperand> Ops;
1435 Ops.push_back(Chain);
1436 Ops.push_back(Result);
1437 Ops.push_back(StackSlot);
1438 Ops.push_back(DAG.getValueType(Op.getValueType()));
1439 Ops.push_back(InFlag);
1440 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1441 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1442 DAG.getSrcValue(NULL));
1447 case ISD::FP_TO_SINT: {
1448 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1449 "Unknown FP_TO_SINT to lower!");
1450 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1452 MachineFunction &MF = DAG.getMachineFunction();
1453 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1454 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1455 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1458 switch (Op.getValueType()) {
1459 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1460 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1461 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1462 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1465 SDOperand Chain = DAG.getEntryNode();
1466 SDOperand Value = Op.getOperand(0);
1468 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1469 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1470 DAG.getSrcValue(0));
1471 std::vector<MVT::ValueType> Tys;
1472 Tys.push_back(MVT::f64);
1473 Tys.push_back(MVT::Other);
1474 std::vector<SDOperand> Ops;
1475 Ops.push_back(Chain);
1476 Ops.push_back(StackSlot);
1477 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
1478 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1479 Chain = Value.getValue(1);
1480 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1481 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1484 // Build the FP_TO_INT*_IN_MEM
1485 std::vector<SDOperand> Ops;
1486 Ops.push_back(Chain);
1487 Ops.push_back(Value);
1488 Ops.push_back(StackSlot);
1489 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1492 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1493 DAG.getSrcValue(NULL));
1495 case ISD::READCYCLECOUNTER: {
1496 std::vector<MVT::ValueType> Tys;
1497 Tys.push_back(MVT::Other);
1498 Tys.push_back(MVT::Flag);
1499 std::vector<SDOperand> Ops;
1500 Ops.push_back(Op.getOperand(0));
1501 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1503 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1504 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1505 MVT::i32, Ops[0].getValue(2)));
1506 Ops.push_back(Ops[1].getValue(1));
1507 Tys[0] = Tys[1] = MVT::i32;
1508 Tys.push_back(MVT::Other);
1509 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1512 MVT::ValueType VT = Op.getValueType();
1513 const Type *OpNTy = MVT::getTypeForValueType(VT);
1514 std::vector<Constant*> CV;
1515 if (VT == MVT::f64) {
1516 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1517 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1519 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1520 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1521 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1522 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1524 Constant *CS = ConstantStruct::get(CV);
1525 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1527 = DAG.getNode(X86ISD::LOAD_PACK,
1528 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1529 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1532 MVT::ValueType VT = Op.getValueType();
1533 const Type *OpNTy = MVT::getTypeForValueType(VT);
1534 std::vector<Constant*> CV;
1535 if (VT == MVT::f64) {
1536 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1537 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1539 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1540 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1541 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1542 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1544 Constant *CS = ConstantStruct::get(CV);
1545 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1547 = DAG.getNode(X86ISD::LOAD_PACK,
1548 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1549 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1552 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1554 SDOperand CC = Op.getOperand(2);
1555 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1556 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1559 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1561 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1562 Op.getOperand(1), Op.getOperand(0));
1564 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1565 Op.getOperand(0), Op.getOperand(1));
1566 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1567 DAG.getConstant(X86CC, MVT::i8), Cond);
1569 assert(isFP && "Illegal integer SetCC!");
1571 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1572 Op.getOperand(0), Op.getOperand(1));
1573 std::vector<MVT::ValueType> Tys;
1574 std::vector<SDOperand> Ops;
1575 switch (SetCCOpcode) {
1576 default: assert(false && "Illegal floating point SetCC!");
1577 case ISD::SETOEQ: { // !PF & ZF
1578 Tys.push_back(MVT::i8);
1579 Tys.push_back(MVT::Flag);
1580 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1581 Ops.push_back(Cond);
1582 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1583 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1584 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1586 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1588 case ISD::SETUNE: { // PF | !ZF
1589 Tys.push_back(MVT::i8);
1590 Tys.push_back(MVT::Flag);
1591 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1592 Ops.push_back(Cond);
1593 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1594 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1595 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1597 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1603 MVT::ValueType VT = Op.getValueType();
1604 bool isFP = MVT::isFloatingPoint(VT);
1605 bool isFPStack = isFP && !X86ScalarSSE;
1606 bool isFPSSE = isFP && X86ScalarSSE;
1607 bool addTest = false;
1608 SDOperand Op0 = Op.getOperand(0);
1610 if (Op0.getOpcode() == ISD::SETCC)
1611 Op0 = LowerOperation(Op0, DAG);
1613 if (Op0.getOpcode() == X86ISD::SETCC) {
1614 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1615 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1616 // have another use it will be eliminated.
1617 // If the X86ISD::SETCC has more than one use, then it's probably better
1618 // to use a test instead of duplicating the X86ISD::CMP (for register
1619 // pressure reason).
1620 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1621 if (!Op0.hasOneUse()) {
1622 std::vector<MVT::ValueType> Tys;
1623 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1624 Tys.push_back(Op0.Val->getValueType(i));
1625 std::vector<SDOperand> Ops;
1626 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1627 Ops.push_back(Op0.getOperand(i));
1628 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1631 CC = Op0.getOperand(0);
1632 Cond = Op0.getOperand(1);
1633 // Make a copy as flag result cannot be used by more than one.
1634 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1635 Cond.getOperand(0), Cond.getOperand(1));
1637 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
1644 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1645 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
1648 std::vector<MVT::ValueType> Tys;
1649 Tys.push_back(Op.getValueType());
1650 Tys.push_back(MVT::Flag);
1651 std::vector<SDOperand> Ops;
1652 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1653 // condition is true.
1654 Ops.push_back(Op.getOperand(2));
1655 Ops.push_back(Op.getOperand(1));
1657 Ops.push_back(Cond);
1658 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
1661 bool addTest = false;
1662 SDOperand Cond = Op.getOperand(1);
1663 SDOperand Dest = Op.getOperand(2);
1665 if (Cond.getOpcode() == ISD::SETCC)
1666 Cond = LowerOperation(Cond, DAG);
1668 if (Cond.getOpcode() == X86ISD::SETCC) {
1669 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1670 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1671 // have another use it will be eliminated.
1672 // If the X86ISD::SETCC has more than one use, then it's probably better
1673 // to use a test instead of duplicating the X86ISD::CMP (for register
1674 // pressure reason).
1675 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1676 if (!Cond.hasOneUse()) {
1677 std::vector<MVT::ValueType> Tys;
1678 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1679 Tys.push_back(Cond.Val->getValueType(i));
1680 std::vector<SDOperand> Ops;
1681 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1682 Ops.push_back(Cond.getOperand(i));
1683 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1686 CC = Cond.getOperand(0);
1687 Cond = Cond.getOperand(1);
1688 // Make a copy as flag result cannot be used by more than one.
1689 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1690 Cond.getOperand(0), Cond.getOperand(1));
1697 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1698 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1700 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1701 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1704 SDOperand InFlag(0, 0);
1705 SDOperand Chain = Op.getOperand(0);
1707 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1708 if (Align == 0) Align = 1;
1710 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1711 // If not DWORD aligned, call memset if size is less than the threshold.
1712 // It knows how to align to the right boundary first.
1713 if ((Align & 3) != 0 ||
1714 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1715 MVT::ValueType IntPtr = getPointerTy();
1716 const Type *IntPtrTy = getTargetData().getIntPtrType();
1717 std::vector<std::pair<SDOperand, const Type*> > Args;
1718 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1719 // Extend the ubyte argument to be an int value for the call.
1720 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1721 Args.push_back(std::make_pair(Val, IntPtrTy));
1722 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1723 std::pair<SDOperand,SDOperand> CallResult =
1724 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1725 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1726 return CallResult.second;
1731 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1732 unsigned BytesLeft = 0;
1735 unsigned Val = ValC->getValue() & 255;
1737 // If the value is a constant, then we can potentially use larger sets.
1738 switch (Align & 3) {
1739 case 2: // WORD aligned
1741 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1742 BytesLeft = I->getValue() % 2;
1743 Val = (Val << 8) | Val;
1746 case 0: // DWORD aligned
1748 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1749 BytesLeft = I->getValue() % 4;
1750 Val = (Val << 8) | Val;
1751 Val = (Val << 16) | Val;
1754 default: // Byte aligned
1756 Count = Op.getOperand(3);
1761 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1763 InFlag = Chain.getValue(1);
1766 Count = Op.getOperand(3);
1767 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1768 InFlag = Chain.getValue(1);
1771 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1772 InFlag = Chain.getValue(1);
1773 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1774 InFlag = Chain.getValue(1);
1775 Chain = DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1776 DAG.getValueType(AVT), InFlag);
1779 // Issue stores for the last 1 - 3 bytes.
1781 unsigned Val = ValC->getValue() & 255;
1782 unsigned Offset = I->getValue() - BytesLeft;
1783 SDOperand DstAddr = Op.getOperand(1);
1784 MVT::ValueType AddrVT = DstAddr.getValueType();
1785 if (BytesLeft >= 2) {
1786 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1787 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1788 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1789 DAG.getConstant(Offset, AddrVT)),
1790 DAG.getSrcValue(NULL));
1795 if (BytesLeft == 1) {
1796 Value = DAG.getConstant(Val, MVT::i8);
1797 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1798 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1799 DAG.getConstant(Offset, AddrVT)),
1800 DAG.getSrcValue(NULL));
1807 SDOperand Chain = Op.getOperand(0);
1809 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1810 if (Align == 0) Align = 1;
1812 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1813 // If not DWORD aligned, call memcpy if size is less than the threshold.
1814 // It knows how to align to the right boundary first.
1815 if ((Align & 3) != 0 ||
1816 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1817 MVT::ValueType IntPtr = getPointerTy();
1818 const Type *IntPtrTy = getTargetData().getIntPtrType();
1819 std::vector<std::pair<SDOperand, const Type*> > Args;
1820 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1821 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1822 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1823 std::pair<SDOperand,SDOperand> CallResult =
1824 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1825 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1826 return CallResult.second;
1831 unsigned BytesLeft = 0;
1832 switch (Align & 3) {
1833 case 2: // WORD aligned
1835 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1836 BytesLeft = I->getValue() % 2;
1838 case 0: // DWORD aligned
1840 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1841 BytesLeft = I->getValue() % 4;
1843 default: // Byte aligned
1845 Count = Op.getOperand(3);
1849 SDOperand InFlag(0, 0);
1850 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1851 InFlag = Chain.getValue(1);
1852 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1853 InFlag = Chain.getValue(1);
1854 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1855 InFlag = Chain.getValue(1);
1856 Chain = DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1857 DAG.getValueType(AVT), InFlag);
1860 // Issue loads and stores for the last 1 - 3 bytes.
1861 unsigned Offset = I->getValue() - BytesLeft;
1862 SDOperand DstAddr = Op.getOperand(1);
1863 MVT::ValueType DstVT = DstAddr.getValueType();
1864 SDOperand SrcAddr = Op.getOperand(2);
1865 MVT::ValueType SrcVT = SrcAddr.getValueType();
1867 if (BytesLeft >= 2) {
1868 Value = DAG.getLoad(MVT::i16, Chain,
1869 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1870 DAG.getConstant(Offset, SrcVT)),
1871 DAG.getSrcValue(NULL));
1872 Chain = Value.getValue(1);
1873 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1874 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1875 DAG.getConstant(Offset, DstVT)),
1876 DAG.getSrcValue(NULL));
1881 if (BytesLeft == 1) {
1882 Value = DAG.getLoad(MVT::i8, Chain,
1883 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1884 DAG.getConstant(Offset, SrcVT)),
1885 DAG.getSrcValue(NULL));
1886 Chain = Value.getValue(1);
1887 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1888 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1889 DAG.getConstant(Offset, DstVT)),
1890 DAG.getSrcValue(NULL));
1897 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1898 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1899 // one of the above mentioned nodes. It has to be wrapped because otherwise
1900 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1901 // be used to form addressing mode. These wrapped nodes will be selected
1903 case ISD::ConstantPool: {
1904 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1905 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1906 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1907 CP->getAlignment()));
1908 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
1909 // With PIC, the address is actually $g + Offset.
1910 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1911 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1912 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1917 case ISD::GlobalAddress: {
1918 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1919 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1920 DAG.getTargetGlobalAddress(GV, getPointerTy()));
1921 if (getTargetMachine().
1922 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1923 // With PIC, the address is actually $g + Offset.
1924 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1925 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1926 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1928 // For Darwin, external and weak symbols are indirect, so we want to load
1929 // the value at address GV, not the value of GV itself. This means that
1930 // the GlobalAddress must be in the base or index register of the address,
1931 // not the GV offset field.
1932 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
1933 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1934 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
1935 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1936 Result, DAG.getSrcValue(NULL));
1941 case ISD::ExternalSymbol: {
1942 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
1943 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1944 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
1945 if (getTargetMachine().
1946 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1947 // With PIC, the address is actually $g + Offset.
1948 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1949 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1950 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1955 case ISD::VASTART: {
1956 // vastart just stores the address of the VarArgsFrameIndex slot into the
1957 // memory location argument.
1958 // FIXME: Replace MVT::i32 with PointerTy
1959 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1960 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1961 Op.getOperand(1), Op.getOperand(2));
1966 switch(Op.getNumOperands()) {
1968 assert(0 && "Do not know how to return this many arguments!");
1971 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1972 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1974 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1975 if (MVT::isInteger(ArgVT))
1976 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1978 else if (!X86ScalarSSE) {
1979 std::vector<MVT::ValueType> Tys;
1980 Tys.push_back(MVT::Other);
1981 Tys.push_back(MVT::Flag);
1982 std::vector<SDOperand> Ops;
1983 Ops.push_back(Op.getOperand(0));
1984 Ops.push_back(Op.getOperand(1));
1985 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1988 SDOperand Chain = Op.getOperand(0);
1989 SDOperand Value = Op.getOperand(1);
1991 if (Value.getOpcode() == ISD::LOAD &&
1992 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
1993 Chain = Value.getOperand(0);
1994 MemLoc = Value.getOperand(1);
1996 // Spill the value to memory and reload it into top of stack.
1997 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1998 MachineFunction &MF = DAG.getMachineFunction();
1999 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2000 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2001 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2002 Value, MemLoc, DAG.getSrcValue(0));
2004 std::vector<MVT::ValueType> Tys;
2005 Tys.push_back(MVT::f64);
2006 Tys.push_back(MVT::Other);
2007 std::vector<SDOperand> Ops;
2008 Ops.push_back(Chain);
2009 Ops.push_back(MemLoc);
2010 Ops.push_back(DAG.getValueType(ArgVT));
2011 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2013 Tys.push_back(MVT::Other);
2014 Tys.push_back(MVT::Flag);
2016 Ops.push_back(Copy.getValue(1));
2017 Ops.push_back(Copy);
2018 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2023 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2025 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2028 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2029 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2035 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2037 default: return NULL;
2038 case X86ISD::SHLD: return "X86ISD::SHLD";
2039 case X86ISD::SHRD: return "X86ISD::SHRD";
2040 case X86ISD::FAND: return "X86ISD::FAND";
2041 case X86ISD::FXOR: return "X86ISD::FXOR";
2042 case X86ISD::FILD: return "X86ISD::FILD";
2043 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
2044 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2045 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2046 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
2047 case X86ISD::FLD: return "X86ISD::FLD";
2048 case X86ISD::FST: return "X86ISD::FST";
2049 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
2050 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
2051 case X86ISD::CALL: return "X86ISD::CALL";
2052 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2053 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2054 case X86ISD::CMP: return "X86ISD::CMP";
2055 case X86ISD::TEST: return "X86ISD::TEST";
2056 case X86ISD::SETCC: return "X86ISD::SETCC";
2057 case X86ISD::CMOV: return "X86ISD::CMOV";
2058 case X86ISD::BRCOND: return "X86ISD::BRCOND";
2059 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
2060 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2061 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
2062 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
2063 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
2064 case X86ISD::Wrapper: return "X86ISD::Wrapper";
2068 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2070 uint64_t &KnownZero,
2072 unsigned Depth) const {
2074 unsigned Opc = Op.getOpcode();
2075 KnownZero = KnownOne = 0; // Don't know anything.
2079 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2082 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2087 std::vector<unsigned> X86TargetLowering::
2088 getRegClassForInlineAsmConstraint(const std::string &Constraint,
2089 MVT::ValueType VT) const {
2090 if (Constraint.size() == 1) {
2091 // FIXME: not handling fp-stack yet!
2092 // FIXME: not handling MMX registers yet ('y' constraint).
2093 switch (Constraint[0]) { // GCC X86 Constraint Letters
2094 default: break; // Unknown constriant letter
2095 case 'r': // GENERAL_REGS
2096 case 'R': // LEGACY_REGS
2097 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2098 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2099 case 'l': // INDEX_REGS
2100 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2101 X86::ESI, X86::EDI, X86::EBP, 0);
2102 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2104 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2105 case 'x': // SSE_REGS if SSE1 allowed
2106 if (Subtarget->hasSSE1())
2107 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2108 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2110 return std::vector<unsigned>();
2111 case 'Y': // SSE_REGS if SSE2 allowed
2112 if (Subtarget->hasSSE2())
2113 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2114 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2116 return std::vector<unsigned>();
2120 return std::vector<unsigned>();