blob: 1317510740b7f92dd60fd68ffe2a5e606c59e32a [file] [log] [blame]
Chris Lattner76ac0682005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000020#include "llvm/Constants.h"
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
361std::vector<SDOperand>
362X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000363 std::vector<SDOperand> Args = TargetLowering::LowerArguments(F, DAG);
364
365 FormalArgs.clear();
Evan Cheng48940d12006-04-27 01:32:22 +0000366 FormalArgLocs.clear();
367
Chris Lattner3d826992006-05-16 06:45:34 +0000368 // This sets BytesToPopOnReturn, BytesCallerReserves, etc. which have to be
369 // set before the rest of the function can be lowered.
Chris Lattner76ac0682005-11-15 00:40:23 +0000370 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
Evan Cheng48940d12006-04-27 01:32:22 +0000371 PreprocessFastCCArguments(Args, F, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000372 else
Evan Cheng48940d12006-04-27 01:32:22 +0000373 PreprocessCCCArguments(Args, F, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000374 return Args;
Chris Lattner76ac0682005-11-15 00:40:23 +0000375}
376
377std::pair<SDOperand, SDOperand>
378X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
379 bool isVarArg, unsigned CallingConv,
380 bool isTailCall,
381 SDOperand Callee, ArgListTy &Args,
382 SelectionDAG &DAG) {
Chris Lattner01dd6df2006-05-19 21:34:04 +0000383 assert((!isVarArg || CallingConv == CallingConv::C ||
384 CallingConv == CallingConv::CSRet) &&
385 "Only CCC/CSRet takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000386
387 // If the callee is a GlobalAddress node (quite common, every direct call is)
388 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
389 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
390 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000391 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
392 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000393
Chris Lattner76ac0682005-11-15 00:40:23 +0000394 if (CallingConv == CallingConv::Fast && EnableFastCC)
395 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
Chris Lattner8be5be82006-05-23 18:50:38 +0000396 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, CallingConv,
397 Callee, Args, DAG);
Chris Lattner76ac0682005-11-15 00:40:23 +0000398}
399
400//===----------------------------------------------------------------------===//
401// C Calling Convention implementation
402//===----------------------------------------------------------------------===//
403
Evan Cheng24eb3f42006-04-27 05:35:28 +0000404/// AddLiveIn - This helper function adds the specified physical register to the
405/// MachineFunction as a live in value. It also creates a corresponding virtual
406/// register for it.
407static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
408 TargetRegisterClass *RC) {
409 assert(RC->contains(PReg) && "Not the correct regclass!");
410 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
411 MF.addLiveIn(PReg, VReg);
412 return VReg;
413}
414
Evan Cheng89001ad2006-04-27 08:31:10 +0000415/// HowToPassCCCArgument - Returns how an formal argument of the specified type
416/// should be passed. If it is through stack, returns the size of the stack
417/// frame; if it is through XMM register, returns the number of XMM registers
418/// are needed.
419static void
420HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
421 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000422 switch (ObjectVT) {
423 default: assert(0 && "Unhandled argument type!");
424 case MVT::i1:
425 case MVT::i8: ObjSize = 1; break;
426 case MVT::i16: ObjSize = 2; break;
427 case MVT::i32: ObjSize = 4; break;
428 case MVT::i64: ObjSize = 8; break;
429 case MVT::f32: ObjSize = 4; break;
430 case MVT::f64: ObjSize = 8; break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000431 case MVT::v16i8:
432 case MVT::v8i16:
433 case MVT::v4i32:
434 case MVT::v2i64:
435 case MVT::v4f32:
436 case MVT::v2f64:
437 if (NumXMMRegs < 3)
438 ObjXMMRegs = 1;
439 else
440 ObjSize = 16;
441 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000442 }
Evan Cheng48940d12006-04-27 01:32:22 +0000443}
444
Evan Cheng24eb3f42006-04-27 05:35:28 +0000445/// getFormalArgObjects - Returns itself if Op is a FORMAL_ARGUMENTS, otherwise
446/// returns the FORMAL_ARGUMENTS node(s) that made up parts of the node.
Evan Cheng48940d12006-04-27 01:32:22 +0000447static std::vector<SDOperand> getFormalArgObjects(SDOperand Op) {
448 unsigned Opc = Op.getOpcode();
449 std::vector<SDOperand> Objs;
450 if (Opc == ISD::TRUNCATE) {
451 Op = Op.getOperand(0);
452 assert(Op.getOpcode() == ISD::AssertSext ||
453 Op.getOpcode() == ISD::AssertZext);
454 Objs.push_back(Op.getOperand(0));
Evan Chengd43c5c62006-04-28 05:25:15 +0000455 } else if (Opc == ISD::FP_ROUND || Opc == ISD::VBIT_CONVERT) {
Evan Cheng48940d12006-04-27 01:32:22 +0000456 Objs.push_back(Op.getOperand(0));
457 } else if (Opc == ISD::BUILD_PAIR) {
458 Objs.push_back(Op.getOperand(0));
459 Objs.push_back(Op.getOperand(1));
460 } else {
461 Objs.push_back(Op);
462 }
463 return Objs;
464}
465
Evan Cheng8c6b2342006-05-17 19:07:40 +0000466void X86TargetLowering::PreprocessCCCArguments(std::vector<SDOperand> &Args,
Evan Cheng48940d12006-04-27 01:32:22 +0000467 Function &F, SelectionDAG &DAG) {
468 unsigned NumArgs = Args.size();
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000469 MachineFunction &MF = DAG.getMachineFunction();
470 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner76ac0682005-11-15 00:40:23 +0000471
Evan Cheng48940d12006-04-27 01:32:22 +0000472 // Add DAG nodes to load the arguments... On entry to a function on the X86,
473 // the stack frame looks like this:
474 //
475 // [ESP] -- return address
476 // [ESP + 4] -- first argument (leftmost lexically)
477 // [ESP + 8] -- second argument, if first argument is four bytes in size
478 // ...
479 //
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000480 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng89001ad2006-04-27 08:31:10 +0000481 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
482 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000483 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng48940d12006-04-27 01:32:22 +0000484 SDOperand Op = Args[i];
485 std::vector<SDOperand> Objs = getFormalArgObjects(Op);
486 for (std::vector<SDOperand>::iterator I = Objs.begin(), E = Objs.end();
487 I != E; ++I) {
488 SDOperand Obj = *I;
489 MVT::ValueType ObjectVT = Obj.getValueType();
490 unsigned ArgIncrement = 4;
Evan Cheng89001ad2006-04-27 08:31:10 +0000491 unsigned ObjSize = 0;
492 unsigned ObjXMMRegs = 0;
493 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
494 if (ObjSize >= 8)
495 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000496
Evan Cheng89001ad2006-04-27 08:31:10 +0000497 if (ObjXMMRegs) {
498 // Passed in a XMM register.
499 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
500 X86::VR128RegisterClass);
501 std::pair<FALocInfo, FALocInfo> Loc =
502 std::make_pair(FALocInfo(FALocInfo::LiveInRegLoc, Reg, ObjectVT),
503 FALocInfo());
504 FormalArgLocs.push_back(Loc);
505 NumXMMRegs += ObjXMMRegs;
506 } else {
507 // Create the frame index object for this incoming parameter...
508 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
509 std::pair<FALocInfo, FALocInfo> Loc =
510 std::make_pair(FALocInfo(FALocInfo::StackFrameLoc, FI), FALocInfo());
511 FormalArgLocs.push_back(Loc);
512 ArgOffset += ArgIncrement; // Move on to the next argument...
513 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000514 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000515 }
516
517 // If the function takes variable number of arguments, make a frame index for
518 // the start of the first vararg value... for expansion of llvm.va_start.
519 if (F.isVarArg())
520 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
521 ReturnAddrIndex = 0; // No return address slot generated yet.
522 BytesToPopOnReturn = 0; // Callee pops nothing.
523 BytesCallerReserves = ArgOffset;
Chris Lattner8be5be82006-05-23 18:50:38 +0000524
525 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
526 // pointer.
527 if (F.getCallingConv() == CallingConv::CSRet &&
528 Subtarget->isTargetDarwin())
529 BytesToPopOnReturn = 4;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000530}
531
532void X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3d826992006-05-16 06:45:34 +0000533 unsigned NumArgs = Op.Val->getNumValues() - 1;
Chris Lattner76ac0682005-11-15 00:40:23 +0000534 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner76ac0682005-11-15 00:40:23 +0000535
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000536 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng89001ad2006-04-27 08:31:10 +0000537 std::pair<FALocInfo, FALocInfo> Loc = FormalArgLocs[i];
538 SDOperand ArgValue;
539 if (Loc.first.Kind == FALocInfo::StackFrameLoc) {
Chris Lattner3d826992006-05-16 06:45:34 +0000540 // Create the SelectionDAG nodes corresponding to a load from this
541 // parameter.
Evan Cheng89001ad2006-04-27 08:31:10 +0000542 unsigned FI = FormalArgLocs[i].first.Loc;
543 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
544 ArgValue = DAG.getLoad(Op.Val->getValueType(i),
545 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
546 } else {
547 // Must be a CopyFromReg
548 ArgValue= DAG.getCopyFromReg(DAG.getEntryNode(), Loc.first.Loc,
549 Loc.first.Typ);
550 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000551 FormalArgs.push_back(ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000552 }
Chris Lattner7b8b8bb2006-05-16 17:08:35 +0000553 // Provide a chain. Note that this isn't the right one, but it works as well
554 // as before.
555 FormalArgs.push_back(DAG.getEntryNode());
Chris Lattner76ac0682005-11-15 00:40:23 +0000556}
557
558std::pair<SDOperand, SDOperand>
559X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
560 bool isVarArg, bool isTailCall,
Chris Lattner8be5be82006-05-23 18:50:38 +0000561 unsigned CallingConv,
Chris Lattner76ac0682005-11-15 00:40:23 +0000562 SDOperand Callee, ArgListTy &Args,
563 SelectionDAG &DAG) {
564 // Count how many bytes are to be pushed on the stack.
565 unsigned NumBytes = 0;
566
Evan Cheng88decde2006-04-28 21:29:37 +0000567 // Keep track of the number of XMM regs passed so far.
568 unsigned NumXMMRegs = 0;
569 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
570
571 std::vector<SDOperand> RegValuesToPass;
Chris Lattner76ac0682005-11-15 00:40:23 +0000572 if (Args.empty()) {
573 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000574 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000575 } else {
576 for (unsigned i = 0, e = Args.size(); i != e; ++i)
577 switch (getValueType(Args[i].second)) {
578 default: assert(0 && "Unknown value type!");
579 case MVT::i1:
580 case MVT::i8:
581 case MVT::i16:
582 case MVT::i32:
583 case MVT::f32:
584 NumBytes += 4;
585 break;
586 case MVT::i64:
587 case MVT::f64:
588 NumBytes += 8;
589 break;
Evan Cheng88decde2006-04-28 21:29:37 +0000590 case MVT::Vector:
591 if (NumXMMRegs < 3)
592 ++NumXMMRegs;
593 else
594 NumBytes += 16;
595 break;
Chris Lattner76ac0682005-11-15 00:40:23 +0000596 }
597
Chris Lattner62c34842006-02-13 09:00:43 +0000598 Chain = DAG.getCALLSEQ_START(Chain,
599 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000600
601 // Arguments go on the stack in reverse order, as specified by the ABI.
602 unsigned ArgOffset = 0;
Evan Cheng88decde2006-04-28 21:29:37 +0000603 NumXMMRegs = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000604 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000605 std::vector<SDOperand> Stores;
Chris Lattner76ac0682005-11-15 00:40:23 +0000606 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000607 switch (getValueType(Args[i].second)) {
608 default: assert(0 && "Unexpected ValueType for argument!");
609 case MVT::i1:
610 case MVT::i8:
611 case MVT::i16:
612 // Promote the integer to 32 bits. If the input type is signed use a
613 // sign extend, otherwise use a zero extend.
614 if (Args[i].second->isSigned())
615 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
616 else
617 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
618
619 // FALL THROUGH
620 case MVT::i32:
Evan Cheng88decde2006-04-28 21:29:37 +0000621 case MVT::f32: {
622 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
623 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner76ac0682005-11-15 00:40:23 +0000624 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
625 Args[i].first, PtrOff,
626 DAG.getSrcValue(NULL)));
627 ArgOffset += 4;
628 break;
Evan Cheng88decde2006-04-28 21:29:37 +0000629 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000630 case MVT::i64:
Evan Cheng88decde2006-04-28 21:29:37 +0000631 case MVT::f64: {
632 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
633 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner76ac0682005-11-15 00:40:23 +0000634 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
635 Args[i].first, PtrOff,
636 DAG.getSrcValue(NULL)));
637 ArgOffset += 8;
638 break;
639 }
Evan Cheng88decde2006-04-28 21:29:37 +0000640 case MVT::Vector:
641 if (NumXMMRegs < 3) {
642 RegValuesToPass.push_back(Args[i].first);
643 NumXMMRegs++;
644 } else {
645 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
646 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
647 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
648 Args[i].first, PtrOff,
649 DAG.getSrcValue(NULL)));
650 ArgOffset += 16;
651 }
652 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000653 }
Evan Cheng88decde2006-04-28 21:29:37 +0000654 if (!Stores.empty())
Chris Lattner76ac0682005-11-15 00:40:23 +0000655 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
656 }
657
658 std::vector<MVT::ValueType> RetVals;
659 MVT::ValueType RetTyVT = getValueType(RetTy);
660 RetVals.push_back(MVT::Other);
661
662 // The result values produced have to be legal. Promote the result.
663 switch (RetTyVT) {
664 case MVT::isVoid: break;
665 default:
666 RetVals.push_back(RetTyVT);
667 break;
668 case MVT::i1:
669 case MVT::i8:
670 case MVT::i16:
671 RetVals.push_back(MVT::i32);
672 break;
673 case MVT::f32:
674 if (X86ScalarSSE)
675 RetVals.push_back(MVT::f32);
676 else
677 RetVals.push_back(MVT::f64);
678 break;
679 case MVT::i64:
680 RetVals.push_back(MVT::i32);
681 RetVals.push_back(MVT::i32);
682 break;
683 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000684
Evan Cheng88decde2006-04-28 21:29:37 +0000685 // Build a sequence of copy-to-reg nodes chained together with token chain
686 // and flag operands which copy the outgoing args into registers.
687 SDOperand InFlag;
688 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
689 unsigned CCReg = XMMArgRegs[i];
690 SDOperand RegToPass = RegValuesToPass[i];
691 assert(RegToPass.getValueType() == MVT::Vector);
Chris Lattner3d826992006-05-16 06:45:34 +0000692 unsigned NumElems =
693 cast<ConstantSDNode>(*(RegToPass.Val->op_end()-2))->getValue();
Evan Cheng88decde2006-04-28 21:29:37 +0000694 MVT::ValueType EVT = cast<VTSDNode>(*(RegToPass.Val->op_end()-1))->getVT();
695 MVT::ValueType PVT = getVectorType(EVT, NumElems);
696 SDOperand CCRegNode = DAG.getRegister(CCReg, PVT);
697 RegToPass = DAG.getNode(ISD::VBIT_CONVERT, PVT, RegToPass);
698 Chain = DAG.getCopyToReg(Chain, CCRegNode, RegToPass, InFlag);
699 InFlag = Chain.getValue(1);
700 }
701
Nate Begeman7e5496d2006-02-17 00:03:04 +0000702 std::vector<MVT::ValueType> NodeTys;
703 NodeTys.push_back(MVT::Other); // Returns a chain
704 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
705 std::vector<SDOperand> Ops;
706 Ops.push_back(Chain);
707 Ops.push_back(Callee);
Evan Cheng88decde2006-04-28 21:29:37 +0000708 if (InFlag.Val)
709 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000710
Nate Begeman7e5496d2006-02-17 00:03:04 +0000711 // FIXME: Do not generate X86ISD::TAILCALL for now.
712 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng88decde2006-04-28 21:29:37 +0000713 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000714
Chris Lattner8be5be82006-05-23 18:50:38 +0000715 // Create the CALLSEQ_END node.
716 unsigned NumBytesForCalleeToPush = 0;
717
718 // If this is is a call to a struct-return function on Darwin/X86, the callee
719 // pops the hidden struct pointer, so we have to push it back.
720 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
721 NumBytesForCalleeToPush = 4;
722
Nate Begeman7e5496d2006-02-17 00:03:04 +0000723 NodeTys.clear();
724 NodeTys.push_back(MVT::Other); // Returns a chain
725 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
726 Ops.clear();
727 Ops.push_back(Chain);
728 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000729 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000730 Ops.push_back(InFlag);
731 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
732 InFlag = Chain.getValue(1);
733
734 SDOperand RetVal;
735 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000736 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000737 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000738 case MVT::i1:
739 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000740 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
741 Chain = RetVal.getValue(1);
742 if (RetTyVT == MVT::i1)
743 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
744 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000745 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000746 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
747 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000748 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000749 case MVT::i32:
750 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
751 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000752 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000753 case MVT::i64: {
754 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
755 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
756 Lo.getValue(2));
757 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
758 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000759 break;
760 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000761 case MVT::f32:
762 case MVT::f64: {
763 std::vector<MVT::ValueType> Tys;
764 Tys.push_back(MVT::f64);
765 Tys.push_back(MVT::Other);
766 Tys.push_back(MVT::Flag);
767 std::vector<SDOperand> Ops;
768 Ops.push_back(Chain);
769 Ops.push_back(InFlag);
770 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
771 Chain = RetVal.getValue(1);
772 InFlag = RetVal.getValue(2);
773 if (X86ScalarSSE) {
774 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
775 // shouldn't be necessary except that RFP cannot be live across
776 // multiple blocks. When stackifier is fixed, they can be uncoupled.
777 MachineFunction &MF = DAG.getMachineFunction();
778 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
779 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
780 Tys.clear();
781 Tys.push_back(MVT::Other);
782 Ops.clear();
783 Ops.push_back(Chain);
784 Ops.push_back(RetVal);
785 Ops.push_back(StackSlot);
786 Ops.push_back(DAG.getValueType(RetTyVT));
787 Ops.push_back(InFlag);
788 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
789 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
790 DAG.getSrcValue(NULL));
791 Chain = RetVal.getValue(1);
792 }
Evan Cheng45e190982006-01-05 00:27:02 +0000793
Nate Begeman7e5496d2006-02-17 00:03:04 +0000794 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
795 // FIXME: we would really like to remember that this FP_ROUND
796 // operation is okay to eliminate if we allow excess FP precision.
797 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
798 break;
799 }
Evan Cheng88decde2006-04-28 21:29:37 +0000800 case MVT::Vector: {
801 const PackedType *PTy = cast<PackedType>(RetTy);
802 MVT::ValueType EVT;
803 MVT::ValueType LVT;
804 unsigned NumRegs = getPackedTypeBreakdown(PTy, EVT, LVT);
805 assert(NumRegs == 1 && "Unsupported type!");
806 RetVal = DAG.getCopyFromReg(Chain, X86::XMM0, EVT, InFlag);
807 Chain = RetVal.getValue(1);
808 break;
809 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000810 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000811 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000812
813 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000814}
815
Chris Lattner76ac0682005-11-15 00:40:23 +0000816//===----------------------------------------------------------------------===//
817// Fast Calling Convention implementation
818//===----------------------------------------------------------------------===//
819//
820// The X86 'fast' calling convention passes up to two integer arguments in
821// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
822// and requires that the callee pop its arguments off the stack (allowing proper
823// tail calls), and has the same return value conventions as C calling convs.
824//
825// This calling convention always arranges for the callee pop value to be 8n+4
826// bytes, which is needed for tail recursion elimination and stack alignment
827// reasons.
828//
829// Note that this can be enhanced in the future to pass fp vals in registers
830// (when we have a global fp allocator) and do other tricks.
831//
832
Chris Lattner388fc4d2006-03-17 17:27:47 +0000833// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
834// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
835// EDX". Anything more is illegal.
836//
837// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000838// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000839// a physreg with a virtreg, this increases the size of the physreg's live
840// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000841// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000842// allocator to wedge itself.
843//
844// This code triggers this problem more often if we pass args in registers,
845// so disable it until this is fixed.
846//
847// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
848// about code being dead.
849//
850static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000851
Chris Lattner76ac0682005-11-15 00:40:23 +0000852
Evan Cheng89001ad2006-04-27 08:31:10 +0000853/// HowToPassFastCCArgument - Returns how an formal argument of the specified
854/// type should be passed. If it is through stack, returns the size of the stack
855/// frame; if it is through integer or XMM register, returns the number of
856/// integer or XMM registers are needed.
Evan Cheng48940d12006-04-27 01:32:22 +0000857static void
Evan Cheng89001ad2006-04-27 08:31:10 +0000858HowToPassFastCCArgument(MVT::ValueType ObjectVT,
859 unsigned NumIntRegs, unsigned NumXMMRegs,
860 unsigned &ObjSize, unsigned &ObjIntRegs,
861 unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000862 ObjSize = 0;
863 NumIntRegs = 0;
864
865 switch (ObjectVT) {
866 default: assert(0 && "Unhandled argument type!");
867 case MVT::i1:
868 case MVT::i8:
869 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000870 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000871 else
872 ObjSize = 1;
873 break;
874 case MVT::i16:
875 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000876 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000877 else
878 ObjSize = 2;
879 break;
880 case MVT::i32:
881 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000882 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000883 else
884 ObjSize = 4;
885 break;
886 case MVT::i64:
887 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000888 ObjIntRegs = 2;
Evan Cheng48940d12006-04-27 01:32:22 +0000889 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000890 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000891 ObjSize = 4;
892 } else
893 ObjSize = 8;
894 case MVT::f32:
895 ObjSize = 4;
896 break;
897 case MVT::f64:
898 ObjSize = 8;
899 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000900 case MVT::v16i8:
901 case MVT::v8i16:
902 case MVT::v4i32:
903 case MVT::v2i64:
904 case MVT::v4f32:
905 case MVT::v2f64:
906 if (NumXMMRegs < 3)
907 ObjXMMRegs = 1;
908 else
909 ObjSize = 16;
910 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000911 }
912}
913
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000914void
Evan Cheng8c6b2342006-05-17 19:07:40 +0000915X86TargetLowering::PreprocessFastCCArguments(std::vector<SDOperand> &Args,
Evan Cheng48940d12006-04-27 01:32:22 +0000916 Function &F, SelectionDAG &DAG) {
917 unsigned NumArgs = Args.size();
Chris Lattner76ac0682005-11-15 00:40:23 +0000918 MachineFunction &MF = DAG.getMachineFunction();
919 MachineFrameInfo *MFI = MF.getFrameInfo();
920
Evan Cheng48940d12006-04-27 01:32:22 +0000921 // Add DAG nodes to load the arguments... On entry to a function the stack
922 // frame looks like this:
923 //
924 // [ESP] -- return address
925 // [ESP + 4] -- first nonreg argument (leftmost lexically)
926 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
927 // ...
Chris Lattner76ac0682005-11-15 00:40:23 +0000928 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
929
930 // Keep track of the number of integer regs passed so far. This can be either
931 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
932 // used).
933 unsigned NumIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000934 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
935 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Chris Lattner43798852006-03-17 05:10:20 +0000936
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000937 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng48940d12006-04-27 01:32:22 +0000938 SDOperand Op = Args[i];
939 std::vector<SDOperand> Objs = getFormalArgObjects(Op);
940 for (std::vector<SDOperand>::iterator I = Objs.begin(), E = Objs.end();
941 I != E; ++I) {
942 SDOperand Obj = *I;
943 MVT::ValueType ObjectVT = Obj.getValueType();
944 unsigned ArgIncrement = 4;
945 unsigned ObjSize = 0;
Evan Cheng24eb3f42006-04-27 05:35:28 +0000946 unsigned ObjIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000947 unsigned ObjXMMRegs = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000948
Evan Cheng89001ad2006-04-27 08:31:10 +0000949 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
950 ObjSize, ObjIntRegs, ObjXMMRegs);
951 if (ObjSize >= 8)
952 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000953
954 unsigned Reg;
955 std::pair<FALocInfo,FALocInfo> Loc = std::make_pair(FALocInfo(),
956 FALocInfo());
Evan Cheng24eb3f42006-04-27 05:35:28 +0000957 if (ObjIntRegs) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000958 switch (ObjectVT) {
959 default: assert(0 && "Unhandled argument type!");
960 case MVT::i1:
961 case MVT::i8:
962 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
Evan Cheng9fee4422006-05-16 07:21:53 +0000963 X86::GR8RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000964 Loc.first.Kind = FALocInfo::LiveInRegLoc;
965 Loc.first.Loc = Reg;
966 Loc.first.Typ = MVT::i8;
967 break;
968 case MVT::i16:
969 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
Evan Cheng9fee4422006-05-16 07:21:53 +0000970 X86::GR16RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000971 Loc.first.Kind = FALocInfo::LiveInRegLoc;
972 Loc.first.Loc = Reg;
973 Loc.first.Typ = MVT::i16;
974 break;
975 case MVT::i32:
976 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Evan Cheng9fee4422006-05-16 07:21:53 +0000977 X86::GR32RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000978 Loc.first.Kind = FALocInfo::LiveInRegLoc;
979 Loc.first.Loc = Reg;
980 Loc.first.Typ = MVT::i32;
981 break;
982 case MVT::i64:
983 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Evan Cheng9fee4422006-05-16 07:21:53 +0000984 X86::GR32RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000985 Loc.first.Kind = FALocInfo::LiveInRegLoc;
986 Loc.first.Loc = Reg;
987 Loc.first.Typ = MVT::i32;
988 if (ObjIntRegs == 2) {
Evan Cheng9fee4422006-05-16 07:21:53 +0000989 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000990 Loc.second.Kind = FALocInfo::LiveInRegLoc;
991 Loc.second.Loc = Reg;
992 Loc.second.Typ = MVT::i32;
993 }
994 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000995 case MVT::v16i8:
996 case MVT::v8i16:
997 case MVT::v4i32:
998 case MVT::v2i64:
999 case MVT::v4f32:
1000 case MVT::v2f64:
1001 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1002 Loc.first.Kind = FALocInfo::LiveInRegLoc;
1003 Loc.first.Loc = Reg;
1004 Loc.first.Typ = ObjectVT;
1005 break;
Evan Cheng24eb3f42006-04-27 05:35:28 +00001006 }
Evan Chenga0374e12006-04-27 05:44:50 +00001007 NumIntRegs += ObjIntRegs;
Evan Cheng89001ad2006-04-27 08:31:10 +00001008 NumXMMRegs += ObjXMMRegs;
Evan Cheng48940d12006-04-27 01:32:22 +00001009 }
1010 if (ObjSize) {
1011 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Evan Cheng24eb3f42006-04-27 05:35:28 +00001012 if (ObjectVT == MVT::i64 && ObjIntRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +00001013 Loc.second.Kind = FALocInfo::StackFrameLoc;
1014 Loc.second.Loc = FI;
1015 } else {
1016 Loc.first.Kind = FALocInfo::StackFrameLoc;
1017 Loc.first.Loc = FI;
1018 }
1019 ArgOffset += ArgIncrement; // Move on to the next argument.
Chris Lattner76ac0682005-11-15 00:40:23 +00001020 }
1021
Evan Cheng48940d12006-04-27 01:32:22 +00001022 FormalArgLocs.push_back(Loc);
Chris Lattner76ac0682005-11-15 00:40:23 +00001023 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001024 }
1025
1026 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1027 // arguments and the arguments after the retaddr has been pushed are aligned.
1028 if ((ArgOffset & 7) == 0)
1029 ArgOffset += 4;
1030
1031 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1032 ReturnAddrIndex = 0; // No return address slot generated yet.
1033 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1034 BytesCallerReserves = 0;
1035
1036 // Finally, inform the code generator which regs we return values in.
1037 switch (getValueType(F.getReturnType())) {
1038 default: assert(0 && "Unknown type!");
1039 case MVT::isVoid: break;
1040 case MVT::i1:
1041 case MVT::i8:
1042 case MVT::i16:
1043 case MVT::i32:
1044 MF.addLiveOut(X86::EAX);
1045 break;
1046 case MVT::i64:
1047 MF.addLiveOut(X86::EAX);
1048 MF.addLiveOut(X86::EDX);
1049 break;
1050 case MVT::f32:
1051 case MVT::f64:
1052 MF.addLiveOut(X86::ST0);
1053 break;
Evan Cheng88decde2006-04-28 21:29:37 +00001054 case MVT::Vector: {
1055 const PackedType *PTy = cast<PackedType>(F.getReturnType());
1056 MVT::ValueType EVT;
1057 MVT::ValueType LVT;
1058 unsigned NumRegs = getPackedTypeBreakdown(PTy, EVT, LVT);
1059 assert(NumRegs == 1 && "Unsupported type!");
1060 MF.addLiveOut(X86::XMM0);
1061 break;
1062 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001063 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001064}
Evan Cheng88decde2006-04-28 21:29:37 +00001065
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001066void
1067X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3d826992006-05-16 06:45:34 +00001068 unsigned NumArgs = Op.Val->getNumValues()-1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001069 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001070
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001071 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng48940d12006-04-27 01:32:22 +00001072 MVT::ValueType VT = Op.Val->getValueType(i);
1073 std::pair<FALocInfo, FALocInfo> Loc = FormalArgLocs[i];
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001074 SDOperand ArgValue;
Evan Cheng48940d12006-04-27 01:32:22 +00001075 if (Loc.first.Kind == FALocInfo::StackFrameLoc) {
Chris Lattner3d826992006-05-16 06:45:34 +00001076 // Create the SelectionDAG nodes corresponding to a load from this
1077 // parameter.
Evan Cheng48940d12006-04-27 01:32:22 +00001078 SDOperand FIN = DAG.getFrameIndex(Loc.first.Loc, MVT::i32);
Chris Lattner3d826992006-05-16 06:45:34 +00001079 ArgValue = DAG.getLoad(Op.Val->getValueType(i), DAG.getEntryNode(), FIN,
Evan Cheng48940d12006-04-27 01:32:22 +00001080 DAG.getSrcValue(NULL));
1081 } else {
1082 // Must be a CopyFromReg
Evan Cheng89001ad2006-04-27 08:31:10 +00001083 ArgValue= DAG.getCopyFromReg(DAG.getEntryNode(), Loc.first.Loc,
1084 Loc.first.Typ);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001085 }
1086
Evan Cheng48940d12006-04-27 01:32:22 +00001087 if (Loc.second.Kind != FALocInfo::None) {
1088 SDOperand ArgValue2;
1089 if (Loc.second.Kind == FALocInfo::StackFrameLoc) {
Chris Lattner3d826992006-05-16 06:45:34 +00001090 // Create the SelectionDAG nodes corresponding to a load from this
1091 // parameter.
Evan Cheng48940d12006-04-27 01:32:22 +00001092 SDOperand FIN = DAG.getFrameIndex(Loc.second.Loc, MVT::i32);
Chris Lattner3d826992006-05-16 06:45:34 +00001093 ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), DAG.getEntryNode(),
1094 FIN, DAG.getSrcValue(NULL));
Evan Cheng48940d12006-04-27 01:32:22 +00001095 } else {
1096 // Must be a CopyFromReg
Evan Cheng89001ad2006-04-27 08:31:10 +00001097 ArgValue2 = DAG.getCopyFromReg(DAG.getEntryNode(),
Evan Cheng48940d12006-04-27 01:32:22 +00001098 Loc.second.Loc, Loc.second.Typ);
1099 }
1100 ArgValue = DAG.getNode(ISD::BUILD_PAIR, VT, ArgValue, ArgValue2);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001101 }
1102 FormalArgs.push_back(ArgValue);
1103 }
Chris Lattner7b8b8bb2006-05-16 17:08:35 +00001104
1105 // Provide a chain. Note that this isn't the right one, but it works as well
1106 // as before.
1107 FormalArgs.push_back(DAG.getEntryNode());
Chris Lattner76ac0682005-11-15 00:40:23 +00001108}
1109
1110std::pair<SDOperand, SDOperand>
1111X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
1112 bool isTailCall, SDOperand Callee,
1113 ArgListTy &Args, SelectionDAG &DAG) {
1114 // Count how many bytes are to be pushed on the stack.
1115 unsigned NumBytes = 0;
1116
1117 // Keep track of the number of integer regs passed so far. This can be either
1118 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1119 // used).
1120 unsigned NumIntRegs = 0;
1121
1122 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1123 switch (getValueType(Args[i].second)) {
1124 default: assert(0 && "Unknown value type!");
1125 case MVT::i1:
1126 case MVT::i8:
1127 case MVT::i16:
1128 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001129 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001130 ++NumIntRegs;
1131 break;
1132 }
1133 // fall through
1134 case MVT::f32:
1135 NumBytes += 4;
1136 break;
1137 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001138 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
1139 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +00001140 break;
Chris Lattner43798852006-03-17 05:10:20 +00001141 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
1142 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +00001143 NumBytes += 4;
1144 break;
1145 }
1146
1147 // fall through
1148 case MVT::f64:
1149 NumBytes += 8;
1150 break;
1151 }
1152
1153 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1154 // arguments and the arguments after the retaddr has been pushed are aligned.
1155 if ((NumBytes & 7) == 0)
1156 NumBytes += 4;
1157
Chris Lattner62c34842006-02-13 09:00:43 +00001158 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +00001159
1160 // Arguments go on the stack in reverse order, as specified by the ABI.
1161 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +00001162 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +00001163 NumIntRegs = 0;
1164 std::vector<SDOperand> Stores;
1165 std::vector<SDOperand> RegValuesToPass;
1166 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1167 switch (getValueType(Args[i].second)) {
1168 default: assert(0 && "Unexpected ValueType for argument!");
1169 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +00001170 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
1171 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +00001172 case MVT::i8:
1173 case MVT::i16:
1174 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001175 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001176 RegValuesToPass.push_back(Args[i].first);
1177 ++NumIntRegs;
1178 break;
1179 }
1180 // Fall through
1181 case MVT::f32: {
1182 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1183 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1184 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1185 Args[i].first, PtrOff,
1186 DAG.getSrcValue(NULL)));
1187 ArgOffset += 4;
1188 break;
1189 }
1190 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001191 // Can pass (at least) part of it in regs?
1192 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001193 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1194 Args[i].first, DAG.getConstant(1, MVT::i32));
1195 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1196 Args[i].first, DAG.getConstant(0, MVT::i32));
1197 RegValuesToPass.push_back(Lo);
1198 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +00001199
1200 // Pass both parts in regs?
1201 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001202 RegValuesToPass.push_back(Hi);
1203 ++NumIntRegs;
1204 } else {
1205 // Pass the high part in memory.
1206 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1207 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1208 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1209 Hi, PtrOff, DAG.getSrcValue(NULL)));
1210 ArgOffset += 4;
1211 }
1212 break;
1213 }
1214 // Fall through
1215 case MVT::f64:
1216 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1217 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1218 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1219 Args[i].first, PtrOff,
1220 DAG.getSrcValue(NULL)));
1221 ArgOffset += 8;
1222 break;
1223 }
1224 }
1225 if (!Stores.empty())
1226 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
1227
1228 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1229 // arguments and the arguments after the retaddr has been pushed are aligned.
1230 if ((ArgOffset & 7) == 0)
1231 ArgOffset += 4;
1232
1233 std::vector<MVT::ValueType> RetVals;
1234 MVT::ValueType RetTyVT = getValueType(RetTy);
1235
1236 RetVals.push_back(MVT::Other);
1237
1238 // The result values produced have to be legal. Promote the result.
1239 switch (RetTyVT) {
1240 case MVT::isVoid: break;
1241 default:
1242 RetVals.push_back(RetTyVT);
1243 break;
1244 case MVT::i1:
1245 case MVT::i8:
1246 case MVT::i16:
1247 RetVals.push_back(MVT::i32);
1248 break;
1249 case MVT::f32:
1250 if (X86ScalarSSE)
1251 RetVals.push_back(MVT::f32);
1252 else
1253 RetVals.push_back(MVT::f64);
1254 break;
1255 case MVT::i64:
1256 RetVals.push_back(MVT::i32);
1257 RetVals.push_back(MVT::i32);
1258 break;
1259 }
1260
Nate Begeman7e5496d2006-02-17 00:03:04 +00001261 // Build a sequence of copy-to-reg nodes chained together with token chain
1262 // and flag operands which copy the outgoing args into registers.
1263 SDOperand InFlag;
1264 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1265 unsigned CCReg;
1266 SDOperand RegToPass = RegValuesToPass[i];
1267 switch (RegToPass.getValueType()) {
1268 default: assert(0 && "Bad thing to pass in regs");
1269 case MVT::i8:
1270 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001271 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001272 case MVT::i16:
1273 CCReg = (i == 0) ? X86::AX : X86::DX;
1274 break;
1275 case MVT::i32:
1276 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1277 break;
1278 }
1279
1280 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1281 InFlag = Chain.getValue(1);
1282 }
1283
1284 std::vector<MVT::ValueType> NodeTys;
1285 NodeTys.push_back(MVT::Other); // Returns a chain
1286 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1287 std::vector<SDOperand> Ops;
1288 Ops.push_back(Chain);
1289 Ops.push_back(Callee);
1290 if (InFlag.Val)
1291 Ops.push_back(InFlag);
1292
1293 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001294 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1295 NodeTys, Ops);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001296 InFlag = Chain.getValue(1);
1297
1298 NodeTys.clear();
1299 NodeTys.push_back(MVT::Other); // Returns a chain
1300 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1301 Ops.clear();
1302 Ops.push_back(Chain);
1303 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1304 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1305 Ops.push_back(InFlag);
1306 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1307 InFlag = Chain.getValue(1);
1308
1309 SDOperand RetVal;
1310 if (RetTyVT != MVT::isVoid) {
1311 switch (RetTyVT) {
1312 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001313 case MVT::i1:
1314 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001315 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1316 Chain = RetVal.getValue(1);
1317 if (RetTyVT == MVT::i1)
1318 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1319 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001320 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001321 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1322 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001323 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001324 case MVT::i32:
1325 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1326 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001327 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001328 case MVT::i64: {
1329 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1330 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1331 Lo.getValue(2));
1332 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1333 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001334 break;
1335 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001336 case MVT::f32:
1337 case MVT::f64: {
1338 std::vector<MVT::ValueType> Tys;
1339 Tys.push_back(MVT::f64);
1340 Tys.push_back(MVT::Other);
1341 Tys.push_back(MVT::Flag);
1342 std::vector<SDOperand> Ops;
1343 Ops.push_back(Chain);
1344 Ops.push_back(InFlag);
1345 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1346 Chain = RetVal.getValue(1);
1347 InFlag = RetVal.getValue(2);
1348 if (X86ScalarSSE) {
1349 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1350 // shouldn't be necessary except that RFP cannot be live across
1351 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1352 MachineFunction &MF = DAG.getMachineFunction();
1353 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1354 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1355 Tys.clear();
1356 Tys.push_back(MVT::Other);
1357 Ops.clear();
1358 Ops.push_back(Chain);
1359 Ops.push_back(RetVal);
1360 Ops.push_back(StackSlot);
1361 Ops.push_back(DAG.getValueType(RetTyVT));
1362 Ops.push_back(InFlag);
1363 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1364 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1365 DAG.getSrcValue(NULL));
1366 Chain = RetVal.getValue(1);
1367 }
Evan Cheng172fce72006-01-06 00:43:03 +00001368
Nate Begeman7e5496d2006-02-17 00:03:04 +00001369 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1370 // FIXME: we would really like to remember that this FP_ROUND
1371 // operation is okay to eliminate if we allow excess FP precision.
1372 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1373 break;
1374 }
1375 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001376 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001377
1378 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001379}
1380
1381SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1382 if (ReturnAddrIndex == 0) {
1383 // Set up a frame object for the return address.
1384 MachineFunction &MF = DAG.getMachineFunction();
1385 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1386 }
1387
1388 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1389}
1390
1391
1392
1393std::pair<SDOperand, SDOperand> X86TargetLowering::
1394LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1395 SelectionDAG &DAG) {
1396 SDOperand Result;
1397 if (Depth) // Depths > 0 not supported yet!
1398 Result = DAG.getConstant(0, getPointerTy());
1399 else {
1400 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1401 if (!isFrameAddress)
1402 // Just load the return address
1403 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1404 DAG.getSrcValue(NULL));
1405 else
1406 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1407 DAG.getConstant(4, MVT::i32));
1408 }
1409 return std::make_pair(Result, Chain);
1410}
1411
Evan Cheng339edad2006-01-11 00:33:36 +00001412/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1413/// which corresponds to the condition code.
1414static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1415 switch (X86CC) {
1416 default: assert(0 && "Unknown X86 conditional code!");
1417 case X86ISD::COND_A: return X86::JA;
1418 case X86ISD::COND_AE: return X86::JAE;
1419 case X86ISD::COND_B: return X86::JB;
1420 case X86ISD::COND_BE: return X86::JBE;
1421 case X86ISD::COND_E: return X86::JE;
1422 case X86ISD::COND_G: return X86::JG;
1423 case X86ISD::COND_GE: return X86::JGE;
1424 case X86ISD::COND_L: return X86::JL;
1425 case X86ISD::COND_LE: return X86::JLE;
1426 case X86ISD::COND_NE: return X86::JNE;
1427 case X86ISD::COND_NO: return X86::JNO;
1428 case X86ISD::COND_NP: return X86::JNP;
1429 case X86ISD::COND_NS: return X86::JNS;
1430 case X86ISD::COND_O: return X86::JO;
1431 case X86ISD::COND_P: return X86::JP;
1432 case X86ISD::COND_S: return X86::JS;
1433 }
1434}
Chris Lattner76ac0682005-11-15 00:40:23 +00001435
Evan Cheng45df7f82006-01-30 23:41:35 +00001436/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1437/// specific condition code. It returns a false if it cannot do a direct
1438/// translation. X86CC is the translated CondCode. Flip is set to true if the
1439/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001440static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1441 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001442 Flip = false;
1443 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001444 if (!isFP) {
1445 switch (SetCCOpcode) {
1446 default: break;
1447 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1448 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1449 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1450 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1451 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1452 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1453 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1454 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1455 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1456 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1457 }
1458 } else {
1459 // On a floating point condition, the flags are set as follows:
1460 // ZF PF CF op
1461 // 0 | 0 | 0 | X > Y
1462 // 0 | 0 | 1 | X < Y
1463 // 1 | 0 | 0 | X == Y
1464 // 1 | 1 | 1 | unordered
1465 switch (SetCCOpcode) {
1466 default: break;
1467 case ISD::SETUEQ:
1468 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001469 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001470 case ISD::SETOGT:
1471 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001472 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001473 case ISD::SETOGE:
1474 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001475 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001476 case ISD::SETULT:
1477 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001478 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001479 case ISD::SETULE:
1480 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1481 case ISD::SETONE:
1482 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1483 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1484 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1485 }
1486 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001487
1488 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001489}
1490
Evan Cheng78038292006-04-05 23:38:46 +00001491static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1492 bool &Flip) {
1493 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1494}
1495
Evan Cheng339edad2006-01-11 00:33:36 +00001496/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1497/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001498/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001499static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001500 switch (X86CC) {
1501 default:
1502 return false;
1503 case X86ISD::COND_B:
1504 case X86ISD::COND_BE:
1505 case X86ISD::COND_E:
1506 case X86ISD::COND_P:
1507 case X86ISD::COND_A:
1508 case X86ISD::COND_AE:
1509 case X86ISD::COND_NE:
1510 case X86ISD::COND_NP:
1511 return true;
1512 }
1513}
1514
Evan Cheng339edad2006-01-11 00:33:36 +00001515MachineBasicBlock *
1516X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1517 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001518 switch (MI->getOpcode()) {
1519 default: assert(false && "Unexpected instr type to insert");
1520 case X86::CMOV_FR32:
Evan Cheng617a6a82006-04-10 07:23:14 +00001521 case X86::CMOV_FR64:
1522 case X86::CMOV_V4F32:
1523 case X86::CMOV_V2F64:
1524 case X86::CMOV_V2I64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001525 // To "insert" a SELECT_CC instruction, we actually have to insert the
1526 // diamond control-flow pattern. The incoming instruction knows the
1527 // destination vreg to set, the condition code register to branch on, the
1528 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001529 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1530 ilist<MachineBasicBlock>::iterator It = BB;
1531 ++It;
1532
1533 // thisMBB:
1534 // ...
1535 // TrueVal = ...
1536 // cmpTY ccX, r1, r2
1537 // bCC copy1MBB
1538 // fallthrough --> copy0MBB
1539 MachineBasicBlock *thisMBB = BB;
1540 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1541 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1542 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1543 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1544 MachineFunction *F = BB->getParent();
1545 F->getBasicBlockList().insert(It, copy0MBB);
1546 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001547 // Update machine-CFG edges by first adding all successors of the current
1548 // block to the new block which will contain the Phi node for the select.
1549 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1550 e = BB->succ_end(); i != e; ++i)
1551 sinkMBB->addSuccessor(*i);
1552 // Next, remove all successors of the current block, and add the true
1553 // and fallthrough blocks as its successors.
1554 while(!BB->succ_empty())
1555 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001556 BB->addSuccessor(copy0MBB);
1557 BB->addSuccessor(sinkMBB);
1558
1559 // copy0MBB:
1560 // %FalseValue = ...
1561 // # fallthrough to sinkMBB
1562 BB = copy0MBB;
1563
1564 // Update machine-CFG edges
1565 BB->addSuccessor(sinkMBB);
1566
1567 // sinkMBB:
1568 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1569 // ...
1570 BB = sinkMBB;
1571 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1572 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1573 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001574
Evan Cheng911c68d2006-01-16 21:21:29 +00001575 delete MI; // The pseudo instruction is gone now.
1576 return BB;
1577 }
Evan Cheng339edad2006-01-11 00:33:36 +00001578
Evan Cheng911c68d2006-01-16 21:21:29 +00001579 case X86::FP_TO_INT16_IN_MEM:
1580 case X86::FP_TO_INT32_IN_MEM:
1581 case X86::FP_TO_INT64_IN_MEM: {
1582 // Change the floating point control register to use "round towards zero"
1583 // mode when truncating to an integer value.
1584 MachineFunction *F = BB->getParent();
1585 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1586 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1587
1588 // Load the old value of the high byte of the control word...
1589 unsigned OldCW =
Evan Cheng9fee4422006-05-16 07:21:53 +00001590 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng911c68d2006-01-16 21:21:29 +00001591 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1592
1593 // Set the high part to be round to zero...
1594 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1595
1596 // Reload the modified control word now...
1597 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1598
1599 // Restore the memory image of control word to original value
1600 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1601
1602 // Get the X86 opcode to use.
1603 unsigned Opc;
1604 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001605 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001606 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1607 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1608 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1609 }
1610
1611 X86AddressMode AM;
1612 MachineOperand &Op = MI->getOperand(0);
1613 if (Op.isRegister()) {
1614 AM.BaseType = X86AddressMode::RegBase;
1615 AM.Base.Reg = Op.getReg();
1616 } else {
1617 AM.BaseType = X86AddressMode::FrameIndexBase;
1618 AM.Base.FrameIndex = Op.getFrameIndex();
1619 }
1620 Op = MI->getOperand(1);
1621 if (Op.isImmediate())
1622 AM.Scale = Op.getImmedValue();
1623 Op = MI->getOperand(2);
1624 if (Op.isImmediate())
1625 AM.IndexReg = Op.getImmedValue();
1626 Op = MI->getOperand(3);
1627 if (Op.isGlobalAddress()) {
1628 AM.GV = Op.getGlobal();
1629 } else {
1630 AM.Disp = Op.getImmedValue();
1631 }
1632 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1633
1634 // Reload the original control word now.
1635 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1636
1637 delete MI; // The pseudo instruction is gone now.
1638 return BB;
1639 }
1640 }
Evan Cheng339edad2006-01-11 00:33:36 +00001641}
1642
1643
1644//===----------------------------------------------------------------------===//
1645// X86 Custom Lowering Hooks
1646//===----------------------------------------------------------------------===//
1647
Evan Chengaf598d22006-03-13 23:18:16 +00001648/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1649/// load. For Darwin, external and weak symbols are indirect, loading the value
1650/// at address GV rather then the value of GV itself. This means that the
1651/// GlobalAddress must be in the base or index register of the address, not the
1652/// GV offset field.
1653static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1654 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1655 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1656}
1657
Evan Chengc995b452006-04-06 23:23:56 +00001658/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001659/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001660static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1661 if (Op.getOpcode() == ISD::UNDEF)
1662 return true;
1663
1664 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001665 return (Val >= Low && Val < Hi);
1666}
1667
1668/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1669/// true if Op is undef or if its value equal to the specified value.
1670static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1671 if (Op.getOpcode() == ISD::UNDEF)
1672 return true;
1673 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001674}
1675
Evan Cheng68ad48b2006-03-22 18:59:22 +00001676/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1677/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1678bool X86::isPSHUFDMask(SDNode *N) {
1679 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1680
1681 if (N->getNumOperands() != 4)
1682 return false;
1683
1684 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001685 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001686 SDOperand Arg = N->getOperand(i);
1687 if (Arg.getOpcode() == ISD::UNDEF) continue;
1688 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1689 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001690 return false;
1691 }
1692
1693 return true;
1694}
1695
1696/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001697/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001698bool X86::isPSHUFHWMask(SDNode *N) {
1699 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1700
1701 if (N->getNumOperands() != 8)
1702 return false;
1703
1704 // Lower quadword copied in order.
1705 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001706 SDOperand Arg = N->getOperand(i);
1707 if (Arg.getOpcode() == ISD::UNDEF) continue;
1708 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1709 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001710 return false;
1711 }
1712
1713 // Upper quadword shuffled.
1714 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001715 SDOperand Arg = N->getOperand(i);
1716 if (Arg.getOpcode() == ISD::UNDEF) continue;
1717 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1718 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001719 if (Val < 4 || Val > 7)
1720 return false;
1721 }
1722
1723 return true;
1724}
1725
1726/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001727/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001728bool X86::isPSHUFLWMask(SDNode *N) {
1729 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1730
1731 if (N->getNumOperands() != 8)
1732 return false;
1733
1734 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001735 for (unsigned i = 4; i != 8; ++i)
1736 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001737 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001738
1739 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001740 for (unsigned i = 0; i != 4; ++i)
1741 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001742 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001743
1744 return true;
1745}
1746
Evan Chengd27fb3e2006-03-24 01:18:28 +00001747/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1748/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng60f0b892006-04-20 08:58:49 +00001749static bool isSHUFPMask(std::vector<SDOperand> &N) {
1750 unsigned NumElems = N.size();
1751 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001752
Evan Cheng60f0b892006-04-20 08:58:49 +00001753 unsigned Half = NumElems / 2;
1754 for (unsigned i = 0; i < Half; ++i)
1755 if (!isUndefOrInRange(N[i], 0, NumElems))
1756 return false;
1757 for (unsigned i = Half; i < NumElems; ++i)
1758 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1759 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001760
1761 return true;
1762}
1763
Evan Cheng60f0b892006-04-20 08:58:49 +00001764bool X86::isSHUFPMask(SDNode *N) {
1765 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1766 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1767 return ::isSHUFPMask(Ops);
1768}
1769
1770/// isCommutedSHUFP - Returns true if the shuffle mask is except
1771/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1772/// half elements to come from vector 1 (which would equal the dest.) and
1773/// the upper half to come from vector 2.
1774static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1775 unsigned NumElems = Ops.size();
1776 if (NumElems != 2 && NumElems != 4) return false;
1777
1778 unsigned Half = NumElems / 2;
1779 for (unsigned i = 0; i < Half; ++i)
1780 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1781 return false;
1782 for (unsigned i = Half; i < NumElems; ++i)
1783 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1784 return false;
1785 return true;
1786}
1787
1788static bool isCommutedSHUFP(SDNode *N) {
1789 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1790 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1791 return isCommutedSHUFP(Ops);
1792}
1793
Evan Cheng2595a682006-03-24 02:58:06 +00001794/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1795/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1796bool X86::isMOVHLPSMask(SDNode *N) {
1797 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1798
Evan Cheng1a194a52006-03-28 06:50:32 +00001799 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001800 return false;
1801
Evan Cheng1a194a52006-03-28 06:50:32 +00001802 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001803 return isUndefOrEqual(N->getOperand(0), 6) &&
1804 isUndefOrEqual(N->getOperand(1), 7) &&
1805 isUndefOrEqual(N->getOperand(2), 2) &&
1806 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001807}
1808
Evan Chengc995b452006-04-06 23:23:56 +00001809/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1810/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1811bool X86::isMOVLPMask(SDNode *N) {
1812 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1813
1814 unsigned NumElems = N->getNumOperands();
1815 if (NumElems != 2 && NumElems != 4)
1816 return false;
1817
Evan Chengac847262006-04-07 21:53:05 +00001818 for (unsigned i = 0; i < NumElems/2; ++i)
1819 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1820 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001821
Evan Chengac847262006-04-07 21:53:05 +00001822 for (unsigned i = NumElems/2; i < NumElems; ++i)
1823 if (!isUndefOrEqual(N->getOperand(i), i))
1824 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001825
1826 return true;
1827}
1828
1829/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001830/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1831/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001832bool X86::isMOVHPMask(SDNode *N) {
1833 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1834
1835 unsigned NumElems = N->getNumOperands();
1836 if (NumElems != 2 && NumElems != 4)
1837 return false;
1838
Evan Chengac847262006-04-07 21:53:05 +00001839 for (unsigned i = 0; i < NumElems/2; ++i)
1840 if (!isUndefOrEqual(N->getOperand(i), i))
1841 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001842
1843 for (unsigned i = 0; i < NumElems/2; ++i) {
1844 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001845 if (!isUndefOrEqual(Arg, i + NumElems))
1846 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001847 }
1848
1849 return true;
1850}
1851
Evan Cheng5df75882006-03-28 00:39:58 +00001852/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1853/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng60f0b892006-04-20 08:58:49 +00001854bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1855 unsigned NumElems = N.size();
Evan Cheng5df75882006-03-28 00:39:58 +00001856 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1857 return false;
1858
1859 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001860 SDOperand BitI = N[i];
1861 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001862 if (!isUndefOrEqual(BitI, j))
1863 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001864 if (V2IsSplat) {
1865 if (isUndefOrEqual(BitI1, NumElems))
1866 return false;
1867 } else {
1868 if (!isUndefOrEqual(BitI1, j + NumElems))
1869 return false;
1870 }
Evan Cheng5df75882006-03-28 00:39:58 +00001871 }
1872
1873 return true;
1874}
1875
Evan Cheng60f0b892006-04-20 08:58:49 +00001876bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1877 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1878 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1879 return ::isUNPCKLMask(Ops, V2IsSplat);
1880}
1881
Evan Cheng2bc32802006-03-28 02:43:26 +00001882/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1883/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng60f0b892006-04-20 08:58:49 +00001884bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1885 unsigned NumElems = N.size();
Evan Cheng2bc32802006-03-28 02:43:26 +00001886 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1887 return false;
1888
1889 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001890 SDOperand BitI = N[i];
1891 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001892 if (!isUndefOrEqual(BitI, j + NumElems/2))
1893 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001894 if (V2IsSplat) {
1895 if (isUndefOrEqual(BitI1, NumElems))
1896 return false;
1897 } else {
1898 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1899 return false;
1900 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001901 }
1902
1903 return true;
1904}
1905
Evan Cheng60f0b892006-04-20 08:58:49 +00001906bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1907 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1908 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1909 return ::isUNPCKHMask(Ops, V2IsSplat);
1910}
1911
Evan Chengf3b52c82006-04-05 07:20:06 +00001912/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1913/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1914/// <0, 0, 1, 1>
1915bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1916 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1917
1918 unsigned NumElems = N->getNumOperands();
1919 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1920 return false;
1921
1922 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1923 SDOperand BitI = N->getOperand(i);
1924 SDOperand BitI1 = N->getOperand(i+1);
1925
Evan Chengac847262006-04-07 21:53:05 +00001926 if (!isUndefOrEqual(BitI, j))
1927 return false;
1928 if (!isUndefOrEqual(BitI1, j))
1929 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001930 }
1931
1932 return true;
1933}
1934
Evan Chenge8b51802006-04-21 01:05:10 +00001935/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1936/// specifies a shuffle of elements that is suitable for input to MOVSS,
1937/// MOVSD, and MOVD, i.e. setting the lowest element.
1938static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001939 unsigned NumElems = N.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001940 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001941 return false;
1942
Evan Cheng60f0b892006-04-20 08:58:49 +00001943 if (!isUndefOrEqual(N[0], NumElems))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001944 return false;
1945
1946 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001947 SDOperand Arg = N[i];
Evan Cheng12ba3e22006-04-11 00:19:04 +00001948 if (!isUndefOrEqual(Arg, i))
1949 return false;
1950 }
1951
1952 return true;
1953}
Evan Chengf3b52c82006-04-05 07:20:06 +00001954
Evan Chenge8b51802006-04-21 01:05:10 +00001955bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001956 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1957 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001958 return ::isMOVLMask(Ops);
Evan Cheng60f0b892006-04-20 08:58:49 +00001959}
1960
Evan Chenge8b51802006-04-21 01:05:10 +00001961/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1962/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001963/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Chenge8b51802006-04-21 01:05:10 +00001964static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001965 unsigned NumElems = Ops.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001966 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001967 return false;
1968
1969 if (!isUndefOrEqual(Ops[0], 0))
1970 return false;
1971
1972 for (unsigned i = 1; i < NumElems; ++i) {
1973 SDOperand Arg = Ops[i];
1974 if (V2IsSplat) {
1975 if (!isUndefOrEqual(Arg, NumElems))
1976 return false;
1977 } else {
1978 if (!isUndefOrEqual(Arg, i+NumElems))
1979 return false;
1980 }
1981 }
1982
1983 return true;
1984}
1985
Evan Chenge8b51802006-04-21 01:05:10 +00001986static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001987 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1988 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001989 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001990}
1991
Evan Cheng5d247f82006-04-14 21:59:03 +00001992/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1993/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1994bool X86::isMOVSHDUPMask(SDNode *N) {
1995 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1996
1997 if (N->getNumOperands() != 4)
1998 return false;
1999
2000 // Expect 1, 1, 3, 3
2001 for (unsigned i = 0; i < 2; ++i) {
2002 SDOperand Arg = N->getOperand(i);
2003 if (Arg.getOpcode() == ISD::UNDEF) continue;
2004 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2005 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2006 if (Val != 1) return false;
2007 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002008
2009 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002010 for (unsigned i = 2; i < 4; ++i) {
2011 SDOperand Arg = N->getOperand(i);
2012 if (Arg.getOpcode() == ISD::UNDEF) continue;
2013 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2014 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2015 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002016 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002017 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002018
Evan Cheng6222cf22006-04-15 05:37:34 +00002019 // Don't use movshdup if it can be done with a shufps.
2020 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002021}
2022
2023/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2024/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2025bool X86::isMOVSLDUPMask(SDNode *N) {
2026 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2027
2028 if (N->getNumOperands() != 4)
2029 return false;
2030
2031 // Expect 0, 0, 2, 2
2032 for (unsigned i = 0; i < 2; ++i) {
2033 SDOperand Arg = N->getOperand(i);
2034 if (Arg.getOpcode() == ISD::UNDEF) continue;
2035 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2036 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2037 if (Val != 0) return false;
2038 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002039
2040 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002041 for (unsigned i = 2; i < 4; ++i) {
2042 SDOperand Arg = N->getOperand(i);
2043 if (Arg.getOpcode() == ISD::UNDEF) continue;
2044 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2045 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2046 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002047 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002048 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002049
Evan Cheng6222cf22006-04-15 05:37:34 +00002050 // Don't use movshdup if it can be done with a shufps.
2051 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002052}
2053
Evan Chengd097e672006-03-22 02:53:00 +00002054/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2055/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00002056static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002057 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2058
Evan Chengd097e672006-03-22 02:53:00 +00002059 // This is a splat operation if each element of the permute is the same, and
2060 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002061 unsigned NumElems = N->getNumOperands();
2062 SDOperand ElementBase;
2063 unsigned i = 0;
2064 for (; i != NumElems; ++i) {
2065 SDOperand Elt = N->getOperand(i);
2066 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
2067 ElementBase = Elt;
2068 break;
2069 }
2070 }
2071
2072 if (!ElementBase.Val)
2073 return false;
2074
2075 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002076 SDOperand Arg = N->getOperand(i);
2077 if (Arg.getOpcode() == ISD::UNDEF) continue;
2078 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002079 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00002080 }
2081
2082 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002083 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00002084}
2085
Evan Cheng5022b342006-04-17 20:43:08 +00002086/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2087/// a splat of a single element and it's a 2 or 4 element mask.
2088bool X86::isSplatMask(SDNode *N) {
2089 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2090
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002091 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00002092 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2093 return false;
2094 return ::isSplatMask(N);
2095}
2096
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002097/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2098/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2099/// instructions.
2100unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002101 unsigned NumOperands = N->getNumOperands();
2102 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2103 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00002104 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002105 unsigned Val = 0;
2106 SDOperand Arg = N->getOperand(NumOperands-i-1);
2107 if (Arg.getOpcode() != ISD::UNDEF)
2108 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00002109 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002110 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00002111 if (i != NumOperands - 1)
2112 Mask <<= Shift;
2113 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002114
2115 return Mask;
2116}
2117
Evan Chengb7fedff2006-03-29 23:07:14 +00002118/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2119/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2120/// instructions.
2121unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2122 unsigned Mask = 0;
2123 // 8 nodes, but we only care about the last 4.
2124 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002125 unsigned Val = 0;
2126 SDOperand Arg = N->getOperand(i);
2127 if (Arg.getOpcode() != ISD::UNDEF)
2128 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002129 Mask |= (Val - 4);
2130 if (i != 4)
2131 Mask <<= 2;
2132 }
2133
2134 return Mask;
2135}
2136
2137/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2138/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2139/// instructions.
2140unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2141 unsigned Mask = 0;
2142 // 8 nodes, but we only care about the first 4.
2143 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002144 unsigned Val = 0;
2145 SDOperand Arg = N->getOperand(i);
2146 if (Arg.getOpcode() != ISD::UNDEF)
2147 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002148 Mask |= Val;
2149 if (i != 0)
2150 Mask <<= 2;
2151 }
2152
2153 return Mask;
2154}
2155
Evan Cheng59a63552006-04-05 01:47:37 +00002156/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2157/// specifies a 8 element shuffle that can be broken into a pair of
2158/// PSHUFHW and PSHUFLW.
2159static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2160 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2161
2162 if (N->getNumOperands() != 8)
2163 return false;
2164
2165 // Lower quadword shuffled.
2166 for (unsigned i = 0; i != 4; ++i) {
2167 SDOperand Arg = N->getOperand(i);
2168 if (Arg.getOpcode() == ISD::UNDEF) continue;
2169 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2170 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2171 if (Val > 4)
2172 return false;
2173 }
2174
2175 // Upper quadword shuffled.
2176 for (unsigned i = 4; i != 8; ++i) {
2177 SDOperand Arg = N->getOperand(i);
2178 if (Arg.getOpcode() == ISD::UNDEF) continue;
2179 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2180 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2181 if (Val < 4 || Val > 7)
2182 return false;
2183 }
2184
2185 return true;
2186}
2187
Evan Chengc995b452006-04-06 23:23:56 +00002188/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2189/// values in ther permute mask.
2190static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2191 SDOperand V1 = Op.getOperand(0);
2192 SDOperand V2 = Op.getOperand(1);
2193 SDOperand Mask = Op.getOperand(2);
2194 MVT::ValueType VT = Op.getValueType();
2195 MVT::ValueType MaskVT = Mask.getValueType();
2196 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2197 unsigned NumElems = Mask.getNumOperands();
2198 std::vector<SDOperand> MaskVec;
2199
2200 for (unsigned i = 0; i != NumElems; ++i) {
2201 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002202 if (Arg.getOpcode() == ISD::UNDEF) {
2203 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2204 continue;
2205 }
Evan Chengc995b452006-04-06 23:23:56 +00002206 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2207 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2208 if (Val < NumElems)
2209 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2210 else
2211 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2212 }
2213
2214 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2215 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2216}
2217
Evan Cheng7855e4d2006-04-19 20:35:22 +00002218/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2219/// match movhlps. The lower half elements should come from upper half of
2220/// V1 (and in order), and the upper half elements should come from the upper
2221/// half of V2 (and in order).
2222static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2223 unsigned NumElems = Mask->getNumOperands();
2224 if (NumElems != 4)
2225 return false;
2226 for (unsigned i = 0, e = 2; i != e; ++i)
2227 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2228 return false;
2229 for (unsigned i = 2; i != 4; ++i)
2230 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2231 return false;
2232 return true;
2233}
2234
Evan Chengc995b452006-04-06 23:23:56 +00002235/// isScalarLoadToVector - Returns true if the node is a scalar load that
2236/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002237static inline bool isScalarLoadToVector(SDNode *N) {
2238 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2239 N = N->getOperand(0).Val;
2240 return (N->getOpcode() == ISD::LOAD);
Evan Chengc995b452006-04-06 23:23:56 +00002241 }
2242 return false;
2243}
2244
Evan Cheng7855e4d2006-04-19 20:35:22 +00002245/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2246/// match movlp{s|d}. The lower half elements should come from lower half of
2247/// V1 (and in order), and the upper half elements should come from the upper
2248/// half of V2 (and in order). And since V1 will become the source of the
2249/// MOVLP, it must be either a vector load or a scalar load to vector.
2250static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2251 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2252 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002253
Evan Cheng7855e4d2006-04-19 20:35:22 +00002254 unsigned NumElems = Mask->getNumOperands();
2255 if (NumElems != 2 && NumElems != 4)
2256 return false;
2257 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2258 if (!isUndefOrEqual(Mask->getOperand(i), i))
2259 return false;
2260 for (unsigned i = NumElems/2; i != NumElems; ++i)
2261 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2262 return false;
2263 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002264}
2265
Evan Cheng60f0b892006-04-20 08:58:49 +00002266/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2267/// all the same.
2268static bool isSplatVector(SDNode *N) {
2269 if (N->getOpcode() != ISD::BUILD_VECTOR)
2270 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002271
Evan Cheng60f0b892006-04-20 08:58:49 +00002272 SDOperand SplatValue = N->getOperand(0);
2273 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2274 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002275 return false;
2276 return true;
2277}
2278
Evan Cheng60f0b892006-04-20 08:58:49 +00002279/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2280/// that point to V2 points to its first element.
2281static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2282 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2283
2284 bool Changed = false;
2285 std::vector<SDOperand> MaskVec;
2286 unsigned NumElems = Mask.getNumOperands();
2287 for (unsigned i = 0; i != NumElems; ++i) {
2288 SDOperand Arg = Mask.getOperand(i);
2289 if (Arg.getOpcode() != ISD::UNDEF) {
2290 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2291 if (Val > NumElems) {
2292 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2293 Changed = true;
2294 }
2295 }
2296 MaskVec.push_back(Arg);
2297 }
2298
2299 if (Changed)
2300 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2301 return Mask;
2302}
2303
Evan Chenge8b51802006-04-21 01:05:10 +00002304/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2305/// operation of specified width.
2306static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002307 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2308 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2309
2310 std::vector<SDOperand> MaskVec;
2311 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2312 for (unsigned i = 1; i != NumElems; ++i)
2313 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2314 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2315}
2316
Evan Cheng5022b342006-04-17 20:43:08 +00002317/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2318/// of specified width.
2319static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2320 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2321 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2322 std::vector<SDOperand> MaskVec;
2323 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2324 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2325 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2326 }
2327 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2328}
2329
Evan Cheng60f0b892006-04-20 08:58:49 +00002330/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2331/// of specified width.
2332static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2333 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2334 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2335 unsigned Half = NumElems/2;
2336 std::vector<SDOperand> MaskVec;
2337 for (unsigned i = 0; i != Half; ++i) {
2338 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2339 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2340 }
2341 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2342}
2343
Evan Chenge8b51802006-04-21 01:05:10 +00002344/// getZeroVector - Returns a vector of specified type with all zero elements.
2345///
2346static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2347 assert(MVT::isVector(VT) && "Expected a vector type");
2348 unsigned NumElems = getVectorNumElements(VT);
2349 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2350 bool isFP = MVT::isFloatingPoint(EVT);
2351 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2352 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2353 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2354}
2355
Evan Cheng5022b342006-04-17 20:43:08 +00002356/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2357///
2358static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2359 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002360 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002361 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002362 unsigned NumElems = Mask.getNumOperands();
2363 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002364 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002365 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002366 NumElems >>= 1;
2367 }
2368 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2369
2370 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002371 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002372 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002373 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002374 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2375}
2376
Evan Chenge8b51802006-04-21 01:05:10 +00002377/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2378/// constant +0.0.
2379static inline bool isZeroNode(SDOperand Elt) {
2380 return ((isa<ConstantSDNode>(Elt) &&
2381 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2382 (isa<ConstantFPSDNode>(Elt) &&
2383 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2384}
2385
Evan Cheng14215c32006-04-21 23:03:30 +00002386/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2387/// vector and zero or undef vector.
2388static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002389 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002390 bool isZero, SelectionDAG &DAG) {
2391 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002392 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2393 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2394 SDOperand Zero = DAG.getConstant(0, EVT);
2395 std::vector<SDOperand> MaskVec(NumElems, Zero);
2396 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2397 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng14215c32006-04-21 23:03:30 +00002398 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002399}
2400
Evan Chengb0461082006-04-24 18:01:45 +00002401/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2402///
2403static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2404 unsigned NumNonZero, unsigned NumZero,
2405 SelectionDAG &DAG) {
2406 if (NumNonZero > 8)
2407 return SDOperand();
2408
2409 SDOperand V(0, 0);
2410 bool First = true;
2411 for (unsigned i = 0; i < 16; ++i) {
2412 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2413 if (ThisIsNonZero && First) {
2414 if (NumZero)
2415 V = getZeroVector(MVT::v8i16, DAG);
2416 else
2417 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2418 First = false;
2419 }
2420
2421 if ((i & 1) != 0) {
2422 SDOperand ThisElt(0, 0), LastElt(0, 0);
2423 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2424 if (LastIsNonZero) {
2425 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2426 }
2427 if (ThisIsNonZero) {
2428 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2429 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2430 ThisElt, DAG.getConstant(8, MVT::i8));
2431 if (LastIsNonZero)
2432 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2433 } else
2434 ThisElt = LastElt;
2435
2436 if (ThisElt.Val)
2437 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2438 DAG.getConstant(i/2, MVT::i32));
2439 }
2440 }
2441
2442 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2443}
2444
2445/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2446///
2447static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2448 unsigned NumNonZero, unsigned NumZero,
2449 SelectionDAG &DAG) {
2450 if (NumNonZero > 4)
2451 return SDOperand();
2452
2453 SDOperand V(0, 0);
2454 bool First = true;
2455 for (unsigned i = 0; i < 8; ++i) {
2456 bool isNonZero = (NonZeros & (1 << i)) != 0;
2457 if (isNonZero) {
2458 if (First) {
2459 if (NumZero)
2460 V = getZeroVector(MVT::v8i16, DAG);
2461 else
2462 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2463 First = false;
2464 }
2465 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2466 DAG.getConstant(i, MVT::i32));
2467 }
2468 }
2469
2470 return V;
2471}
2472
Evan Chenga9467aa2006-04-25 20:13:52 +00002473SDOperand
2474X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2475 // All zero's are handled with pxor.
2476 if (ISD::isBuildVectorAllZeros(Op.Val))
2477 return Op;
2478
2479 // All one's are handled with pcmpeqd.
2480 if (ISD::isBuildVectorAllOnes(Op.Val))
2481 return Op;
2482
2483 MVT::ValueType VT = Op.getValueType();
2484 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2485 unsigned EVTBits = MVT::getSizeInBits(EVT);
2486
2487 unsigned NumElems = Op.getNumOperands();
2488 unsigned NumZero = 0;
2489 unsigned NumNonZero = 0;
2490 unsigned NonZeros = 0;
2491 std::set<SDOperand> Values;
2492 for (unsigned i = 0; i < NumElems; ++i) {
2493 SDOperand Elt = Op.getOperand(i);
2494 if (Elt.getOpcode() != ISD::UNDEF) {
2495 Values.insert(Elt);
2496 if (isZeroNode(Elt))
2497 NumZero++;
2498 else {
2499 NonZeros |= (1 << i);
2500 NumNonZero++;
2501 }
2502 }
2503 }
2504
2505 if (NumNonZero == 0)
2506 // Must be a mix of zero and undef. Return a zero vector.
2507 return getZeroVector(VT, DAG);
2508
2509 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2510 if (Values.size() == 1)
2511 return SDOperand();
2512
2513 // Special case for single non-zero element.
2514 if (NumNonZero == 1) {
2515 unsigned Idx = CountTrailingZeros_32(NonZeros);
2516 SDOperand Item = Op.getOperand(Idx);
2517 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2518 if (Idx == 0)
2519 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2520 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2521 NumZero > 0, DAG);
2522
2523 if (EVTBits == 32) {
2524 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2525 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2526 DAG);
2527 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2528 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2529 std::vector<SDOperand> MaskVec;
2530 for (unsigned i = 0; i < NumElems; i++)
2531 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2532 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2533 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2534 DAG.getNode(ISD::UNDEF, VT), Mask);
2535 }
2536 }
2537
2538 // Let legalizer expand 2-widde build_vector's.
2539 if (EVTBits == 64)
2540 return SDOperand();
2541
2542 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2543 if (EVTBits == 8) {
2544 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2545 if (V.Val) return V;
2546 }
2547
2548 if (EVTBits == 16) {
2549 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2550 if (V.Val) return V;
2551 }
2552
2553 // If element VT is == 32 bits, turn it into a number of shuffles.
2554 std::vector<SDOperand> V(NumElems);
2555 if (NumElems == 4 && NumZero > 0) {
2556 for (unsigned i = 0; i < 4; ++i) {
2557 bool isZero = !(NonZeros & (1 << i));
2558 if (isZero)
2559 V[i] = getZeroVector(VT, DAG);
2560 else
2561 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2562 }
2563
2564 for (unsigned i = 0; i < 2; ++i) {
2565 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2566 default: break;
2567 case 0:
2568 V[i] = V[i*2]; // Must be a zero vector.
2569 break;
2570 case 1:
2571 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2572 getMOVLMask(NumElems, DAG));
2573 break;
2574 case 2:
2575 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2576 getMOVLMask(NumElems, DAG));
2577 break;
2578 case 3:
2579 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2580 getUnpacklMask(NumElems, DAG));
2581 break;
2582 }
2583 }
2584
Evan Cheng9fee4422006-05-16 07:21:53 +00002585 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Chenga9467aa2006-04-25 20:13:52 +00002586 // clears the upper bits.
2587 // FIXME: we can do the same for v4f32 case when we know both parts of
2588 // the lower half come from scalar_to_vector (loadf32). We should do
2589 // that in post legalizer dag combiner with target specific hooks.
2590 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2591 return V[0];
2592 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2593 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2594 std::vector<SDOperand> MaskVec;
2595 bool Reverse = (NonZeros & 0x3) == 2;
2596 for (unsigned i = 0; i < 2; ++i)
2597 if (Reverse)
2598 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2599 else
2600 MaskVec.push_back(DAG.getConstant(i, EVT));
2601 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2602 for (unsigned i = 0; i < 2; ++i)
2603 if (Reverse)
2604 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2605 else
2606 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2607 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2608 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2609 }
2610
2611 if (Values.size() > 2) {
2612 // Expand into a number of unpckl*.
2613 // e.g. for v4f32
2614 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2615 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2616 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2617 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2618 for (unsigned i = 0; i < NumElems; ++i)
2619 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2620 NumElems >>= 1;
2621 while (NumElems != 0) {
2622 for (unsigned i = 0; i < NumElems; ++i)
2623 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2624 UnpckMask);
2625 NumElems >>= 1;
2626 }
2627 return V[0];
2628 }
2629
2630 return SDOperand();
2631}
2632
2633SDOperand
2634X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2635 SDOperand V1 = Op.getOperand(0);
2636 SDOperand V2 = Op.getOperand(1);
2637 SDOperand PermMask = Op.getOperand(2);
2638 MVT::ValueType VT = Op.getValueType();
2639 unsigned NumElems = PermMask.getNumOperands();
2640 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2641 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2642
2643 if (isSplatMask(PermMask.Val)) {
2644 if (NumElems <= 4) return Op;
2645 // Promote it to a v4i32 splat.
2646 return PromoteSplat(Op, DAG);
2647 }
2648
2649 if (X86::isMOVLMask(PermMask.Val))
2650 return (V1IsUndef) ? V2 : Op;
2651
2652 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2653 X86::isMOVSLDUPMask(PermMask.Val) ||
2654 X86::isMOVHLPSMask(PermMask.Val) ||
2655 X86::isMOVHPMask(PermMask.Val) ||
2656 X86::isMOVLPMask(PermMask.Val))
2657 return Op;
2658
2659 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2660 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2661 return CommuteVectorShuffle(Op, DAG);
2662
2663 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2664 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2665 if (V1IsSplat && !V2IsSplat) {
2666 Op = CommuteVectorShuffle(Op, DAG);
2667 V1 = Op.getOperand(0);
2668 V2 = Op.getOperand(1);
2669 PermMask = Op.getOperand(2);
2670 V2IsSplat = true;
2671 }
2672
2673 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2674 if (V2IsUndef) return V1;
2675 Op = CommuteVectorShuffle(Op, DAG);
2676 V1 = Op.getOperand(0);
2677 V2 = Op.getOperand(1);
2678 PermMask = Op.getOperand(2);
2679 if (V2IsSplat) {
2680 // V2 is a splat, so the mask may be malformed. That is, it may point
2681 // to any V2 element. The instruction selectior won't like this. Get
2682 // a corrected mask and commute to form a proper MOVS{S|D}.
2683 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2684 if (NewMask.Val != PermMask.Val)
2685 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2686 }
2687 return Op;
2688 }
2689
2690 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2691 X86::isUNPCKLMask(PermMask.Val) ||
2692 X86::isUNPCKHMask(PermMask.Val))
2693 return Op;
2694
2695 if (V2IsSplat) {
2696 // Normalize mask so all entries that point to V2 points to its first
2697 // element then try to match unpck{h|l} again. If match, return a
2698 // new vector_shuffle with the corrected mask.
2699 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2700 if (NewMask.Val != PermMask.Val) {
2701 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2702 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2703 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2704 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2705 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2706 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2707 }
2708 }
2709 }
2710
2711 // Normalize the node to match x86 shuffle ops if needed
2712 if (V2.getOpcode() != ISD::UNDEF)
2713 if (isCommutedSHUFP(PermMask.Val)) {
2714 Op = CommuteVectorShuffle(Op, DAG);
2715 V1 = Op.getOperand(0);
2716 V2 = Op.getOperand(1);
2717 PermMask = Op.getOperand(2);
2718 }
2719
2720 // If VT is integer, try PSHUF* first, then SHUFP*.
2721 if (MVT::isInteger(VT)) {
2722 if (X86::isPSHUFDMask(PermMask.Val) ||
2723 X86::isPSHUFHWMask(PermMask.Val) ||
2724 X86::isPSHUFLWMask(PermMask.Val)) {
2725 if (V2.getOpcode() != ISD::UNDEF)
2726 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2727 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2728 return Op;
2729 }
2730
2731 if (X86::isSHUFPMask(PermMask.Val))
2732 return Op;
2733
2734 // Handle v8i16 shuffle high / low shuffle node pair.
2735 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2736 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2737 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2738 std::vector<SDOperand> MaskVec;
2739 for (unsigned i = 0; i != 4; ++i)
2740 MaskVec.push_back(PermMask.getOperand(i));
2741 for (unsigned i = 4; i != 8; ++i)
2742 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2743 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2744 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2745 MaskVec.clear();
2746 for (unsigned i = 0; i != 4; ++i)
2747 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2748 for (unsigned i = 4; i != 8; ++i)
2749 MaskVec.push_back(PermMask.getOperand(i));
2750 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2751 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2752 }
2753 } else {
2754 // Floating point cases in the other order.
2755 if (X86::isSHUFPMask(PermMask.Val))
2756 return Op;
2757 if (X86::isPSHUFDMask(PermMask.Val) ||
2758 X86::isPSHUFHWMask(PermMask.Val) ||
2759 X86::isPSHUFLWMask(PermMask.Val)) {
2760 if (V2.getOpcode() != ISD::UNDEF)
2761 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2762 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2763 return Op;
2764 }
2765 }
2766
2767 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002768 MVT::ValueType MaskVT = PermMask.getValueType();
2769 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng3cd43622006-04-28 07:03:38 +00002770 std::vector<std::pair<int, int> > Locs;
2771 Locs.reserve(NumElems);
2772 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2773 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2774 unsigned NumHi = 0;
2775 unsigned NumLo = 0;
2776 // If no more than two elements come from either vector. This can be
2777 // implemented with two shuffles. First shuffle gather the elements.
2778 // The second shuffle, which takes the first shuffle as both of its
2779 // vector operands, put the elements into the right order.
2780 for (unsigned i = 0; i != NumElems; ++i) {
2781 SDOperand Elt = PermMask.getOperand(i);
2782 if (Elt.getOpcode() == ISD::UNDEF) {
2783 Locs[i] = std::make_pair(-1, -1);
2784 } else {
2785 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2786 if (Val < NumElems) {
2787 Locs[i] = std::make_pair(0, NumLo);
2788 Mask1[NumLo] = Elt;
2789 NumLo++;
2790 } else {
2791 Locs[i] = std::make_pair(1, NumHi);
2792 if (2+NumHi < NumElems)
2793 Mask1[2+NumHi] = Elt;
2794 NumHi++;
2795 }
2796 }
2797 }
2798 if (NumLo <= 2 && NumHi <= 2) {
2799 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2800 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2801 for (unsigned i = 0; i != NumElems; ++i) {
2802 if (Locs[i].first == -1)
2803 continue;
2804 else {
2805 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2806 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2807 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2808 }
2809 }
2810
2811 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2812 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2813 }
2814
2815 // Break it into (shuffle shuffle_hi, shuffle_lo).
2816 Locs.clear();
Evan Chenga9467aa2006-04-25 20:13:52 +00002817 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2818 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2819 std::vector<SDOperand> *MaskPtr = &LoMask;
2820 unsigned MaskIdx = 0;
2821 unsigned LoIdx = 0;
2822 unsigned HiIdx = NumElems/2;
2823 for (unsigned i = 0; i != NumElems; ++i) {
2824 if (i == NumElems/2) {
2825 MaskPtr = &HiMask;
2826 MaskIdx = 1;
2827 LoIdx = 0;
2828 HiIdx = NumElems/2;
2829 }
2830 SDOperand Elt = PermMask.getOperand(i);
2831 if (Elt.getOpcode() == ISD::UNDEF) {
2832 Locs[i] = std::make_pair(-1, -1);
2833 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2834 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2835 (*MaskPtr)[LoIdx] = Elt;
2836 LoIdx++;
2837 } else {
2838 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2839 (*MaskPtr)[HiIdx] = Elt;
2840 HiIdx++;
2841 }
2842 }
2843
Chris Lattner3d826992006-05-16 06:45:34 +00002844 SDOperand LoShuffle =
2845 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2846 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2847 SDOperand HiShuffle =
2848 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2849 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Chenga9467aa2006-04-25 20:13:52 +00002850 std::vector<SDOperand> MaskOps;
2851 for (unsigned i = 0; i != NumElems; ++i) {
2852 if (Locs[i].first == -1) {
2853 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2854 } else {
2855 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2856 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2857 }
2858 }
2859 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2860 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2861 }
2862
2863 return SDOperand();
2864}
2865
2866SDOperand
2867X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2868 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2869 return SDOperand();
2870
2871 MVT::ValueType VT = Op.getValueType();
2872 // TODO: handle v16i8.
2873 if (MVT::getSizeInBits(VT) == 16) {
2874 // Transform it so it match pextrw which produces a 32-bit result.
2875 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2876 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2877 Op.getOperand(0), Op.getOperand(1));
2878 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2879 DAG.getValueType(VT));
2880 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2881 } else if (MVT::getSizeInBits(VT) == 32) {
2882 SDOperand Vec = Op.getOperand(0);
2883 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2884 if (Idx == 0)
2885 return Op;
2886
2887 // SHUFPS the element to the lowest double word, then movss.
2888 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2889 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2890 MVT::getVectorBaseType(MaskVT));
2891 std::vector<SDOperand> IdxVec;
2892 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2893 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2894 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2895 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2896 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2897 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2898 Vec, Vec, Mask);
2899 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2900 DAG.getConstant(0, MVT::i32));
2901 } else if (MVT::getSizeInBits(VT) == 64) {
2902 SDOperand Vec = Op.getOperand(0);
2903 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2904 if (Idx == 0)
2905 return Op;
2906
2907 // UNPCKHPD the element to the lowest double word, then movsd.
2908 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2909 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2910 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2911 std::vector<SDOperand> IdxVec;
2912 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2913 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2914 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2915 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2916 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2917 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2918 DAG.getConstant(0, MVT::i32));
2919 }
2920
2921 return SDOperand();
2922}
2923
2924SDOperand
2925X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002926 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002927 // as its second argument.
2928 MVT::ValueType VT = Op.getValueType();
2929 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2930 SDOperand N0 = Op.getOperand(0);
2931 SDOperand N1 = Op.getOperand(1);
2932 SDOperand N2 = Op.getOperand(2);
2933 if (MVT::getSizeInBits(BaseVT) == 16) {
2934 if (N1.getValueType() != MVT::i32)
2935 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2936 if (N2.getValueType() != MVT::i32)
2937 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2938 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2939 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2940 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2941 if (Idx == 0) {
2942 // Use a movss.
2943 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2944 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2945 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2946 std::vector<SDOperand> MaskVec;
2947 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2948 for (unsigned i = 1; i <= 3; ++i)
2949 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2950 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2951 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2952 } else {
2953 // Use two pinsrw instructions to insert a 32 bit value.
2954 Idx <<= 1;
2955 if (MVT::isFloatingPoint(N1.getValueType())) {
2956 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002957 // Just load directly from f32mem to GR32.
Evan Chenga9467aa2006-04-25 20:13:52 +00002958 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2959 N1.getOperand(2));
2960 } else {
2961 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2962 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2963 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2964 DAG.getConstant(0, MVT::i32));
2965 }
2966 }
2967 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2968 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2969 DAG.getConstant(Idx, MVT::i32));
2970 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2971 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2972 DAG.getConstant(Idx+1, MVT::i32));
2973 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2974 }
2975 }
2976
2977 return SDOperand();
2978}
2979
2980SDOperand
2981X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2982 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2983 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2984}
2985
2986// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2987// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2988// one of the above mentioned nodes. It has to be wrapped because otherwise
2989// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2990// be used to form addressing mode. These wrapped nodes will be selected
2991// into MOV32ri.
2992SDOperand
2993X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2994 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2995 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2996 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2997 CP->getAlignment()));
2998 if (Subtarget->isTargetDarwin()) {
2999 // With PIC, the address is actually $g + Offset.
3000 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3001 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3002 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
3003 }
3004
3005 return Result;
3006}
3007
3008SDOperand
3009X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3010 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3011 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003012 DAG.getTargetGlobalAddress(GV,
3013 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003014 if (Subtarget->isTargetDarwin()) {
3015 // With PIC, the address is actually $g + Offset.
3016 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3017 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003018 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3019 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003020
3021 // For Darwin, external and weak symbols are indirect, so we want to load
3022 // the value at address GV, not the value of GV itself. This means that
3023 // the GlobalAddress must be in the base or index register of the address,
3024 // not the GV offset field.
3025 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
3026 DarwinGVRequiresExtraLoad(GV))
3027 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
3028 Result, DAG.getSrcValue(NULL));
3029 }
3030
3031 return Result;
3032}
3033
3034SDOperand
3035X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3036 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3037 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003038 DAG.getTargetExternalSymbol(Sym,
3039 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003040 if (Subtarget->isTargetDarwin()) {
3041 // With PIC, the address is actually $g + Offset.
3042 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3043 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003044 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3045 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003046 }
3047
3048 return Result;
3049}
3050
3051SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00003052 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3053 "Not an i64 shift!");
3054 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3055 SDOperand ShOpLo = Op.getOperand(0);
3056 SDOperand ShOpHi = Op.getOperand(1);
3057 SDOperand ShAmt = Op.getOperand(2);
3058 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00003059 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00003060 : DAG.getConstant(0, MVT::i32);
3061
3062 SDOperand Tmp2, Tmp3;
3063 if (Op.getOpcode() == ISD::SHL_PARTS) {
3064 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3065 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3066 } else {
3067 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00003068 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00003069 }
3070
3071 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
3072 ShAmt, DAG.getConstant(32, MVT::i8));
3073
3074 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00003075 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00003076
3077 std::vector<MVT::ValueType> Tys;
3078 Tys.push_back(MVT::i32);
3079 Tys.push_back(MVT::Flag);
3080 std::vector<SDOperand> Ops;
3081 if (Op.getOpcode() == ISD::SHL_PARTS) {
3082 Ops.push_back(Tmp2);
3083 Ops.push_back(Tmp3);
3084 Ops.push_back(CC);
3085 Ops.push_back(InFlag);
3086 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3087 InFlag = Hi.getValue(1);
3088
3089 Ops.clear();
3090 Ops.push_back(Tmp3);
3091 Ops.push_back(Tmp1);
3092 Ops.push_back(CC);
3093 Ops.push_back(InFlag);
3094 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3095 } else {
3096 Ops.push_back(Tmp2);
3097 Ops.push_back(Tmp3);
3098 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00003099 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00003100 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3101 InFlag = Lo.getValue(1);
3102
3103 Ops.clear();
3104 Ops.push_back(Tmp3);
3105 Ops.push_back(Tmp1);
3106 Ops.push_back(CC);
3107 Ops.push_back(InFlag);
3108 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3109 }
3110
3111 Tys.clear();
3112 Tys.push_back(MVT::i32);
3113 Tys.push_back(MVT::i32);
3114 Ops.clear();
3115 Ops.push_back(Lo);
3116 Ops.push_back(Hi);
3117 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Chenga9467aa2006-04-25 20:13:52 +00003118}
Evan Cheng6305e502006-01-12 22:54:21 +00003119
Evan Chenga9467aa2006-04-25 20:13:52 +00003120SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3121 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3122 Op.getOperand(0).getValueType() >= MVT::i16 &&
3123 "Unknown SINT_TO_FP to lower!");
3124
3125 SDOperand Result;
3126 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3127 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3128 MachineFunction &MF = DAG.getMachineFunction();
3129 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3130 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3131 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
3132 DAG.getEntryNode(), Op.getOperand(0),
3133 StackSlot, DAG.getSrcValue(NULL));
3134
3135 // Build the FILD
3136 std::vector<MVT::ValueType> Tys;
3137 Tys.push_back(MVT::f64);
3138 Tys.push_back(MVT::Other);
3139 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3140 std::vector<SDOperand> Ops;
3141 Ops.push_back(Chain);
3142 Ops.push_back(StackSlot);
3143 Ops.push_back(DAG.getValueType(SrcVT));
3144 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3145 Tys, Ops);
3146
3147 if (X86ScalarSSE) {
3148 Chain = Result.getValue(1);
3149 SDOperand InFlag = Result.getValue(2);
3150
3151 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3152 // shouldn't be necessary except that RFP cannot be live across
3153 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003154 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003155 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003156 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00003157 std::vector<MVT::ValueType> Tys;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003158 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003159 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003160 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003161 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003162 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003163 Ops.push_back(DAG.getValueType(Op.getValueType()));
3164 Ops.push_back(InFlag);
3165 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3166 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3167 DAG.getSrcValue(NULL));
Chris Lattner76ac0682005-11-15 00:40:23 +00003168 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003169
Evan Chenga9467aa2006-04-25 20:13:52 +00003170 return Result;
3171}
3172
3173SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3174 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3175 "Unknown FP_TO_SINT to lower!");
3176 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3177 // stack slot.
3178 MachineFunction &MF = DAG.getMachineFunction();
3179 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3180 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3181 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3182
3183 unsigned Opc;
3184 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003185 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3186 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3187 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3188 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003189 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003190
Evan Chenga9467aa2006-04-25 20:13:52 +00003191 SDOperand Chain = DAG.getEntryNode();
3192 SDOperand Value = Op.getOperand(0);
3193 if (X86ScalarSSE) {
3194 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3195 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3196 DAG.getSrcValue(0));
3197 std::vector<MVT::ValueType> Tys;
3198 Tys.push_back(MVT::f64);
3199 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003200 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003201 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00003202 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003203 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3204 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3205 Chain = Value.getValue(1);
3206 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3207 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3208 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003209
Evan Chenga9467aa2006-04-25 20:13:52 +00003210 // Build the FP_TO_INT*_IN_MEM
3211 std::vector<SDOperand> Ops;
3212 Ops.push_back(Chain);
3213 Ops.push_back(Value);
3214 Ops.push_back(StackSlot);
3215 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Cheng172fce72006-01-06 00:43:03 +00003216
Evan Chenga9467aa2006-04-25 20:13:52 +00003217 // Load the result.
3218 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3219 DAG.getSrcValue(NULL));
3220}
3221
3222SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3223 MVT::ValueType VT = Op.getValueType();
3224 const Type *OpNTy = MVT::getTypeForValueType(VT);
3225 std::vector<Constant*> CV;
3226 if (VT == MVT::f64) {
3227 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3228 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3229 } else {
3230 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3231 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3232 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3233 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3234 }
3235 Constant *CS = ConstantStruct::get(CV);
3236 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3237 SDOperand Mask
3238 = DAG.getNode(X86ISD::LOAD_PACK,
3239 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3240 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3241}
3242
3243SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3244 MVT::ValueType VT = Op.getValueType();
3245 const Type *OpNTy = MVT::getTypeForValueType(VT);
3246 std::vector<Constant*> CV;
3247 if (VT == MVT::f64) {
3248 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3249 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3250 } else {
3251 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3252 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3253 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3254 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3255 }
3256 Constant *CS = ConstantStruct::get(CV);
3257 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3258 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3259 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3260 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3261}
3262
3263SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3264 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3265 SDOperand Cond;
3266 SDOperand CC = Op.getOperand(2);
3267 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3268 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3269 bool Flip;
3270 unsigned X86CC;
3271 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3272 if (Flip)
3273 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3274 Op.getOperand(1), Op.getOperand(0));
3275 else
Evan Cheng45df7f82006-01-30 23:41:35 +00003276 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3277 Op.getOperand(0), Op.getOperand(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003278 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3279 DAG.getConstant(X86CC, MVT::i8), Cond);
3280 } else {
3281 assert(isFP && "Illegal integer SetCC!");
3282
3283 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3284 Op.getOperand(0), Op.getOperand(1));
3285 std::vector<MVT::ValueType> Tys;
3286 std::vector<SDOperand> Ops;
3287 switch (SetCCOpcode) {
Evan Cheng172fce72006-01-06 00:43:03 +00003288 default: assert(false && "Illegal floating point SetCC!");
3289 case ISD::SETOEQ: { // !PF & ZF
3290 Tys.push_back(MVT::i8);
3291 Tys.push_back(MVT::Flag);
3292 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3293 Ops.push_back(Cond);
3294 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3295 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3296 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3297 Tmp1.getValue(1));
3298 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3299 }
Evan Cheng172fce72006-01-06 00:43:03 +00003300 case ISD::SETUNE: { // PF | !ZF
3301 Tys.push_back(MVT::i8);
3302 Tys.push_back(MVT::Flag);
3303 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3304 Ops.push_back(Cond);
3305 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3306 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3307 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3308 Tmp1.getValue(1));
3309 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3310 }
Evan Cheng172fce72006-01-06 00:43:03 +00003311 }
Evan Chengc1583db2005-12-21 20:21:51 +00003312 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003313}
Evan Cheng45df7f82006-01-30 23:41:35 +00003314
Evan Chenga9467aa2006-04-25 20:13:52 +00003315SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3316 MVT::ValueType VT = Op.getValueType();
3317 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3318 bool addTest = false;
3319 SDOperand Op0 = Op.getOperand(0);
3320 SDOperand Cond, CC;
3321 if (Op0.getOpcode() == ISD::SETCC)
3322 Op0 = LowerOperation(Op0, DAG);
Evan Cheng944d1e92006-01-26 02:13:10 +00003323
Evan Chenga9467aa2006-04-25 20:13:52 +00003324 if (Op0.getOpcode() == X86ISD::SETCC) {
3325 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3326 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3327 // have another use it will be eliminated.
3328 // If the X86ISD::SETCC has more than one use, then it's probably better
3329 // to use a test instead of duplicating the X86ISD::CMP (for register
3330 // pressure reason).
3331 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3332 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3333 CmpOpc == X86ISD::UCOMI) {
3334 if (!Op0.hasOneUse()) {
3335 std::vector<MVT::ValueType> Tys;
3336 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3337 Tys.push_back(Op0.Val->getValueType(i));
3338 std::vector<SDOperand> Ops;
3339 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3340 Ops.push_back(Op0.getOperand(i));
3341 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3342 }
3343
3344 CC = Op0.getOperand(0);
3345 Cond = Op0.getOperand(1);
3346 // Make a copy as flag result cannot be used by more than one.
3347 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3348 Cond.getOperand(0), Cond.getOperand(1));
3349 addTest =
3350 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00003351 } else
3352 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003353 } else
3354 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00003355
Evan Chenga9467aa2006-04-25 20:13:52 +00003356 if (addTest) {
3357 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3358 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00003359 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003360
Evan Chenga9467aa2006-04-25 20:13:52 +00003361 std::vector<MVT::ValueType> Tys;
3362 Tys.push_back(Op.getValueType());
3363 Tys.push_back(MVT::Flag);
3364 std::vector<SDOperand> Ops;
3365 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3366 // condition is true.
3367 Ops.push_back(Op.getOperand(2));
3368 Ops.push_back(Op.getOperand(1));
3369 Ops.push_back(CC);
3370 Ops.push_back(Cond);
3371 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3372}
Evan Cheng944d1e92006-01-26 02:13:10 +00003373
Evan Chenga9467aa2006-04-25 20:13:52 +00003374SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3375 bool addTest = false;
3376 SDOperand Cond = Op.getOperand(1);
3377 SDOperand Dest = Op.getOperand(2);
3378 SDOperand CC;
3379 if (Cond.getOpcode() == ISD::SETCC)
3380 Cond = LowerOperation(Cond, DAG);
3381
3382 if (Cond.getOpcode() == X86ISD::SETCC) {
3383 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3384 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3385 // have another use it will be eliminated.
3386 // If the X86ISD::SETCC has more than one use, then it's probably better
3387 // to use a test instead of duplicating the X86ISD::CMP (for register
3388 // pressure reason).
3389 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3390 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3391 CmpOpc == X86ISD::UCOMI) {
3392 if (!Cond.hasOneUse()) {
3393 std::vector<MVT::ValueType> Tys;
3394 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3395 Tys.push_back(Cond.Val->getValueType(i));
3396 std::vector<SDOperand> Ops;
3397 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3398 Ops.push_back(Cond.getOperand(i));
3399 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3400 }
3401
3402 CC = Cond.getOperand(0);
3403 Cond = Cond.getOperand(1);
3404 // Make a copy as flag result cannot be used by more than one.
3405 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3406 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00003407 } else
3408 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003409 } else
3410 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00003411
Evan Chenga9467aa2006-04-25 20:13:52 +00003412 if (addTest) {
3413 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3414 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng6fc31042005-12-19 23:12:38 +00003415 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003416 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3417 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3418}
Evan Chengae986f12006-01-11 22:15:48 +00003419
Evan Chenga9467aa2006-04-25 20:13:52 +00003420SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3421 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3422 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3423 DAG.getTargetJumpTable(JT->getIndex(),
3424 getPointerTy()));
3425 if (Subtarget->isTargetDarwin()) {
3426 // With PIC, the address is actually $g + Offset.
3427 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3428 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003429 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3430 Result);
Evan Chengae986f12006-01-11 22:15:48 +00003431 }
Evan Cheng99470012006-02-25 09:55:19 +00003432
Evan Chenga9467aa2006-04-25 20:13:52 +00003433 return Result;
3434}
Evan Cheng5588de92006-02-18 00:15:05 +00003435
Evan Chenga9467aa2006-04-25 20:13:52 +00003436SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3437 SDOperand Copy;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003438
Evan Chenga9467aa2006-04-25 20:13:52 +00003439 switch(Op.getNumOperands()) {
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003440 default:
3441 assert(0 && "Do not know how to return this many arguments!");
3442 abort();
Chris Lattnerc070c622006-04-17 20:32:50 +00003443 case 1: // ret void.
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003444 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chenga9467aa2006-04-25 20:13:52 +00003445 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003446 case 2: {
3447 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerc070c622006-04-17 20:32:50 +00003448
3449 if (MVT::isVector(ArgVT)) {
3450 // Integer or FP vector result -> XMM0.
3451 if (DAG.getMachineFunction().liveout_empty())
3452 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3453 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3454 SDOperand());
3455 } else if (MVT::isInteger(ArgVT)) {
3456 // Integer result -> EAX
3457 if (DAG.getMachineFunction().liveout_empty())
3458 DAG.getMachineFunction().addLiveOut(X86::EAX);
3459
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003460 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3461 SDOperand());
Chris Lattnerc070c622006-04-17 20:32:50 +00003462 } else if (!X86ScalarSSE) {
3463 // FP return with fp-stack value.
3464 if (DAG.getMachineFunction().liveout_empty())
3465 DAG.getMachineFunction().addLiveOut(X86::ST0);
3466
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003467 std::vector<MVT::ValueType> Tys;
3468 Tys.push_back(MVT::Other);
3469 Tys.push_back(MVT::Flag);
3470 std::vector<SDOperand> Ops;
3471 Ops.push_back(Op.getOperand(0));
3472 Ops.push_back(Op.getOperand(1));
3473 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3474 } else {
Chris Lattnerc070c622006-04-17 20:32:50 +00003475 // FP return with ScalarSSE (return on fp-stack).
3476 if (DAG.getMachineFunction().liveout_empty())
3477 DAG.getMachineFunction().addLiveOut(X86::ST0);
3478
Evan Chenge1ce4d72006-02-01 00:20:21 +00003479 SDOperand MemLoc;
3480 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00003481 SDOperand Value = Op.getOperand(1);
3482
Evan Chenga24617f2006-02-01 01:19:32 +00003483 if (Value.getOpcode() == ISD::LOAD &&
3484 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00003485 Chain = Value.getOperand(0);
3486 MemLoc = Value.getOperand(1);
3487 } else {
3488 // Spill the value to memory and reload it into top of stack.
3489 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3490 MachineFunction &MF = DAG.getMachineFunction();
3491 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3492 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3493 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3494 Value, MemLoc, DAG.getSrcValue(0));
3495 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003496 std::vector<MVT::ValueType> Tys;
3497 Tys.push_back(MVT::f64);
3498 Tys.push_back(MVT::Other);
3499 std::vector<SDOperand> Ops;
3500 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00003501 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003502 Ops.push_back(DAG.getValueType(ArgVT));
3503 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3504 Tys.clear();
3505 Tys.push_back(MVT::Other);
3506 Tys.push_back(MVT::Flag);
3507 Ops.clear();
3508 Ops.push_back(Copy.getValue(1));
3509 Ops.push_back(Copy);
3510 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3511 }
3512 break;
3513 }
3514 case 3:
Chris Lattnerc070c622006-04-17 20:32:50 +00003515 if (DAG.getMachineFunction().liveout_empty()) {
3516 DAG.getMachineFunction().addLiveOut(X86::EAX);
3517 DAG.getMachineFunction().addLiveOut(X86::EDX);
3518 }
3519
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003520 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3521 SDOperand());
3522 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3523 break;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003524 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003525 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3526 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3527 Copy.getValue(1));
3528}
3529
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003530SDOperand
3531X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3532 if (FormalArgs.size() == 0) {
Chris Lattner3d826992006-05-16 06:45:34 +00003533 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003534 if (CC == CallingConv::Fast && EnableFastCC)
3535 LowerFastCCArguments(Op, DAG);
3536 else
3537 LowerCCCArguments(Op, DAG);
3538 }
Chris Lattnerc7df70d2006-05-16 17:14:26 +00003539
3540 // Return the new list of results.
3541 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
3542 Op.Val->value_end());
3543 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, FormalArgs);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003544}
3545
Evan Chenga9467aa2006-04-25 20:13:52 +00003546SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3547 SDOperand InFlag(0, 0);
3548 SDOperand Chain = Op.getOperand(0);
3549 unsigned Align =
3550 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3551 if (Align == 0) Align = 1;
3552
3553 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3554 // If not DWORD aligned, call memset if size is less than the threshold.
3555 // It knows how to align to the right boundary first.
3556 if ((Align & 3) != 0 ||
3557 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3558 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003559 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003560 std::vector<std::pair<SDOperand, const Type*> > Args;
3561 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3562 // Extend the ubyte argument to be an int value for the call.
3563 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3564 Args.push_back(std::make_pair(Val, IntPtrTy));
3565 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3566 std::pair<SDOperand,SDOperand> CallResult =
3567 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3568 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3569 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003570 }
Evan Chengd097e672006-03-22 02:53:00 +00003571
Evan Chenga9467aa2006-04-25 20:13:52 +00003572 MVT::ValueType AVT;
3573 SDOperand Count;
3574 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3575 unsigned BytesLeft = 0;
3576 bool TwoRepStos = false;
3577 if (ValC) {
3578 unsigned ValReg;
3579 unsigned Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003580
Evan Chenga9467aa2006-04-25 20:13:52 +00003581 // If the value is a constant, then we can potentially use larger sets.
3582 switch (Align & 3) {
3583 case 2: // WORD aligned
3584 AVT = MVT::i16;
3585 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3586 BytesLeft = I->getValue() % 2;
3587 Val = (Val << 8) | Val;
3588 ValReg = X86::AX;
3589 break;
3590 case 0: // DWORD aligned
3591 AVT = MVT::i32;
3592 if (I) {
3593 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3594 BytesLeft = I->getValue() % 4;
Evan Chenga3caaee2006-04-19 22:48:17 +00003595 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003596 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3597 DAG.getConstant(2, MVT::i8));
3598 TwoRepStos = true;
Evan Chenga3caaee2006-04-19 22:48:17 +00003599 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003600 Val = (Val << 8) | Val;
3601 Val = (Val << 16) | Val;
3602 ValReg = X86::EAX;
3603 break;
3604 default: // Byte aligned
3605 AVT = MVT::i8;
3606 Count = Op.getOperand(3);
3607 ValReg = X86::AL;
3608 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003609 }
3610
Evan Chenga9467aa2006-04-25 20:13:52 +00003611 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3612 InFlag);
3613 InFlag = Chain.getValue(1);
3614 } else {
3615 AVT = MVT::i8;
3616 Count = Op.getOperand(3);
3617 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3618 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003619 }
Evan Chengb0461082006-04-24 18:01:45 +00003620
Evan Chenga9467aa2006-04-25 20:13:52 +00003621 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3622 InFlag = Chain.getValue(1);
3623 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3624 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003625
Evan Chenga9467aa2006-04-25 20:13:52 +00003626 std::vector<MVT::ValueType> Tys;
3627 Tys.push_back(MVT::Other);
3628 Tys.push_back(MVT::Flag);
3629 std::vector<SDOperand> Ops;
3630 Ops.push_back(Chain);
3631 Ops.push_back(DAG.getValueType(AVT));
3632 Ops.push_back(InFlag);
3633 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengb0461082006-04-24 18:01:45 +00003634
Evan Chenga9467aa2006-04-25 20:13:52 +00003635 if (TwoRepStos) {
3636 InFlag = Chain.getValue(1);
3637 Count = Op.getOperand(3);
3638 MVT::ValueType CVT = Count.getValueType();
3639 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3640 DAG.getConstant(3, CVT));
3641 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3642 InFlag = Chain.getValue(1);
3643 Tys.clear();
3644 Tys.push_back(MVT::Other);
3645 Tys.push_back(MVT::Flag);
3646 Ops.clear();
3647 Ops.push_back(Chain);
3648 Ops.push_back(DAG.getValueType(MVT::i8));
3649 Ops.push_back(InFlag);
3650 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3651 } else if (BytesLeft) {
3652 // Issue stores for the last 1 - 3 bytes.
3653 SDOperand Value;
3654 unsigned Val = ValC->getValue() & 255;
3655 unsigned Offset = I->getValue() - BytesLeft;
3656 SDOperand DstAddr = Op.getOperand(1);
3657 MVT::ValueType AddrVT = DstAddr.getValueType();
3658 if (BytesLeft >= 2) {
3659 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3660 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3661 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3662 DAG.getConstant(Offset, AddrVT)),
3663 DAG.getSrcValue(NULL));
3664 BytesLeft -= 2;
3665 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003666 }
3667
Evan Chenga9467aa2006-04-25 20:13:52 +00003668 if (BytesLeft == 1) {
3669 Value = DAG.getConstant(Val, MVT::i8);
3670 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3671 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3672 DAG.getConstant(Offset, AddrVT)),
3673 DAG.getSrcValue(NULL));
Evan Cheng14215c32006-04-21 23:03:30 +00003674 }
Evan Cheng082c8782006-03-24 07:29:27 +00003675 }
Evan Chengebf10062006-04-03 20:53:28 +00003676
Evan Chenga9467aa2006-04-25 20:13:52 +00003677 return Chain;
3678}
Evan Chengebf10062006-04-03 20:53:28 +00003679
Evan Chenga9467aa2006-04-25 20:13:52 +00003680SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3681 SDOperand Chain = Op.getOperand(0);
3682 unsigned Align =
3683 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3684 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003685
Evan Chenga9467aa2006-04-25 20:13:52 +00003686 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3687 // If not DWORD aligned, call memcpy if size is less than the threshold.
3688 // It knows how to align to the right boundary first.
3689 if ((Align & 3) != 0 ||
3690 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3691 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003692 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003693 std::vector<std::pair<SDOperand, const Type*> > Args;
3694 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3695 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3696 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3697 std::pair<SDOperand,SDOperand> CallResult =
3698 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3699 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3700 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003701 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003702
3703 MVT::ValueType AVT;
3704 SDOperand Count;
3705 unsigned BytesLeft = 0;
3706 bool TwoRepMovs = false;
3707 switch (Align & 3) {
3708 case 2: // WORD aligned
3709 AVT = MVT::i16;
3710 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3711 BytesLeft = I->getValue() % 2;
3712 break;
3713 case 0: // DWORD aligned
3714 AVT = MVT::i32;
3715 if (I) {
3716 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3717 BytesLeft = I->getValue() % 4;
Evan Cheng54212062006-04-17 22:45:49 +00003718 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003719 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3720 DAG.getConstant(2, MVT::i8));
3721 TwoRepMovs = true;
Evan Cheng6e5e2052006-04-17 22:04:06 +00003722 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003723 break;
3724 default: // Byte aligned
3725 AVT = MVT::i8;
3726 Count = Op.getOperand(3);
3727 break;
3728 }
3729
3730 SDOperand InFlag(0, 0);
3731 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3732 InFlag = Chain.getValue(1);
3733 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3734 InFlag = Chain.getValue(1);
3735 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3736 InFlag = Chain.getValue(1);
3737
3738 std::vector<MVT::ValueType> Tys;
3739 Tys.push_back(MVT::Other);
3740 Tys.push_back(MVT::Flag);
3741 std::vector<SDOperand> Ops;
3742 Ops.push_back(Chain);
3743 Ops.push_back(DAG.getValueType(AVT));
3744 Ops.push_back(InFlag);
3745 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3746
3747 if (TwoRepMovs) {
3748 InFlag = Chain.getValue(1);
3749 Count = Op.getOperand(3);
3750 MVT::ValueType CVT = Count.getValueType();
3751 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3752 DAG.getConstant(3, CVT));
3753 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3754 InFlag = Chain.getValue(1);
3755 Tys.clear();
3756 Tys.push_back(MVT::Other);
3757 Tys.push_back(MVT::Flag);
3758 Ops.clear();
3759 Ops.push_back(Chain);
3760 Ops.push_back(DAG.getValueType(MVT::i8));
3761 Ops.push_back(InFlag);
3762 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3763 } else if (BytesLeft) {
3764 // Issue loads and stores for the last 1 - 3 bytes.
3765 unsigned Offset = I->getValue() - BytesLeft;
3766 SDOperand DstAddr = Op.getOperand(1);
3767 MVT::ValueType DstVT = DstAddr.getValueType();
3768 SDOperand SrcAddr = Op.getOperand(2);
3769 MVT::ValueType SrcVT = SrcAddr.getValueType();
3770 SDOperand Value;
3771 if (BytesLeft >= 2) {
3772 Value = DAG.getLoad(MVT::i16, Chain,
3773 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3774 DAG.getConstant(Offset, SrcVT)),
3775 DAG.getSrcValue(NULL));
3776 Chain = Value.getValue(1);
3777 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3778 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3779 DAG.getConstant(Offset, DstVT)),
3780 DAG.getSrcValue(NULL));
3781 BytesLeft -= 2;
3782 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003783 }
3784
Evan Chenga9467aa2006-04-25 20:13:52 +00003785 if (BytesLeft == 1) {
3786 Value = DAG.getLoad(MVT::i8, Chain,
3787 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3788 DAG.getConstant(Offset, SrcVT)),
3789 DAG.getSrcValue(NULL));
3790 Chain = Value.getValue(1);
3791 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3792 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3793 DAG.getConstant(Offset, DstVT)),
3794 DAG.getSrcValue(NULL));
3795 }
Evan Chengcbffa462006-03-31 19:22:53 +00003796 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003797
3798 return Chain;
3799}
3800
3801SDOperand
3802X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3803 std::vector<MVT::ValueType> Tys;
3804 Tys.push_back(MVT::Other);
3805 Tys.push_back(MVT::Flag);
3806 std::vector<SDOperand> Ops;
3807 Ops.push_back(Op.getOperand(0));
3808 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3809 Ops.clear();
3810 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3811 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3812 MVT::i32, Ops[0].getValue(2)));
3813 Ops.push_back(Ops[1].getValue(1));
3814 Tys[0] = Tys[1] = MVT::i32;
3815 Tys.push_back(MVT::Other);
3816 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3817}
3818
3819SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3820 // vastart just stores the address of the VarArgsFrameIndex slot into the
3821 // memory location argument.
3822 // FIXME: Replace MVT::i32 with PointerTy
3823 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3824 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3825 Op.getOperand(1), Op.getOperand(2));
3826}
3827
3828SDOperand
3829X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3830 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3831 switch (IntNo) {
3832 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003833 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003834 case Intrinsic::x86_sse_comieq_ss:
3835 case Intrinsic::x86_sse_comilt_ss:
3836 case Intrinsic::x86_sse_comile_ss:
3837 case Intrinsic::x86_sse_comigt_ss:
3838 case Intrinsic::x86_sse_comige_ss:
3839 case Intrinsic::x86_sse_comineq_ss:
3840 case Intrinsic::x86_sse_ucomieq_ss:
3841 case Intrinsic::x86_sse_ucomilt_ss:
3842 case Intrinsic::x86_sse_ucomile_ss:
3843 case Intrinsic::x86_sse_ucomigt_ss:
3844 case Intrinsic::x86_sse_ucomige_ss:
3845 case Intrinsic::x86_sse_ucomineq_ss:
3846 case Intrinsic::x86_sse2_comieq_sd:
3847 case Intrinsic::x86_sse2_comilt_sd:
3848 case Intrinsic::x86_sse2_comile_sd:
3849 case Intrinsic::x86_sse2_comigt_sd:
3850 case Intrinsic::x86_sse2_comige_sd:
3851 case Intrinsic::x86_sse2_comineq_sd:
3852 case Intrinsic::x86_sse2_ucomieq_sd:
3853 case Intrinsic::x86_sse2_ucomilt_sd:
3854 case Intrinsic::x86_sse2_ucomile_sd:
3855 case Intrinsic::x86_sse2_ucomigt_sd:
3856 case Intrinsic::x86_sse2_ucomige_sd:
3857 case Intrinsic::x86_sse2_ucomineq_sd: {
3858 unsigned Opc = 0;
3859 ISD::CondCode CC = ISD::SETCC_INVALID;
3860 switch (IntNo) {
3861 default: break;
3862 case Intrinsic::x86_sse_comieq_ss:
3863 case Intrinsic::x86_sse2_comieq_sd:
3864 Opc = X86ISD::COMI;
3865 CC = ISD::SETEQ;
3866 break;
Evan Cheng78038292006-04-05 23:38:46 +00003867 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003868 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003869 Opc = X86ISD::COMI;
3870 CC = ISD::SETLT;
3871 break;
3872 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003873 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003874 Opc = X86ISD::COMI;
3875 CC = ISD::SETLE;
3876 break;
3877 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003878 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003879 Opc = X86ISD::COMI;
3880 CC = ISD::SETGT;
3881 break;
3882 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003883 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003884 Opc = X86ISD::COMI;
3885 CC = ISD::SETGE;
3886 break;
3887 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003888 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003889 Opc = X86ISD::COMI;
3890 CC = ISD::SETNE;
3891 break;
3892 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003893 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003894 Opc = X86ISD::UCOMI;
3895 CC = ISD::SETEQ;
3896 break;
3897 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003898 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003899 Opc = X86ISD::UCOMI;
3900 CC = ISD::SETLT;
3901 break;
3902 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003903 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003904 Opc = X86ISD::UCOMI;
3905 CC = ISD::SETLE;
3906 break;
3907 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003908 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003909 Opc = X86ISD::UCOMI;
3910 CC = ISD::SETGT;
3911 break;
3912 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003913 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003914 Opc = X86ISD::UCOMI;
3915 CC = ISD::SETGE;
3916 break;
3917 case Intrinsic::x86_sse_ucomineq_ss:
3918 case Intrinsic::x86_sse2_ucomineq_sd:
3919 Opc = X86ISD::UCOMI;
3920 CC = ISD::SETNE;
3921 break;
Evan Cheng78038292006-04-05 23:38:46 +00003922 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003923 bool Flip;
3924 unsigned X86CC;
3925 translateX86CC(CC, true, X86CC, Flip);
3926 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3927 Op.getOperand(Flip?1:2));
3928 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3929 DAG.getConstant(X86CC, MVT::i8), Cond);
3930 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003931 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003932 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003933}
Evan Cheng6af02632005-12-20 06:22:03 +00003934
Evan Chenga9467aa2006-04-25 20:13:52 +00003935/// LowerOperation - Provide custom lowering hooks for some operations.
3936///
3937SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3938 switch (Op.getOpcode()) {
3939 default: assert(0 && "Should not custom lower this!");
3940 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3941 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3942 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3943 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3944 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3945 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3946 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3947 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3948 case ISD::SHL_PARTS:
3949 case ISD::SRA_PARTS:
3950 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3951 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3952 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3953 case ISD::FABS: return LowerFABS(Op, DAG);
3954 case ISD::FNEG: return LowerFNEG(Op, DAG);
3955 case ISD::SETCC: return LowerSETCC(Op, DAG);
3956 case ISD::SELECT: return LowerSELECT(Op, DAG);
3957 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3958 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3959 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003960 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003961 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3962 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3963 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3964 case ISD::VASTART: return LowerVASTART(Op, DAG);
3965 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3966 }
3967}
3968
Evan Cheng6af02632005-12-20 06:22:03 +00003969const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3970 switch (Opcode) {
3971 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003972 case X86ISD::SHLD: return "X86ISD::SHLD";
3973 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003974 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003975 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003976 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003977 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003978 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3979 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3980 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003981 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003982 case X86ISD::FST: return "X86ISD::FST";
3983 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003984 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003985 case X86ISD::CALL: return "X86ISD::CALL";
3986 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3987 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3988 case X86ISD::CMP: return "X86ISD::CMP";
3989 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003990 case X86ISD::COMI: return "X86ISD::COMI";
3991 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003992 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003993 case X86ISD::CMOV: return "X86ISD::CMOV";
3994 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003995 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003996 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3997 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003998 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003999 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00004000 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00004001 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00004002 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00004003 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00004004 }
4005}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004006
Nate Begeman8a77efe2006-02-16 21:11:51 +00004007void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4008 uint64_t Mask,
4009 uint64_t &KnownZero,
4010 uint64_t &KnownOne,
4011 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004012 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00004013 assert((Opc >= ISD::BUILTIN_OP_END ||
4014 Opc == ISD::INTRINSIC_WO_CHAIN ||
4015 Opc == ISD::INTRINSIC_W_CHAIN ||
4016 Opc == ISD::INTRINSIC_VOID) &&
4017 "Should use MaskedValueIsZero if you don't know whether Op"
4018 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004019
Evan Cheng6d196db2006-04-05 06:11:20 +00004020 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004021 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00004022 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00004023 case X86ISD::SETCC:
4024 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4025 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004026 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004027}
Chris Lattnerc642aa52006-01-31 19:43:35 +00004028
4029std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00004030getRegClassForInlineAsmConstraint(const std::string &Constraint,
4031 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00004032 if (Constraint.size() == 1) {
4033 // FIXME: not handling fp-stack yet!
4034 // FIXME: not handling MMX registers yet ('y' constraint).
4035 switch (Constraint[0]) { // GCC X86 Constraint Letters
4036 default: break; // Unknown constriant letter
4037 case 'r': // GENERAL_REGS
4038 case 'R': // LEGACY_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004039 if (VT == MVT::i32)
4040 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4041 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4042 else if (VT == MVT::i16)
4043 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4044 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4045 else if (VT == MVT::i8)
4046 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4047 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004048 case 'l': // INDEX_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004049 if (VT == MVT::i32)
4050 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4051 X86::ESI, X86::EDI, X86::EBP, 0);
4052 else if (VT == MVT::i16)
4053 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4054 X86::SI, X86::DI, X86::BP, 0);
4055 else if (VT == MVT::i8)
4056 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4057 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004058 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4059 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004060 if (VT == MVT::i32)
4061 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4062 else if (VT == MVT::i16)
4063 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4064 else if (VT == MVT::i8)
4065 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4066 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004067 case 'x': // SSE_REGS if SSE1 allowed
4068 if (Subtarget->hasSSE1())
4069 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4070 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4071 0);
4072 return std::vector<unsigned>();
4073 case 'Y': // SSE_REGS if SSE2 allowed
4074 if (Subtarget->hasSSE2())
4075 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4076 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4077 0);
4078 return std::vector<unsigned>();
4079 }
4080 }
4081
Chris Lattner7ad77df2006-02-22 00:56:39 +00004082 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00004083}
Evan Chengaf598d22006-03-13 23:18:16 +00004084
4085/// isLegalAddressImmediate - Return true if the integer value or
4086/// GlobalValue can be used as the offset of the target addressing mode.
4087bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4088 // X86 allows a sign-extended 32-bit immediate field.
4089 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4090}
4091
4092bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00004093 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00004094 Reloc::Model RModel = getTargetMachine().getRelocationModel();
4095 if (RModel == Reloc::Static)
4096 return true;
4097 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00004098 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00004099 else
4100 return false;
4101 } else
4102 return true;
4103}
Evan Cheng68ad48b2006-03-22 18:59:22 +00004104
4105/// isShuffleMaskLegal - Targets can use this to indicate that they only
4106/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4107/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4108/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00004109bool
4110X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4111 // Only do shuffles on 128-bit vector types for now.
4112 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Chenga3caaee2006-04-19 22:48:17 +00004113 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng5022b342006-04-17 20:43:08 +00004114 isSplatMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00004115 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00004116 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00004117 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00004118 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00004119}
Evan Cheng60f0b892006-04-20 08:58:49 +00004120
4121bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4122 MVT::ValueType EVT,
4123 SelectionDAG &DAG) const {
4124 unsigned NumElts = BVOps.size();
4125 // Only do shuffles on 128-bit vector types for now.
4126 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4127 if (NumElts == 2) return true;
4128 if (NumElts == 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00004129 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng60f0b892006-04-20 08:58:49 +00004130 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4131 }
4132 return false;
4133}