blob: 04b9de348d1e02ebcbdb81fdc8ab1174702eb716 [file] [log] [blame]
Chris Lattner76ac0682005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
20#include "llvm/Function.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetOptions.h"
27using namespace llvm;
28
29// FIXME: temporary.
30#include "llvm/Support/CommandLine.h"
31static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
32 cl::desc("Enable fastcc on X86"));
33
34X86TargetLowering::X86TargetLowering(TargetMachine &TM)
35 : TargetLowering(TM) {
Chris Lattner76ac0682005-11-15 00:40:23 +000036 // Set up the TargetLowering object.
37
38 // X86 is weird, it always uses i8 for shift amounts and setcc results.
39 setShiftAmountType(MVT::i8);
40 setSetCCResultType(MVT::i8);
41 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000042 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000043 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000044 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner76ac0682005-11-15 00:40:23 +000045
46 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000047 addRegisterClass(MVT::i8, X86::R8RegisterClass);
48 addRegisterClass(MVT::i16, X86::R16RegisterClass);
49 addRegisterClass(MVT::i32, X86::R32RegisterClass);
50
51 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
52 // operation.
53 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
54 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
55 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000056
57 if (X86ScalarSSE)
58 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
59 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
60 else
61 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000062
63 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
64 // this operation.
65 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
66 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
67
68 if (!X86ScalarSSE) {
69 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
70 // isn't legal.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
72 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
73 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
74 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
75 }
76
77 // Handle FP_TO_UINT by promoting the destination to a larger signed
78 // conversion.
79 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
80 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
81 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
82
83 if (!X86ScalarSSE)
84 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
85
86 // 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 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
91
Chris Lattner30107e62005-12-23 05:15:23 +000092 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
93 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
94
Evan Cheng6fc31042005-12-19 23:12:38 +000095 if (X86DAGIsel) {
96 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
97 }
Chris Lattner76ac0682005-11-15 00:40:23 +000098 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
99 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
100 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
104 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
105 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
106 setOperationAction(ISD::FREM , MVT::f64 , Expand);
107 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
108 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
109 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
110 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
112 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
113 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
114 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
115 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000116 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000117
Evan Cheng6d2ab042006-01-11 23:20:05 +0000118 if (!X86DAGIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000119 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000120 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
121 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
122 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
123 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
124 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
125 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
126 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000127 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000128
Chris Lattner76ac0682005-11-15 00:40:23 +0000129 setOperationAction(ISD::READIO , MVT::i1 , Expand);
130 setOperationAction(ISD::READIO , MVT::i8 , Expand);
131 setOperationAction(ISD::READIO , MVT::i16 , Expand);
132 setOperationAction(ISD::READIO , MVT::i32 , Expand);
133 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
134 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
135 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
136 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
137
138 // These should be promoted to a larger select which is supported.
139 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
140 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng225a4d02005-12-17 01:21:05 +0000141 if (X86DAGIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000142 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000143 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
144 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000145 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
146 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000147 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
148 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
149 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000150 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
151 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
152 // X86 ret instruction may pop stack.
153 setOperationAction(ISD::RET , MVT::Other, Custom);
154 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000155 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000156 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
157 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
158 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
159 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
160 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
161 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000162 // X86 wants to expand memset / memcpy itself.
163 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
164 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000165 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000166
Chris Lattner9c415362005-11-29 06:16:21 +0000167 // We don't have line number support yet.
168 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000169 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
170 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000171
Chris Lattner78c358d2006-01-15 09:00:21 +0000172 // Expand to the default code.
173 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
174 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
175 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000176
Chris Lattner76ac0682005-11-15 00:40:23 +0000177 if (X86ScalarSSE) {
178 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000179 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
180 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000181
182 // SSE has no load+extend ops
183 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
184 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
185
186 // SSE has no i16 to fp conversion, only i32
187 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
188 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
189
190 // Expand FP_TO_UINT into a select.
191 // FIXME: We would like to use a Custom expander here eventually to do
192 // the optimal thing for SSE vs. the default expansion in the legalizer.
193 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
194
195 // We don't support sin/cos/sqrt/fmod
196 setOperationAction(ISD::FSIN , MVT::f64, Expand);
197 setOperationAction(ISD::FCOS , MVT::f64, Expand);
198 setOperationAction(ISD::FABS , MVT::f64, Expand);
199 setOperationAction(ISD::FNEG , MVT::f64, Expand);
200 setOperationAction(ISD::FREM , MVT::f64, Expand);
201 setOperationAction(ISD::FSIN , MVT::f32, Expand);
202 setOperationAction(ISD::FCOS , MVT::f32, Expand);
203 setOperationAction(ISD::FABS , MVT::f32, Expand);
204 setOperationAction(ISD::FNEG , MVT::f32, Expand);
205 setOperationAction(ISD::FREM , MVT::f32, Expand);
206
207 addLegalFPImmediate(+0.0); // xorps / xorpd
208 } else {
209 // Set up the FP register classes.
210 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
211
Evan Cheng6305e502006-01-12 22:54:21 +0000212 if (X86DAGIsel) {
213 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
214 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
215 }
216
Chris Lattner76ac0682005-11-15 00:40:23 +0000217 if (!UnsafeFPMath) {
218 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
219 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
220 }
221
222 addLegalFPImmediate(+0.0); // FLD0
223 addLegalFPImmediate(+1.0); // FLD1
224 addLegalFPImmediate(-0.0); // FLD0/FCHS
225 addLegalFPImmediate(-1.0); // FLD1/FCHS
226 }
227 computeRegisterProperties();
228
229 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
230 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
231 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
232 allowUnalignedMemoryAccesses = true; // x86 supports it!
233}
234
235std::vector<SDOperand>
236X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
237 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
238 return LowerFastCCArguments(F, DAG);
239 return LowerCCCArguments(F, DAG);
240}
241
242std::pair<SDOperand, SDOperand>
243X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
244 bool isVarArg, unsigned CallingConv,
245 bool isTailCall,
246 SDOperand Callee, ArgListTy &Args,
247 SelectionDAG &DAG) {
248 assert((!isVarArg || CallingConv == CallingConv::C) &&
249 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000250
251 // If the callee is a GlobalAddress node (quite common, every direct call is)
252 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
253 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
254 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000255 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
256 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000257
Chris Lattner76ac0682005-11-15 00:40:23 +0000258 if (CallingConv == CallingConv::Fast && EnableFastCC)
259 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
260 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
261}
262
Evan Chenga74ce622005-12-21 02:39:21 +0000263SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
264 SelectionDAG &DAG) {
265 if (!X86DAGIsel)
266 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
267
268 SDOperand Copy;
269 MVT::ValueType OpVT = Op.getValueType();
270 switch (OpVT) {
271 default: assert(0 && "Unknown type to return!");
272 case MVT::i32:
273 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
274 break;
275 case MVT::i64: {
276 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
277 DAG.getConstant(1, MVT::i32));
278 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
279 DAG.getConstant(0, MVT::i32));
Evan Cheng172fce72006-01-06 00:43:03 +0000280 Copy = DAG.getCopyToReg(Chain, X86::EDX, Hi, SDOperand());
281 Copy = DAG.getCopyToReg(Copy, X86::EAX, Lo, Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000282 break;
283 }
284 case MVT::f32:
Evan Chenga74ce622005-12-21 02:39:21 +0000285 case MVT::f64:
286 if (!X86ScalarSSE) {
287 std::vector<MVT::ValueType> Tys;
288 Tys.push_back(MVT::Other);
289 Tys.push_back(MVT::Flag);
290 std::vector<SDOperand> Ops;
291 Ops.push_back(Chain);
292 Ops.push_back(Op);
293 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
294 } else {
295 // Spill the value to memory and reload it into top of stack.
296 unsigned Size = MVT::getSizeInBits(OpVT)/8;
297 MachineFunction &MF = DAG.getMachineFunction();
298 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
299 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
300 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
301 StackSlot, DAG.getSrcValue(NULL));
302 std::vector<MVT::ValueType> Tys;
303 Tys.push_back(MVT::f64);
304 Tys.push_back(MVT::Other);
305 std::vector<SDOperand> Ops;
306 Ops.push_back(Chain);
307 Ops.push_back(StackSlot);
308 Ops.push_back(DAG.getValueType(OpVT));
309 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
310 Tys.clear();
311 Tys.push_back(MVT::Other);
312 Tys.push_back(MVT::Flag);
313 Ops.clear();
314 Ops.push_back(Copy.getValue(1));
315 Ops.push_back(Copy);
316 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
317 }
318 break;
319 }
Evan Chengc1583db2005-12-21 20:21:51 +0000320
321 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
322 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
323 Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000324}
325
Chris Lattner76ac0682005-11-15 00:40:23 +0000326//===----------------------------------------------------------------------===//
327// C Calling Convention implementation
328//===----------------------------------------------------------------------===//
329
330std::vector<SDOperand>
331X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
332 std::vector<SDOperand> ArgValues;
333
334 MachineFunction &MF = DAG.getMachineFunction();
335 MachineFrameInfo *MFI = MF.getFrameInfo();
336
337 // Add DAG nodes to load the arguments... On entry to a function on the X86,
338 // the stack frame looks like this:
339 //
340 // [ESP] -- return address
341 // [ESP + 4] -- first argument (leftmost lexically)
342 // [ESP + 8] -- second argument, if first argument is four bytes in size
343 // ...
344 //
345 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
346 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
347 MVT::ValueType ObjectVT = getValueType(I->getType());
348 unsigned ArgIncrement = 4;
349 unsigned ObjSize;
350 switch (ObjectVT) {
351 default: assert(0 && "Unhandled argument type!");
352 case MVT::i1:
353 case MVT::i8: ObjSize = 1; break;
354 case MVT::i16: ObjSize = 2; break;
355 case MVT::i32: ObjSize = 4; break;
356 case MVT::i64: ObjSize = ArgIncrement = 8; break;
357 case MVT::f32: ObjSize = 4; break;
358 case MVT::f64: ObjSize = ArgIncrement = 8; break;
359 }
360 // Create the frame index object for this incoming parameter...
361 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
362
363 // Create the SelectionDAG nodes corresponding to a load from this parameter
364 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
365
366 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
367 // dead loads.
368 SDOperand ArgValue;
369 if (!I->use_empty())
370 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
371 DAG.getSrcValue(NULL));
372 else {
373 if (MVT::isInteger(ObjectVT))
374 ArgValue = DAG.getConstant(0, ObjectVT);
375 else
376 ArgValue = DAG.getConstantFP(0, ObjectVT);
377 }
378 ArgValues.push_back(ArgValue);
379
380 ArgOffset += ArgIncrement; // Move on to the next argument...
381 }
382
383 // If the function takes variable number of arguments, make a frame index for
384 // the start of the first vararg value... for expansion of llvm.va_start.
385 if (F.isVarArg())
386 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
387 ReturnAddrIndex = 0; // No return address slot generated yet.
388 BytesToPopOnReturn = 0; // Callee pops nothing.
389 BytesCallerReserves = ArgOffset;
390
391 // Finally, inform the code generator which regs we return values in.
392 switch (getValueType(F.getReturnType())) {
393 default: assert(0 && "Unknown type!");
394 case MVT::isVoid: break;
395 case MVT::i1:
396 case MVT::i8:
397 case MVT::i16:
398 case MVT::i32:
399 MF.addLiveOut(X86::EAX);
400 break;
401 case MVT::i64:
402 MF.addLiveOut(X86::EAX);
403 MF.addLiveOut(X86::EDX);
404 break;
405 case MVT::f32:
406 case MVT::f64:
407 MF.addLiveOut(X86::ST0);
408 break;
409 }
410 return ArgValues;
411}
412
413std::pair<SDOperand, SDOperand>
414X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
415 bool isVarArg, bool isTailCall,
416 SDOperand Callee, ArgListTy &Args,
417 SelectionDAG &DAG) {
418 // Count how many bytes are to be pushed on the stack.
419 unsigned NumBytes = 0;
420
421 if (Args.empty()) {
422 // Save zero bytes.
423 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
424 DAG.getConstant(0, getPointerTy()));
425 } else {
426 for (unsigned i = 0, e = Args.size(); i != e; ++i)
427 switch (getValueType(Args[i].second)) {
428 default: assert(0 && "Unknown value type!");
429 case MVT::i1:
430 case MVT::i8:
431 case MVT::i16:
432 case MVT::i32:
433 case MVT::f32:
434 NumBytes += 4;
435 break;
436 case MVT::i64:
437 case MVT::f64:
438 NumBytes += 8;
439 break;
440 }
441
442 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
443 DAG.getConstant(NumBytes, getPointerTy()));
444
445 // Arguments go on the stack in reverse order, as specified by the ABI.
446 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000447 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000448 std::vector<SDOperand> Stores;
449
450 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
451 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
452 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
453
454 switch (getValueType(Args[i].second)) {
455 default: assert(0 && "Unexpected ValueType for argument!");
456 case MVT::i1:
457 case MVT::i8:
458 case MVT::i16:
459 // Promote the integer to 32 bits. If the input type is signed use a
460 // sign extend, otherwise use a zero extend.
461 if (Args[i].second->isSigned())
462 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
463 else
464 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
465
466 // FALL THROUGH
467 case MVT::i32:
468 case MVT::f32:
469 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
470 Args[i].first, PtrOff,
471 DAG.getSrcValue(NULL)));
472 ArgOffset += 4;
473 break;
474 case MVT::i64:
475 case MVT::f64:
476 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
477 Args[i].first, PtrOff,
478 DAG.getSrcValue(NULL)));
479 ArgOffset += 8;
480 break;
481 }
482 }
483 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
484 }
485
486 std::vector<MVT::ValueType> RetVals;
487 MVT::ValueType RetTyVT = getValueType(RetTy);
488 RetVals.push_back(MVT::Other);
489
490 // The result values produced have to be legal. Promote the result.
491 switch (RetTyVT) {
492 case MVT::isVoid: break;
493 default:
494 RetVals.push_back(RetTyVT);
495 break;
496 case MVT::i1:
497 case MVT::i8:
498 case MVT::i16:
499 RetVals.push_back(MVT::i32);
500 break;
501 case MVT::f32:
502 if (X86ScalarSSE)
503 RetVals.push_back(MVT::f32);
504 else
505 RetVals.push_back(MVT::f64);
506 break;
507 case MVT::i64:
508 RetVals.push_back(MVT::i32);
509 RetVals.push_back(MVT::i32);
510 break;
511 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000512
Evan Cheng45e190982006-01-05 00:27:02 +0000513 if (X86DAGIsel) {
514 std::vector<MVT::ValueType> NodeTys;
515 NodeTys.push_back(MVT::Other); // Returns a chain
516 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000517 std::vector<SDOperand> Ops;
518 Ops.push_back(Chain);
519 Ops.push_back(Callee);
520
Evan Cheng172fce72006-01-06 00:43:03 +0000521 // FIXME: Do not generate X86ISD::TAILCALL for now.
522 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000523 SDOperand InFlag = Chain.getValue(1);
524
Chris Lattner6f33eae2006-01-24 05:17:12 +0000525 NodeTys.clear();
526 NodeTys.push_back(MVT::Other); // Returns a chain
527 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
528 Ops.clear();
529 Ops.push_back(Chain);
530 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
531 Ops.push_back(DAG.getConstant(0, getPointerTy()));
532 Ops.push_back(InFlag);
533 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
534 InFlag = Chain.getValue(1);
535
Evan Cheng45e190982006-01-05 00:27:02 +0000536 SDOperand RetVal;
537 if (RetTyVT != MVT::isVoid) {
538 switch (RetTyVT) {
539 default: assert(0 && "Unknown value type to return!");
540 case MVT::i1:
541 case MVT::i8:
542 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
543 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000544 if (RetTyVT == MVT::i1)
545 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000546 break;
547 case MVT::i16:
548 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
549 Chain = RetVal.getValue(1);
550 break;
551 case MVT::i32:
552 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
553 Chain = RetVal.getValue(1);
554 break;
555 case MVT::i64: {
556 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
557 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
558 Lo.getValue(2));
559 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
560 Chain = Hi.getValue(1);
561 break;
562 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000563 case MVT::f32:
Evan Cheng45e190982006-01-05 00:27:02 +0000564 case MVT::f64: {
565 std::vector<MVT::ValueType> Tys;
566 Tys.push_back(MVT::f64);
567 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +0000568 Tys.push_back(MVT::Flag);
Evan Cheng45e190982006-01-05 00:27:02 +0000569 std::vector<SDOperand> Ops;
570 Ops.push_back(Chain);
571 Ops.push_back(InFlag);
572 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +0000573 Chain = RetVal.getValue(1);
574 InFlag = RetVal.getValue(2);
Evan Cheng45e190982006-01-05 00:27:02 +0000575 if (X86ScalarSSE) {
Evan Cheng561881f2006-01-17 00:37:42 +0000576 // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
577 // shouldn't be necessary except for RFP cannot be live across
578 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng45e190982006-01-05 00:27:02 +0000579 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
580 MachineFunction &MF = DAG.getMachineFunction();
581 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
582 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
583 Tys.clear();
584 Tys.push_back(MVT::Other);
585 Ops.clear();
586 Ops.push_back(Chain);
587 Ops.push_back(RetVal);
588 Ops.push_back(StackSlot);
589 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +0000590 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000591 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
592 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
593 DAG.getSrcValue(NULL));
594 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +0000595 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000596
597 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
598 // FIXME: we would really like to remember that this FP_ROUND
599 // operation is okay to eliminate if we allow excess FP precision.
600 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000601 break;
602 }
603 }
604 }
605
Evan Cheng45e190982006-01-05 00:27:02 +0000606 return std::make_pair(RetVal, Chain);
607 } else {
608 std::vector<SDOperand> Ops;
609 Ops.push_back(Chain);
610 Ops.push_back(Callee);
611 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
612 Ops.push_back(DAG.getConstant(0, getPointerTy()));
613
614 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
615 RetVals, Ops);
616
617 SDOperand ResultVal;
618 switch (RetTyVT) {
619 case MVT::isVoid: break;
620 default:
621 ResultVal = TheCall.getValue(1);
622 break;
623 case MVT::i1:
624 case MVT::i8:
625 case MVT::i16:
626 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
627 break;
628 case MVT::f32:
629 // FIXME: we would really like to remember that this FP_ROUND operation is
630 // okay to eliminate if we allow excess FP precision.
631 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
632 break;
633 case MVT::i64:
634 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
635 TheCall.getValue(2));
636 break;
637 }
638
639 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
640 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000641 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000642}
643
644SDOperand
645X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
646 Value *VAListV, SelectionDAG &DAG) {
647 // vastart just stores the address of the VarArgsFrameIndex slot.
648 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
649 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
650 DAG.getSrcValue(VAListV));
651}
652
653
654std::pair<SDOperand,SDOperand>
655X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
656 Value *VAListV, const Type *ArgTy,
657 SelectionDAG &DAG) {
658 MVT::ValueType ArgVT = getValueType(ArgTy);
659 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
660 VAListP, DAG.getSrcValue(VAListV));
661 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
662 DAG.getSrcValue(NULL));
663 unsigned Amt;
664 if (ArgVT == MVT::i32)
665 Amt = 4;
666 else {
667 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
668 "Other types should have been promoted for varargs!");
669 Amt = 8;
670 }
671 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
672 DAG.getConstant(Amt, Val.getValueType()));
673 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
674 Val, VAListP, DAG.getSrcValue(VAListV));
675 return std::make_pair(Result, Chain);
676}
677
678//===----------------------------------------------------------------------===//
679// Fast Calling Convention implementation
680//===----------------------------------------------------------------------===//
681//
682// The X86 'fast' calling convention passes up to two integer arguments in
683// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
684// and requires that the callee pop its arguments off the stack (allowing proper
685// tail calls), and has the same return value conventions as C calling convs.
686//
687// This calling convention always arranges for the callee pop value to be 8n+4
688// bytes, which is needed for tail recursion elimination and stack alignment
689// reasons.
690//
691// Note that this can be enhanced in the future to pass fp vals in registers
692// (when we have a global fp allocator) and do other tricks.
693//
694
695/// AddLiveIn - This helper function adds the specified physical register to the
696/// MachineFunction as a live in value. It also creates a corresponding virtual
697/// register for it.
698static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
699 TargetRegisterClass *RC) {
700 assert(RC->contains(PReg) && "Not the correct regclass!");
701 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
702 MF.addLiveIn(PReg, VReg);
703 return VReg;
704}
705
706
707std::vector<SDOperand>
708X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
709 std::vector<SDOperand> ArgValues;
710
711 MachineFunction &MF = DAG.getMachineFunction();
712 MachineFrameInfo *MFI = MF.getFrameInfo();
713
714 // Add DAG nodes to load the arguments... On entry to a function the stack
715 // frame looks like this:
716 //
717 // [ESP] -- return address
718 // [ESP + 4] -- first nonreg argument (leftmost lexically)
719 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
720 // ...
721 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
722
723 // Keep track of the number of integer regs passed so far. This can be either
724 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
725 // used).
726 unsigned NumIntRegs = 0;
727
728 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
729 MVT::ValueType ObjectVT = getValueType(I->getType());
730 unsigned ArgIncrement = 4;
731 unsigned ObjSize = 0;
732 SDOperand ArgValue;
733
734 switch (ObjectVT) {
735 default: assert(0 && "Unhandled argument type!");
736 case MVT::i1:
737 case MVT::i8:
738 if (NumIntRegs < 2) {
739 if (!I->use_empty()) {
740 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
741 X86::R8RegisterClass);
742 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
743 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000744 if (ObjectVT == MVT::i1)
745 // FIXME: Should insert a assertzext here.
746 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000747 }
748 ++NumIntRegs;
749 break;
750 }
751
752 ObjSize = 1;
753 break;
754 case MVT::i16:
755 if (NumIntRegs < 2) {
756 if (!I->use_empty()) {
757 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
758 X86::R16RegisterClass);
759 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
760 DAG.setRoot(ArgValue.getValue(1));
761 }
762 ++NumIntRegs;
763 break;
764 }
765 ObjSize = 2;
766 break;
767 case MVT::i32:
768 if (NumIntRegs < 2) {
769 if (!I->use_empty()) {
770 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
771 X86::R32RegisterClass);
772 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
773 DAG.setRoot(ArgValue.getValue(1));
774 }
775 ++NumIntRegs;
776 break;
777 }
778 ObjSize = 4;
779 break;
780 case MVT::i64:
781 if (NumIntRegs == 0) {
782 if (!I->use_empty()) {
783 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
784 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
785
786 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
787 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
788 DAG.setRoot(Hi.getValue(1));
789
790 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
791 }
792 NumIntRegs = 2;
793 break;
794 } else if (NumIntRegs == 1) {
795 if (!I->use_empty()) {
796 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
797 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
798 DAG.setRoot(Low.getValue(1));
799
800 // Load the high part from memory.
801 // Create the frame index object for this incoming parameter...
802 int FI = MFI->CreateFixedObject(4, ArgOffset);
803 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
804 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
805 DAG.getSrcValue(NULL));
806 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
807 }
808 ArgOffset += 4;
809 NumIntRegs = 2;
810 break;
811 }
812 ObjSize = ArgIncrement = 8;
813 break;
814 case MVT::f32: ObjSize = 4; break;
815 case MVT::f64: ObjSize = ArgIncrement = 8; break;
816 }
817
818 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
819 // dead loads.
820 if (ObjSize && !I->use_empty()) {
821 // Create the frame index object for this incoming parameter...
822 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
823
824 // Create the SelectionDAG nodes corresponding to a load from this
825 // parameter.
826 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
827
828 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
829 DAG.getSrcValue(NULL));
830 } else if (ArgValue.Val == 0) {
831 if (MVT::isInteger(ObjectVT))
832 ArgValue = DAG.getConstant(0, ObjectVT);
833 else
834 ArgValue = DAG.getConstantFP(0, ObjectVT);
835 }
836 ArgValues.push_back(ArgValue);
837
838 if (ObjSize)
839 ArgOffset += ArgIncrement; // Move on to the next argument.
840 }
841
842 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
843 // arguments and the arguments after the retaddr has been pushed are aligned.
844 if ((ArgOffset & 7) == 0)
845 ArgOffset += 4;
846
847 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
848 ReturnAddrIndex = 0; // No return address slot generated yet.
849 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
850 BytesCallerReserves = 0;
851
852 // Finally, inform the code generator which regs we return values in.
853 switch (getValueType(F.getReturnType())) {
854 default: assert(0 && "Unknown type!");
855 case MVT::isVoid: break;
856 case MVT::i1:
857 case MVT::i8:
858 case MVT::i16:
859 case MVT::i32:
860 MF.addLiveOut(X86::EAX);
861 break;
862 case MVT::i64:
863 MF.addLiveOut(X86::EAX);
864 MF.addLiveOut(X86::EDX);
865 break;
866 case MVT::f32:
867 case MVT::f64:
868 MF.addLiveOut(X86::ST0);
869 break;
870 }
871 return ArgValues;
872}
873
874std::pair<SDOperand, SDOperand>
875X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
876 bool isTailCall, SDOperand Callee,
877 ArgListTy &Args, SelectionDAG &DAG) {
878 // Count how many bytes are to be pushed on the stack.
879 unsigned NumBytes = 0;
880
881 // Keep track of the number of integer regs passed so far. This can be either
882 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
883 // used).
884 unsigned NumIntRegs = 0;
885
886 for (unsigned i = 0, e = Args.size(); i != e; ++i)
887 switch (getValueType(Args[i].second)) {
888 default: assert(0 && "Unknown value type!");
889 case MVT::i1:
890 case MVT::i8:
891 case MVT::i16:
892 case MVT::i32:
893 if (NumIntRegs < 2) {
894 ++NumIntRegs;
895 break;
896 }
897 // fall through
898 case MVT::f32:
899 NumBytes += 4;
900 break;
901 case MVT::i64:
902 if (NumIntRegs == 0) {
903 NumIntRegs = 2;
904 break;
905 } else if (NumIntRegs == 1) {
906 NumIntRegs = 2;
907 NumBytes += 4;
908 break;
909 }
910
911 // fall through
912 case MVT::f64:
913 NumBytes += 8;
914 break;
915 }
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 ((NumBytes & 7) == 0)
920 NumBytes += 4;
921
922 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
923 DAG.getConstant(NumBytes, getPointerTy()));
924
925 // Arguments go on the stack in reverse order, as specified by the ABI.
926 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000927 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000928 NumIntRegs = 0;
929 std::vector<SDOperand> Stores;
930 std::vector<SDOperand> RegValuesToPass;
931 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
932 switch (getValueType(Args[i].second)) {
933 default: assert(0 && "Unexpected ValueType for argument!");
934 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000935 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
936 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000937 case MVT::i8:
938 case MVT::i16:
939 case MVT::i32:
940 if (NumIntRegs < 2) {
941 RegValuesToPass.push_back(Args[i].first);
942 ++NumIntRegs;
943 break;
944 }
945 // Fall through
946 case MVT::f32: {
947 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
948 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
949 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
950 Args[i].first, PtrOff,
951 DAG.getSrcValue(NULL)));
952 ArgOffset += 4;
953 break;
954 }
955 case MVT::i64:
956 if (NumIntRegs < 2) { // Can pass part of it in regs?
957 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
958 Args[i].first, DAG.getConstant(1, MVT::i32));
959 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
960 Args[i].first, DAG.getConstant(0, MVT::i32));
961 RegValuesToPass.push_back(Lo);
962 ++NumIntRegs;
963 if (NumIntRegs < 2) { // Pass both parts in regs?
964 RegValuesToPass.push_back(Hi);
965 ++NumIntRegs;
966 } else {
967 // Pass the high part in memory.
968 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
969 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
970 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
971 Hi, PtrOff, DAG.getSrcValue(NULL)));
972 ArgOffset += 4;
973 }
974 break;
975 }
976 // Fall through
977 case MVT::f64:
978 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
979 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
980 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
981 Args[i].first, PtrOff,
982 DAG.getSrcValue(NULL)));
983 ArgOffset += 8;
984 break;
985 }
986 }
987 if (!Stores.empty())
988 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
989
990 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
991 // arguments and the arguments after the retaddr has been pushed are aligned.
992 if ((ArgOffset & 7) == 0)
993 ArgOffset += 4;
994
995 std::vector<MVT::ValueType> RetVals;
996 MVT::ValueType RetTyVT = getValueType(RetTy);
997
998 RetVals.push_back(MVT::Other);
999
1000 // The result values produced have to be legal. Promote the result.
1001 switch (RetTyVT) {
1002 case MVT::isVoid: break;
1003 default:
1004 RetVals.push_back(RetTyVT);
1005 break;
1006 case MVT::i1:
1007 case MVT::i8:
1008 case MVT::i16:
1009 RetVals.push_back(MVT::i32);
1010 break;
1011 case MVT::f32:
1012 if (X86ScalarSSE)
1013 RetVals.push_back(MVT::f32);
1014 else
1015 RetVals.push_back(MVT::f64);
1016 break;
1017 case MVT::i64:
1018 RetVals.push_back(MVT::i32);
1019 RetVals.push_back(MVT::i32);
1020 break;
1021 }
1022
Evan Cheng172fce72006-01-06 00:43:03 +00001023 if (X86DAGIsel) {
1024 // Build a sequence of copy-to-reg nodes chained together with token chain
1025 // and flag operands which copy the outgoing args into registers.
1026 SDOperand InFlag;
1027 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1028 unsigned CCReg;
1029 SDOperand RegToPass = RegValuesToPass[i];
1030 switch (RegToPass.getValueType()) {
1031 default: assert(0 && "Bad thing to pass in regs");
1032 case MVT::i8:
1033 CCReg = (i == 0) ? X86::AL : X86::DL;
1034 break;
1035 case MVT::i16:
1036 CCReg = (i == 0) ? X86::AX : X86::DX;
1037 break;
1038 case MVT::i32:
1039 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1040 break;
1041 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001042
Evan Cheng172fce72006-01-06 00:43:03 +00001043 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1044 InFlag = Chain.getValue(1);
1045 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001046
Evan Cheng172fce72006-01-06 00:43:03 +00001047 std::vector<MVT::ValueType> NodeTys;
1048 NodeTys.push_back(MVT::Other); // Returns a chain
1049 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +00001050 std::vector<SDOperand> Ops;
1051 Ops.push_back(Chain);
1052 Ops.push_back(Callee);
1053 if (InFlag.Val)
1054 Ops.push_back(InFlag);
1055
1056 // FIXME: Do not generate X86ISD::TAILCALL for now.
1057 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1058 InFlag = Chain.getValue(1);
1059
Chris Lattner6f33eae2006-01-24 05:17:12 +00001060 NodeTys.clear();
1061 NodeTys.push_back(MVT::Other); // Returns a chain
1062 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1063 Ops.clear();
1064 Ops.push_back(Chain);
1065 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1066 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1067 Ops.push_back(InFlag);
1068 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1069 InFlag = Chain.getValue(1);
1070
Evan Cheng172fce72006-01-06 00:43:03 +00001071 SDOperand RetVal;
1072 if (RetTyVT != MVT::isVoid) {
1073 switch (RetTyVT) {
1074 default: assert(0 && "Unknown value type to return!");
1075 case MVT::i1:
1076 case MVT::i8:
1077 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1078 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +00001079 if (RetTyVT == MVT::i1)
1080 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001081 break;
1082 case MVT::i16:
1083 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1084 Chain = RetVal.getValue(1);
1085 break;
1086 case MVT::i32:
1087 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1088 Chain = RetVal.getValue(1);
1089 break;
1090 case MVT::i64: {
1091 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1092 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1093 Lo.getValue(2));
1094 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1095 Chain = Hi.getValue(1);
1096 break;
1097 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001098 case MVT::f32:
Evan Cheng172fce72006-01-06 00:43:03 +00001099 case MVT::f64: {
1100 std::vector<MVT::ValueType> Tys;
1101 Tys.push_back(MVT::f64);
1102 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +00001103 Tys.push_back(MVT::Flag);
Evan Cheng172fce72006-01-06 00:43:03 +00001104 std::vector<SDOperand> Ops;
1105 Ops.push_back(Chain);
1106 Ops.push_back(InFlag);
1107 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +00001108 Chain = RetVal.getValue(1);
1109 InFlag = RetVal.getValue(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001110 if (X86ScalarSSE) {
Evan Cheng561881f2006-01-17 00:37:42 +00001111 // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
1112 // shouldn't be necessary except for RFP cannot be live across
1113 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng172fce72006-01-06 00:43:03 +00001114 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1115 MachineFunction &MF = DAG.getMachineFunction();
1116 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1117 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1118 Tys.clear();
1119 Tys.push_back(MVT::Other);
1120 Ops.clear();
1121 Ops.push_back(Chain);
1122 Ops.push_back(RetVal);
1123 Ops.push_back(StackSlot);
1124 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +00001125 Ops.push_back(InFlag);
Evan Cheng172fce72006-01-06 00:43:03 +00001126 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1127 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1128 DAG.getSrcValue(NULL));
1129 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +00001130 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001131
1132 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1133 // FIXME: we would really like to remember that this FP_ROUND
1134 // operation is okay to eliminate if we allow excess FP precision.
1135 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001136 break;
1137 }
1138 }
1139 }
1140
Evan Cheng172fce72006-01-06 00:43:03 +00001141 return std::make_pair(RetVal, Chain);
1142 } else {
1143 std::vector<SDOperand> Ops;
1144 Ops.push_back(Chain);
1145 Ops.push_back(Callee);
1146 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1147 // Callee pops all arg values on the stack.
1148 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1149
1150 // Pass register arguments as needed.
1151 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1152
1153 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1154 RetVals, Ops);
1155 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1156
1157 SDOperand ResultVal;
1158 switch (RetTyVT) {
1159 case MVT::isVoid: break;
1160 default:
1161 ResultVal = TheCall.getValue(1);
1162 break;
1163 case MVT::i1:
1164 case MVT::i8:
1165 case MVT::i16:
1166 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1167 break;
1168 case MVT::f32:
1169 // FIXME: we would really like to remember that this FP_ROUND operation is
1170 // okay to eliminate if we allow excess FP precision.
1171 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1172 break;
1173 case MVT::i64:
1174 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1175 TheCall.getValue(2));
1176 break;
1177 }
1178
1179 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001180 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001181}
1182
1183SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1184 if (ReturnAddrIndex == 0) {
1185 // Set up a frame object for the return address.
1186 MachineFunction &MF = DAG.getMachineFunction();
1187 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1188 }
1189
1190 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1191}
1192
1193
1194
1195std::pair<SDOperand, SDOperand> X86TargetLowering::
1196LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1197 SelectionDAG &DAG) {
1198 SDOperand Result;
1199 if (Depth) // Depths > 0 not supported yet!
1200 Result = DAG.getConstant(0, getPointerTy());
1201 else {
1202 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1203 if (!isFrameAddress)
1204 // Just load the return address
1205 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1206 DAG.getSrcValue(NULL));
1207 else
1208 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1209 DAG.getConstant(4, MVT::i32));
1210 }
1211 return std::make_pair(Result, Chain);
1212}
1213
Evan Cheng339edad2006-01-11 00:33:36 +00001214/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1215/// which corresponds to the condition code.
1216static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1217 switch (X86CC) {
1218 default: assert(0 && "Unknown X86 conditional code!");
1219 case X86ISD::COND_A: return X86::JA;
1220 case X86ISD::COND_AE: return X86::JAE;
1221 case X86ISD::COND_B: return X86::JB;
1222 case X86ISD::COND_BE: return X86::JBE;
1223 case X86ISD::COND_E: return X86::JE;
1224 case X86ISD::COND_G: return X86::JG;
1225 case X86ISD::COND_GE: return X86::JGE;
1226 case X86ISD::COND_L: return X86::JL;
1227 case X86ISD::COND_LE: return X86::JLE;
1228 case X86ISD::COND_NE: return X86::JNE;
1229 case X86ISD::COND_NO: return X86::JNO;
1230 case X86ISD::COND_NP: return X86::JNP;
1231 case X86ISD::COND_NS: return X86::JNS;
1232 case X86ISD::COND_O: return X86::JO;
1233 case X86ISD::COND_P: return X86::JP;
1234 case X86ISD::COND_S: return X86::JS;
1235 }
1236}
Chris Lattner76ac0682005-11-15 00:40:23 +00001237
Evan Cheng339edad2006-01-11 00:33:36 +00001238/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1239/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Cheng172fce72006-01-06 00:43:03 +00001240/// do a direct translation.
Evan Cheng339edad2006-01-11 00:33:36 +00001241static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Cheng172fce72006-01-06 00:43:03 +00001242 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1243 unsigned X86CC = X86ISD::COND_INVALID;
1244 if (!isFP) {
1245 switch (SetCCOpcode) {
1246 default: break;
1247 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1248 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1249 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1250 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1251 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1252 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1253 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1254 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1255 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1256 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1257 }
1258 } else {
1259 // On a floating point condition, the flags are set as follows:
1260 // ZF PF CF op
1261 // 0 | 0 | 0 | X > Y
1262 // 0 | 0 | 1 | X < Y
1263 // 1 | 0 | 0 | X == Y
1264 // 1 | 1 | 1 | unordered
1265 switch (SetCCOpcode) {
1266 default: break;
1267 case ISD::SETUEQ:
1268 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1269 case ISD::SETOGT:
1270 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1271 case ISD::SETOGE:
1272 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1273 case ISD::SETULT:
1274 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1275 case ISD::SETULE:
1276 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1277 case ISD::SETONE:
1278 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1279 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1280 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1281 }
1282 }
1283 return X86CC;
1284}
1285
Evan Cheng339edad2006-01-11 00:33:36 +00001286/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1287/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001288/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001289static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001290 switch (X86CC) {
1291 default:
1292 return false;
1293 case X86ISD::COND_B:
1294 case X86ISD::COND_BE:
1295 case X86ISD::COND_E:
1296 case X86ISD::COND_P:
1297 case X86ISD::COND_A:
1298 case X86ISD::COND_AE:
1299 case X86ISD::COND_NE:
1300 case X86ISD::COND_NP:
1301 return true;
1302 }
1303}
1304
Evan Cheng339edad2006-01-11 00:33:36 +00001305MachineBasicBlock *
1306X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1307 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001308 switch (MI->getOpcode()) {
1309 default: assert(false && "Unexpected instr type to insert");
1310 case X86::CMOV_FR32:
1311 case X86::CMOV_FR64: {
1312 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1313 // control-flow pattern. The incoming instruction knows the destination vreg
1314 // to set, the condition code register to branch on, the true/false values to
1315 // select between, and a branch opcode to use.
1316 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1317 ilist<MachineBasicBlock>::iterator It = BB;
1318 ++It;
1319
1320 // thisMBB:
1321 // ...
1322 // TrueVal = ...
1323 // cmpTY ccX, r1, r2
1324 // bCC copy1MBB
1325 // fallthrough --> copy0MBB
1326 MachineBasicBlock *thisMBB = BB;
1327 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1328 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1329 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1330 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1331 MachineFunction *F = BB->getParent();
1332 F->getBasicBlockList().insert(It, copy0MBB);
1333 F->getBasicBlockList().insert(It, sinkMBB);
1334 // Update machine-CFG edges
1335 BB->addSuccessor(copy0MBB);
1336 BB->addSuccessor(sinkMBB);
1337
1338 // copy0MBB:
1339 // %FalseValue = ...
1340 // # fallthrough to sinkMBB
1341 BB = copy0MBB;
1342
1343 // Update machine-CFG edges
1344 BB->addSuccessor(sinkMBB);
1345
1346 // sinkMBB:
1347 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1348 // ...
1349 BB = sinkMBB;
1350 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1351 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1352 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001353
Evan Cheng911c68d2006-01-16 21:21:29 +00001354 delete MI; // The pseudo instruction is gone now.
1355 return BB;
1356 }
Evan Cheng339edad2006-01-11 00:33:36 +00001357
Evan Cheng911c68d2006-01-16 21:21:29 +00001358 case X86::FP_TO_INT16_IN_MEM:
1359 case X86::FP_TO_INT32_IN_MEM:
1360 case X86::FP_TO_INT64_IN_MEM: {
1361 // Change the floating point control register to use "round towards zero"
1362 // mode when truncating to an integer value.
1363 MachineFunction *F = BB->getParent();
1364 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1365 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1366
1367 // Load the old value of the high byte of the control word...
1368 unsigned OldCW =
1369 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1370 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1371
1372 // Set the high part to be round to zero...
1373 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1374
1375 // Reload the modified control word now...
1376 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1377
1378 // Restore the memory image of control word to original value
1379 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1380
1381 // Get the X86 opcode to use.
1382 unsigned Opc;
1383 switch (MI->getOpcode()) {
1384 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1385 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1386 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1387 }
1388
1389 X86AddressMode AM;
1390 MachineOperand &Op = MI->getOperand(0);
1391 if (Op.isRegister()) {
1392 AM.BaseType = X86AddressMode::RegBase;
1393 AM.Base.Reg = Op.getReg();
1394 } else {
1395 AM.BaseType = X86AddressMode::FrameIndexBase;
1396 AM.Base.FrameIndex = Op.getFrameIndex();
1397 }
1398 Op = MI->getOperand(1);
1399 if (Op.isImmediate())
1400 AM.Scale = Op.getImmedValue();
1401 Op = MI->getOperand(2);
1402 if (Op.isImmediate())
1403 AM.IndexReg = Op.getImmedValue();
1404 Op = MI->getOperand(3);
1405 if (Op.isGlobalAddress()) {
1406 AM.GV = Op.getGlobal();
1407 } else {
1408 AM.Disp = Op.getImmedValue();
1409 }
1410 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1411
1412 // Reload the original control word now.
1413 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1414
1415 delete MI; // The pseudo instruction is gone now.
1416 return BB;
1417 }
1418 }
Evan Cheng339edad2006-01-11 00:33:36 +00001419}
1420
1421
1422//===----------------------------------------------------------------------===//
1423// X86 Custom Lowering Hooks
1424//===----------------------------------------------------------------------===//
1425
Chris Lattner76ac0682005-11-15 00:40:23 +00001426/// LowerOperation - Provide custom lowering hooks for some operations.
1427///
1428SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1429 switch (Op.getOpcode()) {
1430 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001431 case ISD::ADD_PARTS:
1432 case ISD::SUB_PARTS: {
1433 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1434 "Not an i64 add/sub!");
1435 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1436 std::vector<MVT::ValueType> Tys;
1437 Tys.push_back(MVT::i32);
1438 Tys.push_back(MVT::Flag);
1439 std::vector<SDOperand> Ops;
1440 Ops.push_back(Op.getOperand(0));
1441 Ops.push_back(Op.getOperand(2));
1442 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1443 Tys, Ops);
1444 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1445 Op.getOperand(1), Op.getOperand(3),
1446 Lo.getValue(1));
1447 Tys.clear();
1448 Tys.push_back(MVT::i32);
1449 Tys.push_back(MVT::i32);
1450 Ops.clear();
1451 Ops.push_back(Lo);
1452 Ops.push_back(Hi);
1453 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1454 }
1455 case ISD::SHL_PARTS:
1456 case ISD::SRA_PARTS:
1457 case ISD::SRL_PARTS: {
1458 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1459 "Not an i64 shift!");
1460 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1461 SDOperand ShOpLo = Op.getOperand(0);
1462 SDOperand ShOpHi = Op.getOperand(1);
1463 SDOperand ShAmt = Op.getOperand(2);
1464 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001465 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001466 : DAG.getConstant(0, MVT::i32);
1467
1468 SDOperand Tmp2, Tmp3;
1469 if (Op.getOpcode() == ISD::SHL_PARTS) {
1470 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1471 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1472 } else {
1473 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001474 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001475 }
1476
1477 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1478 ShAmt, DAG.getConstant(32, MVT::i8));
1479
1480 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001481 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001482
1483 std::vector<MVT::ValueType> Tys;
1484 Tys.push_back(MVT::i32);
1485 Tys.push_back(MVT::Flag);
1486 std::vector<SDOperand> Ops;
1487 if (Op.getOpcode() == ISD::SHL_PARTS) {
1488 Ops.push_back(Tmp2);
1489 Ops.push_back(Tmp3);
1490 Ops.push_back(CC);
1491 Ops.push_back(InFlag);
1492 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1493 InFlag = Hi.getValue(1);
1494
1495 Ops.clear();
1496 Ops.push_back(Tmp3);
1497 Ops.push_back(Tmp1);
1498 Ops.push_back(CC);
1499 Ops.push_back(InFlag);
1500 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1501 } else {
1502 Ops.push_back(Tmp2);
1503 Ops.push_back(Tmp3);
1504 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001505 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001506 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1507 InFlag = Lo.getValue(1);
1508
1509 Ops.clear();
1510 Ops.push_back(Tmp3);
1511 Ops.push_back(Tmp1);
1512 Ops.push_back(CC);
1513 Ops.push_back(InFlag);
1514 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1515 }
1516
1517 Tys.clear();
1518 Tys.push_back(MVT::i32);
1519 Tys.push_back(MVT::i32);
1520 Ops.clear();
1521 Ops.push_back(Lo);
1522 Ops.push_back(Hi);
1523 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1524 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001525 case ISD::SINT_TO_FP: {
1526 assert(Op.getValueType() == MVT::f64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001527 Op.getOperand(0).getValueType() <= MVT::i64 &&
1528 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001529 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001530
1531 SDOperand Result;
1532 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1533 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001534 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001535 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001536 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001537 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1538 DAG.getEntryNode(), Op.getOperand(0),
1539 StackSlot, DAG.getSrcValue(NULL));
1540
1541 // Build the FILD
1542 std::vector<MVT::ValueType> Tys;
1543 Tys.push_back(MVT::f64);
1544 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001545 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001546 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001547 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001548 Ops.push_back(DAG.getValueType(SrcVT));
1549 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
1550 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001551 }
1552 case ISD::FP_TO_SINT: {
1553 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1554 Op.getOperand(0).getValueType() == MVT::f64 &&
1555 "Unknown FP_TO_SINT to lower!");
1556 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1557 // stack slot.
1558 MachineFunction &MF = DAG.getMachineFunction();
1559 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1560 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1561 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1562
1563 unsigned Opc;
1564 switch (Op.getValueType()) {
1565 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1566 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1567 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1568 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1569 }
1570
1571 // Build the FP_TO_INT*_IN_MEM
1572 std::vector<SDOperand> Ops;
1573 Ops.push_back(DAG.getEntryNode());
1574 Ops.push_back(Op.getOperand(0));
1575 Ops.push_back(StackSlot);
1576 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1577
1578 // Load the result.
1579 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1580 DAG.getSrcValue(NULL));
1581 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001582 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001583 std::vector<MVT::ValueType> Tys;
1584 Tys.push_back(MVT::Other);
1585 Tys.push_back(MVT::Flag);
1586 std::vector<SDOperand> Ops;
1587 Ops.push_back(Op.getOperand(0));
1588 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001589 Ops.clear();
1590 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1591 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1592 MVT::i32, Ops[0].getValue(2)));
1593 Ops.push_back(Ops[1].getValue(1));
1594 Tys[0] = Tys[1] = MVT::i32;
1595 Tys.push_back(MVT::Other);
1596 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001597 }
Evan Chengc1583db2005-12-21 20:21:51 +00001598 case ISD::SETCC: {
1599 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1600 SDOperand CC = Op.getOperand(2);
1601 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1602 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001603 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1604 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001605 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001606 if (X86CC != X86ISD::COND_INVALID) {
1607 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1608 DAG.getConstant(X86CC, MVT::i8), Cond);
1609 } else {
1610 assert(isFP && "Illegal integer SetCC!");
1611
1612 std::vector<MVT::ValueType> Tys;
1613 std::vector<SDOperand> Ops;
1614 switch (SetCCOpcode) {
1615 default: assert(false && "Illegal floating point SetCC!");
1616 case ISD::SETOEQ: { // !PF & ZF
1617 Tys.push_back(MVT::i8);
1618 Tys.push_back(MVT::Flag);
1619 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1620 Ops.push_back(Cond);
1621 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1622 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1623 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1624 Tmp1.getValue(1));
1625 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1626 }
1627 case ISD::SETOLT: { // !PF & CF
1628 Tys.push_back(MVT::i8);
1629 Tys.push_back(MVT::Flag);
1630 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1631 Ops.push_back(Cond);
1632 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1633 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1634 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1635 Tmp1.getValue(1));
1636 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1637 }
1638 case ISD::SETOLE: { // !PF & (CF || ZF)
1639 Tys.push_back(MVT::i8);
1640 Tys.push_back(MVT::Flag);
1641 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1642 Ops.push_back(Cond);
1643 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1644 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1645 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1646 Tmp1.getValue(1));
1647 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1648 }
1649 case ISD::SETUGT: { // PF | (!ZF & !CF)
1650 Tys.push_back(MVT::i8);
1651 Tys.push_back(MVT::Flag);
1652 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1653 Ops.push_back(Cond);
1654 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1655 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1656 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1657 Tmp1.getValue(1));
1658 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1659 }
1660 case ISD::SETUGE: { // PF | !CF
1661 Tys.push_back(MVT::i8);
1662 Tys.push_back(MVT::Flag);
1663 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1664 Ops.push_back(Cond);
1665 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1666 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1667 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1668 Tmp1.getValue(1));
1669 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1670 }
1671 case ISD::SETUNE: { // PF | !ZF
1672 Tys.push_back(MVT::i8);
1673 Tys.push_back(MVT::Flag);
1674 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1675 Ops.push_back(Cond);
1676 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1677 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1678 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1679 Tmp1.getValue(1));
1680 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1681 }
1682 }
1683 }
Evan Chengc1583db2005-12-21 20:21:51 +00001684 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001685 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001686 MVT::ValueType VT = Op.getValueType();
1687 bool isFP = MVT::isFloatingPoint(VT);
1688 bool isFPStack = isFP && (X86Vector < SSE2);
1689 bool isFPSSE = isFP && (X86Vector >= SSE2);
Evan Chengfb22e862006-01-13 01:03:02 +00001690 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001691 SDOperand Op0 = Op.getOperand(0);
1692 SDOperand Cond, CC;
1693 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001694 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1695 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1696 // have another use it will be eliminated.
1697 // If the X86ISD::SETCC has more than one use, then it's probably better
1698 // to use a test instead of duplicating the X86ISD::CMP (for register
1699 // pressure reason).
Evan Chengaff08002006-01-25 09:05:09 +00001700 // FIXME: Check number of live Op0 uses since we are in the middle of
1701 // legalization process.
Evan Chengd7faa4b2006-01-13 01:17:24 +00001702 if (Op0.hasOneUse() && Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
Evan Chengfb22e862006-01-13 01:03:02 +00001703 CC = Op0.getOperand(0);
1704 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001705 // Make a copy as flag result cannot be used by more than one.
1706 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1707 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001708 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001709 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001710 } else
1711 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001712 } else if (Op0.getOpcode() == ISD::SETCC) {
1713 CC = Op0.getOperand(2);
1714 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001715 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001716 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001717 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001718 Op0.getOperand(0), Op0.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001719 } else
1720 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001721
Evan Cheng731423f2006-01-13 01:06:49 +00001722 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001723 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001724 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001725 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001726
1727 std::vector<MVT::ValueType> Tys;
1728 Tys.push_back(Op.getValueType());
1729 Tys.push_back(MVT::Flag);
1730 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001731 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1732 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001733 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001734 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001735 Ops.push_back(CC);
1736 Ops.push_back(Cond);
1737 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001738 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001739 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001740 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001741 SDOperand Cond = Op.getOperand(1);
1742 SDOperand Dest = Op.getOperand(2);
1743 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001744 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001745 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1746 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1747 // have another use it will be eliminated.
1748 // If the X86ISD::SETCC has more than one use, then it's probably better
1749 // to use a test instead of duplicating the X86ISD::CMP (for register
1750 // pressure reason).
Evan Chengaff08002006-01-25 09:05:09 +00001751 // FIXME: Check number of live Cond uses since we are in the middle of
1752 // legalization process.
Evan Chengfb22e862006-01-13 01:03:02 +00001753 if (Cond.hasOneUse() && Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1754 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001755 Cond = Cond.getOperand(1);
1756 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001757 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001758 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001759 } else
1760 addTest = true;
Evan Chengc1583db2005-12-21 20:21:51 +00001761 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001762 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001763 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001764 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001765 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001766 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1767 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001768 } else
1769 addTest = true;
1770
1771 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001772 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001773 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1774 }
1775 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1776 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1777 }
Evan Cheng172fce72006-01-06 00:43:03 +00001778 case ISD::RET: {
1779 // Can only be return void.
Evan Cheng9c249c32006-01-09 18:33:28 +00001780 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng172fce72006-01-06 00:43:03 +00001781 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1782 }
Evan Chengae986f12006-01-11 22:15:48 +00001783 case ISD::MEMSET: {
1784 SDOperand InFlag;
1785 SDOperand Chain = Op.getOperand(0);
1786 unsigned Align =
1787 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1788 if (Align == 0) Align = 1;
1789
1790 MVT::ValueType AVT;
1791 SDOperand Count;
1792 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1793 unsigned ValReg;
1794 unsigned Val = ValC->getValue() & 255;
1795
1796 // If the value is a constant, then we can potentially use larger sets.
1797 switch (Align & 3) {
1798 case 2: // WORD aligned
1799 AVT = MVT::i16;
1800 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1801 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1802 else
1803 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1804 DAG.getConstant(1, MVT::i8));
1805 Val = (Val << 8) | Val;
1806 ValReg = X86::AX;
1807 break;
1808 case 0: // DWORD aligned
1809 AVT = MVT::i32;
1810 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1811 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1812 else
1813 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1814 DAG.getConstant(2, MVT::i8));
1815 Val = (Val << 8) | Val;
1816 Val = (Val << 16) | Val;
1817 ValReg = X86::EAX;
1818 break;
1819 default: // Byte aligned
1820 AVT = MVT::i8;
1821 Count = Op.getOperand(3);
1822 ValReg = X86::AL;
1823 break;
1824 }
1825
1826 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1827 InFlag);
1828 InFlag = Chain.getValue(1);
1829 } else {
1830 AVT = MVT::i8;
1831 Count = Op.getOperand(3);
1832 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1833 InFlag = Chain.getValue(1);
1834 }
1835
1836 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1837 InFlag = Chain.getValue(1);
1838 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1839 InFlag = Chain.getValue(1);
1840
1841 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1842 DAG.getValueType(AVT), InFlag);
1843 }
1844 case ISD::MEMCPY: {
1845 SDOperand Chain = Op.getOperand(0);
1846 unsigned Align =
1847 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1848 if (Align == 0) Align = 1;
1849
1850 MVT::ValueType AVT;
1851 SDOperand Count;
1852 switch (Align & 3) {
1853 case 2: // WORD aligned
1854 AVT = MVT::i16;
1855 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1856 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1857 else
1858 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1859 DAG.getConstant(1, MVT::i8));
1860 break;
1861 case 0: // DWORD aligned
1862 AVT = MVT::i32;
1863 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1864 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1865 else
1866 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1867 DAG.getConstant(2, MVT::i8));
1868 break;
1869 default: // Byte aligned
1870 AVT = MVT::i8;
1871 Count = Op.getOperand(3);
1872 break;
1873 }
1874
1875 SDOperand InFlag;
1876 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1877 InFlag = Chain.getValue(1);
1878 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1879 InFlag = Chain.getValue(1);
1880 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1881 InFlag = Chain.getValue(1);
1882
1883 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1884 DAG.getValueType(AVT), InFlag);
1885 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001886 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001887 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001888 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1889 // For Darwin, external and weak symbols are indirect, so we want to load
1890 // the value at address GV, not the value of GV itself. This means that
1891 // the GlobalAddress must be in the base or index register of the address,
1892 // not the GV offset field.
1893 if (getTargetMachine().
1894 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1895 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001896 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1897 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1898 DAG.getSrcValue(NULL));
1899 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001900 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001901 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001902}
Evan Cheng6af02632005-12-20 06:22:03 +00001903
1904const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1905 switch (Opcode) {
1906 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001907 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1908 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1909 case X86ISD::ADC: return "X86ISD::ADC";
1910 case X86ISD::SBB: return "X86ISD::SBB";
1911 case X86ISD::SHLD: return "X86ISD::SHLD";
1912 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6305e502006-01-12 22:54:21 +00001913 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001914 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1915 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1916 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001917 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001918 case X86ISD::FST: return "X86ISD::FST";
1919 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001920 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001921 case X86ISD::CALL: return "X86ISD::CALL";
1922 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1923 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1924 case X86ISD::CMP: return "X86ISD::CMP";
1925 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001926 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001927 case X86ISD::CMOV: return "X86ISD::CMOV";
1928 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001929 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001930 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1931 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001932 }
1933}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001934
1935bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1936 uint64_t Mask) const {
1937
1938 unsigned Opc = Op.getOpcode();
1939
1940 switch (Opc) {
1941 default:
1942 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1943 break;
1944 case X86ISD::SETCC: return (Mask & 1) == 0;
1945 }
1946
1947 return false;
1948}