blob: eaabfd6a96cd542c12e2ffbaef5115d9f839a8b5 [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"
Evan Cheng72d5c252006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/Function.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000027#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnerc642aa52006-01-31 19:43:35 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000030using namespace llvm;
31
32// FIXME: temporary.
33#include "llvm/Support/CommandLine.h"
34static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
36
37X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000039 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
41
Chris Lattner76ac0682005-11-15 00:40:23 +000042 // Set up the TargetLowering object.
43
44 // X86 is weird, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
47 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000048 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000049 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000050 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000051
Chris Lattner76ac0682005-11-15 00:40:23 +000052 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58 // operation.
59 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
61 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000062
63 if (X86ScalarSSE)
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
66 else
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000068
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70 // this operation.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000073 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000074 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000075 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000076 else {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
79 }
Chris Lattner76ac0682005-11-15 00:40:23 +000080
Evan Cheng5b97fcf2006-01-30 08:02:57 +000081 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82 // isn't legal.
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
85
Evan Cheng08390f62006-01-30 22:13:22 +000086 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
90
91 if (X86ScalarSSE) {
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
93 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +000095 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +000096 }
97
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
99 // conversion.
100 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
102 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
103
Evan Chengd13778e2006-02-18 07:26:17 +0000104 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000105 // Expand FP_TO_UINT into a select.
106 // FIXME: We would like to use a Custom expander here eventually to do
107 // the optimal thing for SSE vs. the default expansion in the legalizer.
108 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
109 else
Evan Chengd13778e2006-02-18 07:26:17 +0000110 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
112
Evan Cheng08390f62006-01-30 22:13:22 +0000113 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
114 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000115
Evan Cheng593bea72006-02-17 07:01:52 +0000116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000117 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
118 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000119 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
120 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000121 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
125 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
126 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
127 setOperationAction(ISD::FREM , MVT::f64 , Expand);
128 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
129 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
130 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
132 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
133 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
136 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000137 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000138 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000139
Chris Lattner76ac0682005-11-15 00:40:23 +0000140 // These should be promoted to a larger select which is supported.
141 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
142 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000143
144 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000145 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
146 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
147 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
148 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
149 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
150 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
151 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
152 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
153 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000154 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000155 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000156 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000157 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000158 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000159 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000160 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000161 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
162 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
163 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000164 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000165 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
166 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000167
Chris Lattner9c415362005-11-29 06:16:21 +0000168 // We don't have line number support yet.
169 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000170 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
171 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000172
Nate Begemane74795c2006-01-25 18:21:52 +0000173 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
174 setOperationAction(ISD::VASTART , MVT::Other, Custom);
175
176 // Use the default implementation.
177 setOperationAction(ISD::VAARG , MVT::Other, Expand);
178 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
179 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000180 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
181 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
182 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000183
Chris Lattner9c7f5032006-03-05 05:08:37 +0000184 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
185 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
186
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
Evan Cheng72d5c252006-01-31 22:28:30 +0000196 // Use ANDPD to simulate FABS.
197 setOperationAction(ISD::FABS , MVT::f64, Custom);
198 setOperationAction(ISD::FABS , MVT::f32, Custom);
199
200 // Use XORP to simulate FNEG.
201 setOperationAction(ISD::FNEG , MVT::f64, Custom);
202 setOperationAction(ISD::FNEG , MVT::f32, Custom);
203
Evan Chengd8fba3a2006-02-02 00:28:23 +0000204 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000205 setOperationAction(ISD::FSIN , MVT::f64, Expand);
206 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000207 setOperationAction(ISD::FREM , MVT::f64, Expand);
208 setOperationAction(ISD::FSIN , MVT::f32, Expand);
209 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000210 setOperationAction(ISD::FREM , MVT::f32, Expand);
211
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000212 // Expand FP immediates into loads from the stack, except for the special
213 // cases we handle.
214 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
215 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000216 addLegalFPImmediate(+0.0); // xorps / xorpd
217 } else {
218 // Set up the FP register classes.
219 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000220
221 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
222
Chris Lattner76ac0682005-11-15 00:40:23 +0000223 if (!UnsafeFPMath) {
224 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
225 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
226 }
227
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000228 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000229 addLegalFPImmediate(+0.0); // FLD0
230 addLegalFPImmediate(+1.0); // FLD1
231 addLegalFPImmediate(-0.0); // FLD0/FCHS
232 addLegalFPImmediate(-1.0); // FLD1/FCHS
233 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000234
Evan Cheng19264272006-03-01 01:11:20 +0000235 // First set operation action for all vector types to expand. Then we
236 // will selectively turn on ones that can be effectively codegen'd.
237 for (unsigned VT = (unsigned)MVT::Vector + 1;
238 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
239 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
240 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
241 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
242 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
243 }
244
Evan Cheng9e252e32006-02-22 02:26:30 +0000245 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
246 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
247 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
248 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
249
Evan Cheng19264272006-03-01 01:11:20 +0000250 // FIXME: add MMX packed arithmetics
Evan Cheng9e252e32006-02-22 02:26:30 +0000251 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
252 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
253 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
254 }
255
256 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
257 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
258
Evan Cheng19264272006-03-01 01:11:20 +0000259 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
260 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
261 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
262 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Evan Cheng9e252e32006-02-22 02:26:30 +0000263 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
264 }
265
266 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
267 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
268 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
269 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
270 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
271 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
272
273
Evan Cheng19264272006-03-01 01:11:20 +0000274 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
275 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
276 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
277 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Evan Cheng9e252e32006-02-22 02:26:30 +0000278 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
279 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
280 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
281 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
282 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
283 }
284
Chris Lattner76ac0682005-11-15 00:40:23 +0000285 computeRegisterProperties();
286
Evan Cheng6a374562006-02-14 08:25:08 +0000287 // FIXME: These should be based on subtarget info. Plus, the values should
288 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000289 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
290 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
291 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000292 allowUnalignedMemoryAccesses = true; // x86 supports it!
293}
294
295std::vector<SDOperand>
296X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
297 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
298 return LowerFastCCArguments(F, DAG);
299 return LowerCCCArguments(F, DAG);
300}
301
302std::pair<SDOperand, SDOperand>
303X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
304 bool isVarArg, unsigned CallingConv,
305 bool isTailCall,
306 SDOperand Callee, ArgListTy &Args,
307 SelectionDAG &DAG) {
308 assert((!isVarArg || CallingConv == CallingConv::C) &&
309 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000310
311 // If the callee is a GlobalAddress node (quite common, every direct call is)
312 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
313 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
314 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000315 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
316 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000317
Chris Lattner76ac0682005-11-15 00:40:23 +0000318 if (CallingConv == CallingConv::Fast && EnableFastCC)
319 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
320 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
321}
322
323//===----------------------------------------------------------------------===//
324// C Calling Convention implementation
325//===----------------------------------------------------------------------===//
326
327std::vector<SDOperand>
328X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
329 std::vector<SDOperand> ArgValues;
330
331 MachineFunction &MF = DAG.getMachineFunction();
332 MachineFrameInfo *MFI = MF.getFrameInfo();
333
334 // Add DAG nodes to load the arguments... On entry to a function on the X86,
335 // the stack frame looks like this:
336 //
337 // [ESP] -- return address
338 // [ESP + 4] -- first argument (leftmost lexically)
339 // [ESP + 8] -- second argument, if first argument is four bytes in size
340 // ...
341 //
342 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
343 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
344 MVT::ValueType ObjectVT = getValueType(I->getType());
345 unsigned ArgIncrement = 4;
346 unsigned ObjSize;
347 switch (ObjectVT) {
348 default: assert(0 && "Unhandled argument type!");
349 case MVT::i1:
350 case MVT::i8: ObjSize = 1; break;
351 case MVT::i16: ObjSize = 2; break;
352 case MVT::i32: ObjSize = 4; break;
353 case MVT::i64: ObjSize = ArgIncrement = 8; break;
354 case MVT::f32: ObjSize = 4; break;
355 case MVT::f64: ObjSize = ArgIncrement = 8; break;
356 }
357 // Create the frame index object for this incoming parameter...
358 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
359
360 // Create the SelectionDAG nodes corresponding to a load from this parameter
361 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
362
363 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
364 // dead loads.
365 SDOperand ArgValue;
366 if (!I->use_empty())
367 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
368 DAG.getSrcValue(NULL));
369 else {
370 if (MVT::isInteger(ObjectVT))
371 ArgValue = DAG.getConstant(0, ObjectVT);
372 else
373 ArgValue = DAG.getConstantFP(0, ObjectVT);
374 }
375 ArgValues.push_back(ArgValue);
376
377 ArgOffset += ArgIncrement; // Move on to the next argument...
378 }
379
380 // If the function takes variable number of arguments, make a frame index for
381 // the start of the first vararg value... for expansion of llvm.va_start.
382 if (F.isVarArg())
383 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
384 ReturnAddrIndex = 0; // No return address slot generated yet.
385 BytesToPopOnReturn = 0; // Callee pops nothing.
386 BytesCallerReserves = ArgOffset;
387
388 // Finally, inform the code generator which regs we return values in.
389 switch (getValueType(F.getReturnType())) {
390 default: assert(0 && "Unknown type!");
391 case MVT::isVoid: break;
392 case MVT::i1:
393 case MVT::i8:
394 case MVT::i16:
395 case MVT::i32:
396 MF.addLiveOut(X86::EAX);
397 break;
398 case MVT::i64:
399 MF.addLiveOut(X86::EAX);
400 MF.addLiveOut(X86::EDX);
401 break;
402 case MVT::f32:
403 case MVT::f64:
404 MF.addLiveOut(X86::ST0);
405 break;
406 }
407 return ArgValues;
408}
409
410std::pair<SDOperand, SDOperand>
411X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
412 bool isVarArg, bool isTailCall,
413 SDOperand Callee, ArgListTy &Args,
414 SelectionDAG &DAG) {
415 // Count how many bytes are to be pushed on the stack.
416 unsigned NumBytes = 0;
417
418 if (Args.empty()) {
419 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000420 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000421 } else {
422 for (unsigned i = 0, e = Args.size(); i != e; ++i)
423 switch (getValueType(Args[i].second)) {
424 default: assert(0 && "Unknown value type!");
425 case MVT::i1:
426 case MVT::i8:
427 case MVT::i16:
428 case MVT::i32:
429 case MVT::f32:
430 NumBytes += 4;
431 break;
432 case MVT::i64:
433 case MVT::f64:
434 NumBytes += 8;
435 break;
436 }
437
Chris Lattner62c34842006-02-13 09:00:43 +0000438 Chain = DAG.getCALLSEQ_START(Chain,
439 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000440
441 // Arguments go on the stack in reverse order, as specified by the ABI.
442 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000443 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000444 std::vector<SDOperand> Stores;
445
446 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
447 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
448 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
449
450 switch (getValueType(Args[i].second)) {
451 default: assert(0 && "Unexpected ValueType for argument!");
452 case MVT::i1:
453 case MVT::i8:
454 case MVT::i16:
455 // Promote the integer to 32 bits. If the input type is signed use a
456 // sign extend, otherwise use a zero extend.
457 if (Args[i].second->isSigned())
458 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
459 else
460 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
461
462 // FALL THROUGH
463 case MVT::i32:
464 case MVT::f32:
465 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
466 Args[i].first, PtrOff,
467 DAG.getSrcValue(NULL)));
468 ArgOffset += 4;
469 break;
470 case MVT::i64:
471 case MVT::f64:
472 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
473 Args[i].first, PtrOff,
474 DAG.getSrcValue(NULL)));
475 ArgOffset += 8;
476 break;
477 }
478 }
479 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
480 }
481
482 std::vector<MVT::ValueType> RetVals;
483 MVT::ValueType RetTyVT = getValueType(RetTy);
484 RetVals.push_back(MVT::Other);
485
486 // The result values produced have to be legal. Promote the result.
487 switch (RetTyVT) {
488 case MVT::isVoid: break;
489 default:
490 RetVals.push_back(RetTyVT);
491 break;
492 case MVT::i1:
493 case MVT::i8:
494 case MVT::i16:
495 RetVals.push_back(MVT::i32);
496 break;
497 case MVT::f32:
498 if (X86ScalarSSE)
499 RetVals.push_back(MVT::f32);
500 else
501 RetVals.push_back(MVT::f64);
502 break;
503 case MVT::i64:
504 RetVals.push_back(MVT::i32);
505 RetVals.push_back(MVT::i32);
506 break;
507 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000508
Nate Begeman7e5496d2006-02-17 00:03:04 +0000509 std::vector<MVT::ValueType> NodeTys;
510 NodeTys.push_back(MVT::Other); // Returns a chain
511 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
512 std::vector<SDOperand> Ops;
513 Ops.push_back(Chain);
514 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000515
Nate Begeman7e5496d2006-02-17 00:03:04 +0000516 // FIXME: Do not generate X86ISD::TAILCALL for now.
517 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
518 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000519
Nate Begeman7e5496d2006-02-17 00:03:04 +0000520 NodeTys.clear();
521 NodeTys.push_back(MVT::Other); // Returns a chain
522 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
523 Ops.clear();
524 Ops.push_back(Chain);
525 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
526 Ops.push_back(DAG.getConstant(0, getPointerTy()));
527 Ops.push_back(InFlag);
528 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
529 InFlag = Chain.getValue(1);
530
531 SDOperand RetVal;
532 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000533 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000534 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000535 case MVT::i1:
536 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000537 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
538 Chain = RetVal.getValue(1);
539 if (RetTyVT == MVT::i1)
540 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
541 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000542 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000543 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
544 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000545 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000546 case MVT::i32:
547 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
548 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000549 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000550 case MVT::i64: {
551 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
552 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
553 Lo.getValue(2));
554 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
555 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000556 break;
557 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000558 case MVT::f32:
559 case MVT::f64: {
560 std::vector<MVT::ValueType> Tys;
561 Tys.push_back(MVT::f64);
562 Tys.push_back(MVT::Other);
563 Tys.push_back(MVT::Flag);
564 std::vector<SDOperand> Ops;
565 Ops.push_back(Chain);
566 Ops.push_back(InFlag);
567 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
568 Chain = RetVal.getValue(1);
569 InFlag = RetVal.getValue(2);
570 if (X86ScalarSSE) {
571 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
572 // shouldn't be necessary except that RFP cannot be live across
573 // multiple blocks. When stackifier is fixed, they can be uncoupled.
574 MachineFunction &MF = DAG.getMachineFunction();
575 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
576 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
577 Tys.clear();
578 Tys.push_back(MVT::Other);
579 Ops.clear();
580 Ops.push_back(Chain);
581 Ops.push_back(RetVal);
582 Ops.push_back(StackSlot);
583 Ops.push_back(DAG.getValueType(RetTyVT));
584 Ops.push_back(InFlag);
585 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
586 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
587 DAG.getSrcValue(NULL));
588 Chain = RetVal.getValue(1);
589 }
Evan Cheng45e190982006-01-05 00:27:02 +0000590
Nate Begeman7e5496d2006-02-17 00:03:04 +0000591 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
592 // FIXME: we would really like to remember that this FP_ROUND
593 // operation is okay to eliminate if we allow excess FP precision.
594 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
595 break;
596 }
597 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000598 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000599
600 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000601}
602
Chris Lattner76ac0682005-11-15 00:40:23 +0000603//===----------------------------------------------------------------------===//
604// Fast Calling Convention implementation
605//===----------------------------------------------------------------------===//
606//
607// The X86 'fast' calling convention passes up to two integer arguments in
608// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
609// and requires that the callee pop its arguments off the stack (allowing proper
610// tail calls), and has the same return value conventions as C calling convs.
611//
612// This calling convention always arranges for the callee pop value to be 8n+4
613// bytes, which is needed for tail recursion elimination and stack alignment
614// reasons.
615//
616// Note that this can be enhanced in the future to pass fp vals in registers
617// (when we have a global fp allocator) and do other tricks.
618//
619
620/// AddLiveIn - This helper function adds the specified physical register to the
621/// MachineFunction as a live in value. It also creates a corresponding virtual
622/// register for it.
623static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
624 TargetRegisterClass *RC) {
625 assert(RC->contains(PReg) && "Not the correct regclass!");
626 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
627 MF.addLiveIn(PReg, VReg);
628 return VReg;
629}
630
631
632std::vector<SDOperand>
633X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
634 std::vector<SDOperand> ArgValues;
635
636 MachineFunction &MF = DAG.getMachineFunction();
637 MachineFrameInfo *MFI = MF.getFrameInfo();
638
639 // Add DAG nodes to load the arguments... On entry to a function the stack
640 // frame looks like this:
641 //
642 // [ESP] -- return address
643 // [ESP + 4] -- first nonreg argument (leftmost lexically)
644 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
645 // ...
646 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
647
648 // Keep track of the number of integer regs passed so far. This can be either
649 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
650 // used).
651 unsigned NumIntRegs = 0;
652
653 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
654 MVT::ValueType ObjectVT = getValueType(I->getType());
655 unsigned ArgIncrement = 4;
656 unsigned ObjSize = 0;
657 SDOperand ArgValue;
658
659 switch (ObjectVT) {
660 default: assert(0 && "Unhandled argument type!");
661 case MVT::i1:
662 case MVT::i8:
663 if (NumIntRegs < 2) {
664 if (!I->use_empty()) {
665 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
666 X86::R8RegisterClass);
667 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
668 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000669 if (ObjectVT == MVT::i1)
670 // FIXME: Should insert a assertzext here.
671 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000672 }
673 ++NumIntRegs;
674 break;
675 }
676
677 ObjSize = 1;
678 break;
679 case MVT::i16:
680 if (NumIntRegs < 2) {
681 if (!I->use_empty()) {
682 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
683 X86::R16RegisterClass);
684 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
685 DAG.setRoot(ArgValue.getValue(1));
686 }
687 ++NumIntRegs;
688 break;
689 }
690 ObjSize = 2;
691 break;
692 case MVT::i32:
693 if (NumIntRegs < 2) {
694 if (!I->use_empty()) {
695 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
696 X86::R32RegisterClass);
697 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
698 DAG.setRoot(ArgValue.getValue(1));
699 }
700 ++NumIntRegs;
701 break;
702 }
703 ObjSize = 4;
704 break;
705 case MVT::i64:
706 if (NumIntRegs == 0) {
707 if (!I->use_empty()) {
708 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
709 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
710
711 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
712 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
713 DAG.setRoot(Hi.getValue(1));
714
715 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
716 }
717 NumIntRegs = 2;
718 break;
719 } else if (NumIntRegs == 1) {
720 if (!I->use_empty()) {
721 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
722 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
723 DAG.setRoot(Low.getValue(1));
724
725 // Load the high part from memory.
726 // Create the frame index object for this incoming parameter...
727 int FI = MFI->CreateFixedObject(4, ArgOffset);
728 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
729 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
730 DAG.getSrcValue(NULL));
731 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
732 }
733 ArgOffset += 4;
734 NumIntRegs = 2;
735 break;
736 }
737 ObjSize = ArgIncrement = 8;
738 break;
739 case MVT::f32: ObjSize = 4; break;
740 case MVT::f64: ObjSize = ArgIncrement = 8; break;
741 }
742
743 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
744 // dead loads.
745 if (ObjSize && !I->use_empty()) {
746 // Create the frame index object for this incoming parameter...
747 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
748
749 // Create the SelectionDAG nodes corresponding to a load from this
750 // parameter.
751 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
752
753 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
754 DAG.getSrcValue(NULL));
755 } else if (ArgValue.Val == 0) {
756 if (MVT::isInteger(ObjectVT))
757 ArgValue = DAG.getConstant(0, ObjectVT);
758 else
759 ArgValue = DAG.getConstantFP(0, ObjectVT);
760 }
761 ArgValues.push_back(ArgValue);
762
763 if (ObjSize)
764 ArgOffset += ArgIncrement; // Move on to the next argument.
765 }
766
767 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
768 // arguments and the arguments after the retaddr has been pushed are aligned.
769 if ((ArgOffset & 7) == 0)
770 ArgOffset += 4;
771
772 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
773 ReturnAddrIndex = 0; // No return address slot generated yet.
774 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
775 BytesCallerReserves = 0;
776
777 // Finally, inform the code generator which regs we return values in.
778 switch (getValueType(F.getReturnType())) {
779 default: assert(0 && "Unknown type!");
780 case MVT::isVoid: break;
781 case MVT::i1:
782 case MVT::i8:
783 case MVT::i16:
784 case MVT::i32:
785 MF.addLiveOut(X86::EAX);
786 break;
787 case MVT::i64:
788 MF.addLiveOut(X86::EAX);
789 MF.addLiveOut(X86::EDX);
790 break;
791 case MVT::f32:
792 case MVT::f64:
793 MF.addLiveOut(X86::ST0);
794 break;
795 }
796 return ArgValues;
797}
798
799std::pair<SDOperand, SDOperand>
800X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
801 bool isTailCall, SDOperand Callee,
802 ArgListTy &Args, SelectionDAG &DAG) {
803 // Count how many bytes are to be pushed on the stack.
804 unsigned NumBytes = 0;
805
806 // Keep track of the number of integer regs passed so far. This can be either
807 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
808 // used).
809 unsigned NumIntRegs = 0;
810
811 for (unsigned i = 0, e = Args.size(); i != e; ++i)
812 switch (getValueType(Args[i].second)) {
813 default: assert(0 && "Unknown value type!");
814 case MVT::i1:
815 case MVT::i8:
816 case MVT::i16:
817 case MVT::i32:
818 if (NumIntRegs < 2) {
819 ++NumIntRegs;
820 break;
821 }
822 // fall through
823 case MVT::f32:
824 NumBytes += 4;
825 break;
826 case MVT::i64:
827 if (NumIntRegs == 0) {
828 NumIntRegs = 2;
829 break;
830 } else if (NumIntRegs == 1) {
831 NumIntRegs = 2;
832 NumBytes += 4;
833 break;
834 }
835
836 // fall through
837 case MVT::f64:
838 NumBytes += 8;
839 break;
840 }
841
842 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
843 // arguments and the arguments after the retaddr has been pushed are aligned.
844 if ((NumBytes & 7) == 0)
845 NumBytes += 4;
846
Chris Lattner62c34842006-02-13 09:00:43 +0000847 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000848
849 // Arguments go on the stack in reverse order, as specified by the ABI.
850 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000851 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000852 NumIntRegs = 0;
853 std::vector<SDOperand> Stores;
854 std::vector<SDOperand> RegValuesToPass;
855 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
856 switch (getValueType(Args[i].second)) {
857 default: assert(0 && "Unexpected ValueType for argument!");
858 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000859 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
860 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000861 case MVT::i8:
862 case MVT::i16:
863 case MVT::i32:
864 if (NumIntRegs < 2) {
865 RegValuesToPass.push_back(Args[i].first);
866 ++NumIntRegs;
867 break;
868 }
869 // Fall through
870 case MVT::f32: {
871 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
872 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
873 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
874 Args[i].first, PtrOff,
875 DAG.getSrcValue(NULL)));
876 ArgOffset += 4;
877 break;
878 }
879 case MVT::i64:
880 if (NumIntRegs < 2) { // Can pass part of it in regs?
881 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
882 Args[i].first, DAG.getConstant(1, MVT::i32));
883 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
884 Args[i].first, DAG.getConstant(0, MVT::i32));
885 RegValuesToPass.push_back(Lo);
886 ++NumIntRegs;
887 if (NumIntRegs < 2) { // Pass both parts in regs?
888 RegValuesToPass.push_back(Hi);
889 ++NumIntRegs;
890 } else {
891 // Pass the high part in memory.
892 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
893 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
894 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
895 Hi, PtrOff, DAG.getSrcValue(NULL)));
896 ArgOffset += 4;
897 }
898 break;
899 }
900 // Fall through
901 case MVT::f64:
902 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
903 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
904 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
905 Args[i].first, PtrOff,
906 DAG.getSrcValue(NULL)));
907 ArgOffset += 8;
908 break;
909 }
910 }
911 if (!Stores.empty())
912 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
913
914 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
915 // arguments and the arguments after the retaddr has been pushed are aligned.
916 if ((ArgOffset & 7) == 0)
917 ArgOffset += 4;
918
919 std::vector<MVT::ValueType> RetVals;
920 MVT::ValueType RetTyVT = getValueType(RetTy);
921
922 RetVals.push_back(MVT::Other);
923
924 // The result values produced have to be legal. Promote the result.
925 switch (RetTyVT) {
926 case MVT::isVoid: break;
927 default:
928 RetVals.push_back(RetTyVT);
929 break;
930 case MVT::i1:
931 case MVT::i8:
932 case MVT::i16:
933 RetVals.push_back(MVT::i32);
934 break;
935 case MVT::f32:
936 if (X86ScalarSSE)
937 RetVals.push_back(MVT::f32);
938 else
939 RetVals.push_back(MVT::f64);
940 break;
941 case MVT::i64:
942 RetVals.push_back(MVT::i32);
943 RetVals.push_back(MVT::i32);
944 break;
945 }
946
Nate Begeman7e5496d2006-02-17 00:03:04 +0000947 // Build a sequence of copy-to-reg nodes chained together with token chain
948 // and flag operands which copy the outgoing args into registers.
949 SDOperand InFlag;
950 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
951 unsigned CCReg;
952 SDOperand RegToPass = RegValuesToPass[i];
953 switch (RegToPass.getValueType()) {
954 default: assert(0 && "Bad thing to pass in regs");
955 case MVT::i8:
956 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +0000957 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000958 case MVT::i16:
959 CCReg = (i == 0) ? X86::AX : X86::DX;
960 break;
961 case MVT::i32:
962 CCReg = (i == 0) ? X86::EAX : X86::EDX;
963 break;
964 }
965
966 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
967 InFlag = Chain.getValue(1);
968 }
969
970 std::vector<MVT::ValueType> NodeTys;
971 NodeTys.push_back(MVT::Other); // Returns a chain
972 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
973 std::vector<SDOperand> Ops;
974 Ops.push_back(Chain);
975 Ops.push_back(Callee);
976 if (InFlag.Val)
977 Ops.push_back(InFlag);
978
979 // FIXME: Do not generate X86ISD::TAILCALL for now.
980 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
981 InFlag = Chain.getValue(1);
982
983 NodeTys.clear();
984 NodeTys.push_back(MVT::Other); // Returns a chain
985 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
986 Ops.clear();
987 Ops.push_back(Chain);
988 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
989 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
990 Ops.push_back(InFlag);
991 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
992 InFlag = Chain.getValue(1);
993
994 SDOperand RetVal;
995 if (RetTyVT != MVT::isVoid) {
996 switch (RetTyVT) {
997 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +0000998 case MVT::i1:
999 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001000 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1001 Chain = RetVal.getValue(1);
1002 if (RetTyVT == MVT::i1)
1003 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1004 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001005 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001006 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1007 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001008 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001009 case MVT::i32:
1010 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1011 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001012 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001013 case MVT::i64: {
1014 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1015 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1016 Lo.getValue(2));
1017 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1018 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001019 break;
1020 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001021 case MVT::f32:
1022 case MVT::f64: {
1023 std::vector<MVT::ValueType> Tys;
1024 Tys.push_back(MVT::f64);
1025 Tys.push_back(MVT::Other);
1026 Tys.push_back(MVT::Flag);
1027 std::vector<SDOperand> Ops;
1028 Ops.push_back(Chain);
1029 Ops.push_back(InFlag);
1030 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1031 Chain = RetVal.getValue(1);
1032 InFlag = RetVal.getValue(2);
1033 if (X86ScalarSSE) {
1034 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1035 // shouldn't be necessary except that RFP cannot be live across
1036 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1037 MachineFunction &MF = DAG.getMachineFunction();
1038 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1039 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1040 Tys.clear();
1041 Tys.push_back(MVT::Other);
1042 Ops.clear();
1043 Ops.push_back(Chain);
1044 Ops.push_back(RetVal);
1045 Ops.push_back(StackSlot);
1046 Ops.push_back(DAG.getValueType(RetTyVT));
1047 Ops.push_back(InFlag);
1048 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1049 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1050 DAG.getSrcValue(NULL));
1051 Chain = RetVal.getValue(1);
1052 }
Evan Cheng172fce72006-01-06 00:43:03 +00001053
Nate Begeman7e5496d2006-02-17 00:03:04 +00001054 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1055 // FIXME: we would really like to remember that this FP_ROUND
1056 // operation is okay to eliminate if we allow excess FP precision.
1057 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1058 break;
1059 }
1060 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001061 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001062
1063 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001064}
1065
1066SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1067 if (ReturnAddrIndex == 0) {
1068 // Set up a frame object for the return address.
1069 MachineFunction &MF = DAG.getMachineFunction();
1070 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1071 }
1072
1073 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1074}
1075
1076
1077
1078std::pair<SDOperand, SDOperand> X86TargetLowering::
1079LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1080 SelectionDAG &DAG) {
1081 SDOperand Result;
1082 if (Depth) // Depths > 0 not supported yet!
1083 Result = DAG.getConstant(0, getPointerTy());
1084 else {
1085 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1086 if (!isFrameAddress)
1087 // Just load the return address
1088 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1089 DAG.getSrcValue(NULL));
1090 else
1091 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1092 DAG.getConstant(4, MVT::i32));
1093 }
1094 return std::make_pair(Result, Chain);
1095}
1096
Evan Cheng339edad2006-01-11 00:33:36 +00001097/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1098/// which corresponds to the condition code.
1099static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1100 switch (X86CC) {
1101 default: assert(0 && "Unknown X86 conditional code!");
1102 case X86ISD::COND_A: return X86::JA;
1103 case X86ISD::COND_AE: return X86::JAE;
1104 case X86ISD::COND_B: return X86::JB;
1105 case X86ISD::COND_BE: return X86::JBE;
1106 case X86ISD::COND_E: return X86::JE;
1107 case X86ISD::COND_G: return X86::JG;
1108 case X86ISD::COND_GE: return X86::JGE;
1109 case X86ISD::COND_L: return X86::JL;
1110 case X86ISD::COND_LE: return X86::JLE;
1111 case X86ISD::COND_NE: return X86::JNE;
1112 case X86ISD::COND_NO: return X86::JNO;
1113 case X86ISD::COND_NP: return X86::JNP;
1114 case X86ISD::COND_NS: return X86::JNS;
1115 case X86ISD::COND_O: return X86::JO;
1116 case X86ISD::COND_P: return X86::JP;
1117 case X86ISD::COND_S: return X86::JS;
1118 }
1119}
Chris Lattner76ac0682005-11-15 00:40:23 +00001120
Evan Cheng45df7f82006-01-30 23:41:35 +00001121/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1122/// specific condition code. It returns a false if it cannot do a direct
1123/// translation. X86CC is the translated CondCode. Flip is set to true if the
1124/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001125static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1126 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001127 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001128 Flip = false;
1129 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001130 if (!isFP) {
1131 switch (SetCCOpcode) {
1132 default: break;
1133 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1134 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1135 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1136 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1137 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1138 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1139 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1140 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1141 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1142 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1143 }
1144 } else {
1145 // On a floating point condition, the flags are set as follows:
1146 // ZF PF CF op
1147 // 0 | 0 | 0 | X > Y
1148 // 0 | 0 | 1 | X < Y
1149 // 1 | 0 | 0 | X == Y
1150 // 1 | 1 | 1 | unordered
1151 switch (SetCCOpcode) {
1152 default: break;
1153 case ISD::SETUEQ:
1154 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001155 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001156 case ISD::SETOGT:
1157 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001158 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001159 case ISD::SETOGE:
1160 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001161 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001162 case ISD::SETULT:
1163 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001164 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001165 case ISD::SETULE:
1166 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1167 case ISD::SETONE:
1168 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1169 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1170 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1171 }
1172 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001173
1174 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001175}
1176
Evan Cheng339edad2006-01-11 00:33:36 +00001177/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1178/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001179/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001180static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001181 switch (X86CC) {
1182 default:
1183 return false;
1184 case X86ISD::COND_B:
1185 case X86ISD::COND_BE:
1186 case X86ISD::COND_E:
1187 case X86ISD::COND_P:
1188 case X86ISD::COND_A:
1189 case X86ISD::COND_AE:
1190 case X86ISD::COND_NE:
1191 case X86ISD::COND_NP:
1192 return true;
1193 }
1194}
1195
Evan Cheng339edad2006-01-11 00:33:36 +00001196MachineBasicBlock *
1197X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1198 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001199 switch (MI->getOpcode()) {
1200 default: assert(false && "Unexpected instr type to insert");
1201 case X86::CMOV_FR32:
1202 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001203 // To "insert" a SELECT_CC instruction, we actually have to insert the
1204 // diamond control-flow pattern. The incoming instruction knows the
1205 // destination vreg to set, the condition code register to branch on, the
1206 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1208 ilist<MachineBasicBlock>::iterator It = BB;
1209 ++It;
1210
1211 // thisMBB:
1212 // ...
1213 // TrueVal = ...
1214 // cmpTY ccX, r1, r2
1215 // bCC copy1MBB
1216 // fallthrough --> copy0MBB
1217 MachineBasicBlock *thisMBB = BB;
1218 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1219 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1220 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1221 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1222 MachineFunction *F = BB->getParent();
1223 F->getBasicBlockList().insert(It, copy0MBB);
1224 F->getBasicBlockList().insert(It, sinkMBB);
1225 // Update machine-CFG edges
1226 BB->addSuccessor(copy0MBB);
1227 BB->addSuccessor(sinkMBB);
1228
1229 // copy0MBB:
1230 // %FalseValue = ...
1231 // # fallthrough to sinkMBB
1232 BB = copy0MBB;
1233
1234 // Update machine-CFG edges
1235 BB->addSuccessor(sinkMBB);
1236
1237 // sinkMBB:
1238 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1239 // ...
1240 BB = sinkMBB;
1241 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1242 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1243 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001244
Evan Cheng911c68d2006-01-16 21:21:29 +00001245 delete MI; // The pseudo instruction is gone now.
1246 return BB;
1247 }
Evan Cheng339edad2006-01-11 00:33:36 +00001248
Evan Cheng911c68d2006-01-16 21:21:29 +00001249 case X86::FP_TO_INT16_IN_MEM:
1250 case X86::FP_TO_INT32_IN_MEM:
1251 case X86::FP_TO_INT64_IN_MEM: {
1252 // Change the floating point control register to use "round towards zero"
1253 // mode when truncating to an integer value.
1254 MachineFunction *F = BB->getParent();
1255 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1256 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1257
1258 // Load the old value of the high byte of the control word...
1259 unsigned OldCW =
1260 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1261 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1262
1263 // Set the high part to be round to zero...
1264 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1265
1266 // Reload the modified control word now...
1267 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1268
1269 // Restore the memory image of control word to original value
1270 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1271
1272 // Get the X86 opcode to use.
1273 unsigned Opc;
1274 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001275 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001276 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1277 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1278 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1279 }
1280
1281 X86AddressMode AM;
1282 MachineOperand &Op = MI->getOperand(0);
1283 if (Op.isRegister()) {
1284 AM.BaseType = X86AddressMode::RegBase;
1285 AM.Base.Reg = Op.getReg();
1286 } else {
1287 AM.BaseType = X86AddressMode::FrameIndexBase;
1288 AM.Base.FrameIndex = Op.getFrameIndex();
1289 }
1290 Op = MI->getOperand(1);
1291 if (Op.isImmediate())
1292 AM.Scale = Op.getImmedValue();
1293 Op = MI->getOperand(2);
1294 if (Op.isImmediate())
1295 AM.IndexReg = Op.getImmedValue();
1296 Op = MI->getOperand(3);
1297 if (Op.isGlobalAddress()) {
1298 AM.GV = Op.getGlobal();
1299 } else {
1300 AM.Disp = Op.getImmedValue();
1301 }
1302 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1303
1304 // Reload the original control word now.
1305 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1306
1307 delete MI; // The pseudo instruction is gone now.
1308 return BB;
1309 }
1310 }
Evan Cheng339edad2006-01-11 00:33:36 +00001311}
1312
1313
1314//===----------------------------------------------------------------------===//
1315// X86 Custom Lowering Hooks
1316//===----------------------------------------------------------------------===//
1317
Chris Lattner76ac0682005-11-15 00:40:23 +00001318/// LowerOperation - Provide custom lowering hooks for some operations.
1319///
1320SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1321 switch (Op.getOpcode()) {
1322 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001323 case ISD::SHL_PARTS:
1324 case ISD::SRA_PARTS:
1325 case ISD::SRL_PARTS: {
1326 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1327 "Not an i64 shift!");
1328 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1329 SDOperand ShOpLo = Op.getOperand(0);
1330 SDOperand ShOpHi = Op.getOperand(1);
1331 SDOperand ShAmt = Op.getOperand(2);
1332 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001333 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001334 : DAG.getConstant(0, MVT::i32);
1335
1336 SDOperand Tmp2, Tmp3;
1337 if (Op.getOpcode() == ISD::SHL_PARTS) {
1338 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1339 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1340 } else {
1341 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001342 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001343 }
1344
1345 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1346 ShAmt, DAG.getConstant(32, MVT::i8));
1347
1348 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001349 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001350
1351 std::vector<MVT::ValueType> Tys;
1352 Tys.push_back(MVT::i32);
1353 Tys.push_back(MVT::Flag);
1354 std::vector<SDOperand> Ops;
1355 if (Op.getOpcode() == ISD::SHL_PARTS) {
1356 Ops.push_back(Tmp2);
1357 Ops.push_back(Tmp3);
1358 Ops.push_back(CC);
1359 Ops.push_back(InFlag);
1360 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1361 InFlag = Hi.getValue(1);
1362
1363 Ops.clear();
1364 Ops.push_back(Tmp3);
1365 Ops.push_back(Tmp1);
1366 Ops.push_back(CC);
1367 Ops.push_back(InFlag);
1368 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1369 } else {
1370 Ops.push_back(Tmp2);
1371 Ops.push_back(Tmp3);
1372 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001373 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001374 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1375 InFlag = Lo.getValue(1);
1376
1377 Ops.clear();
1378 Ops.push_back(Tmp3);
1379 Ops.push_back(Tmp1);
1380 Ops.push_back(CC);
1381 Ops.push_back(InFlag);
1382 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1383 }
1384
1385 Tys.clear();
1386 Tys.push_back(MVT::i32);
1387 Tys.push_back(MVT::i32);
1388 Ops.clear();
1389 Ops.push_back(Lo);
1390 Ops.push_back(Hi);
1391 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1392 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001393 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001394 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001395 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001396 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001397
1398 SDOperand Result;
1399 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1400 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001401 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001402 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001403 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001404 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1405 DAG.getEntryNode(), Op.getOperand(0),
1406 StackSlot, DAG.getSrcValue(NULL));
1407
1408 // Build the FILD
1409 std::vector<MVT::ValueType> Tys;
1410 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001411 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001412 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001413 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001414 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001415 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001416 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001417 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1418 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001419
1420 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001421 Chain = Result.getValue(1);
1422 SDOperand InFlag = Result.getValue(2);
1423
Evan Cheng11613a52006-02-04 02:20:30 +00001424 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001425 // shouldn't be necessary except that RFP cannot be live across
1426 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1427 MachineFunction &MF = DAG.getMachineFunction();
1428 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1429 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1430 std::vector<MVT::ValueType> Tys;
1431 Tys.push_back(MVT::Other);
1432 std::vector<SDOperand> Ops;
1433 Ops.push_back(Chain);
1434 Ops.push_back(Result);
1435 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001436 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001437 Ops.push_back(InFlag);
1438 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1439 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1440 DAG.getSrcValue(NULL));
1441 }
1442
Evan Cheng6305e502006-01-12 22:54:21 +00001443 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001444 }
1445 case ISD::FP_TO_SINT: {
1446 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001447 "Unknown FP_TO_SINT to lower!");
1448 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1449 // stack slot.
1450 MachineFunction &MF = DAG.getMachineFunction();
1451 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1452 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1453 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1454
1455 unsigned Opc;
1456 switch (Op.getValueType()) {
1457 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1458 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1459 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1460 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1461 }
1462
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001463 SDOperand Chain = DAG.getEntryNode();
1464 SDOperand Value = Op.getOperand(0);
1465 if (X86ScalarSSE) {
1466 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1467 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1468 DAG.getSrcValue(0));
1469 std::vector<MVT::ValueType> Tys;
1470 Tys.push_back(MVT::f64);
1471 Tys.push_back(MVT::Other);
1472 std::vector<SDOperand> Ops;
1473 Ops.push_back(Chain);
1474 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001475 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001476 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1477 Chain = Value.getValue(1);
1478 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1479 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1480 }
1481
Chris Lattner76ac0682005-11-15 00:40:23 +00001482 // Build the FP_TO_INT*_IN_MEM
1483 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001484 Ops.push_back(Chain);
1485 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001486 Ops.push_back(StackSlot);
1487 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1488
1489 // Load the result.
1490 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1491 DAG.getSrcValue(NULL));
1492 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001493 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001494 std::vector<MVT::ValueType> Tys;
1495 Tys.push_back(MVT::Other);
1496 Tys.push_back(MVT::Flag);
1497 std::vector<SDOperand> Ops;
1498 Ops.push_back(Op.getOperand(0));
1499 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001500 Ops.clear();
1501 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1502 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1503 MVT::i32, Ops[0].getValue(2)));
1504 Ops.push_back(Ops[1].getValue(1));
1505 Tys[0] = Tys[1] = MVT::i32;
1506 Tys.push_back(MVT::Other);
1507 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001508 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001509 case ISD::FABS: {
1510 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00001511 const Type *OpNTy = MVT::getTypeForValueType(VT);
1512 std::vector<Constant*> CV;
1513 if (VT == MVT::f64) {
1514 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1515 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1516 } else {
1517 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1518 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1519 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1520 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1521 }
1522 Constant *CS = ConstantStruct::get(CV);
1523 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1524 SDOperand Mask
1525 = DAG.getNode(X86ISD::LOAD_PACK,
1526 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00001527 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1528 }
Evan Cheng72d5c252006-01-31 22:28:30 +00001529 case ISD::FNEG: {
1530 MVT::ValueType VT = Op.getValueType();
1531 const Type *OpNTy = MVT::getTypeForValueType(VT);
1532 std::vector<Constant*> CV;
1533 if (VT == MVT::f64) {
1534 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1535 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1536 } else {
1537 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1538 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1539 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1540 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1541 }
1542 Constant *CS = ConstantStruct::get(CV);
1543 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1544 SDOperand Mask
1545 = DAG.getNode(X86ISD::LOAD_PACK,
1546 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1547 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1548 }
Evan Chengc1583db2005-12-21 20:21:51 +00001549 case ISD::SETCC: {
1550 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001551 SDOperand Cond;
1552 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001553 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1554 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001555 bool Flip;
1556 unsigned X86CC;
1557 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1558 if (Flip)
1559 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1560 Op.getOperand(1), Op.getOperand(0));
1561 else
1562 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1563 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001564 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1565 DAG.getConstant(X86CC, MVT::i8), Cond);
1566 } else {
1567 assert(isFP && "Illegal integer SetCC!");
1568
Evan Cheng45df7f82006-01-30 23:41:35 +00001569 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1570 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001571 std::vector<MVT::ValueType> Tys;
1572 std::vector<SDOperand> Ops;
1573 switch (SetCCOpcode) {
1574 default: assert(false && "Illegal floating point SetCC!");
1575 case ISD::SETOEQ: { // !PF & ZF
1576 Tys.push_back(MVT::i8);
1577 Tys.push_back(MVT::Flag);
1578 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1579 Ops.push_back(Cond);
1580 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1581 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1582 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1583 Tmp1.getValue(1));
1584 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1585 }
Evan Cheng172fce72006-01-06 00:43:03 +00001586 case ISD::SETUNE: { // PF | !ZF
1587 Tys.push_back(MVT::i8);
1588 Tys.push_back(MVT::Flag);
1589 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1590 Ops.push_back(Cond);
1591 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1592 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1593 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1594 Tmp1.getValue(1));
1595 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1596 }
1597 }
1598 }
Evan Chengc1583db2005-12-21 20:21:51 +00001599 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001600 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001601 MVT::ValueType VT = Op.getValueType();
1602 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001603 bool isFPStack = isFP && !X86ScalarSSE;
1604 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001605 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001606 SDOperand Op0 = Op.getOperand(0);
1607 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001608 if (Op0.getOpcode() == ISD::SETCC)
1609 Op0 = LowerOperation(Op0, DAG);
1610
Evan Cheng73a1ad92006-01-10 20:26:56 +00001611 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001612 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1613 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1614 // have another use it will be eliminated.
1615 // If the X86ISD::SETCC has more than one use, then it's probably better
1616 // to use a test instead of duplicating the X86ISD::CMP (for register
1617 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001618 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1619 if (!Op0.hasOneUse()) {
1620 std::vector<MVT::ValueType> Tys;
1621 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1622 Tys.push_back(Op0.Val->getValueType(i));
1623 std::vector<SDOperand> Ops;
1624 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1625 Ops.push_back(Op0.getOperand(i));
1626 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1627 }
1628
Evan Chengfb22e862006-01-13 01:03:02 +00001629 CC = Op0.getOperand(0);
1630 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001631 // Make a copy as flag result cannot be used by more than one.
1632 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1633 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001634 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001635 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001636 } else
1637 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001638 } else
1639 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001640
Evan Cheng731423f2006-01-13 01:06:49 +00001641 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001642 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001643 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001644 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001645
1646 std::vector<MVT::ValueType> Tys;
1647 Tys.push_back(Op.getValueType());
1648 Tys.push_back(MVT::Flag);
1649 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001650 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1651 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001652 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001653 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001654 Ops.push_back(CC);
1655 Ops.push_back(Cond);
1656 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001657 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001658 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001659 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001660 SDOperand Cond = Op.getOperand(1);
1661 SDOperand Dest = Op.getOperand(2);
1662 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001663 if (Cond.getOpcode() == ISD::SETCC)
1664 Cond = LowerOperation(Cond, DAG);
1665
Evan Chengc1583db2005-12-21 20:21:51 +00001666 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001667 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1668 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1669 // have another use it will be eliminated.
1670 // If the X86ISD::SETCC has more than one use, then it's probably better
1671 // to use a test instead of duplicating the X86ISD::CMP (for register
1672 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001673 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1674 if (!Cond.hasOneUse()) {
1675 std::vector<MVT::ValueType> Tys;
1676 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1677 Tys.push_back(Cond.Val->getValueType(i));
1678 std::vector<SDOperand> Ops;
1679 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1680 Ops.push_back(Cond.getOperand(i));
1681 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1682 }
1683
Evan Chengfb22e862006-01-13 01:03:02 +00001684 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001685 Cond = Cond.getOperand(1);
1686 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001687 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001688 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001689 } else
1690 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001691 } else
1692 addTest = true;
1693
1694 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001695 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001696 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1697 }
1698 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1699 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1700 }
Evan Chengae986f12006-01-11 22:15:48 +00001701 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00001702 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001703 SDOperand Chain = Op.getOperand(0);
1704 unsigned Align =
1705 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1706 if (Align == 0) Align = 1;
1707
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001708 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1709 // If not DWORD aligned, call memset if size is less than the threshold.
1710 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001711 if ((Align & 3) != 0 ||
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001712 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1713 MVT::ValueType IntPtr = getPointerTy();
1714 const Type *IntPtrTy = getTargetData().getIntPtrType();
1715 std::vector<std::pair<SDOperand, const Type*> > Args;
1716 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1717 // Extend the ubyte argument to be an int value for the call.
1718 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1719 Args.push_back(std::make_pair(Val, IntPtrTy));
1720 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1721 std::pair<SDOperand,SDOperand> CallResult =
1722 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1723 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1724 return CallResult.second;
1725 }
1726
Evan Chengae986f12006-01-11 22:15:48 +00001727 MVT::ValueType AVT;
1728 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001729 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1730 unsigned BytesLeft = 0;
1731 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00001732 unsigned ValReg;
1733 unsigned Val = ValC->getValue() & 255;
1734
1735 // If the value is a constant, then we can potentially use larger sets.
1736 switch (Align & 3) {
1737 case 2: // WORD aligned
1738 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001739 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1740 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001741 Val = (Val << 8) | Val;
1742 ValReg = X86::AX;
1743 break;
1744 case 0: // DWORD aligned
1745 AVT = MVT::i32;
Evan Cheng6dc73292006-03-04 02:48:56 +00001746 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1747 BytesLeft = I->getValue() % 4;
Evan Chengae986f12006-01-11 22:15:48 +00001748 Val = (Val << 8) | Val;
1749 Val = (Val << 16) | Val;
1750 ValReg = X86::EAX;
1751 break;
1752 default: // Byte aligned
1753 AVT = MVT::i8;
1754 Count = Op.getOperand(3);
1755 ValReg = X86::AL;
1756 break;
1757 }
1758
1759 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1760 InFlag);
1761 InFlag = Chain.getValue(1);
1762 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001763 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00001764 Count = Op.getOperand(3);
1765 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1766 InFlag = Chain.getValue(1);
1767 }
1768
1769 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1770 InFlag = Chain.getValue(1);
1771 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1772 InFlag = Chain.getValue(1);
Evan Cheng6dc73292006-03-04 02:48:56 +00001773 Chain = DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1774 DAG.getValueType(AVT), InFlag);
Evan Chengae986f12006-01-11 22:15:48 +00001775
Evan Cheng6dc73292006-03-04 02:48:56 +00001776 if (BytesLeft) {
1777 // Issue stores for the last 1 - 3 bytes.
1778 SDOperand Value;
1779 unsigned Val = ValC->getValue() & 255;
1780 unsigned Offset = I->getValue() - BytesLeft;
1781 SDOperand DstAddr = Op.getOperand(1);
1782 MVT::ValueType AddrVT = DstAddr.getValueType();
1783 if (BytesLeft >= 2) {
1784 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1785 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1786 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1787 DAG.getConstant(Offset, AddrVT)),
1788 DAG.getSrcValue(NULL));
1789 BytesLeft -= 2;
1790 Offset += 2;
1791 }
1792
1793 if (BytesLeft == 1) {
1794 Value = DAG.getConstant(Val, MVT::i8);
1795 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1796 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1797 DAG.getConstant(Offset, AddrVT)),
1798 DAG.getSrcValue(NULL));
1799 }
1800 }
1801
1802 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00001803 }
1804 case ISD::MEMCPY: {
1805 SDOperand Chain = Op.getOperand(0);
1806 unsigned Align =
1807 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1808 if (Align == 0) Align = 1;
1809
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001810 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1811 // If not DWORD aligned, call memcpy if size is less than the threshold.
1812 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001813 if ((Align & 3) != 0 ||
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001814 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1815 MVT::ValueType IntPtr = getPointerTy();
1816 const Type *IntPtrTy = getTargetData().getIntPtrType();
1817 std::vector<std::pair<SDOperand, const Type*> > Args;
1818 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1819 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1820 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1821 std::pair<SDOperand,SDOperand> CallResult =
1822 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1823 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1824 return CallResult.second;
1825 }
1826
Evan Chengae986f12006-01-11 22:15:48 +00001827 MVT::ValueType AVT;
1828 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001829 unsigned BytesLeft = 0;
Evan Chengae986f12006-01-11 22:15:48 +00001830 switch (Align & 3) {
1831 case 2: // WORD aligned
1832 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001833 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1834 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001835 break;
1836 case 0: // DWORD aligned
1837 AVT = MVT::i32;
Evan Cheng6dc73292006-03-04 02:48:56 +00001838 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1839 BytesLeft = I->getValue() % 4;
Evan Chengae986f12006-01-11 22:15:48 +00001840 break;
1841 default: // Byte aligned
1842 AVT = MVT::i8;
1843 Count = Op.getOperand(3);
1844 break;
1845 }
1846
Evan Cheng6dc73292006-03-04 02:48:56 +00001847 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001848 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1849 InFlag = Chain.getValue(1);
1850 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1851 InFlag = Chain.getValue(1);
1852 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1853 InFlag = Chain.getValue(1);
Evan Cheng6dc73292006-03-04 02:48:56 +00001854 Chain = DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1855 DAG.getValueType(AVT), InFlag);
Evan Chengae986f12006-01-11 22:15:48 +00001856
Evan Cheng6dc73292006-03-04 02:48:56 +00001857 if (BytesLeft) {
1858 // Issue loads and stores for the last 1 - 3 bytes.
1859 unsigned Offset = I->getValue() - BytesLeft;
1860 SDOperand DstAddr = Op.getOperand(1);
1861 MVT::ValueType DstVT = DstAddr.getValueType();
1862 SDOperand SrcAddr = Op.getOperand(2);
1863 MVT::ValueType SrcVT = SrcAddr.getValueType();
1864 SDOperand Value;
1865 if (BytesLeft >= 2) {
1866 Value = DAG.getLoad(MVT::i16, Chain,
1867 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1868 DAG.getConstant(Offset, SrcVT)),
1869 DAG.getSrcValue(NULL));
1870 Chain = Value.getValue(1);
1871 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1872 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1873 DAG.getConstant(Offset, DstVT)),
1874 DAG.getSrcValue(NULL));
1875 BytesLeft -= 2;
1876 Offset += 2;
1877 }
1878
1879 if (BytesLeft == 1) {
1880 Value = DAG.getLoad(MVT::i8, Chain,
1881 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1882 DAG.getConstant(Offset, SrcVT)),
1883 DAG.getSrcValue(NULL));
1884 Chain = Value.getValue(1);
1885 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1886 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1887 DAG.getConstant(Offset, DstVT)),
1888 DAG.getSrcValue(NULL));
1889 }
1890 }
1891
1892 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00001893 }
Evan Cheng99470012006-02-25 09:55:19 +00001894
1895 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1896 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1897 // one of the above mentioned nodes. It has to be wrapped because otherwise
1898 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1899 // be used to form addressing mode. These wrapped nodes will be selected
1900 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00001901 case ISD::ConstantPool: {
1902 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001903 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1904 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1905 CP->getAlignment()));
Evan Cheng1f342c22006-02-23 02:43:52 +00001906 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00001907 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00001908 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00001909 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1910 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1911 }
1912
1913 return Result;
1914 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001915 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001916 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1917 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1918 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga74ce622005-12-21 02:39:21 +00001919 if (getTargetMachine().
Evan Cheng5588de92006-02-18 00:15:05 +00001920 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00001921 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00001922 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00001923 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1924 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00001925
1926 // For Darwin, external and weak symbols are indirect, so we want to load
1927 // the value at address GV, not the value of GV itself. This means that
1928 // the GlobalAddress must be in the base or index register of the address,
1929 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00001930 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
1931 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1932 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
Evan Cheng5a766802006-02-07 08:38:37 +00001933 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00001934 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00001935 }
Evan Cheng5588de92006-02-18 00:15:05 +00001936
Evan Chengb94db9e2006-01-12 07:56:47 +00001937 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001938 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001939 case ISD::ExternalSymbol: {
1940 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
1941 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1942 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
1943 if (getTargetMachine().
1944 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1945 // With PIC, the address is actually $g + Offset.
1946 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1947 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1948 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1949 }
1950
1951 return Result;
1952 }
Nate Begemane74795c2006-01-25 18:21:52 +00001953 case ISD::VASTART: {
1954 // vastart just stores the address of the VarArgsFrameIndex slot into the
1955 // memory location argument.
1956 // FIXME: Replace MVT::i32 with PointerTy
1957 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1958 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1959 Op.getOperand(1), Op.getOperand(2));
1960 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00001961 case ISD::RET: {
1962 SDOperand Copy;
1963
1964 switch(Op.getNumOperands()) {
1965 default:
1966 assert(0 && "Do not know how to return this many arguments!");
1967 abort();
1968 case 1:
1969 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1970 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1971 case 2: {
1972 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1973 if (MVT::isInteger(ArgVT))
1974 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1975 SDOperand());
1976 else if (!X86ScalarSSE) {
1977 std::vector<MVT::ValueType> Tys;
1978 Tys.push_back(MVT::Other);
1979 Tys.push_back(MVT::Flag);
1980 std::vector<SDOperand> Ops;
1981 Ops.push_back(Op.getOperand(0));
1982 Ops.push_back(Op.getOperand(1));
1983 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1984 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00001985 SDOperand MemLoc;
1986 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00001987 SDOperand Value = Op.getOperand(1);
1988
Evan Chenga24617f2006-02-01 01:19:32 +00001989 if (Value.getOpcode() == ISD::LOAD &&
1990 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00001991 Chain = Value.getOperand(0);
1992 MemLoc = Value.getOperand(1);
1993 } else {
1994 // Spill the value to memory and reload it into top of stack.
1995 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1996 MachineFunction &MF = DAG.getMachineFunction();
1997 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1998 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1999 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2000 Value, MemLoc, DAG.getSrcValue(0));
2001 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002002 std::vector<MVT::ValueType> Tys;
2003 Tys.push_back(MVT::f64);
2004 Tys.push_back(MVT::Other);
2005 std::vector<SDOperand> Ops;
2006 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002007 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002008 Ops.push_back(DAG.getValueType(ArgVT));
2009 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2010 Tys.clear();
2011 Tys.push_back(MVT::Other);
2012 Tys.push_back(MVT::Flag);
2013 Ops.clear();
2014 Ops.push_back(Copy.getValue(1));
2015 Ops.push_back(Copy);
2016 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2017 }
2018 break;
2019 }
2020 case 3:
2021 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2022 SDOperand());
2023 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2024 break;
2025 }
2026 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2027 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2028 Copy.getValue(1));
2029 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002030 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002031}
Evan Cheng6af02632005-12-20 06:22:03 +00002032
2033const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2034 switch (Opcode) {
2035 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00002036 case X86ISD::SHLD: return "X86ISD::SHLD";
2037 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00002038 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00002039 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00002040 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00002041 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00002042 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2043 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2044 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00002045 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00002046 case X86ISD::FST: return "X86ISD::FST";
2047 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00002048 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00002049 case X86ISD::CALL: return "X86ISD::CALL";
2050 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2051 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2052 case X86ISD::CMP: return "X86ISD::CMP";
2053 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00002054 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00002055 case X86ISD::CMOV: return "X86ISD::CMOV";
2056 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00002057 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00002058 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2059 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00002060 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00002061 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002062 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng6af02632005-12-20 06:22:03 +00002063 }
2064}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002065
Nate Begeman8a77efe2006-02-16 21:11:51 +00002066void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2067 uint64_t Mask,
2068 uint64_t &KnownZero,
2069 uint64_t &KnownOne,
2070 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002071
2072 unsigned Opc = Op.getOpcode();
Nate Begeman8a77efe2006-02-16 21:11:51 +00002073 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002074
2075 switch (Opc) {
2076 default:
2077 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2078 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00002079 case X86ISD::SETCC:
2080 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2081 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002082 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002083}
Chris Lattnerc642aa52006-01-31 19:43:35 +00002084
2085std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00002086getRegClassForInlineAsmConstraint(const std::string &Constraint,
2087 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00002088 if (Constraint.size() == 1) {
2089 // FIXME: not handling fp-stack yet!
2090 // FIXME: not handling MMX registers yet ('y' constraint).
2091 switch (Constraint[0]) { // GCC X86 Constraint Letters
2092 default: break; // Unknown constriant letter
2093 case 'r': // GENERAL_REGS
2094 case 'R': // LEGACY_REGS
2095 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2096 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2097 case 'l': // INDEX_REGS
2098 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2099 X86::ESI, X86::EDI, X86::EBP, 0);
2100 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2101 case 'Q': // Q_REGS
2102 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2103 case 'x': // SSE_REGS if SSE1 allowed
2104 if (Subtarget->hasSSE1())
2105 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2106 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2107 0);
2108 return std::vector<unsigned>();
2109 case 'Y': // SSE_REGS if SSE2 allowed
2110 if (Subtarget->hasSSE2())
2111 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2112 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2113 0);
2114 return std::vector<unsigned>();
2115 }
2116 }
2117
Chris Lattner7ad77df2006-02-22 00:56:39 +00002118 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00002119}