blob: 3b02080b28a4155374f6aee0b2045ae339c44a9f [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 Cheng78038292006-04-05 23:38:46 +000022#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000023#include "llvm/ADT/VectorExtras.h"
24#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000028#include "llvm/CodeGen/SelectionDAG.h"
29#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000031#include "llvm/Target/TargetOptions.h"
32using namespace llvm;
33
34// FIXME: temporary.
35#include "llvm/Support/CommandLine.h"
36static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
37 cl::desc("Enable fastcc on X86"));
38
39X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000041 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
43
Chris Lattner76ac0682005-11-15 00:40:23 +000044 // Set up the TargetLowering object.
45
46 // X86 is weird, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
49 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000050 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000051 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000052 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000053
Evan Chengbc047222006-03-22 19:22:18 +000054 if (!Subtarget->isTargetDarwin())
Evan Chengb09a56f2006-03-17 20:31:41 +000055 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
56 setUseUnderscoreSetJmpLongJmp(true);
57
Evan Cheng20931a72006-03-16 21:47:42 +000058 // Add legal addressing mode scale values.
59 addLegalAddressScale(8);
60 addLegalAddressScale(4);
61 addLegalAddressScale(2);
62 // Enter the ones which require both scale + index last. These are more
63 // expensive.
64 addLegalAddressScale(9);
65 addLegalAddressScale(5);
66 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000067
Chris Lattner76ac0682005-11-15 00:40:23 +000068 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000069 addRegisterClass(MVT::i8, X86::R8RegisterClass);
70 addRegisterClass(MVT::i16, X86::R16RegisterClass);
71 addRegisterClass(MVT::i32, X86::R32RegisterClass);
72
73 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
74 // operation.
75 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000078
79 if (X86ScalarSSE)
80 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
81 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
82 else
83 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000084
85 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
86 // this operation.
87 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
88 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000089 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000090 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000091 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000092 else {
93 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
94 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
95 }
Chris Lattner76ac0682005-11-15 00:40:23 +000096
Evan Cheng5b97fcf2006-01-30 08:02:57 +000097 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
98 // isn't legal.
99 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
101
Evan Cheng08390f62006-01-30 22:13:22 +0000102 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
103 // this operation.
104 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
105 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
106
107 if (X86ScalarSSE) {
108 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
109 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000112 }
113
114 // Handle FP_TO_UINT by promoting the destination to a larger signed
115 // conversion.
116 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
119
Evan Chengd13778e2006-02-18 07:26:17 +0000120 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000121 // Expand FP_TO_UINT into a select.
122 // FIXME: We would like to use a Custom expander here eventually to do
123 // the optimal thing for SSE vs. the default expansion in the legalizer.
124 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
125 else
Evan Chengd13778e2006-02-18 07:26:17 +0000126 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000127 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
128
Evan Cheng08390f62006-01-30 22:13:22 +0000129 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
130 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000131
Evan Cheng593bea72006-02-17 07:01:52 +0000132 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000133 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
134 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000135 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
139 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
140 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
141 setOperationAction(ISD::FREM , MVT::f64 , Expand);
142 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
150 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000151 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000152 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000153
Chris Lattner76ac0682005-11-15 00:40:23 +0000154 // These should be promoted to a larger select which is supported.
155 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
156 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000157
158 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000159 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
160 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000168 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000169 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000170 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000171 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000172 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000173 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000174 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000175 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000178 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000179 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
180 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000181
Chris Lattner9c415362005-11-29 06:16:21 +0000182 // We don't have line number support yet.
183 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000184 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000185 // FIXME - use subtarget debug flags
Evan Chengbc047222006-03-22 19:22:18 +0000186 if (!Subtarget->isTargetDarwin())
Evan Cheng30d7b702006-03-07 02:02:57 +0000187 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000188
Nate Begemane74795c2006-01-25 18:21:52 +0000189 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
190 setOperationAction(ISD::VASTART , MVT::Other, Custom);
191
192 // Use the default implementation.
193 setOperationAction(ISD::VAARG , MVT::Other, Expand);
194 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
195 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000196 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
197 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
198 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000199
Chris Lattner9c7f5032006-03-05 05:08:37 +0000200 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
201 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
202
Chris Lattner76ac0682005-11-15 00:40:23 +0000203 if (X86ScalarSSE) {
204 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000205 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
206 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000207
208 // SSE has no load+extend ops
209 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
210 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
211
Evan Cheng72d5c252006-01-31 22:28:30 +0000212 // Use ANDPD to simulate FABS.
213 setOperationAction(ISD::FABS , MVT::f64, Custom);
214 setOperationAction(ISD::FABS , MVT::f32, Custom);
215
216 // Use XORP to simulate FNEG.
217 setOperationAction(ISD::FNEG , MVT::f64, Custom);
218 setOperationAction(ISD::FNEG , MVT::f32, Custom);
219
Evan Chengd8fba3a2006-02-02 00:28:23 +0000220 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 setOperationAction(ISD::FSIN , MVT::f64, Expand);
222 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000223 setOperationAction(ISD::FREM , MVT::f64, Expand);
224 setOperationAction(ISD::FSIN , MVT::f32, Expand);
225 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000226 setOperationAction(ISD::FREM , MVT::f32, Expand);
227
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000228 // Expand FP immediates into loads from the stack, except for the special
229 // cases we handle.
230 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
231 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000232 addLegalFPImmediate(+0.0); // xorps / xorpd
233 } else {
234 // Set up the FP register classes.
235 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000236
237 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
238
Chris Lattner76ac0682005-11-15 00:40:23 +0000239 if (!UnsafeFPMath) {
240 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
241 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
242 }
243
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000244 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000245 addLegalFPImmediate(+0.0); // FLD0
246 addLegalFPImmediate(+1.0); // FLD1
247 addLegalFPImmediate(-0.0); // FLD0/FCHS
248 addLegalFPImmediate(-1.0); // FLD1/FCHS
249 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000250
Evan Cheng19264272006-03-01 01:11:20 +0000251 // First set operation action for all vector types to expand. Then we
252 // will selectively turn on ones that can be effectively codegen'd.
253 for (unsigned VT = (unsigned)MVT::Vector + 1;
254 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
255 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000259 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000260 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000261 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000262 }
263
Evan Chengbc047222006-03-22 19:22:18 +0000264 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000265 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
267 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
268
Evan Cheng19264272006-03-01 01:11:20 +0000269 // FIXME: add MMX packed arithmetics
Evan Chengd5e905d2006-03-21 23:01:21 +0000270 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
272 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000273 }
274
Evan Chengbc047222006-03-22 19:22:18 +0000275 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000276 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
277
Evan Chengd5e905d2006-03-21 23:01:21 +0000278 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
279 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
280 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
281 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
Evan Cheng082c8782006-03-24 07:29:27 +0000282 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000283 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000284 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000285 }
286
Evan Chengbc047222006-03-22 19:22:18 +0000287 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000288 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
290 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
291 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
293
294
Evan Chengd5e905d2006-03-21 23:01:21 +0000295 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chengb9b05502006-03-23 01:57:24 +0000296 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
297 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
298 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Chengd5e905d2006-03-21 23:01:21 +0000299 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
Evan Cheng6f7d31e2006-03-25 01:33:37 +0000300 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
301 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
302 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengd5e905d2006-03-21 23:01:21 +0000303 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
304 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chengb9b05502006-03-23 01:57:24 +0000305 setOperationAction(ISD::LOAD, MVT::v16i8, Legal);
306 setOperationAction(ISD::LOAD, MVT::v8i16, Legal);
307 setOperationAction(ISD::LOAD, MVT::v4i32, Legal);
308 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng5df75882006-03-28 00:39:58 +0000309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
310 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Cheng082c8782006-03-24 07:29:27 +0000311 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
312 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
313 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
314 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
315 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000316 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng5df75882006-03-28 00:39:58 +0000317 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
318 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i16, Custom);
319 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
320 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000322 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000323 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000324 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000325 }
326
Evan Cheng78038292006-04-05 23:38:46 +0000327 // We want to custom lower some of our intrinsics.
328 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
329
Chris Lattner76ac0682005-11-15 00:40:23 +0000330 computeRegisterProperties();
331
Evan Cheng6a374562006-02-14 08:25:08 +0000332 // FIXME: These should be based on subtarget info. Plus, the values should
333 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000334 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
335 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
336 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000337 allowUnalignedMemoryAccesses = true; // x86 supports it!
338}
339
340std::vector<SDOperand>
341X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
342 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
343 return LowerFastCCArguments(F, DAG);
344 return LowerCCCArguments(F, DAG);
345}
346
347std::pair<SDOperand, SDOperand>
348X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
349 bool isVarArg, unsigned CallingConv,
350 bool isTailCall,
351 SDOperand Callee, ArgListTy &Args,
352 SelectionDAG &DAG) {
353 assert((!isVarArg || CallingConv == CallingConv::C) &&
354 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000355
356 // If the callee is a GlobalAddress node (quite common, every direct call is)
357 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
358 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
359 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000360 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
361 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000362
Chris Lattner76ac0682005-11-15 00:40:23 +0000363 if (CallingConv == CallingConv::Fast && EnableFastCC)
364 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
365 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
366}
367
368//===----------------------------------------------------------------------===//
369// C Calling Convention implementation
370//===----------------------------------------------------------------------===//
371
372std::vector<SDOperand>
373X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
374 std::vector<SDOperand> ArgValues;
375
376 MachineFunction &MF = DAG.getMachineFunction();
377 MachineFrameInfo *MFI = MF.getFrameInfo();
378
379 // Add DAG nodes to load the arguments... On entry to a function on the X86,
380 // the stack frame looks like this:
381 //
382 // [ESP] -- return address
383 // [ESP + 4] -- first argument (leftmost lexically)
384 // [ESP + 8] -- second argument, if first argument is four bytes in size
385 // ...
386 //
387 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
388 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
389 MVT::ValueType ObjectVT = getValueType(I->getType());
390 unsigned ArgIncrement = 4;
391 unsigned ObjSize;
392 switch (ObjectVT) {
393 default: assert(0 && "Unhandled argument type!");
394 case MVT::i1:
395 case MVT::i8: ObjSize = 1; break;
396 case MVT::i16: ObjSize = 2; break;
397 case MVT::i32: ObjSize = 4; break;
398 case MVT::i64: ObjSize = ArgIncrement = 8; break;
399 case MVT::f32: ObjSize = 4; break;
400 case MVT::f64: ObjSize = ArgIncrement = 8; break;
401 }
402 // Create the frame index object for this incoming parameter...
403 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
404
405 // Create the SelectionDAG nodes corresponding to a load from this parameter
406 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
407
408 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
409 // dead loads.
410 SDOperand ArgValue;
411 if (!I->use_empty())
412 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
413 DAG.getSrcValue(NULL));
414 else {
415 if (MVT::isInteger(ObjectVT))
416 ArgValue = DAG.getConstant(0, ObjectVT);
417 else
418 ArgValue = DAG.getConstantFP(0, ObjectVT);
419 }
420 ArgValues.push_back(ArgValue);
421
422 ArgOffset += ArgIncrement; // Move on to the next argument...
423 }
424
425 // If the function takes variable number of arguments, make a frame index for
426 // the start of the first vararg value... for expansion of llvm.va_start.
427 if (F.isVarArg())
428 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
429 ReturnAddrIndex = 0; // No return address slot generated yet.
430 BytesToPopOnReturn = 0; // Callee pops nothing.
431 BytesCallerReserves = ArgOffset;
432
433 // Finally, inform the code generator which regs we return values in.
434 switch (getValueType(F.getReturnType())) {
435 default: assert(0 && "Unknown type!");
436 case MVT::isVoid: break;
437 case MVT::i1:
438 case MVT::i8:
439 case MVT::i16:
440 case MVT::i32:
441 MF.addLiveOut(X86::EAX);
442 break;
443 case MVT::i64:
444 MF.addLiveOut(X86::EAX);
445 MF.addLiveOut(X86::EDX);
446 break;
447 case MVT::f32:
448 case MVT::f64:
449 MF.addLiveOut(X86::ST0);
450 break;
451 }
452 return ArgValues;
453}
454
455std::pair<SDOperand, SDOperand>
456X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
457 bool isVarArg, bool isTailCall,
458 SDOperand Callee, ArgListTy &Args,
459 SelectionDAG &DAG) {
460 // Count how many bytes are to be pushed on the stack.
461 unsigned NumBytes = 0;
462
463 if (Args.empty()) {
464 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000465 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000466 } else {
467 for (unsigned i = 0, e = Args.size(); i != e; ++i)
468 switch (getValueType(Args[i].second)) {
469 default: assert(0 && "Unknown value type!");
470 case MVT::i1:
471 case MVT::i8:
472 case MVT::i16:
473 case MVT::i32:
474 case MVT::f32:
475 NumBytes += 4;
476 break;
477 case MVT::i64:
478 case MVT::f64:
479 NumBytes += 8;
480 break;
481 }
482
Chris Lattner62c34842006-02-13 09:00:43 +0000483 Chain = DAG.getCALLSEQ_START(Chain,
484 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000485
486 // Arguments go on the stack in reverse order, as specified by the ABI.
487 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000488 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000489 std::vector<SDOperand> Stores;
490
491 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
492 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
493 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
494
495 switch (getValueType(Args[i].second)) {
496 default: assert(0 && "Unexpected ValueType for argument!");
497 case MVT::i1:
498 case MVT::i8:
499 case MVT::i16:
500 // Promote the integer to 32 bits. If the input type is signed use a
501 // sign extend, otherwise use a zero extend.
502 if (Args[i].second->isSigned())
503 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
504 else
505 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
506
507 // FALL THROUGH
508 case MVT::i32:
509 case MVT::f32:
510 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
511 Args[i].first, PtrOff,
512 DAG.getSrcValue(NULL)));
513 ArgOffset += 4;
514 break;
515 case MVT::i64:
516 case MVT::f64:
517 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
518 Args[i].first, PtrOff,
519 DAG.getSrcValue(NULL)));
520 ArgOffset += 8;
521 break;
522 }
523 }
524 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
525 }
526
527 std::vector<MVT::ValueType> RetVals;
528 MVT::ValueType RetTyVT = getValueType(RetTy);
529 RetVals.push_back(MVT::Other);
530
531 // The result values produced have to be legal. Promote the result.
532 switch (RetTyVT) {
533 case MVT::isVoid: break;
534 default:
535 RetVals.push_back(RetTyVT);
536 break;
537 case MVT::i1:
538 case MVT::i8:
539 case MVT::i16:
540 RetVals.push_back(MVT::i32);
541 break;
542 case MVT::f32:
543 if (X86ScalarSSE)
544 RetVals.push_back(MVT::f32);
545 else
546 RetVals.push_back(MVT::f64);
547 break;
548 case MVT::i64:
549 RetVals.push_back(MVT::i32);
550 RetVals.push_back(MVT::i32);
551 break;
552 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000553
Nate Begeman7e5496d2006-02-17 00:03:04 +0000554 std::vector<MVT::ValueType> NodeTys;
555 NodeTys.push_back(MVT::Other); // Returns a chain
556 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
557 std::vector<SDOperand> Ops;
558 Ops.push_back(Chain);
559 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000560
Nate Begeman7e5496d2006-02-17 00:03:04 +0000561 // FIXME: Do not generate X86ISD::TAILCALL for now.
562 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
563 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000564
Nate Begeman7e5496d2006-02-17 00:03:04 +0000565 NodeTys.clear();
566 NodeTys.push_back(MVT::Other); // Returns a chain
567 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
568 Ops.clear();
569 Ops.push_back(Chain);
570 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
571 Ops.push_back(DAG.getConstant(0, getPointerTy()));
572 Ops.push_back(InFlag);
573 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
574 InFlag = Chain.getValue(1);
575
576 SDOperand RetVal;
577 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000578 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000579 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000580 case MVT::i1:
581 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000582 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
583 Chain = RetVal.getValue(1);
584 if (RetTyVT == MVT::i1)
585 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
586 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000587 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000588 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
589 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000590 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000591 case MVT::i32:
592 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
593 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000594 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000595 case MVT::i64: {
596 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
597 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
598 Lo.getValue(2));
599 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
600 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000601 break;
602 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000603 case MVT::f32:
604 case MVT::f64: {
605 std::vector<MVT::ValueType> Tys;
606 Tys.push_back(MVT::f64);
607 Tys.push_back(MVT::Other);
608 Tys.push_back(MVT::Flag);
609 std::vector<SDOperand> Ops;
610 Ops.push_back(Chain);
611 Ops.push_back(InFlag);
612 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
613 Chain = RetVal.getValue(1);
614 InFlag = RetVal.getValue(2);
615 if (X86ScalarSSE) {
616 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
617 // shouldn't be necessary except that RFP cannot be live across
618 // multiple blocks. When stackifier is fixed, they can be uncoupled.
619 MachineFunction &MF = DAG.getMachineFunction();
620 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
621 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
622 Tys.clear();
623 Tys.push_back(MVT::Other);
624 Ops.clear();
625 Ops.push_back(Chain);
626 Ops.push_back(RetVal);
627 Ops.push_back(StackSlot);
628 Ops.push_back(DAG.getValueType(RetTyVT));
629 Ops.push_back(InFlag);
630 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
631 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
632 DAG.getSrcValue(NULL));
633 Chain = RetVal.getValue(1);
634 }
Evan Cheng45e190982006-01-05 00:27:02 +0000635
Nate Begeman7e5496d2006-02-17 00:03:04 +0000636 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
637 // FIXME: we would really like to remember that this FP_ROUND
638 // operation is okay to eliminate if we allow excess FP precision.
639 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
640 break;
641 }
642 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000643 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000644
645 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000646}
647
Chris Lattner76ac0682005-11-15 00:40:23 +0000648//===----------------------------------------------------------------------===//
649// Fast Calling Convention implementation
650//===----------------------------------------------------------------------===//
651//
652// The X86 'fast' calling convention passes up to two integer arguments in
653// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
654// and requires that the callee pop its arguments off the stack (allowing proper
655// tail calls), and has the same return value conventions as C calling convs.
656//
657// This calling convention always arranges for the callee pop value to be 8n+4
658// bytes, which is needed for tail recursion elimination and stack alignment
659// reasons.
660//
661// Note that this can be enhanced in the future to pass fp vals in registers
662// (when we have a global fp allocator) and do other tricks.
663//
664
665/// AddLiveIn - This helper function adds the specified physical register to the
666/// MachineFunction as a live in value. It also creates a corresponding virtual
667/// register for it.
668static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
669 TargetRegisterClass *RC) {
670 assert(RC->contains(PReg) && "Not the correct regclass!");
671 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
672 MF.addLiveIn(PReg, VReg);
673 return VReg;
674}
675
Chris Lattner388fc4d2006-03-17 17:27:47 +0000676// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
677// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
678// EDX". Anything more is illegal.
679//
680// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000681// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000682// a physreg with a virtreg, this increases the size of the physreg's live
683// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000684// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000685// allocator to wedge itself.
686//
687// This code triggers this problem more often if we pass args in registers,
688// so disable it until this is fixed.
689//
690// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
691// about code being dead.
692//
693static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000694
Chris Lattner76ac0682005-11-15 00:40:23 +0000695
696std::vector<SDOperand>
697X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
698 std::vector<SDOperand> ArgValues;
699
700 MachineFunction &MF = DAG.getMachineFunction();
701 MachineFrameInfo *MFI = MF.getFrameInfo();
702
703 // Add DAG nodes to load the arguments... On entry to a function the stack
704 // frame looks like this:
705 //
706 // [ESP] -- return address
707 // [ESP + 4] -- first nonreg argument (leftmost lexically)
708 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
709 // ...
710 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
711
712 // Keep track of the number of integer regs passed so far. This can be either
713 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
714 // used).
715 unsigned NumIntRegs = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000716
Chris Lattner76ac0682005-11-15 00:40:23 +0000717 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
718 MVT::ValueType ObjectVT = getValueType(I->getType());
719 unsigned ArgIncrement = 4;
720 unsigned ObjSize = 0;
721 SDOperand ArgValue;
722
723 switch (ObjectVT) {
724 default: assert(0 && "Unhandled argument type!");
725 case MVT::i1:
726 case MVT::i8:
Chris Lattner43798852006-03-17 05:10:20 +0000727 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000728 if (!I->use_empty()) {
729 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
730 X86::R8RegisterClass);
731 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
732 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000733 if (ObjectVT == MVT::i1)
734 // FIXME: Should insert a assertzext here.
735 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000736 }
737 ++NumIntRegs;
738 break;
739 }
740
741 ObjSize = 1;
742 break;
743 case MVT::i16:
Chris Lattner43798852006-03-17 05:10:20 +0000744 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000745 if (!I->use_empty()) {
746 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
747 X86::R16RegisterClass);
748 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
749 DAG.setRoot(ArgValue.getValue(1));
750 }
751 ++NumIntRegs;
752 break;
753 }
754 ObjSize = 2;
755 break;
756 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000757 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000758 if (!I->use_empty()) {
Chris Lattner43798852006-03-17 05:10:20 +0000759 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattner76ac0682005-11-15 00:40:23 +0000760 X86::R32RegisterClass);
761 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
762 DAG.setRoot(ArgValue.getValue(1));
763 }
764 ++NumIntRegs;
765 break;
766 }
767 ObjSize = 4;
768 break;
769 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000770 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000771 if (!I->use_empty()) {
772 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
773 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
774
775 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
776 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
777 DAG.setRoot(Hi.getValue(1));
778
779 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
780 }
Chris Lattner43798852006-03-17 05:10:20 +0000781 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000782 break;
Chris Lattner43798852006-03-17 05:10:20 +0000783 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000784 if (!I->use_empty()) {
785 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
786 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
787 DAG.setRoot(Low.getValue(1));
788
789 // Load the high part from memory.
790 // Create the frame index object for this incoming parameter...
791 int FI = MFI->CreateFixedObject(4, ArgOffset);
792 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
793 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
794 DAG.getSrcValue(NULL));
795 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
796 }
797 ArgOffset += 4;
Chris Lattner43798852006-03-17 05:10:20 +0000798 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000799 break;
800 }
801 ObjSize = ArgIncrement = 8;
802 break;
803 case MVT::f32: ObjSize = 4; break;
804 case MVT::f64: ObjSize = ArgIncrement = 8; break;
805 }
806
807 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
808 // dead loads.
809 if (ObjSize && !I->use_empty()) {
810 // Create the frame index object for this incoming parameter...
811 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
812
813 // Create the SelectionDAG nodes corresponding to a load from this
814 // parameter.
815 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
816
817 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
818 DAG.getSrcValue(NULL));
819 } else if (ArgValue.Val == 0) {
820 if (MVT::isInteger(ObjectVT))
821 ArgValue = DAG.getConstant(0, ObjectVT);
822 else
823 ArgValue = DAG.getConstantFP(0, ObjectVT);
824 }
825 ArgValues.push_back(ArgValue);
826
827 if (ObjSize)
828 ArgOffset += ArgIncrement; // Move on to the next argument.
829 }
830
831 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
832 // arguments and the arguments after the retaddr has been pushed are aligned.
833 if ((ArgOffset & 7) == 0)
834 ArgOffset += 4;
835
836 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
837 ReturnAddrIndex = 0; // No return address slot generated yet.
838 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
839 BytesCallerReserves = 0;
840
841 // Finally, inform the code generator which regs we return values in.
842 switch (getValueType(F.getReturnType())) {
843 default: assert(0 && "Unknown type!");
844 case MVT::isVoid: break;
845 case MVT::i1:
846 case MVT::i8:
847 case MVT::i16:
848 case MVT::i32:
849 MF.addLiveOut(X86::EAX);
850 break;
851 case MVT::i64:
852 MF.addLiveOut(X86::EAX);
853 MF.addLiveOut(X86::EDX);
854 break;
855 case MVT::f32:
856 case MVT::f64:
857 MF.addLiveOut(X86::ST0);
858 break;
859 }
860 return ArgValues;
861}
862
863std::pair<SDOperand, SDOperand>
864X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
865 bool isTailCall, SDOperand Callee,
866 ArgListTy &Args, SelectionDAG &DAG) {
867 // Count how many bytes are to be pushed on the stack.
868 unsigned NumBytes = 0;
869
870 // Keep track of the number of integer regs passed so far. This can be either
871 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
872 // used).
873 unsigned NumIntRegs = 0;
874
875 for (unsigned i = 0, e = Args.size(); i != e; ++i)
876 switch (getValueType(Args[i].second)) {
877 default: assert(0 && "Unknown value type!");
878 case MVT::i1:
879 case MVT::i8:
880 case MVT::i16:
881 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000882 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000883 ++NumIntRegs;
884 break;
885 }
886 // fall through
887 case MVT::f32:
888 NumBytes += 4;
889 break;
890 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000891 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
892 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000893 break;
Chris Lattner43798852006-03-17 05:10:20 +0000894 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
895 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000896 NumBytes += 4;
897 break;
898 }
899
900 // fall through
901 case MVT::f64:
902 NumBytes += 8;
903 break;
904 }
905
906 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
907 // arguments and the arguments after the retaddr has been pushed are aligned.
908 if ((NumBytes & 7) == 0)
909 NumBytes += 4;
910
Chris Lattner62c34842006-02-13 09:00:43 +0000911 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000912
913 // Arguments go on the stack in reverse order, as specified by the ABI.
914 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000915 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000916 NumIntRegs = 0;
917 std::vector<SDOperand> Stores;
918 std::vector<SDOperand> RegValuesToPass;
919 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
920 switch (getValueType(Args[i].second)) {
921 default: assert(0 && "Unexpected ValueType for argument!");
922 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000923 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
924 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000925 case MVT::i8:
926 case MVT::i16:
927 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000928 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000929 RegValuesToPass.push_back(Args[i].first);
930 ++NumIntRegs;
931 break;
932 }
933 // Fall through
934 case MVT::f32: {
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 Args[i].first, PtrOff,
939 DAG.getSrcValue(NULL)));
940 ArgOffset += 4;
941 break;
942 }
943 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000944 // Can pass (at least) part of it in regs?
945 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000946 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
947 Args[i].first, DAG.getConstant(1, MVT::i32));
948 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
949 Args[i].first, DAG.getConstant(0, MVT::i32));
950 RegValuesToPass.push_back(Lo);
951 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +0000952
953 // Pass both parts in regs?
954 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000955 RegValuesToPass.push_back(Hi);
956 ++NumIntRegs;
957 } else {
958 // Pass the high part in memory.
959 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
960 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
961 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
962 Hi, PtrOff, DAG.getSrcValue(NULL)));
963 ArgOffset += 4;
964 }
965 break;
966 }
967 // Fall through
968 case MVT::f64:
969 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
970 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
971 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
972 Args[i].first, PtrOff,
973 DAG.getSrcValue(NULL)));
974 ArgOffset += 8;
975 break;
976 }
977 }
978 if (!Stores.empty())
979 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
980
981 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
982 // arguments and the arguments after the retaddr has been pushed are aligned.
983 if ((ArgOffset & 7) == 0)
984 ArgOffset += 4;
985
986 std::vector<MVT::ValueType> RetVals;
987 MVT::ValueType RetTyVT = getValueType(RetTy);
988
989 RetVals.push_back(MVT::Other);
990
991 // The result values produced have to be legal. Promote the result.
992 switch (RetTyVT) {
993 case MVT::isVoid: break;
994 default:
995 RetVals.push_back(RetTyVT);
996 break;
997 case MVT::i1:
998 case MVT::i8:
999 case MVT::i16:
1000 RetVals.push_back(MVT::i32);
1001 break;
1002 case MVT::f32:
1003 if (X86ScalarSSE)
1004 RetVals.push_back(MVT::f32);
1005 else
1006 RetVals.push_back(MVT::f64);
1007 break;
1008 case MVT::i64:
1009 RetVals.push_back(MVT::i32);
1010 RetVals.push_back(MVT::i32);
1011 break;
1012 }
1013
Nate Begeman7e5496d2006-02-17 00:03:04 +00001014 // Build a sequence of copy-to-reg nodes chained together with token chain
1015 // and flag operands which copy the outgoing args into registers.
1016 SDOperand InFlag;
1017 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1018 unsigned CCReg;
1019 SDOperand RegToPass = RegValuesToPass[i];
1020 switch (RegToPass.getValueType()) {
1021 default: assert(0 && "Bad thing to pass in regs");
1022 case MVT::i8:
1023 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001024 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001025 case MVT::i16:
1026 CCReg = (i == 0) ? X86::AX : X86::DX;
1027 break;
1028 case MVT::i32:
1029 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1030 break;
1031 }
1032
1033 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1034 InFlag = Chain.getValue(1);
1035 }
1036
1037 std::vector<MVT::ValueType> NodeTys;
1038 NodeTys.push_back(MVT::Other); // Returns a chain
1039 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1040 std::vector<SDOperand> Ops;
1041 Ops.push_back(Chain);
1042 Ops.push_back(Callee);
1043 if (InFlag.Val)
1044 Ops.push_back(InFlag);
1045
1046 // FIXME: Do not generate X86ISD::TAILCALL for now.
1047 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1048 InFlag = Chain.getValue(1);
1049
1050 NodeTys.clear();
1051 NodeTys.push_back(MVT::Other); // Returns a chain
1052 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1053 Ops.clear();
1054 Ops.push_back(Chain);
1055 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1056 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1057 Ops.push_back(InFlag);
1058 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1059 InFlag = Chain.getValue(1);
1060
1061 SDOperand RetVal;
1062 if (RetTyVT != MVT::isVoid) {
1063 switch (RetTyVT) {
1064 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001065 case MVT::i1:
1066 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001067 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1068 Chain = RetVal.getValue(1);
1069 if (RetTyVT == MVT::i1)
1070 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1071 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001072 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001073 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1074 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001075 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001076 case MVT::i32:
1077 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1078 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001079 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001080 case MVT::i64: {
1081 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1082 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1083 Lo.getValue(2));
1084 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1085 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001086 break;
1087 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001088 case MVT::f32:
1089 case MVT::f64: {
1090 std::vector<MVT::ValueType> Tys;
1091 Tys.push_back(MVT::f64);
1092 Tys.push_back(MVT::Other);
1093 Tys.push_back(MVT::Flag);
1094 std::vector<SDOperand> Ops;
1095 Ops.push_back(Chain);
1096 Ops.push_back(InFlag);
1097 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1098 Chain = RetVal.getValue(1);
1099 InFlag = RetVal.getValue(2);
1100 if (X86ScalarSSE) {
1101 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1102 // shouldn't be necessary except that RFP cannot be live across
1103 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1104 MachineFunction &MF = DAG.getMachineFunction();
1105 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1106 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1107 Tys.clear();
1108 Tys.push_back(MVT::Other);
1109 Ops.clear();
1110 Ops.push_back(Chain);
1111 Ops.push_back(RetVal);
1112 Ops.push_back(StackSlot);
1113 Ops.push_back(DAG.getValueType(RetTyVT));
1114 Ops.push_back(InFlag);
1115 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1116 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1117 DAG.getSrcValue(NULL));
1118 Chain = RetVal.getValue(1);
1119 }
Evan Cheng172fce72006-01-06 00:43:03 +00001120
Nate Begeman7e5496d2006-02-17 00:03:04 +00001121 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1122 // FIXME: we would really like to remember that this FP_ROUND
1123 // operation is okay to eliminate if we allow excess FP precision.
1124 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1125 break;
1126 }
1127 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001128 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001129
1130 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001131}
1132
1133SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1134 if (ReturnAddrIndex == 0) {
1135 // Set up a frame object for the return address.
1136 MachineFunction &MF = DAG.getMachineFunction();
1137 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1138 }
1139
1140 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1141}
1142
1143
1144
1145std::pair<SDOperand, SDOperand> X86TargetLowering::
1146LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1147 SelectionDAG &DAG) {
1148 SDOperand Result;
1149 if (Depth) // Depths > 0 not supported yet!
1150 Result = DAG.getConstant(0, getPointerTy());
1151 else {
1152 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1153 if (!isFrameAddress)
1154 // Just load the return address
1155 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1156 DAG.getSrcValue(NULL));
1157 else
1158 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1159 DAG.getConstant(4, MVT::i32));
1160 }
1161 return std::make_pair(Result, Chain);
1162}
1163
Evan Cheng339edad2006-01-11 00:33:36 +00001164/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1165/// which corresponds to the condition code.
1166static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1167 switch (X86CC) {
1168 default: assert(0 && "Unknown X86 conditional code!");
1169 case X86ISD::COND_A: return X86::JA;
1170 case X86ISD::COND_AE: return X86::JAE;
1171 case X86ISD::COND_B: return X86::JB;
1172 case X86ISD::COND_BE: return X86::JBE;
1173 case X86ISD::COND_E: return X86::JE;
1174 case X86ISD::COND_G: return X86::JG;
1175 case X86ISD::COND_GE: return X86::JGE;
1176 case X86ISD::COND_L: return X86::JL;
1177 case X86ISD::COND_LE: return X86::JLE;
1178 case X86ISD::COND_NE: return X86::JNE;
1179 case X86ISD::COND_NO: return X86::JNO;
1180 case X86ISD::COND_NP: return X86::JNP;
1181 case X86ISD::COND_NS: return X86::JNS;
1182 case X86ISD::COND_O: return X86::JO;
1183 case X86ISD::COND_P: return X86::JP;
1184 case X86ISD::COND_S: return X86::JS;
1185 }
1186}
Chris Lattner76ac0682005-11-15 00:40:23 +00001187
Evan Cheng45df7f82006-01-30 23:41:35 +00001188/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1189/// specific condition code. It returns a false if it cannot do a direct
1190/// translation. X86CC is the translated CondCode. Flip is set to true if the
1191/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001192static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1193 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001194 Flip = false;
1195 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001196 if (!isFP) {
1197 switch (SetCCOpcode) {
1198 default: break;
1199 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1200 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1201 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1202 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1203 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1204 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1205 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1206 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1207 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1208 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1209 }
1210 } else {
1211 // On a floating point condition, the flags are set as follows:
1212 // ZF PF CF op
1213 // 0 | 0 | 0 | X > Y
1214 // 0 | 0 | 1 | X < Y
1215 // 1 | 0 | 0 | X == Y
1216 // 1 | 1 | 1 | unordered
1217 switch (SetCCOpcode) {
1218 default: break;
1219 case ISD::SETUEQ:
1220 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001221 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001222 case ISD::SETOGT:
1223 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001224 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001225 case ISD::SETOGE:
1226 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001227 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001228 case ISD::SETULT:
1229 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001230 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001231 case ISD::SETULE:
1232 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1233 case ISD::SETONE:
1234 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1235 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1236 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1237 }
1238 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001239
1240 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001241}
1242
Evan Cheng78038292006-04-05 23:38:46 +00001243static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1244 bool &Flip) {
1245 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1246}
1247
Evan Cheng339edad2006-01-11 00:33:36 +00001248/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1249/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001250/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001251static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001252 switch (X86CC) {
1253 default:
1254 return false;
1255 case X86ISD::COND_B:
1256 case X86ISD::COND_BE:
1257 case X86ISD::COND_E:
1258 case X86ISD::COND_P:
1259 case X86ISD::COND_A:
1260 case X86ISD::COND_AE:
1261 case X86ISD::COND_NE:
1262 case X86ISD::COND_NP:
1263 return true;
1264 }
1265}
1266
Evan Cheng339edad2006-01-11 00:33:36 +00001267MachineBasicBlock *
1268X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1269 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001270 switch (MI->getOpcode()) {
1271 default: assert(false && "Unexpected instr type to insert");
1272 case X86::CMOV_FR32:
1273 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001274 // To "insert" a SELECT_CC instruction, we actually have to insert the
1275 // diamond control-flow pattern. The incoming instruction knows the
1276 // destination vreg to set, the condition code register to branch on, the
1277 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001278 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1279 ilist<MachineBasicBlock>::iterator It = BB;
1280 ++It;
1281
1282 // thisMBB:
1283 // ...
1284 // TrueVal = ...
1285 // cmpTY ccX, r1, r2
1286 // bCC copy1MBB
1287 // fallthrough --> copy0MBB
1288 MachineBasicBlock *thisMBB = BB;
1289 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1290 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1291 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1292 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1293 MachineFunction *F = BB->getParent();
1294 F->getBasicBlockList().insert(It, copy0MBB);
1295 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001296 // Update machine-CFG edges by first adding all successors of the current
1297 // block to the new block which will contain the Phi node for the select.
1298 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1299 e = BB->succ_end(); i != e; ++i)
1300 sinkMBB->addSuccessor(*i);
1301 // Next, remove all successors of the current block, and add the true
1302 // and fallthrough blocks as its successors.
1303 while(!BB->succ_empty())
1304 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001305 BB->addSuccessor(copy0MBB);
1306 BB->addSuccessor(sinkMBB);
1307
1308 // copy0MBB:
1309 // %FalseValue = ...
1310 // # fallthrough to sinkMBB
1311 BB = copy0MBB;
1312
1313 // Update machine-CFG edges
1314 BB->addSuccessor(sinkMBB);
1315
1316 // sinkMBB:
1317 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1318 // ...
1319 BB = sinkMBB;
1320 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1321 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1322 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001323
Evan Cheng911c68d2006-01-16 21:21:29 +00001324 delete MI; // The pseudo instruction is gone now.
1325 return BB;
1326 }
Evan Cheng339edad2006-01-11 00:33:36 +00001327
Evan Cheng911c68d2006-01-16 21:21:29 +00001328 case X86::FP_TO_INT16_IN_MEM:
1329 case X86::FP_TO_INT32_IN_MEM:
1330 case X86::FP_TO_INT64_IN_MEM: {
1331 // Change the floating point control register to use "round towards zero"
1332 // mode when truncating to an integer value.
1333 MachineFunction *F = BB->getParent();
1334 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1335 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1336
1337 // Load the old value of the high byte of the control word...
1338 unsigned OldCW =
1339 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1340 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1341
1342 // Set the high part to be round to zero...
1343 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1344
1345 // Reload the modified control word now...
1346 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1347
1348 // Restore the memory image of control word to original value
1349 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1350
1351 // Get the X86 opcode to use.
1352 unsigned Opc;
1353 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001354 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001355 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1356 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1357 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1358 }
1359
1360 X86AddressMode AM;
1361 MachineOperand &Op = MI->getOperand(0);
1362 if (Op.isRegister()) {
1363 AM.BaseType = X86AddressMode::RegBase;
1364 AM.Base.Reg = Op.getReg();
1365 } else {
1366 AM.BaseType = X86AddressMode::FrameIndexBase;
1367 AM.Base.FrameIndex = Op.getFrameIndex();
1368 }
1369 Op = MI->getOperand(1);
1370 if (Op.isImmediate())
1371 AM.Scale = Op.getImmedValue();
1372 Op = MI->getOperand(2);
1373 if (Op.isImmediate())
1374 AM.IndexReg = Op.getImmedValue();
1375 Op = MI->getOperand(3);
1376 if (Op.isGlobalAddress()) {
1377 AM.GV = Op.getGlobal();
1378 } else {
1379 AM.Disp = Op.getImmedValue();
1380 }
1381 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1382
1383 // Reload the original control word now.
1384 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1385
1386 delete MI; // The pseudo instruction is gone now.
1387 return BB;
1388 }
1389 }
Evan Cheng339edad2006-01-11 00:33:36 +00001390}
1391
1392
1393//===----------------------------------------------------------------------===//
1394// X86 Custom Lowering Hooks
1395//===----------------------------------------------------------------------===//
1396
Evan Chengaf598d22006-03-13 23:18:16 +00001397/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1398/// load. For Darwin, external and weak symbols are indirect, loading the value
1399/// at address GV rather then the value of GV itself. This means that the
1400/// GlobalAddress must be in the base or index register of the address, not the
1401/// GV offset field.
1402static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1403 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1404 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1405}
1406
Evan Chengc995b452006-04-06 23:23:56 +00001407/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1408/// true if Op is undef or if its value falls within the specified range (L, H).
1409static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1410 if (Op.getOpcode() == ISD::UNDEF)
1411 return true;
1412
1413 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1414 return (Val >= Low && Val <= Hi);
1415}
1416
Evan Cheng68ad48b2006-03-22 18:59:22 +00001417/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1418/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1419bool X86::isPSHUFDMask(SDNode *N) {
1420 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1421
1422 if (N->getNumOperands() != 4)
1423 return false;
1424
1425 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001426 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001427 SDOperand Arg = N->getOperand(i);
1428 if (Arg.getOpcode() == ISD::UNDEF) continue;
1429 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1430 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001431 return false;
1432 }
1433
1434 return true;
1435}
1436
1437/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001438/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001439bool X86::isPSHUFHWMask(SDNode *N) {
1440 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1441
1442 if (N->getNumOperands() != 8)
1443 return false;
1444
1445 // Lower quadword copied in order.
1446 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001447 SDOperand Arg = N->getOperand(i);
1448 if (Arg.getOpcode() == ISD::UNDEF) continue;
1449 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1450 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001451 return false;
1452 }
1453
1454 // Upper quadword shuffled.
1455 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001456 SDOperand Arg = N->getOperand(i);
1457 if (Arg.getOpcode() == ISD::UNDEF) continue;
1458 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1459 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001460 if (Val < 4 || Val > 7)
1461 return false;
1462 }
1463
1464 return true;
1465}
1466
1467/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001468/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001469bool X86::isPSHUFLWMask(SDNode *N) {
1470 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1471
1472 if (N->getNumOperands() != 8)
1473 return false;
1474
1475 // Upper quadword copied in order.
1476 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001477 SDOperand Arg = N->getOperand(i);
1478 if (Arg.getOpcode() == ISD::UNDEF) continue;
1479 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1480 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001481 return false;
1482 }
1483
1484 // Lower quadword shuffled.
1485 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001486 SDOperand Arg = N->getOperand(i);
1487 if (Arg.getOpcode() == ISD::UNDEF) continue;
1488 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1489 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001490 if (Val > 4)
1491 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001492 }
1493
1494 return true;
1495}
1496
Evan Chengd27fb3e2006-03-24 01:18:28 +00001497/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1498/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1499bool X86::isSHUFPMask(SDNode *N) {
1500 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1501
Evan Chenge7ee6a52006-03-24 23:15:12 +00001502 unsigned NumElems = N->getNumOperands();
1503 if (NumElems == 2) {
Evan Chengc995b452006-04-06 23:23:56 +00001504 // The only cases that ought be handled by SHUFPD is
Evan Cheng2595a682006-03-24 02:58:06 +00001505 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
Evan Chengc995b452006-04-06 23:23:56 +00001506 // Dest { 3, 0 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
Evan Cheng2595a682006-03-24 02:58:06 +00001507 // Expect bit 0 == 1, bit1 == 2
1508 SDOperand Bit0 = N->getOperand(0);
1509 SDOperand Bit1 = N->getOperand(1);
Evan Chengc995b452006-04-06 23:23:56 +00001510 if (isUndefOrInRange(Bit0, 0, 0) && isUndefOrInRange(Bit1, 3, 3))
1511 return true;
1512 if (isUndefOrInRange(Bit0, 1, 1) && isUndefOrInRange(Bit1, 2, 2))
1513 return true;
1514 return false;
Evan Cheng2595a682006-03-24 02:58:06 +00001515 }
1516
Evan Chenge7ee6a52006-03-24 23:15:12 +00001517 if (NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001518
1519 // Each half must refer to only one of the vector.
Evan Cheng7e2ff112006-03-30 19:54:57 +00001520 for (unsigned i = 0; i < 2; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001521 SDOperand Arg = N->getOperand(i);
1522 if (Arg.getOpcode() == ISD::UNDEF) continue;
1523 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1524 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7e2ff112006-03-30 19:54:57 +00001525 if (Val >= 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001526 }
Evan Cheng7e2ff112006-03-30 19:54:57 +00001527 for (unsigned i = 2; i < 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001528 SDOperand Arg = N->getOperand(i);
1529 if (Arg.getOpcode() == ISD::UNDEF) continue;
1530 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1531 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7e2ff112006-03-30 19:54:57 +00001532 if (Val < 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001533 }
1534
1535 return true;
1536}
1537
Evan Cheng2595a682006-03-24 02:58:06 +00001538/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1539/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1540bool X86::isMOVHLPSMask(SDNode *N) {
1541 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1542
Evan Cheng1a194a52006-03-28 06:50:32 +00001543 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001544 return false;
1545
Evan Cheng1a194a52006-03-28 06:50:32 +00001546 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Cheng2595a682006-03-24 02:58:06 +00001547 SDOperand Bit0 = N->getOperand(0);
1548 SDOperand Bit1 = N->getOperand(1);
Evan Cheng1a194a52006-03-28 06:50:32 +00001549 SDOperand Bit2 = N->getOperand(2);
1550 SDOperand Bit3 = N->getOperand(3);
Evan Cheng99d72052006-03-31 00:30:29 +00001551
1552 if (Bit0.getOpcode() != ISD::UNDEF) {
1553 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1554 if (cast<ConstantSDNode>(Bit0)->getValue() != 6)
1555 return false;
1556 }
1557
1558 if (Bit1.getOpcode() != ISD::UNDEF) {
1559 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1560 if (cast<ConstantSDNode>(Bit1)->getValue() != 7)
1561 return false;
1562 }
1563
1564 if (Bit2.getOpcode() != ISD::UNDEF) {
1565 assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1566 if (cast<ConstantSDNode>(Bit2)->getValue() != 2)
1567 return false;
1568 }
1569
1570 if (Bit3.getOpcode() != ISD::UNDEF) {
1571 assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1572 if (cast<ConstantSDNode>(Bit3)->getValue() != 3)
1573 return false;
1574 }
1575
1576 return true;
Evan Cheng1a194a52006-03-28 06:50:32 +00001577}
1578
1579/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
1580/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1581bool X86::isMOVLHPSMask(SDNode *N) {
1582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1583
1584 if (N->getNumOperands() != 4)
1585 return false;
1586
1587 // Expect bit0 == 0, bit1 == 1, bit2 == 4, bit3 == 5
1588 SDOperand Bit0 = N->getOperand(0);
1589 SDOperand Bit1 = N->getOperand(1);
1590 SDOperand Bit2 = N->getOperand(2);
1591 SDOperand Bit3 = N->getOperand(3);
Evan Cheng99d72052006-03-31 00:30:29 +00001592
1593 if (Bit0.getOpcode() != ISD::UNDEF) {
1594 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1595 if (cast<ConstantSDNode>(Bit0)->getValue() != 0)
1596 return false;
1597 }
1598
1599 if (Bit1.getOpcode() != ISD::UNDEF) {
1600 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1601 if (cast<ConstantSDNode>(Bit1)->getValue() != 1)
1602 return false;
1603 }
1604
1605 if (Bit2.getOpcode() != ISD::UNDEF) {
1606 assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1607 if (cast<ConstantSDNode>(Bit2)->getValue() != 4)
1608 return false;
1609 }
1610
1611 if (Bit3.getOpcode() != ISD::UNDEF) {
1612 assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1613 if (cast<ConstantSDNode>(Bit3)->getValue() != 5)
1614 return false;
1615 }
1616
1617 return true;
Evan Cheng2595a682006-03-24 02:58:06 +00001618}
1619
Evan Chengc995b452006-04-06 23:23:56 +00001620/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1621/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1622bool X86::isMOVLPMask(SDNode *N) {
1623 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1624
1625 unsigned NumElems = N->getNumOperands();
1626 if (NumElems != 2 && NumElems != 4)
1627 return false;
1628
1629 for (unsigned i = 0; i < NumElems/2; ++i) {
1630 SDOperand Arg = N->getOperand(i);
1631 if (Arg.getOpcode() == ISD::UNDEF) continue;
1632 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1633 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1634 if (Val != i + NumElems) return false;
1635 }
1636
1637 for (unsigned i = NumElems/2; i < NumElems; ++i) {
1638 SDOperand Arg = N->getOperand(i);
1639 if (Arg.getOpcode() == ISD::UNDEF) continue;
1640 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1641 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1642 if (Val != i) return false;
1643 }
1644
1645 return true;
1646}
1647
1648/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1649/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}.
1650bool X86::isMOVHPMask(SDNode *N) {
1651 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1652
1653 unsigned NumElems = N->getNumOperands();
1654 if (NumElems != 2 && NumElems != 4)
1655 return false;
1656
1657 for (unsigned i = 0; i < NumElems/2; ++i) {
1658 SDOperand Arg = N->getOperand(i);
1659 if (Arg.getOpcode() == ISD::UNDEF) continue;
1660 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1661 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1662 if (Val != i) return false;
1663 }
1664
1665 for (unsigned i = 0; i < NumElems/2; ++i) {
1666 SDOperand Arg = N->getOperand(i + NumElems/2);
1667 if (Arg.getOpcode() == ISD::UNDEF) continue;
1668 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1669 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1670 if (Val != i + NumElems) return false;
1671 }
1672
1673 return true;
1674}
1675
Evan Cheng5df75882006-03-28 00:39:58 +00001676/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1677/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1678bool X86::isUNPCKLMask(SDNode *N) {
1679 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1680
1681 unsigned NumElems = N->getNumOperands();
1682 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1683 return false;
1684
1685 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1686 SDOperand BitI = N->getOperand(i);
1687 SDOperand BitI1 = N->getOperand(i+1);
Evan Cheng99d72052006-03-31 00:30:29 +00001688
1689 if (BitI.getOpcode() != ISD::UNDEF) {
1690 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1691 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1692 return false;
1693 }
1694
1695 if (BitI1.getOpcode() != ISD::UNDEF) {
1696 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengd9d0bbb2006-03-31 00:33:57 +00001697 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems)
Evan Cheng99d72052006-03-31 00:30:29 +00001698 return false;
1699 }
Evan Cheng5df75882006-03-28 00:39:58 +00001700 }
1701
1702 return true;
1703}
1704
Evan Cheng2bc32802006-03-28 02:43:26 +00001705/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1706/// specifies a shuffle of elements that is suitable for input to UNPCKH.
1707bool X86::isUNPCKHMask(SDNode *N) {
1708 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1709
1710 unsigned NumElems = N->getNumOperands();
1711 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1712 return false;
1713
1714 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1715 SDOperand BitI = N->getOperand(i);
1716 SDOperand BitI1 = N->getOperand(i+1);
Evan Cheng99d72052006-03-31 00:30:29 +00001717
1718 if (BitI.getOpcode() != ISD::UNDEF) {
1719 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1720 if (cast<ConstantSDNode>(BitI)->getValue() != j + NumElems/2)
1721 return false;
1722 }
1723
1724 if (BitI1.getOpcode() != ISD::UNDEF) {
1725 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengd9d0bbb2006-03-31 00:33:57 +00001726 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems/2 + NumElems)
Evan Cheng99d72052006-03-31 00:30:29 +00001727 return false;
1728 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001729 }
1730
1731 return true;
1732}
1733
Evan Chengf3b52c82006-04-05 07:20:06 +00001734/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1735/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1736/// <0, 0, 1, 1>
1737bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1738 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1739
1740 unsigned NumElems = N->getNumOperands();
1741 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1742 return false;
1743
1744 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1745 SDOperand BitI = N->getOperand(i);
1746 SDOperand BitI1 = N->getOperand(i+1);
1747
1748 if (BitI.getOpcode() != ISD::UNDEF) {
1749 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1750 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1751 return false;
1752 }
1753
1754 if (BitI1.getOpcode() != ISD::UNDEF) {
1755 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
1756 if (cast<ConstantSDNode>(BitI1)->getValue() != j)
1757 return false;
1758 }
1759 }
1760
1761 return true;
1762}
1763
1764
Evan Chengd097e672006-03-22 02:53:00 +00001765/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1766/// a splat of a single element.
1767bool X86::isSplatMask(SDNode *N) {
1768 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1769
1770 // We can only splat 64-bit, and 32-bit quantities.
1771 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1772 return false;
1773
1774 // This is a splat operation if each element of the permute is the same, and
1775 // if the value doesn't reference the second vector.
1776 SDOperand Elt = N->getOperand(0);
1777 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1778 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001779 SDOperand Arg = N->getOperand(i);
1780 if (Arg.getOpcode() == ISD::UNDEF) continue;
1781 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1782 if (Arg != Elt) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001783 }
1784
1785 // Make sure it is a splat of the first vector operand.
1786 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1787}
1788
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001789/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1790/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1791/// instructions.
1792unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001793 unsigned NumOperands = N->getNumOperands();
1794 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1795 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001796 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001797 unsigned Val = 0;
1798 SDOperand Arg = N->getOperand(NumOperands-i-1);
1799 if (Arg.getOpcode() != ISD::UNDEF)
1800 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001801 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001802 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001803 if (i != NumOperands - 1)
1804 Mask <<= Shift;
1805 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001806
1807 return Mask;
1808}
1809
Evan Chengb7fedff2006-03-29 23:07:14 +00001810/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1811/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1812/// instructions.
1813unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1814 unsigned Mask = 0;
1815 // 8 nodes, but we only care about the last 4.
1816 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001817 unsigned Val = 0;
1818 SDOperand Arg = N->getOperand(i);
1819 if (Arg.getOpcode() != ISD::UNDEF)
1820 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001821 Mask |= (Val - 4);
1822 if (i != 4)
1823 Mask <<= 2;
1824 }
1825
1826 return Mask;
1827}
1828
1829/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1830/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1831/// instructions.
1832unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1833 unsigned Mask = 0;
1834 // 8 nodes, but we only care about the first 4.
1835 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001836 unsigned Val = 0;
1837 SDOperand Arg = N->getOperand(i);
1838 if (Arg.getOpcode() != ISD::UNDEF)
1839 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001840 Mask |= Val;
1841 if (i != 0)
1842 Mask <<= 2;
1843 }
1844
1845 return Mask;
1846}
1847
Evan Cheng59a63552006-04-05 01:47:37 +00001848/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1849/// specifies a 8 element shuffle that can be broken into a pair of
1850/// PSHUFHW and PSHUFLW.
1851static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1852 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1853
1854 if (N->getNumOperands() != 8)
1855 return false;
1856
1857 // Lower quadword shuffled.
1858 for (unsigned i = 0; i != 4; ++i) {
1859 SDOperand Arg = N->getOperand(i);
1860 if (Arg.getOpcode() == ISD::UNDEF) continue;
1861 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1862 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1863 if (Val > 4)
1864 return false;
1865 }
1866
1867 // Upper quadword shuffled.
1868 for (unsigned i = 4; i != 8; ++i) {
1869 SDOperand Arg = N->getOperand(i);
1870 if (Arg.getOpcode() == ISD::UNDEF) continue;
1871 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1872 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1873 if (Val < 4 || Val > 7)
1874 return false;
1875 }
1876
1877 return true;
1878}
1879
Evan Chengc995b452006-04-06 23:23:56 +00001880/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
1881/// values in ther permute mask.
1882static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1883 SDOperand V1 = Op.getOperand(0);
1884 SDOperand V2 = Op.getOperand(1);
1885 SDOperand Mask = Op.getOperand(2);
1886 MVT::ValueType VT = Op.getValueType();
1887 MVT::ValueType MaskVT = Mask.getValueType();
1888 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
1889 unsigned NumElems = Mask.getNumOperands();
1890 std::vector<SDOperand> MaskVec;
1891
1892 for (unsigned i = 0; i != NumElems; ++i) {
1893 SDOperand Arg = Mask.getOperand(i);
1894 if (Arg.getOpcode() == ISD::UNDEF) continue;
1895 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1896 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1897 if (Val < NumElems)
1898 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
1899 else
1900 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
1901 }
1902
1903 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
1904 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1905}
1906
1907/// isScalarLoadToVector - Returns true if the node is a scalar load that
1908/// is promoted to a vector.
1909static inline bool isScalarLoadToVector(SDOperand Op) {
1910 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1911 Op = Op.getOperand(0);
1912 return (Op.getOpcode() == ISD::LOAD);
1913 }
1914 return false;
1915}
1916
1917/// ShouldXformedToMOVLP - Return true if the node should be transformed to
1918/// match movlp{d|s}. The lower half elements should come from V1 (and in
1919/// order), and the upper half elements should come from the upper half of
1920/// V2 (not necessarily in order). And since V1 will become the source of
1921/// the MOVLP, it must be a scalar load.
1922static bool ShouldXformedToMOVLP(SDOperand V1, SDOperand V2, SDOperand Mask) {
1923 if (isScalarLoadToVector(V1)) {
1924 unsigned NumElems = Mask.getNumOperands();
1925 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
1926 if (!isUndefOrInRange(Mask.getOperand(i), i, i))
1927 return false;
1928 for (unsigned i = NumElems/2; i != NumElems; ++i)
1929 if (!isUndefOrInRange(Mask.getOperand(i),
1930 NumElems+NumElems/2, NumElems*2-1))
1931 return false;
1932 return true;
1933 }
1934
1935 return false;
1936}
1937
1938/// isLowerFromV2UpperFromV1 - Returns true if the shuffle mask is except
1939/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1940/// half elements to come from vector 1 (which would equal the dest.) and
1941/// the upper half to come from vector 2.
1942static bool isLowerFromV2UpperFromV1(SDOperand Op) {
1943 assert(Op.getOpcode() == ISD::BUILD_VECTOR);
1944
1945 unsigned NumElems = Op.getNumOperands();
1946 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
1947 if (!isUndefOrInRange(Op.getOperand(i), NumElems, NumElems*2-1))
1948 return false;
1949 for (unsigned i = NumElems/2; i != NumElems; ++i)
1950 if (!isUndefOrInRange(Op.getOperand(i), 0, NumElems-1))
1951 return false;
1952 return true;
1953}
1954
Chris Lattner76ac0682005-11-15 00:40:23 +00001955/// LowerOperation - Provide custom lowering hooks for some operations.
1956///
1957SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1958 switch (Op.getOpcode()) {
1959 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001960 case ISD::SHL_PARTS:
1961 case ISD::SRA_PARTS:
1962 case ISD::SRL_PARTS: {
1963 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1964 "Not an i64 shift!");
1965 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1966 SDOperand ShOpLo = Op.getOperand(0);
1967 SDOperand ShOpHi = Op.getOperand(1);
1968 SDOperand ShAmt = Op.getOperand(2);
1969 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001970 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001971 : DAG.getConstant(0, MVT::i32);
1972
1973 SDOperand Tmp2, Tmp3;
1974 if (Op.getOpcode() == ISD::SHL_PARTS) {
1975 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1976 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1977 } else {
1978 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001979 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001980 }
1981
1982 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1983 ShAmt, DAG.getConstant(32, MVT::i8));
1984
1985 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001986 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001987
1988 std::vector<MVT::ValueType> Tys;
1989 Tys.push_back(MVT::i32);
1990 Tys.push_back(MVT::Flag);
1991 std::vector<SDOperand> Ops;
1992 if (Op.getOpcode() == ISD::SHL_PARTS) {
1993 Ops.push_back(Tmp2);
1994 Ops.push_back(Tmp3);
1995 Ops.push_back(CC);
1996 Ops.push_back(InFlag);
1997 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1998 InFlag = Hi.getValue(1);
1999
2000 Ops.clear();
2001 Ops.push_back(Tmp3);
2002 Ops.push_back(Tmp1);
2003 Ops.push_back(CC);
2004 Ops.push_back(InFlag);
2005 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2006 } else {
2007 Ops.push_back(Tmp2);
2008 Ops.push_back(Tmp3);
2009 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00002010 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00002011 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2012 InFlag = Lo.getValue(1);
2013
2014 Ops.clear();
2015 Ops.push_back(Tmp3);
2016 Ops.push_back(Tmp1);
2017 Ops.push_back(CC);
2018 Ops.push_back(InFlag);
2019 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2020 }
2021
2022 Tys.clear();
2023 Tys.push_back(MVT::i32);
2024 Tys.push_back(MVT::i32);
2025 Ops.clear();
2026 Ops.push_back(Lo);
2027 Ops.push_back(Hi);
2028 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
2029 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002030 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00002031 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00002032 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00002033 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00002034
2035 SDOperand Result;
2036 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2037 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00002038 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00002039 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00002040 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00002041 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2042 DAG.getEntryNode(), Op.getOperand(0),
2043 StackSlot, DAG.getSrcValue(NULL));
2044
2045 // Build the FILD
2046 std::vector<MVT::ValueType> Tys;
2047 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002048 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00002049 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00002050 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00002051 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00002052 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00002053 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00002054 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
2055 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002056
2057 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002058 Chain = Result.getValue(1);
2059 SDOperand InFlag = Result.getValue(2);
2060
Evan Cheng11613a52006-02-04 02:20:30 +00002061 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002062 // shouldn't be necessary except that RFP cannot be live across
2063 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2064 MachineFunction &MF = DAG.getMachineFunction();
2065 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2066 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2067 std::vector<MVT::ValueType> Tys;
2068 Tys.push_back(MVT::Other);
2069 std::vector<SDOperand> Ops;
2070 Ops.push_back(Chain);
2071 Ops.push_back(Result);
2072 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00002073 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002074 Ops.push_back(InFlag);
2075 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
2076 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
2077 DAG.getSrcValue(NULL));
2078 }
2079
Evan Cheng6305e502006-01-12 22:54:21 +00002080 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002081 }
2082 case ISD::FP_TO_SINT: {
2083 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00002084 "Unknown FP_TO_SINT to lower!");
2085 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
2086 // stack slot.
2087 MachineFunction &MF = DAG.getMachineFunction();
2088 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
2089 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2090 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2091
2092 unsigned Opc;
2093 switch (Op.getValueType()) {
2094 default: assert(0 && "Invalid FP_TO_SINT to lower!");
2095 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
2096 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
2097 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
2098 }
2099
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002100 SDOperand Chain = DAG.getEntryNode();
2101 SDOperand Value = Op.getOperand(0);
2102 if (X86ScalarSSE) {
2103 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2104 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2105 DAG.getSrcValue(0));
2106 std::vector<MVT::ValueType> Tys;
2107 Tys.push_back(MVT::f64);
2108 Tys.push_back(MVT::Other);
2109 std::vector<SDOperand> Ops;
2110 Ops.push_back(Chain);
2111 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00002112 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002113 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2114 Chain = Value.getValue(1);
2115 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2116 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2117 }
2118
Chris Lattner76ac0682005-11-15 00:40:23 +00002119 // Build the FP_TO_INT*_IN_MEM
2120 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002121 Ops.push_back(Chain);
2122 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00002123 Ops.push_back(StackSlot);
2124 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
2125
2126 // Load the result.
2127 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2128 DAG.getSrcValue(NULL));
2129 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00002130 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00002131 std::vector<MVT::ValueType> Tys;
2132 Tys.push_back(MVT::Other);
2133 Tys.push_back(MVT::Flag);
2134 std::vector<SDOperand> Ops;
2135 Ops.push_back(Op.getOperand(0));
2136 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00002137 Ops.clear();
2138 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
2139 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
2140 MVT::i32, Ops[0].getValue(2)));
2141 Ops.push_back(Ops[1].getValue(1));
2142 Tys[0] = Tys[1] = MVT::i32;
2143 Tys.push_back(MVT::Other);
2144 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00002145 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00002146 case ISD::FABS: {
2147 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00002148 const Type *OpNTy = MVT::getTypeForValueType(VT);
2149 std::vector<Constant*> CV;
2150 if (VT == MVT::f64) {
2151 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2152 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2153 } else {
2154 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2155 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2156 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2157 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2158 }
2159 Constant *CS = ConstantStruct::get(CV);
2160 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2161 SDOperand Mask
2162 = DAG.getNode(X86ISD::LOAD_PACK,
2163 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00002164 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2165 }
Evan Cheng72d5c252006-01-31 22:28:30 +00002166 case ISD::FNEG: {
2167 MVT::ValueType VT = Op.getValueType();
2168 const Type *OpNTy = MVT::getTypeForValueType(VT);
2169 std::vector<Constant*> CV;
2170 if (VT == MVT::f64) {
2171 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2172 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2173 } else {
2174 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2175 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2176 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2177 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2178 }
2179 Constant *CS = ConstantStruct::get(CV);
2180 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2181 SDOperand Mask
2182 = DAG.getNode(X86ISD::LOAD_PACK,
2183 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2184 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2185 }
Evan Chengc1583db2005-12-21 20:21:51 +00002186 case ISD::SETCC: {
2187 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00002188 SDOperand Cond;
2189 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00002190 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2191 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00002192 bool Flip;
2193 unsigned X86CC;
2194 if (translateX86CC(CC, isFP, X86CC, Flip)) {
2195 if (Flip)
2196 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2197 Op.getOperand(1), Op.getOperand(0));
2198 else
2199 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2200 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00002201 return DAG.getNode(X86ISD::SETCC, MVT::i8,
2202 DAG.getConstant(X86CC, MVT::i8), Cond);
2203 } else {
2204 assert(isFP && "Illegal integer SetCC!");
2205
Evan Cheng45df7f82006-01-30 23:41:35 +00002206 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2207 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00002208 std::vector<MVT::ValueType> Tys;
2209 std::vector<SDOperand> Ops;
2210 switch (SetCCOpcode) {
2211 default: assert(false && "Illegal floating point SetCC!");
2212 case ISD::SETOEQ: { // !PF & ZF
2213 Tys.push_back(MVT::i8);
2214 Tys.push_back(MVT::Flag);
2215 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2216 Ops.push_back(Cond);
2217 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2218 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2219 DAG.getConstant(X86ISD::COND_E, MVT::i8),
2220 Tmp1.getValue(1));
2221 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2222 }
Evan Cheng172fce72006-01-06 00:43:03 +00002223 case ISD::SETUNE: { // PF | !ZF
2224 Tys.push_back(MVT::i8);
2225 Tys.push_back(MVT::Flag);
2226 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2227 Ops.push_back(Cond);
2228 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2229 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2230 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2231 Tmp1.getValue(1));
2232 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2233 }
2234 }
2235 }
Evan Chengc1583db2005-12-21 20:21:51 +00002236 }
Evan Cheng225a4d02005-12-17 01:21:05 +00002237 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00002238 MVT::ValueType VT = Op.getValueType();
2239 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00002240 bool isFPStack = isFP && !X86ScalarSSE;
2241 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00002242 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00002243 SDOperand Op0 = Op.getOperand(0);
2244 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00002245 if (Op0.getOpcode() == ISD::SETCC)
2246 Op0 = LowerOperation(Op0, DAG);
2247
Evan Cheng73a1ad92006-01-10 20:26:56 +00002248 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00002249 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2250 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2251 // have another use it will be eliminated.
2252 // If the X86ISD::SETCC has more than one use, then it's probably better
2253 // to use a test instead of duplicating the X86ISD::CMP (for register
2254 // pressure reason).
Evan Cheng78038292006-04-05 23:38:46 +00002255 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
2256 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
2257 CmpOpc == X86ISD::UCOMI) {
Evan Cheng944d1e92006-01-26 02:13:10 +00002258 if (!Op0.hasOneUse()) {
2259 std::vector<MVT::ValueType> Tys;
2260 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2261 Tys.push_back(Op0.Val->getValueType(i));
2262 std::vector<SDOperand> Ops;
2263 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2264 Ops.push_back(Op0.getOperand(i));
2265 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2266 }
2267
Evan Chengfb22e862006-01-13 01:03:02 +00002268 CC = Op0.getOperand(0);
2269 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00002270 // Make a copy as flag result cannot be used by more than one.
Evan Cheng78038292006-04-05 23:38:46 +00002271 Cond = DAG.getNode(CmpOpc, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00002272 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00002273 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00002274 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00002275 } else
2276 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00002277 } else
2278 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00002279
Evan Cheng731423f2006-01-13 01:06:49 +00002280 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00002281 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00002282 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00002283 }
Evan Cheng9c249c32006-01-09 18:33:28 +00002284
2285 std::vector<MVT::ValueType> Tys;
2286 Tys.push_back(Op.getValueType());
2287 Tys.push_back(MVT::Flag);
2288 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00002289 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2290 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00002291 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00002292 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00002293 Ops.push_back(CC);
2294 Ops.push_back(Cond);
2295 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00002296 }
Evan Cheng6fc31042005-12-19 23:12:38 +00002297 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00002298 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00002299 SDOperand Cond = Op.getOperand(1);
2300 SDOperand Dest = Op.getOperand(2);
2301 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00002302 if (Cond.getOpcode() == ISD::SETCC)
2303 Cond = LowerOperation(Cond, DAG);
2304
Evan Chengc1583db2005-12-21 20:21:51 +00002305 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00002306 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2307 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2308 // have another use it will be eliminated.
2309 // If the X86ISD::SETCC has more than one use, then it's probably better
2310 // to use a test instead of duplicating the X86ISD::CMP (for register
2311 // pressure reason).
Evan Cheng78038292006-04-05 23:38:46 +00002312 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
2313 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
2314 CmpOpc == X86ISD::UCOMI) {
Evan Cheng944d1e92006-01-26 02:13:10 +00002315 if (!Cond.hasOneUse()) {
2316 std::vector<MVT::ValueType> Tys;
2317 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2318 Tys.push_back(Cond.Val->getValueType(i));
2319 std::vector<SDOperand> Ops;
2320 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2321 Ops.push_back(Cond.getOperand(i));
2322 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2323 }
2324
Evan Chengfb22e862006-01-13 01:03:02 +00002325 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00002326 Cond = Cond.getOperand(1);
2327 // Make a copy as flag result cannot be used by more than one.
Evan Cheng78038292006-04-05 23:38:46 +00002328 Cond = DAG.getNode(CmpOpc, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00002329 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00002330 } else
2331 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00002332 } else
2333 addTest = true;
2334
2335 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00002336 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00002337 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2338 }
2339 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2340 Op.getOperand(0), Op.getOperand(2), CC, Cond);
2341 }
Evan Chengae986f12006-01-11 22:15:48 +00002342 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00002343 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002344 SDOperand Chain = Op.getOperand(0);
2345 unsigned Align =
2346 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2347 if (Align == 0) Align = 1;
2348
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002349 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2350 // If not DWORD aligned, call memset if size is less than the threshold.
2351 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00002352 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00002353 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002354 MVT::ValueType IntPtr = getPointerTy();
2355 const Type *IntPtrTy = getTargetData().getIntPtrType();
2356 std::vector<std::pair<SDOperand, const Type*> > Args;
2357 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2358 // Extend the ubyte argument to be an int value for the call.
2359 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2360 Args.push_back(std::make_pair(Val, IntPtrTy));
2361 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2362 std::pair<SDOperand,SDOperand> CallResult =
2363 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2364 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2365 return CallResult.second;
2366 }
2367
Evan Chengae986f12006-01-11 22:15:48 +00002368 MVT::ValueType AVT;
2369 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00002370 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2371 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00002372 bool TwoRepStos = false;
Evan Cheng6dc73292006-03-04 02:48:56 +00002373 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00002374 unsigned ValReg;
2375 unsigned Val = ValC->getValue() & 255;
2376
2377 // If the value is a constant, then we can potentially use larger sets.
2378 switch (Align & 3) {
2379 case 2: // WORD aligned
2380 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00002381 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2382 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00002383 Val = (Val << 8) | Val;
2384 ValReg = X86::AX;
2385 break;
2386 case 0: // DWORD aligned
2387 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00002388 if (I) {
2389 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2390 BytesLeft = I->getValue() % 4;
2391 } else {
2392 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2393 DAG.getConstant(2, MVT::i8));
2394 TwoRepStos = true;
2395 }
Evan Chengae986f12006-01-11 22:15:48 +00002396 Val = (Val << 8) | Val;
2397 Val = (Val << 16) | Val;
2398 ValReg = X86::EAX;
2399 break;
2400 default: // Byte aligned
2401 AVT = MVT::i8;
2402 Count = Op.getOperand(3);
2403 ValReg = X86::AL;
2404 break;
2405 }
2406
2407 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2408 InFlag);
2409 InFlag = Chain.getValue(1);
2410 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002411 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00002412 Count = Op.getOperand(3);
2413 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2414 InFlag = Chain.getValue(1);
2415 }
2416
2417 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2418 InFlag = Chain.getValue(1);
2419 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2420 InFlag = Chain.getValue(1);
2421
Evan Chengadc70932006-03-07 23:29:39 +00002422 std::vector<MVT::ValueType> Tys;
2423 Tys.push_back(MVT::Other);
2424 Tys.push_back(MVT::Flag);
2425 std::vector<SDOperand> Ops;
2426 Ops.push_back(Chain);
2427 Ops.push_back(DAG.getValueType(AVT));
2428 Ops.push_back(InFlag);
2429 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2430
2431 if (TwoRepStos) {
2432 InFlag = Chain.getValue(1);
2433 Count = Op.getOperand(3);
2434 MVT::ValueType CVT = Count.getValueType();
2435 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2436 DAG.getConstant(3, CVT));
2437 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2438 InFlag = Chain.getValue(1);
2439 Tys.clear();
2440 Tys.push_back(MVT::Other);
2441 Tys.push_back(MVT::Flag);
2442 Ops.clear();
2443 Ops.push_back(Chain);
2444 Ops.push_back(DAG.getValueType(MVT::i8));
2445 Ops.push_back(InFlag);
2446 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2447 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002448 // Issue stores for the last 1 - 3 bytes.
2449 SDOperand Value;
2450 unsigned Val = ValC->getValue() & 255;
2451 unsigned Offset = I->getValue() - BytesLeft;
2452 SDOperand DstAddr = Op.getOperand(1);
2453 MVT::ValueType AddrVT = DstAddr.getValueType();
2454 if (BytesLeft >= 2) {
2455 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2456 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2457 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2458 DAG.getConstant(Offset, AddrVT)),
2459 DAG.getSrcValue(NULL));
2460 BytesLeft -= 2;
2461 Offset += 2;
2462 }
2463
2464 if (BytesLeft == 1) {
2465 Value = DAG.getConstant(Val, MVT::i8);
2466 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2467 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2468 DAG.getConstant(Offset, AddrVT)),
2469 DAG.getSrcValue(NULL));
2470 }
2471 }
2472
2473 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002474 }
2475 case ISD::MEMCPY: {
2476 SDOperand Chain = Op.getOperand(0);
2477 unsigned Align =
2478 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2479 if (Align == 0) Align = 1;
2480
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002481 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2482 // If not DWORD aligned, call memcpy if size is less than the threshold.
2483 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00002484 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00002485 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002486 MVT::ValueType IntPtr = getPointerTy();
2487 const Type *IntPtrTy = getTargetData().getIntPtrType();
2488 std::vector<std::pair<SDOperand, const Type*> > Args;
2489 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2490 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2491 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2492 std::pair<SDOperand,SDOperand> CallResult =
2493 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2494 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2495 return CallResult.second;
2496 }
2497
Evan Chengae986f12006-01-11 22:15:48 +00002498 MVT::ValueType AVT;
2499 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00002500 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00002501 bool TwoRepMovs = false;
Evan Chengae986f12006-01-11 22:15:48 +00002502 switch (Align & 3) {
2503 case 2: // WORD aligned
2504 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00002505 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2506 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00002507 break;
2508 case 0: // DWORD aligned
2509 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00002510 if (I) {
2511 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2512 BytesLeft = I->getValue() % 4;
2513 } else {
2514 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2515 DAG.getConstant(2, MVT::i8));
2516 TwoRepMovs = true;
2517 }
Evan Chengae986f12006-01-11 22:15:48 +00002518 break;
2519 default: // Byte aligned
2520 AVT = MVT::i8;
2521 Count = Op.getOperand(3);
2522 break;
2523 }
2524
Evan Cheng6dc73292006-03-04 02:48:56 +00002525 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002526 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2527 InFlag = Chain.getValue(1);
2528 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2529 InFlag = Chain.getValue(1);
2530 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2531 InFlag = Chain.getValue(1);
2532
Evan Chengadc70932006-03-07 23:29:39 +00002533 std::vector<MVT::ValueType> Tys;
2534 Tys.push_back(MVT::Other);
2535 Tys.push_back(MVT::Flag);
2536 std::vector<SDOperand> Ops;
2537 Ops.push_back(Chain);
2538 Ops.push_back(DAG.getValueType(AVT));
2539 Ops.push_back(InFlag);
2540 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2541
2542 if (TwoRepMovs) {
2543 InFlag = Chain.getValue(1);
2544 Count = Op.getOperand(3);
2545 MVT::ValueType CVT = Count.getValueType();
2546 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2547 DAG.getConstant(3, CVT));
2548 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2549 InFlag = Chain.getValue(1);
2550 Tys.clear();
2551 Tys.push_back(MVT::Other);
2552 Tys.push_back(MVT::Flag);
2553 Ops.clear();
2554 Ops.push_back(Chain);
2555 Ops.push_back(DAG.getValueType(MVT::i8));
2556 Ops.push_back(InFlag);
2557 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2558 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002559 // Issue loads and stores for the last 1 - 3 bytes.
2560 unsigned Offset = I->getValue() - BytesLeft;
2561 SDOperand DstAddr = Op.getOperand(1);
2562 MVT::ValueType DstVT = DstAddr.getValueType();
2563 SDOperand SrcAddr = Op.getOperand(2);
2564 MVT::ValueType SrcVT = SrcAddr.getValueType();
2565 SDOperand Value;
2566 if (BytesLeft >= 2) {
2567 Value = DAG.getLoad(MVT::i16, Chain,
2568 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2569 DAG.getConstant(Offset, SrcVT)),
2570 DAG.getSrcValue(NULL));
2571 Chain = Value.getValue(1);
2572 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2573 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2574 DAG.getConstant(Offset, DstVT)),
2575 DAG.getSrcValue(NULL));
2576 BytesLeft -= 2;
2577 Offset += 2;
2578 }
2579
2580 if (BytesLeft == 1) {
2581 Value = DAG.getLoad(MVT::i8, Chain,
2582 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2583 DAG.getConstant(Offset, SrcVT)),
2584 DAG.getSrcValue(NULL));
2585 Chain = Value.getValue(1);
2586 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2587 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2588 DAG.getConstant(Offset, DstVT)),
2589 DAG.getSrcValue(NULL));
2590 }
2591 }
2592
2593 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002594 }
Evan Cheng99470012006-02-25 09:55:19 +00002595
2596 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2597 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2598 // one of the above mentioned nodes. It has to be wrapped because otherwise
2599 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2600 // be used to form addressing mode. These wrapped nodes will be selected
2601 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00002602 case ISD::ConstantPool: {
2603 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002604 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2605 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2606 CP->getAlignment()));
Evan Chengbc047222006-03-22 19:22:18 +00002607 if (Subtarget->isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002608 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002609 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00002610 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2611 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2612 }
2613
2614 return Result;
2615 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002616 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002617 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2618 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2619 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengbc047222006-03-22 19:22:18 +00002620 if (Subtarget->isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002621 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002622 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00002623 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2624 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00002625
2626 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Chengaf598d22006-03-13 23:18:16 +00002627 // the value at address GV, not the value of GV itself. This means that
Evan Cheng5588de92006-02-18 00:15:05 +00002628 // the GlobalAddress must be in the base or index register of the address,
2629 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00002630 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Chengaf598d22006-03-13 23:18:16 +00002631 DarwinGVRequiresExtraLoad(GV))
Evan Cheng5a766802006-02-07 08:38:37 +00002632 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00002633 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00002634 }
Evan Cheng5588de92006-02-18 00:15:05 +00002635
Evan Chengb94db9e2006-01-12 07:56:47 +00002636 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002637 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002638 case ISD::ExternalSymbol: {
2639 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2640 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2641 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chengbc047222006-03-22 19:22:18 +00002642 if (Subtarget->isTargetDarwin()) {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002643 // With PIC, the address is actually $g + Offset.
2644 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2645 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2646 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2647 }
2648
2649 return Result;
2650 }
Nate Begemane74795c2006-01-25 18:21:52 +00002651 case ISD::VASTART: {
2652 // vastart just stores the address of the VarArgsFrameIndex slot into the
2653 // memory location argument.
2654 // FIXME: Replace MVT::i32 with PointerTy
2655 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2656 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2657 Op.getOperand(1), Op.getOperand(2));
2658 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002659 case ISD::RET: {
2660 SDOperand Copy;
2661
2662 switch(Op.getNumOperands()) {
2663 default:
2664 assert(0 && "Do not know how to return this many arguments!");
2665 abort();
2666 case 1:
2667 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2668 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2669 case 2: {
2670 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2671 if (MVT::isInteger(ArgVT))
2672 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2673 SDOperand());
2674 else if (!X86ScalarSSE) {
2675 std::vector<MVT::ValueType> Tys;
2676 Tys.push_back(MVT::Other);
2677 Tys.push_back(MVT::Flag);
2678 std::vector<SDOperand> Ops;
2679 Ops.push_back(Op.getOperand(0));
2680 Ops.push_back(Op.getOperand(1));
2681 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2682 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00002683 SDOperand MemLoc;
2684 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00002685 SDOperand Value = Op.getOperand(1);
2686
Evan Chenga24617f2006-02-01 01:19:32 +00002687 if (Value.getOpcode() == ISD::LOAD &&
2688 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00002689 Chain = Value.getOperand(0);
2690 MemLoc = Value.getOperand(1);
2691 } else {
2692 // Spill the value to memory and reload it into top of stack.
2693 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2694 MachineFunction &MF = DAG.getMachineFunction();
2695 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2696 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2697 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2698 Value, MemLoc, DAG.getSrcValue(0));
2699 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002700 std::vector<MVT::ValueType> Tys;
2701 Tys.push_back(MVT::f64);
2702 Tys.push_back(MVT::Other);
2703 std::vector<SDOperand> Ops;
2704 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002705 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002706 Ops.push_back(DAG.getValueType(ArgVT));
2707 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2708 Tys.clear();
2709 Tys.push_back(MVT::Other);
2710 Tys.push_back(MVT::Flag);
2711 Ops.clear();
2712 Ops.push_back(Copy.getValue(1));
2713 Ops.push_back(Copy);
2714 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2715 }
2716 break;
2717 }
2718 case 3:
2719 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2720 SDOperand());
2721 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2722 break;
2723 }
2724 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2725 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2726 Copy.getValue(1));
2727 }
Evan Chengd5e905d2006-03-21 23:01:21 +00002728 case ISD::SCALAR_TO_VECTOR: {
2729 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chenge7ee6a52006-03-24 23:15:12 +00002730 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Chengd5e905d2006-03-21 23:01:21 +00002731 }
Evan Chengd097e672006-03-22 02:53:00 +00002732 case ISD::VECTOR_SHUFFLE: {
2733 SDOperand V1 = Op.getOperand(0);
2734 SDOperand V2 = Op.getOperand(1);
2735 SDOperand PermMask = Op.getOperand(2);
2736 MVT::ValueType VT = Op.getValueType();
Evan Cheng2595a682006-03-24 02:58:06 +00002737 unsigned NumElems = PermMask.getNumOperands();
Evan Chengd097e672006-03-22 02:53:00 +00002738
Evan Chengc995b452006-04-06 23:23:56 +00002739 if (X86::isSplatMask(PermMask.Val))
Evan Cheng2cf42322006-04-05 06:09:26 +00002740 return Op;
Evan Chengc995b452006-04-06 23:23:56 +00002741
2742 // Normalize the node to match x86 shuffle ops if needed
2743 if (V2.getOpcode() != ISD::UNDEF) {
2744 bool DoSwap = false;
2745
2746 if (ShouldXformedToMOVLP(V1, V2, PermMask))
2747 DoSwap = true;
2748 else if (isLowerFromV2UpperFromV1(PermMask))
2749 DoSwap = true;
2750
2751 if (DoSwap) {
2752 Op = CommuteVectorShuffle(Op, DAG);
2753 V1 = Op.getOperand(0);
2754 V2 = Op.getOperand(1);
2755 PermMask = Op.getOperand(2);
2756 }
Evan Cheng500ec162006-03-29 03:04:49 +00002757 }
Evan Chengda59b0d2006-03-29 01:30:51 +00002758
Evan Chengc995b452006-04-06 23:23:56 +00002759 if (NumElems == 2)
2760 return Op;
2761
Evan Chengacc33642006-03-29 19:02:40 +00002762 if (X86::isUNPCKLMask(PermMask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00002763 X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Evan Chengacc33642006-03-29 19:02:40 +00002764 X86::isUNPCKHMask(PermMask.Val))
2765 // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
Evan Cheng2cf42322006-04-05 06:09:26 +00002766 return Op;
Evan Chengacc33642006-03-29 19:02:40 +00002767
Evan Cheng7e2ff112006-03-30 19:54:57 +00002768 // If VT is integer, try PSHUF* first, then SHUFP*.
2769 if (MVT::isInteger(VT)) {
2770 if (X86::isPSHUFDMask(PermMask.Val) ||
2771 X86::isPSHUFHWMask(PermMask.Val) ||
2772 X86::isPSHUFLWMask(PermMask.Val)) {
2773 if (V2.getOpcode() != ISD::UNDEF)
2774 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2775 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002776 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002777 }
2778
2779 if (X86::isSHUFPMask(PermMask.Val))
Evan Chengc995b452006-04-06 23:23:56 +00002780 return Op;
Evan Cheng59a63552006-04-05 01:47:37 +00002781
2782 // Handle v8i16 shuffle high / low shuffle node pair.
2783 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2784 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2785 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2786 std::vector<SDOperand> MaskVec;
2787 for (unsigned i = 0; i != 4; ++i)
2788 MaskVec.push_back(PermMask.getOperand(i));
2789 for (unsigned i = 4; i != 8; ++i)
2790 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2791 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2792 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2793 MaskVec.clear();
2794 for (unsigned i = 0; i != 4; ++i)
2795 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2796 for (unsigned i = 4; i != 8; ++i)
2797 MaskVec.push_back(PermMask.getOperand(i));
2798 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2799 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2800 }
Evan Cheng7e2ff112006-03-30 19:54:57 +00002801 } else {
2802 // Floating point cases in the other order.
2803 if (X86::isSHUFPMask(PermMask.Val))
Evan Chengc995b452006-04-06 23:23:56 +00002804 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002805 if (X86::isPSHUFDMask(PermMask.Val) ||
2806 X86::isPSHUFHWMask(PermMask.Val) ||
2807 X86::isPSHUFLWMask(PermMask.Val)) {
2808 if (V2.getOpcode() != ISD::UNDEF)
2809 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2810 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002811 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002812 }
Evan Chengda59b0d2006-03-29 01:30:51 +00002813 }
Evan Chengd097e672006-03-22 02:53:00 +00002814
Evan Cheng2cf42322006-04-05 06:09:26 +00002815 return SDOperand();
Evan Chengd097e672006-03-22 02:53:00 +00002816 }
Evan Cheng082c8782006-03-24 07:29:27 +00002817 case ISD::BUILD_VECTOR: {
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00002818 // All one's are handled with pcmpeqd.
2819 if (ISD::isBuildVectorAllOnes(Op.Val))
2820 return Op;
2821
Evan Cheng2bc09412006-03-25 09:37:23 +00002822 std::set<SDOperand> Values;
Evan Chenge7ee6a52006-03-24 23:15:12 +00002823 SDOperand Elt0 = Op.getOperand(0);
Evan Cheng2bc09412006-03-25 09:37:23 +00002824 Values.insert(Elt0);
Evan Chenge7ee6a52006-03-24 23:15:12 +00002825 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2826 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2827 (isa<ConstantFPSDNode>(Elt0) &&
2828 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2829 bool RestAreZero = true;
Evan Cheng082c8782006-03-24 07:29:27 +00002830 unsigned NumElems = Op.getNumOperands();
Evan Chenge7ee6a52006-03-24 23:15:12 +00002831 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng2bc09412006-03-25 09:37:23 +00002832 SDOperand Elt = Op.getOperand(i);
2833 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng082c8782006-03-24 07:29:27 +00002834 if (!FPC->isExactlyValue(+0.0))
Evan Chenge7ee6a52006-03-24 23:15:12 +00002835 RestAreZero = false;
Evan Cheng2bc09412006-03-25 09:37:23 +00002836 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng082c8782006-03-24 07:29:27 +00002837 if (!C->isNullValue())
Evan Chenge7ee6a52006-03-24 23:15:12 +00002838 RestAreZero = false;
Evan Cheng082c8782006-03-24 07:29:27 +00002839 } else
Evan Chenge7ee6a52006-03-24 23:15:12 +00002840 RestAreZero = false;
Evan Cheng2bc09412006-03-25 09:37:23 +00002841 Values.insert(Elt);
Evan Cheng082c8782006-03-24 07:29:27 +00002842 }
2843
Evan Chenge7ee6a52006-03-24 23:15:12 +00002844 if (RestAreZero) {
2845 if (Elt0IsZero) return Op;
2846
2847 // Zero extend a scalar to a vector.
2848 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2849 }
2850
Evan Cheng2bc09412006-03-25 09:37:23 +00002851 if (Values.size() > 2) {
2852 // Expand into a number of unpckl*.
2853 // e.g. for v4f32
2854 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2855 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2856 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2857 MVT::ValueType VT = Op.getValueType();
Evan Cheng5df75882006-03-28 00:39:58 +00002858 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2859 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2860 std::vector<SDOperand> MaskVec;
2861 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2862 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2863 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2864 }
2865 SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng2bc09412006-03-25 09:37:23 +00002866 std::vector<SDOperand> V(NumElems);
2867 for (unsigned i = 0; i < NumElems; ++i)
2868 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2869 NumElems >>= 1;
2870 while (NumElems != 0) {
2871 for (unsigned i = 0; i < NumElems; ++i)
Evan Cheng5df75882006-03-28 00:39:58 +00002872 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2873 PermMask);
Evan Cheng2bc09412006-03-25 09:37:23 +00002874 NumElems >>= 1;
2875 }
2876 return V[0];
2877 }
2878
Evan Cheng082c8782006-03-24 07:29:27 +00002879 return SDOperand();
2880 }
Evan Chengcbffa462006-03-31 19:22:53 +00002881 case ISD::EXTRACT_VECTOR_ELT: {
Evan Chengebf10062006-04-03 20:53:28 +00002882 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2883 return SDOperand();
2884
Evan Chengcbffa462006-03-31 19:22:53 +00002885 MVT::ValueType VT = Op.getValueType();
2886 if (MVT::getSizeInBits(VT) == 16) {
Evan Chengebf10062006-04-03 20:53:28 +00002887 // Transform it so it match pextrw which produces a 32-bit result.
Evan Chengcbffa462006-03-31 19:22:53 +00002888 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2889 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2890 Op.getOperand(0), Op.getOperand(1));
2891 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2892 DAG.getValueType(VT));
2893 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Chengebf10062006-04-03 20:53:28 +00002894 } else if (MVT::getSizeInBits(VT) == 32) {
2895 SDOperand Vec = Op.getOperand(0);
2896 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2897 if (Idx == 0)
2898 return Op;
2899
2900 // TODO: if Idex == 2, we can use unpckhps
2901 // SHUFPS the element to the lowest double word, then movss.
2902 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2903 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2904 MVT::getVectorBaseType(MaskVT));
2905 std::vector<SDOperand> IdxVec;
2906 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2907 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2908 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2909 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2910 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2911 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2912 Vec, Vec, Mask);
2913 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2914 DAG.getConstant(0, MVT::i32));
2915 } else if (MVT::getSizeInBits(VT) == 64) {
2916 SDOperand Vec = Op.getOperand(0);
2917 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2918 if (Idx == 0)
2919 return Op;
2920
2921 // UNPCKHPD the element to the lowest double word, then movsd.
Evan Chengb64827e2006-04-03 22:30:54 +00002922 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2923 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Evan Chengebf10062006-04-03 20:53:28 +00002924 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2925 std::vector<SDOperand> IdxVec;
2926 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2927 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2928 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2929 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2930 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2931 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2932 DAG.getConstant(0, MVT::i32));
Evan Chengcbffa462006-03-31 19:22:53 +00002933 }
2934
2935 return SDOperand();
2936 }
2937 case ISD::INSERT_VECTOR_ELT: {
2938 // Transform it so it match pinsrw which expects a 16-bit value in a R32
2939 // as its second argument.
2940 MVT::ValueType VT = Op.getValueType();
2941 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2942 if (MVT::getSizeInBits(BaseVT) == 16) {
2943 SDOperand N1 = Op.getOperand(1);
2944 SDOperand N2 = Op.getOperand(2);
2945 if (N1.getValueType() != MVT::i32)
2946 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2947 if (N2.getValueType() != MVT::i32)
2948 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
Evan Cheng5fd7c692006-03-31 21:55:24 +00002949 return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
Evan Chengcbffa462006-03-31 19:22:53 +00002950 }
2951
2952 return SDOperand();
2953 }
Evan Cheng78038292006-04-05 23:38:46 +00002954 case ISD::INTRINSIC_WO_CHAIN: {
2955 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
2956 switch (IntNo) {
2957 default: return SDOperand(); // Don't custom lower most intrinsics.
2958 // Comparison intrinsics.
2959 case Intrinsic::x86_sse_comieq_ss:
2960 case Intrinsic::x86_sse_comilt_ss:
2961 case Intrinsic::x86_sse_comile_ss:
2962 case Intrinsic::x86_sse_comigt_ss:
2963 case Intrinsic::x86_sse_comige_ss:
2964 case Intrinsic::x86_sse_comineq_ss:
2965 case Intrinsic::x86_sse_ucomieq_ss:
2966 case Intrinsic::x86_sse_ucomilt_ss:
2967 case Intrinsic::x86_sse_ucomile_ss:
2968 case Intrinsic::x86_sse_ucomigt_ss:
2969 case Intrinsic::x86_sse_ucomige_ss:
2970 case Intrinsic::x86_sse_ucomineq_ss:
2971 case Intrinsic::x86_sse2_comieq_sd:
2972 case Intrinsic::x86_sse2_comilt_sd:
2973 case Intrinsic::x86_sse2_comile_sd:
2974 case Intrinsic::x86_sse2_comigt_sd:
2975 case Intrinsic::x86_sse2_comige_sd:
2976 case Intrinsic::x86_sse2_comineq_sd:
2977 case Intrinsic::x86_sse2_ucomieq_sd:
2978 case Intrinsic::x86_sse2_ucomilt_sd:
2979 case Intrinsic::x86_sse2_ucomile_sd:
2980 case Intrinsic::x86_sse2_ucomigt_sd:
2981 case Intrinsic::x86_sse2_ucomige_sd:
2982 case Intrinsic::x86_sse2_ucomineq_sd: {
Evan Chengc995b452006-04-06 23:23:56 +00002983 unsigned Opc = 0;
2984 ISD::CondCode CC = ISD::SETCC_INVALID;
Evan Cheng78038292006-04-05 23:38:46 +00002985 switch (IntNo) {
2986 default: break;
2987 case Intrinsic::x86_sse_comieq_ss:
2988 case Intrinsic::x86_sse2_comieq_sd:
2989 Opc = X86ISD::COMI;
2990 CC = ISD::SETEQ;
2991 break;
2992 case Intrinsic::x86_sse_comilt_ss:
2993 case Intrinsic::x86_sse2_comilt_sd:
2994 Opc = X86ISD::COMI;
2995 CC = ISD::SETLT;
2996 break;
2997 case Intrinsic::x86_sse_comile_ss:
2998 case Intrinsic::x86_sse2_comile_sd:
2999 Opc = X86ISD::COMI;
3000 CC = ISD::SETLE;
3001 break;
3002 case Intrinsic::x86_sse_comigt_ss:
3003 case Intrinsic::x86_sse2_comigt_sd:
3004 Opc = X86ISD::COMI;
3005 CC = ISD::SETGT;
3006 break;
3007 case Intrinsic::x86_sse_comige_ss:
3008 case Intrinsic::x86_sse2_comige_sd:
3009 Opc = X86ISD::COMI;
3010 CC = ISD::SETGE;
3011 break;
3012 case Intrinsic::x86_sse_comineq_ss:
3013 case Intrinsic::x86_sse2_comineq_sd:
3014 Opc = X86ISD::COMI;
3015 CC = ISD::SETNE;
3016 break;
3017 case Intrinsic::x86_sse_ucomieq_ss:
3018 case Intrinsic::x86_sse2_ucomieq_sd:
3019 Opc = X86ISD::UCOMI;
3020 CC = ISD::SETEQ;
3021 break;
3022 case Intrinsic::x86_sse_ucomilt_ss:
3023 case Intrinsic::x86_sse2_ucomilt_sd:
3024 Opc = X86ISD::UCOMI;
3025 CC = ISD::SETLT;
3026 break;
3027 case Intrinsic::x86_sse_ucomile_ss:
3028 case Intrinsic::x86_sse2_ucomile_sd:
3029 Opc = X86ISD::UCOMI;
3030 CC = ISD::SETLE;
3031 break;
3032 case Intrinsic::x86_sse_ucomigt_ss:
3033 case Intrinsic::x86_sse2_ucomigt_sd:
3034 Opc = X86ISD::UCOMI;
3035 CC = ISD::SETGT;
3036 break;
3037 case Intrinsic::x86_sse_ucomige_ss:
3038 case Intrinsic::x86_sse2_ucomige_sd:
3039 Opc = X86ISD::UCOMI;
3040 CC = ISD::SETGE;
3041 break;
3042 case Intrinsic::x86_sse_ucomineq_ss:
3043 case Intrinsic::x86_sse2_ucomineq_sd:
3044 Opc = X86ISD::UCOMI;
3045 CC = ISD::SETNE;
3046 break;
3047 }
3048 bool Flip;
3049 unsigned X86CC;
3050 translateX86CC(CC, true, X86CC, Flip);
3051 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3052 Op.getOperand(Flip?1:2));
3053 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3054 DAG.getConstant(X86CC, MVT::i8), Cond);
3055 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3056 }
3057 }
3058 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003059 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003060}
Evan Cheng6af02632005-12-20 06:22:03 +00003061
3062const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3063 switch (Opcode) {
3064 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003065 case X86ISD::SHLD: return "X86ISD::SHLD";
3066 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003067 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003068 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003069 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003070 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003071 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3072 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3073 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003074 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003075 case X86ISD::FST: return "X86ISD::FST";
3076 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003077 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003078 case X86ISD::CALL: return "X86ISD::CALL";
3079 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3080 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3081 case X86ISD::CMP: return "X86ISD::CMP";
3082 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003083 case X86ISD::COMI: return "X86ISD::COMI";
3084 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003085 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003086 case X86ISD::CMOV: return "X86ISD::CMOV";
3087 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003088 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003089 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3090 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003091 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003092 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00003093 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00003094 case X86ISD::S2VEC: return "X86ISD::S2VEC";
3095 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00003096 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00003097 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00003098 }
3099}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003100
Nate Begeman8a77efe2006-02-16 21:11:51 +00003101void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3102 uint64_t Mask,
3103 uint64_t &KnownZero,
3104 uint64_t &KnownOne,
3105 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003106 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00003107 assert((Opc >= ISD::BUILTIN_OP_END ||
3108 Opc == ISD::INTRINSIC_WO_CHAIN ||
3109 Opc == ISD::INTRINSIC_W_CHAIN ||
3110 Opc == ISD::INTRINSIC_VOID) &&
3111 "Should use MaskedValueIsZero if you don't know whether Op"
3112 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003113
Evan Cheng6d196db2006-04-05 06:11:20 +00003114 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003115 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00003116 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00003117 case X86ISD::SETCC:
3118 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3119 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003120 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003121}
Chris Lattnerc642aa52006-01-31 19:43:35 +00003122
3123std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00003124getRegClassForInlineAsmConstraint(const std::string &Constraint,
3125 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00003126 if (Constraint.size() == 1) {
3127 // FIXME: not handling fp-stack yet!
3128 // FIXME: not handling MMX registers yet ('y' constraint).
3129 switch (Constraint[0]) { // GCC X86 Constraint Letters
3130 default: break; // Unknown constriant letter
3131 case 'r': // GENERAL_REGS
3132 case 'R': // LEGACY_REGS
3133 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3134 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3135 case 'l': // INDEX_REGS
3136 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3137 X86::ESI, X86::EDI, X86::EBP, 0);
3138 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3139 case 'Q': // Q_REGS
3140 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
3141 case 'x': // SSE_REGS if SSE1 allowed
3142 if (Subtarget->hasSSE1())
3143 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3144 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3145 0);
3146 return std::vector<unsigned>();
3147 case 'Y': // SSE_REGS if SSE2 allowed
3148 if (Subtarget->hasSSE2())
3149 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3150 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3151 0);
3152 return std::vector<unsigned>();
3153 }
3154 }
3155
Chris Lattner7ad77df2006-02-22 00:56:39 +00003156 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00003157}
Evan Chengaf598d22006-03-13 23:18:16 +00003158
3159/// isLegalAddressImmediate - Return true if the integer value or
3160/// GlobalValue can be used as the offset of the target addressing mode.
3161bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3162 // X86 allows a sign-extended 32-bit immediate field.
3163 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3164}
3165
3166bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00003167 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00003168 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3169 if (RModel == Reloc::Static)
3170 return true;
3171 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00003172 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00003173 else
3174 return false;
3175 } else
3176 return true;
3177}
Evan Cheng68ad48b2006-03-22 18:59:22 +00003178
3179/// isShuffleMaskLegal - Targets can use this to indicate that they only
3180/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3181/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3182/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00003183bool
3184X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3185 // Only do shuffles on 128-bit vector types for now.
3186 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2595a682006-03-24 02:58:06 +00003187 return (Mask.Val->getNumOperands() == 2 ||
3188 X86::isSplatMask(Mask.Val) ||
Evan Chengd27fb3e2006-03-24 01:18:28 +00003189 X86::isPSHUFDMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00003190 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng5df75882006-03-28 00:39:58 +00003191 X86::isSHUFPMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00003192 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00003193 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00003194 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00003195}