blob: 1aa2c4662dbd0abab61deec1d2cf2844eed2ee52 [file] [log] [blame]
Chris Lattner76ac0682005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/Function.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000027#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnerc642aa52006-01-31 19:43:35 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000030using namespace llvm;
31
32// FIXME: temporary.
33#include "llvm/Support/CommandLine.h"
34static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
36
37X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000039 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
41
Chris Lattner76ac0682005-11-15 00:40:23 +000042 // Set up the TargetLowering object.
43
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);
Evan Cheng83eeefb2006-01-25 09:15:17 +000048 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000049 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000050 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000051
Chris Lattner76ac0682005-11-15 00:40:23 +000052 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58 // operation.
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);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000062
63 if (X86ScalarSSE)
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
66 else
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000068
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70 // this operation.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000073 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000074 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000075 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000076 else {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
79 }
Chris Lattner76ac0682005-11-15 00:40:23 +000080
Evan Cheng5b97fcf2006-01-30 08:02:57 +000081 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82 // isn't legal.
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
85
Evan Cheng08390f62006-01-30 22:13:22 +000086 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
90
91 if (X86ScalarSSE) {
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
93 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +000095 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +000096 }
97
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
99 // conversion.
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);
103
Evan Cheng08390f62006-01-30 22:13:22 +0000104 if (X86ScalarSSE)
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);
109 else
Chris Lattner76ac0682005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
111
Evan Cheng08390f62006-01-30 22:13:22 +0000112 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
113 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000114
Evan Cheng593bea72006-02-17 07:01:52 +0000115 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000116 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
117 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000118 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
119 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000120 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
124 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
125 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
126 setOperationAction(ISD::FREM , MVT::f64 , Expand);
127 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
128 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
129 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
130 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
131 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
132 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
133 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
134 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
135 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000136 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000137 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000138
Chris Lattner76ac0682005-11-15 00:40:23 +0000139 setOperationAction(ISD::READIO , MVT::i1 , Expand);
140 setOperationAction(ISD::READIO , MVT::i8 , Expand);
141 setOperationAction(ISD::READIO , MVT::i16 , Expand);
142 setOperationAction(ISD::READIO , MVT::i32 , Expand);
143 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
144 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
145 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
146 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
147
148 // These should be promoted to a larger select which is supported.
149 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
150 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000151
152 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000153 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
154 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
155 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
156 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
157 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
158 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
159 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
160 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
161 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000162 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000163 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000164 // Darwin ABI issue.
Evan Cheng593bea72006-02-17 07:01:52 +0000165 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000166 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000167 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
168 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
169 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000170 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000171 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
172 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000173
Chris Lattner9c415362005-11-29 06:16:21 +0000174 // We don't have line number support yet.
175 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000176 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
177 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000178
Nate Begemane74795c2006-01-25 18:21:52 +0000179 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
180 setOperationAction(ISD::VASTART , MVT::Other, Custom);
181
182 // Use the default implementation.
183 setOperationAction(ISD::VAARG , MVT::Other, Expand);
184 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
185 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000186 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
187 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
188 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000189
Chris Lattner76ac0682005-11-15 00:40:23 +0000190 if (X86ScalarSSE) {
191 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000192 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
193 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000194
195 // SSE has no load+extend ops
196 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
197 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
198
Evan Cheng72d5c252006-01-31 22:28:30 +0000199 // Use ANDPD to simulate FABS.
200 setOperationAction(ISD::FABS , MVT::f64, Custom);
201 setOperationAction(ISD::FABS , MVT::f32, Custom);
202
203 // Use XORP to simulate FNEG.
204 setOperationAction(ISD::FNEG , MVT::f64, Custom);
205 setOperationAction(ISD::FNEG , MVT::f32, Custom);
206
Evan Chengd8fba3a2006-02-02 00:28:23 +0000207 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000208 setOperationAction(ISD::FSIN , MVT::f64, Expand);
209 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000210 setOperationAction(ISD::FREM , MVT::f64, Expand);
211 setOperationAction(ISD::FSIN , MVT::f32, Expand);
212 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000213 setOperationAction(ISD::FREM , MVT::f32, Expand);
214
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000215 // Expand FP immediates into loads from the stack, except for the special
216 // cases we handle.
217 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
218 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000219 addLegalFPImmediate(+0.0); // xorps / xorpd
220 } else {
221 // Set up the FP register classes.
222 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000223
224 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
225
Chris Lattner76ac0682005-11-15 00:40:23 +0000226 if (!UnsafeFPMath) {
227 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
228 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
229 }
230
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000231 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000232 addLegalFPImmediate(+0.0); // FLD0
233 addLegalFPImmediate(+1.0); // FLD1
234 addLegalFPImmediate(-0.0); // FLD0/FCHS
235 addLegalFPImmediate(-1.0); // FLD1/FCHS
236 }
237 computeRegisterProperties();
238
Evan Cheng6a374562006-02-14 08:25:08 +0000239 // FIXME: These should be based on subtarget info. Plus, the values should
240 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000241 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
242 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
243 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000244 allowUnalignedMemoryAccesses = true; // x86 supports it!
245}
246
247std::vector<SDOperand>
248X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
249 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
250 return LowerFastCCArguments(F, DAG);
251 return LowerCCCArguments(F, DAG);
252}
253
254std::pair<SDOperand, SDOperand>
255X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
256 bool isVarArg, unsigned CallingConv,
257 bool isTailCall,
258 SDOperand Callee, ArgListTy &Args,
259 SelectionDAG &DAG) {
260 assert((!isVarArg || CallingConv == CallingConv::C) &&
261 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000262
263 // If the callee is a GlobalAddress node (quite common, every direct call is)
264 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
265 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
266 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000267 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
268 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000269
Chris Lattner76ac0682005-11-15 00:40:23 +0000270 if (CallingConv == CallingConv::Fast && EnableFastCC)
271 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
272 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
273}
274
275//===----------------------------------------------------------------------===//
276// C Calling Convention implementation
277//===----------------------------------------------------------------------===//
278
279std::vector<SDOperand>
280X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
281 std::vector<SDOperand> ArgValues;
282
283 MachineFunction &MF = DAG.getMachineFunction();
284 MachineFrameInfo *MFI = MF.getFrameInfo();
285
286 // Add DAG nodes to load the arguments... On entry to a function on the X86,
287 // the stack frame looks like this:
288 //
289 // [ESP] -- return address
290 // [ESP + 4] -- first argument (leftmost lexically)
291 // [ESP + 8] -- second argument, if first argument is four bytes in size
292 // ...
293 //
294 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
295 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
296 MVT::ValueType ObjectVT = getValueType(I->getType());
297 unsigned ArgIncrement = 4;
298 unsigned ObjSize;
299 switch (ObjectVT) {
300 default: assert(0 && "Unhandled argument type!");
301 case MVT::i1:
302 case MVT::i8: ObjSize = 1; break;
303 case MVT::i16: ObjSize = 2; break;
304 case MVT::i32: ObjSize = 4; break;
305 case MVT::i64: ObjSize = ArgIncrement = 8; break;
306 case MVT::f32: ObjSize = 4; break;
307 case MVT::f64: ObjSize = ArgIncrement = 8; break;
308 }
309 // Create the frame index object for this incoming parameter...
310 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
311
312 // Create the SelectionDAG nodes corresponding to a load from this parameter
313 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
314
315 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
316 // dead loads.
317 SDOperand ArgValue;
318 if (!I->use_empty())
319 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
320 DAG.getSrcValue(NULL));
321 else {
322 if (MVT::isInteger(ObjectVT))
323 ArgValue = DAG.getConstant(0, ObjectVT);
324 else
325 ArgValue = DAG.getConstantFP(0, ObjectVT);
326 }
327 ArgValues.push_back(ArgValue);
328
329 ArgOffset += ArgIncrement; // Move on to the next argument...
330 }
331
332 // If the function takes variable number of arguments, make a frame index for
333 // the start of the first vararg value... for expansion of llvm.va_start.
334 if (F.isVarArg())
335 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
336 ReturnAddrIndex = 0; // No return address slot generated yet.
337 BytesToPopOnReturn = 0; // Callee pops nothing.
338 BytesCallerReserves = ArgOffset;
339
340 // Finally, inform the code generator which regs we return values in.
341 switch (getValueType(F.getReturnType())) {
342 default: assert(0 && "Unknown type!");
343 case MVT::isVoid: break;
344 case MVT::i1:
345 case MVT::i8:
346 case MVT::i16:
347 case MVT::i32:
348 MF.addLiveOut(X86::EAX);
349 break;
350 case MVT::i64:
351 MF.addLiveOut(X86::EAX);
352 MF.addLiveOut(X86::EDX);
353 break;
354 case MVT::f32:
355 case MVT::f64:
356 MF.addLiveOut(X86::ST0);
357 break;
358 }
359 return ArgValues;
360}
361
362std::pair<SDOperand, SDOperand>
363X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
364 bool isVarArg, bool isTailCall,
365 SDOperand Callee, ArgListTy &Args,
366 SelectionDAG &DAG) {
367 // Count how many bytes are to be pushed on the stack.
368 unsigned NumBytes = 0;
369
370 if (Args.empty()) {
371 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000372 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000373 } else {
374 for (unsigned i = 0, e = Args.size(); i != e; ++i)
375 switch (getValueType(Args[i].second)) {
376 default: assert(0 && "Unknown value type!");
377 case MVT::i1:
378 case MVT::i8:
379 case MVT::i16:
380 case MVT::i32:
381 case MVT::f32:
382 NumBytes += 4;
383 break;
384 case MVT::i64:
385 case MVT::f64:
386 NumBytes += 8;
387 break;
388 }
389
Chris Lattner62c34842006-02-13 09:00:43 +0000390 Chain = DAG.getCALLSEQ_START(Chain,
391 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000392
393 // Arguments go on the stack in reverse order, as specified by the ABI.
394 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000395 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000396 std::vector<SDOperand> Stores;
397
398 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
399 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
400 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
401
402 switch (getValueType(Args[i].second)) {
403 default: assert(0 && "Unexpected ValueType for argument!");
404 case MVT::i1:
405 case MVT::i8:
406 case MVT::i16:
407 // Promote the integer to 32 bits. If the input type is signed use a
408 // sign extend, otherwise use a zero extend.
409 if (Args[i].second->isSigned())
410 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
411 else
412 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
413
414 // FALL THROUGH
415 case MVT::i32:
416 case MVT::f32:
417 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
418 Args[i].first, PtrOff,
419 DAG.getSrcValue(NULL)));
420 ArgOffset += 4;
421 break;
422 case MVT::i64:
423 case MVT::f64:
424 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
425 Args[i].first, PtrOff,
426 DAG.getSrcValue(NULL)));
427 ArgOffset += 8;
428 break;
429 }
430 }
431 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
432 }
433
434 std::vector<MVT::ValueType> RetVals;
435 MVT::ValueType RetTyVT = getValueType(RetTy);
436 RetVals.push_back(MVT::Other);
437
438 // The result values produced have to be legal. Promote the result.
439 switch (RetTyVT) {
440 case MVT::isVoid: break;
441 default:
442 RetVals.push_back(RetTyVT);
443 break;
444 case MVT::i1:
445 case MVT::i8:
446 case MVT::i16:
447 RetVals.push_back(MVT::i32);
448 break;
449 case MVT::f32:
450 if (X86ScalarSSE)
451 RetVals.push_back(MVT::f32);
452 else
453 RetVals.push_back(MVT::f64);
454 break;
455 case MVT::i64:
456 RetVals.push_back(MVT::i32);
457 RetVals.push_back(MVT::i32);
458 break;
459 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000460
Nate Begeman7e5496d2006-02-17 00:03:04 +0000461 std::vector<MVT::ValueType> NodeTys;
462 NodeTys.push_back(MVT::Other); // Returns a chain
463 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
464 std::vector<SDOperand> Ops;
465 Ops.push_back(Chain);
466 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000467
Nate Begeman7e5496d2006-02-17 00:03:04 +0000468 // FIXME: Do not generate X86ISD::TAILCALL for now.
469 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
470 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000471
Nate Begeman7e5496d2006-02-17 00:03:04 +0000472 NodeTys.clear();
473 NodeTys.push_back(MVT::Other); // Returns a chain
474 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
475 Ops.clear();
476 Ops.push_back(Chain);
477 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
478 Ops.push_back(DAG.getConstant(0, getPointerTy()));
479 Ops.push_back(InFlag);
480 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
481 InFlag = Chain.getValue(1);
482
483 SDOperand RetVal;
484 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000485 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000486 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000487 case MVT::i1:
488 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000489 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
490 Chain = RetVal.getValue(1);
491 if (RetTyVT == MVT::i1)
492 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
493 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000494 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000495 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
496 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000497 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000498 case MVT::i32:
499 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
500 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000501 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000502 case MVT::i64: {
503 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
504 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
505 Lo.getValue(2));
506 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
507 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000508 break;
509 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000510 case MVT::f32:
511 case MVT::f64: {
512 std::vector<MVT::ValueType> Tys;
513 Tys.push_back(MVT::f64);
514 Tys.push_back(MVT::Other);
515 Tys.push_back(MVT::Flag);
516 std::vector<SDOperand> Ops;
517 Ops.push_back(Chain);
518 Ops.push_back(InFlag);
519 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
520 Chain = RetVal.getValue(1);
521 InFlag = RetVal.getValue(2);
522 if (X86ScalarSSE) {
523 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
524 // shouldn't be necessary except that RFP cannot be live across
525 // multiple blocks. When stackifier is fixed, they can be uncoupled.
526 MachineFunction &MF = DAG.getMachineFunction();
527 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
528 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
529 Tys.clear();
530 Tys.push_back(MVT::Other);
531 Ops.clear();
532 Ops.push_back(Chain);
533 Ops.push_back(RetVal);
534 Ops.push_back(StackSlot);
535 Ops.push_back(DAG.getValueType(RetTyVT));
536 Ops.push_back(InFlag);
537 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
538 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
539 DAG.getSrcValue(NULL));
540 Chain = RetVal.getValue(1);
541 }
Evan Cheng45e190982006-01-05 00:27:02 +0000542
Nate Begeman7e5496d2006-02-17 00:03:04 +0000543 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
544 // FIXME: we would really like to remember that this FP_ROUND
545 // operation is okay to eliminate if we allow excess FP precision.
546 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
547 break;
548 }
549 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000550 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000551
552 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000553}
554
Chris Lattner76ac0682005-11-15 00:40:23 +0000555//===----------------------------------------------------------------------===//
556// Fast Calling Convention implementation
557//===----------------------------------------------------------------------===//
558//
559// The X86 'fast' calling convention passes up to two integer arguments in
560// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
561// and requires that the callee pop its arguments off the stack (allowing proper
562// tail calls), and has the same return value conventions as C calling convs.
563//
564// This calling convention always arranges for the callee pop value to be 8n+4
565// bytes, which is needed for tail recursion elimination and stack alignment
566// reasons.
567//
568// Note that this can be enhanced in the future to pass fp vals in registers
569// (when we have a global fp allocator) and do other tricks.
570//
571
572/// AddLiveIn - This helper function adds the specified physical register to the
573/// MachineFunction as a live in value. It also creates a corresponding virtual
574/// register for it.
575static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
576 TargetRegisterClass *RC) {
577 assert(RC->contains(PReg) && "Not the correct regclass!");
578 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
579 MF.addLiveIn(PReg, VReg);
580 return VReg;
581}
582
583
584std::vector<SDOperand>
585X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
586 std::vector<SDOperand> ArgValues;
587
588 MachineFunction &MF = DAG.getMachineFunction();
589 MachineFrameInfo *MFI = MF.getFrameInfo();
590
591 // Add DAG nodes to load the arguments... On entry to a function the stack
592 // frame looks like this:
593 //
594 // [ESP] -- return address
595 // [ESP + 4] -- first nonreg argument (leftmost lexically)
596 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
597 // ...
598 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
599
600 // Keep track of the number of integer regs passed so far. This can be either
601 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
602 // used).
603 unsigned NumIntRegs = 0;
604
605 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
606 MVT::ValueType ObjectVT = getValueType(I->getType());
607 unsigned ArgIncrement = 4;
608 unsigned ObjSize = 0;
609 SDOperand ArgValue;
610
611 switch (ObjectVT) {
612 default: assert(0 && "Unhandled argument type!");
613 case MVT::i1:
614 case MVT::i8:
615 if (NumIntRegs < 2) {
616 if (!I->use_empty()) {
617 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
618 X86::R8RegisterClass);
619 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
620 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000621 if (ObjectVT == MVT::i1)
622 // FIXME: Should insert a assertzext here.
623 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000624 }
625 ++NumIntRegs;
626 break;
627 }
628
629 ObjSize = 1;
630 break;
631 case MVT::i16:
632 if (NumIntRegs < 2) {
633 if (!I->use_empty()) {
634 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
635 X86::R16RegisterClass);
636 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
637 DAG.setRoot(ArgValue.getValue(1));
638 }
639 ++NumIntRegs;
640 break;
641 }
642 ObjSize = 2;
643 break;
644 case MVT::i32:
645 if (NumIntRegs < 2) {
646 if (!I->use_empty()) {
647 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
648 X86::R32RegisterClass);
649 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
650 DAG.setRoot(ArgValue.getValue(1));
651 }
652 ++NumIntRegs;
653 break;
654 }
655 ObjSize = 4;
656 break;
657 case MVT::i64:
658 if (NumIntRegs == 0) {
659 if (!I->use_empty()) {
660 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
661 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
662
663 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
664 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
665 DAG.setRoot(Hi.getValue(1));
666
667 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
668 }
669 NumIntRegs = 2;
670 break;
671 } else if (NumIntRegs == 1) {
672 if (!I->use_empty()) {
673 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
674 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
675 DAG.setRoot(Low.getValue(1));
676
677 // Load the high part from memory.
678 // Create the frame index object for this incoming parameter...
679 int FI = MFI->CreateFixedObject(4, ArgOffset);
680 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
681 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
682 DAG.getSrcValue(NULL));
683 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
684 }
685 ArgOffset += 4;
686 NumIntRegs = 2;
687 break;
688 }
689 ObjSize = ArgIncrement = 8;
690 break;
691 case MVT::f32: ObjSize = 4; break;
692 case MVT::f64: ObjSize = ArgIncrement = 8; break;
693 }
694
695 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
696 // dead loads.
697 if (ObjSize && !I->use_empty()) {
698 // Create the frame index object for this incoming parameter...
699 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
700
701 // Create the SelectionDAG nodes corresponding to a load from this
702 // parameter.
703 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
704
705 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
706 DAG.getSrcValue(NULL));
707 } else if (ArgValue.Val == 0) {
708 if (MVT::isInteger(ObjectVT))
709 ArgValue = DAG.getConstant(0, ObjectVT);
710 else
711 ArgValue = DAG.getConstantFP(0, ObjectVT);
712 }
713 ArgValues.push_back(ArgValue);
714
715 if (ObjSize)
716 ArgOffset += ArgIncrement; // Move on to the next argument.
717 }
718
719 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
720 // arguments and the arguments after the retaddr has been pushed are aligned.
721 if ((ArgOffset & 7) == 0)
722 ArgOffset += 4;
723
724 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
725 ReturnAddrIndex = 0; // No return address slot generated yet.
726 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
727 BytesCallerReserves = 0;
728
729 // Finally, inform the code generator which regs we return values in.
730 switch (getValueType(F.getReturnType())) {
731 default: assert(0 && "Unknown type!");
732 case MVT::isVoid: break;
733 case MVT::i1:
734 case MVT::i8:
735 case MVT::i16:
736 case MVT::i32:
737 MF.addLiveOut(X86::EAX);
738 break;
739 case MVT::i64:
740 MF.addLiveOut(X86::EAX);
741 MF.addLiveOut(X86::EDX);
742 break;
743 case MVT::f32:
744 case MVT::f64:
745 MF.addLiveOut(X86::ST0);
746 break;
747 }
748 return ArgValues;
749}
750
751std::pair<SDOperand, SDOperand>
752X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
753 bool isTailCall, SDOperand Callee,
754 ArgListTy &Args, SelectionDAG &DAG) {
755 // Count how many bytes are to be pushed on the stack.
756 unsigned NumBytes = 0;
757
758 // Keep track of the number of integer regs passed so far. This can be either
759 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
760 // used).
761 unsigned NumIntRegs = 0;
762
763 for (unsigned i = 0, e = Args.size(); i != e; ++i)
764 switch (getValueType(Args[i].second)) {
765 default: assert(0 && "Unknown value type!");
766 case MVT::i1:
767 case MVT::i8:
768 case MVT::i16:
769 case MVT::i32:
770 if (NumIntRegs < 2) {
771 ++NumIntRegs;
772 break;
773 }
774 // fall through
775 case MVT::f32:
776 NumBytes += 4;
777 break;
778 case MVT::i64:
779 if (NumIntRegs == 0) {
780 NumIntRegs = 2;
781 break;
782 } else if (NumIntRegs == 1) {
783 NumIntRegs = 2;
784 NumBytes += 4;
785 break;
786 }
787
788 // fall through
789 case MVT::f64:
790 NumBytes += 8;
791 break;
792 }
793
794 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
795 // arguments and the arguments after the retaddr has been pushed are aligned.
796 if ((NumBytes & 7) == 0)
797 NumBytes += 4;
798
Chris Lattner62c34842006-02-13 09:00:43 +0000799 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000800
801 // Arguments go on the stack in reverse order, as specified by the ABI.
802 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000803 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000804 NumIntRegs = 0;
805 std::vector<SDOperand> Stores;
806 std::vector<SDOperand> RegValuesToPass;
807 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
808 switch (getValueType(Args[i].second)) {
809 default: assert(0 && "Unexpected ValueType for argument!");
810 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000811 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
812 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000813 case MVT::i8:
814 case MVT::i16:
815 case MVT::i32:
816 if (NumIntRegs < 2) {
817 RegValuesToPass.push_back(Args[i].first);
818 ++NumIntRegs;
819 break;
820 }
821 // Fall through
822 case MVT::f32: {
823 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
824 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
825 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
826 Args[i].first, PtrOff,
827 DAG.getSrcValue(NULL)));
828 ArgOffset += 4;
829 break;
830 }
831 case MVT::i64:
832 if (NumIntRegs < 2) { // Can pass part of it in regs?
833 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
834 Args[i].first, DAG.getConstant(1, MVT::i32));
835 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
836 Args[i].first, DAG.getConstant(0, MVT::i32));
837 RegValuesToPass.push_back(Lo);
838 ++NumIntRegs;
839 if (NumIntRegs < 2) { // Pass both parts in regs?
840 RegValuesToPass.push_back(Hi);
841 ++NumIntRegs;
842 } else {
843 // Pass the high part in memory.
844 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
845 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
846 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
847 Hi, PtrOff, DAG.getSrcValue(NULL)));
848 ArgOffset += 4;
849 }
850 break;
851 }
852 // Fall through
853 case MVT::f64:
854 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
855 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
856 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
857 Args[i].first, PtrOff,
858 DAG.getSrcValue(NULL)));
859 ArgOffset += 8;
860 break;
861 }
862 }
863 if (!Stores.empty())
864 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
865
866 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
867 // arguments and the arguments after the retaddr has been pushed are aligned.
868 if ((ArgOffset & 7) == 0)
869 ArgOffset += 4;
870
871 std::vector<MVT::ValueType> RetVals;
872 MVT::ValueType RetTyVT = getValueType(RetTy);
873
874 RetVals.push_back(MVT::Other);
875
876 // The result values produced have to be legal. Promote the result.
877 switch (RetTyVT) {
878 case MVT::isVoid: break;
879 default:
880 RetVals.push_back(RetTyVT);
881 break;
882 case MVT::i1:
883 case MVT::i8:
884 case MVT::i16:
885 RetVals.push_back(MVT::i32);
886 break;
887 case MVT::f32:
888 if (X86ScalarSSE)
889 RetVals.push_back(MVT::f32);
890 else
891 RetVals.push_back(MVT::f64);
892 break;
893 case MVT::i64:
894 RetVals.push_back(MVT::i32);
895 RetVals.push_back(MVT::i32);
896 break;
897 }
898
Nate Begeman7e5496d2006-02-17 00:03:04 +0000899 // Build a sequence of copy-to-reg nodes chained together with token chain
900 // and flag operands which copy the outgoing args into registers.
901 SDOperand InFlag;
902 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
903 unsigned CCReg;
904 SDOperand RegToPass = RegValuesToPass[i];
905 switch (RegToPass.getValueType()) {
906 default: assert(0 && "Bad thing to pass in regs");
907 case MVT::i8:
908 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +0000909 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000910 case MVT::i16:
911 CCReg = (i == 0) ? X86::AX : X86::DX;
912 break;
913 case MVT::i32:
914 CCReg = (i == 0) ? X86::EAX : X86::EDX;
915 break;
916 }
917
918 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
919 InFlag = Chain.getValue(1);
920 }
921
922 std::vector<MVT::ValueType> NodeTys;
923 NodeTys.push_back(MVT::Other); // Returns a chain
924 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
925 std::vector<SDOperand> Ops;
926 Ops.push_back(Chain);
927 Ops.push_back(Callee);
928 if (InFlag.Val)
929 Ops.push_back(InFlag);
930
931 // FIXME: Do not generate X86ISD::TAILCALL for now.
932 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
933 InFlag = Chain.getValue(1);
934
935 NodeTys.clear();
936 NodeTys.push_back(MVT::Other); // Returns a chain
937 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
938 Ops.clear();
939 Ops.push_back(Chain);
940 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
941 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
942 Ops.push_back(InFlag);
943 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
944 InFlag = Chain.getValue(1);
945
946 SDOperand RetVal;
947 if (RetTyVT != MVT::isVoid) {
948 switch (RetTyVT) {
949 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +0000950 case MVT::i1:
951 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000952 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
953 Chain = RetVal.getValue(1);
954 if (RetTyVT == MVT::i1)
955 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
956 break;
Evan Cheng172fce72006-01-06 00:43:03 +0000957 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000958 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
959 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +0000960 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000961 case MVT::i32:
962 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
963 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +0000964 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000965 case MVT::i64: {
966 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
967 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
968 Lo.getValue(2));
969 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
970 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +0000971 break;
972 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000973 case MVT::f32:
974 case MVT::f64: {
975 std::vector<MVT::ValueType> Tys;
976 Tys.push_back(MVT::f64);
977 Tys.push_back(MVT::Other);
978 Tys.push_back(MVT::Flag);
979 std::vector<SDOperand> Ops;
980 Ops.push_back(Chain);
981 Ops.push_back(InFlag);
982 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
983 Chain = RetVal.getValue(1);
984 InFlag = RetVal.getValue(2);
985 if (X86ScalarSSE) {
986 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
987 // shouldn't be necessary except that RFP cannot be live across
988 // multiple blocks. When stackifier is fixed, they can be uncoupled.
989 MachineFunction &MF = DAG.getMachineFunction();
990 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
991 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
992 Tys.clear();
993 Tys.push_back(MVT::Other);
994 Ops.clear();
995 Ops.push_back(Chain);
996 Ops.push_back(RetVal);
997 Ops.push_back(StackSlot);
998 Ops.push_back(DAG.getValueType(RetTyVT));
999 Ops.push_back(InFlag);
1000 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1001 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1002 DAG.getSrcValue(NULL));
1003 Chain = RetVal.getValue(1);
1004 }
Evan Cheng172fce72006-01-06 00:43:03 +00001005
Nate Begeman7e5496d2006-02-17 00:03:04 +00001006 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1007 // FIXME: we would really like to remember that this FP_ROUND
1008 // operation is okay to eliminate if we allow excess FP precision.
1009 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1010 break;
1011 }
1012 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001013 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001014
1015 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001016}
1017
1018SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1019 if (ReturnAddrIndex == 0) {
1020 // Set up a frame object for the return address.
1021 MachineFunction &MF = DAG.getMachineFunction();
1022 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1023 }
1024
1025 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1026}
1027
1028
1029
1030std::pair<SDOperand, SDOperand> X86TargetLowering::
1031LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1032 SelectionDAG &DAG) {
1033 SDOperand Result;
1034 if (Depth) // Depths > 0 not supported yet!
1035 Result = DAG.getConstant(0, getPointerTy());
1036 else {
1037 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1038 if (!isFrameAddress)
1039 // Just load the return address
1040 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1041 DAG.getSrcValue(NULL));
1042 else
1043 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1044 DAG.getConstant(4, MVT::i32));
1045 }
1046 return std::make_pair(Result, Chain);
1047}
1048
Evan Cheng339edad2006-01-11 00:33:36 +00001049/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1050/// which corresponds to the condition code.
1051static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1052 switch (X86CC) {
1053 default: assert(0 && "Unknown X86 conditional code!");
1054 case X86ISD::COND_A: return X86::JA;
1055 case X86ISD::COND_AE: return X86::JAE;
1056 case X86ISD::COND_B: return X86::JB;
1057 case X86ISD::COND_BE: return X86::JBE;
1058 case X86ISD::COND_E: return X86::JE;
1059 case X86ISD::COND_G: return X86::JG;
1060 case X86ISD::COND_GE: return X86::JGE;
1061 case X86ISD::COND_L: return X86::JL;
1062 case X86ISD::COND_LE: return X86::JLE;
1063 case X86ISD::COND_NE: return X86::JNE;
1064 case X86ISD::COND_NO: return X86::JNO;
1065 case X86ISD::COND_NP: return X86::JNP;
1066 case X86ISD::COND_NS: return X86::JNS;
1067 case X86ISD::COND_O: return X86::JO;
1068 case X86ISD::COND_P: return X86::JP;
1069 case X86ISD::COND_S: return X86::JS;
1070 }
1071}
Chris Lattner76ac0682005-11-15 00:40:23 +00001072
Evan Cheng45df7f82006-01-30 23:41:35 +00001073/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1074/// specific condition code. It returns a false if it cannot do a direct
1075/// translation. X86CC is the translated CondCode. Flip is set to true if the
1076/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001077static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1078 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001079 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001080 Flip = false;
1081 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001082 if (!isFP) {
1083 switch (SetCCOpcode) {
1084 default: break;
1085 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1086 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1087 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1088 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1089 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1090 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1091 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1092 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1093 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1094 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1095 }
1096 } else {
1097 // On a floating point condition, the flags are set as follows:
1098 // ZF PF CF op
1099 // 0 | 0 | 0 | X > Y
1100 // 0 | 0 | 1 | X < Y
1101 // 1 | 0 | 0 | X == Y
1102 // 1 | 1 | 1 | unordered
1103 switch (SetCCOpcode) {
1104 default: break;
1105 case ISD::SETUEQ:
1106 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001107 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001108 case ISD::SETOGT:
1109 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001110 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001111 case ISD::SETOGE:
1112 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001113 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001114 case ISD::SETULT:
1115 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001116 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001117 case ISD::SETULE:
1118 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1119 case ISD::SETONE:
1120 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1121 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1122 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1123 }
1124 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001125
1126 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001127}
1128
Evan Cheng339edad2006-01-11 00:33:36 +00001129/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1130/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001131/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001132static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001133 switch (X86CC) {
1134 default:
1135 return false;
1136 case X86ISD::COND_B:
1137 case X86ISD::COND_BE:
1138 case X86ISD::COND_E:
1139 case X86ISD::COND_P:
1140 case X86ISD::COND_A:
1141 case X86ISD::COND_AE:
1142 case X86ISD::COND_NE:
1143 case X86ISD::COND_NP:
1144 return true;
1145 }
1146}
1147
Evan Cheng339edad2006-01-11 00:33:36 +00001148MachineBasicBlock *
1149X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1150 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001151 switch (MI->getOpcode()) {
1152 default: assert(false && "Unexpected instr type to insert");
1153 case X86::CMOV_FR32:
1154 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001155 // To "insert" a SELECT_CC instruction, we actually have to insert the
1156 // diamond control-flow pattern. The incoming instruction knows the
1157 // destination vreg to set, the condition code register to branch on, the
1158 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001159 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1160 ilist<MachineBasicBlock>::iterator It = BB;
1161 ++It;
1162
1163 // thisMBB:
1164 // ...
1165 // TrueVal = ...
1166 // cmpTY ccX, r1, r2
1167 // bCC copy1MBB
1168 // fallthrough --> copy0MBB
1169 MachineBasicBlock *thisMBB = BB;
1170 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1171 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1172 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1173 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1174 MachineFunction *F = BB->getParent();
1175 F->getBasicBlockList().insert(It, copy0MBB);
1176 F->getBasicBlockList().insert(It, sinkMBB);
1177 // Update machine-CFG edges
1178 BB->addSuccessor(copy0MBB);
1179 BB->addSuccessor(sinkMBB);
1180
1181 // copy0MBB:
1182 // %FalseValue = ...
1183 // # fallthrough to sinkMBB
1184 BB = copy0MBB;
1185
1186 // Update machine-CFG edges
1187 BB->addSuccessor(sinkMBB);
1188
1189 // sinkMBB:
1190 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1191 // ...
1192 BB = sinkMBB;
1193 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1194 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1195 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001196
Evan Cheng911c68d2006-01-16 21:21:29 +00001197 delete MI; // The pseudo instruction is gone now.
1198 return BB;
1199 }
Evan Cheng339edad2006-01-11 00:33:36 +00001200
Evan Cheng911c68d2006-01-16 21:21:29 +00001201 case X86::FP_TO_INT16_IN_MEM:
1202 case X86::FP_TO_INT32_IN_MEM:
1203 case X86::FP_TO_INT64_IN_MEM: {
1204 // Change the floating point control register to use "round towards zero"
1205 // mode when truncating to an integer value.
1206 MachineFunction *F = BB->getParent();
1207 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1208 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1209
1210 // Load the old value of the high byte of the control word...
1211 unsigned OldCW =
1212 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1213 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1214
1215 // Set the high part to be round to zero...
1216 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1217
1218 // Reload the modified control word now...
1219 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1220
1221 // Restore the memory image of control word to original value
1222 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1223
1224 // Get the X86 opcode to use.
1225 unsigned Opc;
1226 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001227 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001228 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1229 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1230 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1231 }
1232
1233 X86AddressMode AM;
1234 MachineOperand &Op = MI->getOperand(0);
1235 if (Op.isRegister()) {
1236 AM.BaseType = X86AddressMode::RegBase;
1237 AM.Base.Reg = Op.getReg();
1238 } else {
1239 AM.BaseType = X86AddressMode::FrameIndexBase;
1240 AM.Base.FrameIndex = Op.getFrameIndex();
1241 }
1242 Op = MI->getOperand(1);
1243 if (Op.isImmediate())
1244 AM.Scale = Op.getImmedValue();
1245 Op = MI->getOperand(2);
1246 if (Op.isImmediate())
1247 AM.IndexReg = Op.getImmedValue();
1248 Op = MI->getOperand(3);
1249 if (Op.isGlobalAddress()) {
1250 AM.GV = Op.getGlobal();
1251 } else {
1252 AM.Disp = Op.getImmedValue();
1253 }
1254 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1255
1256 // Reload the original control word now.
1257 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1258
1259 delete MI; // The pseudo instruction is gone now.
1260 return BB;
1261 }
1262 }
Evan Cheng339edad2006-01-11 00:33:36 +00001263}
1264
1265
1266//===----------------------------------------------------------------------===//
1267// X86 Custom Lowering Hooks
1268//===----------------------------------------------------------------------===//
1269
Chris Lattner76ac0682005-11-15 00:40:23 +00001270/// LowerOperation - Provide custom lowering hooks for some operations.
1271///
1272SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1273 switch (Op.getOpcode()) {
1274 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001275 case ISD::SHL_PARTS:
1276 case ISD::SRA_PARTS:
1277 case ISD::SRL_PARTS: {
1278 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1279 "Not an i64 shift!");
1280 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1281 SDOperand ShOpLo = Op.getOperand(0);
1282 SDOperand ShOpHi = Op.getOperand(1);
1283 SDOperand ShAmt = Op.getOperand(2);
1284 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001285 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001286 : DAG.getConstant(0, MVT::i32);
1287
1288 SDOperand Tmp2, Tmp3;
1289 if (Op.getOpcode() == ISD::SHL_PARTS) {
1290 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1291 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1292 } else {
1293 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001294 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001295 }
1296
1297 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1298 ShAmt, DAG.getConstant(32, MVT::i8));
1299
1300 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001301 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001302
1303 std::vector<MVT::ValueType> Tys;
1304 Tys.push_back(MVT::i32);
1305 Tys.push_back(MVT::Flag);
1306 std::vector<SDOperand> Ops;
1307 if (Op.getOpcode() == ISD::SHL_PARTS) {
1308 Ops.push_back(Tmp2);
1309 Ops.push_back(Tmp3);
1310 Ops.push_back(CC);
1311 Ops.push_back(InFlag);
1312 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1313 InFlag = Hi.getValue(1);
1314
1315 Ops.clear();
1316 Ops.push_back(Tmp3);
1317 Ops.push_back(Tmp1);
1318 Ops.push_back(CC);
1319 Ops.push_back(InFlag);
1320 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1321 } else {
1322 Ops.push_back(Tmp2);
1323 Ops.push_back(Tmp3);
1324 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001325 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001326 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1327 InFlag = Lo.getValue(1);
1328
1329 Ops.clear();
1330 Ops.push_back(Tmp3);
1331 Ops.push_back(Tmp1);
1332 Ops.push_back(CC);
1333 Ops.push_back(InFlag);
1334 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1335 }
1336
1337 Tys.clear();
1338 Tys.push_back(MVT::i32);
1339 Tys.push_back(MVT::i32);
1340 Ops.clear();
1341 Ops.push_back(Lo);
1342 Ops.push_back(Hi);
1343 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1344 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001345 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001346 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001347 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001348 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001349
1350 SDOperand Result;
1351 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1352 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001353 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001354 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001355 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001356 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1357 DAG.getEntryNode(), Op.getOperand(0),
1358 StackSlot, DAG.getSrcValue(NULL));
1359
1360 // Build the FILD
1361 std::vector<MVT::ValueType> Tys;
1362 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001363 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001364 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001365 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001366 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001367 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001368 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001369 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1370 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001371
1372 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001373 Chain = Result.getValue(1);
1374 SDOperand InFlag = Result.getValue(2);
1375
Evan Cheng11613a52006-02-04 02:20:30 +00001376 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001377 // shouldn't be necessary except that RFP cannot be live across
1378 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1379 MachineFunction &MF = DAG.getMachineFunction();
1380 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1381 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1382 std::vector<MVT::ValueType> Tys;
1383 Tys.push_back(MVT::Other);
1384 std::vector<SDOperand> Ops;
1385 Ops.push_back(Chain);
1386 Ops.push_back(Result);
1387 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001388 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001389 Ops.push_back(InFlag);
1390 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1391 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1392 DAG.getSrcValue(NULL));
1393 }
1394
Evan Cheng6305e502006-01-12 22:54:21 +00001395 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001396 }
1397 case ISD::FP_TO_SINT: {
1398 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001399 "Unknown FP_TO_SINT to lower!");
1400 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1401 // stack slot.
1402 MachineFunction &MF = DAG.getMachineFunction();
1403 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1404 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1405 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1406
1407 unsigned Opc;
1408 switch (Op.getValueType()) {
1409 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1410 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1411 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1412 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1413 }
1414
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001415 SDOperand Chain = DAG.getEntryNode();
1416 SDOperand Value = Op.getOperand(0);
1417 if (X86ScalarSSE) {
1418 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1419 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1420 DAG.getSrcValue(0));
1421 std::vector<MVT::ValueType> Tys;
1422 Tys.push_back(MVT::f64);
1423 Tys.push_back(MVT::Other);
1424 std::vector<SDOperand> Ops;
1425 Ops.push_back(Chain);
1426 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001427 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001428 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1429 Chain = Value.getValue(1);
1430 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1431 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1432 }
1433
Chris Lattner76ac0682005-11-15 00:40:23 +00001434 // Build the FP_TO_INT*_IN_MEM
1435 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001436 Ops.push_back(Chain);
1437 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001438 Ops.push_back(StackSlot);
1439 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1440
1441 // Load the result.
1442 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1443 DAG.getSrcValue(NULL));
1444 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001445 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001446 std::vector<MVT::ValueType> Tys;
1447 Tys.push_back(MVT::Other);
1448 Tys.push_back(MVT::Flag);
1449 std::vector<SDOperand> Ops;
1450 Ops.push_back(Op.getOperand(0));
1451 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001452 Ops.clear();
1453 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1454 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1455 MVT::i32, Ops[0].getValue(2)));
1456 Ops.push_back(Ops[1].getValue(1));
1457 Tys[0] = Tys[1] = MVT::i32;
1458 Tys.push_back(MVT::Other);
1459 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001460 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001461 case ISD::FABS: {
1462 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00001463 const Type *OpNTy = MVT::getTypeForValueType(VT);
1464 std::vector<Constant*> CV;
1465 if (VT == MVT::f64) {
1466 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1467 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1468 } else {
1469 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1470 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1471 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1472 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1473 }
1474 Constant *CS = ConstantStruct::get(CV);
1475 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1476 SDOperand Mask
1477 = DAG.getNode(X86ISD::LOAD_PACK,
1478 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00001479 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1480 }
Evan Cheng72d5c252006-01-31 22:28:30 +00001481 case ISD::FNEG: {
1482 MVT::ValueType VT = Op.getValueType();
1483 const Type *OpNTy = MVT::getTypeForValueType(VT);
1484 std::vector<Constant*> CV;
1485 if (VT == MVT::f64) {
1486 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1487 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1488 } else {
1489 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1490 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1491 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1492 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1493 }
1494 Constant *CS = ConstantStruct::get(CV);
1495 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1496 SDOperand Mask
1497 = DAG.getNode(X86ISD::LOAD_PACK,
1498 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1499 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1500 }
Evan Chengc1583db2005-12-21 20:21:51 +00001501 case ISD::SETCC: {
1502 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001503 SDOperand Cond;
1504 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001505 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1506 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001507 bool Flip;
1508 unsigned X86CC;
1509 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1510 if (Flip)
1511 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1512 Op.getOperand(1), Op.getOperand(0));
1513 else
1514 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1515 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001516 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1517 DAG.getConstant(X86CC, MVT::i8), Cond);
1518 } else {
1519 assert(isFP && "Illegal integer SetCC!");
1520
Evan Cheng45df7f82006-01-30 23:41:35 +00001521 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1522 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001523 std::vector<MVT::ValueType> Tys;
1524 std::vector<SDOperand> Ops;
1525 switch (SetCCOpcode) {
1526 default: assert(false && "Illegal floating point SetCC!");
1527 case ISD::SETOEQ: { // !PF & ZF
1528 Tys.push_back(MVT::i8);
1529 Tys.push_back(MVT::Flag);
1530 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1531 Ops.push_back(Cond);
1532 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1533 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1534 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1535 Tmp1.getValue(1));
1536 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1537 }
Evan Cheng172fce72006-01-06 00:43:03 +00001538 case ISD::SETUNE: { // PF | !ZF
1539 Tys.push_back(MVT::i8);
1540 Tys.push_back(MVT::Flag);
1541 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1542 Ops.push_back(Cond);
1543 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1544 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1545 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1546 Tmp1.getValue(1));
1547 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1548 }
1549 }
1550 }
Evan Chengc1583db2005-12-21 20:21:51 +00001551 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001552 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001553 MVT::ValueType VT = Op.getValueType();
1554 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001555 bool isFPStack = isFP && !X86ScalarSSE;
1556 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001557 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001558 SDOperand Op0 = Op.getOperand(0);
1559 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001560 if (Op0.getOpcode() == ISD::SETCC)
1561 Op0 = LowerOperation(Op0, DAG);
1562
Evan Cheng73a1ad92006-01-10 20:26:56 +00001563 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001564 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1565 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1566 // have another use it will be eliminated.
1567 // If the X86ISD::SETCC has more than one use, then it's probably better
1568 // to use a test instead of duplicating the X86ISD::CMP (for register
1569 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001570 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1571 if (!Op0.hasOneUse()) {
1572 std::vector<MVT::ValueType> Tys;
1573 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1574 Tys.push_back(Op0.Val->getValueType(i));
1575 std::vector<SDOperand> Ops;
1576 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1577 Ops.push_back(Op0.getOperand(i));
1578 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1579 }
1580
Evan Chengfb22e862006-01-13 01:03:02 +00001581 CC = Op0.getOperand(0);
1582 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001583 // Make a copy as flag result cannot be used by more than one.
1584 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1585 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001586 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001587 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001588 } else
1589 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001590 } else
1591 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001592
Evan Cheng731423f2006-01-13 01:06:49 +00001593 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001594 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001595 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001596 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001597
1598 std::vector<MVT::ValueType> Tys;
1599 Tys.push_back(Op.getValueType());
1600 Tys.push_back(MVT::Flag);
1601 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001602 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1603 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001604 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001605 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001606 Ops.push_back(CC);
1607 Ops.push_back(Cond);
1608 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001609 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001610 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001611 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001612 SDOperand Cond = Op.getOperand(1);
1613 SDOperand Dest = Op.getOperand(2);
1614 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001615 if (Cond.getOpcode() == ISD::SETCC)
1616 Cond = LowerOperation(Cond, DAG);
1617
Evan Chengc1583db2005-12-21 20:21:51 +00001618 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001619 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1620 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1621 // have another use it will be eliminated.
1622 // If the X86ISD::SETCC has more than one use, then it's probably better
1623 // to use a test instead of duplicating the X86ISD::CMP (for register
1624 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001625 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1626 if (!Cond.hasOneUse()) {
1627 std::vector<MVT::ValueType> Tys;
1628 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1629 Tys.push_back(Cond.Val->getValueType(i));
1630 std::vector<SDOperand> Ops;
1631 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1632 Ops.push_back(Cond.getOperand(i));
1633 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1634 }
1635
Evan Chengfb22e862006-01-13 01:03:02 +00001636 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001637 Cond = Cond.getOperand(1);
1638 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001639 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001640 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001641 } else
1642 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001643 } else
1644 addTest = true;
1645
1646 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001647 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001648 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1649 }
1650 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1651 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1652 }
Evan Chengae986f12006-01-11 22:15:48 +00001653 case ISD::MEMSET: {
1654 SDOperand InFlag;
1655 SDOperand Chain = Op.getOperand(0);
1656 unsigned Align =
1657 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1658 if (Align == 0) Align = 1;
1659
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001660 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1661 // If not DWORD aligned, call memset if size is less than the threshold.
1662 // It knows how to align to the right boundary first.
1663 if ((Align & 3) != 0 &&
1664 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1665 MVT::ValueType IntPtr = getPointerTy();
1666 const Type *IntPtrTy = getTargetData().getIntPtrType();
1667 std::vector<std::pair<SDOperand, const Type*> > Args;
1668 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1669 // Extend the ubyte argument to be an int value for the call.
1670 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1671 Args.push_back(std::make_pair(Val, IntPtrTy));
1672 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1673 std::pair<SDOperand,SDOperand> CallResult =
1674 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1675 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1676 return CallResult.second;
1677 }
1678
Evan Chengae986f12006-01-11 22:15:48 +00001679 MVT::ValueType AVT;
1680 SDOperand Count;
1681 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1682 unsigned ValReg;
1683 unsigned Val = ValC->getValue() & 255;
1684
1685 // If the value is a constant, then we can potentially use larger sets.
1686 switch (Align & 3) {
1687 case 2: // WORD aligned
1688 AVT = MVT::i16;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001689 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001690 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1691 else
1692 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1693 DAG.getConstant(1, MVT::i8));
1694 Val = (Val << 8) | Val;
1695 ValReg = X86::AX;
1696 break;
1697 case 0: // DWORD aligned
1698 AVT = MVT::i32;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001699 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001700 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1701 else
1702 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1703 DAG.getConstant(2, MVT::i8));
1704 Val = (Val << 8) | Val;
1705 Val = (Val << 16) | Val;
1706 ValReg = X86::EAX;
1707 break;
1708 default: // Byte aligned
1709 AVT = MVT::i8;
1710 Count = Op.getOperand(3);
1711 ValReg = X86::AL;
1712 break;
1713 }
1714
1715 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1716 InFlag);
1717 InFlag = Chain.getValue(1);
1718 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001719 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00001720 Count = Op.getOperand(3);
1721 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1722 InFlag = Chain.getValue(1);
1723 }
1724
1725 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1726 InFlag = Chain.getValue(1);
1727 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1728 InFlag = Chain.getValue(1);
1729
1730 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1731 DAG.getValueType(AVT), InFlag);
1732 }
1733 case ISD::MEMCPY: {
1734 SDOperand Chain = Op.getOperand(0);
1735 unsigned Align =
1736 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1737 if (Align == 0) Align = 1;
1738
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001739 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1740 // If not DWORD aligned, call memcpy if size is less than the threshold.
1741 // It knows how to align to the right boundary first.
1742 if ((Align & 3) != 0 &&
1743 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1744 MVT::ValueType IntPtr = getPointerTy();
1745 const Type *IntPtrTy = getTargetData().getIntPtrType();
1746 std::vector<std::pair<SDOperand, const Type*> > Args;
1747 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1748 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1749 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1750 std::pair<SDOperand,SDOperand> CallResult =
1751 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1752 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1753 return CallResult.second;
1754 }
1755
Evan Chengae986f12006-01-11 22:15:48 +00001756 MVT::ValueType AVT;
1757 SDOperand Count;
1758 switch (Align & 3) {
1759 case 2: // WORD aligned
1760 AVT = MVT::i16;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001761 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001762 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1763 else
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001764 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
Evan Chengae986f12006-01-11 22:15:48 +00001765 break;
1766 case 0: // DWORD aligned
1767 AVT = MVT::i32;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001768 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001769 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1770 else
1771 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1772 DAG.getConstant(2, MVT::i8));
1773 break;
1774 default: // Byte aligned
1775 AVT = MVT::i8;
1776 Count = Op.getOperand(3);
1777 break;
1778 }
1779
1780 SDOperand InFlag;
1781 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1782 InFlag = Chain.getValue(1);
1783 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1784 InFlag = Chain.getValue(1);
1785 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1786 InFlag = Chain.getValue(1);
1787
1788 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1789 DAG.getValueType(AVT), InFlag);
1790 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001791 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001792 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001793 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1794 // For Darwin, external and weak symbols are indirect, so we want to load
1795 // the value at address GV, not the value of GV itself. This means that
1796 // the GlobalAddress must be in the base or index register of the address,
1797 // not the GV offset field.
1798 if (getTargetMachine().
Evan Cheng5a766802006-02-07 08:38:37 +00001799 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals()) {
1800 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1801 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
1802 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1803 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1804 DAG.getSrcValue(NULL));
1805 }
Evan Chengb94db9e2006-01-12 07:56:47 +00001806 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001807 }
Nate Begemane74795c2006-01-25 18:21:52 +00001808 case ISD::VASTART: {
1809 // vastart just stores the address of the VarArgsFrameIndex slot into the
1810 // memory location argument.
1811 // FIXME: Replace MVT::i32 with PointerTy
1812 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1813 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1814 Op.getOperand(1), Op.getOperand(2));
1815 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001816 case ISD::RET: {
1817 SDOperand Copy;
1818
1819 switch(Op.getNumOperands()) {
1820 default:
1821 assert(0 && "Do not know how to return this many arguments!");
1822 abort();
1823 case 1:
1824 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1825 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1826 case 2: {
1827 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1828 if (MVT::isInteger(ArgVT))
1829 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1830 SDOperand());
1831 else if (!X86ScalarSSE) {
1832 std::vector<MVT::ValueType> Tys;
1833 Tys.push_back(MVT::Other);
1834 Tys.push_back(MVT::Flag);
1835 std::vector<SDOperand> Ops;
1836 Ops.push_back(Op.getOperand(0));
1837 Ops.push_back(Op.getOperand(1));
1838 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1839 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00001840 SDOperand MemLoc;
1841 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00001842 SDOperand Value = Op.getOperand(1);
1843
Evan Chenga24617f2006-02-01 01:19:32 +00001844 if (Value.getOpcode() == ISD::LOAD &&
1845 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00001846 Chain = Value.getOperand(0);
1847 MemLoc = Value.getOperand(1);
1848 } else {
1849 // Spill the value to memory and reload it into top of stack.
1850 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1851 MachineFunction &MF = DAG.getMachineFunction();
1852 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1853 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1854 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1855 Value, MemLoc, DAG.getSrcValue(0));
1856 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001857 std::vector<MVT::ValueType> Tys;
1858 Tys.push_back(MVT::f64);
1859 Tys.push_back(MVT::Other);
1860 std::vector<SDOperand> Ops;
1861 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00001862 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001863 Ops.push_back(DAG.getValueType(ArgVT));
1864 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1865 Tys.clear();
1866 Tys.push_back(MVT::Other);
1867 Tys.push_back(MVT::Flag);
1868 Ops.clear();
1869 Ops.push_back(Copy.getValue(1));
1870 Ops.push_back(Copy);
1871 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1872 }
1873 break;
1874 }
1875 case 3:
1876 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1877 SDOperand());
1878 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1879 break;
1880 }
1881 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1882 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1883 Copy.getValue(1));
1884 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001885 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001886}
Evan Cheng6af02632005-12-20 06:22:03 +00001887
1888const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1889 switch (Opcode) {
1890 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001891 case X86ISD::SHLD: return "X86ISD::SHLD";
1892 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00001893 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00001894 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00001895 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00001896 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00001897 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1898 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1899 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001900 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001901 case X86ISD::FST: return "X86ISD::FST";
1902 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001903 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001904 case X86ISD::CALL: return "X86ISD::CALL";
1905 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1906 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1907 case X86ISD::CMP: return "X86ISD::CMP";
1908 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001909 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001910 case X86ISD::CMOV: return "X86ISD::CMOV";
1911 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001912 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001913 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1914 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00001915 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng6af02632005-12-20 06:22:03 +00001916 }
1917}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001918
Nate Begeman8a77efe2006-02-16 21:11:51 +00001919void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1920 uint64_t Mask,
1921 uint64_t &KnownZero,
1922 uint64_t &KnownOne,
1923 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001924
1925 unsigned Opc = Op.getOpcode();
Nate Begeman8a77efe2006-02-16 21:11:51 +00001926 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001927
1928 switch (Opc) {
1929 default:
1930 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1931 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00001932 case X86ISD::SETCC:
1933 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1934 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001935 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001936}
Chris Lattnerc642aa52006-01-31 19:43:35 +00001937
1938std::vector<unsigned> X86TargetLowering::
1939getRegForInlineAsmConstraint(const std::string &Constraint) const {
1940 if (Constraint.size() == 1) {
1941 // FIXME: not handling fp-stack yet!
1942 // FIXME: not handling MMX registers yet ('y' constraint).
1943 switch (Constraint[0]) { // GCC X86 Constraint Letters
1944 default: break; // Unknown constriant letter
1945 case 'r': // GENERAL_REGS
1946 case 'R': // LEGACY_REGS
1947 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1948 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
1949 case 'l': // INDEX_REGS
1950 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1951 X86::ESI, X86::EDI, X86::EBP, 0);
1952 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
1953 case 'Q': // Q_REGS
1954 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
1955 case 'x': // SSE_REGS if SSE1 allowed
1956 if (Subtarget->hasSSE1())
1957 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1958 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1959 0);
1960 return std::vector<unsigned>();
1961 case 'Y': // SSE_REGS if SSE2 allowed
1962 if (Subtarget->hasSSE2())
1963 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1964 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1965 0);
1966 return std::vector<unsigned>();
1967 }
1968 }
1969
1970 // Handle explicit register names.
1971 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
1972}