blob: 1f58fdb760a48bdf292d58e901596a087fef8ba3 [file] [log] [blame]
Chris Lattnera58f5592006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattner76ac0682005-11-15 00:40:23 +00002//
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"
Evan Cheng88decde2006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000022#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000023#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000024#include "llvm/ADT/VectorExtras.h"
25#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000029#include "llvm/CodeGen/SelectionDAG.h"
30#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000032#include "llvm/Target/TargetOptions.h"
33using namespace llvm;
34
35// FIXME: temporary.
36#include "llvm/Support/CommandLine.h"
37static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
38 cl::desc("Enable fastcc on X86"));
39
40X86TargetLowering::X86TargetLowering(TargetMachine &TM)
41 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000042 Subtarget = &TM.getSubtarget<X86Subtarget>();
43 X86ScalarSSE = Subtarget->hasSSE2();
44
Chris Lattner76ac0682005-11-15 00:40:23 +000045 // Set up the TargetLowering object.
46
47 // X86 is weird, it always uses i8 for shift amounts and setcc results.
48 setShiftAmountType(MVT::i8);
49 setSetCCResultType(MVT::i8);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000051 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000052 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000053 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000054
Evan Chengbc047222006-03-22 19:22:18 +000055 if (!Subtarget->isTargetDarwin())
Evan Chengb09a56f2006-03-17 20:31:41 +000056 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
57 setUseUnderscoreSetJmpLongJmp(true);
58
Evan Cheng20931a72006-03-16 21:47:42 +000059 // Add legal addressing mode scale values.
60 addLegalAddressScale(8);
61 addLegalAddressScale(4);
62 addLegalAddressScale(2);
63 // Enter the ones which require both scale + index last. These are more
64 // expensive.
65 addLegalAddressScale(9);
66 addLegalAddressScale(5);
67 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000068
Chris Lattner76ac0682005-11-15 00:40:23 +000069 // Set up the register classes.
Evan Cheng9fee4422006-05-16 07:21:53 +000070 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
71 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
72 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +000073
74 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
75 // operation.
76 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
78 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000079
80 if (X86ScalarSSE)
81 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
83 else
84 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000085
86 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
89 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000090 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000091 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000092 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000093 else {
94 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
95 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
96 }
Chris Lattner76ac0682005-11-15 00:40:23 +000097
Evan Cheng5b97fcf2006-01-30 08:02:57 +000098 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
99 // isn't legal.
100 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
101 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
102
Evan Cheng08390f62006-01-30 22:13:22 +0000103 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
104 // this operation.
105 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
106 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
107
108 if (X86ScalarSSE) {
109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
110 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000112 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000113 }
114
115 // Handle FP_TO_UINT by promoting the destination to a larger signed
116 // conversion.
117 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
119 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
120
Evan Chengd13778e2006-02-18 07:26:17 +0000121 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000122 // Expand FP_TO_UINT into a select.
123 // FIXME: We would like to use a Custom expander here eventually to do
124 // the optimal thing for SSE vs. the default expansion in the legalizer.
125 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
126 else
Evan Chengd13778e2006-02-18 07:26:17 +0000127 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000128 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
129
Evan Cheng08390f62006-01-30 22:13:22 +0000130 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
131 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000132
Evan Cheng593bea72006-02-17 07:01:52 +0000133 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000134 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
135 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000136 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
140 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
141 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
142 setOperationAction(ISD::FREM , MVT::f64 , Expand);
143 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
144 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
146 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
147 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
149 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
150 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
151 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000152 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000153 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000154
Chris Lattner76ac0682005-11-15 00:40:23 +0000155 // These should be promoted to a larger select which is supported.
156 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
157 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000158
159 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000160 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
161 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000169 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000170 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000171 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000172 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000173 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000174 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000175 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000176 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000177 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
179 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000180 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000181 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
182 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000183
Chris Lattner9c415362005-11-29 06:16:21 +0000184 // We don't have line number support yet.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000187 // FIXME - use subtarget debug flags
Evan Chengbc047222006-03-22 19:22:18 +0000188 if (!Subtarget->isTargetDarwin())
Evan Cheng30d7b702006-03-07 02:02:57 +0000189 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000190
Nate Begemane74795c2006-01-25 18:21:52 +0000191 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
192 setOperationAction(ISD::VASTART , MVT::Other, Custom);
193
194 // Use the default implementation.
195 setOperationAction(ISD::VAARG , MVT::Other, Expand);
196 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
197 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000198 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
199 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
200 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000201
Chris Lattner9c7f5032006-03-05 05:08:37 +0000202 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
203 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
204
Chris Lattner76ac0682005-11-15 00:40:23 +0000205 if (X86ScalarSSE) {
206 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000209
Evan Cheng72d5c252006-01-31 22:28:30 +0000210 // Use ANDPD to simulate FABS.
211 setOperationAction(ISD::FABS , MVT::f64, Custom);
212 setOperationAction(ISD::FABS , MVT::f32, Custom);
213
214 // Use XORP to simulate FNEG.
215 setOperationAction(ISD::FNEG , MVT::f64, Custom);
216 setOperationAction(ISD::FNEG , MVT::f32, Custom);
217
Evan Chengd8fba3a2006-02-02 00:28:23 +0000218 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000219 setOperationAction(ISD::FSIN , MVT::f64, Expand);
220 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 setOperationAction(ISD::FREM , MVT::f64, Expand);
222 setOperationAction(ISD::FSIN , MVT::f32, Expand);
223 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000224 setOperationAction(ISD::FREM , MVT::f32, Expand);
225
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000226 // Expand FP immediates into loads from the stack, except for the special
227 // cases we handle.
228 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
229 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000230 addLegalFPImmediate(+0.0); // xorps / xorpd
231 } else {
232 // Set up the FP register classes.
233 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000234
235 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
236
Chris Lattner76ac0682005-11-15 00:40:23 +0000237 if (!UnsafeFPMath) {
238 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
239 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
240 }
241
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000242 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000243 addLegalFPImmediate(+0.0); // FLD0
244 addLegalFPImmediate(+1.0); // FLD1
245 addLegalFPImmediate(-0.0); // FLD0/FCHS
246 addLegalFPImmediate(-1.0); // FLD1/FCHS
247 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000248
Evan Cheng19264272006-03-01 01:11:20 +0000249 // First set operation action for all vector types to expand. Then we
250 // will selectively turn on ones that can be effectively codegen'd.
251 for (unsigned VT = (unsigned)MVT::Vector + 1;
252 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
253 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
254 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000257 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000258 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000259 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000260 }
261
Evan Chengbc047222006-03-22 19:22:18 +0000262 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
266
Evan Cheng19264272006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Evan Chengd5e905d2006-03-21 23:01:21 +0000268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000271 }
272
Evan Chengbc047222006-03-22 19:22:18 +0000273 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng92232302006-04-12 21:21:57 +0000276 setOperationAction(ISD::AND, MVT::v4f32, Legal);
277 setOperationAction(ISD::OR, MVT::v4f32, Legal);
278 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000279 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
280 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
281 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
282 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
283 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
284 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000285 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000286 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000287 }
288
Evan Chengbc047222006-03-22 19:22:18 +0000289 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000290 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
291 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
293 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
295
Evan Cheng617a6a82006-04-10 07:23:14 +0000296 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
297 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
298 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
299 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
300 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
301 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
302 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
303 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000304 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000305 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng92232302006-04-12 21:21:57 +0000306
Evan Cheng617a6a82006-04-10 07:23:14 +0000307 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000309 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000310 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
311 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
312 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000313
Evan Cheng92232302006-04-12 21:21:57 +0000314 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
315 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
316 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
317 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
318 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
319 }
320 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
321 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
322 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
323 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
324 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
325 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
326
327 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
328 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
329 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
330 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
331 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
332 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
333 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
334 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000335 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
336 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000337 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
338 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000339 }
Evan Cheng92232302006-04-12 21:21:57 +0000340
341 // Custom lower v2i64 and v2f64 selects.
342 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000343 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000344 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000345 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000346 }
347
Evan Cheng78038292006-04-05 23:38:46 +0000348 // We want to custom lower some of our intrinsics.
349 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
350
Chris Lattner76ac0682005-11-15 00:40:23 +0000351 computeRegisterProperties();
352
Evan Cheng6a374562006-02-14 08:25:08 +0000353 // FIXME: These should be based on subtarget info. Plus, the values should
354 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000355 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
356 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
357 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000358 allowUnalignedMemoryAccesses = true; // x86 supports it!
359}
360
Chris Lattner76ac0682005-11-15 00:40:23 +0000361//===----------------------------------------------------------------------===//
362// C Calling Convention implementation
363//===----------------------------------------------------------------------===//
364
Evan Cheng24eb3f42006-04-27 05:35:28 +0000365/// AddLiveIn - This helper function adds the specified physical register to the
366/// MachineFunction as a live in value. It also creates a corresponding virtual
367/// register for it.
368static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
369 TargetRegisterClass *RC) {
370 assert(RC->contains(PReg) && "Not the correct regclass!");
371 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
372 MF.addLiveIn(PReg, VReg);
373 return VReg;
374}
375
Evan Cheng89001ad2006-04-27 08:31:10 +0000376/// HowToPassCCCArgument - Returns how an formal argument of the specified type
377/// should be passed. If it is through stack, returns the size of the stack
Evan Cheng763f9b02006-05-26 18:25:43 +0000378/// slot; if it is through XMM register, returns the number of XMM registers
Evan Cheng89001ad2006-04-27 08:31:10 +0000379/// are needed.
380static void
381HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
382 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng8aca43e2006-05-25 23:31:23 +0000383 NumXMMRegs = 0;
384
Evan Cheng48940d12006-04-27 01:32:22 +0000385 switch (ObjectVT) {
386 default: assert(0 && "Unhandled argument type!");
Evan Cheng48940d12006-04-27 01:32:22 +0000387 case MVT::i8: ObjSize = 1; break;
388 case MVT::i16: ObjSize = 2; break;
389 case MVT::i32: ObjSize = 4; break;
390 case MVT::i64: ObjSize = 8; break;
391 case MVT::f32: ObjSize = 4; break;
392 case MVT::f64: ObjSize = 8; break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000393 case MVT::v16i8:
394 case MVT::v8i16:
395 case MVT::v4i32:
396 case MVT::v2i64:
397 case MVT::v4f32:
398 case MVT::v2f64:
399 if (NumXMMRegs < 3)
400 ObjXMMRegs = 1;
401 else
402 ObjSize = 16;
403 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000404 }
Evan Cheng48940d12006-04-27 01:32:22 +0000405}
406
Evan Cheng17e734f2006-05-23 21:06:34 +0000407SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
408 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000409 MachineFunction &MF = DAG.getMachineFunction();
410 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000411 SDOperand Root = Op.getOperand(0);
412 std::vector<SDOperand> ArgValues;
Chris Lattner76ac0682005-11-15 00:40:23 +0000413
Evan Cheng48940d12006-04-27 01:32:22 +0000414 // Add DAG nodes to load the arguments... On entry to a function on the X86,
415 // the stack frame looks like this:
416 //
417 // [ESP] -- return address
418 // [ESP + 4] -- first argument (leftmost lexically)
419 // [ESP + 8] -- second argument, if first argument is four bytes in size
420 // ...
421 //
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000422 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng89001ad2006-04-27 08:31:10 +0000423 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
424 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000425 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000426 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
427 unsigned ArgIncrement = 4;
428 unsigned ObjSize = 0;
429 unsigned ObjXMMRegs = 0;
430 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
431 if (ObjSize >= 8)
432 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000433
Evan Cheng17e734f2006-05-23 21:06:34 +0000434 SDOperand ArgValue;
435 if (ObjXMMRegs) {
436 // Passed in a XMM register.
437 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
Evan Cheng89001ad2006-04-27 08:31:10 +0000438 X86::VR128RegisterClass);
Evan Cheng17e734f2006-05-23 21:06:34 +0000439 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
440 ArgValues.push_back(ArgValue);
441 NumXMMRegs += ObjXMMRegs;
442 } else {
443 // Create the frame index object for this incoming parameter...
444 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
445 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
446 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
447 DAG.getSrcValue(NULL));
448 ArgValues.push_back(ArgValue);
449 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000450 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000451 }
452
Evan Cheng17e734f2006-05-23 21:06:34 +0000453 ArgValues.push_back(Root);
454
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000455 // If the function takes variable number of arguments, make a frame index for
456 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000457 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
458 if (isVarArg)
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000459 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
460 ReturnAddrIndex = 0; // No return address slot generated yet.
461 BytesToPopOnReturn = 0; // Callee pops nothing.
462 BytesCallerReserves = ArgOffset;
Evan Cheng17e734f2006-05-23 21:06:34 +0000463
Chris Lattner8be5be82006-05-23 18:50:38 +0000464 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
465 // pointer.
Evan Cheng17e734f2006-05-23 21:06:34 +0000466 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner8be5be82006-05-23 18:50:38 +0000467 Subtarget->isTargetDarwin())
468 BytesToPopOnReturn = 4;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000469
Evan Cheng17e734f2006-05-23 21:06:34 +0000470 // Return the new list of results.
471 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
472 Op.Val->value_end());
473 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattner76ac0682005-11-15 00:40:23 +0000474}
475
Evan Cheng2a330942006-05-25 00:59:30 +0000476
477SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
478 SDOperand Chain = Op.getOperand(0);
479 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
480 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
481 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
482 SDOperand Callee = Op.getOperand(4);
483 MVT::ValueType RetVT= Op.Val->getValueType(0);
484 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000485
Evan Cheng88decde2006-04-28 21:29:37 +0000486 // Keep track of the number of XMM regs passed so far.
487 unsigned NumXMMRegs = 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000488 static const unsigned XMMArgRegs[] = {
489 X86::XMM0, X86::XMM1, X86::XMM2
490 };
Evan Cheng88decde2006-04-28 21:29:37 +0000491
Evan Cheng2a330942006-05-25 00:59:30 +0000492 // Count how many bytes are to be pushed on the stack.
493 unsigned NumBytes = 0;
494 for (unsigned i = 0; i != NumOps; ++i) {
495 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattner76ac0682005-11-15 00:40:23 +0000496
Evan Cheng2a330942006-05-25 00:59:30 +0000497 switch (Arg.getValueType()) {
498 default: assert(0 && "Unexpected ValueType for argument!");
499 case MVT::i8:
500 case MVT::i16:
501 case MVT::i32:
502 case MVT::f32:
503 NumBytes += 4;
504 break;
505 case MVT::i64:
506 case MVT::f64:
507 NumBytes += 8;
508 break;
509 case MVT::v16i8:
510 case MVT::v8i16:
511 case MVT::v4i32:
512 case MVT::v2i64:
513 case MVT::v4f32:
Evan Cheng0421aca2006-05-25 22:38:31 +0000514 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +0000515 if (NumXMMRegs < 3)
516 ++NumXMMRegs;
517 else
518 NumBytes += 16;
519 break;
520 }
Evan Cheng2a330942006-05-25 00:59:30 +0000521 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000522
Evan Cheng2a330942006-05-25 00:59:30 +0000523 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000524
Evan Cheng2a330942006-05-25 00:59:30 +0000525 // Arguments go on the stack in reverse order, as specified by the ABI.
526 unsigned ArgOffset = 0;
527 NumXMMRegs = 0;
528 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
529 std::vector<SDOperand> MemOpChains;
530 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
531 for (unsigned i = 0; i != NumOps; ++i) {
532 SDOperand Arg = Op.getOperand(5+2*i);
533
534 switch (Arg.getValueType()) {
535 default: assert(0 && "Unexpected ValueType for argument!");
536 case MVT::i8:
Evan Cheng5ee96892006-05-25 18:56:34 +0000537 case MVT::i16: {
Evan Cheng2a330942006-05-25 00:59:30 +0000538 // Promote the integer to 32 bits. If the input type is signed use a
539 // sign extend, otherwise use a zero extend.
540 unsigned ExtOp =
541 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
542 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
543 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng5ee96892006-05-25 18:56:34 +0000544 }
545 // Fallthrough
Evan Cheng2a330942006-05-25 00:59:30 +0000546
547 case MVT::i32:
548 case MVT::f32: {
549 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
550 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
551 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
552 Arg, PtrOff, DAG.getSrcValue(NULL)));
553 ArgOffset += 4;
554 break;
555 }
556 case MVT::i64:
557 case MVT::f64: {
558 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
559 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
560 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
561 Arg, PtrOff, DAG.getSrcValue(NULL)));
562 ArgOffset += 8;
563 break;
564 }
565 case MVT::v16i8:
566 case MVT::v8i16:
567 case MVT::v4i32:
568 case MVT::v2i64:
569 case MVT::v4f32:
Evan Cheng0421aca2006-05-25 22:38:31 +0000570 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +0000571 if (NumXMMRegs < 3) {
572 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
573 NumXMMRegs++;
574 } else {
Evan Cheng88decde2006-04-28 21:29:37 +0000575 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +0000576 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
577 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
578 Arg, PtrOff, DAG.getSrcValue(NULL)));
579 ArgOffset += 16;
Evan Cheng88decde2006-04-28 21:29:37 +0000580 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000581 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000582 }
583
Evan Cheng2a330942006-05-25 00:59:30 +0000584 if (!MemOpChains.empty())
585 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattner76ac0682005-11-15 00:40:23 +0000586
Evan Cheng88decde2006-04-28 21:29:37 +0000587 // Build a sequence of copy-to-reg nodes chained together with token chain
588 // and flag operands which copy the outgoing args into registers.
589 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +0000590 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
591 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
592 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +0000593 InFlag = Chain.getValue(1);
594 }
595
Evan Cheng2a330942006-05-25 00:59:30 +0000596 // If the callee is a GlobalAddress node (quite common, every direct call is)
597 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
598 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
599 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
600 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
601 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
602
Nate Begeman7e5496d2006-02-17 00:03:04 +0000603 std::vector<MVT::ValueType> NodeTys;
604 NodeTys.push_back(MVT::Other); // Returns a chain
605 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
606 std::vector<SDOperand> Ops;
607 Ops.push_back(Chain);
608 Ops.push_back(Callee);
Evan Cheng88decde2006-04-28 21:29:37 +0000609 if (InFlag.Val)
610 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000611
Evan Cheng2a330942006-05-25 00:59:30 +0000612 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
613 NodeTys, Ops);
Evan Cheng88decde2006-04-28 21:29:37 +0000614 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000615
Chris Lattner8be5be82006-05-23 18:50:38 +0000616 // Create the CALLSEQ_END node.
617 unsigned NumBytesForCalleeToPush = 0;
618
619 // If this is is a call to a struct-return function on Darwin/X86, the callee
620 // pops the hidden struct pointer, so we have to push it back.
621 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
622 NumBytesForCalleeToPush = 4;
623
Nate Begeman7e5496d2006-02-17 00:03:04 +0000624 NodeTys.clear();
625 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng2a330942006-05-25 00:59:30 +0000626 if (RetVT != MVT::Other)
627 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman7e5496d2006-02-17 00:03:04 +0000628 Ops.clear();
629 Ops.push_back(Chain);
630 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000631 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000632 Ops.push_back(InFlag);
633 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng2a330942006-05-25 00:59:30 +0000634 if (RetVT != MVT::Other)
635 InFlag = Chain.getValue(1);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000636
Evan Cheng2a330942006-05-25 00:59:30 +0000637 std::vector<SDOperand> ResultVals;
638 NodeTys.clear();
639 switch (RetVT) {
640 default: assert(0 && "Unknown value type to return!");
641 case MVT::Other: break;
642 case MVT::i8:
643 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
644 ResultVals.push_back(Chain.getValue(0));
645 NodeTys.push_back(MVT::i8);
646 break;
647 case MVT::i16:
648 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
649 ResultVals.push_back(Chain.getValue(0));
650 NodeTys.push_back(MVT::i16);
651 break;
652 case MVT::i32:
653 if (Op.Val->getValueType(1) == MVT::i32) {
654 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
655 ResultVals.push_back(Chain.getValue(0));
656 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
657 Chain.getValue(2)).getValue(1);
658 ResultVals.push_back(Chain.getValue(0));
659 NodeTys.push_back(MVT::i32);
660 } else {
661 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
662 ResultVals.push_back(Chain.getValue(0));
Evan Cheng45e190982006-01-05 00:27:02 +0000663 }
Evan Cheng2a330942006-05-25 00:59:30 +0000664 NodeTys.push_back(MVT::i32);
665 break;
666 case MVT::v16i8:
667 case MVT::v8i16:
668 case MVT::v4i32:
669 case MVT::v2i64:
670 case MVT::v4f32:
671 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +0000672 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
673 ResultVals.push_back(Chain.getValue(0));
674 NodeTys.push_back(RetVT);
675 break;
676 case MVT::f32:
677 case MVT::f64: {
678 std::vector<MVT::ValueType> Tys;
679 Tys.push_back(MVT::f64);
680 Tys.push_back(MVT::Other);
681 Tys.push_back(MVT::Flag);
682 std::vector<SDOperand> Ops;
683 Ops.push_back(Chain);
684 Ops.push_back(InFlag);
685 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
686 Chain = RetVal.getValue(1);
687 InFlag = RetVal.getValue(2);
688 if (X86ScalarSSE) {
689 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
690 // shouldn't be necessary except that RFP cannot be live across
691 // multiple blocks. When stackifier is fixed, they can be uncoupled.
692 MachineFunction &MF = DAG.getMachineFunction();
693 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
694 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
695 Tys.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +0000696 Tys.push_back(MVT::Other);
Evan Cheng2a330942006-05-25 00:59:30 +0000697 Ops.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +0000698 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +0000699 Ops.push_back(RetVal);
700 Ops.push_back(StackSlot);
701 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000702 Ops.push_back(InFlag);
Evan Cheng2a330942006-05-25 00:59:30 +0000703 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
704 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
705 DAG.getSrcValue(NULL));
Evan Cheng88decde2006-04-28 21:29:37 +0000706 Chain = RetVal.getValue(1);
Evan Cheng88decde2006-04-28 21:29:37 +0000707 }
Evan Cheng2a330942006-05-25 00:59:30 +0000708
709 if (RetVT == MVT::f32 && !X86ScalarSSE)
710 // FIXME: we would really like to remember that this FP_ROUND
711 // operation is okay to eliminate if we allow excess FP precision.
712 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
713 ResultVals.push_back(RetVal);
714 NodeTys.push_back(RetVT);
715 break;
716 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000717 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000718
Evan Cheng2a330942006-05-25 00:59:30 +0000719 // If the function returns void, just return the chain.
720 if (ResultVals.empty())
721 return Chain;
722
723 // Otherwise, merge everything together with a MERGE_VALUES node.
724 NodeTys.push_back(MVT::Other);
725 ResultVals.push_back(Chain);
726 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
727 return Res.getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000728}
729
Chris Lattner76ac0682005-11-15 00:40:23 +0000730//===----------------------------------------------------------------------===//
731// Fast Calling Convention implementation
732//===----------------------------------------------------------------------===//
733//
734// The X86 'fast' calling convention passes up to two integer arguments in
735// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
736// and requires that the callee pop its arguments off the stack (allowing proper
737// tail calls), and has the same return value conventions as C calling convs.
738//
739// This calling convention always arranges for the callee pop value to be 8n+4
740// bytes, which is needed for tail recursion elimination and stack alignment
741// reasons.
742//
743// Note that this can be enhanced in the future to pass fp vals in registers
744// (when we have a global fp allocator) and do other tricks.
745//
746
Chris Lattner388fc4d2006-03-17 17:27:47 +0000747// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
748// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
749// EDX". Anything more is illegal.
750//
751// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000752// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000753// a physreg with a virtreg, this increases the size of the physreg's live
754// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000755// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000756// allocator to wedge itself.
757//
758// This code triggers this problem more often if we pass args in registers,
759// so disable it until this is fixed.
760//
761// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
762// about code being dead.
763//
764static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000765
Chris Lattner76ac0682005-11-15 00:40:23 +0000766
Evan Cheng89001ad2006-04-27 08:31:10 +0000767/// HowToPassFastCCArgument - Returns how an formal argument of the specified
768/// type should be passed. If it is through stack, returns the size of the stack
Evan Cheng763f9b02006-05-26 18:25:43 +0000769/// slot; if it is through integer or XMM register, returns the number of
Evan Cheng89001ad2006-04-27 08:31:10 +0000770/// integer or XMM registers are needed.
Evan Cheng48940d12006-04-27 01:32:22 +0000771static void
Evan Cheng89001ad2006-04-27 08:31:10 +0000772HowToPassFastCCArgument(MVT::ValueType ObjectVT,
773 unsigned NumIntRegs, unsigned NumXMMRegs,
774 unsigned &ObjSize, unsigned &ObjIntRegs,
775 unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000776 ObjSize = 0;
777 NumIntRegs = 0;
Evan Cheng8aca43e2006-05-25 23:31:23 +0000778 NumXMMRegs = 0;
Evan Cheng48940d12006-04-27 01:32:22 +0000779
780 switch (ObjectVT) {
781 default: assert(0 && "Unhandled argument type!");
Evan Cheng48940d12006-04-27 01:32:22 +0000782 case MVT::i8:
783 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000784 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000785 else
786 ObjSize = 1;
787 break;
788 case MVT::i16:
789 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000790 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000791 else
792 ObjSize = 2;
793 break;
794 case MVT::i32:
795 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000796 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000797 else
798 ObjSize = 4;
799 break;
800 case MVT::i64:
801 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000802 ObjIntRegs = 2;
Evan Cheng48940d12006-04-27 01:32:22 +0000803 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000804 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000805 ObjSize = 4;
806 } else
807 ObjSize = 8;
808 case MVT::f32:
809 ObjSize = 4;
810 break;
811 case MVT::f64:
812 ObjSize = 8;
813 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000814 case MVT::v16i8:
815 case MVT::v8i16:
816 case MVT::v4i32:
817 case MVT::v2i64:
818 case MVT::v4f32:
819 case MVT::v2f64:
820 if (NumXMMRegs < 3)
821 ObjXMMRegs = 1;
822 else
823 ObjSize = 16;
824 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000825 }
826}
827
Evan Cheng17e734f2006-05-23 21:06:34 +0000828SDOperand
829X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
830 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattner76ac0682005-11-15 00:40:23 +0000831 MachineFunction &MF = DAG.getMachineFunction();
832 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000833 SDOperand Root = Op.getOperand(0);
834 std::vector<SDOperand> ArgValues;
Chris Lattner76ac0682005-11-15 00:40:23 +0000835
Evan Cheng48940d12006-04-27 01:32:22 +0000836 // Add DAG nodes to load the arguments... On entry to a function the stack
837 // frame looks like this:
838 //
839 // [ESP] -- return address
840 // [ESP + 4] -- first nonreg argument (leftmost lexically)
841 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
842 // ...
Chris Lattner76ac0682005-11-15 00:40:23 +0000843 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
844
845 // Keep track of the number of integer regs passed so far. This can be either
846 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
847 // used).
848 unsigned NumIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000849 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng2a330942006-05-25 00:59:30 +0000850
851 static const unsigned XMMArgRegs[] = {
852 X86::XMM0, X86::XMM1, X86::XMM2
853 };
Chris Lattner43798852006-03-17 05:10:20 +0000854
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000855 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000856 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
857 unsigned ArgIncrement = 4;
858 unsigned ObjSize = 0;
859 unsigned ObjIntRegs = 0;
860 unsigned ObjXMMRegs = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000861
Evan Cheng17e734f2006-05-23 21:06:34 +0000862 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
863 ObjSize, ObjIntRegs, ObjXMMRegs);
864 if (ObjSize >= 8)
865 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000866
Evan Cheng17e734f2006-05-23 21:06:34 +0000867 unsigned Reg;
868 SDOperand ArgValue;
869 if (ObjIntRegs || ObjXMMRegs) {
870 switch (ObjectVT) {
871 default: assert(0 && "Unhandled argument type!");
Evan Cheng17e734f2006-05-23 21:06:34 +0000872 case MVT::i8:
873 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
874 X86::GR8RegisterClass);
875 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
876 break;
877 case MVT::i16:
878 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
879 X86::GR16RegisterClass);
880 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
881 break;
882 case MVT::i32:
883 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
884 X86::GR32RegisterClass);
885 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
886 break;
887 case MVT::i64:
888 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
889 X86::GR32RegisterClass);
890 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
891 if (ObjIntRegs == 2) {
892 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
893 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
894 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000895 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000896 break;
897 case MVT::v16i8:
898 case MVT::v8i16:
899 case MVT::v4i32:
900 case MVT::v2i64:
901 case MVT::v4f32:
902 case MVT::v2f64:
903 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
904 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
905 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000906 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000907 NumIntRegs += ObjIntRegs;
908 NumXMMRegs += ObjXMMRegs;
Chris Lattner76ac0682005-11-15 00:40:23 +0000909 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000910
911 if (ObjSize) {
912 // Create the SelectionDAG nodes corresponding to a load from this
913 // parameter.
914 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
915 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
916 if (ObjectVT == MVT::i64 && ObjIntRegs) {
917 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
918 DAG.getSrcValue(NULL));
919 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
920 } else
921 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
922 DAG.getSrcValue(NULL));
923 ArgOffset += ArgIncrement; // Move on to the next argument.
924 }
925
926 ArgValues.push_back(ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000927 }
928
Evan Cheng17e734f2006-05-23 21:06:34 +0000929 ArgValues.push_back(Root);
930
Chris Lattner76ac0682005-11-15 00:40:23 +0000931 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
932 // arguments and the arguments after the retaddr has been pushed are aligned.
933 if ((ArgOffset & 7) == 0)
934 ArgOffset += 4;
935
936 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
937 ReturnAddrIndex = 0; // No return address slot generated yet.
938 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
939 BytesCallerReserves = 0;
940
941 // Finally, inform the code generator which regs we return values in.
Evan Cheng17e734f2006-05-23 21:06:34 +0000942 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000943 default: assert(0 && "Unknown type!");
944 case MVT::isVoid: break;
Chris Lattner76ac0682005-11-15 00:40:23 +0000945 case MVT::i8:
946 case MVT::i16:
947 case MVT::i32:
948 MF.addLiveOut(X86::EAX);
949 break;
950 case MVT::i64:
951 MF.addLiveOut(X86::EAX);
952 MF.addLiveOut(X86::EDX);
953 break;
954 case MVT::f32:
955 case MVT::f64:
956 MF.addLiveOut(X86::ST0);
957 break;
Evan Cheng5ee96892006-05-25 18:56:34 +0000958 case MVT::v16i8:
959 case MVT::v8i16:
960 case MVT::v4i32:
961 case MVT::v2i64:
962 case MVT::v4f32:
963 case MVT::v2f64:
Evan Cheng88decde2006-04-28 21:29:37 +0000964 MF.addLiveOut(X86::XMM0);
965 break;
966 }
Evan Cheng88decde2006-04-28 21:29:37 +0000967
Evan Cheng17e734f2006-05-23 21:06:34 +0000968 // Return the new list of results.
969 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
970 Op.Val->value_end());
971 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattner76ac0682005-11-15 00:40:23 +0000972}
973
Evan Cheng2a330942006-05-25 00:59:30 +0000974 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
975 SDOperand Chain = Op.getOperand(0);
976 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
977 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
978 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
979 SDOperand Callee = Op.getOperand(4);
980 MVT::ValueType RetVT= Op.Val->getValueType(0);
981 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
982
Chris Lattner76ac0682005-11-15 00:40:23 +0000983 // Count how many bytes are to be pushed on the stack.
984 unsigned NumBytes = 0;
985
986 // Keep track of the number of integer regs passed so far. This can be either
987 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
988 // used).
989 unsigned NumIntRegs = 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000990 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattner76ac0682005-11-15 00:40:23 +0000991
Evan Cheng2a330942006-05-25 00:59:30 +0000992 static const unsigned GPRArgRegs[][2] = {
993 { X86::AL, X86::DL },
994 { X86::AX, X86::DX },
995 { X86::EAX, X86::EDX }
996 };
997 static const unsigned XMMArgRegs[] = {
998 X86::XMM0, X86::XMM1, X86::XMM2
999 };
1000
1001 for (unsigned i = 0; i != NumOps; ++i) {
1002 SDOperand Arg = Op.getOperand(5+2*i);
1003
1004 switch (Arg.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001005 default: assert(0 && "Unknown value type!");
Chris Lattner76ac0682005-11-15 00:40:23 +00001006 case MVT::i8:
1007 case MVT::i16:
1008 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001009 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001010 ++NumIntRegs;
1011 break;
1012 }
Evan Cheng0421aca2006-05-25 22:38:31 +00001013 // Fall through
Chris Lattner76ac0682005-11-15 00:40:23 +00001014 case MVT::f32:
1015 NumBytes += 4;
1016 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001017 case MVT::f64:
1018 NumBytes += 8;
1019 break;
Evan Cheng2a330942006-05-25 00:59:30 +00001020 case MVT::v16i8:
1021 case MVT::v8i16:
1022 case MVT::v4i32:
1023 case MVT::v2i64:
1024 case MVT::v4f32:
Evan Cheng5ee96892006-05-25 18:56:34 +00001025 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +00001026 if (NumXMMRegs < 3)
1027 NumXMMRegs++;
1028 else
1029 NumBytes += 16;
1030 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001031 }
Evan Cheng2a330942006-05-25 00:59:30 +00001032 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001033
1034 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1035 // arguments and the arguments after the retaddr has been pushed are aligned.
1036 if ((NumBytes & 7) == 0)
1037 NumBytes += 4;
1038
Chris Lattner62c34842006-02-13 09:00:43 +00001039 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +00001040
1041 // Arguments go on the stack in reverse order, as specified by the ABI.
1042 unsigned ArgOffset = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +00001043 NumIntRegs = 0;
Evan Cheng2a330942006-05-25 00:59:30 +00001044 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1045 std::vector<SDOperand> MemOpChains;
1046 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1047 for (unsigned i = 0; i != NumOps; ++i) {
1048 SDOperand Arg = Op.getOperand(5+2*i);
1049
1050 switch (Arg.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001051 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattner76ac0682005-11-15 00:40:23 +00001052 case MVT::i8:
1053 case MVT::i16:
1054 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001055 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng2a330942006-05-25 00:59:30 +00001056 RegsToPass.push_back(
1057 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1058 Arg));
Chris Lattner76ac0682005-11-15 00:40:23 +00001059 ++NumIntRegs;
1060 break;
1061 }
1062 // Fall through
1063 case MVT::f32: {
1064 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001065 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1066 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1067 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner76ac0682005-11-15 00:40:23 +00001068 ArgOffset += 4;
1069 break;
1070 }
Evan Cheng2a330942006-05-25 00:59:30 +00001071 case MVT::f64: {
Chris Lattner76ac0682005-11-15 00:40:23 +00001072 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001073 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1074 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1075 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner76ac0682005-11-15 00:40:23 +00001076 ArgOffset += 8;
1077 break;
1078 }
Evan Cheng2a330942006-05-25 00:59:30 +00001079 case MVT::v16i8:
1080 case MVT::v8i16:
1081 case MVT::v4i32:
1082 case MVT::v2i64:
1083 case MVT::v4f32:
Evan Cheng5ee96892006-05-25 18:56:34 +00001084 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +00001085 if (NumXMMRegs < 3) {
1086 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1087 NumXMMRegs++;
1088 } else {
1089 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1090 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1091 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1092 Arg, PtrOff, DAG.getSrcValue(NULL)));
1093 ArgOffset += 16;
1094 }
1095 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001096 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001097
Evan Cheng2a330942006-05-25 00:59:30 +00001098 if (!MemOpChains.empty())
1099 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattner76ac0682005-11-15 00:40:23 +00001100
Nate Begeman7e5496d2006-02-17 00:03:04 +00001101 // Build a sequence of copy-to-reg nodes chained together with token chain
1102 // and flag operands which copy the outgoing args into registers.
1103 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001104 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1105 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1106 InFlag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001107 InFlag = Chain.getValue(1);
1108 }
1109
Evan Cheng2a330942006-05-25 00:59:30 +00001110 // If the callee is a GlobalAddress node (quite common, every direct call is)
1111 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1112 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1113 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1114 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1115 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1116
Nate Begeman7e5496d2006-02-17 00:03:04 +00001117 std::vector<MVT::ValueType> NodeTys;
1118 NodeTys.push_back(MVT::Other); // Returns a chain
1119 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1120 std::vector<SDOperand> Ops;
1121 Ops.push_back(Chain);
1122 Ops.push_back(Callee);
1123 if (InFlag.Val)
1124 Ops.push_back(InFlag);
1125
1126 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001127 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1128 NodeTys, Ops);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001129 InFlag = Chain.getValue(1);
1130
1131 NodeTys.clear();
1132 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng2a330942006-05-25 00:59:30 +00001133 if (RetVT != MVT::Other)
1134 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman7e5496d2006-02-17 00:03:04 +00001135 Ops.clear();
1136 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001137 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1138 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001139 Ops.push_back(InFlag);
1140 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng2a330942006-05-25 00:59:30 +00001141 if (RetVT != MVT::Other)
1142 InFlag = Chain.getValue(1);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001143
Evan Cheng2a330942006-05-25 00:59:30 +00001144 std::vector<SDOperand> ResultVals;
1145 NodeTys.clear();
1146 switch (RetVT) {
1147 default: assert(0 && "Unknown value type to return!");
1148 case MVT::Other: break;
1149 case MVT::i8:
1150 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1151 ResultVals.push_back(Chain.getValue(0));
1152 NodeTys.push_back(MVT::i8);
1153 break;
1154 case MVT::i16:
1155 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1156 ResultVals.push_back(Chain.getValue(0));
1157 NodeTys.push_back(MVT::i16);
1158 break;
1159 case MVT::i32:
1160 if (Op.Val->getValueType(1) == MVT::i32) {
1161 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1162 ResultVals.push_back(Chain.getValue(0));
1163 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1164 Chain.getValue(2)).getValue(1);
1165 ResultVals.push_back(Chain.getValue(0));
1166 NodeTys.push_back(MVT::i32);
1167 } else {
1168 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1169 ResultVals.push_back(Chain.getValue(0));
Evan Cheng172fce72006-01-06 00:43:03 +00001170 }
Evan Cheng2a330942006-05-25 00:59:30 +00001171 NodeTys.push_back(MVT::i32);
1172 break;
1173 case MVT::v16i8:
1174 case MVT::v8i16:
1175 case MVT::v4i32:
1176 case MVT::v2i64:
1177 case MVT::v4f32:
1178 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +00001179 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1180 ResultVals.push_back(Chain.getValue(0));
1181 NodeTys.push_back(RetVT);
1182 break;
1183 case MVT::f32:
1184 case MVT::f64: {
1185 std::vector<MVT::ValueType> Tys;
1186 Tys.push_back(MVT::f64);
1187 Tys.push_back(MVT::Other);
1188 Tys.push_back(MVT::Flag);
1189 std::vector<SDOperand> Ops;
1190 Ops.push_back(Chain);
1191 Ops.push_back(InFlag);
1192 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1193 Chain = RetVal.getValue(1);
1194 InFlag = RetVal.getValue(2);
1195 if (X86ScalarSSE) {
1196 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1197 // shouldn't be necessary except that RFP cannot be live across
1198 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1199 MachineFunction &MF = DAG.getMachineFunction();
1200 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1201 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1202 Tys.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +00001203 Tys.push_back(MVT::Other);
Evan Cheng2a330942006-05-25 00:59:30 +00001204 Ops.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +00001205 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001206 Ops.push_back(RetVal);
1207 Ops.push_back(StackSlot);
1208 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001209 Ops.push_back(InFlag);
Evan Cheng2a330942006-05-25 00:59:30 +00001210 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1211 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1212 DAG.getSrcValue(NULL));
1213 Chain = RetVal.getValue(1);
1214 }
Evan Cheng172fce72006-01-06 00:43:03 +00001215
Evan Cheng2a330942006-05-25 00:59:30 +00001216 if (RetVT == MVT::f32 && !X86ScalarSSE)
1217 // FIXME: we would really like to remember that this FP_ROUND
1218 // operation is okay to eliminate if we allow excess FP precision.
1219 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1220 ResultVals.push_back(RetVal);
1221 NodeTys.push_back(RetVT);
1222 break;
1223 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001224 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001225
Evan Cheng2a330942006-05-25 00:59:30 +00001226
1227 // If the function returns void, just return the chain.
1228 if (ResultVals.empty())
1229 return Chain;
1230
1231 // Otherwise, merge everything together with a MERGE_VALUES node.
1232 NodeTys.push_back(MVT::Other);
1233 ResultVals.push_back(Chain);
1234 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1235 return Res.getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001236}
1237
1238SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1239 if (ReturnAddrIndex == 0) {
1240 // Set up a frame object for the return address.
1241 MachineFunction &MF = DAG.getMachineFunction();
1242 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1243 }
1244
1245 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1246}
1247
1248
1249
1250std::pair<SDOperand, SDOperand> X86TargetLowering::
1251LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1252 SelectionDAG &DAG) {
1253 SDOperand Result;
1254 if (Depth) // Depths > 0 not supported yet!
1255 Result = DAG.getConstant(0, getPointerTy());
1256 else {
1257 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1258 if (!isFrameAddress)
1259 // Just load the return address
1260 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1261 DAG.getSrcValue(NULL));
1262 else
1263 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1264 DAG.getConstant(4, MVT::i32));
1265 }
1266 return std::make_pair(Result, Chain);
1267}
1268
Evan Cheng339edad2006-01-11 00:33:36 +00001269/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1270/// which corresponds to the condition code.
1271static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1272 switch (X86CC) {
1273 default: assert(0 && "Unknown X86 conditional code!");
1274 case X86ISD::COND_A: return X86::JA;
1275 case X86ISD::COND_AE: return X86::JAE;
1276 case X86ISD::COND_B: return X86::JB;
1277 case X86ISD::COND_BE: return X86::JBE;
1278 case X86ISD::COND_E: return X86::JE;
1279 case X86ISD::COND_G: return X86::JG;
1280 case X86ISD::COND_GE: return X86::JGE;
1281 case X86ISD::COND_L: return X86::JL;
1282 case X86ISD::COND_LE: return X86::JLE;
1283 case X86ISD::COND_NE: return X86::JNE;
1284 case X86ISD::COND_NO: return X86::JNO;
1285 case X86ISD::COND_NP: return X86::JNP;
1286 case X86ISD::COND_NS: return X86::JNS;
1287 case X86ISD::COND_O: return X86::JO;
1288 case X86ISD::COND_P: return X86::JP;
1289 case X86ISD::COND_S: return X86::JS;
1290 }
1291}
Chris Lattner76ac0682005-11-15 00:40:23 +00001292
Evan Cheng45df7f82006-01-30 23:41:35 +00001293/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1294/// specific condition code. It returns a false if it cannot do a direct
1295/// translation. X86CC is the translated CondCode. Flip is set to true if the
1296/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001297static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1298 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001299 Flip = false;
1300 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001301 if (!isFP) {
1302 switch (SetCCOpcode) {
1303 default: break;
1304 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1305 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1306 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1307 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1308 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1309 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1310 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1311 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1312 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1313 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1314 }
1315 } else {
1316 // On a floating point condition, the flags are set as follows:
1317 // ZF PF CF op
1318 // 0 | 0 | 0 | X > Y
1319 // 0 | 0 | 1 | X < Y
1320 // 1 | 0 | 0 | X == Y
1321 // 1 | 1 | 1 | unordered
1322 switch (SetCCOpcode) {
1323 default: break;
1324 case ISD::SETUEQ:
1325 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001326 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001327 case ISD::SETOGT:
1328 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001329 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001330 case ISD::SETOGE:
1331 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001332 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001333 case ISD::SETULT:
1334 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001335 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001336 case ISD::SETULE:
1337 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1338 case ISD::SETONE:
1339 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1340 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1341 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1342 }
1343 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001344
1345 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001346}
1347
Evan Cheng78038292006-04-05 23:38:46 +00001348static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1349 bool &Flip) {
1350 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1351}
1352
Evan Cheng339edad2006-01-11 00:33:36 +00001353/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1354/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001355/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001356static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001357 switch (X86CC) {
1358 default:
1359 return false;
1360 case X86ISD::COND_B:
1361 case X86ISD::COND_BE:
1362 case X86ISD::COND_E:
1363 case X86ISD::COND_P:
1364 case X86ISD::COND_A:
1365 case X86ISD::COND_AE:
1366 case X86ISD::COND_NE:
1367 case X86ISD::COND_NP:
1368 return true;
1369 }
1370}
1371
Evan Cheng339edad2006-01-11 00:33:36 +00001372MachineBasicBlock *
1373X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1374 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001375 switch (MI->getOpcode()) {
1376 default: assert(false && "Unexpected instr type to insert");
1377 case X86::CMOV_FR32:
Evan Cheng617a6a82006-04-10 07:23:14 +00001378 case X86::CMOV_FR64:
1379 case X86::CMOV_V4F32:
1380 case X86::CMOV_V2F64:
1381 case X86::CMOV_V2I64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001382 // To "insert" a SELECT_CC instruction, we actually have to insert the
1383 // diamond control-flow pattern. The incoming instruction knows the
1384 // destination vreg to set, the condition code register to branch on, the
1385 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001386 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1387 ilist<MachineBasicBlock>::iterator It = BB;
1388 ++It;
1389
1390 // thisMBB:
1391 // ...
1392 // TrueVal = ...
1393 // cmpTY ccX, r1, r2
1394 // bCC copy1MBB
1395 // fallthrough --> copy0MBB
1396 MachineBasicBlock *thisMBB = BB;
1397 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1398 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1399 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1400 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1401 MachineFunction *F = BB->getParent();
1402 F->getBasicBlockList().insert(It, copy0MBB);
1403 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001404 // Update machine-CFG edges by first adding all successors of the current
1405 // block to the new block which will contain the Phi node for the select.
1406 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1407 e = BB->succ_end(); i != e; ++i)
1408 sinkMBB->addSuccessor(*i);
1409 // Next, remove all successors of the current block, and add the true
1410 // and fallthrough blocks as its successors.
1411 while(!BB->succ_empty())
1412 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001413 BB->addSuccessor(copy0MBB);
1414 BB->addSuccessor(sinkMBB);
1415
1416 // copy0MBB:
1417 // %FalseValue = ...
1418 // # fallthrough to sinkMBB
1419 BB = copy0MBB;
1420
1421 // Update machine-CFG edges
1422 BB->addSuccessor(sinkMBB);
1423
1424 // sinkMBB:
1425 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1426 // ...
1427 BB = sinkMBB;
1428 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1429 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1430 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001431
Evan Cheng911c68d2006-01-16 21:21:29 +00001432 delete MI; // The pseudo instruction is gone now.
1433 return BB;
1434 }
Evan Cheng339edad2006-01-11 00:33:36 +00001435
Evan Cheng911c68d2006-01-16 21:21:29 +00001436 case X86::FP_TO_INT16_IN_MEM:
1437 case X86::FP_TO_INT32_IN_MEM:
1438 case X86::FP_TO_INT64_IN_MEM: {
1439 // Change the floating point control register to use "round towards zero"
1440 // mode when truncating to an integer value.
1441 MachineFunction *F = BB->getParent();
1442 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1443 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1444
1445 // Load the old value of the high byte of the control word...
1446 unsigned OldCW =
Evan Cheng9fee4422006-05-16 07:21:53 +00001447 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng911c68d2006-01-16 21:21:29 +00001448 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1449
1450 // Set the high part to be round to zero...
1451 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1452
1453 // Reload the modified control word now...
1454 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1455
1456 // Restore the memory image of control word to original value
1457 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1458
1459 // Get the X86 opcode to use.
1460 unsigned Opc;
1461 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001462 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001463 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1464 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1465 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1466 }
1467
1468 X86AddressMode AM;
1469 MachineOperand &Op = MI->getOperand(0);
1470 if (Op.isRegister()) {
1471 AM.BaseType = X86AddressMode::RegBase;
1472 AM.Base.Reg = Op.getReg();
1473 } else {
1474 AM.BaseType = X86AddressMode::FrameIndexBase;
1475 AM.Base.FrameIndex = Op.getFrameIndex();
1476 }
1477 Op = MI->getOperand(1);
1478 if (Op.isImmediate())
1479 AM.Scale = Op.getImmedValue();
1480 Op = MI->getOperand(2);
1481 if (Op.isImmediate())
1482 AM.IndexReg = Op.getImmedValue();
1483 Op = MI->getOperand(3);
1484 if (Op.isGlobalAddress()) {
1485 AM.GV = Op.getGlobal();
1486 } else {
1487 AM.Disp = Op.getImmedValue();
1488 }
1489 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1490
1491 // Reload the original control word now.
1492 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1493
1494 delete MI; // The pseudo instruction is gone now.
1495 return BB;
1496 }
1497 }
Evan Cheng339edad2006-01-11 00:33:36 +00001498}
1499
1500
1501//===----------------------------------------------------------------------===//
1502// X86 Custom Lowering Hooks
1503//===----------------------------------------------------------------------===//
1504
Evan Chengaf598d22006-03-13 23:18:16 +00001505/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1506/// load. For Darwin, external and weak symbols are indirect, loading the value
1507/// at address GV rather then the value of GV itself. This means that the
1508/// GlobalAddress must be in the base or index register of the address, not the
1509/// GV offset field.
1510static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1511 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1512 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1513}
1514
Evan Chengc995b452006-04-06 23:23:56 +00001515/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001516/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001517static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1518 if (Op.getOpcode() == ISD::UNDEF)
1519 return true;
1520
1521 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001522 return (Val >= Low && Val < Hi);
1523}
1524
1525/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1526/// true if Op is undef or if its value equal to the specified value.
1527static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1528 if (Op.getOpcode() == ISD::UNDEF)
1529 return true;
1530 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001531}
1532
Evan Cheng68ad48b2006-03-22 18:59:22 +00001533/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1534/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1535bool X86::isPSHUFDMask(SDNode *N) {
1536 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1537
1538 if (N->getNumOperands() != 4)
1539 return false;
1540
1541 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001542 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001543 SDOperand Arg = N->getOperand(i);
1544 if (Arg.getOpcode() == ISD::UNDEF) continue;
1545 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1546 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001547 return false;
1548 }
1549
1550 return true;
1551}
1552
1553/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001554/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001555bool X86::isPSHUFHWMask(SDNode *N) {
1556 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1557
1558 if (N->getNumOperands() != 8)
1559 return false;
1560
1561 // Lower quadword copied in order.
1562 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001563 SDOperand Arg = N->getOperand(i);
1564 if (Arg.getOpcode() == ISD::UNDEF) continue;
1565 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1566 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001567 return false;
1568 }
1569
1570 // Upper quadword shuffled.
1571 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001572 SDOperand Arg = N->getOperand(i);
1573 if (Arg.getOpcode() == ISD::UNDEF) continue;
1574 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1575 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001576 if (Val < 4 || Val > 7)
1577 return false;
1578 }
1579
1580 return true;
1581}
1582
1583/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001584/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001585bool X86::isPSHUFLWMask(SDNode *N) {
1586 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1587
1588 if (N->getNumOperands() != 8)
1589 return false;
1590
1591 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001592 for (unsigned i = 4; i != 8; ++i)
1593 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001594 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001595
1596 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001597 for (unsigned i = 0; i != 4; ++i)
1598 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001599 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001600
1601 return true;
1602}
1603
Evan Chengd27fb3e2006-03-24 01:18:28 +00001604/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1605/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng60f0b892006-04-20 08:58:49 +00001606static bool isSHUFPMask(std::vector<SDOperand> &N) {
1607 unsigned NumElems = N.size();
1608 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001609
Evan Cheng60f0b892006-04-20 08:58:49 +00001610 unsigned Half = NumElems / 2;
1611 for (unsigned i = 0; i < Half; ++i)
1612 if (!isUndefOrInRange(N[i], 0, NumElems))
1613 return false;
1614 for (unsigned i = Half; i < NumElems; ++i)
1615 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1616 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001617
1618 return true;
1619}
1620
Evan Cheng60f0b892006-04-20 08:58:49 +00001621bool X86::isSHUFPMask(SDNode *N) {
1622 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1623 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1624 return ::isSHUFPMask(Ops);
1625}
1626
1627/// isCommutedSHUFP - Returns true if the shuffle mask is except
1628/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1629/// half elements to come from vector 1 (which would equal the dest.) and
1630/// the upper half to come from vector 2.
1631static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1632 unsigned NumElems = Ops.size();
1633 if (NumElems != 2 && NumElems != 4) return false;
1634
1635 unsigned Half = NumElems / 2;
1636 for (unsigned i = 0; i < Half; ++i)
1637 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1638 return false;
1639 for (unsigned i = Half; i < NumElems; ++i)
1640 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1641 return false;
1642 return true;
1643}
1644
1645static bool isCommutedSHUFP(SDNode *N) {
1646 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1647 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1648 return isCommutedSHUFP(Ops);
1649}
1650
Evan Cheng2595a682006-03-24 02:58:06 +00001651/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1652/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1653bool X86::isMOVHLPSMask(SDNode *N) {
1654 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1655
Evan Cheng1a194a52006-03-28 06:50:32 +00001656 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001657 return false;
1658
Evan Cheng1a194a52006-03-28 06:50:32 +00001659 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001660 return isUndefOrEqual(N->getOperand(0), 6) &&
1661 isUndefOrEqual(N->getOperand(1), 7) &&
1662 isUndefOrEqual(N->getOperand(2), 2) &&
1663 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001664}
1665
Evan Chengc995b452006-04-06 23:23:56 +00001666/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1667/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1668bool X86::isMOVLPMask(SDNode *N) {
1669 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1670
1671 unsigned NumElems = N->getNumOperands();
1672 if (NumElems != 2 && NumElems != 4)
1673 return false;
1674
Evan Chengac847262006-04-07 21:53:05 +00001675 for (unsigned i = 0; i < NumElems/2; ++i)
1676 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1677 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001678
Evan Chengac847262006-04-07 21:53:05 +00001679 for (unsigned i = NumElems/2; i < NumElems; ++i)
1680 if (!isUndefOrEqual(N->getOperand(i), i))
1681 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001682
1683 return true;
1684}
1685
1686/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001687/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1688/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001689bool X86::isMOVHPMask(SDNode *N) {
1690 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1691
1692 unsigned NumElems = N->getNumOperands();
1693 if (NumElems != 2 && NumElems != 4)
1694 return false;
1695
Evan Chengac847262006-04-07 21:53:05 +00001696 for (unsigned i = 0; i < NumElems/2; ++i)
1697 if (!isUndefOrEqual(N->getOperand(i), i))
1698 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001699
1700 for (unsigned i = 0; i < NumElems/2; ++i) {
1701 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001702 if (!isUndefOrEqual(Arg, i + NumElems))
1703 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001704 }
1705
1706 return true;
1707}
1708
Evan Cheng5df75882006-03-28 00:39:58 +00001709/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1710/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng60f0b892006-04-20 08:58:49 +00001711bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1712 unsigned NumElems = N.size();
Evan Cheng5df75882006-03-28 00:39:58 +00001713 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1714 return false;
1715
1716 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001717 SDOperand BitI = N[i];
1718 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001719 if (!isUndefOrEqual(BitI, j))
1720 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001721 if (V2IsSplat) {
1722 if (isUndefOrEqual(BitI1, NumElems))
1723 return false;
1724 } else {
1725 if (!isUndefOrEqual(BitI1, j + NumElems))
1726 return false;
1727 }
Evan Cheng5df75882006-03-28 00:39:58 +00001728 }
1729
1730 return true;
1731}
1732
Evan Cheng60f0b892006-04-20 08:58:49 +00001733bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1734 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1735 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1736 return ::isUNPCKLMask(Ops, V2IsSplat);
1737}
1738
Evan Cheng2bc32802006-03-28 02:43:26 +00001739/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1740/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng60f0b892006-04-20 08:58:49 +00001741bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1742 unsigned NumElems = N.size();
Evan Cheng2bc32802006-03-28 02:43:26 +00001743 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1744 return false;
1745
1746 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001747 SDOperand BitI = N[i];
1748 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001749 if (!isUndefOrEqual(BitI, j + NumElems/2))
1750 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001751 if (V2IsSplat) {
1752 if (isUndefOrEqual(BitI1, NumElems))
1753 return false;
1754 } else {
1755 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1756 return false;
1757 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001758 }
1759
1760 return true;
1761}
1762
Evan Cheng60f0b892006-04-20 08:58:49 +00001763bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1764 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1765 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1766 return ::isUNPCKHMask(Ops, V2IsSplat);
1767}
1768
Evan Chengf3b52c82006-04-05 07:20:06 +00001769/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1770/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1771/// <0, 0, 1, 1>
1772bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1773 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1774
1775 unsigned NumElems = N->getNumOperands();
1776 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1777 return false;
1778
1779 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1780 SDOperand BitI = N->getOperand(i);
1781 SDOperand BitI1 = N->getOperand(i+1);
1782
Evan Chengac847262006-04-07 21:53:05 +00001783 if (!isUndefOrEqual(BitI, j))
1784 return false;
1785 if (!isUndefOrEqual(BitI1, j))
1786 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001787 }
1788
1789 return true;
1790}
1791
Evan Chenge8b51802006-04-21 01:05:10 +00001792/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1793/// specifies a shuffle of elements that is suitable for input to MOVSS,
1794/// MOVSD, and MOVD, i.e. setting the lowest element.
1795static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001796 unsigned NumElems = N.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001797 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001798 return false;
1799
Evan Cheng60f0b892006-04-20 08:58:49 +00001800 if (!isUndefOrEqual(N[0], NumElems))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001801 return false;
1802
1803 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001804 SDOperand Arg = N[i];
Evan Cheng12ba3e22006-04-11 00:19:04 +00001805 if (!isUndefOrEqual(Arg, i))
1806 return false;
1807 }
1808
1809 return true;
1810}
Evan Chengf3b52c82006-04-05 07:20:06 +00001811
Evan Chenge8b51802006-04-21 01:05:10 +00001812bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001813 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1814 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001815 return ::isMOVLMask(Ops);
Evan Cheng60f0b892006-04-20 08:58:49 +00001816}
1817
Evan Chenge8b51802006-04-21 01:05:10 +00001818/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1819/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001820/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Chenge8b51802006-04-21 01:05:10 +00001821static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001822 unsigned NumElems = Ops.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001823 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001824 return false;
1825
1826 if (!isUndefOrEqual(Ops[0], 0))
1827 return false;
1828
1829 for (unsigned i = 1; i < NumElems; ++i) {
1830 SDOperand Arg = Ops[i];
1831 if (V2IsSplat) {
1832 if (!isUndefOrEqual(Arg, NumElems))
1833 return false;
1834 } else {
1835 if (!isUndefOrEqual(Arg, i+NumElems))
1836 return false;
1837 }
1838 }
1839
1840 return true;
1841}
1842
Evan Chenge8b51802006-04-21 01:05:10 +00001843static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001844 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1845 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001846 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001847}
1848
Evan Cheng5d247f82006-04-14 21:59:03 +00001849/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1850/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1851bool X86::isMOVSHDUPMask(SDNode *N) {
1852 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1853
1854 if (N->getNumOperands() != 4)
1855 return false;
1856
1857 // Expect 1, 1, 3, 3
1858 for (unsigned i = 0; i < 2; ++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 != 1) return false;
1864 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001865
1866 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001867 for (unsigned i = 2; i < 4; ++i) {
1868 SDOperand Arg = N->getOperand(i);
1869 if (Arg.getOpcode() == ISD::UNDEF) continue;
1870 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1871 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1872 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001873 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001874 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001875
Evan Cheng6222cf22006-04-15 05:37:34 +00001876 // Don't use movshdup if it can be done with a shufps.
1877 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001878}
1879
1880/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1881/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1882bool X86::isMOVSLDUPMask(SDNode *N) {
1883 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1884
1885 if (N->getNumOperands() != 4)
1886 return false;
1887
1888 // Expect 0, 0, 2, 2
1889 for (unsigned i = 0; i < 2; ++i) {
1890 SDOperand Arg = N->getOperand(i);
1891 if (Arg.getOpcode() == ISD::UNDEF) continue;
1892 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1893 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1894 if (Val != 0) return false;
1895 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001896
1897 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001898 for (unsigned i = 2; i < 4; ++i) {
1899 SDOperand Arg = N->getOperand(i);
1900 if (Arg.getOpcode() == ISD::UNDEF) continue;
1901 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1902 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1903 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001904 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001905 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001906
Evan Cheng6222cf22006-04-15 05:37:34 +00001907 // Don't use movshdup if it can be done with a shufps.
1908 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001909}
1910
Evan Chengd097e672006-03-22 02:53:00 +00001911/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1912/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001913static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001914 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1915
Evan Chengd097e672006-03-22 02:53:00 +00001916 // This is a splat operation if each element of the permute is the same, and
1917 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001918 unsigned NumElems = N->getNumOperands();
1919 SDOperand ElementBase;
1920 unsigned i = 0;
1921 for (; i != NumElems; ++i) {
1922 SDOperand Elt = N->getOperand(i);
1923 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1924 ElementBase = Elt;
1925 break;
1926 }
1927 }
1928
1929 if (!ElementBase.Val)
1930 return false;
1931
1932 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001933 SDOperand Arg = N->getOperand(i);
1934 if (Arg.getOpcode() == ISD::UNDEF) continue;
1935 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001936 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001937 }
1938
1939 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001940 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00001941}
1942
Evan Cheng5022b342006-04-17 20:43:08 +00001943/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1944/// a splat of a single element and it's a 2 or 4 element mask.
1945bool X86::isSplatMask(SDNode *N) {
1946 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1947
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001948 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00001949 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1950 return false;
1951 return ::isSplatMask(N);
1952}
1953
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001954/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1955/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1956/// instructions.
1957unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001958 unsigned NumOperands = N->getNumOperands();
1959 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1960 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001961 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001962 unsigned Val = 0;
1963 SDOperand Arg = N->getOperand(NumOperands-i-1);
1964 if (Arg.getOpcode() != ISD::UNDEF)
1965 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001966 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001967 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001968 if (i != NumOperands - 1)
1969 Mask <<= Shift;
1970 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001971
1972 return Mask;
1973}
1974
Evan Chengb7fedff2006-03-29 23:07:14 +00001975/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1976/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1977/// instructions.
1978unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1979 unsigned Mask = 0;
1980 // 8 nodes, but we only care about the last 4.
1981 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001982 unsigned Val = 0;
1983 SDOperand Arg = N->getOperand(i);
1984 if (Arg.getOpcode() != ISD::UNDEF)
1985 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001986 Mask |= (Val - 4);
1987 if (i != 4)
1988 Mask <<= 2;
1989 }
1990
1991 return Mask;
1992}
1993
1994/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1995/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1996/// instructions.
1997unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1998 unsigned Mask = 0;
1999 // 8 nodes, but we only care about the first 4.
2000 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002001 unsigned Val = 0;
2002 SDOperand Arg = N->getOperand(i);
2003 if (Arg.getOpcode() != ISD::UNDEF)
2004 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002005 Mask |= Val;
2006 if (i != 0)
2007 Mask <<= 2;
2008 }
2009
2010 return Mask;
2011}
2012
Evan Cheng59a63552006-04-05 01:47:37 +00002013/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2014/// specifies a 8 element shuffle that can be broken into a pair of
2015/// PSHUFHW and PSHUFLW.
2016static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2017 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2018
2019 if (N->getNumOperands() != 8)
2020 return false;
2021
2022 // Lower quadword shuffled.
2023 for (unsigned i = 0; i != 4; ++i) {
2024 SDOperand Arg = N->getOperand(i);
2025 if (Arg.getOpcode() == ISD::UNDEF) continue;
2026 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2027 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2028 if (Val > 4)
2029 return false;
2030 }
2031
2032 // Upper quadword shuffled.
2033 for (unsigned i = 4; i != 8; ++i) {
2034 SDOperand Arg = N->getOperand(i);
2035 if (Arg.getOpcode() == ISD::UNDEF) continue;
2036 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2037 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2038 if (Val < 4 || Val > 7)
2039 return false;
2040 }
2041
2042 return true;
2043}
2044
Evan Chengc995b452006-04-06 23:23:56 +00002045/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2046/// values in ther permute mask.
2047static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2048 SDOperand V1 = Op.getOperand(0);
2049 SDOperand V2 = Op.getOperand(1);
2050 SDOperand Mask = Op.getOperand(2);
2051 MVT::ValueType VT = Op.getValueType();
2052 MVT::ValueType MaskVT = Mask.getValueType();
2053 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2054 unsigned NumElems = Mask.getNumOperands();
2055 std::vector<SDOperand> MaskVec;
2056
2057 for (unsigned i = 0; i != NumElems; ++i) {
2058 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002059 if (Arg.getOpcode() == ISD::UNDEF) {
2060 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2061 continue;
2062 }
Evan Chengc995b452006-04-06 23:23:56 +00002063 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2064 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2065 if (Val < NumElems)
2066 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2067 else
2068 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2069 }
2070
2071 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2072 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2073}
2074
Evan Cheng7855e4d2006-04-19 20:35:22 +00002075/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2076/// match movhlps. The lower half elements should come from upper half of
2077/// V1 (and in order), and the upper half elements should come from the upper
2078/// half of V2 (and in order).
2079static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2080 unsigned NumElems = Mask->getNumOperands();
2081 if (NumElems != 4)
2082 return false;
2083 for (unsigned i = 0, e = 2; i != e; ++i)
2084 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2085 return false;
2086 for (unsigned i = 2; i != 4; ++i)
2087 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2088 return false;
2089 return true;
2090}
2091
Evan Chengc995b452006-04-06 23:23:56 +00002092/// isScalarLoadToVector - Returns true if the node is a scalar load that
2093/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002094static inline bool isScalarLoadToVector(SDNode *N) {
2095 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2096 N = N->getOperand(0).Val;
2097 return (N->getOpcode() == ISD::LOAD);
Evan Chengc995b452006-04-06 23:23:56 +00002098 }
2099 return false;
2100}
2101
Evan Cheng7855e4d2006-04-19 20:35:22 +00002102/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2103/// match movlp{s|d}. The lower half elements should come from lower half of
2104/// V1 (and in order), and the upper half elements should come from the upper
2105/// half of V2 (and in order). And since V1 will become the source of the
2106/// MOVLP, it must be either a vector load or a scalar load to vector.
2107static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2108 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2109 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002110
Evan Cheng7855e4d2006-04-19 20:35:22 +00002111 unsigned NumElems = Mask->getNumOperands();
2112 if (NumElems != 2 && NumElems != 4)
2113 return false;
2114 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2115 if (!isUndefOrEqual(Mask->getOperand(i), i))
2116 return false;
2117 for (unsigned i = NumElems/2; i != NumElems; ++i)
2118 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2119 return false;
2120 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002121}
2122
Evan Cheng60f0b892006-04-20 08:58:49 +00002123/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2124/// all the same.
2125static bool isSplatVector(SDNode *N) {
2126 if (N->getOpcode() != ISD::BUILD_VECTOR)
2127 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002128
Evan Cheng60f0b892006-04-20 08:58:49 +00002129 SDOperand SplatValue = N->getOperand(0);
2130 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2131 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002132 return false;
2133 return true;
2134}
2135
Evan Cheng60f0b892006-04-20 08:58:49 +00002136/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2137/// that point to V2 points to its first element.
2138static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2139 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2140
2141 bool Changed = false;
2142 std::vector<SDOperand> MaskVec;
2143 unsigned NumElems = Mask.getNumOperands();
2144 for (unsigned i = 0; i != NumElems; ++i) {
2145 SDOperand Arg = Mask.getOperand(i);
2146 if (Arg.getOpcode() != ISD::UNDEF) {
2147 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2148 if (Val > NumElems) {
2149 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2150 Changed = true;
2151 }
2152 }
2153 MaskVec.push_back(Arg);
2154 }
2155
2156 if (Changed)
2157 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2158 return Mask;
2159}
2160
Evan Chenge8b51802006-04-21 01:05:10 +00002161/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2162/// operation of specified width.
2163static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002164 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2165 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2166
2167 std::vector<SDOperand> MaskVec;
2168 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2169 for (unsigned i = 1; i != NumElems; ++i)
2170 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2171 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2172}
2173
Evan Cheng5022b342006-04-17 20:43:08 +00002174/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2175/// of specified width.
2176static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2177 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2178 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2179 std::vector<SDOperand> MaskVec;
2180 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2181 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2182 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2183 }
2184 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2185}
2186
Evan Cheng60f0b892006-04-20 08:58:49 +00002187/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2188/// of specified width.
2189static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2190 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2191 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2192 unsigned Half = NumElems/2;
2193 std::vector<SDOperand> MaskVec;
2194 for (unsigned i = 0; i != Half; ++i) {
2195 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2196 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2197 }
2198 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2199}
2200
Evan Chenge8b51802006-04-21 01:05:10 +00002201/// getZeroVector - Returns a vector of specified type with all zero elements.
2202///
2203static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2204 assert(MVT::isVector(VT) && "Expected a vector type");
2205 unsigned NumElems = getVectorNumElements(VT);
2206 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2207 bool isFP = MVT::isFloatingPoint(EVT);
2208 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2209 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2210 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2211}
2212
Evan Cheng5022b342006-04-17 20:43:08 +00002213/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2214///
2215static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2216 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002217 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002218 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002219 unsigned NumElems = Mask.getNumOperands();
2220 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002221 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002222 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002223 NumElems >>= 1;
2224 }
2225 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2226
2227 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002228 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002229 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002230 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002231 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2232}
2233
Evan Chenge8b51802006-04-21 01:05:10 +00002234/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2235/// constant +0.0.
2236static inline bool isZeroNode(SDOperand Elt) {
2237 return ((isa<ConstantSDNode>(Elt) &&
2238 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2239 (isa<ConstantFPSDNode>(Elt) &&
2240 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2241}
2242
Evan Cheng14215c32006-04-21 23:03:30 +00002243/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2244/// vector and zero or undef vector.
2245static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002246 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002247 bool isZero, SelectionDAG &DAG) {
2248 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002249 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2250 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2251 SDOperand Zero = DAG.getConstant(0, EVT);
2252 std::vector<SDOperand> MaskVec(NumElems, Zero);
2253 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2254 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng14215c32006-04-21 23:03:30 +00002255 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002256}
2257
Evan Chengb0461082006-04-24 18:01:45 +00002258/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2259///
2260static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2261 unsigned NumNonZero, unsigned NumZero,
2262 SelectionDAG &DAG) {
2263 if (NumNonZero > 8)
2264 return SDOperand();
2265
2266 SDOperand V(0, 0);
2267 bool First = true;
2268 for (unsigned i = 0; i < 16; ++i) {
2269 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2270 if (ThisIsNonZero && First) {
2271 if (NumZero)
2272 V = getZeroVector(MVT::v8i16, DAG);
2273 else
2274 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2275 First = false;
2276 }
2277
2278 if ((i & 1) != 0) {
2279 SDOperand ThisElt(0, 0), LastElt(0, 0);
2280 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2281 if (LastIsNonZero) {
2282 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2283 }
2284 if (ThisIsNonZero) {
2285 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2286 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2287 ThisElt, DAG.getConstant(8, MVT::i8));
2288 if (LastIsNonZero)
2289 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2290 } else
2291 ThisElt = LastElt;
2292
2293 if (ThisElt.Val)
2294 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2295 DAG.getConstant(i/2, MVT::i32));
2296 }
2297 }
2298
2299 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2300}
2301
2302/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2303///
2304static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2305 unsigned NumNonZero, unsigned NumZero,
2306 SelectionDAG &DAG) {
2307 if (NumNonZero > 4)
2308 return SDOperand();
2309
2310 SDOperand V(0, 0);
2311 bool First = true;
2312 for (unsigned i = 0; i < 8; ++i) {
2313 bool isNonZero = (NonZeros & (1 << i)) != 0;
2314 if (isNonZero) {
2315 if (First) {
2316 if (NumZero)
2317 V = getZeroVector(MVT::v8i16, DAG);
2318 else
2319 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2320 First = false;
2321 }
2322 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2323 DAG.getConstant(i, MVT::i32));
2324 }
2325 }
2326
2327 return V;
2328}
2329
Evan Chenga9467aa2006-04-25 20:13:52 +00002330SDOperand
2331X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2332 // All zero's are handled with pxor.
2333 if (ISD::isBuildVectorAllZeros(Op.Val))
2334 return Op;
2335
2336 // All one's are handled with pcmpeqd.
2337 if (ISD::isBuildVectorAllOnes(Op.Val))
2338 return Op;
2339
2340 MVT::ValueType VT = Op.getValueType();
2341 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2342 unsigned EVTBits = MVT::getSizeInBits(EVT);
2343
2344 unsigned NumElems = Op.getNumOperands();
2345 unsigned NumZero = 0;
2346 unsigned NumNonZero = 0;
2347 unsigned NonZeros = 0;
2348 std::set<SDOperand> Values;
2349 for (unsigned i = 0; i < NumElems; ++i) {
2350 SDOperand Elt = Op.getOperand(i);
2351 if (Elt.getOpcode() != ISD::UNDEF) {
2352 Values.insert(Elt);
2353 if (isZeroNode(Elt))
2354 NumZero++;
2355 else {
2356 NonZeros |= (1 << i);
2357 NumNonZero++;
2358 }
2359 }
2360 }
2361
2362 if (NumNonZero == 0)
2363 // Must be a mix of zero and undef. Return a zero vector.
2364 return getZeroVector(VT, DAG);
2365
2366 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2367 if (Values.size() == 1)
2368 return SDOperand();
2369
2370 // Special case for single non-zero element.
2371 if (NumNonZero == 1) {
2372 unsigned Idx = CountTrailingZeros_32(NonZeros);
2373 SDOperand Item = Op.getOperand(Idx);
2374 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2375 if (Idx == 0)
2376 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2377 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2378 NumZero > 0, DAG);
2379
2380 if (EVTBits == 32) {
2381 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2382 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2383 DAG);
2384 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2385 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2386 std::vector<SDOperand> MaskVec;
2387 for (unsigned i = 0; i < NumElems; i++)
2388 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2389 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2390 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2391 DAG.getNode(ISD::UNDEF, VT), Mask);
2392 }
2393 }
2394
2395 // Let legalizer expand 2-widde build_vector's.
2396 if (EVTBits == 64)
2397 return SDOperand();
2398
2399 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2400 if (EVTBits == 8) {
2401 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2402 if (V.Val) return V;
2403 }
2404
2405 if (EVTBits == 16) {
2406 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2407 if (V.Val) return V;
2408 }
2409
2410 // If element VT is == 32 bits, turn it into a number of shuffles.
2411 std::vector<SDOperand> V(NumElems);
2412 if (NumElems == 4 && NumZero > 0) {
2413 for (unsigned i = 0; i < 4; ++i) {
2414 bool isZero = !(NonZeros & (1 << i));
2415 if (isZero)
2416 V[i] = getZeroVector(VT, DAG);
2417 else
2418 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2419 }
2420
2421 for (unsigned i = 0; i < 2; ++i) {
2422 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2423 default: break;
2424 case 0:
2425 V[i] = V[i*2]; // Must be a zero vector.
2426 break;
2427 case 1:
2428 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2429 getMOVLMask(NumElems, DAG));
2430 break;
2431 case 2:
2432 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2433 getMOVLMask(NumElems, DAG));
2434 break;
2435 case 3:
2436 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2437 getUnpacklMask(NumElems, DAG));
2438 break;
2439 }
2440 }
2441
Evan Cheng9fee4422006-05-16 07:21:53 +00002442 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Chenga9467aa2006-04-25 20:13:52 +00002443 // clears the upper bits.
2444 // FIXME: we can do the same for v4f32 case when we know both parts of
2445 // the lower half come from scalar_to_vector (loadf32). We should do
2446 // that in post legalizer dag combiner with target specific hooks.
2447 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2448 return V[0];
2449 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2450 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2451 std::vector<SDOperand> MaskVec;
2452 bool Reverse = (NonZeros & 0x3) == 2;
2453 for (unsigned i = 0; i < 2; ++i)
2454 if (Reverse)
2455 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2456 else
2457 MaskVec.push_back(DAG.getConstant(i, EVT));
2458 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2459 for (unsigned i = 0; i < 2; ++i)
2460 if (Reverse)
2461 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2462 else
2463 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2464 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2465 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2466 }
2467
2468 if (Values.size() > 2) {
2469 // Expand into a number of unpckl*.
2470 // e.g. for v4f32
2471 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2472 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2473 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2474 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2475 for (unsigned i = 0; i < NumElems; ++i)
2476 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2477 NumElems >>= 1;
2478 while (NumElems != 0) {
2479 for (unsigned i = 0; i < NumElems; ++i)
2480 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2481 UnpckMask);
2482 NumElems >>= 1;
2483 }
2484 return V[0];
2485 }
2486
2487 return SDOperand();
2488}
2489
2490SDOperand
2491X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2492 SDOperand V1 = Op.getOperand(0);
2493 SDOperand V2 = Op.getOperand(1);
2494 SDOperand PermMask = Op.getOperand(2);
2495 MVT::ValueType VT = Op.getValueType();
2496 unsigned NumElems = PermMask.getNumOperands();
2497 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2498 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2499
2500 if (isSplatMask(PermMask.Val)) {
2501 if (NumElems <= 4) return Op;
2502 // Promote it to a v4i32 splat.
2503 return PromoteSplat(Op, DAG);
2504 }
2505
2506 if (X86::isMOVLMask(PermMask.Val))
2507 return (V1IsUndef) ? V2 : Op;
2508
2509 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2510 X86::isMOVSLDUPMask(PermMask.Val) ||
2511 X86::isMOVHLPSMask(PermMask.Val) ||
2512 X86::isMOVHPMask(PermMask.Val) ||
2513 X86::isMOVLPMask(PermMask.Val))
2514 return Op;
2515
2516 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2517 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2518 return CommuteVectorShuffle(Op, DAG);
2519
2520 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2521 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2522 if (V1IsSplat && !V2IsSplat) {
2523 Op = CommuteVectorShuffle(Op, DAG);
2524 V1 = Op.getOperand(0);
2525 V2 = Op.getOperand(1);
2526 PermMask = Op.getOperand(2);
2527 V2IsSplat = true;
2528 }
2529
2530 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2531 if (V2IsUndef) return V1;
2532 Op = CommuteVectorShuffle(Op, DAG);
2533 V1 = Op.getOperand(0);
2534 V2 = Op.getOperand(1);
2535 PermMask = Op.getOperand(2);
2536 if (V2IsSplat) {
2537 // V2 is a splat, so the mask may be malformed. That is, it may point
2538 // to any V2 element. The instruction selectior won't like this. Get
2539 // a corrected mask and commute to form a proper MOVS{S|D}.
2540 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2541 if (NewMask.Val != PermMask.Val)
2542 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2543 }
2544 return Op;
2545 }
2546
2547 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2548 X86::isUNPCKLMask(PermMask.Val) ||
2549 X86::isUNPCKHMask(PermMask.Val))
2550 return Op;
2551
2552 if (V2IsSplat) {
2553 // Normalize mask so all entries that point to V2 points to its first
2554 // element then try to match unpck{h|l} again. If match, return a
2555 // new vector_shuffle with the corrected mask.
2556 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2557 if (NewMask.Val != PermMask.Val) {
2558 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2559 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2560 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2561 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2562 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2563 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2564 }
2565 }
2566 }
2567
2568 // Normalize the node to match x86 shuffle ops if needed
2569 if (V2.getOpcode() != ISD::UNDEF)
2570 if (isCommutedSHUFP(PermMask.Val)) {
2571 Op = CommuteVectorShuffle(Op, DAG);
2572 V1 = Op.getOperand(0);
2573 V2 = Op.getOperand(1);
2574 PermMask = Op.getOperand(2);
2575 }
2576
2577 // If VT is integer, try PSHUF* first, then SHUFP*.
2578 if (MVT::isInteger(VT)) {
2579 if (X86::isPSHUFDMask(PermMask.Val) ||
2580 X86::isPSHUFHWMask(PermMask.Val) ||
2581 X86::isPSHUFLWMask(PermMask.Val)) {
2582 if (V2.getOpcode() != ISD::UNDEF)
2583 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2584 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2585 return Op;
2586 }
2587
2588 if (X86::isSHUFPMask(PermMask.Val))
2589 return Op;
2590
2591 // Handle v8i16 shuffle high / low shuffle node pair.
2592 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2593 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2594 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2595 std::vector<SDOperand> MaskVec;
2596 for (unsigned i = 0; i != 4; ++i)
2597 MaskVec.push_back(PermMask.getOperand(i));
2598 for (unsigned i = 4; i != 8; ++i)
2599 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2600 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2601 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2602 MaskVec.clear();
2603 for (unsigned i = 0; i != 4; ++i)
2604 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2605 for (unsigned i = 4; i != 8; ++i)
2606 MaskVec.push_back(PermMask.getOperand(i));
2607 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2608 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2609 }
2610 } else {
2611 // Floating point cases in the other order.
2612 if (X86::isSHUFPMask(PermMask.Val))
2613 return Op;
2614 if (X86::isPSHUFDMask(PermMask.Val) ||
2615 X86::isPSHUFHWMask(PermMask.Val) ||
2616 X86::isPSHUFLWMask(PermMask.Val)) {
2617 if (V2.getOpcode() != ISD::UNDEF)
2618 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2619 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2620 return Op;
2621 }
2622 }
2623
2624 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002625 MVT::ValueType MaskVT = PermMask.getValueType();
2626 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng3cd43622006-04-28 07:03:38 +00002627 std::vector<std::pair<int, int> > Locs;
2628 Locs.reserve(NumElems);
2629 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2630 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2631 unsigned NumHi = 0;
2632 unsigned NumLo = 0;
2633 // If no more than two elements come from either vector. This can be
2634 // implemented with two shuffles. First shuffle gather the elements.
2635 // The second shuffle, which takes the first shuffle as both of its
2636 // vector operands, put the elements into the right order.
2637 for (unsigned i = 0; i != NumElems; ++i) {
2638 SDOperand Elt = PermMask.getOperand(i);
2639 if (Elt.getOpcode() == ISD::UNDEF) {
2640 Locs[i] = std::make_pair(-1, -1);
2641 } else {
2642 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2643 if (Val < NumElems) {
2644 Locs[i] = std::make_pair(0, NumLo);
2645 Mask1[NumLo] = Elt;
2646 NumLo++;
2647 } else {
2648 Locs[i] = std::make_pair(1, NumHi);
2649 if (2+NumHi < NumElems)
2650 Mask1[2+NumHi] = Elt;
2651 NumHi++;
2652 }
2653 }
2654 }
2655 if (NumLo <= 2 && NumHi <= 2) {
2656 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2657 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2658 for (unsigned i = 0; i != NumElems; ++i) {
2659 if (Locs[i].first == -1)
2660 continue;
2661 else {
2662 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2663 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2664 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2665 }
2666 }
2667
2668 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2669 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2670 }
2671
2672 // Break it into (shuffle shuffle_hi, shuffle_lo).
2673 Locs.clear();
Evan Chenga9467aa2006-04-25 20:13:52 +00002674 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2675 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2676 std::vector<SDOperand> *MaskPtr = &LoMask;
2677 unsigned MaskIdx = 0;
2678 unsigned LoIdx = 0;
2679 unsigned HiIdx = NumElems/2;
2680 for (unsigned i = 0; i != NumElems; ++i) {
2681 if (i == NumElems/2) {
2682 MaskPtr = &HiMask;
2683 MaskIdx = 1;
2684 LoIdx = 0;
2685 HiIdx = NumElems/2;
2686 }
2687 SDOperand Elt = PermMask.getOperand(i);
2688 if (Elt.getOpcode() == ISD::UNDEF) {
2689 Locs[i] = std::make_pair(-1, -1);
2690 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2691 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2692 (*MaskPtr)[LoIdx] = Elt;
2693 LoIdx++;
2694 } else {
2695 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2696 (*MaskPtr)[HiIdx] = Elt;
2697 HiIdx++;
2698 }
2699 }
2700
Chris Lattner3d826992006-05-16 06:45:34 +00002701 SDOperand LoShuffle =
2702 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2703 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2704 SDOperand HiShuffle =
2705 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2706 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Chenga9467aa2006-04-25 20:13:52 +00002707 std::vector<SDOperand> MaskOps;
2708 for (unsigned i = 0; i != NumElems; ++i) {
2709 if (Locs[i].first == -1) {
2710 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2711 } else {
2712 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2713 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2714 }
2715 }
2716 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2717 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2718 }
2719
2720 return SDOperand();
2721}
2722
2723SDOperand
2724X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2725 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2726 return SDOperand();
2727
2728 MVT::ValueType VT = Op.getValueType();
2729 // TODO: handle v16i8.
2730 if (MVT::getSizeInBits(VT) == 16) {
2731 // Transform it so it match pextrw which produces a 32-bit result.
2732 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2733 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2734 Op.getOperand(0), Op.getOperand(1));
2735 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2736 DAG.getValueType(VT));
2737 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2738 } else if (MVT::getSizeInBits(VT) == 32) {
2739 SDOperand Vec = Op.getOperand(0);
2740 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2741 if (Idx == 0)
2742 return Op;
2743
2744 // SHUFPS the element to the lowest double word, then movss.
2745 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2746 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2747 MVT::getVectorBaseType(MaskVT));
2748 std::vector<SDOperand> IdxVec;
2749 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2750 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2751 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2752 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2753 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2754 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2755 Vec, Vec, Mask);
2756 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2757 DAG.getConstant(0, MVT::i32));
2758 } else if (MVT::getSizeInBits(VT) == 64) {
2759 SDOperand Vec = Op.getOperand(0);
2760 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2761 if (Idx == 0)
2762 return Op;
2763
2764 // UNPCKHPD the element to the lowest double word, then movsd.
2765 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2766 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2767 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2768 std::vector<SDOperand> IdxVec;
2769 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2770 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2771 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2772 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2773 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2774 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2775 DAG.getConstant(0, MVT::i32));
2776 }
2777
2778 return SDOperand();
2779}
2780
2781SDOperand
2782X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002783 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002784 // as its second argument.
2785 MVT::ValueType VT = Op.getValueType();
2786 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2787 SDOperand N0 = Op.getOperand(0);
2788 SDOperand N1 = Op.getOperand(1);
2789 SDOperand N2 = Op.getOperand(2);
2790 if (MVT::getSizeInBits(BaseVT) == 16) {
2791 if (N1.getValueType() != MVT::i32)
2792 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2793 if (N2.getValueType() != MVT::i32)
2794 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2795 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2796 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2797 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2798 if (Idx == 0) {
2799 // Use a movss.
2800 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2801 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2802 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2803 std::vector<SDOperand> MaskVec;
2804 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2805 for (unsigned i = 1; i <= 3; ++i)
2806 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2807 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2808 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2809 } else {
2810 // Use two pinsrw instructions to insert a 32 bit value.
2811 Idx <<= 1;
2812 if (MVT::isFloatingPoint(N1.getValueType())) {
2813 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002814 // Just load directly from f32mem to GR32.
Evan Chenga9467aa2006-04-25 20:13:52 +00002815 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2816 N1.getOperand(2));
2817 } else {
2818 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2819 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2820 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2821 DAG.getConstant(0, MVT::i32));
2822 }
2823 }
2824 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2825 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2826 DAG.getConstant(Idx, MVT::i32));
2827 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2828 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2829 DAG.getConstant(Idx+1, MVT::i32));
2830 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2831 }
2832 }
2833
2834 return SDOperand();
2835}
2836
2837SDOperand
2838X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2839 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2840 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2841}
2842
2843// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2844// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2845// one of the above mentioned nodes. It has to be wrapped because otherwise
2846// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2847// be used to form addressing mode. These wrapped nodes will be selected
2848// into MOV32ri.
2849SDOperand
2850X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2851 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2852 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2853 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2854 CP->getAlignment()));
2855 if (Subtarget->isTargetDarwin()) {
2856 // With PIC, the address is actually $g + Offset.
2857 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2858 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2859 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2860 }
2861
2862 return Result;
2863}
2864
2865SDOperand
2866X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2867 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2868 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002869 DAG.getTargetGlobalAddress(GV,
2870 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002871 if (Subtarget->isTargetDarwin()) {
2872 // With PIC, the address is actually $g + Offset.
2873 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2874 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002875 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2876 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002877
2878 // For Darwin, external and weak symbols are indirect, so we want to load
2879 // the value at address GV, not the value of GV itself. This means that
2880 // the GlobalAddress must be in the base or index register of the address,
2881 // not the GV offset field.
2882 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2883 DarwinGVRequiresExtraLoad(GV))
2884 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2885 Result, DAG.getSrcValue(NULL));
2886 }
2887
2888 return Result;
2889}
2890
2891SDOperand
2892X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2893 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2894 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002895 DAG.getTargetExternalSymbol(Sym,
2896 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002897 if (Subtarget->isTargetDarwin()) {
2898 // With PIC, the address is actually $g + Offset.
2899 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2900 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002901 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2902 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002903 }
2904
2905 return Result;
2906}
2907
2908SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00002909 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2910 "Not an i64 shift!");
2911 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2912 SDOperand ShOpLo = Op.getOperand(0);
2913 SDOperand ShOpHi = Op.getOperand(1);
2914 SDOperand ShAmt = Op.getOperand(2);
2915 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00002916 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00002917 : DAG.getConstant(0, MVT::i32);
2918
2919 SDOperand Tmp2, Tmp3;
2920 if (Op.getOpcode() == ISD::SHL_PARTS) {
2921 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2922 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2923 } else {
2924 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00002925 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00002926 }
2927
2928 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2929 ShAmt, DAG.getConstant(32, MVT::i8));
2930
2931 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00002932 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00002933
2934 std::vector<MVT::ValueType> Tys;
2935 Tys.push_back(MVT::i32);
2936 Tys.push_back(MVT::Flag);
2937 std::vector<SDOperand> Ops;
2938 if (Op.getOpcode() == ISD::SHL_PARTS) {
2939 Ops.push_back(Tmp2);
2940 Ops.push_back(Tmp3);
2941 Ops.push_back(CC);
2942 Ops.push_back(InFlag);
2943 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2944 InFlag = Hi.getValue(1);
2945
2946 Ops.clear();
2947 Ops.push_back(Tmp3);
2948 Ops.push_back(Tmp1);
2949 Ops.push_back(CC);
2950 Ops.push_back(InFlag);
2951 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2952 } else {
2953 Ops.push_back(Tmp2);
2954 Ops.push_back(Tmp3);
2955 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00002956 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00002957 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2958 InFlag = Lo.getValue(1);
2959
2960 Ops.clear();
2961 Ops.push_back(Tmp3);
2962 Ops.push_back(Tmp1);
2963 Ops.push_back(CC);
2964 Ops.push_back(InFlag);
2965 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2966 }
2967
2968 Tys.clear();
2969 Tys.push_back(MVT::i32);
2970 Tys.push_back(MVT::i32);
2971 Ops.clear();
2972 Ops.push_back(Lo);
2973 Ops.push_back(Hi);
2974 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Chenga9467aa2006-04-25 20:13:52 +00002975}
Evan Cheng6305e502006-01-12 22:54:21 +00002976
Evan Chenga9467aa2006-04-25 20:13:52 +00002977SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2978 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2979 Op.getOperand(0).getValueType() >= MVT::i16 &&
2980 "Unknown SINT_TO_FP to lower!");
2981
2982 SDOperand Result;
2983 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2984 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2985 MachineFunction &MF = DAG.getMachineFunction();
2986 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2987 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2988 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2989 DAG.getEntryNode(), Op.getOperand(0),
2990 StackSlot, DAG.getSrcValue(NULL));
2991
2992 // Build the FILD
2993 std::vector<MVT::ValueType> Tys;
2994 Tys.push_back(MVT::f64);
2995 Tys.push_back(MVT::Other);
2996 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2997 std::vector<SDOperand> Ops;
2998 Ops.push_back(Chain);
2999 Ops.push_back(StackSlot);
3000 Ops.push_back(DAG.getValueType(SrcVT));
3001 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3002 Tys, Ops);
3003
3004 if (X86ScalarSSE) {
3005 Chain = Result.getValue(1);
3006 SDOperand InFlag = Result.getValue(2);
3007
3008 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3009 // shouldn't be necessary except that RFP cannot be live across
3010 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003011 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003012 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003013 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00003014 std::vector<MVT::ValueType> Tys;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003015 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003016 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003017 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003018 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003019 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003020 Ops.push_back(DAG.getValueType(Op.getValueType()));
3021 Ops.push_back(InFlag);
3022 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3023 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3024 DAG.getSrcValue(NULL));
Chris Lattner76ac0682005-11-15 00:40:23 +00003025 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003026
Evan Chenga9467aa2006-04-25 20:13:52 +00003027 return Result;
3028}
3029
3030SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3031 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3032 "Unknown FP_TO_SINT to lower!");
3033 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3034 // stack slot.
3035 MachineFunction &MF = DAG.getMachineFunction();
3036 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3037 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3038 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3039
3040 unsigned Opc;
3041 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003042 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3043 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3044 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3045 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003046 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003047
Evan Chenga9467aa2006-04-25 20:13:52 +00003048 SDOperand Chain = DAG.getEntryNode();
3049 SDOperand Value = Op.getOperand(0);
3050 if (X86ScalarSSE) {
3051 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3052 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3053 DAG.getSrcValue(0));
3054 std::vector<MVT::ValueType> Tys;
3055 Tys.push_back(MVT::f64);
3056 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003057 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003058 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00003059 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003060 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3061 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3062 Chain = Value.getValue(1);
3063 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3064 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3065 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003066
Evan Chenga9467aa2006-04-25 20:13:52 +00003067 // Build the FP_TO_INT*_IN_MEM
3068 std::vector<SDOperand> Ops;
3069 Ops.push_back(Chain);
3070 Ops.push_back(Value);
3071 Ops.push_back(StackSlot);
3072 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Cheng172fce72006-01-06 00:43:03 +00003073
Evan Chenga9467aa2006-04-25 20:13:52 +00003074 // Load the result.
3075 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3076 DAG.getSrcValue(NULL));
3077}
3078
3079SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3080 MVT::ValueType VT = Op.getValueType();
3081 const Type *OpNTy = MVT::getTypeForValueType(VT);
3082 std::vector<Constant*> CV;
3083 if (VT == MVT::f64) {
3084 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3085 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3086 } else {
3087 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3088 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3089 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3090 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3091 }
3092 Constant *CS = ConstantStruct::get(CV);
3093 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3094 SDOperand Mask
3095 = DAG.getNode(X86ISD::LOAD_PACK,
3096 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3097 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3098}
3099
3100SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3101 MVT::ValueType VT = Op.getValueType();
3102 const Type *OpNTy = MVT::getTypeForValueType(VT);
3103 std::vector<Constant*> CV;
3104 if (VT == MVT::f64) {
3105 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3106 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3107 } else {
3108 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3109 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3111 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3112 }
3113 Constant *CS = ConstantStruct::get(CV);
3114 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3115 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3116 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3117 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3118}
3119
3120SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3121 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3122 SDOperand Cond;
3123 SDOperand CC = Op.getOperand(2);
3124 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3125 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3126 bool Flip;
3127 unsigned X86CC;
3128 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3129 if (Flip)
3130 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3131 Op.getOperand(1), Op.getOperand(0));
3132 else
Evan Cheng45df7f82006-01-30 23:41:35 +00003133 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3134 Op.getOperand(0), Op.getOperand(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003135 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3136 DAG.getConstant(X86CC, MVT::i8), Cond);
3137 } else {
3138 assert(isFP && "Illegal integer SetCC!");
3139
3140 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3141 Op.getOperand(0), Op.getOperand(1));
3142 std::vector<MVT::ValueType> Tys;
3143 std::vector<SDOperand> Ops;
3144 switch (SetCCOpcode) {
Evan Cheng172fce72006-01-06 00:43:03 +00003145 default: assert(false && "Illegal floating point SetCC!");
3146 case ISD::SETOEQ: { // !PF & ZF
3147 Tys.push_back(MVT::i8);
3148 Tys.push_back(MVT::Flag);
3149 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3150 Ops.push_back(Cond);
3151 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3152 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3153 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3154 Tmp1.getValue(1));
3155 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3156 }
Evan Cheng172fce72006-01-06 00:43:03 +00003157 case ISD::SETUNE: { // PF | !ZF
3158 Tys.push_back(MVT::i8);
3159 Tys.push_back(MVT::Flag);
3160 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3161 Ops.push_back(Cond);
3162 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3163 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3164 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3165 Tmp1.getValue(1));
3166 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3167 }
Evan Cheng172fce72006-01-06 00:43:03 +00003168 }
Evan Chengc1583db2005-12-21 20:21:51 +00003169 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003170}
Evan Cheng45df7f82006-01-30 23:41:35 +00003171
Evan Chenga9467aa2006-04-25 20:13:52 +00003172SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3173 MVT::ValueType VT = Op.getValueType();
3174 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3175 bool addTest = false;
3176 SDOperand Op0 = Op.getOperand(0);
3177 SDOperand Cond, CC;
3178 if (Op0.getOpcode() == ISD::SETCC)
3179 Op0 = LowerOperation(Op0, DAG);
Evan Cheng944d1e92006-01-26 02:13:10 +00003180
Evan Chenga9467aa2006-04-25 20:13:52 +00003181 if (Op0.getOpcode() == X86ISD::SETCC) {
3182 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3183 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3184 // have another use it will be eliminated.
3185 // If the X86ISD::SETCC has more than one use, then it's probably better
3186 // to use a test instead of duplicating the X86ISD::CMP (for register
3187 // pressure reason).
3188 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3189 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3190 CmpOpc == X86ISD::UCOMI) {
3191 if (!Op0.hasOneUse()) {
3192 std::vector<MVT::ValueType> Tys;
3193 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3194 Tys.push_back(Op0.Val->getValueType(i));
3195 std::vector<SDOperand> Ops;
3196 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3197 Ops.push_back(Op0.getOperand(i));
3198 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3199 }
3200
3201 CC = Op0.getOperand(0);
3202 Cond = Op0.getOperand(1);
3203 // Make a copy as flag result cannot be used by more than one.
3204 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3205 Cond.getOperand(0), Cond.getOperand(1));
3206 addTest =
3207 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00003208 } else
3209 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003210 } else
3211 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00003212
Evan Chenga9467aa2006-04-25 20:13:52 +00003213 if (addTest) {
3214 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3215 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00003216 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003217
Evan Chenga9467aa2006-04-25 20:13:52 +00003218 std::vector<MVT::ValueType> Tys;
3219 Tys.push_back(Op.getValueType());
3220 Tys.push_back(MVT::Flag);
3221 std::vector<SDOperand> Ops;
3222 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3223 // condition is true.
3224 Ops.push_back(Op.getOperand(2));
3225 Ops.push_back(Op.getOperand(1));
3226 Ops.push_back(CC);
3227 Ops.push_back(Cond);
3228 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3229}
Evan Cheng944d1e92006-01-26 02:13:10 +00003230
Evan Chenga9467aa2006-04-25 20:13:52 +00003231SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3232 bool addTest = false;
3233 SDOperand Cond = Op.getOperand(1);
3234 SDOperand Dest = Op.getOperand(2);
3235 SDOperand CC;
3236 if (Cond.getOpcode() == ISD::SETCC)
3237 Cond = LowerOperation(Cond, DAG);
3238
3239 if (Cond.getOpcode() == X86ISD::SETCC) {
3240 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3241 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3242 // have another use it will be eliminated.
3243 // If the X86ISD::SETCC has more than one use, then it's probably better
3244 // to use a test instead of duplicating the X86ISD::CMP (for register
3245 // pressure reason).
3246 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3247 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3248 CmpOpc == X86ISD::UCOMI) {
3249 if (!Cond.hasOneUse()) {
3250 std::vector<MVT::ValueType> Tys;
3251 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3252 Tys.push_back(Cond.Val->getValueType(i));
3253 std::vector<SDOperand> Ops;
3254 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3255 Ops.push_back(Cond.getOperand(i));
3256 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3257 }
3258
3259 CC = Cond.getOperand(0);
3260 Cond = Cond.getOperand(1);
3261 // Make a copy as flag result cannot be used by more than one.
3262 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3263 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00003264 } else
3265 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003266 } else
3267 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00003268
Evan Chenga9467aa2006-04-25 20:13:52 +00003269 if (addTest) {
3270 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3271 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng6fc31042005-12-19 23:12:38 +00003272 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003273 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3274 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3275}
Evan Chengae986f12006-01-11 22:15:48 +00003276
Evan Chenga9467aa2006-04-25 20:13:52 +00003277SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3278 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3279 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3280 DAG.getTargetJumpTable(JT->getIndex(),
3281 getPointerTy()));
3282 if (Subtarget->isTargetDarwin()) {
3283 // With PIC, the address is actually $g + Offset.
3284 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3285 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003286 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3287 Result);
Evan Chengae986f12006-01-11 22:15:48 +00003288 }
Evan Cheng99470012006-02-25 09:55:19 +00003289
Evan Chenga9467aa2006-04-25 20:13:52 +00003290 return Result;
3291}
Evan Cheng5588de92006-02-18 00:15:05 +00003292
Evan Cheng2a330942006-05-25 00:59:30 +00003293SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3294 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3295 if (CallingConv == CallingConv::Fast && EnableFastCC)
3296 return LowerFastCCCallTo(Op, DAG);
3297 else
3298 return LowerCCCCallTo(Op, DAG);
3299}
3300
Evan Chenga9467aa2006-04-25 20:13:52 +00003301SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3302 SDOperand Copy;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003303
Evan Chenga9467aa2006-04-25 20:13:52 +00003304 switch(Op.getNumOperands()) {
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003305 default:
3306 assert(0 && "Do not know how to return this many arguments!");
3307 abort();
Chris Lattnerc070c622006-04-17 20:32:50 +00003308 case 1: // ret void.
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003309 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chenga9467aa2006-04-25 20:13:52 +00003310 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003311 case 2: {
3312 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerc070c622006-04-17 20:32:50 +00003313
3314 if (MVT::isVector(ArgVT)) {
3315 // Integer or FP vector result -> XMM0.
3316 if (DAG.getMachineFunction().liveout_empty())
3317 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3318 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3319 SDOperand());
3320 } else if (MVT::isInteger(ArgVT)) {
3321 // Integer result -> EAX
3322 if (DAG.getMachineFunction().liveout_empty())
3323 DAG.getMachineFunction().addLiveOut(X86::EAX);
3324
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003325 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3326 SDOperand());
Chris Lattnerc070c622006-04-17 20:32:50 +00003327 } else if (!X86ScalarSSE) {
3328 // FP return with fp-stack value.
3329 if (DAG.getMachineFunction().liveout_empty())
3330 DAG.getMachineFunction().addLiveOut(X86::ST0);
3331
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003332 std::vector<MVT::ValueType> Tys;
3333 Tys.push_back(MVT::Other);
3334 Tys.push_back(MVT::Flag);
3335 std::vector<SDOperand> Ops;
3336 Ops.push_back(Op.getOperand(0));
3337 Ops.push_back(Op.getOperand(1));
3338 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3339 } else {
Chris Lattnerc070c622006-04-17 20:32:50 +00003340 // FP return with ScalarSSE (return on fp-stack).
3341 if (DAG.getMachineFunction().liveout_empty())
3342 DAG.getMachineFunction().addLiveOut(X86::ST0);
3343
Evan Chenge1ce4d72006-02-01 00:20:21 +00003344 SDOperand MemLoc;
3345 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00003346 SDOperand Value = Op.getOperand(1);
3347
Evan Chenga24617f2006-02-01 01:19:32 +00003348 if (Value.getOpcode() == ISD::LOAD &&
3349 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00003350 Chain = Value.getOperand(0);
3351 MemLoc = Value.getOperand(1);
3352 } else {
3353 // Spill the value to memory and reload it into top of stack.
3354 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3355 MachineFunction &MF = DAG.getMachineFunction();
3356 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3357 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3358 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3359 Value, MemLoc, DAG.getSrcValue(0));
3360 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003361 std::vector<MVT::ValueType> Tys;
3362 Tys.push_back(MVT::f64);
3363 Tys.push_back(MVT::Other);
3364 std::vector<SDOperand> Ops;
3365 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00003366 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003367 Ops.push_back(DAG.getValueType(ArgVT));
3368 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3369 Tys.clear();
3370 Tys.push_back(MVT::Other);
3371 Tys.push_back(MVT::Flag);
3372 Ops.clear();
3373 Ops.push_back(Copy.getValue(1));
3374 Ops.push_back(Copy);
3375 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3376 }
3377 break;
3378 }
3379 case 3:
Chris Lattnerc070c622006-04-17 20:32:50 +00003380 if (DAG.getMachineFunction().liveout_empty()) {
3381 DAG.getMachineFunction().addLiveOut(X86::EAX);
3382 DAG.getMachineFunction().addLiveOut(X86::EDX);
3383 }
3384
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003385 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3386 SDOperand());
3387 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3388 break;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003389 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003390 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3391 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3392 Copy.getValue(1));
3393}
3394
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003395SDOperand
3396X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng17e734f2006-05-23 21:06:34 +00003397 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3398 if (CC == CallingConv::Fast && EnableFastCC)
3399 return LowerFastCCArguments(Op, DAG);
3400 else
3401 return LowerCCCArguments(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003402}
3403
Evan Chenga9467aa2006-04-25 20:13:52 +00003404SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3405 SDOperand InFlag(0, 0);
3406 SDOperand Chain = Op.getOperand(0);
3407 unsigned Align =
3408 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3409 if (Align == 0) Align = 1;
3410
3411 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3412 // If not DWORD aligned, call memset if size is less than the threshold.
3413 // It knows how to align to the right boundary first.
3414 if ((Align & 3) != 0 ||
3415 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3416 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003417 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003418 std::vector<std::pair<SDOperand, const Type*> > Args;
3419 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3420 // Extend the ubyte argument to be an int value for the call.
3421 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3422 Args.push_back(std::make_pair(Val, IntPtrTy));
3423 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3424 std::pair<SDOperand,SDOperand> CallResult =
3425 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3426 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3427 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003428 }
Evan Chengd097e672006-03-22 02:53:00 +00003429
Evan Chenga9467aa2006-04-25 20:13:52 +00003430 MVT::ValueType AVT;
3431 SDOperand Count;
3432 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3433 unsigned BytesLeft = 0;
3434 bool TwoRepStos = false;
3435 if (ValC) {
3436 unsigned ValReg;
3437 unsigned Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003438
Evan Chenga9467aa2006-04-25 20:13:52 +00003439 // If the value is a constant, then we can potentially use larger sets.
3440 switch (Align & 3) {
3441 case 2: // WORD aligned
3442 AVT = MVT::i16;
3443 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3444 BytesLeft = I->getValue() % 2;
3445 Val = (Val << 8) | Val;
3446 ValReg = X86::AX;
3447 break;
3448 case 0: // DWORD aligned
3449 AVT = MVT::i32;
3450 if (I) {
3451 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3452 BytesLeft = I->getValue() % 4;
Evan Chenga3caaee2006-04-19 22:48:17 +00003453 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003454 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3455 DAG.getConstant(2, MVT::i8));
3456 TwoRepStos = true;
Evan Chenga3caaee2006-04-19 22:48:17 +00003457 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003458 Val = (Val << 8) | Val;
3459 Val = (Val << 16) | Val;
3460 ValReg = X86::EAX;
3461 break;
3462 default: // Byte aligned
3463 AVT = MVT::i8;
3464 Count = Op.getOperand(3);
3465 ValReg = X86::AL;
3466 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003467 }
3468
Evan Chenga9467aa2006-04-25 20:13:52 +00003469 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3470 InFlag);
3471 InFlag = Chain.getValue(1);
3472 } else {
3473 AVT = MVT::i8;
3474 Count = Op.getOperand(3);
3475 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3476 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003477 }
Evan Chengb0461082006-04-24 18:01:45 +00003478
Evan Chenga9467aa2006-04-25 20:13:52 +00003479 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3480 InFlag = Chain.getValue(1);
3481 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3482 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003483
Evan Chenga9467aa2006-04-25 20:13:52 +00003484 std::vector<MVT::ValueType> Tys;
3485 Tys.push_back(MVT::Other);
3486 Tys.push_back(MVT::Flag);
3487 std::vector<SDOperand> Ops;
3488 Ops.push_back(Chain);
3489 Ops.push_back(DAG.getValueType(AVT));
3490 Ops.push_back(InFlag);
3491 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengb0461082006-04-24 18:01:45 +00003492
Evan Chenga9467aa2006-04-25 20:13:52 +00003493 if (TwoRepStos) {
3494 InFlag = Chain.getValue(1);
3495 Count = Op.getOperand(3);
3496 MVT::ValueType CVT = Count.getValueType();
3497 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3498 DAG.getConstant(3, CVT));
3499 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3500 InFlag = Chain.getValue(1);
3501 Tys.clear();
3502 Tys.push_back(MVT::Other);
3503 Tys.push_back(MVT::Flag);
3504 Ops.clear();
3505 Ops.push_back(Chain);
3506 Ops.push_back(DAG.getValueType(MVT::i8));
3507 Ops.push_back(InFlag);
3508 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3509 } else if (BytesLeft) {
3510 // Issue stores for the last 1 - 3 bytes.
3511 SDOperand Value;
3512 unsigned Val = ValC->getValue() & 255;
3513 unsigned Offset = I->getValue() - BytesLeft;
3514 SDOperand DstAddr = Op.getOperand(1);
3515 MVT::ValueType AddrVT = DstAddr.getValueType();
3516 if (BytesLeft >= 2) {
3517 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3518 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3519 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3520 DAG.getConstant(Offset, AddrVT)),
3521 DAG.getSrcValue(NULL));
3522 BytesLeft -= 2;
3523 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003524 }
3525
Evan Chenga9467aa2006-04-25 20:13:52 +00003526 if (BytesLeft == 1) {
3527 Value = DAG.getConstant(Val, MVT::i8);
3528 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3529 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3530 DAG.getConstant(Offset, AddrVT)),
3531 DAG.getSrcValue(NULL));
Evan Cheng14215c32006-04-21 23:03:30 +00003532 }
Evan Cheng082c8782006-03-24 07:29:27 +00003533 }
Evan Chengebf10062006-04-03 20:53:28 +00003534
Evan Chenga9467aa2006-04-25 20:13:52 +00003535 return Chain;
3536}
Evan Chengebf10062006-04-03 20:53:28 +00003537
Evan Chenga9467aa2006-04-25 20:13:52 +00003538SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3539 SDOperand Chain = Op.getOperand(0);
3540 unsigned Align =
3541 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3542 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003543
Evan Chenga9467aa2006-04-25 20:13:52 +00003544 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3545 // If not DWORD aligned, call memcpy if size is less than the threshold.
3546 // It knows how to align to the right boundary first.
3547 if ((Align & 3) != 0 ||
3548 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3549 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003550 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003551 std::vector<std::pair<SDOperand, const Type*> > Args;
3552 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3553 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3554 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3555 std::pair<SDOperand,SDOperand> CallResult =
3556 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3557 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3558 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003559 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003560
3561 MVT::ValueType AVT;
3562 SDOperand Count;
3563 unsigned BytesLeft = 0;
3564 bool TwoRepMovs = false;
3565 switch (Align & 3) {
3566 case 2: // WORD aligned
3567 AVT = MVT::i16;
3568 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3569 BytesLeft = I->getValue() % 2;
3570 break;
3571 case 0: // DWORD aligned
3572 AVT = MVT::i32;
3573 if (I) {
3574 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3575 BytesLeft = I->getValue() % 4;
Evan Cheng54212062006-04-17 22:45:49 +00003576 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003577 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3578 DAG.getConstant(2, MVT::i8));
3579 TwoRepMovs = true;
Evan Cheng6e5e2052006-04-17 22:04:06 +00003580 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003581 break;
3582 default: // Byte aligned
3583 AVT = MVT::i8;
3584 Count = Op.getOperand(3);
3585 break;
3586 }
3587
3588 SDOperand InFlag(0, 0);
3589 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3590 InFlag = Chain.getValue(1);
3591 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3592 InFlag = Chain.getValue(1);
3593 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3594 InFlag = Chain.getValue(1);
3595
3596 std::vector<MVT::ValueType> Tys;
3597 Tys.push_back(MVT::Other);
3598 Tys.push_back(MVT::Flag);
3599 std::vector<SDOperand> Ops;
3600 Ops.push_back(Chain);
3601 Ops.push_back(DAG.getValueType(AVT));
3602 Ops.push_back(InFlag);
3603 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3604
3605 if (TwoRepMovs) {
3606 InFlag = Chain.getValue(1);
3607 Count = Op.getOperand(3);
3608 MVT::ValueType CVT = Count.getValueType();
3609 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3610 DAG.getConstant(3, CVT));
3611 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3612 InFlag = Chain.getValue(1);
3613 Tys.clear();
3614 Tys.push_back(MVT::Other);
3615 Tys.push_back(MVT::Flag);
3616 Ops.clear();
3617 Ops.push_back(Chain);
3618 Ops.push_back(DAG.getValueType(MVT::i8));
3619 Ops.push_back(InFlag);
3620 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3621 } else if (BytesLeft) {
3622 // Issue loads and stores for the last 1 - 3 bytes.
3623 unsigned Offset = I->getValue() - BytesLeft;
3624 SDOperand DstAddr = Op.getOperand(1);
3625 MVT::ValueType DstVT = DstAddr.getValueType();
3626 SDOperand SrcAddr = Op.getOperand(2);
3627 MVT::ValueType SrcVT = SrcAddr.getValueType();
3628 SDOperand Value;
3629 if (BytesLeft >= 2) {
3630 Value = DAG.getLoad(MVT::i16, Chain,
3631 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3632 DAG.getConstant(Offset, SrcVT)),
3633 DAG.getSrcValue(NULL));
3634 Chain = Value.getValue(1);
3635 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3636 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3637 DAG.getConstant(Offset, DstVT)),
3638 DAG.getSrcValue(NULL));
3639 BytesLeft -= 2;
3640 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003641 }
3642
Evan Chenga9467aa2006-04-25 20:13:52 +00003643 if (BytesLeft == 1) {
3644 Value = DAG.getLoad(MVT::i8, Chain,
3645 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3646 DAG.getConstant(Offset, SrcVT)),
3647 DAG.getSrcValue(NULL));
3648 Chain = Value.getValue(1);
3649 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3650 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3651 DAG.getConstant(Offset, DstVT)),
3652 DAG.getSrcValue(NULL));
3653 }
Evan Chengcbffa462006-03-31 19:22:53 +00003654 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003655
3656 return Chain;
3657}
3658
3659SDOperand
3660X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3661 std::vector<MVT::ValueType> Tys;
3662 Tys.push_back(MVT::Other);
3663 Tys.push_back(MVT::Flag);
3664 std::vector<SDOperand> Ops;
3665 Ops.push_back(Op.getOperand(0));
3666 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3667 Ops.clear();
3668 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3669 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3670 MVT::i32, Ops[0].getValue(2)));
3671 Ops.push_back(Ops[1].getValue(1));
3672 Tys[0] = Tys[1] = MVT::i32;
3673 Tys.push_back(MVT::Other);
3674 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3675}
3676
3677SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3678 // vastart just stores the address of the VarArgsFrameIndex slot into the
3679 // memory location argument.
3680 // FIXME: Replace MVT::i32 with PointerTy
3681 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3682 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3683 Op.getOperand(1), Op.getOperand(2));
3684}
3685
3686SDOperand
3687X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3688 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3689 switch (IntNo) {
3690 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003691 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003692 case Intrinsic::x86_sse_comieq_ss:
3693 case Intrinsic::x86_sse_comilt_ss:
3694 case Intrinsic::x86_sse_comile_ss:
3695 case Intrinsic::x86_sse_comigt_ss:
3696 case Intrinsic::x86_sse_comige_ss:
3697 case Intrinsic::x86_sse_comineq_ss:
3698 case Intrinsic::x86_sse_ucomieq_ss:
3699 case Intrinsic::x86_sse_ucomilt_ss:
3700 case Intrinsic::x86_sse_ucomile_ss:
3701 case Intrinsic::x86_sse_ucomigt_ss:
3702 case Intrinsic::x86_sse_ucomige_ss:
3703 case Intrinsic::x86_sse_ucomineq_ss:
3704 case Intrinsic::x86_sse2_comieq_sd:
3705 case Intrinsic::x86_sse2_comilt_sd:
3706 case Intrinsic::x86_sse2_comile_sd:
3707 case Intrinsic::x86_sse2_comigt_sd:
3708 case Intrinsic::x86_sse2_comige_sd:
3709 case Intrinsic::x86_sse2_comineq_sd:
3710 case Intrinsic::x86_sse2_ucomieq_sd:
3711 case Intrinsic::x86_sse2_ucomilt_sd:
3712 case Intrinsic::x86_sse2_ucomile_sd:
3713 case Intrinsic::x86_sse2_ucomigt_sd:
3714 case Intrinsic::x86_sse2_ucomige_sd:
3715 case Intrinsic::x86_sse2_ucomineq_sd: {
3716 unsigned Opc = 0;
3717 ISD::CondCode CC = ISD::SETCC_INVALID;
3718 switch (IntNo) {
3719 default: break;
3720 case Intrinsic::x86_sse_comieq_ss:
3721 case Intrinsic::x86_sse2_comieq_sd:
3722 Opc = X86ISD::COMI;
3723 CC = ISD::SETEQ;
3724 break;
Evan Cheng78038292006-04-05 23:38:46 +00003725 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003726 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003727 Opc = X86ISD::COMI;
3728 CC = ISD::SETLT;
3729 break;
3730 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003731 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003732 Opc = X86ISD::COMI;
3733 CC = ISD::SETLE;
3734 break;
3735 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003736 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003737 Opc = X86ISD::COMI;
3738 CC = ISD::SETGT;
3739 break;
3740 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003741 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003742 Opc = X86ISD::COMI;
3743 CC = ISD::SETGE;
3744 break;
3745 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003746 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003747 Opc = X86ISD::COMI;
3748 CC = ISD::SETNE;
3749 break;
3750 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003751 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003752 Opc = X86ISD::UCOMI;
3753 CC = ISD::SETEQ;
3754 break;
3755 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003756 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003757 Opc = X86ISD::UCOMI;
3758 CC = ISD::SETLT;
3759 break;
3760 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003761 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003762 Opc = X86ISD::UCOMI;
3763 CC = ISD::SETLE;
3764 break;
3765 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003766 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003767 Opc = X86ISD::UCOMI;
3768 CC = ISD::SETGT;
3769 break;
3770 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003771 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003772 Opc = X86ISD::UCOMI;
3773 CC = ISD::SETGE;
3774 break;
3775 case Intrinsic::x86_sse_ucomineq_ss:
3776 case Intrinsic::x86_sse2_ucomineq_sd:
3777 Opc = X86ISD::UCOMI;
3778 CC = ISD::SETNE;
3779 break;
Evan Cheng78038292006-04-05 23:38:46 +00003780 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003781 bool Flip;
3782 unsigned X86CC;
3783 translateX86CC(CC, true, X86CC, Flip);
3784 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3785 Op.getOperand(Flip?1:2));
3786 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3787 DAG.getConstant(X86CC, MVT::i8), Cond);
3788 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003789 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003790 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003791}
Evan Cheng6af02632005-12-20 06:22:03 +00003792
Evan Chenga9467aa2006-04-25 20:13:52 +00003793/// LowerOperation - Provide custom lowering hooks for some operations.
3794///
3795SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3796 switch (Op.getOpcode()) {
3797 default: assert(0 && "Should not custom lower this!");
3798 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3799 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3800 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3801 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3802 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3803 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3804 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3805 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3806 case ISD::SHL_PARTS:
3807 case ISD::SRA_PARTS:
3808 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3809 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3810 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3811 case ISD::FABS: return LowerFABS(Op, DAG);
3812 case ISD::FNEG: return LowerFNEG(Op, DAG);
3813 case ISD::SETCC: return LowerSETCC(Op, DAG);
3814 case ISD::SELECT: return LowerSELECT(Op, DAG);
3815 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3816 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00003817 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003818 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003819 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003820 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3821 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3822 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3823 case ISD::VASTART: return LowerVASTART(Op, DAG);
3824 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3825 }
3826}
3827
Evan Cheng6af02632005-12-20 06:22:03 +00003828const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3829 switch (Opcode) {
3830 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003831 case X86ISD::SHLD: return "X86ISD::SHLD";
3832 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003833 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003834 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003835 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003836 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003837 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3838 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3839 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003840 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003841 case X86ISD::FST: return "X86ISD::FST";
3842 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003843 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003844 case X86ISD::CALL: return "X86ISD::CALL";
3845 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3846 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3847 case X86ISD::CMP: return "X86ISD::CMP";
3848 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003849 case X86ISD::COMI: return "X86ISD::COMI";
3850 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003851 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003852 case X86ISD::CMOV: return "X86ISD::CMOV";
3853 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003854 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003855 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3856 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003857 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003858 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00003859 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00003860 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00003861 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00003862 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00003863 }
3864}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003865
Nate Begeman8a77efe2006-02-16 21:11:51 +00003866void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3867 uint64_t Mask,
3868 uint64_t &KnownZero,
3869 uint64_t &KnownOne,
3870 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003871 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00003872 assert((Opc >= ISD::BUILTIN_OP_END ||
3873 Opc == ISD::INTRINSIC_WO_CHAIN ||
3874 Opc == ISD::INTRINSIC_W_CHAIN ||
3875 Opc == ISD::INTRINSIC_VOID) &&
3876 "Should use MaskedValueIsZero if you don't know whether Op"
3877 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003878
Evan Cheng6d196db2006-04-05 06:11:20 +00003879 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003880 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00003881 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00003882 case X86ISD::SETCC:
3883 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3884 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003885 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003886}
Chris Lattnerc642aa52006-01-31 19:43:35 +00003887
3888std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00003889getRegClassForInlineAsmConstraint(const std::string &Constraint,
3890 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00003891 if (Constraint.size() == 1) {
3892 // FIXME: not handling fp-stack yet!
3893 // FIXME: not handling MMX registers yet ('y' constraint).
3894 switch (Constraint[0]) { // GCC X86 Constraint Letters
3895 default: break; // Unknown constriant letter
3896 case 'r': // GENERAL_REGS
3897 case 'R': // LEGACY_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003898 if (VT == MVT::i32)
3899 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3900 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3901 else if (VT == MVT::i16)
3902 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3903 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3904 else if (VT == MVT::i8)
3905 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3906 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003907 case 'l': // INDEX_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003908 if (VT == MVT::i32)
3909 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3910 X86::ESI, X86::EDI, X86::EBP, 0);
3911 else if (VT == MVT::i16)
3912 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3913 X86::SI, X86::DI, X86::BP, 0);
3914 else if (VT == MVT::i8)
3915 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3916 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003917 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3918 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003919 if (VT == MVT::i32)
3920 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3921 else if (VT == MVT::i16)
3922 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3923 else if (VT == MVT::i8)
3924 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3925 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003926 case 'x': // SSE_REGS if SSE1 allowed
3927 if (Subtarget->hasSSE1())
3928 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3929 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3930 0);
3931 return std::vector<unsigned>();
3932 case 'Y': // SSE_REGS if SSE2 allowed
3933 if (Subtarget->hasSSE2())
3934 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3935 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3936 0);
3937 return std::vector<unsigned>();
3938 }
3939 }
3940
Chris Lattner7ad77df2006-02-22 00:56:39 +00003941 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00003942}
Evan Chengaf598d22006-03-13 23:18:16 +00003943
3944/// isLegalAddressImmediate - Return true if the integer value or
3945/// GlobalValue can be used as the offset of the target addressing mode.
3946bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3947 // X86 allows a sign-extended 32-bit immediate field.
3948 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3949}
3950
3951bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00003952 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00003953 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3954 if (RModel == Reloc::Static)
3955 return true;
3956 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00003957 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00003958 else
3959 return false;
3960 } else
3961 return true;
3962}
Evan Cheng68ad48b2006-03-22 18:59:22 +00003963
3964/// isShuffleMaskLegal - Targets can use this to indicate that they only
3965/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3966/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3967/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00003968bool
3969X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3970 // Only do shuffles on 128-bit vector types for now.
3971 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Chenga3caaee2006-04-19 22:48:17 +00003972 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng5022b342006-04-17 20:43:08 +00003973 isSplatMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00003974 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00003975 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00003976 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00003977 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00003978}
Evan Cheng60f0b892006-04-20 08:58:49 +00003979
3980bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3981 MVT::ValueType EVT,
3982 SelectionDAG &DAG) const {
3983 unsigned NumElts = BVOps.size();
3984 // Only do shuffles on 128-bit vector types for now.
3985 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3986 if (NumElts == 2) return true;
3987 if (NumElts == 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00003988 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng60f0b892006-04-20 08:58:49 +00003989 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3990 }
3991 return false;
3992}