blob: c83e6d058cc3858ba66eed5ba4b2006a571d1603 [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);
Evan Cheng6892f282006-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 Lattnerdbdbf0c2005-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 Lattner21f66852005-12-23 05:15:23 +000091 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
92 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
93
Evan Cheng898101c2005-12-19 23:12:38 +000094 if (X86DAGIsel) {
95 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
96 }
Chris Lattnerdbdbf0c2005-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 Lattnere80242a2005-12-07 17:59:14 +0000101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-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 Lenharthb873ff32005-11-20 21:41:10 +0000115 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000116
Evan Chengeb422a72006-01-11 23:20:05 +0000117 if (!X86DAGIsel) {
Nate Begemand88fc032006-01-14 03:14:10 +0000118 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Chengeb422a72006-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 Begemand88fc032006-01-14 03:14:10 +0000126 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000127
Chris Lattnerdbdbf0c2005-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 Cheng7df96d62005-12-17 01:21:05 +0000140 if (X86DAGIsel) {
Evan Chengd9558e02006-01-06 00:43:03 +0000141 // X86 wants to expand cmov itself.
Evan Cheng7df96d62005-12-17 01:21:05 +0000142 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
143 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Chengd9558e02006-01-06 00:43:03 +0000144 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
145 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengd5781fc2005-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 Chengd9558e02006-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 Cheng3a03ebb2005-12-21 23:05:39 +0000154 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge3413162006-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 Cheng67f92a72006-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 Cheng7df96d62005-12-17 01:21:05 +0000164 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165
Chris Lattnerf73bae12005-11-29 06:16:21 +0000166 // We don't have line number support yet.
167 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000168 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
169 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000170
Chris Lattnere1125522006-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 Lattnerb99329e2006-01-13 02:42:53 +0000175
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000176 if (X86ScalarSSE) {
177 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000178 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
179 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-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 Chenga3195e82006-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 Lattnerdbdbf0c2005-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 Chengd9558e02006-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 Cheng8700e142006-01-11 06:09:51 +0000254 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
255 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000256
Chris Lattnerdbdbf0c2005-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 Chengb077b842005-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 Chengd9558e02006-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 Chengb077b842005-12-21 02:39:21 +0000281 break;
282 }
283 case MVT::f32:
Evan Chengb077b842005-12-21 02:39:21 +0000284 case MVT::f64:
285 if (!X86ScalarSSE) {
286 std::vector<MVT::ValueType> Tys;
287 Tys.push_back(MVT::Other);
288 Tys.push_back(MVT::Flag);
289 std::vector<SDOperand> Ops;
290 Ops.push_back(Chain);
291 Ops.push_back(Op);
292 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
293 } else {
294 // Spill the value to memory and reload it into top of stack.
295 unsigned Size = MVT::getSizeInBits(OpVT)/8;
296 MachineFunction &MF = DAG.getMachineFunction();
297 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
298 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
299 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
300 StackSlot, DAG.getSrcValue(NULL));
301 std::vector<MVT::ValueType> Tys;
302 Tys.push_back(MVT::f64);
303 Tys.push_back(MVT::Other);
304 std::vector<SDOperand> Ops;
305 Ops.push_back(Chain);
306 Ops.push_back(StackSlot);
307 Ops.push_back(DAG.getValueType(OpVT));
308 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
309 Tys.clear();
310 Tys.push_back(MVT::Other);
311 Tys.push_back(MVT::Flag);
312 Ops.clear();
313 Ops.push_back(Copy.getValue(1));
314 Ops.push_back(Copy);
315 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
316 }
317 break;
318 }
Evan Chengd5781fc2005-12-21 20:21:51 +0000319
320 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
321 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
322 Copy.getValue(1));
Evan Chengb077b842005-12-21 02:39:21 +0000323}
324
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325//===----------------------------------------------------------------------===//
326// C Calling Convention implementation
327//===----------------------------------------------------------------------===//
328
329std::vector<SDOperand>
330X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
331 std::vector<SDOperand> ArgValues;
332
333 MachineFunction &MF = DAG.getMachineFunction();
334 MachineFrameInfo *MFI = MF.getFrameInfo();
335
336 // Add DAG nodes to load the arguments... On entry to a function on the X86,
337 // the stack frame looks like this:
338 //
339 // [ESP] -- return address
340 // [ESP + 4] -- first argument (leftmost lexically)
341 // [ESP + 8] -- second argument, if first argument is four bytes in size
342 // ...
343 //
344 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
345 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
346 MVT::ValueType ObjectVT = getValueType(I->getType());
347 unsigned ArgIncrement = 4;
348 unsigned ObjSize;
349 switch (ObjectVT) {
350 default: assert(0 && "Unhandled argument type!");
351 case MVT::i1:
352 case MVT::i8: ObjSize = 1; break;
353 case MVT::i16: ObjSize = 2; break;
354 case MVT::i32: ObjSize = 4; break;
355 case MVT::i64: ObjSize = ArgIncrement = 8; break;
356 case MVT::f32: ObjSize = 4; break;
357 case MVT::f64: ObjSize = ArgIncrement = 8; break;
358 }
359 // Create the frame index object for this incoming parameter...
360 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
361
362 // Create the SelectionDAG nodes corresponding to a load from this parameter
363 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
364
365 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
366 // dead loads.
367 SDOperand ArgValue;
368 if (!I->use_empty())
369 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
370 DAG.getSrcValue(NULL));
371 else {
372 if (MVT::isInteger(ObjectVT))
373 ArgValue = DAG.getConstant(0, ObjectVT);
374 else
375 ArgValue = DAG.getConstantFP(0, ObjectVT);
376 }
377 ArgValues.push_back(ArgValue);
378
379 ArgOffset += ArgIncrement; // Move on to the next argument...
380 }
381
382 // If the function takes variable number of arguments, make a frame index for
383 // the start of the first vararg value... for expansion of llvm.va_start.
384 if (F.isVarArg())
385 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
386 ReturnAddrIndex = 0; // No return address slot generated yet.
387 BytesToPopOnReturn = 0; // Callee pops nothing.
388 BytesCallerReserves = ArgOffset;
389
390 // Finally, inform the code generator which regs we return values in.
391 switch (getValueType(F.getReturnType())) {
392 default: assert(0 && "Unknown type!");
393 case MVT::isVoid: break;
394 case MVT::i1:
395 case MVT::i8:
396 case MVT::i16:
397 case MVT::i32:
398 MF.addLiveOut(X86::EAX);
399 break;
400 case MVT::i64:
401 MF.addLiveOut(X86::EAX);
402 MF.addLiveOut(X86::EDX);
403 break;
404 case MVT::f32:
405 case MVT::f64:
406 MF.addLiveOut(X86::ST0);
407 break;
408 }
409 return ArgValues;
410}
411
412std::pair<SDOperand, SDOperand>
413X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
414 bool isVarArg, bool isTailCall,
415 SDOperand Callee, ArgListTy &Args,
416 SelectionDAG &DAG) {
417 // Count how many bytes are to be pushed on the stack.
418 unsigned NumBytes = 0;
419
420 if (Args.empty()) {
421 // Save zero bytes.
422 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
423 DAG.getConstant(0, getPointerTy()));
424 } else {
425 for (unsigned i = 0, e = Args.size(); i != e; ++i)
426 switch (getValueType(Args[i].second)) {
427 default: assert(0 && "Unknown value type!");
428 case MVT::i1:
429 case MVT::i8:
430 case MVT::i16:
431 case MVT::i32:
432 case MVT::f32:
433 NumBytes += 4;
434 break;
435 case MVT::i64:
436 case MVT::f64:
437 NumBytes += 8;
438 break;
439 }
440
441 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
442 DAG.getConstant(NumBytes, getPointerTy()));
443
444 // Arguments go on the stack in reverse order, as specified by the ABI.
445 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000446 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000447 std::vector<SDOperand> Stores;
448
449 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
450 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
451 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
452
453 switch (getValueType(Args[i].second)) {
454 default: assert(0 && "Unexpected ValueType for argument!");
455 case MVT::i1:
456 case MVT::i8:
457 case MVT::i16:
458 // Promote the integer to 32 bits. If the input type is signed use a
459 // sign extend, otherwise use a zero extend.
460 if (Args[i].second->isSigned())
461 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
462 else
463 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
464
465 // FALL THROUGH
466 case MVT::i32:
467 case MVT::f32:
468 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
469 Args[i].first, PtrOff,
470 DAG.getSrcValue(NULL)));
471 ArgOffset += 4;
472 break;
473 case MVT::i64:
474 case MVT::f64:
475 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
476 Args[i].first, PtrOff,
477 DAG.getSrcValue(NULL)));
478 ArgOffset += 8;
479 break;
480 }
481 }
482 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
483 }
484
485 std::vector<MVT::ValueType> RetVals;
486 MVT::ValueType RetTyVT = getValueType(RetTy);
487 RetVals.push_back(MVT::Other);
488
489 // The result values produced have to be legal. Promote the result.
490 switch (RetTyVT) {
491 case MVT::isVoid: break;
492 default:
493 RetVals.push_back(RetTyVT);
494 break;
495 case MVT::i1:
496 case MVT::i8:
497 case MVT::i16:
498 RetVals.push_back(MVT::i32);
499 break;
500 case MVT::f32:
501 if (X86ScalarSSE)
502 RetVals.push_back(MVT::f32);
503 else
504 RetVals.push_back(MVT::f64);
505 break;
506 case MVT::i64:
507 RetVals.push_back(MVT::i32);
508 RetVals.push_back(MVT::i32);
509 break;
510 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000511
Evan Chengd90eb7f2006-01-05 00:27:02 +0000512 if (X86DAGIsel) {
513 std::vector<MVT::ValueType> NodeTys;
514 NodeTys.push_back(MVT::Other); // Returns a chain
515 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd90eb7f2006-01-05 00:27:02 +0000516 std::vector<SDOperand> Ops;
517 Ops.push_back(Chain);
518 Ops.push_back(Callee);
519
Evan Chengd9558e02006-01-06 00:43:03 +0000520 // FIXME: Do not generate X86ISD::TAILCALL for now.
521 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000522 SDOperand InFlag = Chain.getValue(1);
523
Chris Lattneraf63bb02006-01-24 05:17:12 +0000524 NodeTys.clear();
525 NodeTys.push_back(MVT::Other); // Returns a chain
526 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
527 Ops.clear();
528 Ops.push_back(Chain);
529 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
530 Ops.push_back(DAG.getConstant(0, getPointerTy()));
531 Ops.push_back(InFlag);
532 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
533 InFlag = Chain.getValue(1);
534
Evan Chengd90eb7f2006-01-05 00:27:02 +0000535 SDOperand RetVal;
536 if (RetTyVT != MVT::isVoid) {
537 switch (RetTyVT) {
538 default: assert(0 && "Unknown value type to return!");
539 case MVT::i1:
540 case MVT::i8:
541 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
542 Chain = RetVal.getValue(1);
Evan Cheng68e5d082006-01-18 08:08:38 +0000543 if (RetTyVT == MVT::i1)
544 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000545 break;
546 case MVT::i16:
547 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
548 Chain = RetVal.getValue(1);
549 break;
550 case MVT::i32:
551 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
552 Chain = RetVal.getValue(1);
553 break;
554 case MVT::i64: {
555 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
556 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
557 Lo.getValue(2));
558 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
559 Chain = Hi.getValue(1);
560 break;
561 }
Evan Cheng357c58e2006-01-17 21:58:21 +0000562 case MVT::f32:
Evan Chengd90eb7f2006-01-05 00:27:02 +0000563 case MVT::f64: {
564 std::vector<MVT::ValueType> Tys;
565 Tys.push_back(MVT::f64);
566 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000567 Tys.push_back(MVT::Flag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000568 std::vector<SDOperand> Ops;
569 Ops.push_back(Chain);
570 Ops.push_back(InFlag);
571 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000572 Chain = RetVal.getValue(1);
573 InFlag = RetVal.getValue(2);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000574 if (X86ScalarSSE) {
Evan Cheng2059f882006-01-17 00:37:42 +0000575 // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
576 // shouldn't be necessary except for RFP cannot be live across
577 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Chengd90eb7f2006-01-05 00:27:02 +0000578 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
579 MachineFunction &MF = DAG.getMachineFunction();
580 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
581 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
582 Tys.clear();
583 Tys.push_back(MVT::Other);
584 Ops.clear();
585 Ops.push_back(Chain);
586 Ops.push_back(RetVal);
587 Ops.push_back(StackSlot);
588 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000589 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000590 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
591 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
592 DAG.getSrcValue(NULL));
593 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +0000594 }
Evan Cheng357c58e2006-01-17 21:58:21 +0000595
596 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
597 // FIXME: we would really like to remember that this FP_ROUND
598 // operation is okay to eliminate if we allow excess FP precision.
599 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000600 break;
601 }
602 }
603 }
604
Evan Chengd90eb7f2006-01-05 00:27:02 +0000605 return std::make_pair(RetVal, Chain);
606 } else {
607 std::vector<SDOperand> Ops;
608 Ops.push_back(Chain);
609 Ops.push_back(Callee);
610 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
611 Ops.push_back(DAG.getConstant(0, getPointerTy()));
612
613 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
614 RetVals, Ops);
615
616 SDOperand ResultVal;
617 switch (RetTyVT) {
618 case MVT::isVoid: break;
619 default:
620 ResultVal = TheCall.getValue(1);
621 break;
622 case MVT::i1:
623 case MVT::i8:
624 case MVT::i16:
625 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
626 break;
627 case MVT::f32:
628 // FIXME: we would really like to remember that this FP_ROUND operation is
629 // okay to eliminate if we allow excess FP precision.
630 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
631 break;
632 case MVT::i64:
633 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
634 TheCall.getValue(2));
635 break;
636 }
637
638 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
639 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000640 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000641}
642
643SDOperand
644X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
645 Value *VAListV, SelectionDAG &DAG) {
646 // vastart just stores the address of the VarArgsFrameIndex slot.
647 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
648 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
649 DAG.getSrcValue(VAListV));
650}
651
652
653std::pair<SDOperand,SDOperand>
654X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
655 Value *VAListV, const Type *ArgTy,
656 SelectionDAG &DAG) {
657 MVT::ValueType ArgVT = getValueType(ArgTy);
658 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
659 VAListP, DAG.getSrcValue(VAListV));
660 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
661 DAG.getSrcValue(NULL));
662 unsigned Amt;
663 if (ArgVT == MVT::i32)
664 Amt = 4;
665 else {
666 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
667 "Other types should have been promoted for varargs!");
668 Amt = 8;
669 }
670 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
671 DAG.getConstant(Amt, Val.getValueType()));
672 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
673 Val, VAListP, DAG.getSrcValue(VAListV));
674 return std::make_pair(Result, Chain);
675}
676
677//===----------------------------------------------------------------------===//
678// Fast Calling Convention implementation
679//===----------------------------------------------------------------------===//
680//
681// The X86 'fast' calling convention passes up to two integer arguments in
682// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
683// and requires that the callee pop its arguments off the stack (allowing proper
684// tail calls), and has the same return value conventions as C calling convs.
685//
686// This calling convention always arranges for the callee pop value to be 8n+4
687// bytes, which is needed for tail recursion elimination and stack alignment
688// reasons.
689//
690// Note that this can be enhanced in the future to pass fp vals in registers
691// (when we have a global fp allocator) and do other tricks.
692//
693
694/// AddLiveIn - This helper function adds the specified physical register to the
695/// MachineFunction as a live in value. It also creates a corresponding virtual
696/// register for it.
697static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
698 TargetRegisterClass *RC) {
699 assert(RC->contains(PReg) && "Not the correct regclass!");
700 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
701 MF.addLiveIn(PReg, VReg);
702 return VReg;
703}
704
705
706std::vector<SDOperand>
707X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
708 std::vector<SDOperand> ArgValues;
709
710 MachineFunction &MF = DAG.getMachineFunction();
711 MachineFrameInfo *MFI = MF.getFrameInfo();
712
713 // Add DAG nodes to load the arguments... On entry to a function the stack
714 // frame looks like this:
715 //
716 // [ESP] -- return address
717 // [ESP + 4] -- first nonreg argument (leftmost lexically)
718 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
719 // ...
720 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
721
722 // Keep track of the number of integer regs passed so far. This can be either
723 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
724 // used).
725 unsigned NumIntRegs = 0;
726
727 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
728 MVT::ValueType ObjectVT = getValueType(I->getType());
729 unsigned ArgIncrement = 4;
730 unsigned ObjSize = 0;
731 SDOperand ArgValue;
732
733 switch (ObjectVT) {
734 default: assert(0 && "Unhandled argument type!");
735 case MVT::i1:
736 case MVT::i8:
737 if (NumIntRegs < 2) {
738 if (!I->use_empty()) {
739 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
740 X86::R8RegisterClass);
741 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
742 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000743 if (ObjectVT == MVT::i1)
744 // FIXME: Should insert a assertzext here.
745 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000746 }
747 ++NumIntRegs;
748 break;
749 }
750
751 ObjSize = 1;
752 break;
753 case MVT::i16:
754 if (NumIntRegs < 2) {
755 if (!I->use_empty()) {
756 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
757 X86::R16RegisterClass);
758 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
759 DAG.setRoot(ArgValue.getValue(1));
760 }
761 ++NumIntRegs;
762 break;
763 }
764 ObjSize = 2;
765 break;
766 case MVT::i32:
767 if (NumIntRegs < 2) {
768 if (!I->use_empty()) {
769 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
770 X86::R32RegisterClass);
771 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
772 DAG.setRoot(ArgValue.getValue(1));
773 }
774 ++NumIntRegs;
775 break;
776 }
777 ObjSize = 4;
778 break;
779 case MVT::i64:
780 if (NumIntRegs == 0) {
781 if (!I->use_empty()) {
782 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
783 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
784
785 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
786 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
787 DAG.setRoot(Hi.getValue(1));
788
789 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
790 }
791 NumIntRegs = 2;
792 break;
793 } else if (NumIntRegs == 1) {
794 if (!I->use_empty()) {
795 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
796 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
797 DAG.setRoot(Low.getValue(1));
798
799 // Load the high part from memory.
800 // Create the frame index object for this incoming parameter...
801 int FI = MFI->CreateFixedObject(4, ArgOffset);
802 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
803 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
804 DAG.getSrcValue(NULL));
805 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
806 }
807 ArgOffset += 4;
808 NumIntRegs = 2;
809 break;
810 }
811 ObjSize = ArgIncrement = 8;
812 break;
813 case MVT::f32: ObjSize = 4; break;
814 case MVT::f64: ObjSize = ArgIncrement = 8; break;
815 }
816
817 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
818 // dead loads.
819 if (ObjSize && !I->use_empty()) {
820 // Create the frame index object for this incoming parameter...
821 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
822
823 // Create the SelectionDAG nodes corresponding to a load from this
824 // parameter.
825 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
826
827 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
828 DAG.getSrcValue(NULL));
829 } else if (ArgValue.Val == 0) {
830 if (MVT::isInteger(ObjectVT))
831 ArgValue = DAG.getConstant(0, ObjectVT);
832 else
833 ArgValue = DAG.getConstantFP(0, ObjectVT);
834 }
835 ArgValues.push_back(ArgValue);
836
837 if (ObjSize)
838 ArgOffset += ArgIncrement; // Move on to the next argument.
839 }
840
841 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
842 // arguments and the arguments after the retaddr has been pushed are aligned.
843 if ((ArgOffset & 7) == 0)
844 ArgOffset += 4;
845
846 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
847 ReturnAddrIndex = 0; // No return address slot generated yet.
848 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
849 BytesCallerReserves = 0;
850
851 // Finally, inform the code generator which regs we return values in.
852 switch (getValueType(F.getReturnType())) {
853 default: assert(0 && "Unknown type!");
854 case MVT::isVoid: break;
855 case MVT::i1:
856 case MVT::i8:
857 case MVT::i16:
858 case MVT::i32:
859 MF.addLiveOut(X86::EAX);
860 break;
861 case MVT::i64:
862 MF.addLiveOut(X86::EAX);
863 MF.addLiveOut(X86::EDX);
864 break;
865 case MVT::f32:
866 case MVT::f64:
867 MF.addLiveOut(X86::ST0);
868 break;
869 }
870 return ArgValues;
871}
872
873std::pair<SDOperand, SDOperand>
874X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
875 bool isTailCall, SDOperand Callee,
876 ArgListTy &Args, SelectionDAG &DAG) {
877 // Count how many bytes are to be pushed on the stack.
878 unsigned NumBytes = 0;
879
880 // Keep track of the number of integer regs passed so far. This can be either
881 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
882 // used).
883 unsigned NumIntRegs = 0;
884
885 for (unsigned i = 0, e = Args.size(); i != e; ++i)
886 switch (getValueType(Args[i].second)) {
887 default: assert(0 && "Unknown value type!");
888 case MVT::i1:
889 case MVT::i8:
890 case MVT::i16:
891 case MVT::i32:
892 if (NumIntRegs < 2) {
893 ++NumIntRegs;
894 break;
895 }
896 // fall through
897 case MVT::f32:
898 NumBytes += 4;
899 break;
900 case MVT::i64:
901 if (NumIntRegs == 0) {
902 NumIntRegs = 2;
903 break;
904 } else if (NumIntRegs == 1) {
905 NumIntRegs = 2;
906 NumBytes += 4;
907 break;
908 }
909
910 // fall through
911 case MVT::f64:
912 NumBytes += 8;
913 break;
914 }
915
916 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
917 // arguments and the arguments after the retaddr has been pushed are aligned.
918 if ((NumBytes & 7) == 0)
919 NumBytes += 4;
920
921 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
922 DAG.getConstant(NumBytes, getPointerTy()));
923
924 // Arguments go on the stack in reverse order, as specified by the ABI.
925 unsigned ArgOffset = 0;
926 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
927 X86::ESP, MVT::i32);
928 NumIntRegs = 0;
929 std::vector<SDOperand> Stores;
930 std::vector<SDOperand> RegValuesToPass;
931 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
932 switch (getValueType(Args[i].second)) {
933 default: assert(0 && "Unexpected ValueType for argument!");
934 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000935 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
936 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000937 case MVT::i8:
938 case MVT::i16:
939 case MVT::i32:
940 if (NumIntRegs < 2) {
941 RegValuesToPass.push_back(Args[i].first);
942 ++NumIntRegs;
943 break;
944 }
945 // Fall through
946 case MVT::f32: {
947 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
948 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
949 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
950 Args[i].first, PtrOff,
951 DAG.getSrcValue(NULL)));
952 ArgOffset += 4;
953 break;
954 }
955 case MVT::i64:
956 if (NumIntRegs < 2) { // Can pass part of it in regs?
957 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
958 Args[i].first, DAG.getConstant(1, MVT::i32));
959 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
960 Args[i].first, DAG.getConstant(0, MVT::i32));
961 RegValuesToPass.push_back(Lo);
962 ++NumIntRegs;
963 if (NumIntRegs < 2) { // Pass both parts in regs?
964 RegValuesToPass.push_back(Hi);
965 ++NumIntRegs;
966 } else {
967 // Pass the high part in memory.
968 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
969 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
970 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
971 Hi, PtrOff, DAG.getSrcValue(NULL)));
972 ArgOffset += 4;
973 }
974 break;
975 }
976 // Fall through
977 case MVT::f64:
978 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
979 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
980 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
981 Args[i].first, PtrOff,
982 DAG.getSrcValue(NULL)));
983 ArgOffset += 8;
984 break;
985 }
986 }
987 if (!Stores.empty())
988 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
989
990 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
991 // arguments and the arguments after the retaddr has been pushed are aligned.
992 if ((ArgOffset & 7) == 0)
993 ArgOffset += 4;
994
995 std::vector<MVT::ValueType> RetVals;
996 MVT::ValueType RetTyVT = getValueType(RetTy);
997
998 RetVals.push_back(MVT::Other);
999
1000 // The result values produced have to be legal. Promote the result.
1001 switch (RetTyVT) {
1002 case MVT::isVoid: break;
1003 default:
1004 RetVals.push_back(RetTyVT);
1005 break;
1006 case MVT::i1:
1007 case MVT::i8:
1008 case MVT::i16:
1009 RetVals.push_back(MVT::i32);
1010 break;
1011 case MVT::f32:
1012 if (X86ScalarSSE)
1013 RetVals.push_back(MVT::f32);
1014 else
1015 RetVals.push_back(MVT::f64);
1016 break;
1017 case MVT::i64:
1018 RetVals.push_back(MVT::i32);
1019 RetVals.push_back(MVT::i32);
1020 break;
1021 }
1022
Evan Chengd9558e02006-01-06 00:43:03 +00001023 if (X86DAGIsel) {
1024 // Build a sequence of copy-to-reg nodes chained together with token chain
1025 // and flag operands which copy the outgoing args into registers.
1026 SDOperand InFlag;
1027 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1028 unsigned CCReg;
1029 SDOperand RegToPass = RegValuesToPass[i];
1030 switch (RegToPass.getValueType()) {
1031 default: assert(0 && "Bad thing to pass in regs");
1032 case MVT::i8:
1033 CCReg = (i == 0) ? X86::AL : X86::DL;
1034 break;
1035 case MVT::i16:
1036 CCReg = (i == 0) ? X86::AX : X86::DX;
1037 break;
1038 case MVT::i32:
1039 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1040 break;
1041 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001042
Evan Chengd9558e02006-01-06 00:43:03 +00001043 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1044 InFlag = Chain.getValue(1);
1045 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001046
Evan Chengd9558e02006-01-06 00:43:03 +00001047 std::vector<MVT::ValueType> NodeTys;
1048 NodeTys.push_back(MVT::Other); // Returns a chain
1049 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Chengd9558e02006-01-06 00:43:03 +00001050 std::vector<SDOperand> Ops;
1051 Ops.push_back(Chain);
1052 Ops.push_back(Callee);
1053 if (InFlag.Val)
1054 Ops.push_back(InFlag);
1055
1056 // FIXME: Do not generate X86ISD::TAILCALL for now.
1057 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1058 InFlag = Chain.getValue(1);
1059
Chris Lattneraf63bb02006-01-24 05:17:12 +00001060 NodeTys.clear();
1061 NodeTys.push_back(MVT::Other); // Returns a chain
1062 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1063 Ops.clear();
1064 Ops.push_back(Chain);
1065 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1066 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1067 Ops.push_back(InFlag);
1068 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1069 InFlag = Chain.getValue(1);
1070
Evan Chengd9558e02006-01-06 00:43:03 +00001071 SDOperand RetVal;
1072 if (RetTyVT != MVT::isVoid) {
1073 switch (RetTyVT) {
1074 default: assert(0 && "Unknown value type to return!");
1075 case MVT::i1:
1076 case MVT::i8:
1077 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1078 Chain = RetVal.getValue(1);
Evan Cheng68e5d082006-01-18 08:08:38 +00001079 if (RetTyVT == MVT::i1)
1080 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Chengd9558e02006-01-06 00:43:03 +00001081 break;
1082 case MVT::i16:
1083 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1084 Chain = RetVal.getValue(1);
1085 break;
1086 case MVT::i32:
1087 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1088 Chain = RetVal.getValue(1);
1089 break;
1090 case MVT::i64: {
1091 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1092 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1093 Lo.getValue(2));
1094 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1095 Chain = Hi.getValue(1);
1096 break;
1097 }
Evan Cheng357c58e2006-01-17 21:58:21 +00001098 case MVT::f32:
Evan Chengd9558e02006-01-06 00:43:03 +00001099 case MVT::f64: {
1100 std::vector<MVT::ValueType> Tys;
1101 Tys.push_back(MVT::f64);
1102 Tys.push_back(MVT::Other);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001103 Tys.push_back(MVT::Flag);
Evan Chengd9558e02006-01-06 00:43:03 +00001104 std::vector<SDOperand> Ops;
1105 Ops.push_back(Chain);
1106 Ops.push_back(InFlag);
1107 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001108 Chain = RetVal.getValue(1);
1109 InFlag = RetVal.getValue(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001110 if (X86ScalarSSE) {
Evan Cheng2059f882006-01-17 00:37:42 +00001111 // FIXME:Currently the FST is flagged to the FP_GET_RESULT. This
1112 // shouldn't be necessary except for RFP cannot be live across
1113 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Chengd9558e02006-01-06 00:43:03 +00001114 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1115 MachineFunction &MF = DAG.getMachineFunction();
1116 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1117 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1118 Tys.clear();
1119 Tys.push_back(MVT::Other);
1120 Ops.clear();
1121 Ops.push_back(Chain);
1122 Ops.push_back(RetVal);
1123 Ops.push_back(StackSlot);
1124 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001125 Ops.push_back(InFlag);
Evan Chengd9558e02006-01-06 00:43:03 +00001126 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1127 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1128 DAG.getSrcValue(NULL));
1129 Chain = RetVal.getValue(1);
Evan Cheng42ef0bc2006-01-17 00:19:47 +00001130 }
Evan Cheng357c58e2006-01-17 21:58:21 +00001131
1132 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1133 // FIXME: we would really like to remember that this FP_ROUND
1134 // operation is okay to eliminate if we allow excess FP precision.
1135 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Chengd9558e02006-01-06 00:43:03 +00001136 break;
1137 }
1138 }
1139 }
1140
Evan Chengd9558e02006-01-06 00:43:03 +00001141 return std::make_pair(RetVal, Chain);
1142 } else {
1143 std::vector<SDOperand> Ops;
1144 Ops.push_back(Chain);
1145 Ops.push_back(Callee);
1146 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1147 // Callee pops all arg values on the stack.
1148 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1149
1150 // Pass register arguments as needed.
1151 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1152
1153 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1154 RetVals, Ops);
1155 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1156
1157 SDOperand ResultVal;
1158 switch (RetTyVT) {
1159 case MVT::isVoid: break;
1160 default:
1161 ResultVal = TheCall.getValue(1);
1162 break;
1163 case MVT::i1:
1164 case MVT::i8:
1165 case MVT::i16:
1166 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1167 break;
1168 case MVT::f32:
1169 // FIXME: we would really like to remember that this FP_ROUND operation is
1170 // okay to eliminate if we allow excess FP precision.
1171 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1172 break;
1173 case MVT::i64:
1174 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1175 TheCall.getValue(2));
1176 break;
1177 }
1178
1179 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001180 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001181}
1182
1183SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1184 if (ReturnAddrIndex == 0) {
1185 // Set up a frame object for the return address.
1186 MachineFunction &MF = DAG.getMachineFunction();
1187 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1188 }
1189
1190 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1191}
1192
1193
1194
1195std::pair<SDOperand, SDOperand> X86TargetLowering::
1196LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1197 SelectionDAG &DAG) {
1198 SDOperand Result;
1199 if (Depth) // Depths > 0 not supported yet!
1200 Result = DAG.getConstant(0, getPointerTy());
1201 else {
1202 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1203 if (!isFrameAddress)
1204 // Just load the return address
1205 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1206 DAG.getSrcValue(NULL));
1207 else
1208 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1209 DAG.getConstant(4, MVT::i32));
1210 }
1211 return std::make_pair(Result, Chain);
1212}
1213
Evan Cheng4a460802006-01-11 00:33:36 +00001214/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1215/// which corresponds to the condition code.
1216static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1217 switch (X86CC) {
1218 default: assert(0 && "Unknown X86 conditional code!");
1219 case X86ISD::COND_A: return X86::JA;
1220 case X86ISD::COND_AE: return X86::JAE;
1221 case X86ISD::COND_B: return X86::JB;
1222 case X86ISD::COND_BE: return X86::JBE;
1223 case X86ISD::COND_E: return X86::JE;
1224 case X86ISD::COND_G: return X86::JG;
1225 case X86ISD::COND_GE: return X86::JGE;
1226 case X86ISD::COND_L: return X86::JL;
1227 case X86ISD::COND_LE: return X86::JLE;
1228 case X86ISD::COND_NE: return X86::JNE;
1229 case X86ISD::COND_NO: return X86::JNO;
1230 case X86ISD::COND_NP: return X86::JNP;
1231 case X86ISD::COND_NS: return X86::JNS;
1232 case X86ISD::COND_O: return X86::JO;
1233 case X86ISD::COND_P: return X86::JP;
1234 case X86ISD::COND_S: return X86::JS;
1235 }
1236}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001237
Evan Cheng4a460802006-01-11 00:33:36 +00001238/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1239/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Chengd9558e02006-01-06 00:43:03 +00001240/// do a direct translation.
Evan Cheng4a460802006-01-11 00:33:36 +00001241static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Chengd9558e02006-01-06 00:43:03 +00001242 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1243 unsigned X86CC = X86ISD::COND_INVALID;
1244 if (!isFP) {
1245 switch (SetCCOpcode) {
1246 default: break;
1247 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1248 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1249 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1250 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1251 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1252 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1253 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1254 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1255 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1256 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1257 }
1258 } else {
1259 // On a floating point condition, the flags are set as follows:
1260 // ZF PF CF op
1261 // 0 | 0 | 0 | X > Y
1262 // 0 | 0 | 1 | X < Y
1263 // 1 | 0 | 0 | X == Y
1264 // 1 | 1 | 1 | unordered
1265 switch (SetCCOpcode) {
1266 default: break;
1267 case ISD::SETUEQ:
1268 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1269 case ISD::SETOGT:
1270 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1271 case ISD::SETOGE:
1272 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1273 case ISD::SETULT:
1274 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1275 case ISD::SETULE:
1276 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1277 case ISD::SETONE:
1278 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1279 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1280 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1281 }
1282 }
1283 return X86CC;
1284}
1285
Evan Cheng4a460802006-01-11 00:33:36 +00001286/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1287/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001288/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001289static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001290 switch (X86CC) {
1291 default:
1292 return false;
1293 case X86ISD::COND_B:
1294 case X86ISD::COND_BE:
1295 case X86ISD::COND_E:
1296 case X86ISD::COND_P:
1297 case X86ISD::COND_A:
1298 case X86ISD::COND_AE:
1299 case X86ISD::COND_NE:
1300 case X86ISD::COND_NP:
1301 return true;
1302 }
1303}
1304
Evan Cheng4a460802006-01-11 00:33:36 +00001305MachineBasicBlock *
1306X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1307 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001308 switch (MI->getOpcode()) {
1309 default: assert(false && "Unexpected instr type to insert");
1310 case X86::CMOV_FR32:
1311 case X86::CMOV_FR64: {
1312 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1313 // control-flow pattern. The incoming instruction knows the destination vreg
1314 // to set, the condition code register to branch on, the true/false values to
1315 // select between, and a branch opcode to use.
1316 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1317 ilist<MachineBasicBlock>::iterator It = BB;
1318 ++It;
1319
1320 // thisMBB:
1321 // ...
1322 // TrueVal = ...
1323 // cmpTY ccX, r1, r2
1324 // bCC copy1MBB
1325 // fallthrough --> copy0MBB
1326 MachineBasicBlock *thisMBB = BB;
1327 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1328 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1329 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1330 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1331 MachineFunction *F = BB->getParent();
1332 F->getBasicBlockList().insert(It, copy0MBB);
1333 F->getBasicBlockList().insert(It, sinkMBB);
1334 // Update machine-CFG edges
1335 BB->addSuccessor(copy0MBB);
1336 BB->addSuccessor(sinkMBB);
1337
1338 // copy0MBB:
1339 // %FalseValue = ...
1340 // # fallthrough to sinkMBB
1341 BB = copy0MBB;
1342
1343 // Update machine-CFG edges
1344 BB->addSuccessor(sinkMBB);
1345
1346 // sinkMBB:
1347 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1348 // ...
1349 BB = sinkMBB;
1350 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1351 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1352 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001353
Evan Cheng0cc39452006-01-16 21:21:29 +00001354 delete MI; // The pseudo instruction is gone now.
1355 return BB;
1356 }
Evan Cheng4a460802006-01-11 00:33:36 +00001357
Evan Cheng0cc39452006-01-16 21:21:29 +00001358 case X86::FP_TO_INT16_IN_MEM:
1359 case X86::FP_TO_INT32_IN_MEM:
1360 case X86::FP_TO_INT64_IN_MEM: {
1361 // Change the floating point control register to use "round towards zero"
1362 // mode when truncating to an integer value.
1363 MachineFunction *F = BB->getParent();
1364 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1365 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1366
1367 // Load the old value of the high byte of the control word...
1368 unsigned OldCW =
1369 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1370 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1371
1372 // Set the high part to be round to zero...
1373 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1374
1375 // Reload the modified control word now...
1376 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1377
1378 // Restore the memory image of control word to original value
1379 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1380
1381 // Get the X86 opcode to use.
1382 unsigned Opc;
1383 switch (MI->getOpcode()) {
1384 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1385 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1386 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1387 }
1388
1389 X86AddressMode AM;
1390 MachineOperand &Op = MI->getOperand(0);
1391 if (Op.isRegister()) {
1392 AM.BaseType = X86AddressMode::RegBase;
1393 AM.Base.Reg = Op.getReg();
1394 } else {
1395 AM.BaseType = X86AddressMode::FrameIndexBase;
1396 AM.Base.FrameIndex = Op.getFrameIndex();
1397 }
1398 Op = MI->getOperand(1);
1399 if (Op.isImmediate())
1400 AM.Scale = Op.getImmedValue();
1401 Op = MI->getOperand(2);
1402 if (Op.isImmediate())
1403 AM.IndexReg = Op.getImmedValue();
1404 Op = MI->getOperand(3);
1405 if (Op.isGlobalAddress()) {
1406 AM.GV = Op.getGlobal();
1407 } else {
1408 AM.Disp = Op.getImmedValue();
1409 }
1410 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1411
1412 // Reload the original control word now.
1413 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1414
1415 delete MI; // The pseudo instruction is gone now.
1416 return BB;
1417 }
1418 }
Evan Cheng4a460802006-01-11 00:33:36 +00001419}
1420
1421
1422//===----------------------------------------------------------------------===//
1423// X86 Custom Lowering Hooks
1424//===----------------------------------------------------------------------===//
1425
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001426/// LowerOperation - Provide custom lowering hooks for some operations.
1427///
1428SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1429 switch (Op.getOpcode()) {
1430 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001431 case ISD::ADD_PARTS:
1432 case ISD::SUB_PARTS: {
1433 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1434 "Not an i64 add/sub!");
1435 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1436 std::vector<MVT::ValueType> Tys;
1437 Tys.push_back(MVT::i32);
1438 Tys.push_back(MVT::Flag);
1439 std::vector<SDOperand> Ops;
1440 Ops.push_back(Op.getOperand(0));
1441 Ops.push_back(Op.getOperand(2));
1442 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1443 Tys, Ops);
1444 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1445 Op.getOperand(1), Op.getOperand(3),
1446 Lo.getValue(1));
1447 Tys.clear();
1448 Tys.push_back(MVT::i32);
1449 Tys.push_back(MVT::i32);
1450 Ops.clear();
1451 Ops.push_back(Lo);
1452 Ops.push_back(Hi);
1453 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1454 }
1455 case ISD::SHL_PARTS:
1456 case ISD::SRA_PARTS:
1457 case ISD::SRL_PARTS: {
1458 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1459 "Not an i64 shift!");
1460 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1461 SDOperand ShOpLo = Op.getOperand(0);
1462 SDOperand ShOpHi = Op.getOperand(1);
1463 SDOperand ShAmt = Op.getOperand(2);
1464 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001465 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001466 : DAG.getConstant(0, MVT::i32);
1467
1468 SDOperand Tmp2, Tmp3;
1469 if (Op.getOpcode() == ISD::SHL_PARTS) {
1470 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1471 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1472 } else {
1473 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001474 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001475 }
1476
1477 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1478 ShAmt, DAG.getConstant(32, MVT::i8));
1479
1480 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001481 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001482
1483 std::vector<MVT::ValueType> Tys;
1484 Tys.push_back(MVT::i32);
1485 Tys.push_back(MVT::Flag);
1486 std::vector<SDOperand> Ops;
1487 if (Op.getOpcode() == ISD::SHL_PARTS) {
1488 Ops.push_back(Tmp2);
1489 Ops.push_back(Tmp3);
1490 Ops.push_back(CC);
1491 Ops.push_back(InFlag);
1492 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1493 InFlag = Hi.getValue(1);
1494
1495 Ops.clear();
1496 Ops.push_back(Tmp3);
1497 Ops.push_back(Tmp1);
1498 Ops.push_back(CC);
1499 Ops.push_back(InFlag);
1500 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1501 } else {
1502 Ops.push_back(Tmp2);
1503 Ops.push_back(Tmp3);
1504 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001505 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001506 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1507 InFlag = Lo.getValue(1);
1508
1509 Ops.clear();
1510 Ops.push_back(Tmp3);
1511 Ops.push_back(Tmp1);
1512 Ops.push_back(CC);
1513 Ops.push_back(InFlag);
1514 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1515 }
1516
1517 Tys.clear();
1518 Tys.push_back(MVT::i32);
1519 Tys.push_back(MVT::i32);
1520 Ops.clear();
1521 Ops.push_back(Lo);
1522 Ops.push_back(Hi);
1523 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1524 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001525 case ISD::SINT_TO_FP: {
1526 assert(Op.getValueType() == MVT::f64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001527 Op.getOperand(0).getValueType() <= MVT::i64 &&
1528 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001529 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001530
1531 SDOperand Result;
1532 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1533 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001534 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001535 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001536 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001537 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1538 DAG.getEntryNode(), Op.getOperand(0),
1539 StackSlot, DAG.getSrcValue(NULL));
1540
1541 // Build the FILD
1542 std::vector<MVT::ValueType> Tys;
1543 Tys.push_back(MVT::f64);
1544 Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001545 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001546 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001547 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001548 Ops.push_back(DAG.getValueType(SrcVT));
1549 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
1550 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001551 }
1552 case ISD::FP_TO_SINT: {
1553 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1554 Op.getOperand(0).getValueType() == MVT::f64 &&
1555 "Unknown FP_TO_SINT to lower!");
1556 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1557 // stack slot.
1558 MachineFunction &MF = DAG.getMachineFunction();
1559 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1560 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1561 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1562
1563 unsigned Opc;
1564 switch (Op.getValueType()) {
1565 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1566 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1567 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1568 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1569 }
1570
1571 // Build the FP_TO_INT*_IN_MEM
1572 std::vector<SDOperand> Ops;
1573 Ops.push_back(DAG.getEntryNode());
1574 Ops.push_back(Op.getOperand(0));
1575 Ops.push_back(StackSlot);
1576 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1577
1578 // Load the result.
1579 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1580 DAG.getSrcValue(NULL));
1581 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001582 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001583 std::vector<MVT::ValueType> Tys;
1584 Tys.push_back(MVT::Other);
1585 Tys.push_back(MVT::Flag);
1586 std::vector<SDOperand> Ops;
1587 Ops.push_back(Op.getOperand(0));
1588 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001589 Ops.clear();
1590 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1591 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1592 MVT::i32, Ops[0].getValue(2)));
1593 Ops.push_back(Ops[1].getValue(1));
1594 Tys[0] = Tys[1] = MVT::i32;
1595 Tys.push_back(MVT::Other);
1596 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001597 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001598 case ISD::SETCC: {
1599 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1600 SDOperand CC = Op.getOperand(2);
1601 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1602 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001603 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1604 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng4a460802006-01-11 00:33:36 +00001605 unsigned X86CC = getX86CC(CC, isFP);
Evan Chengd9558e02006-01-06 00:43:03 +00001606 if (X86CC != X86ISD::COND_INVALID) {
1607 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1608 DAG.getConstant(X86CC, MVT::i8), Cond);
1609 } else {
1610 assert(isFP && "Illegal integer SetCC!");
1611
1612 std::vector<MVT::ValueType> Tys;
1613 std::vector<SDOperand> Ops;
1614 switch (SetCCOpcode) {
1615 default: assert(false && "Illegal floating point SetCC!");
1616 case ISD::SETOEQ: { // !PF & ZF
1617 Tys.push_back(MVT::i8);
1618 Tys.push_back(MVT::Flag);
1619 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1620 Ops.push_back(Cond);
1621 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1622 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1623 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1624 Tmp1.getValue(1));
1625 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1626 }
1627 case ISD::SETOLT: { // !PF & CF
1628 Tys.push_back(MVT::i8);
1629 Tys.push_back(MVT::Flag);
1630 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1631 Ops.push_back(Cond);
1632 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1633 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1634 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1635 Tmp1.getValue(1));
1636 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1637 }
1638 case ISD::SETOLE: { // !PF & (CF || ZF)
1639 Tys.push_back(MVT::i8);
1640 Tys.push_back(MVT::Flag);
1641 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1642 Ops.push_back(Cond);
1643 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1644 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1645 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1646 Tmp1.getValue(1));
1647 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1648 }
1649 case ISD::SETUGT: { // PF | (!ZF & !CF)
1650 Tys.push_back(MVT::i8);
1651 Tys.push_back(MVT::Flag);
1652 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1653 Ops.push_back(Cond);
1654 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1655 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1656 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1657 Tmp1.getValue(1));
1658 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1659 }
1660 case ISD::SETUGE: { // PF | !CF
1661 Tys.push_back(MVT::i8);
1662 Tys.push_back(MVT::Flag);
1663 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1664 Ops.push_back(Cond);
1665 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1666 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1667 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1668 Tmp1.getValue(1));
1669 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1670 }
1671 case ISD::SETUNE: { // PF | !ZF
1672 Tys.push_back(MVT::i8);
1673 Tys.push_back(MVT::Flag);
1674 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1675 Ops.push_back(Cond);
1676 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1677 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1678 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1679 Tmp1.getValue(1));
1680 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1681 }
1682 }
1683 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001684 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001685 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001686 MVT::ValueType VT = Op.getValueType();
1687 bool isFP = MVT::isFloatingPoint(VT);
1688 bool isFPStack = isFP && (X86Vector < SSE2);
1689 bool isFPSSE = isFP && (X86Vector >= SSE2);
Evan Cheng1bcee362006-01-13 01:03:02 +00001690 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001691 SDOperand Op0 = Op.getOperand(0);
1692 SDOperand Cond, CC;
1693 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001694 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1695 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1696 // have another use it will be eliminated.
1697 // If the X86ISD::SETCC has more than one use, then it's probably better
1698 // to use a test instead of duplicating the X86ISD::CMP (for register
1699 // pressure reason).
Evan Cheng80ebe382006-01-13 01:17:24 +00001700 if (Op0.hasOneUse() && Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001701 CC = Op0.getOperand(0);
1702 Cond = Op0.getOperand(1);
1703 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001704 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001705 } else
1706 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001707 } else if (Op0.getOpcode() == ISD::SETCC) {
1708 CC = Op0.getOperand(2);
1709 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng4a460802006-01-11 00:33:36 +00001710 unsigned X86CC = getX86CC(CC, isFP);
Evan Chengd9558e02006-01-06 00:43:03 +00001711 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng7df96d62005-12-17 01:21:05 +00001712 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaaca22c2006-01-10 20:26:56 +00001713 Op0.getOperand(0), Op0.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001714 } else
1715 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001716
Evan Cheng189d01e2006-01-13 01:06:49 +00001717 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001718 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001719 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001720 }
Evan Chenge3413162006-01-09 18:33:28 +00001721
1722 std::vector<MVT::ValueType> Tys;
1723 Tys.push_back(Op.getValueType());
1724 Tys.push_back(MVT::Flag);
1725 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001726 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1727 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001728 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001729 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001730 Ops.push_back(CC);
1731 Ops.push_back(Cond);
1732 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001733 }
Evan Cheng898101c2005-12-19 23:12:38 +00001734 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001735 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001736 SDOperand Cond = Op.getOperand(1);
1737 SDOperand Dest = Op.getOperand(2);
1738 SDOperand CC;
Evan Chengd5781fc2005-12-21 20:21:51 +00001739 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001740 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1741 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1742 // have another use it will be eliminated.
1743 // If the X86ISD::SETCC has more than one use, then it's probably better
1744 // to use a test instead of duplicating the X86ISD::CMP (for register
1745 // pressure reason).
1746 if (Cond.hasOneUse() && Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1747 CC = Cond.getOperand(0);
1748 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1749 Cond.getOperand(1).getOperand(0),
1750 Cond.getOperand(1).getOperand(1));
1751 } else
1752 addTest = true;
Evan Chengd5781fc2005-12-21 20:21:51 +00001753 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng898101c2005-12-19 23:12:38 +00001754 CC = Cond.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001755 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng4a460802006-01-11 00:33:36 +00001756 unsigned X86CC = getX86CC(CC, isFP);
Evan Chengd9558e02006-01-06 00:43:03 +00001757 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001758 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1759 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001760 } else
1761 addTest = true;
1762
1763 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001764 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001765 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1766 }
1767 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1768 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1769 }
Evan Chengd9558e02006-01-06 00:43:03 +00001770 case ISD::RET: {
1771 // Can only be return void.
Evan Chenge3413162006-01-09 18:33:28 +00001772 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chengd9558e02006-01-06 00:43:03 +00001773 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1774 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001775 case ISD::MEMSET: {
1776 SDOperand InFlag;
1777 SDOperand Chain = Op.getOperand(0);
1778 unsigned Align =
1779 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1780 if (Align == 0) Align = 1;
1781
1782 MVT::ValueType AVT;
1783 SDOperand Count;
1784 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1785 unsigned ValReg;
1786 unsigned Val = ValC->getValue() & 255;
1787
1788 // If the value is a constant, then we can potentially use larger sets.
1789 switch (Align & 3) {
1790 case 2: // WORD aligned
1791 AVT = MVT::i16;
1792 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1793 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1794 else
1795 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1796 DAG.getConstant(1, MVT::i8));
1797 Val = (Val << 8) | Val;
1798 ValReg = X86::AX;
1799 break;
1800 case 0: // DWORD aligned
1801 AVT = MVT::i32;
1802 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1803 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1804 else
1805 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1806 DAG.getConstant(2, MVT::i8));
1807 Val = (Val << 8) | Val;
1808 Val = (Val << 16) | Val;
1809 ValReg = X86::EAX;
1810 break;
1811 default: // Byte aligned
1812 AVT = MVT::i8;
1813 Count = Op.getOperand(3);
1814 ValReg = X86::AL;
1815 break;
1816 }
1817
1818 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1819 InFlag);
1820 InFlag = Chain.getValue(1);
1821 } else {
1822 AVT = MVT::i8;
1823 Count = Op.getOperand(3);
1824 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1825 InFlag = Chain.getValue(1);
1826 }
1827
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
1833 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1834 DAG.getValueType(AVT), InFlag);
1835 }
1836 case ISD::MEMCPY: {
1837 SDOperand Chain = Op.getOperand(0);
1838 unsigned Align =
1839 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1840 if (Align == 0) Align = 1;
1841
1842 MVT::ValueType AVT;
1843 SDOperand Count;
1844 switch (Align & 3) {
1845 case 2: // WORD aligned
1846 AVT = MVT::i16;
1847 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1848 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1849 else
1850 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1851 DAG.getConstant(1, MVT::i8));
1852 break;
1853 case 0: // DWORD aligned
1854 AVT = MVT::i32;
1855 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1856 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1857 else
1858 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1859 DAG.getConstant(2, MVT::i8));
1860 break;
1861 default: // Byte aligned
1862 AVT = MVT::i8;
1863 Count = Op.getOperand(3);
1864 break;
1865 }
1866
1867 SDOperand InFlag;
1868 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1869 InFlag = Chain.getValue(1);
1870 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1871 InFlag = Chain.getValue(1);
1872 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1873 InFlag = Chain.getValue(1);
1874
1875 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1876 DAG.getValueType(AVT), InFlag);
1877 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001878 case ISD::GlobalAddress: {
Evan Cheng002fe9b2006-01-12 07:56:47 +00001879 SDOperand Result;
Evan Chengb077b842005-12-21 02:39:21 +00001880 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1881 // For Darwin, external and weak symbols are indirect, so we want to load
1882 // the value at address GV, not the value of GV itself. This means that
1883 // the GlobalAddress must be in the base or index register of the address,
1884 // not the GV offset field.
1885 if (getTargetMachine().
1886 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1887 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng002fe9b2006-01-12 07:56:47 +00001888 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1889 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1890 DAG.getSrcValue(NULL));
1891 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001892 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001893 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001894}
Evan Cheng72261582005-12-20 06:22:03 +00001895
1896const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1897 switch (Opcode) {
1898 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001899 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1900 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1901 case X86ISD::ADC: return "X86ISD::ADC";
1902 case X86ISD::SBB: return "X86ISD::SBB";
1903 case X86ISD::SHLD: return "X86ISD::SHLD";
1904 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chenga3195e82006-01-12 22:54:21 +00001905 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng72261582005-12-20 06:22:03 +00001906 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1907 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1908 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001909 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001910 case X86ISD::FST: return "X86ISD::FST";
1911 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001912 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001913 case X86ISD::CALL: return "X86ISD::CALL";
1914 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1915 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1916 case X86ISD::CMP: return "X86ISD::CMP";
1917 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001918 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001919 case X86ISD::CMOV: return "X86ISD::CMOV";
1920 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001921 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng67f92a72006-01-11 22:15:48 +00001922 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1923 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng72261582005-12-20 06:22:03 +00001924 }
1925}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001926
1927bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1928 uint64_t Mask) const {
1929
1930 unsigned Opc = Op.getOpcode();
1931
1932 switch (Opc) {
1933 default:
1934 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1935 break;
1936 case X86ISD::SETCC: return (Mask & 1) == 0;
1937 }
1938
1939 return false;
1940}