blob: ad29b34018f8265bf6f3f3a5c53cdba8a3f4696c [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);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000055
56 if (X86ScalarSSE)
57 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
58 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
59 else
60 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000061
62 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
63 // this operation.
64 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
65 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
66
67 if (!X86ScalarSSE) {
68 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
69 // isn't legal.
70 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
71 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
72 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
73 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
74 }
75
76 // Handle FP_TO_UINT by promoting the destination to a larger signed
77 // conversion.
78 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
79 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
80 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
81
82 if (!X86ScalarSSE)
83 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
84
85 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
86 // this operation.
87 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
88 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
90
Chris Lattner30107e62005-12-23 05:15:23 +000091 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
92 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
93
Evan Cheng6fc31042005-12-19 23:12:38 +000094 if (X86DAGIsel) {
95 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
96 }
Chris Lattner76ac0682005-11-15 00:40:23 +000097 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
98 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
99 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
100 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
103 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
104 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
105 setOperationAction(ISD::FREM , MVT::f64 , Expand);
106 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
107 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
108 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
109 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
110 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
111 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
112 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
113 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
114 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000115 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000116
Evan Cheng6d2ab042006-01-11 23:20:05 +0000117 if (!X86DAGIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000118 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000119 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
120 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
121 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
122 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
123 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
124 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
125 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000126 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000127
Chris Lattner76ac0682005-11-15 00:40:23 +0000128 setOperationAction(ISD::READIO , MVT::i1 , Expand);
129 setOperationAction(ISD::READIO , MVT::i8 , Expand);
130 setOperationAction(ISD::READIO , MVT::i16 , Expand);
131 setOperationAction(ISD::READIO , MVT::i32 , Expand);
132 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
133 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
134 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
135 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
136
137 // These should be promoted to a larger select which is supported.
138 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
139 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng225a4d02005-12-17 01:21:05 +0000140 if (X86DAGIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000141 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000142 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
143 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000144 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
145 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000146 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
147 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
148 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000149 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
150 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
151 // X86 ret instruction may pop stack.
152 setOperationAction(ISD::RET , MVT::Other, Custom);
153 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000154 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000155 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
156 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
157 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
158 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
159 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
160 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000161 // X86 wants to expand memset / memcpy itself.
162 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
163 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000164 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000165
Chris Lattner9c415362005-11-29 06:16:21 +0000166 // We don't have line number support yet.
167 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000168 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
169 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000170
Chris Lattner78c358d2006-01-15 09:00:21 +0000171 // Expand to the default code.
172 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
173 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
174 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000175
Chris Lattner76ac0682005-11-15 00:40:23 +0000176 if (X86ScalarSSE) {
177 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000178 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
179 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000180
181 // SSE has no load+extend ops
182 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
183 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
184
185 // SSE has no i16 to fp conversion, only i32
186 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
187 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
188
189 // Expand FP_TO_UINT into a select.
190 // FIXME: We would like to use a Custom expander here eventually to do
191 // the optimal thing for SSE vs. the default expansion in the legalizer.
192 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
193
194 // We don't support sin/cos/sqrt/fmod
195 setOperationAction(ISD::FSIN , MVT::f64, Expand);
196 setOperationAction(ISD::FCOS , MVT::f64, Expand);
197 setOperationAction(ISD::FABS , MVT::f64, Expand);
198 setOperationAction(ISD::FNEG , MVT::f64, Expand);
199 setOperationAction(ISD::FREM , MVT::f64, Expand);
200 setOperationAction(ISD::FSIN , MVT::f32, Expand);
201 setOperationAction(ISD::FCOS , MVT::f32, Expand);
202 setOperationAction(ISD::FABS , MVT::f32, Expand);
203 setOperationAction(ISD::FNEG , MVT::f32, Expand);
204 setOperationAction(ISD::FREM , MVT::f32, Expand);
205
206 addLegalFPImmediate(+0.0); // xorps / xorpd
207 } else {
208 // Set up the FP register classes.
209 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
210
Evan Cheng6305e502006-01-12 22:54:21 +0000211 if (X86DAGIsel) {
212 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
213 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
214 }
215
Chris Lattner76ac0682005-11-15 00:40:23 +0000216 if (!UnsafeFPMath) {
217 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
218 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
219 }
220
221 addLegalFPImmediate(+0.0); // FLD0
222 addLegalFPImmediate(+1.0); // FLD1
223 addLegalFPImmediate(-0.0); // FLD0/FCHS
224 addLegalFPImmediate(-1.0); // FLD1/FCHS
225 }
226 computeRegisterProperties();
227
228 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
229 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
230 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
231 allowUnalignedMemoryAccesses = true; // x86 supports it!
232}
233
234std::vector<SDOperand>
235X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
236 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
237 return LowerFastCCArguments(F, DAG);
238 return LowerCCCArguments(F, DAG);
239}
240
241std::pair<SDOperand, SDOperand>
242X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
243 bool isVarArg, unsigned CallingConv,
244 bool isTailCall,
245 SDOperand Callee, ArgListTy &Args,
246 SelectionDAG &DAG) {
247 assert((!isVarArg || CallingConv == CallingConv::C) &&
248 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000249
250 // If the callee is a GlobalAddress node (quite common, every direct call is)
251 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
252 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
253 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000254 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
255 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000256
Chris Lattner76ac0682005-11-15 00:40:23 +0000257 if (CallingConv == CallingConv::Fast && EnableFastCC)
258 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
259 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
260}
261
Evan Chenga74ce622005-12-21 02:39:21 +0000262SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
263 SelectionDAG &DAG) {
264 if (!X86DAGIsel)
265 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
266
267 SDOperand Copy;
268 MVT::ValueType OpVT = Op.getValueType();
269 switch (OpVT) {
270 default: assert(0 && "Unknown type to return!");
271 case MVT::i32:
272 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
273 break;
274 case MVT::i64: {
275 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
276 DAG.getConstant(1, MVT::i32));
277 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
278 DAG.getConstant(0, MVT::i32));
Evan Cheng172fce72006-01-06 00:43:03 +0000279 Copy = DAG.getCopyToReg(Chain, X86::EDX, Hi, SDOperand());
280 Copy = DAG.getCopyToReg(Copy, X86::EAX, Lo, Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000281 break;
282 }
283 case MVT::f32:
Evan Chenga74ce622005-12-21 02:39:21 +0000284 case MVT::f64:
285 if (!X86ScalarSSE) {
Evan Cheng9c249c32006-01-09 18:33:28 +0000286 if (OpVT == MVT::f32)
287 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chenga74ce622005-12-21 02:39:21 +0000288 std::vector<MVT::ValueType> Tys;
289 Tys.push_back(MVT::Other);
290 Tys.push_back(MVT::Flag);
291 std::vector<SDOperand> Ops;
292 Ops.push_back(Chain);
293 Ops.push_back(Op);
294 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
295 } else {
296 // Spill the value to memory and reload it into top of stack.
297 unsigned Size = MVT::getSizeInBits(OpVT)/8;
298 MachineFunction &MF = DAG.getMachineFunction();
299 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
300 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
301 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
302 StackSlot, DAG.getSrcValue(NULL));
303 std::vector<MVT::ValueType> Tys;
304 Tys.push_back(MVT::f64);
305 Tys.push_back(MVT::Other);
306 std::vector<SDOperand> Ops;
307 Ops.push_back(Chain);
308 Ops.push_back(StackSlot);
309 Ops.push_back(DAG.getValueType(OpVT));
310 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
311 Tys.clear();
312 Tys.push_back(MVT::Other);
313 Tys.push_back(MVT::Flag);
314 Ops.clear();
315 Ops.push_back(Copy.getValue(1));
316 Ops.push_back(Copy);
317 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
318 }
319 break;
320 }
Evan Chengc1583db2005-12-21 20:21:51 +0000321
322 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
323 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
324 Copy.getValue(1));
Evan Chenga74ce622005-12-21 02:39:21 +0000325}
326
Chris Lattner76ac0682005-11-15 00:40:23 +0000327//===----------------------------------------------------------------------===//
328// C Calling Convention implementation
329//===----------------------------------------------------------------------===//
330
331std::vector<SDOperand>
332X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
333 std::vector<SDOperand> ArgValues;
334
335 MachineFunction &MF = DAG.getMachineFunction();
336 MachineFrameInfo *MFI = MF.getFrameInfo();
337
338 // Add DAG nodes to load the arguments... On entry to a function on the X86,
339 // the stack frame looks like this:
340 //
341 // [ESP] -- return address
342 // [ESP + 4] -- first argument (leftmost lexically)
343 // [ESP + 8] -- second argument, if first argument is four bytes in size
344 // ...
345 //
346 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
347 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
348 MVT::ValueType ObjectVT = getValueType(I->getType());
349 unsigned ArgIncrement = 4;
350 unsigned ObjSize;
351 switch (ObjectVT) {
352 default: assert(0 && "Unhandled argument type!");
353 case MVT::i1:
354 case MVT::i8: ObjSize = 1; break;
355 case MVT::i16: ObjSize = 2; break;
356 case MVT::i32: ObjSize = 4; break;
357 case MVT::i64: ObjSize = ArgIncrement = 8; break;
358 case MVT::f32: ObjSize = 4; break;
359 case MVT::f64: ObjSize = ArgIncrement = 8; break;
360 }
361 // Create the frame index object for this incoming parameter...
362 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
363
364 // Create the SelectionDAG nodes corresponding to a load from this parameter
365 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
366
367 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
368 // dead loads.
369 SDOperand ArgValue;
370 if (!I->use_empty())
371 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
372 DAG.getSrcValue(NULL));
373 else {
374 if (MVT::isInteger(ObjectVT))
375 ArgValue = DAG.getConstant(0, ObjectVT);
376 else
377 ArgValue = DAG.getConstantFP(0, ObjectVT);
378 }
379 ArgValues.push_back(ArgValue);
380
381 ArgOffset += ArgIncrement; // Move on to the next argument...
382 }
383
384 // If the function takes variable number of arguments, make a frame index for
385 // the start of the first vararg value... for expansion of llvm.va_start.
386 if (F.isVarArg())
387 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
388 ReturnAddrIndex = 0; // No return address slot generated yet.
389 BytesToPopOnReturn = 0; // Callee pops nothing.
390 BytesCallerReserves = ArgOffset;
391
392 // Finally, inform the code generator which regs we return values in.
393 switch (getValueType(F.getReturnType())) {
394 default: assert(0 && "Unknown type!");
395 case MVT::isVoid: break;
396 case MVT::i1:
397 case MVT::i8:
398 case MVT::i16:
399 case MVT::i32:
400 MF.addLiveOut(X86::EAX);
401 break;
402 case MVT::i64:
403 MF.addLiveOut(X86::EAX);
404 MF.addLiveOut(X86::EDX);
405 break;
406 case MVT::f32:
407 case MVT::f64:
408 MF.addLiveOut(X86::ST0);
409 break;
410 }
411 return ArgValues;
412}
413
414std::pair<SDOperand, SDOperand>
415X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
416 bool isVarArg, bool isTailCall,
417 SDOperand Callee, ArgListTy &Args,
418 SelectionDAG &DAG) {
419 // Count how many bytes are to be pushed on the stack.
420 unsigned NumBytes = 0;
421
422 if (Args.empty()) {
423 // Save zero bytes.
424 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
425 DAG.getConstant(0, getPointerTy()));
426 } else {
427 for (unsigned i = 0, e = Args.size(); i != e; ++i)
428 switch (getValueType(Args[i].second)) {
429 default: assert(0 && "Unknown value type!");
430 case MVT::i1:
431 case MVT::i8:
432 case MVT::i16:
433 case MVT::i32:
434 case MVT::f32:
435 NumBytes += 4;
436 break;
437 case MVT::i64:
438 case MVT::f64:
439 NumBytes += 8;
440 break;
441 }
442
443 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
444 DAG.getConstant(NumBytes, getPointerTy()));
445
446 // Arguments go on the stack in reverse order, as specified by the ABI.
447 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000448 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000449 std::vector<SDOperand> Stores;
450
451 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
452 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
453 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
454
455 switch (getValueType(Args[i].second)) {
456 default: assert(0 && "Unexpected ValueType for argument!");
457 case MVT::i1:
458 case MVT::i8:
459 case MVT::i16:
460 // Promote the integer to 32 bits. If the input type is signed use a
461 // sign extend, otherwise use a zero extend.
462 if (Args[i].second->isSigned())
463 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
464 else
465 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
466
467 // FALL THROUGH
468 case MVT::i32:
469 case MVT::f32:
470 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
471 Args[i].first, PtrOff,
472 DAG.getSrcValue(NULL)));
473 ArgOffset += 4;
474 break;
475 case MVT::i64:
476 case MVT::f64:
477 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
478 Args[i].first, PtrOff,
479 DAG.getSrcValue(NULL)));
480 ArgOffset += 8;
481 break;
482 }
483 }
484 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
485 }
486
487 std::vector<MVT::ValueType> RetVals;
488 MVT::ValueType RetTyVT = getValueType(RetTy);
489 RetVals.push_back(MVT::Other);
490
491 // The result values produced have to be legal. Promote the result.
492 switch (RetTyVT) {
493 case MVT::isVoid: break;
494 default:
495 RetVals.push_back(RetTyVT);
496 break;
497 case MVT::i1:
498 case MVT::i8:
499 case MVT::i16:
500 RetVals.push_back(MVT::i32);
501 break;
502 case MVT::f32:
503 if (X86ScalarSSE)
504 RetVals.push_back(MVT::f32);
505 else
506 RetVals.push_back(MVT::f64);
507 break;
508 case MVT::i64:
509 RetVals.push_back(MVT::i32);
510 RetVals.push_back(MVT::i32);
511 break;
512 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000513
Evan Cheng45e190982006-01-05 00:27:02 +0000514 if (X86DAGIsel) {
515 std::vector<MVT::ValueType> NodeTys;
516 NodeTys.push_back(MVT::Other); // Returns a chain
517 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000518 std::vector<SDOperand> Ops;
519 Ops.push_back(Chain);
520 Ops.push_back(Callee);
521
Evan Cheng172fce72006-01-06 00:43:03 +0000522 // FIXME: Do not generate X86ISD::TAILCALL for now.
523 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000524 SDOperand InFlag = Chain.getValue(1);
525
526 SDOperand RetVal;
527 if (RetTyVT != MVT::isVoid) {
528 switch (RetTyVT) {
529 default: assert(0 && "Unknown value type to return!");
530 case MVT::i1:
531 case MVT::i8:
532 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
533 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000534 if (RetTyVT == MVT::i1)
535 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000536 break;
537 case MVT::i16:
538 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
539 Chain = RetVal.getValue(1);
540 break;
541 case MVT::i32:
542 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
543 Chain = RetVal.getValue(1);
544 break;
545 case MVT::i64: {
546 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
547 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
548 Lo.getValue(2));
549 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
550 Chain = Hi.getValue(1);
551 break;
552 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000553 case MVT::f32:
Evan Cheng45e190982006-01-05 00:27:02 +0000554 case MVT::f64: {
555 std::vector<MVT::ValueType> Tys;
556 Tys.push_back(MVT::f64);
557 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +0000558 Tys.push_back(MVT::Flag);
Evan Cheng45e190982006-01-05 00:27:02 +0000559 std::vector<SDOperand> Ops;
560 Ops.push_back(Chain);
561 Ops.push_back(InFlag);
562 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +0000563 Chain = RetVal.getValue(1);
564 InFlag = RetVal.getValue(2);
Evan Cheng45e190982006-01-05 00:27:02 +0000565 if (X86ScalarSSE) {
Evan Cheng561881f2006-01-17 00:37:42 +0000566 // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
567 // shouldn't be necessary except for RFP cannot be live across
568 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng45e190982006-01-05 00:27:02 +0000569 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
570 MachineFunction &MF = DAG.getMachineFunction();
571 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
572 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
573 Tys.clear();
574 Tys.push_back(MVT::Other);
575 Ops.clear();
576 Ops.push_back(Chain);
577 Ops.push_back(RetVal);
578 Ops.push_back(StackSlot);
579 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +0000580 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000581 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
582 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
583 DAG.getSrcValue(NULL));
584 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +0000585 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000586
587 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
588 // FIXME: we would really like to remember that this FP_ROUND
589 // operation is okay to eliminate if we allow excess FP precision.
590 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000591 break;
592 }
593 }
594 }
595
596 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
597 DAG.getConstant(NumBytes, getPointerTy()),
598 DAG.getConstant(0, getPointerTy()));
599 return std::make_pair(RetVal, Chain);
600 } else {
601 std::vector<SDOperand> Ops;
602 Ops.push_back(Chain);
603 Ops.push_back(Callee);
604 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
605 Ops.push_back(DAG.getConstant(0, getPointerTy()));
606
607 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
608 RetVals, Ops);
609
610 SDOperand ResultVal;
611 switch (RetTyVT) {
612 case MVT::isVoid: break;
613 default:
614 ResultVal = TheCall.getValue(1);
615 break;
616 case MVT::i1:
617 case MVT::i8:
618 case MVT::i16:
619 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
620 break;
621 case MVT::f32:
622 // FIXME: we would really like to remember that this FP_ROUND operation is
623 // okay to eliminate if we allow excess FP precision.
624 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
625 break;
626 case MVT::i64:
627 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
628 TheCall.getValue(2));
629 break;
630 }
631
632 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
633 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000634 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000635}
636
637SDOperand
638X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
639 Value *VAListV, SelectionDAG &DAG) {
640 // vastart just stores the address of the VarArgsFrameIndex slot.
641 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
642 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
643 DAG.getSrcValue(VAListV));
644}
645
646
647std::pair<SDOperand,SDOperand>
648X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
649 Value *VAListV, const Type *ArgTy,
650 SelectionDAG &DAG) {
651 MVT::ValueType ArgVT = getValueType(ArgTy);
652 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
653 VAListP, DAG.getSrcValue(VAListV));
654 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
655 DAG.getSrcValue(NULL));
656 unsigned Amt;
657 if (ArgVT == MVT::i32)
658 Amt = 4;
659 else {
660 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
661 "Other types should have been promoted for varargs!");
662 Amt = 8;
663 }
664 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
665 DAG.getConstant(Amt, Val.getValueType()));
666 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
667 Val, VAListP, DAG.getSrcValue(VAListV));
668 return std::make_pair(Result, Chain);
669}
670
671//===----------------------------------------------------------------------===//
672// Fast Calling Convention implementation
673//===----------------------------------------------------------------------===//
674//
675// The X86 'fast' calling convention passes up to two integer arguments in
676// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
677// and requires that the callee pop its arguments off the stack (allowing proper
678// tail calls), and has the same return value conventions as C calling convs.
679//
680// This calling convention always arranges for the callee pop value to be 8n+4
681// bytes, which is needed for tail recursion elimination and stack alignment
682// reasons.
683//
684// Note that this can be enhanced in the future to pass fp vals in registers
685// (when we have a global fp allocator) and do other tricks.
686//
687
688/// AddLiveIn - This helper function adds the specified physical register to the
689/// MachineFunction as a live in value. It also creates a corresponding virtual
690/// register for it.
691static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
692 TargetRegisterClass *RC) {
693 assert(RC->contains(PReg) && "Not the correct regclass!");
694 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
695 MF.addLiveIn(PReg, VReg);
696 return VReg;
697}
698
699
700std::vector<SDOperand>
701X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
702 std::vector<SDOperand> ArgValues;
703
704 MachineFunction &MF = DAG.getMachineFunction();
705 MachineFrameInfo *MFI = MF.getFrameInfo();
706
707 // Add DAG nodes to load the arguments... On entry to a function the stack
708 // frame looks like this:
709 //
710 // [ESP] -- return address
711 // [ESP + 4] -- first nonreg argument (leftmost lexically)
712 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
713 // ...
714 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
715
716 // Keep track of the number of integer regs passed so far. This can be either
717 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
718 // used).
719 unsigned NumIntRegs = 0;
720
721 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
722 MVT::ValueType ObjectVT = getValueType(I->getType());
723 unsigned ArgIncrement = 4;
724 unsigned ObjSize = 0;
725 SDOperand ArgValue;
726
727 switch (ObjectVT) {
728 default: assert(0 && "Unhandled argument type!");
729 case MVT::i1:
730 case MVT::i8:
731 if (NumIntRegs < 2) {
732 if (!I->use_empty()) {
733 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
734 X86::R8RegisterClass);
735 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
736 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000737 if (ObjectVT == MVT::i1)
738 // FIXME: Should insert a assertzext here.
739 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000740 }
741 ++NumIntRegs;
742 break;
743 }
744
745 ObjSize = 1;
746 break;
747 case MVT::i16:
748 if (NumIntRegs < 2) {
749 if (!I->use_empty()) {
750 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
751 X86::R16RegisterClass);
752 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
753 DAG.setRoot(ArgValue.getValue(1));
754 }
755 ++NumIntRegs;
756 break;
757 }
758 ObjSize = 2;
759 break;
760 case MVT::i32:
761 if (NumIntRegs < 2) {
762 if (!I->use_empty()) {
763 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
764 X86::R32RegisterClass);
765 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
766 DAG.setRoot(ArgValue.getValue(1));
767 }
768 ++NumIntRegs;
769 break;
770 }
771 ObjSize = 4;
772 break;
773 case MVT::i64:
774 if (NumIntRegs == 0) {
775 if (!I->use_empty()) {
776 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
777 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
778
779 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
780 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
781 DAG.setRoot(Hi.getValue(1));
782
783 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
784 }
785 NumIntRegs = 2;
786 break;
787 } else if (NumIntRegs == 1) {
788 if (!I->use_empty()) {
789 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
790 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
791 DAG.setRoot(Low.getValue(1));
792
793 // Load the high part from memory.
794 // Create the frame index object for this incoming parameter...
795 int FI = MFI->CreateFixedObject(4, ArgOffset);
796 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
797 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
798 DAG.getSrcValue(NULL));
799 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
800 }
801 ArgOffset += 4;
802 NumIntRegs = 2;
803 break;
804 }
805 ObjSize = ArgIncrement = 8;
806 break;
807 case MVT::f32: ObjSize = 4; break;
808 case MVT::f64: ObjSize = ArgIncrement = 8; break;
809 }
810
811 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
812 // dead loads.
813 if (ObjSize && !I->use_empty()) {
814 // Create the frame index object for this incoming parameter...
815 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
816
817 // Create the SelectionDAG nodes corresponding to a load from this
818 // parameter.
819 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
820
821 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
822 DAG.getSrcValue(NULL));
823 } else if (ArgValue.Val == 0) {
824 if (MVT::isInteger(ObjectVT))
825 ArgValue = DAG.getConstant(0, ObjectVT);
826 else
827 ArgValue = DAG.getConstantFP(0, ObjectVT);
828 }
829 ArgValues.push_back(ArgValue);
830
831 if (ObjSize)
832 ArgOffset += ArgIncrement; // Move on to the next argument.
833 }
834
835 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
836 // arguments and the arguments after the retaddr has been pushed are aligned.
837 if ((ArgOffset & 7) == 0)
838 ArgOffset += 4;
839
840 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
841 ReturnAddrIndex = 0; // No return address slot generated yet.
842 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
843 BytesCallerReserves = 0;
844
845 // Finally, inform the code generator which regs we return values in.
846 switch (getValueType(F.getReturnType())) {
847 default: assert(0 && "Unknown type!");
848 case MVT::isVoid: break;
849 case MVT::i1:
850 case MVT::i8:
851 case MVT::i16:
852 case MVT::i32:
853 MF.addLiveOut(X86::EAX);
854 break;
855 case MVT::i64:
856 MF.addLiveOut(X86::EAX);
857 MF.addLiveOut(X86::EDX);
858 break;
859 case MVT::f32:
860 case MVT::f64:
861 MF.addLiveOut(X86::ST0);
862 break;
863 }
864 return ArgValues;
865}
866
867std::pair<SDOperand, SDOperand>
868X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
869 bool isTailCall, SDOperand Callee,
870 ArgListTy &Args, SelectionDAG &DAG) {
871 // Count how many bytes are to be pushed on the stack.
872 unsigned NumBytes = 0;
873
874 // Keep track of the number of integer regs passed so far. This can be either
875 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
876 // used).
877 unsigned NumIntRegs = 0;
878
879 for (unsigned i = 0, e = Args.size(); i != e; ++i)
880 switch (getValueType(Args[i].second)) {
881 default: assert(0 && "Unknown value type!");
882 case MVT::i1:
883 case MVT::i8:
884 case MVT::i16:
885 case MVT::i32:
886 if (NumIntRegs < 2) {
887 ++NumIntRegs;
888 break;
889 }
890 // fall through
891 case MVT::f32:
892 NumBytes += 4;
893 break;
894 case MVT::i64:
895 if (NumIntRegs == 0) {
896 NumIntRegs = 2;
897 break;
898 } else if (NumIntRegs == 1) {
899 NumIntRegs = 2;
900 NumBytes += 4;
901 break;
902 }
903
904 // fall through
905 case MVT::f64:
906 NumBytes += 8;
907 break;
908 }
909
910 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
911 // arguments and the arguments after the retaddr has been pushed are aligned.
912 if ((NumBytes & 7) == 0)
913 NumBytes += 4;
914
915 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
916 DAG.getConstant(NumBytes, getPointerTy()));
917
918 // Arguments go on the stack in reverse order, as specified by the ABI.
919 unsigned ArgOffset = 0;
920 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
921 X86::ESP, MVT::i32);
922 NumIntRegs = 0;
923 std::vector<SDOperand> Stores;
924 std::vector<SDOperand> RegValuesToPass;
925 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
926 switch (getValueType(Args[i].second)) {
927 default: assert(0 && "Unexpected ValueType for argument!");
928 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000929 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
930 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000931 case MVT::i8:
932 case MVT::i16:
933 case MVT::i32:
934 if (NumIntRegs < 2) {
935 RegValuesToPass.push_back(Args[i].first);
936 ++NumIntRegs;
937 break;
938 }
939 // Fall through
940 case MVT::f32: {
941 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
942 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
943 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
944 Args[i].first, PtrOff,
945 DAG.getSrcValue(NULL)));
946 ArgOffset += 4;
947 break;
948 }
949 case MVT::i64:
950 if (NumIntRegs < 2) { // Can pass part of it in regs?
951 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
952 Args[i].first, DAG.getConstant(1, MVT::i32));
953 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
954 Args[i].first, DAG.getConstant(0, MVT::i32));
955 RegValuesToPass.push_back(Lo);
956 ++NumIntRegs;
957 if (NumIntRegs < 2) { // Pass both parts in regs?
958 RegValuesToPass.push_back(Hi);
959 ++NumIntRegs;
960 } else {
961 // Pass the high part in memory.
962 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
963 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
964 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
965 Hi, PtrOff, DAG.getSrcValue(NULL)));
966 ArgOffset += 4;
967 }
968 break;
969 }
970 // Fall through
971 case MVT::f64:
972 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
973 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
974 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
975 Args[i].first, PtrOff,
976 DAG.getSrcValue(NULL)));
977 ArgOffset += 8;
978 break;
979 }
980 }
981 if (!Stores.empty())
982 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
983
984 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
985 // arguments and the arguments after the retaddr has been pushed are aligned.
986 if ((ArgOffset & 7) == 0)
987 ArgOffset += 4;
988
989 std::vector<MVT::ValueType> RetVals;
990 MVT::ValueType RetTyVT = getValueType(RetTy);
991
992 RetVals.push_back(MVT::Other);
993
994 // The result values produced have to be legal. Promote the result.
995 switch (RetTyVT) {
996 case MVT::isVoid: break;
997 default:
998 RetVals.push_back(RetTyVT);
999 break;
1000 case MVT::i1:
1001 case MVT::i8:
1002 case MVT::i16:
1003 RetVals.push_back(MVT::i32);
1004 break;
1005 case MVT::f32:
1006 if (X86ScalarSSE)
1007 RetVals.push_back(MVT::f32);
1008 else
1009 RetVals.push_back(MVT::f64);
1010 break;
1011 case MVT::i64:
1012 RetVals.push_back(MVT::i32);
1013 RetVals.push_back(MVT::i32);
1014 break;
1015 }
1016
Evan Cheng172fce72006-01-06 00:43:03 +00001017 if (X86DAGIsel) {
1018 // Build a sequence of copy-to-reg nodes chained together with token chain
1019 // and flag operands which copy the outgoing args into registers.
1020 SDOperand InFlag;
1021 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1022 unsigned CCReg;
1023 SDOperand RegToPass = RegValuesToPass[i];
1024 switch (RegToPass.getValueType()) {
1025 default: assert(0 && "Bad thing to pass in regs");
1026 case MVT::i8:
1027 CCReg = (i == 0) ? X86::AL : X86::DL;
1028 break;
1029 case MVT::i16:
1030 CCReg = (i == 0) ? X86::AX : X86::DX;
1031 break;
1032 case MVT::i32:
1033 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1034 break;
1035 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001036
Evan Cheng172fce72006-01-06 00:43:03 +00001037 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1038 InFlag = Chain.getValue(1);
1039 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001040
Evan Cheng172fce72006-01-06 00:43:03 +00001041 std::vector<MVT::ValueType> NodeTys;
1042 NodeTys.push_back(MVT::Other); // Returns a chain
1043 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +00001044 std::vector<SDOperand> Ops;
1045 Ops.push_back(Chain);
1046 Ops.push_back(Callee);
1047 if (InFlag.Val)
1048 Ops.push_back(InFlag);
1049
1050 // FIXME: Do not generate X86ISD::TAILCALL for now.
1051 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1052 InFlag = Chain.getValue(1);
1053
1054 SDOperand RetVal;
1055 if (RetTyVT != MVT::isVoid) {
1056 switch (RetTyVT) {
1057 default: assert(0 && "Unknown value type to return!");
1058 case MVT::i1:
1059 case MVT::i8:
1060 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1061 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +00001062 if (RetTyVT == MVT::i1)
1063 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001064 break;
1065 case MVT::i16:
1066 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1067 Chain = RetVal.getValue(1);
1068 break;
1069 case MVT::i32:
1070 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1071 Chain = RetVal.getValue(1);
1072 break;
1073 case MVT::i64: {
1074 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1075 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1076 Lo.getValue(2));
1077 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1078 Chain = Hi.getValue(1);
1079 break;
1080 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001081 case MVT::f32:
Evan Cheng172fce72006-01-06 00:43:03 +00001082 case MVT::f64: {
1083 std::vector<MVT::ValueType> Tys;
1084 Tys.push_back(MVT::f64);
1085 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +00001086 Tys.push_back(MVT::Flag);
Evan Cheng172fce72006-01-06 00:43:03 +00001087 std::vector<SDOperand> Ops;
1088 Ops.push_back(Chain);
1089 Ops.push_back(InFlag);
1090 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +00001091 Chain = RetVal.getValue(1);
1092 InFlag = RetVal.getValue(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001093 if (X86ScalarSSE) {
Evan Cheng561881f2006-01-17 00:37:42 +00001094 // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
1095 // shouldn't be necessary except for RFP cannot be live across
1096 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng172fce72006-01-06 00:43:03 +00001097 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1098 MachineFunction &MF = DAG.getMachineFunction();
1099 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1100 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1101 Tys.clear();
1102 Tys.push_back(MVT::Other);
1103 Ops.clear();
1104 Ops.push_back(Chain);
1105 Ops.push_back(RetVal);
1106 Ops.push_back(StackSlot);
1107 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +00001108 Ops.push_back(InFlag);
Evan Cheng172fce72006-01-06 00:43:03 +00001109 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1110 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1111 DAG.getSrcValue(NULL));
1112 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +00001113 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001114
1115 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1116 // FIXME: we would really like to remember that this FP_ROUND
1117 // operation is okay to eliminate if we allow excess FP precision.
1118 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001119 break;
1120 }
1121 }
1122 }
1123
1124 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1125 DAG.getConstant(ArgOffset, getPointerTy()),
1126 DAG.getConstant(ArgOffset, getPointerTy()));
1127 return std::make_pair(RetVal, Chain);
1128 } else {
1129 std::vector<SDOperand> Ops;
1130 Ops.push_back(Chain);
1131 Ops.push_back(Callee);
1132 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1133 // Callee pops all arg values on the stack.
1134 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1135
1136 // Pass register arguments as needed.
1137 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1138
1139 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1140 RetVals, Ops);
1141 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1142
1143 SDOperand ResultVal;
1144 switch (RetTyVT) {
1145 case MVT::isVoid: break;
1146 default:
1147 ResultVal = TheCall.getValue(1);
1148 break;
1149 case MVT::i1:
1150 case MVT::i8:
1151 case MVT::i16:
1152 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1153 break;
1154 case MVT::f32:
1155 // FIXME: we would really like to remember that this FP_ROUND operation is
1156 // okay to eliminate if we allow excess FP precision.
1157 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1158 break;
1159 case MVT::i64:
1160 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1161 TheCall.getValue(2));
1162 break;
1163 }
1164
1165 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001166 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001167}
1168
1169SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1170 if (ReturnAddrIndex == 0) {
1171 // Set up a frame object for the return address.
1172 MachineFunction &MF = DAG.getMachineFunction();
1173 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1174 }
1175
1176 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1177}
1178
1179
1180
1181std::pair<SDOperand, SDOperand> X86TargetLowering::
1182LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1183 SelectionDAG &DAG) {
1184 SDOperand Result;
1185 if (Depth) // Depths > 0 not supported yet!
1186 Result = DAG.getConstant(0, getPointerTy());
1187 else {
1188 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1189 if (!isFrameAddress)
1190 // Just load the return address
1191 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1192 DAG.getSrcValue(NULL));
1193 else
1194 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1195 DAG.getConstant(4, MVT::i32));
1196 }
1197 return std::make_pair(Result, Chain);
1198}
1199
Evan Cheng339edad2006-01-11 00:33:36 +00001200/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1201/// which corresponds to the condition code.
1202static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1203 switch (X86CC) {
1204 default: assert(0 && "Unknown X86 conditional code!");
1205 case X86ISD::COND_A: return X86::JA;
1206 case X86ISD::COND_AE: return X86::JAE;
1207 case X86ISD::COND_B: return X86::JB;
1208 case X86ISD::COND_BE: return X86::JBE;
1209 case X86ISD::COND_E: return X86::JE;
1210 case X86ISD::COND_G: return X86::JG;
1211 case X86ISD::COND_GE: return X86::JGE;
1212 case X86ISD::COND_L: return X86::JL;
1213 case X86ISD::COND_LE: return X86::JLE;
1214 case X86ISD::COND_NE: return X86::JNE;
1215 case X86ISD::COND_NO: return X86::JNO;
1216 case X86ISD::COND_NP: return X86::JNP;
1217 case X86ISD::COND_NS: return X86::JNS;
1218 case X86ISD::COND_O: return X86::JO;
1219 case X86ISD::COND_P: return X86::JP;
1220 case X86ISD::COND_S: return X86::JS;
1221 }
1222}
Chris Lattner76ac0682005-11-15 00:40:23 +00001223
Evan Cheng339edad2006-01-11 00:33:36 +00001224/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1225/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Cheng172fce72006-01-06 00:43:03 +00001226/// do a direct translation.
Evan Cheng339edad2006-01-11 00:33:36 +00001227static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Cheng172fce72006-01-06 00:43:03 +00001228 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1229 unsigned X86CC = X86ISD::COND_INVALID;
1230 if (!isFP) {
1231 switch (SetCCOpcode) {
1232 default: break;
1233 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1234 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1235 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1236 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1237 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1238 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1239 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1240 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1241 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1242 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1243 }
1244 } else {
1245 // On a floating point condition, the flags are set as follows:
1246 // ZF PF CF op
1247 // 0 | 0 | 0 | X > Y
1248 // 0 | 0 | 1 | X < Y
1249 // 1 | 0 | 0 | X == Y
1250 // 1 | 1 | 1 | unordered
1251 switch (SetCCOpcode) {
1252 default: break;
1253 case ISD::SETUEQ:
1254 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1255 case ISD::SETOGT:
1256 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1257 case ISD::SETOGE:
1258 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1259 case ISD::SETULT:
1260 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1261 case ISD::SETULE:
1262 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1263 case ISD::SETONE:
1264 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1265 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1266 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1267 }
1268 }
1269 return X86CC;
1270}
1271
Evan Cheng339edad2006-01-11 00:33:36 +00001272/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1273/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001274/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001275static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001276 switch (X86CC) {
1277 default:
1278 return false;
1279 case X86ISD::COND_B:
1280 case X86ISD::COND_BE:
1281 case X86ISD::COND_E:
1282 case X86ISD::COND_P:
1283 case X86ISD::COND_A:
1284 case X86ISD::COND_AE:
1285 case X86ISD::COND_NE:
1286 case X86ISD::COND_NP:
1287 return true;
1288 }
1289}
1290
Evan Cheng339edad2006-01-11 00:33:36 +00001291MachineBasicBlock *
1292X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1293 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001294 switch (MI->getOpcode()) {
1295 default: assert(false && "Unexpected instr type to insert");
1296 case X86::CMOV_FR32:
1297 case X86::CMOV_FR64: {
1298 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1299 // control-flow pattern. The incoming instruction knows the destination vreg
1300 // to set, the condition code register to branch on, the true/false values to
1301 // select between, and a branch opcode to use.
1302 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1303 ilist<MachineBasicBlock>::iterator It = BB;
1304 ++It;
1305
1306 // thisMBB:
1307 // ...
1308 // TrueVal = ...
1309 // cmpTY ccX, r1, r2
1310 // bCC copy1MBB
1311 // fallthrough --> copy0MBB
1312 MachineBasicBlock *thisMBB = BB;
1313 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1314 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1315 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1316 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1317 MachineFunction *F = BB->getParent();
1318 F->getBasicBlockList().insert(It, copy0MBB);
1319 F->getBasicBlockList().insert(It, sinkMBB);
1320 // Update machine-CFG edges
1321 BB->addSuccessor(copy0MBB);
1322 BB->addSuccessor(sinkMBB);
1323
1324 // copy0MBB:
1325 // %FalseValue = ...
1326 // # fallthrough to sinkMBB
1327 BB = copy0MBB;
1328
1329 // Update machine-CFG edges
1330 BB->addSuccessor(sinkMBB);
1331
1332 // sinkMBB:
1333 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1334 // ...
1335 BB = sinkMBB;
1336 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1337 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1338 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001339
Evan Cheng911c68d2006-01-16 21:21:29 +00001340 delete MI; // The pseudo instruction is gone now.
1341 return BB;
1342 }
Evan Cheng339edad2006-01-11 00:33:36 +00001343
Evan Cheng911c68d2006-01-16 21:21:29 +00001344 case X86::FP_TO_INT16_IN_MEM:
1345 case X86::FP_TO_INT32_IN_MEM:
1346 case X86::FP_TO_INT64_IN_MEM: {
1347 // Change the floating point control register to use "round towards zero"
1348 // mode when truncating to an integer value.
1349 MachineFunction *F = BB->getParent();
1350 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1351 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1352
1353 // Load the old value of the high byte of the control word...
1354 unsigned OldCW =
1355 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1356 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1357
1358 // Set the high part to be round to zero...
1359 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1360
1361 // Reload the modified control word now...
1362 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1363
1364 // Restore the memory image of control word to original value
1365 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1366
1367 // Get the X86 opcode to use.
1368 unsigned Opc;
1369 switch (MI->getOpcode()) {
1370 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1371 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1372 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1373 }
1374
1375 X86AddressMode AM;
1376 MachineOperand &Op = MI->getOperand(0);
1377 if (Op.isRegister()) {
1378 AM.BaseType = X86AddressMode::RegBase;
1379 AM.Base.Reg = Op.getReg();
1380 } else {
1381 AM.BaseType = X86AddressMode::FrameIndexBase;
1382 AM.Base.FrameIndex = Op.getFrameIndex();
1383 }
1384 Op = MI->getOperand(1);
1385 if (Op.isImmediate())
1386 AM.Scale = Op.getImmedValue();
1387 Op = MI->getOperand(2);
1388 if (Op.isImmediate())
1389 AM.IndexReg = Op.getImmedValue();
1390 Op = MI->getOperand(3);
1391 if (Op.isGlobalAddress()) {
1392 AM.GV = Op.getGlobal();
1393 } else {
1394 AM.Disp = Op.getImmedValue();
1395 }
1396 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1397
1398 // Reload the original control word now.
1399 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1400
1401 delete MI; // The pseudo instruction is gone now.
1402 return BB;
1403 }
1404 }
Evan Cheng339edad2006-01-11 00:33:36 +00001405}
1406
1407
1408//===----------------------------------------------------------------------===//
1409// X86 Custom Lowering Hooks
1410//===----------------------------------------------------------------------===//
1411
Chris Lattner76ac0682005-11-15 00:40:23 +00001412/// LowerOperation - Provide custom lowering hooks for some operations.
1413///
1414SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1415 switch (Op.getOpcode()) {
1416 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001417 case ISD::ADD_PARTS:
1418 case ISD::SUB_PARTS: {
1419 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1420 "Not an i64 add/sub!");
1421 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1422 std::vector<MVT::ValueType> Tys;
1423 Tys.push_back(MVT::i32);
1424 Tys.push_back(MVT::Flag);
1425 std::vector<SDOperand> Ops;
1426 Ops.push_back(Op.getOperand(0));
1427 Ops.push_back(Op.getOperand(2));
1428 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1429 Tys, Ops);
1430 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1431 Op.getOperand(1), Op.getOperand(3),
1432 Lo.getValue(1));
1433 Tys.clear();
1434 Tys.push_back(MVT::i32);
1435 Tys.push_back(MVT::i32);
1436 Ops.clear();
1437 Ops.push_back(Lo);
1438 Ops.push_back(Hi);
1439 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1440 }
1441 case ISD::SHL_PARTS:
1442 case ISD::SRA_PARTS:
1443 case ISD::SRL_PARTS: {
1444 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1445 "Not an i64 shift!");
1446 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1447 SDOperand ShOpLo = Op.getOperand(0);
1448 SDOperand ShOpHi = Op.getOperand(1);
1449 SDOperand ShAmt = Op.getOperand(2);
1450 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001451 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001452 : DAG.getConstant(0, MVT::i32);
1453
1454 SDOperand Tmp2, Tmp3;
1455 if (Op.getOpcode() == ISD::SHL_PARTS) {
1456 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1457 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1458 } else {
1459 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001460 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001461 }
1462
1463 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1464 ShAmt, DAG.getConstant(32, MVT::i8));
1465
1466 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001467 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001468
1469 std::vector<MVT::ValueType> Tys;
1470 Tys.push_back(MVT::i32);
1471 Tys.push_back(MVT::Flag);
1472 std::vector<SDOperand> Ops;
1473 if (Op.getOpcode() == ISD::SHL_PARTS) {
1474 Ops.push_back(Tmp2);
1475 Ops.push_back(Tmp3);
1476 Ops.push_back(CC);
1477 Ops.push_back(InFlag);
1478 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1479 InFlag = Hi.getValue(1);
1480
1481 Ops.clear();
1482 Ops.push_back(Tmp3);
1483 Ops.push_back(Tmp1);
1484 Ops.push_back(CC);
1485 Ops.push_back(InFlag);
1486 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1487 } else {
1488 Ops.push_back(Tmp2);
1489 Ops.push_back(Tmp3);
1490 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001491 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001492 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1493 InFlag = Lo.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 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1501 }
1502
1503 Tys.clear();
1504 Tys.push_back(MVT::i32);
1505 Tys.push_back(MVT::i32);
1506 Ops.clear();
1507 Ops.push_back(Lo);
1508 Ops.push_back(Hi);
1509 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1510 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001511 case ISD::SINT_TO_FP: {
1512 assert(Op.getValueType() == MVT::f64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001513 Op.getOperand(0).getValueType() <= MVT::i64 &&
1514 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001515 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001516
1517 SDOperand Result;
1518 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1519 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001520 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001521 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001522 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001523 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1524 DAG.getEntryNode(), Op.getOperand(0),
1525 StackSlot, DAG.getSrcValue(NULL));
1526
1527 // Build the FILD
1528 std::vector<MVT::ValueType> Tys;
1529 Tys.push_back(MVT::f64);
1530 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001531 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001532 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001533 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001534 Ops.push_back(DAG.getValueType(SrcVT));
1535 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
1536 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001537 }
1538 case ISD::FP_TO_SINT: {
1539 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1540 Op.getOperand(0).getValueType() == MVT::f64 &&
1541 "Unknown FP_TO_SINT to lower!");
1542 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1543 // stack slot.
1544 MachineFunction &MF = DAG.getMachineFunction();
1545 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1546 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1547 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1548
1549 unsigned Opc;
1550 switch (Op.getValueType()) {
1551 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1552 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1553 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1554 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1555 }
1556
1557 // Build the FP_TO_INT*_IN_MEM
1558 std::vector<SDOperand> Ops;
1559 Ops.push_back(DAG.getEntryNode());
1560 Ops.push_back(Op.getOperand(0));
1561 Ops.push_back(StackSlot);
1562 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1563
1564 // Load the result.
1565 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1566 DAG.getSrcValue(NULL));
1567 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001568 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001569 std::vector<MVT::ValueType> Tys;
1570 Tys.push_back(MVT::Other);
1571 Tys.push_back(MVT::Flag);
1572 std::vector<SDOperand> Ops;
1573 Ops.push_back(Op.getOperand(0));
1574 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001575 Ops.clear();
1576 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1577 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1578 MVT::i32, Ops[0].getValue(2)));
1579 Ops.push_back(Ops[1].getValue(1));
1580 Tys[0] = Tys[1] = MVT::i32;
1581 Tys.push_back(MVT::Other);
1582 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001583 }
Evan Chengc1583db2005-12-21 20:21:51 +00001584 case ISD::SETCC: {
1585 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1586 SDOperand CC = Op.getOperand(2);
1587 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1588 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001589 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1590 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001591 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001592 if (X86CC != X86ISD::COND_INVALID) {
1593 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1594 DAG.getConstant(X86CC, MVT::i8), Cond);
1595 } else {
1596 assert(isFP && "Illegal integer SetCC!");
1597
1598 std::vector<MVT::ValueType> Tys;
1599 std::vector<SDOperand> Ops;
1600 switch (SetCCOpcode) {
1601 default: assert(false && "Illegal floating point SetCC!");
1602 case ISD::SETOEQ: { // !PF & ZF
1603 Tys.push_back(MVT::i8);
1604 Tys.push_back(MVT::Flag);
1605 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1606 Ops.push_back(Cond);
1607 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1608 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1609 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1610 Tmp1.getValue(1));
1611 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1612 }
1613 case ISD::SETOLT: { // !PF & CF
1614 Tys.push_back(MVT::i8);
1615 Tys.push_back(MVT::Flag);
1616 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1617 Ops.push_back(Cond);
1618 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1619 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1620 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1621 Tmp1.getValue(1));
1622 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1623 }
1624 case ISD::SETOLE: { // !PF & (CF || ZF)
1625 Tys.push_back(MVT::i8);
1626 Tys.push_back(MVT::Flag);
1627 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1628 Ops.push_back(Cond);
1629 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1630 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1631 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1632 Tmp1.getValue(1));
1633 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1634 }
1635 case ISD::SETUGT: { // PF | (!ZF & !CF)
1636 Tys.push_back(MVT::i8);
1637 Tys.push_back(MVT::Flag);
1638 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1639 Ops.push_back(Cond);
1640 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1641 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1642 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1643 Tmp1.getValue(1));
1644 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1645 }
1646 case ISD::SETUGE: { // PF | !CF
1647 Tys.push_back(MVT::i8);
1648 Tys.push_back(MVT::Flag);
1649 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1650 Ops.push_back(Cond);
1651 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1652 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1653 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1654 Tmp1.getValue(1));
1655 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1656 }
1657 case ISD::SETUNE: { // PF | !ZF
1658 Tys.push_back(MVT::i8);
1659 Tys.push_back(MVT::Flag);
1660 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1661 Ops.push_back(Cond);
1662 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1663 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1664 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1665 Tmp1.getValue(1));
1666 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1667 }
1668 }
1669 }
Evan Chengc1583db2005-12-21 20:21:51 +00001670 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001671 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001672 MVT::ValueType VT = Op.getValueType();
1673 bool isFP = MVT::isFloatingPoint(VT);
1674 bool isFPStack = isFP && (X86Vector < SSE2);
1675 bool isFPSSE = isFP && (X86Vector >= SSE2);
Evan Chengfb22e862006-01-13 01:03:02 +00001676 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001677 SDOperand Op0 = Op.getOperand(0);
1678 SDOperand Cond, CC;
1679 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001680 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1681 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1682 // have another use it will be eliminated.
1683 // If the X86ISD::SETCC has more than one use, then it's probably better
1684 // to use a test instead of duplicating the X86ISD::CMP (for register
1685 // pressure reason).
Evan Chengd7faa4b2006-01-13 01:17:24 +00001686 if (Op0.hasOneUse() && Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
Evan Chengfb22e862006-01-13 01:03:02 +00001687 CC = Op0.getOperand(0);
1688 Cond = Op0.getOperand(1);
1689 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001690 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001691 } else
1692 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001693 } else if (Op0.getOpcode() == ISD::SETCC) {
1694 CC = Op0.getOperand(2);
1695 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001696 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001697 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001698 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001699 Op0.getOperand(0), Op0.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001700 addTest = true;
1701 } else
1702 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001703
Evan Cheng731423f2006-01-13 01:06:49 +00001704 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001705 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001706 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001707 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001708
1709 std::vector<MVT::ValueType> Tys;
1710 Tys.push_back(Op.getValueType());
1711 Tys.push_back(MVT::Flag);
1712 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001713 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1714 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001715 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001716 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001717 Ops.push_back(CC);
1718 Ops.push_back(Cond);
1719 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001720 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001721 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001722 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001723 SDOperand Cond = Op.getOperand(1);
1724 SDOperand Dest = Op.getOperand(2);
1725 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001726 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001727 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1728 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1729 // have another use it will be eliminated.
1730 // If the X86ISD::SETCC has more than one use, then it's probably better
1731 // to use a test instead of duplicating the X86ISD::CMP (for register
1732 // pressure reason).
1733 if (Cond.hasOneUse() && Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1734 CC = Cond.getOperand(0);
1735 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1736 Cond.getOperand(1).getOperand(0),
1737 Cond.getOperand(1).getOperand(1));
1738 } else
1739 addTest = true;
Evan Chengc1583db2005-12-21 20:21:51 +00001740 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001741 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001742 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001743 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001744 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001745 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1746 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001747 } else
1748 addTest = true;
1749
1750 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001751 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001752 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1753 }
1754 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1755 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1756 }
Evan Cheng172fce72006-01-06 00:43:03 +00001757 case ISD::RET: {
1758 // Can only be return void.
Evan Cheng9c249c32006-01-09 18:33:28 +00001759 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng172fce72006-01-06 00:43:03 +00001760 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1761 }
Evan Chengae986f12006-01-11 22:15:48 +00001762 case ISD::MEMSET: {
1763 SDOperand InFlag;
1764 SDOperand Chain = Op.getOperand(0);
1765 unsigned Align =
1766 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1767 if (Align == 0) Align = 1;
1768
1769 MVT::ValueType AVT;
1770 SDOperand Count;
1771 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1772 unsigned ValReg;
1773 unsigned Val = ValC->getValue() & 255;
1774
1775 // If the value is a constant, then we can potentially use larger sets.
1776 switch (Align & 3) {
1777 case 2: // WORD aligned
1778 AVT = MVT::i16;
1779 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1780 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1781 else
1782 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1783 DAG.getConstant(1, MVT::i8));
1784 Val = (Val << 8) | Val;
1785 ValReg = X86::AX;
1786 break;
1787 case 0: // DWORD aligned
1788 AVT = MVT::i32;
1789 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1790 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1791 else
1792 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1793 DAG.getConstant(2, MVT::i8));
1794 Val = (Val << 8) | Val;
1795 Val = (Val << 16) | Val;
1796 ValReg = X86::EAX;
1797 break;
1798 default: // Byte aligned
1799 AVT = MVT::i8;
1800 Count = Op.getOperand(3);
1801 ValReg = X86::AL;
1802 break;
1803 }
1804
1805 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1806 InFlag);
1807 InFlag = Chain.getValue(1);
1808 } else {
1809 AVT = MVT::i8;
1810 Count = Op.getOperand(3);
1811 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1812 InFlag = Chain.getValue(1);
1813 }
1814
1815 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1816 InFlag = Chain.getValue(1);
1817 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1818 InFlag = Chain.getValue(1);
1819
1820 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1821 DAG.getValueType(AVT), InFlag);
1822 }
1823 case ISD::MEMCPY: {
1824 SDOperand Chain = Op.getOperand(0);
1825 unsigned Align =
1826 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1827 if (Align == 0) Align = 1;
1828
1829 MVT::ValueType AVT;
1830 SDOperand Count;
1831 switch (Align & 3) {
1832 case 2: // WORD aligned
1833 AVT = MVT::i16;
1834 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1835 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1836 else
1837 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1838 DAG.getConstant(1, MVT::i8));
1839 break;
1840 case 0: // DWORD aligned
1841 AVT = MVT::i32;
1842 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1843 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1844 else
1845 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1846 DAG.getConstant(2, MVT::i8));
1847 break;
1848 default: // Byte aligned
1849 AVT = MVT::i8;
1850 Count = Op.getOperand(3);
1851 break;
1852 }
1853
1854 SDOperand InFlag;
1855 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1856 InFlag = Chain.getValue(1);
1857 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1858 InFlag = Chain.getValue(1);
1859 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1860 InFlag = Chain.getValue(1);
1861
1862 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1863 DAG.getValueType(AVT), InFlag);
1864 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001865 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001866 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001867 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1868 // For Darwin, external and weak symbols are indirect, so we want to load
1869 // the value at address GV, not the value of GV itself. This means that
1870 // the GlobalAddress must be in the base or index register of the address,
1871 // not the GV offset field.
1872 if (getTargetMachine().
1873 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1874 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001875 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1876 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1877 DAG.getSrcValue(NULL));
1878 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001879 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001880 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001881}
Evan Cheng6af02632005-12-20 06:22:03 +00001882
1883const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1884 switch (Opcode) {
1885 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001886 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1887 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1888 case X86ISD::ADC: return "X86ISD::ADC";
1889 case X86ISD::SBB: return "X86ISD::SBB";
1890 case X86ISD::SHLD: return "X86ISD::SHLD";
1891 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6305e502006-01-12 22:54:21 +00001892 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001893 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1894 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1895 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001896 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001897 case X86ISD::FST: return "X86ISD::FST";
1898 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001899 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001900 case X86ISD::CALL: return "X86ISD::CALL";
1901 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1902 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1903 case X86ISD::CMP: return "X86ISD::CMP";
1904 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001905 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001906 case X86ISD::CMOV: return "X86ISD::CMOV";
1907 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001908 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001909 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1910 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001911 }
1912}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001913
1914bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1915 uint64_t Mask) const {
1916
1917 unsigned Opc = Op.getOpcode();
1918
1919 switch (Opc) {
1920 default:
1921 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1922 break;
1923 case X86ISD::SETCC: return (Mask & 1) == 0;
1924 }
1925
1926 return false;
1927}