blob: 9658d0817415dbd7280d1cf6b4817e6e3df3d53f [file] [log] [blame]
Chris Lattnerc6d05672006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000023#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000024#include "llvm/ADT/VectorExtras.h"
25#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/SelectionDAG.h"
30#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000031#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-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 Cheng559806f2006-01-27 08:10:46 +000042 Subtarget = &TM.getSubtarget<X86Subtarget>();
43 X86ScalarSSE = Subtarget->hasSSE2();
44
Chris Lattnerdbdbf0c2005-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 Cheng0b2afbd2006-01-25 09:15:17 +000051 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000053 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000054
Evan Chenga88973f2006-03-22 19:22:18 +000055 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000056 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
57 setUseUnderscoreSetJmpLongJmp(true);
58
Evan Cheng714554d2006-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 Lattnera54aa942006-01-29 06:26:08 +000068
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000069 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000070 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
71 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
72 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Chris Lattnerdbdbf0c2005-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 Cheng6892f282006-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 Lattnerdbdbf0c2005-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 Begeman4c5dcf52006-02-17 00:03:04 +000090 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000091 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000092 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +000097
Evan Cheng6dab0532006-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 Cheng02568ff2006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000112 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-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 Cheng45af8fd2006-02-18 07:26:17 +0000121 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-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 Cheng45af8fd2006-02-18 07:26:17 +0000127 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
129
Evan Cheng02568ff2006-01-30 22:13:22 +0000130 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
131 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000132
Evan Cheng5298bcc2006-02-17 07:01:52 +0000133 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000134 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
135 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000136 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-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 Lenharthb873ff32005-11-20 21:41:10 +0000152 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000153 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000154
Chris Lattnerdbdbf0c2005-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 Begeman4c5dcf52006-02-17 00:03:04 +0000158
159 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-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 Begeman4c5dcf52006-02-17 00:03:04 +0000169 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000170 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000171 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000172 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000173 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000175 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000176 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-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 Begeman4c5dcf52006-02-17 00:03:04 +0000180 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000181 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
182 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000183
Chris Lattnerf73bae12005-11-29 06:16:21 +0000184 // We don't have line number support yet.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000187 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000188 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000189 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000190
Nate Begemanacc398c2006-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 Lattnere1125522006-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 Lattnerb99329e2006-01-13 02:42:53 +0000201
Chris Lattner9601a862006-03-05 05:08:37 +0000202 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
203 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
204
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205 if (X86ScalarSSE) {
206 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209
Evan Cheng223547a2006-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 Chengd25e9e82006-02-02 00:28:23 +0000218 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 setOperationAction(ISD::FSIN , MVT::f64, Expand);
220 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000224 setOperationAction(ISD::FREM , MVT::f32, Expand);
225
Chris Lattnera54aa942006-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 Lattnerdbdbf0c2005-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 Lattner44d9b9b2006-01-29 06:44:22 +0000234
235 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
236
Chris Lattnerdbdbf0c2005-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 Lattnera54aa942006-01-29 06:26:08 +0000242 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-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 Cheng470a6ad2006-02-22 02:26:30 +0000248
Evan Chengd30bf012006-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 Chengb067a1e2006-03-31 19:22:53 +0000257 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000258 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000259 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000260 }
261
Evan Chenga88973f2006-03-22 19:22:18 +0000262 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-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 Chengd30bf012006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-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 Cheng470a6ad2006-02-22 02:26:30 +0000271 }
272
Evan Chenga88973f2006-03-22 19:22:18 +0000273 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng2c3ae372006-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 Chengf7c378e2006-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 Cheng11e15b32006-04-03 20:53:28 +0000285 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000286 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000287 }
288
Evan Chenga88973f2006-03-22 19:22:18 +0000289 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-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 Chengf7c378e2006-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 Chengf9989842006-04-13 05:10:25 +0000304 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000305 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000306
Evan Chengf7c378e2006-04-10 07:23:14 +0000307 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000309 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-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 Chengf7c378e2006-04-10 07:23:14 +0000313
Evan Cheng2c3ae372006-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 Cheng91b740d2006-04-12 17:12:36 +0000335 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
336 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000337 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
338 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000339 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000340
341 // Custom lower v2i64 and v2f64 selects.
342 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000343 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000344 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000345 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000346 }
347
Evan Cheng6be2c582006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000351 computeRegisterProperties();
352
Evan Cheng87ed7162006-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 Chenga03a5dc2006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000358 allowUnalignedMemoryAccesses = true; // x86 supports it!
359}
360
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000361//===----------------------------------------------------------------------===//
362// C Calling Convention implementation
363//===----------------------------------------------------------------------===//
364
Evan Cheng85e38002006-04-27 05:35:28 +0000365/// AddLiveIn - This helper function adds the specified physical register to the
366/// MachineFunction as a live in value. It also creates a corresponding virtual
367/// register for it.
368static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
369 TargetRegisterClass *RC) {
370 assert(RC->contains(PReg) && "Not the correct regclass!");
371 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
372 MF.addLiveIn(PReg, VReg);
373 return VReg;
374}
375
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000376/// HowToPassCCCArgument - Returns how an formal argument of the specified type
377/// should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +0000378/// slot; if it is through XMM register, returns the number of XMM registers
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000379/// are needed.
380static void
381HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
382 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Chengcc1fc222006-05-25 23:31:23 +0000383 NumXMMRegs = 0;
384
Evan Chengeda65fa2006-04-27 01:32:22 +0000385 switch (ObjectVT) {
386 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000387 case MVT::i8: ObjSize = 1; break;
388 case MVT::i16: ObjSize = 2; break;
389 case MVT::i32: ObjSize = 4; break;
390 case MVT::i64: ObjSize = 8; break;
391 case MVT::f32: ObjSize = 4; break;
392 case MVT::f64: ObjSize = 8; break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000393 case MVT::v16i8:
394 case MVT::v8i16:
395 case MVT::v4i32:
396 case MVT::v2i64:
397 case MVT::v4f32:
398 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000399 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000400 ObjXMMRegs = 1;
401 else
402 ObjSize = 16;
403 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000404 }
Evan Chengeda65fa2006-04-27 01:32:22 +0000405}
406
Evan Cheng25caf632006-05-23 21:06:34 +0000407SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
408 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000409 MachineFunction &MF = DAG.getMachineFunction();
410 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000411 SDOperand Root = Op.getOperand(0);
412 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000413
Evan Chengeda65fa2006-04-27 01:32:22 +0000414 // Add DAG nodes to load the arguments... On entry to a function on the X86,
415 // the stack frame looks like this:
416 //
417 // [ESP] -- return address
418 // [ESP + 4] -- first argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000419 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000420 // ...
421 //
Evan Cheng1bc78042006-04-26 01:20:17 +0000422 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000423 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000424 static const unsigned XMMArgRegs[] = {
425 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
426 };
Evan Cheng1bc78042006-04-26 01:20:17 +0000427 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000428 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
429 unsigned ArgIncrement = 4;
430 unsigned ObjSize = 0;
431 unsigned ObjXMMRegs = 0;
432 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +0000433 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000434 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000435
Evan Cheng25caf632006-05-23 21:06:34 +0000436 SDOperand ArgValue;
437 if (ObjXMMRegs) {
438 // Passed in a XMM register.
439 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000440 X86::VR128RegisterClass);
Evan Cheng25caf632006-05-23 21:06:34 +0000441 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
442 ArgValues.push_back(ArgValue);
443 NumXMMRegs += ObjXMMRegs;
444 } else {
445 // Create the frame index object for this incoming parameter...
446 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
447 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
448 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
449 DAG.getSrcValue(NULL));
450 ArgValues.push_back(ArgValue);
451 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Cheng1bc78042006-04-26 01:20:17 +0000452 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000453 }
454
Evan Cheng25caf632006-05-23 21:06:34 +0000455 ArgValues.push_back(Root);
456
Evan Cheng1bc78042006-04-26 01:20:17 +0000457 // If the function takes variable number of arguments, make a frame index for
458 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000459 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
460 if (isVarArg)
Evan Cheng1bc78042006-04-26 01:20:17 +0000461 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
462 ReturnAddrIndex = 0; // No return address slot generated yet.
463 BytesToPopOnReturn = 0; // Callee pops nothing.
464 BytesCallerReserves = ArgOffset;
Evan Cheng25caf632006-05-23 21:06:34 +0000465
Chris Lattner2d297092006-05-23 18:50:38 +0000466 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
467 // pointer.
Evan Cheng25caf632006-05-23 21:06:34 +0000468 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner2d297092006-05-23 18:50:38 +0000469 Subtarget->isTargetDarwin())
470 BytesToPopOnReturn = 4;
Evan Cheng1bc78042006-04-26 01:20:17 +0000471
Evan Cheng25caf632006-05-23 21:06:34 +0000472 // Return the new list of results.
473 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
474 Op.Val->value_end());
475 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000476}
477
Evan Cheng32fe1032006-05-25 00:59:30 +0000478
479SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
480 SDOperand Chain = Op.getOperand(0);
481 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
482 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
483 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
484 SDOperand Callee = Op.getOperand(4);
485 MVT::ValueType RetVT= Op.Val->getValueType(0);
486 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000487
Evan Cheng347d5f72006-04-28 21:29:37 +0000488 // Keep track of the number of XMM regs passed so far.
489 unsigned NumXMMRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000490 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000491 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000492 };
Evan Cheng347d5f72006-04-28 21:29:37 +0000493
Evan Cheng32fe1032006-05-25 00:59:30 +0000494 // Count how many bytes are to be pushed on the stack.
495 unsigned NumBytes = 0;
496 for (unsigned i = 0; i != NumOps; ++i) {
497 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000498
Evan Cheng32fe1032006-05-25 00:59:30 +0000499 switch (Arg.getValueType()) {
500 default: assert(0 && "Unexpected ValueType for argument!");
501 case MVT::i8:
502 case MVT::i16:
503 case MVT::i32:
504 case MVT::f32:
505 NumBytes += 4;
506 break;
507 case MVT::i64:
508 case MVT::f64:
509 NumBytes += 8;
510 break;
511 case MVT::v16i8:
512 case MVT::v8i16:
513 case MVT::v4i32:
514 case MVT::v2i64:
515 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000516 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000517 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +0000518 ++NumXMMRegs;
519 else
520 NumBytes += 16;
521 break;
522 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000523 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000524
Evan Cheng32fe1032006-05-25 00:59:30 +0000525 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000526
Evan Cheng32fe1032006-05-25 00:59:30 +0000527 // Arguments go on the stack in reverse order, as specified by the ABI.
528 unsigned ArgOffset = 0;
529 NumXMMRegs = 0;
530 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
531 std::vector<SDOperand> MemOpChains;
532 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
533 for (unsigned i = 0; i != NumOps; ++i) {
534 SDOperand Arg = Op.getOperand(5+2*i);
535
536 switch (Arg.getValueType()) {
537 default: assert(0 && "Unexpected ValueType for argument!");
538 case MVT::i8:
Evan Cheng6b5783d2006-05-25 18:56:34 +0000539 case MVT::i16: {
Evan Cheng32fe1032006-05-25 00:59:30 +0000540 // Promote the integer to 32 bits. If the input type is signed use a
541 // sign extend, otherwise use a zero extend.
542 unsigned ExtOp =
543 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
544 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
545 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng6b5783d2006-05-25 18:56:34 +0000546 }
547 // Fallthrough
Evan Cheng32fe1032006-05-25 00:59:30 +0000548
549 case MVT::i32:
550 case MVT::f32: {
551 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
552 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
553 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
554 Arg, PtrOff, DAG.getSrcValue(NULL)));
555 ArgOffset += 4;
556 break;
557 }
558 case MVT::i64:
559 case MVT::f64: {
560 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
561 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
562 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
563 Arg, PtrOff, DAG.getSrcValue(NULL)));
564 ArgOffset += 8;
565 break;
566 }
567 case MVT::v16i8:
568 case MVT::v8i16:
569 case MVT::v4i32:
570 case MVT::v2i64:
571 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000572 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000573 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000574 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
575 NumXMMRegs++;
576 } else {
Evan Cheng347d5f72006-04-28 21:29:37 +0000577 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +0000578 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
579 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
580 Arg, PtrOff, DAG.getSrcValue(NULL)));
581 ArgOffset += 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000582 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000583 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000584 }
585
Evan Cheng32fe1032006-05-25 00:59:30 +0000586 if (!MemOpChains.empty())
587 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000588
Evan Cheng347d5f72006-04-28 21:29:37 +0000589 // Build a sequence of copy-to-reg nodes chained together with token chain
590 // and flag operands which copy the outgoing args into registers.
591 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000592 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
593 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
594 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000595 InFlag = Chain.getValue(1);
596 }
597
Evan Cheng32fe1032006-05-25 00:59:30 +0000598 // If the callee is a GlobalAddress node (quite common, every direct call is)
599 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
600 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
601 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
602 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
603 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
604
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000605 std::vector<MVT::ValueType> NodeTys;
606 NodeTys.push_back(MVT::Other); // Returns a chain
607 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
608 std::vector<SDOperand> Ops;
609 Ops.push_back(Chain);
610 Ops.push_back(Callee);
Evan Cheng347d5f72006-04-28 21:29:37 +0000611 if (InFlag.Val)
612 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000613
Evan Cheng32fe1032006-05-25 00:59:30 +0000614 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
615 NodeTys, Ops);
Evan Cheng347d5f72006-04-28 21:29:37 +0000616 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000617
Chris Lattner2d297092006-05-23 18:50:38 +0000618 // Create the CALLSEQ_END node.
619 unsigned NumBytesForCalleeToPush = 0;
620
621 // If this is is a call to a struct-return function on Darwin/X86, the callee
622 // pops the hidden struct pointer, so we have to push it back.
623 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
624 NumBytesForCalleeToPush = 4;
625
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000626 NodeTys.clear();
627 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +0000628 if (RetVT != MVT::Other)
629 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000630 Ops.clear();
631 Ops.push_back(Chain);
632 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000633 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000634 Ops.push_back(InFlag);
635 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng32fe1032006-05-25 00:59:30 +0000636 if (RetVT != MVT::Other)
637 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000638
Evan Cheng32fe1032006-05-25 00:59:30 +0000639 std::vector<SDOperand> ResultVals;
640 NodeTys.clear();
641 switch (RetVT) {
642 default: assert(0 && "Unknown value type to return!");
643 case MVT::Other: break;
644 case MVT::i8:
645 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
646 ResultVals.push_back(Chain.getValue(0));
647 NodeTys.push_back(MVT::i8);
648 break;
649 case MVT::i16:
650 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
651 ResultVals.push_back(Chain.getValue(0));
652 NodeTys.push_back(MVT::i16);
653 break;
654 case MVT::i32:
655 if (Op.Val->getValueType(1) == MVT::i32) {
656 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
657 ResultVals.push_back(Chain.getValue(0));
658 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
659 Chain.getValue(2)).getValue(1);
660 ResultVals.push_back(Chain.getValue(0));
661 NodeTys.push_back(MVT::i32);
662 } else {
663 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
664 ResultVals.push_back(Chain.getValue(0));
Evan Chengd90eb7f2006-01-05 00:27:02 +0000665 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000666 NodeTys.push_back(MVT::i32);
667 break;
668 case MVT::v16i8:
669 case MVT::v8i16:
670 case MVT::v4i32:
671 case MVT::v2i64:
672 case MVT::v4f32:
673 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +0000674 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
675 ResultVals.push_back(Chain.getValue(0));
676 NodeTys.push_back(RetVT);
677 break;
678 case MVT::f32:
679 case MVT::f64: {
680 std::vector<MVT::ValueType> Tys;
681 Tys.push_back(MVT::f64);
682 Tys.push_back(MVT::Other);
683 Tys.push_back(MVT::Flag);
684 std::vector<SDOperand> Ops;
685 Ops.push_back(Chain);
686 Ops.push_back(InFlag);
687 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
688 Chain = RetVal.getValue(1);
689 InFlag = RetVal.getValue(2);
690 if (X86ScalarSSE) {
691 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
692 // shouldn't be necessary except that RFP cannot be live across
693 // multiple blocks. When stackifier is fixed, they can be uncoupled.
694 MachineFunction &MF = DAG.getMachineFunction();
695 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
696 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
697 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000698 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +0000699 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000700 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +0000701 Ops.push_back(RetVal);
702 Ops.push_back(StackSlot);
703 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000704 Ops.push_back(InFlag);
Evan Cheng32fe1032006-05-25 00:59:30 +0000705 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
706 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
707 DAG.getSrcValue(NULL));
Evan Cheng347d5f72006-04-28 21:29:37 +0000708 Chain = RetVal.getValue(1);
Evan Cheng347d5f72006-04-28 21:29:37 +0000709 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000710
711 if (RetVT == MVT::f32 && !X86ScalarSSE)
712 // FIXME: we would really like to remember that this FP_ROUND
713 // operation is okay to eliminate if we allow excess FP precision.
714 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
715 ResultVals.push_back(RetVal);
716 NodeTys.push_back(RetVT);
717 break;
718 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000719 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000720
Evan Cheng32fe1032006-05-25 00:59:30 +0000721 // If the function returns void, just return the chain.
722 if (ResultVals.empty())
723 return Chain;
724
725 // Otherwise, merge everything together with a MERGE_VALUES node.
726 NodeTys.push_back(MVT::Other);
727 ResultVals.push_back(Chain);
728 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
729 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000730}
731
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000732//===----------------------------------------------------------------------===//
733// Fast Calling Convention implementation
734//===----------------------------------------------------------------------===//
735//
736// The X86 'fast' calling convention passes up to two integer arguments in
737// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
738// and requires that the callee pop its arguments off the stack (allowing proper
739// tail calls), and has the same return value conventions as C calling convs.
740//
741// This calling convention always arranges for the callee pop value to be 8n+4
742// bytes, which is needed for tail recursion elimination and stack alignment
743// reasons.
744//
745// Note that this can be enhanced in the future to pass fp vals in registers
746// (when we have a global fp allocator) and do other tricks.
747//
748
Chris Lattner89fad2c2006-03-17 17:27:47 +0000749// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
750// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
751// EDX". Anything more is illegal.
752//
753// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000754// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000755// a physreg with a virtreg, this increases the size of the physreg's live
756// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000757// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000758// allocator to wedge itself.
759//
760// This code triggers this problem more often if we pass args in registers,
761// so disable it until this is fixed.
762//
763// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
764// about code being dead.
765//
766static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000767
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000768
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000769/// HowToPassFastCCArgument - Returns how an formal argument of the specified
770/// type should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +0000771/// slot; if it is through integer or XMM register, returns the number of
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000772/// integer or XMM registers are needed.
Evan Chengeda65fa2006-04-27 01:32:22 +0000773static void
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000774HowToPassFastCCArgument(MVT::ValueType ObjectVT,
775 unsigned NumIntRegs, unsigned NumXMMRegs,
776 unsigned &ObjSize, unsigned &ObjIntRegs,
777 unsigned &ObjXMMRegs) {
Evan Chengeda65fa2006-04-27 01:32:22 +0000778 ObjSize = 0;
779 NumIntRegs = 0;
Evan Chengcc1fc222006-05-25 23:31:23 +0000780 NumXMMRegs = 0;
Evan Chengeda65fa2006-04-27 01:32:22 +0000781
782 switch (ObjectVT) {
783 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000784 case MVT::i8:
785 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000786 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000787 else
788 ObjSize = 1;
789 break;
790 case MVT::i16:
791 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000792 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000793 else
794 ObjSize = 2;
795 break;
796 case MVT::i32:
797 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000798 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000799 else
800 ObjSize = 4;
801 break;
802 case MVT::i64:
803 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +0000804 ObjIntRegs = 2;
Evan Chengeda65fa2006-04-27 01:32:22 +0000805 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +0000806 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000807 ObjSize = 4;
808 } else
809 ObjSize = 8;
810 case MVT::f32:
811 ObjSize = 4;
812 break;
813 case MVT::f64:
814 ObjSize = 8;
815 break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000816 case MVT::v16i8:
817 case MVT::v8i16:
818 case MVT::v4i32:
819 case MVT::v2i64:
820 case MVT::v4f32:
821 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000822 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000823 ObjXMMRegs = 1;
824 else
825 ObjSize = 16;
826 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000827 }
828}
829
Evan Cheng25caf632006-05-23 21:06:34 +0000830SDOperand
831X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
832 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000833 MachineFunction &MF = DAG.getMachineFunction();
834 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000835 SDOperand Root = Op.getOperand(0);
836 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000837
Evan Chengeda65fa2006-04-27 01:32:22 +0000838 // Add DAG nodes to load the arguments... On entry to a function the stack
839 // frame looks like this:
840 //
841 // [ESP] -- return address
842 // [ESP + 4] -- first nonreg argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000843 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000844 // ...
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000845 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
846
847 // Keep track of the number of integer regs passed so far. This can be either
848 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
849 // used).
850 unsigned NumIntRegs = 0;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000851 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng32fe1032006-05-25 00:59:30 +0000852
853 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000854 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000855 };
Chris Lattner1c636e92006-03-17 05:10:20 +0000856
Evan Cheng1bc78042006-04-26 01:20:17 +0000857 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000858 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
859 unsigned ArgIncrement = 4;
860 unsigned ObjSize = 0;
861 unsigned ObjIntRegs = 0;
862 unsigned ObjXMMRegs = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000863
Evan Cheng25caf632006-05-23 21:06:34 +0000864 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
865 ObjSize, ObjIntRegs, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +0000866 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000867 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000868
Evan Cheng25caf632006-05-23 21:06:34 +0000869 unsigned Reg;
870 SDOperand ArgValue;
871 if (ObjIntRegs || ObjXMMRegs) {
872 switch (ObjectVT) {
873 default: assert(0 && "Unhandled argument type!");
Evan Cheng25caf632006-05-23 21:06:34 +0000874 case MVT::i8:
875 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
876 X86::GR8RegisterClass);
877 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
878 break;
879 case MVT::i16:
880 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
881 X86::GR16RegisterClass);
882 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
883 break;
884 case MVT::i32:
885 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
886 X86::GR32RegisterClass);
887 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
888 break;
889 case MVT::i64:
890 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
891 X86::GR32RegisterClass);
892 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
893 if (ObjIntRegs == 2) {
894 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
895 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
896 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng85e38002006-04-27 05:35:28 +0000897 }
Evan Cheng25caf632006-05-23 21:06:34 +0000898 break;
899 case MVT::v16i8:
900 case MVT::v8i16:
901 case MVT::v4i32:
902 case MVT::v2i64:
903 case MVT::v4f32:
904 case MVT::v2f64:
905 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
906 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
907 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000908 }
Evan Cheng25caf632006-05-23 21:06:34 +0000909 NumIntRegs += ObjIntRegs;
910 NumXMMRegs += ObjXMMRegs;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000911 }
Evan Cheng25caf632006-05-23 21:06:34 +0000912
913 if (ObjSize) {
914 // Create the SelectionDAG nodes corresponding to a load from this
915 // parameter.
916 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
917 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
918 if (ObjectVT == MVT::i64 && ObjIntRegs) {
919 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
920 DAG.getSrcValue(NULL));
921 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
922 } else
923 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
924 DAG.getSrcValue(NULL));
925 ArgOffset += ArgIncrement; // Move on to the next argument.
926 }
927
928 ArgValues.push_back(ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000929 }
930
Evan Cheng25caf632006-05-23 21:06:34 +0000931 ArgValues.push_back(Root);
932
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000933 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
934 // arguments and the arguments after the retaddr has been pushed are aligned.
935 if ((ArgOffset & 7) == 0)
936 ArgOffset += 4;
937
938 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
939 ReturnAddrIndex = 0; // No return address slot generated yet.
940 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
941 BytesCallerReserves = 0;
942
943 // Finally, inform the code generator which regs we return values in.
Evan Cheng25caf632006-05-23 21:06:34 +0000944 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000945 default: assert(0 && "Unknown type!");
946 case MVT::isVoid: break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000947 case MVT::i8:
948 case MVT::i16:
949 case MVT::i32:
950 MF.addLiveOut(X86::EAX);
951 break;
952 case MVT::i64:
953 MF.addLiveOut(X86::EAX);
954 MF.addLiveOut(X86::EDX);
955 break;
956 case MVT::f32:
957 case MVT::f64:
958 MF.addLiveOut(X86::ST0);
959 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +0000960 case MVT::v16i8:
961 case MVT::v8i16:
962 case MVT::v4i32:
963 case MVT::v2i64:
964 case MVT::v4f32:
965 case MVT::v2f64:
Evan Cheng347d5f72006-04-28 21:29:37 +0000966 MF.addLiveOut(X86::XMM0);
967 break;
968 }
Evan Cheng347d5f72006-04-28 21:29:37 +0000969
Evan Cheng25caf632006-05-23 21:06:34 +0000970 // Return the new list of results.
971 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
972 Op.Val->value_end());
973 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000974}
975
Evan Cheng32fe1032006-05-25 00:59:30 +0000976 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
977 SDOperand Chain = Op.getOperand(0);
978 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
979 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
980 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
981 SDOperand Callee = Op.getOperand(4);
982 MVT::ValueType RetVT= Op.Val->getValueType(0);
983 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
984
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000985 // Count how many bytes are to be pushed on the stack.
986 unsigned NumBytes = 0;
987
988 // Keep track of the number of integer regs passed so far. This can be either
989 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
990 // used).
991 unsigned NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000992 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000993
Evan Cheng32fe1032006-05-25 00:59:30 +0000994 static const unsigned GPRArgRegs[][2] = {
995 { X86::AL, X86::DL },
996 { X86::AX, X86::DX },
997 { X86::EAX, X86::EDX }
998 };
999 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001000 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001001 };
1002
1003 for (unsigned i = 0; i != NumOps; ++i) {
1004 SDOperand Arg = Op.getOperand(5+2*i);
1005
1006 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001007 default: assert(0 && "Unknown value type!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001008 case MVT::i8:
1009 case MVT::i16:
1010 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +00001011 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001012 ++NumIntRegs;
1013 break;
1014 }
Evan Cheng25e71d12006-05-25 22:38:31 +00001015 // Fall through
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001016 case MVT::f32:
1017 NumBytes += 4;
1018 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001019 case MVT::f64:
1020 NumBytes += 8;
1021 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001022 case MVT::v16i8:
1023 case MVT::v8i16:
1024 case MVT::v4i32:
1025 case MVT::v2i64:
1026 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001027 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001028 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +00001029 NumXMMRegs++;
1030 else
1031 NumBytes += 16;
1032 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001033 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001034 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001035
1036 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1037 // arguments and the arguments after the retaddr has been pushed are aligned.
1038 if ((NumBytes & 7) == 0)
1039 NumBytes += 4;
1040
Chris Lattner94dd2922006-02-13 09:00:43 +00001041 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001042
1043 // Arguments go on the stack in reverse order, as specified by the ABI.
1044 unsigned ArgOffset = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001045 NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001046 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1047 std::vector<SDOperand> MemOpChains;
1048 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1049 for (unsigned i = 0; i != NumOps; ++i) {
1050 SDOperand Arg = Op.getOperand(5+2*i);
1051
1052 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001053 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001054 case MVT::i8:
1055 case MVT::i16:
1056 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +00001057 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001058 RegsToPass.push_back(
1059 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1060 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001061 ++NumIntRegs;
1062 break;
1063 }
1064 // Fall through
1065 case MVT::f32: {
1066 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001067 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1068 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1069 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001070 ArgOffset += 4;
1071 break;
1072 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001073 case MVT::f64: {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001074 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001075 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1076 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1077 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001078 ArgOffset += 8;
1079 break;
1080 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001081 case MVT::v16i8:
1082 case MVT::v8i16:
1083 case MVT::v4i32:
1084 case MVT::v2i64:
1085 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001086 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001087 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001088 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1089 NumXMMRegs++;
1090 } else {
1091 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1092 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1093 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1094 Arg, PtrOff, DAG.getSrcValue(NULL)));
1095 ArgOffset += 16;
1096 }
1097 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001098 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001099
Evan Cheng32fe1032006-05-25 00:59:30 +00001100 if (!MemOpChains.empty())
1101 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001102
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001103 // Build a sequence of copy-to-reg nodes chained together with token chain
1104 // and flag operands which copy the outgoing args into registers.
1105 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001106 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1107 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1108 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001109 InFlag = Chain.getValue(1);
1110 }
1111
Evan Cheng32fe1032006-05-25 00:59:30 +00001112 // If the callee is a GlobalAddress node (quite common, every direct call is)
1113 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1114 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1115 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1116 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1117 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1118
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001119 std::vector<MVT::ValueType> NodeTys;
1120 NodeTys.push_back(MVT::Other); // Returns a chain
1121 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1122 std::vector<SDOperand> Ops;
1123 Ops.push_back(Chain);
1124 Ops.push_back(Callee);
1125 if (InFlag.Val)
1126 Ops.push_back(InFlag);
1127
1128 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001129 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1130 NodeTys, Ops);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001131 InFlag = Chain.getValue(1);
1132
1133 NodeTys.clear();
1134 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +00001135 if (RetVT != MVT::Other)
1136 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001137 Ops.clear();
1138 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001139 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1140 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001141 Ops.push_back(InFlag);
1142 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng32fe1032006-05-25 00:59:30 +00001143 if (RetVT != MVT::Other)
1144 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001145
Evan Cheng32fe1032006-05-25 00:59:30 +00001146 std::vector<SDOperand> ResultVals;
1147 NodeTys.clear();
1148 switch (RetVT) {
1149 default: assert(0 && "Unknown value type to return!");
1150 case MVT::Other: break;
1151 case MVT::i8:
1152 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1153 ResultVals.push_back(Chain.getValue(0));
1154 NodeTys.push_back(MVT::i8);
1155 break;
1156 case MVT::i16:
1157 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1158 ResultVals.push_back(Chain.getValue(0));
1159 NodeTys.push_back(MVT::i16);
1160 break;
1161 case MVT::i32:
1162 if (Op.Val->getValueType(1) == MVT::i32) {
1163 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1164 ResultVals.push_back(Chain.getValue(0));
1165 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1166 Chain.getValue(2)).getValue(1);
1167 ResultVals.push_back(Chain.getValue(0));
1168 NodeTys.push_back(MVT::i32);
1169 } else {
1170 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1171 ResultVals.push_back(Chain.getValue(0));
Evan Chengd9558e02006-01-06 00:43:03 +00001172 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001173 NodeTys.push_back(MVT::i32);
1174 break;
1175 case MVT::v16i8:
1176 case MVT::v8i16:
1177 case MVT::v4i32:
1178 case MVT::v2i64:
1179 case MVT::v4f32:
1180 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +00001181 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1182 ResultVals.push_back(Chain.getValue(0));
1183 NodeTys.push_back(RetVT);
1184 break;
1185 case MVT::f32:
1186 case MVT::f64: {
1187 std::vector<MVT::ValueType> Tys;
1188 Tys.push_back(MVT::f64);
1189 Tys.push_back(MVT::Other);
1190 Tys.push_back(MVT::Flag);
1191 std::vector<SDOperand> Ops;
1192 Ops.push_back(Chain);
1193 Ops.push_back(InFlag);
1194 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1195 Chain = RetVal.getValue(1);
1196 InFlag = RetVal.getValue(2);
1197 if (X86ScalarSSE) {
1198 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1199 // shouldn't be necessary except that RFP cannot be live across
1200 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1201 MachineFunction &MF = DAG.getMachineFunction();
1202 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1203 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1204 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001205 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +00001206 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001207 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001208 Ops.push_back(RetVal);
1209 Ops.push_back(StackSlot);
1210 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001211 Ops.push_back(InFlag);
Evan Cheng32fe1032006-05-25 00:59:30 +00001212 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1213 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1214 DAG.getSrcValue(NULL));
1215 Chain = RetVal.getValue(1);
1216 }
Evan Chengd9558e02006-01-06 00:43:03 +00001217
Evan Cheng32fe1032006-05-25 00:59:30 +00001218 if (RetVT == MVT::f32 && !X86ScalarSSE)
1219 // FIXME: we would really like to remember that this FP_ROUND
1220 // operation is okay to eliminate if we allow excess FP precision.
1221 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1222 ResultVals.push_back(RetVal);
1223 NodeTys.push_back(RetVT);
1224 break;
1225 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001226 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001227
Evan Cheng32fe1032006-05-25 00:59:30 +00001228
1229 // If the function returns void, just return the chain.
1230 if (ResultVals.empty())
1231 return Chain;
1232
1233 // Otherwise, merge everything together with a MERGE_VALUES node.
1234 NodeTys.push_back(MVT::Other);
1235 ResultVals.push_back(Chain);
1236 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1237 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001238}
1239
1240SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1241 if (ReturnAddrIndex == 0) {
1242 // Set up a frame object for the return address.
1243 MachineFunction &MF = DAG.getMachineFunction();
1244 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1245 }
1246
1247 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1248}
1249
1250
1251
1252std::pair<SDOperand, SDOperand> X86TargetLowering::
1253LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1254 SelectionDAG &DAG) {
1255 SDOperand Result;
1256 if (Depth) // Depths > 0 not supported yet!
1257 Result = DAG.getConstant(0, getPointerTy());
1258 else {
1259 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1260 if (!isFrameAddress)
1261 // Just load the return address
1262 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1263 DAG.getSrcValue(NULL));
1264 else
1265 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1266 DAG.getConstant(4, MVT::i32));
1267 }
1268 return std::make_pair(Result, Chain);
1269}
1270
Evan Cheng4a460802006-01-11 00:33:36 +00001271/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1272/// which corresponds to the condition code.
1273static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1274 switch (X86CC) {
1275 default: assert(0 && "Unknown X86 conditional code!");
1276 case X86ISD::COND_A: return X86::JA;
1277 case X86ISD::COND_AE: return X86::JAE;
1278 case X86ISD::COND_B: return X86::JB;
1279 case X86ISD::COND_BE: return X86::JBE;
1280 case X86ISD::COND_E: return X86::JE;
1281 case X86ISD::COND_G: return X86::JG;
1282 case X86ISD::COND_GE: return X86::JGE;
1283 case X86ISD::COND_L: return X86::JL;
1284 case X86ISD::COND_LE: return X86::JLE;
1285 case X86ISD::COND_NE: return X86::JNE;
1286 case X86ISD::COND_NO: return X86::JNO;
1287 case X86ISD::COND_NP: return X86::JNP;
1288 case X86ISD::COND_NS: return X86::JNS;
1289 case X86ISD::COND_O: return X86::JO;
1290 case X86ISD::COND_P: return X86::JP;
1291 case X86ISD::COND_S: return X86::JS;
1292 }
1293}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001294
Evan Cheng6dfa9992006-01-30 23:41:35 +00001295/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1296/// specific condition code. It returns a false if it cannot do a direct
1297/// translation. X86CC is the translated CondCode. Flip is set to true if the
1298/// the order of comparison operands should be flipped.
Evan Cheng6be2c582006-04-05 23:38:46 +00001299static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1300 unsigned &X86CC, bool &Flip) {
Evan Cheng6dfa9992006-01-30 23:41:35 +00001301 Flip = false;
1302 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001303 if (!isFP) {
1304 switch (SetCCOpcode) {
1305 default: break;
1306 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1307 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1308 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1309 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1310 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1311 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1312 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1313 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1314 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1315 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1316 }
1317 } else {
1318 // On a floating point condition, the flags are set as follows:
1319 // ZF PF CF op
1320 // 0 | 0 | 0 | X > Y
1321 // 0 | 0 | 1 | X < Y
1322 // 1 | 0 | 0 | X == Y
1323 // 1 | 1 | 1 | unordered
1324 switch (SetCCOpcode) {
1325 default: break;
1326 case ISD::SETUEQ:
1327 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001328 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001329 case ISD::SETOGT:
1330 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001331 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001332 case ISD::SETOGE:
1333 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001334 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001335 case ISD::SETULT:
1336 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001337 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001338 case ISD::SETULE:
1339 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1340 case ISD::SETONE:
1341 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1342 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1343 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1344 }
1345 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001346
1347 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001348}
1349
Evan Cheng6be2c582006-04-05 23:38:46 +00001350static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1351 bool &Flip) {
1352 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1353}
1354
Evan Cheng4a460802006-01-11 00:33:36 +00001355/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1356/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001357/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001358static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001359 switch (X86CC) {
1360 default:
1361 return false;
1362 case X86ISD::COND_B:
1363 case X86ISD::COND_BE:
1364 case X86ISD::COND_E:
1365 case X86ISD::COND_P:
1366 case X86ISD::COND_A:
1367 case X86ISD::COND_AE:
1368 case X86ISD::COND_NE:
1369 case X86ISD::COND_NP:
1370 return true;
1371 }
1372}
1373
Evan Cheng4a460802006-01-11 00:33:36 +00001374MachineBasicBlock *
1375X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1376 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001377 switch (MI->getOpcode()) {
1378 default: assert(false && "Unexpected instr type to insert");
1379 case X86::CMOV_FR32:
Evan Chengf7c378e2006-04-10 07:23:14 +00001380 case X86::CMOV_FR64:
1381 case X86::CMOV_V4F32:
1382 case X86::CMOV_V2F64:
1383 case X86::CMOV_V2I64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001384 // To "insert" a SELECT_CC instruction, we actually have to insert the
1385 // diamond control-flow pattern. The incoming instruction knows the
1386 // destination vreg to set, the condition code register to branch on, the
1387 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001388 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1389 ilist<MachineBasicBlock>::iterator It = BB;
1390 ++It;
1391
1392 // thisMBB:
1393 // ...
1394 // TrueVal = ...
1395 // cmpTY ccX, r1, r2
1396 // bCC copy1MBB
1397 // fallthrough --> copy0MBB
1398 MachineBasicBlock *thisMBB = BB;
1399 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1400 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1401 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1402 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1403 MachineFunction *F = BB->getParent();
1404 F->getBasicBlockList().insert(It, copy0MBB);
1405 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001406 // Update machine-CFG edges by first adding all successors of the current
1407 // block to the new block which will contain the Phi node for the select.
1408 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1409 e = BB->succ_end(); i != e; ++i)
1410 sinkMBB->addSuccessor(*i);
1411 // Next, remove all successors of the current block, and add the true
1412 // and fallthrough blocks as its successors.
1413 while(!BB->succ_empty())
1414 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001415 BB->addSuccessor(copy0MBB);
1416 BB->addSuccessor(sinkMBB);
1417
1418 // copy0MBB:
1419 // %FalseValue = ...
1420 // # fallthrough to sinkMBB
1421 BB = copy0MBB;
1422
1423 // Update machine-CFG edges
1424 BB->addSuccessor(sinkMBB);
1425
1426 // sinkMBB:
1427 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1428 // ...
1429 BB = sinkMBB;
1430 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1431 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1432 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001433
Evan Cheng0cc39452006-01-16 21:21:29 +00001434 delete MI; // The pseudo instruction is gone now.
1435 return BB;
1436 }
Evan Cheng4a460802006-01-11 00:33:36 +00001437
Evan Cheng0cc39452006-01-16 21:21:29 +00001438 case X86::FP_TO_INT16_IN_MEM:
1439 case X86::FP_TO_INT32_IN_MEM:
1440 case X86::FP_TO_INT64_IN_MEM: {
1441 // Change the floating point control register to use "round towards zero"
1442 // mode when truncating to an integer value.
1443 MachineFunction *F = BB->getParent();
1444 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1445 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1446
1447 // Load the old value of the high byte of the control word...
1448 unsigned OldCW =
Evan Cheng069287d2006-05-16 07:21:53 +00001449 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng0cc39452006-01-16 21:21:29 +00001450 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1451
1452 // Set the high part to be round to zero...
1453 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1454
1455 // Reload the modified control word now...
1456 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1457
1458 // Restore the memory image of control word to original value
1459 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1460
1461 // Get the X86 opcode to use.
1462 unsigned Opc;
1463 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001464 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001465 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1466 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1467 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1468 }
1469
1470 X86AddressMode AM;
1471 MachineOperand &Op = MI->getOperand(0);
1472 if (Op.isRegister()) {
1473 AM.BaseType = X86AddressMode::RegBase;
1474 AM.Base.Reg = Op.getReg();
1475 } else {
1476 AM.BaseType = X86AddressMode::FrameIndexBase;
1477 AM.Base.FrameIndex = Op.getFrameIndex();
1478 }
1479 Op = MI->getOperand(1);
1480 if (Op.isImmediate())
1481 AM.Scale = Op.getImmedValue();
1482 Op = MI->getOperand(2);
1483 if (Op.isImmediate())
1484 AM.IndexReg = Op.getImmedValue();
1485 Op = MI->getOperand(3);
1486 if (Op.isGlobalAddress()) {
1487 AM.GV = Op.getGlobal();
1488 } else {
1489 AM.Disp = Op.getImmedValue();
1490 }
1491 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1492
1493 // Reload the original control word now.
1494 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1495
1496 delete MI; // The pseudo instruction is gone now.
1497 return BB;
1498 }
1499 }
Evan Cheng4a460802006-01-11 00:33:36 +00001500}
1501
1502
1503//===----------------------------------------------------------------------===//
1504// X86 Custom Lowering Hooks
1505//===----------------------------------------------------------------------===//
1506
Evan Cheng30b37b52006-03-13 23:18:16 +00001507/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1508/// load. For Darwin, external and weak symbols are indirect, loading the value
1509/// at address GV rather then the value of GV itself. This means that the
1510/// GlobalAddress must be in the base or index register of the address, not the
1511/// GV offset field.
1512static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1513 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1514 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1515}
1516
Evan Cheng5ced1d82006-04-06 23:23:56 +00001517/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001518/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001519static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1520 if (Op.getOpcode() == ISD::UNDEF)
1521 return true;
1522
1523 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001524 return (Val >= Low && Val < Hi);
1525}
1526
1527/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1528/// true if Op is undef or if its value equal to the specified value.
1529static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1530 if (Op.getOpcode() == ISD::UNDEF)
1531 return true;
1532 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001533}
1534
Evan Cheng0188ecb2006-03-22 18:59:22 +00001535/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1536/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1537bool X86::isPSHUFDMask(SDNode *N) {
1538 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1539
1540 if (N->getNumOperands() != 4)
1541 return false;
1542
1543 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001544 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001545 SDOperand Arg = N->getOperand(i);
1546 if (Arg.getOpcode() == ISD::UNDEF) continue;
1547 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1548 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001549 return false;
1550 }
1551
1552 return true;
1553}
1554
1555/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001556/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001557bool X86::isPSHUFHWMask(SDNode *N) {
1558 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1559
1560 if (N->getNumOperands() != 8)
1561 return false;
1562
1563 // Lower quadword copied in order.
1564 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001565 SDOperand Arg = N->getOperand(i);
1566 if (Arg.getOpcode() == ISD::UNDEF) continue;
1567 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1568 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001569 return false;
1570 }
1571
1572 // Upper quadword shuffled.
1573 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001574 SDOperand Arg = N->getOperand(i);
1575 if (Arg.getOpcode() == ISD::UNDEF) continue;
1576 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1577 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001578 if (Val < 4 || Val > 7)
1579 return false;
1580 }
1581
1582 return true;
1583}
1584
1585/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001586/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001587bool X86::isPSHUFLWMask(SDNode *N) {
1588 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1589
1590 if (N->getNumOperands() != 8)
1591 return false;
1592
1593 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001594 for (unsigned i = 4; i != 8; ++i)
1595 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001596 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001597
1598 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001599 for (unsigned i = 0; i != 4; ++i)
1600 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001601 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001602
1603 return true;
1604}
1605
Evan Cheng14aed5e2006-03-24 01:18:28 +00001606/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1607/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng39623da2006-04-20 08:58:49 +00001608static bool isSHUFPMask(std::vector<SDOperand> &N) {
1609 unsigned NumElems = N.size();
1610 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001611
Evan Cheng39623da2006-04-20 08:58:49 +00001612 unsigned Half = NumElems / 2;
1613 for (unsigned i = 0; i < Half; ++i)
1614 if (!isUndefOrInRange(N[i], 0, NumElems))
1615 return false;
1616 for (unsigned i = Half; i < NumElems; ++i)
1617 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1618 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001619
1620 return true;
1621}
1622
Evan Cheng39623da2006-04-20 08:58:49 +00001623bool X86::isSHUFPMask(SDNode *N) {
1624 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1625 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1626 return ::isSHUFPMask(Ops);
1627}
1628
1629/// isCommutedSHUFP - Returns true if the shuffle mask is except
1630/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1631/// half elements to come from vector 1 (which would equal the dest.) and
1632/// the upper half to come from vector 2.
1633static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1634 unsigned NumElems = Ops.size();
1635 if (NumElems != 2 && NumElems != 4) return false;
1636
1637 unsigned Half = NumElems / 2;
1638 for (unsigned i = 0; i < Half; ++i)
1639 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1640 return false;
1641 for (unsigned i = Half; i < NumElems; ++i)
1642 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1643 return false;
1644 return true;
1645}
1646
1647static bool isCommutedSHUFP(SDNode *N) {
1648 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1649 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1650 return isCommutedSHUFP(Ops);
1651}
1652
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001653/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1654/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1655bool X86::isMOVHLPSMask(SDNode *N) {
1656 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1657
Evan Cheng2064a2b2006-03-28 06:50:32 +00001658 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001659 return false;
1660
Evan Cheng2064a2b2006-03-28 06:50:32 +00001661 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001662 return isUndefOrEqual(N->getOperand(0), 6) &&
1663 isUndefOrEqual(N->getOperand(1), 7) &&
1664 isUndefOrEqual(N->getOperand(2), 2) &&
1665 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001666}
1667
Evan Cheng5ced1d82006-04-06 23:23:56 +00001668/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1669/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1670bool X86::isMOVLPMask(SDNode *N) {
1671 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1672
1673 unsigned NumElems = N->getNumOperands();
1674 if (NumElems != 2 && NumElems != 4)
1675 return false;
1676
Evan Chengc5cdff22006-04-07 21:53:05 +00001677 for (unsigned i = 0; i < NumElems/2; ++i)
1678 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1679 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001680
Evan Chengc5cdff22006-04-07 21:53:05 +00001681 for (unsigned i = NumElems/2; i < NumElems; ++i)
1682 if (!isUndefOrEqual(N->getOperand(i), i))
1683 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001684
1685 return true;
1686}
1687
1688/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001689/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1690/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001691bool X86::isMOVHPMask(SDNode *N) {
1692 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1693
1694 unsigned NumElems = N->getNumOperands();
1695 if (NumElems != 2 && NumElems != 4)
1696 return false;
1697
Evan Chengc5cdff22006-04-07 21:53:05 +00001698 for (unsigned i = 0; i < NumElems/2; ++i)
1699 if (!isUndefOrEqual(N->getOperand(i), i))
1700 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001701
1702 for (unsigned i = 0; i < NumElems/2; ++i) {
1703 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001704 if (!isUndefOrEqual(Arg, i + NumElems))
1705 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001706 }
1707
1708 return true;
1709}
1710
Evan Cheng0038e592006-03-28 00:39:58 +00001711/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1712/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng39623da2006-04-20 08:58:49 +00001713bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1714 unsigned NumElems = N.size();
Evan Cheng0038e592006-03-28 00:39:58 +00001715 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1716 return false;
1717
1718 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001719 SDOperand BitI = N[i];
1720 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001721 if (!isUndefOrEqual(BitI, j))
1722 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001723 if (V2IsSplat) {
1724 if (isUndefOrEqual(BitI1, NumElems))
1725 return false;
1726 } else {
1727 if (!isUndefOrEqual(BitI1, j + NumElems))
1728 return false;
1729 }
Evan Cheng0038e592006-03-28 00:39:58 +00001730 }
1731
1732 return true;
1733}
1734
Evan Cheng39623da2006-04-20 08:58:49 +00001735bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1736 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1737 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1738 return ::isUNPCKLMask(Ops, V2IsSplat);
1739}
1740
Evan Cheng4fcb9222006-03-28 02:43:26 +00001741/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1742/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng39623da2006-04-20 08:58:49 +00001743bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1744 unsigned NumElems = N.size();
Evan Cheng4fcb9222006-03-28 02:43:26 +00001745 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1746 return false;
1747
1748 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001749 SDOperand BitI = N[i];
1750 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001751 if (!isUndefOrEqual(BitI, j + NumElems/2))
1752 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001753 if (V2IsSplat) {
1754 if (isUndefOrEqual(BitI1, NumElems))
1755 return false;
1756 } else {
1757 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1758 return false;
1759 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001760 }
1761
1762 return true;
1763}
1764
Evan Cheng39623da2006-04-20 08:58:49 +00001765bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1766 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1767 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1768 return ::isUNPCKHMask(Ops, V2IsSplat);
1769}
1770
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001771/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1772/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1773/// <0, 0, 1, 1>
1774bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1775 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1776
1777 unsigned NumElems = N->getNumOperands();
1778 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1779 return false;
1780
1781 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1782 SDOperand BitI = N->getOperand(i);
1783 SDOperand BitI1 = N->getOperand(i+1);
1784
Evan Chengc5cdff22006-04-07 21:53:05 +00001785 if (!isUndefOrEqual(BitI, j))
1786 return false;
1787 if (!isUndefOrEqual(BitI1, j))
1788 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001789 }
1790
1791 return true;
1792}
1793
Evan Cheng017dcc62006-04-21 01:05:10 +00001794/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1795/// specifies a shuffle of elements that is suitable for input to MOVSS,
1796/// MOVSD, and MOVD, i.e. setting the lowest element.
1797static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001798 unsigned NumElems = N.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001799 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001800 return false;
1801
Evan Cheng39623da2006-04-20 08:58:49 +00001802 if (!isUndefOrEqual(N[0], NumElems))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001803 return false;
1804
1805 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00001806 SDOperand Arg = N[i];
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001807 if (!isUndefOrEqual(Arg, i))
1808 return false;
1809 }
1810
1811 return true;
1812}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001813
Evan Cheng017dcc62006-04-21 01:05:10 +00001814bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001815 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1816 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001817 return ::isMOVLMask(Ops);
Evan Cheng39623da2006-04-20 08:58:49 +00001818}
1819
Evan Cheng017dcc62006-04-21 01:05:10 +00001820/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1821/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00001822/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Cheng017dcc62006-04-21 01:05:10 +00001823static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001824 unsigned NumElems = Ops.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001825 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00001826 return false;
1827
1828 if (!isUndefOrEqual(Ops[0], 0))
1829 return false;
1830
1831 for (unsigned i = 1; i < NumElems; ++i) {
1832 SDOperand Arg = Ops[i];
1833 if (V2IsSplat) {
1834 if (!isUndefOrEqual(Arg, NumElems))
1835 return false;
1836 } else {
1837 if (!isUndefOrEqual(Arg, i+NumElems))
1838 return false;
1839 }
1840 }
1841
1842 return true;
1843}
1844
Evan Cheng017dcc62006-04-21 01:05:10 +00001845static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001846 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1847 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001848 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001849}
1850
Evan Chengd9539472006-04-14 21:59:03 +00001851/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1852/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1853bool X86::isMOVSHDUPMask(SDNode *N) {
1854 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1855
1856 if (N->getNumOperands() != 4)
1857 return false;
1858
1859 // Expect 1, 1, 3, 3
1860 for (unsigned i = 0; i < 2; ++i) {
1861 SDOperand Arg = N->getOperand(i);
1862 if (Arg.getOpcode() == ISD::UNDEF) continue;
1863 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1864 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1865 if (Val != 1) return false;
1866 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001867
1868 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001869 for (unsigned i = 2; i < 4; ++i) {
1870 SDOperand Arg = N->getOperand(i);
1871 if (Arg.getOpcode() == ISD::UNDEF) continue;
1872 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1873 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1874 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001875 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001876 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001877
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001878 // Don't use movshdup if it can be done with a shufps.
1879 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001880}
1881
1882/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1883/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1884bool X86::isMOVSLDUPMask(SDNode *N) {
1885 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1886
1887 if (N->getNumOperands() != 4)
1888 return false;
1889
1890 // Expect 0, 0, 2, 2
1891 for (unsigned i = 0; i < 2; ++i) {
1892 SDOperand Arg = N->getOperand(i);
1893 if (Arg.getOpcode() == ISD::UNDEF) continue;
1894 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1895 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1896 if (Val != 0) return false;
1897 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001898
1899 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001900 for (unsigned i = 2; i < 4; ++i) {
1901 SDOperand Arg = N->getOperand(i);
1902 if (Arg.getOpcode() == ISD::UNDEF) continue;
1903 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1904 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1905 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001906 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001907 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001908
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001909 // Don't use movshdup if it can be done with a shufps.
1910 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001911}
1912
Evan Chengb9df0ca2006-03-22 02:53:00 +00001913/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1914/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001915static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001916 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1917
Evan Chengb9df0ca2006-03-22 02:53:00 +00001918 // This is a splat operation if each element of the permute is the same, and
1919 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001920 unsigned NumElems = N->getNumOperands();
1921 SDOperand ElementBase;
1922 unsigned i = 0;
1923 for (; i != NumElems; ++i) {
1924 SDOperand Elt = N->getOperand(i);
1925 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1926 ElementBase = Elt;
1927 break;
1928 }
1929 }
1930
1931 if (!ElementBase.Val)
1932 return false;
1933
1934 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001935 SDOperand Arg = N->getOperand(i);
1936 if (Arg.getOpcode() == ISD::UNDEF) continue;
1937 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001938 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001939 }
1940
1941 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001942 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001943}
1944
Evan Chengc575ca22006-04-17 20:43:08 +00001945/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1946/// a splat of a single element and it's a 2 or 4 element mask.
1947bool X86::isSplatMask(SDNode *N) {
1948 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1949
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001950 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00001951 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1952 return false;
1953 return ::isSplatMask(N);
1954}
1955
Evan Cheng63d33002006-03-22 08:01:21 +00001956/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1957/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1958/// instructions.
1959unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001960 unsigned NumOperands = N->getNumOperands();
1961 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1962 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001963 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001964 unsigned Val = 0;
1965 SDOperand Arg = N->getOperand(NumOperands-i-1);
1966 if (Arg.getOpcode() != ISD::UNDEF)
1967 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001968 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001969 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001970 if (i != NumOperands - 1)
1971 Mask <<= Shift;
1972 }
Evan Cheng63d33002006-03-22 08:01:21 +00001973
1974 return Mask;
1975}
1976
Evan Cheng506d3df2006-03-29 23:07:14 +00001977/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1978/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1979/// instructions.
1980unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1981 unsigned Mask = 0;
1982 // 8 nodes, but we only care about the last 4.
1983 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001984 unsigned Val = 0;
1985 SDOperand Arg = N->getOperand(i);
1986 if (Arg.getOpcode() != ISD::UNDEF)
1987 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001988 Mask |= (Val - 4);
1989 if (i != 4)
1990 Mask <<= 2;
1991 }
1992
1993 return Mask;
1994}
1995
1996/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1997/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1998/// instructions.
1999unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2000 unsigned Mask = 0;
2001 // 8 nodes, but we only care about the first 4.
2002 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002003 unsigned Val = 0;
2004 SDOperand Arg = N->getOperand(i);
2005 if (Arg.getOpcode() != ISD::UNDEF)
2006 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002007 Mask |= Val;
2008 if (i != 0)
2009 Mask <<= 2;
2010 }
2011
2012 return Mask;
2013}
2014
Evan Chengc21a0532006-04-05 01:47:37 +00002015/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2016/// specifies a 8 element shuffle that can be broken into a pair of
2017/// PSHUFHW and PSHUFLW.
2018static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2019 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2020
2021 if (N->getNumOperands() != 8)
2022 return false;
2023
2024 // Lower quadword shuffled.
2025 for (unsigned i = 0; 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 > 4)
2031 return false;
2032 }
2033
2034 // Upper quadword shuffled.
2035 for (unsigned i = 4; i != 8; ++i) {
2036 SDOperand Arg = N->getOperand(i);
2037 if (Arg.getOpcode() == ISD::UNDEF) continue;
2038 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2039 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2040 if (Val < 4 || Val > 7)
2041 return false;
2042 }
2043
2044 return true;
2045}
2046
Evan Cheng5ced1d82006-04-06 23:23:56 +00002047/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2048/// values in ther permute mask.
2049static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2050 SDOperand V1 = Op.getOperand(0);
2051 SDOperand V2 = Op.getOperand(1);
2052 SDOperand Mask = Op.getOperand(2);
2053 MVT::ValueType VT = Op.getValueType();
2054 MVT::ValueType MaskVT = Mask.getValueType();
2055 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2056 unsigned NumElems = Mask.getNumOperands();
2057 std::vector<SDOperand> MaskVec;
2058
2059 for (unsigned i = 0; i != NumElems; ++i) {
2060 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002061 if (Arg.getOpcode() == ISD::UNDEF) {
2062 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2063 continue;
2064 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002065 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2066 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2067 if (Val < NumElems)
2068 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2069 else
2070 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2071 }
2072
2073 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2074 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2075}
2076
Evan Cheng533a0aa2006-04-19 20:35:22 +00002077/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2078/// match movhlps. The lower half elements should come from upper half of
2079/// V1 (and in order), and the upper half elements should come from the upper
2080/// half of V2 (and in order).
2081static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2082 unsigned NumElems = Mask->getNumOperands();
2083 if (NumElems != 4)
2084 return false;
2085 for (unsigned i = 0, e = 2; i != e; ++i)
2086 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2087 return false;
2088 for (unsigned i = 2; i != 4; ++i)
2089 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2090 return false;
2091 return true;
2092}
2093
Evan Cheng5ced1d82006-04-06 23:23:56 +00002094/// isScalarLoadToVector - Returns true if the node is a scalar load that
2095/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002096static inline bool isScalarLoadToVector(SDNode *N) {
2097 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2098 N = N->getOperand(0).Val;
2099 return (N->getOpcode() == ISD::LOAD);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002100 }
2101 return false;
2102}
2103
Evan Cheng533a0aa2006-04-19 20:35:22 +00002104/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2105/// match movlp{s|d}. The lower half elements should come from lower half of
2106/// V1 (and in order), and the upper half elements should come from the upper
2107/// half of V2 (and in order). And since V1 will become the source of the
2108/// MOVLP, it must be either a vector load or a scalar load to vector.
2109static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2110 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2111 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002112
Evan Cheng533a0aa2006-04-19 20:35:22 +00002113 unsigned NumElems = Mask->getNumOperands();
2114 if (NumElems != 2 && NumElems != 4)
2115 return false;
2116 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2117 if (!isUndefOrEqual(Mask->getOperand(i), i))
2118 return false;
2119 for (unsigned i = NumElems/2; i != NumElems; ++i)
2120 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2121 return false;
2122 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002123}
2124
Evan Cheng39623da2006-04-20 08:58:49 +00002125/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2126/// all the same.
2127static bool isSplatVector(SDNode *N) {
2128 if (N->getOpcode() != ISD::BUILD_VECTOR)
2129 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002130
Evan Cheng39623da2006-04-20 08:58:49 +00002131 SDOperand SplatValue = N->getOperand(0);
2132 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2133 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002134 return false;
2135 return true;
2136}
2137
Evan Cheng39623da2006-04-20 08:58:49 +00002138/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2139/// that point to V2 points to its first element.
2140static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2141 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2142
2143 bool Changed = false;
2144 std::vector<SDOperand> MaskVec;
2145 unsigned NumElems = Mask.getNumOperands();
2146 for (unsigned i = 0; i != NumElems; ++i) {
2147 SDOperand Arg = Mask.getOperand(i);
2148 if (Arg.getOpcode() != ISD::UNDEF) {
2149 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2150 if (Val > NumElems) {
2151 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2152 Changed = true;
2153 }
2154 }
2155 MaskVec.push_back(Arg);
2156 }
2157
2158 if (Changed)
2159 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2160 return Mask;
2161}
2162
Evan Cheng017dcc62006-04-21 01:05:10 +00002163/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2164/// operation of specified width.
2165static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002166 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2167 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2168
2169 std::vector<SDOperand> MaskVec;
2170 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2171 for (unsigned i = 1; i != NumElems; ++i)
2172 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2173 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2174}
2175
Evan Chengc575ca22006-04-17 20:43:08 +00002176/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2177/// of specified width.
2178static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2179 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2180 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2181 std::vector<SDOperand> MaskVec;
2182 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2183 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2184 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2185 }
2186 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2187}
2188
Evan Cheng39623da2006-04-20 08:58:49 +00002189/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2190/// of specified width.
2191static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2192 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2193 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2194 unsigned Half = NumElems/2;
2195 std::vector<SDOperand> MaskVec;
2196 for (unsigned i = 0; i != Half; ++i) {
2197 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2198 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2199 }
2200 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2201}
2202
Evan Cheng017dcc62006-04-21 01:05:10 +00002203/// getZeroVector - Returns a vector of specified type with all zero elements.
2204///
2205static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2206 assert(MVT::isVector(VT) && "Expected a vector type");
2207 unsigned NumElems = getVectorNumElements(VT);
2208 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2209 bool isFP = MVT::isFloatingPoint(EVT);
2210 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2211 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2212 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2213}
2214
Evan Chengc575ca22006-04-17 20:43:08 +00002215/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2216///
2217static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2218 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002219 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002220 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002221 unsigned NumElems = Mask.getNumOperands();
2222 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002223 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002224 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002225 NumElems >>= 1;
2226 }
2227 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2228
2229 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002230 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002231 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002232 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002233 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2234}
2235
Evan Cheng017dcc62006-04-21 01:05:10 +00002236/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2237/// constant +0.0.
2238static inline bool isZeroNode(SDOperand Elt) {
2239 return ((isa<ConstantSDNode>(Elt) &&
2240 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2241 (isa<ConstantFPSDNode>(Elt) &&
2242 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2243}
2244
Evan Chengba05f722006-04-21 23:03:30 +00002245/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2246/// vector and zero or undef vector.
2247static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002248 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002249 bool isZero, SelectionDAG &DAG) {
2250 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002251 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2252 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2253 SDOperand Zero = DAG.getConstant(0, EVT);
2254 std::vector<SDOperand> MaskVec(NumElems, Zero);
2255 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2256 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Chengba05f722006-04-21 23:03:30 +00002257 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002258}
2259
Evan Chengc78d3b42006-04-24 18:01:45 +00002260/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2261///
2262static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2263 unsigned NumNonZero, unsigned NumZero,
2264 SelectionDAG &DAG) {
2265 if (NumNonZero > 8)
2266 return SDOperand();
2267
2268 SDOperand V(0, 0);
2269 bool First = true;
2270 for (unsigned i = 0; i < 16; ++i) {
2271 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2272 if (ThisIsNonZero && First) {
2273 if (NumZero)
2274 V = getZeroVector(MVT::v8i16, DAG);
2275 else
2276 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2277 First = false;
2278 }
2279
2280 if ((i & 1) != 0) {
2281 SDOperand ThisElt(0, 0), LastElt(0, 0);
2282 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2283 if (LastIsNonZero) {
2284 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2285 }
2286 if (ThisIsNonZero) {
2287 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2288 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2289 ThisElt, DAG.getConstant(8, MVT::i8));
2290 if (LastIsNonZero)
2291 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2292 } else
2293 ThisElt = LastElt;
2294
2295 if (ThisElt.Val)
2296 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2297 DAG.getConstant(i/2, MVT::i32));
2298 }
2299 }
2300
2301 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2302}
2303
2304/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2305///
2306static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2307 unsigned NumNonZero, unsigned NumZero,
2308 SelectionDAG &DAG) {
2309 if (NumNonZero > 4)
2310 return SDOperand();
2311
2312 SDOperand V(0, 0);
2313 bool First = true;
2314 for (unsigned i = 0; i < 8; ++i) {
2315 bool isNonZero = (NonZeros & (1 << i)) != 0;
2316 if (isNonZero) {
2317 if (First) {
2318 if (NumZero)
2319 V = getZeroVector(MVT::v8i16, DAG);
2320 else
2321 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2322 First = false;
2323 }
2324 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2325 DAG.getConstant(i, MVT::i32));
2326 }
2327 }
2328
2329 return V;
2330}
2331
Evan Cheng0db9fe62006-04-25 20:13:52 +00002332SDOperand
2333X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2334 // All zero's are handled with pxor.
2335 if (ISD::isBuildVectorAllZeros(Op.Val))
2336 return Op;
2337
2338 // All one's are handled with pcmpeqd.
2339 if (ISD::isBuildVectorAllOnes(Op.Val))
2340 return Op;
2341
2342 MVT::ValueType VT = Op.getValueType();
2343 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2344 unsigned EVTBits = MVT::getSizeInBits(EVT);
2345
2346 unsigned NumElems = Op.getNumOperands();
2347 unsigned NumZero = 0;
2348 unsigned NumNonZero = 0;
2349 unsigned NonZeros = 0;
2350 std::set<SDOperand> Values;
2351 for (unsigned i = 0; i < NumElems; ++i) {
2352 SDOperand Elt = Op.getOperand(i);
2353 if (Elt.getOpcode() != ISD::UNDEF) {
2354 Values.insert(Elt);
2355 if (isZeroNode(Elt))
2356 NumZero++;
2357 else {
2358 NonZeros |= (1 << i);
2359 NumNonZero++;
2360 }
2361 }
2362 }
2363
2364 if (NumNonZero == 0)
2365 // Must be a mix of zero and undef. Return a zero vector.
2366 return getZeroVector(VT, DAG);
2367
2368 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2369 if (Values.size() == 1)
2370 return SDOperand();
2371
2372 // Special case for single non-zero element.
2373 if (NumNonZero == 1) {
2374 unsigned Idx = CountTrailingZeros_32(NonZeros);
2375 SDOperand Item = Op.getOperand(Idx);
2376 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2377 if (Idx == 0)
2378 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2379 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2380 NumZero > 0, DAG);
2381
2382 if (EVTBits == 32) {
2383 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2384 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2385 DAG);
2386 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2387 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2388 std::vector<SDOperand> MaskVec;
2389 for (unsigned i = 0; i < NumElems; i++)
2390 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2391 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2392 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2393 DAG.getNode(ISD::UNDEF, VT), Mask);
2394 }
2395 }
2396
2397 // Let legalizer expand 2-widde build_vector's.
2398 if (EVTBits == 64)
2399 return SDOperand();
2400
2401 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2402 if (EVTBits == 8) {
2403 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2404 if (V.Val) return V;
2405 }
2406
2407 if (EVTBits == 16) {
2408 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2409 if (V.Val) return V;
2410 }
2411
2412 // If element VT is == 32 bits, turn it into a number of shuffles.
2413 std::vector<SDOperand> V(NumElems);
2414 if (NumElems == 4 && NumZero > 0) {
2415 for (unsigned i = 0; i < 4; ++i) {
2416 bool isZero = !(NonZeros & (1 << i));
2417 if (isZero)
2418 V[i] = getZeroVector(VT, DAG);
2419 else
2420 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2421 }
2422
2423 for (unsigned i = 0; i < 2; ++i) {
2424 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2425 default: break;
2426 case 0:
2427 V[i] = V[i*2]; // Must be a zero vector.
2428 break;
2429 case 1:
2430 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2431 getMOVLMask(NumElems, DAG));
2432 break;
2433 case 2:
2434 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2435 getMOVLMask(NumElems, DAG));
2436 break;
2437 case 3:
2438 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2439 getUnpacklMask(NumElems, DAG));
2440 break;
2441 }
2442 }
2443
Evan Cheng069287d2006-05-16 07:21:53 +00002444 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002445 // clears the upper bits.
2446 // FIXME: we can do the same for v4f32 case when we know both parts of
2447 // the lower half come from scalar_to_vector (loadf32). We should do
2448 // that in post legalizer dag combiner with target specific hooks.
2449 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2450 return V[0];
2451 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2452 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2453 std::vector<SDOperand> MaskVec;
2454 bool Reverse = (NonZeros & 0x3) == 2;
2455 for (unsigned i = 0; i < 2; ++i)
2456 if (Reverse)
2457 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2458 else
2459 MaskVec.push_back(DAG.getConstant(i, EVT));
2460 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2461 for (unsigned i = 0; i < 2; ++i)
2462 if (Reverse)
2463 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2464 else
2465 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2466 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2467 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2468 }
2469
2470 if (Values.size() > 2) {
2471 // Expand into a number of unpckl*.
2472 // e.g. for v4f32
2473 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2474 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2475 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2476 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2477 for (unsigned i = 0; i < NumElems; ++i)
2478 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2479 NumElems >>= 1;
2480 while (NumElems != 0) {
2481 for (unsigned i = 0; i < NumElems; ++i)
2482 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2483 UnpckMask);
2484 NumElems >>= 1;
2485 }
2486 return V[0];
2487 }
2488
2489 return SDOperand();
2490}
2491
2492SDOperand
2493X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2494 SDOperand V1 = Op.getOperand(0);
2495 SDOperand V2 = Op.getOperand(1);
2496 SDOperand PermMask = Op.getOperand(2);
2497 MVT::ValueType VT = Op.getValueType();
2498 unsigned NumElems = PermMask.getNumOperands();
2499 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2500 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2501
2502 if (isSplatMask(PermMask.Val)) {
2503 if (NumElems <= 4) return Op;
2504 // Promote it to a v4i32 splat.
2505 return PromoteSplat(Op, DAG);
2506 }
2507
2508 if (X86::isMOVLMask(PermMask.Val))
2509 return (V1IsUndef) ? V2 : Op;
2510
2511 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2512 X86::isMOVSLDUPMask(PermMask.Val) ||
2513 X86::isMOVHLPSMask(PermMask.Val) ||
2514 X86::isMOVHPMask(PermMask.Val) ||
2515 X86::isMOVLPMask(PermMask.Val))
2516 return Op;
2517
2518 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2519 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2520 return CommuteVectorShuffle(Op, DAG);
2521
2522 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2523 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2524 if (V1IsSplat && !V2IsSplat) {
2525 Op = CommuteVectorShuffle(Op, DAG);
2526 V1 = Op.getOperand(0);
2527 V2 = Op.getOperand(1);
2528 PermMask = Op.getOperand(2);
2529 V2IsSplat = true;
2530 }
2531
2532 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2533 if (V2IsUndef) return V1;
2534 Op = CommuteVectorShuffle(Op, DAG);
2535 V1 = Op.getOperand(0);
2536 V2 = Op.getOperand(1);
2537 PermMask = Op.getOperand(2);
2538 if (V2IsSplat) {
2539 // V2 is a splat, so the mask may be malformed. That is, it may point
2540 // to any V2 element. The instruction selectior won't like this. Get
2541 // a corrected mask and commute to form a proper MOVS{S|D}.
2542 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2543 if (NewMask.Val != PermMask.Val)
2544 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2545 }
2546 return Op;
2547 }
2548
2549 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2550 X86::isUNPCKLMask(PermMask.Val) ||
2551 X86::isUNPCKHMask(PermMask.Val))
2552 return Op;
2553
2554 if (V2IsSplat) {
2555 // Normalize mask so all entries that point to V2 points to its first
2556 // element then try to match unpck{h|l} again. If match, return a
2557 // new vector_shuffle with the corrected mask.
2558 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2559 if (NewMask.Val != PermMask.Val) {
2560 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2561 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2562 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2563 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2564 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2565 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2566 }
2567 }
2568 }
2569
2570 // Normalize the node to match x86 shuffle ops if needed
2571 if (V2.getOpcode() != ISD::UNDEF)
2572 if (isCommutedSHUFP(PermMask.Val)) {
2573 Op = CommuteVectorShuffle(Op, DAG);
2574 V1 = Op.getOperand(0);
2575 V2 = Op.getOperand(1);
2576 PermMask = Op.getOperand(2);
2577 }
2578
2579 // If VT is integer, try PSHUF* first, then SHUFP*.
2580 if (MVT::isInteger(VT)) {
2581 if (X86::isPSHUFDMask(PermMask.Val) ||
2582 X86::isPSHUFHWMask(PermMask.Val) ||
2583 X86::isPSHUFLWMask(PermMask.Val)) {
2584 if (V2.getOpcode() != ISD::UNDEF)
2585 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2586 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2587 return Op;
2588 }
2589
2590 if (X86::isSHUFPMask(PermMask.Val))
2591 return Op;
2592
2593 // Handle v8i16 shuffle high / low shuffle node pair.
2594 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2595 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2596 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2597 std::vector<SDOperand> MaskVec;
2598 for (unsigned i = 0; i != 4; ++i)
2599 MaskVec.push_back(PermMask.getOperand(i));
2600 for (unsigned i = 4; i != 8; ++i)
2601 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2602 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2603 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2604 MaskVec.clear();
2605 for (unsigned i = 0; i != 4; ++i)
2606 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2607 for (unsigned i = 4; i != 8; ++i)
2608 MaskVec.push_back(PermMask.getOperand(i));
2609 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2610 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2611 }
2612 } else {
2613 // Floating point cases in the other order.
2614 if (X86::isSHUFPMask(PermMask.Val))
2615 return Op;
2616 if (X86::isPSHUFDMask(PermMask.Val) ||
2617 X86::isPSHUFHWMask(PermMask.Val) ||
2618 X86::isPSHUFLWMask(PermMask.Val)) {
2619 if (V2.getOpcode() != ISD::UNDEF)
2620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2621 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2622 return Op;
2623 }
2624 }
2625
2626 if (NumElems == 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002627 MVT::ValueType MaskVT = PermMask.getValueType();
2628 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng43f3bd32006-04-28 07:03:38 +00002629 std::vector<std::pair<int, int> > Locs;
2630 Locs.reserve(NumElems);
2631 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2632 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2633 unsigned NumHi = 0;
2634 unsigned NumLo = 0;
2635 // If no more than two elements come from either vector. This can be
2636 // implemented with two shuffles. First shuffle gather the elements.
2637 // The second shuffle, which takes the first shuffle as both of its
2638 // vector operands, put the elements into the right order.
2639 for (unsigned i = 0; i != NumElems; ++i) {
2640 SDOperand Elt = PermMask.getOperand(i);
2641 if (Elt.getOpcode() == ISD::UNDEF) {
2642 Locs[i] = std::make_pair(-1, -1);
2643 } else {
2644 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2645 if (Val < NumElems) {
2646 Locs[i] = std::make_pair(0, NumLo);
2647 Mask1[NumLo] = Elt;
2648 NumLo++;
2649 } else {
2650 Locs[i] = std::make_pair(1, NumHi);
2651 if (2+NumHi < NumElems)
2652 Mask1[2+NumHi] = Elt;
2653 NumHi++;
2654 }
2655 }
2656 }
2657 if (NumLo <= 2 && NumHi <= 2) {
2658 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2659 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2660 for (unsigned i = 0; i != NumElems; ++i) {
2661 if (Locs[i].first == -1)
2662 continue;
2663 else {
2664 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2665 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2666 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2667 }
2668 }
2669
2670 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2671 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2672 }
2673
2674 // Break it into (shuffle shuffle_hi, shuffle_lo).
2675 Locs.clear();
Evan Cheng0db9fe62006-04-25 20:13:52 +00002676 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2677 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2678 std::vector<SDOperand> *MaskPtr = &LoMask;
2679 unsigned MaskIdx = 0;
2680 unsigned LoIdx = 0;
2681 unsigned HiIdx = NumElems/2;
2682 for (unsigned i = 0; i != NumElems; ++i) {
2683 if (i == NumElems/2) {
2684 MaskPtr = &HiMask;
2685 MaskIdx = 1;
2686 LoIdx = 0;
2687 HiIdx = NumElems/2;
2688 }
2689 SDOperand Elt = PermMask.getOperand(i);
2690 if (Elt.getOpcode() == ISD::UNDEF) {
2691 Locs[i] = std::make_pair(-1, -1);
2692 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2693 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2694 (*MaskPtr)[LoIdx] = Elt;
2695 LoIdx++;
2696 } else {
2697 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2698 (*MaskPtr)[HiIdx] = Elt;
2699 HiIdx++;
2700 }
2701 }
2702
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002703 SDOperand LoShuffle =
2704 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2705 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2706 SDOperand HiShuffle =
2707 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2708 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002709 std::vector<SDOperand> MaskOps;
2710 for (unsigned i = 0; i != NumElems; ++i) {
2711 if (Locs[i].first == -1) {
2712 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2713 } else {
2714 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2715 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2716 }
2717 }
2718 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2719 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2720 }
2721
2722 return SDOperand();
2723}
2724
2725SDOperand
2726X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2727 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2728 return SDOperand();
2729
2730 MVT::ValueType VT = Op.getValueType();
2731 // TODO: handle v16i8.
2732 if (MVT::getSizeInBits(VT) == 16) {
2733 // Transform it so it match pextrw which produces a 32-bit result.
2734 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2735 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2736 Op.getOperand(0), Op.getOperand(1));
2737 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2738 DAG.getValueType(VT));
2739 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2740 } else if (MVT::getSizeInBits(VT) == 32) {
2741 SDOperand Vec = Op.getOperand(0);
2742 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2743 if (Idx == 0)
2744 return Op;
2745
2746 // SHUFPS the element to the lowest double word, then movss.
2747 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2748 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2749 MVT::getVectorBaseType(MaskVT));
2750 std::vector<SDOperand> IdxVec;
2751 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2752 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2753 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2754 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2755 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2756 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2757 Vec, Vec, Mask);
2758 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2759 DAG.getConstant(0, MVT::i32));
2760 } else if (MVT::getSizeInBits(VT) == 64) {
2761 SDOperand Vec = Op.getOperand(0);
2762 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2763 if (Idx == 0)
2764 return Op;
2765
2766 // UNPCKHPD the element to the lowest double word, then movsd.
2767 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2768 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2769 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2770 std::vector<SDOperand> IdxVec;
2771 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2772 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2773 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2774 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2775 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2776 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2777 DAG.getConstant(0, MVT::i32));
2778 }
2779
2780 return SDOperand();
2781}
2782
2783SDOperand
2784X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00002785 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00002786 // as its second argument.
2787 MVT::ValueType VT = Op.getValueType();
2788 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2789 SDOperand N0 = Op.getOperand(0);
2790 SDOperand N1 = Op.getOperand(1);
2791 SDOperand N2 = Op.getOperand(2);
2792 if (MVT::getSizeInBits(BaseVT) == 16) {
2793 if (N1.getValueType() != MVT::i32)
2794 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2795 if (N2.getValueType() != MVT::i32)
2796 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2797 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2798 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2799 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2800 if (Idx == 0) {
2801 // Use a movss.
2802 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2803 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2804 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2805 std::vector<SDOperand> MaskVec;
2806 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2807 for (unsigned i = 1; i <= 3; ++i)
2808 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2809 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2810 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2811 } else {
2812 // Use two pinsrw instructions to insert a 32 bit value.
2813 Idx <<= 1;
2814 if (MVT::isFloatingPoint(N1.getValueType())) {
2815 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng069287d2006-05-16 07:21:53 +00002816 // Just load directly from f32mem to GR32.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002817 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2818 N1.getOperand(2));
2819 } else {
2820 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2821 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2822 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2823 DAG.getConstant(0, MVT::i32));
2824 }
2825 }
2826 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2827 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2828 DAG.getConstant(Idx, MVT::i32));
2829 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2830 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2831 DAG.getConstant(Idx+1, MVT::i32));
2832 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2833 }
2834 }
2835
2836 return SDOperand();
2837}
2838
2839SDOperand
2840X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2841 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2842 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2843}
2844
2845// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2846// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2847// one of the above mentioned nodes. It has to be wrapped because otherwise
2848// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2849// be used to form addressing mode. These wrapped nodes will be selected
2850// into MOV32ri.
2851SDOperand
2852X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2853 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2854 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2855 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2856 CP->getAlignment()));
2857 if (Subtarget->isTargetDarwin()) {
2858 // With PIC, the address is actually $g + Offset.
2859 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2860 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2861 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2862 }
2863
2864 return Result;
2865}
2866
2867SDOperand
2868X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2869 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2870 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002871 DAG.getTargetGlobalAddress(GV,
2872 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002873 if (Subtarget->isTargetDarwin()) {
2874 // With PIC, the address is actually $g + Offset.
2875 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2876 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002877 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2878 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002879
2880 // For Darwin, external and weak symbols are indirect, so we want to load
2881 // the value at address GV, not the value of GV itself. This means that
2882 // the GlobalAddress must be in the base or index register of the address,
2883 // not the GV offset field.
2884 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2885 DarwinGVRequiresExtraLoad(GV))
2886 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2887 Result, DAG.getSrcValue(NULL));
2888 }
2889
2890 return Result;
2891}
2892
2893SDOperand
2894X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2895 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2896 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002897 DAG.getTargetExternalSymbol(Sym,
2898 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002899 if (Subtarget->isTargetDarwin()) {
2900 // With PIC, the address is actually $g + Offset.
2901 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2902 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002903 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2904 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002905 }
2906
2907 return Result;
2908}
2909
2910SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00002911 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2912 "Not an i64 shift!");
2913 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2914 SDOperand ShOpLo = Op.getOperand(0);
2915 SDOperand ShOpHi = Op.getOperand(1);
2916 SDOperand ShAmt = Op.getOperand(2);
2917 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00002918 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00002919 : DAG.getConstant(0, MVT::i32);
2920
2921 SDOperand Tmp2, Tmp3;
2922 if (Op.getOpcode() == ISD::SHL_PARTS) {
2923 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2924 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2925 } else {
2926 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00002927 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00002928 }
2929
2930 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2931 ShAmt, DAG.getConstant(32, MVT::i8));
2932
2933 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00002934 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00002935
2936 std::vector<MVT::ValueType> Tys;
2937 Tys.push_back(MVT::i32);
2938 Tys.push_back(MVT::Flag);
2939 std::vector<SDOperand> Ops;
2940 if (Op.getOpcode() == ISD::SHL_PARTS) {
2941 Ops.push_back(Tmp2);
2942 Ops.push_back(Tmp3);
2943 Ops.push_back(CC);
2944 Ops.push_back(InFlag);
2945 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2946 InFlag = Hi.getValue(1);
2947
2948 Ops.clear();
2949 Ops.push_back(Tmp3);
2950 Ops.push_back(Tmp1);
2951 Ops.push_back(CC);
2952 Ops.push_back(InFlag);
2953 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2954 } else {
2955 Ops.push_back(Tmp2);
2956 Ops.push_back(Tmp3);
2957 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00002958 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00002959 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2960 InFlag = Lo.getValue(1);
2961
2962 Ops.clear();
2963 Ops.push_back(Tmp3);
2964 Ops.push_back(Tmp1);
2965 Ops.push_back(CC);
2966 Ops.push_back(InFlag);
2967 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2968 }
2969
2970 Tys.clear();
2971 Tys.push_back(MVT::i32);
2972 Tys.push_back(MVT::i32);
2973 Ops.clear();
2974 Ops.push_back(Lo);
2975 Ops.push_back(Hi);
2976 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002977}
Evan Chenga3195e82006-01-12 22:54:21 +00002978
Evan Cheng0db9fe62006-04-25 20:13:52 +00002979SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2980 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2981 Op.getOperand(0).getValueType() >= MVT::i16 &&
2982 "Unknown SINT_TO_FP to lower!");
2983
2984 SDOperand Result;
2985 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2986 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2987 MachineFunction &MF = DAG.getMachineFunction();
2988 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2989 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2990 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2991 DAG.getEntryNode(), Op.getOperand(0),
2992 StackSlot, DAG.getSrcValue(NULL));
2993
2994 // Build the FILD
2995 std::vector<MVT::ValueType> Tys;
2996 Tys.push_back(MVT::f64);
2997 Tys.push_back(MVT::Other);
2998 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2999 std::vector<SDOperand> Ops;
3000 Ops.push_back(Chain);
3001 Ops.push_back(StackSlot);
3002 Ops.push_back(DAG.getValueType(SrcVT));
3003 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3004 Tys, Ops);
3005
3006 if (X86ScalarSSE) {
3007 Chain = Result.getValue(1);
3008 SDOperand InFlag = Result.getValue(2);
3009
3010 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3011 // shouldn't be necessary except that RFP cannot be live across
3012 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003013 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003014 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003015 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00003016 std::vector<MVT::ValueType> Tys;
Evan Cheng6dab0532006-01-30 08:02:57 +00003017 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003018 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003019 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003020 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003021 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003022 Ops.push_back(DAG.getValueType(Op.getValueType()));
3023 Ops.push_back(InFlag);
3024 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3025 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3026 DAG.getSrcValue(NULL));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003027 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003028
Evan Cheng0db9fe62006-04-25 20:13:52 +00003029 return Result;
3030}
3031
3032SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3033 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3034 "Unknown FP_TO_SINT to lower!");
3035 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3036 // stack slot.
3037 MachineFunction &MF = DAG.getMachineFunction();
3038 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3039 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3040 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3041
3042 unsigned Opc;
3043 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003044 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3045 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3046 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3047 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003048 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003049
Evan Cheng0db9fe62006-04-25 20:13:52 +00003050 SDOperand Chain = DAG.getEntryNode();
3051 SDOperand Value = Op.getOperand(0);
3052 if (X86ScalarSSE) {
3053 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3054 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3055 DAG.getSrcValue(0));
3056 std::vector<MVT::ValueType> Tys;
3057 Tys.push_back(MVT::f64);
3058 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003059 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00003060 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003061 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003062 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3063 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3064 Chain = Value.getValue(1);
3065 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3066 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3067 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003068
Evan Cheng0db9fe62006-04-25 20:13:52 +00003069 // Build the FP_TO_INT*_IN_MEM
3070 std::vector<SDOperand> Ops;
3071 Ops.push_back(Chain);
3072 Ops.push_back(Value);
3073 Ops.push_back(StackSlot);
3074 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Chengd9558e02006-01-06 00:43:03 +00003075
Evan Cheng0db9fe62006-04-25 20:13:52 +00003076 // Load the result.
3077 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3078 DAG.getSrcValue(NULL));
3079}
3080
3081SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3082 MVT::ValueType VT = Op.getValueType();
3083 const Type *OpNTy = MVT::getTypeForValueType(VT);
3084 std::vector<Constant*> CV;
3085 if (VT == MVT::f64) {
3086 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3087 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3088 } else {
3089 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3090 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3091 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3092 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3093 }
3094 Constant *CS = ConstantStruct::get(CV);
3095 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3096 SDOperand Mask
3097 = DAG.getNode(X86ISD::LOAD_PACK,
3098 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3099 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3100}
3101
3102SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3103 MVT::ValueType VT = Op.getValueType();
3104 const Type *OpNTy = MVT::getTypeForValueType(VT);
3105 std::vector<Constant*> CV;
3106 if (VT == MVT::f64) {
3107 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3108 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3109 } else {
3110 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3111 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3112 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3113 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3114 }
3115 Constant *CS = ConstantStruct::get(CV);
3116 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3117 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3118 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3119 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3120}
3121
3122SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3123 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3124 SDOperand Cond;
3125 SDOperand CC = Op.getOperand(2);
3126 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3127 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3128 bool Flip;
3129 unsigned X86CC;
3130 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3131 if (Flip)
3132 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3133 Op.getOperand(1), Op.getOperand(0));
3134 else
Evan Cheng6dfa9992006-01-30 23:41:35 +00003135 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3136 Op.getOperand(0), Op.getOperand(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003137 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3138 DAG.getConstant(X86CC, MVT::i8), Cond);
3139 } else {
3140 assert(isFP && "Illegal integer SetCC!");
3141
3142 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3143 Op.getOperand(0), Op.getOperand(1));
3144 std::vector<MVT::ValueType> Tys;
3145 std::vector<SDOperand> Ops;
3146 switch (SetCCOpcode) {
Evan Chengd9558e02006-01-06 00:43:03 +00003147 default: assert(false && "Illegal floating point SetCC!");
3148 case ISD::SETOEQ: { // !PF & ZF
3149 Tys.push_back(MVT::i8);
3150 Tys.push_back(MVT::Flag);
3151 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3152 Ops.push_back(Cond);
3153 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3154 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3155 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3156 Tmp1.getValue(1));
3157 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3158 }
Evan Chengd9558e02006-01-06 00:43:03 +00003159 case ISD::SETUNE: { // PF | !ZF
3160 Tys.push_back(MVT::i8);
3161 Tys.push_back(MVT::Flag);
3162 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3163 Ops.push_back(Cond);
3164 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3165 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3166 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3167 Tmp1.getValue(1));
3168 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3169 }
Evan Chengd9558e02006-01-06 00:43:03 +00003170 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003171 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003172}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003173
Evan Cheng0db9fe62006-04-25 20:13:52 +00003174SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3175 MVT::ValueType VT = Op.getValueType();
3176 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3177 bool addTest = false;
3178 SDOperand Op0 = Op.getOperand(0);
3179 SDOperand Cond, CC;
3180 if (Op0.getOpcode() == ISD::SETCC)
3181 Op0 = LowerOperation(Op0, DAG);
Evan Cheng9bba8942006-01-26 02:13:10 +00003182
Evan Cheng0db9fe62006-04-25 20:13:52 +00003183 if (Op0.getOpcode() == X86ISD::SETCC) {
3184 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3185 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3186 // have another use it will be eliminated.
3187 // If the X86ISD::SETCC has more than one use, then it's probably better
3188 // to use a test instead of duplicating the X86ISD::CMP (for register
3189 // pressure reason).
3190 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3191 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3192 CmpOpc == X86ISD::UCOMI) {
3193 if (!Op0.hasOneUse()) {
3194 std::vector<MVT::ValueType> Tys;
3195 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3196 Tys.push_back(Op0.Val->getValueType(i));
3197 std::vector<SDOperand> Ops;
3198 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3199 Ops.push_back(Op0.getOperand(i));
3200 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3201 }
3202
3203 CC = Op0.getOperand(0);
3204 Cond = Op0.getOperand(1);
3205 // Make a copy as flag result cannot be used by more than one.
3206 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3207 Cond.getOperand(0), Cond.getOperand(1));
3208 addTest =
3209 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00003210 } else
3211 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003212 } else
3213 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00003214
Evan Cheng0db9fe62006-04-25 20:13:52 +00003215 if (addTest) {
3216 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3217 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00003218 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003219
Evan Cheng0db9fe62006-04-25 20:13:52 +00003220 std::vector<MVT::ValueType> Tys;
3221 Tys.push_back(Op.getValueType());
3222 Tys.push_back(MVT::Flag);
3223 std::vector<SDOperand> Ops;
3224 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3225 // condition is true.
3226 Ops.push_back(Op.getOperand(2));
3227 Ops.push_back(Op.getOperand(1));
3228 Ops.push_back(CC);
3229 Ops.push_back(Cond);
3230 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3231}
Evan Cheng9bba8942006-01-26 02:13:10 +00003232
Evan Cheng0db9fe62006-04-25 20:13:52 +00003233SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3234 bool addTest = false;
3235 SDOperand Cond = Op.getOperand(1);
3236 SDOperand Dest = Op.getOperand(2);
3237 SDOperand CC;
3238 if (Cond.getOpcode() == ISD::SETCC)
3239 Cond = LowerOperation(Cond, DAG);
3240
3241 if (Cond.getOpcode() == X86ISD::SETCC) {
3242 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3243 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3244 // have another use it will be eliminated.
3245 // If the X86ISD::SETCC has more than one use, then it's probably better
3246 // to use a test instead of duplicating the X86ISD::CMP (for register
3247 // pressure reason).
3248 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3249 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3250 CmpOpc == X86ISD::UCOMI) {
3251 if (!Cond.hasOneUse()) {
3252 std::vector<MVT::ValueType> Tys;
3253 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3254 Tys.push_back(Cond.Val->getValueType(i));
3255 std::vector<SDOperand> Ops;
3256 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3257 Ops.push_back(Cond.getOperand(i));
3258 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3259 }
3260
3261 CC = Cond.getOperand(0);
3262 Cond = Cond.getOperand(1);
3263 // Make a copy as flag result cannot be used by more than one.
3264 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3265 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00003266 } else
3267 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003268 } else
3269 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00003270
Evan Cheng0db9fe62006-04-25 20:13:52 +00003271 if (addTest) {
3272 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3273 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng898101c2005-12-19 23:12:38 +00003274 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003275 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3276 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3277}
Evan Cheng67f92a72006-01-11 22:15:48 +00003278
Evan Cheng0db9fe62006-04-25 20:13:52 +00003279SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3280 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3281 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3282 DAG.getTargetJumpTable(JT->getIndex(),
3283 getPointerTy()));
3284 if (Subtarget->isTargetDarwin()) {
3285 // With PIC, the address is actually $g + Offset.
3286 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3287 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003288 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3289 Result);
Evan Cheng67f92a72006-01-11 22:15:48 +00003290 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00003291
Evan Cheng0db9fe62006-04-25 20:13:52 +00003292 return Result;
3293}
Evan Cheng7ccced62006-02-18 00:15:05 +00003294
Evan Cheng32fe1032006-05-25 00:59:30 +00003295SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3296 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3297 if (CallingConv == CallingConv::Fast && EnableFastCC)
3298 return LowerFastCCCallTo(Op, DAG);
3299 else
3300 return LowerCCCCallTo(Op, DAG);
3301}
3302
Evan Cheng0db9fe62006-04-25 20:13:52 +00003303SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3304 SDOperand Copy;
Nate Begemanee625572006-01-27 21:09:22 +00003305
Evan Cheng0db9fe62006-04-25 20:13:52 +00003306 switch(Op.getNumOperands()) {
Nate Begemanee625572006-01-27 21:09:22 +00003307 default:
3308 assert(0 && "Do not know how to return this many arguments!");
3309 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003310 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00003311 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003312 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begemanee625572006-01-27 21:09:22 +00003313 case 2: {
3314 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003315
3316 if (MVT::isVector(ArgVT)) {
3317 // Integer or FP vector result -> XMM0.
3318 if (DAG.getMachineFunction().liveout_empty())
3319 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3320 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3321 SDOperand());
3322 } else if (MVT::isInteger(ArgVT)) {
3323 // Integer result -> EAX
3324 if (DAG.getMachineFunction().liveout_empty())
3325 DAG.getMachineFunction().addLiveOut(X86::EAX);
3326
Nate Begemanee625572006-01-27 21:09:22 +00003327 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3328 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00003329 } else if (!X86ScalarSSE) {
3330 // FP return with fp-stack value.
3331 if (DAG.getMachineFunction().liveout_empty())
3332 DAG.getMachineFunction().addLiveOut(X86::ST0);
3333
Nate Begemanee625572006-01-27 21:09:22 +00003334 std::vector<MVT::ValueType> Tys;
3335 Tys.push_back(MVT::Other);
3336 Tys.push_back(MVT::Flag);
3337 std::vector<SDOperand> Ops;
3338 Ops.push_back(Op.getOperand(0));
3339 Ops.push_back(Op.getOperand(1));
3340 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3341 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003342 // FP return with ScalarSSE (return on fp-stack).
3343 if (DAG.getMachineFunction().liveout_empty())
3344 DAG.getMachineFunction().addLiveOut(X86::ST0);
3345
Evan Cheng0d084c92006-02-01 00:20:21 +00003346 SDOperand MemLoc;
3347 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003348 SDOperand Value = Op.getOperand(1);
3349
Evan Cheng760df292006-02-01 01:19:32 +00003350 if (Value.getOpcode() == ISD::LOAD &&
3351 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00003352 Chain = Value.getOperand(0);
3353 MemLoc = Value.getOperand(1);
3354 } else {
3355 // Spill the value to memory and reload it into top of stack.
3356 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3357 MachineFunction &MF = DAG.getMachineFunction();
3358 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3359 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3360 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3361 Value, MemLoc, DAG.getSrcValue(0));
3362 }
Nate Begemanee625572006-01-27 21:09:22 +00003363 std::vector<MVT::ValueType> Tys;
3364 Tys.push_back(MVT::f64);
3365 Tys.push_back(MVT::Other);
3366 std::vector<SDOperand> Ops;
3367 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003368 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00003369 Ops.push_back(DAG.getValueType(ArgVT));
3370 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3371 Tys.clear();
3372 Tys.push_back(MVT::Other);
3373 Tys.push_back(MVT::Flag);
3374 Ops.clear();
3375 Ops.push_back(Copy.getValue(1));
3376 Ops.push_back(Copy);
3377 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3378 }
3379 break;
3380 }
3381 case 3:
Chris Lattnerb2be4032006-04-17 20:32:50 +00003382 if (DAG.getMachineFunction().liveout_empty()) {
3383 DAG.getMachineFunction().addLiveOut(X86::EAX);
3384 DAG.getMachineFunction().addLiveOut(X86::EDX);
3385 }
3386
Nate Begemanee625572006-01-27 21:09:22 +00003387 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3388 SDOperand());
3389 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3390 break;
Nate Begemanee625572006-01-27 21:09:22 +00003391 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003392 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3393 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3394 Copy.getValue(1));
3395}
3396
Evan Cheng1bc78042006-04-26 01:20:17 +00003397SDOperand
3398X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng25caf632006-05-23 21:06:34 +00003399 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3400 if (CC == CallingConv::Fast && EnableFastCC)
3401 return LowerFastCCArguments(Op, DAG);
3402 else
3403 return LowerCCCArguments(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003404}
3405
Evan Cheng0db9fe62006-04-25 20:13:52 +00003406SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3407 SDOperand InFlag(0, 0);
3408 SDOperand Chain = Op.getOperand(0);
3409 unsigned Align =
3410 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3411 if (Align == 0) Align = 1;
3412
3413 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3414 // If not DWORD aligned, call memset if size is less than the threshold.
3415 // It knows how to align to the right boundary first.
3416 if ((Align & 3) != 0 ||
3417 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3418 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003419 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003420 std::vector<std::pair<SDOperand, const Type*> > Args;
3421 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3422 // Extend the ubyte argument to be an int value for the call.
3423 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3424 Args.push_back(std::make_pair(Val, IntPtrTy));
3425 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3426 std::pair<SDOperand,SDOperand> CallResult =
3427 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3428 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3429 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003430 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003431
Evan Cheng0db9fe62006-04-25 20:13:52 +00003432 MVT::ValueType AVT;
3433 SDOperand Count;
3434 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3435 unsigned BytesLeft = 0;
3436 bool TwoRepStos = false;
3437 if (ValC) {
3438 unsigned ValReg;
3439 unsigned Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003440
Evan Cheng0db9fe62006-04-25 20:13:52 +00003441 // If the value is a constant, then we can potentially use larger sets.
3442 switch (Align & 3) {
3443 case 2: // WORD aligned
3444 AVT = MVT::i16;
3445 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3446 BytesLeft = I->getValue() % 2;
3447 Val = (Val << 8) | Val;
3448 ValReg = X86::AX;
3449 break;
3450 case 0: // DWORD aligned
3451 AVT = MVT::i32;
3452 if (I) {
3453 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3454 BytesLeft = I->getValue() % 4;
Evan Cheng80d428c2006-04-19 22:48:17 +00003455 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003456 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3457 DAG.getConstant(2, MVT::i8));
3458 TwoRepStos = true;
Evan Cheng80d428c2006-04-19 22:48:17 +00003459 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003460 Val = (Val << 8) | Val;
3461 Val = (Val << 16) | Val;
3462 ValReg = X86::EAX;
3463 break;
3464 default: // Byte aligned
3465 AVT = MVT::i8;
3466 Count = Op.getOperand(3);
3467 ValReg = X86::AL;
3468 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003469 }
3470
Evan Cheng0db9fe62006-04-25 20:13:52 +00003471 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3472 InFlag);
3473 InFlag = Chain.getValue(1);
3474 } else {
3475 AVT = MVT::i8;
3476 Count = Op.getOperand(3);
3477 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3478 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00003479 }
Evan Chengc78d3b42006-04-24 18:01:45 +00003480
Evan Cheng0db9fe62006-04-25 20:13:52 +00003481 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3482 InFlag = Chain.getValue(1);
3483 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3484 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00003485
Evan Cheng0db9fe62006-04-25 20:13:52 +00003486 std::vector<MVT::ValueType> Tys;
3487 Tys.push_back(MVT::Other);
3488 Tys.push_back(MVT::Flag);
3489 std::vector<SDOperand> Ops;
3490 Ops.push_back(Chain);
3491 Ops.push_back(DAG.getValueType(AVT));
3492 Ops.push_back(InFlag);
3493 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengc78d3b42006-04-24 18:01:45 +00003494
Evan Cheng0db9fe62006-04-25 20:13:52 +00003495 if (TwoRepStos) {
3496 InFlag = Chain.getValue(1);
3497 Count = Op.getOperand(3);
3498 MVT::ValueType CVT = Count.getValueType();
3499 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3500 DAG.getConstant(3, CVT));
3501 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3502 InFlag = Chain.getValue(1);
3503 Tys.clear();
3504 Tys.push_back(MVT::Other);
3505 Tys.push_back(MVT::Flag);
3506 Ops.clear();
3507 Ops.push_back(Chain);
3508 Ops.push_back(DAG.getValueType(MVT::i8));
3509 Ops.push_back(InFlag);
3510 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3511 } else if (BytesLeft) {
3512 // Issue stores for the last 1 - 3 bytes.
3513 SDOperand Value;
3514 unsigned Val = ValC->getValue() & 255;
3515 unsigned Offset = I->getValue() - BytesLeft;
3516 SDOperand DstAddr = Op.getOperand(1);
3517 MVT::ValueType AddrVT = DstAddr.getValueType();
3518 if (BytesLeft >= 2) {
3519 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3520 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3521 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3522 DAG.getConstant(Offset, AddrVT)),
3523 DAG.getSrcValue(NULL));
3524 BytesLeft -= 2;
3525 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00003526 }
3527
Evan Cheng0db9fe62006-04-25 20:13:52 +00003528 if (BytesLeft == 1) {
3529 Value = DAG.getConstant(Val, MVT::i8);
3530 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3531 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3532 DAG.getConstant(Offset, AddrVT)),
3533 DAG.getSrcValue(NULL));
Evan Chengba05f722006-04-21 23:03:30 +00003534 }
Evan Cheng386031a2006-03-24 07:29:27 +00003535 }
Evan Cheng11e15b32006-04-03 20:53:28 +00003536
Evan Cheng0db9fe62006-04-25 20:13:52 +00003537 return Chain;
3538}
Evan Cheng11e15b32006-04-03 20:53:28 +00003539
Evan Cheng0db9fe62006-04-25 20:13:52 +00003540SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3541 SDOperand Chain = Op.getOperand(0);
3542 unsigned Align =
3543 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3544 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00003545
Evan Cheng0db9fe62006-04-25 20:13:52 +00003546 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3547 // If not DWORD aligned, call memcpy if size is less than the threshold.
3548 // It knows how to align to the right boundary first.
3549 if ((Align & 3) != 0 ||
3550 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3551 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003552 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003553 std::vector<std::pair<SDOperand, const Type*> > Args;
3554 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3555 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3556 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3557 std::pair<SDOperand,SDOperand> CallResult =
3558 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3559 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3560 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00003561 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003562
3563 MVT::ValueType AVT;
3564 SDOperand Count;
3565 unsigned BytesLeft = 0;
3566 bool TwoRepMovs = false;
3567 switch (Align & 3) {
3568 case 2: // WORD aligned
3569 AVT = MVT::i16;
3570 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3571 BytesLeft = I->getValue() % 2;
3572 break;
3573 case 0: // DWORD aligned
3574 AVT = MVT::i32;
3575 if (I) {
3576 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3577 BytesLeft = I->getValue() % 4;
Evan Chengcdfc3c82006-04-17 22:45:49 +00003578 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003579 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3580 DAG.getConstant(2, MVT::i8));
3581 TwoRepMovs = true;
Evan Cheng5edb8d22006-04-17 22:04:06 +00003582 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003583 break;
3584 default: // Byte aligned
3585 AVT = MVT::i8;
3586 Count = Op.getOperand(3);
3587 break;
3588 }
3589
3590 SDOperand InFlag(0, 0);
3591 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3592 InFlag = Chain.getValue(1);
3593 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3594 InFlag = Chain.getValue(1);
3595 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3596 InFlag = Chain.getValue(1);
3597
3598 std::vector<MVT::ValueType> Tys;
3599 Tys.push_back(MVT::Other);
3600 Tys.push_back(MVT::Flag);
3601 std::vector<SDOperand> Ops;
3602 Ops.push_back(Chain);
3603 Ops.push_back(DAG.getValueType(AVT));
3604 Ops.push_back(InFlag);
3605 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3606
3607 if (TwoRepMovs) {
3608 InFlag = Chain.getValue(1);
3609 Count = Op.getOperand(3);
3610 MVT::ValueType CVT = Count.getValueType();
3611 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3612 DAG.getConstant(3, CVT));
3613 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3614 InFlag = Chain.getValue(1);
3615 Tys.clear();
3616 Tys.push_back(MVT::Other);
3617 Tys.push_back(MVT::Flag);
3618 Ops.clear();
3619 Ops.push_back(Chain);
3620 Ops.push_back(DAG.getValueType(MVT::i8));
3621 Ops.push_back(InFlag);
3622 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3623 } else if (BytesLeft) {
3624 // Issue loads and stores for the last 1 - 3 bytes.
3625 unsigned Offset = I->getValue() - BytesLeft;
3626 SDOperand DstAddr = Op.getOperand(1);
3627 MVT::ValueType DstVT = DstAddr.getValueType();
3628 SDOperand SrcAddr = Op.getOperand(2);
3629 MVT::ValueType SrcVT = SrcAddr.getValueType();
3630 SDOperand Value;
3631 if (BytesLeft >= 2) {
3632 Value = DAG.getLoad(MVT::i16, Chain,
3633 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3634 DAG.getConstant(Offset, SrcVT)),
3635 DAG.getSrcValue(NULL));
3636 Chain = Value.getValue(1);
3637 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3638 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3639 DAG.getConstant(Offset, DstVT)),
3640 DAG.getSrcValue(NULL));
3641 BytesLeft -= 2;
3642 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00003643 }
3644
Evan Cheng0db9fe62006-04-25 20:13:52 +00003645 if (BytesLeft == 1) {
3646 Value = DAG.getLoad(MVT::i8, Chain,
3647 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3648 DAG.getConstant(Offset, SrcVT)),
3649 DAG.getSrcValue(NULL));
3650 Chain = Value.getValue(1);
3651 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3652 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3653 DAG.getConstant(Offset, DstVT)),
3654 DAG.getSrcValue(NULL));
3655 }
Evan Chengb067a1e2006-03-31 19:22:53 +00003656 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003657
3658 return Chain;
3659}
3660
3661SDOperand
3662X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3663 std::vector<MVT::ValueType> Tys;
3664 Tys.push_back(MVT::Other);
3665 Tys.push_back(MVT::Flag);
3666 std::vector<SDOperand> Ops;
3667 Ops.push_back(Op.getOperand(0));
3668 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3669 Ops.clear();
3670 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3671 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3672 MVT::i32, Ops[0].getValue(2)));
3673 Ops.push_back(Ops[1].getValue(1));
3674 Tys[0] = Tys[1] = MVT::i32;
3675 Tys.push_back(MVT::Other);
3676 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3677}
3678
3679SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3680 // vastart just stores the address of the VarArgsFrameIndex slot into the
3681 // memory location argument.
3682 // FIXME: Replace MVT::i32 with PointerTy
3683 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3684 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3685 Op.getOperand(1), Op.getOperand(2));
3686}
3687
3688SDOperand
3689X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3690 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3691 switch (IntNo) {
3692 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00003693 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694 case Intrinsic::x86_sse_comieq_ss:
3695 case Intrinsic::x86_sse_comilt_ss:
3696 case Intrinsic::x86_sse_comile_ss:
3697 case Intrinsic::x86_sse_comigt_ss:
3698 case Intrinsic::x86_sse_comige_ss:
3699 case Intrinsic::x86_sse_comineq_ss:
3700 case Intrinsic::x86_sse_ucomieq_ss:
3701 case Intrinsic::x86_sse_ucomilt_ss:
3702 case Intrinsic::x86_sse_ucomile_ss:
3703 case Intrinsic::x86_sse_ucomigt_ss:
3704 case Intrinsic::x86_sse_ucomige_ss:
3705 case Intrinsic::x86_sse_ucomineq_ss:
3706 case Intrinsic::x86_sse2_comieq_sd:
3707 case Intrinsic::x86_sse2_comilt_sd:
3708 case Intrinsic::x86_sse2_comile_sd:
3709 case Intrinsic::x86_sse2_comigt_sd:
3710 case Intrinsic::x86_sse2_comige_sd:
3711 case Intrinsic::x86_sse2_comineq_sd:
3712 case Intrinsic::x86_sse2_ucomieq_sd:
3713 case Intrinsic::x86_sse2_ucomilt_sd:
3714 case Intrinsic::x86_sse2_ucomile_sd:
3715 case Intrinsic::x86_sse2_ucomigt_sd:
3716 case Intrinsic::x86_sse2_ucomige_sd:
3717 case Intrinsic::x86_sse2_ucomineq_sd: {
3718 unsigned Opc = 0;
3719 ISD::CondCode CC = ISD::SETCC_INVALID;
3720 switch (IntNo) {
3721 default: break;
3722 case Intrinsic::x86_sse_comieq_ss:
3723 case Intrinsic::x86_sse2_comieq_sd:
3724 Opc = X86ISD::COMI;
3725 CC = ISD::SETEQ;
3726 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003727 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003728 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003729 Opc = X86ISD::COMI;
3730 CC = ISD::SETLT;
3731 break;
3732 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003733 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003734 Opc = X86ISD::COMI;
3735 CC = ISD::SETLE;
3736 break;
3737 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003738 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003739 Opc = X86ISD::COMI;
3740 CC = ISD::SETGT;
3741 break;
3742 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003743 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003744 Opc = X86ISD::COMI;
3745 CC = ISD::SETGE;
3746 break;
3747 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003748 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003749 Opc = X86ISD::COMI;
3750 CC = ISD::SETNE;
3751 break;
3752 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003753 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003754 Opc = X86ISD::UCOMI;
3755 CC = ISD::SETEQ;
3756 break;
3757 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003758 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003759 Opc = X86ISD::UCOMI;
3760 CC = ISD::SETLT;
3761 break;
3762 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003763 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003764 Opc = X86ISD::UCOMI;
3765 CC = ISD::SETLE;
3766 break;
3767 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003768 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003769 Opc = X86ISD::UCOMI;
3770 CC = ISD::SETGT;
3771 break;
3772 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003773 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003774 Opc = X86ISD::UCOMI;
3775 CC = ISD::SETGE;
3776 break;
3777 case Intrinsic::x86_sse_ucomineq_ss:
3778 case Intrinsic::x86_sse2_ucomineq_sd:
3779 Opc = X86ISD::UCOMI;
3780 CC = ISD::SETNE;
3781 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003782 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003783 bool Flip;
3784 unsigned X86CC;
3785 translateX86CC(CC, true, X86CC, Flip);
3786 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3787 Op.getOperand(Flip?1:2));
3788 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3789 DAG.getConstant(X86CC, MVT::i8), Cond);
3790 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00003791 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00003792 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003793}
Evan Cheng72261582005-12-20 06:22:03 +00003794
Evan Cheng0db9fe62006-04-25 20:13:52 +00003795/// LowerOperation - Provide custom lowering hooks for some operations.
3796///
3797SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3798 switch (Op.getOpcode()) {
3799 default: assert(0 && "Should not custom lower this!");
3800 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3801 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3802 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3803 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3804 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3805 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3806 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3807 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3808 case ISD::SHL_PARTS:
3809 case ISD::SRA_PARTS:
3810 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3811 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3812 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3813 case ISD::FABS: return LowerFABS(Op, DAG);
3814 case ISD::FNEG: return LowerFNEG(Op, DAG);
3815 case ISD::SETCC: return LowerSETCC(Op, DAG);
3816 case ISD::SELECT: return LowerSELECT(Op, DAG);
3817 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3818 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00003819 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003820 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003821 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003822 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3823 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3824 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3825 case ISD::VASTART: return LowerVASTART(Op, DAG);
3826 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3827 }
3828}
3829
Evan Cheng72261582005-12-20 06:22:03 +00003830const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3831 switch (Opcode) {
3832 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00003833 case X86ISD::SHLD: return "X86ISD::SHLD";
3834 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00003835 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00003836 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00003837 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00003838 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00003839 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3840 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3841 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00003842 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00003843 case X86ISD::FST: return "X86ISD::FST";
3844 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00003845 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00003846 case X86ISD::CALL: return "X86ISD::CALL";
3847 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3848 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3849 case X86ISD::CMP: return "X86ISD::CMP";
3850 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng6be2c582006-04-05 23:38:46 +00003851 case X86ISD::COMI: return "X86ISD::COMI";
3852 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00003853 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00003854 case X86ISD::CMOV: return "X86ISD::CMOV";
3855 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00003856 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00003857 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3858 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00003859 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00003860 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00003861 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00003862 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00003863 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00003864 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00003865 }
3866}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003867
Nate Begeman368e18d2006-02-16 21:11:51 +00003868void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3869 uint64_t Mask,
3870 uint64_t &KnownZero,
3871 uint64_t &KnownOne,
3872 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003873 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00003874 assert((Opc >= ISD::BUILTIN_OP_END ||
3875 Opc == ISD::INTRINSIC_WO_CHAIN ||
3876 Opc == ISD::INTRINSIC_W_CHAIN ||
3877 Opc == ISD::INTRINSIC_VOID) &&
3878 "Should use MaskedValueIsZero if you don't know whether Op"
3879 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003880
Evan Cheng865f0602006-04-05 06:11:20 +00003881 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003882 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00003883 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +00003884 case X86ISD::SETCC:
3885 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3886 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003887 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003888}
Chris Lattner259e97c2006-01-31 19:43:35 +00003889
3890std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00003891getRegClassForInlineAsmConstraint(const std::string &Constraint,
3892 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00003893 if (Constraint.size() == 1) {
3894 // FIXME: not handling fp-stack yet!
3895 // FIXME: not handling MMX registers yet ('y' constraint).
3896 switch (Constraint[0]) { // GCC X86 Constraint Letters
3897 default: break; // Unknown constriant letter
3898 case 'r': // GENERAL_REGS
3899 case 'R': // LEGACY_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003900 if (VT == MVT::i32)
3901 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3902 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3903 else if (VT == MVT::i16)
3904 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3905 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3906 else if (VT == MVT::i8)
3907 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3908 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003909 case 'l': // INDEX_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003910 if (VT == MVT::i32)
3911 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3912 X86::ESI, X86::EDI, X86::EBP, 0);
3913 else if (VT == MVT::i16)
3914 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3915 X86::SI, X86::DI, X86::BP, 0);
3916 else if (VT == MVT::i8)
3917 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3918 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003919 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3920 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003921 if (VT == MVT::i32)
3922 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3923 else if (VT == MVT::i16)
3924 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3925 else if (VT == MVT::i8)
3926 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3927 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003928 case 'x': // SSE_REGS if SSE1 allowed
3929 if (Subtarget->hasSSE1())
3930 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3931 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3932 0);
3933 return std::vector<unsigned>();
3934 case 'Y': // SSE_REGS if SSE2 allowed
3935 if (Subtarget->hasSSE2())
3936 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3937 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3938 0);
3939 return std::vector<unsigned>();
3940 }
3941 }
3942
Chris Lattner1efa40f2006-02-22 00:56:39 +00003943 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00003944}
Evan Cheng30b37b52006-03-13 23:18:16 +00003945
3946/// isLegalAddressImmediate - Return true if the integer value or
3947/// GlobalValue can be used as the offset of the target addressing mode.
3948bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3949 // X86 allows a sign-extended 32-bit immediate field.
3950 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3951}
3952
3953bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00003954 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00003955 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3956 if (RModel == Reloc::Static)
3957 return true;
3958 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00003959 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00003960 else
3961 return false;
3962 } else
3963 return true;
3964}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003965
3966/// isShuffleMaskLegal - Targets can use this to indicate that they only
3967/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3968/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3969/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00003970bool
3971X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3972 // Only do shuffles on 128-bit vector types for now.
3973 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng80d428c2006-04-19 22:48:17 +00003974 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengc575ca22006-04-17 20:43:08 +00003975 isSplatMask(Mask.Val) ||
Evan Chengc21a0532006-04-05 01:47:37 +00003976 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00003977 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003978 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00003979 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00003980}
Evan Cheng39623da2006-04-20 08:58:49 +00003981
3982bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3983 MVT::ValueType EVT,
3984 SelectionDAG &DAG) const {
3985 unsigned NumElts = BVOps.size();
3986 // Only do shuffles on 128-bit vector types for now.
3987 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3988 if (NumElts == 2) return true;
3989 if (NumElts == 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00003990 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng39623da2006-04-20 08:58:49 +00003991 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3992 }
3993 return false;
3994}