blob: bc8459fd0a4e75786af307aa9d9bdac2507aa945 [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
20#include "llvm/Function.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-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 Lattnerdbdbf0c2005-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 Lattner9edba762006-01-13 18:00:54 +000043 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000044
45 // Set up the register classes.
Chris Lattnerdbdbf0c2005-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 Lattner21f66852005-12-23 05:15:23 +000086 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
87 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
88
Evan Cheng898101c2005-12-19 23:12:38 +000089 if (X86DAGIsel) {
90 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
91 }
Chris Lattnerdbdbf0c2005-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 Lattnere80242a2005-12-07 17:59:14 +000096 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-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 Lenharthb873ff32005-11-20 21:41:10 +0000110 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111
Evan Chengeb422a72006-01-11 23:20:05 +0000112 if (!X86DAGIsel) {
Nate Begemand88fc032006-01-14 03:14:10 +0000113 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Chengeb422a72006-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 Begemand88fc032006-01-14 03:14:10 +0000121 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000122
Chris Lattnerdbdbf0c2005-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 Cheng7df96d62005-12-17 01:21:05 +0000135 if (X86DAGIsel) {
Evan Chengd9558e02006-01-06 00:43:03 +0000136 // X86 wants to expand cmov itself.
Evan Cheng7df96d62005-12-17 01:21:05 +0000137 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
138 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Chengd9558e02006-01-06 00:43:03 +0000139 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
140 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengd5781fc2005-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 Chengd9558e02006-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 Cheng3a03ebb2005-12-21 23:05:39 +0000149 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge3413162006-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 Cheng67f92a72006-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 Cheng7df96d62005-12-17 01:21:05 +0000159 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000160
Chris Lattnerf73bae12005-11-29 06:16:21 +0000161 // We don't have line number support yet.
162 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000163 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
164 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000165
Chris Lattnere1125522006-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 Lattnerb99329e2006-01-13 02:42:53 +0000170
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 if (X86ScalarSSE) {
172 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000173 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
174 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-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 Chenga3195e82006-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 Lattnerdbdbf0c2005-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 Chengd9558e02006-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 Cheng8700e142006-01-11 06:09:51 +0000249 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
250 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000251
Chris Lattnerdbdbf0c2005-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 Chengb077b842005-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 Chengd9558e02006-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 Chengb077b842005-12-21 02:39:21 +0000276 break;
277 }
278 case MVT::f32:
Evan Chengb077b842005-12-21 02:39:21 +0000279 case MVT::f64:
280 if (!X86ScalarSSE) {
Evan Chenge3413162006-01-09 18:33:28 +0000281 if (OpVT == MVT::f32)
282 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chengb077b842005-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 Chengd5781fc2005-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 Chengb077b842005-12-21 02:39:21 +0000320}
321
Chris Lattnerdbdbf0c2005-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 Cheng8700e142006-01-11 06:09:51 +0000443 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000508
Evan Chengd90eb7f2006-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 Chengd90eb7f2006-01-05 00:27:02 +0000513 std::vector<SDOperand> Ops;
514 Ops.push_back(Chain);
515 Ops.push_back(Callee);
516
Evan Chengd9558e02006-01-06 00:43:03 +0000517 // FIXME: Do not generate X86ISD::TAILCALL for now.
518 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Chengd90eb7f2006-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 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000546 case MVT::f64: {
547 std::vector<MVT::ValueType> Tys;
548 Tys.push_back(MVT::f64);
549 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000550 Tys.push_back(MVT::Flag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000551 std::vector<SDOperand> Ops;
552 Ops.push_back(Chain);
553 Ops.push_back(InFlag);
554 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000555 Chain = RetVal.getValue(1);
556 InFlag = RetVal.getValue(2);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000557 if (X86ScalarSSE) {
558 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
559 MachineFunction &MF = DAG.getMachineFunction();
560 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
561 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
562 Tys.clear();
563 Tys.push_back(MVT::Other);
564 Ops.clear();
565 Ops.push_back(Chain);
566 Ops.push_back(RetVal);
567 Ops.push_back(StackSlot);
568 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000569 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000570 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
571 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
572 DAG.getSrcValue(NULL));
573 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000574 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000575 break;
576 }
577 }
578 }
579
580 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
581 DAG.getConstant(NumBytes, getPointerTy()),
582 DAG.getConstant(0, getPointerTy()));
583 return std::make_pair(RetVal, Chain);
584 } else {
585 std::vector<SDOperand> Ops;
586 Ops.push_back(Chain);
587 Ops.push_back(Callee);
588 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
589 Ops.push_back(DAG.getConstant(0, getPointerTy()));
590
591 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
592 RetVals, Ops);
593
594 SDOperand ResultVal;
595 switch (RetTyVT) {
596 case MVT::isVoid: break;
597 default:
598 ResultVal = TheCall.getValue(1);
599 break;
600 case MVT::i1:
601 case MVT::i8:
602 case MVT::i16:
603 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
604 break;
605 case MVT::f32:
606 // FIXME: we would really like to remember that this FP_ROUND operation is
607 // okay to eliminate if we allow excess FP precision.
608 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
609 break;
610 case MVT::i64:
611 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
612 TheCall.getValue(2));
613 break;
614 }
615
616 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
617 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000619}
620
621SDOperand
622X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
623 Value *VAListV, SelectionDAG &DAG) {
624 // vastart just stores the address of the VarArgsFrameIndex slot.
625 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
626 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
627 DAG.getSrcValue(VAListV));
628}
629
630
631std::pair<SDOperand,SDOperand>
632X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
633 Value *VAListV, const Type *ArgTy,
634 SelectionDAG &DAG) {
635 MVT::ValueType ArgVT = getValueType(ArgTy);
636 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
637 VAListP, DAG.getSrcValue(VAListV));
638 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
639 DAG.getSrcValue(NULL));
640 unsigned Amt;
641 if (ArgVT == MVT::i32)
642 Amt = 4;
643 else {
644 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
645 "Other types should have been promoted for varargs!");
646 Amt = 8;
647 }
648 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
649 DAG.getConstant(Amt, Val.getValueType()));
650 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
651 Val, VAListP, DAG.getSrcValue(VAListV));
652 return std::make_pair(Result, Chain);
653}
654
655//===----------------------------------------------------------------------===//
656// Fast Calling Convention implementation
657//===----------------------------------------------------------------------===//
658//
659// The X86 'fast' calling convention passes up to two integer arguments in
660// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
661// and requires that the callee pop its arguments off the stack (allowing proper
662// tail calls), and has the same return value conventions as C calling convs.
663//
664// This calling convention always arranges for the callee pop value to be 8n+4
665// bytes, which is needed for tail recursion elimination and stack alignment
666// reasons.
667//
668// Note that this can be enhanced in the future to pass fp vals in registers
669// (when we have a global fp allocator) and do other tricks.
670//
671
672/// AddLiveIn - This helper function adds the specified physical register to the
673/// MachineFunction as a live in value. It also creates a corresponding virtual
674/// register for it.
675static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
676 TargetRegisterClass *RC) {
677 assert(RC->contains(PReg) && "Not the correct regclass!");
678 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
679 MF.addLiveIn(PReg, VReg);
680 return VReg;
681}
682
683
684std::vector<SDOperand>
685X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
686 std::vector<SDOperand> ArgValues;
687
688 MachineFunction &MF = DAG.getMachineFunction();
689 MachineFrameInfo *MFI = MF.getFrameInfo();
690
691 // Add DAG nodes to load the arguments... On entry to a function the stack
692 // frame looks like this:
693 //
694 // [ESP] -- return address
695 // [ESP + 4] -- first nonreg argument (leftmost lexically)
696 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
697 // ...
698 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
699
700 // Keep track of the number of integer regs passed so far. This can be either
701 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
702 // used).
703 unsigned NumIntRegs = 0;
704
705 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
706 MVT::ValueType ObjectVT = getValueType(I->getType());
707 unsigned ArgIncrement = 4;
708 unsigned ObjSize = 0;
709 SDOperand ArgValue;
710
711 switch (ObjectVT) {
712 default: assert(0 && "Unhandled argument type!");
713 case MVT::i1:
714 case MVT::i8:
715 if (NumIntRegs < 2) {
716 if (!I->use_empty()) {
717 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
718 X86::R8RegisterClass);
719 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
720 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000721 if (ObjectVT == MVT::i1)
722 // FIXME: Should insert a assertzext here.
723 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000724 }
725 ++NumIntRegs;
726 break;
727 }
728
729 ObjSize = 1;
730 break;
731 case MVT::i16:
732 if (NumIntRegs < 2) {
733 if (!I->use_empty()) {
734 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
735 X86::R16RegisterClass);
736 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
737 DAG.setRoot(ArgValue.getValue(1));
738 }
739 ++NumIntRegs;
740 break;
741 }
742 ObjSize = 2;
743 break;
744 case MVT::i32:
745 if (NumIntRegs < 2) {
746 if (!I->use_empty()) {
747 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
748 X86::R32RegisterClass);
749 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
750 DAG.setRoot(ArgValue.getValue(1));
751 }
752 ++NumIntRegs;
753 break;
754 }
755 ObjSize = 4;
756 break;
757 case MVT::i64:
758 if (NumIntRegs == 0) {
759 if (!I->use_empty()) {
760 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
761 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
762
763 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
764 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
765 DAG.setRoot(Hi.getValue(1));
766
767 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
768 }
769 NumIntRegs = 2;
770 break;
771 } else if (NumIntRegs == 1) {
772 if (!I->use_empty()) {
773 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
774 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
775 DAG.setRoot(Low.getValue(1));
776
777 // Load the high part from memory.
778 // Create the frame index object for this incoming parameter...
779 int FI = MFI->CreateFixedObject(4, ArgOffset);
780 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
781 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
782 DAG.getSrcValue(NULL));
783 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
784 }
785 ArgOffset += 4;
786 NumIntRegs = 2;
787 break;
788 }
789 ObjSize = ArgIncrement = 8;
790 break;
791 case MVT::f32: ObjSize = 4; break;
792 case MVT::f64: ObjSize = ArgIncrement = 8; break;
793 }
794
795 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
796 // dead loads.
797 if (ObjSize && !I->use_empty()) {
798 // Create the frame index object for this incoming parameter...
799 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
800
801 // Create the SelectionDAG nodes corresponding to a load from this
802 // parameter.
803 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
804
805 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
806 DAG.getSrcValue(NULL));
807 } else if (ArgValue.Val == 0) {
808 if (MVT::isInteger(ObjectVT))
809 ArgValue = DAG.getConstant(0, ObjectVT);
810 else
811 ArgValue = DAG.getConstantFP(0, ObjectVT);
812 }
813 ArgValues.push_back(ArgValue);
814
815 if (ObjSize)
816 ArgOffset += ArgIncrement; // Move on to the next argument.
817 }
818
819 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
820 // arguments and the arguments after the retaddr has been pushed are aligned.
821 if ((ArgOffset & 7) == 0)
822 ArgOffset += 4;
823
824 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
825 ReturnAddrIndex = 0; // No return address slot generated yet.
826 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
827 BytesCallerReserves = 0;
828
829 // Finally, inform the code generator which regs we return values in.
830 switch (getValueType(F.getReturnType())) {
831 default: assert(0 && "Unknown type!");
832 case MVT::isVoid: break;
833 case MVT::i1:
834 case MVT::i8:
835 case MVT::i16:
836 case MVT::i32:
837 MF.addLiveOut(X86::EAX);
838 break;
839 case MVT::i64:
840 MF.addLiveOut(X86::EAX);
841 MF.addLiveOut(X86::EDX);
842 break;
843 case MVT::f32:
844 case MVT::f64:
845 MF.addLiveOut(X86::ST0);
846 break;
847 }
848 return ArgValues;
849}
850
851std::pair<SDOperand, SDOperand>
852X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
853 bool isTailCall, SDOperand Callee,
854 ArgListTy &Args, SelectionDAG &DAG) {
855 // Count how many bytes are to be pushed on the stack.
856 unsigned NumBytes = 0;
857
858 // Keep track of the number of integer regs passed so far. This can be either
859 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
860 // used).
861 unsigned NumIntRegs = 0;
862
863 for (unsigned i = 0, e = Args.size(); i != e; ++i)
864 switch (getValueType(Args[i].second)) {
865 default: assert(0 && "Unknown value type!");
866 case MVT::i1:
867 case MVT::i8:
868 case MVT::i16:
869 case MVT::i32:
870 if (NumIntRegs < 2) {
871 ++NumIntRegs;
872 break;
873 }
874 // fall through
875 case MVT::f32:
876 NumBytes += 4;
877 break;
878 case MVT::i64:
879 if (NumIntRegs == 0) {
880 NumIntRegs = 2;
881 break;
882 } else if (NumIntRegs == 1) {
883 NumIntRegs = 2;
884 NumBytes += 4;
885 break;
886 }
887
888 // fall through
889 case MVT::f64:
890 NumBytes += 8;
891 break;
892 }
893
894 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
895 // arguments and the arguments after the retaddr has been pushed are aligned.
896 if ((NumBytes & 7) == 0)
897 NumBytes += 4;
898
899 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
900 DAG.getConstant(NumBytes, getPointerTy()));
901
902 // Arguments go on the stack in reverse order, as specified by the ABI.
903 unsigned ArgOffset = 0;
904 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
905 X86::ESP, MVT::i32);
906 NumIntRegs = 0;
907 std::vector<SDOperand> Stores;
908 std::vector<SDOperand> RegValuesToPass;
909 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
910 switch (getValueType(Args[i].second)) {
911 default: assert(0 && "Unexpected ValueType for argument!");
912 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000913 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
914 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000915 case MVT::i8:
916 case MVT::i16:
917 case MVT::i32:
918 if (NumIntRegs < 2) {
919 RegValuesToPass.push_back(Args[i].first);
920 ++NumIntRegs;
921 break;
922 }
923 // Fall through
924 case MVT::f32: {
925 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
926 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
927 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
928 Args[i].first, PtrOff,
929 DAG.getSrcValue(NULL)));
930 ArgOffset += 4;
931 break;
932 }
933 case MVT::i64:
934 if (NumIntRegs < 2) { // Can pass part of it in regs?
935 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
936 Args[i].first, DAG.getConstant(1, MVT::i32));
937 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
938 Args[i].first, DAG.getConstant(0, MVT::i32));
939 RegValuesToPass.push_back(Lo);
940 ++NumIntRegs;
941 if (NumIntRegs < 2) { // Pass both parts in regs?
942 RegValuesToPass.push_back(Hi);
943 ++NumIntRegs;
944 } else {
945 // Pass the high part in memory.
946 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
947 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
948 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
949 Hi, PtrOff, DAG.getSrcValue(NULL)));
950 ArgOffset += 4;
951 }
952 break;
953 }
954 // Fall through
955 case MVT::f64:
956 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
957 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
958 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
959 Args[i].first, PtrOff,
960 DAG.getSrcValue(NULL)));
961 ArgOffset += 8;
962 break;
963 }
964 }
965 if (!Stores.empty())
966 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
967
968 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
969 // arguments and the arguments after the retaddr has been pushed are aligned.
970 if ((ArgOffset & 7) == 0)
971 ArgOffset += 4;
972
973 std::vector<MVT::ValueType> RetVals;
974 MVT::ValueType RetTyVT = getValueType(RetTy);
975
976 RetVals.push_back(MVT::Other);
977
978 // The result values produced have to be legal. Promote the result.
979 switch (RetTyVT) {
980 case MVT::isVoid: break;
981 default:
982 RetVals.push_back(RetTyVT);
983 break;
984 case MVT::i1:
985 case MVT::i8:
986 case MVT::i16:
987 RetVals.push_back(MVT::i32);
988 break;
989 case MVT::f32:
990 if (X86ScalarSSE)
991 RetVals.push_back(MVT::f32);
992 else
993 RetVals.push_back(MVT::f64);
994 break;
995 case MVT::i64:
996 RetVals.push_back(MVT::i32);
997 RetVals.push_back(MVT::i32);
998 break;
999 }
1000
Evan Chengd9558e02006-01-06 00:43:03 +00001001 if (X86DAGIsel) {
1002 // Build a sequence of copy-to-reg nodes chained together with token chain
1003 // and flag operands which copy the outgoing args into registers.
1004 SDOperand InFlag;
1005 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1006 unsigned CCReg;
1007 SDOperand RegToPass = RegValuesToPass[i];
1008 switch (RegToPass.getValueType()) {
1009 default: assert(0 && "Bad thing to pass in regs");
1010 case MVT::i8:
1011 CCReg = (i == 0) ? X86::AL : X86::DL;
1012 break;
1013 case MVT::i16:
1014 CCReg = (i == 0) ? X86::AX : X86::DX;
1015 break;
1016 case MVT::i32:
1017 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1018 break;
1019 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001020
Evan Chengd9558e02006-01-06 00:43:03 +00001021 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1022 InFlag = Chain.getValue(1);
1023 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001024
Evan Chengd9558e02006-01-06 00:43:03 +00001025 std::vector<MVT::ValueType> NodeTys;
1026 NodeTys.push_back(MVT::Other); // Returns a chain
1027 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd9558e02006-01-06 00:43:03 +00001028 std::vector<SDOperand> Ops;
1029 Ops.push_back(Chain);
1030 Ops.push_back(Callee);
1031 if (InFlag.Val)
1032 Ops.push_back(InFlag);
1033
1034 // FIXME: Do not generate X86ISD::TAILCALL for now.
1035 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1036 InFlag = Chain.getValue(1);
1037
1038 SDOperand RetVal;
1039 if (RetTyVT != MVT::isVoid) {
1040 switch (RetTyVT) {
1041 default: assert(0 && "Unknown value type to return!");
1042 case MVT::i1:
1043 case MVT::i8:
1044 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1045 Chain = RetVal.getValue(1);
1046 break;
1047 case MVT::i16:
1048 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1049 Chain = RetVal.getValue(1);
1050 break;
1051 case MVT::i32:
1052 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1053 Chain = RetVal.getValue(1);
1054 break;
1055 case MVT::i64: {
1056 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1057 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1058 Lo.getValue(2));
1059 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1060 Chain = Hi.getValue(1);
1061 break;
1062 }
Evan Chengd9558e02006-01-06 00:43:03 +00001063 case MVT::f64: {
1064 std::vector<MVT::ValueType> Tys;
1065 Tys.push_back(MVT::f64);
1066 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001067 Tys.push_back(MVT::Flag);
Evan Chengd9558e02006-01-06 00:43:03 +00001068 std::vector<SDOperand> Ops;
1069 Ops.push_back(Chain);
1070 Ops.push_back(InFlag);
1071 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001072 Chain = RetVal.getValue(1);
1073 InFlag = RetVal.getValue(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001074 if (X86ScalarSSE) {
1075 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1076 MachineFunction &MF = DAG.getMachineFunction();
1077 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1078 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1079 Tys.clear();
1080 Tys.push_back(MVT::Other);
1081 Ops.clear();
1082 Ops.push_back(Chain);
1083 Ops.push_back(RetVal);
1084 Ops.push_back(StackSlot);
1085 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001086 Ops.push_back(InFlag);
Evan Chengd9558e02006-01-06 00:43:03 +00001087 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1088 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1089 DAG.getSrcValue(NULL));
1090 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001091 }
Evan Chengd9558e02006-01-06 00:43:03 +00001092 break;
1093 }
1094 }
1095 }
1096
1097 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1098 DAG.getConstant(ArgOffset, getPointerTy()),
1099 DAG.getConstant(ArgOffset, getPointerTy()));
1100 return std::make_pair(RetVal, Chain);
1101 } else {
1102 std::vector<SDOperand> Ops;
1103 Ops.push_back(Chain);
1104 Ops.push_back(Callee);
1105 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1106 // Callee pops all arg values on the stack.
1107 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1108
1109 // Pass register arguments as needed.
1110 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1111
1112 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1113 RetVals, Ops);
1114 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1115
1116 SDOperand ResultVal;
1117 switch (RetTyVT) {
1118 case MVT::isVoid: break;
1119 default:
1120 ResultVal = TheCall.getValue(1);
1121 break;
1122 case MVT::i1:
1123 case MVT::i8:
1124 case MVT::i16:
1125 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1126 break;
1127 case MVT::f32:
1128 // FIXME: we would really like to remember that this FP_ROUND operation is
1129 // okay to eliminate if we allow excess FP precision.
1130 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1131 break;
1132 case MVT::i64:
1133 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1134 TheCall.getValue(2));
1135 break;
1136 }
1137
1138 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001139 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001140}
1141
1142SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1143 if (ReturnAddrIndex == 0) {
1144 // Set up a frame object for the return address.
1145 MachineFunction &MF = DAG.getMachineFunction();
1146 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1147 }
1148
1149 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1150}
1151
1152
1153
1154std::pair<SDOperand, SDOperand> X86TargetLowering::
1155LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1156 SelectionDAG &DAG) {
1157 SDOperand Result;
1158 if (Depth) // Depths > 0 not supported yet!
1159 Result = DAG.getConstant(0, getPointerTy());
1160 else {
1161 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1162 if (!isFrameAddress)
1163 // Just load the return address
1164 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1165 DAG.getSrcValue(NULL));
1166 else
1167 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1168 DAG.getConstant(4, MVT::i32));
1169 }
1170 return std::make_pair(Result, Chain);
1171}
1172
Evan Cheng4a460802006-01-11 00:33:36 +00001173/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1174/// which corresponds to the condition code.
1175static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1176 switch (X86CC) {
1177 default: assert(0 && "Unknown X86 conditional code!");
1178 case X86ISD::COND_A: return X86::JA;
1179 case X86ISD::COND_AE: return X86::JAE;
1180 case X86ISD::COND_B: return X86::JB;
1181 case X86ISD::COND_BE: return X86::JBE;
1182 case X86ISD::COND_E: return X86::JE;
1183 case X86ISD::COND_G: return X86::JG;
1184 case X86ISD::COND_GE: return X86::JGE;
1185 case X86ISD::COND_L: return X86::JL;
1186 case X86ISD::COND_LE: return X86::JLE;
1187 case X86ISD::COND_NE: return X86::JNE;
1188 case X86ISD::COND_NO: return X86::JNO;
1189 case X86ISD::COND_NP: return X86::JNP;
1190 case X86ISD::COND_NS: return X86::JNS;
1191 case X86ISD::COND_O: return X86::JO;
1192 case X86ISD::COND_P: return X86::JP;
1193 case X86ISD::COND_S: return X86::JS;
1194 }
1195}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001196
Evan Cheng4a460802006-01-11 00:33:36 +00001197/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1198/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Chengd9558e02006-01-06 00:43:03 +00001199/// do a direct translation.
Evan Cheng4a460802006-01-11 00:33:36 +00001200static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Chengd9558e02006-01-06 00:43:03 +00001201 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1202 unsigned X86CC = X86ISD::COND_INVALID;
1203 if (!isFP) {
1204 switch (SetCCOpcode) {
1205 default: break;
1206 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1207 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1208 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1209 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1210 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1211 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1212 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1213 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1214 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1215 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1216 }
1217 } else {
1218 // On a floating point condition, the flags are set as follows:
1219 // ZF PF CF op
1220 // 0 | 0 | 0 | X > Y
1221 // 0 | 0 | 1 | X < Y
1222 // 1 | 0 | 0 | X == Y
1223 // 1 | 1 | 1 | unordered
1224 switch (SetCCOpcode) {
1225 default: break;
1226 case ISD::SETUEQ:
1227 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1228 case ISD::SETOGT:
1229 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1230 case ISD::SETOGE:
1231 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1232 case ISD::SETULT:
1233 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1234 case ISD::SETULE:
1235 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1236 case ISD::SETONE:
1237 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1238 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1239 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1240 }
1241 }
1242 return X86CC;
1243}
1244
Evan Cheng4a460802006-01-11 00:33:36 +00001245/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1246/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001247/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001248static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001249 switch (X86CC) {
1250 default:
1251 return false;
1252 case X86ISD::COND_B:
1253 case X86ISD::COND_BE:
1254 case X86ISD::COND_E:
1255 case X86ISD::COND_P:
1256 case X86ISD::COND_A:
1257 case X86ISD::COND_AE:
1258 case X86ISD::COND_NE:
1259 case X86ISD::COND_NP:
1260 return true;
1261 }
1262}
1263
Evan Cheng4a460802006-01-11 00:33:36 +00001264MachineBasicBlock *
1265X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1266 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001267 switch (MI->getOpcode()) {
1268 default: assert(false && "Unexpected instr type to insert");
1269 case X86::CMOV_FR32:
1270 case X86::CMOV_FR64: {
1271 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1272 // control-flow pattern. The incoming instruction knows the destination vreg
1273 // to set, the condition code register to branch on, the true/false values to
1274 // select between, and a branch opcode to use.
1275 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1276 ilist<MachineBasicBlock>::iterator It = BB;
1277 ++It;
1278
1279 // thisMBB:
1280 // ...
1281 // TrueVal = ...
1282 // cmpTY ccX, r1, r2
1283 // bCC copy1MBB
1284 // fallthrough --> copy0MBB
1285 MachineBasicBlock *thisMBB = BB;
1286 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1287 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1288 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1289 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1290 MachineFunction *F = BB->getParent();
1291 F->getBasicBlockList().insert(It, copy0MBB);
1292 F->getBasicBlockList().insert(It, sinkMBB);
1293 // Update machine-CFG edges
1294 BB->addSuccessor(copy0MBB);
1295 BB->addSuccessor(sinkMBB);
1296
1297 // copy0MBB:
1298 // %FalseValue = ...
1299 // # fallthrough to sinkMBB
1300 BB = copy0MBB;
1301
1302 // Update machine-CFG edges
1303 BB->addSuccessor(sinkMBB);
1304
1305 // sinkMBB:
1306 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1307 // ...
1308 BB = sinkMBB;
1309 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1310 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1311 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001312
Evan Cheng0cc39452006-01-16 21:21:29 +00001313 delete MI; // The pseudo instruction is gone now.
1314 return BB;
1315 }
Evan Cheng4a460802006-01-11 00:33:36 +00001316
Evan Cheng0cc39452006-01-16 21:21:29 +00001317 case X86::FP_TO_INT16_IN_MEM:
1318 case X86::FP_TO_INT32_IN_MEM:
1319 case X86::FP_TO_INT64_IN_MEM: {
1320 // Change the floating point control register to use "round towards zero"
1321 // mode when truncating to an integer value.
1322 MachineFunction *F = BB->getParent();
1323 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1324 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1325
1326 // Load the old value of the high byte of the control word...
1327 unsigned OldCW =
1328 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1329 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1330
1331 // Set the high part to be round to zero...
1332 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1333
1334 // Reload the modified control word now...
1335 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1336
1337 // Restore the memory image of control word to original value
1338 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1339
1340 // Get the X86 opcode to use.
1341 unsigned Opc;
1342 switch (MI->getOpcode()) {
1343 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1344 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1345 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1346 }
1347
1348 X86AddressMode AM;
1349 MachineOperand &Op = MI->getOperand(0);
1350 if (Op.isRegister()) {
1351 AM.BaseType = X86AddressMode::RegBase;
1352 AM.Base.Reg = Op.getReg();
1353 } else {
1354 AM.BaseType = X86AddressMode::FrameIndexBase;
1355 AM.Base.FrameIndex = Op.getFrameIndex();
1356 }
1357 Op = MI->getOperand(1);
1358 if (Op.isImmediate())
1359 AM.Scale = Op.getImmedValue();
1360 Op = MI->getOperand(2);
1361 if (Op.isImmediate())
1362 AM.IndexReg = Op.getImmedValue();
1363 Op = MI->getOperand(3);
1364 if (Op.isGlobalAddress()) {
1365 AM.GV = Op.getGlobal();
1366 } else {
1367 AM.Disp = Op.getImmedValue();
1368 }
1369 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1370
1371 // Reload the original control word now.
1372 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1373
1374 delete MI; // The pseudo instruction is gone now.
1375 return BB;
1376 }
1377 }
Evan Cheng4a460802006-01-11 00:33:36 +00001378}
1379
1380
1381//===----------------------------------------------------------------------===//
1382// X86 Custom Lowering Hooks
1383//===----------------------------------------------------------------------===//
1384
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001385/// LowerOperation - Provide custom lowering hooks for some operations.
1386///
1387SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1388 switch (Op.getOpcode()) {
1389 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001390 case ISD::ADD_PARTS:
1391 case ISD::SUB_PARTS: {
1392 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1393 "Not an i64 add/sub!");
1394 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1395 std::vector<MVT::ValueType> Tys;
1396 Tys.push_back(MVT::i32);
1397 Tys.push_back(MVT::Flag);
1398 std::vector<SDOperand> Ops;
1399 Ops.push_back(Op.getOperand(0));
1400 Ops.push_back(Op.getOperand(2));
1401 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1402 Tys, Ops);
1403 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1404 Op.getOperand(1), Op.getOperand(3),
1405 Lo.getValue(1));
1406 Tys.clear();
1407 Tys.push_back(MVT::i32);
1408 Tys.push_back(MVT::i32);
1409 Ops.clear();
1410 Ops.push_back(Lo);
1411 Ops.push_back(Hi);
1412 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1413 }
1414 case ISD::SHL_PARTS:
1415 case ISD::SRA_PARTS:
1416 case ISD::SRL_PARTS: {
1417 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1418 "Not an i64 shift!");
1419 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1420 SDOperand ShOpLo = Op.getOperand(0);
1421 SDOperand ShOpHi = Op.getOperand(1);
1422 SDOperand ShAmt = Op.getOperand(2);
1423 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng910cd3c2006-01-09 22:29:54 +00001424 DAG.getConstant(31, MVT::i32))
Evan Chenge3413162006-01-09 18:33:28 +00001425 : DAG.getConstant(0, MVT::i32);
1426
1427 SDOperand Tmp2, Tmp3;
1428 if (Op.getOpcode() == ISD::SHL_PARTS) {
1429 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1430 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1431 } else {
1432 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1433 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SHL, MVT::i32, ShOpHi, ShAmt);
1434 }
1435
1436 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1437 ShAmt, DAG.getConstant(32, MVT::i8));
1438
1439 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001440 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001441
1442 std::vector<MVT::ValueType> Tys;
1443 Tys.push_back(MVT::i32);
1444 Tys.push_back(MVT::Flag);
1445 std::vector<SDOperand> Ops;
1446 if (Op.getOpcode() == ISD::SHL_PARTS) {
1447 Ops.push_back(Tmp2);
1448 Ops.push_back(Tmp3);
1449 Ops.push_back(CC);
1450 Ops.push_back(InFlag);
1451 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1452 InFlag = Hi.getValue(1);
1453
1454 Ops.clear();
1455 Ops.push_back(Tmp3);
1456 Ops.push_back(Tmp1);
1457 Ops.push_back(CC);
1458 Ops.push_back(InFlag);
1459 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1460 } else {
1461 Ops.push_back(Tmp2);
1462 Ops.push_back(Tmp3);
1463 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001464 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001465 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1466 InFlag = Lo.getValue(1);
1467
1468 Ops.clear();
1469 Ops.push_back(Tmp3);
1470 Ops.push_back(Tmp1);
1471 Ops.push_back(CC);
1472 Ops.push_back(InFlag);
1473 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1474 }
1475
1476 Tys.clear();
1477 Tys.push_back(MVT::i32);
1478 Tys.push_back(MVT::i32);
1479 Ops.clear();
1480 Ops.push_back(Lo);
1481 Ops.push_back(Hi);
1482 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1483 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001484 case ISD::SINT_TO_FP: {
1485 assert(Op.getValueType() == MVT::f64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001486 Op.getOperand(0).getValueType() <= MVT::i64 &&
1487 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001488 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001489
1490 SDOperand Result;
1491 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1492 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001493 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001494 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001495 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001496 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1497 DAG.getEntryNode(), Op.getOperand(0),
1498 StackSlot, DAG.getSrcValue(NULL));
1499
1500 // Build the FILD
1501 std::vector<MVT::ValueType> Tys;
1502 Tys.push_back(MVT::f64);
1503 Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001504 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001505 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001506 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001507 Ops.push_back(DAG.getValueType(SrcVT));
1508 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
1509 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001510 }
1511 case ISD::FP_TO_SINT: {
1512 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1513 Op.getOperand(0).getValueType() == MVT::f64 &&
1514 "Unknown FP_TO_SINT to lower!");
1515 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1516 // stack slot.
1517 MachineFunction &MF = DAG.getMachineFunction();
1518 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1519 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1520 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1521
1522 unsigned Opc;
1523 switch (Op.getValueType()) {
1524 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1525 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1526 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1527 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1528 }
1529
1530 // Build the FP_TO_INT*_IN_MEM
1531 std::vector<SDOperand> Ops;
1532 Ops.push_back(DAG.getEntryNode());
1533 Ops.push_back(Op.getOperand(0));
1534 Ops.push_back(StackSlot);
1535 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1536
1537 // Load the result.
1538 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1539 DAG.getSrcValue(NULL));
1540 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001541 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001542 std::vector<MVT::ValueType> Tys;
1543 Tys.push_back(MVT::Other);
1544 Tys.push_back(MVT::Flag);
1545 std::vector<SDOperand> Ops;
1546 Ops.push_back(Op.getOperand(0));
1547 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001548 Ops.clear();
1549 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1550 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1551 MVT::i32, Ops[0].getValue(2)));
1552 Ops.push_back(Ops[1].getValue(1));
1553 Tys[0] = Tys[1] = MVT::i32;
1554 Tys.push_back(MVT::Other);
1555 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001556 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001557 case ISD::SETCC: {
1558 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1559 SDOperand CC = Op.getOperand(2);
1560 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1561 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001562 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1563 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng4a460802006-01-11 00:33:36 +00001564 unsigned X86CC = getX86CC(CC, isFP);
Evan Chengd9558e02006-01-06 00:43:03 +00001565 if (X86CC != X86ISD::COND_INVALID) {
1566 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1567 DAG.getConstant(X86CC, MVT::i8), Cond);
1568 } else {
1569 assert(isFP && "Illegal integer SetCC!");
1570
1571 std::vector<MVT::ValueType> Tys;
1572 std::vector<SDOperand> Ops;
1573 switch (SetCCOpcode) {
1574 default: assert(false && "Illegal floating point SetCC!");
1575 case ISD::SETOEQ: { // !PF & ZF
1576 Tys.push_back(MVT::i8);
1577 Tys.push_back(MVT::Flag);
1578 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1579 Ops.push_back(Cond);
1580 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1581 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1582 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1583 Tmp1.getValue(1));
1584 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1585 }
1586 case ISD::SETOLT: { // !PF & CF
1587 Tys.push_back(MVT::i8);
1588 Tys.push_back(MVT::Flag);
1589 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1590 Ops.push_back(Cond);
1591 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1592 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1593 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1594 Tmp1.getValue(1));
1595 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1596 }
1597 case ISD::SETOLE: { // !PF & (CF || ZF)
1598 Tys.push_back(MVT::i8);
1599 Tys.push_back(MVT::Flag);
1600 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1601 Ops.push_back(Cond);
1602 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1603 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1604 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1605 Tmp1.getValue(1));
1606 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1607 }
1608 case ISD::SETUGT: { // PF | (!ZF & !CF)
1609 Tys.push_back(MVT::i8);
1610 Tys.push_back(MVT::Flag);
1611 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1612 Ops.push_back(Cond);
1613 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1614 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1615 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1616 Tmp1.getValue(1));
1617 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1618 }
1619 case ISD::SETUGE: { // PF | !CF
1620 Tys.push_back(MVT::i8);
1621 Tys.push_back(MVT::Flag);
1622 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1623 Ops.push_back(Cond);
1624 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1625 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1626 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1627 Tmp1.getValue(1));
1628 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1629 }
1630 case ISD::SETUNE: { // PF | !ZF
1631 Tys.push_back(MVT::i8);
1632 Tys.push_back(MVT::Flag);
1633 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1634 Ops.push_back(Cond);
1635 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1636 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1637 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1638 Tmp1.getValue(1));
1639 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1640 }
1641 }
1642 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001643 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001644 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001645 MVT::ValueType VT = Op.getValueType();
1646 bool isFP = MVT::isFloatingPoint(VT);
1647 bool isFPStack = isFP && (X86Vector < SSE2);
1648 bool isFPSSE = isFP && (X86Vector >= SSE2);
Evan Cheng1bcee362006-01-13 01:03:02 +00001649 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001650 SDOperand Op0 = Op.getOperand(0);
1651 SDOperand Cond, CC;
1652 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001653 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1654 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1655 // have another use it will be eliminated.
1656 // If the X86ISD::SETCC has more than one use, then it's probably better
1657 // to use a test instead of duplicating the X86ISD::CMP (for register
1658 // pressure reason).
Evan Cheng80ebe382006-01-13 01:17:24 +00001659 if (Op0.hasOneUse() && Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001660 CC = Op0.getOperand(0);
1661 Cond = Op0.getOperand(1);
1662 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001663 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001664 } else
1665 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001666 } else if (Op0.getOpcode() == ISD::SETCC) {
1667 CC = Op0.getOperand(2);
1668 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng4a460802006-01-11 00:33:36 +00001669 unsigned X86CC = getX86CC(CC, isFP);
Evan Chengd9558e02006-01-06 00:43:03 +00001670 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng7df96d62005-12-17 01:21:05 +00001671 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaaca22c2006-01-10 20:26:56 +00001672 Op0.getOperand(0), Op0.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001673 addTest = true;
1674 } else
1675 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001676
Evan Cheng189d01e2006-01-13 01:06:49 +00001677 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001678 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001679 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001680 }
Evan Chenge3413162006-01-09 18:33:28 +00001681
1682 std::vector<MVT::ValueType> Tys;
1683 Tys.push_back(Op.getValueType());
1684 Tys.push_back(MVT::Flag);
1685 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001686 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1687 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001688 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001689 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001690 Ops.push_back(CC);
1691 Ops.push_back(Cond);
1692 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001693 }
Evan Cheng898101c2005-12-19 23:12:38 +00001694 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001695 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001696 SDOperand Cond = Op.getOperand(1);
1697 SDOperand Dest = Op.getOperand(2);
1698 SDOperand CC;
Evan Chengd5781fc2005-12-21 20:21:51 +00001699 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001700 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1701 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1702 // have another use it will be eliminated.
1703 // If the X86ISD::SETCC has more than one use, then it's probably better
1704 // to use a test instead of duplicating the X86ISD::CMP (for register
1705 // pressure reason).
1706 if (Cond.hasOneUse() && Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1707 CC = Cond.getOperand(0);
1708 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1709 Cond.getOperand(1).getOperand(0),
1710 Cond.getOperand(1).getOperand(1));
1711 } else
1712 addTest = true;
Evan Chengd5781fc2005-12-21 20:21:51 +00001713 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng898101c2005-12-19 23:12:38 +00001714 CC = Cond.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001715 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng4a460802006-01-11 00:33:36 +00001716 unsigned X86CC = getX86CC(CC, isFP);
Evan Chengd9558e02006-01-06 00:43:03 +00001717 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001718 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1719 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001720 } else
1721 addTest = true;
1722
1723 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001724 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001725 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1726 }
1727 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1728 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1729 }
Evan Chengd9558e02006-01-06 00:43:03 +00001730 case ISD::RET: {
1731 // Can only be return void.
Evan Chenge3413162006-01-09 18:33:28 +00001732 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chengd9558e02006-01-06 00:43:03 +00001733 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1734 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001735 case ISD::MEMSET: {
1736 SDOperand InFlag;
1737 SDOperand Chain = Op.getOperand(0);
1738 unsigned Align =
1739 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1740 if (Align == 0) Align = 1;
1741
1742 MVT::ValueType AVT;
1743 SDOperand Count;
1744 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1745 unsigned ValReg;
1746 unsigned Val = ValC->getValue() & 255;
1747
1748 // If the value is a constant, then we can potentially use larger sets.
1749 switch (Align & 3) {
1750 case 2: // WORD aligned
1751 AVT = MVT::i16;
1752 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1753 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1754 else
1755 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1756 DAG.getConstant(1, MVT::i8));
1757 Val = (Val << 8) | Val;
1758 ValReg = X86::AX;
1759 break;
1760 case 0: // DWORD aligned
1761 AVT = MVT::i32;
1762 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1763 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1764 else
1765 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1766 DAG.getConstant(2, MVT::i8));
1767 Val = (Val << 8) | Val;
1768 Val = (Val << 16) | Val;
1769 ValReg = X86::EAX;
1770 break;
1771 default: // Byte aligned
1772 AVT = MVT::i8;
1773 Count = Op.getOperand(3);
1774 ValReg = X86::AL;
1775 break;
1776 }
1777
1778 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1779 InFlag);
1780 InFlag = Chain.getValue(1);
1781 } else {
1782 AVT = MVT::i8;
1783 Count = Op.getOperand(3);
1784 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1785 InFlag = Chain.getValue(1);
1786 }
1787
1788 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1789 InFlag = Chain.getValue(1);
1790 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1791 InFlag = Chain.getValue(1);
1792
1793 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1794 DAG.getValueType(AVT), InFlag);
1795 }
1796 case ISD::MEMCPY: {
1797 SDOperand Chain = Op.getOperand(0);
1798 unsigned Align =
1799 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1800 if (Align == 0) Align = 1;
1801
1802 MVT::ValueType AVT;
1803 SDOperand Count;
1804 switch (Align & 3) {
1805 case 2: // WORD aligned
1806 AVT = MVT::i16;
1807 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1808 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1809 else
1810 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1811 DAG.getConstant(1, MVT::i8));
1812 break;
1813 case 0: // DWORD aligned
1814 AVT = MVT::i32;
1815 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1816 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1817 else
1818 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1819 DAG.getConstant(2, MVT::i8));
1820 break;
1821 default: // Byte aligned
1822 AVT = MVT::i8;
1823 Count = Op.getOperand(3);
1824 break;
1825 }
1826
1827 SDOperand InFlag;
1828 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1829 InFlag = Chain.getValue(1);
1830 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1831 InFlag = Chain.getValue(1);
1832 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1833 InFlag = Chain.getValue(1);
1834
1835 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1836 DAG.getValueType(AVT), InFlag);
1837 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001838 case ISD::GlobalAddress: {
Evan Cheng002fe9b2006-01-12 07:56:47 +00001839 SDOperand Result;
Evan Chengb077b842005-12-21 02:39:21 +00001840 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1841 // For Darwin, external and weak symbols are indirect, so we want to load
1842 // the value at address GV, not the value of GV itself. This means that
1843 // the GlobalAddress must be in the base or index register of the address,
1844 // not the GV offset field.
1845 if (getTargetMachine().
1846 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1847 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng002fe9b2006-01-12 07:56:47 +00001848 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1849 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1850 DAG.getSrcValue(NULL));
1851 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001852 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001853 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001854}
Evan Cheng72261582005-12-20 06:22:03 +00001855
1856const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1857 switch (Opcode) {
1858 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001859 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1860 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1861 case X86ISD::ADC: return "X86ISD::ADC";
1862 case X86ISD::SBB: return "X86ISD::SBB";
1863 case X86ISD::SHLD: return "X86ISD::SHLD";
1864 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chenga3195e82006-01-12 22:54:21 +00001865 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng72261582005-12-20 06:22:03 +00001866 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1867 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1868 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001869 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001870 case X86ISD::FST: return "X86ISD::FST";
1871 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001872 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001873 case X86ISD::CALL: return "X86ISD::CALL";
1874 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1875 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1876 case X86ISD::CMP: return "X86ISD::CMP";
1877 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001878 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001879 case X86ISD::CMOV: return "X86ISD::CMOV";
1880 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001881 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng67f92a72006-01-11 22:15:48 +00001882 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1883 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng72261582005-12-20 06:22:03 +00001884 }
1885}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001886
1887bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1888 uint64_t Mask) const {
1889
1890 unsigned Opc = Op.getOpcode();
1891
1892 switch (Opc) {
1893 default:
1894 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1895 break;
1896 case X86ISD::SETCC: return (Mask & 1) == 0;
1897 }
1898
1899 return false;
1900}