blob: 32c2a1fe81173088b5be754d3d4c9e3c7c94e731 [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);
Chris Lattner76ac0682005-11-15 00:40:23 +000076
Evan Cheng5b97fcf2006-01-30 08:02:57 +000077 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
78 // isn't legal.
79 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
80 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
81
Evan Cheng08390f62006-01-30 22:13:22 +000082 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
83 // this operation.
84 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
85 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
86
87 if (X86ScalarSSE) {
88 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
89 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +000090 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +000091 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +000092 }
93
94 // Handle FP_TO_UINT by promoting the destination to a larger signed
95 // conversion.
96 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
97 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
98 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
99
Evan Cheng08390f62006-01-30 22:13:22 +0000100 if (X86ScalarSSE)
101 // Expand FP_TO_UINT into a select.
102 // FIXME: We would like to use a Custom expander here eventually to do
103 // the optimal thing for SSE vs. the default expansion in the legalizer.
104 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
105 else
Chris Lattner76ac0682005-11-15 00:40:23 +0000106 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
107
Evan Cheng08390f62006-01-30 22:13:22 +0000108 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
109 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000110
Nate Begeman7e5496d2006-02-17 00:03:04 +0000111 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000112 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
113 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000114 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
115 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000116 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
120 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
121 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
122 setOperationAction(ISD::FREM , MVT::f64 , Expand);
123 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
124 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
125 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
126 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
127 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
128 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
129 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
130 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
131 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000132 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000133 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000134
Chris Lattner76ac0682005-11-15 00:40:23 +0000135 setOperationAction(ISD::READIO , MVT::i1 , Expand);
136 setOperationAction(ISD::READIO , MVT::i8 , Expand);
137 setOperationAction(ISD::READIO , MVT::i16 , Expand);
138 setOperationAction(ISD::READIO , MVT::i32 , Expand);
139 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
140 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
141 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
142 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
143
144 // These should be promoted to a larger select which is supported.
145 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
146 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000147
148 // X86 wants to expand cmov itself.
149 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
150 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
151 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
152 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
153 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
154 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
155 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
156 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
157 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
158 // X86 ret instruction may pop stack.
159 setOperationAction(ISD::RET , MVT::Other, Custom);
160 // Darwin ABI issue.
161 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
162 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
163 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
164 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
165 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
166 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
167 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
168 // X86 wants to expand memset / memcpy itself.
169 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
170 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000171
Chris Lattner9c415362005-11-29 06:16:21 +0000172 // We don't have line number support yet.
173 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000174 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
175 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000176
Nate Begemane74795c2006-01-25 18:21:52 +0000177 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
178 setOperationAction(ISD::VASTART , MVT::Other, Custom);
179
180 // Use the default implementation.
181 setOperationAction(ISD::VAARG , MVT::Other, Expand);
182 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
183 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000184 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
185 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
186 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000187
Chris Lattner76ac0682005-11-15 00:40:23 +0000188 if (X86ScalarSSE) {
189 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000190 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
191 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000192
193 // SSE has no load+extend ops
194 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
195 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
196
Evan Cheng72d5c252006-01-31 22:28:30 +0000197 // Use ANDPD to simulate FABS.
198 setOperationAction(ISD::FABS , MVT::f64, Custom);
199 setOperationAction(ISD::FABS , MVT::f32, Custom);
200
201 // Use XORP to simulate FNEG.
202 setOperationAction(ISD::FNEG , MVT::f64, Custom);
203 setOperationAction(ISD::FNEG , MVT::f32, Custom);
204
Evan Chengd8fba3a2006-02-02 00:28:23 +0000205 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000206 setOperationAction(ISD::FSIN , MVT::f64, Expand);
207 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000208 setOperationAction(ISD::FREM , MVT::f64, Expand);
209 setOperationAction(ISD::FSIN , MVT::f32, Expand);
210 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000211 setOperationAction(ISD::FREM , MVT::f32, Expand);
212
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000213 // Expand FP immediates into loads from the stack, except for the special
214 // cases we handle.
215 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
216 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000217 addLegalFPImmediate(+0.0); // xorps / xorpd
218 } else {
219 // Set up the FP register classes.
220 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000221
222 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
223
Chris Lattner76ac0682005-11-15 00:40:23 +0000224 if (!UnsafeFPMath) {
225 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
226 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
227 }
228
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000230 addLegalFPImmediate(+0.0); // FLD0
231 addLegalFPImmediate(+1.0); // FLD1
232 addLegalFPImmediate(-0.0); // FLD0/FCHS
233 addLegalFPImmediate(-1.0); // FLD1/FCHS
234 }
235 computeRegisterProperties();
236
Evan Cheng6a374562006-02-14 08:25:08 +0000237 // FIXME: These should be based on subtarget info. Plus, the values should
238 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000239 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
240 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
241 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000242 allowUnalignedMemoryAccesses = true; // x86 supports it!
243}
244
245std::vector<SDOperand>
246X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
247 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
248 return LowerFastCCArguments(F, DAG);
249 return LowerCCCArguments(F, DAG);
250}
251
252std::pair<SDOperand, SDOperand>
253X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
254 bool isVarArg, unsigned CallingConv,
255 bool isTailCall,
256 SDOperand Callee, ArgListTy &Args,
257 SelectionDAG &DAG) {
258 assert((!isVarArg || CallingConv == CallingConv::C) &&
259 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000260
261 // If the callee is a GlobalAddress node (quite common, every direct call is)
262 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
263 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
264 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000265 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
266 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000267
Chris Lattner76ac0682005-11-15 00:40:23 +0000268 if (CallingConv == CallingConv::Fast && EnableFastCC)
269 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
270 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
271}
272
273//===----------------------------------------------------------------------===//
274// C Calling Convention implementation
275//===----------------------------------------------------------------------===//
276
277std::vector<SDOperand>
278X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
279 std::vector<SDOperand> ArgValues;
280
281 MachineFunction &MF = DAG.getMachineFunction();
282 MachineFrameInfo *MFI = MF.getFrameInfo();
283
284 // Add DAG nodes to load the arguments... On entry to a function on the X86,
285 // the stack frame looks like this:
286 //
287 // [ESP] -- return address
288 // [ESP + 4] -- first argument (leftmost lexically)
289 // [ESP + 8] -- second argument, if first argument is four bytes in size
290 // ...
291 //
292 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
293 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
294 MVT::ValueType ObjectVT = getValueType(I->getType());
295 unsigned ArgIncrement = 4;
296 unsigned ObjSize;
297 switch (ObjectVT) {
298 default: assert(0 && "Unhandled argument type!");
299 case MVT::i1:
300 case MVT::i8: ObjSize = 1; break;
301 case MVT::i16: ObjSize = 2; break;
302 case MVT::i32: ObjSize = 4; break;
303 case MVT::i64: ObjSize = ArgIncrement = 8; break;
304 case MVT::f32: ObjSize = 4; break;
305 case MVT::f64: ObjSize = ArgIncrement = 8; break;
306 }
307 // Create the frame index object for this incoming parameter...
308 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
309
310 // Create the SelectionDAG nodes corresponding to a load from this parameter
311 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
312
313 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
314 // dead loads.
315 SDOperand ArgValue;
316 if (!I->use_empty())
317 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
318 DAG.getSrcValue(NULL));
319 else {
320 if (MVT::isInteger(ObjectVT))
321 ArgValue = DAG.getConstant(0, ObjectVT);
322 else
323 ArgValue = DAG.getConstantFP(0, ObjectVT);
324 }
325 ArgValues.push_back(ArgValue);
326
327 ArgOffset += ArgIncrement; // Move on to the next argument...
328 }
329
330 // If the function takes variable number of arguments, make a frame index for
331 // the start of the first vararg value... for expansion of llvm.va_start.
332 if (F.isVarArg())
333 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
334 ReturnAddrIndex = 0; // No return address slot generated yet.
335 BytesToPopOnReturn = 0; // Callee pops nothing.
336 BytesCallerReserves = ArgOffset;
337
338 // Finally, inform the code generator which regs we return values in.
339 switch (getValueType(F.getReturnType())) {
340 default: assert(0 && "Unknown type!");
341 case MVT::isVoid: break;
342 case MVT::i1:
343 case MVT::i8:
344 case MVT::i16:
345 case MVT::i32:
346 MF.addLiveOut(X86::EAX);
347 break;
348 case MVT::i64:
349 MF.addLiveOut(X86::EAX);
350 MF.addLiveOut(X86::EDX);
351 break;
352 case MVT::f32:
353 case MVT::f64:
354 MF.addLiveOut(X86::ST0);
355 break;
356 }
357 return ArgValues;
358}
359
360std::pair<SDOperand, SDOperand>
361X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
362 bool isVarArg, bool isTailCall,
363 SDOperand Callee, ArgListTy &Args,
364 SelectionDAG &DAG) {
365 // Count how many bytes are to be pushed on the stack.
366 unsigned NumBytes = 0;
367
368 if (Args.empty()) {
369 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000370 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000371 } else {
372 for (unsigned i = 0, e = Args.size(); i != e; ++i)
373 switch (getValueType(Args[i].second)) {
374 default: assert(0 && "Unknown value type!");
375 case MVT::i1:
376 case MVT::i8:
377 case MVT::i16:
378 case MVT::i32:
379 case MVT::f32:
380 NumBytes += 4;
381 break;
382 case MVT::i64:
383 case MVT::f64:
384 NumBytes += 8;
385 break;
386 }
387
Chris Lattner62c34842006-02-13 09:00:43 +0000388 Chain = DAG.getCALLSEQ_START(Chain,
389 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000390
391 // Arguments go on the stack in reverse order, as specified by the ABI.
392 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000393 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000394 std::vector<SDOperand> Stores;
395
396 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
397 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
398 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
399
400 switch (getValueType(Args[i].second)) {
401 default: assert(0 && "Unexpected ValueType for argument!");
402 case MVT::i1:
403 case MVT::i8:
404 case MVT::i16:
405 // Promote the integer to 32 bits. If the input type is signed use a
406 // sign extend, otherwise use a zero extend.
407 if (Args[i].second->isSigned())
408 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
409 else
410 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
411
412 // FALL THROUGH
413 case MVT::i32:
414 case MVT::f32:
415 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
416 Args[i].first, PtrOff,
417 DAG.getSrcValue(NULL)));
418 ArgOffset += 4;
419 break;
420 case MVT::i64:
421 case MVT::f64:
422 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
423 Args[i].first, PtrOff,
424 DAG.getSrcValue(NULL)));
425 ArgOffset += 8;
426 break;
427 }
428 }
429 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
430 }
431
432 std::vector<MVT::ValueType> RetVals;
433 MVT::ValueType RetTyVT = getValueType(RetTy);
434 RetVals.push_back(MVT::Other);
435
436 // The result values produced have to be legal. Promote the result.
437 switch (RetTyVT) {
438 case MVT::isVoid: break;
439 default:
440 RetVals.push_back(RetTyVT);
441 break;
442 case MVT::i1:
443 case MVT::i8:
444 case MVT::i16:
445 RetVals.push_back(MVT::i32);
446 break;
447 case MVT::f32:
448 if (X86ScalarSSE)
449 RetVals.push_back(MVT::f32);
450 else
451 RetVals.push_back(MVT::f64);
452 break;
453 case MVT::i64:
454 RetVals.push_back(MVT::i32);
455 RetVals.push_back(MVT::i32);
456 break;
457 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000458
Nate Begeman7e5496d2006-02-17 00:03:04 +0000459 std::vector<MVT::ValueType> NodeTys;
460 NodeTys.push_back(MVT::Other); // Returns a chain
461 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
462 std::vector<SDOperand> Ops;
463 Ops.push_back(Chain);
464 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000465
Nate Begeman7e5496d2006-02-17 00:03:04 +0000466 // FIXME: Do not generate X86ISD::TAILCALL for now.
467 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
468 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000469
Nate Begeman7e5496d2006-02-17 00:03:04 +0000470 NodeTys.clear();
471 NodeTys.push_back(MVT::Other); // Returns a chain
472 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
473 Ops.clear();
474 Ops.push_back(Chain);
475 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
476 Ops.push_back(DAG.getConstant(0, getPointerTy()));
477 Ops.push_back(InFlag);
478 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
479 InFlag = Chain.getValue(1);
480
481 SDOperand RetVal;
482 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000483 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000484 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000485 case MVT::i1:
486 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000487 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
488 Chain = RetVal.getValue(1);
489 if (RetTyVT == MVT::i1)
490 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
491 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000492 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000493 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
494 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000495 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000496 case MVT::i32:
497 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
498 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000499 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000500 case MVT::i64: {
501 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
502 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
503 Lo.getValue(2));
504 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
505 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000506 break;
507 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000508 case MVT::f32:
509 case MVT::f64: {
510 std::vector<MVT::ValueType> Tys;
511 Tys.push_back(MVT::f64);
512 Tys.push_back(MVT::Other);
513 Tys.push_back(MVT::Flag);
514 std::vector<SDOperand> Ops;
515 Ops.push_back(Chain);
516 Ops.push_back(InFlag);
517 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
518 Chain = RetVal.getValue(1);
519 InFlag = RetVal.getValue(2);
520 if (X86ScalarSSE) {
521 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
522 // shouldn't be necessary except that RFP cannot be live across
523 // multiple blocks. When stackifier is fixed, they can be uncoupled.
524 MachineFunction &MF = DAG.getMachineFunction();
525 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
526 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
527 Tys.clear();
528 Tys.push_back(MVT::Other);
529 Ops.clear();
530 Ops.push_back(Chain);
531 Ops.push_back(RetVal);
532 Ops.push_back(StackSlot);
533 Ops.push_back(DAG.getValueType(RetTyVT));
534 Ops.push_back(InFlag);
535 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
536 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
537 DAG.getSrcValue(NULL));
538 Chain = RetVal.getValue(1);
539 }
Evan Cheng45e190982006-01-05 00:27:02 +0000540
Nate Begeman7e5496d2006-02-17 00:03:04 +0000541 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
542 // FIXME: we would really like to remember that this FP_ROUND
543 // operation is okay to eliminate if we allow excess FP precision.
544 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
545 break;
546 }
547 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000548 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000549
550 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000551}
552
Chris Lattner76ac0682005-11-15 00:40:23 +0000553//===----------------------------------------------------------------------===//
554// Fast Calling Convention implementation
555//===----------------------------------------------------------------------===//
556//
557// The X86 'fast' calling convention passes up to two integer arguments in
558// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
559// and requires that the callee pop its arguments off the stack (allowing proper
560// tail calls), and has the same return value conventions as C calling convs.
561//
562// This calling convention always arranges for the callee pop value to be 8n+4
563// bytes, which is needed for tail recursion elimination and stack alignment
564// reasons.
565//
566// Note that this can be enhanced in the future to pass fp vals in registers
567// (when we have a global fp allocator) and do other tricks.
568//
569
570/// AddLiveIn - This helper function adds the specified physical register to the
571/// MachineFunction as a live in value. It also creates a corresponding virtual
572/// register for it.
573static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
574 TargetRegisterClass *RC) {
575 assert(RC->contains(PReg) && "Not the correct regclass!");
576 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
577 MF.addLiveIn(PReg, VReg);
578 return VReg;
579}
580
581
582std::vector<SDOperand>
583X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
584 std::vector<SDOperand> ArgValues;
585
586 MachineFunction &MF = DAG.getMachineFunction();
587 MachineFrameInfo *MFI = MF.getFrameInfo();
588
589 // Add DAG nodes to load the arguments... On entry to a function the stack
590 // frame looks like this:
591 //
592 // [ESP] -- return address
593 // [ESP + 4] -- first nonreg argument (leftmost lexically)
594 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
595 // ...
596 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
597
598 // Keep track of the number of integer regs passed so far. This can be either
599 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
600 // used).
601 unsigned NumIntRegs = 0;
602
603 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
604 MVT::ValueType ObjectVT = getValueType(I->getType());
605 unsigned ArgIncrement = 4;
606 unsigned ObjSize = 0;
607 SDOperand ArgValue;
608
609 switch (ObjectVT) {
610 default: assert(0 && "Unhandled argument type!");
611 case MVT::i1:
612 case MVT::i8:
613 if (NumIntRegs < 2) {
614 if (!I->use_empty()) {
615 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
616 X86::R8RegisterClass);
617 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
618 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000619 if (ObjectVT == MVT::i1)
620 // FIXME: Should insert a assertzext here.
621 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000622 }
623 ++NumIntRegs;
624 break;
625 }
626
627 ObjSize = 1;
628 break;
629 case MVT::i16:
630 if (NumIntRegs < 2) {
631 if (!I->use_empty()) {
632 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
633 X86::R16RegisterClass);
634 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
635 DAG.setRoot(ArgValue.getValue(1));
636 }
637 ++NumIntRegs;
638 break;
639 }
640 ObjSize = 2;
641 break;
642 case MVT::i32:
643 if (NumIntRegs < 2) {
644 if (!I->use_empty()) {
645 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
646 X86::R32RegisterClass);
647 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
648 DAG.setRoot(ArgValue.getValue(1));
649 }
650 ++NumIntRegs;
651 break;
652 }
653 ObjSize = 4;
654 break;
655 case MVT::i64:
656 if (NumIntRegs == 0) {
657 if (!I->use_empty()) {
658 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
659 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
660
661 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
662 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
663 DAG.setRoot(Hi.getValue(1));
664
665 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
666 }
667 NumIntRegs = 2;
668 break;
669 } else if (NumIntRegs == 1) {
670 if (!I->use_empty()) {
671 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
672 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
673 DAG.setRoot(Low.getValue(1));
674
675 // Load the high part from memory.
676 // Create the frame index object for this incoming parameter...
677 int FI = MFI->CreateFixedObject(4, ArgOffset);
678 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
679 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
680 DAG.getSrcValue(NULL));
681 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
682 }
683 ArgOffset += 4;
684 NumIntRegs = 2;
685 break;
686 }
687 ObjSize = ArgIncrement = 8;
688 break;
689 case MVT::f32: ObjSize = 4; break;
690 case MVT::f64: ObjSize = ArgIncrement = 8; break;
691 }
692
693 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
694 // dead loads.
695 if (ObjSize && !I->use_empty()) {
696 // Create the frame index object for this incoming parameter...
697 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
698
699 // Create the SelectionDAG nodes corresponding to a load from this
700 // parameter.
701 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
702
703 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
704 DAG.getSrcValue(NULL));
705 } else if (ArgValue.Val == 0) {
706 if (MVT::isInteger(ObjectVT))
707 ArgValue = DAG.getConstant(0, ObjectVT);
708 else
709 ArgValue = DAG.getConstantFP(0, ObjectVT);
710 }
711 ArgValues.push_back(ArgValue);
712
713 if (ObjSize)
714 ArgOffset += ArgIncrement; // Move on to the next argument.
715 }
716
717 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
718 // arguments and the arguments after the retaddr has been pushed are aligned.
719 if ((ArgOffset & 7) == 0)
720 ArgOffset += 4;
721
722 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
723 ReturnAddrIndex = 0; // No return address slot generated yet.
724 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
725 BytesCallerReserves = 0;
726
727 // Finally, inform the code generator which regs we return values in.
728 switch (getValueType(F.getReturnType())) {
729 default: assert(0 && "Unknown type!");
730 case MVT::isVoid: break;
731 case MVT::i1:
732 case MVT::i8:
733 case MVT::i16:
734 case MVT::i32:
735 MF.addLiveOut(X86::EAX);
736 break;
737 case MVT::i64:
738 MF.addLiveOut(X86::EAX);
739 MF.addLiveOut(X86::EDX);
740 break;
741 case MVT::f32:
742 case MVT::f64:
743 MF.addLiveOut(X86::ST0);
744 break;
745 }
746 return ArgValues;
747}
748
749std::pair<SDOperand, SDOperand>
750X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
751 bool isTailCall, SDOperand Callee,
752 ArgListTy &Args, SelectionDAG &DAG) {
753 // Count how many bytes are to be pushed on the stack.
754 unsigned NumBytes = 0;
755
756 // Keep track of the number of integer regs passed so far. This can be either
757 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
758 // used).
759 unsigned NumIntRegs = 0;
760
761 for (unsigned i = 0, e = Args.size(); i != e; ++i)
762 switch (getValueType(Args[i].second)) {
763 default: assert(0 && "Unknown value type!");
764 case MVT::i1:
765 case MVT::i8:
766 case MVT::i16:
767 case MVT::i32:
768 if (NumIntRegs < 2) {
769 ++NumIntRegs;
770 break;
771 }
772 // fall through
773 case MVT::f32:
774 NumBytes += 4;
775 break;
776 case MVT::i64:
777 if (NumIntRegs == 0) {
778 NumIntRegs = 2;
779 break;
780 } else if (NumIntRegs == 1) {
781 NumIntRegs = 2;
782 NumBytes += 4;
783 break;
784 }
785
786 // fall through
787 case MVT::f64:
788 NumBytes += 8;
789 break;
790 }
791
792 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
793 // arguments and the arguments after the retaddr has been pushed are aligned.
794 if ((NumBytes & 7) == 0)
795 NumBytes += 4;
796
Chris Lattner62c34842006-02-13 09:00:43 +0000797 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000798
799 // Arguments go on the stack in reverse order, as specified by the ABI.
800 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000801 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000802 NumIntRegs = 0;
803 std::vector<SDOperand> Stores;
804 std::vector<SDOperand> RegValuesToPass;
805 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
806 switch (getValueType(Args[i].second)) {
807 default: assert(0 && "Unexpected ValueType for argument!");
808 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000809 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
810 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000811 case MVT::i8:
812 case MVT::i16:
813 case MVT::i32:
814 if (NumIntRegs < 2) {
815 RegValuesToPass.push_back(Args[i].first);
816 ++NumIntRegs;
817 break;
818 }
819 // Fall through
820 case MVT::f32: {
821 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
822 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
823 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
824 Args[i].first, PtrOff,
825 DAG.getSrcValue(NULL)));
826 ArgOffset += 4;
827 break;
828 }
829 case MVT::i64:
830 if (NumIntRegs < 2) { // Can pass part of it in regs?
831 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
832 Args[i].first, DAG.getConstant(1, MVT::i32));
833 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
834 Args[i].first, DAG.getConstant(0, MVT::i32));
835 RegValuesToPass.push_back(Lo);
836 ++NumIntRegs;
837 if (NumIntRegs < 2) { // Pass both parts in regs?
838 RegValuesToPass.push_back(Hi);
839 ++NumIntRegs;
840 } else {
841 // Pass the high part in memory.
842 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
843 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
844 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
845 Hi, PtrOff, DAG.getSrcValue(NULL)));
846 ArgOffset += 4;
847 }
848 break;
849 }
850 // Fall through
851 case MVT::f64:
852 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
853 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
854 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
855 Args[i].first, PtrOff,
856 DAG.getSrcValue(NULL)));
857 ArgOffset += 8;
858 break;
859 }
860 }
861 if (!Stores.empty())
862 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
863
864 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
865 // arguments and the arguments after the retaddr has been pushed are aligned.
866 if ((ArgOffset & 7) == 0)
867 ArgOffset += 4;
868
869 std::vector<MVT::ValueType> RetVals;
870 MVT::ValueType RetTyVT = getValueType(RetTy);
871
872 RetVals.push_back(MVT::Other);
873
874 // The result values produced have to be legal. Promote the result.
875 switch (RetTyVT) {
876 case MVT::isVoid: break;
877 default:
878 RetVals.push_back(RetTyVT);
879 break;
880 case MVT::i1:
881 case MVT::i8:
882 case MVT::i16:
883 RetVals.push_back(MVT::i32);
884 break;
885 case MVT::f32:
886 if (X86ScalarSSE)
887 RetVals.push_back(MVT::f32);
888 else
889 RetVals.push_back(MVT::f64);
890 break;
891 case MVT::i64:
892 RetVals.push_back(MVT::i32);
893 RetVals.push_back(MVT::i32);
894 break;
895 }
896
Nate Begeman7e5496d2006-02-17 00:03:04 +0000897 // Build a sequence of copy-to-reg nodes chained together with token chain
898 // and flag operands which copy the outgoing args into registers.
899 SDOperand InFlag;
900 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
901 unsigned CCReg;
902 SDOperand RegToPass = RegValuesToPass[i];
903 switch (RegToPass.getValueType()) {
904 default: assert(0 && "Bad thing to pass in regs");
905 case MVT::i8:
906 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +0000907 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000908 case MVT::i16:
909 CCReg = (i == 0) ? X86::AX : X86::DX;
910 break;
911 case MVT::i32:
912 CCReg = (i == 0) ? X86::EAX : X86::EDX;
913 break;
914 }
915
916 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
917 InFlag = Chain.getValue(1);
918 }
919
920 std::vector<MVT::ValueType> NodeTys;
921 NodeTys.push_back(MVT::Other); // Returns a chain
922 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
923 std::vector<SDOperand> Ops;
924 Ops.push_back(Chain);
925 Ops.push_back(Callee);
926 if (InFlag.Val)
927 Ops.push_back(InFlag);
928
929 // FIXME: Do not generate X86ISD::TAILCALL for now.
930 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
931 InFlag = Chain.getValue(1);
932
933 NodeTys.clear();
934 NodeTys.push_back(MVT::Other); // Returns a chain
935 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
936 Ops.clear();
937 Ops.push_back(Chain);
938 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
939 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
940 Ops.push_back(InFlag);
941 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
942 InFlag = Chain.getValue(1);
943
944 SDOperand RetVal;
945 if (RetTyVT != MVT::isVoid) {
946 switch (RetTyVT) {
947 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +0000948 case MVT::i1:
949 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000950 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
951 Chain = RetVal.getValue(1);
952 if (RetTyVT == MVT::i1)
953 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
954 break;
Evan Cheng172fce72006-01-06 00:43:03 +0000955 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000956 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
957 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +0000958 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000959 case MVT::i32:
960 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
961 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +0000962 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000963 case MVT::i64: {
964 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
965 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
966 Lo.getValue(2));
967 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
968 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +0000969 break;
970 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000971 case MVT::f32:
972 case MVT::f64: {
973 std::vector<MVT::ValueType> Tys;
974 Tys.push_back(MVT::f64);
975 Tys.push_back(MVT::Other);
976 Tys.push_back(MVT::Flag);
977 std::vector<SDOperand> Ops;
978 Ops.push_back(Chain);
979 Ops.push_back(InFlag);
980 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
981 Chain = RetVal.getValue(1);
982 InFlag = RetVal.getValue(2);
983 if (X86ScalarSSE) {
984 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
985 // shouldn't be necessary except that RFP cannot be live across
986 // multiple blocks. When stackifier is fixed, they can be uncoupled.
987 MachineFunction &MF = DAG.getMachineFunction();
988 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
989 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
990 Tys.clear();
991 Tys.push_back(MVT::Other);
992 Ops.clear();
993 Ops.push_back(Chain);
994 Ops.push_back(RetVal);
995 Ops.push_back(StackSlot);
996 Ops.push_back(DAG.getValueType(RetTyVT));
997 Ops.push_back(InFlag);
998 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
999 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1000 DAG.getSrcValue(NULL));
1001 Chain = RetVal.getValue(1);
1002 }
Evan Cheng172fce72006-01-06 00:43:03 +00001003
Nate Begeman7e5496d2006-02-17 00:03:04 +00001004 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1005 // FIXME: we would really like to remember that this FP_ROUND
1006 // operation is okay to eliminate if we allow excess FP precision.
1007 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1008 break;
1009 }
1010 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001011 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001012
1013 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001014}
1015
1016SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1017 if (ReturnAddrIndex == 0) {
1018 // Set up a frame object for the return address.
1019 MachineFunction &MF = DAG.getMachineFunction();
1020 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1021 }
1022
1023 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1024}
1025
1026
1027
1028std::pair<SDOperand, SDOperand> X86TargetLowering::
1029LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1030 SelectionDAG &DAG) {
1031 SDOperand Result;
1032 if (Depth) // Depths > 0 not supported yet!
1033 Result = DAG.getConstant(0, getPointerTy());
1034 else {
1035 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1036 if (!isFrameAddress)
1037 // Just load the return address
1038 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1039 DAG.getSrcValue(NULL));
1040 else
1041 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1042 DAG.getConstant(4, MVT::i32));
1043 }
1044 return std::make_pair(Result, Chain);
1045}
1046
Evan Cheng339edad2006-01-11 00:33:36 +00001047/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1048/// which corresponds to the condition code.
1049static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1050 switch (X86CC) {
1051 default: assert(0 && "Unknown X86 conditional code!");
1052 case X86ISD::COND_A: return X86::JA;
1053 case X86ISD::COND_AE: return X86::JAE;
1054 case X86ISD::COND_B: return X86::JB;
1055 case X86ISD::COND_BE: return X86::JBE;
1056 case X86ISD::COND_E: return X86::JE;
1057 case X86ISD::COND_G: return X86::JG;
1058 case X86ISD::COND_GE: return X86::JGE;
1059 case X86ISD::COND_L: return X86::JL;
1060 case X86ISD::COND_LE: return X86::JLE;
1061 case X86ISD::COND_NE: return X86::JNE;
1062 case X86ISD::COND_NO: return X86::JNO;
1063 case X86ISD::COND_NP: return X86::JNP;
1064 case X86ISD::COND_NS: return X86::JNS;
1065 case X86ISD::COND_O: return X86::JO;
1066 case X86ISD::COND_P: return X86::JP;
1067 case X86ISD::COND_S: return X86::JS;
1068 }
1069}
Chris Lattner76ac0682005-11-15 00:40:23 +00001070
Evan Cheng45df7f82006-01-30 23:41:35 +00001071/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1072/// specific condition code. It returns a false if it cannot do a direct
1073/// translation. X86CC is the translated CondCode. Flip is set to true if the
1074/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001075static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1076 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001077 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001078 Flip = false;
1079 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001080 if (!isFP) {
1081 switch (SetCCOpcode) {
1082 default: break;
1083 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1084 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1085 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1086 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1087 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1088 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1089 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1090 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1091 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1092 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1093 }
1094 } else {
1095 // On a floating point condition, the flags are set as follows:
1096 // ZF PF CF op
1097 // 0 | 0 | 0 | X > Y
1098 // 0 | 0 | 1 | X < Y
1099 // 1 | 0 | 0 | X == Y
1100 // 1 | 1 | 1 | unordered
1101 switch (SetCCOpcode) {
1102 default: break;
1103 case ISD::SETUEQ:
1104 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001105 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001106 case ISD::SETOGT:
1107 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001108 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001109 case ISD::SETOGE:
1110 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001111 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001112 case ISD::SETULT:
1113 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001114 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001115 case ISD::SETULE:
1116 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1117 case ISD::SETONE:
1118 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1119 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1120 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1121 }
1122 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001123
1124 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001125}
1126
Evan Cheng339edad2006-01-11 00:33:36 +00001127/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1128/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001129/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001130static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001131 switch (X86CC) {
1132 default:
1133 return false;
1134 case X86ISD::COND_B:
1135 case X86ISD::COND_BE:
1136 case X86ISD::COND_E:
1137 case X86ISD::COND_P:
1138 case X86ISD::COND_A:
1139 case X86ISD::COND_AE:
1140 case X86ISD::COND_NE:
1141 case X86ISD::COND_NP:
1142 return true;
1143 }
1144}
1145
Evan Cheng339edad2006-01-11 00:33:36 +00001146MachineBasicBlock *
1147X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1148 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001149 switch (MI->getOpcode()) {
1150 default: assert(false && "Unexpected instr type to insert");
1151 case X86::CMOV_FR32:
1152 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001153 // To "insert" a SELECT_CC instruction, we actually have to insert the
1154 // diamond control-flow pattern. The incoming instruction knows the
1155 // destination vreg to set, the condition code register to branch on, the
1156 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001157 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1158 ilist<MachineBasicBlock>::iterator It = BB;
1159 ++It;
1160
1161 // thisMBB:
1162 // ...
1163 // TrueVal = ...
1164 // cmpTY ccX, r1, r2
1165 // bCC copy1MBB
1166 // fallthrough --> copy0MBB
1167 MachineBasicBlock *thisMBB = BB;
1168 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1169 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1170 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1171 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1172 MachineFunction *F = BB->getParent();
1173 F->getBasicBlockList().insert(It, copy0MBB);
1174 F->getBasicBlockList().insert(It, sinkMBB);
1175 // Update machine-CFG edges
1176 BB->addSuccessor(copy0MBB);
1177 BB->addSuccessor(sinkMBB);
1178
1179 // copy0MBB:
1180 // %FalseValue = ...
1181 // # fallthrough to sinkMBB
1182 BB = copy0MBB;
1183
1184 // Update machine-CFG edges
1185 BB->addSuccessor(sinkMBB);
1186
1187 // sinkMBB:
1188 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1189 // ...
1190 BB = sinkMBB;
1191 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1192 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1193 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001194
Evan Cheng911c68d2006-01-16 21:21:29 +00001195 delete MI; // The pseudo instruction is gone now.
1196 return BB;
1197 }
Evan Cheng339edad2006-01-11 00:33:36 +00001198
Evan Cheng911c68d2006-01-16 21:21:29 +00001199 case X86::FP_TO_INT16_IN_MEM:
1200 case X86::FP_TO_INT32_IN_MEM:
1201 case X86::FP_TO_INT64_IN_MEM: {
1202 // Change the floating point control register to use "round towards zero"
1203 // mode when truncating to an integer value.
1204 MachineFunction *F = BB->getParent();
1205 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1206 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1207
1208 // Load the old value of the high byte of the control word...
1209 unsigned OldCW =
1210 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1211 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1212
1213 // Set the high part to be round to zero...
1214 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1215
1216 // Reload the modified control word now...
1217 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1218
1219 // Restore the memory image of control word to original value
1220 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1221
1222 // Get the X86 opcode to use.
1223 unsigned Opc;
1224 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001225 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001226 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1227 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1228 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1229 }
1230
1231 X86AddressMode AM;
1232 MachineOperand &Op = MI->getOperand(0);
1233 if (Op.isRegister()) {
1234 AM.BaseType = X86AddressMode::RegBase;
1235 AM.Base.Reg = Op.getReg();
1236 } else {
1237 AM.BaseType = X86AddressMode::FrameIndexBase;
1238 AM.Base.FrameIndex = Op.getFrameIndex();
1239 }
1240 Op = MI->getOperand(1);
1241 if (Op.isImmediate())
1242 AM.Scale = Op.getImmedValue();
1243 Op = MI->getOperand(2);
1244 if (Op.isImmediate())
1245 AM.IndexReg = Op.getImmedValue();
1246 Op = MI->getOperand(3);
1247 if (Op.isGlobalAddress()) {
1248 AM.GV = Op.getGlobal();
1249 } else {
1250 AM.Disp = Op.getImmedValue();
1251 }
1252 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1253
1254 // Reload the original control word now.
1255 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1256
1257 delete MI; // The pseudo instruction is gone now.
1258 return BB;
1259 }
1260 }
Evan Cheng339edad2006-01-11 00:33:36 +00001261}
1262
1263
1264//===----------------------------------------------------------------------===//
1265// X86 Custom Lowering Hooks
1266//===----------------------------------------------------------------------===//
1267
Chris Lattner76ac0682005-11-15 00:40:23 +00001268/// LowerOperation - Provide custom lowering hooks for some operations.
1269///
1270SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1271 switch (Op.getOpcode()) {
1272 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001273 case ISD::ADD_PARTS:
1274 case ISD::SUB_PARTS: {
1275 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1276 "Not an i64 add/sub!");
1277 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1278 std::vector<MVT::ValueType> Tys;
1279 Tys.push_back(MVT::i32);
1280 Tys.push_back(MVT::Flag);
1281 std::vector<SDOperand> Ops;
1282 Ops.push_back(Op.getOperand(0));
1283 Ops.push_back(Op.getOperand(2));
1284 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1285 Tys, Ops);
1286 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1287 Op.getOperand(1), Op.getOperand(3),
1288 Lo.getValue(1));
1289 Tys.clear();
1290 Tys.push_back(MVT::i32);
1291 Tys.push_back(MVT::i32);
1292 Ops.clear();
1293 Ops.push_back(Lo);
1294 Ops.push_back(Hi);
1295 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1296 }
1297 case ISD::SHL_PARTS:
1298 case ISD::SRA_PARTS:
1299 case ISD::SRL_PARTS: {
1300 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1301 "Not an i64 shift!");
1302 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1303 SDOperand ShOpLo = Op.getOperand(0);
1304 SDOperand ShOpHi = Op.getOperand(1);
1305 SDOperand ShAmt = Op.getOperand(2);
1306 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001307 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001308 : DAG.getConstant(0, MVT::i32);
1309
1310 SDOperand Tmp2, Tmp3;
1311 if (Op.getOpcode() == ISD::SHL_PARTS) {
1312 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1313 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1314 } else {
1315 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001316 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001317 }
1318
1319 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1320 ShAmt, DAG.getConstant(32, MVT::i8));
1321
1322 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001323 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001324
1325 std::vector<MVT::ValueType> Tys;
1326 Tys.push_back(MVT::i32);
1327 Tys.push_back(MVT::Flag);
1328 std::vector<SDOperand> Ops;
1329 if (Op.getOpcode() == ISD::SHL_PARTS) {
1330 Ops.push_back(Tmp2);
1331 Ops.push_back(Tmp3);
1332 Ops.push_back(CC);
1333 Ops.push_back(InFlag);
1334 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1335 InFlag = Hi.getValue(1);
1336
1337 Ops.clear();
1338 Ops.push_back(Tmp3);
1339 Ops.push_back(Tmp1);
1340 Ops.push_back(CC);
1341 Ops.push_back(InFlag);
1342 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1343 } else {
1344 Ops.push_back(Tmp2);
1345 Ops.push_back(Tmp3);
1346 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001347 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001348 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1349 InFlag = Lo.getValue(1);
1350
1351 Ops.clear();
1352 Ops.push_back(Tmp3);
1353 Ops.push_back(Tmp1);
1354 Ops.push_back(CC);
1355 Ops.push_back(InFlag);
1356 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1357 }
1358
1359 Tys.clear();
1360 Tys.push_back(MVT::i32);
1361 Tys.push_back(MVT::i32);
1362 Ops.clear();
1363 Ops.push_back(Lo);
1364 Ops.push_back(Hi);
1365 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1366 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001367 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001368 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001369 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001370 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001371
1372 SDOperand Result;
1373 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1374 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001375 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001376 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001377 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001378 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1379 DAG.getEntryNode(), Op.getOperand(0),
1380 StackSlot, DAG.getSrcValue(NULL));
1381
1382 // Build the FILD
1383 std::vector<MVT::ValueType> Tys;
1384 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001385 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001386 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001387 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001388 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001389 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001390 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001391 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1392 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001393
1394 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001395 Chain = Result.getValue(1);
1396 SDOperand InFlag = Result.getValue(2);
1397
Evan Cheng11613a52006-02-04 02:20:30 +00001398 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001399 // shouldn't be necessary except that RFP cannot be live across
1400 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1401 MachineFunction &MF = DAG.getMachineFunction();
1402 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1403 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1404 std::vector<MVT::ValueType> Tys;
1405 Tys.push_back(MVT::Other);
1406 std::vector<SDOperand> Ops;
1407 Ops.push_back(Chain);
1408 Ops.push_back(Result);
1409 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001410 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001411 Ops.push_back(InFlag);
1412 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1413 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1414 DAG.getSrcValue(NULL));
1415 }
1416
Evan Cheng6305e502006-01-12 22:54:21 +00001417 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001418 }
1419 case ISD::FP_TO_SINT: {
1420 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001421 "Unknown FP_TO_SINT to lower!");
1422 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1423 // stack slot.
1424 MachineFunction &MF = DAG.getMachineFunction();
1425 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1426 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1427 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1428
1429 unsigned Opc;
1430 switch (Op.getValueType()) {
1431 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1432 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1433 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1434 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1435 }
1436
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001437 SDOperand Chain = DAG.getEntryNode();
1438 SDOperand Value = Op.getOperand(0);
1439 if (X86ScalarSSE) {
1440 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1441 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1442 DAG.getSrcValue(0));
1443 std::vector<MVT::ValueType> Tys;
1444 Tys.push_back(MVT::f64);
1445 Tys.push_back(MVT::Other);
1446 std::vector<SDOperand> Ops;
1447 Ops.push_back(Chain);
1448 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001449 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001450 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1451 Chain = Value.getValue(1);
1452 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1453 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1454 }
1455
Chris Lattner76ac0682005-11-15 00:40:23 +00001456 // Build the FP_TO_INT*_IN_MEM
1457 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001458 Ops.push_back(Chain);
1459 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001460 Ops.push_back(StackSlot);
1461 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1462
1463 // Load the result.
1464 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1465 DAG.getSrcValue(NULL));
1466 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001467 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001468 std::vector<MVT::ValueType> Tys;
1469 Tys.push_back(MVT::Other);
1470 Tys.push_back(MVT::Flag);
1471 std::vector<SDOperand> Ops;
1472 Ops.push_back(Op.getOperand(0));
1473 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001474 Ops.clear();
1475 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1476 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1477 MVT::i32, Ops[0].getValue(2)));
1478 Ops.push_back(Ops[1].getValue(1));
1479 Tys[0] = Tys[1] = MVT::i32;
1480 Tys.push_back(MVT::Other);
1481 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001482 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001483 case ISD::FABS: {
1484 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00001485 const Type *OpNTy = MVT::getTypeForValueType(VT);
1486 std::vector<Constant*> CV;
1487 if (VT == MVT::f64) {
1488 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1489 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1490 } else {
1491 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1492 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1493 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1494 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1495 }
1496 Constant *CS = ConstantStruct::get(CV);
1497 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1498 SDOperand Mask
1499 = DAG.getNode(X86ISD::LOAD_PACK,
1500 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00001501 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1502 }
Evan Cheng72d5c252006-01-31 22:28:30 +00001503 case ISD::FNEG: {
1504 MVT::ValueType VT = Op.getValueType();
1505 const Type *OpNTy = MVT::getTypeForValueType(VT);
1506 std::vector<Constant*> CV;
1507 if (VT == MVT::f64) {
1508 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1509 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1510 } else {
1511 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1512 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1513 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1514 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1515 }
1516 Constant *CS = ConstantStruct::get(CV);
1517 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1518 SDOperand Mask
1519 = DAG.getNode(X86ISD::LOAD_PACK,
1520 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1521 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1522 }
Evan Chengc1583db2005-12-21 20:21:51 +00001523 case ISD::SETCC: {
1524 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001525 SDOperand Cond;
1526 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001527 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1528 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001529 bool Flip;
1530 unsigned X86CC;
1531 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1532 if (Flip)
1533 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1534 Op.getOperand(1), Op.getOperand(0));
1535 else
1536 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1537 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001538 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1539 DAG.getConstant(X86CC, MVT::i8), Cond);
1540 } else {
1541 assert(isFP && "Illegal integer SetCC!");
1542
Evan Cheng45df7f82006-01-30 23:41:35 +00001543 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1544 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001545 std::vector<MVT::ValueType> Tys;
1546 std::vector<SDOperand> Ops;
1547 switch (SetCCOpcode) {
1548 default: assert(false && "Illegal floating point SetCC!");
1549 case ISD::SETOEQ: { // !PF & ZF
1550 Tys.push_back(MVT::i8);
1551 Tys.push_back(MVT::Flag);
1552 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1553 Ops.push_back(Cond);
1554 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1555 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1556 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1557 Tmp1.getValue(1));
1558 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1559 }
Evan Cheng172fce72006-01-06 00:43:03 +00001560 case ISD::SETUNE: { // PF | !ZF
1561 Tys.push_back(MVT::i8);
1562 Tys.push_back(MVT::Flag);
1563 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1564 Ops.push_back(Cond);
1565 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1566 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1567 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1568 Tmp1.getValue(1));
1569 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1570 }
1571 }
1572 }
Evan Chengc1583db2005-12-21 20:21:51 +00001573 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001574 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001575 MVT::ValueType VT = Op.getValueType();
1576 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001577 bool isFPStack = isFP && !X86ScalarSSE;
1578 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001579 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001580 SDOperand Op0 = Op.getOperand(0);
1581 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001582 if (Op0.getOpcode() == ISD::SETCC)
1583 Op0 = LowerOperation(Op0, DAG);
1584
Evan Cheng73a1ad92006-01-10 20:26:56 +00001585 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001586 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1587 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1588 // have another use it will be eliminated.
1589 // If the X86ISD::SETCC has more than one use, then it's probably better
1590 // to use a test instead of duplicating the X86ISD::CMP (for register
1591 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001592 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1593 if (!Op0.hasOneUse()) {
1594 std::vector<MVT::ValueType> Tys;
1595 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1596 Tys.push_back(Op0.Val->getValueType(i));
1597 std::vector<SDOperand> Ops;
1598 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1599 Ops.push_back(Op0.getOperand(i));
1600 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1601 }
1602
Evan Chengfb22e862006-01-13 01:03:02 +00001603 CC = Op0.getOperand(0);
1604 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001605 // Make a copy as flag result cannot be used by more than one.
1606 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1607 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001608 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001609 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001610 } else
1611 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001612 } else
1613 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001614
Evan Cheng731423f2006-01-13 01:06:49 +00001615 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001616 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001617 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001618 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001619
1620 std::vector<MVT::ValueType> Tys;
1621 Tys.push_back(Op.getValueType());
1622 Tys.push_back(MVT::Flag);
1623 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001624 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1625 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001626 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001627 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001628 Ops.push_back(CC);
1629 Ops.push_back(Cond);
1630 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001631 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001632 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001633 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001634 SDOperand Cond = Op.getOperand(1);
1635 SDOperand Dest = Op.getOperand(2);
1636 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001637 if (Cond.getOpcode() == ISD::SETCC)
1638 Cond = LowerOperation(Cond, DAG);
1639
Evan Chengc1583db2005-12-21 20:21:51 +00001640 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001641 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1642 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1643 // have another use it will be eliminated.
1644 // If the X86ISD::SETCC has more than one use, then it's probably better
1645 // to use a test instead of duplicating the X86ISD::CMP (for register
1646 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001647 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1648 if (!Cond.hasOneUse()) {
1649 std::vector<MVT::ValueType> Tys;
1650 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1651 Tys.push_back(Cond.Val->getValueType(i));
1652 std::vector<SDOperand> Ops;
1653 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1654 Ops.push_back(Cond.getOperand(i));
1655 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1656 }
1657
Evan Chengfb22e862006-01-13 01:03:02 +00001658 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001659 Cond = Cond.getOperand(1);
1660 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001661 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001662 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001663 } else
1664 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001665 } else
1666 addTest = true;
1667
1668 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001669 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001670 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1671 }
1672 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1673 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1674 }
Evan Chengae986f12006-01-11 22:15:48 +00001675 case ISD::MEMSET: {
1676 SDOperand InFlag;
1677 SDOperand Chain = Op.getOperand(0);
1678 unsigned Align =
1679 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1680 if (Align == 0) Align = 1;
1681
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001682 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1683 // If not DWORD aligned, call memset if size is less than the threshold.
1684 // It knows how to align to the right boundary first.
1685 if ((Align & 3) != 0 &&
1686 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1687 MVT::ValueType IntPtr = getPointerTy();
1688 const Type *IntPtrTy = getTargetData().getIntPtrType();
1689 std::vector<std::pair<SDOperand, const Type*> > Args;
1690 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1691 // Extend the ubyte argument to be an int value for the call.
1692 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1693 Args.push_back(std::make_pair(Val, IntPtrTy));
1694 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1695 std::pair<SDOperand,SDOperand> CallResult =
1696 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1697 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1698 return CallResult.second;
1699 }
1700
Evan Chengae986f12006-01-11 22:15:48 +00001701 MVT::ValueType AVT;
1702 SDOperand Count;
1703 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1704 unsigned ValReg;
1705 unsigned Val = ValC->getValue() & 255;
1706
1707 // If the value is a constant, then we can potentially use larger sets.
1708 switch (Align & 3) {
1709 case 2: // WORD aligned
1710 AVT = MVT::i16;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001711 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001712 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1713 else
1714 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1715 DAG.getConstant(1, MVT::i8));
1716 Val = (Val << 8) | Val;
1717 ValReg = X86::AX;
1718 break;
1719 case 0: // DWORD aligned
1720 AVT = MVT::i32;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001721 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001722 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1723 else
1724 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1725 DAG.getConstant(2, MVT::i8));
1726 Val = (Val << 8) | Val;
1727 Val = (Val << 16) | Val;
1728 ValReg = X86::EAX;
1729 break;
1730 default: // Byte aligned
1731 AVT = MVT::i8;
1732 Count = Op.getOperand(3);
1733 ValReg = X86::AL;
1734 break;
1735 }
1736
1737 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1738 InFlag);
1739 InFlag = Chain.getValue(1);
1740 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001741 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00001742 Count = Op.getOperand(3);
1743 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1744 InFlag = Chain.getValue(1);
1745 }
1746
1747 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1748 InFlag = Chain.getValue(1);
1749 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1750 InFlag = Chain.getValue(1);
1751
1752 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1753 DAG.getValueType(AVT), InFlag);
1754 }
1755 case ISD::MEMCPY: {
1756 SDOperand Chain = Op.getOperand(0);
1757 unsigned Align =
1758 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1759 if (Align == 0) Align = 1;
1760
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001761 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1762 // If not DWORD aligned, call memcpy if size is less than the threshold.
1763 // It knows how to align to the right boundary first.
1764 if ((Align & 3) != 0 &&
1765 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1766 MVT::ValueType IntPtr = getPointerTy();
1767 const Type *IntPtrTy = getTargetData().getIntPtrType();
1768 std::vector<std::pair<SDOperand, const Type*> > Args;
1769 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1770 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1771 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1772 std::pair<SDOperand,SDOperand> CallResult =
1773 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1774 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1775 return CallResult.second;
1776 }
1777
Evan Chengae986f12006-01-11 22:15:48 +00001778 MVT::ValueType AVT;
1779 SDOperand Count;
1780 switch (Align & 3) {
1781 case 2: // WORD aligned
1782 AVT = MVT::i16;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001783 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001784 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1785 else
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001786 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
Evan Chengae986f12006-01-11 22:15:48 +00001787 break;
1788 case 0: // DWORD aligned
1789 AVT = MVT::i32;
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001790 if (I)
Evan Chengae986f12006-01-11 22:15:48 +00001791 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1792 else
1793 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1794 DAG.getConstant(2, MVT::i8));
1795 break;
1796 default: // Byte aligned
1797 AVT = MVT::i8;
1798 Count = Op.getOperand(3);
1799 break;
1800 }
1801
1802 SDOperand InFlag;
1803 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1804 InFlag = Chain.getValue(1);
1805 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1806 InFlag = Chain.getValue(1);
1807 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1808 InFlag = Chain.getValue(1);
1809
1810 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1811 DAG.getValueType(AVT), InFlag);
1812 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001813 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001814 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001815 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1816 // For Darwin, external and weak symbols are indirect, so we want to load
1817 // the value at address GV, not the value of GV itself. This means that
1818 // the GlobalAddress must be in the base or index register of the address,
1819 // not the GV offset field.
1820 if (getTargetMachine().
Evan Cheng5a766802006-02-07 08:38:37 +00001821 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals()) {
1822 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1823 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
1824 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1825 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1826 DAG.getSrcValue(NULL));
1827 }
Evan Chengb94db9e2006-01-12 07:56:47 +00001828 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001829 }
Nate Begemane74795c2006-01-25 18:21:52 +00001830 case ISD::VASTART: {
1831 // vastart just stores the address of the VarArgsFrameIndex slot into the
1832 // memory location argument.
1833 // FIXME: Replace MVT::i32 with PointerTy
1834 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1835 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1836 Op.getOperand(1), Op.getOperand(2));
1837 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001838 case ISD::RET: {
1839 SDOperand Copy;
1840
1841 switch(Op.getNumOperands()) {
1842 default:
1843 assert(0 && "Do not know how to return this many arguments!");
1844 abort();
1845 case 1:
1846 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1847 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1848 case 2: {
1849 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1850 if (MVT::isInteger(ArgVT))
1851 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1852 SDOperand());
1853 else if (!X86ScalarSSE) {
1854 std::vector<MVT::ValueType> Tys;
1855 Tys.push_back(MVT::Other);
1856 Tys.push_back(MVT::Flag);
1857 std::vector<SDOperand> Ops;
1858 Ops.push_back(Op.getOperand(0));
1859 Ops.push_back(Op.getOperand(1));
1860 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1861 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00001862 SDOperand MemLoc;
1863 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00001864 SDOperand Value = Op.getOperand(1);
1865
Evan Chenga24617f2006-02-01 01:19:32 +00001866 if (Value.getOpcode() == ISD::LOAD &&
1867 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00001868 Chain = Value.getOperand(0);
1869 MemLoc = Value.getOperand(1);
1870 } else {
1871 // Spill the value to memory and reload it into top of stack.
1872 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1873 MachineFunction &MF = DAG.getMachineFunction();
1874 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1875 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1876 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1877 Value, MemLoc, DAG.getSrcValue(0));
1878 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001879 std::vector<MVT::ValueType> Tys;
1880 Tys.push_back(MVT::f64);
1881 Tys.push_back(MVT::Other);
1882 std::vector<SDOperand> Ops;
1883 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00001884 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001885 Ops.push_back(DAG.getValueType(ArgVT));
1886 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1887 Tys.clear();
1888 Tys.push_back(MVT::Other);
1889 Tys.push_back(MVT::Flag);
1890 Ops.clear();
1891 Ops.push_back(Copy.getValue(1));
1892 Ops.push_back(Copy);
1893 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1894 }
1895 break;
1896 }
1897 case 3:
1898 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1899 SDOperand());
1900 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1901 break;
1902 }
1903 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1904 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1905 Copy.getValue(1));
1906 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001907 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001908}
Evan Cheng6af02632005-12-20 06:22:03 +00001909
1910const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1911 switch (Opcode) {
1912 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001913 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1914 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1915 case X86ISD::ADC: return "X86ISD::ADC";
1916 case X86ISD::SBB: return "X86ISD::SBB";
1917 case X86ISD::SHLD: return "X86ISD::SHLD";
1918 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00001919 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00001920 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00001921 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00001922 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00001923 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1924 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1925 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001926 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001927 case X86ISD::FST: return "X86ISD::FST";
1928 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001929 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001930 case X86ISD::CALL: return "X86ISD::CALL";
1931 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1932 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1933 case X86ISD::CMP: return "X86ISD::CMP";
1934 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001935 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001936 case X86ISD::CMOV: return "X86ISD::CMOV";
1937 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001938 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001939 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1940 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00001941 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng6af02632005-12-20 06:22:03 +00001942 }
1943}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001944
Nate Begeman8a77efe2006-02-16 21:11:51 +00001945void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1946 uint64_t Mask,
1947 uint64_t &KnownZero,
1948 uint64_t &KnownOne,
1949 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001950
1951 unsigned Opc = Op.getOpcode();
Nate Begeman8a77efe2006-02-16 21:11:51 +00001952 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001953
1954 switch (Opc) {
1955 default:
1956 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1957 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00001958 case X86ISD::SETCC:
1959 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1960 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001961 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001962}
Chris Lattnerc642aa52006-01-31 19:43:35 +00001963
1964std::vector<unsigned> X86TargetLowering::
1965getRegForInlineAsmConstraint(const std::string &Constraint) const {
1966 if (Constraint.size() == 1) {
1967 // FIXME: not handling fp-stack yet!
1968 // FIXME: not handling MMX registers yet ('y' constraint).
1969 switch (Constraint[0]) { // GCC X86 Constraint Letters
1970 default: break; // Unknown constriant letter
1971 case 'r': // GENERAL_REGS
1972 case 'R': // LEGACY_REGS
1973 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1974 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
1975 case 'l': // INDEX_REGS
1976 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1977 X86::ESI, X86::EDI, X86::EBP, 0);
1978 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
1979 case 'Q': // Q_REGS
1980 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
1981 case 'x': // SSE_REGS if SSE1 allowed
1982 if (Subtarget->hasSSE1())
1983 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1984 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1985 0);
1986 return std::vector<unsigned>();
1987 case 'Y': // SSE_REGS if SSE2 allowed
1988 if (Subtarget->hasSSE2())
1989 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1990 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1991 0);
1992 return std::vector<unsigned>();
1993 }
1994 }
1995
1996 // Handle explicit register names.
1997 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
1998}