blob: fc602778738facf0f3ed74491849a92a53631ad5 [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 +0000361std::pair<SDOperand, SDOperand>
362X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
363 bool isVarArg, unsigned CallingConv,
364 bool isTailCall,
365 SDOperand Callee, ArgListTy &Args,
366 SelectionDAG &DAG) {
Chris Lattner01dd6df2006-05-19 21:34:04 +0000367 assert((!isVarArg || CallingConv == CallingConv::C ||
368 CallingConv == CallingConv::CSRet) &&
369 "Only CCC/CSRet takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000370
371 // If the callee is a GlobalAddress node (quite common, every direct call is)
372 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
373 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
374 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000375 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
376 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000377
Chris Lattner76ac0682005-11-15 00:40:23 +0000378 if (CallingConv == CallingConv::Fast && EnableFastCC)
379 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
Chris Lattner8be5be82006-05-23 18:50:38 +0000380 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, CallingConv,
381 Callee, Args, DAG);
Chris Lattner76ac0682005-11-15 00:40:23 +0000382}
383
384//===----------------------------------------------------------------------===//
385// C Calling Convention implementation
386//===----------------------------------------------------------------------===//
387
Evan Cheng24eb3f42006-04-27 05:35:28 +0000388/// AddLiveIn - This helper function adds the specified physical register to the
389/// MachineFunction as a live in value. It also creates a corresponding virtual
390/// register for it.
391static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
392 TargetRegisterClass *RC) {
393 assert(RC->contains(PReg) && "Not the correct regclass!");
394 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
395 MF.addLiveIn(PReg, VReg);
396 return VReg;
397}
398
Evan Cheng89001ad2006-04-27 08:31:10 +0000399/// HowToPassCCCArgument - Returns how an formal argument of the specified type
400/// should be passed. If it is through stack, returns the size of the stack
401/// frame; if it is through XMM register, returns the number of XMM registers
402/// are needed.
403static void
404HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
405 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000406 switch (ObjectVT) {
407 default: assert(0 && "Unhandled argument type!");
408 case MVT::i1:
409 case MVT::i8: ObjSize = 1; break;
410 case MVT::i16: ObjSize = 2; break;
411 case MVT::i32: ObjSize = 4; break;
412 case MVT::i64: ObjSize = 8; break;
413 case MVT::f32: ObjSize = 4; break;
414 case MVT::f64: ObjSize = 8; break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000415 case MVT::v16i8:
416 case MVT::v8i16:
417 case MVT::v4i32:
418 case MVT::v2i64:
419 case MVT::v4f32:
420 case MVT::v2f64:
421 if (NumXMMRegs < 3)
422 ObjXMMRegs = 1;
423 else
424 ObjSize = 16;
425 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000426 }
Evan Cheng48940d12006-04-27 01:32:22 +0000427}
428
Evan Cheng24eb3f42006-04-27 05:35:28 +0000429/// getFormalArgObjects - Returns itself if Op is a FORMAL_ARGUMENTS, otherwise
430/// returns the FORMAL_ARGUMENTS node(s) that made up parts of the node.
Evan Cheng48940d12006-04-27 01:32:22 +0000431static std::vector<SDOperand> getFormalArgObjects(SDOperand Op) {
432 unsigned Opc = Op.getOpcode();
433 std::vector<SDOperand> Objs;
434 if (Opc == ISD::TRUNCATE) {
435 Op = Op.getOperand(0);
436 assert(Op.getOpcode() == ISD::AssertSext ||
437 Op.getOpcode() == ISD::AssertZext);
438 Objs.push_back(Op.getOperand(0));
Evan Chengd43c5c62006-04-28 05:25:15 +0000439 } else if (Opc == ISD::FP_ROUND || Opc == ISD::VBIT_CONVERT) {
Evan Cheng48940d12006-04-27 01:32:22 +0000440 Objs.push_back(Op.getOperand(0));
441 } else if (Opc == ISD::BUILD_PAIR) {
442 Objs.push_back(Op.getOperand(0));
443 Objs.push_back(Op.getOperand(1));
444 } else {
445 Objs.push_back(Op);
446 }
447 return Objs;
448}
449
Evan Cheng17e734f2006-05-23 21:06:34 +0000450SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
451 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000452 MachineFunction &MF = DAG.getMachineFunction();
453 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000454 SDOperand Root = Op.getOperand(0);
455 std::vector<SDOperand> ArgValues;
Chris Lattner76ac0682005-11-15 00:40:23 +0000456
Evan Cheng48940d12006-04-27 01:32:22 +0000457 // Add DAG nodes to load the arguments... On entry to a function on the X86,
458 // the stack frame looks like this:
459 //
460 // [ESP] -- return address
461 // [ESP + 4] -- first argument (leftmost lexically)
462 // [ESP + 8] -- second argument, if first argument is four bytes in size
463 // ...
464 //
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000465 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng89001ad2006-04-27 08:31:10 +0000466 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
467 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000468 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000469 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
470 unsigned ArgIncrement = 4;
471 unsigned ObjSize = 0;
472 unsigned ObjXMMRegs = 0;
473 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
474 if (ObjSize >= 8)
475 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000476
Evan Cheng17e734f2006-05-23 21:06:34 +0000477 SDOperand ArgValue;
478 if (ObjXMMRegs) {
479 // Passed in a XMM register.
480 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
Evan Cheng89001ad2006-04-27 08:31:10 +0000481 X86::VR128RegisterClass);
Evan Cheng17e734f2006-05-23 21:06:34 +0000482 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
483 ArgValues.push_back(ArgValue);
484 NumXMMRegs += ObjXMMRegs;
485 } else {
486 // Create the frame index object for this incoming parameter...
487 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
488 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
489 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
490 DAG.getSrcValue(NULL));
491 ArgValues.push_back(ArgValue);
492 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000493 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000494 }
495
Evan Cheng17e734f2006-05-23 21:06:34 +0000496 ArgValues.push_back(Root);
497
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000498 // If the function takes variable number of arguments, make a frame index for
499 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000500 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
501 if (isVarArg)
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000502 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
503 ReturnAddrIndex = 0; // No return address slot generated yet.
504 BytesToPopOnReturn = 0; // Callee pops nothing.
505 BytesCallerReserves = ArgOffset;
Evan Cheng17e734f2006-05-23 21:06:34 +0000506
Chris Lattner8be5be82006-05-23 18:50:38 +0000507 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
508 // pointer.
Evan Cheng17e734f2006-05-23 21:06:34 +0000509 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner8be5be82006-05-23 18:50:38 +0000510 Subtarget->isTargetDarwin())
511 BytesToPopOnReturn = 4;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000512
Evan Cheng17e734f2006-05-23 21:06:34 +0000513 // Return the new list of results.
514 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
515 Op.Val->value_end());
516 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattner76ac0682005-11-15 00:40:23 +0000517}
518
519std::pair<SDOperand, SDOperand>
520X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
521 bool isVarArg, bool isTailCall,
Chris Lattner8be5be82006-05-23 18:50:38 +0000522 unsigned CallingConv,
Chris Lattner76ac0682005-11-15 00:40:23 +0000523 SDOperand Callee, ArgListTy &Args,
524 SelectionDAG &DAG) {
525 // Count how many bytes are to be pushed on the stack.
526 unsigned NumBytes = 0;
527
Evan Cheng88decde2006-04-28 21:29:37 +0000528 // Keep track of the number of XMM regs passed so far.
529 unsigned NumXMMRegs = 0;
530 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
531
532 std::vector<SDOperand> RegValuesToPass;
Chris Lattner76ac0682005-11-15 00:40:23 +0000533 if (Args.empty()) {
534 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000535 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000536 } else {
537 for (unsigned i = 0, e = Args.size(); i != e; ++i)
538 switch (getValueType(Args[i].second)) {
539 default: assert(0 && "Unknown value type!");
540 case MVT::i1:
541 case MVT::i8:
542 case MVT::i16:
543 case MVT::i32:
544 case MVT::f32:
545 NumBytes += 4;
546 break;
547 case MVT::i64:
548 case MVT::f64:
549 NumBytes += 8;
550 break;
Evan Cheng88decde2006-04-28 21:29:37 +0000551 case MVT::Vector:
552 if (NumXMMRegs < 3)
553 ++NumXMMRegs;
554 else
555 NumBytes += 16;
556 break;
Chris Lattner76ac0682005-11-15 00:40:23 +0000557 }
558
Chris Lattner62c34842006-02-13 09:00:43 +0000559 Chain = DAG.getCALLSEQ_START(Chain,
560 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000561
562 // Arguments go on the stack in reverse order, as specified by the ABI.
563 unsigned ArgOffset = 0;
Evan Cheng88decde2006-04-28 21:29:37 +0000564 NumXMMRegs = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000565 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000566 std::vector<SDOperand> Stores;
Chris Lattner76ac0682005-11-15 00:40:23 +0000567 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000568 switch (getValueType(Args[i].second)) {
569 default: assert(0 && "Unexpected ValueType for argument!");
570 case MVT::i1:
571 case MVT::i8:
572 case MVT::i16:
573 // Promote the integer to 32 bits. If the input type is signed use a
574 // sign extend, otherwise use a zero extend.
575 if (Args[i].second->isSigned())
576 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
577 else
578 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
579
580 // FALL THROUGH
581 case MVT::i32:
Evan Cheng88decde2006-04-28 21:29:37 +0000582 case MVT::f32: {
583 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
584 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner76ac0682005-11-15 00:40:23 +0000585 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
586 Args[i].first, PtrOff,
587 DAG.getSrcValue(NULL)));
588 ArgOffset += 4;
589 break;
Evan Cheng88decde2006-04-28 21:29:37 +0000590 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000591 case MVT::i64:
Evan Cheng88decde2006-04-28 21:29:37 +0000592 case MVT::f64: {
593 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
594 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner76ac0682005-11-15 00:40:23 +0000595 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
596 Args[i].first, PtrOff,
597 DAG.getSrcValue(NULL)));
598 ArgOffset += 8;
599 break;
600 }
Evan Cheng88decde2006-04-28 21:29:37 +0000601 case MVT::Vector:
602 if (NumXMMRegs < 3) {
603 RegValuesToPass.push_back(Args[i].first);
604 NumXMMRegs++;
605 } else {
606 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
607 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
608 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
609 Args[i].first, PtrOff,
610 DAG.getSrcValue(NULL)));
611 ArgOffset += 16;
612 }
613 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000614 }
Evan Cheng88decde2006-04-28 21:29:37 +0000615 if (!Stores.empty())
Chris Lattner76ac0682005-11-15 00:40:23 +0000616 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
617 }
618
619 std::vector<MVT::ValueType> RetVals;
620 MVT::ValueType RetTyVT = getValueType(RetTy);
621 RetVals.push_back(MVT::Other);
622
623 // The result values produced have to be legal. Promote the result.
624 switch (RetTyVT) {
625 case MVT::isVoid: break;
626 default:
627 RetVals.push_back(RetTyVT);
628 break;
629 case MVT::i1:
630 case MVT::i8:
631 case MVT::i16:
632 RetVals.push_back(MVT::i32);
633 break;
634 case MVT::f32:
635 if (X86ScalarSSE)
636 RetVals.push_back(MVT::f32);
637 else
638 RetVals.push_back(MVT::f64);
639 break;
640 case MVT::i64:
641 RetVals.push_back(MVT::i32);
642 RetVals.push_back(MVT::i32);
643 break;
644 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000645
Evan Cheng88decde2006-04-28 21:29:37 +0000646 // Build a sequence of copy-to-reg nodes chained together with token chain
647 // and flag operands which copy the outgoing args into registers.
648 SDOperand InFlag;
649 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
650 unsigned CCReg = XMMArgRegs[i];
651 SDOperand RegToPass = RegValuesToPass[i];
652 assert(RegToPass.getValueType() == MVT::Vector);
Chris Lattner3d826992006-05-16 06:45:34 +0000653 unsigned NumElems =
654 cast<ConstantSDNode>(*(RegToPass.Val->op_end()-2))->getValue();
Evan Cheng88decde2006-04-28 21:29:37 +0000655 MVT::ValueType EVT = cast<VTSDNode>(*(RegToPass.Val->op_end()-1))->getVT();
656 MVT::ValueType PVT = getVectorType(EVT, NumElems);
657 SDOperand CCRegNode = DAG.getRegister(CCReg, PVT);
658 RegToPass = DAG.getNode(ISD::VBIT_CONVERT, PVT, RegToPass);
659 Chain = DAG.getCopyToReg(Chain, CCRegNode, RegToPass, InFlag);
660 InFlag = Chain.getValue(1);
661 }
662
Nate Begeman7e5496d2006-02-17 00:03:04 +0000663 std::vector<MVT::ValueType> NodeTys;
664 NodeTys.push_back(MVT::Other); // Returns a chain
665 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
666 std::vector<SDOperand> Ops;
667 Ops.push_back(Chain);
668 Ops.push_back(Callee);
Evan Cheng88decde2006-04-28 21:29:37 +0000669 if (InFlag.Val)
670 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000671
Nate Begeman7e5496d2006-02-17 00:03:04 +0000672 // FIXME: Do not generate X86ISD::TAILCALL for now.
673 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng88decde2006-04-28 21:29:37 +0000674 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000675
Chris Lattner8be5be82006-05-23 18:50:38 +0000676 // Create the CALLSEQ_END node.
677 unsigned NumBytesForCalleeToPush = 0;
678
679 // If this is is a call to a struct-return function on Darwin/X86, the callee
680 // pops the hidden struct pointer, so we have to push it back.
681 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
682 NumBytesForCalleeToPush = 4;
683
Nate Begeman7e5496d2006-02-17 00:03:04 +0000684 NodeTys.clear();
685 NodeTys.push_back(MVT::Other); // Returns a chain
686 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
687 Ops.clear();
688 Ops.push_back(Chain);
689 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000690 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000691 Ops.push_back(InFlag);
692 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
693 InFlag = Chain.getValue(1);
694
695 SDOperand RetVal;
696 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000697 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000698 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000699 case MVT::i1:
700 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000701 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
702 Chain = RetVal.getValue(1);
703 if (RetTyVT == MVT::i1)
704 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
705 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000706 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000707 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
708 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000709 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000710 case MVT::i32:
711 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
712 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000713 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000714 case MVT::i64: {
715 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
716 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
717 Lo.getValue(2));
718 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
719 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000720 break;
721 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000722 case MVT::f32:
723 case MVT::f64: {
724 std::vector<MVT::ValueType> Tys;
725 Tys.push_back(MVT::f64);
726 Tys.push_back(MVT::Other);
727 Tys.push_back(MVT::Flag);
728 std::vector<SDOperand> Ops;
729 Ops.push_back(Chain);
730 Ops.push_back(InFlag);
731 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
732 Chain = RetVal.getValue(1);
733 InFlag = RetVal.getValue(2);
734 if (X86ScalarSSE) {
735 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
736 // shouldn't be necessary except that RFP cannot be live across
737 // multiple blocks. When stackifier is fixed, they can be uncoupled.
738 MachineFunction &MF = DAG.getMachineFunction();
739 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
740 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
741 Tys.clear();
742 Tys.push_back(MVT::Other);
743 Ops.clear();
744 Ops.push_back(Chain);
745 Ops.push_back(RetVal);
746 Ops.push_back(StackSlot);
747 Ops.push_back(DAG.getValueType(RetTyVT));
748 Ops.push_back(InFlag);
749 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
750 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
751 DAG.getSrcValue(NULL));
752 Chain = RetVal.getValue(1);
753 }
Evan Cheng45e190982006-01-05 00:27:02 +0000754
Nate Begeman7e5496d2006-02-17 00:03:04 +0000755 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
756 // FIXME: we would really like to remember that this FP_ROUND
757 // operation is okay to eliminate if we allow excess FP precision.
758 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
759 break;
760 }
Evan Cheng88decde2006-04-28 21:29:37 +0000761 case MVT::Vector: {
762 const PackedType *PTy = cast<PackedType>(RetTy);
763 MVT::ValueType EVT;
764 MVT::ValueType LVT;
765 unsigned NumRegs = getPackedTypeBreakdown(PTy, EVT, LVT);
766 assert(NumRegs == 1 && "Unsupported type!");
767 RetVal = DAG.getCopyFromReg(Chain, X86::XMM0, EVT, InFlag);
768 Chain = RetVal.getValue(1);
769 break;
770 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000771 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000772 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000773
774 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000775}
776
Chris Lattner76ac0682005-11-15 00:40:23 +0000777//===----------------------------------------------------------------------===//
778// Fast Calling Convention implementation
779//===----------------------------------------------------------------------===//
780//
781// The X86 'fast' calling convention passes up to two integer arguments in
782// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
783// and requires that the callee pop its arguments off the stack (allowing proper
784// tail calls), and has the same return value conventions as C calling convs.
785//
786// This calling convention always arranges for the callee pop value to be 8n+4
787// bytes, which is needed for tail recursion elimination and stack alignment
788// reasons.
789//
790// Note that this can be enhanced in the future to pass fp vals in registers
791// (when we have a global fp allocator) and do other tricks.
792//
793
Chris Lattner388fc4d2006-03-17 17:27:47 +0000794// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
795// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
796// EDX". Anything more is illegal.
797//
798// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000799// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000800// a physreg with a virtreg, this increases the size of the physreg's live
801// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000802// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000803// allocator to wedge itself.
804//
805// This code triggers this problem more often if we pass args in registers,
806// so disable it until this is fixed.
807//
808// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
809// about code being dead.
810//
811static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000812
Chris Lattner76ac0682005-11-15 00:40:23 +0000813
Evan Cheng89001ad2006-04-27 08:31:10 +0000814/// HowToPassFastCCArgument - Returns how an formal argument of the specified
815/// type should be passed. If it is through stack, returns the size of the stack
816/// frame; if it is through integer or XMM register, returns the number of
817/// integer or XMM registers are needed.
Evan Cheng48940d12006-04-27 01:32:22 +0000818static void
Evan Cheng89001ad2006-04-27 08:31:10 +0000819HowToPassFastCCArgument(MVT::ValueType ObjectVT,
820 unsigned NumIntRegs, unsigned NumXMMRegs,
821 unsigned &ObjSize, unsigned &ObjIntRegs,
822 unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000823 ObjSize = 0;
824 NumIntRegs = 0;
825
826 switch (ObjectVT) {
827 default: assert(0 && "Unhandled argument type!");
828 case MVT::i1:
829 case MVT::i8:
830 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000831 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000832 else
833 ObjSize = 1;
834 break;
835 case MVT::i16:
836 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000837 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000838 else
839 ObjSize = 2;
840 break;
841 case MVT::i32:
842 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000843 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000844 else
845 ObjSize = 4;
846 break;
847 case MVT::i64:
848 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000849 ObjIntRegs = 2;
Evan Cheng48940d12006-04-27 01:32:22 +0000850 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000851 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000852 ObjSize = 4;
853 } else
854 ObjSize = 8;
855 case MVT::f32:
856 ObjSize = 4;
857 break;
858 case MVT::f64:
859 ObjSize = 8;
860 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000861 case MVT::v16i8:
862 case MVT::v8i16:
863 case MVT::v4i32:
864 case MVT::v2i64:
865 case MVT::v4f32:
866 case MVT::v2f64:
867 if (NumXMMRegs < 3)
868 ObjXMMRegs = 1;
869 else
870 ObjSize = 16;
871 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000872 }
873}
874
Evan Cheng17e734f2006-05-23 21:06:34 +0000875SDOperand
876X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
877 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattner76ac0682005-11-15 00:40:23 +0000878 MachineFunction &MF = DAG.getMachineFunction();
879 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000880 SDOperand Root = Op.getOperand(0);
881 std::vector<SDOperand> ArgValues;
Chris Lattner76ac0682005-11-15 00:40:23 +0000882
Evan Cheng48940d12006-04-27 01:32:22 +0000883 // Add DAG nodes to load the arguments... On entry to a function the stack
884 // frame looks like this:
885 //
886 // [ESP] -- return address
887 // [ESP + 4] -- first nonreg argument (leftmost lexically)
888 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
889 // ...
Chris Lattner76ac0682005-11-15 00:40:23 +0000890 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
891
892 // Keep track of the number of integer regs passed so far. This can be either
893 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
894 // used).
895 unsigned NumIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000896 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
897 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Chris Lattner43798852006-03-17 05:10:20 +0000898
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000899 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000900 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
901 unsigned ArgIncrement = 4;
902 unsigned ObjSize = 0;
903 unsigned ObjIntRegs = 0;
904 unsigned ObjXMMRegs = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000905
Evan Cheng17e734f2006-05-23 21:06:34 +0000906 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
907 ObjSize, ObjIntRegs, ObjXMMRegs);
908 if (ObjSize >= 8)
909 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000910
Evan Cheng17e734f2006-05-23 21:06:34 +0000911 unsigned Reg;
912 SDOperand ArgValue;
913 if (ObjIntRegs || ObjXMMRegs) {
914 switch (ObjectVT) {
915 default: assert(0 && "Unhandled argument type!");
916 case MVT::i1:
917 case MVT::i8:
918 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
919 X86::GR8RegisterClass);
920 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
921 break;
922 case MVT::i16:
923 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
924 X86::GR16RegisterClass);
925 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
926 break;
927 case MVT::i32:
928 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
929 X86::GR32RegisterClass);
930 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
931 break;
932 case MVT::i64:
933 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
934 X86::GR32RegisterClass);
935 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
936 if (ObjIntRegs == 2) {
937 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
938 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
939 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000940 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000941 break;
942 case MVT::v16i8:
943 case MVT::v8i16:
944 case MVT::v4i32:
945 case MVT::v2i64:
946 case MVT::v4f32:
947 case MVT::v2f64:
948 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
949 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
950 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000951 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000952 NumIntRegs += ObjIntRegs;
953 NumXMMRegs += ObjXMMRegs;
Chris Lattner76ac0682005-11-15 00:40:23 +0000954 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000955
956 if (ObjSize) {
957 // Create the SelectionDAG nodes corresponding to a load from this
958 // parameter.
959 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
960 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
961 if (ObjectVT == MVT::i64 && ObjIntRegs) {
962 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
963 DAG.getSrcValue(NULL));
964 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
965 } else
966 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
967 DAG.getSrcValue(NULL));
968 ArgOffset += ArgIncrement; // Move on to the next argument.
969 }
970
971 ArgValues.push_back(ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000972 }
973
Evan Cheng17e734f2006-05-23 21:06:34 +0000974 ArgValues.push_back(Root);
975
Chris Lattner76ac0682005-11-15 00:40:23 +0000976 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
977 // arguments and the arguments after the retaddr has been pushed are aligned.
978 if ((ArgOffset & 7) == 0)
979 ArgOffset += 4;
980
981 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
982 ReturnAddrIndex = 0; // No return address slot generated yet.
983 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
984 BytesCallerReserves = 0;
985
986 // Finally, inform the code generator which regs we return values in.
Evan Cheng17e734f2006-05-23 21:06:34 +0000987 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000988 default: assert(0 && "Unknown type!");
989 case MVT::isVoid: break;
990 case MVT::i1:
991 case MVT::i8:
992 case MVT::i16:
993 case MVT::i32:
994 MF.addLiveOut(X86::EAX);
995 break;
996 case MVT::i64:
997 MF.addLiveOut(X86::EAX);
998 MF.addLiveOut(X86::EDX);
999 break;
1000 case MVT::f32:
1001 case MVT::f64:
1002 MF.addLiveOut(X86::ST0);
1003 break;
Evan Cheng88decde2006-04-28 21:29:37 +00001004 case MVT::Vector: {
Evan Cheng17e734f2006-05-23 21:06:34 +00001005 const PackedType *PTy = cast<PackedType>(MF.getFunction()->getReturnType());
Evan Cheng88decde2006-04-28 21:29:37 +00001006 MVT::ValueType EVT;
1007 MVT::ValueType LVT;
1008 unsigned NumRegs = getPackedTypeBreakdown(PTy, EVT, LVT);
1009 assert(NumRegs == 1 && "Unsupported type!");
1010 MF.addLiveOut(X86::XMM0);
1011 break;
1012 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001013 }
Evan Cheng88decde2006-04-28 21:29:37 +00001014
Evan Cheng17e734f2006-05-23 21:06:34 +00001015 // Return the new list of results.
1016 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1017 Op.Val->value_end());
1018 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattner76ac0682005-11-15 00:40:23 +00001019}
1020
1021std::pair<SDOperand, SDOperand>
1022X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
1023 bool isTailCall, SDOperand Callee,
1024 ArgListTy &Args, SelectionDAG &DAG) {
1025 // Count how many bytes are to be pushed on the stack.
1026 unsigned NumBytes = 0;
1027
1028 // Keep track of the number of integer regs passed so far. This can be either
1029 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1030 // used).
1031 unsigned NumIntRegs = 0;
1032
1033 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1034 switch (getValueType(Args[i].second)) {
1035 default: assert(0 && "Unknown value type!");
1036 case MVT::i1:
1037 case MVT::i8:
1038 case MVT::i16:
1039 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001040 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001041 ++NumIntRegs;
1042 break;
1043 }
1044 // fall through
1045 case MVT::f32:
1046 NumBytes += 4;
1047 break;
1048 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001049 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
1050 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +00001051 break;
Chris Lattner43798852006-03-17 05:10:20 +00001052 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
1053 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +00001054 NumBytes += 4;
1055 break;
1056 }
1057
1058 // fall through
1059 case MVT::f64:
1060 NumBytes += 8;
1061 break;
1062 }
1063
1064 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1065 // arguments and the arguments after the retaddr has been pushed are aligned.
1066 if ((NumBytes & 7) == 0)
1067 NumBytes += 4;
1068
Chris Lattner62c34842006-02-13 09:00:43 +00001069 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +00001070
1071 // Arguments go on the stack in reverse order, as specified by the ABI.
1072 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +00001073 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +00001074 NumIntRegs = 0;
1075 std::vector<SDOperand> Stores;
1076 std::vector<SDOperand> RegValuesToPass;
1077 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1078 switch (getValueType(Args[i].second)) {
1079 default: assert(0 && "Unexpected ValueType for argument!");
1080 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +00001081 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
1082 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +00001083 case MVT::i8:
1084 case MVT::i16:
1085 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001086 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001087 RegValuesToPass.push_back(Args[i].first);
1088 ++NumIntRegs;
1089 break;
1090 }
1091 // Fall through
1092 case MVT::f32: {
1093 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1094 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1095 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1096 Args[i].first, PtrOff,
1097 DAG.getSrcValue(NULL)));
1098 ArgOffset += 4;
1099 break;
1100 }
1101 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001102 // Can pass (at least) part of it in regs?
1103 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001104 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1105 Args[i].first, DAG.getConstant(1, MVT::i32));
1106 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1107 Args[i].first, DAG.getConstant(0, MVT::i32));
1108 RegValuesToPass.push_back(Lo);
1109 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +00001110
1111 // Pass both parts in regs?
1112 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001113 RegValuesToPass.push_back(Hi);
1114 ++NumIntRegs;
1115 } else {
1116 // Pass the high part in memory.
1117 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1118 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1119 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1120 Hi, PtrOff, DAG.getSrcValue(NULL)));
1121 ArgOffset += 4;
1122 }
1123 break;
1124 }
1125 // Fall through
1126 case MVT::f64:
1127 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1128 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1129 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1130 Args[i].first, PtrOff,
1131 DAG.getSrcValue(NULL)));
1132 ArgOffset += 8;
1133 break;
1134 }
1135 }
1136 if (!Stores.empty())
1137 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
1138
1139 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1140 // arguments and the arguments after the retaddr has been pushed are aligned.
1141 if ((ArgOffset & 7) == 0)
1142 ArgOffset += 4;
1143
1144 std::vector<MVT::ValueType> RetVals;
1145 MVT::ValueType RetTyVT = getValueType(RetTy);
1146
1147 RetVals.push_back(MVT::Other);
1148
1149 // The result values produced have to be legal. Promote the result.
1150 switch (RetTyVT) {
1151 case MVT::isVoid: break;
1152 default:
1153 RetVals.push_back(RetTyVT);
1154 break;
1155 case MVT::i1:
1156 case MVT::i8:
1157 case MVT::i16:
1158 RetVals.push_back(MVT::i32);
1159 break;
1160 case MVT::f32:
1161 if (X86ScalarSSE)
1162 RetVals.push_back(MVT::f32);
1163 else
1164 RetVals.push_back(MVT::f64);
1165 break;
1166 case MVT::i64:
1167 RetVals.push_back(MVT::i32);
1168 RetVals.push_back(MVT::i32);
1169 break;
1170 }
1171
Nate Begeman7e5496d2006-02-17 00:03:04 +00001172 // Build a sequence of copy-to-reg nodes chained together with token chain
1173 // and flag operands which copy the outgoing args into registers.
1174 SDOperand InFlag;
1175 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1176 unsigned CCReg;
1177 SDOperand RegToPass = RegValuesToPass[i];
1178 switch (RegToPass.getValueType()) {
1179 default: assert(0 && "Bad thing to pass in regs");
1180 case MVT::i8:
1181 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001182 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001183 case MVT::i16:
1184 CCReg = (i == 0) ? X86::AX : X86::DX;
1185 break;
1186 case MVT::i32:
1187 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1188 break;
1189 }
1190
1191 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1192 InFlag = Chain.getValue(1);
1193 }
1194
1195 std::vector<MVT::ValueType> NodeTys;
1196 NodeTys.push_back(MVT::Other); // Returns a chain
1197 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1198 std::vector<SDOperand> Ops;
1199 Ops.push_back(Chain);
1200 Ops.push_back(Callee);
1201 if (InFlag.Val)
1202 Ops.push_back(InFlag);
1203
1204 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001205 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1206 NodeTys, Ops);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001207 InFlag = Chain.getValue(1);
1208
1209 NodeTys.clear();
1210 NodeTys.push_back(MVT::Other); // Returns a chain
1211 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1212 Ops.clear();
1213 Ops.push_back(Chain);
1214 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1215 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1216 Ops.push_back(InFlag);
1217 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1218 InFlag = Chain.getValue(1);
1219
1220 SDOperand RetVal;
1221 if (RetTyVT != MVT::isVoid) {
1222 switch (RetTyVT) {
1223 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001224 case MVT::i1:
1225 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001226 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1227 Chain = RetVal.getValue(1);
1228 if (RetTyVT == MVT::i1)
1229 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1230 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001231 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001232 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1233 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001234 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001235 case MVT::i32:
1236 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1237 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001238 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001239 case MVT::i64: {
1240 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1241 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1242 Lo.getValue(2));
1243 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1244 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001245 break;
1246 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001247 case MVT::f32:
1248 case MVT::f64: {
1249 std::vector<MVT::ValueType> Tys;
1250 Tys.push_back(MVT::f64);
1251 Tys.push_back(MVT::Other);
1252 Tys.push_back(MVT::Flag);
1253 std::vector<SDOperand> Ops;
1254 Ops.push_back(Chain);
1255 Ops.push_back(InFlag);
1256 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1257 Chain = RetVal.getValue(1);
1258 InFlag = RetVal.getValue(2);
1259 if (X86ScalarSSE) {
1260 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1261 // shouldn't be necessary except that RFP cannot be live across
1262 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1263 MachineFunction &MF = DAG.getMachineFunction();
1264 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1265 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1266 Tys.clear();
1267 Tys.push_back(MVT::Other);
1268 Ops.clear();
1269 Ops.push_back(Chain);
1270 Ops.push_back(RetVal);
1271 Ops.push_back(StackSlot);
1272 Ops.push_back(DAG.getValueType(RetTyVT));
1273 Ops.push_back(InFlag);
1274 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1275 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1276 DAG.getSrcValue(NULL));
1277 Chain = RetVal.getValue(1);
1278 }
Evan Cheng172fce72006-01-06 00:43:03 +00001279
Nate Begeman7e5496d2006-02-17 00:03:04 +00001280 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1281 // FIXME: we would really like to remember that this FP_ROUND
1282 // operation is okay to eliminate if we allow excess FP precision.
1283 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1284 break;
1285 }
1286 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001287 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001288
1289 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001290}
1291
1292SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1293 if (ReturnAddrIndex == 0) {
1294 // Set up a frame object for the return address.
1295 MachineFunction &MF = DAG.getMachineFunction();
1296 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1297 }
1298
1299 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1300}
1301
1302
1303
1304std::pair<SDOperand, SDOperand> X86TargetLowering::
1305LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1306 SelectionDAG &DAG) {
1307 SDOperand Result;
1308 if (Depth) // Depths > 0 not supported yet!
1309 Result = DAG.getConstant(0, getPointerTy());
1310 else {
1311 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1312 if (!isFrameAddress)
1313 // Just load the return address
1314 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1315 DAG.getSrcValue(NULL));
1316 else
1317 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1318 DAG.getConstant(4, MVT::i32));
1319 }
1320 return std::make_pair(Result, Chain);
1321}
1322
Evan Cheng339edad2006-01-11 00:33:36 +00001323/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1324/// which corresponds to the condition code.
1325static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1326 switch (X86CC) {
1327 default: assert(0 && "Unknown X86 conditional code!");
1328 case X86ISD::COND_A: return X86::JA;
1329 case X86ISD::COND_AE: return X86::JAE;
1330 case X86ISD::COND_B: return X86::JB;
1331 case X86ISD::COND_BE: return X86::JBE;
1332 case X86ISD::COND_E: return X86::JE;
1333 case X86ISD::COND_G: return X86::JG;
1334 case X86ISD::COND_GE: return X86::JGE;
1335 case X86ISD::COND_L: return X86::JL;
1336 case X86ISD::COND_LE: return X86::JLE;
1337 case X86ISD::COND_NE: return X86::JNE;
1338 case X86ISD::COND_NO: return X86::JNO;
1339 case X86ISD::COND_NP: return X86::JNP;
1340 case X86ISD::COND_NS: return X86::JNS;
1341 case X86ISD::COND_O: return X86::JO;
1342 case X86ISD::COND_P: return X86::JP;
1343 case X86ISD::COND_S: return X86::JS;
1344 }
1345}
Chris Lattner76ac0682005-11-15 00:40:23 +00001346
Evan Cheng45df7f82006-01-30 23:41:35 +00001347/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1348/// specific condition code. It returns a false if it cannot do a direct
1349/// translation. X86CC is the translated CondCode. Flip is set to true if the
1350/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001351static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1352 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001353 Flip = false;
1354 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001355 if (!isFP) {
1356 switch (SetCCOpcode) {
1357 default: break;
1358 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1359 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1360 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1361 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1362 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1363 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1364 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1365 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1366 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1367 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1368 }
1369 } else {
1370 // On a floating point condition, the flags are set as follows:
1371 // ZF PF CF op
1372 // 0 | 0 | 0 | X > Y
1373 // 0 | 0 | 1 | X < Y
1374 // 1 | 0 | 0 | X == Y
1375 // 1 | 1 | 1 | unordered
1376 switch (SetCCOpcode) {
1377 default: break;
1378 case ISD::SETUEQ:
1379 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001380 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001381 case ISD::SETOGT:
1382 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001383 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001384 case ISD::SETOGE:
1385 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001386 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001387 case ISD::SETULT:
1388 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001389 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001390 case ISD::SETULE:
1391 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1392 case ISD::SETONE:
1393 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1394 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1395 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1396 }
1397 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001398
1399 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001400}
1401
Evan Cheng78038292006-04-05 23:38:46 +00001402static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1403 bool &Flip) {
1404 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1405}
1406
Evan Cheng339edad2006-01-11 00:33:36 +00001407/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1408/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001409/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001410static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001411 switch (X86CC) {
1412 default:
1413 return false;
1414 case X86ISD::COND_B:
1415 case X86ISD::COND_BE:
1416 case X86ISD::COND_E:
1417 case X86ISD::COND_P:
1418 case X86ISD::COND_A:
1419 case X86ISD::COND_AE:
1420 case X86ISD::COND_NE:
1421 case X86ISD::COND_NP:
1422 return true;
1423 }
1424}
1425
Evan Cheng339edad2006-01-11 00:33:36 +00001426MachineBasicBlock *
1427X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1428 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001429 switch (MI->getOpcode()) {
1430 default: assert(false && "Unexpected instr type to insert");
1431 case X86::CMOV_FR32:
Evan Cheng617a6a82006-04-10 07:23:14 +00001432 case X86::CMOV_FR64:
1433 case X86::CMOV_V4F32:
1434 case X86::CMOV_V2F64:
1435 case X86::CMOV_V2I64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001436 // To "insert" a SELECT_CC instruction, we actually have to insert the
1437 // diamond control-flow pattern. The incoming instruction knows the
1438 // destination vreg to set, the condition code register to branch on, the
1439 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001440 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1441 ilist<MachineBasicBlock>::iterator It = BB;
1442 ++It;
1443
1444 // thisMBB:
1445 // ...
1446 // TrueVal = ...
1447 // cmpTY ccX, r1, r2
1448 // bCC copy1MBB
1449 // fallthrough --> copy0MBB
1450 MachineBasicBlock *thisMBB = BB;
1451 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1452 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1453 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1454 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1455 MachineFunction *F = BB->getParent();
1456 F->getBasicBlockList().insert(It, copy0MBB);
1457 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001458 // Update machine-CFG edges by first adding all successors of the current
1459 // block to the new block which will contain the Phi node for the select.
1460 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1461 e = BB->succ_end(); i != e; ++i)
1462 sinkMBB->addSuccessor(*i);
1463 // Next, remove all successors of the current block, and add the true
1464 // and fallthrough blocks as its successors.
1465 while(!BB->succ_empty())
1466 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001467 BB->addSuccessor(copy0MBB);
1468 BB->addSuccessor(sinkMBB);
1469
1470 // copy0MBB:
1471 // %FalseValue = ...
1472 // # fallthrough to sinkMBB
1473 BB = copy0MBB;
1474
1475 // Update machine-CFG edges
1476 BB->addSuccessor(sinkMBB);
1477
1478 // sinkMBB:
1479 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1480 // ...
1481 BB = sinkMBB;
1482 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1483 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1484 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001485
Evan Cheng911c68d2006-01-16 21:21:29 +00001486 delete MI; // The pseudo instruction is gone now.
1487 return BB;
1488 }
Evan Cheng339edad2006-01-11 00:33:36 +00001489
Evan Cheng911c68d2006-01-16 21:21:29 +00001490 case X86::FP_TO_INT16_IN_MEM:
1491 case X86::FP_TO_INT32_IN_MEM:
1492 case X86::FP_TO_INT64_IN_MEM: {
1493 // Change the floating point control register to use "round towards zero"
1494 // mode when truncating to an integer value.
1495 MachineFunction *F = BB->getParent();
1496 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1497 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1498
1499 // Load the old value of the high byte of the control word...
1500 unsigned OldCW =
Evan Cheng9fee4422006-05-16 07:21:53 +00001501 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng911c68d2006-01-16 21:21:29 +00001502 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1503
1504 // Set the high part to be round to zero...
1505 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1506
1507 // Reload the modified control word now...
1508 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1509
1510 // Restore the memory image of control word to original value
1511 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1512
1513 // Get the X86 opcode to use.
1514 unsigned Opc;
1515 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001516 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001517 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1518 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1519 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1520 }
1521
1522 X86AddressMode AM;
1523 MachineOperand &Op = MI->getOperand(0);
1524 if (Op.isRegister()) {
1525 AM.BaseType = X86AddressMode::RegBase;
1526 AM.Base.Reg = Op.getReg();
1527 } else {
1528 AM.BaseType = X86AddressMode::FrameIndexBase;
1529 AM.Base.FrameIndex = Op.getFrameIndex();
1530 }
1531 Op = MI->getOperand(1);
1532 if (Op.isImmediate())
1533 AM.Scale = Op.getImmedValue();
1534 Op = MI->getOperand(2);
1535 if (Op.isImmediate())
1536 AM.IndexReg = Op.getImmedValue();
1537 Op = MI->getOperand(3);
1538 if (Op.isGlobalAddress()) {
1539 AM.GV = Op.getGlobal();
1540 } else {
1541 AM.Disp = Op.getImmedValue();
1542 }
1543 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1544
1545 // Reload the original control word now.
1546 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1547
1548 delete MI; // The pseudo instruction is gone now.
1549 return BB;
1550 }
1551 }
Evan Cheng339edad2006-01-11 00:33:36 +00001552}
1553
1554
1555//===----------------------------------------------------------------------===//
1556// X86 Custom Lowering Hooks
1557//===----------------------------------------------------------------------===//
1558
Evan Chengaf598d22006-03-13 23:18:16 +00001559/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1560/// load. For Darwin, external and weak symbols are indirect, loading the value
1561/// at address GV rather then the value of GV itself. This means that the
1562/// GlobalAddress must be in the base or index register of the address, not the
1563/// GV offset field.
1564static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1565 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1566 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1567}
1568
Evan Chengc995b452006-04-06 23:23:56 +00001569/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001570/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001571static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1572 if (Op.getOpcode() == ISD::UNDEF)
1573 return true;
1574
1575 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001576 return (Val >= Low && Val < Hi);
1577}
1578
1579/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1580/// true if Op is undef or if its value equal to the specified value.
1581static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1582 if (Op.getOpcode() == ISD::UNDEF)
1583 return true;
1584 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001585}
1586
Evan Cheng68ad48b2006-03-22 18:59:22 +00001587/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1588/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1589bool X86::isPSHUFDMask(SDNode *N) {
1590 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1591
1592 if (N->getNumOperands() != 4)
1593 return false;
1594
1595 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001596 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001597 SDOperand Arg = N->getOperand(i);
1598 if (Arg.getOpcode() == ISD::UNDEF) continue;
1599 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1600 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001601 return false;
1602 }
1603
1604 return true;
1605}
1606
1607/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001608/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001609bool X86::isPSHUFHWMask(SDNode *N) {
1610 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1611
1612 if (N->getNumOperands() != 8)
1613 return false;
1614
1615 // Lower quadword copied in order.
1616 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001617 SDOperand Arg = N->getOperand(i);
1618 if (Arg.getOpcode() == ISD::UNDEF) continue;
1619 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1620 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001621 return false;
1622 }
1623
1624 // Upper quadword shuffled.
1625 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001626 SDOperand Arg = N->getOperand(i);
1627 if (Arg.getOpcode() == ISD::UNDEF) continue;
1628 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1629 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001630 if (Val < 4 || Val > 7)
1631 return false;
1632 }
1633
1634 return true;
1635}
1636
1637/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001638/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001639bool X86::isPSHUFLWMask(SDNode *N) {
1640 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1641
1642 if (N->getNumOperands() != 8)
1643 return false;
1644
1645 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001646 for (unsigned i = 4; i != 8; ++i)
1647 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001648 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001649
1650 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001651 for (unsigned i = 0; i != 4; ++i)
1652 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001653 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001654
1655 return true;
1656}
1657
Evan Chengd27fb3e2006-03-24 01:18:28 +00001658/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1659/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng60f0b892006-04-20 08:58:49 +00001660static bool isSHUFPMask(std::vector<SDOperand> &N) {
1661 unsigned NumElems = N.size();
1662 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001663
Evan Cheng60f0b892006-04-20 08:58:49 +00001664 unsigned Half = NumElems / 2;
1665 for (unsigned i = 0; i < Half; ++i)
1666 if (!isUndefOrInRange(N[i], 0, NumElems))
1667 return false;
1668 for (unsigned i = Half; i < NumElems; ++i)
1669 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1670 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001671
1672 return true;
1673}
1674
Evan Cheng60f0b892006-04-20 08:58:49 +00001675bool X86::isSHUFPMask(SDNode *N) {
1676 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1677 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1678 return ::isSHUFPMask(Ops);
1679}
1680
1681/// isCommutedSHUFP - Returns true if the shuffle mask is except
1682/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1683/// half elements to come from vector 1 (which would equal the dest.) and
1684/// the upper half to come from vector 2.
1685static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1686 unsigned NumElems = Ops.size();
1687 if (NumElems != 2 && NumElems != 4) return false;
1688
1689 unsigned Half = NumElems / 2;
1690 for (unsigned i = 0; i < Half; ++i)
1691 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1692 return false;
1693 for (unsigned i = Half; i < NumElems; ++i)
1694 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1695 return false;
1696 return true;
1697}
1698
1699static bool isCommutedSHUFP(SDNode *N) {
1700 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1701 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1702 return isCommutedSHUFP(Ops);
1703}
1704
Evan Cheng2595a682006-03-24 02:58:06 +00001705/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1706/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1707bool X86::isMOVHLPSMask(SDNode *N) {
1708 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1709
Evan Cheng1a194a52006-03-28 06:50:32 +00001710 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001711 return false;
1712
Evan Cheng1a194a52006-03-28 06:50:32 +00001713 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001714 return isUndefOrEqual(N->getOperand(0), 6) &&
1715 isUndefOrEqual(N->getOperand(1), 7) &&
1716 isUndefOrEqual(N->getOperand(2), 2) &&
1717 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001718}
1719
Evan Chengc995b452006-04-06 23:23:56 +00001720/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1721/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1722bool X86::isMOVLPMask(SDNode *N) {
1723 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1724
1725 unsigned NumElems = N->getNumOperands();
1726 if (NumElems != 2 && NumElems != 4)
1727 return false;
1728
Evan Chengac847262006-04-07 21:53:05 +00001729 for (unsigned i = 0; i < NumElems/2; ++i)
1730 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1731 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001732
Evan Chengac847262006-04-07 21:53:05 +00001733 for (unsigned i = NumElems/2; i < NumElems; ++i)
1734 if (!isUndefOrEqual(N->getOperand(i), i))
1735 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001736
1737 return true;
1738}
1739
1740/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001741/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1742/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001743bool X86::isMOVHPMask(SDNode *N) {
1744 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1745
1746 unsigned NumElems = N->getNumOperands();
1747 if (NumElems != 2 && NumElems != 4)
1748 return false;
1749
Evan Chengac847262006-04-07 21:53:05 +00001750 for (unsigned i = 0; i < NumElems/2; ++i)
1751 if (!isUndefOrEqual(N->getOperand(i), i))
1752 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001753
1754 for (unsigned i = 0; i < NumElems/2; ++i) {
1755 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001756 if (!isUndefOrEqual(Arg, i + NumElems))
1757 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001758 }
1759
1760 return true;
1761}
1762
Evan Cheng5df75882006-03-28 00:39:58 +00001763/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1764/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng60f0b892006-04-20 08:58:49 +00001765bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1766 unsigned NumElems = N.size();
Evan Cheng5df75882006-03-28 00:39:58 +00001767 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1768 return false;
1769
1770 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001771 SDOperand BitI = N[i];
1772 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001773 if (!isUndefOrEqual(BitI, j))
1774 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001775 if (V2IsSplat) {
1776 if (isUndefOrEqual(BitI1, NumElems))
1777 return false;
1778 } else {
1779 if (!isUndefOrEqual(BitI1, j + NumElems))
1780 return false;
1781 }
Evan Cheng5df75882006-03-28 00:39:58 +00001782 }
1783
1784 return true;
1785}
1786
Evan Cheng60f0b892006-04-20 08:58:49 +00001787bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1788 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1789 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1790 return ::isUNPCKLMask(Ops, V2IsSplat);
1791}
1792
Evan Cheng2bc32802006-03-28 02:43:26 +00001793/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1794/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng60f0b892006-04-20 08:58:49 +00001795bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1796 unsigned NumElems = N.size();
Evan Cheng2bc32802006-03-28 02:43:26 +00001797 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1798 return false;
1799
1800 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001801 SDOperand BitI = N[i];
1802 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001803 if (!isUndefOrEqual(BitI, j + NumElems/2))
1804 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001805 if (V2IsSplat) {
1806 if (isUndefOrEqual(BitI1, NumElems))
1807 return false;
1808 } else {
1809 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1810 return false;
1811 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001812 }
1813
1814 return true;
1815}
1816
Evan Cheng60f0b892006-04-20 08:58:49 +00001817bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1818 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1819 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1820 return ::isUNPCKHMask(Ops, V2IsSplat);
1821}
1822
Evan Chengf3b52c82006-04-05 07:20:06 +00001823/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1824/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1825/// <0, 0, 1, 1>
1826bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1827 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1828
1829 unsigned NumElems = N->getNumOperands();
1830 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1831 return false;
1832
1833 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1834 SDOperand BitI = N->getOperand(i);
1835 SDOperand BitI1 = N->getOperand(i+1);
1836
Evan Chengac847262006-04-07 21:53:05 +00001837 if (!isUndefOrEqual(BitI, j))
1838 return false;
1839 if (!isUndefOrEqual(BitI1, j))
1840 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001841 }
1842
1843 return true;
1844}
1845
Evan Chenge8b51802006-04-21 01:05:10 +00001846/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1847/// specifies a shuffle of elements that is suitable for input to MOVSS,
1848/// MOVSD, and MOVD, i.e. setting the lowest element.
1849static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001850 unsigned NumElems = N.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001851 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001852 return false;
1853
Evan Cheng60f0b892006-04-20 08:58:49 +00001854 if (!isUndefOrEqual(N[0], NumElems))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001855 return false;
1856
1857 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001858 SDOperand Arg = N[i];
Evan Cheng12ba3e22006-04-11 00:19:04 +00001859 if (!isUndefOrEqual(Arg, i))
1860 return false;
1861 }
1862
1863 return true;
1864}
Evan Chengf3b52c82006-04-05 07:20:06 +00001865
Evan Chenge8b51802006-04-21 01:05:10 +00001866bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001867 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1868 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001869 return ::isMOVLMask(Ops);
Evan Cheng60f0b892006-04-20 08:58:49 +00001870}
1871
Evan Chenge8b51802006-04-21 01:05:10 +00001872/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1873/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001874/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Chenge8b51802006-04-21 01:05:10 +00001875static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001876 unsigned NumElems = Ops.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001877 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001878 return false;
1879
1880 if (!isUndefOrEqual(Ops[0], 0))
1881 return false;
1882
1883 for (unsigned i = 1; i < NumElems; ++i) {
1884 SDOperand Arg = Ops[i];
1885 if (V2IsSplat) {
1886 if (!isUndefOrEqual(Arg, NumElems))
1887 return false;
1888 } else {
1889 if (!isUndefOrEqual(Arg, i+NumElems))
1890 return false;
1891 }
1892 }
1893
1894 return true;
1895}
1896
Evan Chenge8b51802006-04-21 01:05:10 +00001897static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001898 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1899 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001900 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001901}
1902
Evan Cheng5d247f82006-04-14 21:59:03 +00001903/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1904/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1905bool X86::isMOVSHDUPMask(SDNode *N) {
1906 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1907
1908 if (N->getNumOperands() != 4)
1909 return false;
1910
1911 // Expect 1, 1, 3, 3
1912 for (unsigned i = 0; i < 2; ++i) {
1913 SDOperand Arg = N->getOperand(i);
1914 if (Arg.getOpcode() == ISD::UNDEF) continue;
1915 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1916 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1917 if (Val != 1) return false;
1918 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001919
1920 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001921 for (unsigned i = 2; i < 4; ++i) {
1922 SDOperand Arg = N->getOperand(i);
1923 if (Arg.getOpcode() == ISD::UNDEF) continue;
1924 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1925 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1926 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001927 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001928 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001929
Evan Cheng6222cf22006-04-15 05:37:34 +00001930 // Don't use movshdup if it can be done with a shufps.
1931 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001932}
1933
1934/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1935/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1936bool X86::isMOVSLDUPMask(SDNode *N) {
1937 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1938
1939 if (N->getNumOperands() != 4)
1940 return false;
1941
1942 // Expect 0, 0, 2, 2
1943 for (unsigned i = 0; i < 2; ++i) {
1944 SDOperand Arg = N->getOperand(i);
1945 if (Arg.getOpcode() == ISD::UNDEF) continue;
1946 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1947 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1948 if (Val != 0) return false;
1949 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001950
1951 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001952 for (unsigned i = 2; i < 4; ++i) {
1953 SDOperand Arg = N->getOperand(i);
1954 if (Arg.getOpcode() == ISD::UNDEF) continue;
1955 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1956 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1957 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001958 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001959 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001960
Evan Cheng6222cf22006-04-15 05:37:34 +00001961 // Don't use movshdup if it can be done with a shufps.
1962 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001963}
1964
Evan Chengd097e672006-03-22 02:53:00 +00001965/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1966/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001967static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001968 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1969
Evan Chengd097e672006-03-22 02:53:00 +00001970 // This is a splat operation if each element of the permute is the same, and
1971 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001972 unsigned NumElems = N->getNumOperands();
1973 SDOperand ElementBase;
1974 unsigned i = 0;
1975 for (; i != NumElems; ++i) {
1976 SDOperand Elt = N->getOperand(i);
1977 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1978 ElementBase = Elt;
1979 break;
1980 }
1981 }
1982
1983 if (!ElementBase.Val)
1984 return false;
1985
1986 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001987 SDOperand Arg = N->getOperand(i);
1988 if (Arg.getOpcode() == ISD::UNDEF) continue;
1989 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001990 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001991 }
1992
1993 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001994 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00001995}
1996
Evan Cheng5022b342006-04-17 20:43:08 +00001997/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1998/// a splat of a single element and it's a 2 or 4 element mask.
1999bool X86::isSplatMask(SDNode *N) {
2000 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2001
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002002 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00002003 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2004 return false;
2005 return ::isSplatMask(N);
2006}
2007
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002008/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2009/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2010/// instructions.
2011unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002012 unsigned NumOperands = N->getNumOperands();
2013 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2014 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00002015 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002016 unsigned Val = 0;
2017 SDOperand Arg = N->getOperand(NumOperands-i-1);
2018 if (Arg.getOpcode() != ISD::UNDEF)
2019 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00002020 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002021 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00002022 if (i != NumOperands - 1)
2023 Mask <<= Shift;
2024 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002025
2026 return Mask;
2027}
2028
Evan Chengb7fedff2006-03-29 23:07:14 +00002029/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2030/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2031/// instructions.
2032unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2033 unsigned Mask = 0;
2034 // 8 nodes, but we only care about the last 4.
2035 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002036 unsigned Val = 0;
2037 SDOperand Arg = N->getOperand(i);
2038 if (Arg.getOpcode() != ISD::UNDEF)
2039 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002040 Mask |= (Val - 4);
2041 if (i != 4)
2042 Mask <<= 2;
2043 }
2044
2045 return Mask;
2046}
2047
2048/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2049/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2050/// instructions.
2051unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2052 unsigned Mask = 0;
2053 // 8 nodes, but we only care about the first 4.
2054 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002055 unsigned Val = 0;
2056 SDOperand Arg = N->getOperand(i);
2057 if (Arg.getOpcode() != ISD::UNDEF)
2058 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002059 Mask |= Val;
2060 if (i != 0)
2061 Mask <<= 2;
2062 }
2063
2064 return Mask;
2065}
2066
Evan Cheng59a63552006-04-05 01:47:37 +00002067/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2068/// specifies a 8 element shuffle that can be broken into a pair of
2069/// PSHUFHW and PSHUFLW.
2070static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2071 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2072
2073 if (N->getNumOperands() != 8)
2074 return false;
2075
2076 // Lower quadword shuffled.
2077 for (unsigned i = 0; i != 4; ++i) {
2078 SDOperand Arg = N->getOperand(i);
2079 if (Arg.getOpcode() == ISD::UNDEF) continue;
2080 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2081 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2082 if (Val > 4)
2083 return false;
2084 }
2085
2086 // Upper quadword shuffled.
2087 for (unsigned i = 4; i != 8; ++i) {
2088 SDOperand Arg = N->getOperand(i);
2089 if (Arg.getOpcode() == ISD::UNDEF) continue;
2090 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2091 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2092 if (Val < 4 || Val > 7)
2093 return false;
2094 }
2095
2096 return true;
2097}
2098
Evan Chengc995b452006-04-06 23:23:56 +00002099/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2100/// values in ther permute mask.
2101static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2102 SDOperand V1 = Op.getOperand(0);
2103 SDOperand V2 = Op.getOperand(1);
2104 SDOperand Mask = Op.getOperand(2);
2105 MVT::ValueType VT = Op.getValueType();
2106 MVT::ValueType MaskVT = Mask.getValueType();
2107 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2108 unsigned NumElems = Mask.getNumOperands();
2109 std::vector<SDOperand> MaskVec;
2110
2111 for (unsigned i = 0; i != NumElems; ++i) {
2112 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002113 if (Arg.getOpcode() == ISD::UNDEF) {
2114 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2115 continue;
2116 }
Evan Chengc995b452006-04-06 23:23:56 +00002117 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2118 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2119 if (Val < NumElems)
2120 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2121 else
2122 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2123 }
2124
2125 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2126 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2127}
2128
Evan Cheng7855e4d2006-04-19 20:35:22 +00002129/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2130/// match movhlps. The lower half elements should come from upper half of
2131/// V1 (and in order), and the upper half elements should come from the upper
2132/// half of V2 (and in order).
2133static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2134 unsigned NumElems = Mask->getNumOperands();
2135 if (NumElems != 4)
2136 return false;
2137 for (unsigned i = 0, e = 2; i != e; ++i)
2138 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2139 return false;
2140 for (unsigned i = 2; i != 4; ++i)
2141 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2142 return false;
2143 return true;
2144}
2145
Evan Chengc995b452006-04-06 23:23:56 +00002146/// isScalarLoadToVector - Returns true if the node is a scalar load that
2147/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002148static inline bool isScalarLoadToVector(SDNode *N) {
2149 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2150 N = N->getOperand(0).Val;
2151 return (N->getOpcode() == ISD::LOAD);
Evan Chengc995b452006-04-06 23:23:56 +00002152 }
2153 return false;
2154}
2155
Evan Cheng7855e4d2006-04-19 20:35:22 +00002156/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2157/// match movlp{s|d}. The lower half elements should come from lower half of
2158/// V1 (and in order), and the upper half elements should come from the upper
2159/// half of V2 (and in order). And since V1 will become the source of the
2160/// MOVLP, it must be either a vector load or a scalar load to vector.
2161static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2162 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2163 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002164
Evan Cheng7855e4d2006-04-19 20:35:22 +00002165 unsigned NumElems = Mask->getNumOperands();
2166 if (NumElems != 2 && NumElems != 4)
2167 return false;
2168 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2169 if (!isUndefOrEqual(Mask->getOperand(i), i))
2170 return false;
2171 for (unsigned i = NumElems/2; i != NumElems; ++i)
2172 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2173 return false;
2174 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002175}
2176
Evan Cheng60f0b892006-04-20 08:58:49 +00002177/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2178/// all the same.
2179static bool isSplatVector(SDNode *N) {
2180 if (N->getOpcode() != ISD::BUILD_VECTOR)
2181 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002182
Evan Cheng60f0b892006-04-20 08:58:49 +00002183 SDOperand SplatValue = N->getOperand(0);
2184 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2185 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002186 return false;
2187 return true;
2188}
2189
Evan Cheng60f0b892006-04-20 08:58:49 +00002190/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2191/// that point to V2 points to its first element.
2192static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2193 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2194
2195 bool Changed = false;
2196 std::vector<SDOperand> MaskVec;
2197 unsigned NumElems = Mask.getNumOperands();
2198 for (unsigned i = 0; i != NumElems; ++i) {
2199 SDOperand Arg = Mask.getOperand(i);
2200 if (Arg.getOpcode() != ISD::UNDEF) {
2201 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2202 if (Val > NumElems) {
2203 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2204 Changed = true;
2205 }
2206 }
2207 MaskVec.push_back(Arg);
2208 }
2209
2210 if (Changed)
2211 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2212 return Mask;
2213}
2214
Evan Chenge8b51802006-04-21 01:05:10 +00002215/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2216/// operation of specified width.
2217static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002218 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2219 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2220
2221 std::vector<SDOperand> MaskVec;
2222 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2223 for (unsigned i = 1; i != NumElems; ++i)
2224 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2225 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2226}
2227
Evan Cheng5022b342006-04-17 20:43:08 +00002228/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2229/// of specified width.
2230static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2231 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2232 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2233 std::vector<SDOperand> MaskVec;
2234 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2235 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2236 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2237 }
2238 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2239}
2240
Evan Cheng60f0b892006-04-20 08:58:49 +00002241/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2242/// of specified width.
2243static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2244 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2245 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2246 unsigned Half = NumElems/2;
2247 std::vector<SDOperand> MaskVec;
2248 for (unsigned i = 0; i != Half; ++i) {
2249 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2250 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2251 }
2252 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2253}
2254
Evan Chenge8b51802006-04-21 01:05:10 +00002255/// getZeroVector - Returns a vector of specified type with all zero elements.
2256///
2257static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2258 assert(MVT::isVector(VT) && "Expected a vector type");
2259 unsigned NumElems = getVectorNumElements(VT);
2260 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2261 bool isFP = MVT::isFloatingPoint(EVT);
2262 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2263 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2264 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2265}
2266
Evan Cheng5022b342006-04-17 20:43:08 +00002267/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2268///
2269static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2270 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002271 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002272 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002273 unsigned NumElems = Mask.getNumOperands();
2274 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002275 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002276 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002277 NumElems >>= 1;
2278 }
2279 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2280
2281 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002282 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002283 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002284 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002285 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2286}
2287
Evan Chenge8b51802006-04-21 01:05:10 +00002288/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2289/// constant +0.0.
2290static inline bool isZeroNode(SDOperand Elt) {
2291 return ((isa<ConstantSDNode>(Elt) &&
2292 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2293 (isa<ConstantFPSDNode>(Elt) &&
2294 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2295}
2296
Evan Cheng14215c32006-04-21 23:03:30 +00002297/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2298/// vector and zero or undef vector.
2299static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002300 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002301 bool isZero, SelectionDAG &DAG) {
2302 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002303 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2304 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2305 SDOperand Zero = DAG.getConstant(0, EVT);
2306 std::vector<SDOperand> MaskVec(NumElems, Zero);
2307 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2308 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng14215c32006-04-21 23:03:30 +00002309 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002310}
2311
Evan Chengb0461082006-04-24 18:01:45 +00002312/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2313///
2314static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2315 unsigned NumNonZero, unsigned NumZero,
2316 SelectionDAG &DAG) {
2317 if (NumNonZero > 8)
2318 return SDOperand();
2319
2320 SDOperand V(0, 0);
2321 bool First = true;
2322 for (unsigned i = 0; i < 16; ++i) {
2323 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2324 if (ThisIsNonZero && First) {
2325 if (NumZero)
2326 V = getZeroVector(MVT::v8i16, DAG);
2327 else
2328 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2329 First = false;
2330 }
2331
2332 if ((i & 1) != 0) {
2333 SDOperand ThisElt(0, 0), LastElt(0, 0);
2334 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2335 if (LastIsNonZero) {
2336 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2337 }
2338 if (ThisIsNonZero) {
2339 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2340 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2341 ThisElt, DAG.getConstant(8, MVT::i8));
2342 if (LastIsNonZero)
2343 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2344 } else
2345 ThisElt = LastElt;
2346
2347 if (ThisElt.Val)
2348 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2349 DAG.getConstant(i/2, MVT::i32));
2350 }
2351 }
2352
2353 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2354}
2355
2356/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2357///
2358static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2359 unsigned NumNonZero, unsigned NumZero,
2360 SelectionDAG &DAG) {
2361 if (NumNonZero > 4)
2362 return SDOperand();
2363
2364 SDOperand V(0, 0);
2365 bool First = true;
2366 for (unsigned i = 0; i < 8; ++i) {
2367 bool isNonZero = (NonZeros & (1 << i)) != 0;
2368 if (isNonZero) {
2369 if (First) {
2370 if (NumZero)
2371 V = getZeroVector(MVT::v8i16, DAG);
2372 else
2373 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2374 First = false;
2375 }
2376 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2377 DAG.getConstant(i, MVT::i32));
2378 }
2379 }
2380
2381 return V;
2382}
2383
Evan Chenga9467aa2006-04-25 20:13:52 +00002384SDOperand
2385X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2386 // All zero's are handled with pxor.
2387 if (ISD::isBuildVectorAllZeros(Op.Val))
2388 return Op;
2389
2390 // All one's are handled with pcmpeqd.
2391 if (ISD::isBuildVectorAllOnes(Op.Val))
2392 return Op;
2393
2394 MVT::ValueType VT = Op.getValueType();
2395 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2396 unsigned EVTBits = MVT::getSizeInBits(EVT);
2397
2398 unsigned NumElems = Op.getNumOperands();
2399 unsigned NumZero = 0;
2400 unsigned NumNonZero = 0;
2401 unsigned NonZeros = 0;
2402 std::set<SDOperand> Values;
2403 for (unsigned i = 0; i < NumElems; ++i) {
2404 SDOperand Elt = Op.getOperand(i);
2405 if (Elt.getOpcode() != ISD::UNDEF) {
2406 Values.insert(Elt);
2407 if (isZeroNode(Elt))
2408 NumZero++;
2409 else {
2410 NonZeros |= (1 << i);
2411 NumNonZero++;
2412 }
2413 }
2414 }
2415
2416 if (NumNonZero == 0)
2417 // Must be a mix of zero and undef. Return a zero vector.
2418 return getZeroVector(VT, DAG);
2419
2420 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2421 if (Values.size() == 1)
2422 return SDOperand();
2423
2424 // Special case for single non-zero element.
2425 if (NumNonZero == 1) {
2426 unsigned Idx = CountTrailingZeros_32(NonZeros);
2427 SDOperand Item = Op.getOperand(Idx);
2428 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2429 if (Idx == 0)
2430 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2431 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2432 NumZero > 0, DAG);
2433
2434 if (EVTBits == 32) {
2435 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2436 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2437 DAG);
2438 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2439 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2440 std::vector<SDOperand> MaskVec;
2441 for (unsigned i = 0; i < NumElems; i++)
2442 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2443 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2444 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2445 DAG.getNode(ISD::UNDEF, VT), Mask);
2446 }
2447 }
2448
2449 // Let legalizer expand 2-widde build_vector's.
2450 if (EVTBits == 64)
2451 return SDOperand();
2452
2453 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2454 if (EVTBits == 8) {
2455 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2456 if (V.Val) return V;
2457 }
2458
2459 if (EVTBits == 16) {
2460 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2461 if (V.Val) return V;
2462 }
2463
2464 // If element VT is == 32 bits, turn it into a number of shuffles.
2465 std::vector<SDOperand> V(NumElems);
2466 if (NumElems == 4 && NumZero > 0) {
2467 for (unsigned i = 0; i < 4; ++i) {
2468 bool isZero = !(NonZeros & (1 << i));
2469 if (isZero)
2470 V[i] = getZeroVector(VT, DAG);
2471 else
2472 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2473 }
2474
2475 for (unsigned i = 0; i < 2; ++i) {
2476 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2477 default: break;
2478 case 0:
2479 V[i] = V[i*2]; // Must be a zero vector.
2480 break;
2481 case 1:
2482 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2483 getMOVLMask(NumElems, DAG));
2484 break;
2485 case 2:
2486 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2487 getMOVLMask(NumElems, DAG));
2488 break;
2489 case 3:
2490 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2491 getUnpacklMask(NumElems, DAG));
2492 break;
2493 }
2494 }
2495
Evan Cheng9fee4422006-05-16 07:21:53 +00002496 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Chenga9467aa2006-04-25 20:13:52 +00002497 // clears the upper bits.
2498 // FIXME: we can do the same for v4f32 case when we know both parts of
2499 // the lower half come from scalar_to_vector (loadf32). We should do
2500 // that in post legalizer dag combiner with target specific hooks.
2501 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2502 return V[0];
2503 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2504 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2505 std::vector<SDOperand> MaskVec;
2506 bool Reverse = (NonZeros & 0x3) == 2;
2507 for (unsigned i = 0; i < 2; ++i)
2508 if (Reverse)
2509 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2510 else
2511 MaskVec.push_back(DAG.getConstant(i, EVT));
2512 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2513 for (unsigned i = 0; i < 2; ++i)
2514 if (Reverse)
2515 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2516 else
2517 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2518 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2519 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2520 }
2521
2522 if (Values.size() > 2) {
2523 // Expand into a number of unpckl*.
2524 // e.g. for v4f32
2525 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2526 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2527 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2528 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2529 for (unsigned i = 0; i < NumElems; ++i)
2530 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2531 NumElems >>= 1;
2532 while (NumElems != 0) {
2533 for (unsigned i = 0; i < NumElems; ++i)
2534 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2535 UnpckMask);
2536 NumElems >>= 1;
2537 }
2538 return V[0];
2539 }
2540
2541 return SDOperand();
2542}
2543
2544SDOperand
2545X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2546 SDOperand V1 = Op.getOperand(0);
2547 SDOperand V2 = Op.getOperand(1);
2548 SDOperand PermMask = Op.getOperand(2);
2549 MVT::ValueType VT = Op.getValueType();
2550 unsigned NumElems = PermMask.getNumOperands();
2551 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2552 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2553
2554 if (isSplatMask(PermMask.Val)) {
2555 if (NumElems <= 4) return Op;
2556 // Promote it to a v4i32 splat.
2557 return PromoteSplat(Op, DAG);
2558 }
2559
2560 if (X86::isMOVLMask(PermMask.Val))
2561 return (V1IsUndef) ? V2 : Op;
2562
2563 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2564 X86::isMOVSLDUPMask(PermMask.Val) ||
2565 X86::isMOVHLPSMask(PermMask.Val) ||
2566 X86::isMOVHPMask(PermMask.Val) ||
2567 X86::isMOVLPMask(PermMask.Val))
2568 return Op;
2569
2570 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2571 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2572 return CommuteVectorShuffle(Op, DAG);
2573
2574 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2575 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2576 if (V1IsSplat && !V2IsSplat) {
2577 Op = CommuteVectorShuffle(Op, DAG);
2578 V1 = Op.getOperand(0);
2579 V2 = Op.getOperand(1);
2580 PermMask = Op.getOperand(2);
2581 V2IsSplat = true;
2582 }
2583
2584 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2585 if (V2IsUndef) return V1;
2586 Op = CommuteVectorShuffle(Op, DAG);
2587 V1 = Op.getOperand(0);
2588 V2 = Op.getOperand(1);
2589 PermMask = Op.getOperand(2);
2590 if (V2IsSplat) {
2591 // V2 is a splat, so the mask may be malformed. That is, it may point
2592 // to any V2 element. The instruction selectior won't like this. Get
2593 // a corrected mask and commute to form a proper MOVS{S|D}.
2594 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2595 if (NewMask.Val != PermMask.Val)
2596 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2597 }
2598 return Op;
2599 }
2600
2601 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2602 X86::isUNPCKLMask(PermMask.Val) ||
2603 X86::isUNPCKHMask(PermMask.Val))
2604 return Op;
2605
2606 if (V2IsSplat) {
2607 // Normalize mask so all entries that point to V2 points to its first
2608 // element then try to match unpck{h|l} again. If match, return a
2609 // new vector_shuffle with the corrected mask.
2610 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2611 if (NewMask.Val != PermMask.Val) {
2612 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2613 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2614 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2615 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2616 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2617 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2618 }
2619 }
2620 }
2621
2622 // Normalize the node to match x86 shuffle ops if needed
2623 if (V2.getOpcode() != ISD::UNDEF)
2624 if (isCommutedSHUFP(PermMask.Val)) {
2625 Op = CommuteVectorShuffle(Op, DAG);
2626 V1 = Op.getOperand(0);
2627 V2 = Op.getOperand(1);
2628 PermMask = Op.getOperand(2);
2629 }
2630
2631 // If VT is integer, try PSHUF* first, then SHUFP*.
2632 if (MVT::isInteger(VT)) {
2633 if (X86::isPSHUFDMask(PermMask.Val) ||
2634 X86::isPSHUFHWMask(PermMask.Val) ||
2635 X86::isPSHUFLWMask(PermMask.Val)) {
2636 if (V2.getOpcode() != ISD::UNDEF)
2637 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2638 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2639 return Op;
2640 }
2641
2642 if (X86::isSHUFPMask(PermMask.Val))
2643 return Op;
2644
2645 // Handle v8i16 shuffle high / low shuffle node pair.
2646 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2647 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2648 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2649 std::vector<SDOperand> MaskVec;
2650 for (unsigned i = 0; i != 4; ++i)
2651 MaskVec.push_back(PermMask.getOperand(i));
2652 for (unsigned i = 4; i != 8; ++i)
2653 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2654 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2655 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2656 MaskVec.clear();
2657 for (unsigned i = 0; i != 4; ++i)
2658 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2659 for (unsigned i = 4; i != 8; ++i)
2660 MaskVec.push_back(PermMask.getOperand(i));
2661 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2662 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2663 }
2664 } else {
2665 // Floating point cases in the other order.
2666 if (X86::isSHUFPMask(PermMask.Val))
2667 return Op;
2668 if (X86::isPSHUFDMask(PermMask.Val) ||
2669 X86::isPSHUFHWMask(PermMask.Val) ||
2670 X86::isPSHUFLWMask(PermMask.Val)) {
2671 if (V2.getOpcode() != ISD::UNDEF)
2672 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2673 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2674 return Op;
2675 }
2676 }
2677
2678 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002679 MVT::ValueType MaskVT = PermMask.getValueType();
2680 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng3cd43622006-04-28 07:03:38 +00002681 std::vector<std::pair<int, int> > Locs;
2682 Locs.reserve(NumElems);
2683 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2684 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2685 unsigned NumHi = 0;
2686 unsigned NumLo = 0;
2687 // If no more than two elements come from either vector. This can be
2688 // implemented with two shuffles. First shuffle gather the elements.
2689 // The second shuffle, which takes the first shuffle as both of its
2690 // vector operands, put the elements into the right order.
2691 for (unsigned i = 0; i != NumElems; ++i) {
2692 SDOperand Elt = PermMask.getOperand(i);
2693 if (Elt.getOpcode() == ISD::UNDEF) {
2694 Locs[i] = std::make_pair(-1, -1);
2695 } else {
2696 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2697 if (Val < NumElems) {
2698 Locs[i] = std::make_pair(0, NumLo);
2699 Mask1[NumLo] = Elt;
2700 NumLo++;
2701 } else {
2702 Locs[i] = std::make_pair(1, NumHi);
2703 if (2+NumHi < NumElems)
2704 Mask1[2+NumHi] = Elt;
2705 NumHi++;
2706 }
2707 }
2708 }
2709 if (NumLo <= 2 && NumHi <= 2) {
2710 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2711 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2712 for (unsigned i = 0; i != NumElems; ++i) {
2713 if (Locs[i].first == -1)
2714 continue;
2715 else {
2716 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2717 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2718 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2719 }
2720 }
2721
2722 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2723 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2724 }
2725
2726 // Break it into (shuffle shuffle_hi, shuffle_lo).
2727 Locs.clear();
Evan Chenga9467aa2006-04-25 20:13:52 +00002728 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2729 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2730 std::vector<SDOperand> *MaskPtr = &LoMask;
2731 unsigned MaskIdx = 0;
2732 unsigned LoIdx = 0;
2733 unsigned HiIdx = NumElems/2;
2734 for (unsigned i = 0; i != NumElems; ++i) {
2735 if (i == NumElems/2) {
2736 MaskPtr = &HiMask;
2737 MaskIdx = 1;
2738 LoIdx = 0;
2739 HiIdx = NumElems/2;
2740 }
2741 SDOperand Elt = PermMask.getOperand(i);
2742 if (Elt.getOpcode() == ISD::UNDEF) {
2743 Locs[i] = std::make_pair(-1, -1);
2744 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2745 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2746 (*MaskPtr)[LoIdx] = Elt;
2747 LoIdx++;
2748 } else {
2749 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2750 (*MaskPtr)[HiIdx] = Elt;
2751 HiIdx++;
2752 }
2753 }
2754
Chris Lattner3d826992006-05-16 06:45:34 +00002755 SDOperand LoShuffle =
2756 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2757 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2758 SDOperand HiShuffle =
2759 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2760 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Chenga9467aa2006-04-25 20:13:52 +00002761 std::vector<SDOperand> MaskOps;
2762 for (unsigned i = 0; i != NumElems; ++i) {
2763 if (Locs[i].first == -1) {
2764 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2765 } else {
2766 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2767 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2768 }
2769 }
2770 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2771 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2772 }
2773
2774 return SDOperand();
2775}
2776
2777SDOperand
2778X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2779 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2780 return SDOperand();
2781
2782 MVT::ValueType VT = Op.getValueType();
2783 // TODO: handle v16i8.
2784 if (MVT::getSizeInBits(VT) == 16) {
2785 // Transform it so it match pextrw which produces a 32-bit result.
2786 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2787 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2788 Op.getOperand(0), Op.getOperand(1));
2789 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2790 DAG.getValueType(VT));
2791 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2792 } else if (MVT::getSizeInBits(VT) == 32) {
2793 SDOperand Vec = Op.getOperand(0);
2794 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2795 if (Idx == 0)
2796 return Op;
2797
2798 // SHUFPS the element to the lowest double word, then movss.
2799 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2800 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2801 MVT::getVectorBaseType(MaskVT));
2802 std::vector<SDOperand> IdxVec;
2803 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2804 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2805 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2806 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2807 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2808 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2809 Vec, Vec, Mask);
2810 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2811 DAG.getConstant(0, MVT::i32));
2812 } else if (MVT::getSizeInBits(VT) == 64) {
2813 SDOperand Vec = Op.getOperand(0);
2814 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2815 if (Idx == 0)
2816 return Op;
2817
2818 // UNPCKHPD the element to the lowest double word, then movsd.
2819 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2820 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2821 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2822 std::vector<SDOperand> IdxVec;
2823 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2824 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2825 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2826 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2827 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2828 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2829 DAG.getConstant(0, MVT::i32));
2830 }
2831
2832 return SDOperand();
2833}
2834
2835SDOperand
2836X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002837 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002838 // as its second argument.
2839 MVT::ValueType VT = Op.getValueType();
2840 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2841 SDOperand N0 = Op.getOperand(0);
2842 SDOperand N1 = Op.getOperand(1);
2843 SDOperand N2 = Op.getOperand(2);
2844 if (MVT::getSizeInBits(BaseVT) == 16) {
2845 if (N1.getValueType() != MVT::i32)
2846 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2847 if (N2.getValueType() != MVT::i32)
2848 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2849 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2850 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2851 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2852 if (Idx == 0) {
2853 // Use a movss.
2854 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2855 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2856 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2857 std::vector<SDOperand> MaskVec;
2858 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2859 for (unsigned i = 1; i <= 3; ++i)
2860 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2861 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2862 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2863 } else {
2864 // Use two pinsrw instructions to insert a 32 bit value.
2865 Idx <<= 1;
2866 if (MVT::isFloatingPoint(N1.getValueType())) {
2867 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002868 // Just load directly from f32mem to GR32.
Evan Chenga9467aa2006-04-25 20:13:52 +00002869 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2870 N1.getOperand(2));
2871 } else {
2872 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2873 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2874 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2875 DAG.getConstant(0, MVT::i32));
2876 }
2877 }
2878 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2879 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2880 DAG.getConstant(Idx, MVT::i32));
2881 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2882 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2883 DAG.getConstant(Idx+1, MVT::i32));
2884 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2885 }
2886 }
2887
2888 return SDOperand();
2889}
2890
2891SDOperand
2892X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2893 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2894 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2895}
2896
2897// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2898// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2899// one of the above mentioned nodes. It has to be wrapped because otherwise
2900// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2901// be used to form addressing mode. These wrapped nodes will be selected
2902// into MOV32ri.
2903SDOperand
2904X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2905 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2906 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2907 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2908 CP->getAlignment()));
2909 if (Subtarget->isTargetDarwin()) {
2910 // With PIC, the address is actually $g + Offset.
2911 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2912 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2913 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2914 }
2915
2916 return Result;
2917}
2918
2919SDOperand
2920X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2921 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2922 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002923 DAG.getTargetGlobalAddress(GV,
2924 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002925 if (Subtarget->isTargetDarwin()) {
2926 // With PIC, the address is actually $g + Offset.
2927 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2928 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002929 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2930 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002931
2932 // For Darwin, external and weak symbols are indirect, so we want to load
2933 // the value at address GV, not the value of GV itself. This means that
2934 // the GlobalAddress must be in the base or index register of the address,
2935 // not the GV offset field.
2936 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2937 DarwinGVRequiresExtraLoad(GV))
2938 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2939 Result, DAG.getSrcValue(NULL));
2940 }
2941
2942 return Result;
2943}
2944
2945SDOperand
2946X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2947 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2948 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002949 DAG.getTargetExternalSymbol(Sym,
2950 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002951 if (Subtarget->isTargetDarwin()) {
2952 // With PIC, the address is actually $g + Offset.
2953 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2954 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002955 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2956 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002957 }
2958
2959 return Result;
2960}
2961
2962SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00002963 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2964 "Not an i64 shift!");
2965 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2966 SDOperand ShOpLo = Op.getOperand(0);
2967 SDOperand ShOpHi = Op.getOperand(1);
2968 SDOperand ShAmt = Op.getOperand(2);
2969 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00002970 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00002971 : DAG.getConstant(0, MVT::i32);
2972
2973 SDOperand Tmp2, Tmp3;
2974 if (Op.getOpcode() == ISD::SHL_PARTS) {
2975 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2976 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2977 } else {
2978 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00002979 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00002980 }
2981
2982 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2983 ShAmt, DAG.getConstant(32, MVT::i8));
2984
2985 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00002986 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00002987
2988 std::vector<MVT::ValueType> Tys;
2989 Tys.push_back(MVT::i32);
2990 Tys.push_back(MVT::Flag);
2991 std::vector<SDOperand> Ops;
2992 if (Op.getOpcode() == ISD::SHL_PARTS) {
2993 Ops.push_back(Tmp2);
2994 Ops.push_back(Tmp3);
2995 Ops.push_back(CC);
2996 Ops.push_back(InFlag);
2997 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2998 InFlag = Hi.getValue(1);
2999
3000 Ops.clear();
3001 Ops.push_back(Tmp3);
3002 Ops.push_back(Tmp1);
3003 Ops.push_back(CC);
3004 Ops.push_back(InFlag);
3005 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3006 } else {
3007 Ops.push_back(Tmp2);
3008 Ops.push_back(Tmp3);
3009 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00003010 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00003011 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3012 InFlag = Lo.getValue(1);
3013
3014 Ops.clear();
3015 Ops.push_back(Tmp3);
3016 Ops.push_back(Tmp1);
3017 Ops.push_back(CC);
3018 Ops.push_back(InFlag);
3019 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3020 }
3021
3022 Tys.clear();
3023 Tys.push_back(MVT::i32);
3024 Tys.push_back(MVT::i32);
3025 Ops.clear();
3026 Ops.push_back(Lo);
3027 Ops.push_back(Hi);
3028 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Chenga9467aa2006-04-25 20:13:52 +00003029}
Evan Cheng6305e502006-01-12 22:54:21 +00003030
Evan Chenga9467aa2006-04-25 20:13:52 +00003031SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3032 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3033 Op.getOperand(0).getValueType() >= MVT::i16 &&
3034 "Unknown SINT_TO_FP to lower!");
3035
3036 SDOperand Result;
3037 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3038 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3039 MachineFunction &MF = DAG.getMachineFunction();
3040 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3041 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3042 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
3043 DAG.getEntryNode(), Op.getOperand(0),
3044 StackSlot, DAG.getSrcValue(NULL));
3045
3046 // Build the FILD
3047 std::vector<MVT::ValueType> Tys;
3048 Tys.push_back(MVT::f64);
3049 Tys.push_back(MVT::Other);
3050 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3051 std::vector<SDOperand> Ops;
3052 Ops.push_back(Chain);
3053 Ops.push_back(StackSlot);
3054 Ops.push_back(DAG.getValueType(SrcVT));
3055 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3056 Tys, Ops);
3057
3058 if (X86ScalarSSE) {
3059 Chain = Result.getValue(1);
3060 SDOperand InFlag = Result.getValue(2);
3061
3062 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3063 // shouldn't be necessary except that RFP cannot be live across
3064 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003065 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003066 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003067 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00003068 std::vector<MVT::ValueType> Tys;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003069 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003070 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003071 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003072 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003073 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003074 Ops.push_back(DAG.getValueType(Op.getValueType()));
3075 Ops.push_back(InFlag);
3076 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3077 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3078 DAG.getSrcValue(NULL));
Chris Lattner76ac0682005-11-15 00:40:23 +00003079 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003080
Evan Chenga9467aa2006-04-25 20:13:52 +00003081 return Result;
3082}
3083
3084SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3085 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3086 "Unknown FP_TO_SINT to lower!");
3087 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3088 // stack slot.
3089 MachineFunction &MF = DAG.getMachineFunction();
3090 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3091 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3092 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3093
3094 unsigned Opc;
3095 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003096 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3097 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3098 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3099 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003100 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003101
Evan Chenga9467aa2006-04-25 20:13:52 +00003102 SDOperand Chain = DAG.getEntryNode();
3103 SDOperand Value = Op.getOperand(0);
3104 if (X86ScalarSSE) {
3105 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3106 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3107 DAG.getSrcValue(0));
3108 std::vector<MVT::ValueType> Tys;
3109 Tys.push_back(MVT::f64);
3110 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003111 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003112 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00003113 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003114 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3115 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3116 Chain = Value.getValue(1);
3117 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3118 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3119 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003120
Evan Chenga9467aa2006-04-25 20:13:52 +00003121 // Build the FP_TO_INT*_IN_MEM
3122 std::vector<SDOperand> Ops;
3123 Ops.push_back(Chain);
3124 Ops.push_back(Value);
3125 Ops.push_back(StackSlot);
3126 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Cheng172fce72006-01-06 00:43:03 +00003127
Evan Chenga9467aa2006-04-25 20:13:52 +00003128 // Load the result.
3129 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3130 DAG.getSrcValue(NULL));
3131}
3132
3133SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3134 MVT::ValueType VT = Op.getValueType();
3135 const Type *OpNTy = MVT::getTypeForValueType(VT);
3136 std::vector<Constant*> CV;
3137 if (VT == MVT::f64) {
3138 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3139 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3140 } else {
3141 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3142 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3143 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3144 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3145 }
3146 Constant *CS = ConstantStruct::get(CV);
3147 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3148 SDOperand Mask
3149 = DAG.getNode(X86ISD::LOAD_PACK,
3150 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3151 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3152}
3153
3154SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3155 MVT::ValueType VT = Op.getValueType();
3156 const Type *OpNTy = MVT::getTypeForValueType(VT);
3157 std::vector<Constant*> CV;
3158 if (VT == MVT::f64) {
3159 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3160 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3161 } else {
3162 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3163 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3164 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3165 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3166 }
3167 Constant *CS = ConstantStruct::get(CV);
3168 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3169 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3170 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3171 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3172}
3173
3174SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3175 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3176 SDOperand Cond;
3177 SDOperand CC = Op.getOperand(2);
3178 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3179 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3180 bool Flip;
3181 unsigned X86CC;
3182 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3183 if (Flip)
3184 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3185 Op.getOperand(1), Op.getOperand(0));
3186 else
Evan Cheng45df7f82006-01-30 23:41:35 +00003187 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3188 Op.getOperand(0), Op.getOperand(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003189 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3190 DAG.getConstant(X86CC, MVT::i8), Cond);
3191 } else {
3192 assert(isFP && "Illegal integer SetCC!");
3193
3194 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3195 Op.getOperand(0), Op.getOperand(1));
3196 std::vector<MVT::ValueType> Tys;
3197 std::vector<SDOperand> Ops;
3198 switch (SetCCOpcode) {
Evan Cheng172fce72006-01-06 00:43:03 +00003199 default: assert(false && "Illegal floating point SetCC!");
3200 case ISD::SETOEQ: { // !PF & ZF
3201 Tys.push_back(MVT::i8);
3202 Tys.push_back(MVT::Flag);
3203 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3204 Ops.push_back(Cond);
3205 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3206 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3207 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3208 Tmp1.getValue(1));
3209 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3210 }
Evan Cheng172fce72006-01-06 00:43:03 +00003211 case ISD::SETUNE: { // PF | !ZF
3212 Tys.push_back(MVT::i8);
3213 Tys.push_back(MVT::Flag);
3214 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3215 Ops.push_back(Cond);
3216 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3217 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3218 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3219 Tmp1.getValue(1));
3220 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3221 }
Evan Cheng172fce72006-01-06 00:43:03 +00003222 }
Evan Chengc1583db2005-12-21 20:21:51 +00003223 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003224}
Evan Cheng45df7f82006-01-30 23:41:35 +00003225
Evan Chenga9467aa2006-04-25 20:13:52 +00003226SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3227 MVT::ValueType VT = Op.getValueType();
3228 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3229 bool addTest = false;
3230 SDOperand Op0 = Op.getOperand(0);
3231 SDOperand Cond, CC;
3232 if (Op0.getOpcode() == ISD::SETCC)
3233 Op0 = LowerOperation(Op0, DAG);
Evan Cheng944d1e92006-01-26 02:13:10 +00003234
Evan Chenga9467aa2006-04-25 20:13:52 +00003235 if (Op0.getOpcode() == X86ISD::SETCC) {
3236 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3237 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3238 // have another use it will be eliminated.
3239 // If the X86ISD::SETCC has more than one use, then it's probably better
3240 // to use a test instead of duplicating the X86ISD::CMP (for register
3241 // pressure reason).
3242 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3243 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3244 CmpOpc == X86ISD::UCOMI) {
3245 if (!Op0.hasOneUse()) {
3246 std::vector<MVT::ValueType> Tys;
3247 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3248 Tys.push_back(Op0.Val->getValueType(i));
3249 std::vector<SDOperand> Ops;
3250 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3251 Ops.push_back(Op0.getOperand(i));
3252 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3253 }
3254
3255 CC = Op0.getOperand(0);
3256 Cond = Op0.getOperand(1);
3257 // Make a copy as flag result cannot be used by more than one.
3258 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3259 Cond.getOperand(0), Cond.getOperand(1));
3260 addTest =
3261 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00003262 } else
3263 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003264 } else
3265 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00003266
Evan Chenga9467aa2006-04-25 20:13:52 +00003267 if (addTest) {
3268 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3269 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00003270 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003271
Evan Chenga9467aa2006-04-25 20:13:52 +00003272 std::vector<MVT::ValueType> Tys;
3273 Tys.push_back(Op.getValueType());
3274 Tys.push_back(MVT::Flag);
3275 std::vector<SDOperand> Ops;
3276 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3277 // condition is true.
3278 Ops.push_back(Op.getOperand(2));
3279 Ops.push_back(Op.getOperand(1));
3280 Ops.push_back(CC);
3281 Ops.push_back(Cond);
3282 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3283}
Evan Cheng944d1e92006-01-26 02:13:10 +00003284
Evan Chenga9467aa2006-04-25 20:13:52 +00003285SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3286 bool addTest = false;
3287 SDOperand Cond = Op.getOperand(1);
3288 SDOperand Dest = Op.getOperand(2);
3289 SDOperand CC;
3290 if (Cond.getOpcode() == ISD::SETCC)
3291 Cond = LowerOperation(Cond, DAG);
3292
3293 if (Cond.getOpcode() == X86ISD::SETCC) {
3294 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3295 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3296 // have another use it will be eliminated.
3297 // If the X86ISD::SETCC has more than one use, then it's probably better
3298 // to use a test instead of duplicating the X86ISD::CMP (for register
3299 // pressure reason).
3300 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3301 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3302 CmpOpc == X86ISD::UCOMI) {
3303 if (!Cond.hasOneUse()) {
3304 std::vector<MVT::ValueType> Tys;
3305 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3306 Tys.push_back(Cond.Val->getValueType(i));
3307 std::vector<SDOperand> Ops;
3308 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3309 Ops.push_back(Cond.getOperand(i));
3310 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3311 }
3312
3313 CC = Cond.getOperand(0);
3314 Cond = Cond.getOperand(1);
3315 // Make a copy as flag result cannot be used by more than one.
3316 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3317 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00003318 } else
3319 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003320 } else
3321 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00003322
Evan Chenga9467aa2006-04-25 20:13:52 +00003323 if (addTest) {
3324 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3325 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng6fc31042005-12-19 23:12:38 +00003326 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003327 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3328 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3329}
Evan Chengae986f12006-01-11 22:15:48 +00003330
Evan Chenga9467aa2006-04-25 20:13:52 +00003331SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3332 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3333 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3334 DAG.getTargetJumpTable(JT->getIndex(),
3335 getPointerTy()));
3336 if (Subtarget->isTargetDarwin()) {
3337 // With PIC, the address is actually $g + Offset.
3338 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3339 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003340 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3341 Result);
Evan Chengae986f12006-01-11 22:15:48 +00003342 }
Evan Cheng99470012006-02-25 09:55:19 +00003343
Evan Chenga9467aa2006-04-25 20:13:52 +00003344 return Result;
3345}
Evan Cheng5588de92006-02-18 00:15:05 +00003346
Evan Chenga9467aa2006-04-25 20:13:52 +00003347SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3348 SDOperand Copy;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003349
Evan Chenga9467aa2006-04-25 20:13:52 +00003350 switch(Op.getNumOperands()) {
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003351 default:
3352 assert(0 && "Do not know how to return this many arguments!");
3353 abort();
Chris Lattnerc070c622006-04-17 20:32:50 +00003354 case 1: // ret void.
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003355 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chenga9467aa2006-04-25 20:13:52 +00003356 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003357 case 2: {
3358 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerc070c622006-04-17 20:32:50 +00003359
3360 if (MVT::isVector(ArgVT)) {
3361 // Integer or FP vector result -> XMM0.
3362 if (DAG.getMachineFunction().liveout_empty())
3363 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3364 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3365 SDOperand());
3366 } else if (MVT::isInteger(ArgVT)) {
3367 // Integer result -> EAX
3368 if (DAG.getMachineFunction().liveout_empty())
3369 DAG.getMachineFunction().addLiveOut(X86::EAX);
3370
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003371 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3372 SDOperand());
Chris Lattnerc070c622006-04-17 20:32:50 +00003373 } else if (!X86ScalarSSE) {
3374 // FP return with fp-stack value.
3375 if (DAG.getMachineFunction().liveout_empty())
3376 DAG.getMachineFunction().addLiveOut(X86::ST0);
3377
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003378 std::vector<MVT::ValueType> Tys;
3379 Tys.push_back(MVT::Other);
3380 Tys.push_back(MVT::Flag);
3381 std::vector<SDOperand> Ops;
3382 Ops.push_back(Op.getOperand(0));
3383 Ops.push_back(Op.getOperand(1));
3384 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3385 } else {
Chris Lattnerc070c622006-04-17 20:32:50 +00003386 // FP return with ScalarSSE (return on fp-stack).
3387 if (DAG.getMachineFunction().liveout_empty())
3388 DAG.getMachineFunction().addLiveOut(X86::ST0);
3389
Evan Chenge1ce4d72006-02-01 00:20:21 +00003390 SDOperand MemLoc;
3391 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00003392 SDOperand Value = Op.getOperand(1);
3393
Evan Chenga24617f2006-02-01 01:19:32 +00003394 if (Value.getOpcode() == ISD::LOAD &&
3395 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00003396 Chain = Value.getOperand(0);
3397 MemLoc = Value.getOperand(1);
3398 } else {
3399 // Spill the value to memory and reload it into top of stack.
3400 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3401 MachineFunction &MF = DAG.getMachineFunction();
3402 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3403 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3404 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3405 Value, MemLoc, DAG.getSrcValue(0));
3406 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003407 std::vector<MVT::ValueType> Tys;
3408 Tys.push_back(MVT::f64);
3409 Tys.push_back(MVT::Other);
3410 std::vector<SDOperand> Ops;
3411 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00003412 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003413 Ops.push_back(DAG.getValueType(ArgVT));
3414 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3415 Tys.clear();
3416 Tys.push_back(MVT::Other);
3417 Tys.push_back(MVT::Flag);
3418 Ops.clear();
3419 Ops.push_back(Copy.getValue(1));
3420 Ops.push_back(Copy);
3421 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3422 }
3423 break;
3424 }
3425 case 3:
Chris Lattnerc070c622006-04-17 20:32:50 +00003426 if (DAG.getMachineFunction().liveout_empty()) {
3427 DAG.getMachineFunction().addLiveOut(X86::EAX);
3428 DAG.getMachineFunction().addLiveOut(X86::EDX);
3429 }
3430
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003431 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3432 SDOperand());
3433 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3434 break;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003435 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003436 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3437 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3438 Copy.getValue(1));
3439}
3440
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003441SDOperand
3442X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng17e734f2006-05-23 21:06:34 +00003443 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3444 if (CC == CallingConv::Fast && EnableFastCC)
3445 return LowerFastCCArguments(Op, DAG);
3446 else
3447 return LowerCCCArguments(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003448}
3449
Evan Chenga9467aa2006-04-25 20:13:52 +00003450SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3451 SDOperand InFlag(0, 0);
3452 SDOperand Chain = Op.getOperand(0);
3453 unsigned Align =
3454 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3455 if (Align == 0) Align = 1;
3456
3457 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3458 // If not DWORD aligned, call memset if size is less than the threshold.
3459 // It knows how to align to the right boundary first.
3460 if ((Align & 3) != 0 ||
3461 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3462 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003463 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003464 std::vector<std::pair<SDOperand, const Type*> > Args;
3465 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3466 // Extend the ubyte argument to be an int value for the call.
3467 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3468 Args.push_back(std::make_pair(Val, IntPtrTy));
3469 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3470 std::pair<SDOperand,SDOperand> CallResult =
3471 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3472 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3473 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003474 }
Evan Chengd097e672006-03-22 02:53:00 +00003475
Evan Chenga9467aa2006-04-25 20:13:52 +00003476 MVT::ValueType AVT;
3477 SDOperand Count;
3478 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3479 unsigned BytesLeft = 0;
3480 bool TwoRepStos = false;
3481 if (ValC) {
3482 unsigned ValReg;
3483 unsigned Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003484
Evan Chenga9467aa2006-04-25 20:13:52 +00003485 // If the value is a constant, then we can potentially use larger sets.
3486 switch (Align & 3) {
3487 case 2: // WORD aligned
3488 AVT = MVT::i16;
3489 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3490 BytesLeft = I->getValue() % 2;
3491 Val = (Val << 8) | Val;
3492 ValReg = X86::AX;
3493 break;
3494 case 0: // DWORD aligned
3495 AVT = MVT::i32;
3496 if (I) {
3497 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3498 BytesLeft = I->getValue() % 4;
Evan Chenga3caaee2006-04-19 22:48:17 +00003499 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003500 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3501 DAG.getConstant(2, MVT::i8));
3502 TwoRepStos = true;
Evan Chenga3caaee2006-04-19 22:48:17 +00003503 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003504 Val = (Val << 8) | Val;
3505 Val = (Val << 16) | Val;
3506 ValReg = X86::EAX;
3507 break;
3508 default: // Byte aligned
3509 AVT = MVT::i8;
3510 Count = Op.getOperand(3);
3511 ValReg = X86::AL;
3512 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003513 }
3514
Evan Chenga9467aa2006-04-25 20:13:52 +00003515 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3516 InFlag);
3517 InFlag = Chain.getValue(1);
3518 } else {
3519 AVT = MVT::i8;
3520 Count = Op.getOperand(3);
3521 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3522 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003523 }
Evan Chengb0461082006-04-24 18:01:45 +00003524
Evan Chenga9467aa2006-04-25 20:13:52 +00003525 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3526 InFlag = Chain.getValue(1);
3527 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3528 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003529
Evan Chenga9467aa2006-04-25 20:13:52 +00003530 std::vector<MVT::ValueType> Tys;
3531 Tys.push_back(MVT::Other);
3532 Tys.push_back(MVT::Flag);
3533 std::vector<SDOperand> Ops;
3534 Ops.push_back(Chain);
3535 Ops.push_back(DAG.getValueType(AVT));
3536 Ops.push_back(InFlag);
3537 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengb0461082006-04-24 18:01:45 +00003538
Evan Chenga9467aa2006-04-25 20:13:52 +00003539 if (TwoRepStos) {
3540 InFlag = Chain.getValue(1);
3541 Count = Op.getOperand(3);
3542 MVT::ValueType CVT = Count.getValueType();
3543 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3544 DAG.getConstant(3, CVT));
3545 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3546 InFlag = Chain.getValue(1);
3547 Tys.clear();
3548 Tys.push_back(MVT::Other);
3549 Tys.push_back(MVT::Flag);
3550 Ops.clear();
3551 Ops.push_back(Chain);
3552 Ops.push_back(DAG.getValueType(MVT::i8));
3553 Ops.push_back(InFlag);
3554 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3555 } else if (BytesLeft) {
3556 // Issue stores for the last 1 - 3 bytes.
3557 SDOperand Value;
3558 unsigned Val = ValC->getValue() & 255;
3559 unsigned Offset = I->getValue() - BytesLeft;
3560 SDOperand DstAddr = Op.getOperand(1);
3561 MVT::ValueType AddrVT = DstAddr.getValueType();
3562 if (BytesLeft >= 2) {
3563 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3564 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3565 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3566 DAG.getConstant(Offset, AddrVT)),
3567 DAG.getSrcValue(NULL));
3568 BytesLeft -= 2;
3569 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003570 }
3571
Evan Chenga9467aa2006-04-25 20:13:52 +00003572 if (BytesLeft == 1) {
3573 Value = DAG.getConstant(Val, MVT::i8);
3574 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3575 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3576 DAG.getConstant(Offset, AddrVT)),
3577 DAG.getSrcValue(NULL));
Evan Cheng14215c32006-04-21 23:03:30 +00003578 }
Evan Cheng082c8782006-03-24 07:29:27 +00003579 }
Evan Chengebf10062006-04-03 20:53:28 +00003580
Evan Chenga9467aa2006-04-25 20:13:52 +00003581 return Chain;
3582}
Evan Chengebf10062006-04-03 20:53:28 +00003583
Evan Chenga9467aa2006-04-25 20:13:52 +00003584SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3585 SDOperand Chain = Op.getOperand(0);
3586 unsigned Align =
3587 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3588 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003589
Evan Chenga9467aa2006-04-25 20:13:52 +00003590 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3591 // If not DWORD aligned, call memcpy if size is less than the threshold.
3592 // It knows how to align to the right boundary first.
3593 if ((Align & 3) != 0 ||
3594 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3595 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003596 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003597 std::vector<std::pair<SDOperand, const Type*> > Args;
3598 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3599 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3600 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3601 std::pair<SDOperand,SDOperand> CallResult =
3602 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3603 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3604 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003605 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003606
3607 MVT::ValueType AVT;
3608 SDOperand Count;
3609 unsigned BytesLeft = 0;
3610 bool TwoRepMovs = false;
3611 switch (Align & 3) {
3612 case 2: // WORD aligned
3613 AVT = MVT::i16;
3614 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3615 BytesLeft = I->getValue() % 2;
3616 break;
3617 case 0: // DWORD aligned
3618 AVT = MVT::i32;
3619 if (I) {
3620 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3621 BytesLeft = I->getValue() % 4;
Evan Cheng54212062006-04-17 22:45:49 +00003622 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003623 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3624 DAG.getConstant(2, MVT::i8));
3625 TwoRepMovs = true;
Evan Cheng6e5e2052006-04-17 22:04:06 +00003626 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003627 break;
3628 default: // Byte aligned
3629 AVT = MVT::i8;
3630 Count = Op.getOperand(3);
3631 break;
3632 }
3633
3634 SDOperand InFlag(0, 0);
3635 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3636 InFlag = Chain.getValue(1);
3637 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3638 InFlag = Chain.getValue(1);
3639 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3640 InFlag = Chain.getValue(1);
3641
3642 std::vector<MVT::ValueType> Tys;
3643 Tys.push_back(MVT::Other);
3644 Tys.push_back(MVT::Flag);
3645 std::vector<SDOperand> Ops;
3646 Ops.push_back(Chain);
3647 Ops.push_back(DAG.getValueType(AVT));
3648 Ops.push_back(InFlag);
3649 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3650
3651 if (TwoRepMovs) {
3652 InFlag = Chain.getValue(1);
3653 Count = Op.getOperand(3);
3654 MVT::ValueType CVT = Count.getValueType();
3655 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3656 DAG.getConstant(3, CVT));
3657 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3658 InFlag = Chain.getValue(1);
3659 Tys.clear();
3660 Tys.push_back(MVT::Other);
3661 Tys.push_back(MVT::Flag);
3662 Ops.clear();
3663 Ops.push_back(Chain);
3664 Ops.push_back(DAG.getValueType(MVT::i8));
3665 Ops.push_back(InFlag);
3666 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3667 } else if (BytesLeft) {
3668 // Issue loads and stores for the last 1 - 3 bytes.
3669 unsigned Offset = I->getValue() - BytesLeft;
3670 SDOperand DstAddr = Op.getOperand(1);
3671 MVT::ValueType DstVT = DstAddr.getValueType();
3672 SDOperand SrcAddr = Op.getOperand(2);
3673 MVT::ValueType SrcVT = SrcAddr.getValueType();
3674 SDOperand Value;
3675 if (BytesLeft >= 2) {
3676 Value = DAG.getLoad(MVT::i16, Chain,
3677 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3678 DAG.getConstant(Offset, SrcVT)),
3679 DAG.getSrcValue(NULL));
3680 Chain = Value.getValue(1);
3681 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3682 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3683 DAG.getConstant(Offset, DstVT)),
3684 DAG.getSrcValue(NULL));
3685 BytesLeft -= 2;
3686 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003687 }
3688
Evan Chenga9467aa2006-04-25 20:13:52 +00003689 if (BytesLeft == 1) {
3690 Value = DAG.getLoad(MVT::i8, Chain,
3691 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3692 DAG.getConstant(Offset, SrcVT)),
3693 DAG.getSrcValue(NULL));
3694 Chain = Value.getValue(1);
3695 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3696 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3697 DAG.getConstant(Offset, DstVT)),
3698 DAG.getSrcValue(NULL));
3699 }
Evan Chengcbffa462006-03-31 19:22:53 +00003700 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003701
3702 return Chain;
3703}
3704
3705SDOperand
3706X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3707 std::vector<MVT::ValueType> Tys;
3708 Tys.push_back(MVT::Other);
3709 Tys.push_back(MVT::Flag);
3710 std::vector<SDOperand> Ops;
3711 Ops.push_back(Op.getOperand(0));
3712 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3713 Ops.clear();
3714 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3715 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3716 MVT::i32, Ops[0].getValue(2)));
3717 Ops.push_back(Ops[1].getValue(1));
3718 Tys[0] = Tys[1] = MVT::i32;
3719 Tys.push_back(MVT::Other);
3720 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3721}
3722
3723SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3724 // vastart just stores the address of the VarArgsFrameIndex slot into the
3725 // memory location argument.
3726 // FIXME: Replace MVT::i32 with PointerTy
3727 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3728 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3729 Op.getOperand(1), Op.getOperand(2));
3730}
3731
3732SDOperand
3733X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3734 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3735 switch (IntNo) {
3736 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003737 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003738 case Intrinsic::x86_sse_comieq_ss:
3739 case Intrinsic::x86_sse_comilt_ss:
3740 case Intrinsic::x86_sse_comile_ss:
3741 case Intrinsic::x86_sse_comigt_ss:
3742 case Intrinsic::x86_sse_comige_ss:
3743 case Intrinsic::x86_sse_comineq_ss:
3744 case Intrinsic::x86_sse_ucomieq_ss:
3745 case Intrinsic::x86_sse_ucomilt_ss:
3746 case Intrinsic::x86_sse_ucomile_ss:
3747 case Intrinsic::x86_sse_ucomigt_ss:
3748 case Intrinsic::x86_sse_ucomige_ss:
3749 case Intrinsic::x86_sse_ucomineq_ss:
3750 case Intrinsic::x86_sse2_comieq_sd:
3751 case Intrinsic::x86_sse2_comilt_sd:
3752 case Intrinsic::x86_sse2_comile_sd:
3753 case Intrinsic::x86_sse2_comigt_sd:
3754 case Intrinsic::x86_sse2_comige_sd:
3755 case Intrinsic::x86_sse2_comineq_sd:
3756 case Intrinsic::x86_sse2_ucomieq_sd:
3757 case Intrinsic::x86_sse2_ucomilt_sd:
3758 case Intrinsic::x86_sse2_ucomile_sd:
3759 case Intrinsic::x86_sse2_ucomigt_sd:
3760 case Intrinsic::x86_sse2_ucomige_sd:
3761 case Intrinsic::x86_sse2_ucomineq_sd: {
3762 unsigned Opc = 0;
3763 ISD::CondCode CC = ISD::SETCC_INVALID;
3764 switch (IntNo) {
3765 default: break;
3766 case Intrinsic::x86_sse_comieq_ss:
3767 case Intrinsic::x86_sse2_comieq_sd:
3768 Opc = X86ISD::COMI;
3769 CC = ISD::SETEQ;
3770 break;
Evan Cheng78038292006-04-05 23:38:46 +00003771 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003772 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003773 Opc = X86ISD::COMI;
3774 CC = ISD::SETLT;
3775 break;
3776 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003777 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003778 Opc = X86ISD::COMI;
3779 CC = ISD::SETLE;
3780 break;
3781 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003782 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003783 Opc = X86ISD::COMI;
3784 CC = ISD::SETGT;
3785 break;
3786 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003787 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003788 Opc = X86ISD::COMI;
3789 CC = ISD::SETGE;
3790 break;
3791 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003792 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003793 Opc = X86ISD::COMI;
3794 CC = ISD::SETNE;
3795 break;
3796 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003797 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003798 Opc = X86ISD::UCOMI;
3799 CC = ISD::SETEQ;
3800 break;
3801 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003802 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003803 Opc = X86ISD::UCOMI;
3804 CC = ISD::SETLT;
3805 break;
3806 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003807 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003808 Opc = X86ISD::UCOMI;
3809 CC = ISD::SETLE;
3810 break;
3811 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003812 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003813 Opc = X86ISD::UCOMI;
3814 CC = ISD::SETGT;
3815 break;
3816 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003817 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003818 Opc = X86ISD::UCOMI;
3819 CC = ISD::SETGE;
3820 break;
3821 case Intrinsic::x86_sse_ucomineq_ss:
3822 case Intrinsic::x86_sse2_ucomineq_sd:
3823 Opc = X86ISD::UCOMI;
3824 CC = ISD::SETNE;
3825 break;
Evan Cheng78038292006-04-05 23:38:46 +00003826 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003827 bool Flip;
3828 unsigned X86CC;
3829 translateX86CC(CC, true, X86CC, Flip);
3830 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3831 Op.getOperand(Flip?1:2));
3832 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3833 DAG.getConstant(X86CC, MVT::i8), Cond);
3834 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003835 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003836 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003837}
Evan Cheng6af02632005-12-20 06:22:03 +00003838
Evan Chenga9467aa2006-04-25 20:13:52 +00003839/// LowerOperation - Provide custom lowering hooks for some operations.
3840///
3841SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3842 switch (Op.getOpcode()) {
3843 default: assert(0 && "Should not custom lower this!");
3844 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3845 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3846 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3847 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3848 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3849 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3850 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3851 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3852 case ISD::SHL_PARTS:
3853 case ISD::SRA_PARTS:
3854 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3855 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3856 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3857 case ISD::FABS: return LowerFABS(Op, DAG);
3858 case ISD::FNEG: return LowerFNEG(Op, DAG);
3859 case ISD::SETCC: return LowerSETCC(Op, DAG);
3860 case ISD::SELECT: return LowerSELECT(Op, DAG);
3861 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3862 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3863 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003864 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003865 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3866 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3867 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3868 case ISD::VASTART: return LowerVASTART(Op, DAG);
3869 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3870 }
3871}
3872
Evan Cheng6af02632005-12-20 06:22:03 +00003873const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3874 switch (Opcode) {
3875 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003876 case X86ISD::SHLD: return "X86ISD::SHLD";
3877 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003878 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003879 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003880 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003881 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003882 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3883 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3884 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003885 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003886 case X86ISD::FST: return "X86ISD::FST";
3887 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003888 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003889 case X86ISD::CALL: return "X86ISD::CALL";
3890 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3891 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3892 case X86ISD::CMP: return "X86ISD::CMP";
3893 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003894 case X86ISD::COMI: return "X86ISD::COMI";
3895 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003896 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003897 case X86ISD::CMOV: return "X86ISD::CMOV";
3898 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003899 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003900 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3901 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003902 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003903 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00003904 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00003905 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00003906 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00003907 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00003908 }
3909}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003910
Nate Begeman8a77efe2006-02-16 21:11:51 +00003911void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3912 uint64_t Mask,
3913 uint64_t &KnownZero,
3914 uint64_t &KnownOne,
3915 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003916 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00003917 assert((Opc >= ISD::BUILTIN_OP_END ||
3918 Opc == ISD::INTRINSIC_WO_CHAIN ||
3919 Opc == ISD::INTRINSIC_W_CHAIN ||
3920 Opc == ISD::INTRINSIC_VOID) &&
3921 "Should use MaskedValueIsZero if you don't know whether Op"
3922 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003923
Evan Cheng6d196db2006-04-05 06:11:20 +00003924 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003925 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00003926 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00003927 case X86ISD::SETCC:
3928 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3929 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003930 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003931}
Chris Lattnerc642aa52006-01-31 19:43:35 +00003932
3933std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00003934getRegClassForInlineAsmConstraint(const std::string &Constraint,
3935 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00003936 if (Constraint.size() == 1) {
3937 // FIXME: not handling fp-stack yet!
3938 // FIXME: not handling MMX registers yet ('y' constraint).
3939 switch (Constraint[0]) { // GCC X86 Constraint Letters
3940 default: break; // Unknown constriant letter
3941 case 'r': // GENERAL_REGS
3942 case 'R': // LEGACY_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003943 if (VT == MVT::i32)
3944 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3945 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3946 else if (VT == MVT::i16)
3947 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3948 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3949 else if (VT == MVT::i8)
3950 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3951 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003952 case 'l': // INDEX_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003953 if (VT == MVT::i32)
3954 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3955 X86::ESI, X86::EDI, X86::EBP, 0);
3956 else if (VT == MVT::i16)
3957 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3958 X86::SI, X86::DI, X86::BP, 0);
3959 else if (VT == MVT::i8)
3960 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3961 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003962 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3963 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003964 if (VT == MVT::i32)
3965 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3966 else if (VT == MVT::i16)
3967 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3968 else if (VT == MVT::i8)
3969 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3970 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003971 case 'x': // SSE_REGS if SSE1 allowed
3972 if (Subtarget->hasSSE1())
3973 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3974 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3975 0);
3976 return std::vector<unsigned>();
3977 case 'Y': // SSE_REGS if SSE2 allowed
3978 if (Subtarget->hasSSE2())
3979 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3980 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3981 0);
3982 return std::vector<unsigned>();
3983 }
3984 }
3985
Chris Lattner7ad77df2006-02-22 00:56:39 +00003986 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00003987}
Evan Chengaf598d22006-03-13 23:18:16 +00003988
3989/// isLegalAddressImmediate - Return true if the integer value or
3990/// GlobalValue can be used as the offset of the target addressing mode.
3991bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3992 // X86 allows a sign-extended 32-bit immediate field.
3993 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3994}
3995
3996bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00003997 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00003998 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3999 if (RModel == Reloc::Static)
4000 return true;
4001 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00004002 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00004003 else
4004 return false;
4005 } else
4006 return true;
4007}
Evan Cheng68ad48b2006-03-22 18:59:22 +00004008
4009/// isShuffleMaskLegal - Targets can use this to indicate that they only
4010/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4011/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4012/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00004013bool
4014X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4015 // Only do shuffles on 128-bit vector types for now.
4016 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Chenga3caaee2006-04-19 22:48:17 +00004017 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng5022b342006-04-17 20:43:08 +00004018 isSplatMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00004019 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00004020 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00004021 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00004022 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00004023}
Evan Cheng60f0b892006-04-20 08:58:49 +00004024
4025bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4026 MVT::ValueType EVT,
4027 SelectionDAG &DAG) const {
4028 unsigned NumElts = BVOps.size();
4029 // Only do shuffles on 128-bit vector types for now.
4030 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4031 if (NumElts == 2) return true;
4032 if (NumElts == 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00004033 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng60f0b892006-04-20 08:58:49 +00004034 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4035 }
4036 return false;
4037}