blob: 4eecb3031422e70e4efe84fdad35c15397bd8a97 [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"
Evan Cheng2dd217b2006-01-31 03:14:29 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattnerc642aa52006-01-31 19:43:35 +000028#include "llvm/ADT/VectorExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000029using namespace llvm;
30
31// FIXME: temporary.
32#include "llvm/Support/CommandLine.h"
33static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
34 cl::desc("Enable fastcc on X86"));
35
36X86TargetLowering::X86TargetLowering(TargetMachine &TM)
37 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000038 Subtarget = &TM.getSubtarget<X86Subtarget>();
39 X86ScalarSSE = Subtarget->hasSSE2();
40
Chris Lattner76ac0682005-11-15 00:40:23 +000041 // Set up the TargetLowering object.
42
43 // X86 is weird, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
46 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000047 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000048 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000049 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000050
Chris Lattner76ac0682005-11-15 00:40:23 +000051 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000052 addRegisterClass(MVT::i8, X86::R8RegisterClass);
53 addRegisterClass(MVT::i16, X86::R16RegisterClass);
54 addRegisterClass(MVT::i32, X86::R32RegisterClass);
55
56 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
57 // operation.
58 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
59 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000061
62 if (X86ScalarSSE)
63 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
64 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
65 else
66 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000067
68 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
69 // this operation.
70 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
71 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Evan Cheng08390f62006-01-30 22:13:22 +000072 if (X86ScalarSSE)
73 // SSE has no i16 to fp conversion, only i32
74 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
75 else if (!X86PatIsel) {
76 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
77 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
78 }
Chris Lattner76ac0682005-11-15 00:40:23 +000079
Evan Cheng5b97fcf2006-01-30 08:02:57 +000080 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
81 // isn't legal.
82 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
83 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
84
Evan Cheng08390f62006-01-30 22:13:22 +000085 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
86 // this operation.
87 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
88 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
89
90 if (X86ScalarSSE) {
91 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
92 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +000093 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +000095 }
96
97 // Handle FP_TO_UINT by promoting the destination to a larger signed
98 // conversion.
99 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
100 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
102
Evan Cheng08390f62006-01-30 22:13:22 +0000103 if (X86ScalarSSE)
104 // Expand FP_TO_UINT into a select.
105 // FIXME: We would like to use a Custom expander here eventually to do
106 // the optimal thing for SSE vs. the default expansion in the legalizer.
107 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
108 else
Chris Lattner76ac0682005-11-15 00:40:23 +0000109 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
110
Evan Cheng08390f62006-01-30 22:13:22 +0000111 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
112 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000113
Evan Chenga814f0b32006-01-27 21:26:54 +0000114 if (!X86PatIsel) {
Evan Cheng6fc31042005-12-19 23:12:38 +0000115 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
116 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000117 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
118 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
123 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
124 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
125 setOperationAction(ISD::FREM , MVT::f64 , Expand);
126 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
127 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
128 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
129 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
130 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
131 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
132 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
133 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
134 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000135 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000136
Evan Chenga814f0b32006-01-27 21:26:54 +0000137 if (X86PatIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000138 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000139 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
140 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
141 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
142 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
143 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
144 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
145 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000146 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000147
Chris Lattner76ac0682005-11-15 00:40:23 +0000148 setOperationAction(ISD::READIO , MVT::i1 , Expand);
149 setOperationAction(ISD::READIO , MVT::i8 , Expand);
150 setOperationAction(ISD::READIO , MVT::i16 , Expand);
151 setOperationAction(ISD::READIO , MVT::i32 , Expand);
152 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
153 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
154 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
155 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
156
157 // These should be promoted to a larger select which is supported.
158 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
159 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Chenga814f0b32006-01-27 21:26:54 +0000160 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000161 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000162 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
163 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000164 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
165 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000166 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
167 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
168 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000169 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
170 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
171 // X86 ret instruction may pop stack.
172 setOperationAction(ISD::RET , MVT::Other, Custom);
173 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000174 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000175 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
176 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
179 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
180 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000181 // X86 wants to expand memset / memcpy itself.
182 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
183 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000184 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000185
Chris Lattner9c415362005-11-29 06:16:21 +0000186 // We don't have line number support yet.
187 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000188 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
189 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000190
Nate Begemane74795c2006-01-25 18:21:52 +0000191 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
192 setOperationAction(ISD::VASTART , MVT::Other, Custom);
193
194 // Use the default implementation.
195 setOperationAction(ISD::VAARG , MVT::Other, Expand);
196 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
197 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000198 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
199 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
200 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000201
Chris Lattner76ac0682005-11-15 00:40:23 +0000202 if (X86ScalarSSE) {
203 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000204 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000206
207 // SSE has no load+extend ops
208 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
209 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
210
Chris Lattner76ac0682005-11-15 00:40:23 +0000211 // We don't support sin/cos/sqrt/fmod
212 setOperationAction(ISD::FSIN , MVT::f64, Expand);
213 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng2dd217b2006-01-31 03:14:29 +0000214 setOperationAction(ISD::FABS , MVT::f64, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000215 setOperationAction(ISD::FNEG , MVT::f64, Expand);
216 setOperationAction(ISD::FREM , MVT::f64, Expand);
217 setOperationAction(ISD::FSIN , MVT::f32, Expand);
218 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng2dd217b2006-01-31 03:14:29 +0000219 setOperationAction(ISD::FABS , MVT::f32, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000220 setOperationAction(ISD::FNEG , MVT::f32, Expand);
221 setOperationAction(ISD::FREM , MVT::f32, Expand);
222
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000223 // Expand FP immediates into loads from the stack, except for the special
224 // cases we handle.
225 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
226 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000227 addLegalFPImmediate(+0.0); // xorps / xorpd
228 } else {
229 // Set up the FP register classes.
230 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000231
232 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
233
Chris Lattner76ac0682005-11-15 00:40:23 +0000234 if (!UnsafeFPMath) {
235 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
236 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
237 }
238
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000239 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000240 addLegalFPImmediate(+0.0); // FLD0
241 addLegalFPImmediate(+1.0); // FLD1
242 addLegalFPImmediate(-0.0); // FLD0/FCHS
243 addLegalFPImmediate(-1.0); // FLD1/FCHS
244 }
245 computeRegisterProperties();
246
247 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
248 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
249 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
250 allowUnalignedMemoryAccesses = true; // x86 supports it!
251}
252
253std::vector<SDOperand>
254X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
255 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
256 return LowerFastCCArguments(F, DAG);
257 return LowerCCCArguments(F, DAG);
258}
259
260std::pair<SDOperand, SDOperand>
261X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
262 bool isVarArg, unsigned CallingConv,
263 bool isTailCall,
264 SDOperand Callee, ArgListTy &Args,
265 SelectionDAG &DAG) {
266 assert((!isVarArg || CallingConv == CallingConv::C) &&
267 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000268
269 // If the callee is a GlobalAddress node (quite common, every direct call is)
270 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
271 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
272 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000273 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
274 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000275
Chris Lattner76ac0682005-11-15 00:40:23 +0000276 if (CallingConv == CallingConv::Fast && EnableFastCC)
277 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
278 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
279}
280
281//===----------------------------------------------------------------------===//
282// C Calling Convention implementation
283//===----------------------------------------------------------------------===//
284
285std::vector<SDOperand>
286X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
287 std::vector<SDOperand> ArgValues;
288
289 MachineFunction &MF = DAG.getMachineFunction();
290 MachineFrameInfo *MFI = MF.getFrameInfo();
291
292 // Add DAG nodes to load the arguments... On entry to a function on the X86,
293 // the stack frame looks like this:
294 //
295 // [ESP] -- return address
296 // [ESP + 4] -- first argument (leftmost lexically)
297 // [ESP + 8] -- second argument, if first argument is four bytes in size
298 // ...
299 //
300 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
301 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
302 MVT::ValueType ObjectVT = getValueType(I->getType());
303 unsigned ArgIncrement = 4;
304 unsigned ObjSize;
305 switch (ObjectVT) {
306 default: assert(0 && "Unhandled argument type!");
307 case MVT::i1:
308 case MVT::i8: ObjSize = 1; break;
309 case MVT::i16: ObjSize = 2; break;
310 case MVT::i32: ObjSize = 4; break;
311 case MVT::i64: ObjSize = ArgIncrement = 8; break;
312 case MVT::f32: ObjSize = 4; break;
313 case MVT::f64: ObjSize = ArgIncrement = 8; break;
314 }
315 // Create the frame index object for this incoming parameter...
316 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
317
318 // Create the SelectionDAG nodes corresponding to a load from this parameter
319 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
320
321 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
322 // dead loads.
323 SDOperand ArgValue;
324 if (!I->use_empty())
325 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
326 DAG.getSrcValue(NULL));
327 else {
328 if (MVT::isInteger(ObjectVT))
329 ArgValue = DAG.getConstant(0, ObjectVT);
330 else
331 ArgValue = DAG.getConstantFP(0, ObjectVT);
332 }
333 ArgValues.push_back(ArgValue);
334
335 ArgOffset += ArgIncrement; // Move on to the next argument...
336 }
337
338 // If the function takes variable number of arguments, make a frame index for
339 // the start of the first vararg value... for expansion of llvm.va_start.
340 if (F.isVarArg())
341 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
342 ReturnAddrIndex = 0; // No return address slot generated yet.
343 BytesToPopOnReturn = 0; // Callee pops nothing.
344 BytesCallerReserves = ArgOffset;
345
346 // Finally, inform the code generator which regs we return values in.
347 switch (getValueType(F.getReturnType())) {
348 default: assert(0 && "Unknown type!");
349 case MVT::isVoid: break;
350 case MVT::i1:
351 case MVT::i8:
352 case MVT::i16:
353 case MVT::i32:
354 MF.addLiveOut(X86::EAX);
355 break;
356 case MVT::i64:
357 MF.addLiveOut(X86::EAX);
358 MF.addLiveOut(X86::EDX);
359 break;
360 case MVT::f32:
361 case MVT::f64:
362 MF.addLiveOut(X86::ST0);
363 break;
364 }
365 return ArgValues;
366}
367
368std::pair<SDOperand, SDOperand>
369X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
370 bool isVarArg, bool isTailCall,
371 SDOperand Callee, ArgListTy &Args,
372 SelectionDAG &DAG) {
373 // Count how many bytes are to be pushed on the stack.
374 unsigned NumBytes = 0;
375
376 if (Args.empty()) {
377 // Save zero bytes.
378 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
379 DAG.getConstant(0, getPointerTy()));
380 } else {
381 for (unsigned i = 0, e = Args.size(); i != e; ++i)
382 switch (getValueType(Args[i].second)) {
383 default: assert(0 && "Unknown value type!");
384 case MVT::i1:
385 case MVT::i8:
386 case MVT::i16:
387 case MVT::i32:
388 case MVT::f32:
389 NumBytes += 4;
390 break;
391 case MVT::i64:
392 case MVT::f64:
393 NumBytes += 8;
394 break;
395 }
396
397 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
398 DAG.getConstant(NumBytes, getPointerTy()));
399
400 // Arguments go on the stack in reverse order, as specified by the ABI.
401 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000402 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000403 std::vector<SDOperand> Stores;
404
405 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
406 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
407 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
408
409 switch (getValueType(Args[i].second)) {
410 default: assert(0 && "Unexpected ValueType for argument!");
411 case MVT::i1:
412 case MVT::i8:
413 case MVT::i16:
414 // Promote the integer to 32 bits. If the input type is signed use a
415 // sign extend, otherwise use a zero extend.
416 if (Args[i].second->isSigned())
417 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
418 else
419 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
420
421 // FALL THROUGH
422 case MVT::i32:
423 case MVT::f32:
424 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
425 Args[i].first, PtrOff,
426 DAG.getSrcValue(NULL)));
427 ArgOffset += 4;
428 break;
429 case MVT::i64:
430 case MVT::f64:
431 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
432 Args[i].first, PtrOff,
433 DAG.getSrcValue(NULL)));
434 ArgOffset += 8;
435 break;
436 }
437 }
438 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
439 }
440
441 std::vector<MVT::ValueType> RetVals;
442 MVT::ValueType RetTyVT = getValueType(RetTy);
443 RetVals.push_back(MVT::Other);
444
445 // The result values produced have to be legal. Promote the result.
446 switch (RetTyVT) {
447 case MVT::isVoid: break;
448 default:
449 RetVals.push_back(RetTyVT);
450 break;
451 case MVT::i1:
452 case MVT::i8:
453 case MVT::i16:
454 RetVals.push_back(MVT::i32);
455 break;
456 case MVT::f32:
457 if (X86ScalarSSE)
458 RetVals.push_back(MVT::f32);
459 else
460 RetVals.push_back(MVT::f64);
461 break;
462 case MVT::i64:
463 RetVals.push_back(MVT::i32);
464 RetVals.push_back(MVT::i32);
465 break;
466 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000467
Evan Chenga814f0b32006-01-27 21:26:54 +0000468 if (!X86PatIsel) {
Evan Cheng45e190982006-01-05 00:27:02 +0000469 std::vector<MVT::ValueType> NodeTys;
470 NodeTys.push_back(MVT::Other); // Returns a chain
471 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000472 std::vector<SDOperand> Ops;
473 Ops.push_back(Chain);
474 Ops.push_back(Callee);
475
Evan Cheng172fce72006-01-06 00:43:03 +0000476 // FIXME: Do not generate X86ISD::TAILCALL for now.
477 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000478 SDOperand InFlag = Chain.getValue(1);
479
Chris Lattner6f33eae2006-01-24 05:17:12 +0000480 NodeTys.clear();
481 NodeTys.push_back(MVT::Other); // Returns a chain
482 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
483 Ops.clear();
484 Ops.push_back(Chain);
485 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
486 Ops.push_back(DAG.getConstant(0, getPointerTy()));
487 Ops.push_back(InFlag);
488 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
489 InFlag = Chain.getValue(1);
490
Evan Cheng45e190982006-01-05 00:27:02 +0000491 SDOperand RetVal;
492 if (RetTyVT != MVT::isVoid) {
493 switch (RetTyVT) {
494 default: assert(0 && "Unknown value type to return!");
495 case MVT::i1:
496 case MVT::i8:
497 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
498 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000499 if (RetTyVT == MVT::i1)
500 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000501 break;
502 case MVT::i16:
503 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
504 Chain = RetVal.getValue(1);
505 break;
506 case MVT::i32:
507 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
508 Chain = RetVal.getValue(1);
509 break;
510 case MVT::i64: {
511 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
512 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
513 Lo.getValue(2));
514 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
515 Chain = Hi.getValue(1);
516 break;
517 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000518 case MVT::f32:
Evan Cheng45e190982006-01-05 00:27:02 +0000519 case MVT::f64: {
520 std::vector<MVT::ValueType> Tys;
521 Tys.push_back(MVT::f64);
522 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +0000523 Tys.push_back(MVT::Flag);
Evan Cheng45e190982006-01-05 00:27:02 +0000524 std::vector<SDOperand> Ops;
525 Ops.push_back(Chain);
526 Ops.push_back(InFlag);
527 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +0000528 Chain = RetVal.getValue(1);
529 InFlag = RetVal.getValue(2);
Evan Cheng45e190982006-01-05 00:27:02 +0000530 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000531 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
532 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +0000533 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng45e190982006-01-05 00:27:02 +0000534 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000535 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng45e190982006-01-05 00:27:02 +0000536 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
537 Tys.clear();
538 Tys.push_back(MVT::Other);
539 Ops.clear();
540 Ops.push_back(Chain);
541 Ops.push_back(RetVal);
542 Ops.push_back(StackSlot);
543 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +0000544 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000545 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
546 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
547 DAG.getSrcValue(NULL));
548 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +0000549 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000550
551 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
552 // FIXME: we would really like to remember that this FP_ROUND
553 // operation is okay to eliminate if we allow excess FP precision.
554 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000555 break;
556 }
557 }
558 }
559
Evan Cheng45e190982006-01-05 00:27:02 +0000560 return std::make_pair(RetVal, Chain);
561 } else {
562 std::vector<SDOperand> Ops;
563 Ops.push_back(Chain);
564 Ops.push_back(Callee);
565 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
566 Ops.push_back(DAG.getConstant(0, getPointerTy()));
567
Chris Lattnerc642aa52006-01-31 19:43:35 +0000568 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
Evan Cheng45e190982006-01-05 00:27:02 +0000569 RetVals, Ops);
570
571 SDOperand ResultVal;
572 switch (RetTyVT) {
573 case MVT::isVoid: break;
574 default:
575 ResultVal = TheCall.getValue(1);
576 break;
577 case MVT::i1:
578 case MVT::i8:
579 case MVT::i16:
580 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
581 break;
582 case MVT::f32:
583 // FIXME: we would really like to remember that this FP_ROUND operation is
584 // okay to eliminate if we allow excess FP precision.
585 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
586 break;
587 case MVT::i64:
588 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
589 TheCall.getValue(2));
590 break;
591 }
592
593 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
594 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000595 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000596}
597
Chris Lattner76ac0682005-11-15 00:40:23 +0000598//===----------------------------------------------------------------------===//
599// Fast Calling Convention implementation
600//===----------------------------------------------------------------------===//
601//
602// The X86 'fast' calling convention passes up to two integer arguments in
603// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
604// and requires that the callee pop its arguments off the stack (allowing proper
605// tail calls), and has the same return value conventions as C calling convs.
606//
607// This calling convention always arranges for the callee pop value to be 8n+4
608// bytes, which is needed for tail recursion elimination and stack alignment
609// reasons.
610//
611// Note that this can be enhanced in the future to pass fp vals in registers
612// (when we have a global fp allocator) and do other tricks.
613//
614
615/// AddLiveIn - This helper function adds the specified physical register to the
616/// MachineFunction as a live in value. It also creates a corresponding virtual
617/// register for it.
618static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
619 TargetRegisterClass *RC) {
620 assert(RC->contains(PReg) && "Not the correct regclass!");
621 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
622 MF.addLiveIn(PReg, VReg);
623 return VReg;
624}
625
626
627std::vector<SDOperand>
628X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
629 std::vector<SDOperand> ArgValues;
630
631 MachineFunction &MF = DAG.getMachineFunction();
632 MachineFrameInfo *MFI = MF.getFrameInfo();
633
634 // Add DAG nodes to load the arguments... On entry to a function the stack
635 // frame looks like this:
636 //
637 // [ESP] -- return address
638 // [ESP + 4] -- first nonreg argument (leftmost lexically)
639 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
640 // ...
641 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
642
643 // Keep track of the number of integer regs passed so far. This can be either
644 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
645 // used).
646 unsigned NumIntRegs = 0;
647
648 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
649 MVT::ValueType ObjectVT = getValueType(I->getType());
650 unsigned ArgIncrement = 4;
651 unsigned ObjSize = 0;
652 SDOperand ArgValue;
653
654 switch (ObjectVT) {
655 default: assert(0 && "Unhandled argument type!");
656 case MVT::i1:
657 case MVT::i8:
658 if (NumIntRegs < 2) {
659 if (!I->use_empty()) {
660 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
661 X86::R8RegisterClass);
662 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
663 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000664 if (ObjectVT == MVT::i1)
665 // FIXME: Should insert a assertzext here.
666 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000667 }
668 ++NumIntRegs;
669 break;
670 }
671
672 ObjSize = 1;
673 break;
674 case MVT::i16:
675 if (NumIntRegs < 2) {
676 if (!I->use_empty()) {
677 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
678 X86::R16RegisterClass);
679 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
680 DAG.setRoot(ArgValue.getValue(1));
681 }
682 ++NumIntRegs;
683 break;
684 }
685 ObjSize = 2;
686 break;
687 case MVT::i32:
688 if (NumIntRegs < 2) {
689 if (!I->use_empty()) {
690 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
691 X86::R32RegisterClass);
692 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
693 DAG.setRoot(ArgValue.getValue(1));
694 }
695 ++NumIntRegs;
696 break;
697 }
698 ObjSize = 4;
699 break;
700 case MVT::i64:
701 if (NumIntRegs == 0) {
702 if (!I->use_empty()) {
703 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
704 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
705
706 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
707 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
708 DAG.setRoot(Hi.getValue(1));
709
710 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
711 }
712 NumIntRegs = 2;
713 break;
714 } else if (NumIntRegs == 1) {
715 if (!I->use_empty()) {
716 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
717 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
718 DAG.setRoot(Low.getValue(1));
719
720 // Load the high part from memory.
721 // Create the frame index object for this incoming parameter...
722 int FI = MFI->CreateFixedObject(4, ArgOffset);
723 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
724 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
725 DAG.getSrcValue(NULL));
726 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
727 }
728 ArgOffset += 4;
729 NumIntRegs = 2;
730 break;
731 }
732 ObjSize = ArgIncrement = 8;
733 break;
734 case MVT::f32: ObjSize = 4; break;
735 case MVT::f64: ObjSize = ArgIncrement = 8; break;
736 }
737
738 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
739 // dead loads.
740 if (ObjSize && !I->use_empty()) {
741 // Create the frame index object for this incoming parameter...
742 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
743
744 // Create the SelectionDAG nodes corresponding to a load from this
745 // parameter.
746 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
747
748 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
749 DAG.getSrcValue(NULL));
750 } else if (ArgValue.Val == 0) {
751 if (MVT::isInteger(ObjectVT))
752 ArgValue = DAG.getConstant(0, ObjectVT);
753 else
754 ArgValue = DAG.getConstantFP(0, ObjectVT);
755 }
756 ArgValues.push_back(ArgValue);
757
758 if (ObjSize)
759 ArgOffset += ArgIncrement; // Move on to the next argument.
760 }
761
762 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
763 // arguments and the arguments after the retaddr has been pushed are aligned.
764 if ((ArgOffset & 7) == 0)
765 ArgOffset += 4;
766
767 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
768 ReturnAddrIndex = 0; // No return address slot generated yet.
769 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
770 BytesCallerReserves = 0;
771
772 // Finally, inform the code generator which regs we return values in.
773 switch (getValueType(F.getReturnType())) {
774 default: assert(0 && "Unknown type!");
775 case MVT::isVoid: break;
776 case MVT::i1:
777 case MVT::i8:
778 case MVT::i16:
779 case MVT::i32:
780 MF.addLiveOut(X86::EAX);
781 break;
782 case MVT::i64:
783 MF.addLiveOut(X86::EAX);
784 MF.addLiveOut(X86::EDX);
785 break;
786 case MVT::f32:
787 case MVT::f64:
788 MF.addLiveOut(X86::ST0);
789 break;
790 }
791 return ArgValues;
792}
793
794std::pair<SDOperand, SDOperand>
795X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
796 bool isTailCall, SDOperand Callee,
797 ArgListTy &Args, SelectionDAG &DAG) {
798 // Count how many bytes are to be pushed on the stack.
799 unsigned NumBytes = 0;
800
801 // Keep track of the number of integer regs passed so far. This can be either
802 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
803 // used).
804 unsigned NumIntRegs = 0;
805
806 for (unsigned i = 0, e = Args.size(); i != e; ++i)
807 switch (getValueType(Args[i].second)) {
808 default: assert(0 && "Unknown value type!");
809 case MVT::i1:
810 case MVT::i8:
811 case MVT::i16:
812 case MVT::i32:
813 if (NumIntRegs < 2) {
814 ++NumIntRegs;
815 break;
816 }
817 // fall through
818 case MVT::f32:
819 NumBytes += 4;
820 break;
821 case MVT::i64:
822 if (NumIntRegs == 0) {
823 NumIntRegs = 2;
824 break;
825 } else if (NumIntRegs == 1) {
826 NumIntRegs = 2;
827 NumBytes += 4;
828 break;
829 }
830
831 // fall through
832 case MVT::f64:
833 NumBytes += 8;
834 break;
835 }
836
837 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
838 // arguments and the arguments after the retaddr has been pushed are aligned.
839 if ((NumBytes & 7) == 0)
840 NumBytes += 4;
841
842 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
843 DAG.getConstant(NumBytes, getPointerTy()));
844
845 // Arguments go on the stack in reverse order, as specified by the ABI.
846 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000847 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000848 NumIntRegs = 0;
849 std::vector<SDOperand> Stores;
850 std::vector<SDOperand> RegValuesToPass;
851 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
852 switch (getValueType(Args[i].second)) {
853 default: assert(0 && "Unexpected ValueType for argument!");
854 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000855 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
856 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000857 case MVT::i8:
858 case MVT::i16:
859 case MVT::i32:
860 if (NumIntRegs < 2) {
861 RegValuesToPass.push_back(Args[i].first);
862 ++NumIntRegs;
863 break;
864 }
865 // Fall through
866 case MVT::f32: {
867 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
868 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
869 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
870 Args[i].first, PtrOff,
871 DAG.getSrcValue(NULL)));
872 ArgOffset += 4;
873 break;
874 }
875 case MVT::i64:
876 if (NumIntRegs < 2) { // Can pass part of it in regs?
877 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
878 Args[i].first, DAG.getConstant(1, MVT::i32));
879 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
880 Args[i].first, DAG.getConstant(0, MVT::i32));
881 RegValuesToPass.push_back(Lo);
882 ++NumIntRegs;
883 if (NumIntRegs < 2) { // Pass both parts in regs?
884 RegValuesToPass.push_back(Hi);
885 ++NumIntRegs;
886 } else {
887 // Pass the high part in memory.
888 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
889 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
890 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
891 Hi, PtrOff, DAG.getSrcValue(NULL)));
892 ArgOffset += 4;
893 }
894 break;
895 }
896 // Fall through
897 case MVT::f64:
898 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
899 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
900 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
901 Args[i].first, PtrOff,
902 DAG.getSrcValue(NULL)));
903 ArgOffset += 8;
904 break;
905 }
906 }
907 if (!Stores.empty())
908 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
909
910 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
911 // arguments and the arguments after the retaddr has been pushed are aligned.
912 if ((ArgOffset & 7) == 0)
913 ArgOffset += 4;
914
915 std::vector<MVT::ValueType> RetVals;
916 MVT::ValueType RetTyVT = getValueType(RetTy);
917
918 RetVals.push_back(MVT::Other);
919
920 // The result values produced have to be legal. Promote the result.
921 switch (RetTyVT) {
922 case MVT::isVoid: break;
923 default:
924 RetVals.push_back(RetTyVT);
925 break;
926 case MVT::i1:
927 case MVT::i8:
928 case MVT::i16:
929 RetVals.push_back(MVT::i32);
930 break;
931 case MVT::f32:
932 if (X86ScalarSSE)
933 RetVals.push_back(MVT::f32);
934 else
935 RetVals.push_back(MVT::f64);
936 break;
937 case MVT::i64:
938 RetVals.push_back(MVT::i32);
939 RetVals.push_back(MVT::i32);
940 break;
941 }
942
Evan Chenga814f0b32006-01-27 21:26:54 +0000943 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000944 // Build a sequence of copy-to-reg nodes chained together with token chain
945 // and flag operands which copy the outgoing args into registers.
946 SDOperand InFlag;
947 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
948 unsigned CCReg;
949 SDOperand RegToPass = RegValuesToPass[i];
950 switch (RegToPass.getValueType()) {
951 default: assert(0 && "Bad thing to pass in regs");
952 case MVT::i8:
953 CCReg = (i == 0) ? X86::AL : X86::DL;
954 break;
955 case MVT::i16:
956 CCReg = (i == 0) ? X86::AX : X86::DX;
957 break;
958 case MVT::i32:
959 CCReg = (i == 0) ? X86::EAX : X86::EDX;
960 break;
961 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000962
Evan Cheng172fce72006-01-06 00:43:03 +0000963 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
964 InFlag = Chain.getValue(1);
965 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000966
Evan Cheng172fce72006-01-06 00:43:03 +0000967 std::vector<MVT::ValueType> NodeTys;
968 NodeTys.push_back(MVT::Other); // Returns a chain
969 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +0000970 std::vector<SDOperand> Ops;
971 Ops.push_back(Chain);
972 Ops.push_back(Callee);
973 if (InFlag.Val)
974 Ops.push_back(InFlag);
975
976 // FIXME: Do not generate X86ISD::TAILCALL for now.
977 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
978 InFlag = Chain.getValue(1);
979
Chris Lattner6f33eae2006-01-24 05:17:12 +0000980 NodeTys.clear();
981 NodeTys.push_back(MVT::Other); // Returns a chain
982 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
983 Ops.clear();
984 Ops.push_back(Chain);
985 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
986 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
987 Ops.push_back(InFlag);
988 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
989 InFlag = Chain.getValue(1);
990
Evan Cheng172fce72006-01-06 00:43:03 +0000991 SDOperand RetVal;
992 if (RetTyVT != MVT::isVoid) {
993 switch (RetTyVT) {
994 default: assert(0 && "Unknown value type to return!");
995 case MVT::i1:
996 case MVT::i8:
997 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
998 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000999 if (RetTyVT == MVT::i1)
1000 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001001 break;
1002 case MVT::i16:
1003 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1004 Chain = RetVal.getValue(1);
1005 break;
1006 case MVT::i32:
1007 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1008 Chain = RetVal.getValue(1);
1009 break;
1010 case MVT::i64: {
1011 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1012 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1013 Lo.getValue(2));
1014 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1015 Chain = Hi.getValue(1);
1016 break;
1017 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001018 case MVT::f32:
Evan Cheng172fce72006-01-06 00:43:03 +00001019 case MVT::f64: {
1020 std::vector<MVT::ValueType> Tys;
1021 Tys.push_back(MVT::f64);
1022 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +00001023 Tys.push_back(MVT::Flag);
Evan Cheng172fce72006-01-06 00:43:03 +00001024 std::vector<SDOperand> Ops;
1025 Ops.push_back(Chain);
1026 Ops.push_back(InFlag);
1027 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +00001028 Chain = RetVal.getValue(1);
1029 InFlag = RetVal.getValue(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001030 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001031 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1032 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +00001033 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng172fce72006-01-06 00:43:03 +00001034 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001035 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng172fce72006-01-06 00:43:03 +00001036 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1037 Tys.clear();
1038 Tys.push_back(MVT::Other);
1039 Ops.clear();
1040 Ops.push_back(Chain);
1041 Ops.push_back(RetVal);
1042 Ops.push_back(StackSlot);
1043 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +00001044 Ops.push_back(InFlag);
Evan Cheng172fce72006-01-06 00:43:03 +00001045 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1046 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1047 DAG.getSrcValue(NULL));
1048 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +00001049 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001050
1051 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1052 // FIXME: we would really like to remember that this FP_ROUND
1053 // operation is okay to eliminate if we allow excess FP precision.
1054 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001055 break;
1056 }
1057 }
1058 }
1059
Evan Cheng172fce72006-01-06 00:43:03 +00001060 return std::make_pair(RetVal, Chain);
1061 } else {
1062 std::vector<SDOperand> Ops;
1063 Ops.push_back(Chain);
1064 Ops.push_back(Callee);
1065 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1066 // Callee pops all arg values on the stack.
1067 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1068
1069 // Pass register arguments as needed.
1070 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1071
Chris Lattnerc642aa52006-01-31 19:43:35 +00001072 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
Evan Cheng172fce72006-01-06 00:43:03 +00001073 RetVals, Ops);
1074 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1075
1076 SDOperand ResultVal;
1077 switch (RetTyVT) {
1078 case MVT::isVoid: break;
1079 default:
1080 ResultVal = TheCall.getValue(1);
1081 break;
1082 case MVT::i1:
1083 case MVT::i8:
1084 case MVT::i16:
1085 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1086 break;
1087 case MVT::f32:
1088 // FIXME: we would really like to remember that this FP_ROUND operation is
1089 // okay to eliminate if we allow excess FP precision.
1090 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1091 break;
1092 case MVT::i64:
1093 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1094 TheCall.getValue(2));
1095 break;
1096 }
1097
1098 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001099 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001100}
1101
1102SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1103 if (ReturnAddrIndex == 0) {
1104 // Set up a frame object for the return address.
1105 MachineFunction &MF = DAG.getMachineFunction();
1106 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1107 }
1108
1109 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1110}
1111
1112
1113
1114std::pair<SDOperand, SDOperand> X86TargetLowering::
1115LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1116 SelectionDAG &DAG) {
1117 SDOperand Result;
1118 if (Depth) // Depths > 0 not supported yet!
1119 Result = DAG.getConstant(0, getPointerTy());
1120 else {
1121 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1122 if (!isFrameAddress)
1123 // Just load the return address
1124 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1125 DAG.getSrcValue(NULL));
1126 else
1127 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1128 DAG.getConstant(4, MVT::i32));
1129 }
1130 return std::make_pair(Result, Chain);
1131}
1132
Evan Cheng339edad2006-01-11 00:33:36 +00001133/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1134/// which corresponds to the condition code.
1135static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1136 switch (X86CC) {
1137 default: assert(0 && "Unknown X86 conditional code!");
1138 case X86ISD::COND_A: return X86::JA;
1139 case X86ISD::COND_AE: return X86::JAE;
1140 case X86ISD::COND_B: return X86::JB;
1141 case X86ISD::COND_BE: return X86::JBE;
1142 case X86ISD::COND_E: return X86::JE;
1143 case X86ISD::COND_G: return X86::JG;
1144 case X86ISD::COND_GE: return X86::JGE;
1145 case X86ISD::COND_L: return X86::JL;
1146 case X86ISD::COND_LE: return X86::JLE;
1147 case X86ISD::COND_NE: return X86::JNE;
1148 case X86ISD::COND_NO: return X86::JNO;
1149 case X86ISD::COND_NP: return X86::JNP;
1150 case X86ISD::COND_NS: return X86::JNS;
1151 case X86ISD::COND_O: return X86::JO;
1152 case X86ISD::COND_P: return X86::JP;
1153 case X86ISD::COND_S: return X86::JS;
1154 }
1155}
Chris Lattner76ac0682005-11-15 00:40:23 +00001156
Evan Cheng45df7f82006-01-30 23:41:35 +00001157/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1158/// specific condition code. It returns a false if it cannot do a direct
1159/// translation. X86CC is the translated CondCode. Flip is set to true if the
1160/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001161static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1162 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001163 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001164 Flip = false;
1165 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001166 if (!isFP) {
1167 switch (SetCCOpcode) {
1168 default: break;
1169 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1170 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1171 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1172 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1173 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1174 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1175 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1176 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1177 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1178 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1179 }
1180 } else {
1181 // On a floating point condition, the flags are set as follows:
1182 // ZF PF CF op
1183 // 0 | 0 | 0 | X > Y
1184 // 0 | 0 | 1 | X < Y
1185 // 1 | 0 | 0 | X == Y
1186 // 1 | 1 | 1 | unordered
1187 switch (SetCCOpcode) {
1188 default: break;
1189 case ISD::SETUEQ:
1190 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001191 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001192 case ISD::SETOGT:
1193 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001194 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001195 case ISD::SETOGE:
1196 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001197 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001198 case ISD::SETULT:
1199 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001200 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001201 case ISD::SETULE:
1202 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1203 case ISD::SETONE:
1204 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1205 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1206 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1207 }
1208 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001209
1210 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001211}
1212
Evan Cheng339edad2006-01-11 00:33:36 +00001213/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1214/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001215/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001216static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001217 switch (X86CC) {
1218 default:
1219 return false;
1220 case X86ISD::COND_B:
1221 case X86ISD::COND_BE:
1222 case X86ISD::COND_E:
1223 case X86ISD::COND_P:
1224 case X86ISD::COND_A:
1225 case X86ISD::COND_AE:
1226 case X86ISD::COND_NE:
1227 case X86ISD::COND_NP:
1228 return true;
1229 }
1230}
1231
Evan Cheng339edad2006-01-11 00:33:36 +00001232MachineBasicBlock *
1233X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1234 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001235 switch (MI->getOpcode()) {
1236 default: assert(false && "Unexpected instr type to insert");
1237 case X86::CMOV_FR32:
1238 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001239 // To "insert" a SELECT_CC instruction, we actually have to insert the
1240 // diamond control-flow pattern. The incoming instruction knows the
1241 // destination vreg to set, the condition code register to branch on, the
1242 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001243 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1244 ilist<MachineBasicBlock>::iterator It = BB;
1245 ++It;
1246
1247 // thisMBB:
1248 // ...
1249 // TrueVal = ...
1250 // cmpTY ccX, r1, r2
1251 // bCC copy1MBB
1252 // fallthrough --> copy0MBB
1253 MachineBasicBlock *thisMBB = BB;
1254 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1255 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1256 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1257 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1258 MachineFunction *F = BB->getParent();
1259 F->getBasicBlockList().insert(It, copy0MBB);
1260 F->getBasicBlockList().insert(It, sinkMBB);
1261 // Update machine-CFG edges
1262 BB->addSuccessor(copy0MBB);
1263 BB->addSuccessor(sinkMBB);
1264
1265 // copy0MBB:
1266 // %FalseValue = ...
1267 // # fallthrough to sinkMBB
1268 BB = copy0MBB;
1269
1270 // Update machine-CFG edges
1271 BB->addSuccessor(sinkMBB);
1272
1273 // sinkMBB:
1274 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1275 // ...
1276 BB = sinkMBB;
1277 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1278 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1279 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001280
Evan Cheng911c68d2006-01-16 21:21:29 +00001281 delete MI; // The pseudo instruction is gone now.
1282 return BB;
1283 }
Evan Cheng339edad2006-01-11 00:33:36 +00001284
Evan Cheng911c68d2006-01-16 21:21:29 +00001285 case X86::FP_TO_INT16_IN_MEM:
1286 case X86::FP_TO_INT32_IN_MEM:
1287 case X86::FP_TO_INT64_IN_MEM: {
1288 // Change the floating point control register to use "round towards zero"
1289 // mode when truncating to an integer value.
1290 MachineFunction *F = BB->getParent();
1291 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1292 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1293
1294 // Load the old value of the high byte of the control word...
1295 unsigned OldCW =
1296 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1297 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1298
1299 // Set the high part to be round to zero...
1300 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1301
1302 // Reload the modified control word now...
1303 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1304
1305 // Restore the memory image of control word to original value
1306 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1307
1308 // Get the X86 opcode to use.
1309 unsigned Opc;
1310 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001311 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001312 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1313 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1314 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1315 }
1316
1317 X86AddressMode AM;
1318 MachineOperand &Op = MI->getOperand(0);
1319 if (Op.isRegister()) {
1320 AM.BaseType = X86AddressMode::RegBase;
1321 AM.Base.Reg = Op.getReg();
1322 } else {
1323 AM.BaseType = X86AddressMode::FrameIndexBase;
1324 AM.Base.FrameIndex = Op.getFrameIndex();
1325 }
1326 Op = MI->getOperand(1);
1327 if (Op.isImmediate())
1328 AM.Scale = Op.getImmedValue();
1329 Op = MI->getOperand(2);
1330 if (Op.isImmediate())
1331 AM.IndexReg = Op.getImmedValue();
1332 Op = MI->getOperand(3);
1333 if (Op.isGlobalAddress()) {
1334 AM.GV = Op.getGlobal();
1335 } else {
1336 AM.Disp = Op.getImmedValue();
1337 }
1338 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1339
1340 // Reload the original control word now.
1341 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1342
1343 delete MI; // The pseudo instruction is gone now.
1344 return BB;
1345 }
1346 }
Evan Cheng339edad2006-01-11 00:33:36 +00001347}
1348
1349
1350//===----------------------------------------------------------------------===//
1351// X86 Custom Lowering Hooks
1352//===----------------------------------------------------------------------===//
1353
Chris Lattner76ac0682005-11-15 00:40:23 +00001354/// LowerOperation - Provide custom lowering hooks for some operations.
1355///
1356SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1357 switch (Op.getOpcode()) {
1358 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001359 case ISD::ADD_PARTS:
1360 case ISD::SUB_PARTS: {
1361 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1362 "Not an i64 add/sub!");
1363 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1364 std::vector<MVT::ValueType> Tys;
1365 Tys.push_back(MVT::i32);
1366 Tys.push_back(MVT::Flag);
1367 std::vector<SDOperand> Ops;
1368 Ops.push_back(Op.getOperand(0));
1369 Ops.push_back(Op.getOperand(2));
1370 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1371 Tys, Ops);
1372 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1373 Op.getOperand(1), Op.getOperand(3),
1374 Lo.getValue(1));
1375 Tys.clear();
1376 Tys.push_back(MVT::i32);
1377 Tys.push_back(MVT::i32);
1378 Ops.clear();
1379 Ops.push_back(Lo);
1380 Ops.push_back(Hi);
1381 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1382 }
1383 case ISD::SHL_PARTS:
1384 case ISD::SRA_PARTS:
1385 case ISD::SRL_PARTS: {
1386 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1387 "Not an i64 shift!");
1388 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1389 SDOperand ShOpLo = Op.getOperand(0);
1390 SDOperand ShOpHi = Op.getOperand(1);
1391 SDOperand ShAmt = Op.getOperand(2);
1392 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001393 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001394 : DAG.getConstant(0, MVT::i32);
1395
1396 SDOperand Tmp2, Tmp3;
1397 if (Op.getOpcode() == ISD::SHL_PARTS) {
1398 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1399 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1400 } else {
1401 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001402 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001403 }
1404
1405 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1406 ShAmt, DAG.getConstant(32, MVT::i8));
1407
1408 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001409 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001410
1411 std::vector<MVT::ValueType> Tys;
1412 Tys.push_back(MVT::i32);
1413 Tys.push_back(MVT::Flag);
1414 std::vector<SDOperand> Ops;
1415 if (Op.getOpcode() == ISD::SHL_PARTS) {
1416 Ops.push_back(Tmp2);
1417 Ops.push_back(Tmp3);
1418 Ops.push_back(CC);
1419 Ops.push_back(InFlag);
1420 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1421 InFlag = Hi.getValue(1);
1422
1423 Ops.clear();
1424 Ops.push_back(Tmp3);
1425 Ops.push_back(Tmp1);
1426 Ops.push_back(CC);
1427 Ops.push_back(InFlag);
1428 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1429 } else {
1430 Ops.push_back(Tmp2);
1431 Ops.push_back(Tmp3);
1432 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001433 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001434 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1435 InFlag = Lo.getValue(1);
1436
1437 Ops.clear();
1438 Ops.push_back(Tmp3);
1439 Ops.push_back(Tmp1);
1440 Ops.push_back(CC);
1441 Ops.push_back(InFlag);
1442 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1443 }
1444
1445 Tys.clear();
1446 Tys.push_back(MVT::i32);
1447 Tys.push_back(MVT::i32);
1448 Ops.clear();
1449 Ops.push_back(Lo);
1450 Ops.push_back(Hi);
1451 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1452 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001453 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001454 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001455 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001456 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001457
1458 SDOperand Result;
1459 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1460 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001461 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001462 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001463 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001464 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1465 DAG.getEntryNode(), Op.getOperand(0),
1466 StackSlot, DAG.getSrcValue(NULL));
1467
1468 // Build the FILD
1469 std::vector<MVT::ValueType> Tys;
1470 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001471 Tys.push_back(MVT::Other);
Evan Cheng6305e502006-01-12 22:54:21 +00001472 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001473 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001474 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001475 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001476 Ops.push_back(DAG.getValueType(SrcVT));
1477 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001478
1479 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001480 Chain = Result.getValue(1);
1481 SDOperand InFlag = Result.getValue(2);
1482
1483 // FIXME: Currently the FST is flagged to the FILD. This
1484 // shouldn't be necessary except that RFP cannot be live across
1485 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1486 MachineFunction &MF = DAG.getMachineFunction();
1487 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1488 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1489 std::vector<MVT::ValueType> Tys;
1490 Tys.push_back(MVT::Other);
1491 std::vector<SDOperand> Ops;
1492 Ops.push_back(Chain);
1493 Ops.push_back(Result);
1494 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001495 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001496 Ops.push_back(InFlag);
1497 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1498 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1499 DAG.getSrcValue(NULL));
1500 }
1501
Evan Cheng6305e502006-01-12 22:54:21 +00001502 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001503 }
1504 case ISD::FP_TO_SINT: {
1505 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001506 "Unknown FP_TO_SINT to lower!");
1507 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1508 // stack slot.
1509 MachineFunction &MF = DAG.getMachineFunction();
1510 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1511 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1512 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1513
1514 unsigned Opc;
1515 switch (Op.getValueType()) {
1516 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1517 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1518 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1519 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1520 }
1521
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001522 SDOperand Chain = DAG.getEntryNode();
1523 SDOperand Value = Op.getOperand(0);
1524 if (X86ScalarSSE) {
1525 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1526 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1527 DAG.getSrcValue(0));
1528 std::vector<MVT::ValueType> Tys;
1529 Tys.push_back(MVT::f64);
1530 Tys.push_back(MVT::Other);
1531 std::vector<SDOperand> Ops;
1532 Ops.push_back(Chain);
1533 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001534 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001535 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1536 Chain = Value.getValue(1);
1537 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1538 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1539 }
1540
Chris Lattner76ac0682005-11-15 00:40:23 +00001541 // Build the FP_TO_INT*_IN_MEM
1542 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001543 Ops.push_back(Chain);
1544 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001545 Ops.push_back(StackSlot);
1546 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1547
1548 // Load the result.
1549 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1550 DAG.getSrcValue(NULL));
1551 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001552 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001553 std::vector<MVT::ValueType> Tys;
1554 Tys.push_back(MVT::Other);
1555 Tys.push_back(MVT::Flag);
1556 std::vector<SDOperand> Ops;
1557 Ops.push_back(Op.getOperand(0));
1558 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001559 Ops.clear();
1560 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1561 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1562 MVT::i32, Ops[0].getValue(2)));
1563 Ops.push_back(Ops[1].getValue(1));
1564 Tys[0] = Tys[1] = MVT::i32;
1565 Tys.push_back(MVT::Other);
1566 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001567 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001568 case ISD::FABS: {
1569 MVT::ValueType VT = Op.getValueType();
1570 SDOperand Mask = (VT == MVT::f64)
1571 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), MVT::f64)
1572 : DAG.getConstantFP(BitsToFloat (~(1U << 31)), MVT::f32);
1573 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1574 }
Evan Chengc1583db2005-12-21 20:21:51 +00001575 case ISD::SETCC: {
1576 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001577 SDOperand Cond;
1578 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001579 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1580 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001581 bool Flip;
1582 unsigned X86CC;
1583 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1584 if (Flip)
1585 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1586 Op.getOperand(1), Op.getOperand(0));
1587 else
1588 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1589 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001590 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1591 DAG.getConstant(X86CC, MVT::i8), Cond);
1592 } else {
1593 assert(isFP && "Illegal integer SetCC!");
1594
Evan Cheng45df7f82006-01-30 23:41:35 +00001595 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1596 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001597 std::vector<MVT::ValueType> Tys;
1598 std::vector<SDOperand> Ops;
1599 switch (SetCCOpcode) {
1600 default: assert(false && "Illegal floating point SetCC!");
1601 case ISD::SETOEQ: { // !PF & ZF
1602 Tys.push_back(MVT::i8);
1603 Tys.push_back(MVT::Flag);
1604 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1605 Ops.push_back(Cond);
1606 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1607 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1608 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1609 Tmp1.getValue(1));
1610 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1611 }
Evan Cheng172fce72006-01-06 00:43:03 +00001612 case ISD::SETUNE: { // PF | !ZF
1613 Tys.push_back(MVT::i8);
1614 Tys.push_back(MVT::Flag);
1615 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1616 Ops.push_back(Cond);
1617 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1618 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1619 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1620 Tmp1.getValue(1));
1621 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1622 }
1623 }
1624 }
Evan Chengc1583db2005-12-21 20:21:51 +00001625 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001626 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001627 MVT::ValueType VT = Op.getValueType();
1628 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001629 bool isFPStack = isFP && !X86ScalarSSE;
1630 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001631 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001632 SDOperand Op0 = Op.getOperand(0);
1633 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001634 if (Op0.getOpcode() == ISD::SETCC)
1635 Op0 = LowerOperation(Op0, DAG);
1636
Evan Cheng73a1ad92006-01-10 20:26:56 +00001637 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001638 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1639 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1640 // have another use it will be eliminated.
1641 // If the X86ISD::SETCC has more than one use, then it's probably better
1642 // to use a test instead of duplicating the X86ISD::CMP (for register
1643 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001644 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1645 if (!Op0.hasOneUse()) {
1646 std::vector<MVT::ValueType> Tys;
1647 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1648 Tys.push_back(Op0.Val->getValueType(i));
1649 std::vector<SDOperand> Ops;
1650 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1651 Ops.push_back(Op0.getOperand(i));
1652 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1653 }
1654
Evan Chengfb22e862006-01-13 01:03:02 +00001655 CC = Op0.getOperand(0);
1656 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001657 // Make a copy as flag result cannot be used by more than one.
1658 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1659 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001660 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001661 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001662 } else
1663 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001664 } else
1665 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001666
Evan Cheng731423f2006-01-13 01:06:49 +00001667 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001668 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001669 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001670 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001671
1672 std::vector<MVT::ValueType> Tys;
1673 Tys.push_back(Op.getValueType());
1674 Tys.push_back(MVT::Flag);
1675 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001676 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1677 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001678 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001679 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001680 Ops.push_back(CC);
1681 Ops.push_back(Cond);
1682 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001683 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001684 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001685 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001686 SDOperand Cond = Op.getOperand(1);
1687 SDOperand Dest = Op.getOperand(2);
1688 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001689 if (Cond.getOpcode() == ISD::SETCC)
1690 Cond = LowerOperation(Cond, DAG);
1691
Evan Chengc1583db2005-12-21 20:21:51 +00001692 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001693 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1694 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1695 // have another use it will be eliminated.
1696 // If the X86ISD::SETCC has more than one use, then it's probably better
1697 // to use a test instead of duplicating the X86ISD::CMP (for register
1698 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001699 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1700 if (!Cond.hasOneUse()) {
1701 std::vector<MVT::ValueType> Tys;
1702 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1703 Tys.push_back(Cond.Val->getValueType(i));
1704 std::vector<SDOperand> Ops;
1705 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1706 Ops.push_back(Cond.getOperand(i));
1707 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1708 }
1709
Evan Chengfb22e862006-01-13 01:03:02 +00001710 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001711 Cond = Cond.getOperand(1);
1712 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001713 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001714 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001715 } else
1716 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001717 } else
1718 addTest = true;
1719
1720 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001721 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001722 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1723 }
1724 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1725 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1726 }
Evan Chengae986f12006-01-11 22:15:48 +00001727 case ISD::MEMSET: {
1728 SDOperand InFlag;
1729 SDOperand Chain = Op.getOperand(0);
1730 unsigned Align =
1731 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1732 if (Align == 0) Align = 1;
1733
1734 MVT::ValueType AVT;
1735 SDOperand Count;
1736 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1737 unsigned ValReg;
1738 unsigned Val = ValC->getValue() & 255;
1739
1740 // If the value is a constant, then we can potentially use larger sets.
1741 switch (Align & 3) {
1742 case 2: // WORD aligned
1743 AVT = MVT::i16;
1744 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1745 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1746 else
1747 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1748 DAG.getConstant(1, MVT::i8));
1749 Val = (Val << 8) | Val;
1750 ValReg = X86::AX;
1751 break;
1752 case 0: // DWORD aligned
1753 AVT = MVT::i32;
1754 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1755 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1756 else
1757 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1758 DAG.getConstant(2, MVT::i8));
1759 Val = (Val << 8) | Val;
1760 Val = (Val << 16) | Val;
1761 ValReg = X86::EAX;
1762 break;
1763 default: // Byte aligned
1764 AVT = MVT::i8;
1765 Count = Op.getOperand(3);
1766 ValReg = X86::AL;
1767 break;
1768 }
1769
1770 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1771 InFlag);
1772 InFlag = Chain.getValue(1);
1773 } else {
1774 AVT = MVT::i8;
1775 Count = Op.getOperand(3);
1776 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1777 InFlag = Chain.getValue(1);
1778 }
1779
1780 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1781 InFlag = Chain.getValue(1);
1782 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1783 InFlag = Chain.getValue(1);
1784
1785 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1786 DAG.getValueType(AVT), InFlag);
1787 }
1788 case ISD::MEMCPY: {
1789 SDOperand Chain = Op.getOperand(0);
1790 unsigned Align =
1791 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1792 if (Align == 0) Align = 1;
1793
1794 MVT::ValueType AVT;
1795 SDOperand Count;
1796 switch (Align & 3) {
1797 case 2: // WORD aligned
1798 AVT = MVT::i16;
1799 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1800 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1801 else
1802 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1803 DAG.getConstant(1, MVT::i8));
1804 break;
1805 case 0: // DWORD aligned
1806 AVT = MVT::i32;
1807 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1808 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1809 else
1810 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1811 DAG.getConstant(2, MVT::i8));
1812 break;
1813 default: // Byte aligned
1814 AVT = MVT::i8;
1815 Count = Op.getOperand(3);
1816 break;
1817 }
1818
1819 SDOperand InFlag;
1820 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1821 InFlag = Chain.getValue(1);
1822 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1823 InFlag = Chain.getValue(1);
1824 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1825 InFlag = Chain.getValue(1);
1826
1827 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1828 DAG.getValueType(AVT), InFlag);
1829 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001830 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001831 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001832 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1833 // For Darwin, external and weak symbols are indirect, so we want to load
1834 // the value at address GV, not the value of GV itself. This means that
1835 // the GlobalAddress must be in the base or index register of the address,
1836 // not the GV offset field.
1837 if (getTargetMachine().
1838 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1839 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001840 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1841 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1842 DAG.getSrcValue(NULL));
1843 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001844 }
Nate Begemane74795c2006-01-25 18:21:52 +00001845 case ISD::VASTART: {
1846 // vastart just stores the address of the VarArgsFrameIndex slot into the
1847 // memory location argument.
1848 // FIXME: Replace MVT::i32 with PointerTy
1849 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1850 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1851 Op.getOperand(1), Op.getOperand(2));
1852 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001853 case ISD::RET: {
1854 SDOperand Copy;
1855
1856 switch(Op.getNumOperands()) {
1857 default:
1858 assert(0 && "Do not know how to return this many arguments!");
1859 abort();
1860 case 1:
1861 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1862 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1863 case 2: {
1864 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1865 if (MVT::isInteger(ArgVT))
1866 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1867 SDOperand());
1868 else if (!X86ScalarSSE) {
1869 std::vector<MVT::ValueType> Tys;
1870 Tys.push_back(MVT::Other);
1871 Tys.push_back(MVT::Flag);
1872 std::vector<SDOperand> Ops;
1873 Ops.push_back(Op.getOperand(0));
1874 Ops.push_back(Op.getOperand(1));
1875 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1876 } else {
1877 // Spill the value to memory and reload it into top of stack.
1878 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1879 MachineFunction &MF = DAG.getMachineFunction();
1880 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1881 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1882 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1883 Op.getOperand(1), StackSlot,
1884 DAG.getSrcValue(0));
1885 std::vector<MVT::ValueType> Tys;
1886 Tys.push_back(MVT::f64);
1887 Tys.push_back(MVT::Other);
1888 std::vector<SDOperand> Ops;
1889 Ops.push_back(Chain);
1890 Ops.push_back(StackSlot);
1891 Ops.push_back(DAG.getValueType(ArgVT));
1892 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1893 Tys.clear();
1894 Tys.push_back(MVT::Other);
1895 Tys.push_back(MVT::Flag);
1896 Ops.clear();
1897 Ops.push_back(Copy.getValue(1));
1898 Ops.push_back(Copy);
1899 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1900 }
1901 break;
1902 }
1903 case 3:
1904 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1905 SDOperand());
1906 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1907 break;
1908 }
1909 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1910 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1911 Copy.getValue(1));
1912 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001913 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001914}
Evan Cheng6af02632005-12-20 06:22:03 +00001915
1916const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1917 switch (Opcode) {
1918 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001919 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1920 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1921 case X86ISD::ADC: return "X86ISD::ADC";
1922 case X86ISD::SBB: return "X86ISD::SBB";
1923 case X86ISD::SHLD: return "X86ISD::SHLD";
1924 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00001925 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng6305e502006-01-12 22:54:21 +00001926 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001927 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1928 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1929 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001930 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001931 case X86ISD::FST: return "X86ISD::FST";
1932 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001933 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001934 case X86ISD::CALL: return "X86ISD::CALL";
1935 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1936 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1937 case X86ISD::CMP: return "X86ISD::CMP";
1938 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001939 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001940 case X86ISD::CMOV: return "X86ISD::CMOV";
1941 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001942 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001943 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1944 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001945 }
1946}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001947
1948bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerf0b24d22006-01-30 04:09:27 +00001949 uint64_t Mask) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001950
1951 unsigned Opc = Op.getOpcode();
1952
1953 switch (Opc) {
1954 default:
1955 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1956 break;
1957 case X86ISD::SETCC: return (Mask & 1) == 0;
1958 }
1959
1960 return false;
1961}
Chris Lattnerc642aa52006-01-31 19:43:35 +00001962
1963std::vector<unsigned> X86TargetLowering::
1964getRegForInlineAsmConstraint(const std::string &Constraint) const {
1965 if (Constraint.size() == 1) {
1966 // FIXME: not handling fp-stack yet!
1967 // FIXME: not handling MMX registers yet ('y' constraint).
1968 switch (Constraint[0]) { // GCC X86 Constraint Letters
1969 default: break; // Unknown constriant letter
1970 case 'r': // GENERAL_REGS
1971 case 'R': // LEGACY_REGS
1972 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1973 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
1974 case 'l': // INDEX_REGS
1975 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
1976 X86::ESI, X86::EDI, X86::EBP, 0);
1977 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
1978 case 'Q': // Q_REGS
1979 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
1980 case 'x': // SSE_REGS if SSE1 allowed
1981 if (Subtarget->hasSSE1())
1982 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1983 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1984 0);
1985 return std::vector<unsigned>();
1986 case 'Y': // SSE_REGS if SSE2 allowed
1987 if (Subtarget->hasSSE2())
1988 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1989 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
1990 0);
1991 return std::vector<unsigned>();
1992 }
1993 }
1994
1995 // Handle explicit register names.
1996 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
1997}