blob: 78600804f688f2cd7a0fd2f3577531b1d9b2d23b [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);
396 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
397}
398
399//===----------------------------------------------------------------------===//
400// C Calling Convention implementation
401//===----------------------------------------------------------------------===//
402
Evan Cheng24eb3f42006-04-27 05:35:28 +0000403/// AddLiveIn - This helper function adds the specified physical register to the
404/// MachineFunction as a live in value. It also creates a corresponding virtual
405/// register for it.
406static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
407 TargetRegisterClass *RC) {
408 assert(RC->contains(PReg) && "Not the correct regclass!");
409 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
410 MF.addLiveIn(PReg, VReg);
411 return VReg;
412}
413
Evan Cheng89001ad2006-04-27 08:31:10 +0000414/// HowToPassCCCArgument - Returns how an formal argument of the specified type
415/// should be passed. If it is through stack, returns the size of the stack
416/// frame; if it is through XMM register, returns the number of XMM registers
417/// are needed.
418static void
419HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
420 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000421 switch (ObjectVT) {
422 default: assert(0 && "Unhandled argument type!");
423 case MVT::i1:
424 case MVT::i8: ObjSize = 1; break;
425 case MVT::i16: ObjSize = 2; break;
426 case MVT::i32: ObjSize = 4; break;
427 case MVT::i64: ObjSize = 8; break;
428 case MVT::f32: ObjSize = 4; break;
429 case MVT::f64: ObjSize = 8; break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000430 case MVT::v16i8:
431 case MVT::v8i16:
432 case MVT::v4i32:
433 case MVT::v2i64:
434 case MVT::v4f32:
435 case MVT::v2f64:
436 if (NumXMMRegs < 3)
437 ObjXMMRegs = 1;
438 else
439 ObjSize = 16;
440 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000441 }
Evan Cheng48940d12006-04-27 01:32:22 +0000442}
443
Evan Cheng24eb3f42006-04-27 05:35:28 +0000444/// getFormalArgObjects - Returns itself if Op is a FORMAL_ARGUMENTS, otherwise
445/// returns the FORMAL_ARGUMENTS node(s) that made up parts of the node.
Evan Cheng48940d12006-04-27 01:32:22 +0000446static std::vector<SDOperand> getFormalArgObjects(SDOperand Op) {
447 unsigned Opc = Op.getOpcode();
448 std::vector<SDOperand> Objs;
449 if (Opc == ISD::TRUNCATE) {
450 Op = Op.getOperand(0);
451 assert(Op.getOpcode() == ISD::AssertSext ||
452 Op.getOpcode() == ISD::AssertZext);
453 Objs.push_back(Op.getOperand(0));
Evan Chengd43c5c62006-04-28 05:25:15 +0000454 } else if (Opc == ISD::FP_ROUND || Opc == ISD::VBIT_CONVERT) {
Evan Cheng48940d12006-04-27 01:32:22 +0000455 Objs.push_back(Op.getOperand(0));
456 } else if (Opc == ISD::BUILD_PAIR) {
457 Objs.push_back(Op.getOperand(0));
458 Objs.push_back(Op.getOperand(1));
459 } else {
460 Objs.push_back(Op);
461 }
462 return Objs;
463}
464
Evan Cheng8c6b2342006-05-17 19:07:40 +0000465void X86TargetLowering::PreprocessCCCArguments(std::vector<SDOperand> &Args,
Evan Cheng48940d12006-04-27 01:32:22 +0000466 Function &F, SelectionDAG &DAG) {
467 unsigned NumArgs = Args.size();
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000468 MachineFunction &MF = DAG.getMachineFunction();
469 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner76ac0682005-11-15 00:40:23 +0000470
Evan Cheng48940d12006-04-27 01:32:22 +0000471 // Add DAG nodes to load the arguments... On entry to a function on the X86,
472 // the stack frame looks like this:
473 //
474 // [ESP] -- return address
475 // [ESP + 4] -- first argument (leftmost lexically)
476 // [ESP + 8] -- second argument, if first argument is four bytes in size
477 // ...
478 //
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000479 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng89001ad2006-04-27 08:31:10 +0000480 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
481 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000482 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng48940d12006-04-27 01:32:22 +0000483 SDOperand Op = Args[i];
484 std::vector<SDOperand> Objs = getFormalArgObjects(Op);
485 for (std::vector<SDOperand>::iterator I = Objs.begin(), E = Objs.end();
486 I != E; ++I) {
487 SDOperand Obj = *I;
488 MVT::ValueType ObjectVT = Obj.getValueType();
489 unsigned ArgIncrement = 4;
Evan Cheng89001ad2006-04-27 08:31:10 +0000490 unsigned ObjSize = 0;
491 unsigned ObjXMMRegs = 0;
492 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
493 if (ObjSize >= 8)
494 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000495
Evan Cheng89001ad2006-04-27 08:31:10 +0000496 if (ObjXMMRegs) {
497 // Passed in a XMM register.
498 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
499 X86::VR128RegisterClass);
500 std::pair<FALocInfo, FALocInfo> Loc =
501 std::make_pair(FALocInfo(FALocInfo::LiveInRegLoc, Reg, ObjectVT),
502 FALocInfo());
503 FormalArgLocs.push_back(Loc);
504 NumXMMRegs += ObjXMMRegs;
505 } else {
506 // Create the frame index object for this incoming parameter...
507 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
508 std::pair<FALocInfo, FALocInfo> Loc =
509 std::make_pair(FALocInfo(FALocInfo::StackFrameLoc, FI), FALocInfo());
510 FormalArgLocs.push_back(Loc);
511 ArgOffset += ArgIncrement; // Move on to the next argument...
512 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000513 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000514 }
515
516 // If the function takes variable number of arguments, make a frame index for
517 // the start of the first vararg value... for expansion of llvm.va_start.
518 if (F.isVarArg())
519 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
520 ReturnAddrIndex = 0; // No return address slot generated yet.
521 BytesToPopOnReturn = 0; // Callee pops nothing.
522 BytesCallerReserves = ArgOffset;
523}
524
525void X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3d826992006-05-16 06:45:34 +0000526 unsigned NumArgs = Op.Val->getNumValues() - 1;
Chris Lattner76ac0682005-11-15 00:40:23 +0000527 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner76ac0682005-11-15 00:40:23 +0000528
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000529 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng89001ad2006-04-27 08:31:10 +0000530 std::pair<FALocInfo, FALocInfo> Loc = FormalArgLocs[i];
531 SDOperand ArgValue;
532 if (Loc.first.Kind == FALocInfo::StackFrameLoc) {
Chris Lattner3d826992006-05-16 06:45:34 +0000533 // Create the SelectionDAG nodes corresponding to a load from this
534 // parameter.
Evan Cheng89001ad2006-04-27 08:31:10 +0000535 unsigned FI = FormalArgLocs[i].first.Loc;
536 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
537 ArgValue = DAG.getLoad(Op.Val->getValueType(i),
538 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
539 } else {
540 // Must be a CopyFromReg
541 ArgValue= DAG.getCopyFromReg(DAG.getEntryNode(), Loc.first.Loc,
542 Loc.first.Typ);
543 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000544 FormalArgs.push_back(ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000545 }
Chris Lattner7b8b8bb2006-05-16 17:08:35 +0000546 // Provide a chain. Note that this isn't the right one, but it works as well
547 // as before.
548 FormalArgs.push_back(DAG.getEntryNode());
Chris Lattner76ac0682005-11-15 00:40:23 +0000549}
550
551std::pair<SDOperand, SDOperand>
552X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
553 bool isVarArg, bool isTailCall,
554 SDOperand Callee, ArgListTy &Args,
555 SelectionDAG &DAG) {
556 // Count how many bytes are to be pushed on the stack.
557 unsigned NumBytes = 0;
558
Evan Cheng88decde2006-04-28 21:29:37 +0000559 // Keep track of the number of XMM regs passed so far.
560 unsigned NumXMMRegs = 0;
561 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
562
563 std::vector<SDOperand> RegValuesToPass;
Chris Lattner76ac0682005-11-15 00:40:23 +0000564 if (Args.empty()) {
565 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000566 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000567 } else {
568 for (unsigned i = 0, e = Args.size(); i != e; ++i)
569 switch (getValueType(Args[i].second)) {
570 default: assert(0 && "Unknown value type!");
571 case MVT::i1:
572 case MVT::i8:
573 case MVT::i16:
574 case MVT::i32:
575 case MVT::f32:
576 NumBytes += 4;
577 break;
578 case MVT::i64:
579 case MVT::f64:
580 NumBytes += 8;
581 break;
Evan Cheng88decde2006-04-28 21:29:37 +0000582 case MVT::Vector:
583 if (NumXMMRegs < 3)
584 ++NumXMMRegs;
585 else
586 NumBytes += 16;
587 break;
Chris Lattner76ac0682005-11-15 00:40:23 +0000588 }
589
Chris Lattner62c34842006-02-13 09:00:43 +0000590 Chain = DAG.getCALLSEQ_START(Chain,
591 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000592
593 // Arguments go on the stack in reverse order, as specified by the ABI.
594 unsigned ArgOffset = 0;
Evan Cheng88decde2006-04-28 21:29:37 +0000595 NumXMMRegs = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000596 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000597 std::vector<SDOperand> Stores;
Chris Lattner76ac0682005-11-15 00:40:23 +0000598 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000599 switch (getValueType(Args[i].second)) {
600 default: assert(0 && "Unexpected ValueType for argument!");
601 case MVT::i1:
602 case MVT::i8:
603 case MVT::i16:
604 // Promote the integer to 32 bits. If the input type is signed use a
605 // sign extend, otherwise use a zero extend.
606 if (Args[i].second->isSigned())
607 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
608 else
609 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
610
611 // FALL THROUGH
612 case MVT::i32:
Evan Cheng88decde2006-04-28 21:29:37 +0000613 case MVT::f32: {
614 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
615 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner76ac0682005-11-15 00:40:23 +0000616 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
617 Args[i].first, PtrOff,
618 DAG.getSrcValue(NULL)));
619 ArgOffset += 4;
620 break;
Evan Cheng88decde2006-04-28 21:29:37 +0000621 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000622 case MVT::i64:
Evan Cheng88decde2006-04-28 21:29:37 +0000623 case MVT::f64: {
624 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
625 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner76ac0682005-11-15 00:40:23 +0000626 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
627 Args[i].first, PtrOff,
628 DAG.getSrcValue(NULL)));
629 ArgOffset += 8;
630 break;
631 }
Evan Cheng88decde2006-04-28 21:29:37 +0000632 case MVT::Vector:
633 if (NumXMMRegs < 3) {
634 RegValuesToPass.push_back(Args[i].first);
635 NumXMMRegs++;
636 } else {
637 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
638 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
639 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
640 Args[i].first, PtrOff,
641 DAG.getSrcValue(NULL)));
642 ArgOffset += 16;
643 }
644 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000645 }
Evan Cheng88decde2006-04-28 21:29:37 +0000646 if (!Stores.empty())
Chris Lattner76ac0682005-11-15 00:40:23 +0000647 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
648 }
649
650 std::vector<MVT::ValueType> RetVals;
651 MVT::ValueType RetTyVT = getValueType(RetTy);
652 RetVals.push_back(MVT::Other);
653
654 // The result values produced have to be legal. Promote the result.
655 switch (RetTyVT) {
656 case MVT::isVoid: break;
657 default:
658 RetVals.push_back(RetTyVT);
659 break;
660 case MVT::i1:
661 case MVT::i8:
662 case MVT::i16:
663 RetVals.push_back(MVT::i32);
664 break;
665 case MVT::f32:
666 if (X86ScalarSSE)
667 RetVals.push_back(MVT::f32);
668 else
669 RetVals.push_back(MVT::f64);
670 break;
671 case MVT::i64:
672 RetVals.push_back(MVT::i32);
673 RetVals.push_back(MVT::i32);
674 break;
675 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000676
Evan Cheng88decde2006-04-28 21:29:37 +0000677 // Build a sequence of copy-to-reg nodes chained together with token chain
678 // and flag operands which copy the outgoing args into registers.
679 SDOperand InFlag;
680 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
681 unsigned CCReg = XMMArgRegs[i];
682 SDOperand RegToPass = RegValuesToPass[i];
683 assert(RegToPass.getValueType() == MVT::Vector);
Chris Lattner3d826992006-05-16 06:45:34 +0000684 unsigned NumElems =
685 cast<ConstantSDNode>(*(RegToPass.Val->op_end()-2))->getValue();
Evan Cheng88decde2006-04-28 21:29:37 +0000686 MVT::ValueType EVT = cast<VTSDNode>(*(RegToPass.Val->op_end()-1))->getVT();
687 MVT::ValueType PVT = getVectorType(EVT, NumElems);
688 SDOperand CCRegNode = DAG.getRegister(CCReg, PVT);
689 RegToPass = DAG.getNode(ISD::VBIT_CONVERT, PVT, RegToPass);
690 Chain = DAG.getCopyToReg(Chain, CCRegNode, RegToPass, InFlag);
691 InFlag = Chain.getValue(1);
692 }
693
Nate Begeman7e5496d2006-02-17 00:03:04 +0000694 std::vector<MVT::ValueType> NodeTys;
695 NodeTys.push_back(MVT::Other); // Returns a chain
696 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
697 std::vector<SDOperand> Ops;
698 Ops.push_back(Chain);
699 Ops.push_back(Callee);
Evan Cheng88decde2006-04-28 21:29:37 +0000700 if (InFlag.Val)
701 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000702
Nate Begeman7e5496d2006-02-17 00:03:04 +0000703 // FIXME: Do not generate X86ISD::TAILCALL for now.
704 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
Evan Cheng88decde2006-04-28 21:29:37 +0000705 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000706
Nate Begeman7e5496d2006-02-17 00:03:04 +0000707 NodeTys.clear();
708 NodeTys.push_back(MVT::Other); // Returns a chain
709 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
710 Ops.clear();
711 Ops.push_back(Chain);
712 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
713 Ops.push_back(DAG.getConstant(0, getPointerTy()));
714 Ops.push_back(InFlag);
715 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
716 InFlag = Chain.getValue(1);
717
718 SDOperand RetVal;
719 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000720 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000721 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000722 case MVT::i1:
723 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000724 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
725 Chain = RetVal.getValue(1);
726 if (RetTyVT == MVT::i1)
727 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
728 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000729 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000730 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
731 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000732 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000733 case MVT::i32:
734 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
735 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000736 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000737 case MVT::i64: {
738 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
739 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
740 Lo.getValue(2));
741 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
742 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000743 break;
744 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000745 case MVT::f32:
746 case MVT::f64: {
747 std::vector<MVT::ValueType> Tys;
748 Tys.push_back(MVT::f64);
749 Tys.push_back(MVT::Other);
750 Tys.push_back(MVT::Flag);
751 std::vector<SDOperand> Ops;
752 Ops.push_back(Chain);
753 Ops.push_back(InFlag);
754 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
755 Chain = RetVal.getValue(1);
756 InFlag = RetVal.getValue(2);
757 if (X86ScalarSSE) {
758 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
759 // shouldn't be necessary except that RFP cannot be live across
760 // multiple blocks. When stackifier is fixed, they can be uncoupled.
761 MachineFunction &MF = DAG.getMachineFunction();
762 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
763 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
764 Tys.clear();
765 Tys.push_back(MVT::Other);
766 Ops.clear();
767 Ops.push_back(Chain);
768 Ops.push_back(RetVal);
769 Ops.push_back(StackSlot);
770 Ops.push_back(DAG.getValueType(RetTyVT));
771 Ops.push_back(InFlag);
772 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
773 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
774 DAG.getSrcValue(NULL));
775 Chain = RetVal.getValue(1);
776 }
Evan Cheng45e190982006-01-05 00:27:02 +0000777
Nate Begeman7e5496d2006-02-17 00:03:04 +0000778 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
779 // FIXME: we would really like to remember that this FP_ROUND
780 // operation is okay to eliminate if we allow excess FP precision.
781 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
782 break;
783 }
Evan Cheng88decde2006-04-28 21:29:37 +0000784 case MVT::Vector: {
785 const PackedType *PTy = cast<PackedType>(RetTy);
786 MVT::ValueType EVT;
787 MVT::ValueType LVT;
788 unsigned NumRegs = getPackedTypeBreakdown(PTy, EVT, LVT);
789 assert(NumRegs == 1 && "Unsupported type!");
790 RetVal = DAG.getCopyFromReg(Chain, X86::XMM0, EVT, InFlag);
791 Chain = RetVal.getValue(1);
792 break;
793 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000794 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000795 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000796
797 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000798}
799
Chris Lattner76ac0682005-11-15 00:40:23 +0000800//===----------------------------------------------------------------------===//
801// Fast Calling Convention implementation
802//===----------------------------------------------------------------------===//
803//
804// The X86 'fast' calling convention passes up to two integer arguments in
805// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
806// and requires that the callee pop its arguments off the stack (allowing proper
807// tail calls), and has the same return value conventions as C calling convs.
808//
809// This calling convention always arranges for the callee pop value to be 8n+4
810// bytes, which is needed for tail recursion elimination and stack alignment
811// reasons.
812//
813// Note that this can be enhanced in the future to pass fp vals in registers
814// (when we have a global fp allocator) and do other tricks.
815//
816
Chris Lattner388fc4d2006-03-17 17:27:47 +0000817// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
818// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
819// EDX". Anything more is illegal.
820//
821// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000822// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000823// a physreg with a virtreg, this increases the size of the physreg's live
824// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000825// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000826// allocator to wedge itself.
827//
828// This code triggers this problem more often if we pass args in registers,
829// so disable it until this is fixed.
830//
831// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
832// about code being dead.
833//
834static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000835
Chris Lattner76ac0682005-11-15 00:40:23 +0000836
Evan Cheng89001ad2006-04-27 08:31:10 +0000837/// HowToPassFastCCArgument - Returns how an formal argument of the specified
838/// type should be passed. If it is through stack, returns the size of the stack
839/// frame; if it is through integer or XMM register, returns the number of
840/// integer or XMM registers are needed.
Evan Cheng48940d12006-04-27 01:32:22 +0000841static void
Evan Cheng89001ad2006-04-27 08:31:10 +0000842HowToPassFastCCArgument(MVT::ValueType ObjectVT,
843 unsigned NumIntRegs, unsigned NumXMMRegs,
844 unsigned &ObjSize, unsigned &ObjIntRegs,
845 unsigned &ObjXMMRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000846 ObjSize = 0;
847 NumIntRegs = 0;
848
849 switch (ObjectVT) {
850 default: assert(0 && "Unhandled argument type!");
851 case MVT::i1:
852 case MVT::i8:
853 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000854 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000855 else
856 ObjSize = 1;
857 break;
858 case MVT::i16:
859 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000860 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000861 else
862 ObjSize = 2;
863 break;
864 case MVT::i32:
865 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng24eb3f42006-04-27 05:35:28 +0000866 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000867 else
868 ObjSize = 4;
869 break;
870 case MVT::i64:
871 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000872 ObjIntRegs = 2;
Evan Cheng48940d12006-04-27 01:32:22 +0000873 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000874 ObjIntRegs = 1;
Evan Cheng48940d12006-04-27 01:32:22 +0000875 ObjSize = 4;
876 } else
877 ObjSize = 8;
878 case MVT::f32:
879 ObjSize = 4;
880 break;
881 case MVT::f64:
882 ObjSize = 8;
883 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000884 case MVT::v16i8:
885 case MVT::v8i16:
886 case MVT::v4i32:
887 case MVT::v2i64:
888 case MVT::v4f32:
889 case MVT::v2f64:
890 if (NumXMMRegs < 3)
891 ObjXMMRegs = 1;
892 else
893 ObjSize = 16;
894 break;
Evan Cheng48940d12006-04-27 01:32:22 +0000895 }
896}
897
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000898void
Evan Cheng8c6b2342006-05-17 19:07:40 +0000899X86TargetLowering::PreprocessFastCCArguments(std::vector<SDOperand> &Args,
Evan Cheng48940d12006-04-27 01:32:22 +0000900 Function &F, SelectionDAG &DAG) {
901 unsigned NumArgs = Args.size();
Chris Lattner76ac0682005-11-15 00:40:23 +0000902 MachineFunction &MF = DAG.getMachineFunction();
903 MachineFrameInfo *MFI = MF.getFrameInfo();
904
Evan Cheng48940d12006-04-27 01:32:22 +0000905 // Add DAG nodes to load the arguments... On entry to a function the stack
906 // frame looks like this:
907 //
908 // [ESP] -- return address
909 // [ESP + 4] -- first nonreg argument (leftmost lexically)
910 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
911 // ...
Chris Lattner76ac0682005-11-15 00:40:23 +0000912 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
913
914 // Keep track of the number of integer regs passed so far. This can be either
915 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
916 // used).
917 unsigned NumIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000918 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
919 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
Chris Lattner43798852006-03-17 05:10:20 +0000920
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000921 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng48940d12006-04-27 01:32:22 +0000922 SDOperand Op = Args[i];
923 std::vector<SDOperand> Objs = getFormalArgObjects(Op);
924 for (std::vector<SDOperand>::iterator I = Objs.begin(), E = Objs.end();
925 I != E; ++I) {
926 SDOperand Obj = *I;
927 MVT::ValueType ObjectVT = Obj.getValueType();
928 unsigned ArgIncrement = 4;
929 unsigned ObjSize = 0;
Evan Cheng24eb3f42006-04-27 05:35:28 +0000930 unsigned ObjIntRegs = 0;
Evan Cheng89001ad2006-04-27 08:31:10 +0000931 unsigned ObjXMMRegs = 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000932
Evan Cheng89001ad2006-04-27 08:31:10 +0000933 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
934 ObjSize, ObjIntRegs, ObjXMMRegs);
935 if (ObjSize >= 8)
936 ArgIncrement = ObjSize;
Evan Cheng48940d12006-04-27 01:32:22 +0000937
938 unsigned Reg;
939 std::pair<FALocInfo,FALocInfo> Loc = std::make_pair(FALocInfo(),
940 FALocInfo());
Evan Cheng24eb3f42006-04-27 05:35:28 +0000941 if (ObjIntRegs) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000942 switch (ObjectVT) {
943 default: assert(0 && "Unhandled argument type!");
944 case MVT::i1:
945 case MVT::i8:
946 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
Evan Cheng9fee4422006-05-16 07:21:53 +0000947 X86::GR8RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000948 Loc.first.Kind = FALocInfo::LiveInRegLoc;
949 Loc.first.Loc = Reg;
950 Loc.first.Typ = MVT::i8;
951 break;
952 case MVT::i16:
953 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
Evan Cheng9fee4422006-05-16 07:21:53 +0000954 X86::GR16RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000955 Loc.first.Kind = FALocInfo::LiveInRegLoc;
956 Loc.first.Loc = Reg;
957 Loc.first.Typ = MVT::i16;
958 break;
959 case MVT::i32:
960 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Evan Cheng9fee4422006-05-16 07:21:53 +0000961 X86::GR32RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000962 Loc.first.Kind = FALocInfo::LiveInRegLoc;
963 Loc.first.Loc = Reg;
964 Loc.first.Typ = MVT::i32;
965 break;
966 case MVT::i64:
967 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Evan Cheng9fee4422006-05-16 07:21:53 +0000968 X86::GR32RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000969 Loc.first.Kind = FALocInfo::LiveInRegLoc;
970 Loc.first.Loc = Reg;
971 Loc.first.Typ = MVT::i32;
972 if (ObjIntRegs == 2) {
Evan Cheng9fee4422006-05-16 07:21:53 +0000973 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000974 Loc.second.Kind = FALocInfo::LiveInRegLoc;
975 Loc.second.Loc = Reg;
976 Loc.second.Typ = MVT::i32;
977 }
978 break;
Evan Cheng89001ad2006-04-27 08:31:10 +0000979 case MVT::v16i8:
980 case MVT::v8i16:
981 case MVT::v4i32:
982 case MVT::v2i64:
983 case MVT::v4f32:
984 case MVT::v2f64:
985 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
986 Loc.first.Kind = FALocInfo::LiveInRegLoc;
987 Loc.first.Loc = Reg;
988 Loc.first.Typ = ObjectVT;
989 break;
Evan Cheng24eb3f42006-04-27 05:35:28 +0000990 }
Evan Chenga0374e12006-04-27 05:44:50 +0000991 NumIntRegs += ObjIntRegs;
Evan Cheng89001ad2006-04-27 08:31:10 +0000992 NumXMMRegs += ObjXMMRegs;
Evan Cheng48940d12006-04-27 01:32:22 +0000993 }
994 if (ObjSize) {
995 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000996 if (ObjectVT == MVT::i64 && ObjIntRegs) {
Evan Cheng48940d12006-04-27 01:32:22 +0000997 Loc.second.Kind = FALocInfo::StackFrameLoc;
998 Loc.second.Loc = FI;
999 } else {
1000 Loc.first.Kind = FALocInfo::StackFrameLoc;
1001 Loc.first.Loc = FI;
1002 }
1003 ArgOffset += ArgIncrement; // Move on to the next argument.
Chris Lattner76ac0682005-11-15 00:40:23 +00001004 }
1005
Evan Cheng48940d12006-04-27 01:32:22 +00001006 FormalArgLocs.push_back(Loc);
Chris Lattner76ac0682005-11-15 00:40:23 +00001007 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001008 }
1009
1010 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1011 // arguments and the arguments after the retaddr has been pushed are aligned.
1012 if ((ArgOffset & 7) == 0)
1013 ArgOffset += 4;
1014
1015 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1016 ReturnAddrIndex = 0; // No return address slot generated yet.
1017 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1018 BytesCallerReserves = 0;
1019
1020 // Finally, inform the code generator which regs we return values in.
1021 switch (getValueType(F.getReturnType())) {
1022 default: assert(0 && "Unknown type!");
1023 case MVT::isVoid: break;
1024 case MVT::i1:
1025 case MVT::i8:
1026 case MVT::i16:
1027 case MVT::i32:
1028 MF.addLiveOut(X86::EAX);
1029 break;
1030 case MVT::i64:
1031 MF.addLiveOut(X86::EAX);
1032 MF.addLiveOut(X86::EDX);
1033 break;
1034 case MVT::f32:
1035 case MVT::f64:
1036 MF.addLiveOut(X86::ST0);
1037 break;
Evan Cheng88decde2006-04-28 21:29:37 +00001038 case MVT::Vector: {
1039 const PackedType *PTy = cast<PackedType>(F.getReturnType());
1040 MVT::ValueType EVT;
1041 MVT::ValueType LVT;
1042 unsigned NumRegs = getPackedTypeBreakdown(PTy, EVT, LVT);
1043 assert(NumRegs == 1 && "Unsupported type!");
1044 MF.addLiveOut(X86::XMM0);
1045 break;
1046 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001047 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001048}
Evan Cheng88decde2006-04-28 21:29:37 +00001049
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001050void
1051X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3d826992006-05-16 06:45:34 +00001052 unsigned NumArgs = Op.Val->getNumValues()-1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001053 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001054
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001055 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng48940d12006-04-27 01:32:22 +00001056 MVT::ValueType VT = Op.Val->getValueType(i);
1057 std::pair<FALocInfo, FALocInfo> Loc = FormalArgLocs[i];
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001058 SDOperand ArgValue;
Evan Cheng48940d12006-04-27 01:32:22 +00001059 if (Loc.first.Kind == FALocInfo::StackFrameLoc) {
Chris Lattner3d826992006-05-16 06:45:34 +00001060 // Create the SelectionDAG nodes corresponding to a load from this
1061 // parameter.
Evan Cheng48940d12006-04-27 01:32:22 +00001062 SDOperand FIN = DAG.getFrameIndex(Loc.first.Loc, MVT::i32);
Chris Lattner3d826992006-05-16 06:45:34 +00001063 ArgValue = DAG.getLoad(Op.Val->getValueType(i), DAG.getEntryNode(), FIN,
Evan Cheng48940d12006-04-27 01:32:22 +00001064 DAG.getSrcValue(NULL));
1065 } else {
1066 // Must be a CopyFromReg
Evan Cheng89001ad2006-04-27 08:31:10 +00001067 ArgValue= DAG.getCopyFromReg(DAG.getEntryNode(), Loc.first.Loc,
1068 Loc.first.Typ);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001069 }
1070
Evan Cheng48940d12006-04-27 01:32:22 +00001071 if (Loc.second.Kind != FALocInfo::None) {
1072 SDOperand ArgValue2;
1073 if (Loc.second.Kind == FALocInfo::StackFrameLoc) {
Chris Lattner3d826992006-05-16 06:45:34 +00001074 // Create the SelectionDAG nodes corresponding to a load from this
1075 // parameter.
Evan Cheng48940d12006-04-27 01:32:22 +00001076 SDOperand FIN = DAG.getFrameIndex(Loc.second.Loc, MVT::i32);
Chris Lattner3d826992006-05-16 06:45:34 +00001077 ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), DAG.getEntryNode(),
1078 FIN, DAG.getSrcValue(NULL));
Evan Cheng48940d12006-04-27 01:32:22 +00001079 } else {
1080 // Must be a CopyFromReg
Evan Cheng89001ad2006-04-27 08:31:10 +00001081 ArgValue2 = DAG.getCopyFromReg(DAG.getEntryNode(),
Evan Cheng48940d12006-04-27 01:32:22 +00001082 Loc.second.Loc, Loc.second.Typ);
1083 }
1084 ArgValue = DAG.getNode(ISD::BUILD_PAIR, VT, ArgValue, ArgValue2);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001085 }
1086 FormalArgs.push_back(ArgValue);
1087 }
Chris Lattner7b8b8bb2006-05-16 17:08:35 +00001088
1089 // Provide a chain. Note that this isn't the right one, but it works as well
1090 // as before.
1091 FormalArgs.push_back(DAG.getEntryNode());
Chris Lattner76ac0682005-11-15 00:40:23 +00001092}
1093
1094std::pair<SDOperand, SDOperand>
1095X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
1096 bool isTailCall, SDOperand Callee,
1097 ArgListTy &Args, SelectionDAG &DAG) {
1098 // Count how many bytes are to be pushed on the stack.
1099 unsigned NumBytes = 0;
1100
1101 // Keep track of the number of integer regs passed so far. This can be either
1102 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1103 // used).
1104 unsigned NumIntRegs = 0;
1105
1106 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1107 switch (getValueType(Args[i].second)) {
1108 default: assert(0 && "Unknown value type!");
1109 case MVT::i1:
1110 case MVT::i8:
1111 case MVT::i16:
1112 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001113 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001114 ++NumIntRegs;
1115 break;
1116 }
1117 // fall through
1118 case MVT::f32:
1119 NumBytes += 4;
1120 break;
1121 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001122 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
1123 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +00001124 break;
Chris Lattner43798852006-03-17 05:10:20 +00001125 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
1126 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +00001127 NumBytes += 4;
1128 break;
1129 }
1130
1131 // fall through
1132 case MVT::f64:
1133 NumBytes += 8;
1134 break;
1135 }
1136
1137 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1138 // arguments and the arguments after the retaddr has been pushed are aligned.
1139 if ((NumBytes & 7) == 0)
1140 NumBytes += 4;
1141
Chris Lattner62c34842006-02-13 09:00:43 +00001142 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +00001143
1144 // Arguments go on the stack in reverse order, as specified by the ABI.
1145 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +00001146 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +00001147 NumIntRegs = 0;
1148 std::vector<SDOperand> Stores;
1149 std::vector<SDOperand> RegValuesToPass;
1150 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1151 switch (getValueType(Args[i].second)) {
1152 default: assert(0 && "Unexpected ValueType for argument!");
1153 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +00001154 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
1155 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +00001156 case MVT::i8:
1157 case MVT::i16:
1158 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001159 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001160 RegValuesToPass.push_back(Args[i].first);
1161 ++NumIntRegs;
1162 break;
1163 }
1164 // Fall through
1165 case MVT::f32: {
1166 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1167 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1168 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1169 Args[i].first, PtrOff,
1170 DAG.getSrcValue(NULL)));
1171 ArgOffset += 4;
1172 break;
1173 }
1174 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001175 // Can pass (at least) part of it in regs?
1176 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001177 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1178 Args[i].first, DAG.getConstant(1, MVT::i32));
1179 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1180 Args[i].first, DAG.getConstant(0, MVT::i32));
1181 RegValuesToPass.push_back(Lo);
1182 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +00001183
1184 // Pass both parts in regs?
1185 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001186 RegValuesToPass.push_back(Hi);
1187 ++NumIntRegs;
1188 } else {
1189 // Pass the high part in memory.
1190 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1191 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1192 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1193 Hi, PtrOff, DAG.getSrcValue(NULL)));
1194 ArgOffset += 4;
1195 }
1196 break;
1197 }
1198 // Fall through
1199 case MVT::f64:
1200 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1201 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1202 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1203 Args[i].first, PtrOff,
1204 DAG.getSrcValue(NULL)));
1205 ArgOffset += 8;
1206 break;
1207 }
1208 }
1209 if (!Stores.empty())
1210 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
1211
1212 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1213 // arguments and the arguments after the retaddr has been pushed are aligned.
1214 if ((ArgOffset & 7) == 0)
1215 ArgOffset += 4;
1216
1217 std::vector<MVT::ValueType> RetVals;
1218 MVT::ValueType RetTyVT = getValueType(RetTy);
1219
1220 RetVals.push_back(MVT::Other);
1221
1222 // The result values produced have to be legal. Promote the result.
1223 switch (RetTyVT) {
1224 case MVT::isVoid: break;
1225 default:
1226 RetVals.push_back(RetTyVT);
1227 break;
1228 case MVT::i1:
1229 case MVT::i8:
1230 case MVT::i16:
1231 RetVals.push_back(MVT::i32);
1232 break;
1233 case MVT::f32:
1234 if (X86ScalarSSE)
1235 RetVals.push_back(MVT::f32);
1236 else
1237 RetVals.push_back(MVT::f64);
1238 break;
1239 case MVT::i64:
1240 RetVals.push_back(MVT::i32);
1241 RetVals.push_back(MVT::i32);
1242 break;
1243 }
1244
Nate Begeman7e5496d2006-02-17 00:03:04 +00001245 // Build a sequence of copy-to-reg nodes chained together with token chain
1246 // and flag operands which copy the outgoing args into registers.
1247 SDOperand InFlag;
1248 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1249 unsigned CCReg;
1250 SDOperand RegToPass = RegValuesToPass[i];
1251 switch (RegToPass.getValueType()) {
1252 default: assert(0 && "Bad thing to pass in regs");
1253 case MVT::i8:
1254 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001255 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001256 case MVT::i16:
1257 CCReg = (i == 0) ? X86::AX : X86::DX;
1258 break;
1259 case MVT::i32:
1260 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1261 break;
1262 }
1263
1264 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1265 InFlag = Chain.getValue(1);
1266 }
1267
1268 std::vector<MVT::ValueType> NodeTys;
1269 NodeTys.push_back(MVT::Other); // Returns a chain
1270 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1271 std::vector<SDOperand> Ops;
1272 Ops.push_back(Chain);
1273 Ops.push_back(Callee);
1274 if (InFlag.Val)
1275 Ops.push_back(InFlag);
1276
1277 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001278 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1279 NodeTys, Ops);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001280 InFlag = Chain.getValue(1);
1281
1282 NodeTys.clear();
1283 NodeTys.push_back(MVT::Other); // Returns a chain
1284 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1285 Ops.clear();
1286 Ops.push_back(Chain);
1287 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1288 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1289 Ops.push_back(InFlag);
1290 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1291 InFlag = Chain.getValue(1);
1292
1293 SDOperand RetVal;
1294 if (RetTyVT != MVT::isVoid) {
1295 switch (RetTyVT) {
1296 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001297 case MVT::i1:
1298 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001299 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1300 Chain = RetVal.getValue(1);
1301 if (RetTyVT == MVT::i1)
1302 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1303 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001304 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001305 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1306 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001307 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001308 case MVT::i32:
1309 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1310 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001311 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001312 case MVT::i64: {
1313 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1314 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1315 Lo.getValue(2));
1316 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1317 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001318 break;
1319 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001320 case MVT::f32:
1321 case MVT::f64: {
1322 std::vector<MVT::ValueType> Tys;
1323 Tys.push_back(MVT::f64);
1324 Tys.push_back(MVT::Other);
1325 Tys.push_back(MVT::Flag);
1326 std::vector<SDOperand> Ops;
1327 Ops.push_back(Chain);
1328 Ops.push_back(InFlag);
1329 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1330 Chain = RetVal.getValue(1);
1331 InFlag = RetVal.getValue(2);
1332 if (X86ScalarSSE) {
1333 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1334 // shouldn't be necessary except that RFP cannot be live across
1335 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1336 MachineFunction &MF = DAG.getMachineFunction();
1337 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1338 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1339 Tys.clear();
1340 Tys.push_back(MVT::Other);
1341 Ops.clear();
1342 Ops.push_back(Chain);
1343 Ops.push_back(RetVal);
1344 Ops.push_back(StackSlot);
1345 Ops.push_back(DAG.getValueType(RetTyVT));
1346 Ops.push_back(InFlag);
1347 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1348 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1349 DAG.getSrcValue(NULL));
1350 Chain = RetVal.getValue(1);
1351 }
Evan Cheng172fce72006-01-06 00:43:03 +00001352
Nate Begeman7e5496d2006-02-17 00:03:04 +00001353 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1354 // FIXME: we would really like to remember that this FP_ROUND
1355 // operation is okay to eliminate if we allow excess FP precision.
1356 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1357 break;
1358 }
1359 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001360 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001361
1362 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001363}
1364
1365SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1366 if (ReturnAddrIndex == 0) {
1367 // Set up a frame object for the return address.
1368 MachineFunction &MF = DAG.getMachineFunction();
1369 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1370 }
1371
1372 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1373}
1374
1375
1376
1377std::pair<SDOperand, SDOperand> X86TargetLowering::
1378LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1379 SelectionDAG &DAG) {
1380 SDOperand Result;
1381 if (Depth) // Depths > 0 not supported yet!
1382 Result = DAG.getConstant(0, getPointerTy());
1383 else {
1384 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1385 if (!isFrameAddress)
1386 // Just load the return address
1387 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1388 DAG.getSrcValue(NULL));
1389 else
1390 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1391 DAG.getConstant(4, MVT::i32));
1392 }
1393 return std::make_pair(Result, Chain);
1394}
1395
Evan Cheng339edad2006-01-11 00:33:36 +00001396/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1397/// which corresponds to the condition code.
1398static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1399 switch (X86CC) {
1400 default: assert(0 && "Unknown X86 conditional code!");
1401 case X86ISD::COND_A: return X86::JA;
1402 case X86ISD::COND_AE: return X86::JAE;
1403 case X86ISD::COND_B: return X86::JB;
1404 case X86ISD::COND_BE: return X86::JBE;
1405 case X86ISD::COND_E: return X86::JE;
1406 case X86ISD::COND_G: return X86::JG;
1407 case X86ISD::COND_GE: return X86::JGE;
1408 case X86ISD::COND_L: return X86::JL;
1409 case X86ISD::COND_LE: return X86::JLE;
1410 case X86ISD::COND_NE: return X86::JNE;
1411 case X86ISD::COND_NO: return X86::JNO;
1412 case X86ISD::COND_NP: return X86::JNP;
1413 case X86ISD::COND_NS: return X86::JNS;
1414 case X86ISD::COND_O: return X86::JO;
1415 case X86ISD::COND_P: return X86::JP;
1416 case X86ISD::COND_S: return X86::JS;
1417 }
1418}
Chris Lattner76ac0682005-11-15 00:40:23 +00001419
Evan Cheng45df7f82006-01-30 23:41:35 +00001420/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1421/// specific condition code. It returns a false if it cannot do a direct
1422/// translation. X86CC is the translated CondCode. Flip is set to true if the
1423/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001424static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1425 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001426 Flip = false;
1427 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001428 if (!isFP) {
1429 switch (SetCCOpcode) {
1430 default: break;
1431 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1432 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1433 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1434 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1435 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1436 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1437 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1438 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1439 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1440 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1441 }
1442 } else {
1443 // On a floating point condition, the flags are set as follows:
1444 // ZF PF CF op
1445 // 0 | 0 | 0 | X > Y
1446 // 0 | 0 | 1 | X < Y
1447 // 1 | 0 | 0 | X == Y
1448 // 1 | 1 | 1 | unordered
1449 switch (SetCCOpcode) {
1450 default: break;
1451 case ISD::SETUEQ:
1452 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001453 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001454 case ISD::SETOGT:
1455 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001456 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001457 case ISD::SETOGE:
1458 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001459 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001460 case ISD::SETULT:
1461 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001462 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001463 case ISD::SETULE:
1464 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1465 case ISD::SETONE:
1466 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1467 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1468 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1469 }
1470 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001471
1472 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001473}
1474
Evan Cheng78038292006-04-05 23:38:46 +00001475static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1476 bool &Flip) {
1477 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1478}
1479
Evan Cheng339edad2006-01-11 00:33:36 +00001480/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1481/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001482/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001483static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001484 switch (X86CC) {
1485 default:
1486 return false;
1487 case X86ISD::COND_B:
1488 case X86ISD::COND_BE:
1489 case X86ISD::COND_E:
1490 case X86ISD::COND_P:
1491 case X86ISD::COND_A:
1492 case X86ISD::COND_AE:
1493 case X86ISD::COND_NE:
1494 case X86ISD::COND_NP:
1495 return true;
1496 }
1497}
1498
Evan Cheng339edad2006-01-11 00:33:36 +00001499MachineBasicBlock *
1500X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1501 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001502 switch (MI->getOpcode()) {
1503 default: assert(false && "Unexpected instr type to insert");
1504 case X86::CMOV_FR32:
Evan Cheng617a6a82006-04-10 07:23:14 +00001505 case X86::CMOV_FR64:
1506 case X86::CMOV_V4F32:
1507 case X86::CMOV_V2F64:
1508 case X86::CMOV_V2I64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001509 // To "insert" a SELECT_CC instruction, we actually have to insert the
1510 // diamond control-flow pattern. The incoming instruction knows the
1511 // destination vreg to set, the condition code register to branch on, the
1512 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001513 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1514 ilist<MachineBasicBlock>::iterator It = BB;
1515 ++It;
1516
1517 // thisMBB:
1518 // ...
1519 // TrueVal = ...
1520 // cmpTY ccX, r1, r2
1521 // bCC copy1MBB
1522 // fallthrough --> copy0MBB
1523 MachineBasicBlock *thisMBB = BB;
1524 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1525 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1526 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1527 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1528 MachineFunction *F = BB->getParent();
1529 F->getBasicBlockList().insert(It, copy0MBB);
1530 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001531 // Update machine-CFG edges by first adding all successors of the current
1532 // block to the new block which will contain the Phi node for the select.
1533 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1534 e = BB->succ_end(); i != e; ++i)
1535 sinkMBB->addSuccessor(*i);
1536 // Next, remove all successors of the current block, and add the true
1537 // and fallthrough blocks as its successors.
1538 while(!BB->succ_empty())
1539 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001540 BB->addSuccessor(copy0MBB);
1541 BB->addSuccessor(sinkMBB);
1542
1543 // copy0MBB:
1544 // %FalseValue = ...
1545 // # fallthrough to sinkMBB
1546 BB = copy0MBB;
1547
1548 // Update machine-CFG edges
1549 BB->addSuccessor(sinkMBB);
1550
1551 // sinkMBB:
1552 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1553 // ...
1554 BB = sinkMBB;
1555 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1556 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1557 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001558
Evan Cheng911c68d2006-01-16 21:21:29 +00001559 delete MI; // The pseudo instruction is gone now.
1560 return BB;
1561 }
Evan Cheng339edad2006-01-11 00:33:36 +00001562
Evan Cheng911c68d2006-01-16 21:21:29 +00001563 case X86::FP_TO_INT16_IN_MEM:
1564 case X86::FP_TO_INT32_IN_MEM:
1565 case X86::FP_TO_INT64_IN_MEM: {
1566 // Change the floating point control register to use "round towards zero"
1567 // mode when truncating to an integer value.
1568 MachineFunction *F = BB->getParent();
1569 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1570 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1571
1572 // Load the old value of the high byte of the control word...
1573 unsigned OldCW =
Evan Cheng9fee4422006-05-16 07:21:53 +00001574 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng911c68d2006-01-16 21:21:29 +00001575 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1576
1577 // Set the high part to be round to zero...
1578 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1579
1580 // Reload the modified control word now...
1581 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1582
1583 // Restore the memory image of control word to original value
1584 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1585
1586 // Get the X86 opcode to use.
1587 unsigned Opc;
1588 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001589 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001590 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1591 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1592 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1593 }
1594
1595 X86AddressMode AM;
1596 MachineOperand &Op = MI->getOperand(0);
1597 if (Op.isRegister()) {
1598 AM.BaseType = X86AddressMode::RegBase;
1599 AM.Base.Reg = Op.getReg();
1600 } else {
1601 AM.BaseType = X86AddressMode::FrameIndexBase;
1602 AM.Base.FrameIndex = Op.getFrameIndex();
1603 }
1604 Op = MI->getOperand(1);
1605 if (Op.isImmediate())
1606 AM.Scale = Op.getImmedValue();
1607 Op = MI->getOperand(2);
1608 if (Op.isImmediate())
1609 AM.IndexReg = Op.getImmedValue();
1610 Op = MI->getOperand(3);
1611 if (Op.isGlobalAddress()) {
1612 AM.GV = Op.getGlobal();
1613 } else {
1614 AM.Disp = Op.getImmedValue();
1615 }
1616 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1617
1618 // Reload the original control word now.
1619 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1620
1621 delete MI; // The pseudo instruction is gone now.
1622 return BB;
1623 }
1624 }
Evan Cheng339edad2006-01-11 00:33:36 +00001625}
1626
1627
1628//===----------------------------------------------------------------------===//
1629// X86 Custom Lowering Hooks
1630//===----------------------------------------------------------------------===//
1631
Evan Chengaf598d22006-03-13 23:18:16 +00001632/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1633/// load. For Darwin, external and weak symbols are indirect, loading the value
1634/// at address GV rather then the value of GV itself. This means that the
1635/// GlobalAddress must be in the base or index register of the address, not the
1636/// GV offset field.
1637static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1638 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1639 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1640}
1641
Evan Chengc995b452006-04-06 23:23:56 +00001642/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001643/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001644static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1645 if (Op.getOpcode() == ISD::UNDEF)
1646 return true;
1647
1648 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001649 return (Val >= Low && Val < Hi);
1650}
1651
1652/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1653/// true if Op is undef or if its value equal to the specified value.
1654static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1655 if (Op.getOpcode() == ISD::UNDEF)
1656 return true;
1657 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001658}
1659
Evan Cheng68ad48b2006-03-22 18:59:22 +00001660/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1661/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1662bool X86::isPSHUFDMask(SDNode *N) {
1663 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1664
1665 if (N->getNumOperands() != 4)
1666 return false;
1667
1668 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001669 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001670 SDOperand Arg = N->getOperand(i);
1671 if (Arg.getOpcode() == ISD::UNDEF) continue;
1672 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1673 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001674 return false;
1675 }
1676
1677 return true;
1678}
1679
1680/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001681/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001682bool X86::isPSHUFHWMask(SDNode *N) {
1683 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1684
1685 if (N->getNumOperands() != 8)
1686 return false;
1687
1688 // Lower quadword copied in order.
1689 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001690 SDOperand Arg = N->getOperand(i);
1691 if (Arg.getOpcode() == ISD::UNDEF) continue;
1692 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1693 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001694 return false;
1695 }
1696
1697 // Upper quadword shuffled.
1698 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001699 SDOperand Arg = N->getOperand(i);
1700 if (Arg.getOpcode() == ISD::UNDEF) continue;
1701 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1702 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001703 if (Val < 4 || Val > 7)
1704 return false;
1705 }
1706
1707 return true;
1708}
1709
1710/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001711/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001712bool X86::isPSHUFLWMask(SDNode *N) {
1713 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1714
1715 if (N->getNumOperands() != 8)
1716 return false;
1717
1718 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001719 for (unsigned i = 4; i != 8; ++i)
1720 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001721 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001722
1723 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001724 for (unsigned i = 0; i != 4; ++i)
1725 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001726 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001727
1728 return true;
1729}
1730
Evan Chengd27fb3e2006-03-24 01:18:28 +00001731/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1732/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng60f0b892006-04-20 08:58:49 +00001733static bool isSHUFPMask(std::vector<SDOperand> &N) {
1734 unsigned NumElems = N.size();
1735 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001736
Evan Cheng60f0b892006-04-20 08:58:49 +00001737 unsigned Half = NumElems / 2;
1738 for (unsigned i = 0; i < Half; ++i)
1739 if (!isUndefOrInRange(N[i], 0, NumElems))
1740 return false;
1741 for (unsigned i = Half; i < NumElems; ++i)
1742 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1743 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001744
1745 return true;
1746}
1747
Evan Cheng60f0b892006-04-20 08:58:49 +00001748bool X86::isSHUFPMask(SDNode *N) {
1749 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1750 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1751 return ::isSHUFPMask(Ops);
1752}
1753
1754/// isCommutedSHUFP - Returns true if the shuffle mask is except
1755/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1756/// half elements to come from vector 1 (which would equal the dest.) and
1757/// the upper half to come from vector 2.
1758static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1759 unsigned NumElems = Ops.size();
1760 if (NumElems != 2 && NumElems != 4) return false;
1761
1762 unsigned Half = NumElems / 2;
1763 for (unsigned i = 0; i < Half; ++i)
1764 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1765 return false;
1766 for (unsigned i = Half; i < NumElems; ++i)
1767 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1768 return false;
1769 return true;
1770}
1771
1772static bool isCommutedSHUFP(SDNode *N) {
1773 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1774 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1775 return isCommutedSHUFP(Ops);
1776}
1777
Evan Cheng2595a682006-03-24 02:58:06 +00001778/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1779/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1780bool X86::isMOVHLPSMask(SDNode *N) {
1781 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1782
Evan Cheng1a194a52006-03-28 06:50:32 +00001783 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001784 return false;
1785
Evan Cheng1a194a52006-03-28 06:50:32 +00001786 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001787 return isUndefOrEqual(N->getOperand(0), 6) &&
1788 isUndefOrEqual(N->getOperand(1), 7) &&
1789 isUndefOrEqual(N->getOperand(2), 2) &&
1790 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001791}
1792
Evan Chengc995b452006-04-06 23:23:56 +00001793/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1794/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1795bool X86::isMOVLPMask(SDNode *N) {
1796 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1797
1798 unsigned NumElems = N->getNumOperands();
1799 if (NumElems != 2 && NumElems != 4)
1800 return false;
1801
Evan Chengac847262006-04-07 21:53:05 +00001802 for (unsigned i = 0; i < NumElems/2; ++i)
1803 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1804 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001805
Evan Chengac847262006-04-07 21:53:05 +00001806 for (unsigned i = NumElems/2; i < NumElems; ++i)
1807 if (!isUndefOrEqual(N->getOperand(i), i))
1808 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001809
1810 return true;
1811}
1812
1813/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001814/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1815/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001816bool X86::isMOVHPMask(SDNode *N) {
1817 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1818
1819 unsigned NumElems = N->getNumOperands();
1820 if (NumElems != 2 && NumElems != 4)
1821 return false;
1822
Evan Chengac847262006-04-07 21:53:05 +00001823 for (unsigned i = 0; i < NumElems/2; ++i)
1824 if (!isUndefOrEqual(N->getOperand(i), i))
1825 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001826
1827 for (unsigned i = 0; i < NumElems/2; ++i) {
1828 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001829 if (!isUndefOrEqual(Arg, i + NumElems))
1830 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001831 }
1832
1833 return true;
1834}
1835
Evan Cheng5df75882006-03-28 00:39:58 +00001836/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1837/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng60f0b892006-04-20 08:58:49 +00001838bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1839 unsigned NumElems = N.size();
Evan Cheng5df75882006-03-28 00:39:58 +00001840 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1841 return false;
1842
1843 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001844 SDOperand BitI = N[i];
1845 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001846 if (!isUndefOrEqual(BitI, j))
1847 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001848 if (V2IsSplat) {
1849 if (isUndefOrEqual(BitI1, NumElems))
1850 return false;
1851 } else {
1852 if (!isUndefOrEqual(BitI1, j + NumElems))
1853 return false;
1854 }
Evan Cheng5df75882006-03-28 00:39:58 +00001855 }
1856
1857 return true;
1858}
1859
Evan Cheng60f0b892006-04-20 08:58:49 +00001860bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1861 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1862 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1863 return ::isUNPCKLMask(Ops, V2IsSplat);
1864}
1865
Evan Cheng2bc32802006-03-28 02:43:26 +00001866/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1867/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng60f0b892006-04-20 08:58:49 +00001868bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1869 unsigned NumElems = N.size();
Evan Cheng2bc32802006-03-28 02:43:26 +00001870 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1871 return false;
1872
1873 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001874 SDOperand BitI = N[i];
1875 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001876 if (!isUndefOrEqual(BitI, j + NumElems/2))
1877 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001878 if (V2IsSplat) {
1879 if (isUndefOrEqual(BitI1, NumElems))
1880 return false;
1881 } else {
1882 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1883 return false;
1884 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001885 }
1886
1887 return true;
1888}
1889
Evan Cheng60f0b892006-04-20 08:58:49 +00001890bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1891 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1892 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1893 return ::isUNPCKHMask(Ops, V2IsSplat);
1894}
1895
Evan Chengf3b52c82006-04-05 07:20:06 +00001896/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1897/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1898/// <0, 0, 1, 1>
1899bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1900 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1901
1902 unsigned NumElems = N->getNumOperands();
1903 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1904 return false;
1905
1906 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1907 SDOperand BitI = N->getOperand(i);
1908 SDOperand BitI1 = N->getOperand(i+1);
1909
Evan Chengac847262006-04-07 21:53:05 +00001910 if (!isUndefOrEqual(BitI, j))
1911 return false;
1912 if (!isUndefOrEqual(BitI1, j))
1913 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001914 }
1915
1916 return true;
1917}
1918
Evan Chenge8b51802006-04-21 01:05:10 +00001919/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1920/// specifies a shuffle of elements that is suitable for input to MOVSS,
1921/// MOVSD, and MOVD, i.e. setting the lowest element.
1922static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001923 unsigned NumElems = N.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001924 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001925 return false;
1926
Evan Cheng60f0b892006-04-20 08:58:49 +00001927 if (!isUndefOrEqual(N[0], NumElems))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001928 return false;
1929
1930 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001931 SDOperand Arg = N[i];
Evan Cheng12ba3e22006-04-11 00:19:04 +00001932 if (!isUndefOrEqual(Arg, i))
1933 return false;
1934 }
1935
1936 return true;
1937}
Evan Chengf3b52c82006-04-05 07:20:06 +00001938
Evan Chenge8b51802006-04-21 01:05:10 +00001939bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001940 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1941 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001942 return ::isMOVLMask(Ops);
Evan Cheng60f0b892006-04-20 08:58:49 +00001943}
1944
Evan Chenge8b51802006-04-21 01:05:10 +00001945/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1946/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001947/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Chenge8b51802006-04-21 01:05:10 +00001948static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001949 unsigned NumElems = Ops.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001950 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001951 return false;
1952
1953 if (!isUndefOrEqual(Ops[0], 0))
1954 return false;
1955
1956 for (unsigned i = 1; i < NumElems; ++i) {
1957 SDOperand Arg = Ops[i];
1958 if (V2IsSplat) {
1959 if (!isUndefOrEqual(Arg, NumElems))
1960 return false;
1961 } else {
1962 if (!isUndefOrEqual(Arg, i+NumElems))
1963 return false;
1964 }
1965 }
1966
1967 return true;
1968}
1969
Evan Chenge8b51802006-04-21 01:05:10 +00001970static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001971 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1972 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001973 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001974}
1975
Evan Cheng5d247f82006-04-14 21:59:03 +00001976/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1977/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1978bool X86::isMOVSHDUPMask(SDNode *N) {
1979 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1980
1981 if (N->getNumOperands() != 4)
1982 return false;
1983
1984 // Expect 1, 1, 3, 3
1985 for (unsigned i = 0; i < 2; ++i) {
1986 SDOperand Arg = N->getOperand(i);
1987 if (Arg.getOpcode() == ISD::UNDEF) continue;
1988 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1989 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1990 if (Val != 1) return false;
1991 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001992
1993 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001994 for (unsigned i = 2; i < 4; ++i) {
1995 SDOperand Arg = N->getOperand(i);
1996 if (Arg.getOpcode() == ISD::UNDEF) continue;
1997 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1998 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1999 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002000 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002001 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002002
Evan Cheng6222cf22006-04-15 05:37:34 +00002003 // Don't use movshdup if it can be done with a shufps.
2004 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002005}
2006
2007/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2008/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2009bool X86::isMOVSLDUPMask(SDNode *N) {
2010 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2011
2012 if (N->getNumOperands() != 4)
2013 return false;
2014
2015 // Expect 0, 0, 2, 2
2016 for (unsigned i = 0; i < 2; ++i) {
2017 SDOperand Arg = N->getOperand(i);
2018 if (Arg.getOpcode() == ISD::UNDEF) continue;
2019 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2020 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2021 if (Val != 0) return false;
2022 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002023
2024 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002025 for (unsigned i = 2; i < 4; ++i) {
2026 SDOperand Arg = N->getOperand(i);
2027 if (Arg.getOpcode() == ISD::UNDEF) continue;
2028 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2029 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2030 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002031 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002032 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002033
Evan Cheng6222cf22006-04-15 05:37:34 +00002034 // Don't use movshdup if it can be done with a shufps.
2035 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002036}
2037
Evan Chengd097e672006-03-22 02:53:00 +00002038/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2039/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00002040static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002041 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2042
Evan Chengd097e672006-03-22 02:53:00 +00002043 // This is a splat operation if each element of the permute is the same, and
2044 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002045 unsigned NumElems = N->getNumOperands();
2046 SDOperand ElementBase;
2047 unsigned i = 0;
2048 for (; i != NumElems; ++i) {
2049 SDOperand Elt = N->getOperand(i);
2050 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
2051 ElementBase = Elt;
2052 break;
2053 }
2054 }
2055
2056 if (!ElementBase.Val)
2057 return false;
2058
2059 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002060 SDOperand Arg = N->getOperand(i);
2061 if (Arg.getOpcode() == ISD::UNDEF) continue;
2062 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002063 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00002064 }
2065
2066 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002067 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00002068}
2069
Evan Cheng5022b342006-04-17 20:43:08 +00002070/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2071/// a splat of a single element and it's a 2 or 4 element mask.
2072bool X86::isSplatMask(SDNode *N) {
2073 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2074
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002075 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00002076 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2077 return false;
2078 return ::isSplatMask(N);
2079}
2080
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002081/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2082/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2083/// instructions.
2084unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002085 unsigned NumOperands = N->getNumOperands();
2086 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2087 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00002088 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002089 unsigned Val = 0;
2090 SDOperand Arg = N->getOperand(NumOperands-i-1);
2091 if (Arg.getOpcode() != ISD::UNDEF)
2092 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00002093 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002094 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00002095 if (i != NumOperands - 1)
2096 Mask <<= Shift;
2097 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002098
2099 return Mask;
2100}
2101
Evan Chengb7fedff2006-03-29 23:07:14 +00002102/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2103/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2104/// instructions.
2105unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2106 unsigned Mask = 0;
2107 // 8 nodes, but we only care about the last 4.
2108 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002109 unsigned Val = 0;
2110 SDOperand Arg = N->getOperand(i);
2111 if (Arg.getOpcode() != ISD::UNDEF)
2112 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002113 Mask |= (Val - 4);
2114 if (i != 4)
2115 Mask <<= 2;
2116 }
2117
2118 return Mask;
2119}
2120
2121/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2122/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2123/// instructions.
2124unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2125 unsigned Mask = 0;
2126 // 8 nodes, but we only care about the first 4.
2127 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002128 unsigned Val = 0;
2129 SDOperand Arg = N->getOperand(i);
2130 if (Arg.getOpcode() != ISD::UNDEF)
2131 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002132 Mask |= Val;
2133 if (i != 0)
2134 Mask <<= 2;
2135 }
2136
2137 return Mask;
2138}
2139
Evan Cheng59a63552006-04-05 01:47:37 +00002140/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2141/// specifies a 8 element shuffle that can be broken into a pair of
2142/// PSHUFHW and PSHUFLW.
2143static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2144 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2145
2146 if (N->getNumOperands() != 8)
2147 return false;
2148
2149 // Lower quadword shuffled.
2150 for (unsigned i = 0; i != 4; ++i) {
2151 SDOperand Arg = N->getOperand(i);
2152 if (Arg.getOpcode() == ISD::UNDEF) continue;
2153 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2154 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2155 if (Val > 4)
2156 return false;
2157 }
2158
2159 // Upper quadword shuffled.
2160 for (unsigned i = 4; i != 8; ++i) {
2161 SDOperand Arg = N->getOperand(i);
2162 if (Arg.getOpcode() == ISD::UNDEF) continue;
2163 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2164 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2165 if (Val < 4 || Val > 7)
2166 return false;
2167 }
2168
2169 return true;
2170}
2171
Evan Chengc995b452006-04-06 23:23:56 +00002172/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2173/// values in ther permute mask.
2174static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2175 SDOperand V1 = Op.getOperand(0);
2176 SDOperand V2 = Op.getOperand(1);
2177 SDOperand Mask = Op.getOperand(2);
2178 MVT::ValueType VT = Op.getValueType();
2179 MVT::ValueType MaskVT = Mask.getValueType();
2180 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2181 unsigned NumElems = Mask.getNumOperands();
2182 std::vector<SDOperand> MaskVec;
2183
2184 for (unsigned i = 0; i != NumElems; ++i) {
2185 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002186 if (Arg.getOpcode() == ISD::UNDEF) {
2187 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2188 continue;
2189 }
Evan Chengc995b452006-04-06 23:23:56 +00002190 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2191 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2192 if (Val < NumElems)
2193 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2194 else
2195 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2196 }
2197
2198 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2199 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2200}
2201
Evan Cheng7855e4d2006-04-19 20:35:22 +00002202/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2203/// match movhlps. The lower half elements should come from upper half of
2204/// V1 (and in order), and the upper half elements should come from the upper
2205/// half of V2 (and in order).
2206static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2207 unsigned NumElems = Mask->getNumOperands();
2208 if (NumElems != 4)
2209 return false;
2210 for (unsigned i = 0, e = 2; i != e; ++i)
2211 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2212 return false;
2213 for (unsigned i = 2; i != 4; ++i)
2214 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2215 return false;
2216 return true;
2217}
2218
Evan Chengc995b452006-04-06 23:23:56 +00002219/// isScalarLoadToVector - Returns true if the node is a scalar load that
2220/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002221static inline bool isScalarLoadToVector(SDNode *N) {
2222 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2223 N = N->getOperand(0).Val;
2224 return (N->getOpcode() == ISD::LOAD);
Evan Chengc995b452006-04-06 23:23:56 +00002225 }
2226 return false;
2227}
2228
Evan Cheng7855e4d2006-04-19 20:35:22 +00002229/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2230/// match movlp{s|d}. The lower half elements should come from lower half of
2231/// V1 (and in order), and the upper half elements should come from the upper
2232/// half of V2 (and in order). And since V1 will become the source of the
2233/// MOVLP, it must be either a vector load or a scalar load to vector.
2234static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2235 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2236 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002237
Evan Cheng7855e4d2006-04-19 20:35:22 +00002238 unsigned NumElems = Mask->getNumOperands();
2239 if (NumElems != 2 && NumElems != 4)
2240 return false;
2241 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2242 if (!isUndefOrEqual(Mask->getOperand(i), i))
2243 return false;
2244 for (unsigned i = NumElems/2; i != NumElems; ++i)
2245 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2246 return false;
2247 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002248}
2249
Evan Cheng60f0b892006-04-20 08:58:49 +00002250/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2251/// all the same.
2252static bool isSplatVector(SDNode *N) {
2253 if (N->getOpcode() != ISD::BUILD_VECTOR)
2254 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002255
Evan Cheng60f0b892006-04-20 08:58:49 +00002256 SDOperand SplatValue = N->getOperand(0);
2257 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2258 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002259 return false;
2260 return true;
2261}
2262
Evan Cheng60f0b892006-04-20 08:58:49 +00002263/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2264/// that point to V2 points to its first element.
2265static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2266 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2267
2268 bool Changed = false;
2269 std::vector<SDOperand> MaskVec;
2270 unsigned NumElems = Mask.getNumOperands();
2271 for (unsigned i = 0; i != NumElems; ++i) {
2272 SDOperand Arg = Mask.getOperand(i);
2273 if (Arg.getOpcode() != ISD::UNDEF) {
2274 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2275 if (Val > NumElems) {
2276 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2277 Changed = true;
2278 }
2279 }
2280 MaskVec.push_back(Arg);
2281 }
2282
2283 if (Changed)
2284 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2285 return Mask;
2286}
2287
Evan Chenge8b51802006-04-21 01:05:10 +00002288/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2289/// operation of specified width.
2290static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002291 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2292 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2293
2294 std::vector<SDOperand> MaskVec;
2295 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2296 for (unsigned i = 1; i != NumElems; ++i)
2297 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2298 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2299}
2300
Evan Cheng5022b342006-04-17 20:43:08 +00002301/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2302/// of specified width.
2303static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2304 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2305 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2306 std::vector<SDOperand> MaskVec;
2307 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2308 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2309 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2310 }
2311 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2312}
2313
Evan Cheng60f0b892006-04-20 08:58:49 +00002314/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2315/// of specified width.
2316static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2317 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2318 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2319 unsigned Half = NumElems/2;
2320 std::vector<SDOperand> MaskVec;
2321 for (unsigned i = 0; i != Half; ++i) {
2322 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2323 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2324 }
2325 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2326}
2327
Evan Chenge8b51802006-04-21 01:05:10 +00002328/// getZeroVector - Returns a vector of specified type with all zero elements.
2329///
2330static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2331 assert(MVT::isVector(VT) && "Expected a vector type");
2332 unsigned NumElems = getVectorNumElements(VT);
2333 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2334 bool isFP = MVT::isFloatingPoint(EVT);
2335 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2336 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2337 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2338}
2339
Evan Cheng5022b342006-04-17 20:43:08 +00002340/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2341///
2342static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2343 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002344 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002345 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002346 unsigned NumElems = Mask.getNumOperands();
2347 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002348 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002349 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002350 NumElems >>= 1;
2351 }
2352 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2353
2354 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002355 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002356 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002357 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002358 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2359}
2360
Evan Chenge8b51802006-04-21 01:05:10 +00002361/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2362/// constant +0.0.
2363static inline bool isZeroNode(SDOperand Elt) {
2364 return ((isa<ConstantSDNode>(Elt) &&
2365 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2366 (isa<ConstantFPSDNode>(Elt) &&
2367 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2368}
2369
Evan Cheng14215c32006-04-21 23:03:30 +00002370/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2371/// vector and zero or undef vector.
2372static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002373 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002374 bool isZero, SelectionDAG &DAG) {
2375 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002376 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2377 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2378 SDOperand Zero = DAG.getConstant(0, EVT);
2379 std::vector<SDOperand> MaskVec(NumElems, Zero);
2380 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2381 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng14215c32006-04-21 23:03:30 +00002382 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002383}
2384
Evan Chengb0461082006-04-24 18:01:45 +00002385/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2386///
2387static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2388 unsigned NumNonZero, unsigned NumZero,
2389 SelectionDAG &DAG) {
2390 if (NumNonZero > 8)
2391 return SDOperand();
2392
2393 SDOperand V(0, 0);
2394 bool First = true;
2395 for (unsigned i = 0; i < 16; ++i) {
2396 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2397 if (ThisIsNonZero && First) {
2398 if (NumZero)
2399 V = getZeroVector(MVT::v8i16, DAG);
2400 else
2401 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2402 First = false;
2403 }
2404
2405 if ((i & 1) != 0) {
2406 SDOperand ThisElt(0, 0), LastElt(0, 0);
2407 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2408 if (LastIsNonZero) {
2409 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2410 }
2411 if (ThisIsNonZero) {
2412 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2413 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2414 ThisElt, DAG.getConstant(8, MVT::i8));
2415 if (LastIsNonZero)
2416 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2417 } else
2418 ThisElt = LastElt;
2419
2420 if (ThisElt.Val)
2421 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2422 DAG.getConstant(i/2, MVT::i32));
2423 }
2424 }
2425
2426 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2427}
2428
2429/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2430///
2431static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2432 unsigned NumNonZero, unsigned NumZero,
2433 SelectionDAG &DAG) {
2434 if (NumNonZero > 4)
2435 return SDOperand();
2436
2437 SDOperand V(0, 0);
2438 bool First = true;
2439 for (unsigned i = 0; i < 8; ++i) {
2440 bool isNonZero = (NonZeros & (1 << i)) != 0;
2441 if (isNonZero) {
2442 if (First) {
2443 if (NumZero)
2444 V = getZeroVector(MVT::v8i16, DAG);
2445 else
2446 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2447 First = false;
2448 }
2449 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2450 DAG.getConstant(i, MVT::i32));
2451 }
2452 }
2453
2454 return V;
2455}
2456
Evan Chenga9467aa2006-04-25 20:13:52 +00002457SDOperand
2458X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2459 // All zero's are handled with pxor.
2460 if (ISD::isBuildVectorAllZeros(Op.Val))
2461 return Op;
2462
2463 // All one's are handled with pcmpeqd.
2464 if (ISD::isBuildVectorAllOnes(Op.Val))
2465 return Op;
2466
2467 MVT::ValueType VT = Op.getValueType();
2468 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2469 unsigned EVTBits = MVT::getSizeInBits(EVT);
2470
2471 unsigned NumElems = Op.getNumOperands();
2472 unsigned NumZero = 0;
2473 unsigned NumNonZero = 0;
2474 unsigned NonZeros = 0;
2475 std::set<SDOperand> Values;
2476 for (unsigned i = 0; i < NumElems; ++i) {
2477 SDOperand Elt = Op.getOperand(i);
2478 if (Elt.getOpcode() != ISD::UNDEF) {
2479 Values.insert(Elt);
2480 if (isZeroNode(Elt))
2481 NumZero++;
2482 else {
2483 NonZeros |= (1 << i);
2484 NumNonZero++;
2485 }
2486 }
2487 }
2488
2489 if (NumNonZero == 0)
2490 // Must be a mix of zero and undef. Return a zero vector.
2491 return getZeroVector(VT, DAG);
2492
2493 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2494 if (Values.size() == 1)
2495 return SDOperand();
2496
2497 // Special case for single non-zero element.
2498 if (NumNonZero == 1) {
2499 unsigned Idx = CountTrailingZeros_32(NonZeros);
2500 SDOperand Item = Op.getOperand(Idx);
2501 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2502 if (Idx == 0)
2503 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2504 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2505 NumZero > 0, DAG);
2506
2507 if (EVTBits == 32) {
2508 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2509 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2510 DAG);
2511 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2512 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2513 std::vector<SDOperand> MaskVec;
2514 for (unsigned i = 0; i < NumElems; i++)
2515 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2516 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2517 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2518 DAG.getNode(ISD::UNDEF, VT), Mask);
2519 }
2520 }
2521
2522 // Let legalizer expand 2-widde build_vector's.
2523 if (EVTBits == 64)
2524 return SDOperand();
2525
2526 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2527 if (EVTBits == 8) {
2528 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2529 if (V.Val) return V;
2530 }
2531
2532 if (EVTBits == 16) {
2533 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2534 if (V.Val) return V;
2535 }
2536
2537 // If element VT is == 32 bits, turn it into a number of shuffles.
2538 std::vector<SDOperand> V(NumElems);
2539 if (NumElems == 4 && NumZero > 0) {
2540 for (unsigned i = 0; i < 4; ++i) {
2541 bool isZero = !(NonZeros & (1 << i));
2542 if (isZero)
2543 V[i] = getZeroVector(VT, DAG);
2544 else
2545 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2546 }
2547
2548 for (unsigned i = 0; i < 2; ++i) {
2549 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2550 default: break;
2551 case 0:
2552 V[i] = V[i*2]; // Must be a zero vector.
2553 break;
2554 case 1:
2555 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2556 getMOVLMask(NumElems, DAG));
2557 break;
2558 case 2:
2559 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2560 getMOVLMask(NumElems, DAG));
2561 break;
2562 case 3:
2563 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2564 getUnpacklMask(NumElems, DAG));
2565 break;
2566 }
2567 }
2568
Evan Cheng9fee4422006-05-16 07:21:53 +00002569 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Chenga9467aa2006-04-25 20:13:52 +00002570 // clears the upper bits.
2571 // FIXME: we can do the same for v4f32 case when we know both parts of
2572 // the lower half come from scalar_to_vector (loadf32). We should do
2573 // that in post legalizer dag combiner with target specific hooks.
2574 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2575 return V[0];
2576 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2577 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2578 std::vector<SDOperand> MaskVec;
2579 bool Reverse = (NonZeros & 0x3) == 2;
2580 for (unsigned i = 0; i < 2; ++i)
2581 if (Reverse)
2582 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2583 else
2584 MaskVec.push_back(DAG.getConstant(i, EVT));
2585 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2586 for (unsigned i = 0; i < 2; ++i)
2587 if (Reverse)
2588 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2589 else
2590 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2591 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2592 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2593 }
2594
2595 if (Values.size() > 2) {
2596 // Expand into a number of unpckl*.
2597 // e.g. for v4f32
2598 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2599 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2600 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2601 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2602 for (unsigned i = 0; i < NumElems; ++i)
2603 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2604 NumElems >>= 1;
2605 while (NumElems != 0) {
2606 for (unsigned i = 0; i < NumElems; ++i)
2607 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2608 UnpckMask);
2609 NumElems >>= 1;
2610 }
2611 return V[0];
2612 }
2613
2614 return SDOperand();
2615}
2616
2617SDOperand
2618X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2619 SDOperand V1 = Op.getOperand(0);
2620 SDOperand V2 = Op.getOperand(1);
2621 SDOperand PermMask = Op.getOperand(2);
2622 MVT::ValueType VT = Op.getValueType();
2623 unsigned NumElems = PermMask.getNumOperands();
2624 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2625 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2626
2627 if (isSplatMask(PermMask.Val)) {
2628 if (NumElems <= 4) return Op;
2629 // Promote it to a v4i32 splat.
2630 return PromoteSplat(Op, DAG);
2631 }
2632
2633 if (X86::isMOVLMask(PermMask.Val))
2634 return (V1IsUndef) ? V2 : Op;
2635
2636 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2637 X86::isMOVSLDUPMask(PermMask.Val) ||
2638 X86::isMOVHLPSMask(PermMask.Val) ||
2639 X86::isMOVHPMask(PermMask.Val) ||
2640 X86::isMOVLPMask(PermMask.Val))
2641 return Op;
2642
2643 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2644 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2645 return CommuteVectorShuffle(Op, DAG);
2646
2647 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2648 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2649 if (V1IsSplat && !V2IsSplat) {
2650 Op = CommuteVectorShuffle(Op, DAG);
2651 V1 = Op.getOperand(0);
2652 V2 = Op.getOperand(1);
2653 PermMask = Op.getOperand(2);
2654 V2IsSplat = true;
2655 }
2656
2657 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2658 if (V2IsUndef) return V1;
2659 Op = CommuteVectorShuffle(Op, DAG);
2660 V1 = Op.getOperand(0);
2661 V2 = Op.getOperand(1);
2662 PermMask = Op.getOperand(2);
2663 if (V2IsSplat) {
2664 // V2 is a splat, so the mask may be malformed. That is, it may point
2665 // to any V2 element. The instruction selectior won't like this. Get
2666 // a corrected mask and commute to form a proper MOVS{S|D}.
2667 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2668 if (NewMask.Val != PermMask.Val)
2669 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2670 }
2671 return Op;
2672 }
2673
2674 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2675 X86::isUNPCKLMask(PermMask.Val) ||
2676 X86::isUNPCKHMask(PermMask.Val))
2677 return Op;
2678
2679 if (V2IsSplat) {
2680 // Normalize mask so all entries that point to V2 points to its first
2681 // element then try to match unpck{h|l} again. If match, return a
2682 // new vector_shuffle with the corrected mask.
2683 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2684 if (NewMask.Val != PermMask.Val) {
2685 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2686 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2687 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2688 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2689 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2690 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2691 }
2692 }
2693 }
2694
2695 // Normalize the node to match x86 shuffle ops if needed
2696 if (V2.getOpcode() != ISD::UNDEF)
2697 if (isCommutedSHUFP(PermMask.Val)) {
2698 Op = CommuteVectorShuffle(Op, DAG);
2699 V1 = Op.getOperand(0);
2700 V2 = Op.getOperand(1);
2701 PermMask = Op.getOperand(2);
2702 }
2703
2704 // If VT is integer, try PSHUF* first, then SHUFP*.
2705 if (MVT::isInteger(VT)) {
2706 if (X86::isPSHUFDMask(PermMask.Val) ||
2707 X86::isPSHUFHWMask(PermMask.Val) ||
2708 X86::isPSHUFLWMask(PermMask.Val)) {
2709 if (V2.getOpcode() != ISD::UNDEF)
2710 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2711 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2712 return Op;
2713 }
2714
2715 if (X86::isSHUFPMask(PermMask.Val))
2716 return Op;
2717
2718 // Handle v8i16 shuffle high / low shuffle node pair.
2719 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2720 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2721 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2722 std::vector<SDOperand> MaskVec;
2723 for (unsigned i = 0; i != 4; ++i)
2724 MaskVec.push_back(PermMask.getOperand(i));
2725 for (unsigned i = 4; i != 8; ++i)
2726 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2727 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2728 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2729 MaskVec.clear();
2730 for (unsigned i = 0; i != 4; ++i)
2731 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2732 for (unsigned i = 4; i != 8; ++i)
2733 MaskVec.push_back(PermMask.getOperand(i));
2734 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2735 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2736 }
2737 } else {
2738 // Floating point cases in the other order.
2739 if (X86::isSHUFPMask(PermMask.Val))
2740 return Op;
2741 if (X86::isPSHUFDMask(PermMask.Val) ||
2742 X86::isPSHUFHWMask(PermMask.Val) ||
2743 X86::isPSHUFLWMask(PermMask.Val)) {
2744 if (V2.getOpcode() != ISD::UNDEF)
2745 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2746 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2747 return Op;
2748 }
2749 }
2750
2751 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002752 MVT::ValueType MaskVT = PermMask.getValueType();
2753 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng3cd43622006-04-28 07:03:38 +00002754 std::vector<std::pair<int, int> > Locs;
2755 Locs.reserve(NumElems);
2756 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2757 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2758 unsigned NumHi = 0;
2759 unsigned NumLo = 0;
2760 // If no more than two elements come from either vector. This can be
2761 // implemented with two shuffles. First shuffle gather the elements.
2762 // The second shuffle, which takes the first shuffle as both of its
2763 // vector operands, put the elements into the right order.
2764 for (unsigned i = 0; i != NumElems; ++i) {
2765 SDOperand Elt = PermMask.getOperand(i);
2766 if (Elt.getOpcode() == ISD::UNDEF) {
2767 Locs[i] = std::make_pair(-1, -1);
2768 } else {
2769 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2770 if (Val < NumElems) {
2771 Locs[i] = std::make_pair(0, NumLo);
2772 Mask1[NumLo] = Elt;
2773 NumLo++;
2774 } else {
2775 Locs[i] = std::make_pair(1, NumHi);
2776 if (2+NumHi < NumElems)
2777 Mask1[2+NumHi] = Elt;
2778 NumHi++;
2779 }
2780 }
2781 }
2782 if (NumLo <= 2 && NumHi <= 2) {
2783 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2784 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2785 for (unsigned i = 0; i != NumElems; ++i) {
2786 if (Locs[i].first == -1)
2787 continue;
2788 else {
2789 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2790 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2791 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2792 }
2793 }
2794
2795 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2796 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2797 }
2798
2799 // Break it into (shuffle shuffle_hi, shuffle_lo).
2800 Locs.clear();
Evan Chenga9467aa2006-04-25 20:13:52 +00002801 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2802 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2803 std::vector<SDOperand> *MaskPtr = &LoMask;
2804 unsigned MaskIdx = 0;
2805 unsigned LoIdx = 0;
2806 unsigned HiIdx = NumElems/2;
2807 for (unsigned i = 0; i != NumElems; ++i) {
2808 if (i == NumElems/2) {
2809 MaskPtr = &HiMask;
2810 MaskIdx = 1;
2811 LoIdx = 0;
2812 HiIdx = NumElems/2;
2813 }
2814 SDOperand Elt = PermMask.getOperand(i);
2815 if (Elt.getOpcode() == ISD::UNDEF) {
2816 Locs[i] = std::make_pair(-1, -1);
2817 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2818 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2819 (*MaskPtr)[LoIdx] = Elt;
2820 LoIdx++;
2821 } else {
2822 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2823 (*MaskPtr)[HiIdx] = Elt;
2824 HiIdx++;
2825 }
2826 }
2827
Chris Lattner3d826992006-05-16 06:45:34 +00002828 SDOperand LoShuffle =
2829 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2830 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2831 SDOperand HiShuffle =
2832 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2833 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Chenga9467aa2006-04-25 20:13:52 +00002834 std::vector<SDOperand> MaskOps;
2835 for (unsigned i = 0; i != NumElems; ++i) {
2836 if (Locs[i].first == -1) {
2837 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2838 } else {
2839 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2840 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2841 }
2842 }
2843 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2844 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2845 }
2846
2847 return SDOperand();
2848}
2849
2850SDOperand
2851X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2852 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2853 return SDOperand();
2854
2855 MVT::ValueType VT = Op.getValueType();
2856 // TODO: handle v16i8.
2857 if (MVT::getSizeInBits(VT) == 16) {
2858 // Transform it so it match pextrw which produces a 32-bit result.
2859 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2860 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2861 Op.getOperand(0), Op.getOperand(1));
2862 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2863 DAG.getValueType(VT));
2864 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2865 } else if (MVT::getSizeInBits(VT) == 32) {
2866 SDOperand Vec = Op.getOperand(0);
2867 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2868 if (Idx == 0)
2869 return Op;
2870
2871 // SHUFPS the element to the lowest double word, then movss.
2872 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2873 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2874 MVT::getVectorBaseType(MaskVT));
2875 std::vector<SDOperand> IdxVec;
2876 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2877 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2878 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2879 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2880 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2881 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2882 Vec, Vec, Mask);
2883 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2884 DAG.getConstant(0, MVT::i32));
2885 } else if (MVT::getSizeInBits(VT) == 64) {
2886 SDOperand Vec = Op.getOperand(0);
2887 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2888 if (Idx == 0)
2889 return Op;
2890
2891 // UNPCKHPD the element to the lowest double word, then movsd.
2892 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2893 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2894 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2895 std::vector<SDOperand> IdxVec;
2896 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2897 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2898 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2899 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2900 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2901 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2902 DAG.getConstant(0, MVT::i32));
2903 }
2904
2905 return SDOperand();
2906}
2907
2908SDOperand
2909X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002910 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002911 // as its second argument.
2912 MVT::ValueType VT = Op.getValueType();
2913 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2914 SDOperand N0 = Op.getOperand(0);
2915 SDOperand N1 = Op.getOperand(1);
2916 SDOperand N2 = Op.getOperand(2);
2917 if (MVT::getSizeInBits(BaseVT) == 16) {
2918 if (N1.getValueType() != MVT::i32)
2919 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2920 if (N2.getValueType() != MVT::i32)
2921 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2922 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2923 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2924 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2925 if (Idx == 0) {
2926 // Use a movss.
2927 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2928 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2929 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2930 std::vector<SDOperand> MaskVec;
2931 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2932 for (unsigned i = 1; i <= 3; ++i)
2933 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2934 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2935 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2936 } else {
2937 // Use two pinsrw instructions to insert a 32 bit value.
2938 Idx <<= 1;
2939 if (MVT::isFloatingPoint(N1.getValueType())) {
2940 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002941 // Just load directly from f32mem to GR32.
Evan Chenga9467aa2006-04-25 20:13:52 +00002942 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2943 N1.getOperand(2));
2944 } else {
2945 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2946 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2947 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2948 DAG.getConstant(0, MVT::i32));
2949 }
2950 }
2951 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2952 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2953 DAG.getConstant(Idx, MVT::i32));
2954 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2955 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2956 DAG.getConstant(Idx+1, MVT::i32));
2957 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2958 }
2959 }
2960
2961 return SDOperand();
2962}
2963
2964SDOperand
2965X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2966 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2967 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2968}
2969
2970// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2971// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2972// one of the above mentioned nodes. It has to be wrapped because otherwise
2973// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2974// be used to form addressing mode. These wrapped nodes will be selected
2975// into MOV32ri.
2976SDOperand
2977X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2978 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2979 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2980 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2981 CP->getAlignment()));
2982 if (Subtarget->isTargetDarwin()) {
2983 // With PIC, the address is actually $g + Offset.
2984 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2985 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2986 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2987 }
2988
2989 return Result;
2990}
2991
2992SDOperand
2993X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2994 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2995 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00002996 DAG.getTargetGlobalAddress(GV,
2997 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002998 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(),
Chris Lattner3d826992006-05-16 06:45:34 +00003002 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3003 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003004
3005 // For Darwin, external and weak symbols are indirect, so we want to load
3006 // the value at address GV, not the value of GV itself. This means that
3007 // the GlobalAddress must be in the base or index register of the address,
3008 // not the GV offset field.
3009 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
3010 DarwinGVRequiresExtraLoad(GV))
3011 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
3012 Result, DAG.getSrcValue(NULL));
3013 }
3014
3015 return Result;
3016}
3017
3018SDOperand
3019X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3020 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3021 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003022 DAG.getTargetExternalSymbol(Sym,
3023 getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003024 if (Subtarget->isTargetDarwin()) {
3025 // With PIC, the address is actually $g + Offset.
3026 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3027 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003028 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3029 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003030 }
3031
3032 return Result;
3033}
3034
3035SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00003036 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3037 "Not an i64 shift!");
3038 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3039 SDOperand ShOpLo = Op.getOperand(0);
3040 SDOperand ShOpHi = Op.getOperand(1);
3041 SDOperand ShAmt = Op.getOperand(2);
3042 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00003043 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00003044 : DAG.getConstant(0, MVT::i32);
3045
3046 SDOperand Tmp2, Tmp3;
3047 if (Op.getOpcode() == ISD::SHL_PARTS) {
3048 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3049 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3050 } else {
3051 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00003052 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00003053 }
3054
3055 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
3056 ShAmt, DAG.getConstant(32, MVT::i8));
3057
3058 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00003059 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00003060
3061 std::vector<MVT::ValueType> Tys;
3062 Tys.push_back(MVT::i32);
3063 Tys.push_back(MVT::Flag);
3064 std::vector<SDOperand> Ops;
3065 if (Op.getOpcode() == ISD::SHL_PARTS) {
3066 Ops.push_back(Tmp2);
3067 Ops.push_back(Tmp3);
3068 Ops.push_back(CC);
3069 Ops.push_back(InFlag);
3070 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3071 InFlag = Hi.getValue(1);
3072
3073 Ops.clear();
3074 Ops.push_back(Tmp3);
3075 Ops.push_back(Tmp1);
3076 Ops.push_back(CC);
3077 Ops.push_back(InFlag);
3078 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3079 } else {
3080 Ops.push_back(Tmp2);
3081 Ops.push_back(Tmp3);
3082 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00003083 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00003084 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3085 InFlag = Lo.getValue(1);
3086
3087 Ops.clear();
3088 Ops.push_back(Tmp3);
3089 Ops.push_back(Tmp1);
3090 Ops.push_back(CC);
3091 Ops.push_back(InFlag);
3092 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
3093 }
3094
3095 Tys.clear();
3096 Tys.push_back(MVT::i32);
3097 Tys.push_back(MVT::i32);
3098 Ops.clear();
3099 Ops.push_back(Lo);
3100 Ops.push_back(Hi);
3101 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Chenga9467aa2006-04-25 20:13:52 +00003102}
Evan Cheng6305e502006-01-12 22:54:21 +00003103
Evan Chenga9467aa2006-04-25 20:13:52 +00003104SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3105 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3106 Op.getOperand(0).getValueType() >= MVT::i16 &&
3107 "Unknown SINT_TO_FP to lower!");
3108
3109 SDOperand Result;
3110 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3111 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3112 MachineFunction &MF = DAG.getMachineFunction();
3113 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3114 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3115 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
3116 DAG.getEntryNode(), Op.getOperand(0),
3117 StackSlot, DAG.getSrcValue(NULL));
3118
3119 // Build the FILD
3120 std::vector<MVT::ValueType> Tys;
3121 Tys.push_back(MVT::f64);
3122 Tys.push_back(MVT::Other);
3123 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3124 std::vector<SDOperand> Ops;
3125 Ops.push_back(Chain);
3126 Ops.push_back(StackSlot);
3127 Ops.push_back(DAG.getValueType(SrcVT));
3128 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3129 Tys, Ops);
3130
3131 if (X86ScalarSSE) {
3132 Chain = Result.getValue(1);
3133 SDOperand InFlag = Result.getValue(2);
3134
3135 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3136 // shouldn't be necessary except that RFP cannot be live across
3137 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003138 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003139 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003140 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00003141 std::vector<MVT::ValueType> Tys;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003142 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003143 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003144 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003145 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003146 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003147 Ops.push_back(DAG.getValueType(Op.getValueType()));
3148 Ops.push_back(InFlag);
3149 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3150 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3151 DAG.getSrcValue(NULL));
Chris Lattner76ac0682005-11-15 00:40:23 +00003152 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003153
Evan Chenga9467aa2006-04-25 20:13:52 +00003154 return Result;
3155}
3156
3157SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3158 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3159 "Unknown FP_TO_SINT to lower!");
3160 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3161 // stack slot.
3162 MachineFunction &MF = DAG.getMachineFunction();
3163 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3164 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3165 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3166
3167 unsigned Opc;
3168 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003169 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3170 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3171 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3172 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003173 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003174
Evan Chenga9467aa2006-04-25 20:13:52 +00003175 SDOperand Chain = DAG.getEntryNode();
3176 SDOperand Value = Op.getOperand(0);
3177 if (X86ScalarSSE) {
3178 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3179 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3180 DAG.getSrcValue(0));
3181 std::vector<MVT::ValueType> Tys;
3182 Tys.push_back(MVT::f64);
3183 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00003184 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00003185 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00003186 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003187 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3188 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3189 Chain = Value.getValue(1);
3190 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3191 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3192 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003193
Evan Chenga9467aa2006-04-25 20:13:52 +00003194 // Build the FP_TO_INT*_IN_MEM
3195 std::vector<SDOperand> Ops;
3196 Ops.push_back(Chain);
3197 Ops.push_back(Value);
3198 Ops.push_back(StackSlot);
3199 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Cheng172fce72006-01-06 00:43:03 +00003200
Evan Chenga9467aa2006-04-25 20:13:52 +00003201 // Load the result.
3202 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3203 DAG.getSrcValue(NULL));
3204}
3205
3206SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3207 MVT::ValueType VT = Op.getValueType();
3208 const Type *OpNTy = MVT::getTypeForValueType(VT);
3209 std::vector<Constant*> CV;
3210 if (VT == MVT::f64) {
3211 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3212 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3213 } else {
3214 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3215 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3216 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3217 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3218 }
3219 Constant *CS = ConstantStruct::get(CV);
3220 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3221 SDOperand Mask
3222 = DAG.getNode(X86ISD::LOAD_PACK,
3223 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3224 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3225}
3226
3227SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3228 MVT::ValueType VT = Op.getValueType();
3229 const Type *OpNTy = MVT::getTypeForValueType(VT);
3230 std::vector<Constant*> CV;
3231 if (VT == MVT::f64) {
3232 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3233 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3234 } else {
3235 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3236 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3237 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3238 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3239 }
3240 Constant *CS = ConstantStruct::get(CV);
3241 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3242 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3243 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3244 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3245}
3246
3247SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3248 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3249 SDOperand Cond;
3250 SDOperand CC = Op.getOperand(2);
3251 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3252 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3253 bool Flip;
3254 unsigned X86CC;
3255 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3256 if (Flip)
3257 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3258 Op.getOperand(1), Op.getOperand(0));
3259 else
Evan Cheng45df7f82006-01-30 23:41:35 +00003260 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3261 Op.getOperand(0), Op.getOperand(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003262 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3263 DAG.getConstant(X86CC, MVT::i8), Cond);
3264 } else {
3265 assert(isFP && "Illegal integer SetCC!");
3266
3267 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3268 Op.getOperand(0), Op.getOperand(1));
3269 std::vector<MVT::ValueType> Tys;
3270 std::vector<SDOperand> Ops;
3271 switch (SetCCOpcode) {
Evan Cheng172fce72006-01-06 00:43:03 +00003272 default: assert(false && "Illegal floating point SetCC!");
3273 case ISD::SETOEQ: { // !PF & ZF
3274 Tys.push_back(MVT::i8);
3275 Tys.push_back(MVT::Flag);
3276 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3277 Ops.push_back(Cond);
3278 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3279 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3280 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3281 Tmp1.getValue(1));
3282 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3283 }
Evan Cheng172fce72006-01-06 00:43:03 +00003284 case ISD::SETUNE: { // PF | !ZF
3285 Tys.push_back(MVT::i8);
3286 Tys.push_back(MVT::Flag);
3287 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3288 Ops.push_back(Cond);
3289 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3290 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3291 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3292 Tmp1.getValue(1));
3293 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3294 }
Evan Cheng172fce72006-01-06 00:43:03 +00003295 }
Evan Chengc1583db2005-12-21 20:21:51 +00003296 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003297}
Evan Cheng45df7f82006-01-30 23:41:35 +00003298
Evan Chenga9467aa2006-04-25 20:13:52 +00003299SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3300 MVT::ValueType VT = Op.getValueType();
3301 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3302 bool addTest = false;
3303 SDOperand Op0 = Op.getOperand(0);
3304 SDOperand Cond, CC;
3305 if (Op0.getOpcode() == ISD::SETCC)
3306 Op0 = LowerOperation(Op0, DAG);
Evan Cheng944d1e92006-01-26 02:13:10 +00003307
Evan Chenga9467aa2006-04-25 20:13:52 +00003308 if (Op0.getOpcode() == X86ISD::SETCC) {
3309 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3310 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3311 // have another use it will be eliminated.
3312 // If the X86ISD::SETCC has more than one use, then it's probably better
3313 // to use a test instead of duplicating the X86ISD::CMP (for register
3314 // pressure reason).
3315 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3316 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3317 CmpOpc == X86ISD::UCOMI) {
3318 if (!Op0.hasOneUse()) {
3319 std::vector<MVT::ValueType> Tys;
3320 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3321 Tys.push_back(Op0.Val->getValueType(i));
3322 std::vector<SDOperand> Ops;
3323 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3324 Ops.push_back(Op0.getOperand(i));
3325 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3326 }
3327
3328 CC = Op0.getOperand(0);
3329 Cond = Op0.getOperand(1);
3330 // Make a copy as flag result cannot be used by more than one.
3331 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3332 Cond.getOperand(0), Cond.getOperand(1));
3333 addTest =
3334 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00003335 } else
3336 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003337 } else
3338 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00003339
Evan Chenga9467aa2006-04-25 20:13:52 +00003340 if (addTest) {
3341 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3342 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00003343 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003344
Evan Chenga9467aa2006-04-25 20:13:52 +00003345 std::vector<MVT::ValueType> Tys;
3346 Tys.push_back(Op.getValueType());
3347 Tys.push_back(MVT::Flag);
3348 std::vector<SDOperand> Ops;
3349 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3350 // condition is true.
3351 Ops.push_back(Op.getOperand(2));
3352 Ops.push_back(Op.getOperand(1));
3353 Ops.push_back(CC);
3354 Ops.push_back(Cond);
3355 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3356}
Evan Cheng944d1e92006-01-26 02:13:10 +00003357
Evan Chenga9467aa2006-04-25 20:13:52 +00003358SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3359 bool addTest = false;
3360 SDOperand Cond = Op.getOperand(1);
3361 SDOperand Dest = Op.getOperand(2);
3362 SDOperand CC;
3363 if (Cond.getOpcode() == ISD::SETCC)
3364 Cond = LowerOperation(Cond, DAG);
3365
3366 if (Cond.getOpcode() == X86ISD::SETCC) {
3367 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3368 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3369 // have another use it will be eliminated.
3370 // If the X86ISD::SETCC has more than one use, then it's probably better
3371 // to use a test instead of duplicating the X86ISD::CMP (for register
3372 // pressure reason).
3373 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3374 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3375 CmpOpc == X86ISD::UCOMI) {
3376 if (!Cond.hasOneUse()) {
3377 std::vector<MVT::ValueType> Tys;
3378 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3379 Tys.push_back(Cond.Val->getValueType(i));
3380 std::vector<SDOperand> Ops;
3381 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3382 Ops.push_back(Cond.getOperand(i));
3383 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3384 }
3385
3386 CC = Cond.getOperand(0);
3387 Cond = Cond.getOperand(1);
3388 // Make a copy as flag result cannot be used by more than one.
3389 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3390 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00003391 } else
3392 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003393 } else
3394 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00003395
Evan Chenga9467aa2006-04-25 20:13:52 +00003396 if (addTest) {
3397 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3398 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng6fc31042005-12-19 23:12:38 +00003399 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003400 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3401 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3402}
Evan Chengae986f12006-01-11 22:15:48 +00003403
Evan Chenga9467aa2006-04-25 20:13:52 +00003404SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3405 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3406 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3407 DAG.getTargetJumpTable(JT->getIndex(),
3408 getPointerTy()));
3409 if (Subtarget->isTargetDarwin()) {
3410 // With PIC, the address is actually $g + Offset.
3411 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3412 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner3d826992006-05-16 06:45:34 +00003413 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3414 Result);
Evan Chengae986f12006-01-11 22:15:48 +00003415 }
Evan Cheng99470012006-02-25 09:55:19 +00003416
Evan Chenga9467aa2006-04-25 20:13:52 +00003417 return Result;
3418}
Evan Cheng5588de92006-02-18 00:15:05 +00003419
Evan Chenga9467aa2006-04-25 20:13:52 +00003420SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3421 SDOperand Copy;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003422
Evan Chenga9467aa2006-04-25 20:13:52 +00003423 switch(Op.getNumOperands()) {
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003424 default:
3425 assert(0 && "Do not know how to return this many arguments!");
3426 abort();
Chris Lattnerc070c622006-04-17 20:32:50 +00003427 case 1: // ret void.
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003428 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chenga9467aa2006-04-25 20:13:52 +00003429 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003430 case 2: {
3431 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerc070c622006-04-17 20:32:50 +00003432
3433 if (MVT::isVector(ArgVT)) {
3434 // Integer or FP vector result -> XMM0.
3435 if (DAG.getMachineFunction().liveout_empty())
3436 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3437 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3438 SDOperand());
3439 } else if (MVT::isInteger(ArgVT)) {
3440 // Integer result -> EAX
3441 if (DAG.getMachineFunction().liveout_empty())
3442 DAG.getMachineFunction().addLiveOut(X86::EAX);
3443
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003444 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3445 SDOperand());
Chris Lattnerc070c622006-04-17 20:32:50 +00003446 } else if (!X86ScalarSSE) {
3447 // FP return with fp-stack value.
3448 if (DAG.getMachineFunction().liveout_empty())
3449 DAG.getMachineFunction().addLiveOut(X86::ST0);
3450
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003451 std::vector<MVT::ValueType> Tys;
3452 Tys.push_back(MVT::Other);
3453 Tys.push_back(MVT::Flag);
3454 std::vector<SDOperand> Ops;
3455 Ops.push_back(Op.getOperand(0));
3456 Ops.push_back(Op.getOperand(1));
3457 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3458 } else {
Chris Lattnerc070c622006-04-17 20:32:50 +00003459 // FP return with ScalarSSE (return on fp-stack).
3460 if (DAG.getMachineFunction().liveout_empty())
3461 DAG.getMachineFunction().addLiveOut(X86::ST0);
3462
Evan Chenge1ce4d72006-02-01 00:20:21 +00003463 SDOperand MemLoc;
3464 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00003465 SDOperand Value = Op.getOperand(1);
3466
Evan Chenga24617f2006-02-01 01:19:32 +00003467 if (Value.getOpcode() == ISD::LOAD &&
3468 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00003469 Chain = Value.getOperand(0);
3470 MemLoc = Value.getOperand(1);
3471 } else {
3472 // Spill the value to memory and reload it into top of stack.
3473 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3474 MachineFunction &MF = DAG.getMachineFunction();
3475 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3476 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3477 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3478 Value, MemLoc, DAG.getSrcValue(0));
3479 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003480 std::vector<MVT::ValueType> Tys;
3481 Tys.push_back(MVT::f64);
3482 Tys.push_back(MVT::Other);
3483 std::vector<SDOperand> Ops;
3484 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00003485 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003486 Ops.push_back(DAG.getValueType(ArgVT));
3487 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3488 Tys.clear();
3489 Tys.push_back(MVT::Other);
3490 Tys.push_back(MVT::Flag);
3491 Ops.clear();
3492 Ops.push_back(Copy.getValue(1));
3493 Ops.push_back(Copy);
3494 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3495 }
3496 break;
3497 }
3498 case 3:
Chris Lattnerc070c622006-04-17 20:32:50 +00003499 if (DAG.getMachineFunction().liveout_empty()) {
3500 DAG.getMachineFunction().addLiveOut(X86::EAX);
3501 DAG.getMachineFunction().addLiveOut(X86::EDX);
3502 }
3503
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003504 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3505 SDOperand());
3506 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3507 break;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003508 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003509 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3510 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3511 Copy.getValue(1));
3512}
3513
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003514SDOperand
3515X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3516 if (FormalArgs.size() == 0) {
Chris Lattner3d826992006-05-16 06:45:34 +00003517 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003518 if (CC == CallingConv::Fast && EnableFastCC)
3519 LowerFastCCArguments(Op, DAG);
3520 else
3521 LowerCCCArguments(Op, DAG);
3522 }
Chris Lattnerc7df70d2006-05-16 17:14:26 +00003523
3524 // Return the new list of results.
3525 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
3526 Op.Val->value_end());
3527 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, FormalArgs);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003528}
3529
Evan Chenga9467aa2006-04-25 20:13:52 +00003530SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3531 SDOperand InFlag(0, 0);
3532 SDOperand Chain = Op.getOperand(0);
3533 unsigned Align =
3534 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3535 if (Align == 0) Align = 1;
3536
3537 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3538 // If not DWORD aligned, call memset if size is less than the threshold.
3539 // It knows how to align to the right boundary first.
3540 if ((Align & 3) != 0 ||
3541 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3542 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003543 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003544 std::vector<std::pair<SDOperand, const Type*> > Args;
3545 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3546 // Extend the ubyte argument to be an int value for the call.
3547 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3548 Args.push_back(std::make_pair(Val, IntPtrTy));
3549 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3550 std::pair<SDOperand,SDOperand> CallResult =
3551 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3552 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3553 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003554 }
Evan Chengd097e672006-03-22 02:53:00 +00003555
Evan Chenga9467aa2006-04-25 20:13:52 +00003556 MVT::ValueType AVT;
3557 SDOperand Count;
3558 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3559 unsigned BytesLeft = 0;
3560 bool TwoRepStos = false;
3561 if (ValC) {
3562 unsigned ValReg;
3563 unsigned Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003564
Evan Chenga9467aa2006-04-25 20:13:52 +00003565 // If the value is a constant, then we can potentially use larger sets.
3566 switch (Align & 3) {
3567 case 2: // WORD aligned
3568 AVT = MVT::i16;
3569 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3570 BytesLeft = I->getValue() % 2;
3571 Val = (Val << 8) | Val;
3572 ValReg = X86::AX;
3573 break;
3574 case 0: // DWORD aligned
3575 AVT = MVT::i32;
3576 if (I) {
3577 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3578 BytesLeft = I->getValue() % 4;
Evan Chenga3caaee2006-04-19 22:48:17 +00003579 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003580 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3581 DAG.getConstant(2, MVT::i8));
3582 TwoRepStos = true;
Evan Chenga3caaee2006-04-19 22:48:17 +00003583 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003584 Val = (Val << 8) | Val;
3585 Val = (Val << 16) | Val;
3586 ValReg = X86::EAX;
3587 break;
3588 default: // Byte aligned
3589 AVT = MVT::i8;
3590 Count = Op.getOperand(3);
3591 ValReg = X86::AL;
3592 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003593 }
3594
Evan Chenga9467aa2006-04-25 20:13:52 +00003595 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3596 InFlag);
3597 InFlag = Chain.getValue(1);
3598 } else {
3599 AVT = MVT::i8;
3600 Count = Op.getOperand(3);
3601 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3602 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003603 }
Evan Chengb0461082006-04-24 18:01:45 +00003604
Evan Chenga9467aa2006-04-25 20:13:52 +00003605 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3606 InFlag = Chain.getValue(1);
3607 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3608 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003609
Evan Chenga9467aa2006-04-25 20:13:52 +00003610 std::vector<MVT::ValueType> Tys;
3611 Tys.push_back(MVT::Other);
3612 Tys.push_back(MVT::Flag);
3613 std::vector<SDOperand> Ops;
3614 Ops.push_back(Chain);
3615 Ops.push_back(DAG.getValueType(AVT));
3616 Ops.push_back(InFlag);
3617 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengb0461082006-04-24 18:01:45 +00003618
Evan Chenga9467aa2006-04-25 20:13:52 +00003619 if (TwoRepStos) {
3620 InFlag = Chain.getValue(1);
3621 Count = Op.getOperand(3);
3622 MVT::ValueType CVT = Count.getValueType();
3623 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3624 DAG.getConstant(3, CVT));
3625 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3626 InFlag = Chain.getValue(1);
3627 Tys.clear();
3628 Tys.push_back(MVT::Other);
3629 Tys.push_back(MVT::Flag);
3630 Ops.clear();
3631 Ops.push_back(Chain);
3632 Ops.push_back(DAG.getValueType(MVT::i8));
3633 Ops.push_back(InFlag);
3634 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3635 } else if (BytesLeft) {
3636 // Issue stores for the last 1 - 3 bytes.
3637 SDOperand Value;
3638 unsigned Val = ValC->getValue() & 255;
3639 unsigned Offset = I->getValue() - BytesLeft;
3640 SDOperand DstAddr = Op.getOperand(1);
3641 MVT::ValueType AddrVT = DstAddr.getValueType();
3642 if (BytesLeft >= 2) {
3643 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3644 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3645 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3646 DAG.getConstant(Offset, AddrVT)),
3647 DAG.getSrcValue(NULL));
3648 BytesLeft -= 2;
3649 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003650 }
3651
Evan Chenga9467aa2006-04-25 20:13:52 +00003652 if (BytesLeft == 1) {
3653 Value = DAG.getConstant(Val, MVT::i8);
3654 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3655 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3656 DAG.getConstant(Offset, AddrVT)),
3657 DAG.getSrcValue(NULL));
Evan Cheng14215c32006-04-21 23:03:30 +00003658 }
Evan Cheng082c8782006-03-24 07:29:27 +00003659 }
Evan Chengebf10062006-04-03 20:53:28 +00003660
Evan Chenga9467aa2006-04-25 20:13:52 +00003661 return Chain;
3662}
Evan Chengebf10062006-04-03 20:53:28 +00003663
Evan Chenga9467aa2006-04-25 20:13:52 +00003664SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3665 SDOperand Chain = Op.getOperand(0);
3666 unsigned Align =
3667 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3668 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003669
Evan Chenga9467aa2006-04-25 20:13:52 +00003670 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3671 // If not DWORD aligned, call memcpy if size is less than the threshold.
3672 // It knows how to align to the right boundary first.
3673 if ((Align & 3) != 0 ||
3674 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3675 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003676 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Chenga9467aa2006-04-25 20:13:52 +00003677 std::vector<std::pair<SDOperand, const Type*> > Args;
3678 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3679 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3680 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3681 std::pair<SDOperand,SDOperand> CallResult =
3682 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3683 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3684 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003685 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003686
3687 MVT::ValueType AVT;
3688 SDOperand Count;
3689 unsigned BytesLeft = 0;
3690 bool TwoRepMovs = false;
3691 switch (Align & 3) {
3692 case 2: // WORD aligned
3693 AVT = MVT::i16;
3694 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3695 BytesLeft = I->getValue() % 2;
3696 break;
3697 case 0: // DWORD aligned
3698 AVT = MVT::i32;
3699 if (I) {
3700 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3701 BytesLeft = I->getValue() % 4;
Evan Cheng54212062006-04-17 22:45:49 +00003702 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003703 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3704 DAG.getConstant(2, MVT::i8));
3705 TwoRepMovs = true;
Evan Cheng6e5e2052006-04-17 22:04:06 +00003706 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003707 break;
3708 default: // Byte aligned
3709 AVT = MVT::i8;
3710 Count = Op.getOperand(3);
3711 break;
3712 }
3713
3714 SDOperand InFlag(0, 0);
3715 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3716 InFlag = Chain.getValue(1);
3717 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3718 InFlag = Chain.getValue(1);
3719 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3720 InFlag = Chain.getValue(1);
3721
3722 std::vector<MVT::ValueType> Tys;
3723 Tys.push_back(MVT::Other);
3724 Tys.push_back(MVT::Flag);
3725 std::vector<SDOperand> Ops;
3726 Ops.push_back(Chain);
3727 Ops.push_back(DAG.getValueType(AVT));
3728 Ops.push_back(InFlag);
3729 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3730
3731 if (TwoRepMovs) {
3732 InFlag = Chain.getValue(1);
3733 Count = Op.getOperand(3);
3734 MVT::ValueType CVT = Count.getValueType();
3735 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3736 DAG.getConstant(3, CVT));
3737 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3738 InFlag = Chain.getValue(1);
3739 Tys.clear();
3740 Tys.push_back(MVT::Other);
3741 Tys.push_back(MVT::Flag);
3742 Ops.clear();
3743 Ops.push_back(Chain);
3744 Ops.push_back(DAG.getValueType(MVT::i8));
3745 Ops.push_back(InFlag);
3746 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3747 } else if (BytesLeft) {
3748 // Issue loads and stores for the last 1 - 3 bytes.
3749 unsigned Offset = I->getValue() - BytesLeft;
3750 SDOperand DstAddr = Op.getOperand(1);
3751 MVT::ValueType DstVT = DstAddr.getValueType();
3752 SDOperand SrcAddr = Op.getOperand(2);
3753 MVT::ValueType SrcVT = SrcAddr.getValueType();
3754 SDOperand Value;
3755 if (BytesLeft >= 2) {
3756 Value = DAG.getLoad(MVT::i16, Chain,
3757 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3758 DAG.getConstant(Offset, SrcVT)),
3759 DAG.getSrcValue(NULL));
3760 Chain = Value.getValue(1);
3761 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3762 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3763 DAG.getConstant(Offset, DstVT)),
3764 DAG.getSrcValue(NULL));
3765 BytesLeft -= 2;
3766 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003767 }
3768
Evan Chenga9467aa2006-04-25 20:13:52 +00003769 if (BytesLeft == 1) {
3770 Value = DAG.getLoad(MVT::i8, Chain,
3771 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3772 DAG.getConstant(Offset, SrcVT)),
3773 DAG.getSrcValue(NULL));
3774 Chain = Value.getValue(1);
3775 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3776 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3777 DAG.getConstant(Offset, DstVT)),
3778 DAG.getSrcValue(NULL));
3779 }
Evan Chengcbffa462006-03-31 19:22:53 +00003780 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003781
3782 return Chain;
3783}
3784
3785SDOperand
3786X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3787 std::vector<MVT::ValueType> Tys;
3788 Tys.push_back(MVT::Other);
3789 Tys.push_back(MVT::Flag);
3790 std::vector<SDOperand> Ops;
3791 Ops.push_back(Op.getOperand(0));
3792 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3793 Ops.clear();
3794 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3795 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3796 MVT::i32, Ops[0].getValue(2)));
3797 Ops.push_back(Ops[1].getValue(1));
3798 Tys[0] = Tys[1] = MVT::i32;
3799 Tys.push_back(MVT::Other);
3800 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3801}
3802
3803SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3804 // vastart just stores the address of the VarArgsFrameIndex slot into the
3805 // memory location argument.
3806 // FIXME: Replace MVT::i32 with PointerTy
3807 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3808 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3809 Op.getOperand(1), Op.getOperand(2));
3810}
3811
3812SDOperand
3813X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3814 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3815 switch (IntNo) {
3816 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003817 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003818 case Intrinsic::x86_sse_comieq_ss:
3819 case Intrinsic::x86_sse_comilt_ss:
3820 case Intrinsic::x86_sse_comile_ss:
3821 case Intrinsic::x86_sse_comigt_ss:
3822 case Intrinsic::x86_sse_comige_ss:
3823 case Intrinsic::x86_sse_comineq_ss:
3824 case Intrinsic::x86_sse_ucomieq_ss:
3825 case Intrinsic::x86_sse_ucomilt_ss:
3826 case Intrinsic::x86_sse_ucomile_ss:
3827 case Intrinsic::x86_sse_ucomigt_ss:
3828 case Intrinsic::x86_sse_ucomige_ss:
3829 case Intrinsic::x86_sse_ucomineq_ss:
3830 case Intrinsic::x86_sse2_comieq_sd:
3831 case Intrinsic::x86_sse2_comilt_sd:
3832 case Intrinsic::x86_sse2_comile_sd:
3833 case Intrinsic::x86_sse2_comigt_sd:
3834 case Intrinsic::x86_sse2_comige_sd:
3835 case Intrinsic::x86_sse2_comineq_sd:
3836 case Intrinsic::x86_sse2_ucomieq_sd:
3837 case Intrinsic::x86_sse2_ucomilt_sd:
3838 case Intrinsic::x86_sse2_ucomile_sd:
3839 case Intrinsic::x86_sse2_ucomigt_sd:
3840 case Intrinsic::x86_sse2_ucomige_sd:
3841 case Intrinsic::x86_sse2_ucomineq_sd: {
3842 unsigned Opc = 0;
3843 ISD::CondCode CC = ISD::SETCC_INVALID;
3844 switch (IntNo) {
3845 default: break;
3846 case Intrinsic::x86_sse_comieq_ss:
3847 case Intrinsic::x86_sse2_comieq_sd:
3848 Opc = X86ISD::COMI;
3849 CC = ISD::SETEQ;
3850 break;
Evan Cheng78038292006-04-05 23:38:46 +00003851 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003852 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003853 Opc = X86ISD::COMI;
3854 CC = ISD::SETLT;
3855 break;
3856 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003857 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003858 Opc = X86ISD::COMI;
3859 CC = ISD::SETLE;
3860 break;
3861 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003862 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003863 Opc = X86ISD::COMI;
3864 CC = ISD::SETGT;
3865 break;
3866 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003867 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003868 Opc = X86ISD::COMI;
3869 CC = ISD::SETGE;
3870 break;
3871 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003872 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003873 Opc = X86ISD::COMI;
3874 CC = ISD::SETNE;
3875 break;
3876 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003877 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003878 Opc = X86ISD::UCOMI;
3879 CC = ISD::SETEQ;
3880 break;
3881 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003882 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003883 Opc = X86ISD::UCOMI;
3884 CC = ISD::SETLT;
3885 break;
3886 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003887 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003888 Opc = X86ISD::UCOMI;
3889 CC = ISD::SETLE;
3890 break;
3891 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003892 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003893 Opc = X86ISD::UCOMI;
3894 CC = ISD::SETGT;
3895 break;
3896 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003897 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003898 Opc = X86ISD::UCOMI;
3899 CC = ISD::SETGE;
3900 break;
3901 case Intrinsic::x86_sse_ucomineq_ss:
3902 case Intrinsic::x86_sse2_ucomineq_sd:
3903 Opc = X86ISD::UCOMI;
3904 CC = ISD::SETNE;
3905 break;
Evan Cheng78038292006-04-05 23:38:46 +00003906 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003907 bool Flip;
3908 unsigned X86CC;
3909 translateX86CC(CC, true, X86CC, Flip);
3910 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3911 Op.getOperand(Flip?1:2));
3912 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3913 DAG.getConstant(X86CC, MVT::i8), Cond);
3914 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003915 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003916 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003917}
Evan Cheng6af02632005-12-20 06:22:03 +00003918
Evan Chenga9467aa2006-04-25 20:13:52 +00003919/// LowerOperation - Provide custom lowering hooks for some operations.
3920///
3921SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3922 switch (Op.getOpcode()) {
3923 default: assert(0 && "Should not custom lower this!");
3924 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3925 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3926 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3927 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3928 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3929 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3930 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3931 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3932 case ISD::SHL_PARTS:
3933 case ISD::SRA_PARTS:
3934 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3935 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3936 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3937 case ISD::FABS: return LowerFABS(Op, DAG);
3938 case ISD::FNEG: return LowerFNEG(Op, DAG);
3939 case ISD::SETCC: return LowerSETCC(Op, DAG);
3940 case ISD::SELECT: return LowerSELECT(Op, DAG);
3941 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3942 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3943 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003944 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003945 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3946 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3947 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3948 case ISD::VASTART: return LowerVASTART(Op, DAG);
3949 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3950 }
3951}
3952
Evan Cheng6af02632005-12-20 06:22:03 +00003953const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3954 switch (Opcode) {
3955 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003956 case X86ISD::SHLD: return "X86ISD::SHLD";
3957 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003958 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003959 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003960 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003961 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003962 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3963 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3964 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003965 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003966 case X86ISD::FST: return "X86ISD::FST";
3967 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003968 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003969 case X86ISD::CALL: return "X86ISD::CALL";
3970 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3971 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3972 case X86ISD::CMP: return "X86ISD::CMP";
3973 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003974 case X86ISD::COMI: return "X86ISD::COMI";
3975 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003976 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003977 case X86ISD::CMOV: return "X86ISD::CMOV";
3978 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003979 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003980 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3981 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003982 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003983 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00003984 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00003985 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00003986 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00003987 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00003988 }
3989}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003990
Nate Begeman8a77efe2006-02-16 21:11:51 +00003991void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3992 uint64_t Mask,
3993 uint64_t &KnownZero,
3994 uint64_t &KnownOne,
3995 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003996 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00003997 assert((Opc >= ISD::BUILTIN_OP_END ||
3998 Opc == ISD::INTRINSIC_WO_CHAIN ||
3999 Opc == ISD::INTRINSIC_W_CHAIN ||
4000 Opc == ISD::INTRINSIC_VOID) &&
4001 "Should use MaskedValueIsZero if you don't know whether Op"
4002 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004003
Evan Cheng6d196db2006-04-05 06:11:20 +00004004 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004005 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00004006 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00004007 case X86ISD::SETCC:
4008 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4009 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004010 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004011}
Chris Lattnerc642aa52006-01-31 19:43:35 +00004012
4013std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00004014getRegClassForInlineAsmConstraint(const std::string &Constraint,
4015 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00004016 if (Constraint.size() == 1) {
4017 // FIXME: not handling fp-stack yet!
4018 // FIXME: not handling MMX registers yet ('y' constraint).
4019 switch (Constraint[0]) { // GCC X86 Constraint Letters
4020 default: break; // Unknown constriant letter
4021 case 'r': // GENERAL_REGS
4022 case 'R': // LEGACY_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004023 if (VT == MVT::i32)
4024 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4025 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4026 else if (VT == MVT::i16)
4027 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4028 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4029 else if (VT == MVT::i8)
4030 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4031 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004032 case 'l': // INDEX_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004033 if (VT == MVT::i32)
4034 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4035 X86::ESI, X86::EDI, X86::EBP, 0);
4036 else if (VT == MVT::i16)
4037 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4038 X86::SI, X86::DI, X86::BP, 0);
4039 else if (VT == MVT::i8)
4040 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4041 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004042 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4043 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004044 if (VT == MVT::i32)
4045 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4046 else if (VT == MVT::i16)
4047 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4048 else if (VT == MVT::i8)
4049 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4050 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004051 case 'x': // SSE_REGS if SSE1 allowed
4052 if (Subtarget->hasSSE1())
4053 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4054 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4055 0);
4056 return std::vector<unsigned>();
4057 case 'Y': // SSE_REGS if SSE2 allowed
4058 if (Subtarget->hasSSE2())
4059 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4060 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4061 0);
4062 return std::vector<unsigned>();
4063 }
4064 }
4065
Chris Lattner7ad77df2006-02-22 00:56:39 +00004066 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00004067}
Evan Chengaf598d22006-03-13 23:18:16 +00004068
4069/// isLegalAddressImmediate - Return true if the integer value or
4070/// GlobalValue can be used as the offset of the target addressing mode.
4071bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4072 // X86 allows a sign-extended 32-bit immediate field.
4073 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4074}
4075
4076bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00004077 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00004078 Reloc::Model RModel = getTargetMachine().getRelocationModel();
4079 if (RModel == Reloc::Static)
4080 return true;
4081 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00004082 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00004083 else
4084 return false;
4085 } else
4086 return true;
4087}
Evan Cheng68ad48b2006-03-22 18:59:22 +00004088
4089/// isShuffleMaskLegal - Targets can use this to indicate that they only
4090/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4091/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4092/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00004093bool
4094X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4095 // Only do shuffles on 128-bit vector types for now.
4096 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Chenga3caaee2006-04-19 22:48:17 +00004097 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng5022b342006-04-17 20:43:08 +00004098 isSplatMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00004099 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00004100 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00004101 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00004102 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00004103}
Evan Cheng60f0b892006-04-20 08:58:49 +00004104
4105bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4106 MVT::ValueType EVT,
4107 SelectionDAG &DAG) const {
4108 unsigned NumElts = BVOps.size();
4109 // Only do shuffles on 128-bit vector types for now.
4110 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4111 if (NumElts == 2) return true;
4112 if (NumElts == 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00004113 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng60f0b892006-04-20 08:58:49 +00004114 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4115 }
4116 return false;
4117}