blob: fafaf00ed1c68c19ef104a2d302dd0d94879849c [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
Evan Chengd5e905d2006-03-21 23:01:21 +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 Chengd5e905d2006-03-21 23:01:21 +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);
280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
Evan Chengd097e672006-03-22 02:53:00 +0000281 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000282 }
283
284 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
285 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
290
291
Evan Chengd5e905d2006-03-21 23:01:21 +0000292 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
293 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
294 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
295 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
298 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
299 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
300 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Expand);
301 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
302 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000303 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000304 }
305
Chris Lattner76ac0682005-11-15 00:40:23 +0000306 computeRegisterProperties();
307
Evan Cheng6a374562006-02-14 08:25:08 +0000308 // FIXME: These should be based on subtarget info. Plus, the values should
309 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000310 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
311 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
312 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000313 allowUnalignedMemoryAccesses = true; // x86 supports it!
314}
315
316std::vector<SDOperand>
317X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
318 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
319 return LowerFastCCArguments(F, DAG);
320 return LowerCCCArguments(F, DAG);
321}
322
323std::pair<SDOperand, SDOperand>
324X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
325 bool isVarArg, unsigned CallingConv,
326 bool isTailCall,
327 SDOperand Callee, ArgListTy &Args,
328 SelectionDAG &DAG) {
329 assert((!isVarArg || CallingConv == CallingConv::C) &&
330 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000331
332 // If the callee is a GlobalAddress node (quite common, every direct call is)
333 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
334 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
335 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000336 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
337 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000338
Chris Lattner76ac0682005-11-15 00:40:23 +0000339 if (CallingConv == CallingConv::Fast && EnableFastCC)
340 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
341 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
342}
343
344//===----------------------------------------------------------------------===//
345// C Calling Convention implementation
346//===----------------------------------------------------------------------===//
347
348std::vector<SDOperand>
349X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
350 std::vector<SDOperand> ArgValues;
351
352 MachineFunction &MF = DAG.getMachineFunction();
353 MachineFrameInfo *MFI = MF.getFrameInfo();
354
355 // Add DAG nodes to load the arguments... On entry to a function on the X86,
356 // the stack frame looks like this:
357 //
358 // [ESP] -- return address
359 // [ESP + 4] -- first argument (leftmost lexically)
360 // [ESP + 8] -- second argument, if first argument is four bytes in size
361 // ...
362 //
363 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
364 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
365 MVT::ValueType ObjectVT = getValueType(I->getType());
366 unsigned ArgIncrement = 4;
367 unsigned ObjSize;
368 switch (ObjectVT) {
369 default: assert(0 && "Unhandled argument type!");
370 case MVT::i1:
371 case MVT::i8: ObjSize = 1; break;
372 case MVT::i16: ObjSize = 2; break;
373 case MVT::i32: ObjSize = 4; break;
374 case MVT::i64: ObjSize = ArgIncrement = 8; break;
375 case MVT::f32: ObjSize = 4; break;
376 case MVT::f64: ObjSize = ArgIncrement = 8; break;
377 }
378 // Create the frame index object for this incoming parameter...
379 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
380
381 // Create the SelectionDAG nodes corresponding to a load from this parameter
382 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
383
384 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
385 // dead loads.
386 SDOperand ArgValue;
387 if (!I->use_empty())
388 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
389 DAG.getSrcValue(NULL));
390 else {
391 if (MVT::isInteger(ObjectVT))
392 ArgValue = DAG.getConstant(0, ObjectVT);
393 else
394 ArgValue = DAG.getConstantFP(0, ObjectVT);
395 }
396 ArgValues.push_back(ArgValue);
397
398 ArgOffset += ArgIncrement; // Move on to the next argument...
399 }
400
401 // If the function takes variable number of arguments, make a frame index for
402 // the start of the first vararg value... for expansion of llvm.va_start.
403 if (F.isVarArg())
404 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
405 ReturnAddrIndex = 0; // No return address slot generated yet.
406 BytesToPopOnReturn = 0; // Callee pops nothing.
407 BytesCallerReserves = ArgOffset;
408
409 // Finally, inform the code generator which regs we return values in.
410 switch (getValueType(F.getReturnType())) {
411 default: assert(0 && "Unknown type!");
412 case MVT::isVoid: break;
413 case MVT::i1:
414 case MVT::i8:
415 case MVT::i16:
416 case MVT::i32:
417 MF.addLiveOut(X86::EAX);
418 break;
419 case MVT::i64:
420 MF.addLiveOut(X86::EAX);
421 MF.addLiveOut(X86::EDX);
422 break;
423 case MVT::f32:
424 case MVT::f64:
425 MF.addLiveOut(X86::ST0);
426 break;
427 }
428 return ArgValues;
429}
430
431std::pair<SDOperand, SDOperand>
432X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
433 bool isVarArg, bool isTailCall,
434 SDOperand Callee, ArgListTy &Args,
435 SelectionDAG &DAG) {
436 // Count how many bytes are to be pushed on the stack.
437 unsigned NumBytes = 0;
438
439 if (Args.empty()) {
440 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000441 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000442 } else {
443 for (unsigned i = 0, e = Args.size(); i != e; ++i)
444 switch (getValueType(Args[i].second)) {
445 default: assert(0 && "Unknown value type!");
446 case MVT::i1:
447 case MVT::i8:
448 case MVT::i16:
449 case MVT::i32:
450 case MVT::f32:
451 NumBytes += 4;
452 break;
453 case MVT::i64:
454 case MVT::f64:
455 NumBytes += 8;
456 break;
457 }
458
Chris Lattner62c34842006-02-13 09:00:43 +0000459 Chain = DAG.getCALLSEQ_START(Chain,
460 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000461
462 // Arguments go on the stack in reverse order, as specified by the ABI.
463 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000464 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000465 std::vector<SDOperand> Stores;
466
467 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
468 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
469 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
470
471 switch (getValueType(Args[i].second)) {
472 default: assert(0 && "Unexpected ValueType for argument!");
473 case MVT::i1:
474 case MVT::i8:
475 case MVT::i16:
476 // Promote the integer to 32 bits. If the input type is signed use a
477 // sign extend, otherwise use a zero extend.
478 if (Args[i].second->isSigned())
479 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
480 else
481 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
482
483 // FALL THROUGH
484 case MVT::i32:
485 case MVT::f32:
486 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
487 Args[i].first, PtrOff,
488 DAG.getSrcValue(NULL)));
489 ArgOffset += 4;
490 break;
491 case MVT::i64:
492 case MVT::f64:
493 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
494 Args[i].first, PtrOff,
495 DAG.getSrcValue(NULL)));
496 ArgOffset += 8;
497 break;
498 }
499 }
500 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
501 }
502
503 std::vector<MVT::ValueType> RetVals;
504 MVT::ValueType RetTyVT = getValueType(RetTy);
505 RetVals.push_back(MVT::Other);
506
507 // The result values produced have to be legal. Promote the result.
508 switch (RetTyVT) {
509 case MVT::isVoid: break;
510 default:
511 RetVals.push_back(RetTyVT);
512 break;
513 case MVT::i1:
514 case MVT::i8:
515 case MVT::i16:
516 RetVals.push_back(MVT::i32);
517 break;
518 case MVT::f32:
519 if (X86ScalarSSE)
520 RetVals.push_back(MVT::f32);
521 else
522 RetVals.push_back(MVT::f64);
523 break;
524 case MVT::i64:
525 RetVals.push_back(MVT::i32);
526 RetVals.push_back(MVT::i32);
527 break;
528 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000529
Nate Begeman7e5496d2006-02-17 00:03:04 +0000530 std::vector<MVT::ValueType> NodeTys;
531 NodeTys.push_back(MVT::Other); // Returns a chain
532 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
533 std::vector<SDOperand> Ops;
534 Ops.push_back(Chain);
535 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000536
Nate Begeman7e5496d2006-02-17 00:03:04 +0000537 // FIXME: Do not generate X86ISD::TAILCALL for now.
538 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
539 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000540
Nate Begeman7e5496d2006-02-17 00:03:04 +0000541 NodeTys.clear();
542 NodeTys.push_back(MVT::Other); // Returns a chain
543 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
544 Ops.clear();
545 Ops.push_back(Chain);
546 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
547 Ops.push_back(DAG.getConstant(0, getPointerTy()));
548 Ops.push_back(InFlag);
549 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
550 InFlag = Chain.getValue(1);
551
552 SDOperand RetVal;
553 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000554 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000555 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000556 case MVT::i1:
557 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000558 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
559 Chain = RetVal.getValue(1);
560 if (RetTyVT == MVT::i1)
561 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
562 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000563 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000564 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, 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::i32:
568 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
569 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000570 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000571 case MVT::i64: {
572 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
573 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
574 Lo.getValue(2));
575 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
576 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000577 break;
578 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000579 case MVT::f32:
580 case MVT::f64: {
581 std::vector<MVT::ValueType> Tys;
582 Tys.push_back(MVT::f64);
583 Tys.push_back(MVT::Other);
584 Tys.push_back(MVT::Flag);
585 std::vector<SDOperand> Ops;
586 Ops.push_back(Chain);
587 Ops.push_back(InFlag);
588 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
589 Chain = RetVal.getValue(1);
590 InFlag = RetVal.getValue(2);
591 if (X86ScalarSSE) {
592 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
593 // shouldn't be necessary except that RFP cannot be live across
594 // multiple blocks. When stackifier is fixed, they can be uncoupled.
595 MachineFunction &MF = DAG.getMachineFunction();
596 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
597 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
598 Tys.clear();
599 Tys.push_back(MVT::Other);
600 Ops.clear();
601 Ops.push_back(Chain);
602 Ops.push_back(RetVal);
603 Ops.push_back(StackSlot);
604 Ops.push_back(DAG.getValueType(RetTyVT));
605 Ops.push_back(InFlag);
606 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
607 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
608 DAG.getSrcValue(NULL));
609 Chain = RetVal.getValue(1);
610 }
Evan Cheng45e190982006-01-05 00:27:02 +0000611
Nate Begeman7e5496d2006-02-17 00:03:04 +0000612 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
613 // FIXME: we would really like to remember that this FP_ROUND
614 // operation is okay to eliminate if we allow excess FP precision.
615 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
616 break;
617 }
618 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000619 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000620
621 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000622}
623
Chris Lattner76ac0682005-11-15 00:40:23 +0000624//===----------------------------------------------------------------------===//
625// Fast Calling Convention implementation
626//===----------------------------------------------------------------------===//
627//
628// The X86 'fast' calling convention passes up to two integer arguments in
629// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
630// and requires that the callee pop its arguments off the stack (allowing proper
631// tail calls), and has the same return value conventions as C calling convs.
632//
633// This calling convention always arranges for the callee pop value to be 8n+4
634// bytes, which is needed for tail recursion elimination and stack alignment
635// reasons.
636//
637// Note that this can be enhanced in the future to pass fp vals in registers
638// (when we have a global fp allocator) and do other tricks.
639//
640
641/// AddLiveIn - This helper function adds the specified physical register to the
642/// MachineFunction as a live in value. It also creates a corresponding virtual
643/// register for it.
644static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
645 TargetRegisterClass *RC) {
646 assert(RC->contains(PReg) && "Not the correct regclass!");
647 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
648 MF.addLiveIn(PReg, VReg);
649 return VReg;
650}
651
Chris Lattner388fc4d2006-03-17 17:27:47 +0000652// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
653// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
654// EDX". Anything more is illegal.
655//
656// FIXME: The linscan register allocator currently has problem with
657// coallescing. At the time of this writing, whenever it decides to coallesce
658// a physreg with a virtreg, this increases the size of the physreg's live
659// range, and the live range cannot ever be reduced. This causes problems if
660// too many physregs are coalleced with virtregs, which can cause the register
661// allocator to wedge itself.
662//
663// This code triggers this problem more often if we pass args in registers,
664// so disable it until this is fixed.
665//
666// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
667// about code being dead.
668//
669static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000670
Chris Lattner76ac0682005-11-15 00:40:23 +0000671
672std::vector<SDOperand>
673X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
674 std::vector<SDOperand> ArgValues;
675
676 MachineFunction &MF = DAG.getMachineFunction();
677 MachineFrameInfo *MFI = MF.getFrameInfo();
678
679 // Add DAG nodes to load the arguments... On entry to a function the stack
680 // frame looks like this:
681 //
682 // [ESP] -- return address
683 // [ESP + 4] -- first nonreg argument (leftmost lexically)
684 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
685 // ...
686 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
687
688 // Keep track of the number of integer regs passed so far. This can be either
689 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
690 // used).
691 unsigned NumIntRegs = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000692
Chris Lattner76ac0682005-11-15 00:40:23 +0000693 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
694 MVT::ValueType ObjectVT = getValueType(I->getType());
695 unsigned ArgIncrement = 4;
696 unsigned ObjSize = 0;
697 SDOperand ArgValue;
698
699 switch (ObjectVT) {
700 default: assert(0 && "Unhandled argument type!");
701 case MVT::i1:
702 case MVT::i8:
Chris Lattner43798852006-03-17 05:10:20 +0000703 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000704 if (!I->use_empty()) {
705 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
706 X86::R8RegisterClass);
707 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
708 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000709 if (ObjectVT == MVT::i1)
710 // FIXME: Should insert a assertzext here.
711 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000712 }
713 ++NumIntRegs;
714 break;
715 }
716
717 ObjSize = 1;
718 break;
719 case MVT::i16:
Chris Lattner43798852006-03-17 05:10:20 +0000720 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000721 if (!I->use_empty()) {
722 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
723 X86::R16RegisterClass);
724 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
725 DAG.setRoot(ArgValue.getValue(1));
726 }
727 ++NumIntRegs;
728 break;
729 }
730 ObjSize = 2;
731 break;
732 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000733 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000734 if (!I->use_empty()) {
Chris Lattner43798852006-03-17 05:10:20 +0000735 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattner76ac0682005-11-15 00:40:23 +0000736 X86::R32RegisterClass);
737 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
738 DAG.setRoot(ArgValue.getValue(1));
739 }
740 ++NumIntRegs;
741 break;
742 }
743 ObjSize = 4;
744 break;
745 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000746 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000747 if (!I->use_empty()) {
748 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
749 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
750
751 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
752 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
753 DAG.setRoot(Hi.getValue(1));
754
755 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
756 }
Chris Lattner43798852006-03-17 05:10:20 +0000757 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000758 break;
Chris Lattner43798852006-03-17 05:10:20 +0000759 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000760 if (!I->use_empty()) {
761 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
762 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
763 DAG.setRoot(Low.getValue(1));
764
765 // Load the high part from memory.
766 // Create the frame index object for this incoming parameter...
767 int FI = MFI->CreateFixedObject(4, ArgOffset);
768 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
769 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
770 DAG.getSrcValue(NULL));
771 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
772 }
773 ArgOffset += 4;
Chris Lattner43798852006-03-17 05:10:20 +0000774 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000775 break;
776 }
777 ObjSize = ArgIncrement = 8;
778 break;
779 case MVT::f32: ObjSize = 4; break;
780 case MVT::f64: ObjSize = ArgIncrement = 8; break;
781 }
782
783 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
784 // dead loads.
785 if (ObjSize && !I->use_empty()) {
786 // Create the frame index object for this incoming parameter...
787 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
788
789 // Create the SelectionDAG nodes corresponding to a load from this
790 // parameter.
791 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
792
793 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
794 DAG.getSrcValue(NULL));
795 } else if (ArgValue.Val == 0) {
796 if (MVT::isInteger(ObjectVT))
797 ArgValue = DAG.getConstant(0, ObjectVT);
798 else
799 ArgValue = DAG.getConstantFP(0, ObjectVT);
800 }
801 ArgValues.push_back(ArgValue);
802
803 if (ObjSize)
804 ArgOffset += ArgIncrement; // Move on to the next argument.
805 }
806
807 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
808 // arguments and the arguments after the retaddr has been pushed are aligned.
809 if ((ArgOffset & 7) == 0)
810 ArgOffset += 4;
811
812 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
813 ReturnAddrIndex = 0; // No return address slot generated yet.
814 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
815 BytesCallerReserves = 0;
816
817 // Finally, inform the code generator which regs we return values in.
818 switch (getValueType(F.getReturnType())) {
819 default: assert(0 && "Unknown type!");
820 case MVT::isVoid: break;
821 case MVT::i1:
822 case MVT::i8:
823 case MVT::i16:
824 case MVT::i32:
825 MF.addLiveOut(X86::EAX);
826 break;
827 case MVT::i64:
828 MF.addLiveOut(X86::EAX);
829 MF.addLiveOut(X86::EDX);
830 break;
831 case MVT::f32:
832 case MVT::f64:
833 MF.addLiveOut(X86::ST0);
834 break;
835 }
836 return ArgValues;
837}
838
839std::pair<SDOperand, SDOperand>
840X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
841 bool isTailCall, SDOperand Callee,
842 ArgListTy &Args, SelectionDAG &DAG) {
843 // Count how many bytes are to be pushed on the stack.
844 unsigned NumBytes = 0;
845
846 // Keep track of the number of integer regs passed so far. This can be either
847 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
848 // used).
849 unsigned NumIntRegs = 0;
850
851 for (unsigned i = 0, e = Args.size(); i != e; ++i)
852 switch (getValueType(Args[i].second)) {
853 default: assert(0 && "Unknown value type!");
854 case MVT::i1:
855 case MVT::i8:
856 case MVT::i16:
857 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000858 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000859 ++NumIntRegs;
860 break;
861 }
862 // fall through
863 case MVT::f32:
864 NumBytes += 4;
865 break;
866 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000867 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
868 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000869 break;
Chris Lattner43798852006-03-17 05:10:20 +0000870 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
871 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000872 NumBytes += 4;
873 break;
874 }
875
876 // fall through
877 case MVT::f64:
878 NumBytes += 8;
879 break;
880 }
881
882 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
883 // arguments and the arguments after the retaddr has been pushed are aligned.
884 if ((NumBytes & 7) == 0)
885 NumBytes += 4;
886
Chris Lattner62c34842006-02-13 09:00:43 +0000887 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000888
889 // Arguments go on the stack in reverse order, as specified by the ABI.
890 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000891 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000892 NumIntRegs = 0;
893 std::vector<SDOperand> Stores;
894 std::vector<SDOperand> RegValuesToPass;
895 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
896 switch (getValueType(Args[i].second)) {
897 default: assert(0 && "Unexpected ValueType for argument!");
898 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000899 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
900 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000901 case MVT::i8:
902 case MVT::i16:
903 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000904 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000905 RegValuesToPass.push_back(Args[i].first);
906 ++NumIntRegs;
907 break;
908 }
909 // Fall through
910 case MVT::f32: {
911 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
912 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
913 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
914 Args[i].first, PtrOff,
915 DAG.getSrcValue(NULL)));
916 ArgOffset += 4;
917 break;
918 }
919 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000920 // Can pass (at least) part of it in regs?
921 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000922 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
923 Args[i].first, DAG.getConstant(1, MVT::i32));
924 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
925 Args[i].first, DAG.getConstant(0, MVT::i32));
926 RegValuesToPass.push_back(Lo);
927 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +0000928
929 // Pass both parts in regs?
930 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000931 RegValuesToPass.push_back(Hi);
932 ++NumIntRegs;
933 } else {
934 // Pass the high part in memory.
935 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
936 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
937 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
938 Hi, PtrOff, DAG.getSrcValue(NULL)));
939 ArgOffset += 4;
940 }
941 break;
942 }
943 // Fall through
944 case MVT::f64:
945 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
946 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
947 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
948 Args[i].first, PtrOff,
949 DAG.getSrcValue(NULL)));
950 ArgOffset += 8;
951 break;
952 }
953 }
954 if (!Stores.empty())
955 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
956
957 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
958 // arguments and the arguments after the retaddr has been pushed are aligned.
959 if ((ArgOffset & 7) == 0)
960 ArgOffset += 4;
961
962 std::vector<MVT::ValueType> RetVals;
963 MVT::ValueType RetTyVT = getValueType(RetTy);
964
965 RetVals.push_back(MVT::Other);
966
967 // The result values produced have to be legal. Promote the result.
968 switch (RetTyVT) {
969 case MVT::isVoid: break;
970 default:
971 RetVals.push_back(RetTyVT);
972 break;
973 case MVT::i1:
974 case MVT::i8:
975 case MVT::i16:
976 RetVals.push_back(MVT::i32);
977 break;
978 case MVT::f32:
979 if (X86ScalarSSE)
980 RetVals.push_back(MVT::f32);
981 else
982 RetVals.push_back(MVT::f64);
983 break;
984 case MVT::i64:
985 RetVals.push_back(MVT::i32);
986 RetVals.push_back(MVT::i32);
987 break;
988 }
989
Nate Begeman7e5496d2006-02-17 00:03:04 +0000990 // Build a sequence of copy-to-reg nodes chained together with token chain
991 // and flag operands which copy the outgoing args into registers.
992 SDOperand InFlag;
993 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
994 unsigned CCReg;
995 SDOperand RegToPass = RegValuesToPass[i];
996 switch (RegToPass.getValueType()) {
997 default: assert(0 && "Bad thing to pass in regs");
998 case MVT::i8:
999 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001000 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001001 case MVT::i16:
1002 CCReg = (i == 0) ? X86::AX : X86::DX;
1003 break;
1004 case MVT::i32:
1005 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1006 break;
1007 }
1008
1009 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1010 InFlag = Chain.getValue(1);
1011 }
1012
1013 std::vector<MVT::ValueType> NodeTys;
1014 NodeTys.push_back(MVT::Other); // Returns a chain
1015 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1016 std::vector<SDOperand> Ops;
1017 Ops.push_back(Chain);
1018 Ops.push_back(Callee);
1019 if (InFlag.Val)
1020 Ops.push_back(InFlag);
1021
1022 // FIXME: Do not generate X86ISD::TAILCALL for now.
1023 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1024 InFlag = Chain.getValue(1);
1025
1026 NodeTys.clear();
1027 NodeTys.push_back(MVT::Other); // Returns a chain
1028 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1029 Ops.clear();
1030 Ops.push_back(Chain);
1031 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1032 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1033 Ops.push_back(InFlag);
1034 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1035 InFlag = Chain.getValue(1);
1036
1037 SDOperand RetVal;
1038 if (RetTyVT != MVT::isVoid) {
1039 switch (RetTyVT) {
1040 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001041 case MVT::i1:
1042 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001043 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1044 Chain = RetVal.getValue(1);
1045 if (RetTyVT == MVT::i1)
1046 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1047 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001048 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001049 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, 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::i32:
1053 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1054 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001055 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001056 case MVT::i64: {
1057 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1058 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1059 Lo.getValue(2));
1060 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1061 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001062 break;
1063 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001064 case MVT::f32:
1065 case MVT::f64: {
1066 std::vector<MVT::ValueType> Tys;
1067 Tys.push_back(MVT::f64);
1068 Tys.push_back(MVT::Other);
1069 Tys.push_back(MVT::Flag);
1070 std::vector<SDOperand> Ops;
1071 Ops.push_back(Chain);
1072 Ops.push_back(InFlag);
1073 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1074 Chain = RetVal.getValue(1);
1075 InFlag = RetVal.getValue(2);
1076 if (X86ScalarSSE) {
1077 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1078 // shouldn't be necessary except that RFP cannot be live across
1079 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1080 MachineFunction &MF = DAG.getMachineFunction();
1081 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1082 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1083 Tys.clear();
1084 Tys.push_back(MVT::Other);
1085 Ops.clear();
1086 Ops.push_back(Chain);
1087 Ops.push_back(RetVal);
1088 Ops.push_back(StackSlot);
1089 Ops.push_back(DAG.getValueType(RetTyVT));
1090 Ops.push_back(InFlag);
1091 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1092 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1093 DAG.getSrcValue(NULL));
1094 Chain = RetVal.getValue(1);
1095 }
Evan Cheng172fce72006-01-06 00:43:03 +00001096
Nate Begeman7e5496d2006-02-17 00:03:04 +00001097 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1098 // FIXME: we would really like to remember that this FP_ROUND
1099 // operation is okay to eliminate if we allow excess FP precision.
1100 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1101 break;
1102 }
1103 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001104 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001105
1106 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001107}
1108
1109SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1110 if (ReturnAddrIndex == 0) {
1111 // Set up a frame object for the return address.
1112 MachineFunction &MF = DAG.getMachineFunction();
1113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1114 }
1115
1116 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1117}
1118
1119
1120
1121std::pair<SDOperand, SDOperand> X86TargetLowering::
1122LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1123 SelectionDAG &DAG) {
1124 SDOperand Result;
1125 if (Depth) // Depths > 0 not supported yet!
1126 Result = DAG.getConstant(0, getPointerTy());
1127 else {
1128 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1129 if (!isFrameAddress)
1130 // Just load the return address
1131 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1132 DAG.getSrcValue(NULL));
1133 else
1134 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1135 DAG.getConstant(4, MVT::i32));
1136 }
1137 return std::make_pair(Result, Chain);
1138}
1139
Evan Cheng339edad2006-01-11 00:33:36 +00001140/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1141/// which corresponds to the condition code.
1142static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1143 switch (X86CC) {
1144 default: assert(0 && "Unknown X86 conditional code!");
1145 case X86ISD::COND_A: return X86::JA;
1146 case X86ISD::COND_AE: return X86::JAE;
1147 case X86ISD::COND_B: return X86::JB;
1148 case X86ISD::COND_BE: return X86::JBE;
1149 case X86ISD::COND_E: return X86::JE;
1150 case X86ISD::COND_G: return X86::JG;
1151 case X86ISD::COND_GE: return X86::JGE;
1152 case X86ISD::COND_L: return X86::JL;
1153 case X86ISD::COND_LE: return X86::JLE;
1154 case X86ISD::COND_NE: return X86::JNE;
1155 case X86ISD::COND_NO: return X86::JNO;
1156 case X86ISD::COND_NP: return X86::JNP;
1157 case X86ISD::COND_NS: return X86::JNS;
1158 case X86ISD::COND_O: return X86::JO;
1159 case X86ISD::COND_P: return X86::JP;
1160 case X86ISD::COND_S: return X86::JS;
1161 }
1162}
Chris Lattner76ac0682005-11-15 00:40:23 +00001163
Evan Cheng45df7f82006-01-30 23:41:35 +00001164/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1165/// specific condition code. It returns a false if it cannot do a direct
1166/// translation. X86CC is the translated CondCode. Flip is set to true if the
1167/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001168static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1169 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001170 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001171 Flip = false;
1172 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001173 if (!isFP) {
1174 switch (SetCCOpcode) {
1175 default: break;
1176 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1177 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1178 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1179 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1180 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1181 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1182 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1183 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1184 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1185 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1186 }
1187 } else {
1188 // On a floating point condition, the flags are set as follows:
1189 // ZF PF CF op
1190 // 0 | 0 | 0 | X > Y
1191 // 0 | 0 | 1 | X < Y
1192 // 1 | 0 | 0 | X == Y
1193 // 1 | 1 | 1 | unordered
1194 switch (SetCCOpcode) {
1195 default: break;
1196 case ISD::SETUEQ:
1197 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001198 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001199 case ISD::SETOGT:
1200 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001201 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001202 case ISD::SETOGE:
1203 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001204 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001205 case ISD::SETULT:
1206 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001207 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001208 case ISD::SETULE:
1209 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1210 case ISD::SETONE:
1211 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1212 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1213 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1214 }
1215 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001216
1217 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001218}
1219
Evan Cheng339edad2006-01-11 00:33:36 +00001220/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1221/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001222/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001223static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001224 switch (X86CC) {
1225 default:
1226 return false;
1227 case X86ISD::COND_B:
1228 case X86ISD::COND_BE:
1229 case X86ISD::COND_E:
1230 case X86ISD::COND_P:
1231 case X86ISD::COND_A:
1232 case X86ISD::COND_AE:
1233 case X86ISD::COND_NE:
1234 case X86ISD::COND_NP:
1235 return true;
1236 }
1237}
1238
Evan Cheng339edad2006-01-11 00:33:36 +00001239MachineBasicBlock *
1240X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1241 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001242 switch (MI->getOpcode()) {
1243 default: assert(false && "Unexpected instr type to insert");
1244 case X86::CMOV_FR32:
1245 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001246 // To "insert" a SELECT_CC instruction, we actually have to insert the
1247 // diamond control-flow pattern. The incoming instruction knows the
1248 // destination vreg to set, the condition code register to branch on, the
1249 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 ilist<MachineBasicBlock>::iterator It = BB;
1252 ++It;
1253
1254 // thisMBB:
1255 // ...
1256 // TrueVal = ...
1257 // cmpTY ccX, r1, r2
1258 // bCC copy1MBB
1259 // fallthrough --> copy0MBB
1260 MachineBasicBlock *thisMBB = BB;
1261 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1262 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1263 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1264 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1265 MachineFunction *F = BB->getParent();
1266 F->getBasicBlockList().insert(It, copy0MBB);
1267 F->getBasicBlockList().insert(It, sinkMBB);
1268 // Update machine-CFG edges
1269 BB->addSuccessor(copy0MBB);
1270 BB->addSuccessor(sinkMBB);
1271
1272 // copy0MBB:
1273 // %FalseValue = ...
1274 // # fallthrough to sinkMBB
1275 BB = copy0MBB;
1276
1277 // Update machine-CFG edges
1278 BB->addSuccessor(sinkMBB);
1279
1280 // sinkMBB:
1281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1282 // ...
1283 BB = sinkMBB;
1284 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001287
Evan Cheng911c68d2006-01-16 21:21:29 +00001288 delete MI; // The pseudo instruction is gone now.
1289 return BB;
1290 }
Evan Cheng339edad2006-01-11 00:33:36 +00001291
Evan Cheng911c68d2006-01-16 21:21:29 +00001292 case X86::FP_TO_INT16_IN_MEM:
1293 case X86::FP_TO_INT32_IN_MEM:
1294 case X86::FP_TO_INT64_IN_MEM: {
1295 // Change the floating point control register to use "round towards zero"
1296 // mode when truncating to an integer value.
1297 MachineFunction *F = BB->getParent();
1298 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1299 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1300
1301 // Load the old value of the high byte of the control word...
1302 unsigned OldCW =
1303 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1304 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1305
1306 // Set the high part to be round to zero...
1307 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1308
1309 // Reload the modified control word now...
1310 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1311
1312 // Restore the memory image of control word to original value
1313 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1314
1315 // Get the X86 opcode to use.
1316 unsigned Opc;
1317 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001318 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001319 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1320 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1321 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1322 }
1323
1324 X86AddressMode AM;
1325 MachineOperand &Op = MI->getOperand(0);
1326 if (Op.isRegister()) {
1327 AM.BaseType = X86AddressMode::RegBase;
1328 AM.Base.Reg = Op.getReg();
1329 } else {
1330 AM.BaseType = X86AddressMode::FrameIndexBase;
1331 AM.Base.FrameIndex = Op.getFrameIndex();
1332 }
1333 Op = MI->getOperand(1);
1334 if (Op.isImmediate())
1335 AM.Scale = Op.getImmedValue();
1336 Op = MI->getOperand(2);
1337 if (Op.isImmediate())
1338 AM.IndexReg = Op.getImmedValue();
1339 Op = MI->getOperand(3);
1340 if (Op.isGlobalAddress()) {
1341 AM.GV = Op.getGlobal();
1342 } else {
1343 AM.Disp = Op.getImmedValue();
1344 }
1345 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1346
1347 // Reload the original control word now.
1348 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1349
1350 delete MI; // The pseudo instruction is gone now.
1351 return BB;
1352 }
1353 }
Evan Cheng339edad2006-01-11 00:33:36 +00001354}
1355
1356
1357//===----------------------------------------------------------------------===//
1358// X86 Custom Lowering Hooks
1359//===----------------------------------------------------------------------===//
1360
Evan Chengaf598d22006-03-13 23:18:16 +00001361/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1362/// load. For Darwin, external and weak symbols are indirect, loading the value
1363/// at address GV rather then the value of GV itself. This means that the
1364/// GlobalAddress must be in the base or index register of the address, not the
1365/// GV offset field.
1366static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1367 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1368 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1369}
1370
Evan Chengd097e672006-03-22 02:53:00 +00001371/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1372/// a splat of a single element.
1373bool X86::isSplatMask(SDNode *N) {
1374 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1375
1376 // We can only splat 64-bit, and 32-bit quantities.
1377 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1378 return false;
1379
1380 // This is a splat operation if each element of the permute is the same, and
1381 // if the value doesn't reference the second vector.
1382 SDOperand Elt = N->getOperand(0);
1383 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1384 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1385 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1386 "Invalid VECTOR_SHUFFLE mask!");
1387 if (N->getOperand(i) != Elt) return false;
1388 }
1389
1390 // Make sure it is a splat of the first vector operand.
1391 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1392}
1393
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001394/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1395/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1396/// instructions.
1397unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001398 unsigned NumOperands = N->getNumOperands();
1399 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1400 unsigned Mask = 0;
1401 unsigned i = NumOperands - 1;
1402 do {
1403 Mask |= cast<ConstantSDNode>(N->getOperand(i))->getValue();
1404 Mask <<= Shift;
1405 --i;
1406 } while (i != 0);
1407
1408 return Mask;
1409}
1410
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001411/// getShufflePSHUFDImmediate - Return the appropriate immediate to shuffle
1412/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFD instruction.
1413unsigned X86::getShufflePSHUFDImmediate(SDNode *N) {
1414 unsigned NumOperands = N->getNumOperands();
1415 unsigned Mask = 0;
1416
1417 assert(NumOperands == 4 && "Expect v4f32 / v4i32 vector operand");
1418
1419 unsigned i = NumOperands - 1;
1420 do {
1421 uint64_t Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1422 // Second vector operand must be undef. We can have it point to anything
1423 // we want.
1424 if (Val >= NumOperands) Val = 0;
1425 Mask |= Val;
1426 Mask <<= 2;
1427 --i;
1428 } while (i != 0);
1429
1430 return Mask;
1431}
1432
Chris Lattner76ac0682005-11-15 00:40:23 +00001433/// LowerOperation - Provide custom lowering hooks for some operations.
1434///
1435SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1436 switch (Op.getOpcode()) {
1437 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001438 case ISD::SHL_PARTS:
1439 case ISD::SRA_PARTS:
1440 case ISD::SRL_PARTS: {
1441 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1442 "Not an i64 shift!");
1443 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1444 SDOperand ShOpLo = Op.getOperand(0);
1445 SDOperand ShOpHi = Op.getOperand(1);
1446 SDOperand ShAmt = Op.getOperand(2);
1447 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001448 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001449 : DAG.getConstant(0, MVT::i32);
1450
1451 SDOperand Tmp2, Tmp3;
1452 if (Op.getOpcode() == ISD::SHL_PARTS) {
1453 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1454 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1455 } else {
1456 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001457 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001458 }
1459
1460 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1461 ShAmt, DAG.getConstant(32, MVT::i8));
1462
1463 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001464 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001465
1466 std::vector<MVT::ValueType> Tys;
1467 Tys.push_back(MVT::i32);
1468 Tys.push_back(MVT::Flag);
1469 std::vector<SDOperand> Ops;
1470 if (Op.getOpcode() == ISD::SHL_PARTS) {
1471 Ops.push_back(Tmp2);
1472 Ops.push_back(Tmp3);
1473 Ops.push_back(CC);
1474 Ops.push_back(InFlag);
1475 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1476 InFlag = Hi.getValue(1);
1477
1478 Ops.clear();
1479 Ops.push_back(Tmp3);
1480 Ops.push_back(Tmp1);
1481 Ops.push_back(CC);
1482 Ops.push_back(InFlag);
1483 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1484 } else {
1485 Ops.push_back(Tmp2);
1486 Ops.push_back(Tmp3);
1487 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001488 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001489 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1490 InFlag = Lo.getValue(1);
1491
1492 Ops.clear();
1493 Ops.push_back(Tmp3);
1494 Ops.push_back(Tmp1);
1495 Ops.push_back(CC);
1496 Ops.push_back(InFlag);
1497 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1498 }
1499
1500 Tys.clear();
1501 Tys.push_back(MVT::i32);
1502 Tys.push_back(MVT::i32);
1503 Ops.clear();
1504 Ops.push_back(Lo);
1505 Ops.push_back(Hi);
1506 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1507 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001508 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001509 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001510 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001511 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001512
1513 SDOperand Result;
1514 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1515 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001516 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001517 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001518 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001519 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1520 DAG.getEntryNode(), Op.getOperand(0),
1521 StackSlot, DAG.getSrcValue(NULL));
1522
1523 // Build the FILD
1524 std::vector<MVT::ValueType> Tys;
1525 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001526 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001527 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001528 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001529 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001530 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001531 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001532 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1533 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001534
1535 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001536 Chain = Result.getValue(1);
1537 SDOperand InFlag = Result.getValue(2);
1538
Evan Cheng11613a52006-02-04 02:20:30 +00001539 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001540 // shouldn't be necessary except that RFP cannot be live across
1541 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1542 MachineFunction &MF = DAG.getMachineFunction();
1543 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1544 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1545 std::vector<MVT::ValueType> Tys;
1546 Tys.push_back(MVT::Other);
1547 std::vector<SDOperand> Ops;
1548 Ops.push_back(Chain);
1549 Ops.push_back(Result);
1550 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001551 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001552 Ops.push_back(InFlag);
1553 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1554 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1555 DAG.getSrcValue(NULL));
1556 }
1557
Evan Cheng6305e502006-01-12 22:54:21 +00001558 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001559 }
1560 case ISD::FP_TO_SINT: {
1561 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001562 "Unknown FP_TO_SINT to lower!");
1563 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1564 // stack slot.
1565 MachineFunction &MF = DAG.getMachineFunction();
1566 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1567 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1568 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1569
1570 unsigned Opc;
1571 switch (Op.getValueType()) {
1572 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1573 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1574 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1575 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1576 }
1577
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001578 SDOperand Chain = DAG.getEntryNode();
1579 SDOperand Value = Op.getOperand(0);
1580 if (X86ScalarSSE) {
1581 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1582 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1583 DAG.getSrcValue(0));
1584 std::vector<MVT::ValueType> Tys;
1585 Tys.push_back(MVT::f64);
1586 Tys.push_back(MVT::Other);
1587 std::vector<SDOperand> Ops;
1588 Ops.push_back(Chain);
1589 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001590 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001591 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1592 Chain = Value.getValue(1);
1593 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1594 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1595 }
1596
Chris Lattner76ac0682005-11-15 00:40:23 +00001597 // Build the FP_TO_INT*_IN_MEM
1598 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001599 Ops.push_back(Chain);
1600 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00001601 Ops.push_back(StackSlot);
1602 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1603
1604 // Load the result.
1605 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1606 DAG.getSrcValue(NULL));
1607 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001608 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00001609 std::vector<MVT::ValueType> Tys;
1610 Tys.push_back(MVT::Other);
1611 Tys.push_back(MVT::Flag);
1612 std::vector<SDOperand> Ops;
1613 Ops.push_back(Op.getOperand(0));
1614 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00001615 Ops.clear();
1616 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1617 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1618 MVT::i32, Ops[0].getValue(2)));
1619 Ops.push_back(Ops[1].getValue(1));
1620 Tys[0] = Tys[1] = MVT::i32;
1621 Tys.push_back(MVT::Other);
1622 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00001623 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00001624 case ISD::FABS: {
1625 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00001626 const Type *OpNTy = MVT::getTypeForValueType(VT);
1627 std::vector<Constant*> CV;
1628 if (VT == MVT::f64) {
1629 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1630 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1631 } else {
1632 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1633 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1634 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1635 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1636 }
1637 Constant *CS = ConstantStruct::get(CV);
1638 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1639 SDOperand Mask
1640 = DAG.getNode(X86ISD::LOAD_PACK,
1641 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00001642 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1643 }
Evan Cheng72d5c252006-01-31 22:28:30 +00001644 case ISD::FNEG: {
1645 MVT::ValueType VT = Op.getValueType();
1646 const Type *OpNTy = MVT::getTypeForValueType(VT);
1647 std::vector<Constant*> CV;
1648 if (VT == MVT::f64) {
1649 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1650 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1651 } else {
1652 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1653 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1654 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1655 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1656 }
1657 Constant *CS = ConstantStruct::get(CV);
1658 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1659 SDOperand Mask
1660 = DAG.getNode(X86ISD::LOAD_PACK,
1661 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1662 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1663 }
Evan Chengc1583db2005-12-21 20:21:51 +00001664 case ISD::SETCC: {
1665 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00001666 SDOperand Cond;
1667 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00001668 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1669 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00001670 bool Flip;
1671 unsigned X86CC;
1672 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1673 if (Flip)
1674 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1675 Op.getOperand(1), Op.getOperand(0));
1676 else
1677 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1678 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001679 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1680 DAG.getConstant(X86CC, MVT::i8), Cond);
1681 } else {
1682 assert(isFP && "Illegal integer SetCC!");
1683
Evan Cheng45df7f82006-01-30 23:41:35 +00001684 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1685 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00001686 std::vector<MVT::ValueType> Tys;
1687 std::vector<SDOperand> Ops;
1688 switch (SetCCOpcode) {
1689 default: assert(false && "Illegal floating point SetCC!");
1690 case ISD::SETOEQ: { // !PF & ZF
1691 Tys.push_back(MVT::i8);
1692 Tys.push_back(MVT::Flag);
1693 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1694 Ops.push_back(Cond);
1695 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1696 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1697 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1698 Tmp1.getValue(1));
1699 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1700 }
Evan Cheng172fce72006-01-06 00:43:03 +00001701 case ISD::SETUNE: { // PF | !ZF
1702 Tys.push_back(MVT::i8);
1703 Tys.push_back(MVT::Flag);
1704 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1705 Ops.push_back(Cond);
1706 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1707 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1708 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1709 Tmp1.getValue(1));
1710 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1711 }
1712 }
1713 }
Evan Chengc1583db2005-12-21 20:21:51 +00001714 }
Evan Cheng225a4d02005-12-17 01:21:05 +00001715 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001716 MVT::ValueType VT = Op.getValueType();
1717 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00001718 bool isFPStack = isFP && !X86ScalarSSE;
1719 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00001720 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001721 SDOperand Op0 = Op.getOperand(0);
1722 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001723 if (Op0.getOpcode() == ISD::SETCC)
1724 Op0 = LowerOperation(Op0, DAG);
1725
Evan Cheng73a1ad92006-01-10 20:26:56 +00001726 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001727 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1728 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1729 // have another use it will be eliminated.
1730 // If the X86ISD::SETCC has more than one use, then it's probably better
1731 // to use a test instead of duplicating the X86ISD::CMP (for register
1732 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001733 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1734 if (!Op0.hasOneUse()) {
1735 std::vector<MVT::ValueType> Tys;
1736 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1737 Tys.push_back(Op0.Val->getValueType(i));
1738 std::vector<SDOperand> Ops;
1739 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1740 Ops.push_back(Op0.getOperand(i));
1741 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1742 }
1743
Evan Chengfb22e862006-01-13 01:03:02 +00001744 CC = Op0.getOperand(0);
1745 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00001746 // Make a copy as flag result cannot be used by more than one.
1747 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1748 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001749 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00001750 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00001751 } else
1752 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001753 } else
1754 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00001755
Evan Cheng731423f2006-01-13 01:06:49 +00001756 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00001757 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00001758 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00001759 }
Evan Cheng9c249c32006-01-09 18:33:28 +00001760
1761 std::vector<MVT::ValueType> Tys;
1762 Tys.push_back(Op.getValueType());
1763 Tys.push_back(MVT::Flag);
1764 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00001765 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1766 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00001767 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00001768 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00001769 Ops.push_back(CC);
1770 Ops.push_back(Cond);
1771 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00001772 }
Evan Cheng6fc31042005-12-19 23:12:38 +00001773 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00001774 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00001775 SDOperand Cond = Op.getOperand(1);
1776 SDOperand Dest = Op.getOperand(2);
1777 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00001778 if (Cond.getOpcode() == ISD::SETCC)
1779 Cond = LowerOperation(Cond, DAG);
1780
Evan Chengc1583db2005-12-21 20:21:51 +00001781 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00001782 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1783 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1784 // have another use it will be eliminated.
1785 // If the X86ISD::SETCC has more than one use, then it's probably better
1786 // to use a test instead of duplicating the X86ISD::CMP (for register
1787 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00001788 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1789 if (!Cond.hasOneUse()) {
1790 std::vector<MVT::ValueType> Tys;
1791 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1792 Tys.push_back(Cond.Val->getValueType(i));
1793 std::vector<SDOperand> Ops;
1794 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1795 Ops.push_back(Cond.getOperand(i));
1796 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1797 }
1798
Evan Chengfb22e862006-01-13 01:03:02 +00001799 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00001800 Cond = Cond.getOperand(1);
1801 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00001802 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00001803 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00001804 } else
1805 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00001806 } else
1807 addTest = true;
1808
1809 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00001810 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00001811 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1812 }
1813 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1814 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1815 }
Evan Chengae986f12006-01-11 22:15:48 +00001816 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00001817 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00001818 SDOperand Chain = Op.getOperand(0);
1819 unsigned Align =
1820 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1821 if (Align == 0) Align = 1;
1822
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001823 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1824 // If not DWORD aligned, call memset if size is less than the threshold.
1825 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001826 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00001827 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001828 MVT::ValueType IntPtr = getPointerTy();
1829 const Type *IntPtrTy = getTargetData().getIntPtrType();
1830 std::vector<std::pair<SDOperand, const Type*> > Args;
1831 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1832 // Extend the ubyte argument to be an int value for the call.
1833 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1834 Args.push_back(std::make_pair(Val, IntPtrTy));
1835 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1836 std::pair<SDOperand,SDOperand> CallResult =
1837 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1838 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1839 return CallResult.second;
1840 }
1841
Evan Chengae986f12006-01-11 22:15:48 +00001842 MVT::ValueType AVT;
1843 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001844 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1845 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00001846 bool TwoRepStos = false;
Evan Cheng6dc73292006-03-04 02:48:56 +00001847 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00001848 unsigned ValReg;
1849 unsigned Val = ValC->getValue() & 255;
1850
1851 // If the value is a constant, then we can potentially use larger sets.
1852 switch (Align & 3) {
1853 case 2: // WORD aligned
1854 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001855 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1856 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001857 Val = (Val << 8) | Val;
1858 ValReg = X86::AX;
1859 break;
1860 case 0: // DWORD aligned
1861 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00001862 if (I) {
1863 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1864 BytesLeft = I->getValue() % 4;
1865 } else {
1866 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1867 DAG.getConstant(2, MVT::i8));
1868 TwoRepStos = true;
1869 }
Evan Chengae986f12006-01-11 22:15:48 +00001870 Val = (Val << 8) | Val;
1871 Val = (Val << 16) | Val;
1872 ValReg = X86::EAX;
1873 break;
1874 default: // Byte aligned
1875 AVT = MVT::i8;
1876 Count = Op.getOperand(3);
1877 ValReg = X86::AL;
1878 break;
1879 }
1880
1881 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1882 InFlag);
1883 InFlag = Chain.getValue(1);
1884 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001885 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00001886 Count = Op.getOperand(3);
1887 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1888 InFlag = Chain.getValue(1);
1889 }
1890
1891 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1892 InFlag = Chain.getValue(1);
1893 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1894 InFlag = Chain.getValue(1);
1895
Evan Chengadc70932006-03-07 23:29:39 +00001896 std::vector<MVT::ValueType> Tys;
1897 Tys.push_back(MVT::Other);
1898 Tys.push_back(MVT::Flag);
1899 std::vector<SDOperand> Ops;
1900 Ops.push_back(Chain);
1901 Ops.push_back(DAG.getValueType(AVT));
1902 Ops.push_back(InFlag);
1903 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1904
1905 if (TwoRepStos) {
1906 InFlag = Chain.getValue(1);
1907 Count = Op.getOperand(3);
1908 MVT::ValueType CVT = Count.getValueType();
1909 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1910 DAG.getConstant(3, CVT));
1911 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1912 InFlag = Chain.getValue(1);
1913 Tys.clear();
1914 Tys.push_back(MVT::Other);
1915 Tys.push_back(MVT::Flag);
1916 Ops.clear();
1917 Ops.push_back(Chain);
1918 Ops.push_back(DAG.getValueType(MVT::i8));
1919 Ops.push_back(InFlag);
1920 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1921 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00001922 // Issue stores for the last 1 - 3 bytes.
1923 SDOperand Value;
1924 unsigned Val = ValC->getValue() & 255;
1925 unsigned Offset = I->getValue() - BytesLeft;
1926 SDOperand DstAddr = Op.getOperand(1);
1927 MVT::ValueType AddrVT = DstAddr.getValueType();
1928 if (BytesLeft >= 2) {
1929 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1930 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1931 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1932 DAG.getConstant(Offset, AddrVT)),
1933 DAG.getSrcValue(NULL));
1934 BytesLeft -= 2;
1935 Offset += 2;
1936 }
1937
1938 if (BytesLeft == 1) {
1939 Value = DAG.getConstant(Val, MVT::i8);
1940 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1941 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1942 DAG.getConstant(Offset, AddrVT)),
1943 DAG.getSrcValue(NULL));
1944 }
1945 }
1946
1947 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00001948 }
1949 case ISD::MEMCPY: {
1950 SDOperand Chain = Op.getOperand(0);
1951 unsigned Align =
1952 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1953 if (Align == 0) Align = 1;
1954
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001955 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1956 // If not DWORD aligned, call memcpy if size is less than the threshold.
1957 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00001958 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00001959 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00001960 MVT::ValueType IntPtr = getPointerTy();
1961 const Type *IntPtrTy = getTargetData().getIntPtrType();
1962 std::vector<std::pair<SDOperand, const Type*> > Args;
1963 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1964 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1965 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1966 std::pair<SDOperand,SDOperand> CallResult =
1967 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1968 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1969 return CallResult.second;
1970 }
1971
Evan Chengae986f12006-01-11 22:15:48 +00001972 MVT::ValueType AVT;
1973 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00001974 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00001975 bool TwoRepMovs = false;
Evan Chengae986f12006-01-11 22:15:48 +00001976 switch (Align & 3) {
1977 case 2: // WORD aligned
1978 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00001979 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1980 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00001981 break;
1982 case 0: // DWORD aligned
1983 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00001984 if (I) {
1985 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1986 BytesLeft = I->getValue() % 4;
1987 } else {
1988 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1989 DAG.getConstant(2, MVT::i8));
1990 TwoRepMovs = true;
1991 }
Evan Chengae986f12006-01-11 22:15:48 +00001992 break;
1993 default: // Byte aligned
1994 AVT = MVT::i8;
1995 Count = Op.getOperand(3);
1996 break;
1997 }
1998
Evan Cheng6dc73292006-03-04 02:48:56 +00001999 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002000 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2001 InFlag = Chain.getValue(1);
2002 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2003 InFlag = Chain.getValue(1);
2004 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2005 InFlag = Chain.getValue(1);
2006
Evan Chengadc70932006-03-07 23:29:39 +00002007 std::vector<MVT::ValueType> Tys;
2008 Tys.push_back(MVT::Other);
2009 Tys.push_back(MVT::Flag);
2010 std::vector<SDOperand> Ops;
2011 Ops.push_back(Chain);
2012 Ops.push_back(DAG.getValueType(AVT));
2013 Ops.push_back(InFlag);
2014 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2015
2016 if (TwoRepMovs) {
2017 InFlag = Chain.getValue(1);
2018 Count = Op.getOperand(3);
2019 MVT::ValueType CVT = Count.getValueType();
2020 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2021 DAG.getConstant(3, CVT));
2022 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2023 InFlag = Chain.getValue(1);
2024 Tys.clear();
2025 Tys.push_back(MVT::Other);
2026 Tys.push_back(MVT::Flag);
2027 Ops.clear();
2028 Ops.push_back(Chain);
2029 Ops.push_back(DAG.getValueType(MVT::i8));
2030 Ops.push_back(InFlag);
2031 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2032 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002033 // Issue loads and stores for the last 1 - 3 bytes.
2034 unsigned Offset = I->getValue() - BytesLeft;
2035 SDOperand DstAddr = Op.getOperand(1);
2036 MVT::ValueType DstVT = DstAddr.getValueType();
2037 SDOperand SrcAddr = Op.getOperand(2);
2038 MVT::ValueType SrcVT = SrcAddr.getValueType();
2039 SDOperand Value;
2040 if (BytesLeft >= 2) {
2041 Value = DAG.getLoad(MVT::i16, Chain,
2042 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2043 DAG.getConstant(Offset, SrcVT)),
2044 DAG.getSrcValue(NULL));
2045 Chain = Value.getValue(1);
2046 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2047 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2048 DAG.getConstant(Offset, DstVT)),
2049 DAG.getSrcValue(NULL));
2050 BytesLeft -= 2;
2051 Offset += 2;
2052 }
2053
2054 if (BytesLeft == 1) {
2055 Value = DAG.getLoad(MVT::i8, Chain,
2056 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2057 DAG.getConstant(Offset, SrcVT)),
2058 DAG.getSrcValue(NULL));
2059 Chain = Value.getValue(1);
2060 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2061 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2062 DAG.getConstant(Offset, DstVT)),
2063 DAG.getSrcValue(NULL));
2064 }
2065 }
2066
2067 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002068 }
Evan Cheng99470012006-02-25 09:55:19 +00002069
2070 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2071 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2072 // one of the above mentioned nodes. It has to be wrapped because otherwise
2073 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2074 // be used to form addressing mode. These wrapped nodes will be selected
2075 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00002076 case ISD::ConstantPool: {
2077 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002078 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2079 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2080 CP->getAlignment()));
Evan Cheng1f342c22006-02-23 02:43:52 +00002081 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002082 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002083 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00002084 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2085 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2086 }
2087
2088 return Result;
2089 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002090 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002091 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2092 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2093 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga74ce622005-12-21 02:39:21 +00002094 if (getTargetMachine().
Evan Cheng5588de92006-02-18 00:15:05 +00002095 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002096 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002097 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00002098 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2099 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00002100
2101 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Chengaf598d22006-03-13 23:18:16 +00002102 // the value at address GV, not the value of GV itself. This means that
Evan Cheng5588de92006-02-18 00:15:05 +00002103 // the GlobalAddress must be in the base or index register of the address,
2104 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00002105 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Chengaf598d22006-03-13 23:18:16 +00002106 DarwinGVRequiresExtraLoad(GV))
Evan Cheng5a766802006-02-07 08:38:37 +00002107 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00002108 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00002109 }
Evan Cheng5588de92006-02-18 00:15:05 +00002110
Evan Chengb94db9e2006-01-12 07:56:47 +00002111 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002112 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002113 case ISD::ExternalSymbol: {
2114 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2115 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2116 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2117 if (getTargetMachine().
2118 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2119 // With PIC, the address is actually $g + Offset.
2120 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2121 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2122 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2123 }
2124
2125 return Result;
2126 }
Nate Begemane74795c2006-01-25 18:21:52 +00002127 case ISD::VASTART: {
2128 // vastart just stores the address of the VarArgsFrameIndex slot into the
2129 // memory location argument.
2130 // FIXME: Replace MVT::i32 with PointerTy
2131 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2132 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2133 Op.getOperand(1), Op.getOperand(2));
2134 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002135 case ISD::RET: {
2136 SDOperand Copy;
2137
2138 switch(Op.getNumOperands()) {
2139 default:
2140 assert(0 && "Do not know how to return this many arguments!");
2141 abort();
2142 case 1:
2143 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2144 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2145 case 2: {
2146 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2147 if (MVT::isInteger(ArgVT))
2148 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2149 SDOperand());
2150 else if (!X86ScalarSSE) {
2151 std::vector<MVT::ValueType> Tys;
2152 Tys.push_back(MVT::Other);
2153 Tys.push_back(MVT::Flag);
2154 std::vector<SDOperand> Ops;
2155 Ops.push_back(Op.getOperand(0));
2156 Ops.push_back(Op.getOperand(1));
2157 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2158 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00002159 SDOperand MemLoc;
2160 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00002161 SDOperand Value = Op.getOperand(1);
2162
Evan Chenga24617f2006-02-01 01:19:32 +00002163 if (Value.getOpcode() == ISD::LOAD &&
2164 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00002165 Chain = Value.getOperand(0);
2166 MemLoc = Value.getOperand(1);
2167 } else {
2168 // Spill the value to memory and reload it into top of stack.
2169 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2170 MachineFunction &MF = DAG.getMachineFunction();
2171 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2172 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2173 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2174 Value, MemLoc, DAG.getSrcValue(0));
2175 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002176 std::vector<MVT::ValueType> Tys;
2177 Tys.push_back(MVT::f64);
2178 Tys.push_back(MVT::Other);
2179 std::vector<SDOperand> Ops;
2180 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002181 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002182 Ops.push_back(DAG.getValueType(ArgVT));
2183 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2184 Tys.clear();
2185 Tys.push_back(MVT::Other);
2186 Tys.push_back(MVT::Flag);
2187 Ops.clear();
2188 Ops.push_back(Copy.getValue(1));
2189 Ops.push_back(Copy);
2190 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2191 }
2192 break;
2193 }
2194 case 3:
2195 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2196 SDOperand());
2197 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2198 break;
2199 }
2200 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2201 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2202 Copy.getValue(1));
2203 }
Evan Chengd5e905d2006-03-21 23:01:21 +00002204 case ISD::SCALAR_TO_VECTOR: {
2205 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2206 return DAG.getNode(X86ISD::SCALAR_TO_VECTOR, Op.getValueType(), AnyExt);
2207 }
Evan Chengd097e672006-03-22 02:53:00 +00002208 case ISD::VECTOR_SHUFFLE: {
2209 SDOperand V1 = Op.getOperand(0);
2210 SDOperand V2 = Op.getOperand(1);
2211 SDOperand PermMask = Op.getOperand(2);
2212 MVT::ValueType VT = Op.getValueType();
2213
2214 if (V2.getOpcode() == ISD::UNDEF) {
2215 // Handle splat cases.
2216 if (X86::isSplatMask(PermMask.Val)) {
2217 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2218 // Use unpcklpd
2219 return DAG.getNode(X86ISD::UNPCKLP, VT, V1, V1);
2220 // Leave the VECTOR_SHUFFLE alone. It matches SHUFP*.
Chris Lattnerf5e36c82006-03-22 04:18:34 +00002221 return SDOperand();
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002222 } else if (VT == MVT::v4f32)
Evan Chengd097e672006-03-22 02:53:00 +00002223 // Leave the VECTOR_SHUFFLE alone. It matches PSHUFD.
Chris Lattnerf5e36c82006-03-22 04:18:34 +00002224 return SDOperand();
Evan Chengd097e672006-03-22 02:53:00 +00002225 }
2226
2227 // TODO.
Chris Lattnerf5e36c82006-03-22 04:18:34 +00002228 assert(0 && "TODO");
2229 abort();
Evan Chengd097e672006-03-22 02:53:00 +00002230 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002231 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002232}
Evan Cheng6af02632005-12-20 06:22:03 +00002233
2234const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2235 switch (Opcode) {
2236 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00002237 case X86ISD::SHLD: return "X86ISD::SHLD";
2238 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00002239 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00002240 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00002241 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00002242 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00002243 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2244 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2245 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00002246 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00002247 case X86ISD::FST: return "X86ISD::FST";
2248 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00002249 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00002250 case X86ISD::CALL: return "X86ISD::CALL";
2251 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2252 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2253 case X86ISD::CMP: return "X86ISD::CMP";
2254 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00002255 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00002256 case X86ISD::CMOV: return "X86ISD::CMOV";
2257 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00002258 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00002259 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2260 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00002261 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00002262 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002263 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengd5e905d2006-03-21 23:01:21 +00002264 case X86ISD::SCALAR_TO_VECTOR: return "X86ISD::SCALAR_TO_VECTOR";
Evan Chengd097e672006-03-22 02:53:00 +00002265 case X86ISD::UNPCKLP: return "X86ISD::UNPCKLP";
Evan Cheng6af02632005-12-20 06:22:03 +00002266 }
2267}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002268
Nate Begeman8a77efe2006-02-16 21:11:51 +00002269void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2270 uint64_t Mask,
2271 uint64_t &KnownZero,
2272 uint64_t &KnownOne,
2273 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002274
2275 unsigned Opc = Op.getOpcode();
Nate Begeman8a77efe2006-02-16 21:11:51 +00002276 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002277
2278 switch (Opc) {
2279 default:
2280 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2281 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00002282 case X86ISD::SETCC:
2283 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2284 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002285 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002286}
Chris Lattnerc642aa52006-01-31 19:43:35 +00002287
2288std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00002289getRegClassForInlineAsmConstraint(const std::string &Constraint,
2290 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00002291 if (Constraint.size() == 1) {
2292 // FIXME: not handling fp-stack yet!
2293 // FIXME: not handling MMX registers yet ('y' constraint).
2294 switch (Constraint[0]) { // GCC X86 Constraint Letters
2295 default: break; // Unknown constriant letter
2296 case 'r': // GENERAL_REGS
2297 case 'R': // LEGACY_REGS
2298 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2299 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2300 case 'l': // INDEX_REGS
2301 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2302 X86::ESI, X86::EDI, X86::EBP, 0);
2303 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2304 case 'Q': // Q_REGS
2305 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2306 case 'x': // SSE_REGS if SSE1 allowed
2307 if (Subtarget->hasSSE1())
2308 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2309 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2310 0);
2311 return std::vector<unsigned>();
2312 case 'Y': // SSE_REGS if SSE2 allowed
2313 if (Subtarget->hasSSE2())
2314 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2315 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2316 0);
2317 return std::vector<unsigned>();
2318 }
2319 }
2320
Chris Lattner7ad77df2006-02-22 00:56:39 +00002321 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00002322}
Evan Chengaf598d22006-03-13 23:18:16 +00002323
2324/// isLegalAddressImmediate - Return true if the integer value or
2325/// GlobalValue can be used as the offset of the target addressing mode.
2326bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2327 // X86 allows a sign-extended 32-bit immediate field.
2328 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2329}
2330
2331bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2332 if (getTargetMachine().
2333 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2334 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2335 if (RModel == Reloc::Static)
2336 return true;
2337 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00002338 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00002339 else
2340 return false;
2341 } else
2342 return true;
2343}