blob: 39d537ae4e19598ae9624072094f51d1a9c3fc77 [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"
20#include "llvm/Function.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetOptions.h"
27using namespace llvm;
28
29// FIXME: temporary.
30#include "llvm/Support/CommandLine.h"
31static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
32 cl::desc("Enable fastcc on X86"));
33
34X86TargetLowering::X86TargetLowering(TargetMachine &TM)
35 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000036 Subtarget = &TM.getSubtarget<X86Subtarget>();
37 X86ScalarSSE = Subtarget->hasSSE2();
38
Chris Lattner76ac0682005-11-15 00:40:23 +000039 // Set up the TargetLowering object.
40
41 // X86 is weird, it always uses i8 for shift amounts and setcc results.
42 setShiftAmountType(MVT::i8);
43 setSetCCResultType(MVT::i8);
44 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000045 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000047 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000048
Chris Lattner76ac0682005-11-15 00:40:23 +000049 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000050 addRegisterClass(MVT::i8, X86::R8RegisterClass);
51 addRegisterClass(MVT::i16, X86::R16RegisterClass);
52 addRegisterClass(MVT::i32, X86::R32RegisterClass);
53
54 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
55 // operation.
56 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
57 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
58 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000059
60 if (X86ScalarSSE)
61 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
62 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
63 else
64 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000065
66 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
67 // this operation.
68 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
69 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Evan Cheng08390f62006-01-30 22:13:22 +000070 if (X86ScalarSSE)
71 // SSE has no i16 to fp conversion, only i32
72 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
73 else if (!X86PatIsel) {
74 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
75 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
76 }
Chris Lattner76ac0682005-11-15 00:40:23 +000077
Evan Cheng5b97fcf2006-01-30 08:02:57 +000078 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
79 // isn't legal.
80 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
81 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
82
Evan Cheng08390f62006-01-30 22:13:22 +000083 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
84 // this operation.
85 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
86 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
87
88 if (X86ScalarSSE) {
89 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
90 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +000091 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +000092 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +000093 }
94
95 // Handle FP_TO_UINT by promoting the destination to a larger signed
96 // conversion.
97 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
98 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
99 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
100
Evan Cheng08390f62006-01-30 22:13:22 +0000101 if (X86ScalarSSE)
102 // Expand FP_TO_UINT into a select.
103 // FIXME: We would like to use a Custom expander here eventually to do
104 // the optimal thing for SSE vs. the default expansion in the legalizer.
105 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
106 else
Chris Lattner76ac0682005-11-15 00:40:23 +0000107 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
108
Evan Cheng08390f62006-01-30 22:13:22 +0000109 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
110 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000111
Evan Chenga814f0b32006-01-27 21:26:54 +0000112 if (!X86PatIsel) {
Evan Cheng6fc31042005-12-19 23:12:38 +0000113 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
114 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000115 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
116 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
117 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
121 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
122 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
123 setOperationAction(ISD::FREM , MVT::f64 , Expand);
124 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
125 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
126 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
127 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
128 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
129 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
130 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
131 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
132 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000133 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000134
Evan Chenga814f0b32006-01-27 21:26:54 +0000135 if (X86PatIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000136 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000137 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
138 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
139 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
140 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
141 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
142 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
143 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000144 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000145
Chris Lattner76ac0682005-11-15 00:40:23 +0000146 setOperationAction(ISD::READIO , MVT::i1 , Expand);
147 setOperationAction(ISD::READIO , MVT::i8 , Expand);
148 setOperationAction(ISD::READIO , MVT::i16 , Expand);
149 setOperationAction(ISD::READIO , MVT::i32 , Expand);
150 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
151 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
152 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
153 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
154
155 // These should be promoted to a larger select which is supported.
156 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
157 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Chenga814f0b32006-01-27 21:26:54 +0000158 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000159 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000160 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
161 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000162 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000164 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000167 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
169 // X86 ret instruction may pop stack.
170 setOperationAction(ISD::RET , MVT::Other, Custom);
171 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000172 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
174 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
175 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000179 // X86 wants to expand memset / memcpy itself.
180 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
181 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000182 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000183
Chris Lattner9c415362005-11-29 06:16:21 +0000184 // We don't have line number support yet.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
187 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000188
Nate Begemane74795c2006-01-25 18:21:52 +0000189 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
190 setOperationAction(ISD::VASTART , MVT::Other, Custom);
191
192 // Use the default implementation.
193 setOperationAction(ISD::VAARG , MVT::Other, Expand);
194 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
195 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000196 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
197 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
198 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000199
Chris Lattner76ac0682005-11-15 00:40:23 +0000200 if (X86ScalarSSE) {
201 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000202 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
203 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000204
205 // SSE has no load+extend ops
206 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
207 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
208
Chris Lattner76ac0682005-11-15 00:40:23 +0000209 // We don't support sin/cos/sqrt/fmod
210 setOperationAction(ISD::FSIN , MVT::f64, Expand);
211 setOperationAction(ISD::FCOS , MVT::f64, Expand);
212 setOperationAction(ISD::FABS , MVT::f64, Expand);
213 setOperationAction(ISD::FNEG , MVT::f64, Expand);
214 setOperationAction(ISD::FREM , MVT::f64, Expand);
215 setOperationAction(ISD::FSIN , MVT::f32, Expand);
216 setOperationAction(ISD::FCOS , MVT::f32, Expand);
217 setOperationAction(ISD::FABS , MVT::f32, Expand);
218 setOperationAction(ISD::FNEG , MVT::f32, Expand);
219 setOperationAction(ISD::FREM , MVT::f32, Expand);
220
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000221 // Expand FP immediates into loads from the stack, except for the special
222 // cases we handle.
223 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
224 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000225 addLegalFPImmediate(+0.0); // xorps / xorpd
226 } else {
227 // Set up the FP register classes.
228 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000229
230 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
231
Chris Lattner76ac0682005-11-15 00:40:23 +0000232 if (!UnsafeFPMath) {
233 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
234 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
235 }
236
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000237 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000238 addLegalFPImmediate(+0.0); // FLD0
239 addLegalFPImmediate(+1.0); // FLD1
240 addLegalFPImmediate(-0.0); // FLD0/FCHS
241 addLegalFPImmediate(-1.0); // FLD1/FCHS
242 }
243 computeRegisterProperties();
244
245 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
246 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
247 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
248 allowUnalignedMemoryAccesses = true; // x86 supports it!
249}
250
251std::vector<SDOperand>
252X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
253 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
254 return LowerFastCCArguments(F, DAG);
255 return LowerCCCArguments(F, DAG);
256}
257
258std::pair<SDOperand, SDOperand>
259X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
260 bool isVarArg, unsigned CallingConv,
261 bool isTailCall,
262 SDOperand Callee, ArgListTy &Args,
263 SelectionDAG &DAG) {
264 assert((!isVarArg || CallingConv == CallingConv::C) &&
265 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000266
267 // If the callee is a GlobalAddress node (quite common, every direct call is)
268 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
269 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
270 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000271 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
272 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000273
Chris Lattner76ac0682005-11-15 00:40:23 +0000274 if (CallingConv == CallingConv::Fast && EnableFastCC)
275 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
276 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
277}
278
279//===----------------------------------------------------------------------===//
280// C Calling Convention implementation
281//===----------------------------------------------------------------------===//
282
283std::vector<SDOperand>
284X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
285 std::vector<SDOperand> ArgValues;
286
287 MachineFunction &MF = DAG.getMachineFunction();
288 MachineFrameInfo *MFI = MF.getFrameInfo();
289
290 // Add DAG nodes to load the arguments... On entry to a function on the X86,
291 // the stack frame looks like this:
292 //
293 // [ESP] -- return address
294 // [ESP + 4] -- first argument (leftmost lexically)
295 // [ESP + 8] -- second argument, if first argument is four bytes in size
296 // ...
297 //
298 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
299 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
300 MVT::ValueType ObjectVT = getValueType(I->getType());
301 unsigned ArgIncrement = 4;
302 unsigned ObjSize;
303 switch (ObjectVT) {
304 default: assert(0 && "Unhandled argument type!");
305 case MVT::i1:
306 case MVT::i8: ObjSize = 1; break;
307 case MVT::i16: ObjSize = 2; break;
308 case MVT::i32: ObjSize = 4; break;
309 case MVT::i64: ObjSize = ArgIncrement = 8; break;
310 case MVT::f32: ObjSize = 4; break;
311 case MVT::f64: ObjSize = ArgIncrement = 8; break;
312 }
313 // Create the frame index object for this incoming parameter...
314 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
315
316 // Create the SelectionDAG nodes corresponding to a load from this parameter
317 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
318
319 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
320 // dead loads.
321 SDOperand ArgValue;
322 if (!I->use_empty())
323 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
324 DAG.getSrcValue(NULL));
325 else {
326 if (MVT::isInteger(ObjectVT))
327 ArgValue = DAG.getConstant(0, ObjectVT);
328 else
329 ArgValue = DAG.getConstantFP(0, ObjectVT);
330 }
331 ArgValues.push_back(ArgValue);
332
333 ArgOffset += ArgIncrement; // Move on to the next argument...
334 }
335
336 // If the function takes variable number of arguments, make a frame index for
337 // the start of the first vararg value... for expansion of llvm.va_start.
338 if (F.isVarArg())
339 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
340 ReturnAddrIndex = 0; // No return address slot generated yet.
341 BytesToPopOnReturn = 0; // Callee pops nothing.
342 BytesCallerReserves = ArgOffset;
343
344 // Finally, inform the code generator which regs we return values in.
345 switch (getValueType(F.getReturnType())) {
346 default: assert(0 && "Unknown type!");
347 case MVT::isVoid: break;
348 case MVT::i1:
349 case MVT::i8:
350 case MVT::i16:
351 case MVT::i32:
352 MF.addLiveOut(X86::EAX);
353 break;
354 case MVT::i64:
355 MF.addLiveOut(X86::EAX);
356 MF.addLiveOut(X86::EDX);
357 break;
358 case MVT::f32:
359 case MVT::f64:
360 MF.addLiveOut(X86::ST0);
361 break;
362 }
363 return ArgValues;
364}
365
366std::pair<SDOperand, SDOperand>
367X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
368 bool isVarArg, bool isTailCall,
369 SDOperand Callee, ArgListTy &Args,
370 SelectionDAG &DAG) {
371 // Count how many bytes are to be pushed on the stack.
372 unsigned NumBytes = 0;
373
374 if (Args.empty()) {
375 // Save zero bytes.
376 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
377 DAG.getConstant(0, getPointerTy()));
378 } else {
379 for (unsigned i = 0, e = Args.size(); i != e; ++i)
380 switch (getValueType(Args[i].second)) {
381 default: assert(0 && "Unknown value type!");
382 case MVT::i1:
383 case MVT::i8:
384 case MVT::i16:
385 case MVT::i32:
386 case MVT::f32:
387 NumBytes += 4;
388 break;
389 case MVT::i64:
390 case MVT::f64:
391 NumBytes += 8;
392 break;
393 }
394
395 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
396 DAG.getConstant(NumBytes, getPointerTy()));
397
398 // Arguments go on the stack in reverse order, as specified by the ABI.
399 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000400 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000401 std::vector<SDOperand> Stores;
402
403 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
404 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
405 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
406
407 switch (getValueType(Args[i].second)) {
408 default: assert(0 && "Unexpected ValueType for argument!");
409 case MVT::i1:
410 case MVT::i8:
411 case MVT::i16:
412 // Promote the integer to 32 bits. If the input type is signed use a
413 // sign extend, otherwise use a zero extend.
414 if (Args[i].second->isSigned())
415 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
416 else
417 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
418
419 // FALL THROUGH
420 case MVT::i32:
421 case MVT::f32:
422 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
423 Args[i].first, PtrOff,
424 DAG.getSrcValue(NULL)));
425 ArgOffset += 4;
426 break;
427 case MVT::i64:
428 case MVT::f64:
429 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
430 Args[i].first, PtrOff,
431 DAG.getSrcValue(NULL)));
432 ArgOffset += 8;
433 break;
434 }
435 }
436 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
437 }
438
439 std::vector<MVT::ValueType> RetVals;
440 MVT::ValueType RetTyVT = getValueType(RetTy);
441 RetVals.push_back(MVT::Other);
442
443 // The result values produced have to be legal. Promote the result.
444 switch (RetTyVT) {
445 case MVT::isVoid: break;
446 default:
447 RetVals.push_back(RetTyVT);
448 break;
449 case MVT::i1:
450 case MVT::i8:
451 case MVT::i16:
452 RetVals.push_back(MVT::i32);
453 break;
454 case MVT::f32:
455 if (X86ScalarSSE)
456 RetVals.push_back(MVT::f32);
457 else
458 RetVals.push_back(MVT::f64);
459 break;
460 case MVT::i64:
461 RetVals.push_back(MVT::i32);
462 RetVals.push_back(MVT::i32);
463 break;
464 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000465
Evan Chenga814f0b32006-01-27 21:26:54 +0000466 if (!X86PatIsel) {
Evan Cheng45e190982006-01-05 00:27:02 +0000467 std::vector<MVT::ValueType> NodeTys;
468 NodeTys.push_back(MVT::Other); // Returns a chain
469 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000470 std::vector<SDOperand> Ops;
471 Ops.push_back(Chain);
472 Ops.push_back(Callee);
473
Evan Cheng172fce72006-01-06 00:43:03 +0000474 // FIXME: Do not generate X86ISD::TAILCALL for now.
475 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000476 SDOperand InFlag = Chain.getValue(1);
477
Chris Lattner6f33eae2006-01-24 05:17:12 +0000478 NodeTys.clear();
479 NodeTys.push_back(MVT::Other); // Returns a chain
480 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
481 Ops.clear();
482 Ops.push_back(Chain);
483 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
484 Ops.push_back(DAG.getConstant(0, getPointerTy()));
485 Ops.push_back(InFlag);
486 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
487 InFlag = Chain.getValue(1);
488
Evan Cheng45e190982006-01-05 00:27:02 +0000489 SDOperand RetVal;
490 if (RetTyVT != MVT::isVoid) {
491 switch (RetTyVT) {
492 default: assert(0 && "Unknown value type to return!");
493 case MVT::i1:
494 case MVT::i8:
495 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
496 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000497 if (RetTyVT == MVT::i1)
498 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000499 break;
500 case MVT::i16:
501 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
502 Chain = RetVal.getValue(1);
503 break;
504 case MVT::i32:
505 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
506 Chain = RetVal.getValue(1);
507 break;
508 case MVT::i64: {
509 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
510 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
511 Lo.getValue(2));
512 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
513 Chain = Hi.getValue(1);
514 break;
515 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000516 case MVT::f32:
Evan Cheng45e190982006-01-05 00:27:02 +0000517 case MVT::f64: {
518 std::vector<MVT::ValueType> Tys;
519 Tys.push_back(MVT::f64);
520 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +0000521 Tys.push_back(MVT::Flag);
Evan Cheng45e190982006-01-05 00:27:02 +0000522 std::vector<SDOperand> Ops;
523 Ops.push_back(Chain);
524 Ops.push_back(InFlag);
525 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +0000526 Chain = RetVal.getValue(1);
527 InFlag = RetVal.getValue(2);
Evan Cheng45e190982006-01-05 00:27:02 +0000528 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000529 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
530 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +0000531 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng45e190982006-01-05 00:27:02 +0000532 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000533 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng45e190982006-01-05 00:27:02 +0000534 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
535 Tys.clear();
536 Tys.push_back(MVT::Other);
537 Ops.clear();
538 Ops.push_back(Chain);
539 Ops.push_back(RetVal);
540 Ops.push_back(StackSlot);
541 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +0000542 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000543 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
544 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
545 DAG.getSrcValue(NULL));
546 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +0000547 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000548
549 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
550 // FIXME: we would really like to remember that this FP_ROUND
551 // operation is okay to eliminate if we allow excess FP precision.
552 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000553 break;
554 }
555 }
556 }
557
Evan Cheng45e190982006-01-05 00:27:02 +0000558 return std::make_pair(RetVal, Chain);
559 } else {
560 std::vector<SDOperand> Ops;
561 Ops.push_back(Chain);
562 Ops.push_back(Callee);
563 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
564 Ops.push_back(DAG.getConstant(0, getPointerTy()));
565
566 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
567 RetVals, Ops);
568
569 SDOperand ResultVal;
570 switch (RetTyVT) {
571 case MVT::isVoid: break;
572 default:
573 ResultVal = TheCall.getValue(1);
574 break;
575 case MVT::i1:
576 case MVT::i8:
577 case MVT::i16:
578 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
579 break;
580 case MVT::f32:
581 // FIXME: we would really like to remember that this FP_ROUND operation is
582 // okay to eliminate if we allow excess FP precision.
583 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
584 break;
585 case MVT::i64:
586 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
587 TheCall.getValue(2));
588 break;
589 }
590
591 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
592 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000593 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000594}
595
Chris Lattner76ac0682005-11-15 00:40:23 +0000596//===----------------------------------------------------------------------===//
597// Fast Calling Convention implementation
598//===----------------------------------------------------------------------===//
599//
600// The X86 'fast' calling convention passes up to two integer arguments in
601// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
602// and requires that the callee pop its arguments off the stack (allowing proper
603// tail calls), and has the same return value conventions as C calling convs.
604//
605// This calling convention always arranges for the callee pop value to be 8n+4
606// bytes, which is needed for tail recursion elimination and stack alignment
607// reasons.
608//
609// Note that this can be enhanced in the future to pass fp vals in registers
610// (when we have a global fp allocator) and do other tricks.
611//
612
613/// AddLiveIn - This helper function adds the specified physical register to the
614/// MachineFunction as a live in value. It also creates a corresponding virtual
615/// register for it.
616static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
617 TargetRegisterClass *RC) {
618 assert(RC->contains(PReg) && "Not the correct regclass!");
619 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
620 MF.addLiveIn(PReg, VReg);
621 return VReg;
622}
623
624
625std::vector<SDOperand>
626X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
627 std::vector<SDOperand> ArgValues;
628
629 MachineFunction &MF = DAG.getMachineFunction();
630 MachineFrameInfo *MFI = MF.getFrameInfo();
631
632 // Add DAG nodes to load the arguments... On entry to a function the stack
633 // frame looks like this:
634 //
635 // [ESP] -- return address
636 // [ESP + 4] -- first nonreg argument (leftmost lexically)
637 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
638 // ...
639 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
640
641 // Keep track of the number of integer regs passed so far. This can be either
642 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
643 // used).
644 unsigned NumIntRegs = 0;
645
646 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
647 MVT::ValueType ObjectVT = getValueType(I->getType());
648 unsigned ArgIncrement = 4;
649 unsigned ObjSize = 0;
650 SDOperand ArgValue;
651
652 switch (ObjectVT) {
653 default: assert(0 && "Unhandled argument type!");
654 case MVT::i1:
655 case MVT::i8:
656 if (NumIntRegs < 2) {
657 if (!I->use_empty()) {
658 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
659 X86::R8RegisterClass);
660 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
661 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000662 if (ObjectVT == MVT::i1)
663 // FIXME: Should insert a assertzext here.
664 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000665 }
666 ++NumIntRegs;
667 break;
668 }
669
670 ObjSize = 1;
671 break;
672 case MVT::i16:
673 if (NumIntRegs < 2) {
674 if (!I->use_empty()) {
675 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
676 X86::R16RegisterClass);
677 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
678 DAG.setRoot(ArgValue.getValue(1));
679 }
680 ++NumIntRegs;
681 break;
682 }
683 ObjSize = 2;
684 break;
685 case MVT::i32:
686 if (NumIntRegs < 2) {
687 if (!I->use_empty()) {
688 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
689 X86::R32RegisterClass);
690 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
691 DAG.setRoot(ArgValue.getValue(1));
692 }
693 ++NumIntRegs;
694 break;
695 }
696 ObjSize = 4;
697 break;
698 case MVT::i64:
699 if (NumIntRegs == 0) {
700 if (!I->use_empty()) {
701 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
702 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
703
704 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
705 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
706 DAG.setRoot(Hi.getValue(1));
707
708 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
709 }
710 NumIntRegs = 2;
711 break;
712 } else if (NumIntRegs == 1) {
713 if (!I->use_empty()) {
714 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
715 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
716 DAG.setRoot(Low.getValue(1));
717
718 // Load the high part from memory.
719 // Create the frame index object for this incoming parameter...
720 int FI = MFI->CreateFixedObject(4, ArgOffset);
721 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
722 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
723 DAG.getSrcValue(NULL));
724 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
725 }
726 ArgOffset += 4;
727 NumIntRegs = 2;
728 break;
729 }
730 ObjSize = ArgIncrement = 8;
731 break;
732 case MVT::f32: ObjSize = 4; break;
733 case MVT::f64: ObjSize = ArgIncrement = 8; break;
734 }
735
736 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
737 // dead loads.
738 if (ObjSize && !I->use_empty()) {
739 // Create the frame index object for this incoming parameter...
740 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
741
742 // Create the SelectionDAG nodes corresponding to a load from this
743 // parameter.
744 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
745
746 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
747 DAG.getSrcValue(NULL));
748 } else if (ArgValue.Val == 0) {
749 if (MVT::isInteger(ObjectVT))
750 ArgValue = DAG.getConstant(0, ObjectVT);
751 else
752 ArgValue = DAG.getConstantFP(0, ObjectVT);
753 }
754 ArgValues.push_back(ArgValue);
755
756 if (ObjSize)
757 ArgOffset += ArgIncrement; // Move on to the next argument.
758 }
759
760 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
761 // arguments and the arguments after the retaddr has been pushed are aligned.
762 if ((ArgOffset & 7) == 0)
763 ArgOffset += 4;
764
765 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
766 ReturnAddrIndex = 0; // No return address slot generated yet.
767 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
768 BytesCallerReserves = 0;
769
770 // Finally, inform the code generator which regs we return values in.
771 switch (getValueType(F.getReturnType())) {
772 default: assert(0 && "Unknown type!");
773 case MVT::isVoid: break;
774 case MVT::i1:
775 case MVT::i8:
776 case MVT::i16:
777 case MVT::i32:
778 MF.addLiveOut(X86::EAX);
779 break;
780 case MVT::i64:
781 MF.addLiveOut(X86::EAX);
782 MF.addLiveOut(X86::EDX);
783 break;
784 case MVT::f32:
785 case MVT::f64:
786 MF.addLiveOut(X86::ST0);
787 break;
788 }
789 return ArgValues;
790}
791
792std::pair<SDOperand, SDOperand>
793X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
794 bool isTailCall, SDOperand Callee,
795 ArgListTy &Args, SelectionDAG &DAG) {
796 // Count how many bytes are to be pushed on the stack.
797 unsigned NumBytes = 0;
798
799 // Keep track of the number of integer regs passed so far. This can be either
800 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
801 // used).
802 unsigned NumIntRegs = 0;
803
804 for (unsigned i = 0, e = Args.size(); i != e; ++i)
805 switch (getValueType(Args[i].second)) {
806 default: assert(0 && "Unknown value type!");
807 case MVT::i1:
808 case MVT::i8:
809 case MVT::i16:
810 case MVT::i32:
811 if (NumIntRegs < 2) {
812 ++NumIntRegs;
813 break;
814 }
815 // fall through
816 case MVT::f32:
817 NumBytes += 4;
818 break;
819 case MVT::i64:
820 if (NumIntRegs == 0) {
821 NumIntRegs = 2;
822 break;
823 } else if (NumIntRegs == 1) {
824 NumIntRegs = 2;
825 NumBytes += 4;
826 break;
827 }
828
829 // fall through
830 case MVT::f64:
831 NumBytes += 8;
832 break;
833 }
834
835 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
836 // arguments and the arguments after the retaddr has been pushed are aligned.
837 if ((NumBytes & 7) == 0)
838 NumBytes += 4;
839
840 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
841 DAG.getConstant(NumBytes, getPointerTy()));
842
843 // Arguments go on the stack in reverse order, as specified by the ABI.
844 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000845 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000846 NumIntRegs = 0;
847 std::vector<SDOperand> Stores;
848 std::vector<SDOperand> RegValuesToPass;
849 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
850 switch (getValueType(Args[i].second)) {
851 default: assert(0 && "Unexpected ValueType for argument!");
852 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000853 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
854 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000855 case MVT::i8:
856 case MVT::i16:
857 case MVT::i32:
858 if (NumIntRegs < 2) {
859 RegValuesToPass.push_back(Args[i].first);
860 ++NumIntRegs;
861 break;
862 }
863 // Fall through
864 case MVT::f32: {
865 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
866 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
867 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
868 Args[i].first, PtrOff,
869 DAG.getSrcValue(NULL)));
870 ArgOffset += 4;
871 break;
872 }
873 case MVT::i64:
874 if (NumIntRegs < 2) { // Can pass part of it in regs?
875 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
876 Args[i].first, DAG.getConstant(1, MVT::i32));
877 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
878 Args[i].first, DAG.getConstant(0, MVT::i32));
879 RegValuesToPass.push_back(Lo);
880 ++NumIntRegs;
881 if (NumIntRegs < 2) { // Pass both parts in regs?
882 RegValuesToPass.push_back(Hi);
883 ++NumIntRegs;
884 } else {
885 // Pass the high part in memory.
886 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
887 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
888 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
889 Hi, PtrOff, DAG.getSrcValue(NULL)));
890 ArgOffset += 4;
891 }
892 break;
893 }
894 // Fall through
895 case MVT::f64:
896 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
897 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
898 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
899 Args[i].first, PtrOff,
900 DAG.getSrcValue(NULL)));
901 ArgOffset += 8;
902 break;
903 }
904 }
905 if (!Stores.empty())
906 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
907
908 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
909 // arguments and the arguments after the retaddr has been pushed are aligned.
910 if ((ArgOffset & 7) == 0)
911 ArgOffset += 4;
912
913 std::vector<MVT::ValueType> RetVals;
914 MVT::ValueType RetTyVT = getValueType(RetTy);
915
916 RetVals.push_back(MVT::Other);
917
918 // The result values produced have to be legal. Promote the result.
919 switch (RetTyVT) {
920 case MVT::isVoid: break;
921 default:
922 RetVals.push_back(RetTyVT);
923 break;
924 case MVT::i1:
925 case MVT::i8:
926 case MVT::i16:
927 RetVals.push_back(MVT::i32);
928 break;
929 case MVT::f32:
930 if (X86ScalarSSE)
931 RetVals.push_back(MVT::f32);
932 else
933 RetVals.push_back(MVT::f64);
934 break;
935 case MVT::i64:
936 RetVals.push_back(MVT::i32);
937 RetVals.push_back(MVT::i32);
938 break;
939 }
940
Evan Chenga814f0b32006-01-27 21:26:54 +0000941 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000942 // Build a sequence of copy-to-reg nodes chained together with token chain
943 // and flag operands which copy the outgoing args into registers.
944 SDOperand InFlag;
945 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
946 unsigned CCReg;
947 SDOperand RegToPass = RegValuesToPass[i];
948 switch (RegToPass.getValueType()) {
949 default: assert(0 && "Bad thing to pass in regs");
950 case MVT::i8:
951 CCReg = (i == 0) ? X86::AL : X86::DL;
952 break;
953 case MVT::i16:
954 CCReg = (i == 0) ? X86::AX : X86::DX;
955 break;
956 case MVT::i32:
957 CCReg = (i == 0) ? X86::EAX : X86::EDX;
958 break;
959 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000960
Evan Cheng172fce72006-01-06 00:43:03 +0000961 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
962 InFlag = Chain.getValue(1);
963 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000964
Evan Cheng172fce72006-01-06 00:43:03 +0000965 std::vector<MVT::ValueType> NodeTys;
966 NodeTys.push_back(MVT::Other); // Returns a chain
967 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +0000968 std::vector<SDOperand> Ops;
969 Ops.push_back(Chain);
970 Ops.push_back(Callee);
971 if (InFlag.Val)
972 Ops.push_back(InFlag);
973
974 // FIXME: Do not generate X86ISD::TAILCALL for now.
975 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
976 InFlag = Chain.getValue(1);
977
Chris Lattner6f33eae2006-01-24 05:17:12 +0000978 NodeTys.clear();
979 NodeTys.push_back(MVT::Other); // Returns a chain
980 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
981 Ops.clear();
982 Ops.push_back(Chain);
983 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
984 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
985 Ops.push_back(InFlag);
986 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
987 InFlag = Chain.getValue(1);
988
Evan Cheng172fce72006-01-06 00:43:03 +0000989 SDOperand RetVal;
990 if (RetTyVT != MVT::isVoid) {
991 switch (RetTyVT) {
992 default: assert(0 && "Unknown value type to return!");
993 case MVT::i1:
994 case MVT::i8:
995 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
996 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000997 if (RetTyVT == MVT::i1)
998 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +0000999 break;
1000 case MVT::i16:
1001 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1002 Chain = RetVal.getValue(1);
1003 break;
1004 case MVT::i32:
1005 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1006 Chain = RetVal.getValue(1);
1007 break;
1008 case MVT::i64: {
1009 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1010 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1011 Lo.getValue(2));
1012 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1013 Chain = Hi.getValue(1);
1014 break;
1015 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001016 case MVT::f32:
Evan Cheng172fce72006-01-06 00:43:03 +00001017 case MVT::f64: {
1018 std::vector<MVT::ValueType> Tys;
1019 Tys.push_back(MVT::f64);
1020 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +00001021 Tys.push_back(MVT::Flag);
Evan Cheng172fce72006-01-06 00:43:03 +00001022 std::vector<SDOperand> Ops;
1023 Ops.push_back(Chain);
1024 Ops.push_back(InFlag);
1025 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +00001026 Chain = RetVal.getValue(1);
1027 InFlag = RetVal.getValue(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001028 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001029 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1030 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +00001031 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng172fce72006-01-06 00:43:03 +00001032 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001033 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng172fce72006-01-06 00:43:03 +00001034 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1035 Tys.clear();
1036 Tys.push_back(MVT::Other);
1037 Ops.clear();
1038 Ops.push_back(Chain);
1039 Ops.push_back(RetVal);
1040 Ops.push_back(StackSlot);
1041 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +00001042 Ops.push_back(InFlag);
Evan Cheng172fce72006-01-06 00:43:03 +00001043 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1044 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1045 DAG.getSrcValue(NULL));
1046 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +00001047 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001048
1049 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1050 // FIXME: we would really like to remember that this FP_ROUND
1051 // operation is okay to eliminate if we allow excess FP precision.
1052 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001053 break;
1054 }
1055 }
1056 }
1057
Evan Cheng172fce72006-01-06 00:43:03 +00001058 return std::make_pair(RetVal, Chain);
1059 } else {
1060 std::vector<SDOperand> Ops;
1061 Ops.push_back(Chain);
1062 Ops.push_back(Callee);
1063 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1064 // Callee pops all arg values on the stack.
1065 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1066
1067 // Pass register arguments as needed.
1068 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1069
1070 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1071 RetVals, Ops);
1072 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1073
1074 SDOperand ResultVal;
1075 switch (RetTyVT) {
1076 case MVT::isVoid: break;
1077 default:
1078 ResultVal = TheCall.getValue(1);
1079 break;
1080 case MVT::i1:
1081 case MVT::i8:
1082 case MVT::i16:
1083 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1084 break;
1085 case MVT::f32:
1086 // FIXME: we would really like to remember that this FP_ROUND operation is
1087 // okay to eliminate if we allow excess FP precision.
1088 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1089 break;
1090 case MVT::i64:
1091 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1092 TheCall.getValue(2));
1093 break;
1094 }
1095
1096 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001097 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001098}
1099
1100SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1101 if (ReturnAddrIndex == 0) {
1102 // Set up a frame object for the return address.
1103 MachineFunction &MF = DAG.getMachineFunction();
1104 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1105 }
1106
1107 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1108}
1109
1110
1111
1112std::pair<SDOperand, SDOperand> X86TargetLowering::
1113LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1114 SelectionDAG &DAG) {
1115 SDOperand Result;
1116 if (Depth) // Depths > 0 not supported yet!
1117 Result = DAG.getConstant(0, getPointerTy());
1118 else {
1119 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1120 if (!isFrameAddress)
1121 // Just load the return address
1122 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1123 DAG.getSrcValue(NULL));
1124 else
1125 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1126 DAG.getConstant(4, MVT::i32));
1127 }
1128 return std::make_pair(Result, Chain);
1129}
1130
Evan Cheng339edad2006-01-11 00:33:36 +00001131/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1132/// which corresponds to the condition code.
1133static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1134 switch (X86CC) {
1135 default: assert(0 && "Unknown X86 conditional code!");
1136 case X86ISD::COND_A: return X86::JA;
1137 case X86ISD::COND_AE: return X86::JAE;
1138 case X86ISD::COND_B: return X86::JB;
1139 case X86ISD::COND_BE: return X86::JBE;
1140 case X86ISD::COND_E: return X86::JE;
1141 case X86ISD::COND_G: return X86::JG;
1142 case X86ISD::COND_GE: return X86::JGE;
1143 case X86ISD::COND_L: return X86::JL;
1144 case X86ISD::COND_LE: return X86::JLE;
1145 case X86ISD::COND_NE: return X86::JNE;
1146 case X86ISD::COND_NO: return X86::JNO;
1147 case X86ISD::COND_NP: return X86::JNP;
1148 case X86ISD::COND_NS: return X86::JNS;
1149 case X86ISD::COND_O: return X86::JO;
1150 case X86ISD::COND_P: return X86::JP;
1151 case X86ISD::COND_S: return X86::JS;
1152 }
1153}
Chris Lattner76ac0682005-11-15 00:40:23 +00001154
Evan Cheng339edad2006-01-11 00:33:36 +00001155/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1156/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Cheng172fce72006-01-06 00:43:03 +00001157/// do a direct translation.
Evan Cheng339edad2006-01-11 00:33:36 +00001158static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Cheng172fce72006-01-06 00:43:03 +00001159 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1160 unsigned X86CC = X86ISD::COND_INVALID;
1161 if (!isFP) {
1162 switch (SetCCOpcode) {
1163 default: break;
1164 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1165 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1166 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1167 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1168 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1169 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1170 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1171 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1172 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1173 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1174 }
1175 } else {
1176 // On a floating point condition, the flags are set as follows:
1177 // ZF PF CF op
1178 // 0 | 0 | 0 | X > Y
1179 // 0 | 0 | 1 | X < Y
1180 // 1 | 0 | 0 | X == Y
1181 // 1 | 1 | 1 | unordered
1182 switch (SetCCOpcode) {
1183 default: break;
1184 case ISD::SETUEQ:
1185 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1186 case ISD::SETOGT:
1187 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1188 case ISD::SETOGE:
1189 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1190 case ISD::SETULT:
1191 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1192 case ISD::SETULE:
1193 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1194 case ISD::SETONE:
1195 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1196 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1197 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1198 }
1199 }
1200 return X86CC;
1201}
1202
Evan Cheng339edad2006-01-11 00:33:36 +00001203/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1204/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001205/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001206static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001207 switch (X86CC) {
1208 default:
1209 return false;
1210 case X86ISD::COND_B:
1211 case X86ISD::COND_BE:
1212 case X86ISD::COND_E:
1213 case X86ISD::COND_P:
1214 case X86ISD::COND_A:
1215 case X86ISD::COND_AE:
1216 case X86ISD::COND_NE:
1217 case X86ISD::COND_NP:
1218 return true;
1219 }
1220}
1221
Evan Cheng339edad2006-01-11 00:33:36 +00001222MachineBasicBlock *
1223X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1224 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001225 switch (MI->getOpcode()) {
1226 default: assert(false && "Unexpected instr type to insert");
1227 case X86::CMOV_FR32:
1228 case X86::CMOV_FR64: {
1229 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1230 // control-flow pattern. The incoming instruction knows the destination vreg
1231 // to set, the condition code register to branch on, the true/false values to
1232 // select between, and a branch opcode to use.
1233 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1234 ilist<MachineBasicBlock>::iterator It = BB;
1235 ++It;
1236
1237 // thisMBB:
1238 // ...
1239 // TrueVal = ...
1240 // cmpTY ccX, r1, r2
1241 // bCC copy1MBB
1242 // fallthrough --> copy0MBB
1243 MachineBasicBlock *thisMBB = BB;
1244 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1245 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1246 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1247 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1248 MachineFunction *F = BB->getParent();
1249 F->getBasicBlockList().insert(It, copy0MBB);
1250 F->getBasicBlockList().insert(It, sinkMBB);
1251 // Update machine-CFG edges
1252 BB->addSuccessor(copy0MBB);
1253 BB->addSuccessor(sinkMBB);
1254
1255 // copy0MBB:
1256 // %FalseValue = ...
1257 // # fallthrough to sinkMBB
1258 BB = copy0MBB;
1259
1260 // Update machine-CFG edges
1261 BB->addSuccessor(sinkMBB);
1262
1263 // sinkMBB:
1264 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1265 // ...
1266 BB = sinkMBB;
1267 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1268 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1269 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001270
Evan Cheng911c68d2006-01-16 21:21:29 +00001271 delete MI; // The pseudo instruction is gone now.
1272 return BB;
1273 }
Evan Cheng339edad2006-01-11 00:33:36 +00001274
Evan Cheng911c68d2006-01-16 21:21:29 +00001275 case X86::FP_TO_INT16_IN_MEM:
1276 case X86::FP_TO_INT32_IN_MEM:
1277 case X86::FP_TO_INT64_IN_MEM: {
1278 // Change the floating point control register to use "round towards zero"
1279 // mode when truncating to an integer value.
1280 MachineFunction *F = BB->getParent();
1281 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1282 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1283
1284 // Load the old value of the high byte of the control word...
1285 unsigned OldCW =
1286 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1287 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1288
1289 // Set the high part to be round to zero...
1290 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1291
1292 // Reload the modified control word now...
1293 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1294
1295 // Restore the memory image of control word to original value
1296 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1297
1298 // Get the X86 opcode to use.
1299 unsigned Opc;
1300 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001301 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001302 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1303 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1304 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1305 }
1306
1307 X86AddressMode AM;
1308 MachineOperand &Op = MI->getOperand(0);
1309 if (Op.isRegister()) {
1310 AM.BaseType = X86AddressMode::RegBase;
1311 AM.Base.Reg = Op.getReg();
1312 } else {
1313 AM.BaseType = X86AddressMode::FrameIndexBase;
1314 AM.Base.FrameIndex = Op.getFrameIndex();
1315 }
1316 Op = MI->getOperand(1);
1317 if (Op.isImmediate())
1318 AM.Scale = Op.getImmedValue();
1319 Op = MI->getOperand(2);
1320 if (Op.isImmediate())
1321 AM.IndexReg = Op.getImmedValue();
1322 Op = MI->getOperand(3);
1323 if (Op.isGlobalAddress()) {
1324 AM.GV = Op.getGlobal();
1325 } else {
1326 AM.Disp = Op.getImmedValue();
1327 }
1328 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1329
1330 // Reload the original control word now.
1331 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1332
1333 delete MI; // The pseudo instruction is gone now.
1334 return BB;
1335 }
1336 }
Evan Cheng339edad2006-01-11 00:33:36 +00001337}
1338
1339
1340//===----------------------------------------------------------------------===//
1341// X86 Custom Lowering Hooks
1342//===----------------------------------------------------------------------===//
1343
Chris Lattner76ac0682005-11-15 00:40:23 +00001344/// LowerOperation - Provide custom lowering hooks for some operations.
1345///
1346SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1347 switch (Op.getOpcode()) {
1348 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001349 case ISD::ADD_PARTS:
1350 case ISD::SUB_PARTS: {
1351 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1352 "Not an i64 add/sub!");
1353 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1354 std::vector<MVT::ValueType> Tys;
1355 Tys.push_back(MVT::i32);
1356 Tys.push_back(MVT::Flag);
1357 std::vector<SDOperand> Ops;
1358 Ops.push_back(Op.getOperand(0));
1359 Ops.push_back(Op.getOperand(2));
1360 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1361 Tys, Ops);
1362 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1363 Op.getOperand(1), Op.getOperand(3),
1364 Lo.getValue(1));
1365 Tys.clear();
1366 Tys.push_back(MVT::i32);
1367 Tys.push_back(MVT::i32);
1368 Ops.clear();
1369 Ops.push_back(Lo);
1370 Ops.push_back(Hi);
1371 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1372 }
1373 case ISD::SHL_PARTS:
1374 case ISD::SRA_PARTS:
1375 case ISD::SRL_PARTS: {
1376 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1377 "Not an i64 shift!");
1378 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1379 SDOperand ShOpLo = Op.getOperand(0);
1380 SDOperand ShOpHi = Op.getOperand(1);
1381 SDOperand ShAmt = Op.getOperand(2);
1382 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001383 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001384 : DAG.getConstant(0, MVT::i32);
1385
1386 SDOperand Tmp2, Tmp3;
1387 if (Op.getOpcode() == ISD::SHL_PARTS) {
1388 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1389 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1390 } else {
1391 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001392 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001393 }
1394
1395 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1396 ShAmt, DAG.getConstant(32, MVT::i8));
1397
1398 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001399 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001400
1401 std::vector<MVT::ValueType> Tys;
1402 Tys.push_back(MVT::i32);
1403 Tys.push_back(MVT::Flag);
1404 std::vector<SDOperand> Ops;
1405 if (Op.getOpcode() == ISD::SHL_PARTS) {
1406 Ops.push_back(Tmp2);
1407 Ops.push_back(Tmp3);
1408 Ops.push_back(CC);
1409 Ops.push_back(InFlag);
1410 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1411 InFlag = Hi.getValue(1);
1412
1413 Ops.clear();
1414 Ops.push_back(Tmp3);
1415 Ops.push_back(Tmp1);
1416 Ops.push_back(CC);
1417 Ops.push_back(InFlag);
1418 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1419 } else {
1420 Ops.push_back(Tmp2);
1421 Ops.push_back(Tmp3);
1422 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001423 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001424 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1425 InFlag = Lo.getValue(1);
1426
1427 Ops.clear();
1428 Ops.push_back(Tmp3);
1429 Ops.push_back(Tmp1);
1430 Ops.push_back(CC);
1431 Ops.push_back(InFlag);
1432 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1433 }
1434
1435 Tys.clear();
1436 Tys.push_back(MVT::i32);
1437 Tys.push_back(MVT::i32);
1438 Ops.clear();
1439 Ops.push_back(Lo);
1440 Ops.push_back(Hi);
1441 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1442 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001443 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001444 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001445 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001446 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001447
1448 SDOperand Result;
1449 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1450 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001451 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001452 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001453 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001454 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1455 DAG.getEntryNode(), Op.getOperand(0),
1456 StackSlot, DAG.getSrcValue(NULL));
1457
1458 // Build the FILD
1459 std::vector<MVT::ValueType> Tys;
1460 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001461 Tys.push_back(MVT::Other);
Evan Cheng6305e502006-01-12 22:54:21 +00001462 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001463 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001464 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001465 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001466 Ops.push_back(DAG.getValueType(SrcVT));
1467 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001468
1469 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001470 Chain = Result.getValue(1);
1471 SDOperand InFlag = Result.getValue(2);
1472
1473 // FIXME: Currently the FST is flagged to the FILD. This
1474 // shouldn't be necessary except that RFP cannot be live across
1475 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1476 MachineFunction &MF = DAG.getMachineFunction();
1477 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1478 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1479 std::vector<MVT::ValueType> Tys;
1480 Tys.push_back(MVT::Other);
1481 std::vector<SDOperand> Ops;
1482 Ops.push_back(Chain);
1483 Ops.push_back(Result);
1484 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001485 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001486 Ops.push_back(InFlag);
1487 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1488 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1489 DAG.getSrcValue(NULL));
1490 }
1491
Evan Cheng6305e502006-01-12 22:54:21 +00001492 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001493 }
1494 case ISD::FP_TO_SINT: {
1495 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001496 "Unknown FP_TO_SINT to lower!");
1497 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1498 // stack slot.
1499 MachineFunction &MF = DAG.getMachineFunction();
1500 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1501 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1502 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1503
1504 unsigned Opc;
1505 switch (Op.getValueType()) {
1506 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1507 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1508 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1509 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1510 }
1511
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001512 SDOperand Chain = DAG.getEntryNode();
1513 SDOperand Value = Op.getOperand(0);
1514 if (X86ScalarSSE) {
1515 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1516 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1517 DAG.getSrcValue(0));
1518 std::vector<MVT::ValueType> Tys;
1519 Tys.push_back(MVT::f64);
1520 Tys.push_back(MVT::Other);
1521 std::vector<SDOperand> Ops;
1522 Ops.push_back(Chain);
1523 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001524 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001525 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1526 Chain = Value.getValue(1);
1527 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1528 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1529 }
1530
Chris Lattner76ac0682005-11-15 00:40:23 +00001531 // Build the FP_TO_INT*_IN_MEM
1532 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001533 Ops.push_back(Chain);
1534 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001535 Ops.push_back(StackSlot);
1536 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1537
1538 // Load the result.
1539 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1540 DAG.getSrcValue(NULL));
1541 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001542 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001543 std::vector<MVT::ValueType> Tys;
1544 Tys.push_back(MVT::Other);
1545 Tys.push_back(MVT::Flag);
1546 std::vector<SDOperand> Ops;
1547 Ops.push_back(Op.getOperand(0));
1548 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001549 Ops.clear();
1550 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1551 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1552 MVT::i32, Ops[0].getValue(2)));
1553 Ops.push_back(Ops[1].getValue(1));
1554 Tys[0] = Tys[1] = MVT::i32;
1555 Tys.push_back(MVT::Other);
1556 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001557 }
Evan Chengc1583db2005-12-21 20:21:51 +00001558 case ISD::SETCC: {
1559 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1560 SDOperand CC = Op.getOperand(2);
1561 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1562 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001563 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1564 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001565 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001566 if (X86CC != X86ISD::COND_INVALID) {
1567 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1568 DAG.getConstant(X86CC, MVT::i8), Cond);
1569 } else {
1570 assert(isFP && "Illegal integer SetCC!");
1571
1572 std::vector<MVT::ValueType> Tys;
1573 std::vector<SDOperand> Ops;
1574 switch (SetCCOpcode) {
1575 default: assert(false && "Illegal floating point SetCC!");
1576 case ISD::SETOEQ: { // !PF & ZF
1577 Tys.push_back(MVT::i8);
1578 Tys.push_back(MVT::Flag);
1579 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1580 Ops.push_back(Cond);
1581 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1582 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1583 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1584 Tmp1.getValue(1));
1585 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1586 }
1587 case ISD::SETOLT: { // !PF & CF
1588 Tys.push_back(MVT::i8);
1589 Tys.push_back(MVT::Flag);
1590 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1591 Ops.push_back(Cond);
1592 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1593 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1594 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1595 Tmp1.getValue(1));
1596 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1597 }
1598 case ISD::SETOLE: { // !PF & (CF || ZF)
1599 Tys.push_back(MVT::i8);
1600 Tys.push_back(MVT::Flag);
1601 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1602 Ops.push_back(Cond);
1603 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1604 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1605 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1606 Tmp1.getValue(1));
1607 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1608 }
1609 case ISD::SETUGT: { // PF | (!ZF & !CF)
1610 Tys.push_back(MVT::i8);
1611 Tys.push_back(MVT::Flag);
1612 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1613 Ops.push_back(Cond);
1614 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1615 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1616 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1617 Tmp1.getValue(1));
1618 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1619 }
1620 case ISD::SETUGE: { // PF | !CF
1621 Tys.push_back(MVT::i8);
1622 Tys.push_back(MVT::Flag);
1623 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1624 Ops.push_back(Cond);
1625 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1626 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1627 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1628 Tmp1.getValue(1));
1629 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1630 }
1631 case ISD::SETUNE: { // PF | !ZF
1632 Tys.push_back(MVT::i8);
1633 Tys.push_back(MVT::Flag);
1634 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1635 Ops.push_back(Cond);
1636 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1637 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1638 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1639 Tmp1.getValue(1));
1640 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1641 }
1642 }
1643 }
Evan Chengc1583db2005-12-21 20:21:51 +00001644 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001645 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001646 MVT::ValueType VT = Op.getValueType();
1647 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001648 bool isFPStack = isFP && !X86ScalarSSE;
1649 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001650 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001651 SDOperand Op0 = Op.getOperand(0);
1652 SDOperand Cond, CC;
1653 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001654 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1655 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1656 // have another use it will be eliminated.
1657 // If the X86ISD::SETCC has more than one use, then it's probably better
1658 // to use a test instead of duplicating the X86ISD::CMP (for register
1659 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001660 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1661 if (!Op0.hasOneUse()) {
1662 std::vector<MVT::ValueType> Tys;
1663 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1664 Tys.push_back(Op0.Val->getValueType(i));
1665 std::vector<SDOperand> Ops;
1666 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1667 Ops.push_back(Op0.getOperand(i));
1668 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1669 }
1670
Evan Chengfb22e862006-01-13 01:03:02 +00001671 CC = Op0.getOperand(0);
1672 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001673 // Make a copy as flag result cannot be used by more than one.
1674 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1675 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001676 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001677 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001678 } else
1679 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001680 } else if (Op0.getOpcode() == ISD::SETCC) {
1681 CC = Op0.getOperand(2);
1682 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001683 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001684 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001685 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001686 Op0.getOperand(0), Op0.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001687 } else
1688 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001689
Evan Cheng731423f2006-01-13 01:06:49 +00001690 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001691 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001692 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001693 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001694
1695 std::vector<MVT::ValueType> Tys;
1696 Tys.push_back(Op.getValueType());
1697 Tys.push_back(MVT::Flag);
1698 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001699 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1700 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001701 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001702 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001703 Ops.push_back(CC);
1704 Ops.push_back(Cond);
1705 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001706 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001707 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001708 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001709 SDOperand Cond = Op.getOperand(1);
1710 SDOperand Dest = Op.getOperand(2);
1711 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001712 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001713 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1714 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1715 // have another use it will be eliminated.
1716 // If the X86ISD::SETCC has more than one use, then it's probably better
1717 // to use a test instead of duplicating the X86ISD::CMP (for register
1718 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001719 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1720 if (!Cond.hasOneUse()) {
1721 std::vector<MVT::ValueType> Tys;
1722 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1723 Tys.push_back(Cond.Val->getValueType(i));
1724 std::vector<SDOperand> Ops;
1725 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1726 Ops.push_back(Cond.getOperand(i));
1727 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1728 }
1729
Evan Chengfb22e862006-01-13 01:03:02 +00001730 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001731 Cond = Cond.getOperand(1);
1732 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001733 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001734 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001735 } else
1736 addTest = true;
Evan Chengc1583db2005-12-21 20:21:51 +00001737 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001738 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001739 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001740 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001741 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001742 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1743 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001744 } else
1745 addTest = true;
1746
1747 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001748 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001749 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1750 }
1751 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1752 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1753 }
Evan Chengae986f12006-01-11 22:15:48 +00001754 case ISD::MEMSET: {
1755 SDOperand InFlag;
1756 SDOperand Chain = Op.getOperand(0);
1757 unsigned Align =
1758 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1759 if (Align == 0) Align = 1;
1760
1761 MVT::ValueType AVT;
1762 SDOperand Count;
1763 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1764 unsigned ValReg;
1765 unsigned Val = ValC->getValue() & 255;
1766
1767 // If the value is a constant, then we can potentially use larger sets.
1768 switch (Align & 3) {
1769 case 2: // WORD aligned
1770 AVT = MVT::i16;
1771 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1772 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1773 else
1774 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1775 DAG.getConstant(1, MVT::i8));
1776 Val = (Val << 8) | Val;
1777 ValReg = X86::AX;
1778 break;
1779 case 0: // DWORD aligned
1780 AVT = MVT::i32;
1781 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1782 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1783 else
1784 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1785 DAG.getConstant(2, MVT::i8));
1786 Val = (Val << 8) | Val;
1787 Val = (Val << 16) | Val;
1788 ValReg = X86::EAX;
1789 break;
1790 default: // Byte aligned
1791 AVT = MVT::i8;
1792 Count = Op.getOperand(3);
1793 ValReg = X86::AL;
1794 break;
1795 }
1796
1797 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1798 InFlag);
1799 InFlag = Chain.getValue(1);
1800 } else {
1801 AVT = MVT::i8;
1802 Count = Op.getOperand(3);
1803 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1804 InFlag = Chain.getValue(1);
1805 }
1806
1807 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1808 InFlag = Chain.getValue(1);
1809 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1810 InFlag = Chain.getValue(1);
1811
1812 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1813 DAG.getValueType(AVT), InFlag);
1814 }
1815 case ISD::MEMCPY: {
1816 SDOperand Chain = Op.getOperand(0);
1817 unsigned Align =
1818 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1819 if (Align == 0) Align = 1;
1820
1821 MVT::ValueType AVT;
1822 SDOperand Count;
1823 switch (Align & 3) {
1824 case 2: // WORD aligned
1825 AVT = MVT::i16;
1826 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1827 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1828 else
1829 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1830 DAG.getConstant(1, MVT::i8));
1831 break;
1832 case 0: // DWORD aligned
1833 AVT = MVT::i32;
1834 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1835 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1836 else
1837 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1838 DAG.getConstant(2, MVT::i8));
1839 break;
1840 default: // Byte aligned
1841 AVT = MVT::i8;
1842 Count = Op.getOperand(3);
1843 break;
1844 }
1845
1846 SDOperand InFlag;
1847 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1848 InFlag = Chain.getValue(1);
1849 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1850 InFlag = Chain.getValue(1);
1851 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1852 InFlag = Chain.getValue(1);
1853
1854 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1855 DAG.getValueType(AVT), InFlag);
1856 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001857 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001858 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001859 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1860 // For Darwin, external and weak symbols are indirect, so we want to load
1861 // the value at address GV, not the value of GV itself. This means that
1862 // the GlobalAddress must be in the base or index register of the address,
1863 // not the GV offset field.
1864 if (getTargetMachine().
1865 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1866 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001867 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1868 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1869 DAG.getSrcValue(NULL));
1870 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001871 }
Nate Begemane74795c2006-01-25 18:21:52 +00001872 case ISD::VASTART: {
1873 // vastart just stores the address of the VarArgsFrameIndex slot into the
1874 // memory location argument.
1875 // FIXME: Replace MVT::i32 with PointerTy
1876 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1877 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1878 Op.getOperand(1), Op.getOperand(2));
1879 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001880 case ISD::RET: {
1881 SDOperand Copy;
1882
1883 switch(Op.getNumOperands()) {
1884 default:
1885 assert(0 && "Do not know how to return this many arguments!");
1886 abort();
1887 case 1:
1888 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1889 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1890 case 2: {
1891 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1892 if (MVT::isInteger(ArgVT))
1893 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1894 SDOperand());
1895 else if (!X86ScalarSSE) {
1896 std::vector<MVT::ValueType> Tys;
1897 Tys.push_back(MVT::Other);
1898 Tys.push_back(MVT::Flag);
1899 std::vector<SDOperand> Ops;
1900 Ops.push_back(Op.getOperand(0));
1901 Ops.push_back(Op.getOperand(1));
1902 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1903 } else {
1904 // Spill the value to memory and reload it into top of stack.
1905 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1906 MachineFunction &MF = DAG.getMachineFunction();
1907 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1908 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1909 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1910 Op.getOperand(1), StackSlot,
1911 DAG.getSrcValue(0));
1912 std::vector<MVT::ValueType> Tys;
1913 Tys.push_back(MVT::f64);
1914 Tys.push_back(MVT::Other);
1915 std::vector<SDOperand> Ops;
1916 Ops.push_back(Chain);
1917 Ops.push_back(StackSlot);
1918 Ops.push_back(DAG.getValueType(ArgVT));
1919 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1920 Tys.clear();
1921 Tys.push_back(MVT::Other);
1922 Tys.push_back(MVT::Flag);
1923 Ops.clear();
1924 Ops.push_back(Copy.getValue(1));
1925 Ops.push_back(Copy);
1926 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1927 }
1928 break;
1929 }
1930 case 3:
1931 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1932 SDOperand());
1933 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1934 break;
1935 }
1936 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1937 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1938 Copy.getValue(1));
1939 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001940 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001941}
Evan Cheng6af02632005-12-20 06:22:03 +00001942
1943const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1944 switch (Opcode) {
1945 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001946 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1947 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1948 case X86ISD::ADC: return "X86ISD::ADC";
1949 case X86ISD::SBB: return "X86ISD::SBB";
1950 case X86ISD::SHLD: return "X86ISD::SHLD";
1951 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6305e502006-01-12 22:54:21 +00001952 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001953 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1954 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1955 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001956 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001957 case X86ISD::FST: return "X86ISD::FST";
1958 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001959 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001960 case X86ISD::CALL: return "X86ISD::CALL";
1961 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1962 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1963 case X86ISD::CMP: return "X86ISD::CMP";
1964 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001965 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001966 case X86ISD::CMOV: return "X86ISD::CMOV";
1967 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001968 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001969 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1970 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001971 }
1972}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001973
1974bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerf0b24d22006-01-30 04:09:27 +00001975 uint64_t Mask) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001976
1977 unsigned Opc = Op.getOpcode();
1978
1979 switch (Opc) {
1980 default:
1981 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1982 break;
1983 case X86ISD::SETCC: return (Mask & 1) == 0;
1984 }
1985
1986 return false;
1987}