blob: bd53e61ab3568362c918b8f94f7851d213791ea3 [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);
42 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000043 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner76ac0682005-11-15 00:40:23 +000044
45 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000046 addRegisterClass(MVT::i8, X86::R8RegisterClass);
47 addRegisterClass(MVT::i16, X86::R16RegisterClass);
48 addRegisterClass(MVT::i32, X86::R32RegisterClass);
49
50 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
51 // operation.
52 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
53 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
54 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
55 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
56
57 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
58 // this operation.
59 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
61
62 if (!X86ScalarSSE) {
63 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
64 // isn't legal.
65 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
66 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
67 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
68 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
69 }
70
71 // Handle FP_TO_UINT by promoting the destination to a larger signed
72 // conversion.
73 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
74 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
75 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
76
77 if (!X86ScalarSSE)
78 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
79
80 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
81 // this operation.
82 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
83 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
85
Chris Lattner30107e62005-12-23 05:15:23 +000086 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
87 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
88
Evan Cheng6fc31042005-12-19 23:12:38 +000089 if (X86DAGIsel) {
90 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
91 }
Chris Lattner76ac0682005-11-15 00:40:23 +000092 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
93 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
94 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
95 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +000096 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +000097 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
98 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
99 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
100 setOperationAction(ISD::FREM , MVT::f64 , Expand);
101 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
103 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
104 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
105 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
106 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
107 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
108 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
109 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000110 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000111
Evan Cheng6d2ab042006-01-11 23:20:05 +0000112 if (!X86DAGIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000113 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000114 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
115 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
116 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
117 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
118 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
119 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
120 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000121 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000122
Chris Lattner76ac0682005-11-15 00:40:23 +0000123 setOperationAction(ISD::READIO , MVT::i1 , Expand);
124 setOperationAction(ISD::READIO , MVT::i8 , Expand);
125 setOperationAction(ISD::READIO , MVT::i16 , Expand);
126 setOperationAction(ISD::READIO , MVT::i32 , Expand);
127 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
128 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
129 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
130 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
131
132 // These should be promoted to a larger select which is supported.
133 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
134 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng225a4d02005-12-17 01:21:05 +0000135 if (X86DAGIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000136 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000137 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
138 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000139 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
140 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000141 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
142 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
143 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000144 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
145 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
146 // X86 ret instruction may pop stack.
147 setOperationAction(ISD::RET , MVT::Other, Custom);
148 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000149 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000150 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
151 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
152 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
153 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
154 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
155 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000156 // X86 wants to expand memset / memcpy itself.
157 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
158 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000159 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000160
Chris Lattner9c415362005-11-29 06:16:21 +0000161 // We don't have line number support yet.
162 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000163 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
164 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000165
Chris Lattner78c358d2006-01-15 09:00:21 +0000166 // Expand to the default code.
167 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
168 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
169 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000170
Chris Lattner76ac0682005-11-15 00:40:23 +0000171 if (X86ScalarSSE) {
172 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000173 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
174 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000175
176 // SSE has no load+extend ops
177 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
178 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
179
180 // SSE has no i16 to fp conversion, only i32
181 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
182 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
183
184 // Expand FP_TO_UINT into a select.
185 // FIXME: We would like to use a Custom expander here eventually to do
186 // the optimal thing for SSE vs. the default expansion in the legalizer.
187 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
188
189 // We don't support sin/cos/sqrt/fmod
190 setOperationAction(ISD::FSIN , MVT::f64, Expand);
191 setOperationAction(ISD::FCOS , MVT::f64, Expand);
192 setOperationAction(ISD::FABS , MVT::f64, Expand);
193 setOperationAction(ISD::FNEG , MVT::f64, Expand);
194 setOperationAction(ISD::FREM , MVT::f64, Expand);
195 setOperationAction(ISD::FSIN , MVT::f32, Expand);
196 setOperationAction(ISD::FCOS , MVT::f32, Expand);
197 setOperationAction(ISD::FABS , MVT::f32, Expand);
198 setOperationAction(ISD::FNEG , MVT::f32, Expand);
199 setOperationAction(ISD::FREM , MVT::f32, Expand);
200
201 addLegalFPImmediate(+0.0); // xorps / xorpd
202 } else {
203 // Set up the FP register classes.
204 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
205
Evan Cheng6305e502006-01-12 22:54:21 +0000206 if (X86DAGIsel) {
207 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
208 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
209 }
210
Chris Lattner76ac0682005-11-15 00:40:23 +0000211 if (!UnsafeFPMath) {
212 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
213 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
214 }
215
216 addLegalFPImmediate(+0.0); // FLD0
217 addLegalFPImmediate(+1.0); // FLD1
218 addLegalFPImmediate(-0.0); // FLD0/FCHS
219 addLegalFPImmediate(-1.0); // FLD1/FCHS
220 }
221 computeRegisterProperties();
222
223 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
224 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
225 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
226 allowUnalignedMemoryAccesses = true; // x86 supports it!
227}
228
229std::vector<SDOperand>
230X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
231 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
232 return LowerFastCCArguments(F, DAG);
233 return LowerCCCArguments(F, DAG);
234}
235
236std::pair<SDOperand, SDOperand>
237X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
238 bool isVarArg, unsigned CallingConv,
239 bool isTailCall,
240 SDOperand Callee, ArgListTy &Args,
241 SelectionDAG &DAG) {
242 assert((!isVarArg || CallingConv == CallingConv::C) &&
243 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000244
245 // If the callee is a GlobalAddress node (quite common, every direct call is)
246 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
247 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
248 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000249 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
250 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000251
Chris Lattner76ac0682005-11-15 00:40:23 +0000252 if (CallingConv == CallingConv::Fast && EnableFastCC)
253 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
254 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
255}
256
Evan Chenga74ce622005-12-21 02:39:21 +0000257SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
258 SelectionDAG &DAG) {
259 if (!X86DAGIsel)
260 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
261
262 SDOperand Copy;
263 MVT::ValueType OpVT = Op.getValueType();
264 switch (OpVT) {
265 default: assert(0 && "Unknown type to return!");
266 case MVT::i32:
267 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
268 break;
269 case MVT::i64: {
270 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
271 DAG.getConstant(1, MVT::i32));
272 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
273 DAG.getConstant(0, MVT::i32));
Evan Cheng172fce72006-01-06 00:43:03 +0000274 Copy = DAG.getCopyToReg(Chain, X86::EDX, Hi, SDOperand());
275 Copy = DAG.getCopyToReg(Copy, X86::EAX, Lo, Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000276 break;
277 }
278 case MVT::f32:
Evan Chenga74ce622005-12-21 02:39:21 +0000279 case MVT::f64:
280 if (!X86ScalarSSE) {
Evan Cheng9c249c32006-01-09 18:33:28 +0000281 if (OpVT == MVT::f32)
282 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chenga74ce622005-12-21 02:39:21 +0000283 std::vector<MVT::ValueType> Tys;
284 Tys.push_back(MVT::Other);
285 Tys.push_back(MVT::Flag);
286 std::vector<SDOperand> Ops;
287 Ops.push_back(Chain);
288 Ops.push_back(Op);
289 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
290 } else {
291 // Spill the value to memory and reload it into top of stack.
292 unsigned Size = MVT::getSizeInBits(OpVT)/8;
293 MachineFunction &MF = DAG.getMachineFunction();
294 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
295 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
296 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
297 StackSlot, DAG.getSrcValue(NULL));
298 std::vector<MVT::ValueType> Tys;
299 Tys.push_back(MVT::f64);
300 Tys.push_back(MVT::Other);
301 std::vector<SDOperand> Ops;
302 Ops.push_back(Chain);
303 Ops.push_back(StackSlot);
304 Ops.push_back(DAG.getValueType(OpVT));
305 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
306 Tys.clear();
307 Tys.push_back(MVT::Other);
308 Tys.push_back(MVT::Flag);
309 Ops.clear();
310 Ops.push_back(Copy.getValue(1));
311 Ops.push_back(Copy);
312 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
313 }
314 break;
315 }
Evan Chengc1583db2005-12-21 20:21:51 +0000316
317 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
318 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
319 Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000320}
321
Chris Lattner76ac0682005-11-15 00:40:23 +0000322//===----------------------------------------------------------------------===//
323// C Calling Convention implementation
324//===----------------------------------------------------------------------===//
325
326std::vector<SDOperand>
327X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
328 std::vector<SDOperand> ArgValues;
329
330 MachineFunction &MF = DAG.getMachineFunction();
331 MachineFrameInfo *MFI = MF.getFrameInfo();
332
333 // Add DAG nodes to load the arguments... On entry to a function on the X86,
334 // the stack frame looks like this:
335 //
336 // [ESP] -- return address
337 // [ESP + 4] -- first argument (leftmost lexically)
338 // [ESP + 8] -- second argument, if first argument is four bytes in size
339 // ...
340 //
341 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
342 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
343 MVT::ValueType ObjectVT = getValueType(I->getType());
344 unsigned ArgIncrement = 4;
345 unsigned ObjSize;
346 switch (ObjectVT) {
347 default: assert(0 && "Unhandled argument type!");
348 case MVT::i1:
349 case MVT::i8: ObjSize = 1; break;
350 case MVT::i16: ObjSize = 2; break;
351 case MVT::i32: ObjSize = 4; break;
352 case MVT::i64: ObjSize = ArgIncrement = 8; break;
353 case MVT::f32: ObjSize = 4; break;
354 case MVT::f64: ObjSize = ArgIncrement = 8; break;
355 }
356 // Create the frame index object for this incoming parameter...
357 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
358
359 // Create the SelectionDAG nodes corresponding to a load from this parameter
360 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
361
362 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
363 // dead loads.
364 SDOperand ArgValue;
365 if (!I->use_empty())
366 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
367 DAG.getSrcValue(NULL));
368 else {
369 if (MVT::isInteger(ObjectVT))
370 ArgValue = DAG.getConstant(0, ObjectVT);
371 else
372 ArgValue = DAG.getConstantFP(0, ObjectVT);
373 }
374 ArgValues.push_back(ArgValue);
375
376 ArgOffset += ArgIncrement; // Move on to the next argument...
377 }
378
379 // If the function takes variable number of arguments, make a frame index for
380 // the start of the first vararg value... for expansion of llvm.va_start.
381 if (F.isVarArg())
382 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
383 ReturnAddrIndex = 0; // No return address slot generated yet.
384 BytesToPopOnReturn = 0; // Callee pops nothing.
385 BytesCallerReserves = ArgOffset;
386
387 // Finally, inform the code generator which regs we return values in.
388 switch (getValueType(F.getReturnType())) {
389 default: assert(0 && "Unknown type!");
390 case MVT::isVoid: break;
391 case MVT::i1:
392 case MVT::i8:
393 case MVT::i16:
394 case MVT::i32:
395 MF.addLiveOut(X86::EAX);
396 break;
397 case MVT::i64:
398 MF.addLiveOut(X86::EAX);
399 MF.addLiveOut(X86::EDX);
400 break;
401 case MVT::f32:
402 case MVT::f64:
403 MF.addLiveOut(X86::ST0);
404 break;
405 }
406 return ArgValues;
407}
408
409std::pair<SDOperand, SDOperand>
410X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
411 bool isVarArg, bool isTailCall,
412 SDOperand Callee, ArgListTy &Args,
413 SelectionDAG &DAG) {
414 // Count how many bytes are to be pushed on the stack.
415 unsigned NumBytes = 0;
416
417 if (Args.empty()) {
418 // Save zero bytes.
419 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
420 DAG.getConstant(0, getPointerTy()));
421 } else {
422 for (unsigned i = 0, e = Args.size(); i != e; ++i)
423 switch (getValueType(Args[i].second)) {
424 default: assert(0 && "Unknown value type!");
425 case MVT::i1:
426 case MVT::i8:
427 case MVT::i16:
428 case MVT::i32:
429 case MVT::f32:
430 NumBytes += 4;
431 break;
432 case MVT::i64:
433 case MVT::f64:
434 NumBytes += 8;
435 break;
436 }
437
438 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
439 DAG.getConstant(NumBytes, getPointerTy()));
440
441 // Arguments go on the stack in reverse order, as specified by the ABI.
442 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000443 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000444 std::vector<SDOperand> Stores;
445
446 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
447 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
448 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
449
450 switch (getValueType(Args[i].second)) {
451 default: assert(0 && "Unexpected ValueType for argument!");
452 case MVT::i1:
453 case MVT::i8:
454 case MVT::i16:
455 // Promote the integer to 32 bits. If the input type is signed use a
456 // sign extend, otherwise use a zero extend.
457 if (Args[i].second->isSigned())
458 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
459 else
460 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
461
462 // FALL THROUGH
463 case MVT::i32:
464 case MVT::f32:
465 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
466 Args[i].first, PtrOff,
467 DAG.getSrcValue(NULL)));
468 ArgOffset += 4;
469 break;
470 case MVT::i64:
471 case MVT::f64:
472 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
473 Args[i].first, PtrOff,
474 DAG.getSrcValue(NULL)));
475 ArgOffset += 8;
476 break;
477 }
478 }
479 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
480 }
481
482 std::vector<MVT::ValueType> RetVals;
483 MVT::ValueType RetTyVT = getValueType(RetTy);
484 RetVals.push_back(MVT::Other);
485
486 // The result values produced have to be legal. Promote the result.
487 switch (RetTyVT) {
488 case MVT::isVoid: break;
489 default:
490 RetVals.push_back(RetTyVT);
491 break;
492 case MVT::i1:
493 case MVT::i8:
494 case MVT::i16:
495 RetVals.push_back(MVT::i32);
496 break;
497 case MVT::f32:
498 if (X86ScalarSSE)
499 RetVals.push_back(MVT::f32);
500 else
501 RetVals.push_back(MVT::f64);
502 break;
503 case MVT::i64:
504 RetVals.push_back(MVT::i32);
505 RetVals.push_back(MVT::i32);
506 break;
507 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000508
Evan Cheng45e190982006-01-05 00:27:02 +0000509 if (X86DAGIsel) {
510 std::vector<MVT::ValueType> NodeTys;
511 NodeTys.push_back(MVT::Other); // Returns a chain
512 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000513 std::vector<SDOperand> Ops;
514 Ops.push_back(Chain);
515 Ops.push_back(Callee);
516
Evan Cheng172fce72006-01-06 00:43:03 +0000517 // FIXME: Do not generate X86ISD::TAILCALL for now.
518 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000519 SDOperand InFlag = Chain.getValue(1);
520
521 SDOperand RetVal;
522 if (RetTyVT != MVT::isVoid) {
523 switch (RetTyVT) {
524 default: assert(0 && "Unknown value type to return!");
525 case MVT::i1:
526 case MVT::i8:
527 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
528 Chain = RetVal.getValue(1);
529 break;
530 case MVT::i16:
531 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
532 Chain = RetVal.getValue(1);
533 break;
534 case MVT::i32:
535 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
536 Chain = RetVal.getValue(1);
537 break;
538 case MVT::i64: {
539 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
540 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
541 Lo.getValue(2));
542 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
543 Chain = Hi.getValue(1);
544 break;
545 }
546 case MVT::f32:
547 case MVT::f64: {
548 std::vector<MVT::ValueType> Tys;
549 Tys.push_back(MVT::f64);
550 Tys.push_back(MVT::Other);
551 std::vector<SDOperand> Ops;
552 Ops.push_back(Chain);
553 Ops.push_back(InFlag);
554 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
555 Chain = RetVal.getValue(1);
556 if (X86ScalarSSE) {
557 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
558 MachineFunction &MF = DAG.getMachineFunction();
559 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
560 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
561 Tys.clear();
562 Tys.push_back(MVT::Other);
563 Ops.clear();
564 Ops.push_back(Chain);
565 Ops.push_back(RetVal);
566 Ops.push_back(StackSlot);
567 Ops.push_back(DAG.getValueType(RetTyVT));
568 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
569 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
570 DAG.getSrcValue(NULL));
571 Chain = RetVal.getValue(1);
572 } else if (RetTyVT == MVT::f32)
573 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
574 break;
575 }
576 }
577 }
578
579 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
580 DAG.getConstant(NumBytes, getPointerTy()),
581 DAG.getConstant(0, getPointerTy()));
582 return std::make_pair(RetVal, Chain);
583 } else {
584 std::vector<SDOperand> Ops;
585 Ops.push_back(Chain);
586 Ops.push_back(Callee);
587 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
588 Ops.push_back(DAG.getConstant(0, getPointerTy()));
589
590 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
591 RetVals, Ops);
592
593 SDOperand ResultVal;
594 switch (RetTyVT) {
595 case MVT::isVoid: break;
596 default:
597 ResultVal = TheCall.getValue(1);
598 break;
599 case MVT::i1:
600 case MVT::i8:
601 case MVT::i16:
602 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
603 break;
604 case MVT::f32:
605 // FIXME: we would really like to remember that this FP_ROUND operation is
606 // okay to eliminate if we allow excess FP precision.
607 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
608 break;
609 case MVT::i64:
610 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
611 TheCall.getValue(2));
612 break;
613 }
614
615 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
616 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000617 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000618}
619
620SDOperand
621X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
622 Value *VAListV, SelectionDAG &DAG) {
623 // vastart just stores the address of the VarArgsFrameIndex slot.
624 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
625 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
626 DAG.getSrcValue(VAListV));
627}
628
629
630std::pair<SDOperand,SDOperand>
631X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
632 Value *VAListV, const Type *ArgTy,
633 SelectionDAG &DAG) {
634 MVT::ValueType ArgVT = getValueType(ArgTy);
635 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
636 VAListP, DAG.getSrcValue(VAListV));
637 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
638 DAG.getSrcValue(NULL));
639 unsigned Amt;
640 if (ArgVT == MVT::i32)
641 Amt = 4;
642 else {
643 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
644 "Other types should have been promoted for varargs!");
645 Amt = 8;
646 }
647 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
648 DAG.getConstant(Amt, Val.getValueType()));
649 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
650 Val, VAListP, DAG.getSrcValue(VAListV));
651 return std::make_pair(Result, Chain);
652}
653
654//===----------------------------------------------------------------------===//
655// Fast Calling Convention implementation
656//===----------------------------------------------------------------------===//
657//
658// The X86 'fast' calling convention passes up to two integer arguments in
659// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
660// and requires that the callee pop its arguments off the stack (allowing proper
661// tail calls), and has the same return value conventions as C calling convs.
662//
663// This calling convention always arranges for the callee pop value to be 8n+4
664// bytes, which is needed for tail recursion elimination and stack alignment
665// reasons.
666//
667// Note that this can be enhanced in the future to pass fp vals in registers
668// (when we have a global fp allocator) and do other tricks.
669//
670
671/// AddLiveIn - This helper function adds the specified physical register to the
672/// MachineFunction as a live in value. It also creates a corresponding virtual
673/// register for it.
674static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
675 TargetRegisterClass *RC) {
676 assert(RC->contains(PReg) && "Not the correct regclass!");
677 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
678 MF.addLiveIn(PReg, VReg);
679 return VReg;
680}
681
682
683std::vector<SDOperand>
684X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
685 std::vector<SDOperand> ArgValues;
686
687 MachineFunction &MF = DAG.getMachineFunction();
688 MachineFrameInfo *MFI = MF.getFrameInfo();
689
690 // Add DAG nodes to load the arguments... On entry to a function the stack
691 // frame looks like this:
692 //
693 // [ESP] -- return address
694 // [ESP + 4] -- first nonreg argument (leftmost lexically)
695 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
696 // ...
697 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
698
699 // Keep track of the number of integer regs passed so far. This can be either
700 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
701 // used).
702 unsigned NumIntRegs = 0;
703
704 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
705 MVT::ValueType ObjectVT = getValueType(I->getType());
706 unsigned ArgIncrement = 4;
707 unsigned ObjSize = 0;
708 SDOperand ArgValue;
709
710 switch (ObjectVT) {
711 default: assert(0 && "Unhandled argument type!");
712 case MVT::i1:
713 case MVT::i8:
714 if (NumIntRegs < 2) {
715 if (!I->use_empty()) {
716 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
717 X86::R8RegisterClass);
718 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
719 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000720 if (ObjectVT == MVT::i1)
721 // FIXME: Should insert a assertzext here.
722 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000723 }
724 ++NumIntRegs;
725 break;
726 }
727
728 ObjSize = 1;
729 break;
730 case MVT::i16:
731 if (NumIntRegs < 2) {
732 if (!I->use_empty()) {
733 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
734 X86::R16RegisterClass);
735 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
736 DAG.setRoot(ArgValue.getValue(1));
737 }
738 ++NumIntRegs;
739 break;
740 }
741 ObjSize = 2;
742 break;
743 case MVT::i32:
744 if (NumIntRegs < 2) {
745 if (!I->use_empty()) {
746 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
747 X86::R32RegisterClass);
748 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
749 DAG.setRoot(ArgValue.getValue(1));
750 }
751 ++NumIntRegs;
752 break;
753 }
754 ObjSize = 4;
755 break;
756 case MVT::i64:
757 if (NumIntRegs == 0) {
758 if (!I->use_empty()) {
759 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
760 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
761
762 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
763 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
764 DAG.setRoot(Hi.getValue(1));
765
766 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
767 }
768 NumIntRegs = 2;
769 break;
770 } else if (NumIntRegs == 1) {
771 if (!I->use_empty()) {
772 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
773 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
774 DAG.setRoot(Low.getValue(1));
775
776 // Load the high part from memory.
777 // Create the frame index object for this incoming parameter...
778 int FI = MFI->CreateFixedObject(4, ArgOffset);
779 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
780 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
781 DAG.getSrcValue(NULL));
782 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
783 }
784 ArgOffset += 4;
785 NumIntRegs = 2;
786 break;
787 }
788 ObjSize = ArgIncrement = 8;
789 break;
790 case MVT::f32: ObjSize = 4; break;
791 case MVT::f64: ObjSize = ArgIncrement = 8; break;
792 }
793
794 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
795 // dead loads.
796 if (ObjSize && !I->use_empty()) {
797 // Create the frame index object for this incoming parameter...
798 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
799
800 // Create the SelectionDAG nodes corresponding to a load from this
801 // parameter.
802 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
803
804 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
805 DAG.getSrcValue(NULL));
806 } else if (ArgValue.Val == 0) {
807 if (MVT::isInteger(ObjectVT))
808 ArgValue = DAG.getConstant(0, ObjectVT);
809 else
810 ArgValue = DAG.getConstantFP(0, ObjectVT);
811 }
812 ArgValues.push_back(ArgValue);
813
814 if (ObjSize)
815 ArgOffset += ArgIncrement; // Move on to the next argument.
816 }
817
818 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
819 // arguments and the arguments after the retaddr has been pushed are aligned.
820 if ((ArgOffset & 7) == 0)
821 ArgOffset += 4;
822
823 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
824 ReturnAddrIndex = 0; // No return address slot generated yet.
825 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
826 BytesCallerReserves = 0;
827
828 // Finally, inform the code generator which regs we return values in.
829 switch (getValueType(F.getReturnType())) {
830 default: assert(0 && "Unknown type!");
831 case MVT::isVoid: break;
832 case MVT::i1:
833 case MVT::i8:
834 case MVT::i16:
835 case MVT::i32:
836 MF.addLiveOut(X86::EAX);
837 break;
838 case MVT::i64:
839 MF.addLiveOut(X86::EAX);
840 MF.addLiveOut(X86::EDX);
841 break;
842 case MVT::f32:
843 case MVT::f64:
844 MF.addLiveOut(X86::ST0);
845 break;
846 }
847 return ArgValues;
848}
849
850std::pair<SDOperand, SDOperand>
851X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
852 bool isTailCall, SDOperand Callee,
853 ArgListTy &Args, SelectionDAG &DAG) {
854 // Count how many bytes are to be pushed on the stack.
855 unsigned NumBytes = 0;
856
857 // Keep track of the number of integer regs passed so far. This can be either
858 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
859 // used).
860 unsigned NumIntRegs = 0;
861
862 for (unsigned i = 0, e = Args.size(); i != e; ++i)
863 switch (getValueType(Args[i].second)) {
864 default: assert(0 && "Unknown value type!");
865 case MVT::i1:
866 case MVT::i8:
867 case MVT::i16:
868 case MVT::i32:
869 if (NumIntRegs < 2) {
870 ++NumIntRegs;
871 break;
872 }
873 // fall through
874 case MVT::f32:
875 NumBytes += 4;
876 break;
877 case MVT::i64:
878 if (NumIntRegs == 0) {
879 NumIntRegs = 2;
880 break;
881 } else if (NumIntRegs == 1) {
882 NumIntRegs = 2;
883 NumBytes += 4;
884 break;
885 }
886
887 // fall through
888 case MVT::f64:
889 NumBytes += 8;
890 break;
891 }
892
893 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
894 // arguments and the arguments after the retaddr has been pushed are aligned.
895 if ((NumBytes & 7) == 0)
896 NumBytes += 4;
897
898 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
899 DAG.getConstant(NumBytes, getPointerTy()));
900
901 // Arguments go on the stack in reverse order, as specified by the ABI.
902 unsigned ArgOffset = 0;
903 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
904 X86::ESP, MVT::i32);
905 NumIntRegs = 0;
906 std::vector<SDOperand> Stores;
907 std::vector<SDOperand> RegValuesToPass;
908 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
909 switch (getValueType(Args[i].second)) {
910 default: assert(0 && "Unexpected ValueType for argument!");
911 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000912 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
913 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000914 case MVT::i8:
915 case MVT::i16:
916 case MVT::i32:
917 if (NumIntRegs < 2) {
918 RegValuesToPass.push_back(Args[i].first);
919 ++NumIntRegs;
920 break;
921 }
922 // Fall through
923 case MVT::f32: {
924 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
925 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
926 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
927 Args[i].first, PtrOff,
928 DAG.getSrcValue(NULL)));
929 ArgOffset += 4;
930 break;
931 }
932 case MVT::i64:
933 if (NumIntRegs < 2) { // Can pass part of it in regs?
934 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
935 Args[i].first, DAG.getConstant(1, MVT::i32));
936 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
937 Args[i].first, DAG.getConstant(0, MVT::i32));
938 RegValuesToPass.push_back(Lo);
939 ++NumIntRegs;
940 if (NumIntRegs < 2) { // Pass both parts in regs?
941 RegValuesToPass.push_back(Hi);
942 ++NumIntRegs;
943 } else {
944 // Pass the high part in memory.
945 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
946 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
947 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
948 Hi, PtrOff, DAG.getSrcValue(NULL)));
949 ArgOffset += 4;
950 }
951 break;
952 }
953 // Fall through
954 case MVT::f64:
955 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
956 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
957 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
958 Args[i].first, PtrOff,
959 DAG.getSrcValue(NULL)));
960 ArgOffset += 8;
961 break;
962 }
963 }
964 if (!Stores.empty())
965 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
966
967 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
968 // arguments and the arguments after the retaddr has been pushed are aligned.
969 if ((ArgOffset & 7) == 0)
970 ArgOffset += 4;
971
972 std::vector<MVT::ValueType> RetVals;
973 MVT::ValueType RetTyVT = getValueType(RetTy);
974
975 RetVals.push_back(MVT::Other);
976
977 // The result values produced have to be legal. Promote the result.
978 switch (RetTyVT) {
979 case MVT::isVoid: break;
980 default:
981 RetVals.push_back(RetTyVT);
982 break;
983 case MVT::i1:
984 case MVT::i8:
985 case MVT::i16:
986 RetVals.push_back(MVT::i32);
987 break;
988 case MVT::f32:
989 if (X86ScalarSSE)
990 RetVals.push_back(MVT::f32);
991 else
992 RetVals.push_back(MVT::f64);
993 break;
994 case MVT::i64:
995 RetVals.push_back(MVT::i32);
996 RetVals.push_back(MVT::i32);
997 break;
998 }
999
Evan Cheng172fce72006-01-06 00:43:03 +00001000 if (X86DAGIsel) {
1001 // Build a sequence of copy-to-reg nodes chained together with token chain
1002 // and flag operands which copy the outgoing args into registers.
1003 SDOperand InFlag;
1004 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1005 unsigned CCReg;
1006 SDOperand RegToPass = RegValuesToPass[i];
1007 switch (RegToPass.getValueType()) {
1008 default: assert(0 && "Bad thing to pass in regs");
1009 case MVT::i8:
1010 CCReg = (i == 0) ? X86::AL : X86::DL;
1011 break;
1012 case MVT::i16:
1013 CCReg = (i == 0) ? X86::AX : X86::DX;
1014 break;
1015 case MVT::i32:
1016 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1017 break;
1018 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001019
Evan Cheng172fce72006-01-06 00:43:03 +00001020 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1021 InFlag = Chain.getValue(1);
1022 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001023
Evan Cheng172fce72006-01-06 00:43:03 +00001024 std::vector<MVT::ValueType> NodeTys;
1025 NodeTys.push_back(MVT::Other); // Returns a chain
1026 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +00001027 std::vector<SDOperand> Ops;
1028 Ops.push_back(Chain);
1029 Ops.push_back(Callee);
1030 if (InFlag.Val)
1031 Ops.push_back(InFlag);
1032
1033 // FIXME: Do not generate X86ISD::TAILCALL for now.
1034 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1035 InFlag = Chain.getValue(1);
1036
1037 SDOperand RetVal;
1038 if (RetTyVT != MVT::isVoid) {
1039 switch (RetTyVT) {
1040 default: assert(0 && "Unknown value type to return!");
1041 case MVT::i1:
1042 case MVT::i8:
1043 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1044 Chain = RetVal.getValue(1);
1045 break;
1046 case MVT::i16:
1047 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1048 Chain = RetVal.getValue(1);
1049 break;
1050 case MVT::i32:
1051 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1052 Chain = RetVal.getValue(1);
1053 break;
1054 case MVT::i64: {
1055 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1056 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1057 Lo.getValue(2));
1058 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1059 Chain = Hi.getValue(1);
1060 break;
1061 }
1062 case MVT::f32:
1063 case MVT::f64: {
1064 std::vector<MVT::ValueType> Tys;
1065 Tys.push_back(MVT::f64);
1066 Tys.push_back(MVT::Other);
1067 std::vector<SDOperand> Ops;
1068 Ops.push_back(Chain);
1069 Ops.push_back(InFlag);
1070 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1071 Chain = RetVal.getValue(1);
1072 if (X86ScalarSSE) {
1073 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1074 MachineFunction &MF = DAG.getMachineFunction();
1075 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1076 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1077 Tys.clear();
1078 Tys.push_back(MVT::Other);
1079 Ops.clear();
1080 Ops.push_back(Chain);
1081 Ops.push_back(RetVal);
1082 Ops.push_back(StackSlot);
1083 Ops.push_back(DAG.getValueType(RetTyVT));
1084 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1085 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1086 DAG.getSrcValue(NULL));
1087 Chain = RetVal.getValue(1);
1088 } else if (RetTyVT == MVT::f32)
1089 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1090 break;
1091 }
1092 }
1093 }
1094
1095 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1096 DAG.getConstant(ArgOffset, getPointerTy()),
1097 DAG.getConstant(ArgOffset, getPointerTy()));
1098 return std::make_pair(RetVal, Chain);
1099 } else {
1100 std::vector<SDOperand> Ops;
1101 Ops.push_back(Chain);
1102 Ops.push_back(Callee);
1103 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1104 // Callee pops all arg values on the stack.
1105 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1106
1107 // Pass register arguments as needed.
1108 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1109
1110 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1111 RetVals, Ops);
1112 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1113
1114 SDOperand ResultVal;
1115 switch (RetTyVT) {
1116 case MVT::isVoid: break;
1117 default:
1118 ResultVal = TheCall.getValue(1);
1119 break;
1120 case MVT::i1:
1121 case MVT::i8:
1122 case MVT::i16:
1123 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1124 break;
1125 case MVT::f32:
1126 // FIXME: we would really like to remember that this FP_ROUND operation is
1127 // okay to eliminate if we allow excess FP precision.
1128 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1129 break;
1130 case MVT::i64:
1131 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1132 TheCall.getValue(2));
1133 break;
1134 }
1135
1136 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001137 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001138}
1139
1140SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1141 if (ReturnAddrIndex == 0) {
1142 // Set up a frame object for the return address.
1143 MachineFunction &MF = DAG.getMachineFunction();
1144 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1145 }
1146
1147 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1148}
1149
1150
1151
1152std::pair<SDOperand, SDOperand> X86TargetLowering::
1153LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1154 SelectionDAG &DAG) {
1155 SDOperand Result;
1156 if (Depth) // Depths > 0 not supported yet!
1157 Result = DAG.getConstant(0, getPointerTy());
1158 else {
1159 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1160 if (!isFrameAddress)
1161 // Just load the return address
1162 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1163 DAG.getSrcValue(NULL));
1164 else
1165 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1166 DAG.getConstant(4, MVT::i32));
1167 }
1168 return std::make_pair(Result, Chain);
1169}
1170
Evan Cheng339edad2006-01-11 00:33:36 +00001171/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1172/// which corresponds to the condition code.
1173static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1174 switch (X86CC) {
1175 default: assert(0 && "Unknown X86 conditional code!");
1176 case X86ISD::COND_A: return X86::JA;
1177 case X86ISD::COND_AE: return X86::JAE;
1178 case X86ISD::COND_B: return X86::JB;
1179 case X86ISD::COND_BE: return X86::JBE;
1180 case X86ISD::COND_E: return X86::JE;
1181 case X86ISD::COND_G: return X86::JG;
1182 case X86ISD::COND_GE: return X86::JGE;
1183 case X86ISD::COND_L: return X86::JL;
1184 case X86ISD::COND_LE: return X86::JLE;
1185 case X86ISD::COND_NE: return X86::JNE;
1186 case X86ISD::COND_NO: return X86::JNO;
1187 case X86ISD::COND_NP: return X86::JNP;
1188 case X86ISD::COND_NS: return X86::JNS;
1189 case X86ISD::COND_O: return X86::JO;
1190 case X86ISD::COND_P: return X86::JP;
1191 case X86ISD::COND_S: return X86::JS;
1192 }
1193}
Chris Lattner76ac0682005-11-15 00:40:23 +00001194
Evan Cheng339edad2006-01-11 00:33:36 +00001195/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1196/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Cheng172fce72006-01-06 00:43:03 +00001197/// do a direct translation.
Evan Cheng339edad2006-01-11 00:33:36 +00001198static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Cheng172fce72006-01-06 00:43:03 +00001199 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1200 unsigned X86CC = X86ISD::COND_INVALID;
1201 if (!isFP) {
1202 switch (SetCCOpcode) {
1203 default: break;
1204 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1205 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1206 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1207 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1208 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1209 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1210 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1211 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1212 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1213 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1214 }
1215 } else {
1216 // On a floating point condition, the flags are set as follows:
1217 // ZF PF CF op
1218 // 0 | 0 | 0 | X > Y
1219 // 0 | 0 | 1 | X < Y
1220 // 1 | 0 | 0 | X == Y
1221 // 1 | 1 | 1 | unordered
1222 switch (SetCCOpcode) {
1223 default: break;
1224 case ISD::SETUEQ:
1225 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1226 case ISD::SETOGT:
1227 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1228 case ISD::SETOGE:
1229 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1230 case ISD::SETULT:
1231 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1232 case ISD::SETULE:
1233 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1234 case ISD::SETONE:
1235 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1236 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1237 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1238 }
1239 }
1240 return X86CC;
1241}
1242
Evan Cheng339edad2006-01-11 00:33:36 +00001243/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1244/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001245/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001246static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001247 switch (X86CC) {
1248 default:
1249 return false;
1250 case X86ISD::COND_B:
1251 case X86ISD::COND_BE:
1252 case X86ISD::COND_E:
1253 case X86ISD::COND_P:
1254 case X86ISD::COND_A:
1255 case X86ISD::COND_AE:
1256 case X86ISD::COND_NE:
1257 case X86ISD::COND_NP:
1258 return true;
1259 }
1260}
1261
Evan Cheng339edad2006-01-11 00:33:36 +00001262MachineBasicBlock *
1263X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1264 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001265 switch (MI->getOpcode()) {
1266 default: assert(false && "Unexpected instr type to insert");
1267 case X86::CMOV_FR32:
1268 case X86::CMOV_FR64: {
1269 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1270 // control-flow pattern. The incoming instruction knows the destination vreg
1271 // to set, the condition code register to branch on, the true/false values to
1272 // select between, and a branch opcode to use.
1273 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1274 ilist<MachineBasicBlock>::iterator It = BB;
1275 ++It;
1276
1277 // thisMBB:
1278 // ...
1279 // TrueVal = ...
1280 // cmpTY ccX, r1, r2
1281 // bCC copy1MBB
1282 // fallthrough --> copy0MBB
1283 MachineBasicBlock *thisMBB = BB;
1284 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1285 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1286 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1287 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1288 MachineFunction *F = BB->getParent();
1289 F->getBasicBlockList().insert(It, copy0MBB);
1290 F->getBasicBlockList().insert(It, sinkMBB);
1291 // Update machine-CFG edges
1292 BB->addSuccessor(copy0MBB);
1293 BB->addSuccessor(sinkMBB);
1294
1295 // copy0MBB:
1296 // %FalseValue = ...
1297 // # fallthrough to sinkMBB
1298 BB = copy0MBB;
1299
1300 // Update machine-CFG edges
1301 BB->addSuccessor(sinkMBB);
1302
1303 // sinkMBB:
1304 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1305 // ...
1306 BB = sinkMBB;
1307 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1308 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1309 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001310
Evan Cheng911c68d2006-01-16 21:21:29 +00001311 delete MI; // The pseudo instruction is gone now.
1312 return BB;
1313 }
Evan Cheng339edad2006-01-11 00:33:36 +00001314
Evan Cheng911c68d2006-01-16 21:21:29 +00001315 case X86::FP_TO_INT16_IN_MEM:
1316 case X86::FP_TO_INT32_IN_MEM:
1317 case X86::FP_TO_INT64_IN_MEM: {
1318 // Change the floating point control register to use "round towards zero"
1319 // mode when truncating to an integer value.
1320 MachineFunction *F = BB->getParent();
1321 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1322 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1323
1324 // Load the old value of the high byte of the control word...
1325 unsigned OldCW =
1326 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1327 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1328
1329 // Set the high part to be round to zero...
1330 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1331
1332 // Reload the modified control word now...
1333 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1334
1335 // Restore the memory image of control word to original value
1336 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1337
1338 // Get the X86 opcode to use.
1339 unsigned Opc;
1340 switch (MI->getOpcode()) {
1341 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1342 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1343 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1344 }
1345
1346 X86AddressMode AM;
1347 MachineOperand &Op = MI->getOperand(0);
1348 if (Op.isRegister()) {
1349 AM.BaseType = X86AddressMode::RegBase;
1350 AM.Base.Reg = Op.getReg();
1351 } else {
1352 AM.BaseType = X86AddressMode::FrameIndexBase;
1353 AM.Base.FrameIndex = Op.getFrameIndex();
1354 }
1355 Op = MI->getOperand(1);
1356 if (Op.isImmediate())
1357 AM.Scale = Op.getImmedValue();
1358 Op = MI->getOperand(2);
1359 if (Op.isImmediate())
1360 AM.IndexReg = Op.getImmedValue();
1361 Op = MI->getOperand(3);
1362 if (Op.isGlobalAddress()) {
1363 AM.GV = Op.getGlobal();
1364 } else {
1365 AM.Disp = Op.getImmedValue();
1366 }
1367 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1368
1369 // Reload the original control word now.
1370 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1371
1372 delete MI; // The pseudo instruction is gone now.
1373 return BB;
1374 }
1375 }
Evan Cheng339edad2006-01-11 00:33:36 +00001376}
1377
1378
1379//===----------------------------------------------------------------------===//
1380// X86 Custom Lowering Hooks
1381//===----------------------------------------------------------------------===//
1382
Chris Lattner76ac0682005-11-15 00:40:23 +00001383/// LowerOperation - Provide custom lowering hooks for some operations.
1384///
1385SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1386 switch (Op.getOpcode()) {
1387 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001388 case ISD::ADD_PARTS:
1389 case ISD::SUB_PARTS: {
1390 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1391 "Not an i64 add/sub!");
1392 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1393 std::vector<MVT::ValueType> Tys;
1394 Tys.push_back(MVT::i32);
1395 Tys.push_back(MVT::Flag);
1396 std::vector<SDOperand> Ops;
1397 Ops.push_back(Op.getOperand(0));
1398 Ops.push_back(Op.getOperand(2));
1399 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1400 Tys, Ops);
1401 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1402 Op.getOperand(1), Op.getOperand(3),
1403 Lo.getValue(1));
1404 Tys.clear();
1405 Tys.push_back(MVT::i32);
1406 Tys.push_back(MVT::i32);
1407 Ops.clear();
1408 Ops.push_back(Lo);
1409 Ops.push_back(Hi);
1410 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1411 }
1412 case ISD::SHL_PARTS:
1413 case ISD::SRA_PARTS:
1414 case ISD::SRL_PARTS: {
1415 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1416 "Not an i64 shift!");
1417 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1418 SDOperand ShOpLo = Op.getOperand(0);
1419 SDOperand ShOpHi = Op.getOperand(1);
1420 SDOperand ShAmt = Op.getOperand(2);
1421 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng12181af2006-01-09 22:29:54 +00001422 DAG.getConstant(31, MVT::i32))
Evan Cheng9c249c32006-01-09 18:33:28 +00001423 : DAG.getConstant(0, MVT::i32);
1424
1425 SDOperand Tmp2, Tmp3;
1426 if (Op.getOpcode() == ISD::SHL_PARTS) {
1427 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1428 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1429 } else {
1430 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1431 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SHL, MVT::i32, ShOpHi, ShAmt);
1432 }
1433
1434 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1435 ShAmt, DAG.getConstant(32, MVT::i8));
1436
1437 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001438 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001439
1440 std::vector<MVT::ValueType> Tys;
1441 Tys.push_back(MVT::i32);
1442 Tys.push_back(MVT::Flag);
1443 std::vector<SDOperand> Ops;
1444 if (Op.getOpcode() == ISD::SHL_PARTS) {
1445 Ops.push_back(Tmp2);
1446 Ops.push_back(Tmp3);
1447 Ops.push_back(CC);
1448 Ops.push_back(InFlag);
1449 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1450 InFlag = Hi.getValue(1);
1451
1452 Ops.clear();
1453 Ops.push_back(Tmp3);
1454 Ops.push_back(Tmp1);
1455 Ops.push_back(CC);
1456 Ops.push_back(InFlag);
1457 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1458 } else {
1459 Ops.push_back(Tmp2);
1460 Ops.push_back(Tmp3);
1461 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001462 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001463 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1464 InFlag = Lo.getValue(1);
1465
1466 Ops.clear();
1467 Ops.push_back(Tmp3);
1468 Ops.push_back(Tmp1);
1469 Ops.push_back(CC);
1470 Ops.push_back(InFlag);
1471 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1472 }
1473
1474 Tys.clear();
1475 Tys.push_back(MVT::i32);
1476 Tys.push_back(MVT::i32);
1477 Ops.clear();
1478 Ops.push_back(Lo);
1479 Ops.push_back(Hi);
1480 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1481 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001482 case ISD::SINT_TO_FP: {
1483 assert(Op.getValueType() == MVT::f64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001484 Op.getOperand(0).getValueType() <= MVT::i64 &&
1485 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001486 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001487
1488 SDOperand Result;
1489 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1490 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001491 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001492 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001493 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001494 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1495 DAG.getEntryNode(), Op.getOperand(0),
1496 StackSlot, DAG.getSrcValue(NULL));
1497
1498 // Build the FILD
1499 std::vector<MVT::ValueType> Tys;
1500 Tys.push_back(MVT::f64);
1501 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001502 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001503 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001504 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001505 Ops.push_back(DAG.getValueType(SrcVT));
1506 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
1507 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001508 }
1509 case ISD::FP_TO_SINT: {
1510 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1511 Op.getOperand(0).getValueType() == MVT::f64 &&
1512 "Unknown FP_TO_SINT to lower!");
1513 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1514 // stack slot.
1515 MachineFunction &MF = DAG.getMachineFunction();
1516 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1517 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1518 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1519
1520 unsigned Opc;
1521 switch (Op.getValueType()) {
1522 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1523 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1524 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1525 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1526 }
1527
1528 // Build the FP_TO_INT*_IN_MEM
1529 std::vector<SDOperand> Ops;
1530 Ops.push_back(DAG.getEntryNode());
1531 Ops.push_back(Op.getOperand(0));
1532 Ops.push_back(StackSlot);
1533 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1534
1535 // Load the result.
1536 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1537 DAG.getSrcValue(NULL));
1538 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001539 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001540 std::vector<MVT::ValueType> Tys;
1541 Tys.push_back(MVT::Other);
1542 Tys.push_back(MVT::Flag);
1543 std::vector<SDOperand> Ops;
1544 Ops.push_back(Op.getOperand(0));
1545 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001546 Ops.clear();
1547 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1548 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1549 MVT::i32, Ops[0].getValue(2)));
1550 Ops.push_back(Ops[1].getValue(1));
1551 Tys[0] = Tys[1] = MVT::i32;
1552 Tys.push_back(MVT::Other);
1553 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001554 }
Evan Chengc1583db2005-12-21 20:21:51 +00001555 case ISD::SETCC: {
1556 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1557 SDOperand CC = Op.getOperand(2);
1558 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1559 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001560 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1561 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001562 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001563 if (X86CC != X86ISD::COND_INVALID) {
1564 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1565 DAG.getConstant(X86CC, MVT::i8), Cond);
1566 } else {
1567 assert(isFP && "Illegal integer SetCC!");
1568
1569 std::vector<MVT::ValueType> Tys;
1570 std::vector<SDOperand> Ops;
1571 switch (SetCCOpcode) {
1572 default: assert(false && "Illegal floating point SetCC!");
1573 case ISD::SETOEQ: { // !PF & ZF
1574 Tys.push_back(MVT::i8);
1575 Tys.push_back(MVT::Flag);
1576 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1577 Ops.push_back(Cond);
1578 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1579 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1580 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1581 Tmp1.getValue(1));
1582 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1583 }
1584 case ISD::SETOLT: { // !PF & CF
1585 Tys.push_back(MVT::i8);
1586 Tys.push_back(MVT::Flag);
1587 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1588 Ops.push_back(Cond);
1589 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1590 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1591 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1592 Tmp1.getValue(1));
1593 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1594 }
1595 case ISD::SETOLE: { // !PF & (CF || ZF)
1596 Tys.push_back(MVT::i8);
1597 Tys.push_back(MVT::Flag);
1598 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1599 Ops.push_back(Cond);
1600 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1601 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1602 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1603 Tmp1.getValue(1));
1604 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1605 }
1606 case ISD::SETUGT: { // PF | (!ZF & !CF)
1607 Tys.push_back(MVT::i8);
1608 Tys.push_back(MVT::Flag);
1609 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1610 Ops.push_back(Cond);
1611 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1612 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1613 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1614 Tmp1.getValue(1));
1615 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1616 }
1617 case ISD::SETUGE: { // PF | !CF
1618 Tys.push_back(MVT::i8);
1619 Tys.push_back(MVT::Flag);
1620 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1621 Ops.push_back(Cond);
1622 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1623 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1624 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1625 Tmp1.getValue(1));
1626 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1627 }
1628 case ISD::SETUNE: { // PF | !ZF
1629 Tys.push_back(MVT::i8);
1630 Tys.push_back(MVT::Flag);
1631 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1632 Ops.push_back(Cond);
1633 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1634 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1635 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1636 Tmp1.getValue(1));
1637 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1638 }
1639 }
1640 }
Evan Chengc1583db2005-12-21 20:21:51 +00001641 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001642 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001643 MVT::ValueType VT = Op.getValueType();
1644 bool isFP = MVT::isFloatingPoint(VT);
1645 bool isFPStack = isFP && (X86Vector < SSE2);
1646 bool isFPSSE = isFP && (X86Vector >= SSE2);
Evan Chengfb22e862006-01-13 01:03:02 +00001647 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001648 SDOperand Op0 = Op.getOperand(0);
1649 SDOperand Cond, CC;
1650 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001651 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1652 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1653 // have another use it will be eliminated.
1654 // If the X86ISD::SETCC has more than one use, then it's probably better
1655 // to use a test instead of duplicating the X86ISD::CMP (for register
1656 // pressure reason).
Evan Chengd7faa4b2006-01-13 01:17:24 +00001657 if (Op0.hasOneUse() && Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
Evan Chengfb22e862006-01-13 01:03:02 +00001658 CC = Op0.getOperand(0);
1659 Cond = Op0.getOperand(1);
1660 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001661 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001662 } else
1663 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001664 } else if (Op0.getOpcode() == ISD::SETCC) {
1665 CC = Op0.getOperand(2);
1666 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001667 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001668 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001669 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001670 Op0.getOperand(0), Op0.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001671 addTest = true;
1672 } else
1673 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001674
Evan Cheng731423f2006-01-13 01:06:49 +00001675 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001676 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001677 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001678 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001679
1680 std::vector<MVT::ValueType> Tys;
1681 Tys.push_back(Op.getValueType());
1682 Tys.push_back(MVT::Flag);
1683 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001684 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1685 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001686 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001687 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001688 Ops.push_back(CC);
1689 Ops.push_back(Cond);
1690 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001691 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001692 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001693 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001694 SDOperand Cond = Op.getOperand(1);
1695 SDOperand Dest = Op.getOperand(2);
1696 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001697 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001698 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1699 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1700 // have another use it will be eliminated.
1701 // If the X86ISD::SETCC has more than one use, then it's probably better
1702 // to use a test instead of duplicating the X86ISD::CMP (for register
1703 // pressure reason).
1704 if (Cond.hasOneUse() && Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1705 CC = Cond.getOperand(0);
1706 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1707 Cond.getOperand(1).getOperand(0),
1708 Cond.getOperand(1).getOperand(1));
1709 } else
1710 addTest = true;
Evan Chengc1583db2005-12-21 20:21:51 +00001711 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001712 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001713 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001714 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001715 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001716 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1717 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001718 } else
1719 addTest = true;
1720
1721 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001722 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001723 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1724 }
1725 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1726 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1727 }
Evan Cheng172fce72006-01-06 00:43:03 +00001728 case ISD::RET: {
1729 // Can only be return void.
Evan Cheng9c249c32006-01-09 18:33:28 +00001730 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng172fce72006-01-06 00:43:03 +00001731 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1732 }
Evan Chengae986f12006-01-11 22:15:48 +00001733 case ISD::MEMSET: {
1734 SDOperand InFlag;
1735 SDOperand Chain = Op.getOperand(0);
1736 unsigned Align =
1737 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1738 if (Align == 0) Align = 1;
1739
1740 MVT::ValueType AVT;
1741 SDOperand Count;
1742 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1743 unsigned ValReg;
1744 unsigned Val = ValC->getValue() & 255;
1745
1746 // If the value is a constant, then we can potentially use larger sets.
1747 switch (Align & 3) {
1748 case 2: // WORD aligned
1749 AVT = MVT::i16;
1750 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1751 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1752 else
1753 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1754 DAG.getConstant(1, MVT::i8));
1755 Val = (Val << 8) | Val;
1756 ValReg = X86::AX;
1757 break;
1758 case 0: // DWORD aligned
1759 AVT = MVT::i32;
1760 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1761 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1762 else
1763 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1764 DAG.getConstant(2, MVT::i8));
1765 Val = (Val << 8) | Val;
1766 Val = (Val << 16) | Val;
1767 ValReg = X86::EAX;
1768 break;
1769 default: // Byte aligned
1770 AVT = MVT::i8;
1771 Count = Op.getOperand(3);
1772 ValReg = X86::AL;
1773 break;
1774 }
1775
1776 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1777 InFlag);
1778 InFlag = Chain.getValue(1);
1779 } else {
1780 AVT = MVT::i8;
1781 Count = Op.getOperand(3);
1782 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1783 InFlag = Chain.getValue(1);
1784 }
1785
1786 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1787 InFlag = Chain.getValue(1);
1788 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1789 InFlag = Chain.getValue(1);
1790
1791 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1792 DAG.getValueType(AVT), InFlag);
1793 }
1794 case ISD::MEMCPY: {
1795 SDOperand Chain = Op.getOperand(0);
1796 unsigned Align =
1797 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1798 if (Align == 0) Align = 1;
1799
1800 MVT::ValueType AVT;
1801 SDOperand Count;
1802 switch (Align & 3) {
1803 case 2: // WORD aligned
1804 AVT = MVT::i16;
1805 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1806 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1807 else
1808 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1809 DAG.getConstant(1, MVT::i8));
1810 break;
1811 case 0: // DWORD aligned
1812 AVT = MVT::i32;
1813 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1814 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1815 else
1816 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1817 DAG.getConstant(2, MVT::i8));
1818 break;
1819 default: // Byte aligned
1820 AVT = MVT::i8;
1821 Count = Op.getOperand(3);
1822 break;
1823 }
1824
1825 SDOperand InFlag;
1826 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1827 InFlag = Chain.getValue(1);
1828 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1829 InFlag = Chain.getValue(1);
1830 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1831 InFlag = Chain.getValue(1);
1832
1833 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1834 DAG.getValueType(AVT), InFlag);
1835 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001836 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001837 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001838 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1839 // For Darwin, external and weak symbols are indirect, so we want to load
1840 // the value at address GV, not the value of GV itself. This means that
1841 // the GlobalAddress must be in the base or index register of the address,
1842 // not the GV offset field.
1843 if (getTargetMachine().
1844 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1845 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001846 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1847 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1848 DAG.getSrcValue(NULL));
1849 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001850 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001851 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001852}
Evan Cheng6af02632005-12-20 06:22:03 +00001853
1854const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1855 switch (Opcode) {
1856 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001857 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1858 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1859 case X86ISD::ADC: return "X86ISD::ADC";
1860 case X86ISD::SBB: return "X86ISD::SBB";
1861 case X86ISD::SHLD: return "X86ISD::SHLD";
1862 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6305e502006-01-12 22:54:21 +00001863 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001864 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1865 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1866 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001867 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001868 case X86ISD::FST: return "X86ISD::FST";
1869 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001870 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001871 case X86ISD::CALL: return "X86ISD::CALL";
1872 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1873 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1874 case X86ISD::CMP: return "X86ISD::CMP";
1875 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001876 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001877 case X86ISD::CMOV: return "X86ISD::CMOV";
1878 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001879 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001880 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1881 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001882 }
1883}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001884
1885bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1886 uint64_t Mask) const {
1887
1888 unsigned Opc = Op.getOpcode();
1889
1890 switch (Opc) {
1891 default:
1892 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1893 break;
1894 case X86ISD::SETCC: return (Mask & 1) == 0;
1895 }
1896
1897 return false;
1898}