blob: 154e54aa4dd331677c895a0e8deddba22bb5f636 [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"
Evan Chengaf598d22006-03-13 23:18:16 +000022#include "llvm/ADT/VectorExtras.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000027#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000030#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33// FIXME: temporary.
34#include "llvm/Support/CommandLine.h"
35static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
36 cl::desc("Enable fastcc on X86"));
37
38X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42
Chris Lattner76ac0682005-11-15 00:40:23 +000043 // Set up the TargetLowering object.
44
45 // X86 is weird, it always uses i8 for shift amounts and setcc results.
46 setShiftAmountType(MVT::i8);
47 setSetCCResultType(MVT::i8);
48 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000049 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000051 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000052
53 // Add legal addressing mode scale values.
54 addLegalAddressScale(8);
55 addLegalAddressScale(4);
56 addLegalAddressScale(2);
57 // Enter the ones which require both scale + index last. These are more
58 // expensive.
59 addLegalAddressScale(9);
60 addLegalAddressScale(5);
61 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000062
Chris Lattner76ac0682005-11-15 00:40:23 +000063 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000064 addRegisterClass(MVT::i8, X86::R8RegisterClass);
65 addRegisterClass(MVT::i16, X86::R16RegisterClass);
66 addRegisterClass(MVT::i32, X86::R32RegisterClass);
67
68 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
69 // operation.
70 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
71 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
72 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000073
74 if (X86ScalarSSE)
75 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
76 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
77 else
78 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000079
80 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
81 // this operation.
82 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
83 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000084 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000085 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000086 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000087 else {
88 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
89 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
90 }
Chris Lattner76ac0682005-11-15 00:40:23 +000091
Evan Cheng5b97fcf2006-01-30 08:02:57 +000092 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
93 // isn't legal.
94 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
95 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
96
Evan Cheng08390f62006-01-30 22:13:22 +000097 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
98 // this operation.
99 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
101
102 if (X86ScalarSSE) {
103 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
104 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000105 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000106 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000107 }
108
109 // Handle FP_TO_UINT by promoting the destination to a larger signed
110 // conversion.
111 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
112 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
113 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
114
Evan Chengd13778e2006-02-18 07:26:17 +0000115 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000116 // Expand FP_TO_UINT into a select.
117 // FIXME: We would like to use a Custom expander here eventually to do
118 // the optimal thing for SSE vs. the default expansion in the legalizer.
119 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
120 else
Evan Chengd13778e2006-02-18 07:26:17 +0000121 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000122 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
123
Evan Cheng08390f62006-01-30 22:13:22 +0000124 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
125 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000126
Evan Cheng593bea72006-02-17 07:01:52 +0000127 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000128 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
129 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000130 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000133 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
134 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
135 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
136 setOperationAction(ISD::FREM , MVT::f64 , Expand);
137 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
138 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
139 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
140 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
141 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
142 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
143 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
144 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
145 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000146 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000147 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000148
Chris Lattner76ac0682005-11-15 00:40:23 +0000149 // These should be promoted to a larger select which is supported.
150 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
151 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000152
153 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000154 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
155 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
156 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
157 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
158 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
159 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
160 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
161 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
162 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000163 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000164 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000165 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000166 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000167 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000168 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000169 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000170 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
171 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
172 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000173 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000174 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
175 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000176
Chris Lattner9c415362005-11-29 06:16:21 +0000177 // We don't have line number support yet.
178 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000179 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000180 // FIXME - use subtarget debug flags
181 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
182 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000183
Nate Begemane74795c2006-01-25 18:21:52 +0000184 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
185 setOperationAction(ISD::VASTART , MVT::Other, Custom);
186
187 // Use the default implementation.
188 setOperationAction(ISD::VAARG , MVT::Other, Expand);
189 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
190 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000191 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
192 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
193 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000194
Chris Lattner9c7f5032006-03-05 05:08:37 +0000195 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
196 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
197
Chris Lattner76ac0682005-11-15 00:40:23 +0000198 if (X86ScalarSSE) {
199 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000200 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
201 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000202
203 // SSE has no load+extend ops
204 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
205 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
206
Evan Cheng72d5c252006-01-31 22:28:30 +0000207 // Use ANDPD to simulate FABS.
208 setOperationAction(ISD::FABS , MVT::f64, Custom);
209 setOperationAction(ISD::FABS , MVT::f32, Custom);
210
211 // Use XORP to simulate FNEG.
212 setOperationAction(ISD::FNEG , MVT::f64, Custom);
213 setOperationAction(ISD::FNEG , MVT::f32, Custom);
214
Evan Chengd8fba3a2006-02-02 00:28:23 +0000215 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000216 setOperationAction(ISD::FSIN , MVT::f64, Expand);
217 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000218 setOperationAction(ISD::FREM , MVT::f64, Expand);
219 setOperationAction(ISD::FSIN , MVT::f32, Expand);
220 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 setOperationAction(ISD::FREM , MVT::f32, Expand);
222
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000223 // Expand FP immediates into loads from the stack, except for the special
224 // cases we handle.
225 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
226 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000227 addLegalFPImmediate(+0.0); // xorps / xorpd
228 } else {
229 // Set up the FP register classes.
230 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000231
232 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
233
Chris Lattner76ac0682005-11-15 00:40:23 +0000234 if (!UnsafeFPMath) {
235 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
236 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
237 }
238
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000239 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000240 addLegalFPImmediate(+0.0); // FLD0
241 addLegalFPImmediate(+1.0); // FLD1
242 addLegalFPImmediate(-0.0); // FLD0/FCHS
243 addLegalFPImmediate(-1.0); // FLD1/FCHS
244 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000245
Evan Cheng19264272006-03-01 01:11:20 +0000246 // First set operation action for all vector types to expand. Then we
247 // will selectively turn on ones that can be effectively codegen'd.
248 for (unsigned VT = (unsigned)MVT::Vector + 1;
249 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
250 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
251 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
252 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
253 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
254 }
255
Evan Cheng9e252e32006-02-22 02:26:30 +0000256 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
257 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
258 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
259 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
260
Evan Cheng19264272006-03-01 01:11:20 +0000261 // FIXME: add MMX packed arithmetics
Evan Cheng9e252e32006-02-22 02:26:30 +0000262 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
263 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
264 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
265 }
266
267 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
268 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
269
Evan Cheng19264272006-03-01 01:11:20 +0000270 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
271 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
272 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
273 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Evan Cheng9e252e32006-02-22 02:26:30 +0000274 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
275 }
276
277 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
278 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
279 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
280 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
281 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
282 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
283
284
Evan Cheng19264272006-03-01 01:11:20 +0000285 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
286 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
287 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
288 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Evan Cheng9e252e32006-02-22 02:26:30 +0000289 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
290 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
291 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
292 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
293 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
294 }
295
Chris Lattner76ac0682005-11-15 00:40:23 +0000296 computeRegisterProperties();
297
Evan Cheng6a374562006-02-14 08:25:08 +0000298 // FIXME: These should be based on subtarget info. Plus, the values should
299 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000300 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
301 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
302 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000303 allowUnalignedMemoryAccesses = true; // x86 supports it!
304}
305
306std::vector<SDOperand>
307X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
308 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
309 return LowerFastCCArguments(F, DAG);
310 return LowerCCCArguments(F, DAG);
311}
312
313std::pair<SDOperand, SDOperand>
314X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
315 bool isVarArg, unsigned CallingConv,
316 bool isTailCall,
317 SDOperand Callee, ArgListTy &Args,
318 SelectionDAG &DAG) {
319 assert((!isVarArg || CallingConv == CallingConv::C) &&
320 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000321
322 // If the callee is a GlobalAddress node (quite common, every direct call is)
323 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
324 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
325 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000326 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
327 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000328
Chris Lattner76ac0682005-11-15 00:40:23 +0000329 if (CallingConv == CallingConv::Fast && EnableFastCC)
330 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
331 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
332}
333
334//===----------------------------------------------------------------------===//
335// C Calling Convention implementation
336//===----------------------------------------------------------------------===//
337
338std::vector<SDOperand>
339X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
340 std::vector<SDOperand> ArgValues;
341
342 MachineFunction &MF = DAG.getMachineFunction();
343 MachineFrameInfo *MFI = MF.getFrameInfo();
344
345 // Add DAG nodes to load the arguments... On entry to a function on the X86,
346 // the stack frame looks like this:
347 //
348 // [ESP] -- return address
349 // [ESP + 4] -- first argument (leftmost lexically)
350 // [ESP + 8] -- second argument, if first argument is four bytes in size
351 // ...
352 //
353 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
354 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
355 MVT::ValueType ObjectVT = getValueType(I->getType());
356 unsigned ArgIncrement = 4;
357 unsigned ObjSize;
358 switch (ObjectVT) {
359 default: assert(0 && "Unhandled argument type!");
360 case MVT::i1:
361 case MVT::i8: ObjSize = 1; break;
362 case MVT::i16: ObjSize = 2; break;
363 case MVT::i32: ObjSize = 4; break;
364 case MVT::i64: ObjSize = ArgIncrement = 8; break;
365 case MVT::f32: ObjSize = 4; break;
366 case MVT::f64: ObjSize = ArgIncrement = 8; break;
367 }
368 // Create the frame index object for this incoming parameter...
369 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
370
371 // Create the SelectionDAG nodes corresponding to a load from this parameter
372 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
373
374 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
375 // dead loads.
376 SDOperand ArgValue;
377 if (!I->use_empty())
378 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
379 DAG.getSrcValue(NULL));
380 else {
381 if (MVT::isInteger(ObjectVT))
382 ArgValue = DAG.getConstant(0, ObjectVT);
383 else
384 ArgValue = DAG.getConstantFP(0, ObjectVT);
385 }
386 ArgValues.push_back(ArgValue);
387
388 ArgOffset += ArgIncrement; // Move on to the next argument...
389 }
390
391 // If the function takes variable number of arguments, make a frame index for
392 // the start of the first vararg value... for expansion of llvm.va_start.
393 if (F.isVarArg())
394 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
395 ReturnAddrIndex = 0; // No return address slot generated yet.
396 BytesToPopOnReturn = 0; // Callee pops nothing.
397 BytesCallerReserves = ArgOffset;
398
399 // Finally, inform the code generator which regs we return values in.
400 switch (getValueType(F.getReturnType())) {
401 default: assert(0 && "Unknown type!");
402 case MVT::isVoid: break;
403 case MVT::i1:
404 case MVT::i8:
405 case MVT::i16:
406 case MVT::i32:
407 MF.addLiveOut(X86::EAX);
408 break;
409 case MVT::i64:
410 MF.addLiveOut(X86::EAX);
411 MF.addLiveOut(X86::EDX);
412 break;
413 case MVT::f32:
414 case MVT::f64:
415 MF.addLiveOut(X86::ST0);
416 break;
417 }
418 return ArgValues;
419}
420
421std::pair<SDOperand, SDOperand>
422X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
423 bool isVarArg, bool isTailCall,
424 SDOperand Callee, ArgListTy &Args,
425 SelectionDAG &DAG) {
426 // Count how many bytes are to be pushed on the stack.
427 unsigned NumBytes = 0;
428
429 if (Args.empty()) {
430 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000431 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000432 } else {
433 for (unsigned i = 0, e = Args.size(); i != e; ++i)
434 switch (getValueType(Args[i].second)) {
435 default: assert(0 && "Unknown value type!");
436 case MVT::i1:
437 case MVT::i8:
438 case MVT::i16:
439 case MVT::i32:
440 case MVT::f32:
441 NumBytes += 4;
442 break;
443 case MVT::i64:
444 case MVT::f64:
445 NumBytes += 8;
446 break;
447 }
448
Chris Lattner62c34842006-02-13 09:00:43 +0000449 Chain = DAG.getCALLSEQ_START(Chain,
450 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000451
452 // Arguments go on the stack in reverse order, as specified by the ABI.
453 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000454 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000455 std::vector<SDOperand> Stores;
456
457 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
458 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
459 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
460
461 switch (getValueType(Args[i].second)) {
462 default: assert(0 && "Unexpected ValueType for argument!");
463 case MVT::i1:
464 case MVT::i8:
465 case MVT::i16:
466 // Promote the integer to 32 bits. If the input type is signed use a
467 // sign extend, otherwise use a zero extend.
468 if (Args[i].second->isSigned())
469 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
470 else
471 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
472
473 // FALL THROUGH
474 case MVT::i32:
475 case MVT::f32:
476 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
477 Args[i].first, PtrOff,
478 DAG.getSrcValue(NULL)));
479 ArgOffset += 4;
480 break;
481 case MVT::i64:
482 case MVT::f64:
483 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
484 Args[i].first, PtrOff,
485 DAG.getSrcValue(NULL)));
486 ArgOffset += 8;
487 break;
488 }
489 }
490 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
491 }
492
493 std::vector<MVT::ValueType> RetVals;
494 MVT::ValueType RetTyVT = getValueType(RetTy);
495 RetVals.push_back(MVT::Other);
496
497 // The result values produced have to be legal. Promote the result.
498 switch (RetTyVT) {
499 case MVT::isVoid: break;
500 default:
501 RetVals.push_back(RetTyVT);
502 break;
503 case MVT::i1:
504 case MVT::i8:
505 case MVT::i16:
506 RetVals.push_back(MVT::i32);
507 break;
508 case MVT::f32:
509 if (X86ScalarSSE)
510 RetVals.push_back(MVT::f32);
511 else
512 RetVals.push_back(MVT::f64);
513 break;
514 case MVT::i64:
515 RetVals.push_back(MVT::i32);
516 RetVals.push_back(MVT::i32);
517 break;
518 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000519
Nate Begeman7e5496d2006-02-17 00:03:04 +0000520 std::vector<MVT::ValueType> NodeTys;
521 NodeTys.push_back(MVT::Other); // Returns a chain
522 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
523 std::vector<SDOperand> Ops;
524 Ops.push_back(Chain);
525 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000526
Nate Begeman7e5496d2006-02-17 00:03:04 +0000527 // FIXME: Do not generate X86ISD::TAILCALL for now.
528 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
529 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000530
Nate Begeman7e5496d2006-02-17 00:03:04 +0000531 NodeTys.clear();
532 NodeTys.push_back(MVT::Other); // Returns a chain
533 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
534 Ops.clear();
535 Ops.push_back(Chain);
536 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
537 Ops.push_back(DAG.getConstant(0, getPointerTy()));
538 Ops.push_back(InFlag);
539 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
540 InFlag = Chain.getValue(1);
541
542 SDOperand RetVal;
543 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000544 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000545 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000546 case MVT::i1:
547 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000548 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
549 Chain = RetVal.getValue(1);
550 if (RetTyVT == MVT::i1)
551 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
552 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000553 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000554 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
555 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000556 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000557 case MVT::i32:
558 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
559 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000560 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000561 case MVT::i64: {
562 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
563 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
564 Lo.getValue(2));
565 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
566 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000567 break;
568 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000569 case MVT::f32:
570 case MVT::f64: {
571 std::vector<MVT::ValueType> Tys;
572 Tys.push_back(MVT::f64);
573 Tys.push_back(MVT::Other);
574 Tys.push_back(MVT::Flag);
575 std::vector<SDOperand> Ops;
576 Ops.push_back(Chain);
577 Ops.push_back(InFlag);
578 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
579 Chain = RetVal.getValue(1);
580 InFlag = RetVal.getValue(2);
581 if (X86ScalarSSE) {
582 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
583 // shouldn't be necessary except that RFP cannot be live across
584 // multiple blocks. When stackifier is fixed, they can be uncoupled.
585 MachineFunction &MF = DAG.getMachineFunction();
586 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
587 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
588 Tys.clear();
589 Tys.push_back(MVT::Other);
590 Ops.clear();
591 Ops.push_back(Chain);
592 Ops.push_back(RetVal);
593 Ops.push_back(StackSlot);
594 Ops.push_back(DAG.getValueType(RetTyVT));
595 Ops.push_back(InFlag);
596 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
597 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
598 DAG.getSrcValue(NULL));
599 Chain = RetVal.getValue(1);
600 }
Evan Cheng45e190982006-01-05 00:27:02 +0000601
Nate Begeman7e5496d2006-02-17 00:03:04 +0000602 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
603 // FIXME: we would really like to remember that this FP_ROUND
604 // operation is okay to eliminate if we allow excess FP precision.
605 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
606 break;
607 }
608 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000609 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000610
611 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000612}
613
Chris Lattner76ac0682005-11-15 00:40:23 +0000614//===----------------------------------------------------------------------===//
615// Fast Calling Convention implementation
616//===----------------------------------------------------------------------===//
617//
618// The X86 'fast' calling convention passes up to two integer arguments in
619// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
620// and requires that the callee pop its arguments off the stack (allowing proper
621// tail calls), and has the same return value conventions as C calling convs.
622//
623// This calling convention always arranges for the callee pop value to be 8n+4
624// bytes, which is needed for tail recursion elimination and stack alignment
625// reasons.
626//
627// Note that this can be enhanced in the future to pass fp vals in registers
628// (when we have a global fp allocator) and do other tricks.
629//
630
631/// AddLiveIn - This helper function adds the specified physical register to the
632/// MachineFunction as a live in value. It also creates a corresponding virtual
633/// register for it.
634static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
635 TargetRegisterClass *RC) {
636 assert(RC->contains(PReg) && "Not the correct regclass!");
637 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
638 MF.addLiveIn(PReg, VReg);
639 return VReg;
640}
641
642
643std::vector<SDOperand>
644X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
645 std::vector<SDOperand> ArgValues;
646
647 MachineFunction &MF = DAG.getMachineFunction();
648 MachineFrameInfo *MFI = MF.getFrameInfo();
649
650 // Add DAG nodes to load the arguments... On entry to a function the stack
651 // frame looks like this:
652 //
653 // [ESP] -- return address
654 // [ESP + 4] -- first nonreg argument (leftmost lexically)
655 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
656 // ...
657 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
658
659 // Keep track of the number of integer regs passed so far. This can be either
660 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
661 // used).
662 unsigned NumIntRegs = 0;
663
664 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
665 MVT::ValueType ObjectVT = getValueType(I->getType());
666 unsigned ArgIncrement = 4;
667 unsigned ObjSize = 0;
668 SDOperand ArgValue;
669
670 switch (ObjectVT) {
671 default: assert(0 && "Unhandled argument type!");
672 case MVT::i1:
673 case MVT::i8:
674 if (NumIntRegs < 2) {
675 if (!I->use_empty()) {
676 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
677 X86::R8RegisterClass);
678 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
679 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000680 if (ObjectVT == MVT::i1)
681 // FIXME: Should insert a assertzext here.
682 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000683 }
684 ++NumIntRegs;
685 break;
686 }
687
688 ObjSize = 1;
689 break;
690 case MVT::i16:
691 if (NumIntRegs < 2) {
692 if (!I->use_empty()) {
693 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
694 X86::R16RegisterClass);
695 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
696 DAG.setRoot(ArgValue.getValue(1));
697 }
698 ++NumIntRegs;
699 break;
700 }
701 ObjSize = 2;
702 break;
703 case MVT::i32:
704 if (NumIntRegs < 2) {
705 if (!I->use_empty()) {
706 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
707 X86::R32RegisterClass);
708 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
709 DAG.setRoot(ArgValue.getValue(1));
710 }
711 ++NumIntRegs;
712 break;
713 }
714 ObjSize = 4;
715 break;
716 case MVT::i64:
717 if (NumIntRegs == 0) {
718 if (!I->use_empty()) {
719 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
720 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
721
722 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
723 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
724 DAG.setRoot(Hi.getValue(1));
725
726 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
727 }
728 NumIntRegs = 2;
729 break;
730 } else if (NumIntRegs == 1) {
731 if (!I->use_empty()) {
732 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
733 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
734 DAG.setRoot(Low.getValue(1));
735
736 // Load the high part from memory.
737 // Create the frame index object for this incoming parameter...
738 int FI = MFI->CreateFixedObject(4, ArgOffset);
739 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
740 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
741 DAG.getSrcValue(NULL));
742 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
743 }
744 ArgOffset += 4;
745 NumIntRegs = 2;
746 break;
747 }
748 ObjSize = ArgIncrement = 8;
749 break;
750 case MVT::f32: ObjSize = 4; break;
751 case MVT::f64: ObjSize = ArgIncrement = 8; break;
752 }
753
754 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
755 // dead loads.
756 if (ObjSize && !I->use_empty()) {
757 // Create the frame index object for this incoming parameter...
758 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
759
760 // Create the SelectionDAG nodes corresponding to a load from this
761 // parameter.
762 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
763
764 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
765 DAG.getSrcValue(NULL));
766 } else if (ArgValue.Val == 0) {
767 if (MVT::isInteger(ObjectVT))
768 ArgValue = DAG.getConstant(0, ObjectVT);
769 else
770 ArgValue = DAG.getConstantFP(0, ObjectVT);
771 }
772 ArgValues.push_back(ArgValue);
773
774 if (ObjSize)
775 ArgOffset += ArgIncrement; // Move on to the next argument.
776 }
777
778 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
779 // arguments and the arguments after the retaddr has been pushed are aligned.
780 if ((ArgOffset & 7) == 0)
781 ArgOffset += 4;
782
783 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
784 ReturnAddrIndex = 0; // No return address slot generated yet.
785 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
786 BytesCallerReserves = 0;
787
788 // Finally, inform the code generator which regs we return values in.
789 switch (getValueType(F.getReturnType())) {
790 default: assert(0 && "Unknown type!");
791 case MVT::isVoid: break;
792 case MVT::i1:
793 case MVT::i8:
794 case MVT::i16:
795 case MVT::i32:
796 MF.addLiveOut(X86::EAX);
797 break;
798 case MVT::i64:
799 MF.addLiveOut(X86::EAX);
800 MF.addLiveOut(X86::EDX);
801 break;
802 case MVT::f32:
803 case MVT::f64:
804 MF.addLiveOut(X86::ST0);
805 break;
806 }
807 return ArgValues;
808}
809
810std::pair<SDOperand, SDOperand>
811X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
812 bool isTailCall, SDOperand Callee,
813 ArgListTy &Args, SelectionDAG &DAG) {
814 // Count how many bytes are to be pushed on the stack.
815 unsigned NumBytes = 0;
816
817 // Keep track of the number of integer regs passed so far. This can be either
818 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
819 // used).
820 unsigned NumIntRegs = 0;
821
822 for (unsigned i = 0, e = Args.size(); i != e; ++i)
823 switch (getValueType(Args[i].second)) {
824 default: assert(0 && "Unknown value type!");
825 case MVT::i1:
826 case MVT::i8:
827 case MVT::i16:
828 case MVT::i32:
829 if (NumIntRegs < 2) {
830 ++NumIntRegs;
831 break;
832 }
833 // fall through
834 case MVT::f32:
835 NumBytes += 4;
836 break;
837 case MVT::i64:
838 if (NumIntRegs == 0) {
839 NumIntRegs = 2;
840 break;
841 } else if (NumIntRegs == 1) {
842 NumIntRegs = 2;
843 NumBytes += 4;
844 break;
845 }
846
847 // fall through
848 case MVT::f64:
849 NumBytes += 8;
850 break;
851 }
852
853 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
854 // arguments and the arguments after the retaddr has been pushed are aligned.
855 if ((NumBytes & 7) == 0)
856 NumBytes += 4;
857
Chris Lattner62c34842006-02-13 09:00:43 +0000858 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000859
860 // Arguments go on the stack in reverse order, as specified by the ABI.
861 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000862 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000863 NumIntRegs = 0;
864 std::vector<SDOperand> Stores;
865 std::vector<SDOperand> RegValuesToPass;
866 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
867 switch (getValueType(Args[i].second)) {
868 default: assert(0 && "Unexpected ValueType for argument!");
869 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000870 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
871 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000872 case MVT::i8:
873 case MVT::i16:
874 case MVT::i32:
875 if (NumIntRegs < 2) {
876 RegValuesToPass.push_back(Args[i].first);
877 ++NumIntRegs;
878 break;
879 }
880 // Fall through
881 case MVT::f32: {
882 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
883 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
884 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
885 Args[i].first, PtrOff,
886 DAG.getSrcValue(NULL)));
887 ArgOffset += 4;
888 break;
889 }
890 case MVT::i64:
891 if (NumIntRegs < 2) { // Can pass part of it in regs?
892 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
893 Args[i].first, DAG.getConstant(1, MVT::i32));
894 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
895 Args[i].first, DAG.getConstant(0, MVT::i32));
896 RegValuesToPass.push_back(Lo);
897 ++NumIntRegs;
898 if (NumIntRegs < 2) { // Pass both parts in regs?
899 RegValuesToPass.push_back(Hi);
900 ++NumIntRegs;
901 } else {
902 // Pass the high part in memory.
903 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
904 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
905 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
906 Hi, PtrOff, DAG.getSrcValue(NULL)));
907 ArgOffset += 4;
908 }
909 break;
910 }
911 // Fall through
912 case MVT::f64:
913 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
914 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
915 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
916 Args[i].first, PtrOff,
917 DAG.getSrcValue(NULL)));
918 ArgOffset += 8;
919 break;
920 }
921 }
922 if (!Stores.empty())
923 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
924
925 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
926 // arguments and the arguments after the retaddr has been pushed are aligned.
927 if ((ArgOffset & 7) == 0)
928 ArgOffset += 4;
929
930 std::vector<MVT::ValueType> RetVals;
931 MVT::ValueType RetTyVT = getValueType(RetTy);
932
933 RetVals.push_back(MVT::Other);
934
935 // The result values produced have to be legal. Promote the result.
936 switch (RetTyVT) {
937 case MVT::isVoid: break;
938 default:
939 RetVals.push_back(RetTyVT);
940 break;
941 case MVT::i1:
942 case MVT::i8:
943 case MVT::i16:
944 RetVals.push_back(MVT::i32);
945 break;
946 case MVT::f32:
947 if (X86ScalarSSE)
948 RetVals.push_back(MVT::f32);
949 else
950 RetVals.push_back(MVT::f64);
951 break;
952 case MVT::i64:
953 RetVals.push_back(MVT::i32);
954 RetVals.push_back(MVT::i32);
955 break;
956 }
957
Nate Begeman7e5496d2006-02-17 00:03:04 +0000958 // Build a sequence of copy-to-reg nodes chained together with token chain
959 // and flag operands which copy the outgoing args into registers.
960 SDOperand InFlag;
961 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
962 unsigned CCReg;
963 SDOperand RegToPass = RegValuesToPass[i];
964 switch (RegToPass.getValueType()) {
965 default: assert(0 && "Bad thing to pass in regs");
966 case MVT::i8:
967 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +0000968 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000969 case MVT::i16:
970 CCReg = (i == 0) ? X86::AX : X86::DX;
971 break;
972 case MVT::i32:
973 CCReg = (i == 0) ? X86::EAX : X86::EDX;
974 break;
975 }
976
977 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
978 InFlag = Chain.getValue(1);
979 }
980
981 std::vector<MVT::ValueType> NodeTys;
982 NodeTys.push_back(MVT::Other); // Returns a chain
983 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
984 std::vector<SDOperand> Ops;
985 Ops.push_back(Chain);
986 Ops.push_back(Callee);
987 if (InFlag.Val)
988 Ops.push_back(InFlag);
989
990 // FIXME: Do not generate X86ISD::TAILCALL for now.
991 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
992 InFlag = Chain.getValue(1);
993
994 NodeTys.clear();
995 NodeTys.push_back(MVT::Other); // Returns a chain
996 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
997 Ops.clear();
998 Ops.push_back(Chain);
999 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1000 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1001 Ops.push_back(InFlag);
1002 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1003 InFlag = Chain.getValue(1);
1004
1005 SDOperand RetVal;
1006 if (RetTyVT != MVT::isVoid) {
1007 switch (RetTyVT) {
1008 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001009 case MVT::i1:
1010 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001011 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1012 Chain = RetVal.getValue(1);
1013 if (RetTyVT == MVT::i1)
1014 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1015 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001016 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001017 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1018 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001019 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001020 case MVT::i32:
1021 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1022 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001023 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001024 case MVT::i64: {
1025 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1026 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1027 Lo.getValue(2));
1028 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1029 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001030 break;
1031 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001032 case MVT::f32:
1033 case MVT::f64: {
1034 std::vector<MVT::ValueType> Tys;
1035 Tys.push_back(MVT::f64);
1036 Tys.push_back(MVT::Other);
1037 Tys.push_back(MVT::Flag);
1038 std::vector<SDOperand> Ops;
1039 Ops.push_back(Chain);
1040 Ops.push_back(InFlag);
1041 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1042 Chain = RetVal.getValue(1);
1043 InFlag = RetVal.getValue(2);
1044 if (X86ScalarSSE) {
1045 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1046 // shouldn't be necessary except that RFP cannot be live across
1047 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1048 MachineFunction &MF = DAG.getMachineFunction();
1049 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1050 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1051 Tys.clear();
1052 Tys.push_back(MVT::Other);
1053 Ops.clear();
1054 Ops.push_back(Chain);
1055 Ops.push_back(RetVal);
1056 Ops.push_back(StackSlot);
1057 Ops.push_back(DAG.getValueType(RetTyVT));
1058 Ops.push_back(InFlag);
1059 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1060 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1061 DAG.getSrcValue(NULL));
1062 Chain = RetVal.getValue(1);
1063 }
Evan Cheng172fce72006-01-06 00:43:03 +00001064
Nate Begeman7e5496d2006-02-17 00:03:04 +00001065 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1066 // FIXME: we would really like to remember that this FP_ROUND
1067 // operation is okay to eliminate if we allow excess FP precision.
1068 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1069 break;
1070 }
1071 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001072 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001073
1074 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001075}
1076
1077SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1078 if (ReturnAddrIndex == 0) {
1079 // Set up a frame object for the return address.
1080 MachineFunction &MF = DAG.getMachineFunction();
1081 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1082 }
1083
1084 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1085}
1086
1087
1088
1089std::pair<SDOperand, SDOperand> X86TargetLowering::
1090LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1091 SelectionDAG &DAG) {
1092 SDOperand Result;
1093 if (Depth) // Depths > 0 not supported yet!
1094 Result = DAG.getConstant(0, getPointerTy());
1095 else {
1096 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1097 if (!isFrameAddress)
1098 // Just load the return address
1099 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1100 DAG.getSrcValue(NULL));
1101 else
1102 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1103 DAG.getConstant(4, MVT::i32));
1104 }
1105 return std::make_pair(Result, Chain);
1106}
1107
Evan Cheng339edad2006-01-11 00:33:36 +00001108/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1109/// which corresponds to the condition code.
1110static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1111 switch (X86CC) {
1112 default: assert(0 && "Unknown X86 conditional code!");
1113 case X86ISD::COND_A: return X86::JA;
1114 case X86ISD::COND_AE: return X86::JAE;
1115 case X86ISD::COND_B: return X86::JB;
1116 case X86ISD::COND_BE: return X86::JBE;
1117 case X86ISD::COND_E: return X86::JE;
1118 case X86ISD::COND_G: return X86::JG;
1119 case X86ISD::COND_GE: return X86::JGE;
1120 case X86ISD::COND_L: return X86::JL;
1121 case X86ISD::COND_LE: return X86::JLE;
1122 case X86ISD::COND_NE: return X86::JNE;
1123 case X86ISD::COND_NO: return X86::JNO;
1124 case X86ISD::COND_NP: return X86::JNP;
1125 case X86ISD::COND_NS: return X86::JNS;
1126 case X86ISD::COND_O: return X86::JO;
1127 case X86ISD::COND_P: return X86::JP;
1128 case X86ISD::COND_S: return X86::JS;
1129 }
1130}
Chris Lattner76ac0682005-11-15 00:40:23 +00001131
Evan Cheng45df7f82006-01-30 23:41:35 +00001132/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1133/// specific condition code. It returns a false if it cannot do a direct
1134/// translation. X86CC is the translated CondCode. Flip is set to true if the
1135/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001136static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1137 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001138 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001139 Flip = false;
1140 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001141 if (!isFP) {
1142 switch (SetCCOpcode) {
1143 default: break;
1144 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1145 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1146 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1147 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1148 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1149 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1150 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1151 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1152 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1153 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1154 }
1155 } else {
1156 // On a floating point condition, the flags are set as follows:
1157 // ZF PF CF op
1158 // 0 | 0 | 0 | X > Y
1159 // 0 | 0 | 1 | X < Y
1160 // 1 | 0 | 0 | X == Y
1161 // 1 | 1 | 1 | unordered
1162 switch (SetCCOpcode) {
1163 default: break;
1164 case ISD::SETUEQ:
1165 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001166 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001167 case ISD::SETOGT:
1168 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001169 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001170 case ISD::SETOGE:
1171 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001172 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001173 case ISD::SETULT:
1174 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001175 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001176 case ISD::SETULE:
1177 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1178 case ISD::SETONE:
1179 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1180 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1181 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1182 }
1183 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001184
1185 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001186}
1187
Evan Cheng339edad2006-01-11 00:33:36 +00001188/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1189/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001190/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001191static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001192 switch (X86CC) {
1193 default:
1194 return false;
1195 case X86ISD::COND_B:
1196 case X86ISD::COND_BE:
1197 case X86ISD::COND_E:
1198 case X86ISD::COND_P:
1199 case X86ISD::COND_A:
1200 case X86ISD::COND_AE:
1201 case X86ISD::COND_NE:
1202 case X86ISD::COND_NP:
1203 return true;
1204 }
1205}
1206
Evan Cheng339edad2006-01-11 00:33:36 +00001207MachineBasicBlock *
1208X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1209 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001210 switch (MI->getOpcode()) {
1211 default: assert(false && "Unexpected instr type to insert");
1212 case X86::CMOV_FR32:
1213 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001214 // To "insert" a SELECT_CC instruction, we actually have to insert the
1215 // diamond control-flow pattern. The incoming instruction knows the
1216 // destination vreg to set, the condition code register to branch on, the
1217 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1219 ilist<MachineBasicBlock>::iterator It = BB;
1220 ++It;
1221
1222 // thisMBB:
1223 // ...
1224 // TrueVal = ...
1225 // cmpTY ccX, r1, r2
1226 // bCC copy1MBB
1227 // fallthrough --> copy0MBB
1228 MachineBasicBlock *thisMBB = BB;
1229 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1230 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1231 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1232 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1233 MachineFunction *F = BB->getParent();
1234 F->getBasicBlockList().insert(It, copy0MBB);
1235 F->getBasicBlockList().insert(It, sinkMBB);
1236 // Update machine-CFG edges
1237 BB->addSuccessor(copy0MBB);
1238 BB->addSuccessor(sinkMBB);
1239
1240 // copy0MBB:
1241 // %FalseValue = ...
1242 // # fallthrough to sinkMBB
1243 BB = copy0MBB;
1244
1245 // Update machine-CFG edges
1246 BB->addSuccessor(sinkMBB);
1247
1248 // sinkMBB:
1249 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1250 // ...
1251 BB = sinkMBB;
1252 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1253 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1254 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001255
Evan Cheng911c68d2006-01-16 21:21:29 +00001256 delete MI; // The pseudo instruction is gone now.
1257 return BB;
1258 }
Evan Cheng339edad2006-01-11 00:33:36 +00001259
Evan Cheng911c68d2006-01-16 21:21:29 +00001260 case X86::FP_TO_INT16_IN_MEM:
1261 case X86::FP_TO_INT32_IN_MEM:
1262 case X86::FP_TO_INT64_IN_MEM: {
1263 // Change the floating point control register to use "round towards zero"
1264 // mode when truncating to an integer value.
1265 MachineFunction *F = BB->getParent();
1266 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1267 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1268
1269 // Load the old value of the high byte of the control word...
1270 unsigned OldCW =
1271 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1272 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1273
1274 // Set the high part to be round to zero...
1275 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1276
1277 // Reload the modified control word now...
1278 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1279
1280 // Restore the memory image of control word to original value
1281 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1282
1283 // Get the X86 opcode to use.
1284 unsigned Opc;
1285 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001286 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001287 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1288 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1289 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1290 }
1291
1292 X86AddressMode AM;
1293 MachineOperand &Op = MI->getOperand(0);
1294 if (Op.isRegister()) {
1295 AM.BaseType = X86AddressMode::RegBase;
1296 AM.Base.Reg = Op.getReg();
1297 } else {
1298 AM.BaseType = X86AddressMode::FrameIndexBase;
1299 AM.Base.FrameIndex = Op.getFrameIndex();
1300 }
1301 Op = MI->getOperand(1);
1302 if (Op.isImmediate())
1303 AM.Scale = Op.getImmedValue();
1304 Op = MI->getOperand(2);
1305 if (Op.isImmediate())
1306 AM.IndexReg = Op.getImmedValue();
1307 Op = MI->getOperand(3);
1308 if (Op.isGlobalAddress()) {
1309 AM.GV = Op.getGlobal();
1310 } else {
1311 AM.Disp = Op.getImmedValue();
1312 }
1313 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1314
1315 // Reload the original control word now.
1316 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1317
1318 delete MI; // The pseudo instruction is gone now.
1319 return BB;
1320 }
1321 }
Evan Cheng339edad2006-01-11 00:33:36 +00001322}
1323
1324
1325//===----------------------------------------------------------------------===//
1326// X86 Custom Lowering Hooks
1327//===----------------------------------------------------------------------===//
1328
Evan Chengaf598d22006-03-13 23:18:16 +00001329/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1330/// load. For Darwin, external and weak symbols are indirect, loading the value
1331/// at address GV rather then the value of GV itself. This means that the
1332/// GlobalAddress must be in the base or index register of the address, not the
1333/// GV offset field.
1334static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1335 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1336 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1337}
1338
Chris Lattner76ac0682005-11-15 00:40:23 +00001339/// LowerOperation - Provide custom lowering hooks for some operations.
1340///
1341SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1342 switch (Op.getOpcode()) {
1343 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001344 case ISD::SHL_PARTS:
1345 case ISD::SRA_PARTS:
1346 case ISD::SRL_PARTS: {
1347 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1348 "Not an i64 shift!");
1349 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1350 SDOperand ShOpLo = Op.getOperand(0);
1351 SDOperand ShOpHi = Op.getOperand(1);
1352 SDOperand ShAmt = Op.getOperand(2);
1353 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001354 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001355 : DAG.getConstant(0, MVT::i32);
1356
1357 SDOperand Tmp2, Tmp3;
1358 if (Op.getOpcode() == ISD::SHL_PARTS) {
1359 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1360 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1361 } else {
1362 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001363 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001364 }
1365
1366 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1367 ShAmt, DAG.getConstant(32, MVT::i8));
1368
1369 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001370 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001371
1372 std::vector<MVT::ValueType> Tys;
1373 Tys.push_back(MVT::i32);
1374 Tys.push_back(MVT::Flag);
1375 std::vector<SDOperand> Ops;
1376 if (Op.getOpcode() == ISD::SHL_PARTS) {
1377 Ops.push_back(Tmp2);
1378 Ops.push_back(Tmp3);
1379 Ops.push_back(CC);
1380 Ops.push_back(InFlag);
1381 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1382 InFlag = Hi.getValue(1);
1383
1384 Ops.clear();
1385 Ops.push_back(Tmp3);
1386 Ops.push_back(Tmp1);
1387 Ops.push_back(CC);
1388 Ops.push_back(InFlag);
1389 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1390 } else {
1391 Ops.push_back(Tmp2);
1392 Ops.push_back(Tmp3);
1393 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001394 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001395 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1396 InFlag = Lo.getValue(1);
1397
1398 Ops.clear();
1399 Ops.push_back(Tmp3);
1400 Ops.push_back(Tmp1);
1401 Ops.push_back(CC);
1402 Ops.push_back(InFlag);
1403 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1404 }
1405
1406 Tys.clear();
1407 Tys.push_back(MVT::i32);
1408 Tys.push_back(MVT::i32);
1409 Ops.clear();
1410 Ops.push_back(Lo);
1411 Ops.push_back(Hi);
1412 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1413 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001414 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001415 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001416 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001417 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001418
1419 SDOperand Result;
1420 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1421 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001422 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001423 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001424 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001425 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1426 DAG.getEntryNode(), Op.getOperand(0),
1427 StackSlot, DAG.getSrcValue(NULL));
1428
1429 // Build the FILD
1430 std::vector<MVT::ValueType> Tys;
1431 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001432 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001433 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001434 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001435 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001436 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001437 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001438 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1439 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001440
1441 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001442 Chain = Result.getValue(1);
1443 SDOperand InFlag = Result.getValue(2);
1444
Evan Cheng11613a52006-02-04 02:20:30 +00001445 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001446 // shouldn't be necessary except that RFP cannot be live across
1447 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1448 MachineFunction &MF = DAG.getMachineFunction();
1449 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1450 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1451 std::vector<MVT::ValueType> Tys;
1452 Tys.push_back(MVT::Other);
1453 std::vector<SDOperand> Ops;
1454 Ops.push_back(Chain);
1455 Ops.push_back(Result);
1456 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001457 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001458 Ops.push_back(InFlag);
1459 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1460 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1461 DAG.getSrcValue(NULL));
1462 }
1463
Evan Cheng6305e502006-01-12 22:54:21 +00001464 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001465 }
1466 case ISD::FP_TO_SINT: {
1467 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001468 "Unknown FP_TO_SINT to lower!");
1469 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1470 // stack slot.
1471 MachineFunction &MF = DAG.getMachineFunction();
1472 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1473 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1474 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1475
1476 unsigned Opc;
1477 switch (Op.getValueType()) {
1478 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1479 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1480 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1481 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1482 }
1483
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001484 SDOperand Chain = DAG.getEntryNode();
1485 SDOperand Value = Op.getOperand(0);
1486 if (X86ScalarSSE) {
1487 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1488 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1489 DAG.getSrcValue(0));
1490 std::vector<MVT::ValueType> Tys;
1491 Tys.push_back(MVT::f64);
1492 Tys.push_back(MVT::Other);
1493 std::vector<SDOperand> Ops;
1494 Ops.push_back(Chain);
1495 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001496 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001497 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1498 Chain = Value.getValue(1);
1499 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1500 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1501 }
1502
Chris Lattner76ac0682005-11-15 00:40:23 +00001503 // Build the FP_TO_INT*_IN_MEM
1504 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001505 Ops.push_back(Chain);
1506 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001507 Ops.push_back(StackSlot);
1508 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1509
1510 // Load the result.
1511 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1512 DAG.getSrcValue(NULL));
1513 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001514 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001515 std::vector<MVT::ValueType> Tys;
1516 Tys.push_back(MVT::Other);
1517 Tys.push_back(MVT::Flag);
1518 std::vector<SDOperand> Ops;
1519 Ops.push_back(Op.getOperand(0));
1520 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001521 Ops.clear();
1522 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1523 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1524 MVT::i32, Ops[0].getValue(2)));
1525 Ops.push_back(Ops[1].getValue(1));
1526 Tys[0] = Tys[1] = MVT::i32;
1527 Tys.push_back(MVT::Other);
1528 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001529 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001530 case ISD::FABS: {
1531 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00001532 const Type *OpNTy = MVT::getTypeForValueType(VT);
1533 std::vector<Constant*> CV;
1534 if (VT == MVT::f64) {
1535 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1536 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1537 } else {
1538 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1539 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1540 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1541 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1542 }
1543 Constant *CS = ConstantStruct::get(CV);
1544 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1545 SDOperand Mask
1546 = DAG.getNode(X86ISD::LOAD_PACK,
1547 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00001548 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1549 }
Evan Cheng72d5c252006-01-31 22:28:30 +00001550 case ISD::FNEG: {
1551 MVT::ValueType VT = Op.getValueType();
1552 const Type *OpNTy = MVT::getTypeForValueType(VT);
1553 std::vector<Constant*> CV;
1554 if (VT == MVT::f64) {
1555 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1556 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1557 } else {
1558 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1559 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1560 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1561 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1562 }
1563 Constant *CS = ConstantStruct::get(CV);
1564 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1565 SDOperand Mask
1566 = DAG.getNode(X86ISD::LOAD_PACK,
1567 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1568 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1569 }
Evan Chengc1583db2005-12-21 20:21:51 +00001570 case ISD::SETCC: {
1571 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001572 SDOperand Cond;
1573 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001574 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1575 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001576 bool Flip;
1577 unsigned X86CC;
1578 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1579 if (Flip)
1580 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1581 Op.getOperand(1), Op.getOperand(0));
1582 else
1583 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1584 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001585 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1586 DAG.getConstant(X86CC, MVT::i8), Cond);
1587 } else {
1588 assert(isFP && "Illegal integer SetCC!");
1589
Evan Cheng45df7f82006-01-30 23:41:35 +00001590 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1591 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001592 std::vector<MVT::ValueType> Tys;
1593 std::vector<SDOperand> Ops;
1594 switch (SetCCOpcode) {
1595 default: assert(false && "Illegal floating point SetCC!");
1596 case ISD::SETOEQ: { // !PF & ZF
1597 Tys.push_back(MVT::i8);
1598 Tys.push_back(MVT::Flag);
1599 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1600 Ops.push_back(Cond);
1601 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1602 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1603 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1604 Tmp1.getValue(1));
1605 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1606 }
Evan Cheng172fce72006-01-06 00:43:03 +00001607 case ISD::SETUNE: { // PF | !ZF
1608 Tys.push_back(MVT::i8);
1609 Tys.push_back(MVT::Flag);
1610 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1611 Ops.push_back(Cond);
1612 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1613 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1614 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1615 Tmp1.getValue(1));
1616 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1617 }
1618 }
1619 }
Evan Chengc1583db2005-12-21 20:21:51 +00001620 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001621 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001622 MVT::ValueType VT = Op.getValueType();
1623 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001624 bool isFPStack = isFP && !X86ScalarSSE;
1625 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001626 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001627 SDOperand Op0 = Op.getOperand(0);
1628 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001629 if (Op0.getOpcode() == ISD::SETCC)
1630 Op0 = LowerOperation(Op0, DAG);
1631
Evan Cheng73a1ad92006-01-10 20:26:56 +00001632 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001633 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1634 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1635 // have another use it will be eliminated.
1636 // If the X86ISD::SETCC has more than one use, then it's probably better
1637 // to use a test instead of duplicating the X86ISD::CMP (for register
1638 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001639 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1640 if (!Op0.hasOneUse()) {
1641 std::vector<MVT::ValueType> Tys;
1642 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1643 Tys.push_back(Op0.Val->getValueType(i));
1644 std::vector<SDOperand> Ops;
1645 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1646 Ops.push_back(Op0.getOperand(i));
1647 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1648 }
1649
Evan Chengfb22e862006-01-13 01:03:02 +00001650 CC = Op0.getOperand(0);
1651 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001652 // Make a copy as flag result cannot be used by more than one.
1653 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1654 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001655 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001656 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001657 } else
1658 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001659 } else
1660 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001661
Evan Cheng731423f2006-01-13 01:06:49 +00001662 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001663 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001664 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001665 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001666
1667 std::vector<MVT::ValueType> Tys;
1668 Tys.push_back(Op.getValueType());
1669 Tys.push_back(MVT::Flag);
1670 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001671 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1672 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001673 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001674 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001675 Ops.push_back(CC);
1676 Ops.push_back(Cond);
1677 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001678 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001679 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001680 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001681 SDOperand Cond = Op.getOperand(1);
1682 SDOperand Dest = Op.getOperand(2);
1683 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001684 if (Cond.getOpcode() == ISD::SETCC)
1685 Cond = LowerOperation(Cond, DAG);
1686
Evan Chengc1583db2005-12-21 20:21:51 +00001687 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001688 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1689 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1690 // have another use it will be eliminated.
1691 // If the X86ISD::SETCC has more than one use, then it's probably better
1692 // to use a test instead of duplicating the X86ISD::CMP (for register
1693 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001694 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1695 if (!Cond.hasOneUse()) {
1696 std::vector<MVT::ValueType> Tys;
1697 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1698 Tys.push_back(Cond.Val->getValueType(i));
1699 std::vector<SDOperand> Ops;
1700 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1701 Ops.push_back(Cond.getOperand(i));
1702 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1703 }
1704
Evan Chengfb22e862006-01-13 01:03:02 +00001705 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001706 Cond = Cond.getOperand(1);
1707 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001708 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001709 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001710 } else
1711 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001712 } else
1713 addTest = true;
1714
1715 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001716 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001717 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1718 }
1719 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1720 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1721 }
Evan Chengae986f12006-01-11 22:15:48 +00001722 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00001723 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001724 SDOperand Chain = Op.getOperand(0);
1725 unsigned Align =
1726 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1727 if (Align == 0) Align = 1;
1728
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001729 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1730 // If not DWORD aligned, call memset if size is less than the threshold.
1731 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001732 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00001733 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001734 MVT::ValueType IntPtr = getPointerTy();
1735 const Type *IntPtrTy = getTargetData().getIntPtrType();
1736 std::vector<std::pair<SDOperand, const Type*> > Args;
1737 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1738 // Extend the ubyte argument to be an int value for the call.
1739 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1740 Args.push_back(std::make_pair(Val, IntPtrTy));
1741 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1742 std::pair<SDOperand,SDOperand> CallResult =
1743 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1744 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1745 return CallResult.second;
1746 }
1747
Evan Chengae986f12006-01-11 22:15:48 +00001748 MVT::ValueType AVT;
1749 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001750 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1751 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00001752 bool TwoRepStos = false;
Evan Cheng6dc73292006-03-04 02:48:56 +00001753 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00001754 unsigned ValReg;
1755 unsigned Val = ValC->getValue() & 255;
1756
1757 // If the value is a constant, then we can potentially use larger sets.
1758 switch (Align & 3) {
1759 case 2: // WORD aligned
1760 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001761 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1762 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001763 Val = (Val << 8) | Val;
1764 ValReg = X86::AX;
1765 break;
1766 case 0: // DWORD aligned
1767 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00001768 if (I) {
1769 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1770 BytesLeft = I->getValue() % 4;
1771 } else {
1772 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1773 DAG.getConstant(2, MVT::i8));
1774 TwoRepStos = true;
1775 }
Evan Chengae986f12006-01-11 22:15:48 +00001776 Val = (Val << 8) | Val;
1777 Val = (Val << 16) | Val;
1778 ValReg = X86::EAX;
1779 break;
1780 default: // Byte aligned
1781 AVT = MVT::i8;
1782 Count = Op.getOperand(3);
1783 ValReg = X86::AL;
1784 break;
1785 }
1786
1787 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1788 InFlag);
1789 InFlag = Chain.getValue(1);
1790 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001791 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00001792 Count = Op.getOperand(3);
1793 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1794 InFlag = Chain.getValue(1);
1795 }
1796
1797 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1798 InFlag = Chain.getValue(1);
1799 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1800 InFlag = Chain.getValue(1);
1801
Evan Chengadc70932006-03-07 23:29:39 +00001802 std::vector<MVT::ValueType> Tys;
1803 Tys.push_back(MVT::Other);
1804 Tys.push_back(MVT::Flag);
1805 std::vector<SDOperand> Ops;
1806 Ops.push_back(Chain);
1807 Ops.push_back(DAG.getValueType(AVT));
1808 Ops.push_back(InFlag);
1809 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1810
1811 if (TwoRepStos) {
1812 InFlag = Chain.getValue(1);
1813 Count = Op.getOperand(3);
1814 MVT::ValueType CVT = Count.getValueType();
1815 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1816 DAG.getConstant(3, CVT));
1817 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1818 InFlag = Chain.getValue(1);
1819 Tys.clear();
1820 Tys.push_back(MVT::Other);
1821 Tys.push_back(MVT::Flag);
1822 Ops.clear();
1823 Ops.push_back(Chain);
1824 Ops.push_back(DAG.getValueType(MVT::i8));
1825 Ops.push_back(InFlag);
1826 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1827 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00001828 // Issue stores for the last 1 - 3 bytes.
1829 SDOperand Value;
1830 unsigned Val = ValC->getValue() & 255;
1831 unsigned Offset = I->getValue() - BytesLeft;
1832 SDOperand DstAddr = Op.getOperand(1);
1833 MVT::ValueType AddrVT = DstAddr.getValueType();
1834 if (BytesLeft >= 2) {
1835 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1836 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1837 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1838 DAG.getConstant(Offset, AddrVT)),
1839 DAG.getSrcValue(NULL));
1840 BytesLeft -= 2;
1841 Offset += 2;
1842 }
1843
1844 if (BytesLeft == 1) {
1845 Value = DAG.getConstant(Val, MVT::i8);
1846 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1847 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1848 DAG.getConstant(Offset, AddrVT)),
1849 DAG.getSrcValue(NULL));
1850 }
1851 }
1852
1853 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00001854 }
1855 case ISD::MEMCPY: {
1856 SDOperand Chain = Op.getOperand(0);
1857 unsigned Align =
1858 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1859 if (Align == 0) Align = 1;
1860
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001861 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1862 // If not DWORD aligned, call memcpy if size is less than the threshold.
1863 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001864 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00001865 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001866 MVT::ValueType IntPtr = getPointerTy();
1867 const Type *IntPtrTy = getTargetData().getIntPtrType();
1868 std::vector<std::pair<SDOperand, const Type*> > Args;
1869 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1870 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1871 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1872 std::pair<SDOperand,SDOperand> CallResult =
1873 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1874 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1875 return CallResult.second;
1876 }
1877
Evan Chengae986f12006-01-11 22:15:48 +00001878 MVT::ValueType AVT;
1879 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001880 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00001881 bool TwoRepMovs = false;
Evan Chengae986f12006-01-11 22:15:48 +00001882 switch (Align & 3) {
1883 case 2: // WORD aligned
1884 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001885 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1886 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001887 break;
1888 case 0: // DWORD aligned
1889 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00001890 if (I) {
1891 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1892 BytesLeft = I->getValue() % 4;
1893 } else {
1894 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1895 DAG.getConstant(2, MVT::i8));
1896 TwoRepMovs = true;
1897 }
Evan Chengae986f12006-01-11 22:15:48 +00001898 break;
1899 default: // Byte aligned
1900 AVT = MVT::i8;
1901 Count = Op.getOperand(3);
1902 break;
1903 }
1904
Evan Cheng6dc73292006-03-04 02:48:56 +00001905 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001906 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1907 InFlag = Chain.getValue(1);
1908 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1909 InFlag = Chain.getValue(1);
1910 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1911 InFlag = Chain.getValue(1);
1912
Evan Chengadc70932006-03-07 23:29:39 +00001913 std::vector<MVT::ValueType> Tys;
1914 Tys.push_back(MVT::Other);
1915 Tys.push_back(MVT::Flag);
1916 std::vector<SDOperand> Ops;
1917 Ops.push_back(Chain);
1918 Ops.push_back(DAG.getValueType(AVT));
1919 Ops.push_back(InFlag);
1920 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1921
1922 if (TwoRepMovs) {
1923 InFlag = Chain.getValue(1);
1924 Count = Op.getOperand(3);
1925 MVT::ValueType CVT = Count.getValueType();
1926 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1927 DAG.getConstant(3, CVT));
1928 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1929 InFlag = Chain.getValue(1);
1930 Tys.clear();
1931 Tys.push_back(MVT::Other);
1932 Tys.push_back(MVT::Flag);
1933 Ops.clear();
1934 Ops.push_back(Chain);
1935 Ops.push_back(DAG.getValueType(MVT::i8));
1936 Ops.push_back(InFlag);
1937 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1938 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00001939 // Issue loads and stores for the last 1 - 3 bytes.
1940 unsigned Offset = I->getValue() - BytesLeft;
1941 SDOperand DstAddr = Op.getOperand(1);
1942 MVT::ValueType DstVT = DstAddr.getValueType();
1943 SDOperand SrcAddr = Op.getOperand(2);
1944 MVT::ValueType SrcVT = SrcAddr.getValueType();
1945 SDOperand Value;
1946 if (BytesLeft >= 2) {
1947 Value = DAG.getLoad(MVT::i16, Chain,
1948 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1949 DAG.getConstant(Offset, SrcVT)),
1950 DAG.getSrcValue(NULL));
1951 Chain = Value.getValue(1);
1952 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1953 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1954 DAG.getConstant(Offset, DstVT)),
1955 DAG.getSrcValue(NULL));
1956 BytesLeft -= 2;
1957 Offset += 2;
1958 }
1959
1960 if (BytesLeft == 1) {
1961 Value = DAG.getLoad(MVT::i8, Chain,
1962 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1963 DAG.getConstant(Offset, SrcVT)),
1964 DAG.getSrcValue(NULL));
1965 Chain = Value.getValue(1);
1966 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1967 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1968 DAG.getConstant(Offset, DstVT)),
1969 DAG.getSrcValue(NULL));
1970 }
1971 }
1972
1973 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00001974 }
Evan Cheng99470012006-02-25 09:55:19 +00001975
1976 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1977 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1978 // one of the above mentioned nodes. It has to be wrapped because otherwise
1979 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1980 // be used to form addressing mode. These wrapped nodes will be selected
1981 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00001982 case ISD::ConstantPool: {
1983 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001984 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1985 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1986 CP->getAlignment()));
Evan Cheng1f342c22006-02-23 02:43:52 +00001987 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00001988 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00001989 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00001990 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1991 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1992 }
1993
1994 return Result;
1995 }
Evan Cheng5c59d492005-12-23 07:31:11 +00001996 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001997 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1998 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1999 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga74ce622005-12-21 02:39:21 +00002000 if (getTargetMachine().
Evan Cheng5588de92006-02-18 00:15:05 +00002001 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002002 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002003 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00002004 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2005 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00002006
2007 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Chengaf598d22006-03-13 23:18:16 +00002008 // the value at address GV, not the value of GV itself. This means that
Evan Cheng5588de92006-02-18 00:15:05 +00002009 // the GlobalAddress must be in the base or index register of the address,
2010 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00002011 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Chengaf598d22006-03-13 23:18:16 +00002012 DarwinGVRequiresExtraLoad(GV))
Evan Cheng5a766802006-02-07 08:38:37 +00002013 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00002014 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00002015 }
Evan Cheng5588de92006-02-18 00:15:05 +00002016
Evan Chengb94db9e2006-01-12 07:56:47 +00002017 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002018 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002019 case ISD::ExternalSymbol: {
2020 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2021 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2022 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2023 if (getTargetMachine().
2024 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2025 // With PIC, the address is actually $g + Offset.
2026 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2027 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2028 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2029 }
2030
2031 return Result;
2032 }
Nate Begemane74795c2006-01-25 18:21:52 +00002033 case ISD::VASTART: {
2034 // vastart just stores the address of the VarArgsFrameIndex slot into the
2035 // memory location argument.
2036 // FIXME: Replace MVT::i32 with PointerTy
2037 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2038 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2039 Op.getOperand(1), Op.getOperand(2));
2040 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002041 case ISD::RET: {
2042 SDOperand Copy;
2043
2044 switch(Op.getNumOperands()) {
2045 default:
2046 assert(0 && "Do not know how to return this many arguments!");
2047 abort();
2048 case 1:
2049 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2050 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2051 case 2: {
2052 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2053 if (MVT::isInteger(ArgVT))
2054 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2055 SDOperand());
2056 else if (!X86ScalarSSE) {
2057 std::vector<MVT::ValueType> Tys;
2058 Tys.push_back(MVT::Other);
2059 Tys.push_back(MVT::Flag);
2060 std::vector<SDOperand> Ops;
2061 Ops.push_back(Op.getOperand(0));
2062 Ops.push_back(Op.getOperand(1));
2063 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2064 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00002065 SDOperand MemLoc;
2066 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00002067 SDOperand Value = Op.getOperand(1);
2068
Evan Chenga24617f2006-02-01 01:19:32 +00002069 if (Value.getOpcode() == ISD::LOAD &&
2070 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00002071 Chain = Value.getOperand(0);
2072 MemLoc = Value.getOperand(1);
2073 } else {
2074 // Spill the value to memory and reload it into top of stack.
2075 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2076 MachineFunction &MF = DAG.getMachineFunction();
2077 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2078 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2079 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2080 Value, MemLoc, DAG.getSrcValue(0));
2081 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002082 std::vector<MVT::ValueType> Tys;
2083 Tys.push_back(MVT::f64);
2084 Tys.push_back(MVT::Other);
2085 std::vector<SDOperand> Ops;
2086 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002087 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002088 Ops.push_back(DAG.getValueType(ArgVT));
2089 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2090 Tys.clear();
2091 Tys.push_back(MVT::Other);
2092 Tys.push_back(MVT::Flag);
2093 Ops.clear();
2094 Ops.push_back(Copy.getValue(1));
2095 Ops.push_back(Copy);
2096 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2097 }
2098 break;
2099 }
2100 case 3:
2101 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2102 SDOperand());
2103 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2104 break;
2105 }
2106 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2107 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2108 Copy.getValue(1));
2109 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002110 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002111}
Evan Cheng6af02632005-12-20 06:22:03 +00002112
2113const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2114 switch (Opcode) {
2115 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00002116 case X86ISD::SHLD: return "X86ISD::SHLD";
2117 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00002118 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00002119 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00002120 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00002121 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00002122 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2123 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2124 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00002125 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00002126 case X86ISD::FST: return "X86ISD::FST";
2127 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00002128 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00002129 case X86ISD::CALL: return "X86ISD::CALL";
2130 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2131 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2132 case X86ISD::CMP: return "X86ISD::CMP";
2133 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00002134 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00002135 case X86ISD::CMOV: return "X86ISD::CMOV";
2136 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00002137 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00002138 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2139 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00002140 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00002141 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002142 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng6af02632005-12-20 06:22:03 +00002143 }
2144}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002145
Nate Begeman8a77efe2006-02-16 21:11:51 +00002146void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2147 uint64_t Mask,
2148 uint64_t &KnownZero,
2149 uint64_t &KnownOne,
2150 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002151
2152 unsigned Opc = Op.getOpcode();
Nate Begeman8a77efe2006-02-16 21:11:51 +00002153 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002154
2155 switch (Opc) {
2156 default:
2157 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2158 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00002159 case X86ISD::SETCC:
2160 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2161 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002162 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002163}
Chris Lattnerc642aa52006-01-31 19:43:35 +00002164
2165std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00002166getRegClassForInlineAsmConstraint(const std::string &Constraint,
2167 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00002168 if (Constraint.size() == 1) {
2169 // FIXME: not handling fp-stack yet!
2170 // FIXME: not handling MMX registers yet ('y' constraint).
2171 switch (Constraint[0]) { // GCC X86 Constraint Letters
2172 default: break; // Unknown constriant letter
2173 case 'r': // GENERAL_REGS
2174 case 'R': // LEGACY_REGS
2175 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2176 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2177 case 'l': // INDEX_REGS
2178 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2179 X86::ESI, X86::EDI, X86::EBP, 0);
2180 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2181 case 'Q': // Q_REGS
2182 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2183 case 'x': // SSE_REGS if SSE1 allowed
2184 if (Subtarget->hasSSE1())
2185 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2186 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2187 0);
2188 return std::vector<unsigned>();
2189 case 'Y': // SSE_REGS if SSE2 allowed
2190 if (Subtarget->hasSSE2())
2191 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2192 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2193 0);
2194 return std::vector<unsigned>();
2195 }
2196 }
2197
Chris Lattner7ad77df2006-02-22 00:56:39 +00002198 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00002199}
Evan Chengaf598d22006-03-13 23:18:16 +00002200
2201/// isLegalAddressImmediate - Return true if the integer value or
2202/// GlobalValue can be used as the offset of the target addressing mode.
2203bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2204 // X86 allows a sign-extended 32-bit immediate field.
2205 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2206}
2207
2208bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2209 if (getTargetMachine().
2210 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2211 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2212 if (RModel == Reloc::Static)
2213 return true;
2214 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00002215 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00002216 else
2217 return false;
2218 } else
2219 return true;
2220}