blob: 174c7979043434a1afccb3b5d84a6cee229996c3 [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
Evan Chengb09a56f2006-03-17 20:31:41 +000053 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
54 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
55 setUseUnderscoreSetJmpLongJmp(true);
56
Evan Cheng20931a72006-03-16 21:47:42 +000057 // Add legal addressing mode scale values.
58 addLegalAddressScale(8);
59 addLegalAddressScale(4);
60 addLegalAddressScale(2);
61 // Enter the ones which require both scale + index last. These are more
62 // expensive.
63 addLegalAddressScale(9);
64 addLegalAddressScale(5);
65 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000066
Chris Lattner76ac0682005-11-15 00:40:23 +000067 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000068 addRegisterClass(MVT::i8, X86::R8RegisterClass);
69 addRegisterClass(MVT::i16, X86::R16RegisterClass);
70 addRegisterClass(MVT::i32, X86::R32RegisterClass);
71
72 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
73 // operation.
74 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
75 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000077
78 if (X86ScalarSSE)
79 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
80 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
81 else
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000083
84 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
85 // this operation.
86 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
87 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000088 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000089 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000090 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000091 else {
92 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
93 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
94 }
Chris Lattner76ac0682005-11-15 00:40:23 +000095
Evan Cheng5b97fcf2006-01-30 08:02:57 +000096 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
97 // isn't legal.
98 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
99 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
100
Evan Cheng08390f62006-01-30 22:13:22 +0000101 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
102 // this operation.
103 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
104 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
105
106 if (X86ScalarSSE) {
107 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
108 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000111 }
112
113 // Handle FP_TO_UINT by promoting the destination to a larger signed
114 // conversion.
115 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
116 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
118
Evan Chengd13778e2006-02-18 07:26:17 +0000119 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000120 // Expand FP_TO_UINT into a select.
121 // FIXME: We would like to use a Custom expander here eventually to do
122 // the optimal thing for SSE vs. the default expansion in the legalizer.
123 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
124 else
Evan Chengd13778e2006-02-18 07:26:17 +0000125 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
127
Evan Cheng08390f62006-01-30 22:13:22 +0000128 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
129 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000130
Evan Cheng593bea72006-02-17 07:01:52 +0000131 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000132 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
133 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000134 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
135 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
138 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
139 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
140 setOperationAction(ISD::FREM , MVT::f64 , Expand);
141 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
142 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
143 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
145 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
146 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
149 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000150 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000151 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000152
Chris Lattner76ac0682005-11-15 00:40:23 +0000153 // These should be promoted to a larger select which is supported.
154 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
155 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000156
157 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000158 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
159 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
160 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
162 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
165 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000167 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000168 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000169 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000170 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000171 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000172 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000174 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
175 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000177 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000178 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
179 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000180
Chris Lattner9c415362005-11-29 06:16:21 +0000181 // We don't have line number support yet.
182 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000183 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000184 // FIXME - use subtarget debug flags
185 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
186 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000187
Nate Begemane74795c2006-01-25 18:21:52 +0000188 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
189 setOperationAction(ISD::VASTART , MVT::Other, Custom);
190
191 // Use the default implementation.
192 setOperationAction(ISD::VAARG , MVT::Other, Expand);
193 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
194 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
197 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000198
Chris Lattner9c7f5032006-03-05 05:08:37 +0000199 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
200 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
201
Chris Lattner76ac0682005-11-15 00:40:23 +0000202 if (X86ScalarSSE) {
203 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000204 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000206
207 // SSE has no load+extend ops
208 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
209 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
210
Evan Cheng72d5c252006-01-31 22:28:30 +0000211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
Evan Chengd8fba3a2006-02-02 00:28:23 +0000219 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f32, Expand);
226
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000227 // Expand FP immediates into loads from the stack, except for the special
228 // cases we handle.
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // xorps / xorpd
232 } else {
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000235
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237
Chris Lattner76ac0682005-11-15 00:40:23 +0000238 if (!UnsafeFPMath) {
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
241 }
242
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
248 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000249
Evan Cheng19264272006-03-01 01:11:20 +0000250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattner80b6bd272006-03-20 06:18:01 +0000258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000260 }
261
Evan Cheng9e252e32006-02-22 02:26:30 +0000262 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
266
Evan Cheng19264272006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Chris Lattnerf7b6e722006-03-19 01:13:28 +0000268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000271 }
272
273 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng19264272006-03-01 01:11:20 +0000276 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
277 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
278 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
279 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Chris Lattnerf7b6e722006-03-19 01:13:28 +0000280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000281 }
282
283 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
284 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
285 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
289
290
Evan Cheng19264272006-03-01 01:11:20 +0000291 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
292 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
293 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
294 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Chris Lattnerf7b6e722006-03-19 01:13:28 +0000295 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
298 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
299 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000300 }
301
Chris Lattner76ac0682005-11-15 00:40:23 +0000302 computeRegisterProperties();
303
Evan Cheng6a374562006-02-14 08:25:08 +0000304 // FIXME: These should be based on subtarget info. Plus, the values should
305 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000306 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
307 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
308 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000309 allowUnalignedMemoryAccesses = true; // x86 supports it!
310}
311
312std::vector<SDOperand>
313X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
314 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
315 return LowerFastCCArguments(F, DAG);
316 return LowerCCCArguments(F, DAG);
317}
318
319std::pair<SDOperand, SDOperand>
320X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
321 bool isVarArg, unsigned CallingConv,
322 bool isTailCall,
323 SDOperand Callee, ArgListTy &Args,
324 SelectionDAG &DAG) {
325 assert((!isVarArg || CallingConv == CallingConv::C) &&
326 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000327
328 // If the callee is a GlobalAddress node (quite common, every direct call is)
329 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
330 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
331 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000332 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
333 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000334
Chris Lattner76ac0682005-11-15 00:40:23 +0000335 if (CallingConv == CallingConv::Fast && EnableFastCC)
336 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
337 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
338}
339
340//===----------------------------------------------------------------------===//
341// C Calling Convention implementation
342//===----------------------------------------------------------------------===//
343
344std::vector<SDOperand>
345X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
346 std::vector<SDOperand> ArgValues;
347
348 MachineFunction &MF = DAG.getMachineFunction();
349 MachineFrameInfo *MFI = MF.getFrameInfo();
350
351 // Add DAG nodes to load the arguments... On entry to a function on the X86,
352 // the stack frame looks like this:
353 //
354 // [ESP] -- return address
355 // [ESP + 4] -- first argument (leftmost lexically)
356 // [ESP + 8] -- second argument, if first argument is four bytes in size
357 // ...
358 //
359 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
360 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
361 MVT::ValueType ObjectVT = getValueType(I->getType());
362 unsigned ArgIncrement = 4;
363 unsigned ObjSize;
364 switch (ObjectVT) {
365 default: assert(0 && "Unhandled argument type!");
366 case MVT::i1:
367 case MVT::i8: ObjSize = 1; break;
368 case MVT::i16: ObjSize = 2; break;
369 case MVT::i32: ObjSize = 4; break;
370 case MVT::i64: ObjSize = ArgIncrement = 8; break;
371 case MVT::f32: ObjSize = 4; break;
372 case MVT::f64: ObjSize = ArgIncrement = 8; break;
373 }
374 // Create the frame index object for this incoming parameter...
375 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
376
377 // Create the SelectionDAG nodes corresponding to a load from this parameter
378 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
379
380 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
381 // dead loads.
382 SDOperand ArgValue;
383 if (!I->use_empty())
384 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
385 DAG.getSrcValue(NULL));
386 else {
387 if (MVT::isInteger(ObjectVT))
388 ArgValue = DAG.getConstant(0, ObjectVT);
389 else
390 ArgValue = DAG.getConstantFP(0, ObjectVT);
391 }
392 ArgValues.push_back(ArgValue);
393
394 ArgOffset += ArgIncrement; // Move on to the next argument...
395 }
396
397 // If the function takes variable number of arguments, make a frame index for
398 // the start of the first vararg value... for expansion of llvm.va_start.
399 if (F.isVarArg())
400 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
401 ReturnAddrIndex = 0; // No return address slot generated yet.
402 BytesToPopOnReturn = 0; // Callee pops nothing.
403 BytesCallerReserves = ArgOffset;
404
405 // Finally, inform the code generator which regs we return values in.
406 switch (getValueType(F.getReturnType())) {
407 default: assert(0 && "Unknown type!");
408 case MVT::isVoid: break;
409 case MVT::i1:
410 case MVT::i8:
411 case MVT::i16:
412 case MVT::i32:
413 MF.addLiveOut(X86::EAX);
414 break;
415 case MVT::i64:
416 MF.addLiveOut(X86::EAX);
417 MF.addLiveOut(X86::EDX);
418 break;
419 case MVT::f32:
420 case MVT::f64:
421 MF.addLiveOut(X86::ST0);
422 break;
423 }
424 return ArgValues;
425}
426
427std::pair<SDOperand, SDOperand>
428X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
429 bool isVarArg, bool isTailCall,
430 SDOperand Callee, ArgListTy &Args,
431 SelectionDAG &DAG) {
432 // Count how many bytes are to be pushed on the stack.
433 unsigned NumBytes = 0;
434
435 if (Args.empty()) {
436 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000437 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000438 } else {
439 for (unsigned i = 0, e = Args.size(); i != e; ++i)
440 switch (getValueType(Args[i].second)) {
441 default: assert(0 && "Unknown value type!");
442 case MVT::i1:
443 case MVT::i8:
444 case MVT::i16:
445 case MVT::i32:
446 case MVT::f32:
447 NumBytes += 4;
448 break;
449 case MVT::i64:
450 case MVT::f64:
451 NumBytes += 8;
452 break;
453 }
454
Chris Lattner62c34842006-02-13 09:00:43 +0000455 Chain = DAG.getCALLSEQ_START(Chain,
456 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000457
458 // Arguments go on the stack in reverse order, as specified by the ABI.
459 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000460 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000461 std::vector<SDOperand> Stores;
462
463 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
464 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
465 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
466
467 switch (getValueType(Args[i].second)) {
468 default: assert(0 && "Unexpected ValueType for argument!");
469 case MVT::i1:
470 case MVT::i8:
471 case MVT::i16:
472 // Promote the integer to 32 bits. If the input type is signed use a
473 // sign extend, otherwise use a zero extend.
474 if (Args[i].second->isSigned())
475 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
476 else
477 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
478
479 // FALL THROUGH
480 case MVT::i32:
481 case MVT::f32:
482 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
483 Args[i].first, PtrOff,
484 DAG.getSrcValue(NULL)));
485 ArgOffset += 4;
486 break;
487 case MVT::i64:
488 case MVT::f64:
489 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
490 Args[i].first, PtrOff,
491 DAG.getSrcValue(NULL)));
492 ArgOffset += 8;
493 break;
494 }
495 }
496 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
497 }
498
499 std::vector<MVT::ValueType> RetVals;
500 MVT::ValueType RetTyVT = getValueType(RetTy);
501 RetVals.push_back(MVT::Other);
502
503 // The result values produced have to be legal. Promote the result.
504 switch (RetTyVT) {
505 case MVT::isVoid: break;
506 default:
507 RetVals.push_back(RetTyVT);
508 break;
509 case MVT::i1:
510 case MVT::i8:
511 case MVT::i16:
512 RetVals.push_back(MVT::i32);
513 break;
514 case MVT::f32:
515 if (X86ScalarSSE)
516 RetVals.push_back(MVT::f32);
517 else
518 RetVals.push_back(MVT::f64);
519 break;
520 case MVT::i64:
521 RetVals.push_back(MVT::i32);
522 RetVals.push_back(MVT::i32);
523 break;
524 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000525
Nate Begeman7e5496d2006-02-17 00:03:04 +0000526 std::vector<MVT::ValueType> NodeTys;
527 NodeTys.push_back(MVT::Other); // Returns a chain
528 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
529 std::vector<SDOperand> Ops;
530 Ops.push_back(Chain);
531 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000532
Nate Begeman7e5496d2006-02-17 00:03:04 +0000533 // FIXME: Do not generate X86ISD::TAILCALL for now.
534 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
535 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000536
Nate Begeman7e5496d2006-02-17 00:03:04 +0000537 NodeTys.clear();
538 NodeTys.push_back(MVT::Other); // Returns a chain
539 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
540 Ops.clear();
541 Ops.push_back(Chain);
542 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
543 Ops.push_back(DAG.getConstant(0, getPointerTy()));
544 Ops.push_back(InFlag);
545 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
546 InFlag = Chain.getValue(1);
547
548 SDOperand RetVal;
549 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000550 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000551 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000552 case MVT::i1:
553 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000554 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
555 Chain = RetVal.getValue(1);
556 if (RetTyVT == MVT::i1)
557 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
558 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000559 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000560 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
561 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000562 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000563 case MVT::i32:
564 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
565 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000566 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000567 case MVT::i64: {
568 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
569 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
570 Lo.getValue(2));
571 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
572 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000573 break;
574 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000575 case MVT::f32:
576 case MVT::f64: {
577 std::vector<MVT::ValueType> Tys;
578 Tys.push_back(MVT::f64);
579 Tys.push_back(MVT::Other);
580 Tys.push_back(MVT::Flag);
581 std::vector<SDOperand> Ops;
582 Ops.push_back(Chain);
583 Ops.push_back(InFlag);
584 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
585 Chain = RetVal.getValue(1);
586 InFlag = RetVal.getValue(2);
587 if (X86ScalarSSE) {
588 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
589 // shouldn't be necessary except that RFP cannot be live across
590 // multiple blocks. When stackifier is fixed, they can be uncoupled.
591 MachineFunction &MF = DAG.getMachineFunction();
592 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
593 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
594 Tys.clear();
595 Tys.push_back(MVT::Other);
596 Ops.clear();
597 Ops.push_back(Chain);
598 Ops.push_back(RetVal);
599 Ops.push_back(StackSlot);
600 Ops.push_back(DAG.getValueType(RetTyVT));
601 Ops.push_back(InFlag);
602 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
603 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
604 DAG.getSrcValue(NULL));
605 Chain = RetVal.getValue(1);
606 }
Evan Cheng45e190982006-01-05 00:27:02 +0000607
Nate Begeman7e5496d2006-02-17 00:03:04 +0000608 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
609 // FIXME: we would really like to remember that this FP_ROUND
610 // operation is okay to eliminate if we allow excess FP precision.
611 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
612 break;
613 }
614 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000615 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000616
617 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000618}
619
Chris Lattner76ac0682005-11-15 00:40:23 +0000620//===----------------------------------------------------------------------===//
621// Fast Calling Convention implementation
622//===----------------------------------------------------------------------===//
623//
624// The X86 'fast' calling convention passes up to two integer arguments in
625// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
626// and requires that the callee pop its arguments off the stack (allowing proper
627// tail calls), and has the same return value conventions as C calling convs.
628//
629// This calling convention always arranges for the callee pop value to be 8n+4
630// bytes, which is needed for tail recursion elimination and stack alignment
631// reasons.
632//
633// Note that this can be enhanced in the future to pass fp vals in registers
634// (when we have a global fp allocator) and do other tricks.
635//
636
637/// AddLiveIn - This helper function adds the specified physical register to the
638/// MachineFunction as a live in value. It also creates a corresponding virtual
639/// register for it.
640static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
641 TargetRegisterClass *RC) {
642 assert(RC->contains(PReg) && "Not the correct regclass!");
643 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
644 MF.addLiveIn(PReg, VReg);
645 return VReg;
646}
647
Chris Lattner388fc4d2006-03-17 17:27:47 +0000648// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
649// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
650// EDX". Anything more is illegal.
651//
652// FIXME: The linscan register allocator currently has problem with
653// coallescing. At the time of this writing, whenever it decides to coallesce
654// a physreg with a virtreg, this increases the size of the physreg's live
655// range, and the live range cannot ever be reduced. This causes problems if
656// too many physregs are coalleced with virtregs, which can cause the register
657// allocator to wedge itself.
658//
659// This code triggers this problem more often if we pass args in registers,
660// so disable it until this is fixed.
661//
662// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
663// about code being dead.
664//
665static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000666
Chris Lattner76ac0682005-11-15 00:40:23 +0000667
668std::vector<SDOperand>
669X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
670 std::vector<SDOperand> ArgValues;
671
672 MachineFunction &MF = DAG.getMachineFunction();
673 MachineFrameInfo *MFI = MF.getFrameInfo();
674
675 // Add DAG nodes to load the arguments... On entry to a function the stack
676 // frame looks like this:
677 //
678 // [ESP] -- return address
679 // [ESP + 4] -- first nonreg argument (leftmost lexically)
680 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
681 // ...
682 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
683
684 // Keep track of the number of integer regs passed so far. This can be either
685 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
686 // used).
687 unsigned NumIntRegs = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000688
Chris Lattner76ac0682005-11-15 00:40:23 +0000689 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
690 MVT::ValueType ObjectVT = getValueType(I->getType());
691 unsigned ArgIncrement = 4;
692 unsigned ObjSize = 0;
693 SDOperand ArgValue;
694
695 switch (ObjectVT) {
696 default: assert(0 && "Unhandled argument type!");
697 case MVT::i1:
698 case MVT::i8:
Chris Lattner43798852006-03-17 05:10:20 +0000699 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000700 if (!I->use_empty()) {
701 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
702 X86::R8RegisterClass);
703 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
704 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000705 if (ObjectVT == MVT::i1)
706 // FIXME: Should insert a assertzext here.
707 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000708 }
709 ++NumIntRegs;
710 break;
711 }
712
713 ObjSize = 1;
714 break;
715 case MVT::i16:
Chris Lattner43798852006-03-17 05:10:20 +0000716 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000717 if (!I->use_empty()) {
718 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
719 X86::R16RegisterClass);
720 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
721 DAG.setRoot(ArgValue.getValue(1));
722 }
723 ++NumIntRegs;
724 break;
725 }
726 ObjSize = 2;
727 break;
728 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000729 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000730 if (!I->use_empty()) {
Chris Lattner43798852006-03-17 05:10:20 +0000731 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattner76ac0682005-11-15 00:40:23 +0000732 X86::R32RegisterClass);
733 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
734 DAG.setRoot(ArgValue.getValue(1));
735 }
736 ++NumIntRegs;
737 break;
738 }
739 ObjSize = 4;
740 break;
741 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000742 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000743 if (!I->use_empty()) {
744 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
745 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
746
747 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
748 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
749 DAG.setRoot(Hi.getValue(1));
750
751 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
752 }
Chris Lattner43798852006-03-17 05:10:20 +0000753 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000754 break;
Chris Lattner43798852006-03-17 05:10:20 +0000755 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000756 if (!I->use_empty()) {
757 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
758 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
759 DAG.setRoot(Low.getValue(1));
760
761 // Load the high part from memory.
762 // Create the frame index object for this incoming parameter...
763 int FI = MFI->CreateFixedObject(4, ArgOffset);
764 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
765 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
766 DAG.getSrcValue(NULL));
767 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
768 }
769 ArgOffset += 4;
Chris Lattner43798852006-03-17 05:10:20 +0000770 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000771 break;
772 }
773 ObjSize = ArgIncrement = 8;
774 break;
775 case MVT::f32: ObjSize = 4; break;
776 case MVT::f64: ObjSize = ArgIncrement = 8; break;
777 }
778
779 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
780 // dead loads.
781 if (ObjSize && !I->use_empty()) {
782 // Create the frame index object for this incoming parameter...
783 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
784
785 // Create the SelectionDAG nodes corresponding to a load from this
786 // parameter.
787 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
788
789 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
790 DAG.getSrcValue(NULL));
791 } else if (ArgValue.Val == 0) {
792 if (MVT::isInteger(ObjectVT))
793 ArgValue = DAG.getConstant(0, ObjectVT);
794 else
795 ArgValue = DAG.getConstantFP(0, ObjectVT);
796 }
797 ArgValues.push_back(ArgValue);
798
799 if (ObjSize)
800 ArgOffset += ArgIncrement; // Move on to the next argument.
801 }
802
803 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
804 // arguments and the arguments after the retaddr has been pushed are aligned.
805 if ((ArgOffset & 7) == 0)
806 ArgOffset += 4;
807
808 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
809 ReturnAddrIndex = 0; // No return address slot generated yet.
810 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
811 BytesCallerReserves = 0;
812
813 // Finally, inform the code generator which regs we return values in.
814 switch (getValueType(F.getReturnType())) {
815 default: assert(0 && "Unknown type!");
816 case MVT::isVoid: break;
817 case MVT::i1:
818 case MVT::i8:
819 case MVT::i16:
820 case MVT::i32:
821 MF.addLiveOut(X86::EAX);
822 break;
823 case MVT::i64:
824 MF.addLiveOut(X86::EAX);
825 MF.addLiveOut(X86::EDX);
826 break;
827 case MVT::f32:
828 case MVT::f64:
829 MF.addLiveOut(X86::ST0);
830 break;
831 }
832 return ArgValues;
833}
834
835std::pair<SDOperand, SDOperand>
836X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
837 bool isTailCall, SDOperand Callee,
838 ArgListTy &Args, SelectionDAG &DAG) {
839 // Count how many bytes are to be pushed on the stack.
840 unsigned NumBytes = 0;
841
842 // Keep track of the number of integer regs passed so far. This can be either
843 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
844 // used).
845 unsigned NumIntRegs = 0;
846
847 for (unsigned i = 0, e = Args.size(); i != e; ++i)
848 switch (getValueType(Args[i].second)) {
849 default: assert(0 && "Unknown value type!");
850 case MVT::i1:
851 case MVT::i8:
852 case MVT::i16:
853 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000854 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000855 ++NumIntRegs;
856 break;
857 }
858 // fall through
859 case MVT::f32:
860 NumBytes += 4;
861 break;
862 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000863 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
864 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000865 break;
Chris Lattner43798852006-03-17 05:10:20 +0000866 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
867 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000868 NumBytes += 4;
869 break;
870 }
871
872 // fall through
873 case MVT::f64:
874 NumBytes += 8;
875 break;
876 }
877
878 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
879 // arguments and the arguments after the retaddr has been pushed are aligned.
880 if ((NumBytes & 7) == 0)
881 NumBytes += 4;
882
Chris Lattner62c34842006-02-13 09:00:43 +0000883 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000884
885 // Arguments go on the stack in reverse order, as specified by the ABI.
886 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000887 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000888 NumIntRegs = 0;
889 std::vector<SDOperand> Stores;
890 std::vector<SDOperand> RegValuesToPass;
891 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
892 switch (getValueType(Args[i].second)) {
893 default: assert(0 && "Unexpected ValueType for argument!");
894 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000895 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
896 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000897 case MVT::i8:
898 case MVT::i16:
899 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000900 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000901 RegValuesToPass.push_back(Args[i].first);
902 ++NumIntRegs;
903 break;
904 }
905 // Fall through
906 case MVT::f32: {
907 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
908 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
909 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
910 Args[i].first, PtrOff,
911 DAG.getSrcValue(NULL)));
912 ArgOffset += 4;
913 break;
914 }
915 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000916 // Can pass (at least) part of it in regs?
917 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000918 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
919 Args[i].first, DAG.getConstant(1, MVT::i32));
920 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
921 Args[i].first, DAG.getConstant(0, MVT::i32));
922 RegValuesToPass.push_back(Lo);
923 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +0000924
925 // Pass both parts in regs?
926 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000927 RegValuesToPass.push_back(Hi);
928 ++NumIntRegs;
929 } else {
930 // Pass the high part in memory.
931 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
932 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
933 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
934 Hi, PtrOff, DAG.getSrcValue(NULL)));
935 ArgOffset += 4;
936 }
937 break;
938 }
939 // Fall through
940 case MVT::f64:
941 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
942 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
943 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
944 Args[i].first, PtrOff,
945 DAG.getSrcValue(NULL)));
946 ArgOffset += 8;
947 break;
948 }
949 }
950 if (!Stores.empty())
951 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
952
953 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
954 // arguments and the arguments after the retaddr has been pushed are aligned.
955 if ((ArgOffset & 7) == 0)
956 ArgOffset += 4;
957
958 std::vector<MVT::ValueType> RetVals;
959 MVT::ValueType RetTyVT = getValueType(RetTy);
960
961 RetVals.push_back(MVT::Other);
962
963 // The result values produced have to be legal. Promote the result.
964 switch (RetTyVT) {
965 case MVT::isVoid: break;
966 default:
967 RetVals.push_back(RetTyVT);
968 break;
969 case MVT::i1:
970 case MVT::i8:
971 case MVT::i16:
972 RetVals.push_back(MVT::i32);
973 break;
974 case MVT::f32:
975 if (X86ScalarSSE)
976 RetVals.push_back(MVT::f32);
977 else
978 RetVals.push_back(MVT::f64);
979 break;
980 case MVT::i64:
981 RetVals.push_back(MVT::i32);
982 RetVals.push_back(MVT::i32);
983 break;
984 }
985
Nate Begeman7e5496d2006-02-17 00:03:04 +0000986 // Build a sequence of copy-to-reg nodes chained together with token chain
987 // and flag operands which copy the outgoing args into registers.
988 SDOperand InFlag;
989 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
990 unsigned CCReg;
991 SDOperand RegToPass = RegValuesToPass[i];
992 switch (RegToPass.getValueType()) {
993 default: assert(0 && "Bad thing to pass in regs");
994 case MVT::i8:
995 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +0000996 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000997 case MVT::i16:
998 CCReg = (i == 0) ? X86::AX : X86::DX;
999 break;
1000 case MVT::i32:
1001 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1002 break;
1003 }
1004
1005 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1006 InFlag = Chain.getValue(1);
1007 }
1008
1009 std::vector<MVT::ValueType> NodeTys;
1010 NodeTys.push_back(MVT::Other); // Returns a chain
1011 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1012 std::vector<SDOperand> Ops;
1013 Ops.push_back(Chain);
1014 Ops.push_back(Callee);
1015 if (InFlag.Val)
1016 Ops.push_back(InFlag);
1017
1018 // FIXME: Do not generate X86ISD::TAILCALL for now.
1019 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1020 InFlag = Chain.getValue(1);
1021
1022 NodeTys.clear();
1023 NodeTys.push_back(MVT::Other); // Returns a chain
1024 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1025 Ops.clear();
1026 Ops.push_back(Chain);
1027 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1028 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1029 Ops.push_back(InFlag);
1030 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1031 InFlag = Chain.getValue(1);
1032
1033 SDOperand RetVal;
1034 if (RetTyVT != MVT::isVoid) {
1035 switch (RetTyVT) {
1036 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001037 case MVT::i1:
1038 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001039 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1040 Chain = RetVal.getValue(1);
1041 if (RetTyVT == MVT::i1)
1042 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1043 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001044 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001045 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1046 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001047 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001048 case MVT::i32:
1049 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1050 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001051 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001052 case MVT::i64: {
1053 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1054 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1055 Lo.getValue(2));
1056 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1057 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001058 break;
1059 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001060 case MVT::f32:
1061 case MVT::f64: {
1062 std::vector<MVT::ValueType> Tys;
1063 Tys.push_back(MVT::f64);
1064 Tys.push_back(MVT::Other);
1065 Tys.push_back(MVT::Flag);
1066 std::vector<SDOperand> Ops;
1067 Ops.push_back(Chain);
1068 Ops.push_back(InFlag);
1069 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1070 Chain = RetVal.getValue(1);
1071 InFlag = RetVal.getValue(2);
1072 if (X86ScalarSSE) {
1073 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1074 // shouldn't be necessary except that RFP cannot be live across
1075 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1076 MachineFunction &MF = DAG.getMachineFunction();
1077 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1078 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1079 Tys.clear();
1080 Tys.push_back(MVT::Other);
1081 Ops.clear();
1082 Ops.push_back(Chain);
1083 Ops.push_back(RetVal);
1084 Ops.push_back(StackSlot);
1085 Ops.push_back(DAG.getValueType(RetTyVT));
1086 Ops.push_back(InFlag);
1087 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1088 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1089 DAG.getSrcValue(NULL));
1090 Chain = RetVal.getValue(1);
1091 }
Evan Cheng172fce72006-01-06 00:43:03 +00001092
Nate Begeman7e5496d2006-02-17 00:03:04 +00001093 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1094 // FIXME: we would really like to remember that this FP_ROUND
1095 // operation is okay to eliminate if we allow excess FP precision.
1096 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1097 break;
1098 }
1099 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001100 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001101
1102 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001103}
1104
1105SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1106 if (ReturnAddrIndex == 0) {
1107 // Set up a frame object for the return address.
1108 MachineFunction &MF = DAG.getMachineFunction();
1109 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1110 }
1111
1112 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1113}
1114
1115
1116
1117std::pair<SDOperand, SDOperand> X86TargetLowering::
1118LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1119 SelectionDAG &DAG) {
1120 SDOperand Result;
1121 if (Depth) // Depths > 0 not supported yet!
1122 Result = DAG.getConstant(0, getPointerTy());
1123 else {
1124 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1125 if (!isFrameAddress)
1126 // Just load the return address
1127 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1128 DAG.getSrcValue(NULL));
1129 else
1130 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1131 DAG.getConstant(4, MVT::i32));
1132 }
1133 return std::make_pair(Result, Chain);
1134}
1135
Evan Cheng339edad2006-01-11 00:33:36 +00001136/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1137/// which corresponds to the condition code.
1138static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1139 switch (X86CC) {
1140 default: assert(0 && "Unknown X86 conditional code!");
1141 case X86ISD::COND_A: return X86::JA;
1142 case X86ISD::COND_AE: return X86::JAE;
1143 case X86ISD::COND_B: return X86::JB;
1144 case X86ISD::COND_BE: return X86::JBE;
1145 case X86ISD::COND_E: return X86::JE;
1146 case X86ISD::COND_G: return X86::JG;
1147 case X86ISD::COND_GE: return X86::JGE;
1148 case X86ISD::COND_L: return X86::JL;
1149 case X86ISD::COND_LE: return X86::JLE;
1150 case X86ISD::COND_NE: return X86::JNE;
1151 case X86ISD::COND_NO: return X86::JNO;
1152 case X86ISD::COND_NP: return X86::JNP;
1153 case X86ISD::COND_NS: return X86::JNS;
1154 case X86ISD::COND_O: return X86::JO;
1155 case X86ISD::COND_P: return X86::JP;
1156 case X86ISD::COND_S: return X86::JS;
1157 }
1158}
Chris Lattner76ac0682005-11-15 00:40:23 +00001159
Evan Cheng45df7f82006-01-30 23:41:35 +00001160/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1161/// specific condition code. It returns a false if it cannot do a direct
1162/// translation. X86CC is the translated CondCode. Flip is set to true if the
1163/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001164static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1165 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001166 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001167 Flip = false;
1168 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001169 if (!isFP) {
1170 switch (SetCCOpcode) {
1171 default: break;
1172 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1173 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1174 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1175 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1176 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1177 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1178 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1179 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1180 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1181 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1182 }
1183 } else {
1184 // On a floating point condition, the flags are set as follows:
1185 // ZF PF CF op
1186 // 0 | 0 | 0 | X > Y
1187 // 0 | 0 | 1 | X < Y
1188 // 1 | 0 | 0 | X == Y
1189 // 1 | 1 | 1 | unordered
1190 switch (SetCCOpcode) {
1191 default: break;
1192 case ISD::SETUEQ:
1193 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001194 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001195 case ISD::SETOGT:
1196 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001197 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001198 case ISD::SETOGE:
1199 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001200 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001201 case ISD::SETULT:
1202 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001203 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001204 case ISD::SETULE:
1205 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1206 case ISD::SETONE:
1207 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1208 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1209 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1210 }
1211 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001212
1213 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001214}
1215
Evan Cheng339edad2006-01-11 00:33:36 +00001216/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1217/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001218/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001219static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001220 switch (X86CC) {
1221 default:
1222 return false;
1223 case X86ISD::COND_B:
1224 case X86ISD::COND_BE:
1225 case X86ISD::COND_E:
1226 case X86ISD::COND_P:
1227 case X86ISD::COND_A:
1228 case X86ISD::COND_AE:
1229 case X86ISD::COND_NE:
1230 case X86ISD::COND_NP:
1231 return true;
1232 }
1233}
1234
Evan Cheng339edad2006-01-11 00:33:36 +00001235MachineBasicBlock *
1236X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1237 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001238 switch (MI->getOpcode()) {
1239 default: assert(false && "Unexpected instr type to insert");
1240 case X86::CMOV_FR32:
1241 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001242 // To "insert" a SELECT_CC instruction, we actually have to insert the
1243 // diamond control-flow pattern. The incoming instruction knows the
1244 // destination vreg to set, the condition code register to branch on, the
1245 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001246 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1247 ilist<MachineBasicBlock>::iterator It = BB;
1248 ++It;
1249
1250 // thisMBB:
1251 // ...
1252 // TrueVal = ...
1253 // cmpTY ccX, r1, r2
1254 // bCC copy1MBB
1255 // fallthrough --> copy0MBB
1256 MachineBasicBlock *thisMBB = BB;
1257 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1258 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1259 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1260 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1261 MachineFunction *F = BB->getParent();
1262 F->getBasicBlockList().insert(It, copy0MBB);
1263 F->getBasicBlockList().insert(It, sinkMBB);
1264 // Update machine-CFG edges
1265 BB->addSuccessor(copy0MBB);
1266 BB->addSuccessor(sinkMBB);
1267
1268 // copy0MBB:
1269 // %FalseValue = ...
1270 // # fallthrough to sinkMBB
1271 BB = copy0MBB;
1272
1273 // Update machine-CFG edges
1274 BB->addSuccessor(sinkMBB);
1275
1276 // sinkMBB:
1277 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1278 // ...
1279 BB = sinkMBB;
1280 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1281 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1282 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001283
Evan Cheng911c68d2006-01-16 21:21:29 +00001284 delete MI; // The pseudo instruction is gone now.
1285 return BB;
1286 }
Evan Cheng339edad2006-01-11 00:33:36 +00001287
Evan Cheng911c68d2006-01-16 21:21:29 +00001288 case X86::FP_TO_INT16_IN_MEM:
1289 case X86::FP_TO_INT32_IN_MEM:
1290 case X86::FP_TO_INT64_IN_MEM: {
1291 // Change the floating point control register to use "round towards zero"
1292 // mode when truncating to an integer value.
1293 MachineFunction *F = BB->getParent();
1294 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1295 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1296
1297 // Load the old value of the high byte of the control word...
1298 unsigned OldCW =
1299 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1300 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1301
1302 // Set the high part to be round to zero...
1303 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1304
1305 // Reload the modified control word now...
1306 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1307
1308 // Restore the memory image of control word to original value
1309 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1310
1311 // Get the X86 opcode to use.
1312 unsigned Opc;
1313 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001314 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001315 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1316 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1317 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1318 }
1319
1320 X86AddressMode AM;
1321 MachineOperand &Op = MI->getOperand(0);
1322 if (Op.isRegister()) {
1323 AM.BaseType = X86AddressMode::RegBase;
1324 AM.Base.Reg = Op.getReg();
1325 } else {
1326 AM.BaseType = X86AddressMode::FrameIndexBase;
1327 AM.Base.FrameIndex = Op.getFrameIndex();
1328 }
1329 Op = MI->getOperand(1);
1330 if (Op.isImmediate())
1331 AM.Scale = Op.getImmedValue();
1332 Op = MI->getOperand(2);
1333 if (Op.isImmediate())
1334 AM.IndexReg = Op.getImmedValue();
1335 Op = MI->getOperand(3);
1336 if (Op.isGlobalAddress()) {
1337 AM.GV = Op.getGlobal();
1338 } else {
1339 AM.Disp = Op.getImmedValue();
1340 }
1341 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1342
1343 // Reload the original control word now.
1344 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1345
1346 delete MI; // The pseudo instruction is gone now.
1347 return BB;
1348 }
1349 }
Evan Cheng339edad2006-01-11 00:33:36 +00001350}
1351
1352
1353//===----------------------------------------------------------------------===//
1354// X86 Custom Lowering Hooks
1355//===----------------------------------------------------------------------===//
1356
Evan Chengaf598d22006-03-13 23:18:16 +00001357/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1358/// load. For Darwin, external and weak symbols are indirect, loading the value
1359/// at address GV rather then the value of GV itself. This means that the
1360/// GlobalAddress must be in the base or index register of the address, not the
1361/// GV offset field.
1362static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1363 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1364 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1365}
1366
Chris Lattner76ac0682005-11-15 00:40:23 +00001367/// LowerOperation - Provide custom lowering hooks for some operations.
1368///
1369SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1370 switch (Op.getOpcode()) {
1371 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001372 case ISD::SHL_PARTS:
1373 case ISD::SRA_PARTS:
1374 case ISD::SRL_PARTS: {
1375 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1376 "Not an i64 shift!");
1377 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1378 SDOperand ShOpLo = Op.getOperand(0);
1379 SDOperand ShOpHi = Op.getOperand(1);
1380 SDOperand ShAmt = Op.getOperand(2);
1381 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001382 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001383 : DAG.getConstant(0, MVT::i32);
1384
1385 SDOperand Tmp2, Tmp3;
1386 if (Op.getOpcode() == ISD::SHL_PARTS) {
1387 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1388 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1389 } else {
1390 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001391 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001392 }
1393
1394 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1395 ShAmt, DAG.getConstant(32, MVT::i8));
1396
1397 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001398 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001399
1400 std::vector<MVT::ValueType> Tys;
1401 Tys.push_back(MVT::i32);
1402 Tys.push_back(MVT::Flag);
1403 std::vector<SDOperand> Ops;
1404 if (Op.getOpcode() == ISD::SHL_PARTS) {
1405 Ops.push_back(Tmp2);
1406 Ops.push_back(Tmp3);
1407 Ops.push_back(CC);
1408 Ops.push_back(InFlag);
1409 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1410 InFlag = Hi.getValue(1);
1411
1412 Ops.clear();
1413 Ops.push_back(Tmp3);
1414 Ops.push_back(Tmp1);
1415 Ops.push_back(CC);
1416 Ops.push_back(InFlag);
1417 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1418 } else {
1419 Ops.push_back(Tmp2);
1420 Ops.push_back(Tmp3);
1421 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001422 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001423 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1424 InFlag = Lo.getValue(1);
1425
1426 Ops.clear();
1427 Ops.push_back(Tmp3);
1428 Ops.push_back(Tmp1);
1429 Ops.push_back(CC);
1430 Ops.push_back(InFlag);
1431 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1432 }
1433
1434 Tys.clear();
1435 Tys.push_back(MVT::i32);
1436 Tys.push_back(MVT::i32);
1437 Ops.clear();
1438 Ops.push_back(Lo);
1439 Ops.push_back(Hi);
1440 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1441 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001442 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001443 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001444 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001445 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001446
1447 SDOperand Result;
1448 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1449 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001450 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001451 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001452 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001453 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1454 DAG.getEntryNode(), Op.getOperand(0),
1455 StackSlot, DAG.getSrcValue(NULL));
1456
1457 // Build the FILD
1458 std::vector<MVT::ValueType> Tys;
1459 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001460 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001461 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001462 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001463 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001464 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001465 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001466 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1467 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001468
1469 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001470 Chain = Result.getValue(1);
1471 SDOperand InFlag = Result.getValue(2);
1472
Evan Cheng11613a52006-02-04 02:20:30 +00001473 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001474 // shouldn't be necessary except that RFP cannot be live across
1475 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1476 MachineFunction &MF = DAG.getMachineFunction();
1477 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1478 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1479 std::vector<MVT::ValueType> Tys;
1480 Tys.push_back(MVT::Other);
1481 std::vector<SDOperand> Ops;
1482 Ops.push_back(Chain);
1483 Ops.push_back(Result);
1484 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001485 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001486 Ops.push_back(InFlag);
1487 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1488 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1489 DAG.getSrcValue(NULL));
1490 }
1491
Evan Cheng6305e502006-01-12 22:54:21 +00001492 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001493 }
1494 case ISD::FP_TO_SINT: {
1495 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001496 "Unknown FP_TO_SINT to lower!");
1497 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1498 // stack slot.
1499 MachineFunction &MF = DAG.getMachineFunction();
1500 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1501 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1502 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1503
1504 unsigned Opc;
1505 switch (Op.getValueType()) {
1506 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1507 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1508 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1509 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1510 }
1511
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001512 SDOperand Chain = DAG.getEntryNode();
1513 SDOperand Value = Op.getOperand(0);
1514 if (X86ScalarSSE) {
1515 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1516 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1517 DAG.getSrcValue(0));
1518 std::vector<MVT::ValueType> Tys;
1519 Tys.push_back(MVT::f64);
1520 Tys.push_back(MVT::Other);
1521 std::vector<SDOperand> Ops;
1522 Ops.push_back(Chain);
1523 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001524 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001525 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1526 Chain = Value.getValue(1);
1527 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1528 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1529 }
1530
Chris Lattner76ac0682005-11-15 00:40:23 +00001531 // Build the FP_TO_INT*_IN_MEM
1532 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001533 Ops.push_back(Chain);
1534 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001535 Ops.push_back(StackSlot);
1536 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1537
1538 // Load the result.
1539 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1540 DAG.getSrcValue(NULL));
1541 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001542 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001543 std::vector<MVT::ValueType> Tys;
1544 Tys.push_back(MVT::Other);
1545 Tys.push_back(MVT::Flag);
1546 std::vector<SDOperand> Ops;
1547 Ops.push_back(Op.getOperand(0));
1548 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001549 Ops.clear();
1550 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1551 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1552 MVT::i32, Ops[0].getValue(2)));
1553 Ops.push_back(Ops[1].getValue(1));
1554 Tys[0] = Tys[1] = MVT::i32;
1555 Tys.push_back(MVT::Other);
1556 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001557 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001558 case ISD::FABS: {
1559 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00001560 const Type *OpNTy = MVT::getTypeForValueType(VT);
1561 std::vector<Constant*> CV;
1562 if (VT == MVT::f64) {
1563 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1564 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1565 } else {
1566 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1567 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1568 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1569 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1570 }
1571 Constant *CS = ConstantStruct::get(CV);
1572 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1573 SDOperand Mask
1574 = DAG.getNode(X86ISD::LOAD_PACK,
1575 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00001576 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1577 }
Evan Cheng72d5c252006-01-31 22:28:30 +00001578 case ISD::FNEG: {
1579 MVT::ValueType VT = Op.getValueType();
1580 const Type *OpNTy = MVT::getTypeForValueType(VT);
1581 std::vector<Constant*> CV;
1582 if (VT == MVT::f64) {
1583 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1584 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1585 } else {
1586 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1587 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1588 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1589 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1590 }
1591 Constant *CS = ConstantStruct::get(CV);
1592 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1593 SDOperand Mask
1594 = DAG.getNode(X86ISD::LOAD_PACK,
1595 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1596 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1597 }
Evan Chengc1583db2005-12-21 20:21:51 +00001598 case ISD::SETCC: {
1599 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001600 SDOperand Cond;
1601 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001602 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1603 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001604 bool Flip;
1605 unsigned X86CC;
1606 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1607 if (Flip)
1608 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1609 Op.getOperand(1), Op.getOperand(0));
1610 else
1611 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1612 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001613 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1614 DAG.getConstant(X86CC, MVT::i8), Cond);
1615 } else {
1616 assert(isFP && "Illegal integer SetCC!");
1617
Evan Cheng45df7f82006-01-30 23:41:35 +00001618 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1619 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001620 std::vector<MVT::ValueType> Tys;
1621 std::vector<SDOperand> Ops;
1622 switch (SetCCOpcode) {
1623 default: assert(false && "Illegal floating point SetCC!");
1624 case ISD::SETOEQ: { // !PF & ZF
1625 Tys.push_back(MVT::i8);
1626 Tys.push_back(MVT::Flag);
1627 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1628 Ops.push_back(Cond);
1629 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1630 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1631 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1632 Tmp1.getValue(1));
1633 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1634 }
Evan Cheng172fce72006-01-06 00:43:03 +00001635 case ISD::SETUNE: { // PF | !ZF
1636 Tys.push_back(MVT::i8);
1637 Tys.push_back(MVT::Flag);
1638 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1639 Ops.push_back(Cond);
1640 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1641 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1642 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1643 Tmp1.getValue(1));
1644 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1645 }
1646 }
1647 }
Evan Chengc1583db2005-12-21 20:21:51 +00001648 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001649 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001650 MVT::ValueType VT = Op.getValueType();
1651 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001652 bool isFPStack = isFP && !X86ScalarSSE;
1653 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001654 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001655 SDOperand Op0 = Op.getOperand(0);
1656 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001657 if (Op0.getOpcode() == ISD::SETCC)
1658 Op0 = LowerOperation(Op0, DAG);
1659
Evan Cheng73a1ad92006-01-10 20:26:56 +00001660 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001661 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1662 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1663 // have another use it will be eliminated.
1664 // If the X86ISD::SETCC has more than one use, then it's probably better
1665 // to use a test instead of duplicating the X86ISD::CMP (for register
1666 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001667 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1668 if (!Op0.hasOneUse()) {
1669 std::vector<MVT::ValueType> Tys;
1670 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1671 Tys.push_back(Op0.Val->getValueType(i));
1672 std::vector<SDOperand> Ops;
1673 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1674 Ops.push_back(Op0.getOperand(i));
1675 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1676 }
1677
Evan Chengfb22e862006-01-13 01:03:02 +00001678 CC = Op0.getOperand(0);
1679 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001680 // Make a copy as flag result cannot be used by more than one.
1681 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1682 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001683 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001684 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001685 } else
1686 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001687 } else
1688 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001689
Evan Cheng731423f2006-01-13 01:06:49 +00001690 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001691 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001692 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001693 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001694
1695 std::vector<MVT::ValueType> Tys;
1696 Tys.push_back(Op.getValueType());
1697 Tys.push_back(MVT::Flag);
1698 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001699 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1700 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001701 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001702 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001703 Ops.push_back(CC);
1704 Ops.push_back(Cond);
1705 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001706 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001707 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001708 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001709 SDOperand Cond = Op.getOperand(1);
1710 SDOperand Dest = Op.getOperand(2);
1711 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001712 if (Cond.getOpcode() == ISD::SETCC)
1713 Cond = LowerOperation(Cond, DAG);
1714
Evan Chengc1583db2005-12-21 20:21:51 +00001715 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001716 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1717 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1718 // have another use it will be eliminated.
1719 // If the X86ISD::SETCC has more than one use, then it's probably better
1720 // to use a test instead of duplicating the X86ISD::CMP (for register
1721 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001722 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1723 if (!Cond.hasOneUse()) {
1724 std::vector<MVT::ValueType> Tys;
1725 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1726 Tys.push_back(Cond.Val->getValueType(i));
1727 std::vector<SDOperand> Ops;
1728 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1729 Ops.push_back(Cond.getOperand(i));
1730 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1731 }
1732
Evan Chengfb22e862006-01-13 01:03:02 +00001733 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001734 Cond = Cond.getOperand(1);
1735 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001736 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001737 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001738 } else
1739 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001740 } else
1741 addTest = true;
1742
1743 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001744 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001745 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1746 }
1747 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1748 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1749 }
Evan Chengae986f12006-01-11 22:15:48 +00001750 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00001751 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001752 SDOperand Chain = Op.getOperand(0);
1753 unsigned Align =
1754 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1755 if (Align == 0) Align = 1;
1756
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001757 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1758 // If not DWORD aligned, call memset if size is less than the threshold.
1759 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001760 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00001761 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001762 MVT::ValueType IntPtr = getPointerTy();
1763 const Type *IntPtrTy = getTargetData().getIntPtrType();
1764 std::vector<std::pair<SDOperand, const Type*> > Args;
1765 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1766 // Extend the ubyte argument to be an int value for the call.
1767 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1768 Args.push_back(std::make_pair(Val, IntPtrTy));
1769 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1770 std::pair<SDOperand,SDOperand> CallResult =
1771 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1772 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1773 return CallResult.second;
1774 }
1775
Evan Chengae986f12006-01-11 22:15:48 +00001776 MVT::ValueType AVT;
1777 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001778 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1779 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00001780 bool TwoRepStos = false;
Evan Cheng6dc73292006-03-04 02:48:56 +00001781 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00001782 unsigned ValReg;
1783 unsigned Val = ValC->getValue() & 255;
1784
1785 // If the value is a constant, then we can potentially use larger sets.
1786 switch (Align & 3) {
1787 case 2: // WORD aligned
1788 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001789 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1790 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001791 Val = (Val << 8) | Val;
1792 ValReg = X86::AX;
1793 break;
1794 case 0: // DWORD aligned
1795 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00001796 if (I) {
1797 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1798 BytesLeft = I->getValue() % 4;
1799 } else {
1800 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1801 DAG.getConstant(2, MVT::i8));
1802 TwoRepStos = true;
1803 }
Evan Chengae986f12006-01-11 22:15:48 +00001804 Val = (Val << 8) | Val;
1805 Val = (Val << 16) | Val;
1806 ValReg = X86::EAX;
1807 break;
1808 default: // Byte aligned
1809 AVT = MVT::i8;
1810 Count = Op.getOperand(3);
1811 ValReg = X86::AL;
1812 break;
1813 }
1814
1815 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1816 InFlag);
1817 InFlag = Chain.getValue(1);
1818 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001819 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00001820 Count = Op.getOperand(3);
1821 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1822 InFlag = Chain.getValue(1);
1823 }
1824
1825 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1826 InFlag = Chain.getValue(1);
1827 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1828 InFlag = Chain.getValue(1);
1829
Evan Chengadc70932006-03-07 23:29:39 +00001830 std::vector<MVT::ValueType> Tys;
1831 Tys.push_back(MVT::Other);
1832 Tys.push_back(MVT::Flag);
1833 std::vector<SDOperand> Ops;
1834 Ops.push_back(Chain);
1835 Ops.push_back(DAG.getValueType(AVT));
1836 Ops.push_back(InFlag);
1837 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1838
1839 if (TwoRepStos) {
1840 InFlag = Chain.getValue(1);
1841 Count = Op.getOperand(3);
1842 MVT::ValueType CVT = Count.getValueType();
1843 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1844 DAG.getConstant(3, CVT));
1845 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1846 InFlag = Chain.getValue(1);
1847 Tys.clear();
1848 Tys.push_back(MVT::Other);
1849 Tys.push_back(MVT::Flag);
1850 Ops.clear();
1851 Ops.push_back(Chain);
1852 Ops.push_back(DAG.getValueType(MVT::i8));
1853 Ops.push_back(InFlag);
1854 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1855 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00001856 // Issue stores for the last 1 - 3 bytes.
1857 SDOperand Value;
1858 unsigned Val = ValC->getValue() & 255;
1859 unsigned Offset = I->getValue() - BytesLeft;
1860 SDOperand DstAddr = Op.getOperand(1);
1861 MVT::ValueType AddrVT = DstAddr.getValueType();
1862 if (BytesLeft >= 2) {
1863 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1864 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1865 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1866 DAG.getConstant(Offset, AddrVT)),
1867 DAG.getSrcValue(NULL));
1868 BytesLeft -= 2;
1869 Offset += 2;
1870 }
1871
1872 if (BytesLeft == 1) {
1873 Value = DAG.getConstant(Val, MVT::i8);
1874 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1875 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1876 DAG.getConstant(Offset, AddrVT)),
1877 DAG.getSrcValue(NULL));
1878 }
1879 }
1880
1881 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00001882 }
1883 case ISD::MEMCPY: {
1884 SDOperand Chain = Op.getOperand(0);
1885 unsigned Align =
1886 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1887 if (Align == 0) Align = 1;
1888
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001889 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1890 // If not DWORD aligned, call memcpy if size is less than the threshold.
1891 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001892 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00001893 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001894 MVT::ValueType IntPtr = getPointerTy();
1895 const Type *IntPtrTy = getTargetData().getIntPtrType();
1896 std::vector<std::pair<SDOperand, const Type*> > Args;
1897 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1898 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1899 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1900 std::pair<SDOperand,SDOperand> CallResult =
1901 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1902 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1903 return CallResult.second;
1904 }
1905
Evan Chengae986f12006-01-11 22:15:48 +00001906 MVT::ValueType AVT;
1907 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001908 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00001909 bool TwoRepMovs = false;
Evan Chengae986f12006-01-11 22:15:48 +00001910 switch (Align & 3) {
1911 case 2: // WORD aligned
1912 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001913 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1914 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001915 break;
1916 case 0: // DWORD aligned
1917 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00001918 if (I) {
1919 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1920 BytesLeft = I->getValue() % 4;
1921 } else {
1922 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1923 DAG.getConstant(2, MVT::i8));
1924 TwoRepMovs = true;
1925 }
Evan Chengae986f12006-01-11 22:15:48 +00001926 break;
1927 default: // Byte aligned
1928 AVT = MVT::i8;
1929 Count = Op.getOperand(3);
1930 break;
1931 }
1932
Evan Cheng6dc73292006-03-04 02:48:56 +00001933 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001934 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1935 InFlag = Chain.getValue(1);
1936 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1937 InFlag = Chain.getValue(1);
1938 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1939 InFlag = Chain.getValue(1);
1940
Evan Chengadc70932006-03-07 23:29:39 +00001941 std::vector<MVT::ValueType> Tys;
1942 Tys.push_back(MVT::Other);
1943 Tys.push_back(MVT::Flag);
1944 std::vector<SDOperand> Ops;
1945 Ops.push_back(Chain);
1946 Ops.push_back(DAG.getValueType(AVT));
1947 Ops.push_back(InFlag);
1948 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1949
1950 if (TwoRepMovs) {
1951 InFlag = Chain.getValue(1);
1952 Count = Op.getOperand(3);
1953 MVT::ValueType CVT = Count.getValueType();
1954 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1955 DAG.getConstant(3, CVT));
1956 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1957 InFlag = Chain.getValue(1);
1958 Tys.clear();
1959 Tys.push_back(MVT::Other);
1960 Tys.push_back(MVT::Flag);
1961 Ops.clear();
1962 Ops.push_back(Chain);
1963 Ops.push_back(DAG.getValueType(MVT::i8));
1964 Ops.push_back(InFlag);
1965 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1966 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00001967 // Issue loads and stores for the last 1 - 3 bytes.
1968 unsigned Offset = I->getValue() - BytesLeft;
1969 SDOperand DstAddr = Op.getOperand(1);
1970 MVT::ValueType DstVT = DstAddr.getValueType();
1971 SDOperand SrcAddr = Op.getOperand(2);
1972 MVT::ValueType SrcVT = SrcAddr.getValueType();
1973 SDOperand Value;
1974 if (BytesLeft >= 2) {
1975 Value = DAG.getLoad(MVT::i16, Chain,
1976 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1977 DAG.getConstant(Offset, SrcVT)),
1978 DAG.getSrcValue(NULL));
1979 Chain = Value.getValue(1);
1980 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1981 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1982 DAG.getConstant(Offset, DstVT)),
1983 DAG.getSrcValue(NULL));
1984 BytesLeft -= 2;
1985 Offset += 2;
1986 }
1987
1988 if (BytesLeft == 1) {
1989 Value = DAG.getLoad(MVT::i8, Chain,
1990 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1991 DAG.getConstant(Offset, SrcVT)),
1992 DAG.getSrcValue(NULL));
1993 Chain = Value.getValue(1);
1994 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1995 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1996 DAG.getConstant(Offset, DstVT)),
1997 DAG.getSrcValue(NULL));
1998 }
1999 }
2000
2001 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002002 }
Evan Cheng99470012006-02-25 09:55:19 +00002003
2004 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2005 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2006 // one of the above mentioned nodes. It has to be wrapped because otherwise
2007 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2008 // be used to form addressing mode. These wrapped nodes will be selected
2009 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00002010 case ISD::ConstantPool: {
2011 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002012 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2013 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2014 CP->getAlignment()));
Evan Cheng1f342c22006-02-23 02:43:52 +00002015 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002016 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002017 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00002018 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2019 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2020 }
2021
2022 return Result;
2023 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002024 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002025 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2026 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2027 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga74ce622005-12-21 02:39:21 +00002028 if (getTargetMachine().
Evan Cheng5588de92006-02-18 00:15:05 +00002029 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002030 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002031 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00002032 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2033 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00002034
2035 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Chengaf598d22006-03-13 23:18:16 +00002036 // the value at address GV, not the value of GV itself. This means that
Evan Cheng5588de92006-02-18 00:15:05 +00002037 // the GlobalAddress must be in the base or index register of the address,
2038 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00002039 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Chengaf598d22006-03-13 23:18:16 +00002040 DarwinGVRequiresExtraLoad(GV))
Evan Cheng5a766802006-02-07 08:38:37 +00002041 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00002042 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00002043 }
Evan Cheng5588de92006-02-18 00:15:05 +00002044
Evan Chengb94db9e2006-01-12 07:56:47 +00002045 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002046 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002047 case ISD::ExternalSymbol: {
2048 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2049 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2050 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2051 if (getTargetMachine().
2052 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2053 // With PIC, the address is actually $g + Offset.
2054 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2055 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2056 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2057 }
2058
2059 return Result;
2060 }
Nate Begemane74795c2006-01-25 18:21:52 +00002061 case ISD::VASTART: {
2062 // vastart just stores the address of the VarArgsFrameIndex slot into the
2063 // memory location argument.
2064 // FIXME: Replace MVT::i32 with PointerTy
2065 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2066 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2067 Op.getOperand(1), Op.getOperand(2));
2068 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002069 case ISD::RET: {
2070 SDOperand Copy;
2071
2072 switch(Op.getNumOperands()) {
2073 default:
2074 assert(0 && "Do not know how to return this many arguments!");
2075 abort();
2076 case 1:
2077 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2078 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2079 case 2: {
2080 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2081 if (MVT::isInteger(ArgVT))
2082 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2083 SDOperand());
2084 else if (!X86ScalarSSE) {
2085 std::vector<MVT::ValueType> Tys;
2086 Tys.push_back(MVT::Other);
2087 Tys.push_back(MVT::Flag);
2088 std::vector<SDOperand> Ops;
2089 Ops.push_back(Op.getOperand(0));
2090 Ops.push_back(Op.getOperand(1));
2091 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2092 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00002093 SDOperand MemLoc;
2094 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00002095 SDOperand Value = Op.getOperand(1);
2096
Evan Chenga24617f2006-02-01 01:19:32 +00002097 if (Value.getOpcode() == ISD::LOAD &&
2098 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00002099 Chain = Value.getOperand(0);
2100 MemLoc = Value.getOperand(1);
2101 } else {
2102 // Spill the value to memory and reload it into top of stack.
2103 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2104 MachineFunction &MF = DAG.getMachineFunction();
2105 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2106 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2107 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2108 Value, MemLoc, DAG.getSrcValue(0));
2109 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002110 std::vector<MVT::ValueType> Tys;
2111 Tys.push_back(MVT::f64);
2112 Tys.push_back(MVT::Other);
2113 std::vector<SDOperand> Ops;
2114 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002115 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002116 Ops.push_back(DAG.getValueType(ArgVT));
2117 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2118 Tys.clear();
2119 Tys.push_back(MVT::Other);
2120 Tys.push_back(MVT::Flag);
2121 Ops.clear();
2122 Ops.push_back(Copy.getValue(1));
2123 Ops.push_back(Copy);
2124 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2125 }
2126 break;
2127 }
2128 case 3:
2129 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2130 SDOperand());
2131 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2132 break;
2133 }
2134 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2135 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2136 Copy.getValue(1));
2137 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002138 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002139}
Evan Cheng6af02632005-12-20 06:22:03 +00002140
2141const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2142 switch (Opcode) {
2143 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00002144 case X86ISD::SHLD: return "X86ISD::SHLD";
2145 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00002146 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00002147 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00002148 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00002149 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00002150 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2151 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2152 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00002153 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00002154 case X86ISD::FST: return "X86ISD::FST";
2155 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00002156 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00002157 case X86ISD::CALL: return "X86ISD::CALL";
2158 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2159 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2160 case X86ISD::CMP: return "X86ISD::CMP";
2161 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00002162 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00002163 case X86ISD::CMOV: return "X86ISD::CMOV";
2164 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00002165 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00002166 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2167 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00002168 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00002169 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002170 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng6af02632005-12-20 06:22:03 +00002171 }
2172}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002173
Nate Begeman8a77efe2006-02-16 21:11:51 +00002174void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2175 uint64_t Mask,
2176 uint64_t &KnownZero,
2177 uint64_t &KnownOne,
2178 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002179
2180 unsigned Opc = Op.getOpcode();
Nate Begeman8a77efe2006-02-16 21:11:51 +00002181 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002182
2183 switch (Opc) {
2184 default:
2185 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2186 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00002187 case X86ISD::SETCC:
2188 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2189 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002190 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002191}
Chris Lattnerc642aa52006-01-31 19:43:35 +00002192
2193std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00002194getRegClassForInlineAsmConstraint(const std::string &Constraint,
2195 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00002196 if (Constraint.size() == 1) {
2197 // FIXME: not handling fp-stack yet!
2198 // FIXME: not handling MMX registers yet ('y' constraint).
2199 switch (Constraint[0]) { // GCC X86 Constraint Letters
2200 default: break; // Unknown constriant letter
2201 case 'r': // GENERAL_REGS
2202 case 'R': // LEGACY_REGS
2203 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2204 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2205 case 'l': // INDEX_REGS
2206 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2207 X86::ESI, X86::EDI, X86::EBP, 0);
2208 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2209 case 'Q': // Q_REGS
2210 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2211 case 'x': // SSE_REGS if SSE1 allowed
2212 if (Subtarget->hasSSE1())
2213 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2214 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2215 0);
2216 return std::vector<unsigned>();
2217 case 'Y': // SSE_REGS if SSE2 allowed
2218 if (Subtarget->hasSSE2())
2219 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2220 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2221 0);
2222 return std::vector<unsigned>();
2223 }
2224 }
2225
Chris Lattner7ad77df2006-02-22 00:56:39 +00002226 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00002227}
Evan Chengaf598d22006-03-13 23:18:16 +00002228
2229/// isLegalAddressImmediate - Return true if the integer value or
2230/// GlobalValue can be used as the offset of the target addressing mode.
2231bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2232 // X86 allows a sign-extended 32-bit immediate field.
2233 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2234}
2235
2236bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2237 if (getTargetMachine().
2238 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2239 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2240 if (RModel == Reloc::Static)
2241 return true;
2242 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00002243 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00002244 else
2245 return false;
2246 } else
2247 return true;
2248}