blob: 82cf457dade910148354b739602e4c62370f5bee [file] [log] [blame]
Chris Lattnera58f5592006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattner76ac0682005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng88decde2006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000022#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000023#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000024#include "llvm/ADT/VectorExtras.h"
25#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000029#include "llvm/CodeGen/SelectionDAG.h"
30#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000032#include "llvm/Target/TargetOptions.h"
33using namespace llvm;
34
35// FIXME: temporary.
36#include "llvm/Support/CommandLine.h"
37static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
38 cl::desc("Enable fastcc on X86"));
39
40X86TargetLowering::X86TargetLowering(TargetMachine &TM)
41 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000042 Subtarget = &TM.getSubtarget<X86Subtarget>();
43 X86ScalarSSE = Subtarget->hasSSE2();
44
Chris Lattner76ac0682005-11-15 00:40:23 +000045 // Set up the TargetLowering object.
46
47 // X86 is weird, it always uses i8 for shift amounts and setcc results.
48 setShiftAmountType(MVT::i8);
49 setSetCCResultType(MVT::i8);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000051 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000052 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000053 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000054
Evan Chengbc047222006-03-22 19:22:18 +000055 if (!Subtarget->isTargetDarwin())
Evan Chengb09a56f2006-03-17 20:31:41 +000056 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
57 setUseUnderscoreSetJmpLongJmp(true);
58
Evan Cheng20931a72006-03-16 21:47:42 +000059 // Add legal addressing mode scale values.
60 addLegalAddressScale(8);
61 addLegalAddressScale(4);
62 addLegalAddressScale(2);
63 // Enter the ones which require both scale + index last. These are more
64 // expensive.
65 addLegalAddressScale(9);
66 addLegalAddressScale(5);
67 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000068
Chris Lattner76ac0682005-11-15 00:40:23 +000069 // Set up the register classes.
Evan Cheng9fee4422006-05-16 07:21:53 +000070 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
71 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
72 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +000073
74 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
75 // operation.
76 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
78 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000079
80 if (X86ScalarSSE)
81 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
83 else
84 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000085
86 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
89 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000090 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000091 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000092 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000093 else {
94 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
95 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
96 }
Chris Lattner76ac0682005-11-15 00:40:23 +000097
Evan Cheng5b97fcf2006-01-30 08:02:57 +000098 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
99 // isn't legal.
100 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
101 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
102
Evan Cheng08390f62006-01-30 22:13:22 +0000103 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
104 // this operation.
105 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
106 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
107
108 if (X86ScalarSSE) {
109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
110 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000112 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000113 }
114
115 // Handle FP_TO_UINT by promoting the destination to a larger signed
116 // conversion.
117 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
119 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
120
Evan Chengd13778e2006-02-18 07:26:17 +0000121 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000122 // Expand FP_TO_UINT into a select.
123 // FIXME: We would like to use a Custom expander here eventually to do
124 // the optimal thing for SSE vs. the default expansion in the legalizer.
125 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
126 else
Evan Chengd13778e2006-02-18 07:26:17 +0000127 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000128 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
129
Evan Cheng08390f62006-01-30 22:13:22 +0000130 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
131 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000132
Evan Cheng593bea72006-02-17 07:01:52 +0000133 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000134 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
135 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000136 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
140 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
141 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
142 setOperationAction(ISD::FREM , MVT::f64 , Expand);
143 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
144 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
146 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
147 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
149 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
150 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
151 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000152 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000153 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000154
Chris Lattner76ac0682005-11-15 00:40:23 +0000155 // These should be promoted to a larger select which is supported.
156 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
157 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000158
159 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000160 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
161 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000169 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000170 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000171 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000172 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000173 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000174 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000175 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000176 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000177 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
179 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000180 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000181 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
182 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000183
Chris Lattner9c415362005-11-29 06:16:21 +0000184 // We don't have line number support yet.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000187 // FIXME - use subtarget debug flags
Evan Chengbc047222006-03-22 19:22:18 +0000188 if (!Subtarget->isTargetDarwin())
Evan Cheng30d7b702006-03-07 02:02:57 +0000189 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000190
Nate Begemane74795c2006-01-25 18:21:52 +0000191 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
192 setOperationAction(ISD::VASTART , MVT::Other, Custom);
193
194 // Use the default implementation.
195 setOperationAction(ISD::VAARG , MVT::Other, Expand);
196 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
197 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000198 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
199 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
200 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000201
Chris Lattner9c7f5032006-03-05 05:08:37 +0000202 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
203 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
204
Chris Lattner76ac0682005-11-15 00:40:23 +0000205 if (X86ScalarSSE) {
206 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000209
Evan Cheng72d5c252006-01-31 22:28:30 +0000210 // Use ANDPD to simulate FABS.
211 setOperationAction(ISD::FABS , MVT::f64, Custom);
212 setOperationAction(ISD::FABS , MVT::f32, Custom);
213
214 // Use XORP to simulate FNEG.
215 setOperationAction(ISD::FNEG , MVT::f64, Custom);
216 setOperationAction(ISD::FNEG , MVT::f32, Custom);
217
Evan Chengd8fba3a2006-02-02 00:28:23 +0000218 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000219 setOperationAction(ISD::FSIN , MVT::f64, Expand);
220 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 setOperationAction(ISD::FREM , MVT::f64, Expand);
222 setOperationAction(ISD::FSIN , MVT::f32, Expand);
223 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000224 setOperationAction(ISD::FREM , MVT::f32, Expand);
225
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000226 // Expand FP immediates into loads from the stack, except for the special
227 // cases we handle.
228 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
229 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000230 addLegalFPImmediate(+0.0); // xorps / xorpd
231 } else {
232 // Set up the FP register classes.
233 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000234
235 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
236
Chris Lattner76ac0682005-11-15 00:40:23 +0000237 if (!UnsafeFPMath) {
238 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
239 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
240 }
241
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000242 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000243 addLegalFPImmediate(+0.0); // FLD0
244 addLegalFPImmediate(+1.0); // FLD1
245 addLegalFPImmediate(-0.0); // FLD0/FCHS
246 addLegalFPImmediate(-1.0); // FLD1/FCHS
247 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000248
Evan Cheng19264272006-03-01 01:11:20 +0000249 // First set operation action for all vector types to expand. Then we
250 // will selectively turn on ones that can be effectively codegen'd.
251 for (unsigned VT = (unsigned)MVT::Vector + 1;
252 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
253 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
254 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000257 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000258 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000259 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000260 }
261
Evan Chengbc047222006-03-22 19:22:18 +0000262 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
266
Evan Cheng19264272006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Evan Chengd5e905d2006-03-21 23:01:21 +0000268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000271 }
272
Evan Chengbc047222006-03-22 19:22:18 +0000273 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng92232302006-04-12 21:21:57 +0000276 setOperationAction(ISD::AND, MVT::v4f32, Legal);
277 setOperationAction(ISD::OR, MVT::v4f32, Legal);
278 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000279 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
280 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
281 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
282 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
283 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
284 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000285 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000286 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000287 }
288
Evan Chengbc047222006-03-22 19:22:18 +0000289 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000290 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
291 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
293 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
295
Evan Cheng617a6a82006-04-10 07:23:14 +0000296 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
297 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
298 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
299 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
300 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
301 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
302 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
303 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000304 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000305 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng92232302006-04-12 21:21:57 +0000306
Evan Cheng617a6a82006-04-10 07:23:14 +0000307 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000309 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000310 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
311 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
312 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000313
Evan Cheng92232302006-04-12 21:21:57 +0000314 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
315 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
316 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
317 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
318 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
319 }
320 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
321 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
322 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
323 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
324 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
325 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
326
327 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
328 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
329 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
330 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
331 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
332 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
333 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
334 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000335 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
336 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000337 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
338 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000339 }
Evan Cheng92232302006-04-12 21:21:57 +0000340
341 // Custom lower v2i64 and v2f64 selects.
342 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000343 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000344 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000345 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000346 }
347
Evan Cheng78038292006-04-05 23:38:46 +0000348 // We want to custom lower some of our intrinsics.
349 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
350
Chris Lattner76ac0682005-11-15 00:40:23 +0000351 computeRegisterProperties();
352
Evan Cheng6a374562006-02-14 08:25:08 +0000353 // FIXME: These should be based on subtarget info. Plus, the values should
354 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000355 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
356 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
357 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000358 allowUnalignedMemoryAccesses = true; // x86 supports it!
359}
360
Chris Lattner76ac0682005-11-15 00:40:23 +0000361//===----------------------------------------------------------------------===//
362// C Calling Convention implementation
363//===----------------------------------------------------------------------===//
364
Evan Cheng24eb3f42006-04-27 05:35:28 +0000365/// AddLiveIn - This helper function adds the specified physical register to the
366/// MachineFunction as a live in value. It also creates a corresponding virtual
367/// register for it.
368static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
369 TargetRegisterClass *RC) {
370 assert(RC->contains(PReg) && "Not the correct regclass!");
371 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
372 MF.addLiveIn(PReg, VReg);
373 return VReg;
374}
375
Evan Cheng89001ad2006-04-27 08:31:10 +0000376/// HowToPassCCCArgument - Returns how an formal argument of the specified type
377/// should be passed. If it is through stack, returns the size of the stack
378/// frame; if it is through XMM register, returns the number of XMM registers
379/// are needed.
380static void
381HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
382 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000383 switch (ObjectVT) {
384 default: assert(0 && "Unhandled argument type!");
385 case MVT::i1:
386 case MVT::i8: ObjSize = 1; break;
387 case MVT::i16: ObjSize = 2; break;
388 case MVT::i32: ObjSize = 4; break;
389 case MVT::i64: ObjSize = 8; break;
390 case MVT::f32: ObjSize = 4; break;
391 case MVT::f64: ObjSize = 8; break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000392 case MVT::v16i8:
393 case MVT::v8i16:
394 case MVT::v4i32:
395 case MVT::v2i64:
396 case MVT::v4f32:
397 case MVT::v2f64:
398 if (NumXMMRegs < 3)
399 ObjXMMRegs = 1;
400 else
401 ObjSize = 16;
402 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000403 }
Evan Cheng48940d12006-04-27 01:32:22 +0000404}
405
Evan Cheng17e734f2006-05-23 21:06:34 +0000406SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
407 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000408 MachineFunction &MF = DAG.getMachineFunction();
409 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000410 SDOperand Root = Op.getOperand(0);
411 std::vector<SDOperand> ArgValues;
Chris Lattner76ac0682005-11-15 00:40:23 +0000412
Evan Cheng48940d12006-04-27 01:32:22 +0000413 // Add DAG nodes to load the arguments... On entry to a function on the X86,
414 // the stack frame looks like this:
415 //
416 // [ESP] -- return address
417 // [ESP + 4] -- first argument (leftmost lexically)
418 // [ESP + 8] -- second argument, if first argument is four bytes in size
419 // ...
420 //
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000421 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng89001ad2006-04-27 08:31:10 +0000422 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
423 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000424 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000425 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
426 unsigned ArgIncrement = 4;
427 unsigned ObjSize = 0;
428 unsigned ObjXMMRegs = 0;
429 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
430 if (ObjSize >= 8)
431 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000432
Evan Cheng17e734f2006-05-23 21:06:34 +0000433 SDOperand ArgValue;
434 if (ObjXMMRegs) {
435 // Passed in a XMM register.
436 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
Evan Cheng89001ad2006-04-27 08:31:10 +0000437 X86::VR128RegisterClass);
Evan Cheng17e734f2006-05-23 21:06:34 +0000438 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
439 ArgValues.push_back(ArgValue);
440 NumXMMRegs += ObjXMMRegs;
441 } else {
442 // Create the frame index object for this incoming parameter...
443 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
444 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
445 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
446 DAG.getSrcValue(NULL));
447 ArgValues.push_back(ArgValue);
448 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000449 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000450 }
451
Evan Cheng17e734f2006-05-23 21:06:34 +0000452 ArgValues.push_back(Root);
453
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000454 // If the function takes variable number of arguments, make a frame index for
455 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000456 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
457 if (isVarArg)
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000458 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
459 ReturnAddrIndex = 0; // No return address slot generated yet.
460 BytesToPopOnReturn = 0; // Callee pops nothing.
461 BytesCallerReserves = ArgOffset;
Evan Cheng17e734f2006-05-23 21:06:34 +0000462
Chris Lattner8be5be82006-05-23 18:50:38 +0000463 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
464 // pointer.
Evan Cheng17e734f2006-05-23 21:06:34 +0000465 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner8be5be82006-05-23 18:50:38 +0000466 Subtarget->isTargetDarwin())
467 BytesToPopOnReturn = 4;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000468
Evan Cheng17e734f2006-05-23 21:06:34 +0000469 // Return the new list of results.
470 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
471 Op.Val->value_end());
472 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattner76ac0682005-11-15 00:40:23 +0000473}
474
Evan Cheng2a330942006-05-25 00:59:30 +0000475
476SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
477 SDOperand Chain = Op.getOperand(0);
478 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
479 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
480 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
481 SDOperand Callee = Op.getOperand(4);
482 MVT::ValueType RetVT= Op.Val->getValueType(0);
483 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000484
Evan Cheng88decde2006-04-28 21:29:37 +0000485 // Keep track of the number of XMM regs passed so far.
486 unsigned NumXMMRegs = 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000487 static const unsigned XMMArgRegs[] = {
488 X86::XMM0, X86::XMM1, X86::XMM2
489 };
Evan Cheng88decde2006-04-28 21:29:37 +0000490
Evan Cheng2a330942006-05-25 00:59:30 +0000491 // Count how many bytes are to be pushed on the stack.
492 unsigned NumBytes = 0;
493 for (unsigned i = 0; i != NumOps; ++i) {
494 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattner76ac0682005-11-15 00:40:23 +0000495
Evan Cheng2a330942006-05-25 00:59:30 +0000496 switch (Arg.getValueType()) {
497 default: assert(0 && "Unexpected ValueType for argument!");
498 case MVT::i8:
499 case MVT::i16:
500 case MVT::i32:
501 case MVT::f32:
502 NumBytes += 4;
503 break;
504 case MVT::i64:
505 case MVT::f64:
506 NumBytes += 8;
507 break;
508 case MVT::v16i8:
509 case MVT::v8i16:
510 case MVT::v4i32:
511 case MVT::v2i64:
512 case MVT::v4f32:
Evan Cheng0421aca2006-05-25 22:38:31 +0000513 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +0000514 if (NumXMMRegs < 3)
515 ++NumXMMRegs;
516 else
517 NumBytes += 16;
518 break;
519 }
Evan Cheng2a330942006-05-25 00:59:30 +0000520 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000521
Evan Cheng2a330942006-05-25 00:59:30 +0000522 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000523
Evan Cheng2a330942006-05-25 00:59:30 +0000524 // Arguments go on the stack in reverse order, as specified by the ABI.
525 unsigned ArgOffset = 0;
526 NumXMMRegs = 0;
527 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
528 std::vector<SDOperand> MemOpChains;
529 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
530 for (unsigned i = 0; i != NumOps; ++i) {
531 SDOperand Arg = Op.getOperand(5+2*i);
532
533 switch (Arg.getValueType()) {
534 default: assert(0 && "Unexpected ValueType for argument!");
535 case MVT::i8:
Evan Cheng5ee96892006-05-25 18:56:34 +0000536 case MVT::i16: {
Evan Cheng2a330942006-05-25 00:59:30 +0000537 // Promote the integer to 32 bits. If the input type is signed use a
538 // sign extend, otherwise use a zero extend.
539 unsigned ExtOp =
540 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
541 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
542 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng5ee96892006-05-25 18:56:34 +0000543 }
544 // Fallthrough
Evan Cheng2a330942006-05-25 00:59:30 +0000545
546 case MVT::i32:
547 case MVT::f32: {
548 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
549 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
550 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
551 Arg, PtrOff, DAG.getSrcValue(NULL)));
552 ArgOffset += 4;
553 break;
554 }
555 case MVT::i64:
556 case MVT::f64: {
557 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
558 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
559 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
560 Arg, PtrOff, DAG.getSrcValue(NULL)));
561 ArgOffset += 8;
562 break;
563 }
564 case MVT::v16i8:
565 case MVT::v8i16:
566 case MVT::v4i32:
567 case MVT::v2i64:
568 case MVT::v4f32:
Evan Cheng0421aca2006-05-25 22:38:31 +0000569 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +0000570 if (NumXMMRegs < 3) {
571 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
572 NumXMMRegs++;
573 } else {
Evan Cheng88decde2006-04-28 21:29:37 +0000574 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +0000575 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
576 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
577 Arg, PtrOff, DAG.getSrcValue(NULL)));
578 ArgOffset += 16;
Evan Cheng88decde2006-04-28 21:29:37 +0000579 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000580 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000581 }
582
Evan Cheng2a330942006-05-25 00:59:30 +0000583 if (!MemOpChains.empty())
584 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattner76ac0682005-11-15 00:40:23 +0000585
Evan Cheng88decde2006-04-28 21:29:37 +0000586 // Build a sequence of copy-to-reg nodes chained together with token chain
587 // and flag operands which copy the outgoing args into registers.
588 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +0000589 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
590 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
591 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +0000592 InFlag = Chain.getValue(1);
593 }
594
Evan Cheng2a330942006-05-25 00:59:30 +0000595 // If the callee is a GlobalAddress node (quite common, every direct call is)
596 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
597 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
598 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
599 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
600 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
601
Nate Begeman7e5496d2006-02-17 00:03:04 +0000602 std::vector<MVT::ValueType> NodeTys;
603 NodeTys.push_back(MVT::Other); // Returns a chain
604 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
605 std::vector<SDOperand> Ops;
606 Ops.push_back(Chain);
607 Ops.push_back(Callee);
Evan Cheng88decde2006-04-28 21:29:37 +0000608 if (InFlag.Val)
609 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000610
Evan Cheng2a330942006-05-25 00:59:30 +0000611 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
612 NodeTys, Ops);
Evan Cheng88decde2006-04-28 21:29:37 +0000613 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000614
Chris Lattner8be5be82006-05-23 18:50:38 +0000615 // Create the CALLSEQ_END node.
616 unsigned NumBytesForCalleeToPush = 0;
617
618 // If this is is a call to a struct-return function on Darwin/X86, the callee
619 // pops the hidden struct pointer, so we have to push it back.
620 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
621 NumBytesForCalleeToPush = 4;
622
Nate Begeman7e5496d2006-02-17 00:03:04 +0000623 NodeTys.clear();
624 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng2a330942006-05-25 00:59:30 +0000625 if (RetVT != MVT::Other)
626 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman7e5496d2006-02-17 00:03:04 +0000627 Ops.clear();
628 Ops.push_back(Chain);
629 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000630 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000631 Ops.push_back(InFlag);
632 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng2a330942006-05-25 00:59:30 +0000633 if (RetVT != MVT::Other)
634 InFlag = Chain.getValue(1);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000635
Evan Cheng2a330942006-05-25 00:59:30 +0000636 std::vector<SDOperand> ResultVals;
637 NodeTys.clear();
638 switch (RetVT) {
639 default: assert(0 && "Unknown value type to return!");
640 case MVT::Other: break;
641 case MVT::i8:
642 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
643 ResultVals.push_back(Chain.getValue(0));
644 NodeTys.push_back(MVT::i8);
645 break;
646 case MVT::i16:
647 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
648 ResultVals.push_back(Chain.getValue(0));
649 NodeTys.push_back(MVT::i16);
650 break;
651 case MVT::i32:
652 if (Op.Val->getValueType(1) == MVT::i32) {
653 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
654 ResultVals.push_back(Chain.getValue(0));
655 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
656 Chain.getValue(2)).getValue(1);
657 ResultVals.push_back(Chain.getValue(0));
658 NodeTys.push_back(MVT::i32);
659 } else {
660 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
661 ResultVals.push_back(Chain.getValue(0));
Evan Cheng45e190982006-01-05 00:27:02 +0000662 }
Evan Cheng2a330942006-05-25 00:59:30 +0000663 NodeTys.push_back(MVT::i32);
664 break;
665 case MVT::v16i8:
666 case MVT::v8i16:
667 case MVT::v4i32:
668 case MVT::v2i64:
669 case MVT::v4f32:
670 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +0000671 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
672 ResultVals.push_back(Chain.getValue(0));
673 NodeTys.push_back(RetVT);
674 break;
675 case MVT::f32:
676 case MVT::f64: {
677 std::vector<MVT::ValueType> Tys;
678 Tys.push_back(MVT::f64);
679 Tys.push_back(MVT::Other);
680 Tys.push_back(MVT::Flag);
681 std::vector<SDOperand> Ops;
682 Ops.push_back(Chain);
683 Ops.push_back(InFlag);
684 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
685 Chain = RetVal.getValue(1);
686 InFlag = RetVal.getValue(2);
687 if (X86ScalarSSE) {
688 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
689 // shouldn't be necessary except that RFP cannot be live across
690 // multiple blocks. When stackifier is fixed, they can be uncoupled.
691 MachineFunction &MF = DAG.getMachineFunction();
692 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
693 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
694 Tys.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +0000695 Tys.push_back(MVT::Other);
Evan Cheng2a330942006-05-25 00:59:30 +0000696 Ops.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +0000697 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +0000698 Ops.push_back(RetVal);
699 Ops.push_back(StackSlot);
700 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000701 Ops.push_back(InFlag);
Evan Cheng2a330942006-05-25 00:59:30 +0000702 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
703 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
704 DAG.getSrcValue(NULL));
Evan Cheng88decde2006-04-28 21:29:37 +0000705 Chain = RetVal.getValue(1);
Evan Cheng88decde2006-04-28 21:29:37 +0000706 }
Evan Cheng2a330942006-05-25 00:59:30 +0000707
708 if (RetVT == MVT::f32 && !X86ScalarSSE)
709 // FIXME: we would really like to remember that this FP_ROUND
710 // operation is okay to eliminate if we allow excess FP precision.
711 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
712 ResultVals.push_back(RetVal);
713 NodeTys.push_back(RetVT);
714 break;
715 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000716 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000717
Evan Cheng2a330942006-05-25 00:59:30 +0000718 // If the function returns void, just return the chain.
719 if (ResultVals.empty())
720 return Chain;
721
722 // Otherwise, merge everything together with a MERGE_VALUES node.
723 NodeTys.push_back(MVT::Other);
724 ResultVals.push_back(Chain);
725 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
726 return Res.getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000727}
728
Chris Lattner76ac0682005-11-15 00:40:23 +0000729//===----------------------------------------------------------------------===//
730// Fast Calling Convention implementation
731//===----------------------------------------------------------------------===//
732//
733// The X86 'fast' calling convention passes up to two integer arguments in
734// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
735// and requires that the callee pop its arguments off the stack (allowing proper
736// tail calls), and has the same return value conventions as C calling convs.
737//
738// This calling convention always arranges for the callee pop value to be 8n+4
739// bytes, which is needed for tail recursion elimination and stack alignment
740// reasons.
741//
742// Note that this can be enhanced in the future to pass fp vals in registers
743// (when we have a global fp allocator) and do other tricks.
744//
745
Chris Lattner388fc4d2006-03-17 17:27:47 +0000746// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
747// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
748// EDX". Anything more is illegal.
749//
750// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000751// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000752// a physreg with a virtreg, this increases the size of the physreg's live
753// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000754// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000755// allocator to wedge itself.
756//
757// This code triggers this problem more often if we pass args in registers,
758// so disable it until this is fixed.
759//
760// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
761// about code being dead.
762//
763static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000764
Chris Lattner76ac0682005-11-15 00:40:23 +0000765
Evan Cheng89001ad2006-04-27 08:31:10 +0000766/// HowToPassFastCCArgument - Returns how an formal argument of the specified
767/// type should be passed. If it is through stack, returns the size of the stack
768/// frame; if it is through integer or XMM register, returns the number of
769/// integer or XMM registers are needed.
Evan Cheng48940d12006-04-27 01:32:22 +0000770static void
Evan Cheng89001ad2006-04-27 08:31:10 +0000771HowToPassFastCCArgument(MVT::ValueType ObjectVT,
772 unsigned NumIntRegs, unsigned NumXMMRegs,
773 unsigned &ObjSize, unsigned &ObjIntRegs,
774 unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000775 ObjSize = 0;
776 NumIntRegs = 0;
777
778 switch (ObjectVT) {
779 default: assert(0 && "Unhandled argument type!");
780 case MVT::i1:
781 case MVT::i8:
782 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000783 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000784 else
785 ObjSize = 1;
786 break;
787 case MVT::i16:
788 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000789 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000790 else
791 ObjSize = 2;
792 break;
793 case MVT::i32:
794 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000795 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000796 else
797 ObjSize = 4;
798 break;
799 case MVT::i64:
800 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000801 ObjIntRegs = 2;
Evan Cheng48940d12006-04-27 01:32:22 +0000802 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000803 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000804 ObjSize = 4;
805 } else
806 ObjSize = 8;
807 case MVT::f32:
808 ObjSize = 4;
809 break;
810 case MVT::f64:
811 ObjSize = 8;
812 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000813 case MVT::v16i8:
814 case MVT::v8i16:
815 case MVT::v4i32:
816 case MVT::v2i64:
817 case MVT::v4f32:
818 case MVT::v2f64:
819 if (NumXMMRegs < 3)
820 ObjXMMRegs = 1;
821 else
822 ObjSize = 16;
823 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000824 }
825}
826
Evan Cheng17e734f2006-05-23 21:06:34 +0000827SDOperand
828X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
829 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattner76ac0682005-11-15 00:40:23 +0000830 MachineFunction &MF = DAG.getMachineFunction();
831 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000832 SDOperand Root = Op.getOperand(0);
833 std::vector<SDOperand> ArgValues;
Chris Lattner76ac0682005-11-15 00:40:23 +0000834
Evan Cheng48940d12006-04-27 01:32:22 +0000835 // Add DAG nodes to load the arguments... On entry to a function the stack
836 // frame looks like this:
837 //
838 // [ESP] -- return address
839 // [ESP + 4] -- first nonreg argument (leftmost lexically)
840 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
841 // ...
Chris Lattner76ac0682005-11-15 00:40:23 +0000842 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
843
844 // Keep track of the number of integer regs passed so far. This can be either
845 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
846 // used).
847 unsigned NumIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000848 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng2a330942006-05-25 00:59:30 +0000849
850 static const unsigned XMMArgRegs[] = {
851 X86::XMM0, X86::XMM1, X86::XMM2
852 };
Chris Lattner43798852006-03-17 05:10:20 +0000853
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000854 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000855 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
856 unsigned ArgIncrement = 4;
857 unsigned ObjSize = 0;
858 unsigned ObjIntRegs = 0;
859 unsigned ObjXMMRegs = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000860
Evan Cheng17e734f2006-05-23 21:06:34 +0000861 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
862 ObjSize, ObjIntRegs, ObjXMMRegs);
863 if (ObjSize >= 8)
864 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000865
Evan Cheng17e734f2006-05-23 21:06:34 +0000866 unsigned Reg;
867 SDOperand ArgValue;
868 if (ObjIntRegs || ObjXMMRegs) {
869 switch (ObjectVT) {
870 default: assert(0 && "Unhandled argument type!");
871 case MVT::i1:
872 case MVT::i8:
873 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
874 X86::GR8RegisterClass);
875 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
876 break;
877 case MVT::i16:
878 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
879 X86::GR16RegisterClass);
880 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
881 break;
882 case MVT::i32:
883 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
884 X86::GR32RegisterClass);
885 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
886 break;
887 case MVT::i64:
888 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
889 X86::GR32RegisterClass);
890 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
891 if (ObjIntRegs == 2) {
892 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
893 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
894 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000895 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000896 break;
897 case MVT::v16i8:
898 case MVT::v8i16:
899 case MVT::v4i32:
900 case MVT::v2i64:
901 case MVT::v4f32:
902 case MVT::v2f64:
903 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
904 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
905 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000906 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000907 NumIntRegs += ObjIntRegs;
908 NumXMMRegs += ObjXMMRegs;
Chris Lattner76ac0682005-11-15 00:40:23 +0000909 }
Evan Cheng17e734f2006-05-23 21:06:34 +0000910
911 if (ObjSize) {
912 // Create the SelectionDAG nodes corresponding to a load from this
913 // parameter.
914 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
915 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
916 if (ObjectVT == MVT::i64 && ObjIntRegs) {
917 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
918 DAG.getSrcValue(NULL));
919 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
920 } else
921 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
922 DAG.getSrcValue(NULL));
923 ArgOffset += ArgIncrement; // Move on to the next argument.
924 }
925
926 ArgValues.push_back(ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000927 }
928
Evan Cheng17e734f2006-05-23 21:06:34 +0000929 ArgValues.push_back(Root);
930
Chris Lattner76ac0682005-11-15 00:40:23 +0000931 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
932 // arguments and the arguments after the retaddr has been pushed are aligned.
933 if ((ArgOffset & 7) == 0)
934 ArgOffset += 4;
935
936 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
937 ReturnAddrIndex = 0; // No return address slot generated yet.
938 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
939 BytesCallerReserves = 0;
940
941 // Finally, inform the code generator which regs we return values in.
Evan Cheng17e734f2006-05-23 21:06:34 +0000942 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000943 default: assert(0 && "Unknown type!");
944 case MVT::isVoid: break;
945 case MVT::i1:
946 case MVT::i8:
947 case MVT::i16:
948 case MVT::i32:
949 MF.addLiveOut(X86::EAX);
950 break;
951 case MVT::i64:
952 MF.addLiveOut(X86::EAX);
953 MF.addLiveOut(X86::EDX);
954 break;
955 case MVT::f32:
956 case MVT::f64:
957 MF.addLiveOut(X86::ST0);
958 break;
Evan Cheng5ee96892006-05-25 18:56:34 +0000959 case MVT::v16i8:
960 case MVT::v8i16:
961 case MVT::v4i32:
962 case MVT::v2i64:
963 case MVT::v4f32:
964 case MVT::v2f64:
Evan Cheng88decde2006-04-28 21:29:37 +0000965 MF.addLiveOut(X86::XMM0);
966 break;
967 }
Evan Cheng88decde2006-04-28 21:29:37 +0000968
Evan Cheng17e734f2006-05-23 21:06:34 +0000969 // Return the new list of results.
970 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
971 Op.Val->value_end());
972 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattner76ac0682005-11-15 00:40:23 +0000973}
974
Evan Cheng2a330942006-05-25 00:59:30 +0000975 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
976 SDOperand Chain = Op.getOperand(0);
977 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
978 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
979 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
980 SDOperand Callee = Op.getOperand(4);
981 MVT::ValueType RetVT= Op.Val->getValueType(0);
982 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
983
Chris Lattner76ac0682005-11-15 00:40:23 +0000984 // Count how many bytes are to be pushed on the stack.
985 unsigned NumBytes = 0;
986
987 // Keep track of the number of integer regs passed so far. This can be either
988 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
989 // used).
990 unsigned NumIntRegs = 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000991 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattner76ac0682005-11-15 00:40:23 +0000992
Evan Cheng2a330942006-05-25 00:59:30 +0000993 static const unsigned GPRArgRegs[][2] = {
994 { X86::AL, X86::DL },
995 { X86::AX, X86::DX },
996 { X86::EAX, X86::EDX }
997 };
998 static const unsigned XMMArgRegs[] = {
999 X86::XMM0, X86::XMM1, X86::XMM2
1000 };
1001
1002 for (unsigned i = 0; i != NumOps; ++i) {
1003 SDOperand Arg = Op.getOperand(5+2*i);
1004
1005 switch (Arg.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001006 default: assert(0 && "Unknown value type!");
Chris Lattner76ac0682005-11-15 00:40:23 +00001007 case MVT::i8:
1008 case MVT::i16:
1009 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001010 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001011 ++NumIntRegs;
1012 break;
1013 }
Evan Cheng0421aca2006-05-25 22:38:31 +00001014 // Fall through
Chris Lattner76ac0682005-11-15 00:40:23 +00001015 case MVT::f32:
1016 NumBytes += 4;
1017 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001018 case MVT::f64:
1019 NumBytes += 8;
1020 break;
Evan Cheng2a330942006-05-25 00:59:30 +00001021 case MVT::v16i8:
1022 case MVT::v8i16:
1023 case MVT::v4i32:
1024 case MVT::v2i64:
1025 case MVT::v4f32:
Evan Cheng5ee96892006-05-25 18:56:34 +00001026 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +00001027 if (NumXMMRegs < 3)
1028 NumXMMRegs++;
1029 else
1030 NumBytes += 16;
1031 break;
Chris Lattner76ac0682005-11-15 00:40:23 +00001032 }
Evan Cheng2a330942006-05-25 00:59:30 +00001033 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001034
1035 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1036 // arguments and the arguments after the retaddr has been pushed are aligned.
1037 if ((NumBytes & 7) == 0)
1038 NumBytes += 4;
1039
Chris Lattner62c34842006-02-13 09:00:43 +00001040 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +00001041
1042 // Arguments go on the stack in reverse order, as specified by the ABI.
1043 unsigned ArgOffset = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +00001044 NumIntRegs = 0;
Evan Cheng2a330942006-05-25 00:59:30 +00001045 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1046 std::vector<SDOperand> MemOpChains;
1047 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1048 for (unsigned i = 0; i != NumOps; ++i) {
1049 SDOperand Arg = Op.getOperand(5+2*i);
1050
1051 switch (Arg.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001052 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattner76ac0682005-11-15 00:40:23 +00001053 case MVT::i8:
1054 case MVT::i16:
1055 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001056 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng2a330942006-05-25 00:59:30 +00001057 RegsToPass.push_back(
1058 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1059 Arg));
Chris Lattner76ac0682005-11-15 00:40:23 +00001060 ++NumIntRegs;
1061 break;
1062 }
1063 // Fall through
1064 case MVT::f32: {
1065 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001066 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1067 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1068 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner76ac0682005-11-15 00:40:23 +00001069 ArgOffset += 4;
1070 break;
1071 }
Evan Cheng2a330942006-05-25 00:59:30 +00001072 case MVT::f64: {
Chris Lattner76ac0682005-11-15 00:40:23 +00001073 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001074 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1075 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1076 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner76ac0682005-11-15 00:40:23 +00001077 ArgOffset += 8;
1078 break;
1079 }
Evan Cheng2a330942006-05-25 00:59:30 +00001080 case MVT::v16i8:
1081 case MVT::v8i16:
1082 case MVT::v4i32:
1083 case MVT::v2i64:
1084 case MVT::v4f32:
Evan Cheng5ee96892006-05-25 18:56:34 +00001085 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +00001086 if (NumXMMRegs < 3) {
1087 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1088 NumXMMRegs++;
1089 } else {
1090 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1091 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1092 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1093 Arg, PtrOff, DAG.getSrcValue(NULL)));
1094 ArgOffset += 16;
1095 }
1096 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001097 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001098
Evan Cheng2a330942006-05-25 00:59:30 +00001099 if (!MemOpChains.empty())
1100 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattner76ac0682005-11-15 00:40:23 +00001101
Nate Begeman7e5496d2006-02-17 00:03:04 +00001102 // Build a sequence of copy-to-reg nodes chained together with token chain
1103 // and flag operands which copy the outgoing args into registers.
1104 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001105 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1106 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1107 InFlag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001108 InFlag = Chain.getValue(1);
1109 }
1110
Evan Cheng2a330942006-05-25 00:59:30 +00001111 // If the callee is a GlobalAddress node (quite common, every direct call is)
1112 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1113 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1114 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1115 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1116 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1117
Nate Begeman7e5496d2006-02-17 00:03:04 +00001118 std::vector<MVT::ValueType> NodeTys;
1119 NodeTys.push_back(MVT::Other); // Returns a chain
1120 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1121 std::vector<SDOperand> Ops;
1122 Ops.push_back(Chain);
1123 Ops.push_back(Callee);
1124 if (InFlag.Val)
1125 Ops.push_back(InFlag);
1126
1127 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001128 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1129 NodeTys, Ops);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001130 InFlag = Chain.getValue(1);
1131
1132 NodeTys.clear();
1133 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng2a330942006-05-25 00:59:30 +00001134 if (RetVT != MVT::Other)
1135 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman7e5496d2006-02-17 00:03:04 +00001136 Ops.clear();
1137 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001138 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1139 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001140 Ops.push_back(InFlag);
1141 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng2a330942006-05-25 00:59:30 +00001142 if (RetVT != MVT::Other)
1143 InFlag = Chain.getValue(1);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001144
Evan Cheng2a330942006-05-25 00:59:30 +00001145 std::vector<SDOperand> ResultVals;
1146 NodeTys.clear();
1147 switch (RetVT) {
1148 default: assert(0 && "Unknown value type to return!");
1149 case MVT::Other: break;
1150 case MVT::i8:
1151 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1152 ResultVals.push_back(Chain.getValue(0));
1153 NodeTys.push_back(MVT::i8);
1154 break;
1155 case MVT::i16:
1156 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1157 ResultVals.push_back(Chain.getValue(0));
1158 NodeTys.push_back(MVT::i16);
1159 break;
1160 case MVT::i32:
1161 if (Op.Val->getValueType(1) == MVT::i32) {
1162 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1163 ResultVals.push_back(Chain.getValue(0));
1164 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1165 Chain.getValue(2)).getValue(1);
1166 ResultVals.push_back(Chain.getValue(0));
1167 NodeTys.push_back(MVT::i32);
1168 } else {
1169 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1170 ResultVals.push_back(Chain.getValue(0));
Evan Cheng172fce72006-01-06 00:43:03 +00001171 }
Evan Cheng2a330942006-05-25 00:59:30 +00001172 NodeTys.push_back(MVT::i32);
1173 break;
1174 case MVT::v16i8:
1175 case MVT::v8i16:
1176 case MVT::v4i32:
1177 case MVT::v2i64:
1178 case MVT::v4f32:
1179 case MVT::v2f64:
Evan Cheng2a330942006-05-25 00:59:30 +00001180 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1181 ResultVals.push_back(Chain.getValue(0));
1182 NodeTys.push_back(RetVT);
1183 break;
1184 case MVT::f32:
1185 case MVT::f64: {
1186 std::vector<MVT::ValueType> Tys;
1187 Tys.push_back(MVT::f64);
1188 Tys.push_back(MVT::Other);
1189 Tys.push_back(MVT::Flag);
1190 std::vector<SDOperand> Ops;
1191 Ops.push_back(Chain);
1192 Ops.push_back(InFlag);
1193 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1194 Chain = RetVal.getValue(1);
1195 InFlag = RetVal.getValue(2);
1196 if (X86ScalarSSE) {
1197 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1198 // shouldn't be necessary except that RFP cannot be live across
1199 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1200 MachineFunction &MF = DAG.getMachineFunction();
1201 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1202 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1203 Tys.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +00001204 Tys.push_back(MVT::Other);
Evan Cheng2a330942006-05-25 00:59:30 +00001205 Ops.clear();
Nate Begeman7e5496d2006-02-17 00:03:04 +00001206 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001207 Ops.push_back(RetVal);
1208 Ops.push_back(StackSlot);
1209 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001210 Ops.push_back(InFlag);
Evan Cheng2a330942006-05-25 00:59:30 +00001211 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1212 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1213 DAG.getSrcValue(NULL));
1214 Chain = RetVal.getValue(1);
1215 }
Evan Cheng172fce72006-01-06 00:43:03 +00001216
Evan Cheng2a330942006-05-25 00:59:30 +00001217 if (RetVT == MVT::f32 && !X86ScalarSSE)
1218 // FIXME: we would really like to remember that this FP_ROUND
1219 // operation is okay to eliminate if we allow excess FP precision.
1220 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1221 ResultVals.push_back(RetVal);
1222 NodeTys.push_back(RetVT);
1223 break;
1224 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001225 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001226
Evan Cheng2a330942006-05-25 00:59:30 +00001227
1228 // If the function returns void, just return the chain.
1229 if (ResultVals.empty())
1230 return Chain;
1231
1232 // Otherwise, merge everything together with a MERGE_VALUES node.
1233 NodeTys.push_back(MVT::Other);
1234 ResultVals.push_back(Chain);
1235 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1236 return Res.getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001237}
1238
1239SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1240 if (ReturnAddrIndex == 0) {
1241 // Set up a frame object for the return address.
1242 MachineFunction &MF = DAG.getMachineFunction();
1243 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1244 }
1245
1246 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1247}
1248
1249
1250
1251std::pair<SDOperand, SDOperand> X86TargetLowering::
1252LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1253 SelectionDAG &DAG) {
1254 SDOperand Result;
1255 if (Depth) // Depths > 0 not supported yet!
1256 Result = DAG.getConstant(0, getPointerTy());
1257 else {
1258 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1259 if (!isFrameAddress)
1260 // Just load the return address
1261 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1262 DAG.getSrcValue(NULL));
1263 else
1264 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1265 DAG.getConstant(4, MVT::i32));
1266 }
1267 return std::make_pair(Result, Chain);
1268}
1269
Evan Cheng339edad2006-01-11 00:33:36 +00001270/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1271/// which corresponds to the condition code.
1272static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1273 switch (X86CC) {
1274 default: assert(0 && "Unknown X86 conditional code!");
1275 case X86ISD::COND_A: return X86::JA;
1276 case X86ISD::COND_AE: return X86::JAE;
1277 case X86ISD::COND_B: return X86::JB;
1278 case X86ISD::COND_BE: return X86::JBE;
1279 case X86ISD::COND_E: return X86::JE;
1280 case X86ISD::COND_G: return X86::JG;
1281 case X86ISD::COND_GE: return X86::JGE;
1282 case X86ISD::COND_L: return X86::JL;
1283 case X86ISD::COND_LE: return X86::JLE;
1284 case X86ISD::COND_NE: return X86::JNE;
1285 case X86ISD::COND_NO: return X86::JNO;
1286 case X86ISD::COND_NP: return X86::JNP;
1287 case X86ISD::COND_NS: return X86::JNS;
1288 case X86ISD::COND_O: return X86::JO;
1289 case X86ISD::COND_P: return X86::JP;
1290 case X86ISD::COND_S: return X86::JS;
1291 }
1292}
Chris Lattner76ac0682005-11-15 00:40:23 +00001293
Evan Cheng45df7f82006-01-30 23:41:35 +00001294/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1295/// specific condition code. It returns a false if it cannot do a direct
1296/// translation. X86CC is the translated CondCode. Flip is set to true if the
1297/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001298static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1299 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001300 Flip = false;
1301 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001302 if (!isFP) {
1303 switch (SetCCOpcode) {
1304 default: break;
1305 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1306 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1307 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1308 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1309 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1310 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1311 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1312 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1313 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1314 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1315 }
1316 } else {
1317 // On a floating point condition, the flags are set as follows:
1318 // ZF PF CF op
1319 // 0 | 0 | 0 | X > Y
1320 // 0 | 0 | 1 | X < Y
1321 // 1 | 0 | 0 | X == Y
1322 // 1 | 1 | 1 | unordered
1323 switch (SetCCOpcode) {
1324 default: break;
1325 case ISD::SETUEQ:
1326 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001327 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001328 case ISD::SETOGT:
1329 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001330 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001331 case ISD::SETOGE:
1332 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001333 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001334 case ISD::SETULT:
1335 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001336 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001337 case ISD::SETULE:
1338 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1339 case ISD::SETONE:
1340 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1341 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1342 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1343 }
1344 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001345
1346 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001347}
1348
Evan Cheng78038292006-04-05 23:38:46 +00001349static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1350 bool &Flip) {
1351 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1352}
1353
Evan Cheng339edad2006-01-11 00:33:36 +00001354/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1355/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001356/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001357static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001358 switch (X86CC) {
1359 default:
1360 return false;
1361 case X86ISD::COND_B:
1362 case X86ISD::COND_BE:
1363 case X86ISD::COND_E:
1364 case X86ISD::COND_P:
1365 case X86ISD::COND_A:
1366 case X86ISD::COND_AE:
1367 case X86ISD::COND_NE:
1368 case X86ISD::COND_NP:
1369 return true;
1370 }
1371}
1372
Evan Cheng339edad2006-01-11 00:33:36 +00001373MachineBasicBlock *
1374X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1375 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001376 switch (MI->getOpcode()) {
1377 default: assert(false && "Unexpected instr type to insert");
1378 case X86::CMOV_FR32:
Evan Cheng617a6a82006-04-10 07:23:14 +00001379 case X86::CMOV_FR64:
1380 case X86::CMOV_V4F32:
1381 case X86::CMOV_V2F64:
1382 case X86::CMOV_V2I64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001383 // To "insert" a SELECT_CC instruction, we actually have to insert the
1384 // diamond control-flow pattern. The incoming instruction knows the
1385 // destination vreg to set, the condition code register to branch on, the
1386 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001387 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1388 ilist<MachineBasicBlock>::iterator It = BB;
1389 ++It;
1390
1391 // thisMBB:
1392 // ...
1393 // TrueVal = ...
1394 // cmpTY ccX, r1, r2
1395 // bCC copy1MBB
1396 // fallthrough --> copy0MBB
1397 MachineBasicBlock *thisMBB = BB;
1398 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1399 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1400 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1401 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1402 MachineFunction *F = BB->getParent();
1403 F->getBasicBlockList().insert(It, copy0MBB);
1404 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001405 // Update machine-CFG edges by first adding all successors of the current
1406 // block to the new block which will contain the Phi node for the select.
1407 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1408 e = BB->succ_end(); i != e; ++i)
1409 sinkMBB->addSuccessor(*i);
1410 // Next, remove all successors of the current block, and add the true
1411 // and fallthrough blocks as its successors.
1412 while(!BB->succ_empty())
1413 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001414 BB->addSuccessor(copy0MBB);
1415 BB->addSuccessor(sinkMBB);
1416
1417 // copy0MBB:
1418 // %FalseValue = ...
1419 // # fallthrough to sinkMBB
1420 BB = copy0MBB;
1421
1422 // Update machine-CFG edges
1423 BB->addSuccessor(sinkMBB);
1424
1425 // sinkMBB:
1426 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1427 // ...
1428 BB = sinkMBB;
1429 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1430 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1431 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001432
Evan Cheng911c68d2006-01-16 21:21:29 +00001433 delete MI; // The pseudo instruction is gone now.
1434 return BB;
1435 }
Evan Cheng339edad2006-01-11 00:33:36 +00001436
Evan Cheng911c68d2006-01-16 21:21:29 +00001437 case X86::FP_TO_INT16_IN_MEM:
1438 case X86::FP_TO_INT32_IN_MEM:
1439 case X86::FP_TO_INT64_IN_MEM: {
1440 // Change the floating point control register to use "round towards zero"
1441 // mode when truncating to an integer value.
1442 MachineFunction *F = BB->getParent();
1443 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1444 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1445
1446 // Load the old value of the high byte of the control word...
1447 unsigned OldCW =
Evan Cheng9fee4422006-05-16 07:21:53 +00001448 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng911c68d2006-01-16 21:21:29 +00001449 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1450
1451 // Set the high part to be round to zero...
1452 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1453
1454 // Reload the modified control word now...
1455 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1456
1457 // Restore the memory image of control word to original value
1458 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1459
1460 // Get the X86 opcode to use.
1461 unsigned Opc;
1462 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001463 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001464 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1465 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1466 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1467 }
1468
1469 X86AddressMode AM;
1470 MachineOperand &Op = MI->getOperand(0);
1471 if (Op.isRegister()) {
1472 AM.BaseType = X86AddressMode::RegBase;
1473 AM.Base.Reg = Op.getReg();
1474 } else {
1475 AM.BaseType = X86AddressMode::FrameIndexBase;
1476 AM.Base.FrameIndex = Op.getFrameIndex();
1477 }
1478 Op = MI->getOperand(1);
1479 if (Op.isImmediate())
1480 AM.Scale = Op.getImmedValue();
1481 Op = MI->getOperand(2);
1482 if (Op.isImmediate())
1483 AM.IndexReg = Op.getImmedValue();
1484 Op = MI->getOperand(3);
1485 if (Op.isGlobalAddress()) {
1486 AM.GV = Op.getGlobal();
1487 } else {
1488 AM.Disp = Op.getImmedValue();
1489 }
1490 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1491
1492 // Reload the original control word now.
1493 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1494
1495 delete MI; // The pseudo instruction is gone now.
1496 return BB;
1497 }
1498 }
Evan Cheng339edad2006-01-11 00:33:36 +00001499}
1500
1501
1502//===----------------------------------------------------------------------===//
1503// X86 Custom Lowering Hooks
1504//===----------------------------------------------------------------------===//
1505
Evan Chengaf598d22006-03-13 23:18:16 +00001506/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1507/// load. For Darwin, external and weak symbols are indirect, loading the value
1508/// at address GV rather then the value of GV itself. This means that the
1509/// GlobalAddress must be in the base or index register of the address, not the
1510/// GV offset field.
1511static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1512 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1513 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1514}
1515
Evan Chengc995b452006-04-06 23:23:56 +00001516/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001517/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001518static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1519 if (Op.getOpcode() == ISD::UNDEF)
1520 return true;
1521
1522 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001523 return (Val >= Low && Val < Hi);
1524}
1525
1526/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1527/// true if Op is undef or if its value equal to the specified value.
1528static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1529 if (Op.getOpcode() == ISD::UNDEF)
1530 return true;
1531 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001532}
1533
Evan Cheng68ad48b2006-03-22 18:59:22 +00001534/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1535/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1536bool X86::isPSHUFDMask(SDNode *N) {
1537 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1538
1539 if (N->getNumOperands() != 4)
1540 return false;
1541
1542 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001543 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001544 SDOperand Arg = N->getOperand(i);
1545 if (Arg.getOpcode() == ISD::UNDEF) continue;
1546 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1547 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001548 return false;
1549 }
1550
1551 return true;
1552}
1553
1554/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001555/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001556bool X86::isPSHUFHWMask(SDNode *N) {
1557 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1558
1559 if (N->getNumOperands() != 8)
1560 return false;
1561
1562 // Lower quadword copied in order.
1563 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001564 SDOperand Arg = N->getOperand(i);
1565 if (Arg.getOpcode() == ISD::UNDEF) continue;
1566 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1567 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001568 return false;
1569 }
1570
1571 // Upper quadword shuffled.
1572 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001573 SDOperand Arg = N->getOperand(i);
1574 if (Arg.getOpcode() == ISD::UNDEF) continue;
1575 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1576 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001577 if (Val < 4 || Val > 7)
1578 return false;
1579 }
1580
1581 return true;
1582}
1583
1584/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001585/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001586bool X86::isPSHUFLWMask(SDNode *N) {
1587 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1588
1589 if (N->getNumOperands() != 8)
1590 return false;
1591
1592 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001593 for (unsigned i = 4; i != 8; ++i)
1594 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001595 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001596
1597 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001598 for (unsigned i = 0; i != 4; ++i)
1599 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001600 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001601
1602 return true;
1603}
1604
Evan Chengd27fb3e2006-03-24 01:18:28 +00001605/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1606/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng60f0b892006-04-20 08:58:49 +00001607static bool isSHUFPMask(std::vector<SDOperand> &N) {
1608 unsigned NumElems = N.size();
1609 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001610
Evan Cheng60f0b892006-04-20 08:58:49 +00001611 unsigned Half = NumElems / 2;
1612 for (unsigned i = 0; i < Half; ++i)
1613 if (!isUndefOrInRange(N[i], 0, NumElems))
1614 return false;
1615 for (unsigned i = Half; i < NumElems; ++i)
1616 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1617 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001618
1619 return true;
1620}
1621
Evan Cheng60f0b892006-04-20 08:58:49 +00001622bool X86::isSHUFPMask(SDNode *N) {
1623 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1624 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1625 return ::isSHUFPMask(Ops);
1626}
1627
1628/// isCommutedSHUFP - Returns true if the shuffle mask is except
1629/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1630/// half elements to come from vector 1 (which would equal the dest.) and
1631/// the upper half to come from vector 2.
1632static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1633 unsigned NumElems = Ops.size();
1634 if (NumElems != 2 && NumElems != 4) return false;
1635
1636 unsigned Half = NumElems / 2;
1637 for (unsigned i = 0; i < Half; ++i)
1638 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1639 return false;
1640 for (unsigned i = Half; i < NumElems; ++i)
1641 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1642 return false;
1643 return true;
1644}
1645
1646static bool isCommutedSHUFP(SDNode *N) {
1647 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1648 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1649 return isCommutedSHUFP(Ops);
1650}
1651
Evan Cheng2595a682006-03-24 02:58:06 +00001652/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1653/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1654bool X86::isMOVHLPSMask(SDNode *N) {
1655 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1656
Evan Cheng1a194a52006-03-28 06:50:32 +00001657 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001658 return false;
1659
Evan Cheng1a194a52006-03-28 06:50:32 +00001660 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001661 return isUndefOrEqual(N->getOperand(0), 6) &&
1662 isUndefOrEqual(N->getOperand(1), 7) &&
1663 isUndefOrEqual(N->getOperand(2), 2) &&
1664 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001665}
1666
Evan Chengc995b452006-04-06 23:23:56 +00001667/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1668/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1669bool X86::isMOVLPMask(SDNode *N) {
1670 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1671
1672 unsigned NumElems = N->getNumOperands();
1673 if (NumElems != 2 && NumElems != 4)
1674 return false;
1675
Evan Chengac847262006-04-07 21:53:05 +00001676 for (unsigned i = 0; i < NumElems/2; ++i)
1677 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1678 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001679
Evan Chengac847262006-04-07 21:53:05 +00001680 for (unsigned i = NumElems/2; i < NumElems; ++i)
1681 if (!isUndefOrEqual(N->getOperand(i), i))
1682 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001683
1684 return true;
1685}
1686
1687/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001688/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1689/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001690bool X86::isMOVHPMask(SDNode *N) {
1691 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1692
1693 unsigned NumElems = N->getNumOperands();
1694 if (NumElems != 2 && NumElems != 4)
1695 return false;
1696
Evan Chengac847262006-04-07 21:53:05 +00001697 for (unsigned i = 0; i < NumElems/2; ++i)
1698 if (!isUndefOrEqual(N->getOperand(i), i))
1699 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001700
1701 for (unsigned i = 0; i < NumElems/2; ++i) {
1702 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001703 if (!isUndefOrEqual(Arg, i + NumElems))
1704 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001705 }
1706
1707 return true;
1708}
1709
Evan Cheng5df75882006-03-28 00:39:58 +00001710/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1711/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng60f0b892006-04-20 08:58:49 +00001712bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1713 unsigned NumElems = N.size();
Evan Cheng5df75882006-03-28 00:39:58 +00001714 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1715 return false;
1716
1717 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001718 SDOperand BitI = N[i];
1719 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001720 if (!isUndefOrEqual(BitI, j))
1721 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001722 if (V2IsSplat) {
1723 if (isUndefOrEqual(BitI1, NumElems))
1724 return false;
1725 } else {
1726 if (!isUndefOrEqual(BitI1, j + NumElems))
1727 return false;
1728 }
Evan Cheng5df75882006-03-28 00:39:58 +00001729 }
1730
1731 return true;
1732}
1733
Evan Cheng60f0b892006-04-20 08:58:49 +00001734bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1735 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1736 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1737 return ::isUNPCKLMask(Ops, V2IsSplat);
1738}
1739
Evan Cheng2bc32802006-03-28 02:43:26 +00001740/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1741/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng60f0b892006-04-20 08:58:49 +00001742bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1743 unsigned NumElems = N.size();
Evan Cheng2bc32802006-03-28 02:43:26 +00001744 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1745 return false;
1746
1747 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001748 SDOperand BitI = N[i];
1749 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001750 if (!isUndefOrEqual(BitI, j + NumElems/2))
1751 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001752 if (V2IsSplat) {
1753 if (isUndefOrEqual(BitI1, NumElems))
1754 return false;
1755 } else {
1756 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1757 return false;
1758 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001759 }
1760
1761 return true;
1762}
1763
Evan Cheng60f0b892006-04-20 08:58:49 +00001764bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1765 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1766 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1767 return ::isUNPCKHMask(Ops, V2IsSplat);
1768}
1769
Evan Chengf3b52c82006-04-05 07:20:06 +00001770/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1771/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1772/// <0, 0, 1, 1>
1773bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1774 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1775
1776 unsigned NumElems = N->getNumOperands();
1777 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1778 return false;
1779
1780 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1781 SDOperand BitI = N->getOperand(i);
1782 SDOperand BitI1 = N->getOperand(i+1);
1783
Evan Chengac847262006-04-07 21:53:05 +00001784 if (!isUndefOrEqual(BitI, j))
1785 return false;
1786 if (!isUndefOrEqual(BitI1, j))
1787 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001788 }
1789
1790 return true;
1791}
1792
Evan Chenge8b51802006-04-21 01:05:10 +00001793/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1794/// specifies a shuffle of elements that is suitable for input to MOVSS,
1795/// MOVSD, and MOVD, i.e. setting the lowest element.
1796static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001797 unsigned NumElems = N.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001798 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001799 return false;
1800
Evan Cheng60f0b892006-04-20 08:58:49 +00001801 if (!isUndefOrEqual(N[0], NumElems))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001802 return false;
1803
1804 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001805 SDOperand Arg = N[i];
Evan Cheng12ba3e22006-04-11 00:19:04 +00001806 if (!isUndefOrEqual(Arg, i))
1807 return false;
1808 }
1809
1810 return true;
1811}
Evan Chengf3b52c82006-04-05 07:20:06 +00001812
Evan Chenge8b51802006-04-21 01:05:10 +00001813bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001814 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1815 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001816 return ::isMOVLMask(Ops);
Evan Cheng60f0b892006-04-20 08:58:49 +00001817}
1818
Evan Chenge8b51802006-04-21 01:05:10 +00001819/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1820/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001821/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Chenge8b51802006-04-21 01:05:10 +00001822static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001823 unsigned NumElems = Ops.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001824 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001825 return false;
1826
1827 if (!isUndefOrEqual(Ops[0], 0))
1828 return false;
1829
1830 for (unsigned i = 1; i < NumElems; ++i) {
1831 SDOperand Arg = Ops[i];
1832 if (V2IsSplat) {
1833 if (!isUndefOrEqual(Arg, NumElems))
1834 return false;
1835 } else {
1836 if (!isUndefOrEqual(Arg, i+NumElems))
1837 return false;
1838 }
1839 }
1840
1841 return true;
1842}
1843
Evan Chenge8b51802006-04-21 01:05:10 +00001844static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001845 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1846 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001847 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001848}
1849
Evan Cheng5d247f82006-04-14 21:59:03 +00001850/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1851/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1852bool X86::isMOVSHDUPMask(SDNode *N) {
1853 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1854
1855 if (N->getNumOperands() != 4)
1856 return false;
1857
1858 // Expect 1, 1, 3, 3
1859 for (unsigned i = 0; i < 2; ++i) {
1860 SDOperand Arg = N->getOperand(i);
1861 if (Arg.getOpcode() == ISD::UNDEF) continue;
1862 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1863 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1864 if (Val != 1) return false;
1865 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001866
1867 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001868 for (unsigned i = 2; i < 4; ++i) {
1869 SDOperand Arg = N->getOperand(i);
1870 if (Arg.getOpcode() == ISD::UNDEF) continue;
1871 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1872 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1873 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001874 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001875 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001876
Evan Cheng6222cf22006-04-15 05:37:34 +00001877 // Don't use movshdup if it can be done with a shufps.
1878 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001879}
1880
1881/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1882/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1883bool X86::isMOVSLDUPMask(SDNode *N) {
1884 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1885
1886 if (N->getNumOperands() != 4)
1887 return false;
1888
1889 // Expect 0, 0, 2, 2
1890 for (unsigned i = 0; i < 2; ++i) {
1891 SDOperand Arg = N->getOperand(i);
1892 if (Arg.getOpcode() == ISD::UNDEF) continue;
1893 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1894 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1895 if (Val != 0) return false;
1896 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001897
1898 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001899 for (unsigned i = 2; i < 4; ++i) {
1900 SDOperand Arg = N->getOperand(i);
1901 if (Arg.getOpcode() == ISD::UNDEF) continue;
1902 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1903 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1904 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001905 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001906 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001907
Evan Cheng6222cf22006-04-15 05:37:34 +00001908 // Don't use movshdup if it can be done with a shufps.
1909 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001910}
1911
Evan Chengd097e672006-03-22 02:53:00 +00001912/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1913/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001914static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001915 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1916
Evan Chengd097e672006-03-22 02:53:00 +00001917 // This is a splat operation if each element of the permute is the same, and
1918 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001919 unsigned NumElems = N->getNumOperands();
1920 SDOperand ElementBase;
1921 unsigned i = 0;
1922 for (; i != NumElems; ++i) {
1923 SDOperand Elt = N->getOperand(i);
1924 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1925 ElementBase = Elt;
1926 break;
1927 }
1928 }
1929
1930 if (!ElementBase.Val)
1931 return false;
1932
1933 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001934 SDOperand Arg = N->getOperand(i);
1935 if (Arg.getOpcode() == ISD::UNDEF) continue;
1936 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001937 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001938 }
1939
1940 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001941 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00001942}
1943
Evan Cheng5022b342006-04-17 20:43:08 +00001944/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1945/// a splat of a single element and it's a 2 or 4 element mask.
1946bool X86::isSplatMask(SDNode *N) {
1947 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1948
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001949 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00001950 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1951 return false;
1952 return ::isSplatMask(N);
1953}
1954
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001955/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1956/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1957/// instructions.
1958unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001959 unsigned NumOperands = N->getNumOperands();
1960 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1961 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001962 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001963 unsigned Val = 0;
1964 SDOperand Arg = N->getOperand(NumOperands-i-1);
1965 if (Arg.getOpcode() != ISD::UNDEF)
1966 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001967 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001968 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001969 if (i != NumOperands - 1)
1970 Mask <<= Shift;
1971 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001972
1973 return Mask;
1974}
1975
Evan Chengb7fedff2006-03-29 23:07:14 +00001976/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1977/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1978/// instructions.
1979unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1980 unsigned Mask = 0;
1981 // 8 nodes, but we only care about the last 4.
1982 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001983 unsigned Val = 0;
1984 SDOperand Arg = N->getOperand(i);
1985 if (Arg.getOpcode() != ISD::UNDEF)
1986 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001987 Mask |= (Val - 4);
1988 if (i != 4)
1989 Mask <<= 2;
1990 }
1991
1992 return Mask;
1993}
1994
1995/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1996/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1997/// instructions.
1998unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1999 unsigned Mask = 0;
2000 // 8 nodes, but we only care about the first 4.
2001 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002002 unsigned Val = 0;
2003 SDOperand Arg = N->getOperand(i);
2004 if (Arg.getOpcode() != ISD::UNDEF)
2005 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002006 Mask |= Val;
2007 if (i != 0)
2008 Mask <<= 2;
2009 }
2010
2011 return Mask;
2012}
2013
Evan Cheng59a63552006-04-05 01:47:37 +00002014/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2015/// specifies a 8 element shuffle that can be broken into a pair of
2016/// PSHUFHW and PSHUFLW.
2017static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2018 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2019
2020 if (N->getNumOperands() != 8)
2021 return false;
2022
2023 // Lower quadword shuffled.
2024 for (unsigned i = 0; i != 4; ++i) {
2025 SDOperand Arg = N->getOperand(i);
2026 if (Arg.getOpcode() == ISD::UNDEF) continue;
2027 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2028 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2029 if (Val > 4)
2030 return false;
2031 }
2032
2033 // Upper quadword shuffled.
2034 for (unsigned i = 4; i != 8; ++i) {
2035 SDOperand Arg = N->getOperand(i);
2036 if (Arg.getOpcode() == ISD::UNDEF) continue;
2037 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2038 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2039 if (Val < 4 || Val > 7)
2040 return false;
2041 }
2042
2043 return true;
2044}
2045
Evan Chengc995b452006-04-06 23:23:56 +00002046/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2047/// values in ther permute mask.
2048static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2049 SDOperand V1 = Op.getOperand(0);
2050 SDOperand V2 = Op.getOperand(1);
2051 SDOperand Mask = Op.getOperand(2);
2052 MVT::ValueType VT = Op.getValueType();
2053 MVT::ValueType MaskVT = Mask.getValueType();
2054 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2055 unsigned NumElems = Mask.getNumOperands();
2056 std::vector<SDOperand> MaskVec;
2057
2058 for (unsigned i = 0; i != NumElems; ++i) {
2059 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002060 if (Arg.getOpcode() == ISD::UNDEF) {
2061 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2062 continue;
2063 }
Evan Chengc995b452006-04-06 23:23:56 +00002064 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2065 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2066 if (Val < NumElems)
2067 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2068 else
2069 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2070 }
2071
2072 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2073 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2074}
2075
Evan Cheng7855e4d2006-04-19 20:35:22 +00002076/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2077/// match movhlps. The lower half elements should come from upper half of
2078/// V1 (and in order), and the upper half elements should come from the upper
2079/// half of V2 (and in order).
2080static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2081 unsigned NumElems = Mask->getNumOperands();
2082 if (NumElems != 4)
2083 return false;
2084 for (unsigned i = 0, e = 2; i != e; ++i)
2085 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2086 return false;
2087 for (unsigned i = 2; i != 4; ++i)
2088 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2089 return false;
2090 return true;
2091}
2092
Evan Chengc995b452006-04-06 23:23:56 +00002093/// isScalarLoadToVector - Returns true if the node is a scalar load that
2094/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002095static inline bool isScalarLoadToVector(SDNode *N) {
2096 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2097 N = N->getOperand(0).Val;
2098 return (N->getOpcode() == ISD::LOAD);
Evan Chengc995b452006-04-06 23:23:56 +00002099 }
2100 return false;
2101}
2102
Evan Cheng7855e4d2006-04-19 20:35:22 +00002103/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2104/// match movlp{s|d}. The lower half elements should come from lower half of
2105/// V1 (and in order), and the upper half elements should come from the upper
2106/// half of V2 (and in order). And since V1 will become the source of the
2107/// MOVLP, it must be either a vector load or a scalar load to vector.
2108static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2109 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2110 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002111
Evan Cheng7855e4d2006-04-19 20:35:22 +00002112 unsigned NumElems = Mask->getNumOperands();
2113 if (NumElems != 2 && NumElems != 4)
2114 return false;
2115 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2116 if (!isUndefOrEqual(Mask->getOperand(i), i))
2117 return false;
2118 for (unsigned i = NumElems/2; i != NumElems; ++i)
2119 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2120 return false;
2121 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002122}
2123
Evan Cheng60f0b892006-04-20 08:58:49 +00002124/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2125/// all the same.
2126static bool isSplatVector(SDNode *N) {
2127 if (N->getOpcode() != ISD::BUILD_VECTOR)
2128 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002129
Evan Cheng60f0b892006-04-20 08:58:49 +00002130 SDOperand SplatValue = N->getOperand(0);
2131 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2132 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002133 return false;
2134 return true;
2135}
2136
Evan Cheng60f0b892006-04-20 08:58:49 +00002137/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2138/// that point to V2 points to its first element.
2139static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2140 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2141
2142 bool Changed = false;
2143 std::vector<SDOperand> MaskVec;
2144 unsigned NumElems = Mask.getNumOperands();
2145 for (unsigned i = 0; i != NumElems; ++i) {
2146 SDOperand Arg = Mask.getOperand(i);
2147 if (Arg.getOpcode() != ISD::UNDEF) {
2148 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2149 if (Val > NumElems) {
2150 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2151 Changed = true;
2152 }
2153 }
2154 MaskVec.push_back(Arg);
2155 }
2156
2157 if (Changed)
2158 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2159 return Mask;
2160}
2161
Evan Chenge8b51802006-04-21 01:05:10 +00002162/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2163/// operation of specified width.
2164static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002165 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2166 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2167
2168 std::vector<SDOperand> MaskVec;
2169 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2170 for (unsigned i = 1; i != NumElems; ++i)
2171 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2172 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2173}
2174
Evan Cheng5022b342006-04-17 20:43:08 +00002175/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2176/// of specified width.
2177static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2178 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2179 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2180 std::vector<SDOperand> MaskVec;
2181 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2182 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2183 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2184 }
2185 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2186}
2187
Evan Cheng60f0b892006-04-20 08:58:49 +00002188/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2189/// of specified width.
2190static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2191 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2192 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2193 unsigned Half = NumElems/2;
2194 std::vector<SDOperand> MaskVec;
2195 for (unsigned i = 0; i != Half; ++i) {
2196 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2197 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2198 }
2199 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2200}
2201
Evan Chenge8b51802006-04-21 01:05:10 +00002202/// getZeroVector - Returns a vector of specified type with all zero elements.
2203///
2204static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2205 assert(MVT::isVector(VT) && "Expected a vector type");
2206 unsigned NumElems = getVectorNumElements(VT);
2207 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2208 bool isFP = MVT::isFloatingPoint(EVT);
2209 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2210 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2211 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2212}
2213
Evan Cheng5022b342006-04-17 20:43:08 +00002214/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2215///
2216static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2217 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002218 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002219 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002220 unsigned NumElems = Mask.getNumOperands();
2221 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002222 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002223 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002224 NumElems >>= 1;
2225 }
2226 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2227
2228 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002229 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002230 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002231 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002232 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2233}
2234
Evan Chenge8b51802006-04-21 01:05:10 +00002235/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2236/// constant +0.0.
2237static inline bool isZeroNode(SDOperand Elt) {
2238 return ((isa<ConstantSDNode>(Elt) &&
2239 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2240 (isa<ConstantFPSDNode>(Elt) &&
2241 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2242}
2243
Evan Cheng14215c32006-04-21 23:03:30 +00002244/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2245/// vector and zero or undef vector.
2246static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002247 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002248 bool isZero, SelectionDAG &DAG) {
2249 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002250 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2251 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2252 SDOperand Zero = DAG.getConstant(0, EVT);
2253 std::vector<SDOperand> MaskVec(NumElems, Zero);
2254 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2255 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng14215c32006-04-21 23:03:30 +00002256 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002257}
2258
Evan Chengb0461082006-04-24 18:01:45 +00002259/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2260///
2261static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2262 unsigned NumNonZero, unsigned NumZero,
2263 SelectionDAG &DAG) {
2264 if (NumNonZero > 8)
2265 return SDOperand();
2266
2267 SDOperand V(0, 0);
2268 bool First = true;
2269 for (unsigned i = 0; i < 16; ++i) {
2270 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2271 if (ThisIsNonZero && First) {
2272 if (NumZero)
2273 V = getZeroVector(MVT::v8i16, DAG);
2274 else
2275 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2276 First = false;
2277 }
2278
2279 if ((i & 1) != 0) {
2280 SDOperand ThisElt(0, 0), LastElt(0, 0);
2281 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2282 if (LastIsNonZero) {
2283 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2284 }
2285 if (ThisIsNonZero) {
2286 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2287 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2288 ThisElt, DAG.getConstant(8, MVT::i8));
2289 if (LastIsNonZero)
2290 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2291 } else
2292 ThisElt = LastElt;
2293
2294 if (ThisElt.Val)
2295 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2296 DAG.getConstant(i/2, MVT::i32));
2297 }
2298 }
2299
2300 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2301}
2302
2303/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2304///
2305static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2306 unsigned NumNonZero, unsigned NumZero,
2307 SelectionDAG &DAG) {
2308 if (NumNonZero > 4)
2309 return SDOperand();
2310
2311 SDOperand V(0, 0);
2312 bool First = true;
2313 for (unsigned i = 0; i < 8; ++i) {
2314 bool isNonZero = (NonZeros & (1 << i)) != 0;
2315 if (isNonZero) {
2316 if (First) {
2317 if (NumZero)
2318 V = getZeroVector(MVT::v8i16, DAG);
2319 else
2320 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2321 First = false;
2322 }
2323 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2324 DAG.getConstant(i, MVT::i32));
2325 }
2326 }
2327
2328 return V;
2329}
2330
Evan Chenga9467aa2006-04-25 20:13:52 +00002331SDOperand
2332X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2333 // All zero's are handled with pxor.
2334 if (ISD::isBuildVectorAllZeros(Op.Val))
2335 return Op;
2336
2337 // All one's are handled with pcmpeqd.
2338 if (ISD::isBuildVectorAllOnes(Op.Val))
2339 return Op;
2340
2341 MVT::ValueType VT = Op.getValueType();
2342 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2343 unsigned EVTBits = MVT::getSizeInBits(EVT);
2344
2345 unsigned NumElems = Op.getNumOperands();
2346 unsigned NumZero = 0;
2347 unsigned NumNonZero = 0;
2348 unsigned NonZeros = 0;
2349 std::set<SDOperand> Values;
2350 for (unsigned i = 0; i < NumElems; ++i) {
2351 SDOperand Elt = Op.getOperand(i);
2352 if (Elt.getOpcode() != ISD::UNDEF) {
2353 Values.insert(Elt);
2354 if (isZeroNode(Elt))
2355 NumZero++;
2356 else {
2357 NonZeros |= (1 << i);
2358 NumNonZero++;
2359 }
2360 }
2361 }
2362
2363 if (NumNonZero == 0)
2364 // Must be a mix of zero and undef. Return a zero vector.
2365 return getZeroVector(VT, DAG);
2366
2367 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2368 if (Values.size() == 1)
2369 return SDOperand();
2370
2371 // Special case for single non-zero element.
2372 if (NumNonZero == 1) {
2373 unsigned Idx = CountTrailingZeros_32(NonZeros);
2374 SDOperand Item = Op.getOperand(Idx);
2375 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2376 if (Idx == 0)
2377 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2378 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2379 NumZero > 0, DAG);
2380
2381 if (EVTBits == 32) {
2382 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2383 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2384 DAG);
2385 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2386 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2387 std::vector<SDOperand> MaskVec;
2388 for (unsigned i = 0; i < NumElems; i++)
2389 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2390 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2391 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2392 DAG.getNode(ISD::UNDEF, VT), Mask);
2393 }
2394 }
2395
2396 // Let legalizer expand 2-widde build_vector's.
2397 if (EVTBits == 64)
2398 return SDOperand();
2399
2400 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2401 if (EVTBits == 8) {
2402 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2403 if (V.Val) return V;
2404 }
2405
2406 if (EVTBits == 16) {
2407 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2408 if (V.Val) return V;
2409 }
2410
2411 // If element VT is == 32 bits, turn it into a number of shuffles.
2412 std::vector<SDOperand> V(NumElems);
2413 if (NumElems == 4 && NumZero > 0) {
2414 for (unsigned i = 0; i < 4; ++i) {
2415 bool isZero = !(NonZeros & (1 << i));
2416 if (isZero)
2417 V[i] = getZeroVector(VT, DAG);
2418 else
2419 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2420 }
2421
2422 for (unsigned i = 0; i < 2; ++i) {
2423 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2424 default: break;
2425 case 0:
2426 V[i] = V[i*2]; // Must be a zero vector.
2427 break;
2428 case 1:
2429 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2430 getMOVLMask(NumElems, DAG));
2431 break;
2432 case 2:
2433 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2434 getMOVLMask(NumElems, DAG));
2435 break;
2436 case 3:
2437 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2438 getUnpacklMask(NumElems, DAG));
2439 break;
2440 }
2441 }
2442
Evan Cheng9fee4422006-05-16 07:21:53 +00002443 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Chenga9467aa2006-04-25 20:13:52 +00002444 // clears the upper bits.
2445 // FIXME: we can do the same for v4f32 case when we know both parts of
2446 // the lower half come from scalar_to_vector (loadf32). We should do
2447 // that in post legalizer dag combiner with target specific hooks.
2448 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2449 return V[0];
2450 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2451 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2452 std::vector<SDOperand> MaskVec;
2453 bool Reverse = (NonZeros & 0x3) == 2;
2454 for (unsigned i = 0; i < 2; ++i)
2455 if (Reverse)
2456 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2457 else
2458 MaskVec.push_back(DAG.getConstant(i, EVT));
2459 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2460 for (unsigned i = 0; i < 2; ++i)
2461 if (Reverse)
2462 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2463 else
2464 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2465 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2466 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2467 }
2468
2469 if (Values.size() > 2) {
2470 // Expand into a number of unpckl*.
2471 // e.g. for v4f32
2472 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2473 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2474 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2475 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2476 for (unsigned i = 0; i < NumElems; ++i)
2477 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2478 NumElems >>= 1;
2479 while (NumElems != 0) {
2480 for (unsigned i = 0; i < NumElems; ++i)
2481 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2482 UnpckMask);
2483 NumElems >>= 1;
2484 }
2485 return V[0];
2486 }
2487
2488 return SDOperand();
2489}
2490
2491SDOperand
2492X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2493 SDOperand V1 = Op.getOperand(0);
2494 SDOperand V2 = Op.getOperand(1);
2495 SDOperand PermMask = Op.getOperand(2);
2496 MVT::ValueType VT = Op.getValueType();
2497 unsigned NumElems = PermMask.getNumOperands();
2498 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2499 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2500
2501 if (isSplatMask(PermMask.Val)) {
2502 if (NumElems <= 4) return Op;
2503 // Promote it to a v4i32 splat.
2504 return PromoteSplat(Op, DAG);
2505 }
2506
2507 if (X86::isMOVLMask(PermMask.Val))
2508 return (V1IsUndef) ? V2 : Op;
2509
2510 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2511 X86::isMOVSLDUPMask(PermMask.Val) ||
2512 X86::isMOVHLPSMask(PermMask.Val) ||
2513 X86::isMOVHPMask(PermMask.Val) ||
2514 X86::isMOVLPMask(PermMask.Val))
2515 return Op;
2516
2517 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2518 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2519 return CommuteVectorShuffle(Op, DAG);
2520
2521 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2522 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2523 if (V1IsSplat && !V2IsSplat) {
2524 Op = CommuteVectorShuffle(Op, DAG);
2525 V1 = Op.getOperand(0);
2526 V2 = Op.getOperand(1);
2527 PermMask = Op.getOperand(2);
2528 V2IsSplat = true;
2529 }
2530
2531 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2532 if (V2IsUndef) return V1;
2533 Op = CommuteVectorShuffle(Op, DAG);
2534 V1 = Op.getOperand(0);
2535 V2 = Op.getOperand(1);
2536 PermMask = Op.getOperand(2);
2537 if (V2IsSplat) {
2538 // V2 is a splat, so the mask may be malformed. That is, it may point
2539 // to any V2 element. The instruction selectior won't like this. Get
2540 // a corrected mask and commute to form a proper MOVS{S|D}.
2541 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2542 if (NewMask.Val != PermMask.Val)
2543 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2544 }
2545 return Op;
2546 }
2547
2548 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2549 X86::isUNPCKLMask(PermMask.Val) ||
2550 X86::isUNPCKHMask(PermMask.Val))
2551 return Op;
2552
2553 if (V2IsSplat) {
2554 // Normalize mask so all entries that point to V2 points to its first
2555 // element then try to match unpck{h|l} again. If match, return a
2556 // new vector_shuffle with the corrected mask.
2557 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2558 if (NewMask.Val != PermMask.Val) {
2559 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2560 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2561 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2562 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2563 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2564 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2565 }
2566 }
2567 }
2568
2569 // Normalize the node to match x86 shuffle ops if needed
2570 if (V2.getOpcode() != ISD::UNDEF)
2571 if (isCommutedSHUFP(PermMask.Val)) {
2572 Op = CommuteVectorShuffle(Op, DAG);
2573 V1 = Op.getOperand(0);
2574 V2 = Op.getOperand(1);
2575 PermMask = Op.getOperand(2);
2576 }
2577
2578 // If VT is integer, try PSHUF* first, then SHUFP*.
2579 if (MVT::isInteger(VT)) {
2580 if (X86::isPSHUFDMask(PermMask.Val) ||
2581 X86::isPSHUFHWMask(PermMask.Val) ||
2582 X86::isPSHUFLWMask(PermMask.Val)) {
2583 if (V2.getOpcode() != ISD::UNDEF)
2584 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2585 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2586 return Op;
2587 }
2588
2589 if (X86::isSHUFPMask(PermMask.Val))
2590 return Op;
2591
2592 // Handle v8i16 shuffle high / low shuffle node pair.
2593 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2594 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2595 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2596 std::vector<SDOperand> MaskVec;
2597 for (unsigned i = 0; i != 4; ++i)
2598 MaskVec.push_back(PermMask.getOperand(i));
2599 for (unsigned i = 4; i != 8; ++i)
2600 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2601 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2602 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2603 MaskVec.clear();
2604 for (unsigned i = 0; i != 4; ++i)
2605 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2606 for (unsigned i = 4; i != 8; ++i)
2607 MaskVec.push_back(PermMask.getOperand(i));
2608 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2609 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2610 }
2611 } else {
2612 // Floating point cases in the other order.
2613 if (X86::isSHUFPMask(PermMask.Val))
2614 return Op;
2615 if (X86::isPSHUFDMask(PermMask.Val) ||
2616 X86::isPSHUFHWMask(PermMask.Val) ||
2617 X86::isPSHUFLWMask(PermMask.Val)) {
2618 if (V2.getOpcode() != ISD::UNDEF)
2619 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2620 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2621 return Op;
2622 }
2623 }
2624
2625 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002626 MVT::ValueType MaskVT = PermMask.getValueType();
2627 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng3cd43622006-04-28 07:03:38 +00002628 std::vector<std::pair<int, int> > Locs;
2629 Locs.reserve(NumElems);
2630 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2631 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2632 unsigned NumHi = 0;
2633 unsigned NumLo = 0;
2634 // If no more than two elements come from either vector. This can be
2635 // implemented with two shuffles. First shuffle gather the elements.
2636 // The second shuffle, which takes the first shuffle as both of its
2637 // vector operands, put the elements into the right order.
2638 for (unsigned i = 0; i != NumElems; ++i) {
2639 SDOperand Elt = PermMask.getOperand(i);
2640 if (Elt.getOpcode() == ISD::UNDEF) {
2641 Locs[i] = std::make_pair(-1, -1);
2642 } else {
2643 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2644 if (Val < NumElems) {
2645 Locs[i] = std::make_pair(0, NumLo);
2646 Mask1[NumLo] = Elt;
2647 NumLo++;
2648 } else {
2649 Locs[i] = std::make_pair(1, NumHi);
2650 if (2+NumHi < NumElems)
2651 Mask1[2+NumHi] = Elt;
2652 NumHi++;
2653 }
2654 }
2655 }
2656 if (NumLo <= 2 && NumHi <= 2) {
2657 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2658 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2659 for (unsigned i = 0; i != NumElems; ++i) {
2660 if (Locs[i].first == -1)
2661 continue;
2662 else {
2663 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2664 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2665 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2666 }
2667 }
2668
2669 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2670 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2671 }
2672
2673 // Break it into (shuffle shuffle_hi, shuffle_lo).
2674 Locs.clear();
Evan Chenga9467aa2006-04-25 20:13:52 +00002675 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2676 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2677 std::vector<SDOperand> *MaskPtr = &LoMask;
2678 unsigned MaskIdx = 0;
2679 unsigned LoIdx = 0;
2680 unsigned HiIdx = NumElems/2;
2681 for (unsigned i = 0; i != NumElems; ++i) {
2682 if (i == NumElems/2) {
2683 MaskPtr = &HiMask;
2684 MaskIdx = 1;
2685 LoIdx = 0;
2686 HiIdx = NumElems/2;
2687 }
2688 SDOperand Elt = PermMask.getOperand(i);
2689 if (Elt.getOpcode() == ISD::UNDEF) {
2690 Locs[i] = std::make_pair(-1, -1);
2691 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2692 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2693 (*MaskPtr)[LoIdx] = Elt;
2694 LoIdx++;
2695 } else {
2696 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2697 (*MaskPtr)[HiIdx] = Elt;
2698 HiIdx++;
2699 }
2700 }
2701
Chris Lattner3d826992006-05-16 06:45:34 +00002702 SDOperand LoShuffle =
2703 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2704 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2705 SDOperand HiShuffle =
2706 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2707 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Chenga9467aa2006-04-25 20:13:52 +00002708 std::vector<SDOperand> MaskOps;
2709 for (unsigned i = 0; i != NumElems; ++i) {
2710 if (Locs[i].first == -1) {
2711 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2712 } else {
2713 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2714 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2715 }
2716 }
2717 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2718 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2719 }
2720
2721 return SDOperand();
2722}
2723
2724SDOperand
2725X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2726 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2727 return SDOperand();
2728
2729 MVT::ValueType VT = Op.getValueType();
2730 // TODO: handle v16i8.
2731 if (MVT::getSizeInBits(VT) == 16) {
2732 // Transform it so it match pextrw which produces a 32-bit result.
2733 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2734 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2735 Op.getOperand(0), Op.getOperand(1));
2736 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2737 DAG.getValueType(VT));
2738 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2739 } else if (MVT::getSizeInBits(VT) == 32) {
2740 SDOperand Vec = Op.getOperand(0);
2741 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2742 if (Idx == 0)
2743 return Op;
2744
2745 // SHUFPS the element to the lowest double word, then movss.
2746 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2747 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2748 MVT::getVectorBaseType(MaskVT));
2749 std::vector<SDOperand> IdxVec;
2750 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2751 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2752 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2753 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2754 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2755 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2756 Vec, Vec, Mask);
2757 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2758 DAG.getConstant(0, MVT::i32));
2759 } else if (MVT::getSizeInBits(VT) == 64) {
2760 SDOperand Vec = Op.getOperand(0);
2761 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2762 if (Idx == 0)
2763 return Op;
2764
2765 // UNPCKHPD the element to the lowest double word, then movsd.
2766 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2767 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2768 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2769 std::vector<SDOperand> IdxVec;
2770 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2771 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2772 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2773 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2774 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2775 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2776 DAG.getConstant(0, MVT::i32));
2777 }
2778
2779 return SDOperand();
2780}
2781
2782SDOperand
2783X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002784 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002785 // as its second argument.
2786 MVT::ValueType VT = Op.getValueType();
2787 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2788 SDOperand N0 = Op.getOperand(0);
2789 SDOperand N1 = Op.getOperand(1);
2790 SDOperand N2 = Op.getOperand(2);
2791 if (MVT::getSizeInBits(BaseVT) == 16) {
2792 if (N1.getValueType() != MVT::i32)
2793 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2794 if (N2.getValueType() != MVT::i32)
2795 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2796 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2797 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2798 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2799 if (Idx == 0) {
2800 // Use a movss.
2801 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2802 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2803 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2804 std::vector<SDOperand> MaskVec;
2805 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2806 for (unsigned i = 1; i <= 3; ++i)
2807 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2808 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2809 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2810 } else {
2811 // Use two pinsrw instructions to insert a 32 bit value.
2812 Idx <<= 1;
2813 if (MVT::isFloatingPoint(N1.getValueType())) {
2814 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002815 // Just load directly from f32mem to GR32.
Evan Chenga9467aa2006-04-25 20:13:52 +00002816 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2817 N1.getOperand(2));
2818 } else {
2819 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2820 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2821 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2822 DAG.getConstant(0, MVT::i32));
2823 }
2824 }
2825 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2826 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2827 DAG.getConstant(Idx, MVT::i32));
2828 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2829 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2830 DAG.getConstant(Idx+1, MVT::i32));
2831 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2832 }
2833 }
2834
2835 return SDOperand();
2836}
2837
2838SDOperand
2839X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2840 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2841 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2842}
2843
2844// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2845// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2846// one of the above mentioned nodes. It has to be wrapped because otherwise
2847// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2848// be used to form addressing mode. These wrapped nodes will be selected
2849// into MOV32ri.
2850SDOperand
2851X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2852 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2853 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2854 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2855 CP->getAlignment()));
2856 if (Subtarget->isTargetDarwin()) {
2857 // With PIC, the address is actually $g + Offset.
2858 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2859 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2860 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2861 }
2862
2863 return Result;
2864}
2865
2866SDOperand
2867X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2868 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2869 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002870 DAG.getTargetGlobalAddress(GV,
2871 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002872 if (Subtarget->isTargetDarwin()) {
2873 // With PIC, the address is actually $g + Offset.
2874 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2875 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002876 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2877 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002878
2879 // For Darwin, external and weak symbols are indirect, so we want to load
2880 // the value at address GV, not the value of GV itself. This means that
2881 // the GlobalAddress must be in the base or index register of the address,
2882 // not the GV offset field.
2883 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2884 DarwinGVRequiresExtraLoad(GV))
2885 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2886 Result, DAG.getSrcValue(NULL));
2887 }
2888
2889 return Result;
2890}
2891
2892SDOperand
2893X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2894 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2895 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002896 DAG.getTargetExternalSymbol(Sym,
2897 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002898 if (Subtarget->isTargetDarwin()) {
2899 // With PIC, the address is actually $g + Offset.
2900 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2901 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002902 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2903 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002904 }
2905
2906 return Result;
2907}
2908
2909SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00002910 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2911 "Not an i64 shift!");
2912 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2913 SDOperand ShOpLo = Op.getOperand(0);
2914 SDOperand ShOpHi = Op.getOperand(1);
2915 SDOperand ShAmt = Op.getOperand(2);
2916 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00002917 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00002918 : DAG.getConstant(0, MVT::i32);
2919
2920 SDOperand Tmp2, Tmp3;
2921 if (Op.getOpcode() == ISD::SHL_PARTS) {
2922 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2923 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2924 } else {
2925 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00002926 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00002927 }
2928
2929 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2930 ShAmt, DAG.getConstant(32, MVT::i8));
2931
2932 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00002933 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00002934
2935 std::vector<MVT::ValueType> Tys;
2936 Tys.push_back(MVT::i32);
2937 Tys.push_back(MVT::Flag);
2938 std::vector<SDOperand> Ops;
2939 if (Op.getOpcode() == ISD::SHL_PARTS) {
2940 Ops.push_back(Tmp2);
2941 Ops.push_back(Tmp3);
2942 Ops.push_back(CC);
2943 Ops.push_back(InFlag);
2944 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2945 InFlag = Hi.getValue(1);
2946
2947 Ops.clear();
2948 Ops.push_back(Tmp3);
2949 Ops.push_back(Tmp1);
2950 Ops.push_back(CC);
2951 Ops.push_back(InFlag);
2952 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2953 } else {
2954 Ops.push_back(Tmp2);
2955 Ops.push_back(Tmp3);
2956 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00002957 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00002958 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2959 InFlag = Lo.getValue(1);
2960
2961 Ops.clear();
2962 Ops.push_back(Tmp3);
2963 Ops.push_back(Tmp1);
2964 Ops.push_back(CC);
2965 Ops.push_back(InFlag);
2966 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2967 }
2968
2969 Tys.clear();
2970 Tys.push_back(MVT::i32);
2971 Tys.push_back(MVT::i32);
2972 Ops.clear();
2973 Ops.push_back(Lo);
2974 Ops.push_back(Hi);
2975 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Chenga9467aa2006-04-25 20:13:52 +00002976}
Evan Cheng6305e502006-01-12 22:54:21 +00002977
Evan Chenga9467aa2006-04-25 20:13:52 +00002978SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2979 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2980 Op.getOperand(0).getValueType() >= MVT::i16 &&
2981 "Unknown SINT_TO_FP to lower!");
2982
2983 SDOperand Result;
2984 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2985 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2986 MachineFunction &MF = DAG.getMachineFunction();
2987 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2988 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2989 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2990 DAG.getEntryNode(), Op.getOperand(0),
2991 StackSlot, DAG.getSrcValue(NULL));
2992
2993 // Build the FILD
2994 std::vector<MVT::ValueType> Tys;
2995 Tys.push_back(MVT::f64);
2996 Tys.push_back(MVT::Other);
2997 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2998 std::vector<SDOperand> Ops;
2999 Ops.push_back(Chain);
3000 Ops.push_back(StackSlot);
3001 Ops.push_back(DAG.getValueType(SrcVT));
3002 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3003 Tys, Ops);
3004
3005 if (X86ScalarSSE) {
3006 Chain = Result.getValue(1);
3007 SDOperand InFlag = Result.getValue(2);
3008
3009 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3010 // shouldn't be necessary except that RFP cannot be live across
3011 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003012 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003013 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003014 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00003015 std::vector<MVT::ValueType> Tys;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003016 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003017 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003018 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003019 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003020 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003021 Ops.push_back(DAG.getValueType(Op.getValueType()));
3022 Ops.push_back(InFlag);
3023 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3024 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3025 DAG.getSrcValue(NULL));
Chris Lattner76ac0682005-11-15 00:40:23 +00003026 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003027
Evan Chenga9467aa2006-04-25 20:13:52 +00003028 return Result;
3029}
3030
3031SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3032 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3033 "Unknown FP_TO_SINT to lower!");
3034 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3035 // stack slot.
3036 MachineFunction &MF = DAG.getMachineFunction();
3037 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3038 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3039 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3040
3041 unsigned Opc;
3042 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003043 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3044 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3045 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3046 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003047 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003048
Evan Chenga9467aa2006-04-25 20:13:52 +00003049 SDOperand Chain = DAG.getEntryNode();
3050 SDOperand Value = Op.getOperand(0);
3051 if (X86ScalarSSE) {
3052 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3053 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3054 DAG.getSrcValue(0));
3055 std::vector<MVT::ValueType> Tys;
3056 Tys.push_back(MVT::f64);
3057 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003058 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003059 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00003060 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003061 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3062 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3063 Chain = Value.getValue(1);
3064 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3065 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3066 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003067
Evan Chenga9467aa2006-04-25 20:13:52 +00003068 // Build the FP_TO_INT*_IN_MEM
3069 std::vector<SDOperand> Ops;
3070 Ops.push_back(Chain);
3071 Ops.push_back(Value);
3072 Ops.push_back(StackSlot);
3073 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Cheng172fce72006-01-06 00:43:03 +00003074
Evan Chenga9467aa2006-04-25 20:13:52 +00003075 // Load the result.
3076 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3077 DAG.getSrcValue(NULL));
3078}
3079
3080SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3081 MVT::ValueType VT = Op.getValueType();
3082 const Type *OpNTy = MVT::getTypeForValueType(VT);
3083 std::vector<Constant*> CV;
3084 if (VT == MVT::f64) {
3085 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3086 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3087 } else {
3088 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3089 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3090 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3091 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3092 }
3093 Constant *CS = ConstantStruct::get(CV);
3094 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3095 SDOperand Mask
3096 = DAG.getNode(X86ISD::LOAD_PACK,
3097 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3098 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3099}
3100
3101SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3102 MVT::ValueType VT = Op.getValueType();
3103 const Type *OpNTy = MVT::getTypeForValueType(VT);
3104 std::vector<Constant*> CV;
3105 if (VT == MVT::f64) {
3106 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3107 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3108 } else {
3109 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3111 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3112 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3113 }
3114 Constant *CS = ConstantStruct::get(CV);
3115 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3116 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3117 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3118 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3119}
3120
3121SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3122 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3123 SDOperand Cond;
3124 SDOperand CC = Op.getOperand(2);
3125 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3126 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3127 bool Flip;
3128 unsigned X86CC;
3129 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3130 if (Flip)
3131 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3132 Op.getOperand(1), Op.getOperand(0));
3133 else
Evan Cheng45df7f82006-01-30 23:41:35 +00003134 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3135 Op.getOperand(0), Op.getOperand(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003136 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3137 DAG.getConstant(X86CC, MVT::i8), Cond);
3138 } else {
3139 assert(isFP && "Illegal integer SetCC!");
3140
3141 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3142 Op.getOperand(0), Op.getOperand(1));
3143 std::vector<MVT::ValueType> Tys;
3144 std::vector<SDOperand> Ops;
3145 switch (SetCCOpcode) {
Evan Cheng172fce72006-01-06 00:43:03 +00003146 default: assert(false && "Illegal floating point SetCC!");
3147 case ISD::SETOEQ: { // !PF & ZF
3148 Tys.push_back(MVT::i8);
3149 Tys.push_back(MVT::Flag);
3150 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3151 Ops.push_back(Cond);
3152 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3153 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3154 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3155 Tmp1.getValue(1));
3156 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3157 }
Evan Cheng172fce72006-01-06 00:43:03 +00003158 case ISD::SETUNE: { // PF | !ZF
3159 Tys.push_back(MVT::i8);
3160 Tys.push_back(MVT::Flag);
3161 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3162 Ops.push_back(Cond);
3163 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3164 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3165 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3166 Tmp1.getValue(1));
3167 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3168 }
Evan Cheng172fce72006-01-06 00:43:03 +00003169 }
Evan Chengc1583db2005-12-21 20:21:51 +00003170 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003171}
Evan Cheng45df7f82006-01-30 23:41:35 +00003172
Evan Chenga9467aa2006-04-25 20:13:52 +00003173SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3174 MVT::ValueType VT = Op.getValueType();
3175 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3176 bool addTest = false;
3177 SDOperand Op0 = Op.getOperand(0);
3178 SDOperand Cond, CC;
3179 if (Op0.getOpcode() == ISD::SETCC)
3180 Op0 = LowerOperation(Op0, DAG);
Evan Cheng944d1e92006-01-26 02:13:10 +00003181
Evan Chenga9467aa2006-04-25 20:13:52 +00003182 if (Op0.getOpcode() == X86ISD::SETCC) {
3183 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3184 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3185 // have another use it will be eliminated.
3186 // If the X86ISD::SETCC has more than one use, then it's probably better
3187 // to use a test instead of duplicating the X86ISD::CMP (for register
3188 // pressure reason).
3189 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3190 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3191 CmpOpc == X86ISD::UCOMI) {
3192 if (!Op0.hasOneUse()) {
3193 std::vector<MVT::ValueType> Tys;
3194 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3195 Tys.push_back(Op0.Val->getValueType(i));
3196 std::vector<SDOperand> Ops;
3197 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3198 Ops.push_back(Op0.getOperand(i));
3199 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3200 }
3201
3202 CC = Op0.getOperand(0);
3203 Cond = Op0.getOperand(1);
3204 // Make a copy as flag result cannot be used by more than one.
3205 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3206 Cond.getOperand(0), Cond.getOperand(1));
3207 addTest =
3208 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00003209 } else
3210 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003211 } else
3212 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00003213
Evan Chenga9467aa2006-04-25 20:13:52 +00003214 if (addTest) {
3215 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3216 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00003217 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003218
Evan Chenga9467aa2006-04-25 20:13:52 +00003219 std::vector<MVT::ValueType> Tys;
3220 Tys.push_back(Op.getValueType());
3221 Tys.push_back(MVT::Flag);
3222 std::vector<SDOperand> Ops;
3223 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3224 // condition is true.
3225 Ops.push_back(Op.getOperand(2));
3226 Ops.push_back(Op.getOperand(1));
3227 Ops.push_back(CC);
3228 Ops.push_back(Cond);
3229 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3230}
Evan Cheng944d1e92006-01-26 02:13:10 +00003231
Evan Chenga9467aa2006-04-25 20:13:52 +00003232SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3233 bool addTest = false;
3234 SDOperand Cond = Op.getOperand(1);
3235 SDOperand Dest = Op.getOperand(2);
3236 SDOperand CC;
3237 if (Cond.getOpcode() == ISD::SETCC)
3238 Cond = LowerOperation(Cond, DAG);
3239
3240 if (Cond.getOpcode() == X86ISD::SETCC) {
3241 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3242 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3243 // have another use it will be eliminated.
3244 // If the X86ISD::SETCC has more than one use, then it's probably better
3245 // to use a test instead of duplicating the X86ISD::CMP (for register
3246 // pressure reason).
3247 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3248 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3249 CmpOpc == X86ISD::UCOMI) {
3250 if (!Cond.hasOneUse()) {
3251 std::vector<MVT::ValueType> Tys;
3252 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3253 Tys.push_back(Cond.Val->getValueType(i));
3254 std::vector<SDOperand> Ops;
3255 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3256 Ops.push_back(Cond.getOperand(i));
3257 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3258 }
3259
3260 CC = Cond.getOperand(0);
3261 Cond = Cond.getOperand(1);
3262 // Make a copy as flag result cannot be used by more than one.
3263 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3264 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00003265 } else
3266 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003267 } else
3268 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00003269
Evan Chenga9467aa2006-04-25 20:13:52 +00003270 if (addTest) {
3271 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3272 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng6fc31042005-12-19 23:12:38 +00003273 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003274 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3275 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3276}
Evan Chengae986f12006-01-11 22:15:48 +00003277
Evan Chenga9467aa2006-04-25 20:13:52 +00003278SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3279 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3280 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3281 DAG.getTargetJumpTable(JT->getIndex(),
3282 getPointerTy()));
3283 if (Subtarget->isTargetDarwin()) {
3284 // With PIC, the address is actually $g + Offset.
3285 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3286 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003287 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3288 Result);
Evan Chengae986f12006-01-11 22:15:48 +00003289 }
Evan Cheng99470012006-02-25 09:55:19 +00003290
Evan Chenga9467aa2006-04-25 20:13:52 +00003291 return Result;
3292}
Evan Cheng5588de92006-02-18 00:15:05 +00003293
Evan Cheng2a330942006-05-25 00:59:30 +00003294SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3295 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3296 if (CallingConv == CallingConv::Fast && EnableFastCC)
3297 return LowerFastCCCallTo(Op, DAG);
3298 else
3299 return LowerCCCCallTo(Op, DAG);
3300}
3301
Evan Chenga9467aa2006-04-25 20:13:52 +00003302SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3303 SDOperand Copy;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003304
Evan Chenga9467aa2006-04-25 20:13:52 +00003305 switch(Op.getNumOperands()) {
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003306 default:
3307 assert(0 && "Do not know how to return this many arguments!");
3308 abort();
Chris Lattnerc070c622006-04-17 20:32:50 +00003309 case 1: // ret void.
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003310 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chenga9467aa2006-04-25 20:13:52 +00003311 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003312 case 2: {
3313 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerc070c622006-04-17 20:32:50 +00003314
3315 if (MVT::isVector(ArgVT)) {
3316 // Integer or FP vector result -> XMM0.
3317 if (DAG.getMachineFunction().liveout_empty())
3318 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3319 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3320 SDOperand());
3321 } else if (MVT::isInteger(ArgVT)) {
3322 // Integer result -> EAX
3323 if (DAG.getMachineFunction().liveout_empty())
3324 DAG.getMachineFunction().addLiveOut(X86::EAX);
3325
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003326 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3327 SDOperand());
Chris Lattnerc070c622006-04-17 20:32:50 +00003328 } else if (!X86ScalarSSE) {
3329 // FP return with fp-stack value.
3330 if (DAG.getMachineFunction().liveout_empty())
3331 DAG.getMachineFunction().addLiveOut(X86::ST0);
3332
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003333 std::vector<MVT::ValueType> Tys;
3334 Tys.push_back(MVT::Other);
3335 Tys.push_back(MVT::Flag);
3336 std::vector<SDOperand> Ops;
3337 Ops.push_back(Op.getOperand(0));
3338 Ops.push_back(Op.getOperand(1));
3339 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3340 } else {
Chris Lattnerc070c622006-04-17 20:32:50 +00003341 // FP return with ScalarSSE (return on fp-stack).
3342 if (DAG.getMachineFunction().liveout_empty())
3343 DAG.getMachineFunction().addLiveOut(X86::ST0);
3344
Evan Chenge1ce4d72006-02-01 00:20:21 +00003345 SDOperand MemLoc;
3346 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00003347 SDOperand Value = Op.getOperand(1);
3348
Evan Chenga24617f2006-02-01 01:19:32 +00003349 if (Value.getOpcode() == ISD::LOAD &&
3350 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00003351 Chain = Value.getOperand(0);
3352 MemLoc = Value.getOperand(1);
3353 } else {
3354 // Spill the value to memory and reload it into top of stack.
3355 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3356 MachineFunction &MF = DAG.getMachineFunction();
3357 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3358 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3359 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3360 Value, MemLoc, DAG.getSrcValue(0));
3361 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003362 std::vector<MVT::ValueType> Tys;
3363 Tys.push_back(MVT::f64);
3364 Tys.push_back(MVT::Other);
3365 std::vector<SDOperand> Ops;
3366 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00003367 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003368 Ops.push_back(DAG.getValueType(ArgVT));
3369 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3370 Tys.clear();
3371 Tys.push_back(MVT::Other);
3372 Tys.push_back(MVT::Flag);
3373 Ops.clear();
3374 Ops.push_back(Copy.getValue(1));
3375 Ops.push_back(Copy);
3376 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3377 }
3378 break;
3379 }
3380 case 3:
Chris Lattnerc070c622006-04-17 20:32:50 +00003381 if (DAG.getMachineFunction().liveout_empty()) {
3382 DAG.getMachineFunction().addLiveOut(X86::EAX);
3383 DAG.getMachineFunction().addLiveOut(X86::EDX);
3384 }
3385
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003386 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3387 SDOperand());
3388 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3389 break;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003390 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003391 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3392 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3393 Copy.getValue(1));
3394}
3395
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003396SDOperand
3397X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng17e734f2006-05-23 21:06:34 +00003398 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3399 if (CC == CallingConv::Fast && EnableFastCC)
3400 return LowerFastCCArguments(Op, DAG);
3401 else
3402 return LowerCCCArguments(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003403}
3404
Evan Chenga9467aa2006-04-25 20:13:52 +00003405SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3406 SDOperand InFlag(0, 0);
3407 SDOperand Chain = Op.getOperand(0);
3408 unsigned Align =
3409 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3410 if (Align == 0) Align = 1;
3411
3412 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3413 // If not DWORD aligned, call memset if size is less than the threshold.
3414 // It knows how to align to the right boundary first.
3415 if ((Align & 3) != 0 ||
3416 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3417 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003418 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003419 std::vector<std::pair<SDOperand, const Type*> > Args;
3420 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3421 // Extend the ubyte argument to be an int value for the call.
3422 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3423 Args.push_back(std::make_pair(Val, IntPtrTy));
3424 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3425 std::pair<SDOperand,SDOperand> CallResult =
3426 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3427 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3428 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003429 }
Evan Chengd097e672006-03-22 02:53:00 +00003430
Evan Chenga9467aa2006-04-25 20:13:52 +00003431 MVT::ValueType AVT;
3432 SDOperand Count;
3433 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3434 unsigned BytesLeft = 0;
3435 bool TwoRepStos = false;
3436 if (ValC) {
3437 unsigned ValReg;
3438 unsigned Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003439
Evan Chenga9467aa2006-04-25 20:13:52 +00003440 // If the value is a constant, then we can potentially use larger sets.
3441 switch (Align & 3) {
3442 case 2: // WORD aligned
3443 AVT = MVT::i16;
3444 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3445 BytesLeft = I->getValue() % 2;
3446 Val = (Val << 8) | Val;
3447 ValReg = X86::AX;
3448 break;
3449 case 0: // DWORD aligned
3450 AVT = MVT::i32;
3451 if (I) {
3452 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3453 BytesLeft = I->getValue() % 4;
Evan Chenga3caaee2006-04-19 22:48:17 +00003454 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003455 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3456 DAG.getConstant(2, MVT::i8));
3457 TwoRepStos = true;
Evan Chenga3caaee2006-04-19 22:48:17 +00003458 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003459 Val = (Val << 8) | Val;
3460 Val = (Val << 16) | Val;
3461 ValReg = X86::EAX;
3462 break;
3463 default: // Byte aligned
3464 AVT = MVT::i8;
3465 Count = Op.getOperand(3);
3466 ValReg = X86::AL;
3467 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003468 }
3469
Evan Chenga9467aa2006-04-25 20:13:52 +00003470 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3471 InFlag);
3472 InFlag = Chain.getValue(1);
3473 } else {
3474 AVT = MVT::i8;
3475 Count = Op.getOperand(3);
3476 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3477 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003478 }
Evan Chengb0461082006-04-24 18:01:45 +00003479
Evan Chenga9467aa2006-04-25 20:13:52 +00003480 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3481 InFlag = Chain.getValue(1);
3482 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3483 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003484
Evan Chenga9467aa2006-04-25 20:13:52 +00003485 std::vector<MVT::ValueType> Tys;
3486 Tys.push_back(MVT::Other);
3487 Tys.push_back(MVT::Flag);
3488 std::vector<SDOperand> Ops;
3489 Ops.push_back(Chain);
3490 Ops.push_back(DAG.getValueType(AVT));
3491 Ops.push_back(InFlag);
3492 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengb0461082006-04-24 18:01:45 +00003493
Evan Chenga9467aa2006-04-25 20:13:52 +00003494 if (TwoRepStos) {
3495 InFlag = Chain.getValue(1);
3496 Count = Op.getOperand(3);
3497 MVT::ValueType CVT = Count.getValueType();
3498 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3499 DAG.getConstant(3, CVT));
3500 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3501 InFlag = Chain.getValue(1);
3502 Tys.clear();
3503 Tys.push_back(MVT::Other);
3504 Tys.push_back(MVT::Flag);
3505 Ops.clear();
3506 Ops.push_back(Chain);
3507 Ops.push_back(DAG.getValueType(MVT::i8));
3508 Ops.push_back(InFlag);
3509 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3510 } else if (BytesLeft) {
3511 // Issue stores for the last 1 - 3 bytes.
3512 SDOperand Value;
3513 unsigned Val = ValC->getValue() & 255;
3514 unsigned Offset = I->getValue() - BytesLeft;
3515 SDOperand DstAddr = Op.getOperand(1);
3516 MVT::ValueType AddrVT = DstAddr.getValueType();
3517 if (BytesLeft >= 2) {
3518 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3519 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3520 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3521 DAG.getConstant(Offset, AddrVT)),
3522 DAG.getSrcValue(NULL));
3523 BytesLeft -= 2;
3524 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003525 }
3526
Evan Chenga9467aa2006-04-25 20:13:52 +00003527 if (BytesLeft == 1) {
3528 Value = DAG.getConstant(Val, MVT::i8);
3529 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3530 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3531 DAG.getConstant(Offset, AddrVT)),
3532 DAG.getSrcValue(NULL));
Evan Cheng14215c32006-04-21 23:03:30 +00003533 }
Evan Cheng082c8782006-03-24 07:29:27 +00003534 }
Evan Chengebf10062006-04-03 20:53:28 +00003535
Evan Chenga9467aa2006-04-25 20:13:52 +00003536 return Chain;
3537}
Evan Chengebf10062006-04-03 20:53:28 +00003538
Evan Chenga9467aa2006-04-25 20:13:52 +00003539SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3540 SDOperand Chain = Op.getOperand(0);
3541 unsigned Align =
3542 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3543 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003544
Evan Chenga9467aa2006-04-25 20:13:52 +00003545 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3546 // If not DWORD aligned, call memcpy if size is less than the threshold.
3547 // It knows how to align to the right boundary first.
3548 if ((Align & 3) != 0 ||
3549 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3550 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003551 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003552 std::vector<std::pair<SDOperand, const Type*> > Args;
3553 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3554 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3555 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3556 std::pair<SDOperand,SDOperand> CallResult =
3557 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3558 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3559 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003560 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003561
3562 MVT::ValueType AVT;
3563 SDOperand Count;
3564 unsigned BytesLeft = 0;
3565 bool TwoRepMovs = false;
3566 switch (Align & 3) {
3567 case 2: // WORD aligned
3568 AVT = MVT::i16;
3569 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3570 BytesLeft = I->getValue() % 2;
3571 break;
3572 case 0: // DWORD aligned
3573 AVT = MVT::i32;
3574 if (I) {
3575 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3576 BytesLeft = I->getValue() % 4;
Evan Cheng54212062006-04-17 22:45:49 +00003577 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003578 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3579 DAG.getConstant(2, MVT::i8));
3580 TwoRepMovs = true;
Evan Cheng6e5e2052006-04-17 22:04:06 +00003581 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003582 break;
3583 default: // Byte aligned
3584 AVT = MVT::i8;
3585 Count = Op.getOperand(3);
3586 break;
3587 }
3588
3589 SDOperand InFlag(0, 0);
3590 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3591 InFlag = Chain.getValue(1);
3592 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3593 InFlag = Chain.getValue(1);
3594 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3595 InFlag = Chain.getValue(1);
3596
3597 std::vector<MVT::ValueType> Tys;
3598 Tys.push_back(MVT::Other);
3599 Tys.push_back(MVT::Flag);
3600 std::vector<SDOperand> Ops;
3601 Ops.push_back(Chain);
3602 Ops.push_back(DAG.getValueType(AVT));
3603 Ops.push_back(InFlag);
3604 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3605
3606 if (TwoRepMovs) {
3607 InFlag = Chain.getValue(1);
3608 Count = Op.getOperand(3);
3609 MVT::ValueType CVT = Count.getValueType();
3610 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3611 DAG.getConstant(3, CVT));
3612 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3613 InFlag = Chain.getValue(1);
3614 Tys.clear();
3615 Tys.push_back(MVT::Other);
3616 Tys.push_back(MVT::Flag);
3617 Ops.clear();
3618 Ops.push_back(Chain);
3619 Ops.push_back(DAG.getValueType(MVT::i8));
3620 Ops.push_back(InFlag);
3621 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3622 } else if (BytesLeft) {
3623 // Issue loads and stores for the last 1 - 3 bytes.
3624 unsigned Offset = I->getValue() - BytesLeft;
3625 SDOperand DstAddr = Op.getOperand(1);
3626 MVT::ValueType DstVT = DstAddr.getValueType();
3627 SDOperand SrcAddr = Op.getOperand(2);
3628 MVT::ValueType SrcVT = SrcAddr.getValueType();
3629 SDOperand Value;
3630 if (BytesLeft >= 2) {
3631 Value = DAG.getLoad(MVT::i16, Chain,
3632 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3633 DAG.getConstant(Offset, SrcVT)),
3634 DAG.getSrcValue(NULL));
3635 Chain = Value.getValue(1);
3636 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3637 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3638 DAG.getConstant(Offset, DstVT)),
3639 DAG.getSrcValue(NULL));
3640 BytesLeft -= 2;
3641 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003642 }
3643
Evan Chenga9467aa2006-04-25 20:13:52 +00003644 if (BytesLeft == 1) {
3645 Value = DAG.getLoad(MVT::i8, Chain,
3646 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3647 DAG.getConstant(Offset, SrcVT)),
3648 DAG.getSrcValue(NULL));
3649 Chain = Value.getValue(1);
3650 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3651 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3652 DAG.getConstant(Offset, DstVT)),
3653 DAG.getSrcValue(NULL));
3654 }
Evan Chengcbffa462006-03-31 19:22:53 +00003655 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003656
3657 return Chain;
3658}
3659
3660SDOperand
3661X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3662 std::vector<MVT::ValueType> Tys;
3663 Tys.push_back(MVT::Other);
3664 Tys.push_back(MVT::Flag);
3665 std::vector<SDOperand> Ops;
3666 Ops.push_back(Op.getOperand(0));
3667 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3668 Ops.clear();
3669 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3670 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3671 MVT::i32, Ops[0].getValue(2)));
3672 Ops.push_back(Ops[1].getValue(1));
3673 Tys[0] = Tys[1] = MVT::i32;
3674 Tys.push_back(MVT::Other);
3675 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3676}
3677
3678SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3679 // vastart just stores the address of the VarArgsFrameIndex slot into the
3680 // memory location argument.
3681 // FIXME: Replace MVT::i32 with PointerTy
3682 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3683 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3684 Op.getOperand(1), Op.getOperand(2));
3685}
3686
3687SDOperand
3688X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3689 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3690 switch (IntNo) {
3691 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003692 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003693 case Intrinsic::x86_sse_comieq_ss:
3694 case Intrinsic::x86_sse_comilt_ss:
3695 case Intrinsic::x86_sse_comile_ss:
3696 case Intrinsic::x86_sse_comigt_ss:
3697 case Intrinsic::x86_sse_comige_ss:
3698 case Intrinsic::x86_sse_comineq_ss:
3699 case Intrinsic::x86_sse_ucomieq_ss:
3700 case Intrinsic::x86_sse_ucomilt_ss:
3701 case Intrinsic::x86_sse_ucomile_ss:
3702 case Intrinsic::x86_sse_ucomigt_ss:
3703 case Intrinsic::x86_sse_ucomige_ss:
3704 case Intrinsic::x86_sse_ucomineq_ss:
3705 case Intrinsic::x86_sse2_comieq_sd:
3706 case Intrinsic::x86_sse2_comilt_sd:
3707 case Intrinsic::x86_sse2_comile_sd:
3708 case Intrinsic::x86_sse2_comigt_sd:
3709 case Intrinsic::x86_sse2_comige_sd:
3710 case Intrinsic::x86_sse2_comineq_sd:
3711 case Intrinsic::x86_sse2_ucomieq_sd:
3712 case Intrinsic::x86_sse2_ucomilt_sd:
3713 case Intrinsic::x86_sse2_ucomile_sd:
3714 case Intrinsic::x86_sse2_ucomigt_sd:
3715 case Intrinsic::x86_sse2_ucomige_sd:
3716 case Intrinsic::x86_sse2_ucomineq_sd: {
3717 unsigned Opc = 0;
3718 ISD::CondCode CC = ISD::SETCC_INVALID;
3719 switch (IntNo) {
3720 default: break;
3721 case Intrinsic::x86_sse_comieq_ss:
3722 case Intrinsic::x86_sse2_comieq_sd:
3723 Opc = X86ISD::COMI;
3724 CC = ISD::SETEQ;
3725 break;
Evan Cheng78038292006-04-05 23:38:46 +00003726 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003727 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003728 Opc = X86ISD::COMI;
3729 CC = ISD::SETLT;
3730 break;
3731 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003732 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003733 Opc = X86ISD::COMI;
3734 CC = ISD::SETLE;
3735 break;
3736 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003737 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003738 Opc = X86ISD::COMI;
3739 CC = ISD::SETGT;
3740 break;
3741 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003742 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003743 Opc = X86ISD::COMI;
3744 CC = ISD::SETGE;
3745 break;
3746 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003747 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003748 Opc = X86ISD::COMI;
3749 CC = ISD::SETNE;
3750 break;
3751 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003752 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003753 Opc = X86ISD::UCOMI;
3754 CC = ISD::SETEQ;
3755 break;
3756 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003757 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003758 Opc = X86ISD::UCOMI;
3759 CC = ISD::SETLT;
3760 break;
3761 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003762 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003763 Opc = X86ISD::UCOMI;
3764 CC = ISD::SETLE;
3765 break;
3766 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003767 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003768 Opc = X86ISD::UCOMI;
3769 CC = ISD::SETGT;
3770 break;
3771 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003772 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003773 Opc = X86ISD::UCOMI;
3774 CC = ISD::SETGE;
3775 break;
3776 case Intrinsic::x86_sse_ucomineq_ss:
3777 case Intrinsic::x86_sse2_ucomineq_sd:
3778 Opc = X86ISD::UCOMI;
3779 CC = ISD::SETNE;
3780 break;
Evan Cheng78038292006-04-05 23:38:46 +00003781 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003782 bool Flip;
3783 unsigned X86CC;
3784 translateX86CC(CC, true, X86CC, Flip);
3785 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3786 Op.getOperand(Flip?1:2));
3787 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3788 DAG.getConstant(X86CC, MVT::i8), Cond);
3789 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003790 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003791 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003792}
Evan Cheng6af02632005-12-20 06:22:03 +00003793
Evan Chenga9467aa2006-04-25 20:13:52 +00003794/// LowerOperation - Provide custom lowering hooks for some operations.
3795///
3796SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3797 switch (Op.getOpcode()) {
3798 default: assert(0 && "Should not custom lower this!");
3799 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3800 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3801 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3802 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3803 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3804 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3805 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3806 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3807 case ISD::SHL_PARTS:
3808 case ISD::SRA_PARTS:
3809 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3810 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3811 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3812 case ISD::FABS: return LowerFABS(Op, DAG);
3813 case ISD::FNEG: return LowerFNEG(Op, DAG);
3814 case ISD::SETCC: return LowerSETCC(Op, DAG);
3815 case ISD::SELECT: return LowerSELECT(Op, DAG);
3816 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3817 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00003818 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003819 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003820 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003821 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3822 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3823 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3824 case ISD::VASTART: return LowerVASTART(Op, DAG);
3825 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3826 }
3827}
3828
Evan Cheng6af02632005-12-20 06:22:03 +00003829const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3830 switch (Opcode) {
3831 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003832 case X86ISD::SHLD: return "X86ISD::SHLD";
3833 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003834 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003835 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003836 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003837 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003838 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3839 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3840 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003841 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003842 case X86ISD::FST: return "X86ISD::FST";
3843 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003844 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003845 case X86ISD::CALL: return "X86ISD::CALL";
3846 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3847 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3848 case X86ISD::CMP: return "X86ISD::CMP";
3849 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003850 case X86ISD::COMI: return "X86ISD::COMI";
3851 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003852 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003853 case X86ISD::CMOV: return "X86ISD::CMOV";
3854 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003855 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003856 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3857 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003858 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003859 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00003860 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00003861 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00003862 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00003863 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00003864 }
3865}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003866
Nate Begeman8a77efe2006-02-16 21:11:51 +00003867void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3868 uint64_t Mask,
3869 uint64_t &KnownZero,
3870 uint64_t &KnownOne,
3871 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003872 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00003873 assert((Opc >= ISD::BUILTIN_OP_END ||
3874 Opc == ISD::INTRINSIC_WO_CHAIN ||
3875 Opc == ISD::INTRINSIC_W_CHAIN ||
3876 Opc == ISD::INTRINSIC_VOID) &&
3877 "Should use MaskedValueIsZero if you don't know whether Op"
3878 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003879
Evan Cheng6d196db2006-04-05 06:11:20 +00003880 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003881 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00003882 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00003883 case X86ISD::SETCC:
3884 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3885 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003886 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003887}
Chris Lattnerc642aa52006-01-31 19:43:35 +00003888
3889std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00003890getRegClassForInlineAsmConstraint(const std::string &Constraint,
3891 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00003892 if (Constraint.size() == 1) {
3893 // FIXME: not handling fp-stack yet!
3894 // FIXME: not handling MMX registers yet ('y' constraint).
3895 switch (Constraint[0]) { // GCC X86 Constraint Letters
3896 default: break; // Unknown constriant letter
3897 case 'r': // GENERAL_REGS
3898 case 'R': // LEGACY_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003899 if (VT == MVT::i32)
3900 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3901 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3902 else if (VT == MVT::i16)
3903 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3904 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3905 else if (VT == MVT::i8)
3906 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3907 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003908 case 'l': // INDEX_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003909 if (VT == MVT::i32)
3910 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3911 X86::ESI, X86::EDI, X86::EBP, 0);
3912 else if (VT == MVT::i16)
3913 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3914 X86::SI, X86::DI, X86::BP, 0);
3915 else if (VT == MVT::i8)
3916 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3917 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003918 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3919 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00003920 if (VT == MVT::i32)
3921 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3922 else if (VT == MVT::i16)
3923 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3924 else if (VT == MVT::i8)
3925 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3926 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00003927 case 'x': // SSE_REGS if SSE1 allowed
3928 if (Subtarget->hasSSE1())
3929 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3930 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3931 0);
3932 return std::vector<unsigned>();
3933 case 'Y': // SSE_REGS if SSE2 allowed
3934 if (Subtarget->hasSSE2())
3935 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3936 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3937 0);
3938 return std::vector<unsigned>();
3939 }
3940 }
3941
Chris Lattner7ad77df2006-02-22 00:56:39 +00003942 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00003943}
Evan Chengaf598d22006-03-13 23:18:16 +00003944
3945/// isLegalAddressImmediate - Return true if the integer value or
3946/// GlobalValue can be used as the offset of the target addressing mode.
3947bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3948 // X86 allows a sign-extended 32-bit immediate field.
3949 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3950}
3951
3952bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00003953 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00003954 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3955 if (RModel == Reloc::Static)
3956 return true;
3957 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00003958 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00003959 else
3960 return false;
3961 } else
3962 return true;
3963}
Evan Cheng68ad48b2006-03-22 18:59:22 +00003964
3965/// isShuffleMaskLegal - Targets can use this to indicate that they only
3966/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3967/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3968/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00003969bool
3970X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3971 // Only do shuffles on 128-bit vector types for now.
3972 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Chenga3caaee2006-04-19 22:48:17 +00003973 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng5022b342006-04-17 20:43:08 +00003974 isSplatMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00003975 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00003976 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00003977 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00003978 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00003979}
Evan Cheng60f0b892006-04-20 08:58:49 +00003980
3981bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3982 MVT::ValueType EVT,
3983 SelectionDAG &DAG) const {
3984 unsigned NumElts = BVOps.size();
3985 // Only do shuffles on 128-bit vector types for now.
3986 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3987 if (NumElts == 2) return true;
3988 if (NumElts == 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00003989 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng60f0b892006-04-20 08:58:49 +00003990 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3991 }
3992 return false;
3993}