blob: e71ce868d4fcf3954b98a4d83fb07cf1e1f68c05 [file] [log] [blame]
Chris Lattner76ac0682005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
20#include "llvm/Function.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetOptions.h"
27using namespace llvm;
28
29// FIXME: temporary.
30#include "llvm/Support/CommandLine.h"
31static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
32 cl::desc("Enable fastcc on X86"));
33
34X86TargetLowering::X86TargetLowering(TargetMachine &TM)
35 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000036 Subtarget = &TM.getSubtarget<X86Subtarget>();
37 X86ScalarSSE = Subtarget->hasSSE2();
38
Chris Lattner76ac0682005-11-15 00:40:23 +000039 // Set up the TargetLowering object.
40
41 // X86 is weird, it always uses i8 for shift amounts and setcc results.
42 setShiftAmountType(MVT::i8);
43 setSetCCResultType(MVT::i8);
44 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000045 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000047 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000048
Chris Lattner76ac0682005-11-15 00:40:23 +000049 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000050 addRegisterClass(MVT::i8, X86::R8RegisterClass);
51 addRegisterClass(MVT::i16, X86::R16RegisterClass);
52 addRegisterClass(MVT::i32, X86::R32RegisterClass);
53
54 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
55 // operation.
56 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
57 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
58 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000059
60 if (X86ScalarSSE)
61 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
62 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
63 else
64 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000065
66 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
67 // this operation.
68 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
69 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
70
Evan Cheng5b97fcf2006-01-30 08:02:57 +000071 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
72 // isn't legal.
73 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
74 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
75
Chris Lattner76ac0682005-11-15 00:40:23 +000076 if (!X86ScalarSSE) {
Chris Lattner76ac0682005-11-15 00:40:23 +000077 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
78 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
79 }
80
81 // Handle FP_TO_UINT by promoting the destination to a larger signed
82 // conversion.
83 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
84 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
85 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
86
87 if (!X86ScalarSSE)
88 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
89
90 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
91 // this operation.
92 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
93 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
94 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
95
Chris Lattner30107e62005-12-23 05:15:23 +000096 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
97 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
98
Evan Chenga814f0b32006-01-27 21:26:54 +000099 if (!X86PatIsel) {
Evan Cheng6fc31042005-12-19 23:12:38 +0000100 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
101 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000102 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
103 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
104 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
105 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
108 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
109 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
110 setOperationAction(ISD::FREM , MVT::f64 , Expand);
111 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
112 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
113 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
114 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
115 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
116 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
117 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
118 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
119 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000120 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000121
Evan Chenga814f0b32006-01-27 21:26:54 +0000122 if (X86PatIsel) {
Nate Begeman2fba8a32006-01-14 03:14:10 +0000123 setOperationAction(ISD::BSWAP , MVT::i32 , Expand);
Evan Cheng6d2ab042006-01-11 23:20:05 +0000124 setOperationAction(ISD::ROTL , MVT::i8 , Expand);
125 setOperationAction(ISD::ROTR , MVT::i8 , Expand);
126 setOperationAction(ISD::ROTL , MVT::i16 , Expand);
127 setOperationAction(ISD::ROTR , MVT::i16 , Expand);
128 setOperationAction(ISD::ROTL , MVT::i32 , Expand);
129 setOperationAction(ISD::ROTR , MVT::i32 , Expand);
130 }
Nate Begeman2fba8a32006-01-14 03:14:10 +0000131 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000132
Chris Lattner76ac0682005-11-15 00:40:23 +0000133 setOperationAction(ISD::READIO , MVT::i1 , Expand);
134 setOperationAction(ISD::READIO , MVT::i8 , Expand);
135 setOperationAction(ISD::READIO , MVT::i16 , Expand);
136 setOperationAction(ISD::READIO , MVT::i32 , Expand);
137 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
138 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
139 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
140 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
141
142 // These should be promoted to a larger select which is supported.
143 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
144 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Chenga814f0b32006-01-27 21:26:54 +0000145 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000146 // X86 wants to expand cmov itself.
Evan Cheng225a4d02005-12-17 01:21:05 +0000147 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
148 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000149 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
150 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Evan Chengc1583db2005-12-21 20:21:51 +0000151 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
152 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
153 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng172fce72006-01-06 00:43:03 +0000154 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
155 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
156 // X86 ret instruction may pop stack.
157 setOperationAction(ISD::RET , MVT::Other, Custom);
158 // Darwin ABI issue.
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000159 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng9c249c32006-01-09 18:33:28 +0000160 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
161 setOperationAction(ISD::ADD_PARTS , MVT::i32 , Custom);
162 setOperationAction(ISD::SUB_PARTS , MVT::i32 , Custom);
163 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
164 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
165 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Evan Chengae986f12006-01-11 22:15:48 +0000166 // X86 wants to expand memset / memcpy itself.
167 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
168 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Cheng225a4d02005-12-17 01:21:05 +0000169 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000170
Chris Lattner9c415362005-11-29 06:16:21 +0000171 // We don't have line number support yet.
172 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000173 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
174 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000175
Nate Begemane74795c2006-01-25 18:21:52 +0000176 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
177 setOperationAction(ISD::VASTART , MVT::Other, Custom);
178
179 // Use the default implementation.
180 setOperationAction(ISD::VAARG , MVT::Other, Expand);
181 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
182 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000183 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
184 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
185 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000186
Chris Lattner76ac0682005-11-15 00:40:23 +0000187 if (X86ScalarSSE) {
188 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000189 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
190 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000191
192 // SSE has no load+extend ops
193 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
194 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
195
196 // SSE has no i16 to fp conversion, only i32
197 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
198 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
199
200 // Expand FP_TO_UINT into a select.
201 // FIXME: We would like to use a Custom expander here eventually to do
202 // the optimal thing for SSE vs. the default expansion in the legalizer.
203 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
204
205 // We don't support sin/cos/sqrt/fmod
206 setOperationAction(ISD::FSIN , MVT::f64, Expand);
207 setOperationAction(ISD::FCOS , MVT::f64, Expand);
208 setOperationAction(ISD::FABS , MVT::f64, Expand);
209 setOperationAction(ISD::FNEG , MVT::f64, Expand);
210 setOperationAction(ISD::FREM , MVT::f64, Expand);
211 setOperationAction(ISD::FSIN , MVT::f32, Expand);
212 setOperationAction(ISD::FCOS , MVT::f32, Expand);
213 setOperationAction(ISD::FABS , MVT::f32, Expand);
214 setOperationAction(ISD::FNEG , MVT::f32, Expand);
215 setOperationAction(ISD::FREM , MVT::f32, Expand);
216
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000217 // Expand FP immediates into loads from the stack, except for the special
218 // cases we handle.
219 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
220 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 addLegalFPImmediate(+0.0); // xorps / xorpd
222 } else {
223 // Set up the FP register classes.
224 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000225
226 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
227
Evan Chenga814f0b32006-01-27 21:26:54 +0000228 if (!X86PatIsel) {
Evan Cheng6305e502006-01-12 22:54:21 +0000229 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
230 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
231 }
232
Chris Lattner76ac0682005-11-15 00:40:23 +0000233 if (!UnsafeFPMath) {
234 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
235 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
236 }
237
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000238 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000239 addLegalFPImmediate(+0.0); // FLD0
240 addLegalFPImmediate(+1.0); // FLD1
241 addLegalFPImmediate(-0.0); // FLD0/FCHS
242 addLegalFPImmediate(-1.0); // FLD1/FCHS
243 }
244 computeRegisterProperties();
245
246 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
247 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
248 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
249 allowUnalignedMemoryAccesses = true; // x86 supports it!
250}
251
252std::vector<SDOperand>
253X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
254 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
255 return LowerFastCCArguments(F, DAG);
256 return LowerCCCArguments(F, DAG);
257}
258
259std::pair<SDOperand, SDOperand>
260X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
261 bool isVarArg, unsigned CallingConv,
262 bool isTailCall,
263 SDOperand Callee, ArgListTy &Args,
264 SelectionDAG &DAG) {
265 assert((!isVarArg || CallingConv == CallingConv::C) &&
266 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000267
268 // If the callee is a GlobalAddress node (quite common, every direct call is)
269 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
270 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
271 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000272 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
273 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000274
Chris Lattner76ac0682005-11-15 00:40:23 +0000275 if (CallingConv == CallingConv::Fast && EnableFastCC)
276 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
277 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
278}
279
280//===----------------------------------------------------------------------===//
281// C Calling Convention implementation
282//===----------------------------------------------------------------------===//
283
284std::vector<SDOperand>
285X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
286 std::vector<SDOperand> ArgValues;
287
288 MachineFunction &MF = DAG.getMachineFunction();
289 MachineFrameInfo *MFI = MF.getFrameInfo();
290
291 // Add DAG nodes to load the arguments... On entry to a function on the X86,
292 // the stack frame looks like this:
293 //
294 // [ESP] -- return address
295 // [ESP + 4] -- first argument (leftmost lexically)
296 // [ESP + 8] -- second argument, if first argument is four bytes in size
297 // ...
298 //
299 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
300 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
301 MVT::ValueType ObjectVT = getValueType(I->getType());
302 unsigned ArgIncrement = 4;
303 unsigned ObjSize;
304 switch (ObjectVT) {
305 default: assert(0 && "Unhandled argument type!");
306 case MVT::i1:
307 case MVT::i8: ObjSize = 1; break;
308 case MVT::i16: ObjSize = 2; break;
309 case MVT::i32: ObjSize = 4; break;
310 case MVT::i64: ObjSize = ArgIncrement = 8; break;
311 case MVT::f32: ObjSize = 4; break;
312 case MVT::f64: ObjSize = ArgIncrement = 8; break;
313 }
314 // Create the frame index object for this incoming parameter...
315 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
316
317 // Create the SelectionDAG nodes corresponding to a load from this parameter
318 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
319
320 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
321 // dead loads.
322 SDOperand ArgValue;
323 if (!I->use_empty())
324 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
325 DAG.getSrcValue(NULL));
326 else {
327 if (MVT::isInteger(ObjectVT))
328 ArgValue = DAG.getConstant(0, ObjectVT);
329 else
330 ArgValue = DAG.getConstantFP(0, ObjectVT);
331 }
332 ArgValues.push_back(ArgValue);
333
334 ArgOffset += ArgIncrement; // Move on to the next argument...
335 }
336
337 // If the function takes variable number of arguments, make a frame index for
338 // the start of the first vararg value... for expansion of llvm.va_start.
339 if (F.isVarArg())
340 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
341 ReturnAddrIndex = 0; // No return address slot generated yet.
342 BytesToPopOnReturn = 0; // Callee pops nothing.
343 BytesCallerReserves = ArgOffset;
344
345 // Finally, inform the code generator which regs we return values in.
346 switch (getValueType(F.getReturnType())) {
347 default: assert(0 && "Unknown type!");
348 case MVT::isVoid: break;
349 case MVT::i1:
350 case MVT::i8:
351 case MVT::i16:
352 case MVT::i32:
353 MF.addLiveOut(X86::EAX);
354 break;
355 case MVT::i64:
356 MF.addLiveOut(X86::EAX);
357 MF.addLiveOut(X86::EDX);
358 break;
359 case MVT::f32:
360 case MVT::f64:
361 MF.addLiveOut(X86::ST0);
362 break;
363 }
364 return ArgValues;
365}
366
367std::pair<SDOperand, SDOperand>
368X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
369 bool isVarArg, bool isTailCall,
370 SDOperand Callee, ArgListTy &Args,
371 SelectionDAG &DAG) {
372 // Count how many bytes are to be pushed on the stack.
373 unsigned NumBytes = 0;
374
375 if (Args.empty()) {
376 // Save zero bytes.
377 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
378 DAG.getConstant(0, getPointerTy()));
379 } else {
380 for (unsigned i = 0, e = Args.size(); i != e; ++i)
381 switch (getValueType(Args[i].second)) {
382 default: assert(0 && "Unknown value type!");
383 case MVT::i1:
384 case MVT::i8:
385 case MVT::i16:
386 case MVT::i32:
387 case MVT::f32:
388 NumBytes += 4;
389 break;
390 case MVT::i64:
391 case MVT::f64:
392 NumBytes += 8;
393 break;
394 }
395
396 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
397 DAG.getConstant(NumBytes, getPointerTy()));
398
399 // Arguments go on the stack in reverse order, as specified by the ABI.
400 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000401 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000402 std::vector<SDOperand> Stores;
403
404 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
405 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
406 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
407
408 switch (getValueType(Args[i].second)) {
409 default: assert(0 && "Unexpected ValueType for argument!");
410 case MVT::i1:
411 case MVT::i8:
412 case MVT::i16:
413 // Promote the integer to 32 bits. If the input type is signed use a
414 // sign extend, otherwise use a zero extend.
415 if (Args[i].second->isSigned())
416 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
417 else
418 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
419
420 // FALL THROUGH
421 case MVT::i32:
422 case MVT::f32:
423 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
424 Args[i].first, PtrOff,
425 DAG.getSrcValue(NULL)));
426 ArgOffset += 4;
427 break;
428 case MVT::i64:
429 case MVT::f64:
430 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
431 Args[i].first, PtrOff,
432 DAG.getSrcValue(NULL)));
433 ArgOffset += 8;
434 break;
435 }
436 }
437 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
438 }
439
440 std::vector<MVT::ValueType> RetVals;
441 MVT::ValueType RetTyVT = getValueType(RetTy);
442 RetVals.push_back(MVT::Other);
443
444 // The result values produced have to be legal. Promote the result.
445 switch (RetTyVT) {
446 case MVT::isVoid: break;
447 default:
448 RetVals.push_back(RetTyVT);
449 break;
450 case MVT::i1:
451 case MVT::i8:
452 case MVT::i16:
453 RetVals.push_back(MVT::i32);
454 break;
455 case MVT::f32:
456 if (X86ScalarSSE)
457 RetVals.push_back(MVT::f32);
458 else
459 RetVals.push_back(MVT::f64);
460 break;
461 case MVT::i64:
462 RetVals.push_back(MVT::i32);
463 RetVals.push_back(MVT::i32);
464 break;
465 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000466
Evan Chenga814f0b32006-01-27 21:26:54 +0000467 if (!X86PatIsel) {
Evan Cheng45e190982006-01-05 00:27:02 +0000468 std::vector<MVT::ValueType> NodeTys;
469 NodeTys.push_back(MVT::Other); // Returns a chain
470 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng45e190982006-01-05 00:27:02 +0000471 std::vector<SDOperand> Ops;
472 Ops.push_back(Chain);
473 Ops.push_back(Callee);
474
Evan Cheng172fce72006-01-06 00:43:03 +0000475 // FIXME: Do not generate X86ISD::TAILCALL for now.
476 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng45e190982006-01-05 00:27:02 +0000477 SDOperand InFlag = Chain.getValue(1);
478
Chris Lattner6f33eae2006-01-24 05:17:12 +0000479 NodeTys.clear();
480 NodeTys.push_back(MVT::Other); // Returns a chain
481 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
482 Ops.clear();
483 Ops.push_back(Chain);
484 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
485 Ops.push_back(DAG.getConstant(0, getPointerTy()));
486 Ops.push_back(InFlag);
487 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
488 InFlag = Chain.getValue(1);
489
Evan Cheng45e190982006-01-05 00:27:02 +0000490 SDOperand RetVal;
491 if (RetTyVT != MVT::isVoid) {
492 switch (RetTyVT) {
493 default: assert(0 && "Unknown value type to return!");
494 case MVT::i1:
495 case MVT::i8:
496 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
497 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000498 if (RetTyVT == MVT::i1)
499 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000500 break;
501 case MVT::i16:
502 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
503 Chain = RetVal.getValue(1);
504 break;
505 case MVT::i32:
506 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
507 Chain = RetVal.getValue(1);
508 break;
509 case MVT::i64: {
510 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
511 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
512 Lo.getValue(2));
513 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
514 Chain = Hi.getValue(1);
515 break;
516 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000517 case MVT::f32:
Evan Cheng45e190982006-01-05 00:27:02 +0000518 case MVT::f64: {
519 std::vector<MVT::ValueType> Tys;
520 Tys.push_back(MVT::f64);
521 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +0000522 Tys.push_back(MVT::Flag);
Evan Cheng45e190982006-01-05 00:27:02 +0000523 std::vector<SDOperand> Ops;
524 Ops.push_back(Chain);
525 Ops.push_back(InFlag);
526 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +0000527 Chain = RetVal.getValue(1);
528 InFlag = RetVal.getValue(2);
Evan Cheng45e190982006-01-05 00:27:02 +0000529 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000530 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
531 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +0000532 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng45e190982006-01-05 00:27:02 +0000533 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000534 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng45e190982006-01-05 00:27:02 +0000535 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
536 Tys.clear();
537 Tys.push_back(MVT::Other);
538 Ops.clear();
539 Ops.push_back(Chain);
540 Ops.push_back(RetVal);
541 Ops.push_back(StackSlot);
542 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +0000543 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000544 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
545 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
546 DAG.getSrcValue(NULL));
547 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +0000548 }
Evan Chengfeaed4d2006-01-17 21:58:21 +0000549
550 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
551 // FIXME: we would really like to remember that this FP_ROUND
552 // operation is okay to eliminate if we allow excess FP precision.
553 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng45e190982006-01-05 00:27:02 +0000554 break;
555 }
556 }
557 }
558
Evan Cheng45e190982006-01-05 00:27:02 +0000559 return std::make_pair(RetVal, Chain);
560 } else {
561 std::vector<SDOperand> Ops;
562 Ops.push_back(Chain);
563 Ops.push_back(Callee);
564 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
565 Ops.push_back(DAG.getConstant(0, getPointerTy()));
566
567 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
568 RetVals, Ops);
569
570 SDOperand ResultVal;
571 switch (RetTyVT) {
572 case MVT::isVoid: break;
573 default:
574 ResultVal = TheCall.getValue(1);
575 break;
576 case MVT::i1:
577 case MVT::i8:
578 case MVT::i16:
579 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
580 break;
581 case MVT::f32:
582 // FIXME: we would really like to remember that this FP_ROUND operation is
583 // okay to eliminate if we allow excess FP precision.
584 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
585 break;
586 case MVT::i64:
587 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
588 TheCall.getValue(2));
589 break;
590 }
591
592 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
593 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000594 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000595}
596
Chris Lattner76ac0682005-11-15 00:40:23 +0000597//===----------------------------------------------------------------------===//
598// Fast Calling Convention implementation
599//===----------------------------------------------------------------------===//
600//
601// The X86 'fast' calling convention passes up to two integer arguments in
602// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
603// and requires that the callee pop its arguments off the stack (allowing proper
604// tail calls), and has the same return value conventions as C calling convs.
605//
606// This calling convention always arranges for the callee pop value to be 8n+4
607// bytes, which is needed for tail recursion elimination and stack alignment
608// reasons.
609//
610// Note that this can be enhanced in the future to pass fp vals in registers
611// (when we have a global fp allocator) and do other tricks.
612//
613
614/// AddLiveIn - This helper function adds the specified physical register to the
615/// MachineFunction as a live in value. It also creates a corresponding virtual
616/// register for it.
617static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
618 TargetRegisterClass *RC) {
619 assert(RC->contains(PReg) && "Not the correct regclass!");
620 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
621 MF.addLiveIn(PReg, VReg);
622 return VReg;
623}
624
625
626std::vector<SDOperand>
627X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
628 std::vector<SDOperand> ArgValues;
629
630 MachineFunction &MF = DAG.getMachineFunction();
631 MachineFrameInfo *MFI = MF.getFrameInfo();
632
633 // Add DAG nodes to load the arguments... On entry to a function the stack
634 // frame looks like this:
635 //
636 // [ESP] -- return address
637 // [ESP + 4] -- first nonreg argument (leftmost lexically)
638 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
639 // ...
640 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
641
642 // Keep track of the number of integer regs passed so far. This can be either
643 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
644 // used).
645 unsigned NumIntRegs = 0;
646
647 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
648 MVT::ValueType ObjectVT = getValueType(I->getType());
649 unsigned ArgIncrement = 4;
650 unsigned ObjSize = 0;
651 SDOperand ArgValue;
652
653 switch (ObjectVT) {
654 default: assert(0 && "Unhandled argument type!");
655 case MVT::i1:
656 case MVT::i8:
657 if (NumIntRegs < 2) {
658 if (!I->use_empty()) {
659 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
660 X86::R8RegisterClass);
661 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
662 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000663 if (ObjectVT == MVT::i1)
664 // FIXME: Should insert a assertzext here.
665 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000666 }
667 ++NumIntRegs;
668 break;
669 }
670
671 ObjSize = 1;
672 break;
673 case MVT::i16:
674 if (NumIntRegs < 2) {
675 if (!I->use_empty()) {
676 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
677 X86::R16RegisterClass);
678 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
679 DAG.setRoot(ArgValue.getValue(1));
680 }
681 ++NumIntRegs;
682 break;
683 }
684 ObjSize = 2;
685 break;
686 case MVT::i32:
687 if (NumIntRegs < 2) {
688 if (!I->use_empty()) {
689 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
690 X86::R32RegisterClass);
691 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
692 DAG.setRoot(ArgValue.getValue(1));
693 }
694 ++NumIntRegs;
695 break;
696 }
697 ObjSize = 4;
698 break;
699 case MVT::i64:
700 if (NumIntRegs == 0) {
701 if (!I->use_empty()) {
702 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
703 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
704
705 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
706 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
707 DAG.setRoot(Hi.getValue(1));
708
709 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
710 }
711 NumIntRegs = 2;
712 break;
713 } else if (NumIntRegs == 1) {
714 if (!I->use_empty()) {
715 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
716 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
717 DAG.setRoot(Low.getValue(1));
718
719 // Load the high part from memory.
720 // Create the frame index object for this incoming parameter...
721 int FI = MFI->CreateFixedObject(4, ArgOffset);
722 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
723 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
724 DAG.getSrcValue(NULL));
725 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
726 }
727 ArgOffset += 4;
728 NumIntRegs = 2;
729 break;
730 }
731 ObjSize = ArgIncrement = 8;
732 break;
733 case MVT::f32: ObjSize = 4; break;
734 case MVT::f64: ObjSize = ArgIncrement = 8; break;
735 }
736
737 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
738 // dead loads.
739 if (ObjSize && !I->use_empty()) {
740 // Create the frame index object for this incoming parameter...
741 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
742
743 // Create the SelectionDAG nodes corresponding to a load from this
744 // parameter.
745 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
746
747 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
748 DAG.getSrcValue(NULL));
749 } else if (ArgValue.Val == 0) {
750 if (MVT::isInteger(ObjectVT))
751 ArgValue = DAG.getConstant(0, ObjectVT);
752 else
753 ArgValue = DAG.getConstantFP(0, ObjectVT);
754 }
755 ArgValues.push_back(ArgValue);
756
757 if (ObjSize)
758 ArgOffset += ArgIncrement; // Move on to the next argument.
759 }
760
761 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
762 // arguments and the arguments after the retaddr has been pushed are aligned.
763 if ((ArgOffset & 7) == 0)
764 ArgOffset += 4;
765
766 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
767 ReturnAddrIndex = 0; // No return address slot generated yet.
768 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
769 BytesCallerReserves = 0;
770
771 // Finally, inform the code generator which regs we return values in.
772 switch (getValueType(F.getReturnType())) {
773 default: assert(0 && "Unknown type!");
774 case MVT::isVoid: break;
775 case MVT::i1:
776 case MVT::i8:
777 case MVT::i16:
778 case MVT::i32:
779 MF.addLiveOut(X86::EAX);
780 break;
781 case MVT::i64:
782 MF.addLiveOut(X86::EAX);
783 MF.addLiveOut(X86::EDX);
784 break;
785 case MVT::f32:
786 case MVT::f64:
787 MF.addLiveOut(X86::ST0);
788 break;
789 }
790 return ArgValues;
791}
792
793std::pair<SDOperand, SDOperand>
794X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
795 bool isTailCall, SDOperand Callee,
796 ArgListTy &Args, SelectionDAG &DAG) {
797 // Count how many bytes are to be pushed on the stack.
798 unsigned NumBytes = 0;
799
800 // Keep track of the number of integer regs passed so far. This can be either
801 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
802 // used).
803 unsigned NumIntRegs = 0;
804
805 for (unsigned i = 0, e = Args.size(); i != e; ++i)
806 switch (getValueType(Args[i].second)) {
807 default: assert(0 && "Unknown value type!");
808 case MVT::i1:
809 case MVT::i8:
810 case MVT::i16:
811 case MVT::i32:
812 if (NumIntRegs < 2) {
813 ++NumIntRegs;
814 break;
815 }
816 // fall through
817 case MVT::f32:
818 NumBytes += 4;
819 break;
820 case MVT::i64:
821 if (NumIntRegs == 0) {
822 NumIntRegs = 2;
823 break;
824 } else if (NumIntRegs == 1) {
825 NumIntRegs = 2;
826 NumBytes += 4;
827 break;
828 }
829
830 // fall through
831 case MVT::f64:
832 NumBytes += 8;
833 break;
834 }
835
836 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
837 // arguments and the arguments after the retaddr has been pushed are aligned.
838 if ((NumBytes & 7) == 0)
839 NumBytes += 4;
840
841 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
842 DAG.getConstant(NumBytes, getPointerTy()));
843
844 // Arguments go on the stack in reverse order, as specified by the ABI.
845 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000846 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000847 NumIntRegs = 0;
848 std::vector<SDOperand> Stores;
849 std::vector<SDOperand> RegValuesToPass;
850 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
851 switch (getValueType(Args[i].second)) {
852 default: assert(0 && "Unexpected ValueType for argument!");
853 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000854 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
855 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000856 case MVT::i8:
857 case MVT::i16:
858 case MVT::i32:
859 if (NumIntRegs < 2) {
860 RegValuesToPass.push_back(Args[i].first);
861 ++NumIntRegs;
862 break;
863 }
864 // Fall through
865 case MVT::f32: {
866 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
867 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
868 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
869 Args[i].first, PtrOff,
870 DAG.getSrcValue(NULL)));
871 ArgOffset += 4;
872 break;
873 }
874 case MVT::i64:
875 if (NumIntRegs < 2) { // Can pass part of it in regs?
876 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
877 Args[i].first, DAG.getConstant(1, MVT::i32));
878 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
879 Args[i].first, DAG.getConstant(0, MVT::i32));
880 RegValuesToPass.push_back(Lo);
881 ++NumIntRegs;
882 if (NumIntRegs < 2) { // Pass both parts in regs?
883 RegValuesToPass.push_back(Hi);
884 ++NumIntRegs;
885 } else {
886 // Pass the high part in memory.
887 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
888 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
889 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
890 Hi, PtrOff, DAG.getSrcValue(NULL)));
891 ArgOffset += 4;
892 }
893 break;
894 }
895 // Fall through
896 case MVT::f64:
897 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
898 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
899 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
900 Args[i].first, PtrOff,
901 DAG.getSrcValue(NULL)));
902 ArgOffset += 8;
903 break;
904 }
905 }
906 if (!Stores.empty())
907 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
908
909 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
910 // arguments and the arguments after the retaddr has been pushed are aligned.
911 if ((ArgOffset & 7) == 0)
912 ArgOffset += 4;
913
914 std::vector<MVT::ValueType> RetVals;
915 MVT::ValueType RetTyVT = getValueType(RetTy);
916
917 RetVals.push_back(MVT::Other);
918
919 // The result values produced have to be legal. Promote the result.
920 switch (RetTyVT) {
921 case MVT::isVoid: break;
922 default:
923 RetVals.push_back(RetTyVT);
924 break;
925 case MVT::i1:
926 case MVT::i8:
927 case MVT::i16:
928 RetVals.push_back(MVT::i32);
929 break;
930 case MVT::f32:
931 if (X86ScalarSSE)
932 RetVals.push_back(MVT::f32);
933 else
934 RetVals.push_back(MVT::f64);
935 break;
936 case MVT::i64:
937 RetVals.push_back(MVT::i32);
938 RetVals.push_back(MVT::i32);
939 break;
940 }
941
Evan Chenga814f0b32006-01-27 21:26:54 +0000942 if (!X86PatIsel) {
Evan Cheng172fce72006-01-06 00:43:03 +0000943 // Build a sequence of copy-to-reg nodes chained together with token chain
944 // and flag operands which copy the outgoing args into registers.
945 SDOperand InFlag;
946 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
947 unsigned CCReg;
948 SDOperand RegToPass = RegValuesToPass[i];
949 switch (RegToPass.getValueType()) {
950 default: assert(0 && "Bad thing to pass in regs");
951 case MVT::i8:
952 CCReg = (i == 0) ? X86::AL : X86::DL;
953 break;
954 case MVT::i16:
955 CCReg = (i == 0) ? X86::AX : X86::DX;
956 break;
957 case MVT::i32:
958 CCReg = (i == 0) ? X86::EAX : X86::EDX;
959 break;
960 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000961
Evan Cheng172fce72006-01-06 00:43:03 +0000962 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
963 InFlag = Chain.getValue(1);
964 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000965
Evan Cheng172fce72006-01-06 00:43:03 +0000966 std::vector<MVT::ValueType> NodeTys;
967 NodeTys.push_back(MVT::Other); // Returns a chain
968 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Evan Cheng172fce72006-01-06 00:43:03 +0000969 std::vector<SDOperand> Ops;
970 Ops.push_back(Chain);
971 Ops.push_back(Callee);
972 if (InFlag.Val)
973 Ops.push_back(InFlag);
974
975 // FIXME: Do not generate X86ISD::TAILCALL for now.
976 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
977 InFlag = Chain.getValue(1);
978
Chris Lattner6f33eae2006-01-24 05:17:12 +0000979 NodeTys.clear();
980 NodeTys.push_back(MVT::Other); // Returns a chain
981 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
982 Ops.clear();
983 Ops.push_back(Chain);
984 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
985 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
986 Ops.push_back(InFlag);
987 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
988 InFlag = Chain.getValue(1);
989
Evan Cheng172fce72006-01-06 00:43:03 +0000990 SDOperand RetVal;
991 if (RetTyVT != MVT::isVoid) {
992 switch (RetTyVT) {
993 default: assert(0 && "Unknown value type to return!");
994 case MVT::i1:
995 case MVT::i8:
996 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
997 Chain = RetVal.getValue(1);
Evan Cheng4b3774e2006-01-18 08:08:38 +0000998 if (RetTyVT == MVT::i1)
999 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001000 break;
1001 case MVT::i16:
1002 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1003 Chain = RetVal.getValue(1);
1004 break;
1005 case MVT::i32:
1006 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1007 Chain = RetVal.getValue(1);
1008 break;
1009 case MVT::i64: {
1010 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1011 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1012 Lo.getValue(2));
1013 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1014 Chain = Hi.getValue(1);
1015 break;
1016 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001017 case MVT::f32:
Evan Cheng172fce72006-01-06 00:43:03 +00001018 case MVT::f64: {
1019 std::vector<MVT::ValueType> Tys;
1020 Tys.push_back(MVT::f64);
1021 Tys.push_back(MVT::Other);
Evan Chengbec9d722006-01-17 00:19:47 +00001022 Tys.push_back(MVT::Flag);
Evan Cheng172fce72006-01-06 00:43:03 +00001023 std::vector<SDOperand> Ops;
1024 Ops.push_back(Chain);
1025 Ops.push_back(InFlag);
1026 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
Evan Chengbec9d722006-01-17 00:19:47 +00001027 Chain = RetVal.getValue(1);
1028 InFlag = RetVal.getValue(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001029 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001030 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1031 // shouldn't be necessary except that RFP cannot be live across
Evan Cheng561881f2006-01-17 00:37:42 +00001032 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Evan Cheng172fce72006-01-06 00:43:03 +00001033 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001034 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Evan Cheng172fce72006-01-06 00:43:03 +00001035 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1036 Tys.clear();
1037 Tys.push_back(MVT::Other);
1038 Ops.clear();
1039 Ops.push_back(Chain);
1040 Ops.push_back(RetVal);
1041 Ops.push_back(StackSlot);
1042 Ops.push_back(DAG.getValueType(RetTyVT));
Evan Chengbec9d722006-01-17 00:19:47 +00001043 Ops.push_back(InFlag);
Evan Cheng172fce72006-01-06 00:43:03 +00001044 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1045 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1046 DAG.getSrcValue(NULL));
1047 Chain = RetVal.getValue(1);
Evan Chengbec9d722006-01-17 00:19:47 +00001048 }
Evan Chengfeaed4d2006-01-17 21:58:21 +00001049
1050 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1051 // FIXME: we would really like to remember that this FP_ROUND
1052 // operation is okay to eliminate if we allow excess FP precision.
1053 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
Evan Cheng172fce72006-01-06 00:43:03 +00001054 break;
1055 }
1056 }
1057 }
1058
Evan Cheng172fce72006-01-06 00:43:03 +00001059 return std::make_pair(RetVal, Chain);
1060 } else {
1061 std::vector<SDOperand> Ops;
1062 Ops.push_back(Chain);
1063 Ops.push_back(Callee);
1064 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1065 // Callee pops all arg values on the stack.
1066 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1067
1068 // Pass register arguments as needed.
1069 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
1070
1071 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1072 RetVals, Ops);
1073 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
1074
1075 SDOperand ResultVal;
1076 switch (RetTyVT) {
1077 case MVT::isVoid: break;
1078 default:
1079 ResultVal = TheCall.getValue(1);
1080 break;
1081 case MVT::i1:
1082 case MVT::i8:
1083 case MVT::i16:
1084 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
1085 break;
1086 case MVT::f32:
1087 // FIXME: we would really like to remember that this FP_ROUND operation is
1088 // okay to eliminate if we allow excess FP precision.
1089 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
1090 break;
1091 case MVT::i64:
1092 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
1093 TheCall.getValue(2));
1094 break;
1095 }
1096
1097 return std::make_pair(ResultVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001098 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001099}
1100
1101SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1102 if (ReturnAddrIndex == 0) {
1103 // Set up a frame object for the return address.
1104 MachineFunction &MF = DAG.getMachineFunction();
1105 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1106 }
1107
1108 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1109}
1110
1111
1112
1113std::pair<SDOperand, SDOperand> X86TargetLowering::
1114LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1115 SelectionDAG &DAG) {
1116 SDOperand Result;
1117 if (Depth) // Depths > 0 not supported yet!
1118 Result = DAG.getConstant(0, getPointerTy());
1119 else {
1120 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1121 if (!isFrameAddress)
1122 // Just load the return address
1123 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1124 DAG.getSrcValue(NULL));
1125 else
1126 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1127 DAG.getConstant(4, MVT::i32));
1128 }
1129 return std::make_pair(Result, Chain);
1130}
1131
Evan Cheng339edad2006-01-11 00:33:36 +00001132/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1133/// which corresponds to the condition code.
1134static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1135 switch (X86CC) {
1136 default: assert(0 && "Unknown X86 conditional code!");
1137 case X86ISD::COND_A: return X86::JA;
1138 case X86ISD::COND_AE: return X86::JAE;
1139 case X86ISD::COND_B: return X86::JB;
1140 case X86ISD::COND_BE: return X86::JBE;
1141 case X86ISD::COND_E: return X86::JE;
1142 case X86ISD::COND_G: return X86::JG;
1143 case X86ISD::COND_GE: return X86::JGE;
1144 case X86ISD::COND_L: return X86::JL;
1145 case X86ISD::COND_LE: return X86::JLE;
1146 case X86ISD::COND_NE: return X86::JNE;
1147 case X86ISD::COND_NO: return X86::JNO;
1148 case X86ISD::COND_NP: return X86::JNP;
1149 case X86ISD::COND_NS: return X86::JNS;
1150 case X86ISD::COND_O: return X86::JO;
1151 case X86ISD::COND_P: return X86::JP;
1152 case X86ISD::COND_S: return X86::JS;
1153 }
1154}
Chris Lattner76ac0682005-11-15 00:40:23 +00001155
Evan Cheng339edad2006-01-11 00:33:36 +00001156/// getX86CC - do a one to one translation of a ISD::CondCode to the X86
1157/// specific condition code. It returns a X86ISD::COND_INVALID if it cannot
Evan Cheng172fce72006-01-06 00:43:03 +00001158/// do a direct translation.
Evan Cheng339edad2006-01-11 00:33:36 +00001159static unsigned getX86CC(SDOperand CC, bool isFP) {
Evan Cheng172fce72006-01-06 00:43:03 +00001160 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1161 unsigned X86CC = X86ISD::COND_INVALID;
1162 if (!isFP) {
1163 switch (SetCCOpcode) {
1164 default: break;
1165 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1166 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1167 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1168 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1169 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1170 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1171 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1172 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1173 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1174 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1175 }
1176 } else {
1177 // On a floating point condition, the flags are set as follows:
1178 // ZF PF CF op
1179 // 0 | 0 | 0 | X > Y
1180 // 0 | 0 | 1 | X < Y
1181 // 1 | 0 | 0 | X == Y
1182 // 1 | 1 | 1 | unordered
1183 switch (SetCCOpcode) {
1184 default: break;
1185 case ISD::SETUEQ:
1186 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1187 case ISD::SETOGT:
1188 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1189 case ISD::SETOGE:
1190 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1191 case ISD::SETULT:
1192 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1193 case ISD::SETULE:
1194 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1195 case ISD::SETONE:
1196 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1197 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1198 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1199 }
1200 }
1201 return X86CC;
1202}
1203
Evan Cheng339edad2006-01-11 00:33:36 +00001204/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1205/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001206/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001207static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001208 switch (X86CC) {
1209 default:
1210 return false;
1211 case X86ISD::COND_B:
1212 case X86ISD::COND_BE:
1213 case X86ISD::COND_E:
1214 case X86ISD::COND_P:
1215 case X86ISD::COND_A:
1216 case X86ISD::COND_AE:
1217 case X86ISD::COND_NE:
1218 case X86ISD::COND_NP:
1219 return true;
1220 }
1221}
1222
Evan Cheng339edad2006-01-11 00:33:36 +00001223MachineBasicBlock *
1224X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1225 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001226 switch (MI->getOpcode()) {
1227 default: assert(false && "Unexpected instr type to insert");
1228 case X86::CMOV_FR32:
1229 case X86::CMOV_FR64: {
1230 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1231 // control-flow pattern. The incoming instruction knows the destination vreg
1232 // to set, the condition code register to branch on, the true/false values to
1233 // select between, and a branch opcode to use.
1234 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1235 ilist<MachineBasicBlock>::iterator It = BB;
1236 ++It;
1237
1238 // thisMBB:
1239 // ...
1240 // TrueVal = ...
1241 // cmpTY ccX, r1, r2
1242 // bCC copy1MBB
1243 // fallthrough --> copy0MBB
1244 MachineBasicBlock *thisMBB = BB;
1245 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1246 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1247 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1248 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1249 MachineFunction *F = BB->getParent();
1250 F->getBasicBlockList().insert(It, copy0MBB);
1251 F->getBasicBlockList().insert(It, sinkMBB);
1252 // Update machine-CFG edges
1253 BB->addSuccessor(copy0MBB);
1254 BB->addSuccessor(sinkMBB);
1255
1256 // copy0MBB:
1257 // %FalseValue = ...
1258 // # fallthrough to sinkMBB
1259 BB = copy0MBB;
1260
1261 // Update machine-CFG edges
1262 BB->addSuccessor(sinkMBB);
1263
1264 // sinkMBB:
1265 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1266 // ...
1267 BB = sinkMBB;
1268 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1269 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1270 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001271
Evan Cheng911c68d2006-01-16 21:21:29 +00001272 delete MI; // The pseudo instruction is gone now.
1273 return BB;
1274 }
Evan Cheng339edad2006-01-11 00:33:36 +00001275
Evan Cheng911c68d2006-01-16 21:21:29 +00001276 case X86::FP_TO_INT16_IN_MEM:
1277 case X86::FP_TO_INT32_IN_MEM:
1278 case X86::FP_TO_INT64_IN_MEM: {
1279 // Change the floating point control register to use "round towards zero"
1280 // mode when truncating to an integer value.
1281 MachineFunction *F = BB->getParent();
1282 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1283 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1284
1285 // Load the old value of the high byte of the control word...
1286 unsigned OldCW =
1287 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1288 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1289
1290 // Set the high part to be round to zero...
1291 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1292
1293 // Reload the modified control word now...
1294 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1295
1296 // Restore the memory image of control word to original value
1297 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1298
1299 // Get the X86 opcode to use.
1300 unsigned Opc;
1301 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001302 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001303 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1304 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1305 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1306 }
1307
1308 X86AddressMode AM;
1309 MachineOperand &Op = MI->getOperand(0);
1310 if (Op.isRegister()) {
1311 AM.BaseType = X86AddressMode::RegBase;
1312 AM.Base.Reg = Op.getReg();
1313 } else {
1314 AM.BaseType = X86AddressMode::FrameIndexBase;
1315 AM.Base.FrameIndex = Op.getFrameIndex();
1316 }
1317 Op = MI->getOperand(1);
1318 if (Op.isImmediate())
1319 AM.Scale = Op.getImmedValue();
1320 Op = MI->getOperand(2);
1321 if (Op.isImmediate())
1322 AM.IndexReg = Op.getImmedValue();
1323 Op = MI->getOperand(3);
1324 if (Op.isGlobalAddress()) {
1325 AM.GV = Op.getGlobal();
1326 } else {
1327 AM.Disp = Op.getImmedValue();
1328 }
1329 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1330
1331 // Reload the original control word now.
1332 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1333
1334 delete MI; // The pseudo instruction is gone now.
1335 return BB;
1336 }
1337 }
Evan Cheng339edad2006-01-11 00:33:36 +00001338}
1339
1340
1341//===----------------------------------------------------------------------===//
1342// X86 Custom Lowering Hooks
1343//===----------------------------------------------------------------------===//
1344
Chris Lattner76ac0682005-11-15 00:40:23 +00001345/// LowerOperation - Provide custom lowering hooks for some operations.
1346///
1347SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1348 switch (Op.getOpcode()) {
1349 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001350 case ISD::ADD_PARTS:
1351 case ISD::SUB_PARTS: {
1352 assert(Op.getNumOperands() == 4 && Op.getValueType() == MVT::i32 &&
1353 "Not an i64 add/sub!");
1354 bool isAdd = Op.getOpcode() == ISD::ADD_PARTS;
1355 std::vector<MVT::ValueType> Tys;
1356 Tys.push_back(MVT::i32);
1357 Tys.push_back(MVT::Flag);
1358 std::vector<SDOperand> Ops;
1359 Ops.push_back(Op.getOperand(0));
1360 Ops.push_back(Op.getOperand(2));
1361 SDOperand Lo = DAG.getNode(isAdd ? X86ISD::ADD_FLAG : X86ISD::SUB_FLAG,
1362 Tys, Ops);
1363 SDOperand Hi = DAG.getNode(isAdd ? X86ISD::ADC : X86ISD::SBB, MVT::i32,
1364 Op.getOperand(1), Op.getOperand(3),
1365 Lo.getValue(1));
1366 Tys.clear();
1367 Tys.push_back(MVT::i32);
1368 Tys.push_back(MVT::i32);
1369 Ops.clear();
1370 Ops.push_back(Lo);
1371 Ops.push_back(Hi);
1372 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1373 }
1374 case ISD::SHL_PARTS:
1375 case ISD::SRA_PARTS:
1376 case ISD::SRL_PARTS: {
1377 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1378 "Not an i64 shift!");
1379 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1380 SDOperand ShOpLo = Op.getOperand(0);
1381 SDOperand ShOpHi = Op.getOperand(1);
1382 SDOperand ShAmt = Op.getOperand(2);
1383 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001384 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001385 : DAG.getConstant(0, MVT::i32);
1386
1387 SDOperand Tmp2, Tmp3;
1388 if (Op.getOpcode() == ISD::SHL_PARTS) {
1389 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1390 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1391 } else {
1392 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001393 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001394 }
1395
1396 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1397 ShAmt, DAG.getConstant(32, MVT::i8));
1398
1399 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001400 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001401
1402 std::vector<MVT::ValueType> Tys;
1403 Tys.push_back(MVT::i32);
1404 Tys.push_back(MVT::Flag);
1405 std::vector<SDOperand> Ops;
1406 if (Op.getOpcode() == ISD::SHL_PARTS) {
1407 Ops.push_back(Tmp2);
1408 Ops.push_back(Tmp3);
1409 Ops.push_back(CC);
1410 Ops.push_back(InFlag);
1411 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1412 InFlag = Hi.getValue(1);
1413
1414 Ops.clear();
1415 Ops.push_back(Tmp3);
1416 Ops.push_back(Tmp1);
1417 Ops.push_back(CC);
1418 Ops.push_back(InFlag);
1419 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1420 } else {
1421 Ops.push_back(Tmp2);
1422 Ops.push_back(Tmp3);
1423 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001424 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001425 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1426 InFlag = Lo.getValue(1);
1427
1428 Ops.clear();
1429 Ops.push_back(Tmp3);
1430 Ops.push_back(Tmp1);
1431 Ops.push_back(CC);
1432 Ops.push_back(InFlag);
1433 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1434 }
1435
1436 Tys.clear();
1437 Tys.push_back(MVT::i32);
1438 Tys.push_back(MVT::i32);
1439 Ops.clear();
1440 Ops.push_back(Lo);
1441 Ops.push_back(Hi);
1442 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1443 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001444 case ISD::SINT_TO_FP: {
1445 assert(Op.getValueType() == MVT::f64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001446 Op.getOperand(0).getValueType() <= MVT::i64 &&
1447 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001448 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001449
1450 SDOperand Result;
1451 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1452 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001453 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001454 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001455 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001456 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1457 DAG.getEntryNode(), Op.getOperand(0),
1458 StackSlot, DAG.getSrcValue(NULL));
1459
1460 // Build the FILD
1461 std::vector<MVT::ValueType> Tys;
1462 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001463 Tys.push_back(MVT::Other);
Evan Cheng6305e502006-01-12 22:54:21 +00001464 Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001465 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001466 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001467 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001468 Ops.push_back(DAG.getValueType(SrcVT));
1469 Result = DAG.getNode(X86ISD::FILD, Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001470
1471 if (X86ScalarSSE) {
1472 assert(Op.getValueType() == MVT::f64 && "Invalid SINT_TO_FP to lower!");
1473 Chain = Result.getValue(1);
1474 SDOperand InFlag = Result.getValue(2);
1475
1476 // FIXME: Currently the FST is flagged to the FILD. This
1477 // shouldn't be necessary except that RFP cannot be live across
1478 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1479 MachineFunction &MF = DAG.getMachineFunction();
1480 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1481 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1482 std::vector<MVT::ValueType> Tys;
1483 Tys.push_back(MVT::Other);
1484 std::vector<SDOperand> Ops;
1485 Ops.push_back(Chain);
1486 Ops.push_back(Result);
1487 Ops.push_back(StackSlot);
1488 Ops.push_back(DAG.getValueType(MVT::f64));
1489 Ops.push_back(InFlag);
1490 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1491 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1492 DAG.getSrcValue(NULL));
1493 }
1494
Evan Cheng6305e502006-01-12 22:54:21 +00001495 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001496 }
1497 case ISD::FP_TO_SINT: {
1498 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1499 Op.getOperand(0).getValueType() == MVT::f64 &&
1500 "Unknown FP_TO_SINT to lower!");
1501 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1502 // stack slot.
1503 MachineFunction &MF = DAG.getMachineFunction();
1504 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1505 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1506 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1507
1508 unsigned Opc;
1509 switch (Op.getValueType()) {
1510 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1511 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1512 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1513 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1514 }
1515
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001516 SDOperand Chain = DAG.getEntryNode();
1517 SDOperand Value = Op.getOperand(0);
1518 if (X86ScalarSSE) {
1519 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1520 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1521 DAG.getSrcValue(0));
1522 std::vector<MVT::ValueType> Tys;
1523 Tys.push_back(MVT::f64);
1524 Tys.push_back(MVT::Other);
1525 std::vector<SDOperand> Ops;
1526 Ops.push_back(Chain);
1527 Ops.push_back(StackSlot);
1528 Ops.push_back(DAG.getValueType(MVT::f64));
1529 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1530 Chain = Value.getValue(1);
1531 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1532 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1533 }
1534
Chris Lattner76ac0682005-11-15 00:40:23 +00001535 // Build the FP_TO_INT*_IN_MEM
1536 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001537 Ops.push_back(Chain);
1538 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001539 Ops.push_back(StackSlot);
1540 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1541
1542 // Load the result.
1543 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1544 DAG.getSrcValue(NULL));
1545 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001546 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001547 std::vector<MVT::ValueType> Tys;
1548 Tys.push_back(MVT::Other);
1549 Tys.push_back(MVT::Flag);
1550 std::vector<SDOperand> Ops;
1551 Ops.push_back(Op.getOperand(0));
1552 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001553 Ops.clear();
1554 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1555 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1556 MVT::i32, Ops[0].getValue(2)));
1557 Ops.push_back(Ops[1].getValue(1));
1558 Tys[0] = Tys[1] = MVT::i32;
1559 Tys.push_back(MVT::Other);
1560 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001561 }
Evan Chengc1583db2005-12-21 20:21:51 +00001562 case ISD::SETCC: {
1563 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1564 SDOperand CC = Op.getOperand(2);
1565 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1566 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001567 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1568 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001569 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001570 if (X86CC != X86ISD::COND_INVALID) {
1571 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1572 DAG.getConstant(X86CC, MVT::i8), Cond);
1573 } else {
1574 assert(isFP && "Illegal integer SetCC!");
1575
1576 std::vector<MVT::ValueType> Tys;
1577 std::vector<SDOperand> Ops;
1578 switch (SetCCOpcode) {
1579 default: assert(false && "Illegal floating point SetCC!");
1580 case ISD::SETOEQ: { // !PF & ZF
1581 Tys.push_back(MVT::i8);
1582 Tys.push_back(MVT::Flag);
1583 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1584 Ops.push_back(Cond);
1585 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1586 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1587 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1588 Tmp1.getValue(1));
1589 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1590 }
1591 case ISD::SETOLT: { // !PF & CF
1592 Tys.push_back(MVT::i8);
1593 Tys.push_back(MVT::Flag);
1594 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1595 Ops.push_back(Cond);
1596 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1597 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1598 DAG.getConstant(X86ISD::COND_B, MVT::i8),
1599 Tmp1.getValue(1));
1600 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1601 }
1602 case ISD::SETOLE: { // !PF & (CF || ZF)
1603 Tys.push_back(MVT::i8);
1604 Tys.push_back(MVT::Flag);
1605 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1606 Ops.push_back(Cond);
1607 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1608 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1609 DAG.getConstant(X86ISD::COND_BE, MVT::i8),
1610 Tmp1.getValue(1));
1611 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1612 }
1613 case ISD::SETUGT: { // PF | (!ZF & !CF)
1614 Tys.push_back(MVT::i8);
1615 Tys.push_back(MVT::Flag);
1616 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1617 Ops.push_back(Cond);
1618 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1619 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1620 DAG.getConstant(X86ISD::COND_A, MVT::i8),
1621 Tmp1.getValue(1));
1622 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1623 }
1624 case ISD::SETUGE: { // PF | !CF
1625 Tys.push_back(MVT::i8);
1626 Tys.push_back(MVT::Flag);
1627 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1628 Ops.push_back(Cond);
1629 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1630 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1631 DAG.getConstant(X86ISD::COND_AE, MVT::i8),
1632 Tmp1.getValue(1));
1633 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1634 }
1635 case ISD::SETUNE: { // PF | !ZF
1636 Tys.push_back(MVT::i8);
1637 Tys.push_back(MVT::Flag);
1638 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1639 Ops.push_back(Cond);
1640 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1641 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1642 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1643 Tmp1.getValue(1));
1644 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1645 }
1646 }
1647 }
Evan Chengc1583db2005-12-21 20:21:51 +00001648 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001649 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001650 MVT::ValueType VT = Op.getValueType();
1651 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001652 bool isFPStack = isFP && !X86ScalarSSE;
1653 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001654 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001655 SDOperand Op0 = Op.getOperand(0);
1656 SDOperand Cond, CC;
1657 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001658 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1659 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1660 // have another use it will be eliminated.
1661 // If the X86ISD::SETCC has more than one use, then it's probably better
1662 // to use a test instead of duplicating the X86ISD::CMP (for register
1663 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001664 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1665 if (!Op0.hasOneUse()) {
1666 std::vector<MVT::ValueType> Tys;
1667 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1668 Tys.push_back(Op0.Val->getValueType(i));
1669 std::vector<SDOperand> Ops;
1670 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1671 Ops.push_back(Op0.getOperand(i));
1672 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1673 }
1674
Evan Chengfb22e862006-01-13 01:03:02 +00001675 CC = Op0.getOperand(0);
1676 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001677 // Make a copy as flag result cannot be used by more than one.
1678 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1679 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001680 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001681 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001682 } else
1683 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001684 } else if (Op0.getOpcode() == ISD::SETCC) {
1685 CC = Op0.getOperand(2);
1686 bool isFP = MVT::isFloatingPoint(Op0.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001687 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001688 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng225a4d02005-12-17 01:21:05 +00001689 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng73a1ad92006-01-10 20:26:56 +00001690 Op0.getOperand(0), Op0.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001691 } else
1692 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001693
Evan Cheng731423f2006-01-13 01:06:49 +00001694 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001695 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001696 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001697 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001698
1699 std::vector<MVT::ValueType> Tys;
1700 Tys.push_back(Op.getValueType());
1701 Tys.push_back(MVT::Flag);
1702 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001703 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1704 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001705 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001706 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001707 Ops.push_back(CC);
1708 Ops.push_back(Cond);
1709 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001710 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001711 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001712 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001713 SDOperand Cond = Op.getOperand(1);
1714 SDOperand Dest = Op.getOperand(2);
1715 SDOperand CC;
Evan Chengc1583db2005-12-21 20:21:51 +00001716 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001717 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1718 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1719 // have another use it will be eliminated.
1720 // If the X86ISD::SETCC has more than one use, then it's probably better
1721 // to use a test instead of duplicating the X86ISD::CMP (for register
1722 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001723 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1724 if (!Cond.hasOneUse()) {
1725 std::vector<MVT::ValueType> Tys;
1726 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1727 Tys.push_back(Cond.Val->getValueType(i));
1728 std::vector<SDOperand> Ops;
1729 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1730 Ops.push_back(Cond.getOperand(i));
1731 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1732 }
1733
Evan Chengfb22e862006-01-13 01:03:02 +00001734 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001735 Cond = Cond.getOperand(1);
1736 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001737 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001738 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001739 } else
1740 addTest = true;
Evan Chengc1583db2005-12-21 20:21:51 +00001741 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng6fc31042005-12-19 23:12:38 +00001742 CC = Cond.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001743 bool isFP = MVT::isFloatingPoint(Cond.getOperand(1).getValueType());
Evan Cheng339edad2006-01-11 00:33:36 +00001744 unsigned X86CC = getX86CC(CC, isFP);
Evan Cheng172fce72006-01-06 00:43:03 +00001745 CC = DAG.getConstant(X86CC, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001746 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1747 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001748 } else
1749 addTest = true;
1750
1751 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001752 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001753 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1754 }
1755 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1756 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1757 }
Evan Chengae986f12006-01-11 22:15:48 +00001758 case ISD::MEMSET: {
1759 SDOperand InFlag;
1760 SDOperand Chain = Op.getOperand(0);
1761 unsigned Align =
1762 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1763 if (Align == 0) Align = 1;
1764
1765 MVT::ValueType AVT;
1766 SDOperand Count;
1767 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1768 unsigned ValReg;
1769 unsigned Val = ValC->getValue() & 255;
1770
1771 // If the value is a constant, then we can potentially use larger sets.
1772 switch (Align & 3) {
1773 case 2: // WORD aligned
1774 AVT = MVT::i16;
1775 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1776 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1777 else
1778 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1779 DAG.getConstant(1, MVT::i8));
1780 Val = (Val << 8) | Val;
1781 ValReg = X86::AX;
1782 break;
1783 case 0: // DWORD aligned
1784 AVT = MVT::i32;
1785 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1786 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1787 else
1788 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1789 DAG.getConstant(2, MVT::i8));
1790 Val = (Val << 8) | Val;
1791 Val = (Val << 16) | Val;
1792 ValReg = X86::EAX;
1793 break;
1794 default: // Byte aligned
1795 AVT = MVT::i8;
1796 Count = Op.getOperand(3);
1797 ValReg = X86::AL;
1798 break;
1799 }
1800
1801 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1802 InFlag);
1803 InFlag = Chain.getValue(1);
1804 } else {
1805 AVT = MVT::i8;
1806 Count = Op.getOperand(3);
1807 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1808 InFlag = Chain.getValue(1);
1809 }
1810
1811 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1812 InFlag = Chain.getValue(1);
1813 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1814 InFlag = Chain.getValue(1);
1815
1816 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1817 DAG.getValueType(AVT), InFlag);
1818 }
1819 case ISD::MEMCPY: {
1820 SDOperand Chain = Op.getOperand(0);
1821 unsigned Align =
1822 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1823 if (Align == 0) Align = 1;
1824
1825 MVT::ValueType AVT;
1826 SDOperand Count;
1827 switch (Align & 3) {
1828 case 2: // WORD aligned
1829 AVT = MVT::i16;
1830 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1831 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1832 else
1833 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1834 DAG.getConstant(1, MVT::i8));
1835 break;
1836 case 0: // DWORD aligned
1837 AVT = MVT::i32;
1838 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
1839 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1840 else
1841 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1842 DAG.getConstant(2, MVT::i8));
1843 break;
1844 default: // Byte aligned
1845 AVT = MVT::i8;
1846 Count = Op.getOperand(3);
1847 break;
1848 }
1849
1850 SDOperand InFlag;
1851 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1852 InFlag = Chain.getValue(1);
1853 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1854 InFlag = Chain.getValue(1);
1855 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1856 InFlag = Chain.getValue(1);
1857
1858 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1859 DAG.getValueType(AVT), InFlag);
1860 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001861 case ISD::GlobalAddress: {
Evan Chengb94db9e2006-01-12 07:56:47 +00001862 SDOperand Result;
Evan Chenga74ce622005-12-21 02:39:21 +00001863 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1864 // For Darwin, external and weak symbols are indirect, so we want to load
1865 // the value at address GV, not the value of GV itself. This means that
1866 // the GlobalAddress must be in the base or index register of the address,
1867 // not the GV offset field.
1868 if (getTargetMachine().
1869 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1870 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Chengb94db9e2006-01-12 07:56:47 +00001871 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1872 DAG.getTargetGlobalAddress(GV, getPointerTy()),
1873 DAG.getSrcValue(NULL));
1874 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001875 }
Nate Begemane74795c2006-01-25 18:21:52 +00001876 case ISD::VASTART: {
1877 // vastart just stores the address of the VarArgsFrameIndex slot into the
1878 // memory location argument.
1879 // FIXME: Replace MVT::i32 with PointerTy
1880 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1881 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1882 Op.getOperand(1), Op.getOperand(2));
1883 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001884 case ISD::RET: {
1885 SDOperand Copy;
1886
1887 switch(Op.getNumOperands()) {
1888 default:
1889 assert(0 && "Do not know how to return this many arguments!");
1890 abort();
1891 case 1:
1892 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1893 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1894 case 2: {
1895 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1896 if (MVT::isInteger(ArgVT))
1897 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1898 SDOperand());
1899 else if (!X86ScalarSSE) {
1900 std::vector<MVT::ValueType> Tys;
1901 Tys.push_back(MVT::Other);
1902 Tys.push_back(MVT::Flag);
1903 std::vector<SDOperand> Ops;
1904 Ops.push_back(Op.getOperand(0));
1905 Ops.push_back(Op.getOperand(1));
1906 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1907 } else {
1908 // Spill the value to memory and reload it into top of stack.
1909 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1910 MachineFunction &MF = DAG.getMachineFunction();
1911 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1912 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1913 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1914 Op.getOperand(1), StackSlot,
1915 DAG.getSrcValue(0));
1916 std::vector<MVT::ValueType> Tys;
1917 Tys.push_back(MVT::f64);
1918 Tys.push_back(MVT::Other);
1919 std::vector<SDOperand> Ops;
1920 Ops.push_back(Chain);
1921 Ops.push_back(StackSlot);
1922 Ops.push_back(DAG.getValueType(ArgVT));
1923 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1924 Tys.clear();
1925 Tys.push_back(MVT::Other);
1926 Tys.push_back(MVT::Flag);
1927 Ops.clear();
1928 Ops.push_back(Copy.getValue(1));
1929 Ops.push_back(Copy);
1930 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1931 }
1932 break;
1933 }
1934 case 3:
1935 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1936 SDOperand());
1937 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1938 break;
1939 }
1940 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1941 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1942 Copy.getValue(1));
1943 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001944 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001945}
Evan Cheng6af02632005-12-20 06:22:03 +00001946
1947const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1948 switch (Opcode) {
1949 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00001950 case X86ISD::ADD_FLAG: return "X86ISD::ADD_FLAG";
1951 case X86ISD::SUB_FLAG: return "X86ISD::SUB_FLAG";
1952 case X86ISD::ADC: return "X86ISD::ADC";
1953 case X86ISD::SBB: return "X86ISD::SBB";
1954 case X86ISD::SHLD: return "X86ISD::SHLD";
1955 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng6305e502006-01-12 22:54:21 +00001956 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng6af02632005-12-20 06:22:03 +00001957 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1958 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1959 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00001960 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00001961 case X86ISD::FST: return "X86ISD::FST";
1962 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00001963 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00001964 case X86ISD::CALL: return "X86ISD::CALL";
1965 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1966 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1967 case X86ISD::CMP: return "X86ISD::CMP";
1968 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00001969 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00001970 case X86ISD::CMOV: return "X86ISD::CMOV";
1971 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00001972 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Chengae986f12006-01-11 22:15:48 +00001973 case X86ISD::REP_STOS: return "X86ISD::RET_STOS";
1974 case X86ISD::REP_MOVS: return "X86ISD::RET_MOVS";
Evan Cheng6af02632005-12-20 06:22:03 +00001975 }
1976}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001977
1978bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerf0b24d22006-01-30 04:09:27 +00001979 uint64_t Mask) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001980
1981 unsigned Opc = Op.getOpcode();
1982
1983 switch (Opc) {
1984 default:
1985 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1986 break;
1987 case X86ISD::SETCC: return (Mask & 1) == 0;
1988 }
1989
1990 return false;
1991}