blob: 172b8a128e24a5432ebac27f78d0f6aeb5eeec45 [file] [log] [blame]
Chris Lattnerdbdbf0c2005-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 Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/Function.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000027#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner259e97c2006-01-31 19:43:35 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030using namespace llvm;
31
32// FIXME: temporary.
33#include "llvm/Support/CommandLine.h"
34static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
36
37X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000039 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
41
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042 // Set up the TargetLowering object.
43
44 // X86 is weird, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
47 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000048 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000050 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattnera54aa942006-01-29 06:26:08 +000051
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58 // operation.
59 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
61 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000062
63 if (X86ScalarSSE)
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
66 else
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70 // this operation.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +000073 if (X86ScalarSSE)
74 // SSE has no i16 to fp conversion, only i32
75 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
76 else if (!X86PatIsel) {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
79 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080
Evan Cheng6dab0532006-01-30 08:02:57 +000081 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82 // isn't legal.
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
85
Evan Cheng02568ff2006-01-30 22:13:22 +000086 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
90
91 if (X86ScalarSSE) {
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
93 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +000095 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096 }
97
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
99 // conversion.
100 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
102 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
103
Evan Cheng02568ff2006-01-30 22:13:22 +0000104 if (X86ScalarSSE)
105 // Expand FP_TO_UINT into a select.
106 // FIXME: We would like to use a Custom expander here eventually to do
107 // the optimal thing for SSE vs. the default expansion in the legalizer.
108 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
109 else
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
111
Evan Cheng02568ff2006-01-30 22:13:22 +0000112 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
113 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000114
Evan Cheng8e44f072006-01-27 21:26:54 +0000115 if (!X86PatIsel) {
Evan Cheng898101c2005-12-19 23:12:38 +0000116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
117 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000118 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
119 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000120 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
121 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000122 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
126 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
127 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
128 setOperationAction(ISD::FREM , MVT::f64 , Expand);
129 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
130 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
132 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
133 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
135 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
136 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
137 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000138 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000139
Evan Cheng8e44f072006-01-27 21:26:54 +0000140 if (X86PatIsel) {
Nate Begemand88fc032006-01-14 03:14:10 +0000141 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Chengeb422a72006-01-11 23:20:05 +0000142 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
143 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
144 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
145 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
146 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
147 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
148 }
Nate Begemand88fc032006-01-14 03:14:10 +0000149 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000150
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151 setOperationAction(ISD::READIO , MVT::i1 , Expand);
152 setOperationAction(ISD::READIO , MVT::i8 , Expand);
153 setOperationAction(ISD::READIO , MVT::i16 , Expand);
154 setOperationAction(ISD::READIO , MVT::i32 , Expand);
155 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
156 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
157 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
158 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
159
160 // These should be promoted to a larger select which is supported.
161 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
162 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng8e44f072006-01-27 21:26:54 +0000163 if (!X86PatIsel) {
Evan Chengd9558e02006-01-06 00:43:03 +0000164 // X86 wants to expand cmov itself.
Evan Cheng7df96d62005-12-17 01:21:05 +0000165 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
166 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Chengd9558e02006-01-06 00:43:03 +0000167 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
168 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengd5781fc2005-12-21 20:21:51 +0000169 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
170 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
171 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Chengd9558e02006-01-06 00:43:03 +0000172 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
173 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
174 // X86 ret instruction may pop stack.
175 setOperationAction(ISD::RET , MVT::Other, Custom);
176 // Darwin ABI issue.
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000177 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge3413162006-01-09 18:33:28 +0000178 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
179 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
180 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
181 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
182 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
183 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Cheng67f92a72006-01-11 22:15:48 +0000184 // X86 wants to expand memset / memcpy itself.
185 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
186 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng7df96d62005-12-17 01:21:05 +0000187 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
Chris Lattnerf73bae12005-11-29 06:16:21 +0000189 // We don't have line number support yet.
190 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000191 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
192 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000193
Nate Begemanacc398c2006-01-25 18:21:52 +0000194 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
195 setOperationAction(ISD::VASTART , MVT::Other, Custom);
196
197 // Use the default implementation.
198 setOperationAction(ISD::VAARG , MVT::Other, Expand);
199 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
200 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000201 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
202 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
203 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000204
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205 if (X86ScalarSSE) {
206 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209
210 // SSE has no load+extend ops
211 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
212 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
213
Evan Cheng223547a2006-01-31 22:28:30 +0000214 // Use ANDPD to simulate FABS.
215 setOperationAction(ISD::FABS , MVT::f64, Custom);
216 setOperationAction(ISD::FABS , MVT::f32, Custom);
217
218 // Use XORP to simulate FNEG.
219 setOperationAction(ISD::FNEG , MVT::f64, Custom);
220 setOperationAction(ISD::FNEG , MVT::f32, Custom);
221
Evan Chengd25e9e82006-02-02 00:28:23 +0000222 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::FSIN , MVT::f64, Expand);
224 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f64, Expand);
226 setOperationAction(ISD::FSIN , MVT::f32, Expand);
227 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228 setOperationAction(ISD::FREM , MVT::f32, Expand);
229
Chris Lattnera54aa942006-01-29 06:26:08 +0000230 // Expand FP immediates into loads from the stack, except for the special
231 // cases we handle.
232 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
233 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000234 addLegalFPImmediate(+0.0); // xorps / xorpd
235 } else {
236 // Set up the FP register classes.
237 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000238
239 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
240
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000241 if (!UnsafeFPMath) {
242 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
243 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
244 }
245
Chris Lattnera54aa942006-01-29 06:26:08 +0000246 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 addLegalFPImmediate(+0.0); // FLD0
248 addLegalFPImmediate(+1.0); // FLD1
249 addLegalFPImmediate(-0.0); // FLD0/FCHS
250 addLegalFPImmediate(-1.0); // FLD1/FCHS
251 }
252 computeRegisterProperties();
253
Evan Cheng87ed7162006-02-14 08:25:08 +0000254 // FIXME: These should be based on subtarget info. Plus, the values should
255 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000256 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
257 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
258 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259 allowUnalignedMemoryAccesses = true; // x86 supports it!
260}
261
262std::vector<SDOperand>
263X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
264 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
265 return LowerFastCCArguments(F, DAG);
266 return LowerCCCArguments(F, DAG);
267}
268
269std::pair<SDOperand, SDOperand>
270X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
271 bool isVarArg, unsigned CallingConv,
272 bool isTailCall,
273 SDOperand Callee, ArgListTy &Args,
274 SelectionDAG &DAG) {
275 assert((!isVarArg || CallingConv == CallingConv::C) &&
276 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000277
278 // If the callee is a GlobalAddress node (quite common, every direct call is)
279 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
280 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
281 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000282 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
283 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000284
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285 if (CallingConv == CallingConv::Fast && EnableFastCC)
286 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
287 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
288}
289
290//===----------------------------------------------------------------------===//
291// C Calling Convention implementation
292//===----------------------------------------------------------------------===//
293
294std::vector<SDOperand>
295X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
296 std::vector<SDOperand> ArgValues;
297
298 MachineFunction &MF = DAG.getMachineFunction();
299 MachineFrameInfo *MFI = MF.getFrameInfo();
300
301 // Add DAG nodes to load the arguments... On entry to a function on the X86,
302 // the stack frame looks like this:
303 //
304 // [ESP] -- return address
305 // [ESP + 4] -- first argument (leftmost lexically)
306 // [ESP + 8] -- second argument, if first argument is four bytes in size
307 // ...
308 //
309 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
310 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
311 MVT::ValueType ObjectVT = getValueType(I->getType());
312 unsigned ArgIncrement = 4;
313 unsigned ObjSize;
314 switch (ObjectVT) {
315 default: assert(0 && "Unhandled argument type!");
316 case MVT::i1:
317 case MVT::i8: ObjSize = 1; break;
318 case MVT::i16: ObjSize = 2; break;
319 case MVT::i32: ObjSize = 4; break;
320 case MVT::i64: ObjSize = ArgIncrement = 8; break;
321 case MVT::f32: ObjSize = 4; break;
322 case MVT::f64: ObjSize = ArgIncrement = 8; break;
323 }
324 // Create the frame index object for this incoming parameter...
325 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
326
327 // Create the SelectionDAG nodes corresponding to a load from this parameter
328 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
329
330 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
331 // dead loads.
332 SDOperand ArgValue;
333 if (!I->use_empty())
334 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
335 DAG.getSrcValue(NULL));
336 else {
337 if (MVT::isInteger(ObjectVT))
338 ArgValue = DAG.getConstant(0, ObjectVT);
339 else
340 ArgValue = DAG.getConstantFP(0, ObjectVT);
341 }
342 ArgValues.push_back(ArgValue);
343
344 ArgOffset += ArgIncrement; // Move on to the next argument...
345 }
346
347 // If the function takes variable number of arguments, make a frame index for
348 // the start of the first vararg value... for expansion of llvm.va_start.
349 if (F.isVarArg())
350 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
351 ReturnAddrIndex = 0; // No return address slot generated yet.
352 BytesToPopOnReturn = 0; // Callee pops nothing.
353 BytesCallerReserves = ArgOffset;
354
355 // Finally, inform the code generator which regs we return values in.
356 switch (getValueType(F.getReturnType())) {
357 default: assert(0 && "Unknown type!");
358 case MVT::isVoid: break;
359 case MVT::i1:
360 case MVT::i8:
361 case MVT::i16:
362 case MVT::i32:
363 MF.addLiveOut(X86::EAX);
364 break;
365 case MVT::i64:
366 MF.addLiveOut(X86::EAX);
367 MF.addLiveOut(X86::EDX);
368 break;
369 case MVT::f32:
370 case MVT::f64:
371 MF.addLiveOut(X86::ST0);
372 break;
373 }
374 return ArgValues;
375}
376
377std::pair<SDOperand, SDOperand>
378X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
379 bool isVarArg, bool isTailCall,
380 SDOperand Callee, ArgListTy &Args,
381 SelectionDAG &DAG) {
382 // Count how many bytes are to be pushed on the stack.
383 unsigned NumBytes = 0;
384
385 if (Args.empty()) {
386 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000387 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000388 } else {
389 for (unsigned i = 0, e = Args.size(); i != e; ++i)
390 switch (getValueType(Args[i].second)) {
391 default: assert(0 && "Unknown value type!");
392 case MVT::i1:
393 case MVT::i8:
394 case MVT::i16:
395 case MVT::i32:
396 case MVT::f32:
397 NumBytes += 4;
398 break;
399 case MVT::i64:
400 case MVT::f64:
401 NumBytes += 8;
402 break;
403 }
404
Chris Lattner94dd2922006-02-13 09:00:43 +0000405 Chain = DAG.getCALLSEQ_START(Chain,
406 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000407
408 // Arguments go on the stack in reverse order, as specified by the ABI.
409 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000410 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000411 std::vector<SDOperand> Stores;
412
413 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
414 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
415 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
416
417 switch (getValueType(Args[i].second)) {
418 default: assert(0 && "Unexpected ValueType for argument!");
419 case MVT::i1:
420 case MVT::i8:
421 case MVT::i16:
422 // Promote the integer to 32 bits. If the input type is signed use a
423 // sign extend, otherwise use a zero extend.
424 if (Args[i].second->isSigned())
425 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
426 else
427 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
428
429 // FALL THROUGH
430 case MVT::i32:
431 case MVT::f32:
432 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
433 Args[i].first, PtrOff,
434 DAG.getSrcValue(NULL)));
435 ArgOffset += 4;
436 break;
437 case MVT::i64:
438 case MVT::f64:
439 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
440 Args[i].first, PtrOff,
441 DAG.getSrcValue(NULL)));
442 ArgOffset += 8;
443 break;
444 }
445 }
446 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
447 }
448
449 std::vector<MVT::ValueType> RetVals;
450 MVT::ValueType RetTyVT = getValueType(RetTy);
451 RetVals.push_back(MVT::Other);
452
453 // The result values produced have to be legal. Promote the result.
454 switch (RetTyVT) {
455 case MVT::isVoid: break;
456 default:
457 RetVals.push_back(RetTyVT);
458 break;
459 case MVT::i1:
460 case MVT::i8:
461 case MVT::i16:
462 RetVals.push_back(MVT::i32);
463 break;
464 case MVT::f32:
465 if (X86ScalarSSE)
466 RetVals.push_back(MVT::f32);
467 else
468 RetVals.push_back(MVT::f64);
469 break;
470 case MVT::i64:
471 RetVals.push_back(MVT::i32);
472 RetVals.push_back(MVT::i32);
473 break;
474 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000475
Evan Cheng8e44f072006-01-27 21:26:54 +0000476 if (!X86PatIsel) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000477 std::vector<MVT::ValueType> NodeTys;
478 NodeTys.push_back(MVT::Other); // Returns a chain
479 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd90eb7f2006-01-05 00:27:02 +0000480 std::vector<SDOperand> Ops;
481 Ops.push_back(Chain);
482 Ops.push_back(Callee);
483
Evan Chengd9558e02006-01-06 00:43:03 +0000484 // FIXME: Do not generate X86ISD::TAILCALL for now.
485 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000486 SDOperand InFlag = Chain.getValue(1);
487
Chris Lattneraf63bb02006-01-24 05:17:12 +0000488 NodeTys.clear();
489 NodeTys.push_back(MVT::Other); // Returns a chain
490 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
491 Ops.clear();
492 Ops.push_back(Chain);
493 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
494 Ops.push_back(DAG.getConstant(0, getPointerTy()));
495 Ops.push_back(InFlag);
496 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
497 InFlag = Chain.getValue(1);
498
Evan Chengd90eb7f2006-01-05 00:27:02 +0000499 SDOperand RetVal;
500 if (RetTyVT != MVT::isVoid) {
501 switch (RetTyVT) {
502 default: assert(0 && "Unknown value type to return!");
503 case MVT::i1:
504 case MVT::i8:
505 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
506 Chain = RetVal.getValue(1);
Evan Cheng68e5d082006-01-18 08:08:38 +0000507 if (RetTyVT == MVT::i1)
508 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000509 break;
510 case MVT::i16:
511 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
512 Chain = RetVal.getValue(1);
513 break;
514 case MVT::i32:
515 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
516 Chain = RetVal.getValue(1);
517 break;
518 case MVT::i64: {
519 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
520 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
521 Lo.getValue(2));
522 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
523 Chain = Hi.getValue(1);
524 break;
525 }
Evan Cheng357c58e2006-01-17 21:58:21 +0000526 case MVT::f32:
Evan Chengd90eb7f2006-01-05 00:27:02 +0000527 case MVT::f64: {
528 std::vector<MVT::ValueType> Tys;
529 Tys.push_back(MVT::f64);
530 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000531 Tys.push_back(MVT::Flag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000532 std::vector<SDOperand> Ops;
533 Ops.push_back(Chain);
534 Ops.push_back(InFlag);
535 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000536 Chain = RetVal.getValue(1);
537 InFlag = RetVal.getValue(2);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000538 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +0000539 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
540 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng2059f882006-01-17 00:37:42 +0000541 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Chengd90eb7f2006-01-05 00:27:02 +0000542 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6dab0532006-01-30 08:02:57 +0000543 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000544 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
545 Tys.clear();
546 Tys.push_back(MVT::Other);
547 Ops.clear();
548 Ops.push_back(Chain);
549 Ops.push_back(RetVal);
550 Ops.push_back(StackSlot);
551 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000552 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000553 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
554 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
555 DAG.getSrcValue(NULL));
556 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000557 }
Evan Cheng357c58e2006-01-17 21:58:21 +0000558
559 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
560 // FIXME: we would really like to remember that this FP_ROUND
561 // operation is okay to eliminate if we allow excess FP precision.
562 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000563 break;
564 }
565 }
566 }
567
Evan Chengd90eb7f2006-01-05 00:27:02 +0000568 return std::make_pair(RetVal, Chain);
569 } else {
570 std::vector<SDOperand> Ops;
571 Ops.push_back(Chain);
572 Ops.push_back(Callee);
573 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
574 Ops.push_back(DAG.getConstant(0, getPointerTy()));
575
Chris Lattner259e97c2006-01-31 19:43:35 +0000576 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
Evan Chengd90eb7f2006-01-05 00:27:02 +0000577 RetVals, Ops);
578
579 SDOperand ResultVal;
580 switch (RetTyVT) {
581 case MVT::isVoid: break;
582 default:
583 ResultVal = TheCall.getValue(1);
584 break;
585 case MVT::i1:
586 case MVT::i8:
587 case MVT::i16:
588 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
589 break;
590 case MVT::f32:
591 // FIXME: we would really like to remember that this FP_ROUND operation is
592 // okay to eliminate if we allow excess FP precision.
593 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
594 break;
595 case MVT::i64:
596 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
597 TheCall.getValue(2));
598 break;
599 }
600
601 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
602 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000603 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000604}
605
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000606//===----------------------------------------------------------------------===//
607// Fast Calling Convention implementation
608//===----------------------------------------------------------------------===//
609//
610// The X86 'fast' calling convention passes up to two integer arguments in
611// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
612// and requires that the callee pop its arguments off the stack (allowing proper
613// tail calls), and has the same return value conventions as C calling convs.
614//
615// This calling convention always arranges for the callee pop value to be 8n+4
616// bytes, which is needed for tail recursion elimination and stack alignment
617// reasons.
618//
619// Note that this can be enhanced in the future to pass fp vals in registers
620// (when we have a global fp allocator) and do other tricks.
621//
622
623/// AddLiveIn - This helper function adds the specified physical register to the
624/// MachineFunction as a live in value. It also creates a corresponding virtual
625/// register for it.
626static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
627 TargetRegisterClass *RC) {
628 assert(RC->contains(PReg) && "Not the correct regclass!");
629 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
630 MF.addLiveIn(PReg, VReg);
631 return VReg;
632}
633
634
635std::vector<SDOperand>
636X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
637 std::vector<SDOperand> ArgValues;
638
639 MachineFunction &MF = DAG.getMachineFunction();
640 MachineFrameInfo *MFI = MF.getFrameInfo();
641
642 // Add DAG nodes to load the arguments... On entry to a function the stack
643 // frame looks like this:
644 //
645 // [ESP] -- return address
646 // [ESP + 4] -- first nonreg argument (leftmost lexically)
647 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
648 // ...
649 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
650
651 // Keep track of the number of integer regs passed so far. This can be either
652 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
653 // used).
654 unsigned NumIntRegs = 0;
655
656 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
657 MVT::ValueType ObjectVT = getValueType(I->getType());
658 unsigned ArgIncrement = 4;
659 unsigned ObjSize = 0;
660 SDOperand ArgValue;
661
662 switch (ObjectVT) {
663 default: assert(0 && "Unhandled argument type!");
664 case MVT::i1:
665 case MVT::i8:
666 if (NumIntRegs < 2) {
667 if (!I->use_empty()) {
668 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
669 X86::R8RegisterClass);
670 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
671 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000672 if (ObjectVT == MVT::i1)
673 // FIXME: Should insert a assertzext here.
674 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
676 ++NumIntRegs;
677 break;
678 }
679
680 ObjSize = 1;
681 break;
682 case MVT::i16:
683 if (NumIntRegs < 2) {
684 if (!I->use_empty()) {
685 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
686 X86::R16RegisterClass);
687 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
688 DAG.setRoot(ArgValue.getValue(1));
689 }
690 ++NumIntRegs;
691 break;
692 }
693 ObjSize = 2;
694 break;
695 case MVT::i32:
696 if (NumIntRegs < 2) {
697 if (!I->use_empty()) {
698 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
699 X86::R32RegisterClass);
700 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
701 DAG.setRoot(ArgValue.getValue(1));
702 }
703 ++NumIntRegs;
704 break;
705 }
706 ObjSize = 4;
707 break;
708 case MVT::i64:
709 if (NumIntRegs == 0) {
710 if (!I->use_empty()) {
711 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
712 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
713
714 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
715 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
716 DAG.setRoot(Hi.getValue(1));
717
718 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
719 }
720 NumIntRegs = 2;
721 break;
722 } else if (NumIntRegs == 1) {
723 if (!I->use_empty()) {
724 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
725 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
726 DAG.setRoot(Low.getValue(1));
727
728 // Load the high part from memory.
729 // Create the frame index object for this incoming parameter...
730 int FI = MFI->CreateFixedObject(4, ArgOffset);
731 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
732 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
733 DAG.getSrcValue(NULL));
734 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
735 }
736 ArgOffset += 4;
737 NumIntRegs = 2;
738 break;
739 }
740 ObjSize = ArgIncrement = 8;
741 break;
742 case MVT::f32: ObjSize = 4; break;
743 case MVT::f64: ObjSize = ArgIncrement = 8; break;
744 }
745
746 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
747 // dead loads.
748 if (ObjSize && !I->use_empty()) {
749 // Create the frame index object for this incoming parameter...
750 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
751
752 // Create the SelectionDAG nodes corresponding to a load from this
753 // parameter.
754 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
755
756 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
757 DAG.getSrcValue(NULL));
758 } else if (ArgValue.Val == 0) {
759 if (MVT::isInteger(ObjectVT))
760 ArgValue = DAG.getConstant(0, ObjectVT);
761 else
762 ArgValue = DAG.getConstantFP(0, ObjectVT);
763 }
764 ArgValues.push_back(ArgValue);
765
766 if (ObjSize)
767 ArgOffset += ArgIncrement; // Move on to the next argument.
768 }
769
770 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
771 // arguments and the arguments after the retaddr has been pushed are aligned.
772 if ((ArgOffset & 7) == 0)
773 ArgOffset += 4;
774
775 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
776 ReturnAddrIndex = 0; // No return address slot generated yet.
777 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
778 BytesCallerReserves = 0;
779
780 // Finally, inform the code generator which regs we return values in.
781 switch (getValueType(F.getReturnType())) {
782 default: assert(0 && "Unknown type!");
783 case MVT::isVoid: break;
784 case MVT::i1:
785 case MVT::i8:
786 case MVT::i16:
787 case MVT::i32:
788 MF.addLiveOut(X86::EAX);
789 break;
790 case MVT::i64:
791 MF.addLiveOut(X86::EAX);
792 MF.addLiveOut(X86::EDX);
793 break;
794 case MVT::f32:
795 case MVT::f64:
796 MF.addLiveOut(X86::ST0);
797 break;
798 }
799 return ArgValues;
800}
801
802std::pair<SDOperand, SDOperand>
803X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
804 bool isTailCall, SDOperand Callee,
805 ArgListTy &Args, SelectionDAG &DAG) {
806 // Count how many bytes are to be pushed on the stack.
807 unsigned NumBytes = 0;
808
809 // Keep track of the number of integer regs passed so far. This can be either
810 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
811 // used).
812 unsigned NumIntRegs = 0;
813
814 for (unsigned i = 0, e = Args.size(); i != e; ++i)
815 switch (getValueType(Args[i].second)) {
816 default: assert(0 && "Unknown value type!");
817 case MVT::i1:
818 case MVT::i8:
819 case MVT::i16:
820 case MVT::i32:
821 if (NumIntRegs < 2) {
822 ++NumIntRegs;
823 break;
824 }
825 // fall through
826 case MVT::f32:
827 NumBytes += 4;
828 break;
829 case MVT::i64:
830 if (NumIntRegs == 0) {
831 NumIntRegs = 2;
832 break;
833 } else if (NumIntRegs == 1) {
834 NumIntRegs = 2;
835 NumBytes += 4;
836 break;
837 }
838
839 // fall through
840 case MVT::f64:
841 NumBytes += 8;
842 break;
843 }
844
845 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
846 // arguments and the arguments after the retaddr has been pushed are aligned.
847 if ((NumBytes & 7) == 0)
848 NumBytes += 4;
849
Chris Lattner94dd2922006-02-13 09:00:43 +0000850 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000851
852 // Arguments go on the stack in reverse order, as specified by the ABI.
853 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000854 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000855 NumIntRegs = 0;
856 std::vector<SDOperand> Stores;
857 std::vector<SDOperand> RegValuesToPass;
858 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
859 switch (getValueType(Args[i].second)) {
860 default: assert(0 && "Unexpected ValueType for argument!");
861 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000862 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
863 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000864 case MVT::i8:
865 case MVT::i16:
866 case MVT::i32:
867 if (NumIntRegs < 2) {
868 RegValuesToPass.push_back(Args[i].first);
869 ++NumIntRegs;
870 break;
871 }
872 // Fall through
873 case MVT::f32: {
874 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
875 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
876 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
877 Args[i].first, PtrOff,
878 DAG.getSrcValue(NULL)));
879 ArgOffset += 4;
880 break;
881 }
882 case MVT::i64:
883 if (NumIntRegs < 2) { // Can pass part of it in regs?
884 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
885 Args[i].first, DAG.getConstant(1, MVT::i32));
886 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
887 Args[i].first, DAG.getConstant(0, MVT::i32));
888 RegValuesToPass.push_back(Lo);
889 ++NumIntRegs;
890 if (NumIntRegs < 2) { // Pass both parts in regs?
891 RegValuesToPass.push_back(Hi);
892 ++NumIntRegs;
893 } else {
894 // Pass the high part in memory.
895 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
896 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
897 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
898 Hi, PtrOff, DAG.getSrcValue(NULL)));
899 ArgOffset += 4;
900 }
901 break;
902 }
903 // Fall through
904 case MVT::f64:
905 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
906 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
907 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
908 Args[i].first, PtrOff,
909 DAG.getSrcValue(NULL)));
910 ArgOffset += 8;
911 break;
912 }
913 }
914 if (!Stores.empty())
915 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
916
917 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
918 // arguments and the arguments after the retaddr has been pushed are aligned.
919 if ((ArgOffset & 7) == 0)
920 ArgOffset += 4;
921
922 std::vector<MVT::ValueType> RetVals;
923 MVT::ValueType RetTyVT = getValueType(RetTy);
924
925 RetVals.push_back(MVT::Other);
926
927 // The result values produced have to be legal. Promote the result.
928 switch (RetTyVT) {
929 case MVT::isVoid: break;
930 default:
931 RetVals.push_back(RetTyVT);
932 break;
933 case MVT::i1:
934 case MVT::i8:
935 case MVT::i16:
936 RetVals.push_back(MVT::i32);
937 break;
938 case MVT::f32:
939 if (X86ScalarSSE)
940 RetVals.push_back(MVT::f32);
941 else
942 RetVals.push_back(MVT::f64);
943 break;
944 case MVT::i64:
945 RetVals.push_back(MVT::i32);
946 RetVals.push_back(MVT::i32);
947 break;
948 }
949
Evan Cheng8e44f072006-01-27 21:26:54 +0000950 if (!X86PatIsel) {
Evan Chengd9558e02006-01-06 00:43:03 +0000951 // Build a sequence of copy-to-reg nodes chained together with token chain
952 // and flag operands which copy the outgoing args into registers.
953 SDOperand InFlag;
954 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
955 unsigned CCReg;
956 SDOperand RegToPass = RegValuesToPass[i];
957 switch (RegToPass.getValueType()) {
958 default: assert(0 && "Bad thing to pass in regs");
959 case MVT::i8:
960 CCReg = (i == 0) ? X86::AL : X86::DL;
961 break;
962 case MVT::i16:
963 CCReg = (i == 0) ? X86::AX : X86::DX;
964 break;
965 case MVT::i32:
966 CCReg = (i == 0) ? X86::EAX : X86::EDX;
967 break;
968 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000969
Evan Chengd9558e02006-01-06 00:43:03 +0000970 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
971 InFlag = Chain.getValue(1);
972 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000973
Evan Chengd9558e02006-01-06 00:43:03 +0000974 std::vector<MVT::ValueType> NodeTys;
975 NodeTys.push_back(MVT::Other); // Returns a chain
976 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd9558e02006-01-06 00:43:03 +0000977 std::vector<SDOperand> Ops;
978 Ops.push_back(Chain);
979 Ops.push_back(Callee);
980 if (InFlag.Val)
981 Ops.push_back(InFlag);
982
983 // FIXME: Do not generate X86ISD::TAILCALL for now.
984 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
985 InFlag = Chain.getValue(1);
986
Chris Lattneraf63bb02006-01-24 05:17:12 +0000987 NodeTys.clear();
988 NodeTys.push_back(MVT::Other); // Returns a chain
989 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
990 Ops.clear();
991 Ops.push_back(Chain);
992 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
993 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
994 Ops.push_back(InFlag);
995 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
996 InFlag = Chain.getValue(1);
997
Evan Chengd9558e02006-01-06 00:43:03 +0000998 SDOperand RetVal;
999 if (RetTyVT != MVT::isVoid) {
1000 switch (RetTyVT) {
1001 default: assert(0 && "Unknown value type to return!");
1002 case MVT::i1:
1003 case MVT::i8:
1004 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1005 Chain = RetVal.getValue(1);
Evan Cheng68e5d082006-01-18 08:08:38 +00001006 if (RetTyVT == MVT::i1)
1007 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Chengd9558e02006-01-06 00:43:03 +00001008 break;
1009 case MVT::i16:
1010 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1011 Chain = RetVal.getValue(1);
1012 break;
1013 case MVT::i32:
1014 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1015 Chain = RetVal.getValue(1);
1016 break;
1017 case MVT::i64: {
1018 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1019 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1020 Lo.getValue(2));
1021 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1022 Chain = Hi.getValue(1);
1023 break;
1024 }
Evan Cheng357c58e2006-01-17 21:58:21 +00001025 case MVT::f32:
Evan Chengd9558e02006-01-06 00:43:03 +00001026 case MVT::f64: {
1027 std::vector<MVT::ValueType> Tys;
1028 Tys.push_back(MVT::f64);
1029 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001030 Tys.push_back(MVT::Flag);
Evan Chengd9558e02006-01-06 00:43:03 +00001031 std::vector<SDOperand> Ops;
1032 Ops.push_back(Chain);
1033 Ops.push_back(InFlag);
1034 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001035 Chain = RetVal.getValue(1);
1036 InFlag = RetVal.getValue(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001037 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001038 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1039 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng2059f882006-01-17 00:37:42 +00001040 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Chengd9558e02006-01-06 00:43:03 +00001041 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6dab0532006-01-30 08:02:57 +00001042 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Chengd9558e02006-01-06 00:43:03 +00001043 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1044 Tys.clear();
1045 Tys.push_back(MVT::Other);
1046 Ops.clear();
1047 Ops.push_back(Chain);
1048 Ops.push_back(RetVal);
1049 Ops.push_back(StackSlot);
1050 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001051 Ops.push_back(InFlag);
Evan Chengd9558e02006-01-06 00:43:03 +00001052 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1053 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1054 DAG.getSrcValue(NULL));
1055 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001056 }
Evan Cheng357c58e2006-01-17 21:58:21 +00001057
1058 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1059 // FIXME: we would really like to remember that this FP_ROUND
1060 // operation is okay to eliminate if we allow excess FP precision.
1061 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Chengd9558e02006-01-06 00:43:03 +00001062 break;
1063 }
1064 }
1065 }
1066
Evan Chengd9558e02006-01-06 00:43:03 +00001067 return std::make_pair(RetVal, Chain);
1068 } else {
1069 std::vector<SDOperand> Ops;
1070 Ops.push_back(Chain);
1071 Ops.push_back(Callee);
1072 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1073 // Callee pops all arg values on the stack.
1074 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1075
1076 // Pass register arguments as needed.
1077 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1078
Chris Lattner259e97c2006-01-31 19:43:35 +00001079 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL :X86ISD::CALL,
Evan Chengd9558e02006-01-06 00:43:03 +00001080 RetVals, Ops);
1081 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1082
1083 SDOperand ResultVal;
1084 switch (RetTyVT) {
1085 case MVT::isVoid: break;
1086 default:
1087 ResultVal = TheCall.getValue(1);
1088 break;
1089 case MVT::i1:
1090 case MVT::i8:
1091 case MVT::i16:
1092 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1093 break;
1094 case MVT::f32:
1095 // FIXME: we would really like to remember that this FP_ROUND operation is
1096 // okay to eliminate if we allow excess FP precision.
1097 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1098 break;
1099 case MVT::i64:
1100 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1101 TheCall.getValue(2));
1102 break;
1103 }
1104
1105 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001106 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001107}
1108
1109SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1110 if (ReturnAddrIndex == 0) {
1111 // Set up a frame object for the return address.
1112 MachineFunction &MF = DAG.getMachineFunction();
1113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1114 }
1115
1116 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1117}
1118
1119
1120
1121std::pair<SDOperand, SDOperand> X86TargetLowering::
1122LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1123 SelectionDAG &DAG) {
1124 SDOperand Result;
1125 if (Depth) // Depths > 0 not supported yet!
1126 Result = DAG.getConstant(0, getPointerTy());
1127 else {
1128 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1129 if (!isFrameAddress)
1130 // Just load the return address
1131 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1132 DAG.getSrcValue(NULL));
1133 else
1134 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1135 DAG.getConstant(4, MVT::i32));
1136 }
1137 return std::make_pair(Result, Chain);
1138}
1139
Evan Cheng4a460802006-01-11 00:33:36 +00001140/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1141/// which corresponds to the condition code.
1142static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1143 switch (X86CC) {
1144 default: assert(0 && "Unknown X86 conditional code!");
1145 case X86ISD::COND_A: return X86::JA;
1146 case X86ISD::COND_AE: return X86::JAE;
1147 case X86ISD::COND_B: return X86::JB;
1148 case X86ISD::COND_BE: return X86::JBE;
1149 case X86ISD::COND_E: return X86::JE;
1150 case X86ISD::COND_G: return X86::JG;
1151 case X86ISD::COND_GE: return X86::JGE;
1152 case X86ISD::COND_L: return X86::JL;
1153 case X86ISD::COND_LE: return X86::JLE;
1154 case X86ISD::COND_NE: return X86::JNE;
1155 case X86ISD::COND_NO: return X86::JNO;
1156 case X86ISD::COND_NP: return X86::JNP;
1157 case X86ISD::COND_NS: return X86::JNS;
1158 case X86ISD::COND_O: return X86::JO;
1159 case X86ISD::COND_P: return X86::JP;
1160 case X86ISD::COND_S: return X86::JS;
1161 }
1162}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001163
Evan Cheng6dfa9992006-01-30 23:41:35 +00001164/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1165/// specific condition code. It returns a false if it cannot do a direct
1166/// translation. X86CC is the translated CondCode. Flip is set to true if the
1167/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001168static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1169 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001170 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001171 Flip = false;
1172 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001173 if (!isFP) {
1174 switch (SetCCOpcode) {
1175 default: break;
1176 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1177 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1178 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1179 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1180 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1181 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1182 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1183 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1184 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1185 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1186 }
1187 } else {
1188 // On a floating point condition, the flags are set as follows:
1189 // ZF PF CF op
1190 // 0 | 0 | 0 | X > Y
1191 // 0 | 0 | 1 | X < Y
1192 // 1 | 0 | 0 | X == Y
1193 // 1 | 1 | 1 | unordered
1194 switch (SetCCOpcode) {
1195 default: break;
1196 case ISD::SETUEQ:
1197 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001198 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001199 case ISD::SETOGT:
1200 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001201 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001202 case ISD::SETOGE:
1203 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001204 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001205 case ISD::SETULT:
1206 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001207 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001208 case ISD::SETULE:
1209 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1210 case ISD::SETONE:
1211 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1212 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1213 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1214 }
1215 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001216
1217 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001218}
1219
Evan Cheng4a460802006-01-11 00:33:36 +00001220/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1221/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001222/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001223static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001224 switch (X86CC) {
1225 default:
1226 return false;
1227 case X86ISD::COND_B:
1228 case X86ISD::COND_BE:
1229 case X86ISD::COND_E:
1230 case X86ISD::COND_P:
1231 case X86ISD::COND_A:
1232 case X86ISD::COND_AE:
1233 case X86ISD::COND_NE:
1234 case X86ISD::COND_NP:
1235 return true;
1236 }
1237}
1238
Evan Cheng4a460802006-01-11 00:33:36 +00001239MachineBasicBlock *
1240X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1241 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001242 switch (MI->getOpcode()) {
1243 default: assert(false && "Unexpected instr type to insert");
1244 case X86::CMOV_FR32:
1245 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001246 // To "insert" a SELECT_CC instruction, we actually have to insert the
1247 // diamond control-flow pattern. The incoming instruction knows the
1248 // destination vreg to set, the condition code register to branch on, the
1249 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 ilist<MachineBasicBlock>::iterator It = BB;
1252 ++It;
1253
1254 // thisMBB:
1255 // ...
1256 // TrueVal = ...
1257 // cmpTY ccX, r1, r2
1258 // bCC copy1MBB
1259 // fallthrough --> copy0MBB
1260 MachineBasicBlock *thisMBB = BB;
1261 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1262 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1263 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1264 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1265 MachineFunction *F = BB->getParent();
1266 F->getBasicBlockList().insert(It, copy0MBB);
1267 F->getBasicBlockList().insert(It, sinkMBB);
1268 // Update machine-CFG edges
1269 BB->addSuccessor(copy0MBB);
1270 BB->addSuccessor(sinkMBB);
1271
1272 // copy0MBB:
1273 // %FalseValue = ...
1274 // # fallthrough to sinkMBB
1275 BB = copy0MBB;
1276
1277 // Update machine-CFG edges
1278 BB->addSuccessor(sinkMBB);
1279
1280 // sinkMBB:
1281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1282 // ...
1283 BB = sinkMBB;
1284 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001287
Evan Cheng0cc39452006-01-16 21:21:29 +00001288 delete MI; // The pseudo instruction is gone now.
1289 return BB;
1290 }
Evan Cheng4a460802006-01-11 00:33:36 +00001291
Evan Cheng0cc39452006-01-16 21:21:29 +00001292 case X86::FP_TO_INT16_IN_MEM:
1293 case X86::FP_TO_INT32_IN_MEM:
1294 case X86::FP_TO_INT64_IN_MEM: {
1295 // Change the floating point control register to use "round towards zero"
1296 // mode when truncating to an integer value.
1297 MachineFunction *F = BB->getParent();
1298 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1299 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1300
1301 // Load the old value of the high byte of the control word...
1302 unsigned OldCW =
1303 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1304 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1305
1306 // Set the high part to be round to zero...
1307 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1308
1309 // Reload the modified control word now...
1310 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1311
1312 // Restore the memory image of control word to original value
1313 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1314
1315 // Get the X86 opcode to use.
1316 unsigned Opc;
1317 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001318 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001319 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1320 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1321 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1322 }
1323
1324 X86AddressMode AM;
1325 MachineOperand &Op = MI->getOperand(0);
1326 if (Op.isRegister()) {
1327 AM.BaseType = X86AddressMode::RegBase;
1328 AM.Base.Reg = Op.getReg();
1329 } else {
1330 AM.BaseType = X86AddressMode::FrameIndexBase;
1331 AM.Base.FrameIndex = Op.getFrameIndex();
1332 }
1333 Op = MI->getOperand(1);
1334 if (Op.isImmediate())
1335 AM.Scale = Op.getImmedValue();
1336 Op = MI->getOperand(2);
1337 if (Op.isImmediate())
1338 AM.IndexReg = Op.getImmedValue();
1339 Op = MI->getOperand(3);
1340 if (Op.isGlobalAddress()) {
1341 AM.GV = Op.getGlobal();
1342 } else {
1343 AM.Disp = Op.getImmedValue();
1344 }
1345 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1346
1347 // Reload the original control word now.
1348 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1349
1350 delete MI; // The pseudo instruction is gone now.
1351 return BB;
1352 }
1353 }
Evan Cheng4a460802006-01-11 00:33:36 +00001354}
1355
1356
1357//===----------------------------------------------------------------------===//
1358// X86 Custom Lowering Hooks
1359//===----------------------------------------------------------------------===//
1360
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001361/// LowerOperation - Provide custom lowering hooks for some operations.
1362///
1363SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1364 switch (Op.getOpcode()) {
1365 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001366 case ISD::ADD_PARTS:
1367 case ISD::SUB_PARTS: {
1368 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1369 "Not an i64 add/sub!");
1370 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1371 std::vector<MVT::ValueType> Tys;
1372 Tys.push_back(MVT::i32);
1373 Tys.push_back(MVT::Flag);
1374 std::vector<SDOperand> Ops;
1375 Ops.push_back(Op.getOperand(0));
1376 Ops.push_back(Op.getOperand(2));
1377 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1378 Tys, Ops);
1379 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1380 Op.getOperand(1), Op.getOperand(3),
1381 Lo.getValue(1));
1382 Tys.clear();
1383 Tys.push_back(MVT::i32);
1384 Tys.push_back(MVT::i32);
1385 Ops.clear();
1386 Ops.push_back(Lo);
1387 Ops.push_back(Hi);
1388 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1389 }
1390 case ISD::SHL_PARTS:
1391 case ISD::SRA_PARTS:
1392 case ISD::SRL_PARTS: {
1393 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1394 "Not an i64 shift!");
1395 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1396 SDOperand ShOpLo = Op.getOperand(0);
1397 SDOperand ShOpHi = Op.getOperand(1);
1398 SDOperand ShAmt = Op.getOperand(2);
1399 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001400 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001401 : DAG.getConstant(0, MVT::i32);
1402
1403 SDOperand Tmp2, Tmp3;
1404 if (Op.getOpcode() == ISD::SHL_PARTS) {
1405 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1406 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1407 } else {
1408 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001409 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001410 }
1411
1412 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1413 ShAmt, DAG.getConstant(32, MVT::i8));
1414
1415 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001416 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001417
1418 std::vector<MVT::ValueType> Tys;
1419 Tys.push_back(MVT::i32);
1420 Tys.push_back(MVT::Flag);
1421 std::vector<SDOperand> Ops;
1422 if (Op.getOpcode() == ISD::SHL_PARTS) {
1423 Ops.push_back(Tmp2);
1424 Ops.push_back(Tmp3);
1425 Ops.push_back(CC);
1426 Ops.push_back(InFlag);
1427 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1428 InFlag = Hi.getValue(1);
1429
1430 Ops.clear();
1431 Ops.push_back(Tmp3);
1432 Ops.push_back(Tmp1);
1433 Ops.push_back(CC);
1434 Ops.push_back(InFlag);
1435 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1436 } else {
1437 Ops.push_back(Tmp2);
1438 Ops.push_back(Tmp3);
1439 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001440 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001441 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1442 InFlag = Lo.getValue(1);
1443
1444 Ops.clear();
1445 Ops.push_back(Tmp3);
1446 Ops.push_back(Tmp1);
1447 Ops.push_back(CC);
1448 Ops.push_back(InFlag);
1449 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1450 }
1451
1452 Tys.clear();
1453 Tys.push_back(MVT::i32);
1454 Tys.push_back(MVT::i32);
1455 Ops.clear();
1456 Ops.push_back(Lo);
1457 Ops.push_back(Hi);
1458 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001460 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001461 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001462 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001463 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001464
1465 SDOperand Result;
1466 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1467 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001468 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001469 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001470 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001471 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1472 DAG.getEntryNode(), Op.getOperand(0),
1473 StackSlot, DAG.getSrcValue(NULL));
1474
1475 // Build the FILD
1476 std::vector<MVT::ValueType> Tys;
1477 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001478 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001479 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001480 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001481 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001482 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001483 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001484 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1485 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001486
1487 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001488 Chain = Result.getValue(1);
1489 SDOperand InFlag = Result.getValue(2);
1490
Evan Chenge3de85b2006-02-04 02:20:30 +00001491 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001492 // shouldn't be necessary except that RFP cannot be live across
1493 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1494 MachineFunction &MF = DAG.getMachineFunction();
1495 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1496 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1497 std::vector<MVT::ValueType> Tys;
1498 Tys.push_back(MVT::Other);
1499 std::vector<SDOperand> Ops;
1500 Ops.push_back(Chain);
1501 Ops.push_back(Result);
1502 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001503 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001504 Ops.push_back(InFlag);
1505 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1506 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1507 DAG.getSrcValue(NULL));
1508 }
1509
Evan Chenga3195e82006-01-12 22:54:21 +00001510 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001511 }
1512 case ISD::FP_TO_SINT: {
1513 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001514 "Unknown FP_TO_SINT to lower!");
1515 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1516 // stack slot.
1517 MachineFunction &MF = DAG.getMachineFunction();
1518 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1519 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1520 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1521
1522 unsigned Opc;
1523 switch (Op.getValueType()) {
1524 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1525 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1526 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1527 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1528 }
1529
Evan Cheng6dab0532006-01-30 08:02:57 +00001530 SDOperand Chain = DAG.getEntryNode();
1531 SDOperand Value = Op.getOperand(0);
1532 if (X86ScalarSSE) {
1533 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1534 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1535 DAG.getSrcValue(0));
1536 std::vector<MVT::ValueType> Tys;
1537 Tys.push_back(MVT::f64);
1538 Tys.push_back(MVT::Other);
1539 std::vector<SDOperand> Ops;
1540 Ops.push_back(Chain);
1541 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001542 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001543 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1544 Chain = Value.getValue(1);
1545 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1546 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1547 }
1548
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549 // Build the FP_TO_INT*_IN_MEM
1550 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001551 Ops.push_back(Chain);
1552 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001553 Ops.push_back(StackSlot);
1554 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1555
1556 // Load the result.
1557 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1558 DAG.getSrcValue(NULL));
1559 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001560 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001561 std::vector<MVT::ValueType> Tys;
1562 Tys.push_back(MVT::Other);
1563 Tys.push_back(MVT::Flag);
1564 std::vector<SDOperand> Ops;
1565 Ops.push_back(Op.getOperand(0));
1566 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001567 Ops.clear();
1568 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1569 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1570 MVT::i32, Ops[0].getValue(2)));
1571 Ops.push_back(Ops[1].getValue(1));
1572 Tys[0] = Tys[1] = MVT::i32;
1573 Tys.push_back(MVT::Other);
1574 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001575 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001576 case ISD::FABS: {
1577 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001578 const Type *OpNTy = MVT::getTypeForValueType(VT);
1579 std::vector<Constant*> CV;
1580 if (VT == MVT::f64) {
1581 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1582 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1583 } else {
1584 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1585 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1586 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1588 }
1589 Constant *CS = ConstantStruct::get(CV);
1590 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1591 SDOperand Mask
1592 = DAG.getNode(X86ISD::LOAD_PACK,
1593 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001594 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1595 }
Evan Cheng223547a2006-01-31 22:28:30 +00001596 case ISD::FNEG: {
1597 MVT::ValueType VT = Op.getValueType();
1598 const Type *OpNTy = MVT::getTypeForValueType(VT);
1599 std::vector<Constant*> CV;
1600 if (VT == MVT::f64) {
1601 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1602 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1603 } else {
1604 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1605 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1606 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1607 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1608 }
1609 Constant *CS = ConstantStruct::get(CV);
1610 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1611 SDOperand Mask
1612 = DAG.getNode(X86ISD::LOAD_PACK,
1613 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1614 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1615 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001616 case ISD::SETCC: {
1617 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001618 SDOperand Cond;
1619 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001620 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1621 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001622 bool Flip;
1623 unsigned X86CC;
1624 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1625 if (Flip)
1626 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1627 Op.getOperand(1), Op.getOperand(0));
1628 else
1629 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1630 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001631 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1632 DAG.getConstant(X86CC, MVT::i8), Cond);
1633 } else {
1634 assert(isFP && "Illegal integer SetCC!");
1635
Evan Cheng6dfa9992006-01-30 23:41:35 +00001636 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1637 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001638 std::vector<MVT::ValueType> Tys;
1639 std::vector<SDOperand> Ops;
1640 switch (SetCCOpcode) {
1641 default: assert(false && "Illegal floating point SetCC!");
1642 case ISD::SETOEQ: { // !PF & ZF
1643 Tys.push_back(MVT::i8);
1644 Tys.push_back(MVT::Flag);
1645 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1646 Ops.push_back(Cond);
1647 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1648 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1649 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1650 Tmp1.getValue(1));
1651 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1652 }
Evan Chengd9558e02006-01-06 00:43:03 +00001653 case ISD::SETUNE: { // PF | !ZF
1654 Tys.push_back(MVT::i8);
1655 Tys.push_back(MVT::Flag);
1656 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1657 Ops.push_back(Cond);
1658 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1659 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1660 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1661 Tmp1.getValue(1));
1662 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1663 }
1664 }
1665 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001666 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001667 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001668 MVT::ValueType VT = Op.getValueType();
1669 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001670 bool isFPStack = isFP && !X86ScalarSSE;
1671 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001672 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001673 SDOperand Op0 = Op.getOperand(0);
1674 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001675 if (Op0.getOpcode() == ISD::SETCC)
1676 Op0 = LowerOperation(Op0, DAG);
1677
Evan Chengaaca22c2006-01-10 20:26:56 +00001678 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001679 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1680 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1681 // have another use it will be eliminated.
1682 // If the X86ISD::SETCC has more than one use, then it's probably better
1683 // to use a test instead of duplicating the X86ISD::CMP (for register
1684 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001685 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1686 if (!Op0.hasOneUse()) {
1687 std::vector<MVT::ValueType> Tys;
1688 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1689 Tys.push_back(Op0.Val->getValueType(i));
1690 std::vector<SDOperand> Ops;
1691 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1692 Ops.push_back(Op0.getOperand(i));
1693 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1694 }
1695
Evan Cheng1bcee362006-01-13 01:03:02 +00001696 CC = Op0.getOperand(0);
1697 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001698 // Make a copy as flag result cannot be used by more than one.
1699 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1700 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001701 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001702 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001703 } else
1704 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001705 } else
1706 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001707
Evan Cheng189d01e2006-01-13 01:06:49 +00001708 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001709 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001710 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001711 }
Evan Chenge3413162006-01-09 18:33:28 +00001712
1713 std::vector<MVT::ValueType> Tys;
1714 Tys.push_back(Op.getValueType());
1715 Tys.push_back(MVT::Flag);
1716 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001717 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1718 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001719 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001720 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001721 Ops.push_back(CC);
1722 Ops.push_back(Cond);
1723 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001724 }
Evan Cheng898101c2005-12-19 23:12:38 +00001725 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001726 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001727 SDOperand Cond = Op.getOperand(1);
1728 SDOperand Dest = Op.getOperand(2);
1729 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001730 if (Cond.getOpcode() == ISD::SETCC)
1731 Cond = LowerOperation(Cond, DAG);
1732
Evan Chengd5781fc2005-12-21 20:21:51 +00001733 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001734 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1735 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1736 // have another use it will be eliminated.
1737 // If the X86ISD::SETCC has more than one use, then it's probably better
1738 // to use a test instead of duplicating the X86ISD::CMP (for register
1739 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001740 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1741 if (!Cond.hasOneUse()) {
1742 std::vector<MVT::ValueType> Tys;
1743 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1744 Tys.push_back(Cond.Val->getValueType(i));
1745 std::vector<SDOperand> Ops;
1746 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1747 Ops.push_back(Cond.getOperand(i));
1748 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1749 }
1750
Evan Cheng1bcee362006-01-13 01:03:02 +00001751 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001752 Cond = Cond.getOperand(1);
1753 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001754 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001755 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001756 } else
1757 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001758 } else
1759 addTest = true;
1760
1761 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001762 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001763 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1764 }
1765 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1766 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1767 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001768 case ISD::MEMSET: {
1769 SDOperand InFlag;
1770 SDOperand Chain = Op.getOperand(0);
1771 unsigned Align =
1772 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1773 if (Align == 0) Align = 1;
1774
1775 MVT::ValueType AVT;
1776 SDOperand Count;
1777 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1778 unsigned ValReg;
1779 unsigned Val = ValC->getValue() & 255;
1780
1781 // If the value is a constant, then we can potentially use larger sets.
1782 switch (Align & 3) {
1783 case 2: // WORD aligned
1784 AVT = MVT::i16;
1785 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1786 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1787 else
1788 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1789 DAG.getConstant(1, MVT::i8));
1790 Val = (Val << 8) | Val;
1791 ValReg = X86::AX;
1792 break;
1793 case 0: // DWORD aligned
1794 AVT = MVT::i32;
1795 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1796 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1797 else
1798 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1799 DAG.getConstant(2, MVT::i8));
1800 Val = (Val << 8) | Val;
1801 Val = (Val << 16) | Val;
1802 ValReg = X86::EAX;
1803 break;
1804 default: // Byte aligned
1805 AVT = MVT::i8;
1806 Count = Op.getOperand(3);
1807 ValReg = X86::AL;
1808 break;
1809 }
1810
1811 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1812 InFlag);
1813 InFlag = Chain.getValue(1);
1814 } else {
1815 AVT = MVT::i8;
1816 Count = Op.getOperand(3);
1817 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1818 InFlag = Chain.getValue(1);
1819 }
1820
1821 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1822 InFlag = Chain.getValue(1);
1823 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1824 InFlag = Chain.getValue(1);
1825
1826 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1827 DAG.getValueType(AVT), InFlag);
1828 }
1829 case ISD::MEMCPY: {
1830 SDOperand Chain = Op.getOperand(0);
1831 unsigned Align =
1832 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1833 if (Align == 0) Align = 1;
1834
1835 MVT::ValueType AVT;
1836 SDOperand Count;
1837 switch (Align & 3) {
1838 case 2: // WORD aligned
1839 AVT = MVT::i16;
1840 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1841 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1842 else
1843 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1844 DAG.getConstant(1, MVT::i8));
1845 break;
1846 case 0: // DWORD aligned
1847 AVT = MVT::i32;
1848 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1849 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1850 else
1851 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1852 DAG.getConstant(2, MVT::i8));
1853 break;
1854 default: // Byte aligned
1855 AVT = MVT::i8;
1856 Count = Op.getOperand(3);
1857 break;
1858 }
1859
1860 SDOperand InFlag;
1861 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1862 InFlag = Chain.getValue(1);
1863 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1864 InFlag = Chain.getValue(1);
1865 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1866 InFlag = Chain.getValue(1);
1867
1868 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1869 DAG.getValueType(AVT), InFlag);
1870 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001871 case ISD::GlobalAddress: {
Evan Cheng002fe9b2006-01-12 07:56:47 +00001872 SDOperand Result;
Evan Chengb077b842005-12-21 02:39:21 +00001873 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1874 // For Darwin, external and weak symbols are indirect, so we want to load
1875 // the value at address GV, not the value of GV itself. This means that
1876 // the GlobalAddress must be in the base or index register of the address,
1877 // not the GV offset field.
1878 if (getTargetMachine().
Evan Cheng2338c5c2006-02-07 08:38:37 +00001879 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals()) {
1880 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1881 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
1882 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1883 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1884 DAG.getSrcValue(NULL));
1885 }
Evan Cheng002fe9b2006-01-12 07:56:47 +00001886 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001887 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001888 case ISD::VASTART: {
1889 // vastart just stores the address of the VarArgsFrameIndex slot into the
1890 // memory location argument.
1891 // FIXME: Replace MVT::i32 with PointerTy
1892 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1893 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1894 Op.getOperand(1), Op.getOperand(2));
1895 }
Nate Begemanee625572006-01-27 21:09:22 +00001896 case ISD::RET: {
1897 SDOperand Copy;
1898
1899 switch(Op.getNumOperands()) {
1900 default:
1901 assert(0 && "Do not know how to return this many arguments!");
1902 abort();
1903 case 1:
1904 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1905 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1906 case 2: {
1907 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1908 if (MVT::isInteger(ArgVT))
1909 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1910 SDOperand());
1911 else if (!X86ScalarSSE) {
1912 std::vector<MVT::ValueType> Tys;
1913 Tys.push_back(MVT::Other);
1914 Tys.push_back(MVT::Flag);
1915 std::vector<SDOperand> Ops;
1916 Ops.push_back(Op.getOperand(0));
1917 Ops.push_back(Op.getOperand(1));
1918 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1919 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00001920 SDOperand MemLoc;
1921 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001922 SDOperand Value = Op.getOperand(1);
1923
Evan Cheng760df292006-02-01 01:19:32 +00001924 if (Value.getOpcode() == ISD::LOAD &&
1925 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00001926 Chain = Value.getOperand(0);
1927 MemLoc = Value.getOperand(1);
1928 } else {
1929 // Spill the value to memory and reload it into top of stack.
1930 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1931 MachineFunction &MF = DAG.getMachineFunction();
1932 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1933 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1934 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1935 Value, MemLoc, DAG.getSrcValue(0));
1936 }
Nate Begemanee625572006-01-27 21:09:22 +00001937 std::vector<MVT::ValueType> Tys;
1938 Tys.push_back(MVT::f64);
1939 Tys.push_back(MVT::Other);
1940 std::vector<SDOperand> Ops;
1941 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001942 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00001943 Ops.push_back(DAG.getValueType(ArgVT));
1944 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1945 Tys.clear();
1946 Tys.push_back(MVT::Other);
1947 Tys.push_back(MVT::Flag);
1948 Ops.clear();
1949 Ops.push_back(Copy.getValue(1));
1950 Ops.push_back(Copy);
1951 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1952 }
1953 break;
1954 }
1955 case 3:
1956 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1957 SDOperand());
1958 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1959 break;
1960 }
1961 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1962 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1963 Copy.getValue(1));
1964 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001965 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001966}
Evan Cheng72261582005-12-20 06:22:03 +00001967
1968const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1969 switch (Opcode) {
1970 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001971 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1972 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1973 case X86ISD::ADC: return "X86ISD::ADC";
1974 case X86ISD::SBB: return "X86ISD::SBB";
1975 case X86ISD::SHLD: return "X86ISD::SHLD";
1976 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00001977 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00001978 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00001979 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00001980 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00001981 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1982 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1983 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001984 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001985 case X86ISD::FST: return "X86ISD::FST";
1986 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001987 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001988 case X86ISD::CALL: return "X86ISD::CALL";
1989 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1990 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1991 case X86ISD::CMP: return "X86ISD::CMP";
1992 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001993 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001994 case X86ISD::CMOV: return "X86ISD::CMOV";
1995 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001996 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng67f92a72006-01-11 22:15:48 +00001997 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1998 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00001999 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng72261582005-12-20 06:22:03 +00002000 }
2001}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002002
2003bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002004 uint64_t Mask) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002005
2006 unsigned Opc = Op.getOpcode();
2007
2008 switch (Opc) {
2009 default:
2010 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2011 break;
2012 case X86ISD::SETCC: return (Mask & 1) == 0;
2013 }
2014
2015 return false;
2016}
Chris Lattner259e97c2006-01-31 19:43:35 +00002017
2018std::vector<unsigned> X86TargetLowering::
2019getRegForInlineAsmConstraint(const std::string &Constraint) const {
2020 if (Constraint.size() == 1) {
2021 // FIXME: not handling fp-stack yet!
2022 // FIXME: not handling MMX registers yet ('y' constraint).
2023 switch (Constraint[0]) { // GCC X86 Constraint Letters
2024 default: break; // Unknown constriant letter
2025 case 'r': // GENERAL_REGS
2026 case 'R': // LEGACY_REGS
2027 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2028 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2029 case 'l': // INDEX_REGS
2030 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2031 X86::ESI, X86::EDI, X86::EBP, 0);
2032 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2033 case 'Q': // Q_REGS
2034 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2035 case 'x': // SSE_REGS if SSE1 allowed
2036 if (Subtarget->hasSSE1())
2037 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2038 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2039 0);
2040 return std::vector<unsigned>();
2041 case 'Y': // SSE_REGS if SSE2 allowed
2042 if (Subtarget->hasSSE2())
2043 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2044 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2045 0);
2046 return std::vector<unsigned>();
2047 }
2048 }
2049
2050 // Handle explicit register names.
2051 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
2052}