blob: 1d2f6e4fc8f618b0b6febc54791e189e4fe42731 [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 {
Evan Cheng3fddf242006-05-26 20:37:47 +0000445 // XMM arguments have to be aligned on 16-byte boundary.
446 if (ObjSize == 16)
447 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +0000448 // Create the frame index object for this incoming parameter...
449 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
450 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
451 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
452 DAG.getSrcValue(NULL));
453 ArgValues.push_back(ArgValue);
454 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Cheng1bc78042006-04-26 01:20:17 +0000455 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000456 }
457
Evan Cheng25caf632006-05-23 21:06:34 +0000458 ArgValues.push_back(Root);
459
Evan Cheng1bc78042006-04-26 01:20:17 +0000460 // If the function takes variable number of arguments, make a frame index for
461 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000462 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
463 if (isVarArg)
Evan Cheng1bc78042006-04-26 01:20:17 +0000464 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
465 ReturnAddrIndex = 0; // No return address slot generated yet.
466 BytesToPopOnReturn = 0; // Callee pops nothing.
467 BytesCallerReserves = ArgOffset;
Evan Cheng25caf632006-05-23 21:06:34 +0000468
Chris Lattner2d297092006-05-23 18:50:38 +0000469 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
470 // pointer.
Evan Cheng25caf632006-05-23 21:06:34 +0000471 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner2d297092006-05-23 18:50:38 +0000472 Subtarget->isTargetDarwin())
473 BytesToPopOnReturn = 4;
Evan Cheng1bc78042006-04-26 01:20:17 +0000474
Evan Cheng25caf632006-05-23 21:06:34 +0000475 // Return the new list of results.
476 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
477 Op.Val->value_end());
478 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000479}
480
Evan Cheng32fe1032006-05-25 00:59:30 +0000481
482SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
483 SDOperand Chain = Op.getOperand(0);
484 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
485 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
486 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
487 SDOperand Callee = Op.getOperand(4);
488 MVT::ValueType RetVT= Op.Val->getValueType(0);
489 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000490
Evan Cheng347d5f72006-04-28 21:29:37 +0000491 // Keep track of the number of XMM regs passed so far.
492 unsigned NumXMMRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000493 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000494 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000495 };
Evan Cheng347d5f72006-04-28 21:29:37 +0000496
Evan Cheng32fe1032006-05-25 00:59:30 +0000497 // Count how many bytes are to be pushed on the stack.
498 unsigned NumBytes = 0;
499 for (unsigned i = 0; i != NumOps; ++i) {
500 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000501
Evan Cheng32fe1032006-05-25 00:59:30 +0000502 switch (Arg.getValueType()) {
503 default: assert(0 && "Unexpected ValueType for argument!");
504 case MVT::i8:
505 case MVT::i16:
506 case MVT::i32:
507 case MVT::f32:
508 NumBytes += 4;
509 break;
510 case MVT::i64:
511 case MVT::f64:
512 NumBytes += 8;
513 break;
514 case MVT::v16i8:
515 case MVT::v8i16:
516 case MVT::v4i32:
517 case MVT::v2i64:
518 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000519 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000520 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +0000521 ++NumXMMRegs;
Evan Cheng3fddf242006-05-26 20:37:47 +0000522 else {
523 // XMM arguments have to be aligned on 16-byte boundary.
524 NumBytes = ((NumBytes + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +0000525 NumBytes += 16;
Evan Cheng3fddf242006-05-26 20:37:47 +0000526 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000527 break;
528 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000529 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000530
Evan Cheng32fe1032006-05-25 00:59:30 +0000531 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000532
Evan Cheng32fe1032006-05-25 00:59:30 +0000533 // Arguments go on the stack in reverse order, as specified by the ABI.
534 unsigned ArgOffset = 0;
535 NumXMMRegs = 0;
536 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
537 std::vector<SDOperand> MemOpChains;
538 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
539 for (unsigned i = 0; i != NumOps; ++i) {
540 SDOperand Arg = Op.getOperand(5+2*i);
541
542 switch (Arg.getValueType()) {
543 default: assert(0 && "Unexpected ValueType for argument!");
544 case MVT::i8:
Evan Cheng6b5783d2006-05-25 18:56:34 +0000545 case MVT::i16: {
Evan Cheng32fe1032006-05-25 00:59:30 +0000546 // Promote the integer to 32 bits. If the input type is signed use a
547 // sign extend, otherwise use a zero extend.
548 unsigned ExtOp =
549 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
550 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
551 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng6b5783d2006-05-25 18:56:34 +0000552 }
553 // Fallthrough
Evan Cheng32fe1032006-05-25 00:59:30 +0000554
555 case MVT::i32:
556 case MVT::f32: {
557 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
558 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
559 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
560 Arg, PtrOff, DAG.getSrcValue(NULL)));
561 ArgOffset += 4;
562 break;
563 }
564 case MVT::i64:
565 case MVT::f64: {
566 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
567 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
568 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
569 Arg, PtrOff, DAG.getSrcValue(NULL)));
570 ArgOffset += 8;
571 break;
572 }
573 case MVT::v16i8:
574 case MVT::v8i16:
575 case MVT::v4i32:
576 case MVT::v2i64:
577 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000578 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000579 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000580 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
581 NumXMMRegs++;
582 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +0000583 // XMM arguments have to be aligned on 16-byte boundary.
584 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000585 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +0000586 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
587 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
588 Arg, PtrOff, DAG.getSrcValue(NULL)));
589 ArgOffset += 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000590 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000591 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000592 }
593
Evan Cheng32fe1032006-05-25 00:59:30 +0000594 if (!MemOpChains.empty())
595 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000596
Evan Cheng347d5f72006-04-28 21:29:37 +0000597 // Build a sequence of copy-to-reg nodes chained together with token chain
598 // and flag operands which copy the outgoing args into registers.
599 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000600 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
601 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
602 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000603 InFlag = Chain.getValue(1);
604 }
605
Evan Cheng32fe1032006-05-25 00:59:30 +0000606 // If the callee is a GlobalAddress node (quite common, every direct call is)
607 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
608 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
609 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
610 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
611 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
612
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000613 std::vector<MVT::ValueType> NodeTys;
614 NodeTys.push_back(MVT::Other); // Returns a chain
615 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
616 std::vector<SDOperand> Ops;
617 Ops.push_back(Chain);
618 Ops.push_back(Callee);
Evan Cheng347d5f72006-04-28 21:29:37 +0000619 if (InFlag.Val)
620 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000621
Evan Cheng32fe1032006-05-25 00:59:30 +0000622 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
623 NodeTys, Ops);
Evan Cheng347d5f72006-04-28 21:29:37 +0000624 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000625
Chris Lattner2d297092006-05-23 18:50:38 +0000626 // Create the CALLSEQ_END node.
627 unsigned NumBytesForCalleeToPush = 0;
628
629 // If this is is a call to a struct-return function on Darwin/X86, the callee
630 // pops the hidden struct pointer, so we have to push it back.
631 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
632 NumBytesForCalleeToPush = 4;
633
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000634 NodeTys.clear();
635 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +0000636 if (RetVT != MVT::Other)
637 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000638 Ops.clear();
639 Ops.push_back(Chain);
640 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000641 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000642 Ops.push_back(InFlag);
643 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng32fe1032006-05-25 00:59:30 +0000644 if (RetVT != MVT::Other)
645 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000646
Evan Cheng32fe1032006-05-25 00:59:30 +0000647 std::vector<SDOperand> ResultVals;
648 NodeTys.clear();
649 switch (RetVT) {
650 default: assert(0 && "Unknown value type to return!");
651 case MVT::Other: break;
652 case MVT::i8:
653 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
654 ResultVals.push_back(Chain.getValue(0));
655 NodeTys.push_back(MVT::i8);
656 break;
657 case MVT::i16:
658 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
659 ResultVals.push_back(Chain.getValue(0));
660 NodeTys.push_back(MVT::i16);
661 break;
662 case MVT::i32:
663 if (Op.Val->getValueType(1) == MVT::i32) {
664 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
665 ResultVals.push_back(Chain.getValue(0));
666 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
667 Chain.getValue(2)).getValue(1);
668 ResultVals.push_back(Chain.getValue(0));
669 NodeTys.push_back(MVT::i32);
670 } else {
671 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
672 ResultVals.push_back(Chain.getValue(0));
Evan Chengd90eb7f2006-01-05 00:27:02 +0000673 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000674 NodeTys.push_back(MVT::i32);
675 break;
676 case MVT::v16i8:
677 case MVT::v8i16:
678 case MVT::v4i32:
679 case MVT::v2i64:
680 case MVT::v4f32:
681 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +0000682 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
683 ResultVals.push_back(Chain.getValue(0));
684 NodeTys.push_back(RetVT);
685 break;
686 case MVT::f32:
687 case MVT::f64: {
688 std::vector<MVT::ValueType> Tys;
689 Tys.push_back(MVT::f64);
690 Tys.push_back(MVT::Other);
691 Tys.push_back(MVT::Flag);
692 std::vector<SDOperand> Ops;
693 Ops.push_back(Chain);
694 Ops.push_back(InFlag);
695 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
696 Chain = RetVal.getValue(1);
697 InFlag = RetVal.getValue(2);
698 if (X86ScalarSSE) {
699 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
700 // shouldn't be necessary except that RFP cannot be live across
701 // multiple blocks. When stackifier is fixed, they can be uncoupled.
702 MachineFunction &MF = DAG.getMachineFunction();
703 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
704 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
705 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000706 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +0000707 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000708 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +0000709 Ops.push_back(RetVal);
710 Ops.push_back(StackSlot);
711 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000712 Ops.push_back(InFlag);
Evan Cheng32fe1032006-05-25 00:59:30 +0000713 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
714 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
715 DAG.getSrcValue(NULL));
Evan Cheng347d5f72006-04-28 21:29:37 +0000716 Chain = RetVal.getValue(1);
Evan Cheng347d5f72006-04-28 21:29:37 +0000717 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000718
719 if (RetVT == MVT::f32 && !X86ScalarSSE)
720 // FIXME: we would really like to remember that this FP_ROUND
721 // operation is okay to eliminate if we allow excess FP precision.
722 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
723 ResultVals.push_back(RetVal);
724 NodeTys.push_back(RetVT);
725 break;
726 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000727 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000728
Evan Cheng32fe1032006-05-25 00:59:30 +0000729 // If the function returns void, just return the chain.
730 if (ResultVals.empty())
731 return Chain;
732
733 // Otherwise, merge everything together with a MERGE_VALUES node.
734 NodeTys.push_back(MVT::Other);
735 ResultVals.push_back(Chain);
736 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
737 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000738}
739
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000740//===----------------------------------------------------------------------===//
741// Fast Calling Convention implementation
742//===----------------------------------------------------------------------===//
743//
744// The X86 'fast' calling convention passes up to two integer arguments in
745// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
746// and requires that the callee pop its arguments off the stack (allowing proper
747// tail calls), and has the same return value conventions as C calling convs.
748//
749// This calling convention always arranges for the callee pop value to be 8n+4
750// bytes, which is needed for tail recursion elimination and stack alignment
751// reasons.
752//
753// Note that this can be enhanced in the future to pass fp vals in registers
754// (when we have a global fp allocator) and do other tricks.
755//
756
Chris Lattner89fad2c2006-03-17 17:27:47 +0000757// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
758// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
759// EDX". Anything more is illegal.
760//
761// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000762// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000763// a physreg with a virtreg, this increases the size of the physreg's live
764// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000765// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000766// allocator to wedge itself.
767//
768// This code triggers this problem more often if we pass args in registers,
769// so disable it until this is fixed.
770//
771// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
772// about code being dead.
773//
774static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000775
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000776
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000777/// HowToPassFastCCArgument - Returns how an formal argument of the specified
778/// type should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +0000779/// slot; if it is through integer or XMM register, returns the number of
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000780/// integer or XMM registers are needed.
Evan Chengeda65fa2006-04-27 01:32:22 +0000781static void
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000782HowToPassFastCCArgument(MVT::ValueType ObjectVT,
783 unsigned NumIntRegs, unsigned NumXMMRegs,
784 unsigned &ObjSize, unsigned &ObjIntRegs,
785 unsigned &ObjXMMRegs) {
Evan Chengeda65fa2006-04-27 01:32:22 +0000786 ObjSize = 0;
787 NumIntRegs = 0;
Evan Chengcc1fc222006-05-25 23:31:23 +0000788 NumXMMRegs = 0;
Evan Chengeda65fa2006-04-27 01:32:22 +0000789
790 switch (ObjectVT) {
791 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000792 case MVT::i8:
793 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000794 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000795 else
796 ObjSize = 1;
797 break;
798 case MVT::i16:
799 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000800 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000801 else
802 ObjSize = 2;
803 break;
804 case MVT::i32:
805 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000806 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000807 else
808 ObjSize = 4;
809 break;
810 case MVT::i64:
811 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +0000812 ObjIntRegs = 2;
Evan Chengeda65fa2006-04-27 01:32:22 +0000813 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +0000814 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000815 ObjSize = 4;
816 } else
817 ObjSize = 8;
818 case MVT::f32:
819 ObjSize = 4;
820 break;
821 case MVT::f64:
822 ObjSize = 8;
823 break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000824 case MVT::v16i8:
825 case MVT::v8i16:
826 case MVT::v4i32:
827 case MVT::v2i64:
828 case MVT::v4f32:
829 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000830 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000831 ObjXMMRegs = 1;
832 else
833 ObjSize = 16;
834 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000835 }
836}
837
Evan Cheng25caf632006-05-23 21:06:34 +0000838SDOperand
839X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
840 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000841 MachineFunction &MF = DAG.getMachineFunction();
842 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000843 SDOperand Root = Op.getOperand(0);
844 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000845
Evan Chengeda65fa2006-04-27 01:32:22 +0000846 // Add DAG nodes to load the arguments... On entry to a function the stack
847 // frame looks like this:
848 //
849 // [ESP] -- return address
850 // [ESP + 4] -- first nonreg argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000851 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000852 // ...
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000853 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
854
855 // Keep track of the number of integer regs passed so far. This can be either
856 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
857 // used).
858 unsigned NumIntRegs = 0;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000859 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng32fe1032006-05-25 00:59:30 +0000860
861 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000862 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000863 };
Chris Lattner1c636e92006-03-17 05:10:20 +0000864
Evan Cheng1bc78042006-04-26 01:20:17 +0000865 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000866 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
867 unsigned ArgIncrement = 4;
868 unsigned ObjSize = 0;
869 unsigned ObjIntRegs = 0;
870 unsigned ObjXMMRegs = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000871
Evan Cheng25caf632006-05-23 21:06:34 +0000872 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
873 ObjSize, ObjIntRegs, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +0000874 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000875 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000876
Evan Cheng25caf632006-05-23 21:06:34 +0000877 unsigned Reg;
878 SDOperand ArgValue;
879 if (ObjIntRegs || ObjXMMRegs) {
880 switch (ObjectVT) {
881 default: assert(0 && "Unhandled argument type!");
Evan Cheng25caf632006-05-23 21:06:34 +0000882 case MVT::i8:
883 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
884 X86::GR8RegisterClass);
885 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
886 break;
887 case MVT::i16:
888 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
889 X86::GR16RegisterClass);
890 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
891 break;
892 case MVT::i32:
893 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
894 X86::GR32RegisterClass);
895 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
896 break;
897 case MVT::i64:
898 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
899 X86::GR32RegisterClass);
900 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
901 if (ObjIntRegs == 2) {
902 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
903 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
904 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng85e38002006-04-27 05:35:28 +0000905 }
Evan Cheng25caf632006-05-23 21:06:34 +0000906 break;
907 case MVT::v16i8:
908 case MVT::v8i16:
909 case MVT::v4i32:
910 case MVT::v2i64:
911 case MVT::v4f32:
912 case MVT::v2f64:
913 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
914 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
915 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000916 }
Evan Cheng25caf632006-05-23 21:06:34 +0000917 NumIntRegs += ObjIntRegs;
918 NumXMMRegs += ObjXMMRegs;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000919 }
Evan Cheng25caf632006-05-23 21:06:34 +0000920
921 if (ObjSize) {
Evan Cheng3fddf242006-05-26 20:37:47 +0000922 // XMM arguments have to be aligned on 16-byte boundary.
923 if (ObjSize == 16)
924 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +0000925 // Create the SelectionDAG nodes corresponding to a load from this
926 // parameter.
927 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
928 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
929 if (ObjectVT == MVT::i64 && ObjIntRegs) {
930 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
931 DAG.getSrcValue(NULL));
932 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
933 } else
934 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
935 DAG.getSrcValue(NULL));
936 ArgOffset += ArgIncrement; // Move on to the next argument.
937 }
938
939 ArgValues.push_back(ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000940 }
941
Evan Cheng25caf632006-05-23 21:06:34 +0000942 ArgValues.push_back(Root);
943
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000944 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
945 // arguments and the arguments after the retaddr has been pushed are aligned.
946 if ((ArgOffset & 7) == 0)
947 ArgOffset += 4;
948
949 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
950 ReturnAddrIndex = 0; // No return address slot generated yet.
951 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
952 BytesCallerReserves = 0;
953
954 // Finally, inform the code generator which regs we return values in.
Evan Cheng25caf632006-05-23 21:06:34 +0000955 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000956 default: assert(0 && "Unknown type!");
957 case MVT::isVoid: break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000958 case MVT::i8:
959 case MVT::i16:
960 case MVT::i32:
961 MF.addLiveOut(X86::EAX);
962 break;
963 case MVT::i64:
964 MF.addLiveOut(X86::EAX);
965 MF.addLiveOut(X86::EDX);
966 break;
967 case MVT::f32:
968 case MVT::f64:
969 MF.addLiveOut(X86::ST0);
970 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +0000971 case MVT::v16i8:
972 case MVT::v8i16:
973 case MVT::v4i32:
974 case MVT::v2i64:
975 case MVT::v4f32:
976 case MVT::v2f64:
Evan Cheng347d5f72006-04-28 21:29:37 +0000977 MF.addLiveOut(X86::XMM0);
978 break;
979 }
Evan Cheng347d5f72006-04-28 21:29:37 +0000980
Evan Cheng25caf632006-05-23 21:06:34 +0000981 // Return the new list of results.
982 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
983 Op.Val->value_end());
984 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000985}
986
Evan Cheng32fe1032006-05-25 00:59:30 +0000987 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
988 SDOperand Chain = Op.getOperand(0);
989 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
990 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
991 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
992 SDOperand Callee = Op.getOperand(4);
993 MVT::ValueType RetVT= Op.Val->getValueType(0);
994 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
995
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000996 // Count how many bytes are to be pushed on the stack.
997 unsigned NumBytes = 0;
998
999 // Keep track of the number of integer regs passed so far. This can be either
1000 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1001 // used).
1002 unsigned NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001003 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001004
Evan Cheng32fe1032006-05-25 00:59:30 +00001005 static const unsigned GPRArgRegs[][2] = {
1006 { X86::AL, X86::DL },
1007 { X86::AX, X86::DX },
1008 { X86::EAX, X86::EDX }
1009 };
1010 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001011 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001012 };
1013
1014 for (unsigned i = 0; i != NumOps; ++i) {
1015 SDOperand Arg = Op.getOperand(5+2*i);
1016
1017 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001018 default: assert(0 && "Unknown value type!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001019 case MVT::i8:
1020 case MVT::i16:
1021 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +00001022 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001023 ++NumIntRegs;
1024 break;
1025 }
Evan Cheng25e71d12006-05-25 22:38:31 +00001026 // Fall through
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001027 case MVT::f32:
1028 NumBytes += 4;
1029 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001030 case MVT::f64:
1031 NumBytes += 8;
1032 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001033 case MVT::v16i8:
1034 case MVT::v8i16:
1035 case MVT::v4i32:
1036 case MVT::v2i64:
1037 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001038 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001039 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +00001040 NumXMMRegs++;
Evan Cheng3fddf242006-05-26 20:37:47 +00001041 else {
1042 // XMM arguments have to be aligned on 16-byte boundary.
1043 NumBytes = ((NumBytes + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +00001044 NumBytes += 16;
Evan Cheng3fddf242006-05-26 20:37:47 +00001045 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001046 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001047 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001048 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001049
1050 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1051 // arguments and the arguments after the retaddr has been pushed are aligned.
1052 if ((NumBytes & 7) == 0)
1053 NumBytes += 4;
1054
Chris Lattner94dd2922006-02-13 09:00:43 +00001055 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001056
1057 // Arguments go on the stack in reverse order, as specified by the ABI.
1058 unsigned ArgOffset = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001059 NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001060 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1061 std::vector<SDOperand> MemOpChains;
1062 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1063 for (unsigned i = 0; i != NumOps; ++i) {
1064 SDOperand Arg = Op.getOperand(5+2*i);
1065
1066 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001067 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001068 case MVT::i8:
1069 case MVT::i16:
1070 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +00001071 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001072 RegsToPass.push_back(
1073 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1074 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001075 ++NumIntRegs;
1076 break;
1077 }
1078 // Fall through
1079 case MVT::f32: {
1080 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001081 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1082 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1083 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001084 ArgOffset += 4;
1085 break;
1086 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001087 case MVT::f64: {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001088 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001089 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1090 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1091 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001092 ArgOffset += 8;
1093 break;
1094 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001095 case MVT::v16i8:
1096 case MVT::v8i16:
1097 case MVT::v4i32:
1098 case MVT::v2i64:
1099 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001100 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001101 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001102 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1103 NumXMMRegs++;
1104 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +00001105 // XMM arguments have to be aligned on 16-byte boundary.
1106 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +00001107 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1108 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1109 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1110 Arg, PtrOff, DAG.getSrcValue(NULL)));
1111 ArgOffset += 16;
1112 }
1113 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001114 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001115
Evan Cheng32fe1032006-05-25 00:59:30 +00001116 if (!MemOpChains.empty())
1117 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001119 // Build a sequence of copy-to-reg nodes chained together with token chain
1120 // and flag operands which copy the outgoing args into registers.
1121 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001122 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1123 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1124 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001125 InFlag = Chain.getValue(1);
1126 }
1127
Evan Cheng32fe1032006-05-25 00:59:30 +00001128 // If the callee is a GlobalAddress node (quite common, every direct call is)
1129 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1130 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1131 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1132 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1133 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1134
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001135 std::vector<MVT::ValueType> NodeTys;
1136 NodeTys.push_back(MVT::Other); // Returns a chain
1137 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1138 std::vector<SDOperand> Ops;
1139 Ops.push_back(Chain);
1140 Ops.push_back(Callee);
1141 if (InFlag.Val)
1142 Ops.push_back(InFlag);
1143
1144 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001145 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1146 NodeTys, Ops);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001147 InFlag = Chain.getValue(1);
1148
1149 NodeTys.clear();
1150 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +00001151 if (RetVT != MVT::Other)
1152 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001153 Ops.clear();
1154 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001155 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1156 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001157 Ops.push_back(InFlag);
1158 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng32fe1032006-05-25 00:59:30 +00001159 if (RetVT != MVT::Other)
1160 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001161
Evan Cheng32fe1032006-05-25 00:59:30 +00001162 std::vector<SDOperand> ResultVals;
1163 NodeTys.clear();
1164 switch (RetVT) {
1165 default: assert(0 && "Unknown value type to return!");
1166 case MVT::Other: break;
1167 case MVT::i8:
1168 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1169 ResultVals.push_back(Chain.getValue(0));
1170 NodeTys.push_back(MVT::i8);
1171 break;
1172 case MVT::i16:
1173 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1174 ResultVals.push_back(Chain.getValue(0));
1175 NodeTys.push_back(MVT::i16);
1176 break;
1177 case MVT::i32:
1178 if (Op.Val->getValueType(1) == MVT::i32) {
1179 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1180 ResultVals.push_back(Chain.getValue(0));
1181 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1182 Chain.getValue(2)).getValue(1);
1183 ResultVals.push_back(Chain.getValue(0));
1184 NodeTys.push_back(MVT::i32);
1185 } else {
1186 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1187 ResultVals.push_back(Chain.getValue(0));
Evan Chengd9558e02006-01-06 00:43:03 +00001188 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001189 NodeTys.push_back(MVT::i32);
1190 break;
1191 case MVT::v16i8:
1192 case MVT::v8i16:
1193 case MVT::v4i32:
1194 case MVT::v2i64:
1195 case MVT::v4f32:
1196 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +00001197 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1198 ResultVals.push_back(Chain.getValue(0));
1199 NodeTys.push_back(RetVT);
1200 break;
1201 case MVT::f32:
1202 case MVT::f64: {
1203 std::vector<MVT::ValueType> Tys;
1204 Tys.push_back(MVT::f64);
1205 Tys.push_back(MVT::Other);
1206 Tys.push_back(MVT::Flag);
1207 std::vector<SDOperand> Ops;
1208 Ops.push_back(Chain);
1209 Ops.push_back(InFlag);
1210 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1211 Chain = RetVal.getValue(1);
1212 InFlag = RetVal.getValue(2);
1213 if (X86ScalarSSE) {
1214 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1215 // shouldn't be necessary except that RFP cannot be live across
1216 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1217 MachineFunction &MF = DAG.getMachineFunction();
1218 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1219 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1220 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001221 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +00001222 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001223 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001224 Ops.push_back(RetVal);
1225 Ops.push_back(StackSlot);
1226 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001227 Ops.push_back(InFlag);
Evan Cheng32fe1032006-05-25 00:59:30 +00001228 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1229 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1230 DAG.getSrcValue(NULL));
1231 Chain = RetVal.getValue(1);
1232 }
Evan Chengd9558e02006-01-06 00:43:03 +00001233
Evan Cheng32fe1032006-05-25 00:59:30 +00001234 if (RetVT == MVT::f32 && !X86ScalarSSE)
1235 // FIXME: we would really like to remember that this FP_ROUND
1236 // operation is okay to eliminate if we allow excess FP precision.
1237 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1238 ResultVals.push_back(RetVal);
1239 NodeTys.push_back(RetVT);
1240 break;
1241 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001242 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001243
Evan Cheng32fe1032006-05-25 00:59:30 +00001244
1245 // If the function returns void, just return the chain.
1246 if (ResultVals.empty())
1247 return Chain;
1248
1249 // Otherwise, merge everything together with a MERGE_VALUES node.
1250 NodeTys.push_back(MVT::Other);
1251 ResultVals.push_back(Chain);
1252 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1253 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001254}
1255
1256SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1257 if (ReturnAddrIndex == 0) {
1258 // Set up a frame object for the return address.
1259 MachineFunction &MF = DAG.getMachineFunction();
1260 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1261 }
1262
1263 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1264}
1265
1266
1267
1268std::pair<SDOperand, SDOperand> X86TargetLowering::
1269LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1270 SelectionDAG &DAG) {
1271 SDOperand Result;
1272 if (Depth) // Depths > 0 not supported yet!
1273 Result = DAG.getConstant(0, getPointerTy());
1274 else {
1275 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1276 if (!isFrameAddress)
1277 // Just load the return address
1278 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1279 DAG.getSrcValue(NULL));
1280 else
1281 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1282 DAG.getConstant(4, MVT::i32));
1283 }
1284 return std::make_pair(Result, Chain);
1285}
1286
Evan Cheng4a460802006-01-11 00:33:36 +00001287/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1288/// which corresponds to the condition code.
1289static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1290 switch (X86CC) {
1291 default: assert(0 && "Unknown X86 conditional code!");
1292 case X86ISD::COND_A: return X86::JA;
1293 case X86ISD::COND_AE: return X86::JAE;
1294 case X86ISD::COND_B: return X86::JB;
1295 case X86ISD::COND_BE: return X86::JBE;
1296 case X86ISD::COND_E: return X86::JE;
1297 case X86ISD::COND_G: return X86::JG;
1298 case X86ISD::COND_GE: return X86::JGE;
1299 case X86ISD::COND_L: return X86::JL;
1300 case X86ISD::COND_LE: return X86::JLE;
1301 case X86ISD::COND_NE: return X86::JNE;
1302 case X86ISD::COND_NO: return X86::JNO;
1303 case X86ISD::COND_NP: return X86::JNP;
1304 case X86ISD::COND_NS: return X86::JNS;
1305 case X86ISD::COND_O: return X86::JO;
1306 case X86ISD::COND_P: return X86::JP;
1307 case X86ISD::COND_S: return X86::JS;
1308 }
1309}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001310
Evan Cheng6dfa9992006-01-30 23:41:35 +00001311/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1312/// specific condition code. It returns a false if it cannot do a direct
1313/// translation. X86CC is the translated CondCode. Flip is set to true if the
1314/// the order of comparison operands should be flipped.
Evan Cheng6be2c582006-04-05 23:38:46 +00001315static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1316 unsigned &X86CC, bool &Flip) {
Evan Cheng6dfa9992006-01-30 23:41:35 +00001317 Flip = false;
1318 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001319 if (!isFP) {
1320 switch (SetCCOpcode) {
1321 default: break;
1322 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1323 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1324 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1325 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1326 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1327 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1328 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1329 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1330 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1331 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1332 }
1333 } else {
1334 // On a floating point condition, the flags are set as follows:
1335 // ZF PF CF op
1336 // 0 | 0 | 0 | X > Y
1337 // 0 | 0 | 1 | X < Y
1338 // 1 | 0 | 0 | X == Y
1339 // 1 | 1 | 1 | unordered
1340 switch (SetCCOpcode) {
1341 default: break;
1342 case ISD::SETUEQ:
1343 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001344 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001345 case ISD::SETOGT:
1346 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001347 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001348 case ISD::SETOGE:
1349 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001350 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001351 case ISD::SETULT:
1352 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001353 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001354 case ISD::SETULE:
1355 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1356 case ISD::SETONE:
1357 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1358 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1359 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1360 }
1361 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001362
1363 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001364}
1365
Evan Cheng6be2c582006-04-05 23:38:46 +00001366static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1367 bool &Flip) {
1368 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1369}
1370
Evan Cheng4a460802006-01-11 00:33:36 +00001371/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1372/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001373/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001374static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001375 switch (X86CC) {
1376 default:
1377 return false;
1378 case X86ISD::COND_B:
1379 case X86ISD::COND_BE:
1380 case X86ISD::COND_E:
1381 case X86ISD::COND_P:
1382 case X86ISD::COND_A:
1383 case X86ISD::COND_AE:
1384 case X86ISD::COND_NE:
1385 case X86ISD::COND_NP:
1386 return true;
1387 }
1388}
1389
Evan Cheng4a460802006-01-11 00:33:36 +00001390MachineBasicBlock *
1391X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1392 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001393 switch (MI->getOpcode()) {
1394 default: assert(false && "Unexpected instr type to insert");
1395 case X86::CMOV_FR32:
Evan Chengf7c378e2006-04-10 07:23:14 +00001396 case X86::CMOV_FR64:
1397 case X86::CMOV_V4F32:
1398 case X86::CMOV_V2F64:
1399 case X86::CMOV_V2I64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001400 // To "insert" a SELECT_CC instruction, we actually have to insert the
1401 // diamond control-flow pattern. The incoming instruction knows the
1402 // destination vreg to set, the condition code register to branch on, the
1403 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001404 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1405 ilist<MachineBasicBlock>::iterator It = BB;
1406 ++It;
1407
1408 // thisMBB:
1409 // ...
1410 // TrueVal = ...
1411 // cmpTY ccX, r1, r2
1412 // bCC copy1MBB
1413 // fallthrough --> copy0MBB
1414 MachineBasicBlock *thisMBB = BB;
1415 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1416 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1417 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1418 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1419 MachineFunction *F = BB->getParent();
1420 F->getBasicBlockList().insert(It, copy0MBB);
1421 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001422 // Update machine-CFG edges by first adding all successors of the current
1423 // block to the new block which will contain the Phi node for the select.
1424 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1425 e = BB->succ_end(); i != e; ++i)
1426 sinkMBB->addSuccessor(*i);
1427 // Next, remove all successors of the current block, and add the true
1428 // and fallthrough blocks as its successors.
1429 while(!BB->succ_empty())
1430 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001431 BB->addSuccessor(copy0MBB);
1432 BB->addSuccessor(sinkMBB);
1433
1434 // copy0MBB:
1435 // %FalseValue = ...
1436 // # fallthrough to sinkMBB
1437 BB = copy0MBB;
1438
1439 // Update machine-CFG edges
1440 BB->addSuccessor(sinkMBB);
1441
1442 // sinkMBB:
1443 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1444 // ...
1445 BB = sinkMBB;
1446 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1447 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1448 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001449
Evan Cheng0cc39452006-01-16 21:21:29 +00001450 delete MI; // The pseudo instruction is gone now.
1451 return BB;
1452 }
Evan Cheng4a460802006-01-11 00:33:36 +00001453
Evan Cheng0cc39452006-01-16 21:21:29 +00001454 case X86::FP_TO_INT16_IN_MEM:
1455 case X86::FP_TO_INT32_IN_MEM:
1456 case X86::FP_TO_INT64_IN_MEM: {
1457 // Change the floating point control register to use "round towards zero"
1458 // mode when truncating to an integer value.
1459 MachineFunction *F = BB->getParent();
1460 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1461 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1462
1463 // Load the old value of the high byte of the control word...
1464 unsigned OldCW =
Evan Cheng069287d2006-05-16 07:21:53 +00001465 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng0cc39452006-01-16 21:21:29 +00001466 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1467
1468 // Set the high part to be round to zero...
1469 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1470
1471 // Reload the modified control word now...
1472 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1473
1474 // Restore the memory image of control word to original value
1475 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1476
1477 // Get the X86 opcode to use.
1478 unsigned Opc;
1479 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001480 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001481 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1482 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1483 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1484 }
1485
1486 X86AddressMode AM;
1487 MachineOperand &Op = MI->getOperand(0);
1488 if (Op.isRegister()) {
1489 AM.BaseType = X86AddressMode::RegBase;
1490 AM.Base.Reg = Op.getReg();
1491 } else {
1492 AM.BaseType = X86AddressMode::FrameIndexBase;
1493 AM.Base.FrameIndex = Op.getFrameIndex();
1494 }
1495 Op = MI->getOperand(1);
1496 if (Op.isImmediate())
1497 AM.Scale = Op.getImmedValue();
1498 Op = MI->getOperand(2);
1499 if (Op.isImmediate())
1500 AM.IndexReg = Op.getImmedValue();
1501 Op = MI->getOperand(3);
1502 if (Op.isGlobalAddress()) {
1503 AM.GV = Op.getGlobal();
1504 } else {
1505 AM.Disp = Op.getImmedValue();
1506 }
1507 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1508
1509 // Reload the original control word now.
1510 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1511
1512 delete MI; // The pseudo instruction is gone now.
1513 return BB;
1514 }
1515 }
Evan Cheng4a460802006-01-11 00:33:36 +00001516}
1517
1518
1519//===----------------------------------------------------------------------===//
1520// X86 Custom Lowering Hooks
1521//===----------------------------------------------------------------------===//
1522
Evan Cheng30b37b52006-03-13 23:18:16 +00001523/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1524/// load. For Darwin, external and weak symbols are indirect, loading the value
1525/// at address GV rather then the value of GV itself. This means that the
1526/// GlobalAddress must be in the base or index register of the address, not the
1527/// GV offset field.
1528static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1529 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1530 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1531}
1532
Evan Cheng5ced1d82006-04-06 23:23:56 +00001533/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001534/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001535static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1536 if (Op.getOpcode() == ISD::UNDEF)
1537 return true;
1538
1539 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001540 return (Val >= Low && Val < Hi);
1541}
1542
1543/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1544/// true if Op is undef or if its value equal to the specified value.
1545static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1546 if (Op.getOpcode() == ISD::UNDEF)
1547 return true;
1548 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001549}
1550
Evan Cheng0188ecb2006-03-22 18:59:22 +00001551/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1552/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1553bool X86::isPSHUFDMask(SDNode *N) {
1554 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1555
1556 if (N->getNumOperands() != 4)
1557 return false;
1558
1559 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001560 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001561 SDOperand Arg = N->getOperand(i);
1562 if (Arg.getOpcode() == ISD::UNDEF) continue;
1563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1564 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001565 return false;
1566 }
1567
1568 return true;
1569}
1570
1571/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001572/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001573bool X86::isPSHUFHWMask(SDNode *N) {
1574 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1575
1576 if (N->getNumOperands() != 8)
1577 return false;
1578
1579 // Lower quadword copied in order.
1580 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001581 SDOperand Arg = N->getOperand(i);
1582 if (Arg.getOpcode() == ISD::UNDEF) continue;
1583 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1584 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001585 return false;
1586 }
1587
1588 // Upper quadword shuffled.
1589 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001590 SDOperand Arg = N->getOperand(i);
1591 if (Arg.getOpcode() == ISD::UNDEF) continue;
1592 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1593 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001594 if (Val < 4 || Val > 7)
1595 return false;
1596 }
1597
1598 return true;
1599}
1600
1601/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001602/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001603bool X86::isPSHUFLWMask(SDNode *N) {
1604 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1605
1606 if (N->getNumOperands() != 8)
1607 return false;
1608
1609 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001610 for (unsigned i = 4; i != 8; ++i)
1611 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001612 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001613
1614 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001615 for (unsigned i = 0; i != 4; ++i)
1616 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001617 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001618
1619 return true;
1620}
1621
Evan Cheng14aed5e2006-03-24 01:18:28 +00001622/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1623/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng39623da2006-04-20 08:58:49 +00001624static bool isSHUFPMask(std::vector<SDOperand> &N) {
1625 unsigned NumElems = N.size();
1626 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001627
Evan Cheng39623da2006-04-20 08:58:49 +00001628 unsigned Half = NumElems / 2;
1629 for (unsigned i = 0; i < Half; ++i)
1630 if (!isUndefOrInRange(N[i], 0, NumElems))
1631 return false;
1632 for (unsigned i = Half; i < NumElems; ++i)
1633 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1634 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001635
1636 return true;
1637}
1638
Evan Cheng39623da2006-04-20 08:58:49 +00001639bool X86::isSHUFPMask(SDNode *N) {
1640 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1641 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1642 return ::isSHUFPMask(Ops);
1643}
1644
1645/// isCommutedSHUFP - Returns true if the shuffle mask is except
1646/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1647/// half elements to come from vector 1 (which would equal the dest.) and
1648/// the upper half to come from vector 2.
1649static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1650 unsigned NumElems = Ops.size();
1651 if (NumElems != 2 && NumElems != 4) return false;
1652
1653 unsigned Half = NumElems / 2;
1654 for (unsigned i = 0; i < Half; ++i)
1655 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1656 return false;
1657 for (unsigned i = Half; i < NumElems; ++i)
1658 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1659 return false;
1660 return true;
1661}
1662
1663static bool isCommutedSHUFP(SDNode *N) {
1664 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1665 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1666 return isCommutedSHUFP(Ops);
1667}
1668
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001669/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1670/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1671bool X86::isMOVHLPSMask(SDNode *N) {
1672 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1673
Evan Cheng2064a2b2006-03-28 06:50:32 +00001674 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001675 return false;
1676
Evan Cheng2064a2b2006-03-28 06:50:32 +00001677 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001678 return isUndefOrEqual(N->getOperand(0), 6) &&
1679 isUndefOrEqual(N->getOperand(1), 7) &&
1680 isUndefOrEqual(N->getOperand(2), 2) &&
1681 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001682}
1683
Evan Cheng5ced1d82006-04-06 23:23:56 +00001684/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1685/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1686bool X86::isMOVLPMask(SDNode *N) {
1687 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1688
1689 unsigned NumElems = N->getNumOperands();
1690 if (NumElems != 2 && NumElems != 4)
1691 return false;
1692
Evan Chengc5cdff22006-04-07 21:53:05 +00001693 for (unsigned i = 0; i < NumElems/2; ++i)
1694 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1695 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001696
Evan Chengc5cdff22006-04-07 21:53:05 +00001697 for (unsigned i = NumElems/2; i < NumElems; ++i)
1698 if (!isUndefOrEqual(N->getOperand(i), i))
1699 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001700
1701 return true;
1702}
1703
1704/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001705/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1706/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001707bool X86::isMOVHPMask(SDNode *N) {
1708 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1709
1710 unsigned NumElems = N->getNumOperands();
1711 if (NumElems != 2 && NumElems != 4)
1712 return false;
1713
Evan Chengc5cdff22006-04-07 21:53:05 +00001714 for (unsigned i = 0; i < NumElems/2; ++i)
1715 if (!isUndefOrEqual(N->getOperand(i), i))
1716 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001717
1718 for (unsigned i = 0; i < NumElems/2; ++i) {
1719 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001720 if (!isUndefOrEqual(Arg, i + NumElems))
1721 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001722 }
1723
1724 return true;
1725}
1726
Evan Cheng0038e592006-03-28 00:39:58 +00001727/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1728/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng39623da2006-04-20 08:58:49 +00001729bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1730 unsigned NumElems = N.size();
Evan Cheng0038e592006-03-28 00:39:58 +00001731 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1732 return false;
1733
1734 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001735 SDOperand BitI = N[i];
1736 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001737 if (!isUndefOrEqual(BitI, j))
1738 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001739 if (V2IsSplat) {
1740 if (isUndefOrEqual(BitI1, NumElems))
1741 return false;
1742 } else {
1743 if (!isUndefOrEqual(BitI1, j + NumElems))
1744 return false;
1745 }
Evan Cheng0038e592006-03-28 00:39:58 +00001746 }
1747
1748 return true;
1749}
1750
Evan Cheng39623da2006-04-20 08:58:49 +00001751bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1752 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1753 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1754 return ::isUNPCKLMask(Ops, V2IsSplat);
1755}
1756
Evan Cheng4fcb9222006-03-28 02:43:26 +00001757/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1758/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng39623da2006-04-20 08:58:49 +00001759bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1760 unsigned NumElems = N.size();
Evan Cheng4fcb9222006-03-28 02:43:26 +00001761 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1762 return false;
1763
1764 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001765 SDOperand BitI = N[i];
1766 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001767 if (!isUndefOrEqual(BitI, j + NumElems/2))
1768 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001769 if (V2IsSplat) {
1770 if (isUndefOrEqual(BitI1, NumElems))
1771 return false;
1772 } else {
1773 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1774 return false;
1775 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001776 }
1777
1778 return true;
1779}
1780
Evan Cheng39623da2006-04-20 08:58:49 +00001781bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1782 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1783 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1784 return ::isUNPCKHMask(Ops, V2IsSplat);
1785}
1786
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001787/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1788/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1789/// <0, 0, 1, 1>
1790bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1791 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1792
1793 unsigned NumElems = N->getNumOperands();
1794 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1795 return false;
1796
1797 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1798 SDOperand BitI = N->getOperand(i);
1799 SDOperand BitI1 = N->getOperand(i+1);
1800
Evan Chengc5cdff22006-04-07 21:53:05 +00001801 if (!isUndefOrEqual(BitI, j))
1802 return false;
1803 if (!isUndefOrEqual(BitI1, j))
1804 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001805 }
1806
1807 return true;
1808}
1809
Evan Cheng017dcc62006-04-21 01:05:10 +00001810/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1811/// specifies a shuffle of elements that is suitable for input to MOVSS,
1812/// MOVSD, and MOVD, i.e. setting the lowest element.
1813static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001814 unsigned NumElems = N.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001815 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001816 return false;
1817
Evan Cheng39623da2006-04-20 08:58:49 +00001818 if (!isUndefOrEqual(N[0], NumElems))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001819 return false;
1820
1821 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00001822 SDOperand Arg = N[i];
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001823 if (!isUndefOrEqual(Arg, i))
1824 return false;
1825 }
1826
1827 return true;
1828}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001829
Evan Cheng017dcc62006-04-21 01:05:10 +00001830bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001831 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1832 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001833 return ::isMOVLMask(Ops);
Evan Cheng39623da2006-04-20 08:58:49 +00001834}
1835
Evan Cheng017dcc62006-04-21 01:05:10 +00001836/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1837/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00001838/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Cheng017dcc62006-04-21 01:05:10 +00001839static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001840 unsigned NumElems = Ops.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001841 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00001842 return false;
1843
1844 if (!isUndefOrEqual(Ops[0], 0))
1845 return false;
1846
1847 for (unsigned i = 1; i < NumElems; ++i) {
1848 SDOperand Arg = Ops[i];
1849 if (V2IsSplat) {
1850 if (!isUndefOrEqual(Arg, NumElems))
1851 return false;
1852 } else {
1853 if (!isUndefOrEqual(Arg, i+NumElems))
1854 return false;
1855 }
1856 }
1857
1858 return true;
1859}
1860
Evan Cheng017dcc62006-04-21 01:05:10 +00001861static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001862 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1863 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001864 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001865}
1866
Evan Chengd9539472006-04-14 21:59:03 +00001867/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1868/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1869bool X86::isMOVSHDUPMask(SDNode *N) {
1870 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1871
1872 if (N->getNumOperands() != 4)
1873 return false;
1874
1875 // Expect 1, 1, 3, 3
1876 for (unsigned i = 0; i < 2; ++i) {
1877 SDOperand Arg = N->getOperand(i);
1878 if (Arg.getOpcode() == ISD::UNDEF) continue;
1879 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1880 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1881 if (Val != 1) return false;
1882 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001883
1884 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001885 for (unsigned i = 2; i < 4; ++i) {
1886 SDOperand Arg = N->getOperand(i);
1887 if (Arg.getOpcode() == ISD::UNDEF) continue;
1888 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1889 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1890 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001891 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001892 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001893
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001894 // Don't use movshdup if it can be done with a shufps.
1895 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001896}
1897
1898/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1899/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1900bool X86::isMOVSLDUPMask(SDNode *N) {
1901 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1902
1903 if (N->getNumOperands() != 4)
1904 return false;
1905
1906 // Expect 0, 0, 2, 2
1907 for (unsigned i = 0; i < 2; ++i) {
1908 SDOperand Arg = N->getOperand(i);
1909 if (Arg.getOpcode() == ISD::UNDEF) continue;
1910 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1911 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1912 if (Val != 0) return false;
1913 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001914
1915 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001916 for (unsigned i = 2; i < 4; ++i) {
1917 SDOperand Arg = N->getOperand(i);
1918 if (Arg.getOpcode() == ISD::UNDEF) continue;
1919 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1920 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1921 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001922 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001923 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001924
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001925 // Don't use movshdup if it can be done with a shufps.
1926 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001927}
1928
Evan Chengb9df0ca2006-03-22 02:53:00 +00001929/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1930/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001931static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001932 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1933
Evan Chengb9df0ca2006-03-22 02:53:00 +00001934 // This is a splat operation if each element of the permute is the same, and
1935 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001936 unsigned NumElems = N->getNumOperands();
1937 SDOperand ElementBase;
1938 unsigned i = 0;
1939 for (; i != NumElems; ++i) {
1940 SDOperand Elt = N->getOperand(i);
1941 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1942 ElementBase = Elt;
1943 break;
1944 }
1945 }
1946
1947 if (!ElementBase.Val)
1948 return false;
1949
1950 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001951 SDOperand Arg = N->getOperand(i);
1952 if (Arg.getOpcode() == ISD::UNDEF) continue;
1953 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001954 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001955 }
1956
1957 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001958 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001959}
1960
Evan Chengc575ca22006-04-17 20:43:08 +00001961/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1962/// a splat of a single element and it's a 2 or 4 element mask.
1963bool X86::isSplatMask(SDNode *N) {
1964 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1965
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001966 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00001967 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1968 return false;
1969 return ::isSplatMask(N);
1970}
1971
Evan Cheng63d33002006-03-22 08:01:21 +00001972/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1973/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1974/// instructions.
1975unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001976 unsigned NumOperands = N->getNumOperands();
1977 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1978 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001979 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001980 unsigned Val = 0;
1981 SDOperand Arg = N->getOperand(NumOperands-i-1);
1982 if (Arg.getOpcode() != ISD::UNDEF)
1983 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001984 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001985 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001986 if (i != NumOperands - 1)
1987 Mask <<= Shift;
1988 }
Evan Cheng63d33002006-03-22 08:01:21 +00001989
1990 return Mask;
1991}
1992
Evan Cheng506d3df2006-03-29 23:07:14 +00001993/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1994/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1995/// instructions.
1996unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1997 unsigned Mask = 0;
1998 // 8 nodes, but we only care about the last 4.
1999 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002000 unsigned Val = 0;
2001 SDOperand Arg = N->getOperand(i);
2002 if (Arg.getOpcode() != ISD::UNDEF)
2003 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002004 Mask |= (Val - 4);
2005 if (i != 4)
2006 Mask <<= 2;
2007 }
2008
2009 return Mask;
2010}
2011
2012/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2013/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2014/// instructions.
2015unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2016 unsigned Mask = 0;
2017 // 8 nodes, but we only care about the first 4.
2018 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002019 unsigned Val = 0;
2020 SDOperand Arg = N->getOperand(i);
2021 if (Arg.getOpcode() != ISD::UNDEF)
2022 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002023 Mask |= Val;
2024 if (i != 0)
2025 Mask <<= 2;
2026 }
2027
2028 return Mask;
2029}
2030
Evan Chengc21a0532006-04-05 01:47:37 +00002031/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2032/// specifies a 8 element shuffle that can be broken into a pair of
2033/// PSHUFHW and PSHUFLW.
2034static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2035 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2036
2037 if (N->getNumOperands() != 8)
2038 return false;
2039
2040 // Lower quadword shuffled.
2041 for (unsigned i = 0; i != 4; ++i) {
2042 SDOperand Arg = N->getOperand(i);
2043 if (Arg.getOpcode() == ISD::UNDEF) continue;
2044 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2045 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2046 if (Val > 4)
2047 return false;
2048 }
2049
2050 // Upper quadword shuffled.
2051 for (unsigned i = 4; i != 8; ++i) {
2052 SDOperand Arg = N->getOperand(i);
2053 if (Arg.getOpcode() == ISD::UNDEF) continue;
2054 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2055 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2056 if (Val < 4 || Val > 7)
2057 return false;
2058 }
2059
2060 return true;
2061}
2062
Evan Cheng5ced1d82006-04-06 23:23:56 +00002063/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2064/// values in ther permute mask.
2065static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2066 SDOperand V1 = Op.getOperand(0);
2067 SDOperand V2 = Op.getOperand(1);
2068 SDOperand Mask = Op.getOperand(2);
2069 MVT::ValueType VT = Op.getValueType();
2070 MVT::ValueType MaskVT = Mask.getValueType();
2071 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2072 unsigned NumElems = Mask.getNumOperands();
2073 std::vector<SDOperand> MaskVec;
2074
2075 for (unsigned i = 0; i != NumElems; ++i) {
2076 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002077 if (Arg.getOpcode() == ISD::UNDEF) {
2078 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2079 continue;
2080 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2082 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2083 if (Val < NumElems)
2084 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2085 else
2086 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2087 }
2088
2089 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2090 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2091}
2092
Evan Cheng533a0aa2006-04-19 20:35:22 +00002093/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2094/// match movhlps. The lower half elements should come from upper half of
2095/// V1 (and in order), and the upper half elements should come from the upper
2096/// half of V2 (and in order).
2097static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2098 unsigned NumElems = Mask->getNumOperands();
2099 if (NumElems != 4)
2100 return false;
2101 for (unsigned i = 0, e = 2; i != e; ++i)
2102 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2103 return false;
2104 for (unsigned i = 2; i != 4; ++i)
2105 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2106 return false;
2107 return true;
2108}
2109
Evan Cheng5ced1d82006-04-06 23:23:56 +00002110/// isScalarLoadToVector - Returns true if the node is a scalar load that
2111/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002112static inline bool isScalarLoadToVector(SDNode *N) {
2113 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2114 N = N->getOperand(0).Val;
2115 return (N->getOpcode() == ISD::LOAD);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002116 }
2117 return false;
2118}
2119
Evan Cheng533a0aa2006-04-19 20:35:22 +00002120/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2121/// match movlp{s|d}. The lower half elements should come from lower half of
2122/// V1 (and in order), and the upper half elements should come from the upper
2123/// half of V2 (and in order). And since V1 will become the source of the
2124/// MOVLP, it must be either a vector load or a scalar load to vector.
2125static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2126 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2127 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002128
Evan Cheng533a0aa2006-04-19 20:35:22 +00002129 unsigned NumElems = Mask->getNumOperands();
2130 if (NumElems != 2 && NumElems != 4)
2131 return false;
2132 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2133 if (!isUndefOrEqual(Mask->getOperand(i), i))
2134 return false;
2135 for (unsigned i = NumElems/2; i != NumElems; ++i)
2136 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2137 return false;
2138 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002139}
2140
Evan Cheng39623da2006-04-20 08:58:49 +00002141/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2142/// all the same.
2143static bool isSplatVector(SDNode *N) {
2144 if (N->getOpcode() != ISD::BUILD_VECTOR)
2145 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002146
Evan Cheng39623da2006-04-20 08:58:49 +00002147 SDOperand SplatValue = N->getOperand(0);
2148 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2149 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002150 return false;
2151 return true;
2152}
2153
Evan Cheng39623da2006-04-20 08:58:49 +00002154/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2155/// that point to V2 points to its first element.
2156static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2157 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2158
2159 bool Changed = false;
2160 std::vector<SDOperand> MaskVec;
2161 unsigned NumElems = Mask.getNumOperands();
2162 for (unsigned i = 0; i != NumElems; ++i) {
2163 SDOperand Arg = Mask.getOperand(i);
2164 if (Arg.getOpcode() != ISD::UNDEF) {
2165 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2166 if (Val > NumElems) {
2167 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2168 Changed = true;
2169 }
2170 }
2171 MaskVec.push_back(Arg);
2172 }
2173
2174 if (Changed)
2175 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2176 return Mask;
2177}
2178
Evan Cheng017dcc62006-04-21 01:05:10 +00002179/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2180/// operation of specified width.
2181static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002182 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2183 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2184
2185 std::vector<SDOperand> MaskVec;
2186 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2187 for (unsigned i = 1; i != NumElems; ++i)
2188 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2189 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2190}
2191
Evan Chengc575ca22006-04-17 20:43:08 +00002192/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2193/// of specified width.
2194static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2195 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2196 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2197 std::vector<SDOperand> MaskVec;
2198 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2199 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2200 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2201 }
2202 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2203}
2204
Evan Cheng39623da2006-04-20 08:58:49 +00002205/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2206/// of specified width.
2207static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2208 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2209 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2210 unsigned Half = NumElems/2;
2211 std::vector<SDOperand> MaskVec;
2212 for (unsigned i = 0; i != Half; ++i) {
2213 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2214 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2215 }
2216 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2217}
2218
Evan Cheng017dcc62006-04-21 01:05:10 +00002219/// getZeroVector - Returns a vector of specified type with all zero elements.
2220///
2221static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2222 assert(MVT::isVector(VT) && "Expected a vector type");
2223 unsigned NumElems = getVectorNumElements(VT);
2224 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2225 bool isFP = MVT::isFloatingPoint(EVT);
2226 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2227 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2228 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2229}
2230
Evan Chengc575ca22006-04-17 20:43:08 +00002231/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2232///
2233static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2234 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002235 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002236 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002237 unsigned NumElems = Mask.getNumOperands();
2238 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002239 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002240 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002241 NumElems >>= 1;
2242 }
2243 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2244
2245 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002246 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002247 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002248 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002249 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2250}
2251
Evan Cheng017dcc62006-04-21 01:05:10 +00002252/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2253/// constant +0.0.
2254static inline bool isZeroNode(SDOperand Elt) {
2255 return ((isa<ConstantSDNode>(Elt) &&
2256 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2257 (isa<ConstantFPSDNode>(Elt) &&
2258 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2259}
2260
Evan Chengba05f722006-04-21 23:03:30 +00002261/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2262/// vector and zero or undef vector.
2263static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002264 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002265 bool isZero, SelectionDAG &DAG) {
2266 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002267 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2268 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2269 SDOperand Zero = DAG.getConstant(0, EVT);
2270 std::vector<SDOperand> MaskVec(NumElems, Zero);
2271 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2272 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Chengba05f722006-04-21 23:03:30 +00002273 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002274}
2275
Evan Chengc78d3b42006-04-24 18:01:45 +00002276/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2277///
2278static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2279 unsigned NumNonZero, unsigned NumZero,
2280 SelectionDAG &DAG) {
2281 if (NumNonZero > 8)
2282 return SDOperand();
2283
2284 SDOperand V(0, 0);
2285 bool First = true;
2286 for (unsigned i = 0; i < 16; ++i) {
2287 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2288 if (ThisIsNonZero && First) {
2289 if (NumZero)
2290 V = getZeroVector(MVT::v8i16, DAG);
2291 else
2292 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2293 First = false;
2294 }
2295
2296 if ((i & 1) != 0) {
2297 SDOperand ThisElt(0, 0), LastElt(0, 0);
2298 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2299 if (LastIsNonZero) {
2300 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2301 }
2302 if (ThisIsNonZero) {
2303 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2304 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2305 ThisElt, DAG.getConstant(8, MVT::i8));
2306 if (LastIsNonZero)
2307 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2308 } else
2309 ThisElt = LastElt;
2310
2311 if (ThisElt.Val)
2312 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2313 DAG.getConstant(i/2, MVT::i32));
2314 }
2315 }
2316
2317 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2318}
2319
2320/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2321///
2322static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2323 unsigned NumNonZero, unsigned NumZero,
2324 SelectionDAG &DAG) {
2325 if (NumNonZero > 4)
2326 return SDOperand();
2327
2328 SDOperand V(0, 0);
2329 bool First = true;
2330 for (unsigned i = 0; i < 8; ++i) {
2331 bool isNonZero = (NonZeros & (1 << i)) != 0;
2332 if (isNonZero) {
2333 if (First) {
2334 if (NumZero)
2335 V = getZeroVector(MVT::v8i16, DAG);
2336 else
2337 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2338 First = false;
2339 }
2340 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2341 DAG.getConstant(i, MVT::i32));
2342 }
2343 }
2344
2345 return V;
2346}
2347
Evan Cheng0db9fe62006-04-25 20:13:52 +00002348SDOperand
2349X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2350 // All zero's are handled with pxor.
2351 if (ISD::isBuildVectorAllZeros(Op.Val))
2352 return Op;
2353
2354 // All one's are handled with pcmpeqd.
2355 if (ISD::isBuildVectorAllOnes(Op.Val))
2356 return Op;
2357
2358 MVT::ValueType VT = Op.getValueType();
2359 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2360 unsigned EVTBits = MVT::getSizeInBits(EVT);
2361
2362 unsigned NumElems = Op.getNumOperands();
2363 unsigned NumZero = 0;
2364 unsigned NumNonZero = 0;
2365 unsigned NonZeros = 0;
2366 std::set<SDOperand> Values;
2367 for (unsigned i = 0; i < NumElems; ++i) {
2368 SDOperand Elt = Op.getOperand(i);
2369 if (Elt.getOpcode() != ISD::UNDEF) {
2370 Values.insert(Elt);
2371 if (isZeroNode(Elt))
2372 NumZero++;
2373 else {
2374 NonZeros |= (1 << i);
2375 NumNonZero++;
2376 }
2377 }
2378 }
2379
2380 if (NumNonZero == 0)
2381 // Must be a mix of zero and undef. Return a zero vector.
2382 return getZeroVector(VT, DAG);
2383
2384 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2385 if (Values.size() == 1)
2386 return SDOperand();
2387
2388 // Special case for single non-zero element.
2389 if (NumNonZero == 1) {
2390 unsigned Idx = CountTrailingZeros_32(NonZeros);
2391 SDOperand Item = Op.getOperand(Idx);
2392 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2393 if (Idx == 0)
2394 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2395 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2396 NumZero > 0, DAG);
2397
2398 if (EVTBits == 32) {
2399 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2400 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2401 DAG);
2402 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2403 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2404 std::vector<SDOperand> MaskVec;
2405 for (unsigned i = 0; i < NumElems; i++)
2406 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2407 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2408 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2409 DAG.getNode(ISD::UNDEF, VT), Mask);
2410 }
2411 }
2412
2413 // Let legalizer expand 2-widde build_vector's.
2414 if (EVTBits == 64)
2415 return SDOperand();
2416
2417 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2418 if (EVTBits == 8) {
2419 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2420 if (V.Val) return V;
2421 }
2422
2423 if (EVTBits == 16) {
2424 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2425 if (V.Val) return V;
2426 }
2427
2428 // If element VT is == 32 bits, turn it into a number of shuffles.
2429 std::vector<SDOperand> V(NumElems);
2430 if (NumElems == 4 && NumZero > 0) {
2431 for (unsigned i = 0; i < 4; ++i) {
2432 bool isZero = !(NonZeros & (1 << i));
2433 if (isZero)
2434 V[i] = getZeroVector(VT, DAG);
2435 else
2436 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2437 }
2438
2439 for (unsigned i = 0; i < 2; ++i) {
2440 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2441 default: break;
2442 case 0:
2443 V[i] = V[i*2]; // Must be a zero vector.
2444 break;
2445 case 1:
2446 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2447 getMOVLMask(NumElems, DAG));
2448 break;
2449 case 2:
2450 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2451 getMOVLMask(NumElems, DAG));
2452 break;
2453 case 3:
2454 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2455 getUnpacklMask(NumElems, DAG));
2456 break;
2457 }
2458 }
2459
Evan Cheng069287d2006-05-16 07:21:53 +00002460 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002461 // clears the upper bits.
2462 // FIXME: we can do the same for v4f32 case when we know both parts of
2463 // the lower half come from scalar_to_vector (loadf32). We should do
2464 // that in post legalizer dag combiner with target specific hooks.
2465 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2466 return V[0];
2467 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2468 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2469 std::vector<SDOperand> MaskVec;
2470 bool Reverse = (NonZeros & 0x3) == 2;
2471 for (unsigned i = 0; i < 2; ++i)
2472 if (Reverse)
2473 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2474 else
2475 MaskVec.push_back(DAG.getConstant(i, EVT));
2476 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2477 for (unsigned i = 0; i < 2; ++i)
2478 if (Reverse)
2479 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2480 else
2481 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2482 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2483 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2484 }
2485
2486 if (Values.size() > 2) {
2487 // Expand into a number of unpckl*.
2488 // e.g. for v4f32
2489 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2490 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2491 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2492 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2493 for (unsigned i = 0; i < NumElems; ++i)
2494 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2495 NumElems >>= 1;
2496 while (NumElems != 0) {
2497 for (unsigned i = 0; i < NumElems; ++i)
2498 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2499 UnpckMask);
2500 NumElems >>= 1;
2501 }
2502 return V[0];
2503 }
2504
2505 return SDOperand();
2506}
2507
2508SDOperand
2509X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2510 SDOperand V1 = Op.getOperand(0);
2511 SDOperand V2 = Op.getOperand(1);
2512 SDOperand PermMask = Op.getOperand(2);
2513 MVT::ValueType VT = Op.getValueType();
2514 unsigned NumElems = PermMask.getNumOperands();
2515 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2516 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2517
2518 if (isSplatMask(PermMask.Val)) {
2519 if (NumElems <= 4) return Op;
2520 // Promote it to a v4i32 splat.
2521 return PromoteSplat(Op, DAG);
2522 }
2523
2524 if (X86::isMOVLMask(PermMask.Val))
2525 return (V1IsUndef) ? V2 : Op;
2526
2527 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2528 X86::isMOVSLDUPMask(PermMask.Val) ||
2529 X86::isMOVHLPSMask(PermMask.Val) ||
2530 X86::isMOVHPMask(PermMask.Val) ||
2531 X86::isMOVLPMask(PermMask.Val))
2532 return Op;
2533
2534 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2535 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2536 return CommuteVectorShuffle(Op, DAG);
2537
2538 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2539 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2540 if (V1IsSplat && !V2IsSplat) {
2541 Op = CommuteVectorShuffle(Op, DAG);
2542 V1 = Op.getOperand(0);
2543 V2 = Op.getOperand(1);
2544 PermMask = Op.getOperand(2);
2545 V2IsSplat = true;
2546 }
2547
2548 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2549 if (V2IsUndef) return V1;
2550 Op = CommuteVectorShuffle(Op, DAG);
2551 V1 = Op.getOperand(0);
2552 V2 = Op.getOperand(1);
2553 PermMask = Op.getOperand(2);
2554 if (V2IsSplat) {
2555 // V2 is a splat, so the mask may be malformed. That is, it may point
2556 // to any V2 element. The instruction selectior won't like this. Get
2557 // a corrected mask and commute to form a proper MOVS{S|D}.
2558 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2559 if (NewMask.Val != PermMask.Val)
2560 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2561 }
2562 return Op;
2563 }
2564
2565 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2566 X86::isUNPCKLMask(PermMask.Val) ||
2567 X86::isUNPCKHMask(PermMask.Val))
2568 return Op;
2569
2570 if (V2IsSplat) {
2571 // Normalize mask so all entries that point to V2 points to its first
2572 // element then try to match unpck{h|l} again. If match, return a
2573 // new vector_shuffle with the corrected mask.
2574 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2575 if (NewMask.Val != PermMask.Val) {
2576 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2577 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2578 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2579 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2580 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2581 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2582 }
2583 }
2584 }
2585
2586 // Normalize the node to match x86 shuffle ops if needed
2587 if (V2.getOpcode() != ISD::UNDEF)
2588 if (isCommutedSHUFP(PermMask.Val)) {
2589 Op = CommuteVectorShuffle(Op, DAG);
2590 V1 = Op.getOperand(0);
2591 V2 = Op.getOperand(1);
2592 PermMask = Op.getOperand(2);
2593 }
2594
2595 // If VT is integer, try PSHUF* first, then SHUFP*.
2596 if (MVT::isInteger(VT)) {
2597 if (X86::isPSHUFDMask(PermMask.Val) ||
2598 X86::isPSHUFHWMask(PermMask.Val) ||
2599 X86::isPSHUFLWMask(PermMask.Val)) {
2600 if (V2.getOpcode() != ISD::UNDEF)
2601 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2602 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2603 return Op;
2604 }
2605
2606 if (X86::isSHUFPMask(PermMask.Val))
2607 return Op;
2608
2609 // Handle v8i16 shuffle high / low shuffle node pair.
2610 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2611 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2612 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2613 std::vector<SDOperand> MaskVec;
2614 for (unsigned i = 0; i != 4; ++i)
2615 MaskVec.push_back(PermMask.getOperand(i));
2616 for (unsigned i = 4; i != 8; ++i)
2617 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2618 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2619 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2620 MaskVec.clear();
2621 for (unsigned i = 0; i != 4; ++i)
2622 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2623 for (unsigned i = 4; i != 8; ++i)
2624 MaskVec.push_back(PermMask.getOperand(i));
2625 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2626 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2627 }
2628 } else {
2629 // Floating point cases in the other order.
2630 if (X86::isSHUFPMask(PermMask.Val))
2631 return Op;
2632 if (X86::isPSHUFDMask(PermMask.Val) ||
2633 X86::isPSHUFHWMask(PermMask.Val) ||
2634 X86::isPSHUFLWMask(PermMask.Val)) {
2635 if (V2.getOpcode() != ISD::UNDEF)
2636 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2637 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2638 return Op;
2639 }
2640 }
2641
2642 if (NumElems == 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002643 MVT::ValueType MaskVT = PermMask.getValueType();
2644 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng43f3bd32006-04-28 07:03:38 +00002645 std::vector<std::pair<int, int> > Locs;
2646 Locs.reserve(NumElems);
2647 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2648 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2649 unsigned NumHi = 0;
2650 unsigned NumLo = 0;
2651 // If no more than two elements come from either vector. This can be
2652 // implemented with two shuffles. First shuffle gather the elements.
2653 // The second shuffle, which takes the first shuffle as both of its
2654 // vector operands, put the elements into the right order.
2655 for (unsigned i = 0; i != NumElems; ++i) {
2656 SDOperand Elt = PermMask.getOperand(i);
2657 if (Elt.getOpcode() == ISD::UNDEF) {
2658 Locs[i] = std::make_pair(-1, -1);
2659 } else {
2660 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2661 if (Val < NumElems) {
2662 Locs[i] = std::make_pair(0, NumLo);
2663 Mask1[NumLo] = Elt;
2664 NumLo++;
2665 } else {
2666 Locs[i] = std::make_pair(1, NumHi);
2667 if (2+NumHi < NumElems)
2668 Mask1[2+NumHi] = Elt;
2669 NumHi++;
2670 }
2671 }
2672 }
2673 if (NumLo <= 2 && NumHi <= 2) {
2674 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2675 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2676 for (unsigned i = 0; i != NumElems; ++i) {
2677 if (Locs[i].first == -1)
2678 continue;
2679 else {
2680 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2681 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2682 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2683 }
2684 }
2685
2686 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2687 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2688 }
2689
2690 // Break it into (shuffle shuffle_hi, shuffle_lo).
2691 Locs.clear();
Evan Cheng0db9fe62006-04-25 20:13:52 +00002692 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2693 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2694 std::vector<SDOperand> *MaskPtr = &LoMask;
2695 unsigned MaskIdx = 0;
2696 unsigned LoIdx = 0;
2697 unsigned HiIdx = NumElems/2;
2698 for (unsigned i = 0; i != NumElems; ++i) {
2699 if (i == NumElems/2) {
2700 MaskPtr = &HiMask;
2701 MaskIdx = 1;
2702 LoIdx = 0;
2703 HiIdx = NumElems/2;
2704 }
2705 SDOperand Elt = PermMask.getOperand(i);
2706 if (Elt.getOpcode() == ISD::UNDEF) {
2707 Locs[i] = std::make_pair(-1, -1);
2708 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2709 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2710 (*MaskPtr)[LoIdx] = Elt;
2711 LoIdx++;
2712 } else {
2713 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2714 (*MaskPtr)[HiIdx] = Elt;
2715 HiIdx++;
2716 }
2717 }
2718
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002719 SDOperand LoShuffle =
2720 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2721 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2722 SDOperand HiShuffle =
2723 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2724 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002725 std::vector<SDOperand> MaskOps;
2726 for (unsigned i = 0; i != NumElems; ++i) {
2727 if (Locs[i].first == -1) {
2728 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2729 } else {
2730 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2731 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2732 }
2733 }
2734 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2735 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2736 }
2737
2738 return SDOperand();
2739}
2740
2741SDOperand
2742X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2743 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2744 return SDOperand();
2745
2746 MVT::ValueType VT = Op.getValueType();
2747 // TODO: handle v16i8.
2748 if (MVT::getSizeInBits(VT) == 16) {
2749 // Transform it so it match pextrw which produces a 32-bit result.
2750 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2751 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2752 Op.getOperand(0), Op.getOperand(1));
2753 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2754 DAG.getValueType(VT));
2755 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2756 } else if (MVT::getSizeInBits(VT) == 32) {
2757 SDOperand Vec = Op.getOperand(0);
2758 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2759 if (Idx == 0)
2760 return Op;
2761
2762 // SHUFPS the element to the lowest double word, then movss.
2763 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2764 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2765 MVT::getVectorBaseType(MaskVT));
2766 std::vector<SDOperand> IdxVec;
2767 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2768 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2769 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2770 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2771 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2772 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2773 Vec, Vec, Mask);
2774 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2775 DAG.getConstant(0, MVT::i32));
2776 } else if (MVT::getSizeInBits(VT) == 64) {
2777 SDOperand Vec = Op.getOperand(0);
2778 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2779 if (Idx == 0)
2780 return Op;
2781
2782 // UNPCKHPD the element to the lowest double word, then movsd.
2783 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2784 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2785 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2786 std::vector<SDOperand> IdxVec;
2787 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2788 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2789 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2790 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2791 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2792 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2793 DAG.getConstant(0, MVT::i32));
2794 }
2795
2796 return SDOperand();
2797}
2798
2799SDOperand
2800X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00002801 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00002802 // as its second argument.
2803 MVT::ValueType VT = Op.getValueType();
2804 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2805 SDOperand N0 = Op.getOperand(0);
2806 SDOperand N1 = Op.getOperand(1);
2807 SDOperand N2 = Op.getOperand(2);
2808 if (MVT::getSizeInBits(BaseVT) == 16) {
2809 if (N1.getValueType() != MVT::i32)
2810 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2811 if (N2.getValueType() != MVT::i32)
2812 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2813 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2814 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2815 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2816 if (Idx == 0) {
2817 // Use a movss.
2818 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2819 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2820 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2821 std::vector<SDOperand> MaskVec;
2822 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2823 for (unsigned i = 1; i <= 3; ++i)
2824 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2825 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2826 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2827 } else {
2828 // Use two pinsrw instructions to insert a 32 bit value.
2829 Idx <<= 1;
2830 if (MVT::isFloatingPoint(N1.getValueType())) {
2831 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng069287d2006-05-16 07:21:53 +00002832 // Just load directly from f32mem to GR32.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002833 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2834 N1.getOperand(2));
2835 } else {
2836 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2837 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2838 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2839 DAG.getConstant(0, MVT::i32));
2840 }
2841 }
2842 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2843 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2844 DAG.getConstant(Idx, MVT::i32));
2845 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2846 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2847 DAG.getConstant(Idx+1, MVT::i32));
2848 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2849 }
2850 }
2851
2852 return SDOperand();
2853}
2854
2855SDOperand
2856X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2857 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2858 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2859}
2860
2861// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2862// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2863// one of the above mentioned nodes. It has to be wrapped because otherwise
2864// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2865// be used to form addressing mode. These wrapped nodes will be selected
2866// into MOV32ri.
2867SDOperand
2868X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2869 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2870 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2871 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2872 CP->getAlignment()));
2873 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(),
2877 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2878 }
2879
2880 return Result;
2881}
2882
2883SDOperand
2884X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2885 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2886 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002887 DAG.getTargetGlobalAddress(GV,
2888 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002889 if (Subtarget->isTargetDarwin()) {
2890 // With PIC, the address is actually $g + Offset.
2891 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2892 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002893 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2894 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002895
2896 // For Darwin, external and weak symbols are indirect, so we want to load
2897 // the value at address GV, not the value of GV itself. This means that
2898 // the GlobalAddress must be in the base or index register of the address,
2899 // not the GV offset field.
2900 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2901 DarwinGVRequiresExtraLoad(GV))
2902 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2903 Result, DAG.getSrcValue(NULL));
2904 }
2905
2906 return Result;
2907}
2908
2909SDOperand
2910X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2911 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2912 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002913 DAG.getTargetExternalSymbol(Sym,
2914 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002915 if (Subtarget->isTargetDarwin()) {
2916 // With PIC, the address is actually $g + Offset.
2917 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2918 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002919 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2920 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002921 }
2922
2923 return Result;
2924}
2925
2926SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00002927 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2928 "Not an i64 shift!");
2929 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2930 SDOperand ShOpLo = Op.getOperand(0);
2931 SDOperand ShOpHi = Op.getOperand(1);
2932 SDOperand ShAmt = Op.getOperand(2);
2933 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00002934 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00002935 : DAG.getConstant(0, MVT::i32);
2936
2937 SDOperand Tmp2, Tmp3;
2938 if (Op.getOpcode() == ISD::SHL_PARTS) {
2939 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2940 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2941 } else {
2942 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00002943 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00002944 }
2945
2946 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2947 ShAmt, DAG.getConstant(32, MVT::i8));
2948
2949 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00002950 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00002951
2952 std::vector<MVT::ValueType> Tys;
2953 Tys.push_back(MVT::i32);
2954 Tys.push_back(MVT::Flag);
2955 std::vector<SDOperand> Ops;
2956 if (Op.getOpcode() == ISD::SHL_PARTS) {
2957 Ops.push_back(Tmp2);
2958 Ops.push_back(Tmp3);
2959 Ops.push_back(CC);
2960 Ops.push_back(InFlag);
2961 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2962 InFlag = Hi.getValue(1);
2963
2964 Ops.clear();
2965 Ops.push_back(Tmp3);
2966 Ops.push_back(Tmp1);
2967 Ops.push_back(CC);
2968 Ops.push_back(InFlag);
2969 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2970 } else {
2971 Ops.push_back(Tmp2);
2972 Ops.push_back(Tmp3);
2973 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00002974 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00002975 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2976 InFlag = Lo.getValue(1);
2977
2978 Ops.clear();
2979 Ops.push_back(Tmp3);
2980 Ops.push_back(Tmp1);
2981 Ops.push_back(CC);
2982 Ops.push_back(InFlag);
2983 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2984 }
2985
2986 Tys.clear();
2987 Tys.push_back(MVT::i32);
2988 Tys.push_back(MVT::i32);
2989 Ops.clear();
2990 Ops.push_back(Lo);
2991 Ops.push_back(Hi);
2992 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002993}
Evan Chenga3195e82006-01-12 22:54:21 +00002994
Evan Cheng0db9fe62006-04-25 20:13:52 +00002995SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2996 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2997 Op.getOperand(0).getValueType() >= MVT::i16 &&
2998 "Unknown SINT_TO_FP to lower!");
2999
3000 SDOperand Result;
3001 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3002 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3003 MachineFunction &MF = DAG.getMachineFunction();
3004 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3005 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3006 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
3007 DAG.getEntryNode(), Op.getOperand(0),
3008 StackSlot, DAG.getSrcValue(NULL));
3009
3010 // Build the FILD
3011 std::vector<MVT::ValueType> Tys;
3012 Tys.push_back(MVT::f64);
3013 Tys.push_back(MVT::Other);
3014 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3015 std::vector<SDOperand> Ops;
3016 Ops.push_back(Chain);
3017 Ops.push_back(StackSlot);
3018 Ops.push_back(DAG.getValueType(SrcVT));
3019 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3020 Tys, Ops);
3021
3022 if (X86ScalarSSE) {
3023 Chain = Result.getValue(1);
3024 SDOperand InFlag = Result.getValue(2);
3025
3026 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3027 // shouldn't be necessary except that RFP cannot be live across
3028 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003029 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003030 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003031 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00003032 std::vector<MVT::ValueType> Tys;
Evan Cheng6dab0532006-01-30 08:02:57 +00003033 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003034 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003035 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003036 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003037 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003038 Ops.push_back(DAG.getValueType(Op.getValueType()));
3039 Ops.push_back(InFlag);
3040 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3041 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3042 DAG.getSrcValue(NULL));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003043 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003044
Evan Cheng0db9fe62006-04-25 20:13:52 +00003045 return Result;
3046}
3047
3048SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3049 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3050 "Unknown FP_TO_SINT to lower!");
3051 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3052 // stack slot.
3053 MachineFunction &MF = DAG.getMachineFunction();
3054 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3055 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3056 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3057
3058 unsigned Opc;
3059 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003060 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3061 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3062 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3063 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003064 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003065
Evan Cheng0db9fe62006-04-25 20:13:52 +00003066 SDOperand Chain = DAG.getEntryNode();
3067 SDOperand Value = Op.getOperand(0);
3068 if (X86ScalarSSE) {
3069 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3070 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3071 DAG.getSrcValue(0));
3072 std::vector<MVT::ValueType> Tys;
3073 Tys.push_back(MVT::f64);
3074 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003075 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00003076 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003077 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003078 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3079 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3080 Chain = Value.getValue(1);
3081 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3082 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3083 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003084
Evan Cheng0db9fe62006-04-25 20:13:52 +00003085 // Build the FP_TO_INT*_IN_MEM
3086 std::vector<SDOperand> Ops;
3087 Ops.push_back(Chain);
3088 Ops.push_back(Value);
3089 Ops.push_back(StackSlot);
3090 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Chengd9558e02006-01-06 00:43:03 +00003091
Evan Cheng0db9fe62006-04-25 20:13:52 +00003092 // Load the result.
3093 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3094 DAG.getSrcValue(NULL));
3095}
3096
3097SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3098 MVT::ValueType VT = Op.getValueType();
3099 const Type *OpNTy = MVT::getTypeForValueType(VT);
3100 std::vector<Constant*> CV;
3101 if (VT == MVT::f64) {
3102 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3103 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3104 } else {
3105 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3106 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3107 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3108 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3109 }
3110 Constant *CS = ConstantStruct::get(CV);
3111 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3112 SDOperand Mask
3113 = DAG.getNode(X86ISD::LOAD_PACK,
3114 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3115 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3116}
3117
3118SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3119 MVT::ValueType VT = Op.getValueType();
3120 const Type *OpNTy = MVT::getTypeForValueType(VT);
3121 std::vector<Constant*> CV;
3122 if (VT == MVT::f64) {
3123 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3124 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3125 } else {
3126 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3127 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3128 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3129 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3130 }
3131 Constant *CS = ConstantStruct::get(CV);
3132 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3133 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3134 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3135 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3136}
3137
3138SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3139 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3140 SDOperand Cond;
3141 SDOperand CC = Op.getOperand(2);
3142 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3143 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3144 bool Flip;
3145 unsigned X86CC;
3146 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3147 if (Flip)
3148 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3149 Op.getOperand(1), Op.getOperand(0));
3150 else
Evan Cheng6dfa9992006-01-30 23:41:35 +00003151 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3152 Op.getOperand(0), Op.getOperand(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003153 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3154 DAG.getConstant(X86CC, MVT::i8), Cond);
3155 } else {
3156 assert(isFP && "Illegal integer SetCC!");
3157
3158 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3159 Op.getOperand(0), Op.getOperand(1));
3160 std::vector<MVT::ValueType> Tys;
3161 std::vector<SDOperand> Ops;
3162 switch (SetCCOpcode) {
Evan Chengd9558e02006-01-06 00:43:03 +00003163 default: assert(false && "Illegal floating point SetCC!");
3164 case ISD::SETOEQ: { // !PF & ZF
3165 Tys.push_back(MVT::i8);
3166 Tys.push_back(MVT::Flag);
3167 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3168 Ops.push_back(Cond);
3169 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3170 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3171 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3172 Tmp1.getValue(1));
3173 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3174 }
Evan Chengd9558e02006-01-06 00:43:03 +00003175 case ISD::SETUNE: { // PF | !ZF
3176 Tys.push_back(MVT::i8);
3177 Tys.push_back(MVT::Flag);
3178 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3179 Ops.push_back(Cond);
3180 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3181 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3182 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3183 Tmp1.getValue(1));
3184 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3185 }
Evan Chengd9558e02006-01-06 00:43:03 +00003186 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003187 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003188}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003189
Evan Cheng0db9fe62006-04-25 20:13:52 +00003190SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3191 MVT::ValueType VT = Op.getValueType();
3192 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3193 bool addTest = false;
3194 SDOperand Op0 = Op.getOperand(0);
3195 SDOperand Cond, CC;
3196 if (Op0.getOpcode() == ISD::SETCC)
3197 Op0 = LowerOperation(Op0, DAG);
Evan Cheng9bba8942006-01-26 02:13:10 +00003198
Evan Cheng0db9fe62006-04-25 20:13:52 +00003199 if (Op0.getOpcode() == X86ISD::SETCC) {
3200 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3201 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3202 // have another use it will be eliminated.
3203 // If the X86ISD::SETCC has more than one use, then it's probably better
3204 // to use a test instead of duplicating the X86ISD::CMP (for register
3205 // pressure reason).
3206 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3207 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3208 CmpOpc == X86ISD::UCOMI) {
3209 if (!Op0.hasOneUse()) {
3210 std::vector<MVT::ValueType> Tys;
3211 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3212 Tys.push_back(Op0.Val->getValueType(i));
3213 std::vector<SDOperand> Ops;
3214 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3215 Ops.push_back(Op0.getOperand(i));
3216 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3217 }
3218
3219 CC = Op0.getOperand(0);
3220 Cond = Op0.getOperand(1);
3221 // Make a copy as flag result cannot be used by more than one.
3222 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3223 Cond.getOperand(0), Cond.getOperand(1));
3224 addTest =
3225 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00003226 } else
3227 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003228 } else
3229 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00003230
Evan Cheng0db9fe62006-04-25 20:13:52 +00003231 if (addTest) {
3232 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3233 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00003234 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003235
Evan Cheng0db9fe62006-04-25 20:13:52 +00003236 std::vector<MVT::ValueType> Tys;
3237 Tys.push_back(Op.getValueType());
3238 Tys.push_back(MVT::Flag);
3239 std::vector<SDOperand> Ops;
3240 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3241 // condition is true.
3242 Ops.push_back(Op.getOperand(2));
3243 Ops.push_back(Op.getOperand(1));
3244 Ops.push_back(CC);
3245 Ops.push_back(Cond);
3246 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3247}
Evan Cheng9bba8942006-01-26 02:13:10 +00003248
Evan Cheng0db9fe62006-04-25 20:13:52 +00003249SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3250 bool addTest = false;
3251 SDOperand Cond = Op.getOperand(1);
3252 SDOperand Dest = Op.getOperand(2);
3253 SDOperand CC;
3254 if (Cond.getOpcode() == ISD::SETCC)
3255 Cond = LowerOperation(Cond, DAG);
3256
3257 if (Cond.getOpcode() == X86ISD::SETCC) {
3258 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3259 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3260 // have another use it will be eliminated.
3261 // If the X86ISD::SETCC has more than one use, then it's probably better
3262 // to use a test instead of duplicating the X86ISD::CMP (for register
3263 // pressure reason).
3264 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3265 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3266 CmpOpc == X86ISD::UCOMI) {
3267 if (!Cond.hasOneUse()) {
3268 std::vector<MVT::ValueType> Tys;
3269 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3270 Tys.push_back(Cond.Val->getValueType(i));
3271 std::vector<SDOperand> Ops;
3272 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3273 Ops.push_back(Cond.getOperand(i));
3274 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3275 }
3276
3277 CC = Cond.getOperand(0);
3278 Cond = Cond.getOperand(1);
3279 // Make a copy as flag result cannot be used by more than one.
3280 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3281 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00003282 } else
3283 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003284 } else
3285 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00003286
Evan Cheng0db9fe62006-04-25 20:13:52 +00003287 if (addTest) {
3288 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3289 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng898101c2005-12-19 23:12:38 +00003290 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003291 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3292 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3293}
Evan Cheng67f92a72006-01-11 22:15:48 +00003294
Evan Cheng0db9fe62006-04-25 20:13:52 +00003295SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3296 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3297 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3298 DAG.getTargetJumpTable(JT->getIndex(),
3299 getPointerTy()));
3300 if (Subtarget->isTargetDarwin()) {
3301 // With PIC, the address is actually $g + Offset.
3302 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3303 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003304 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3305 Result);
Evan Cheng67f92a72006-01-11 22:15:48 +00003306 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00003307
Evan Cheng0db9fe62006-04-25 20:13:52 +00003308 return Result;
3309}
Evan Cheng7ccced62006-02-18 00:15:05 +00003310
Evan Cheng32fe1032006-05-25 00:59:30 +00003311SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3312 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3313 if (CallingConv == CallingConv::Fast && EnableFastCC)
3314 return LowerFastCCCallTo(Op, DAG);
3315 else
3316 return LowerCCCCallTo(Op, DAG);
3317}
3318
Evan Cheng0db9fe62006-04-25 20:13:52 +00003319SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3320 SDOperand Copy;
Nate Begemanee625572006-01-27 21:09:22 +00003321
Evan Cheng0db9fe62006-04-25 20:13:52 +00003322 switch(Op.getNumOperands()) {
Nate Begemanee625572006-01-27 21:09:22 +00003323 default:
3324 assert(0 && "Do not know how to return this many arguments!");
3325 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003326 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00003327 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003328 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begemanee625572006-01-27 21:09:22 +00003329 case 2: {
3330 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003331
3332 if (MVT::isVector(ArgVT)) {
3333 // Integer or FP vector result -> XMM0.
3334 if (DAG.getMachineFunction().liveout_empty())
3335 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3336 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3337 SDOperand());
3338 } else if (MVT::isInteger(ArgVT)) {
3339 // Integer result -> EAX
3340 if (DAG.getMachineFunction().liveout_empty())
3341 DAG.getMachineFunction().addLiveOut(X86::EAX);
3342
Nate Begemanee625572006-01-27 21:09:22 +00003343 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3344 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00003345 } else if (!X86ScalarSSE) {
3346 // FP return with fp-stack value.
3347 if (DAG.getMachineFunction().liveout_empty())
3348 DAG.getMachineFunction().addLiveOut(X86::ST0);
3349
Nate Begemanee625572006-01-27 21:09:22 +00003350 std::vector<MVT::ValueType> Tys;
3351 Tys.push_back(MVT::Other);
3352 Tys.push_back(MVT::Flag);
3353 std::vector<SDOperand> Ops;
3354 Ops.push_back(Op.getOperand(0));
3355 Ops.push_back(Op.getOperand(1));
3356 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3357 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003358 // FP return with ScalarSSE (return on fp-stack).
3359 if (DAG.getMachineFunction().liveout_empty())
3360 DAG.getMachineFunction().addLiveOut(X86::ST0);
3361
Evan Cheng0d084c92006-02-01 00:20:21 +00003362 SDOperand MemLoc;
3363 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003364 SDOperand Value = Op.getOperand(1);
3365
Evan Cheng760df292006-02-01 01:19:32 +00003366 if (Value.getOpcode() == ISD::LOAD &&
3367 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00003368 Chain = Value.getOperand(0);
3369 MemLoc = Value.getOperand(1);
3370 } else {
3371 // Spill the value to memory and reload it into top of stack.
3372 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3373 MachineFunction &MF = DAG.getMachineFunction();
3374 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3375 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3376 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3377 Value, MemLoc, DAG.getSrcValue(0));
3378 }
Nate Begemanee625572006-01-27 21:09:22 +00003379 std::vector<MVT::ValueType> Tys;
3380 Tys.push_back(MVT::f64);
3381 Tys.push_back(MVT::Other);
3382 std::vector<SDOperand> Ops;
3383 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003384 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00003385 Ops.push_back(DAG.getValueType(ArgVT));
3386 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3387 Tys.clear();
3388 Tys.push_back(MVT::Other);
3389 Tys.push_back(MVT::Flag);
3390 Ops.clear();
3391 Ops.push_back(Copy.getValue(1));
3392 Ops.push_back(Copy);
3393 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3394 }
3395 break;
3396 }
3397 case 3:
Chris Lattnerb2be4032006-04-17 20:32:50 +00003398 if (DAG.getMachineFunction().liveout_empty()) {
3399 DAG.getMachineFunction().addLiveOut(X86::EAX);
3400 DAG.getMachineFunction().addLiveOut(X86::EDX);
3401 }
3402
Nate Begemanee625572006-01-27 21:09:22 +00003403 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3404 SDOperand());
3405 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3406 break;
Nate Begemanee625572006-01-27 21:09:22 +00003407 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003408 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3409 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3410 Copy.getValue(1));
3411}
3412
Evan Cheng1bc78042006-04-26 01:20:17 +00003413SDOperand
3414X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng25caf632006-05-23 21:06:34 +00003415 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3416 if (CC == CallingConv::Fast && EnableFastCC)
3417 return LowerFastCCArguments(Op, DAG);
3418 else
3419 return LowerCCCArguments(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003420}
3421
Evan Cheng0db9fe62006-04-25 20:13:52 +00003422SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3423 SDOperand InFlag(0, 0);
3424 SDOperand Chain = Op.getOperand(0);
3425 unsigned Align =
3426 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3427 if (Align == 0) Align = 1;
3428
3429 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3430 // If not DWORD aligned, call memset if size is less than the threshold.
3431 // It knows how to align to the right boundary first.
3432 if ((Align & 3) != 0 ||
3433 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3434 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003435 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003436 std::vector<std::pair<SDOperand, const Type*> > Args;
3437 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3438 // Extend the ubyte argument to be an int value for the call.
3439 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3440 Args.push_back(std::make_pair(Val, IntPtrTy));
3441 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3442 std::pair<SDOperand,SDOperand> CallResult =
3443 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3444 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3445 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003446 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003447
Evan Cheng0db9fe62006-04-25 20:13:52 +00003448 MVT::ValueType AVT;
3449 SDOperand Count;
3450 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3451 unsigned BytesLeft = 0;
3452 bool TwoRepStos = false;
3453 if (ValC) {
3454 unsigned ValReg;
3455 unsigned Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456
Evan Cheng0db9fe62006-04-25 20:13:52 +00003457 // If the value is a constant, then we can potentially use larger sets.
3458 switch (Align & 3) {
3459 case 2: // WORD aligned
3460 AVT = MVT::i16;
3461 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3462 BytesLeft = I->getValue() % 2;
3463 Val = (Val << 8) | Val;
3464 ValReg = X86::AX;
3465 break;
3466 case 0: // DWORD aligned
3467 AVT = MVT::i32;
3468 if (I) {
3469 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3470 BytesLeft = I->getValue() % 4;
Evan Cheng80d428c2006-04-19 22:48:17 +00003471 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003472 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3473 DAG.getConstant(2, MVT::i8));
3474 TwoRepStos = true;
Evan Cheng80d428c2006-04-19 22:48:17 +00003475 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003476 Val = (Val << 8) | Val;
3477 Val = (Val << 16) | Val;
3478 ValReg = X86::EAX;
3479 break;
3480 default: // Byte aligned
3481 AVT = MVT::i8;
3482 Count = Op.getOperand(3);
3483 ValReg = X86::AL;
3484 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003485 }
3486
Evan Cheng0db9fe62006-04-25 20:13:52 +00003487 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3488 InFlag);
3489 InFlag = Chain.getValue(1);
3490 } else {
3491 AVT = MVT::i8;
3492 Count = Op.getOperand(3);
3493 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3494 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00003495 }
Evan Chengc78d3b42006-04-24 18:01:45 +00003496
Evan Cheng0db9fe62006-04-25 20:13:52 +00003497 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3498 InFlag = Chain.getValue(1);
3499 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3500 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00003501
Evan Cheng0db9fe62006-04-25 20:13:52 +00003502 std::vector<MVT::ValueType> Tys;
3503 Tys.push_back(MVT::Other);
3504 Tys.push_back(MVT::Flag);
3505 std::vector<SDOperand> Ops;
3506 Ops.push_back(Chain);
3507 Ops.push_back(DAG.getValueType(AVT));
3508 Ops.push_back(InFlag);
3509 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengc78d3b42006-04-24 18:01:45 +00003510
Evan Cheng0db9fe62006-04-25 20:13:52 +00003511 if (TwoRepStos) {
3512 InFlag = Chain.getValue(1);
3513 Count = Op.getOperand(3);
3514 MVT::ValueType CVT = Count.getValueType();
3515 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3516 DAG.getConstant(3, CVT));
3517 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3518 InFlag = Chain.getValue(1);
3519 Tys.clear();
3520 Tys.push_back(MVT::Other);
3521 Tys.push_back(MVT::Flag);
3522 Ops.clear();
3523 Ops.push_back(Chain);
3524 Ops.push_back(DAG.getValueType(MVT::i8));
3525 Ops.push_back(InFlag);
3526 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3527 } else if (BytesLeft) {
3528 // Issue stores for the last 1 - 3 bytes.
3529 SDOperand Value;
3530 unsigned Val = ValC->getValue() & 255;
3531 unsigned Offset = I->getValue() - BytesLeft;
3532 SDOperand DstAddr = Op.getOperand(1);
3533 MVT::ValueType AddrVT = DstAddr.getValueType();
3534 if (BytesLeft >= 2) {
3535 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3536 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3537 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3538 DAG.getConstant(Offset, AddrVT)),
3539 DAG.getSrcValue(NULL));
3540 BytesLeft -= 2;
3541 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00003542 }
3543
Evan Cheng0db9fe62006-04-25 20:13:52 +00003544 if (BytesLeft == 1) {
3545 Value = DAG.getConstant(Val, MVT::i8);
3546 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3547 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3548 DAG.getConstant(Offset, AddrVT)),
3549 DAG.getSrcValue(NULL));
Evan Chengba05f722006-04-21 23:03:30 +00003550 }
Evan Cheng386031a2006-03-24 07:29:27 +00003551 }
Evan Cheng11e15b32006-04-03 20:53:28 +00003552
Evan Cheng0db9fe62006-04-25 20:13:52 +00003553 return Chain;
3554}
Evan Cheng11e15b32006-04-03 20:53:28 +00003555
Evan Cheng0db9fe62006-04-25 20:13:52 +00003556SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3557 SDOperand Chain = Op.getOperand(0);
3558 unsigned Align =
3559 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3560 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00003561
Evan Cheng0db9fe62006-04-25 20:13:52 +00003562 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3563 // If not DWORD aligned, call memcpy if size is less than the threshold.
3564 // It knows how to align to the right boundary first.
3565 if ((Align & 3) != 0 ||
3566 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3567 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003568 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003569 std::vector<std::pair<SDOperand, const Type*> > Args;
3570 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3571 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3572 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3573 std::pair<SDOperand,SDOperand> CallResult =
3574 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3575 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3576 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00003577 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003578
3579 MVT::ValueType AVT;
3580 SDOperand Count;
3581 unsigned BytesLeft = 0;
3582 bool TwoRepMovs = false;
3583 switch (Align & 3) {
3584 case 2: // WORD aligned
3585 AVT = MVT::i16;
3586 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3587 BytesLeft = I->getValue() % 2;
3588 break;
3589 case 0: // DWORD aligned
3590 AVT = MVT::i32;
3591 if (I) {
3592 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3593 BytesLeft = I->getValue() % 4;
Evan Chengcdfc3c82006-04-17 22:45:49 +00003594 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003595 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3596 DAG.getConstant(2, MVT::i8));
3597 TwoRepMovs = true;
Evan Cheng5edb8d22006-04-17 22:04:06 +00003598 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003599 break;
3600 default: // Byte aligned
3601 AVT = MVT::i8;
3602 Count = Op.getOperand(3);
3603 break;
3604 }
3605
3606 SDOperand InFlag(0, 0);
3607 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3608 InFlag = Chain.getValue(1);
3609 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3610 InFlag = Chain.getValue(1);
3611 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3612 InFlag = Chain.getValue(1);
3613
3614 std::vector<MVT::ValueType> Tys;
3615 Tys.push_back(MVT::Other);
3616 Tys.push_back(MVT::Flag);
3617 std::vector<SDOperand> Ops;
3618 Ops.push_back(Chain);
3619 Ops.push_back(DAG.getValueType(AVT));
3620 Ops.push_back(InFlag);
3621 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3622
3623 if (TwoRepMovs) {
3624 InFlag = Chain.getValue(1);
3625 Count = Op.getOperand(3);
3626 MVT::ValueType CVT = Count.getValueType();
3627 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3628 DAG.getConstant(3, CVT));
3629 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3630 InFlag = Chain.getValue(1);
3631 Tys.clear();
3632 Tys.push_back(MVT::Other);
3633 Tys.push_back(MVT::Flag);
3634 Ops.clear();
3635 Ops.push_back(Chain);
3636 Ops.push_back(DAG.getValueType(MVT::i8));
3637 Ops.push_back(InFlag);
3638 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3639 } else if (BytesLeft) {
3640 // Issue loads and stores for the last 1 - 3 bytes.
3641 unsigned Offset = I->getValue() - BytesLeft;
3642 SDOperand DstAddr = Op.getOperand(1);
3643 MVT::ValueType DstVT = DstAddr.getValueType();
3644 SDOperand SrcAddr = Op.getOperand(2);
3645 MVT::ValueType SrcVT = SrcAddr.getValueType();
3646 SDOperand Value;
3647 if (BytesLeft >= 2) {
3648 Value = DAG.getLoad(MVT::i16, Chain,
3649 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3650 DAG.getConstant(Offset, SrcVT)),
3651 DAG.getSrcValue(NULL));
3652 Chain = Value.getValue(1);
3653 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3654 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3655 DAG.getConstant(Offset, DstVT)),
3656 DAG.getSrcValue(NULL));
3657 BytesLeft -= 2;
3658 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00003659 }
3660
Evan Cheng0db9fe62006-04-25 20:13:52 +00003661 if (BytesLeft == 1) {
3662 Value = DAG.getLoad(MVT::i8, Chain,
3663 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3664 DAG.getConstant(Offset, SrcVT)),
3665 DAG.getSrcValue(NULL));
3666 Chain = Value.getValue(1);
3667 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3668 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3669 DAG.getConstant(Offset, DstVT)),
3670 DAG.getSrcValue(NULL));
3671 }
Evan Chengb067a1e2006-03-31 19:22:53 +00003672 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003673
3674 return Chain;
3675}
3676
3677SDOperand
3678X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3679 std::vector<MVT::ValueType> Tys;
3680 Tys.push_back(MVT::Other);
3681 Tys.push_back(MVT::Flag);
3682 std::vector<SDOperand> Ops;
3683 Ops.push_back(Op.getOperand(0));
3684 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3685 Ops.clear();
3686 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3687 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3688 MVT::i32, Ops[0].getValue(2)));
3689 Ops.push_back(Ops[1].getValue(1));
3690 Tys[0] = Tys[1] = MVT::i32;
3691 Tys.push_back(MVT::Other);
3692 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3693}
3694
3695SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3696 // vastart just stores the address of the VarArgsFrameIndex slot into the
3697 // memory location argument.
3698 // FIXME: Replace MVT::i32 with PointerTy
3699 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3700 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3701 Op.getOperand(1), Op.getOperand(2));
3702}
3703
3704SDOperand
3705X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3706 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3707 switch (IntNo) {
3708 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00003709 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003710 case Intrinsic::x86_sse_comieq_ss:
3711 case Intrinsic::x86_sse_comilt_ss:
3712 case Intrinsic::x86_sse_comile_ss:
3713 case Intrinsic::x86_sse_comigt_ss:
3714 case Intrinsic::x86_sse_comige_ss:
3715 case Intrinsic::x86_sse_comineq_ss:
3716 case Intrinsic::x86_sse_ucomieq_ss:
3717 case Intrinsic::x86_sse_ucomilt_ss:
3718 case Intrinsic::x86_sse_ucomile_ss:
3719 case Intrinsic::x86_sse_ucomigt_ss:
3720 case Intrinsic::x86_sse_ucomige_ss:
3721 case Intrinsic::x86_sse_ucomineq_ss:
3722 case Intrinsic::x86_sse2_comieq_sd:
3723 case Intrinsic::x86_sse2_comilt_sd:
3724 case Intrinsic::x86_sse2_comile_sd:
3725 case Intrinsic::x86_sse2_comigt_sd:
3726 case Intrinsic::x86_sse2_comige_sd:
3727 case Intrinsic::x86_sse2_comineq_sd:
3728 case Intrinsic::x86_sse2_ucomieq_sd:
3729 case Intrinsic::x86_sse2_ucomilt_sd:
3730 case Intrinsic::x86_sse2_ucomile_sd:
3731 case Intrinsic::x86_sse2_ucomigt_sd:
3732 case Intrinsic::x86_sse2_ucomige_sd:
3733 case Intrinsic::x86_sse2_ucomineq_sd: {
3734 unsigned Opc = 0;
3735 ISD::CondCode CC = ISD::SETCC_INVALID;
3736 switch (IntNo) {
3737 default: break;
3738 case Intrinsic::x86_sse_comieq_ss:
3739 case Intrinsic::x86_sse2_comieq_sd:
3740 Opc = X86ISD::COMI;
3741 CC = ISD::SETEQ;
3742 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003743 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003744 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003745 Opc = X86ISD::COMI;
3746 CC = ISD::SETLT;
3747 break;
3748 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003749 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003750 Opc = X86ISD::COMI;
3751 CC = ISD::SETLE;
3752 break;
3753 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003754 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003755 Opc = X86ISD::COMI;
3756 CC = ISD::SETGT;
3757 break;
3758 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003759 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003760 Opc = X86ISD::COMI;
3761 CC = ISD::SETGE;
3762 break;
3763 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003764 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003765 Opc = X86ISD::COMI;
3766 CC = ISD::SETNE;
3767 break;
3768 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003769 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 Opc = X86ISD::UCOMI;
3771 CC = ISD::SETEQ;
3772 break;
3773 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003774 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003775 Opc = X86ISD::UCOMI;
3776 CC = ISD::SETLT;
3777 break;
3778 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003779 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003780 Opc = X86ISD::UCOMI;
3781 CC = ISD::SETLE;
3782 break;
3783 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003784 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003785 Opc = X86ISD::UCOMI;
3786 CC = ISD::SETGT;
3787 break;
3788 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003789 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003790 Opc = X86ISD::UCOMI;
3791 CC = ISD::SETGE;
3792 break;
3793 case Intrinsic::x86_sse_ucomineq_ss:
3794 case Intrinsic::x86_sse2_ucomineq_sd:
3795 Opc = X86ISD::UCOMI;
3796 CC = ISD::SETNE;
3797 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003798 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003799 bool Flip;
3800 unsigned X86CC;
3801 translateX86CC(CC, true, X86CC, Flip);
3802 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3803 Op.getOperand(Flip?1:2));
3804 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3805 DAG.getConstant(X86CC, MVT::i8), Cond);
3806 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00003807 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00003808 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003809}
Evan Cheng72261582005-12-20 06:22:03 +00003810
Evan Cheng0db9fe62006-04-25 20:13:52 +00003811/// LowerOperation - Provide custom lowering hooks for some operations.
3812///
3813SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3814 switch (Op.getOpcode()) {
3815 default: assert(0 && "Should not custom lower this!");
3816 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3817 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3818 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3819 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3820 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3821 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3822 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3823 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3824 case ISD::SHL_PARTS:
3825 case ISD::SRA_PARTS:
3826 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3827 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3828 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3829 case ISD::FABS: return LowerFABS(Op, DAG);
3830 case ISD::FNEG: return LowerFNEG(Op, DAG);
3831 case ISD::SETCC: return LowerSETCC(Op, DAG);
3832 case ISD::SELECT: return LowerSELECT(Op, DAG);
3833 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3834 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00003835 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003836 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003837 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003838 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3839 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3840 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3841 case ISD::VASTART: return LowerVASTART(Op, DAG);
3842 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3843 }
3844}
3845
Evan Cheng72261582005-12-20 06:22:03 +00003846const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3847 switch (Opcode) {
3848 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00003849 case X86ISD::SHLD: return "X86ISD::SHLD";
3850 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00003851 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00003852 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00003853 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00003854 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00003855 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3856 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3857 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00003858 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00003859 case X86ISD::FST: return "X86ISD::FST";
3860 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00003861 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00003862 case X86ISD::CALL: return "X86ISD::CALL";
3863 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3864 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3865 case X86ISD::CMP: return "X86ISD::CMP";
3866 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng6be2c582006-04-05 23:38:46 +00003867 case X86ISD::COMI: return "X86ISD::COMI";
3868 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00003869 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00003870 case X86ISD::CMOV: return "X86ISD::CMOV";
3871 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00003872 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00003873 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3874 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00003875 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00003876 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00003877 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00003878 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00003879 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00003880 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00003881 }
3882}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003883
Nate Begeman368e18d2006-02-16 21:11:51 +00003884void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3885 uint64_t Mask,
3886 uint64_t &KnownZero,
3887 uint64_t &KnownOne,
3888 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003889 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00003890 assert((Opc >= ISD::BUILTIN_OP_END ||
3891 Opc == ISD::INTRINSIC_WO_CHAIN ||
3892 Opc == ISD::INTRINSIC_W_CHAIN ||
3893 Opc == ISD::INTRINSIC_VOID) &&
3894 "Should use MaskedValueIsZero if you don't know whether Op"
3895 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003896
Evan Cheng865f0602006-04-05 06:11:20 +00003897 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003898 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00003899 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +00003900 case X86ISD::SETCC:
3901 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3902 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003903 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003904}
Chris Lattner259e97c2006-01-31 19:43:35 +00003905
3906std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00003907getRegClassForInlineAsmConstraint(const std::string &Constraint,
3908 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00003909 if (Constraint.size() == 1) {
3910 // FIXME: not handling fp-stack yet!
3911 // FIXME: not handling MMX registers yet ('y' constraint).
3912 switch (Constraint[0]) { // GCC X86 Constraint Letters
3913 default: break; // Unknown constriant letter
3914 case 'r': // GENERAL_REGS
3915 case 'R': // LEGACY_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003916 if (VT == MVT::i32)
3917 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3918 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3919 else if (VT == MVT::i16)
3920 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3921 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3922 else if (VT == MVT::i8)
3923 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3924 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003925 case 'l': // INDEX_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003926 if (VT == MVT::i32)
3927 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3928 X86::ESI, X86::EDI, X86::EBP, 0);
3929 else if (VT == MVT::i16)
3930 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3931 X86::SI, X86::DI, X86::BP, 0);
3932 else if (VT == MVT::i8)
3933 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3934 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003935 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3936 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003937 if (VT == MVT::i32)
3938 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3939 else if (VT == MVT::i16)
3940 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3941 else if (VT == MVT::i8)
3942 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3943 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003944 case 'x': // SSE_REGS if SSE1 allowed
3945 if (Subtarget->hasSSE1())
3946 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3947 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3948 0);
3949 return std::vector<unsigned>();
3950 case 'Y': // SSE_REGS if SSE2 allowed
3951 if (Subtarget->hasSSE2())
3952 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3953 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3954 0);
3955 return std::vector<unsigned>();
3956 }
3957 }
3958
Chris Lattner1efa40f2006-02-22 00:56:39 +00003959 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00003960}
Evan Cheng30b37b52006-03-13 23:18:16 +00003961
3962/// isLegalAddressImmediate - Return true if the integer value or
3963/// GlobalValue can be used as the offset of the target addressing mode.
3964bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3965 // X86 allows a sign-extended 32-bit immediate field.
3966 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3967}
3968
3969bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00003970 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00003971 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3972 if (RModel == Reloc::Static)
3973 return true;
3974 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00003975 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00003976 else
3977 return false;
3978 } else
3979 return true;
3980}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003981
3982/// isShuffleMaskLegal - Targets can use this to indicate that they only
3983/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3984/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3985/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00003986bool
3987X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3988 // Only do shuffles on 128-bit vector types for now.
3989 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng80d428c2006-04-19 22:48:17 +00003990 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengc575ca22006-04-17 20:43:08 +00003991 isSplatMask(Mask.Val) ||
Evan Chengc21a0532006-04-05 01:47:37 +00003992 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00003993 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003994 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00003995 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00003996}
Evan Cheng39623da2006-04-20 08:58:49 +00003997
3998bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3999 MVT::ValueType EVT,
4000 SelectionDAG &DAG) const {
4001 unsigned NumElts = BVOps.size();
4002 // Only do shuffles on 128-bit vector types for now.
4003 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4004 if (NumElts == 2) return true;
4005 if (NumElts == 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00004006 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng39623da2006-04-20 08:58:49 +00004007 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4008 }
4009 return false;
4010}