blob: 3baff5cd93f2c12f705cded996935fab85412ef4 [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"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000025#include "llvm/ADT/VectorExtras.h"
26#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/SelectionDAG.h"
31#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000032#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000033#include "llvm/Target/TargetOptions.h"
34using namespace llvm;
35
36// FIXME: temporary.
37#include "llvm/Support/CommandLine.h"
38static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
39 cl::desc("Enable fastcc on X86"));
40
41X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000043 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
Evan Cheng25ab6902006-09-08 06:48:29 +000045 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Cheng559806f2006-01-27 08:10:46 +000046
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000047 // Set up the TargetLowering object.
48
49 // X86 is weird, it always uses i8 for shift amounts and setcc results.
50 setShiftAmountType(MVT::i8);
51 setSetCCResultType(MVT::i8);
52 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000053 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000055 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000056
Evan Chenga88973f2006-03-22 19:22:18 +000057 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000058 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
59 setUseUnderscoreSetJmpLongJmp(true);
60
Evan Cheng714554d2006-03-16 21:47:42 +000061 // Add legal addressing mode scale values.
62 addLegalAddressScale(8);
63 addLegalAddressScale(4);
64 addLegalAddressScale(2);
65 // Enter the ones which require both scale + index last. These are more
66 // expensive.
67 addLegalAddressScale(9);
68 addLegalAddressScale(5);
69 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000070
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000071 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000072 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
73 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
74 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000075 if (Subtarget->is64Bit())
76 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000077
78 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
79 // operation.
80 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
81 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
82 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000083
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit()) {
85 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000086 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000087 } else {
88 if (X86ScalarSSE)
89 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
90 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
91 else
92 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
93 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094
95 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
96 // this operation.
97 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
98 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000099 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +0000100 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +0000101 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000102 else {
103 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
104 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
105 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000106
Evan Cheng25ab6902006-09-08 06:48:29 +0000107 if (!Subtarget->is64Bit()) {
108 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
109 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
110 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
111 }
Evan Cheng6dab0532006-01-30 08:02:57 +0000112
Evan Cheng02568ff2006-01-30 22:13:22 +0000113 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
116 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
117
118 if (X86ScalarSSE) {
119 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
120 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000122 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000123 }
124
125 // Handle FP_TO_UINT by promoting the destination to a larger signed
126 // conversion.
127 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
128 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
129 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
130
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit()) {
132 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000134 } else {
135 if (X86ScalarSSE && !Subtarget->hasSSE3())
136 // Expand FP_TO_UINT into a select.
137 // FIXME: We would like to use a Custom expander here eventually to do
138 // the optimal thing for SSE vs. the default expansion in the legalizer.
139 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
140 else
141 // With SSE3 we can use fisttpll to convert to a signed i64.
142 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
143 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000144
Evan Cheng02568ff2006-01-30 22:13:22 +0000145 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
146 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000147
Evan Cheng5298bcc2006-02-17 07:01:52 +0000148 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000149 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
150 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000152 if (Subtarget->is64Bit())
153 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
157 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
158 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
159 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000160
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
162 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
163 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
164 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
165 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
166 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
167 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
168 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
169 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000170 if (Subtarget->is64Bit()) {
171 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
172 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
173 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
174 }
175
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000176 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000177 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // These should be promoted to a larger select which is supported.
180 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
181 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000182 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000183 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
184 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
185 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
186 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
187 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
188 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
189 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
190 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
191 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 if (Subtarget->is64Bit()) {
193 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
194 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
195 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000196 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000197 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000198 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000199 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000200 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000201 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000202 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000203 if (Subtarget->is64Bit()) {
204 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
205 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
206 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
207 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
208 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000209 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000210 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
211 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
212 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000213 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000214 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
215 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216
Chris Lattnerf73bae12005-11-29 06:16:21 +0000217 // We don't have line number support yet.
218 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000219 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000220 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000221 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000222 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000223
Nate Begemanacc398c2006-01-25 18:21:52 +0000224 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
225 setOperationAction(ISD::VASTART , MVT::Other, Custom);
226
227 // Use the default implementation.
228 setOperationAction(ISD::VAARG , MVT::Other, Expand);
229 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
230 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000231 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
232 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000233 if (Subtarget->is64Bit())
234 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000235 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000236
Chris Lattner9601a862006-03-05 05:08:37 +0000237 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
238 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 if (X86ScalarSSE) {
241 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000242 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
243 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244
Evan Cheng223547a2006-01-31 22:28:30 +0000245 // Use ANDPD to simulate FABS.
246 setOperationAction(ISD::FABS , MVT::f64, Custom);
247 setOperationAction(ISD::FABS , MVT::f32, Custom);
248
249 // Use XORP to simulate FNEG.
250 setOperationAction(ISD::FNEG , MVT::f64, Custom);
251 setOperationAction(ISD::FNEG , MVT::f32, Custom);
252
Evan Chengd25e9e82006-02-02 00:28:23 +0000253 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000254 setOperationAction(ISD::FSIN , MVT::f64, Expand);
255 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000256 setOperationAction(ISD::FREM , MVT::f64, Expand);
257 setOperationAction(ISD::FSIN , MVT::f32, Expand);
258 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259 setOperationAction(ISD::FREM , MVT::f32, Expand);
260
Chris Lattnera54aa942006-01-29 06:26:08 +0000261 // Expand FP immediates into loads from the stack, except for the special
262 // cases we handle.
263 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
264 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265 addLegalFPImmediate(+0.0); // xorps / xorpd
266 } else {
267 // Set up the FP register classes.
268 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000269
270 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
271
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000272 if (!UnsafeFPMath) {
273 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
274 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
275 }
276
Chris Lattnera54aa942006-01-29 06:26:08 +0000277 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000278 addLegalFPImmediate(+0.0); // FLD0
279 addLegalFPImmediate(+1.0); // FLD1
280 addLegalFPImmediate(-0.0); // FLD0/FCHS
281 addLegalFPImmediate(-1.0); // FLD1/FCHS
282 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000283
Evan Chengd30bf012006-03-01 01:11:20 +0000284 // First set operation action for all vector types to expand. Then we
285 // will selectively turn on ones that can be effectively codegen'd.
286 for (unsigned VT = (unsigned)MVT::Vector + 1;
287 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
288 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
289 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
290 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
291 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000292 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000293 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000294 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000295 }
296
Evan Chenga88973f2006-03-22 19:22:18 +0000297 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000298 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
299 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
300 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
301
Evan Chengd30bf012006-03-01 01:11:20 +0000302 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000303 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
304 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
305 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000306 }
307
Evan Chenga88973f2006-03-22 19:22:18 +0000308 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000309 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
310
Evan Cheng2c3ae372006-04-12 21:21:57 +0000311 setOperationAction(ISD::AND, MVT::v4f32, Legal);
312 setOperationAction(ISD::OR, MVT::v4f32, Legal);
313 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000314 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
315 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
316 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
317 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
318 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
319 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000321 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000322 }
323
Evan Chenga88973f2006-03-22 19:22:18 +0000324 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000325 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
326 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
327 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
328 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
329 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
330
Evan Chengf7c378e2006-04-10 07:23:14 +0000331 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
332 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
333 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
334 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
335 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
336 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
337 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
338 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000339 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000340 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000341
Evan Chengf7c378e2006-04-10 07:23:14 +0000342 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
343 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000344 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000345 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
346 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
347 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000348
Evan Cheng2c3ae372006-04-12 21:21:57 +0000349 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
350 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
351 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
352 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
353 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
354 }
355 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
356 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
357 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
358 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
359 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
360 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
361
362 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
363 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
364 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
365 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
366 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
367 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
368 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
369 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000370 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
371 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000372 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
373 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000374 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000375
376 // Custom lower v2i64 and v2f64 selects.
377 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000378 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000379 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000380 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000381 }
382
Evan Cheng6be2c582006-04-05 23:38:46 +0000383 // We want to custom lower some of our intrinsics.
384 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
385
Evan Cheng206ee9d2006-07-07 08:33:52 +0000386 // We have target-specific dag combine patterns for the following nodes:
387 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
388
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000389 computeRegisterProperties();
390
Evan Cheng87ed7162006-02-14 08:25:08 +0000391 // FIXME: These should be based on subtarget info. Plus, the values should
392 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000393 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
394 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
395 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000396 allowUnalignedMemoryAccesses = true; // x86 supports it!
397}
398
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000399//===----------------------------------------------------------------------===//
400// C Calling Convention implementation
401//===----------------------------------------------------------------------===//
402
Evan Cheng85e38002006-04-27 05:35:28 +0000403/// AddLiveIn - This helper function adds the specified physical register to the
404/// MachineFunction as a live in value. It also creates a corresponding virtual
405/// register for it.
406static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
407 TargetRegisterClass *RC) {
408 assert(RC->contains(PReg) && "Not the correct regclass!");
409 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
410 MF.addLiveIn(PReg, VReg);
411 return VReg;
412}
413
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000414/// HowToPassCCCArgument - Returns how an formal argument of the specified type
415/// should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +0000416/// slot; if it is through XMM register, returns the number of XMM registers
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000417/// are needed.
418static void
419HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
420 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng26755342006-06-01 05:53:27 +0000421 ObjXMMRegs = 0;
Evan Chengcc1fc222006-05-25 23:31:23 +0000422
Evan Chengeda65fa2006-04-27 01:32:22 +0000423 switch (ObjectVT) {
424 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000425 case MVT::i8: ObjSize = 1; break;
426 case MVT::i16: ObjSize = 2; break;
427 case MVT::i32: ObjSize = 4; break;
428 case MVT::i64: ObjSize = 8; break;
429 case MVT::f32: ObjSize = 4; break;
430 case MVT::f64: ObjSize = 8; break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000431 case MVT::v16i8:
432 case MVT::v8i16:
433 case MVT::v4i32:
434 case MVT::v2i64:
435 case MVT::v4f32:
436 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000437 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000438 ObjXMMRegs = 1;
439 else
440 ObjSize = 16;
441 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000442 }
Evan Chengeda65fa2006-04-27 01:32:22 +0000443}
444
Evan Cheng25caf632006-05-23 21:06:34 +0000445SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
446 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000447 MachineFunction &MF = DAG.getMachineFunction();
448 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000449 SDOperand Root = Op.getOperand(0);
450 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Evan Chengeda65fa2006-04-27 01:32:22 +0000452 // Add DAG nodes to load the arguments... On entry to a function on the X86,
453 // the stack frame looks like this:
454 //
455 // [ESP] -- return address
456 // [ESP + 4] -- first argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000457 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000458 // ...
459 //
Evan Cheng1bc78042006-04-26 01:20:17 +0000460 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000461 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000462 static const unsigned XMMArgRegs[] = {
463 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
464 };
Evan Cheng1bc78042006-04-26 01:20:17 +0000465 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000466 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
467 unsigned ArgIncrement = 4;
468 unsigned ObjSize = 0;
469 unsigned ObjXMMRegs = 0;
470 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +0000471 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000472 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000473
Evan Cheng25caf632006-05-23 21:06:34 +0000474 SDOperand ArgValue;
475 if (ObjXMMRegs) {
476 // Passed in a XMM register.
477 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
Evan Cheng25ab6902006-09-08 06:48:29 +0000478 X86::VR128RegisterClass);
Evan Cheng25caf632006-05-23 21:06:34 +0000479 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
480 ArgValues.push_back(ArgValue);
481 NumXMMRegs += ObjXMMRegs;
482 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +0000483 // XMM arguments have to be aligned on 16-byte boundary.
484 if (ObjSize == 16)
485 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +0000486 // Create the frame index object for this incoming parameter...
487 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
488 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
489 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
490 DAG.getSrcValue(NULL));
491 ArgValues.push_back(ArgValue);
492 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Cheng1bc78042006-04-26 01:20:17 +0000493 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000494 }
495
Evan Cheng25caf632006-05-23 21:06:34 +0000496 ArgValues.push_back(Root);
497
Evan Cheng1bc78042006-04-26 01:20:17 +0000498 // If the function takes variable number of arguments, make a frame index for
499 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000500 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
501 if (isVarArg)
Evan Cheng1bc78042006-04-26 01:20:17 +0000502 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Evan Cheng25ab6902006-09-08 06:48:29 +0000503 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
504 ReturnAddrIndex = 0; // No return address slot generated yet.
505 BytesToPopOnReturn = 0; // Callee pops nothing.
Evan Cheng1bc78042006-04-26 01:20:17 +0000506 BytesCallerReserves = ArgOffset;
Evan Cheng25caf632006-05-23 21:06:34 +0000507
Chris Lattner2d297092006-05-23 18:50:38 +0000508 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
509 // pointer.
Evan Cheng25caf632006-05-23 21:06:34 +0000510 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner2d297092006-05-23 18:50:38 +0000511 Subtarget->isTargetDarwin())
512 BytesToPopOnReturn = 4;
Evan Cheng1bc78042006-04-26 01:20:17 +0000513
Evan Cheng25caf632006-05-23 21:06:34 +0000514 // Return the new list of results.
515 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
516 Op.Val->value_end());
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000517 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000518}
519
Evan Cheng32fe1032006-05-25 00:59:30 +0000520
521SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
522 SDOperand Chain = Op.getOperand(0);
523 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
524 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
525 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
526 SDOperand Callee = Op.getOperand(4);
527 MVT::ValueType RetVT= Op.Val->getValueType(0);
528 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000529
Evan Cheng347d5f72006-04-28 21:29:37 +0000530 // Keep track of the number of XMM regs passed so far.
531 unsigned NumXMMRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000532 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000533 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000534 };
Evan Cheng347d5f72006-04-28 21:29:37 +0000535
Evan Cheng32fe1032006-05-25 00:59:30 +0000536 // Count how many bytes are to be pushed on the stack.
537 unsigned NumBytes = 0;
538 for (unsigned i = 0; i != NumOps; ++i) {
539 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540
Evan Cheng32fe1032006-05-25 00:59:30 +0000541 switch (Arg.getValueType()) {
542 default: assert(0 && "Unexpected ValueType for argument!");
543 case MVT::i8:
544 case MVT::i16:
545 case MVT::i32:
546 case MVT::f32:
547 NumBytes += 4;
548 break;
549 case MVT::i64:
550 case MVT::f64:
551 NumBytes += 8;
552 break;
553 case MVT::v16i8:
554 case MVT::v8i16:
555 case MVT::v4i32:
556 case MVT::v2i64:
557 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000558 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000559 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +0000560 ++NumXMMRegs;
Evan Cheng3fddf242006-05-26 20:37:47 +0000561 else {
562 // XMM arguments have to be aligned on 16-byte boundary.
563 NumBytes = ((NumBytes + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +0000564 NumBytes += 16;
Evan Cheng3fddf242006-05-26 20:37:47 +0000565 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000566 break;
567 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000568 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000569
Evan Cheng32fe1032006-05-25 00:59:30 +0000570 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000571
Evan Cheng32fe1032006-05-25 00:59:30 +0000572 // Arguments go on the stack in reverse order, as specified by the ABI.
573 unsigned ArgOffset = 0;
574 NumXMMRegs = 0;
575 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
576 std::vector<SDOperand> MemOpChains;
Evan Cheng25ab6902006-09-08 06:48:29 +0000577 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +0000578 for (unsigned i = 0; i != NumOps; ++i) {
579 SDOperand Arg = Op.getOperand(5+2*i);
580
581 switch (Arg.getValueType()) {
582 default: assert(0 && "Unexpected ValueType for argument!");
583 case MVT::i8:
Evan Cheng6b5783d2006-05-25 18:56:34 +0000584 case MVT::i16: {
Evan Cheng32fe1032006-05-25 00:59:30 +0000585 // Promote the integer to 32 bits. If the input type is signed use a
586 // sign extend, otherwise use a zero extend.
587 unsigned ExtOp =
588 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
589 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
590 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng6b5783d2006-05-25 18:56:34 +0000591 }
592 // Fallthrough
Evan Cheng32fe1032006-05-25 00:59:30 +0000593
594 case MVT::i32:
595 case MVT::f32: {
596 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
597 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
598 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
599 Arg, PtrOff, DAG.getSrcValue(NULL)));
600 ArgOffset += 4;
601 break;
602 }
603 case MVT::i64:
604 case MVT::f64: {
605 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
606 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
607 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
608 Arg, PtrOff, DAG.getSrcValue(NULL)));
609 ArgOffset += 8;
610 break;
611 }
612 case MVT::v16i8:
613 case MVT::v8i16:
614 case MVT::v4i32:
615 case MVT::v2i64:
616 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000617 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000618 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000619 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
620 NumXMMRegs++;
621 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +0000622 // XMM arguments have to be aligned on 16-byte boundary.
623 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000624 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +0000625 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
626 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
627 Arg, PtrOff, DAG.getSrcValue(NULL)));
628 ArgOffset += 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000629 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000630 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631 }
632
Evan Cheng32fe1032006-05-25 00:59:30 +0000633 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000634 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
635 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636
Evan Cheng347d5f72006-04-28 21:29:37 +0000637 // Build a sequence of copy-to-reg nodes chained together with token chain
638 // and flag operands which copy the outgoing args into registers.
639 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000640 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
641 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
642 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000643 InFlag = Chain.getValue(1);
644 }
645
Evan Cheng32fe1032006-05-25 00:59:30 +0000646 // If the callee is a GlobalAddress node (quite common, every direct call is)
647 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
648 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
649 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
650 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
651 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
652
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000653 std::vector<MVT::ValueType> NodeTys;
654 NodeTys.push_back(MVT::Other); // Returns a chain
655 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
656 std::vector<SDOperand> Ops;
657 Ops.push_back(Chain);
658 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +0000659
660 // Add argument registers to the end of the list so that they are known live
661 // into the call.
662 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
663 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
664 RegsToPass[i].second.getValueType()));
665
Evan Cheng347d5f72006-04-28 21:29:37 +0000666 if (InFlag.Val)
667 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000668
Evan Cheng32fe1032006-05-25 00:59:30 +0000669 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000670 NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +0000671 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000672
Chris Lattner2d297092006-05-23 18:50:38 +0000673 // Create the CALLSEQ_END node.
674 unsigned NumBytesForCalleeToPush = 0;
675
676 // If this is is a call to a struct-return function on Darwin/X86, the callee
677 // pops the hidden struct pointer, so we have to push it back.
678 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
679 NumBytesForCalleeToPush = 4;
680
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000681 NodeTys.clear();
682 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +0000683 if (RetVT != MVT::Other)
684 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000685 Ops.clear();
686 Ops.push_back(Chain);
687 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000688 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000689 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000690 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000691 if (RetVT != MVT::Other)
692 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000693
Evan Cheng32fe1032006-05-25 00:59:30 +0000694 std::vector<SDOperand> ResultVals;
695 NodeTys.clear();
696 switch (RetVT) {
697 default: assert(0 && "Unknown value type to return!");
698 case MVT::Other: break;
699 case MVT::i8:
700 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
701 ResultVals.push_back(Chain.getValue(0));
702 NodeTys.push_back(MVT::i8);
703 break;
704 case MVT::i16:
705 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
706 ResultVals.push_back(Chain.getValue(0));
707 NodeTys.push_back(MVT::i16);
708 break;
709 case MVT::i32:
710 if (Op.Val->getValueType(1) == MVT::i32) {
711 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
712 ResultVals.push_back(Chain.getValue(0));
713 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
714 Chain.getValue(2)).getValue(1);
715 ResultVals.push_back(Chain.getValue(0));
716 NodeTys.push_back(MVT::i32);
717 } else {
718 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
719 ResultVals.push_back(Chain.getValue(0));
Evan Chengd90eb7f2006-01-05 00:27:02 +0000720 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000721 NodeTys.push_back(MVT::i32);
722 break;
723 case MVT::v16i8:
724 case MVT::v8i16:
725 case MVT::v4i32:
726 case MVT::v2i64:
727 case MVT::v4f32:
728 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +0000729 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
730 ResultVals.push_back(Chain.getValue(0));
731 NodeTys.push_back(RetVT);
732 break;
733 case MVT::f32:
734 case MVT::f64: {
735 std::vector<MVT::ValueType> Tys;
736 Tys.push_back(MVT::f64);
737 Tys.push_back(MVT::Other);
738 Tys.push_back(MVT::Flag);
739 std::vector<SDOperand> Ops;
740 Ops.push_back(Chain);
741 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000742 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
743 &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000744 Chain = RetVal.getValue(1);
745 InFlag = RetVal.getValue(2);
746 if (X86ScalarSSE) {
747 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
748 // shouldn't be necessary except that RFP cannot be live across
749 // multiple blocks. When stackifier is fixed, they can be uncoupled.
750 MachineFunction &MF = DAG.getMachineFunction();
751 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
752 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
753 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000754 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +0000755 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000756 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +0000757 Ops.push_back(RetVal);
758 Ops.push_back(StackSlot);
759 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000760 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000761 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000762 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
763 DAG.getSrcValue(NULL));
Evan Cheng347d5f72006-04-28 21:29:37 +0000764 Chain = RetVal.getValue(1);
Evan Cheng347d5f72006-04-28 21:29:37 +0000765 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000766
767 if (RetVT == MVT::f32 && !X86ScalarSSE)
768 // FIXME: we would really like to remember that this FP_ROUND
769 // operation is okay to eliminate if we allow excess FP precision.
770 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
771 ResultVals.push_back(RetVal);
772 NodeTys.push_back(RetVT);
773 break;
774 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000775 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000776
Evan Cheng32fe1032006-05-25 00:59:30 +0000777 // If the function returns void, just return the chain.
778 if (ResultVals.empty())
779 return Chain;
780
781 // Otherwise, merge everything together with a MERGE_VALUES node.
782 NodeTys.push_back(MVT::Other);
783 ResultVals.push_back(Chain);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000784 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
785 &ResultVals[0], ResultVals.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000786 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000787}
788
Evan Cheng25ab6902006-09-08 06:48:29 +0000789
790//===----------------------------------------------------------------------===//
791// X86-64 C Calling Convention implementation
792//===----------------------------------------------------------------------===//
793
794/// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified
795/// type should be passed. If it is through stack, returns the size of the stack
796/// slot; if it is through integer or XMM register, returns the number of
797/// integer or XMM registers are needed.
798static void
799HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT,
800 unsigned NumIntRegs, unsigned NumXMMRegs,
801 unsigned &ObjSize, unsigned &ObjIntRegs,
802 unsigned &ObjXMMRegs) {
803 ObjSize = 0;
804 ObjIntRegs = 0;
805 ObjXMMRegs = 0;
806
807 switch (ObjectVT) {
808 default: assert(0 && "Unhandled argument type!");
809 case MVT::i8:
810 case MVT::i16:
811 case MVT::i32:
812 case MVT::i64:
813 if (NumIntRegs < 6)
814 ObjIntRegs = 1;
815 else {
816 switch (ObjectVT) {
817 default: break;
818 case MVT::i8: ObjSize = 1; break;
819 case MVT::i16: ObjSize = 2; break;
820 case MVT::i32: ObjSize = 4; break;
821 case MVT::i64: ObjSize = 8; break;
822 }
823 }
824 break;
825 case MVT::f32:
826 case MVT::f64:
827 case MVT::v16i8:
828 case MVT::v8i16:
829 case MVT::v4i32:
830 case MVT::v2i64:
831 case MVT::v4f32:
832 case MVT::v2f64:
833 if (NumXMMRegs < 8)
834 ObjXMMRegs = 1;
835 else {
836 switch (ObjectVT) {
837 default: break;
838 case MVT::f32: ObjSize = 4; break;
839 case MVT::f64: ObjSize = 8; break;
840 case MVT::v16i8:
841 case MVT::v8i16:
842 case MVT::v4i32:
843 case MVT::v2i64:
844 case MVT::v4f32:
845 case MVT::v2f64: ObjSize = 16; break;
846 }
847 break;
848 }
849 }
850}
851
852SDOperand
853X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
854 unsigned NumArgs = Op.Val->getNumValues() - 1;
855 MachineFunction &MF = DAG.getMachineFunction();
856 MachineFrameInfo *MFI = MF.getFrameInfo();
857 SDOperand Root = Op.getOperand(0);
858 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
859 std::vector<SDOperand> ArgValues;
860
861 // Add DAG nodes to load the arguments... On entry to a function on the X86,
862 // the stack frame looks like this:
863 //
864 // [RSP] -- return address
865 // [RSP + 8] -- first nonreg argument (leftmost lexically)
866 // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size
867 // ...
868 //
869 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
870 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
871 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
872
873 static const unsigned GPR8ArgRegs[] = {
874 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
875 };
876 static const unsigned GPR16ArgRegs[] = {
877 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
878 };
879 static const unsigned GPR32ArgRegs[] = {
880 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
881 };
882 static const unsigned GPR64ArgRegs[] = {
883 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
884 };
885 static const unsigned XMMArgRegs[] = {
886 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
887 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
888 };
889
890 for (unsigned i = 0; i < NumArgs; ++i) {
891 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
892 unsigned ArgIncrement = 8;
893 unsigned ObjSize = 0;
894 unsigned ObjIntRegs = 0;
895 unsigned ObjXMMRegs = 0;
896
897 // FIXME: __int128 and long double support?
898 HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
899 ObjSize, ObjIntRegs, ObjXMMRegs);
900 if (ObjSize > 8)
901 ArgIncrement = ObjSize;
902
903 unsigned Reg = 0;
904 SDOperand ArgValue;
905 if (ObjIntRegs || ObjXMMRegs) {
906 switch (ObjectVT) {
907 default: assert(0 && "Unhandled argument type!");
908 case MVT::i8:
909 case MVT::i16:
910 case MVT::i32:
911 case MVT::i64: {
912 TargetRegisterClass *RC = NULL;
913 switch (ObjectVT) {
914 default: break;
915 case MVT::i8:
916 RC = X86::GR8RegisterClass;
917 Reg = GPR8ArgRegs[NumIntRegs];
918 break;
919 case MVT::i16:
920 RC = X86::GR16RegisterClass;
921 Reg = GPR16ArgRegs[NumIntRegs];
922 break;
923 case MVT::i32:
924 RC = X86::GR32RegisterClass;
925 Reg = GPR32ArgRegs[NumIntRegs];
926 break;
927 case MVT::i64:
928 RC = X86::GR64RegisterClass;
929 Reg = GPR64ArgRegs[NumIntRegs];
930 break;
931 }
932 Reg = AddLiveIn(MF, Reg, RC);
933 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
934 break;
935 }
936 case MVT::f32:
937 case MVT::f64:
938 case MVT::v16i8:
939 case MVT::v8i16:
940 case MVT::v4i32:
941 case MVT::v2i64:
942 case MVT::v4f32:
943 case MVT::v2f64: {
944 TargetRegisterClass *RC= (ObjectVT == MVT::f32) ?
945 X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ?
946 X86::FR64RegisterClass : X86::VR128RegisterClass);
947 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC);
948 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
949 break;
950 }
951 }
952 NumIntRegs += ObjIntRegs;
953 NumXMMRegs += ObjXMMRegs;
954 } else if (ObjSize) {
955 // XMM arguments have to be aligned on 16-byte boundary.
956 if (ObjSize == 16)
957 ArgOffset = ((ArgOffset + 15) / 16) * 16;
958 // Create the SelectionDAG nodes corresponding to a load from this
959 // parameter.
960 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
961 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
962 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
963 DAG.getSrcValue(NULL));
964 ArgOffset += ArgIncrement; // Move on to the next argument.
965 }
966
967 ArgValues.push_back(ArgValue);
968 }
969
970 // If the function takes variable number of arguments, make a frame index for
971 // the start of the first vararg value... for expansion of llvm.va_start.
972 if (isVarArg) {
973 // For X86-64, if there are vararg parameters that are passed via
974 // registers, then we must store them to their spots on the stack so they
975 // may be loaded by deferencing the result of va_next.
976 VarArgsGPOffset = NumIntRegs * 8;
977 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
978 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
979 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
980
981 // Store the integer parameter registers.
982 std::vector<SDOperand> MemOps;
983 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
984 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
985 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
986 for (; NumIntRegs != 6; ++NumIntRegs) {
987 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
988 X86::GR64RegisterClass);
989 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
990 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
991 Val, FIN, DAG.getSrcValue(NULL));
992 MemOps.push_back(Store);
993 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
994 DAG.getConstant(8, getPointerTy()));
995 }
996
997 // Now store the XMM (fp + vector) parameter registers.
998 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
999 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1000 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1001 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1002 X86::VR128RegisterClass);
1003 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1004 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1005 Val, FIN, DAG.getSrcValue(NULL));
1006 MemOps.push_back(Store);
1007 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1008 DAG.getConstant(16, getPointerTy()));
1009 }
1010 if (!MemOps.empty())
1011 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1012 &MemOps[0], MemOps.size());
1013 }
1014
1015 ArgValues.push_back(Root);
1016
1017 ReturnAddrIndex = 0; // No return address slot generated yet.
1018 BytesToPopOnReturn = 0; // Callee pops nothing.
1019 BytesCallerReserves = ArgOffset;
1020
1021 // Return the new list of results.
1022 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1023 Op.Val->value_end());
1024 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1025}
1026
1027SDOperand
1028X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) {
1029 SDOperand Chain = Op.getOperand(0);
1030 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1031 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1032 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1033 SDOperand Callee = Op.getOperand(4);
1034 MVT::ValueType RetVT= Op.Val->getValueType(0);
1035 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1036
1037 // Count how many bytes are to be pushed on the stack.
1038 unsigned NumBytes = 0;
1039 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1040 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1041
1042 static const unsigned GPR8ArgRegs[] = {
1043 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1044 };
1045 static const unsigned GPR16ArgRegs[] = {
1046 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1047 };
1048 static const unsigned GPR32ArgRegs[] = {
1049 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1050 };
1051 static const unsigned GPR64ArgRegs[] = {
1052 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1053 };
1054 static const unsigned XMMArgRegs[] = {
1055 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1056 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1057 };
1058
1059 for (unsigned i = 0; i != NumOps; ++i) {
1060 SDOperand Arg = Op.getOperand(5+2*i);
1061 MVT::ValueType ArgVT = Arg.getValueType();
1062
1063 switch (ArgVT) {
1064 default: assert(0 && "Unknown value type!");
1065 case MVT::i8:
1066 case MVT::i16:
1067 case MVT::i32:
1068 case MVT::i64:
1069 if (NumIntRegs < 6)
1070 ++NumIntRegs;
1071 else
1072 NumBytes += 8;
1073 break;
1074 case MVT::f32:
1075 case MVT::f64:
1076 case MVT::v16i8:
1077 case MVT::v8i16:
1078 case MVT::v4i32:
1079 case MVT::v2i64:
1080 case MVT::v4f32:
1081 case MVT::v2f64:
1082 if (NumXMMRegs < 8)
1083 NumXMMRegs++;
1084 else if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1085 NumBytes += 8;
1086 else {
1087 // XMM arguments have to be aligned on 16-byte boundary.
1088 NumBytes = ((NumBytes + 15) / 16) * 16;
1089 NumBytes += 16;
1090 }
1091 break;
1092 }
1093 }
1094
1095 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1096
1097 // Arguments go on the stack in reverse order, as specified by the ABI.
1098 unsigned ArgOffset = 0;
1099 NumIntRegs = 0;
1100 NumXMMRegs = 0;
1101 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1102 std::vector<SDOperand> MemOpChains;
1103 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1104 for (unsigned i = 0; i != NumOps; ++i) {
1105 SDOperand Arg = Op.getOperand(5+2*i);
1106 MVT::ValueType ArgVT = Arg.getValueType();
1107
1108 switch (ArgVT) {
1109 default: assert(0 && "Unexpected ValueType for argument!");
1110 case MVT::i8:
1111 case MVT::i16:
1112 case MVT::i32:
1113 case MVT::i64:
1114 if (NumIntRegs < 6) {
1115 unsigned Reg = 0;
1116 switch (ArgVT) {
1117 default: break;
1118 case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break;
1119 case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break;
1120 case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break;
1121 case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break;
1122 }
1123 RegsToPass.push_back(std::make_pair(Reg, Arg));
1124 ++NumIntRegs;
1125 } else {
1126 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1127 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1128 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1129 Arg, PtrOff, DAG.getSrcValue(NULL)));
1130 ArgOffset += 8;
1131 }
1132 break;
1133 case MVT::f32:
1134 case MVT::f64:
1135 case MVT::v16i8:
1136 case MVT::v8i16:
1137 case MVT::v4i32:
1138 case MVT::v2i64:
1139 case MVT::v4f32:
1140 case MVT::v2f64:
1141 if (NumXMMRegs < 8) {
1142 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1143 NumXMMRegs++;
1144 } else {
1145 if (ArgVT != MVT::f32 && ArgVT != MVT::f64) {
1146 // XMM arguments have to be aligned on 16-byte boundary.
1147 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1148 }
1149 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1150 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1151 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1152 Arg, PtrOff, DAG.getSrcValue(NULL)));
1153 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1154 ArgOffset += 8;
1155 else
1156 ArgOffset += 16;
1157 }
1158 }
1159 }
1160
1161 if (!MemOpChains.empty())
1162 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1163 &MemOpChains[0], MemOpChains.size());
1164
1165 // Build a sequence of copy-to-reg nodes chained together with token chain
1166 // and flag operands which copy the outgoing args into registers.
1167 SDOperand InFlag;
1168 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1169 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1170 InFlag);
1171 InFlag = Chain.getValue(1);
1172 }
1173
1174 if (isVarArg) {
1175 // From AMD64 ABI document:
1176 // For calls that may call functions that use varargs or stdargs
1177 // (prototype-less calls or calls to functions containing ellipsis (...) in
1178 // the declaration) %al is used as hidden argument to specify the number
1179 // of SSE registers used. The contents of %al do not need to match exactly
1180 // the number of registers, but must be an ubound on the number of SSE
1181 // registers used and is in the range 0 - 8 inclusive.
1182 Chain = DAG.getCopyToReg(Chain, X86::AL,
1183 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1184 InFlag = Chain.getValue(1);
1185 }
1186
1187 // If the callee is a GlobalAddress node (quite common, every direct call is)
1188 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1189 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1190 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1191 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1192 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1193
1194 std::vector<MVT::ValueType> NodeTys;
1195 NodeTys.push_back(MVT::Other); // Returns a chain
1196 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1197 std::vector<SDOperand> Ops;
1198 Ops.push_back(Chain);
1199 Ops.push_back(Callee);
1200
1201 // Add argument registers to the end of the list so that they are known live
1202 // into the call.
1203 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1204 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1205 RegsToPass[i].second.getValueType()));
1206
1207 if (InFlag.Val)
1208 Ops.push_back(InFlag);
1209
1210 // FIXME: Do not generate X86ISD::TAILCALL for now.
1211 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1212 NodeTys, &Ops[0], Ops.size());
1213 InFlag = Chain.getValue(1);
1214
1215 NodeTys.clear();
1216 NodeTys.push_back(MVT::Other); // Returns a chain
1217 if (RetVT != MVT::Other)
1218 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1219 Ops.clear();
1220 Ops.push_back(Chain);
1221 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1222 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1223 Ops.push_back(InFlag);
1224 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1225 if (RetVT != MVT::Other)
1226 InFlag = Chain.getValue(1);
1227
1228 std::vector<SDOperand> ResultVals;
1229 NodeTys.clear();
1230 switch (RetVT) {
1231 default: assert(0 && "Unknown value type to return!");
1232 case MVT::Other: break;
1233 case MVT::i8:
1234 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1235 ResultVals.push_back(Chain.getValue(0));
1236 NodeTys.push_back(MVT::i8);
1237 break;
1238 case MVT::i16:
1239 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1240 ResultVals.push_back(Chain.getValue(0));
1241 NodeTys.push_back(MVT::i16);
1242 break;
1243 case MVT::i32:
1244 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1245 ResultVals.push_back(Chain.getValue(0));
1246 NodeTys.push_back(MVT::i32);
1247 break;
1248 case MVT::i64:
1249 if (Op.Val->getValueType(1) == MVT::i64) {
1250 // FIXME: __int128 support?
1251 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1252 ResultVals.push_back(Chain.getValue(0));
1253 Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64,
1254 Chain.getValue(2)).getValue(1);
1255 ResultVals.push_back(Chain.getValue(0));
1256 NodeTys.push_back(MVT::i64);
1257 } else {
1258 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1259 ResultVals.push_back(Chain.getValue(0));
1260 }
1261 NodeTys.push_back(MVT::i64);
1262 break;
1263 case MVT::f32:
1264 case MVT::f64:
1265 case MVT::v16i8:
1266 case MVT::v8i16:
1267 case MVT::v4i32:
1268 case MVT::v2i64:
1269 case MVT::v4f32:
1270 case MVT::v2f64:
1271 // FIXME: long double support?
1272 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1273 ResultVals.push_back(Chain.getValue(0));
1274 NodeTys.push_back(RetVT);
1275 break;
1276 }
1277
1278 // If the function returns void, just return the chain.
1279 if (ResultVals.empty())
1280 return Chain;
1281
1282 // Otherwise, merge everything together with a MERGE_VALUES node.
1283 NodeTys.push_back(MVT::Other);
1284 ResultVals.push_back(Chain);
1285 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1286 &ResultVals[0], ResultVals.size());
1287 return Res.getValue(Op.ResNo);
1288}
1289
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001290//===----------------------------------------------------------------------===//
1291// Fast Calling Convention implementation
1292//===----------------------------------------------------------------------===//
1293//
1294// The X86 'fast' calling convention passes up to two integer arguments in
1295// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1296// and requires that the callee pop its arguments off the stack (allowing proper
1297// tail calls), and has the same return value conventions as C calling convs.
1298//
1299// This calling convention always arranges for the callee pop value to be 8n+4
1300// bytes, which is needed for tail recursion elimination and stack alignment
1301// reasons.
1302//
1303// Note that this can be enhanced in the future to pass fp vals in registers
1304// (when we have a global fp allocator) and do other tricks.
1305//
1306
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001307/// HowToPassFastCCArgument - Returns how an formal argument of the specified
1308/// type should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +00001309/// slot; if it is through integer or XMM register, returns the number of
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001310/// integer or XMM registers are needed.
Evan Chengeda65fa2006-04-27 01:32:22 +00001311static void
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001312HowToPassFastCCArgument(MVT::ValueType ObjectVT,
1313 unsigned NumIntRegs, unsigned NumXMMRegs,
1314 unsigned &ObjSize, unsigned &ObjIntRegs,
1315 unsigned &ObjXMMRegs) {
Evan Chengeda65fa2006-04-27 01:32:22 +00001316 ObjSize = 0;
Evan Cheng26755342006-06-01 05:53:27 +00001317 ObjIntRegs = 0;
1318 ObjXMMRegs = 0;
Evan Chengeda65fa2006-04-27 01:32:22 +00001319
1320 switch (ObjectVT) {
1321 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +00001322 case MVT::i8:
Evan Chengda08d2c2006-06-24 08:36:10 +00001323#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +00001324 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +00001325 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +00001326 else
Evan Chengda08d2c2006-06-24 08:36:10 +00001327#endif
Evan Chengeda65fa2006-04-27 01:32:22 +00001328 ObjSize = 1;
1329 break;
1330 case MVT::i16:
Evan Chengda08d2c2006-06-24 08:36:10 +00001331#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +00001332 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +00001333 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +00001334 else
Evan Chengda08d2c2006-06-24 08:36:10 +00001335#endif
Evan Chengeda65fa2006-04-27 01:32:22 +00001336 ObjSize = 2;
1337 break;
1338 case MVT::i32:
Evan Chengda08d2c2006-06-24 08:36:10 +00001339#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +00001340 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +00001341 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +00001342 else
Evan Chengda08d2c2006-06-24 08:36:10 +00001343#endif
Evan Chengeda65fa2006-04-27 01:32:22 +00001344 ObjSize = 4;
1345 break;
1346 case MVT::i64:
Evan Chengda08d2c2006-06-24 08:36:10 +00001347#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +00001348 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +00001349 ObjIntRegs = 2;
Evan Chengeda65fa2006-04-27 01:32:22 +00001350 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +00001351 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +00001352 ObjSize = 4;
1353 } else
Evan Chengda08d2c2006-06-24 08:36:10 +00001354#endif
Evan Chengeda65fa2006-04-27 01:32:22 +00001355 ObjSize = 8;
1356 case MVT::f32:
1357 ObjSize = 4;
1358 break;
1359 case MVT::f64:
1360 ObjSize = 8;
1361 break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001362 case MVT::v16i8:
1363 case MVT::v8i16:
1364 case MVT::v4i32:
1365 case MVT::v2i64:
1366 case MVT::v4f32:
1367 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001368 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001369 ObjXMMRegs = 1;
1370 else
1371 ObjSize = 16;
1372 break;
Evan Chengeda65fa2006-04-27 01:32:22 +00001373 }
1374}
1375
Evan Cheng25caf632006-05-23 21:06:34 +00001376SDOperand
1377X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1378 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001379 MachineFunction &MF = DAG.getMachineFunction();
1380 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001381 SDOperand Root = Op.getOperand(0);
1382 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001383
Evan Chengeda65fa2006-04-27 01:32:22 +00001384 // Add DAG nodes to load the arguments... On entry to a function the stack
1385 // frame looks like this:
1386 //
1387 // [ESP] -- return address
1388 // [ESP + 4] -- first nonreg argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +00001389 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +00001390 // ...
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001391 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1392
1393 // Keep track of the number of integer regs passed so far. This can be either
1394 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1395 // used).
1396 unsigned NumIntRegs = 0;
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001397 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng32fe1032006-05-25 00:59:30 +00001398
1399 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001400 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001401 };
Chris Lattner1c636e92006-03-17 05:10:20 +00001402
Evan Cheng1bc78042006-04-26 01:20:17 +00001403 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +00001404 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1405 unsigned ArgIncrement = 4;
1406 unsigned ObjSize = 0;
1407 unsigned ObjIntRegs = 0;
1408 unsigned ObjXMMRegs = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001409
Evan Cheng25caf632006-05-23 21:06:34 +00001410 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
1411 ObjSize, ObjIntRegs, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +00001412 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +00001413 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +00001414
Evan Cheng04b25622006-06-01 00:30:39 +00001415 unsigned Reg = 0;
Evan Cheng25caf632006-05-23 21:06:34 +00001416 SDOperand ArgValue;
1417 if (ObjIntRegs || ObjXMMRegs) {
1418 switch (ObjectVT) {
1419 default: assert(0 && "Unhandled argument type!");
Evan Cheng25caf632006-05-23 21:06:34 +00001420 case MVT::i8:
1421 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
1422 X86::GR8RegisterClass);
1423 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
1424 break;
1425 case MVT::i16:
1426 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
1427 X86::GR16RegisterClass);
1428 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
1429 break;
1430 case MVT::i32:
1431 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1432 X86::GR32RegisterClass);
1433 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1434 break;
1435 case MVT::i64:
1436 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1437 X86::GR32RegisterClass);
1438 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1439 if (ObjIntRegs == 2) {
1440 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
1441 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1442 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng85e38002006-04-27 05:35:28 +00001443 }
Evan Cheng25caf632006-05-23 21:06:34 +00001444 break;
1445 case MVT::v16i8:
1446 case MVT::v8i16:
1447 case MVT::v4i32:
1448 case MVT::v2i64:
1449 case MVT::v4f32:
1450 case MVT::v2f64:
1451 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1452 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1453 break;
Evan Chengeda65fa2006-04-27 01:32:22 +00001454 }
Evan Cheng25caf632006-05-23 21:06:34 +00001455 NumIntRegs += ObjIntRegs;
1456 NumXMMRegs += ObjXMMRegs;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001457 }
Evan Cheng25caf632006-05-23 21:06:34 +00001458
1459 if (ObjSize) {
Evan Cheng3fddf242006-05-26 20:37:47 +00001460 // XMM arguments have to be aligned on 16-byte boundary.
1461 if (ObjSize == 16)
1462 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +00001463 // Create the SelectionDAG nodes corresponding to a load from this
1464 // parameter.
1465 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1466 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1467 if (ObjectVT == MVT::i64 && ObjIntRegs) {
1468 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
1469 DAG.getSrcValue(NULL));
1470 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1471 } else
1472 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
1473 DAG.getSrcValue(NULL));
1474 ArgOffset += ArgIncrement; // Move on to the next argument.
1475 }
1476
1477 ArgValues.push_back(ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001478 }
1479
Evan Cheng25caf632006-05-23 21:06:34 +00001480 ArgValues.push_back(Root);
1481
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001482 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1483 // arguments and the arguments after the retaddr has been pushed are aligned.
1484 if ((ArgOffset & 7) == 0)
1485 ArgOffset += 4;
1486
1487 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001488 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001489 ReturnAddrIndex = 0; // No return address slot generated yet.
1490 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1491 BytesCallerReserves = 0;
1492
1493 // Finally, inform the code generator which regs we return values in.
Evan Cheng25caf632006-05-23 21:06:34 +00001494 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001495 default: assert(0 && "Unknown type!");
1496 case MVT::isVoid: break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001497 case MVT::i8:
1498 case MVT::i16:
1499 case MVT::i32:
1500 MF.addLiveOut(X86::EAX);
1501 break;
1502 case MVT::i64:
1503 MF.addLiveOut(X86::EAX);
1504 MF.addLiveOut(X86::EDX);
1505 break;
1506 case MVT::f32:
1507 case MVT::f64:
1508 MF.addLiveOut(X86::ST0);
1509 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001510 case MVT::v16i8:
1511 case MVT::v8i16:
1512 case MVT::v4i32:
1513 case MVT::v2i64:
1514 case MVT::v4f32:
1515 case MVT::v2f64:
Evan Cheng347d5f72006-04-28 21:29:37 +00001516 MF.addLiveOut(X86::XMM0);
1517 break;
1518 }
Evan Cheng347d5f72006-04-28 21:29:37 +00001519
Evan Cheng25caf632006-05-23 21:06:34 +00001520 // Return the new list of results.
1521 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1522 Op.Val->value_end());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001523 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001524}
1525
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001526SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG){
Evan Cheng32fe1032006-05-25 00:59:30 +00001527 SDOperand Chain = Op.getOperand(0);
1528 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1529 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1530 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1531 SDOperand Callee = Op.getOperand(4);
1532 MVT::ValueType RetVT= Op.Val->getValueType(0);
1533 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1534
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001535 // Count how many bytes are to be pushed on the stack.
1536 unsigned NumBytes = 0;
1537
1538 // Keep track of the number of integer regs passed so far. This can be either
1539 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1540 // used).
1541 unsigned NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001542 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001543
Evan Cheng32fe1032006-05-25 00:59:30 +00001544 static const unsigned GPRArgRegs[][2] = {
1545 { X86::AL, X86::DL },
1546 { X86::AX, X86::DX },
1547 { X86::EAX, X86::EDX }
1548 };
1549 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001550 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001551 };
1552
1553 for (unsigned i = 0; i != NumOps; ++i) {
1554 SDOperand Arg = Op.getOperand(5+2*i);
1555
1556 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001557 default: assert(0 && "Unknown value type!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001558 case MVT::i8:
1559 case MVT::i16:
1560 case MVT::i32:
Evan Chengda08d2c2006-06-24 08:36:10 +00001561#if FASTCC_NUM_INT_ARGS_INREGS > 0
Chris Lattner1c636e92006-03-17 05:10:20 +00001562 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001563 ++NumIntRegs;
1564 break;
1565 }
Evan Chengda08d2c2006-06-24 08:36:10 +00001566#endif
Evan Cheng25e71d12006-05-25 22:38:31 +00001567 // Fall through
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001568 case MVT::f32:
1569 NumBytes += 4;
1570 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001571 case MVT::f64:
1572 NumBytes += 8;
1573 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001574 case MVT::v16i8:
1575 case MVT::v8i16:
1576 case MVT::v4i32:
1577 case MVT::v2i64:
1578 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001579 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001580 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +00001581 NumXMMRegs++;
Evan Cheng3fddf242006-05-26 20:37:47 +00001582 else {
1583 // XMM arguments have to be aligned on 16-byte boundary.
1584 NumBytes = ((NumBytes + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +00001585 NumBytes += 16;
Evan Cheng3fddf242006-05-26 20:37:47 +00001586 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001587 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001588 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001589 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001590
1591 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1592 // arguments and the arguments after the retaddr has been pushed are aligned.
1593 if ((NumBytes & 7) == 0)
1594 NumBytes += 4;
1595
Chris Lattner94dd2922006-02-13 09:00:43 +00001596 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001597
1598 // Arguments go on the stack in reverse order, as specified by the ABI.
1599 unsigned ArgOffset = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001600 NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001601 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1602 std::vector<SDOperand> MemOpChains;
Evan Cheng25ab6902006-09-08 06:48:29 +00001603 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001604 for (unsigned i = 0; i != NumOps; ++i) {
1605 SDOperand Arg = Op.getOperand(5+2*i);
1606
1607 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001608 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001609 case MVT::i8:
1610 case MVT::i16:
1611 case MVT::i32:
Evan Chengda08d2c2006-06-24 08:36:10 +00001612#if FASTCC_NUM_INT_ARGS_INREGS > 0
Chris Lattner1c636e92006-03-17 05:10:20 +00001613 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001614 RegsToPass.push_back(
1615 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1616 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001617 ++NumIntRegs;
1618 break;
1619 }
Evan Chengda08d2c2006-06-24 08:36:10 +00001620#endif
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001621 // Fall through
1622 case MVT::f32: {
1623 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001624 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1625 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1626 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001627 ArgOffset += 4;
1628 break;
1629 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001630 case MVT::f64: {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001631 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001632 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1633 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1634 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001635 ArgOffset += 8;
1636 break;
1637 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001638 case MVT::v16i8:
1639 case MVT::v8i16:
1640 case MVT::v4i32:
1641 case MVT::v2i64:
1642 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001643 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001644 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001645 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1646 NumXMMRegs++;
1647 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +00001648 // XMM arguments have to be aligned on 16-byte boundary.
1649 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +00001650 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1651 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1652 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1653 Arg, PtrOff, DAG.getSrcValue(NULL)));
1654 ArgOffset += 16;
1655 }
1656 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001658
Evan Cheng32fe1032006-05-25 00:59:30 +00001659 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001660 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1661 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001662
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001663 // Build a sequence of copy-to-reg nodes chained together with token chain
1664 // and flag operands which copy the outgoing args into registers.
1665 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001666 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1667 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1668 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001669 InFlag = Chain.getValue(1);
1670 }
1671
Evan Cheng32fe1032006-05-25 00:59:30 +00001672 // If the callee is a GlobalAddress node (quite common, every direct call is)
1673 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1674 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1675 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1676 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1677 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1678
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001679 std::vector<MVT::ValueType> NodeTys;
1680 NodeTys.push_back(MVT::Other); // Returns a chain
1681 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1682 std::vector<SDOperand> Ops;
1683 Ops.push_back(Chain);
1684 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001685
1686 // Add argument registers to the end of the list so that they are known live
1687 // into the call.
1688 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1689 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1690 RegsToPass[i].second.getValueType()));
1691
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001692 if (InFlag.Val)
1693 Ops.push_back(InFlag);
1694
1695 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001696 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001697 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001698 InFlag = Chain.getValue(1);
1699
1700 NodeTys.clear();
1701 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +00001702 if (RetVT != MVT::Other)
1703 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001704 Ops.clear();
1705 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001706 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1707 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001708 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001709 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001710 if (RetVT != MVT::Other)
1711 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001712
Evan Cheng32fe1032006-05-25 00:59:30 +00001713 std::vector<SDOperand> ResultVals;
1714 NodeTys.clear();
1715 switch (RetVT) {
1716 default: assert(0 && "Unknown value type to return!");
1717 case MVT::Other: break;
1718 case MVT::i8:
1719 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1720 ResultVals.push_back(Chain.getValue(0));
1721 NodeTys.push_back(MVT::i8);
1722 break;
1723 case MVT::i16:
1724 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1725 ResultVals.push_back(Chain.getValue(0));
1726 NodeTys.push_back(MVT::i16);
1727 break;
1728 case MVT::i32:
1729 if (Op.Val->getValueType(1) == MVT::i32) {
1730 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1731 ResultVals.push_back(Chain.getValue(0));
1732 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1733 Chain.getValue(2)).getValue(1);
1734 ResultVals.push_back(Chain.getValue(0));
1735 NodeTys.push_back(MVT::i32);
1736 } else {
1737 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1738 ResultVals.push_back(Chain.getValue(0));
Evan Chengd9558e02006-01-06 00:43:03 +00001739 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001740 NodeTys.push_back(MVT::i32);
1741 break;
1742 case MVT::v16i8:
1743 case MVT::v8i16:
1744 case MVT::v4i32:
1745 case MVT::v2i64:
1746 case MVT::v4f32:
1747 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +00001748 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1749 ResultVals.push_back(Chain.getValue(0));
1750 NodeTys.push_back(RetVT);
1751 break;
1752 case MVT::f32:
1753 case MVT::f64: {
1754 std::vector<MVT::ValueType> Tys;
1755 Tys.push_back(MVT::f64);
1756 Tys.push_back(MVT::Other);
1757 Tys.push_back(MVT::Flag);
1758 std::vector<SDOperand> Ops;
1759 Ops.push_back(Chain);
1760 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001761 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1762 &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001763 Chain = RetVal.getValue(1);
1764 InFlag = RetVal.getValue(2);
1765 if (X86ScalarSSE) {
1766 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1767 // shouldn't be necessary except that RFP cannot be live across
1768 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1769 MachineFunction &MF = DAG.getMachineFunction();
1770 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1771 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1772 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001773 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +00001774 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001775 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001776 Ops.push_back(RetVal);
1777 Ops.push_back(StackSlot);
1778 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001779 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001780 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001781 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1782 DAG.getSrcValue(NULL));
1783 Chain = RetVal.getValue(1);
1784 }
Evan Chengd9558e02006-01-06 00:43:03 +00001785
Evan Cheng32fe1032006-05-25 00:59:30 +00001786 if (RetVT == MVT::f32 && !X86ScalarSSE)
1787 // FIXME: we would really like to remember that this FP_ROUND
1788 // operation is okay to eliminate if we allow excess FP precision.
1789 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1790 ResultVals.push_back(RetVal);
1791 NodeTys.push_back(RetVT);
1792 break;
1793 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001794 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001795
Evan Cheng32fe1032006-05-25 00:59:30 +00001796
1797 // If the function returns void, just return the chain.
1798 if (ResultVals.empty())
1799 return Chain;
1800
1801 // Otherwise, merge everything together with a MERGE_VALUES node.
1802 NodeTys.push_back(MVT::Other);
1803 ResultVals.push_back(Chain);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001804 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1805 &ResultVals[0], ResultVals.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001806 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001807}
1808
1809SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1810 if (ReturnAddrIndex == 0) {
1811 // Set up a frame object for the return address.
1812 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng25ab6902006-09-08 06:48:29 +00001813 if (Subtarget->is64Bit())
1814 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1815 else
1816 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001817 }
1818
Evan Cheng25ab6902006-09-08 06:48:29 +00001819 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001820}
1821
1822
1823
1824std::pair<SDOperand, SDOperand> X86TargetLowering::
1825LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1826 SelectionDAG &DAG) {
1827 SDOperand Result;
1828 if (Depth) // Depths > 0 not supported yet!
1829 Result = DAG.getConstant(0, getPointerTy());
1830 else {
1831 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1832 if (!isFrameAddress)
1833 // Just load the return address
Evan Cheng25ab6902006-09-08 06:48:29 +00001834 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001835 DAG.getSrcValue(NULL));
1836 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001837 Result = DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
1838 DAG.getConstant(4, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001839 }
1840 return std::make_pair(Result, Chain);
1841}
1842
Evan Cheng4a460802006-01-11 00:33:36 +00001843/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1844/// which corresponds to the condition code.
1845static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1846 switch (X86CC) {
1847 default: assert(0 && "Unknown X86 conditional code!");
1848 case X86ISD::COND_A: return X86::JA;
1849 case X86ISD::COND_AE: return X86::JAE;
1850 case X86ISD::COND_B: return X86::JB;
1851 case X86ISD::COND_BE: return X86::JBE;
1852 case X86ISD::COND_E: return X86::JE;
1853 case X86ISD::COND_G: return X86::JG;
1854 case X86ISD::COND_GE: return X86::JGE;
1855 case X86ISD::COND_L: return X86::JL;
1856 case X86ISD::COND_LE: return X86::JLE;
1857 case X86ISD::COND_NE: return X86::JNE;
1858 case X86ISD::COND_NO: return X86::JNO;
1859 case X86ISD::COND_NP: return X86::JNP;
1860 case X86ISD::COND_NS: return X86::JNS;
1861 case X86ISD::COND_O: return X86::JO;
1862 case X86ISD::COND_P: return X86::JP;
1863 case X86ISD::COND_S: return X86::JS;
1864 }
1865}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001866
Evan Cheng6dfa9992006-01-30 23:41:35 +00001867/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1868/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001869/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1870/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001871static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001872 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1873 SelectionDAG &DAG) {
Evan Cheng6dfa9992006-01-30 23:41:35 +00001874 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001875 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001876 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1877 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1878 // X > -1 -> X == 0, jump !sign.
1879 RHS = DAG.getConstant(0, RHS.getValueType());
1880 X86CC = X86ISD::COND_NS;
1881 return true;
1882 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1883 // X < 0 -> X == 0, jump on sign.
1884 X86CC = X86ISD::COND_S;
1885 return true;
1886 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001887 }
1888
Evan Chengd9558e02006-01-06 00:43:03 +00001889 switch (SetCCOpcode) {
1890 default: break;
1891 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1892 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1893 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1894 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1895 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1896 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1897 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1898 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1899 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1900 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1901 }
1902 } else {
1903 // On a floating point condition, the flags are set as follows:
1904 // ZF PF CF op
1905 // 0 | 0 | 0 | X > Y
1906 // 0 | 0 | 1 | X < Y
1907 // 1 | 0 | 0 | X == Y
1908 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001909 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001910 switch (SetCCOpcode) {
1911 default: break;
1912 case ISD::SETUEQ:
1913 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001914 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001915 case ISD::SETOGT:
1916 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001917 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001918 case ISD::SETOGE:
1919 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001920 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001921 case ISD::SETULT:
1922 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001923 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001924 case ISD::SETULE:
1925 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1926 case ISD::SETONE:
1927 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1928 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1929 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1930 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001931 if (Flip)
1932 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001933 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001934
1935 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001936}
1937
Evan Cheng4a460802006-01-11 00:33:36 +00001938/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1939/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001940/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001941static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001942 switch (X86CC) {
1943 default:
1944 return false;
1945 case X86ISD::COND_B:
1946 case X86ISD::COND_BE:
1947 case X86ISD::COND_E:
1948 case X86ISD::COND_P:
1949 case X86ISD::COND_A:
1950 case X86ISD::COND_AE:
1951 case X86ISD::COND_NE:
1952 case X86ISD::COND_NP:
1953 return true;
1954 }
1955}
1956
Evan Cheng30b37b52006-03-13 23:18:16 +00001957/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1958/// load. For Darwin, external and weak symbols are indirect, loading the value
1959/// at address GV rather then the value of GV itself. This means that the
1960/// GlobalAddress must be in the base or index register of the address, not the
1961/// GV offset field.
1962static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1963 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1964 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1965}
1966
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00001967/// WindowsGVRequiresExtraLoad - true if accessing the GV requires an extra
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001968/// load. For Windows, dllimported variables (not functions!) are indirect,
1969/// loading the value at address GV rather then the value of GV itself. This
1970/// means that the GlobalAddress must be in the base or index register of the
1971/// address, not the GV offset field.
1972static bool WindowsGVRequiresExtraLoad(GlobalValue *GV) {
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00001973 return (GV->hasDLLImportLinkage());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001974}
1975
Evan Cheng5ced1d82006-04-06 23:23:56 +00001976/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001977/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001978static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1979 if (Op.getOpcode() == ISD::UNDEF)
1980 return true;
1981
1982 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001983 return (Val >= Low && Val < Hi);
1984}
1985
1986/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1987/// true if Op is undef or if its value equal to the specified value.
1988static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1989 if (Op.getOpcode() == ISD::UNDEF)
1990 return true;
1991 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001992}
1993
Evan Cheng0188ecb2006-03-22 18:59:22 +00001994/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1995/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1996bool X86::isPSHUFDMask(SDNode *N) {
1997 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1998
1999 if (N->getNumOperands() != 4)
2000 return false;
2001
2002 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002003 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002004 SDOperand Arg = N->getOperand(i);
2005 if (Arg.getOpcode() == ISD::UNDEF) continue;
2006 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2007 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00002008 return false;
2009 }
2010
2011 return true;
2012}
2013
2014/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002015/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002016bool X86::isPSHUFHWMask(SDNode *N) {
2017 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2018
2019 if (N->getNumOperands() != 8)
2020 return false;
2021
2022 // Lower quadword copied in order.
2023 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002024 SDOperand Arg = N->getOperand(i);
2025 if (Arg.getOpcode() == ISD::UNDEF) continue;
2026 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2027 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002028 return false;
2029 }
2030
2031 // Upper quadword shuffled.
2032 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002033 SDOperand Arg = N->getOperand(i);
2034 if (Arg.getOpcode() == ISD::UNDEF) continue;
2035 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2036 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002037 if (Val < 4 || Val > 7)
2038 return false;
2039 }
2040
2041 return true;
2042}
2043
2044/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002045/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002046bool X86::isPSHUFLWMask(SDNode *N) {
2047 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2048
2049 if (N->getNumOperands() != 8)
2050 return false;
2051
2052 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002053 for (unsigned i = 4; i != 8; ++i)
2054 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002055 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002056
2057 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002058 for (unsigned i = 0; i != 4; ++i)
2059 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002060 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002061
2062 return true;
2063}
2064
Evan Cheng14aed5e2006-03-24 01:18:28 +00002065/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2066/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng39623da2006-04-20 08:58:49 +00002067static bool isSHUFPMask(std::vector<SDOperand> &N) {
2068 unsigned NumElems = N.size();
2069 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002070
Evan Cheng39623da2006-04-20 08:58:49 +00002071 unsigned Half = NumElems / 2;
2072 for (unsigned i = 0; i < Half; ++i)
2073 if (!isUndefOrInRange(N[i], 0, NumElems))
2074 return false;
2075 for (unsigned i = Half; i < NumElems; ++i)
2076 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
2077 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002078
2079 return true;
2080}
2081
Evan Cheng39623da2006-04-20 08:58:49 +00002082bool X86::isSHUFPMask(SDNode *N) {
2083 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2084 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2085 return ::isSHUFPMask(Ops);
2086}
2087
2088/// isCommutedSHUFP - Returns true if the shuffle mask is except
2089/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2090/// half elements to come from vector 1 (which would equal the dest.) and
2091/// the upper half to come from vector 2.
2092static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
2093 unsigned NumElems = Ops.size();
2094 if (NumElems != 2 && NumElems != 4) return false;
2095
2096 unsigned Half = NumElems / 2;
2097 for (unsigned i = 0; i < Half; ++i)
2098 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
2099 return false;
2100 for (unsigned i = Half; i < NumElems; ++i)
2101 if (!isUndefOrInRange(Ops[i], 0, NumElems))
2102 return false;
2103 return true;
2104}
2105
2106static bool isCommutedSHUFP(SDNode *N) {
2107 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2108 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2109 return isCommutedSHUFP(Ops);
2110}
2111
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002112/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2113/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2114bool X86::isMOVHLPSMask(SDNode *N) {
2115 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2116
Evan Cheng2064a2b2006-03-28 06:50:32 +00002117 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002118 return false;
2119
Evan Cheng2064a2b2006-03-28 06:50:32 +00002120 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002121 return isUndefOrEqual(N->getOperand(0), 6) &&
2122 isUndefOrEqual(N->getOperand(1), 7) &&
2123 isUndefOrEqual(N->getOperand(2), 2) &&
2124 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002125}
2126
Evan Cheng5ced1d82006-04-06 23:23:56 +00002127/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2128/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2129bool X86::isMOVLPMask(SDNode *N) {
2130 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2131
2132 unsigned NumElems = N->getNumOperands();
2133 if (NumElems != 2 && NumElems != 4)
2134 return false;
2135
Evan Chengc5cdff22006-04-07 21:53:05 +00002136 for (unsigned i = 0; i < NumElems/2; ++i)
2137 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2138 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002139
Evan Chengc5cdff22006-04-07 21:53:05 +00002140 for (unsigned i = NumElems/2; i < NumElems; ++i)
2141 if (!isUndefOrEqual(N->getOperand(i), i))
2142 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002143
2144 return true;
2145}
2146
2147/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002148/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2149/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002150bool X86::isMOVHPMask(SDNode *N) {
2151 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2152
2153 unsigned NumElems = N->getNumOperands();
2154 if (NumElems != 2 && NumElems != 4)
2155 return false;
2156
Evan Chengc5cdff22006-04-07 21:53:05 +00002157 for (unsigned i = 0; i < NumElems/2; ++i)
2158 if (!isUndefOrEqual(N->getOperand(i), i))
2159 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002160
2161 for (unsigned i = 0; i < NumElems/2; ++i) {
2162 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002163 if (!isUndefOrEqual(Arg, i + NumElems))
2164 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002165 }
2166
2167 return true;
2168}
2169
Evan Cheng0038e592006-03-28 00:39:58 +00002170/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2171/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng39623da2006-04-20 08:58:49 +00002172bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2173 unsigned NumElems = N.size();
Evan Cheng0038e592006-03-28 00:39:58 +00002174 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2175 return false;
2176
2177 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00002178 SDOperand BitI = N[i];
2179 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002180 if (!isUndefOrEqual(BitI, j))
2181 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002182 if (V2IsSplat) {
2183 if (isUndefOrEqual(BitI1, NumElems))
2184 return false;
2185 } else {
2186 if (!isUndefOrEqual(BitI1, j + NumElems))
2187 return false;
2188 }
Evan Cheng0038e592006-03-28 00:39:58 +00002189 }
2190
2191 return true;
2192}
2193
Evan Cheng39623da2006-04-20 08:58:49 +00002194bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2195 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2196 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2197 return ::isUNPCKLMask(Ops, V2IsSplat);
2198}
2199
Evan Cheng4fcb9222006-03-28 02:43:26 +00002200/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2201/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng39623da2006-04-20 08:58:49 +00002202bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2203 unsigned NumElems = N.size();
Evan Cheng4fcb9222006-03-28 02:43:26 +00002204 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2205 return false;
2206
2207 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00002208 SDOperand BitI = N[i];
2209 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002210 if (!isUndefOrEqual(BitI, j + NumElems/2))
2211 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002212 if (V2IsSplat) {
2213 if (isUndefOrEqual(BitI1, NumElems))
2214 return false;
2215 } else {
2216 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
2217 return false;
2218 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002219 }
2220
2221 return true;
2222}
2223
Evan Cheng39623da2006-04-20 08:58:49 +00002224bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2225 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2226 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2227 return ::isUNPCKHMask(Ops, V2IsSplat);
2228}
2229
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002230/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2231/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2232/// <0, 0, 1, 1>
2233bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2234 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2235
2236 unsigned NumElems = N->getNumOperands();
2237 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
2238 return false;
2239
2240 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2241 SDOperand BitI = N->getOperand(i);
2242 SDOperand BitI1 = N->getOperand(i+1);
2243
Evan Chengc5cdff22006-04-07 21:53:05 +00002244 if (!isUndefOrEqual(BitI, j))
2245 return false;
2246 if (!isUndefOrEqual(BitI1, j))
2247 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002248 }
2249
2250 return true;
2251}
2252
Evan Cheng017dcc62006-04-21 01:05:10 +00002253/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2254/// specifies a shuffle of elements that is suitable for input to MOVSS,
2255/// MOVSD, and MOVD, i.e. setting the lowest element.
2256static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002257 unsigned NumElems = N.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00002258 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002259 return false;
2260
Evan Cheng39623da2006-04-20 08:58:49 +00002261 if (!isUndefOrEqual(N[0], NumElems))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002262 return false;
2263
2264 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002265 SDOperand Arg = N[i];
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002266 if (!isUndefOrEqual(Arg, i))
2267 return false;
2268 }
2269
2270 return true;
2271}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002272
Evan Cheng017dcc62006-04-21 01:05:10 +00002273bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002274 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2275 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00002276 return ::isMOVLMask(Ops);
Evan Cheng39623da2006-04-20 08:58:49 +00002277}
2278
Evan Cheng017dcc62006-04-21 01:05:10 +00002279/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2280/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002281/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Cheng8cf723d2006-09-08 01:50:06 +00002282static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false,
2283 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002284 unsigned NumElems = Ops.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00002285 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002286 return false;
2287
2288 if (!isUndefOrEqual(Ops[0], 0))
2289 return false;
2290
2291 for (unsigned i = 1; i < NumElems; ++i) {
2292 SDOperand Arg = Ops[i];
Evan Cheng8cf723d2006-09-08 01:50:06 +00002293 if (!(isUndefOrEqual(Arg, i+NumElems) ||
2294 (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) ||
2295 (V2IsSplat && isUndefOrEqual(Arg, NumElems))))
2296 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002297 }
2298
2299 return true;
2300}
2301
Evan Cheng8cf723d2006-09-08 01:50:06 +00002302static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2303 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002304 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2305 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng8cf723d2006-09-08 01:50:06 +00002306 return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002307}
2308
Evan Chengd9539472006-04-14 21:59:03 +00002309/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2310/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2311bool X86::isMOVSHDUPMask(SDNode *N) {
2312 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2313
2314 if (N->getNumOperands() != 4)
2315 return false;
2316
2317 // Expect 1, 1, 3, 3
2318 for (unsigned i = 0; i < 2; ++i) {
2319 SDOperand Arg = N->getOperand(i);
2320 if (Arg.getOpcode() == ISD::UNDEF) continue;
2321 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2322 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2323 if (Val != 1) return false;
2324 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002325
2326 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002327 for (unsigned i = 2; i < 4; ++i) {
2328 SDOperand Arg = N->getOperand(i);
2329 if (Arg.getOpcode() == ISD::UNDEF) continue;
2330 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2331 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2332 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002333 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002334 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002335
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002336 // Don't use movshdup if it can be done with a shufps.
2337 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002338}
2339
2340/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2341/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2342bool X86::isMOVSLDUPMask(SDNode *N) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344
2345 if (N->getNumOperands() != 4)
2346 return false;
2347
2348 // Expect 0, 0, 2, 2
2349 for (unsigned i = 0; i < 2; ++i) {
2350 SDOperand Arg = N->getOperand(i);
2351 if (Arg.getOpcode() == ISD::UNDEF) continue;
2352 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2353 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2354 if (Val != 0) return false;
2355 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002356
2357 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002358 for (unsigned i = 2; i < 4; ++i) {
2359 SDOperand Arg = N->getOperand(i);
2360 if (Arg.getOpcode() == ISD::UNDEF) continue;
2361 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2362 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2363 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002364 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002365 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002366
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002367 // Don't use movshdup if it can be done with a shufps.
2368 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002369}
2370
Evan Chengb9df0ca2006-03-22 02:53:00 +00002371/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2372/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002373static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002374 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2375
Evan Chengb9df0ca2006-03-22 02:53:00 +00002376 // This is a splat operation if each element of the permute is the same, and
2377 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002378 unsigned NumElems = N->getNumOperands();
2379 SDOperand ElementBase;
2380 unsigned i = 0;
2381 for (; i != NumElems; ++i) {
2382 SDOperand Elt = N->getOperand(i);
2383 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
2384 ElementBase = Elt;
2385 break;
2386 }
2387 }
2388
2389 if (!ElementBase.Val)
2390 return false;
2391
2392 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002393 SDOperand Arg = N->getOperand(i);
2394 if (Arg.getOpcode() == ISD::UNDEF) continue;
2395 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002396 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002397 }
2398
2399 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002400 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002401}
2402
Evan Chengc575ca22006-04-17 20:43:08 +00002403/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2404/// a splat of a single element and it's a 2 or 4 element mask.
2405bool X86::isSplatMask(SDNode *N) {
2406 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2407
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002408 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002409 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2410 return false;
2411 return ::isSplatMask(N);
2412}
2413
Evan Cheng63d33002006-03-22 08:01:21 +00002414/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2415/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2416/// instructions.
2417unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002418 unsigned NumOperands = N->getNumOperands();
2419 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2420 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002421 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002422 unsigned Val = 0;
2423 SDOperand Arg = N->getOperand(NumOperands-i-1);
2424 if (Arg.getOpcode() != ISD::UNDEF)
2425 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002426 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002427 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002428 if (i != NumOperands - 1)
2429 Mask <<= Shift;
2430 }
Evan Cheng63d33002006-03-22 08:01:21 +00002431
2432 return Mask;
2433}
2434
Evan Cheng506d3df2006-03-29 23:07:14 +00002435/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2436/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2437/// instructions.
2438unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2439 unsigned Mask = 0;
2440 // 8 nodes, but we only care about the last 4.
2441 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002442 unsigned Val = 0;
2443 SDOperand Arg = N->getOperand(i);
2444 if (Arg.getOpcode() != ISD::UNDEF)
2445 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002446 Mask |= (Val - 4);
2447 if (i != 4)
2448 Mask <<= 2;
2449 }
2450
2451 return Mask;
2452}
2453
2454/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2455/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2456/// instructions.
2457unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2458 unsigned Mask = 0;
2459 // 8 nodes, but we only care about the first 4.
2460 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002461 unsigned Val = 0;
2462 SDOperand Arg = N->getOperand(i);
2463 if (Arg.getOpcode() != ISD::UNDEF)
2464 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002465 Mask |= Val;
2466 if (i != 0)
2467 Mask <<= 2;
2468 }
2469
2470 return Mask;
2471}
2472
Evan Chengc21a0532006-04-05 01:47:37 +00002473/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2474/// specifies a 8 element shuffle that can be broken into a pair of
2475/// PSHUFHW and PSHUFLW.
2476static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2477 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2478
2479 if (N->getNumOperands() != 8)
2480 return false;
2481
2482 // Lower quadword shuffled.
2483 for (unsigned i = 0; i != 4; ++i) {
2484 SDOperand Arg = N->getOperand(i);
2485 if (Arg.getOpcode() == ISD::UNDEF) continue;
2486 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2487 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2488 if (Val > 4)
2489 return false;
2490 }
2491
2492 // Upper quadword shuffled.
2493 for (unsigned i = 4; i != 8; ++i) {
2494 SDOperand Arg = N->getOperand(i);
2495 if (Arg.getOpcode() == ISD::UNDEF) continue;
2496 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2497 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2498 if (Val < 4 || Val > 7)
2499 return false;
2500 }
2501
2502 return true;
2503}
2504
Evan Cheng5ced1d82006-04-06 23:23:56 +00002505/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2506/// values in ther permute mask.
2507static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2508 SDOperand V1 = Op.getOperand(0);
2509 SDOperand V2 = Op.getOperand(1);
2510 SDOperand Mask = Op.getOperand(2);
2511 MVT::ValueType VT = Op.getValueType();
2512 MVT::ValueType MaskVT = Mask.getValueType();
2513 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2514 unsigned NumElems = Mask.getNumOperands();
2515 std::vector<SDOperand> MaskVec;
2516
2517 for (unsigned i = 0; i != NumElems; ++i) {
2518 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002519 if (Arg.getOpcode() == ISD::UNDEF) {
2520 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2521 continue;
2522 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002523 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2524 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2525 if (Val < NumElems)
2526 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2527 else
2528 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2529 }
2530
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002531 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng5ced1d82006-04-06 23:23:56 +00002532 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2533}
2534
Evan Cheng533a0aa2006-04-19 20:35:22 +00002535/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2536/// match movhlps. The lower half elements should come from upper half of
2537/// V1 (and in order), and the upper half elements should come from the upper
2538/// half of V2 (and in order).
2539static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2540 unsigned NumElems = Mask->getNumOperands();
2541 if (NumElems != 4)
2542 return false;
2543 for (unsigned i = 0, e = 2; i != e; ++i)
2544 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2545 return false;
2546 for (unsigned i = 2; i != 4; ++i)
2547 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2548 return false;
2549 return true;
2550}
2551
Evan Cheng5ced1d82006-04-06 23:23:56 +00002552/// isScalarLoadToVector - Returns true if the node is a scalar load that
2553/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002554static inline bool isScalarLoadToVector(SDNode *N) {
2555 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2556 N = N->getOperand(0).Val;
2557 return (N->getOpcode() == ISD::LOAD);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002558 }
2559 return false;
2560}
2561
Evan Cheng533a0aa2006-04-19 20:35:22 +00002562/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2563/// match movlp{s|d}. The lower half elements should come from lower half of
2564/// V1 (and in order), and the upper half elements should come from the upper
2565/// half of V2 (and in order). And since V1 will become the source of the
2566/// MOVLP, it must be either a vector load or a scalar load to vector.
2567static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2568 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2569 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002570
Evan Cheng533a0aa2006-04-19 20:35:22 +00002571 unsigned NumElems = Mask->getNumOperands();
2572 if (NumElems != 2 && NumElems != 4)
2573 return false;
2574 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2575 if (!isUndefOrEqual(Mask->getOperand(i), i))
2576 return false;
2577 for (unsigned i = NumElems/2; i != NumElems; ++i)
2578 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2579 return false;
2580 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002581}
2582
Evan Cheng39623da2006-04-20 08:58:49 +00002583/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2584/// all the same.
2585static bool isSplatVector(SDNode *N) {
2586 if (N->getOpcode() != ISD::BUILD_VECTOR)
2587 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002588
Evan Cheng39623da2006-04-20 08:58:49 +00002589 SDOperand SplatValue = N->getOperand(0);
2590 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2591 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002592 return false;
2593 return true;
2594}
2595
Evan Cheng8cf723d2006-09-08 01:50:06 +00002596/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2597/// to an undef.
2598static bool isUndefShuffle(SDNode *N) {
2599 if (N->getOpcode() != ISD::BUILD_VECTOR)
2600 return false;
2601
2602 SDOperand V1 = N->getOperand(0);
2603 SDOperand V2 = N->getOperand(1);
2604 SDOperand Mask = N->getOperand(2);
2605 unsigned NumElems = Mask.getNumOperands();
2606 for (unsigned i = 0; i != NumElems; ++i) {
2607 SDOperand Arg = Mask.getOperand(i);
2608 if (Arg.getOpcode() != ISD::UNDEF) {
2609 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2610 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2611 return false;
2612 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2613 return false;
2614 }
2615 }
2616 return true;
2617}
2618
Evan Cheng39623da2006-04-20 08:58:49 +00002619/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2620/// that point to V2 points to its first element.
2621static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2622 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2623
2624 bool Changed = false;
2625 std::vector<SDOperand> MaskVec;
2626 unsigned NumElems = Mask.getNumOperands();
2627 for (unsigned i = 0; i != NumElems; ++i) {
2628 SDOperand Arg = Mask.getOperand(i);
2629 if (Arg.getOpcode() != ISD::UNDEF) {
2630 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2631 if (Val > NumElems) {
2632 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2633 Changed = true;
2634 }
2635 }
2636 MaskVec.push_back(Arg);
2637 }
2638
2639 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002640 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2641 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002642 return Mask;
2643}
2644
Evan Cheng017dcc62006-04-21 01:05:10 +00002645/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2646/// operation of specified width.
2647static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002648 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2649 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2650
2651 std::vector<SDOperand> MaskVec;
2652 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2653 for (unsigned i = 1; i != NumElems; ++i)
2654 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002655 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002656}
2657
Evan Chengc575ca22006-04-17 20:43:08 +00002658/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2659/// of specified width.
2660static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2661 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2662 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2663 std::vector<SDOperand> MaskVec;
2664 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2665 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2666 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2667 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002668 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002669}
2670
Evan Cheng39623da2006-04-20 08:58:49 +00002671/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2672/// of specified width.
2673static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2674 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2675 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2676 unsigned Half = NumElems/2;
2677 std::vector<SDOperand> MaskVec;
2678 for (unsigned i = 0; i != Half; ++i) {
2679 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2680 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2681 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002682 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002683}
2684
Evan Cheng017dcc62006-04-21 01:05:10 +00002685/// getZeroVector - Returns a vector of specified type with all zero elements.
2686///
2687static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2688 assert(MVT::isVector(VT) && "Expected a vector type");
2689 unsigned NumElems = getVectorNumElements(VT);
2690 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2691 bool isFP = MVT::isFloatingPoint(EVT);
2692 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2693 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002694 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
Evan Cheng017dcc62006-04-21 01:05:10 +00002695}
2696
Evan Chengc575ca22006-04-17 20:43:08 +00002697/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2698///
2699static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2700 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002701 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002702 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002703 unsigned NumElems = Mask.getNumOperands();
2704 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002705 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002706 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002707 NumElems >>= 1;
2708 }
2709 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2710
2711 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002712 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002713 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002714 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002715 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2716}
2717
Evan Cheng017dcc62006-04-21 01:05:10 +00002718/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2719/// constant +0.0.
2720static inline bool isZeroNode(SDOperand Elt) {
2721 return ((isa<ConstantSDNode>(Elt) &&
2722 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2723 (isa<ConstantFPSDNode>(Elt) &&
2724 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2725}
2726
Evan Chengba05f722006-04-21 23:03:30 +00002727/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2728/// vector and zero or undef vector.
2729static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002730 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002731 bool isZero, SelectionDAG &DAG) {
2732 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002733 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2734 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2735 SDOperand Zero = DAG.getConstant(0, EVT);
2736 std::vector<SDOperand> MaskVec(NumElems, Zero);
2737 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002738 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2739 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002740 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002741}
2742
Evan Chengc78d3b42006-04-24 18:01:45 +00002743/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2744///
2745static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2746 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002747 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002748 if (NumNonZero > 8)
2749 return SDOperand();
2750
2751 SDOperand V(0, 0);
2752 bool First = true;
2753 for (unsigned i = 0; i < 16; ++i) {
2754 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2755 if (ThisIsNonZero && First) {
2756 if (NumZero)
2757 V = getZeroVector(MVT::v8i16, DAG);
2758 else
2759 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2760 First = false;
2761 }
2762
2763 if ((i & 1) != 0) {
2764 SDOperand ThisElt(0, 0), LastElt(0, 0);
2765 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2766 if (LastIsNonZero) {
2767 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2768 }
2769 if (ThisIsNonZero) {
2770 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2771 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2772 ThisElt, DAG.getConstant(8, MVT::i8));
2773 if (LastIsNonZero)
2774 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2775 } else
2776 ThisElt = LastElt;
2777
2778 if (ThisElt.Val)
2779 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00002780 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002781 }
2782 }
2783
2784 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2785}
2786
2787/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2788///
2789static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2790 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002791 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002792 if (NumNonZero > 4)
2793 return SDOperand();
2794
2795 SDOperand V(0, 0);
2796 bool First = true;
2797 for (unsigned i = 0; i < 8; ++i) {
2798 bool isNonZero = (NonZeros & (1 << i)) != 0;
2799 if (isNonZero) {
2800 if (First) {
2801 if (NumZero)
2802 V = getZeroVector(MVT::v8i16, DAG);
2803 else
2804 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2805 First = false;
2806 }
2807 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00002808 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002809 }
2810 }
2811
2812 return V;
2813}
2814
Evan Cheng0db9fe62006-04-25 20:13:52 +00002815SDOperand
2816X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2817 // All zero's are handled with pxor.
2818 if (ISD::isBuildVectorAllZeros(Op.Val))
2819 return Op;
2820
2821 // All one's are handled with pcmpeqd.
2822 if (ISD::isBuildVectorAllOnes(Op.Val))
2823 return Op;
2824
2825 MVT::ValueType VT = Op.getValueType();
2826 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2827 unsigned EVTBits = MVT::getSizeInBits(EVT);
2828
2829 unsigned NumElems = Op.getNumOperands();
2830 unsigned NumZero = 0;
2831 unsigned NumNonZero = 0;
2832 unsigned NonZeros = 0;
2833 std::set<SDOperand> Values;
2834 for (unsigned i = 0; i < NumElems; ++i) {
2835 SDOperand Elt = Op.getOperand(i);
2836 if (Elt.getOpcode() != ISD::UNDEF) {
2837 Values.insert(Elt);
2838 if (isZeroNode(Elt))
2839 NumZero++;
2840 else {
2841 NonZeros |= (1 << i);
2842 NumNonZero++;
2843 }
2844 }
2845 }
2846
2847 if (NumNonZero == 0)
2848 // Must be a mix of zero and undef. Return a zero vector.
2849 return getZeroVector(VT, DAG);
2850
2851 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2852 if (Values.size() == 1)
2853 return SDOperand();
2854
2855 // Special case for single non-zero element.
2856 if (NumNonZero == 1) {
2857 unsigned Idx = CountTrailingZeros_32(NonZeros);
2858 SDOperand Item = Op.getOperand(Idx);
2859 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2860 if (Idx == 0)
2861 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2862 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2863 NumZero > 0, DAG);
2864
2865 if (EVTBits == 32) {
2866 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2867 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2868 DAG);
2869 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2870 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2871 std::vector<SDOperand> MaskVec;
2872 for (unsigned i = 0; i < NumElems; i++)
2873 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002874 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2875 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002876 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2877 DAG.getNode(ISD::UNDEF, VT), Mask);
2878 }
2879 }
2880
2881 // Let legalizer expand 2-widde build_vector's.
2882 if (EVTBits == 64)
2883 return SDOperand();
2884
2885 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2886 if (EVTBits == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002887 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2888 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002889 if (V.Val) return V;
2890 }
2891
2892 if (EVTBits == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002893 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2894 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002895 if (V.Val) return V;
2896 }
2897
2898 // If element VT is == 32 bits, turn it into a number of shuffles.
2899 std::vector<SDOperand> V(NumElems);
2900 if (NumElems == 4 && NumZero > 0) {
2901 for (unsigned i = 0; i < 4; ++i) {
2902 bool isZero = !(NonZeros & (1 << i));
2903 if (isZero)
2904 V[i] = getZeroVector(VT, DAG);
2905 else
2906 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2907 }
2908
2909 for (unsigned i = 0; i < 2; ++i) {
2910 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2911 default: break;
2912 case 0:
2913 V[i] = V[i*2]; // Must be a zero vector.
2914 break;
2915 case 1:
2916 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2917 getMOVLMask(NumElems, DAG));
2918 break;
2919 case 2:
2920 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2921 getMOVLMask(NumElems, DAG));
2922 break;
2923 case 3:
2924 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2925 getUnpacklMask(NumElems, DAG));
2926 break;
2927 }
2928 }
2929
Evan Cheng069287d2006-05-16 07:21:53 +00002930 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002931 // clears the upper bits.
2932 // FIXME: we can do the same for v4f32 case when we know both parts of
2933 // the lower half come from scalar_to_vector (loadf32). We should do
2934 // that in post legalizer dag combiner with target specific hooks.
2935 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2936 return V[0];
2937 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2938 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2939 std::vector<SDOperand> MaskVec;
2940 bool Reverse = (NonZeros & 0x3) == 2;
2941 for (unsigned i = 0; i < 2; ++i)
2942 if (Reverse)
2943 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2944 else
2945 MaskVec.push_back(DAG.getConstant(i, EVT));
2946 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2947 for (unsigned i = 0; i < 2; ++i)
2948 if (Reverse)
2949 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2950 else
2951 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002952 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2953 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002954 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2955 }
2956
2957 if (Values.size() > 2) {
2958 // Expand into a number of unpckl*.
2959 // e.g. for v4f32
2960 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2961 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2962 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2963 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2964 for (unsigned i = 0; i < NumElems; ++i)
2965 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2966 NumElems >>= 1;
2967 while (NumElems != 0) {
2968 for (unsigned i = 0; i < NumElems; ++i)
2969 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2970 UnpckMask);
2971 NumElems >>= 1;
2972 }
2973 return V[0];
2974 }
2975
2976 return SDOperand();
2977}
2978
2979SDOperand
2980X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2981 SDOperand V1 = Op.getOperand(0);
2982 SDOperand V2 = Op.getOperand(1);
2983 SDOperand PermMask = Op.getOperand(2);
2984 MVT::ValueType VT = Op.getValueType();
2985 unsigned NumElems = PermMask.getNumOperands();
2986 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2987 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2988
Evan Cheng8cf723d2006-09-08 01:50:06 +00002989 if (isUndefShuffle(Op.Val))
2990 return DAG.getNode(ISD::UNDEF, VT);
2991
Evan Cheng0db9fe62006-04-25 20:13:52 +00002992 if (isSplatMask(PermMask.Val)) {
2993 if (NumElems <= 4) return Op;
2994 // Promote it to a v4i32 splat.
2995 return PromoteSplat(Op, DAG);
2996 }
2997
2998 if (X86::isMOVLMask(PermMask.Val))
2999 return (V1IsUndef) ? V2 : Op;
3000
3001 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3002 X86::isMOVSLDUPMask(PermMask.Val) ||
3003 X86::isMOVHLPSMask(PermMask.Val) ||
3004 X86::isMOVHPMask(PermMask.Val) ||
3005 X86::isMOVLPMask(PermMask.Val))
3006 return Op;
3007
3008 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3009 ShouldXformToMOVLP(V1.Val, PermMask.Val))
3010 return CommuteVectorShuffle(Op, DAG);
3011
Evan Cheng8cf723d2006-09-08 01:50:06 +00003012 bool V1IsSplat = isSplatVector(V1.Val);
3013 bool V2IsSplat = isSplatVector(V2.Val);
3014 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003015 Op = CommuteVectorShuffle(Op, DAG);
3016 V1 = Op.getOperand(0);
3017 V2 = Op.getOperand(1);
3018 PermMask = Op.getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00003019 std::swap(V1IsSplat, V2IsSplat);
3020 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003021 }
3022
Evan Cheng8cf723d2006-09-08 01:50:06 +00003023 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003024 if (V2IsUndef) return V1;
3025 Op = CommuteVectorShuffle(Op, DAG);
3026 V1 = Op.getOperand(0);
3027 V2 = Op.getOperand(1);
3028 PermMask = Op.getOperand(2);
3029 if (V2IsSplat) {
3030 // V2 is a splat, so the mask may be malformed. That is, it may point
3031 // to any V2 element. The instruction selectior won't like this. Get
3032 // a corrected mask and commute to form a proper MOVS{S|D}.
3033 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3034 if (NewMask.Val != PermMask.Val)
3035 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3036 }
3037 return Op;
3038 }
3039
3040 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3041 X86::isUNPCKLMask(PermMask.Val) ||
3042 X86::isUNPCKHMask(PermMask.Val))
3043 return Op;
3044
3045 if (V2IsSplat) {
3046 // Normalize mask so all entries that point to V2 points to its first
3047 // element then try to match unpck{h|l} again. If match, return a
3048 // new vector_shuffle with the corrected mask.
3049 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3050 if (NewMask.Val != PermMask.Val) {
3051 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3052 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3053 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3054 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3055 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3056 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3057 }
3058 }
3059 }
3060
3061 // Normalize the node to match x86 shuffle ops if needed
3062 if (V2.getOpcode() != ISD::UNDEF)
3063 if (isCommutedSHUFP(PermMask.Val)) {
3064 Op = CommuteVectorShuffle(Op, DAG);
3065 V1 = Op.getOperand(0);
3066 V2 = Op.getOperand(1);
3067 PermMask = Op.getOperand(2);
3068 }
3069
3070 // If VT is integer, try PSHUF* first, then SHUFP*.
3071 if (MVT::isInteger(VT)) {
3072 if (X86::isPSHUFDMask(PermMask.Val) ||
3073 X86::isPSHUFHWMask(PermMask.Val) ||
3074 X86::isPSHUFLWMask(PermMask.Val)) {
3075 if (V2.getOpcode() != ISD::UNDEF)
3076 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3077 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3078 return Op;
3079 }
3080
3081 if (X86::isSHUFPMask(PermMask.Val))
3082 return Op;
3083
3084 // Handle v8i16 shuffle high / low shuffle node pair.
3085 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3086 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3087 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3088 std::vector<SDOperand> MaskVec;
3089 for (unsigned i = 0; i != 4; ++i)
3090 MaskVec.push_back(PermMask.getOperand(i));
3091 for (unsigned i = 4; i != 8; ++i)
3092 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003093 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3094 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003095 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3096 MaskVec.clear();
3097 for (unsigned i = 0; i != 4; ++i)
3098 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3099 for (unsigned i = 4; i != 8; ++i)
3100 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00003101 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003102 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3103 }
3104 } else {
3105 // Floating point cases in the other order.
3106 if (X86::isSHUFPMask(PermMask.Val))
3107 return Op;
3108 if (X86::isPSHUFDMask(PermMask.Val) ||
3109 X86::isPSHUFHWMask(PermMask.Val) ||
3110 X86::isPSHUFLWMask(PermMask.Val)) {
3111 if (V2.getOpcode() != ISD::UNDEF)
3112 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3113 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3114 return Op;
3115 }
3116 }
3117
3118 if (NumElems == 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003119 MVT::ValueType MaskVT = PermMask.getValueType();
3120 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng43f3bd32006-04-28 07:03:38 +00003121 std::vector<std::pair<int, int> > Locs;
3122 Locs.reserve(NumElems);
3123 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3124 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3125 unsigned NumHi = 0;
3126 unsigned NumLo = 0;
3127 // If no more than two elements come from either vector. This can be
3128 // implemented with two shuffles. First shuffle gather the elements.
3129 // The second shuffle, which takes the first shuffle as both of its
3130 // vector operands, put the elements into the right order.
3131 for (unsigned i = 0; i != NumElems; ++i) {
3132 SDOperand Elt = PermMask.getOperand(i);
3133 if (Elt.getOpcode() == ISD::UNDEF) {
3134 Locs[i] = std::make_pair(-1, -1);
3135 } else {
3136 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3137 if (Val < NumElems) {
3138 Locs[i] = std::make_pair(0, NumLo);
3139 Mask1[NumLo] = Elt;
3140 NumLo++;
3141 } else {
3142 Locs[i] = std::make_pair(1, NumHi);
3143 if (2+NumHi < NumElems)
3144 Mask1[2+NumHi] = Elt;
3145 NumHi++;
3146 }
3147 }
3148 }
3149 if (NumLo <= 2 && NumHi <= 2) {
3150 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003151 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3152 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003153 for (unsigned i = 0; i != NumElems; ++i) {
3154 if (Locs[i].first == -1)
3155 continue;
3156 else {
3157 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3158 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3159 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3160 }
3161 }
3162
3163 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003164 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3165 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003166 }
3167
3168 // Break it into (shuffle shuffle_hi, shuffle_lo).
3169 Locs.clear();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003170 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3171 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3172 std::vector<SDOperand> *MaskPtr = &LoMask;
3173 unsigned MaskIdx = 0;
3174 unsigned LoIdx = 0;
3175 unsigned HiIdx = NumElems/2;
3176 for (unsigned i = 0; i != NumElems; ++i) {
3177 if (i == NumElems/2) {
3178 MaskPtr = &HiMask;
3179 MaskIdx = 1;
3180 LoIdx = 0;
3181 HiIdx = NumElems/2;
3182 }
3183 SDOperand Elt = PermMask.getOperand(i);
3184 if (Elt.getOpcode() == ISD::UNDEF) {
3185 Locs[i] = std::make_pair(-1, -1);
3186 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3187 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3188 (*MaskPtr)[LoIdx] = Elt;
3189 LoIdx++;
3190 } else {
3191 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3192 (*MaskPtr)[HiIdx] = Elt;
3193 HiIdx++;
3194 }
3195 }
3196
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003197 SDOperand LoShuffle =
3198 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003199 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3200 &LoMask[0], LoMask.size()));
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003201 SDOperand HiShuffle =
3202 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003203 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3204 &HiMask[0], HiMask.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003205 std::vector<SDOperand> MaskOps;
3206 for (unsigned i = 0; i != NumElems; ++i) {
3207 if (Locs[i].first == -1) {
3208 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3209 } else {
3210 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3211 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3212 }
3213 }
3214 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003215 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3216 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003217 }
3218
3219 return SDOperand();
3220}
3221
3222SDOperand
3223X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3224 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3225 return SDOperand();
3226
3227 MVT::ValueType VT = Op.getValueType();
3228 // TODO: handle v16i8.
3229 if (MVT::getSizeInBits(VT) == 16) {
3230 // Transform it so it match pextrw which produces a 32-bit result.
3231 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3232 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3233 Op.getOperand(0), Op.getOperand(1));
3234 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3235 DAG.getValueType(VT));
3236 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3237 } else if (MVT::getSizeInBits(VT) == 32) {
3238 SDOperand Vec = Op.getOperand(0);
3239 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3240 if (Idx == 0)
3241 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003242 // SHUFPS the element to the lowest double word, then movss.
3243 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003244 std::vector<SDOperand> IdxVec;
3245 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
3246 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3247 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3248 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003249 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3250 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003251 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3252 Vec, Vec, Mask);
3253 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003254 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003255 } else if (MVT::getSizeInBits(VT) == 64) {
3256 SDOperand Vec = Op.getOperand(0);
3257 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3258 if (Idx == 0)
3259 return Op;
3260
3261 // UNPCKHPD the element to the lowest double word, then movsd.
3262 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3263 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3264 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3265 std::vector<SDOperand> IdxVec;
3266 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
3267 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003268 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3269 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003270 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3271 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3272 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003273 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003274 }
3275
3276 return SDOperand();
3277}
3278
3279SDOperand
3280X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00003281 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00003282 // as its second argument.
3283 MVT::ValueType VT = Op.getValueType();
3284 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3285 SDOperand N0 = Op.getOperand(0);
3286 SDOperand N1 = Op.getOperand(1);
3287 SDOperand N2 = Op.getOperand(2);
3288 if (MVT::getSizeInBits(BaseVT) == 16) {
3289 if (N1.getValueType() != MVT::i32)
3290 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3291 if (N2.getValueType() != MVT::i32)
3292 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3293 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3294 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3295 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3296 if (Idx == 0) {
3297 // Use a movss.
3298 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3299 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3300 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3301 std::vector<SDOperand> MaskVec;
3302 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3303 for (unsigned i = 1; i <= 3; ++i)
3304 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3305 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003306 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3307 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003308 } else {
3309 // Use two pinsrw instructions to insert a 32 bit value.
3310 Idx <<= 1;
3311 if (MVT::isFloatingPoint(N1.getValueType())) {
3312 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng069287d2006-05-16 07:21:53 +00003313 // Just load directly from f32mem to GR32.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003314 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
3315 N1.getOperand(2));
3316 } else {
3317 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3318 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3319 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003320 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003321 }
3322 }
3323 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3324 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003325 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003326 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3327 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003328 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003329 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3330 }
3331 }
3332
3333 return SDOperand();
3334}
3335
3336SDOperand
3337X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3338 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3339 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3340}
3341
3342// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3343// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3344// one of the above mentioned nodes. It has to be wrapped because otherwise
3345// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3346// be used to form addressing mode. These wrapped nodes will be selected
3347// into MOV32ri.
3348SDOperand
3349X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3350 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3351 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Evan Chengc356a572006-09-12 21:04:05 +00003352 DAG.getTargetConstantPool(CP->getConstVal(),
3353 getPointerTy(),
3354 CP->getAlignment()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003355 if (Subtarget->isTargetDarwin()) {
3356 // With PIC, the address is actually $g + Offset.
Evan Cheng25ab6902006-09-08 06:48:29 +00003357 if (!Subtarget->is64Bit() &&
3358 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003359 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3360 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
3361 }
3362
3363 return Result;
3364}
3365
3366SDOperand
3367X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3368 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3369 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003370 DAG.getTargetGlobalAddress(GV,
3371 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003372 if (Subtarget->isTargetDarwin()) {
3373 // With PIC, the address is actually $g + Offset.
Evan Cheng25ab6902006-09-08 06:48:29 +00003374 if (!Subtarget->is64Bit() &&
3375 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003376 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003377 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3378 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003379
3380 // For Darwin, external and weak symbols are indirect, so we want to load
3381 // the value at address GV, not the value of GV itself. This means that
3382 // the GlobalAddress must be in the base or index register of the address,
3383 // not the GV offset field.
3384 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
3385 DarwinGVRequiresExtraLoad(GV))
Evan Cheng25ab6902006-09-08 06:48:29 +00003386 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003387 Result, DAG.getSrcValue(NULL));
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00003388 } else if (Subtarget->isTargetCygwin() || Subtarget->isTargetWindows()) {
3389 // FIXME: What's about PIC?
3390 if (WindowsGVRequiresExtraLoad(GV)) {
3391 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(),
3392 Result, DAG.getSrcValue(NULL));
3393 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003394 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00003395
Evan Cheng0db9fe62006-04-25 20:13:52 +00003396
3397 return Result;
3398}
3399
3400SDOperand
3401X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3402 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3403 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003404 DAG.getTargetExternalSymbol(Sym,
3405 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003406 if (Subtarget->isTargetDarwin()) {
3407 // With PIC, the address is actually $g + Offset.
Evan Cheng25ab6902006-09-08 06:48:29 +00003408 if (!Subtarget->is64Bit() &&
3409 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003410 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003411 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3412 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003413 }
3414
3415 return Result;
3416}
3417
3418SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00003419 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3420 "Not an i64 shift!");
3421 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3422 SDOperand ShOpLo = Op.getOperand(0);
3423 SDOperand ShOpHi = Op.getOperand(1);
3424 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng734503b2006-09-11 02:19:56 +00003425 SDOperand Tmp1 = isSRA ?
3426 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3427 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003428
3429 SDOperand Tmp2, Tmp3;
3430 if (Op.getOpcode() == ISD::SHL_PARTS) {
3431 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3432 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3433 } else {
3434 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00003435 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00003436 }
3437
Evan Cheng734503b2006-09-11 02:19:56 +00003438 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3439 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3440 DAG.getConstant(32, MVT::i8));
3441 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3442 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Chenge3413162006-01-09 18:33:28 +00003443
3444 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00003445 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00003446
Evan Cheng734503b2006-09-11 02:19:56 +00003447 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3448 SmallVector<SDOperand, 4> Ops;
Evan Chenge3413162006-01-09 18:33:28 +00003449 if (Op.getOpcode() == ISD::SHL_PARTS) {
3450 Ops.push_back(Tmp2);
3451 Ops.push_back(Tmp3);
3452 Ops.push_back(CC);
3453 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003454 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003455 InFlag = Hi.getValue(1);
3456
3457 Ops.clear();
3458 Ops.push_back(Tmp3);
3459 Ops.push_back(Tmp1);
3460 Ops.push_back(CC);
3461 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003462 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003463 } else {
3464 Ops.push_back(Tmp2);
3465 Ops.push_back(Tmp3);
3466 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00003467 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003468 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003469 InFlag = Lo.getValue(1);
3470
3471 Ops.clear();
3472 Ops.push_back(Tmp3);
3473 Ops.push_back(Tmp1);
3474 Ops.push_back(CC);
3475 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003476 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003477 }
3478
Evan Cheng734503b2006-09-11 02:19:56 +00003479 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003480 Ops.clear();
3481 Ops.push_back(Lo);
3482 Ops.push_back(Hi);
Evan Cheng734503b2006-09-11 02:19:56 +00003483 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003484}
Evan Chenga3195e82006-01-12 22:54:21 +00003485
Evan Cheng0db9fe62006-04-25 20:13:52 +00003486SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3487 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3488 Op.getOperand(0).getValueType() >= MVT::i16 &&
3489 "Unknown SINT_TO_FP to lower!");
3490
3491 SDOperand Result;
3492 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3493 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3494 MachineFunction &MF = DAG.getMachineFunction();
3495 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3496 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3497 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
3498 DAG.getEntryNode(), Op.getOperand(0),
3499 StackSlot, DAG.getSrcValue(NULL));
3500
3501 // Build the FILD
3502 std::vector<MVT::ValueType> Tys;
3503 Tys.push_back(MVT::f64);
3504 Tys.push_back(MVT::Other);
3505 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3506 std::vector<SDOperand> Ops;
3507 Ops.push_back(Chain);
3508 Ops.push_back(StackSlot);
3509 Ops.push_back(DAG.getValueType(SrcVT));
3510 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003511 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003512
3513 if (X86ScalarSSE) {
3514 Chain = Result.getValue(1);
3515 SDOperand InFlag = Result.getValue(2);
3516
3517 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3518 // shouldn't be necessary except that RFP cannot be live across
3519 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003520 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003521 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003522 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00003523 std::vector<MVT::ValueType> Tys;
Evan Cheng6dab0532006-01-30 08:02:57 +00003524 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003525 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003526 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003527 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003528 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003529 Ops.push_back(DAG.getValueType(Op.getValueType()));
3530 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003531 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003532 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3533 DAG.getSrcValue(NULL));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003534 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003535
Evan Cheng0db9fe62006-04-25 20:13:52 +00003536 return Result;
3537}
3538
3539SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3540 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3541 "Unknown FP_TO_SINT to lower!");
3542 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3543 // stack slot.
3544 MachineFunction &MF = DAG.getMachineFunction();
3545 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3546 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3547 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3548
3549 unsigned Opc;
3550 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003551 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3552 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3553 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3554 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003555 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003556
Evan Cheng0db9fe62006-04-25 20:13:52 +00003557 SDOperand Chain = DAG.getEntryNode();
3558 SDOperand Value = Op.getOperand(0);
3559 if (X86ScalarSSE) {
3560 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3561 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3562 DAG.getSrcValue(0));
3563 std::vector<MVT::ValueType> Tys;
3564 Tys.push_back(MVT::f64);
3565 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003566 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00003567 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003568 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003569 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003570 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003571 Chain = Value.getValue(1);
3572 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3573 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3574 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003575
Evan Cheng0db9fe62006-04-25 20:13:52 +00003576 // Build the FP_TO_INT*_IN_MEM
3577 std::vector<SDOperand> Ops;
3578 Ops.push_back(Chain);
3579 Ops.push_back(Value);
3580 Ops.push_back(StackSlot);
Evan Cheng311ace02006-08-11 07:35:45 +00003581 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
Evan Chengd9558e02006-01-06 00:43:03 +00003582
Evan Cheng0db9fe62006-04-25 20:13:52 +00003583 // Load the result.
3584 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3585 DAG.getSrcValue(NULL));
3586}
3587
3588SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3589 MVT::ValueType VT = Op.getValueType();
3590 const Type *OpNTy = MVT::getTypeForValueType(VT);
3591 std::vector<Constant*> CV;
3592 if (VT == MVT::f64) {
3593 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3594 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3595 } else {
3596 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3597 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3598 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3599 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3600 }
3601 Constant *CS = ConstantStruct::get(CV);
3602 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Evan Cheng64a752f2006-08-11 09:08:15 +00003603 std::vector<MVT::ValueType> Tys;
3604 Tys.push_back(VT);
3605 Tys.push_back(MVT::Other);
3606 SmallVector<SDOperand, 3> Ops;
3607 Ops.push_back(DAG.getEntryNode());
3608 Ops.push_back(CPIdx);
3609 Ops.push_back(DAG.getSrcValue(NULL));
3610 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003611 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3612}
3613
3614SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3615 MVT::ValueType VT = Op.getValueType();
3616 const Type *OpNTy = MVT::getTypeForValueType(VT);
3617 std::vector<Constant*> CV;
3618 if (VT == MVT::f64) {
3619 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3620 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3621 } else {
3622 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3623 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3624 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3625 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3626 }
3627 Constant *CS = ConstantStruct::get(CV);
3628 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Evan Cheng64a752f2006-08-11 09:08:15 +00003629 std::vector<MVT::ValueType> Tys;
3630 Tys.push_back(VT);
3631 Tys.push_back(MVT::Other);
3632 SmallVector<SDOperand, 3> Ops;
3633 Ops.push_back(DAG.getEntryNode());
3634 Ops.push_back(CPIdx);
3635 Ops.push_back(DAG.getSrcValue(NULL));
3636 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003637 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3638}
3639
Evan Cheng734503b2006-09-11 02:19:56 +00003640SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3641 SDOperand Chain) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003642 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3643 SDOperand Cond;
Evan Cheng734503b2006-09-11 02:19:56 +00003644 SDOperand Op0 = Op.getOperand(0);
3645 SDOperand Op1 = Op.getOperand(1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003646 SDOperand CC = Op.getOperand(2);
3647 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng734503b2006-09-11 02:19:56 +00003648 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003649 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003650 unsigned X86CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003651
Evan Cheng734503b2006-09-11 02:19:56 +00003652 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Chris Lattnerf9570512006-09-13 03:22:10 +00003653 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3654 Op0, Op1, DAG)) {
Evan Cheng734503b2006-09-11 02:19:56 +00003655 SDOperand Ops1[] = { Chain, Op0, Op1 };
3656 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops1, 3).getValue(1);
3657 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3658 return DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
3659 }
3660
3661 assert(isFP && "Illegal integer SetCC!");
3662
3663 SDOperand COps[] = { Chain, Op0, Op1 };
3664 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3665
3666 switch (SetCCOpcode) {
3667 default: assert(false && "Illegal floating point SetCC!");
3668 case ISD::SETOEQ: { // !PF & ZF
3669 SDOperand Ops1[] = { DAG.getConstant(X86ISD::COND_NP, MVT::i8), Cond };
3670 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops1, 2);
3671 SDOperand Ops2[] = { DAG.getConstant(X86ISD::COND_E, MVT::i8),
3672 Tmp1.getValue(1) };
3673 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
3674 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3675 }
3676 case ISD::SETUNE: { // PF | !ZF
3677 SDOperand Ops1[] = { DAG.getConstant(X86ISD::COND_P, MVT::i8), Cond };
3678 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops1, 2);
3679 SDOperand Ops2[] = { DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3680 Tmp1.getValue(1) };
3681 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
3682 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3683 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003684 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003685}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003686
Evan Cheng0db9fe62006-04-25 20:13:52 +00003687SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003688 bool addTest = true;
3689 SDOperand Chain = DAG.getEntryNode();
3690 SDOperand Cond = Op.getOperand(0);
3691 SDOperand CC;
3692 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng9bba8942006-01-26 02:13:10 +00003693
Evan Cheng734503b2006-09-11 02:19:56 +00003694 if (Cond.getOpcode() == ISD::SETCC)
3695 Cond = LowerSETCC(Cond, DAG, Chain);
3696
3697 if (Cond.getOpcode() == X86ISD::SETCC) {
3698 CC = Cond.getOperand(0);
3699
Evan Cheng0db9fe62006-04-25 20:13:52 +00003700 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng734503b2006-09-11 02:19:56 +00003701 // (since flag operand cannot be shared). Use it as the condition setting
3702 // operand in place of the X86ISD::SETCC.
3703 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Cheng0db9fe62006-04-25 20:13:52 +00003704 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng734503b2006-09-11 02:19:56 +00003705 // pressure reason)?
3706 SDOperand Cmp = Cond.getOperand(1);
3707 unsigned Opc = Cmp.getOpcode();
3708 bool IllegalFPCMov = !X86ScalarSSE &&
3709 MVT::isFloatingPoint(Op.getValueType()) &&
3710 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3711 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3712 !IllegalFPCMov) {
3713 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3714 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3715 addTest = false;
3716 }
3717 }
Evan Chengaaca22c2006-01-10 20:26:56 +00003718
Evan Cheng0db9fe62006-04-25 20:13:52 +00003719 if (addTest) {
3720 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003721 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3722 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng7df96d62005-12-17 01:21:05 +00003723 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003724
Evan Cheng734503b2006-09-11 02:19:56 +00003725 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3726 SmallVector<SDOperand, 4> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003727 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3728 // condition is true.
3729 Ops.push_back(Op.getOperand(2));
3730 Ops.push_back(Op.getOperand(1));
3731 Ops.push_back(CC);
Evan Cheng734503b2006-09-11 02:19:56 +00003732 Ops.push_back(Cond.getValue(1));
3733 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003734}
Evan Cheng9bba8942006-01-26 02:13:10 +00003735
Evan Cheng0db9fe62006-04-25 20:13:52 +00003736SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003737 bool addTest = true;
3738 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003739 SDOperand Cond = Op.getOperand(1);
3740 SDOperand Dest = Op.getOperand(2);
3741 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00003742 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3743
Evan Cheng0db9fe62006-04-25 20:13:52 +00003744 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng734503b2006-09-11 02:19:56 +00003745 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003746
3747 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00003748 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003749
Evan Cheng734503b2006-09-11 02:19:56 +00003750 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3751 // (since flag operand cannot be shared). Use it as the condition setting
3752 // operand in place of the X86ISD::SETCC.
3753 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3754 // to use a test instead of duplicating the X86ISD::CMP (for register
3755 // pressure reason)?
3756 SDOperand Cmp = Cond.getOperand(1);
3757 unsigned Opc = Cmp.getOpcode();
3758 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3759 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3760 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3761 addTest = false;
3762 }
3763 }
Evan Cheng1bcee362006-01-13 01:03:02 +00003764
Evan Cheng0db9fe62006-04-25 20:13:52 +00003765 if (addTest) {
3766 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003767 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3768 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng898101c2005-12-19 23:12:38 +00003769 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng734503b2006-09-11 02:19:56 +00003771 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003772}
Evan Cheng67f92a72006-01-11 22:15:48 +00003773
Evan Cheng0db9fe62006-04-25 20:13:52 +00003774SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3775 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3776 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3777 DAG.getTargetJumpTable(JT->getIndex(),
3778 getPointerTy()));
3779 if (Subtarget->isTargetDarwin()) {
3780 // With PIC, the address is actually $g + Offset.
Evan Cheng25ab6902006-09-08 06:48:29 +00003781 if (!Subtarget->is64Bit() &&
3782 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003783 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003784 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3785 Result);
Evan Cheng67f92a72006-01-11 22:15:48 +00003786 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00003787
Evan Cheng0db9fe62006-04-25 20:13:52 +00003788 return Result;
3789}
Evan Cheng7ccced62006-02-18 00:15:05 +00003790
Evan Cheng32fe1032006-05-25 00:59:30 +00003791SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3792 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00003793 if (Subtarget->is64Bit())
3794 return LowerX86_64CCCCallTo(Op, DAG);
3795 else if (CallingConv == CallingConv::Fast && EnableFastCC)
Evan Cheng32fe1032006-05-25 00:59:30 +00003796 return LowerFastCCCallTo(Op, DAG);
3797 else
3798 return LowerCCCCallTo(Op, DAG);
3799}
3800
Evan Cheng0db9fe62006-04-25 20:13:52 +00003801SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3802 SDOperand Copy;
Nate Begemanee625572006-01-27 21:09:22 +00003803
Evan Cheng0db9fe62006-04-25 20:13:52 +00003804 switch(Op.getNumOperands()) {
Nate Begemanee625572006-01-27 21:09:22 +00003805 default:
3806 assert(0 && "Do not know how to return this many arguments!");
3807 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003808 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00003809 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003810 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Evan Cheng6848be12006-05-26 23:10:12 +00003811 case 3: {
Nate Begemanee625572006-01-27 21:09:22 +00003812 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003813
Evan Cheng25ab6902006-09-08 06:48:29 +00003814 if (MVT::isVector(ArgVT) ||
3815 (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003816 // Integer or FP vector result -> XMM0.
3817 if (DAG.getMachineFunction().liveout_empty())
3818 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3819 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3820 SDOperand());
3821 } else if (MVT::isInteger(ArgVT)) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003822 // Integer result -> EAX / RAX.
3823 // The C calling convention guarantees the return value has been
3824 // promoted to at least MVT::i32. The X86-64 ABI doesn't require the
3825 // value to be promoted MVT::i64. So we don't have to extend it to
3826 // 64-bit. Return the value in EAX, but mark RAX as liveout.
3827 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Chris Lattnerb2be4032006-04-17 20:32:50 +00003828 if (DAG.getMachineFunction().liveout_empty())
Evan Cheng25ab6902006-09-08 06:48:29 +00003829 DAG.getMachineFunction().addLiveOut(Reg);
Chris Lattnerb2be4032006-04-17 20:32:50 +00003830
Evan Cheng25ab6902006-09-08 06:48:29 +00003831 Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX;
3832 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1),
Nate Begemanee625572006-01-27 21:09:22 +00003833 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00003834 } else if (!X86ScalarSSE) {
3835 // FP return with fp-stack value.
3836 if (DAG.getMachineFunction().liveout_empty())
3837 DAG.getMachineFunction().addLiveOut(X86::ST0);
3838
Nate Begemanee625572006-01-27 21:09:22 +00003839 std::vector<MVT::ValueType> Tys;
3840 Tys.push_back(MVT::Other);
3841 Tys.push_back(MVT::Flag);
3842 std::vector<SDOperand> Ops;
3843 Ops.push_back(Op.getOperand(0));
3844 Ops.push_back(Op.getOperand(1));
Evan Cheng311ace02006-08-11 07:35:45 +00003845 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
Nate Begemanee625572006-01-27 21:09:22 +00003846 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003847 // FP return with ScalarSSE (return on fp-stack).
3848 if (DAG.getMachineFunction().liveout_empty())
3849 DAG.getMachineFunction().addLiveOut(X86::ST0);
3850
Evan Cheng0d084c92006-02-01 00:20:21 +00003851 SDOperand MemLoc;
3852 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003853 SDOperand Value = Op.getOperand(1);
3854
Evan Cheng760df292006-02-01 01:19:32 +00003855 if (Value.getOpcode() == ISD::LOAD &&
3856 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00003857 Chain = Value.getOperand(0);
3858 MemLoc = Value.getOperand(1);
3859 } else {
3860 // Spill the value to memory and reload it into top of stack.
3861 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3862 MachineFunction &MF = DAG.getMachineFunction();
3863 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3864 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3865 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3866 Value, MemLoc, DAG.getSrcValue(0));
3867 }
Nate Begemanee625572006-01-27 21:09:22 +00003868 std::vector<MVT::ValueType> Tys;
3869 Tys.push_back(MVT::f64);
3870 Tys.push_back(MVT::Other);
3871 std::vector<SDOperand> Ops;
3872 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003873 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00003874 Ops.push_back(DAG.getValueType(ArgVT));
Evan Cheng311ace02006-08-11 07:35:45 +00003875 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
Nate Begemanee625572006-01-27 21:09:22 +00003876 Tys.clear();
3877 Tys.push_back(MVT::Other);
3878 Tys.push_back(MVT::Flag);
3879 Ops.clear();
3880 Ops.push_back(Copy.getValue(1));
3881 Ops.push_back(Copy);
Evan Cheng311ace02006-08-11 07:35:45 +00003882 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
Nate Begemanee625572006-01-27 21:09:22 +00003883 }
3884 break;
3885 }
Evan Cheng25ab6902006-09-08 06:48:29 +00003886 case 5: {
3887 unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
3888 unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX;
Chris Lattnerb2be4032006-04-17 20:32:50 +00003889 if (DAG.getMachineFunction().liveout_empty()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003890 DAG.getMachineFunction().addLiveOut(Reg1);
3891 DAG.getMachineFunction().addLiveOut(Reg2);
Chris Lattnerb2be4032006-04-17 20:32:50 +00003892 }
3893
Evan Cheng25ab6902006-09-08 06:48:29 +00003894 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3),
Nate Begemanee625572006-01-27 21:09:22 +00003895 SDOperand());
Evan Cheng25ab6902006-09-08 06:48:29 +00003896 Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1));
Nate Begemanee625572006-01-27 21:09:22 +00003897 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00003898 }
Nate Begemanee625572006-01-27 21:09:22 +00003899 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003900 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
Evan Cheng25ab6902006-09-08 06:48:29 +00003901 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003902 Copy.getValue(1));
3903}
3904
Evan Cheng1bc78042006-04-26 01:20:17 +00003905SDOperand
3906X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00003907 MachineFunction &MF = DAG.getMachineFunction();
3908 const Function* Fn = MF.getFunction();
3909 if (Fn->hasExternalLinkage() &&
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00003910 Subtarget->isTargetCygwin() &&
Evan Chengb12223e2006-06-09 06:24:42 +00003911 Fn->getName() == "main")
Evan Chenge8bd0a32006-06-06 23:30:24 +00003912 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3913
Evan Cheng25caf632006-05-23 21:06:34 +00003914 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00003915 if (Subtarget->is64Bit())
3916 return LowerX86_64CCCArguments(Op, DAG);
3917 else if (CC == CallingConv::Fast && EnableFastCC)
Evan Cheng25caf632006-05-23 21:06:34 +00003918 return LowerFastCCArguments(Op, DAG);
3919 else
3920 return LowerCCCArguments(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003921}
3922
Evan Cheng0db9fe62006-04-25 20:13:52 +00003923SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3924 SDOperand InFlag(0, 0);
3925 SDOperand Chain = Op.getOperand(0);
3926 unsigned Align =
3927 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3928 if (Align == 0) Align = 1;
3929
3930 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3931 // If not DWORD aligned, call memset if size is less than the threshold.
3932 // It knows how to align to the right boundary first.
3933 if ((Align & 3) != 0 ||
3934 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3935 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003936 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003937 std::vector<std::pair<SDOperand, const Type*> > Args;
3938 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3939 // Extend the ubyte argument to be an int value for the call.
3940 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3941 Args.push_back(std::make_pair(Val, IntPtrTy));
3942 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3943 std::pair<SDOperand,SDOperand> CallResult =
3944 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3945 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3946 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003947 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003948
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 MVT::ValueType AVT;
3950 SDOperand Count;
3951 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3952 unsigned BytesLeft = 0;
3953 bool TwoRepStos = false;
3954 if (ValC) {
3955 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00003956 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003957
Evan Cheng0db9fe62006-04-25 20:13:52 +00003958 // If the value is a constant, then we can potentially use larger sets.
3959 switch (Align & 3) {
3960 case 2: // WORD aligned
3961 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003962 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00003963 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00003965 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00003966 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00003967 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003968 Val = (Val << 8) | Val;
3969 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00003970 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3971 AVT = MVT::i64;
3972 ValReg = X86::RAX;
3973 Val = (Val << 32) | Val;
3974 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 break;
3976 default: // Byte aligned
3977 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003978 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00003979 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003980 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003981 }
3982
Evan Cheng25ab6902006-09-08 06:48:29 +00003983 if (AVT > MVT::i8) {
3984 if (I) {
3985 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3986 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3987 BytesLeft = I->getValue() % UBytes;
3988 } else {
3989 assert(AVT >= MVT::i32 &&
3990 "Do not use rep;stos if not at least DWORD aligned");
3991 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3992 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3993 TwoRepStos = true;
3994 }
3995 }
3996
Evan Cheng0db9fe62006-04-25 20:13:52 +00003997 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3998 InFlag);
3999 InFlag = Chain.getValue(1);
4000 } else {
4001 AVT = MVT::i8;
4002 Count = Op.getOperand(3);
4003 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4004 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004005 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004006
Evan Cheng25ab6902006-09-08 06:48:29 +00004007 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4008 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004009 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004010 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4011 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004012 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004013
Evan Cheng0db9fe62006-04-25 20:13:52 +00004014 std::vector<MVT::ValueType> Tys;
4015 Tys.push_back(MVT::Other);
4016 Tys.push_back(MVT::Flag);
4017 std::vector<SDOperand> Ops;
4018 Ops.push_back(Chain);
4019 Ops.push_back(DAG.getValueType(AVT));
4020 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004021 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004022
Evan Cheng0db9fe62006-04-25 20:13:52 +00004023 if (TwoRepStos) {
4024 InFlag = Chain.getValue(1);
4025 Count = Op.getOperand(3);
4026 MVT::ValueType CVT = Count.getValueType();
4027 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004028 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4029 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4030 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004031 InFlag = Chain.getValue(1);
4032 Tys.clear();
4033 Tys.push_back(MVT::Other);
4034 Tys.push_back(MVT::Flag);
4035 Ops.clear();
4036 Ops.push_back(Chain);
4037 Ops.push_back(DAG.getValueType(MVT::i8));
4038 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004039 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004040 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004041 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004042 SDOperand Value;
4043 unsigned Val = ValC->getValue() & 255;
4044 unsigned Offset = I->getValue() - BytesLeft;
4045 SDOperand DstAddr = Op.getOperand(1);
4046 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004047 if (BytesLeft >= 4) {
4048 Val = (Val << 8) | Val;
4049 Val = (Val << 16) | Val;
4050 Value = DAG.getConstant(Val, MVT::i32);
4051 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
4052 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4053 DAG.getConstant(Offset, AddrVT)),
4054 DAG.getSrcValue(NULL));
4055 BytesLeft -= 4;
4056 Offset += 4;
4057 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004058 if (BytesLeft >= 2) {
4059 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4060 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
4061 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4062 DAG.getConstant(Offset, AddrVT)),
4063 DAG.getSrcValue(NULL));
4064 BytesLeft -= 2;
4065 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004066 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004067 if (BytesLeft == 1) {
4068 Value = DAG.getConstant(Val, MVT::i8);
4069 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
4070 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4071 DAG.getConstant(Offset, AddrVT)),
4072 DAG.getSrcValue(NULL));
Evan Chengba05f722006-04-21 23:03:30 +00004073 }
Evan Cheng386031a2006-03-24 07:29:27 +00004074 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004075
Evan Cheng0db9fe62006-04-25 20:13:52 +00004076 return Chain;
4077}
Evan Cheng11e15b32006-04-03 20:53:28 +00004078
Evan Cheng0db9fe62006-04-25 20:13:52 +00004079SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4080 SDOperand Chain = Op.getOperand(0);
4081 unsigned Align =
4082 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4083 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00004084
Evan Cheng0db9fe62006-04-25 20:13:52 +00004085 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4086 // If not DWORD aligned, call memcpy if size is less than the threshold.
4087 // It knows how to align to the right boundary first.
4088 if ((Align & 3) != 0 ||
4089 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4090 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004091 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092 std::vector<std::pair<SDOperand, const Type*> > Args;
4093 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
4094 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
4095 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
4096 std::pair<SDOperand,SDOperand> CallResult =
4097 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
4098 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4099 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00004100 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004101
4102 MVT::ValueType AVT;
4103 SDOperand Count;
4104 unsigned BytesLeft = 0;
4105 bool TwoRepMovs = false;
4106 switch (Align & 3) {
4107 case 2: // WORD aligned
4108 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004109 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004110 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004111 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004112 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4113 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004114 break;
4115 default: // Byte aligned
4116 AVT = MVT::i8;
4117 Count = Op.getOperand(3);
4118 break;
4119 }
4120
Evan Cheng25ab6902006-09-08 06:48:29 +00004121 if (AVT > MVT::i8) {
4122 if (I) {
4123 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4124 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4125 BytesLeft = I->getValue() % UBytes;
4126 } else {
4127 assert(AVT >= MVT::i32 &&
4128 "Do not use rep;movs if not at least DWORD aligned");
4129 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4130 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4131 TwoRepMovs = true;
4132 }
4133 }
4134
Evan Cheng0db9fe62006-04-25 20:13:52 +00004135 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004136 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4137 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004138 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004139 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4140 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004141 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004142 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4143 Op.getOperand(2), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004144 InFlag = Chain.getValue(1);
4145
4146 std::vector<MVT::ValueType> Tys;
4147 Tys.push_back(MVT::Other);
4148 Tys.push_back(MVT::Flag);
4149 std::vector<SDOperand> Ops;
4150 Ops.push_back(Chain);
4151 Ops.push_back(DAG.getValueType(AVT));
4152 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004153 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004154
4155 if (TwoRepMovs) {
4156 InFlag = Chain.getValue(1);
4157 Count = Op.getOperand(3);
4158 MVT::ValueType CVT = Count.getValueType();
4159 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004160 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4161 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4162 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004163 InFlag = Chain.getValue(1);
4164 Tys.clear();
4165 Tys.push_back(MVT::Other);
4166 Tys.push_back(MVT::Flag);
4167 Ops.clear();
4168 Ops.push_back(Chain);
4169 Ops.push_back(DAG.getValueType(MVT::i8));
4170 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004171 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004172 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004173 // Issue loads and stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004174 unsigned Offset = I->getValue() - BytesLeft;
4175 SDOperand DstAddr = Op.getOperand(1);
4176 MVT::ValueType DstVT = DstAddr.getValueType();
4177 SDOperand SrcAddr = Op.getOperand(2);
4178 MVT::ValueType SrcVT = SrcAddr.getValueType();
4179 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004180 if (BytesLeft >= 4) {
4181 Value = DAG.getLoad(MVT::i32, Chain,
4182 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4183 DAG.getConstant(Offset, SrcVT)),
4184 DAG.getSrcValue(NULL));
4185 Chain = Value.getValue(1);
4186 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
4187 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4188 DAG.getConstant(Offset, DstVT)),
4189 DAG.getSrcValue(NULL));
4190 BytesLeft -= 4;
4191 Offset += 4;
4192 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004193 if (BytesLeft >= 2) {
4194 Value = DAG.getLoad(MVT::i16, Chain,
4195 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4196 DAG.getConstant(Offset, SrcVT)),
4197 DAG.getSrcValue(NULL));
4198 Chain = Value.getValue(1);
4199 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
4200 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4201 DAG.getConstant(Offset, DstVT)),
4202 DAG.getSrcValue(NULL));
4203 BytesLeft -= 2;
4204 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004205 }
4206
Evan Cheng0db9fe62006-04-25 20:13:52 +00004207 if (BytesLeft == 1) {
4208 Value = DAG.getLoad(MVT::i8, Chain,
4209 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4210 DAG.getConstant(Offset, SrcVT)),
4211 DAG.getSrcValue(NULL));
4212 Chain = Value.getValue(1);
4213 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
4214 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4215 DAG.getConstant(Offset, DstVT)),
4216 DAG.getSrcValue(NULL));
4217 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004218 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004219
4220 return Chain;
4221}
4222
4223SDOperand
4224X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4225 std::vector<MVT::ValueType> Tys;
4226 Tys.push_back(MVT::Other);
4227 Tys.push_back(MVT::Flag);
4228 std::vector<SDOperand> Ops;
4229 Ops.push_back(Op.getOperand(0));
Evan Cheng311ace02006-08-11 07:35:45 +00004230 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004231 Ops.clear();
4232 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
4233 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
4234 MVT::i32, Ops[0].getValue(2)));
4235 Ops.push_back(Ops[1].getValue(1));
4236 Tys[0] = Tys[1] = MVT::i32;
4237 Tys.push_back(MVT::Other);
Evan Cheng311ace02006-08-11 07:35:45 +00004238 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004239}
4240
4241SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004242 if (!Subtarget->is64Bit()) {
4243 // vastart just stores the address of the VarArgsFrameIndex slot into the
4244 // memory location argument.
4245 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4246 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
4247 Op.getOperand(1), Op.getOperand(2));
4248 }
4249
4250 // __va_list_tag:
4251 // gp_offset (0 - 6 * 8)
4252 // fp_offset (48 - 48 + 8 * 16)
4253 // overflow_arg_area (point to parameters coming in memory).
4254 // reg_save_area
4255 std::vector<SDOperand> MemOps;
4256 SDOperand FIN = Op.getOperand(1);
4257 // Store gp_offset
4258 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
4259 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4260 FIN, Op.getOperand(2));
4261 MemOps.push_back(Store);
4262
4263 // Store fp_offset
4264 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4265 DAG.getConstant(4, getPointerTy()));
4266 Store = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
4267 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4268 FIN, Op.getOperand(2));
4269 MemOps.push_back(Store);
4270
4271 // Store ptr to overflow_arg_area
4272 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4273 DAG.getConstant(4, getPointerTy()));
4274 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4275 Store = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
4276 OVFIN, FIN, Op.getOperand(2));
4277 MemOps.push_back(Store);
4278
4279 // Store ptr to reg_save_area.
4280 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4281 DAG.getConstant(8, getPointerTy()));
4282 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4283 Store = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
4284 RSFIN, FIN, Op.getOperand(2));
4285 MemOps.push_back(Store);
4286 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004287}
4288
4289SDOperand
4290X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4291 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4292 switch (IntNo) {
4293 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004294 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004295 case Intrinsic::x86_sse_comieq_ss:
4296 case Intrinsic::x86_sse_comilt_ss:
4297 case Intrinsic::x86_sse_comile_ss:
4298 case Intrinsic::x86_sse_comigt_ss:
4299 case Intrinsic::x86_sse_comige_ss:
4300 case Intrinsic::x86_sse_comineq_ss:
4301 case Intrinsic::x86_sse_ucomieq_ss:
4302 case Intrinsic::x86_sse_ucomilt_ss:
4303 case Intrinsic::x86_sse_ucomile_ss:
4304 case Intrinsic::x86_sse_ucomigt_ss:
4305 case Intrinsic::x86_sse_ucomige_ss:
4306 case Intrinsic::x86_sse_ucomineq_ss:
4307 case Intrinsic::x86_sse2_comieq_sd:
4308 case Intrinsic::x86_sse2_comilt_sd:
4309 case Intrinsic::x86_sse2_comile_sd:
4310 case Intrinsic::x86_sse2_comigt_sd:
4311 case Intrinsic::x86_sse2_comige_sd:
4312 case Intrinsic::x86_sse2_comineq_sd:
4313 case Intrinsic::x86_sse2_ucomieq_sd:
4314 case Intrinsic::x86_sse2_ucomilt_sd:
4315 case Intrinsic::x86_sse2_ucomile_sd:
4316 case Intrinsic::x86_sse2_ucomigt_sd:
4317 case Intrinsic::x86_sse2_ucomige_sd:
4318 case Intrinsic::x86_sse2_ucomineq_sd: {
4319 unsigned Opc = 0;
4320 ISD::CondCode CC = ISD::SETCC_INVALID;
4321 switch (IntNo) {
4322 default: break;
4323 case Intrinsic::x86_sse_comieq_ss:
4324 case Intrinsic::x86_sse2_comieq_sd:
4325 Opc = X86ISD::COMI;
4326 CC = ISD::SETEQ;
4327 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004328 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004329 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 Opc = X86ISD::COMI;
4331 CC = ISD::SETLT;
4332 break;
4333 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004334 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 Opc = X86ISD::COMI;
4336 CC = ISD::SETLE;
4337 break;
4338 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004339 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004340 Opc = X86ISD::COMI;
4341 CC = ISD::SETGT;
4342 break;
4343 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004344 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004345 Opc = X86ISD::COMI;
4346 CC = ISD::SETGE;
4347 break;
4348 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004349 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004350 Opc = X86ISD::COMI;
4351 CC = ISD::SETNE;
4352 break;
4353 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004354 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004355 Opc = X86ISD::UCOMI;
4356 CC = ISD::SETEQ;
4357 break;
4358 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004359 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004360 Opc = X86ISD::UCOMI;
4361 CC = ISD::SETLT;
4362 break;
4363 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004364 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004365 Opc = X86ISD::UCOMI;
4366 CC = ISD::SETLE;
4367 break;
4368 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004369 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 Opc = X86ISD::UCOMI;
4371 CC = ISD::SETGT;
4372 break;
4373 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004374 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004375 Opc = X86ISD::UCOMI;
4376 CC = ISD::SETGE;
4377 break;
4378 case Intrinsic::x86_sse_ucomineq_ss:
4379 case Intrinsic::x86_sse2_ucomineq_sd:
4380 Opc = X86ISD::UCOMI;
4381 CC = ISD::SETNE;
4382 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004383 }
Evan Cheng734503b2006-09-11 02:19:56 +00004384
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004386 SDOperand LHS = Op.getOperand(1);
4387 SDOperand RHS = Op.getOperand(2);
4388 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004389
4390 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattnerf9570512006-09-13 03:22:10 +00004391 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng734503b2006-09-11 02:19:56 +00004392 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4393 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4394 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4395 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004396 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004397 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004398 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004399}
Evan Cheng72261582005-12-20 06:22:03 +00004400
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401/// LowerOperation - Provide custom lowering hooks for some operations.
4402///
4403SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4404 switch (Op.getOpcode()) {
4405 default: assert(0 && "Should not custom lower this!");
4406 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4407 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4408 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4409 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4410 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4411 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4412 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4413 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4414 case ISD::SHL_PARTS:
4415 case ISD::SRA_PARTS:
4416 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4417 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4418 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4419 case ISD::FABS: return LowerFABS(Op, DAG);
4420 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004421 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 case ISD::SELECT: return LowerSELECT(Op, DAG);
4423 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4424 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00004425 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00004427 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004428 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4429 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4430 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4431 case ISD::VASTART: return LowerVASTART(Op, DAG);
4432 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4433 }
4434}
4435
Evan Cheng72261582005-12-20 06:22:03 +00004436const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4437 switch (Opcode) {
4438 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00004439 case X86ISD::SHLD: return "X86ISD::SHLD";
4440 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00004441 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00004442 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00004443 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00004444 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00004445 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4446 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4447 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00004448 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00004449 case X86ISD::FST: return "X86ISD::FST";
4450 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00004451 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00004452 case X86ISD::CALL: return "X86ISD::CALL";
4453 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4454 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4455 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00004456 case X86ISD::COMI: return "X86ISD::COMI";
4457 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00004458 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00004459 case X86ISD::CMOV: return "X86ISD::CMOV";
4460 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00004461 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00004462 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4463 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00004464 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng206ee9d2006-07-07 08:33:52 +00004465 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
Evan Cheng7ccced62006-02-18 00:15:05 +00004466 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00004467 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00004468 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00004469 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00004470 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00004471 }
4472}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004473
Evan Cheng60c07e12006-07-05 22:17:51 +00004474/// isLegalAddressImmediate - Return true if the integer value or
4475/// GlobalValue can be used as the offset of the target addressing mode.
4476bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4477 // X86 allows a sign-extended 32-bit immediate field.
4478 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4479}
4480
4481bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
4482 // GV is 64-bit but displacement field is 32-bit unless we are in small code
4483 // model. Mac OS X happens to support only small PIC code model.
4484 // FIXME: better support for other OS's.
4485 if (Subtarget->is64Bit() && !Subtarget->isTargetDarwin())
4486 return false;
4487 if (Subtarget->isTargetDarwin()) {
4488 Reloc::Model RModel = getTargetMachine().getRelocationModel();
4489 if (RModel == Reloc::Static)
4490 return true;
4491 else if (RModel == Reloc::DynamicNoPIC)
4492 return !DarwinGVRequiresExtraLoad(GV);
4493 else
4494 return false;
4495 } else
4496 return true;
4497}
4498
4499/// isShuffleMaskLegal - Targets can use this to indicate that they only
4500/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4501/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4502/// are assumed to be legal.
4503bool
4504X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4505 // Only do shuffles on 128-bit vector types for now.
4506 if (MVT::getSizeInBits(VT) == 64) return false;
4507 return (Mask.Val->getNumOperands() <= 4 ||
4508 isSplatMask(Mask.Val) ||
4509 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4510 X86::isUNPCKLMask(Mask.Val) ||
4511 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4512 X86::isUNPCKHMask(Mask.Val));
4513}
4514
4515bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4516 MVT::ValueType EVT,
4517 SelectionDAG &DAG) const {
4518 unsigned NumElts = BVOps.size();
4519 // Only do shuffles on 128-bit vector types for now.
4520 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4521 if (NumElts == 2) return true;
4522 if (NumElts == 4) {
4523 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
4524 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4525 }
4526 return false;
4527}
4528
4529//===----------------------------------------------------------------------===//
4530// X86 Scheduler Hooks
4531//===----------------------------------------------------------------------===//
4532
4533MachineBasicBlock *
4534X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4535 MachineBasicBlock *BB) {
4536 switch (MI->getOpcode()) {
4537 default: assert(false && "Unexpected instr type to insert");
4538 case X86::CMOV_FR32:
4539 case X86::CMOV_FR64:
4540 case X86::CMOV_V4F32:
4541 case X86::CMOV_V2F64:
4542 case X86::CMOV_V2I64: {
4543 // To "insert" a SELECT_CC instruction, we actually have to insert the
4544 // diamond control-flow pattern. The incoming instruction knows the
4545 // destination vreg to set, the condition code register to branch on, the
4546 // true/false values to select between, and a branch opcode to use.
4547 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4548 ilist<MachineBasicBlock>::iterator It = BB;
4549 ++It;
4550
4551 // thisMBB:
4552 // ...
4553 // TrueVal = ...
4554 // cmpTY ccX, r1, r2
4555 // bCC copy1MBB
4556 // fallthrough --> copy0MBB
4557 MachineBasicBlock *thisMBB = BB;
4558 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4559 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4560 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
4561 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
4562 MachineFunction *F = BB->getParent();
4563 F->getBasicBlockList().insert(It, copy0MBB);
4564 F->getBasicBlockList().insert(It, sinkMBB);
4565 // Update machine-CFG edges by first adding all successors of the current
4566 // block to the new block which will contain the Phi node for the select.
4567 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4568 e = BB->succ_end(); i != e; ++i)
4569 sinkMBB->addSuccessor(*i);
4570 // Next, remove all successors of the current block, and add the true
4571 // and fallthrough blocks as its successors.
4572 while(!BB->succ_empty())
4573 BB->removeSuccessor(BB->succ_begin());
4574 BB->addSuccessor(copy0MBB);
4575 BB->addSuccessor(sinkMBB);
4576
4577 // copy0MBB:
4578 // %FalseValue = ...
4579 // # fallthrough to sinkMBB
4580 BB = copy0MBB;
4581
4582 // Update machine-CFG edges
4583 BB->addSuccessor(sinkMBB);
4584
4585 // sinkMBB:
4586 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4587 // ...
4588 BB = sinkMBB;
4589 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
4590 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4591 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4592
4593 delete MI; // The pseudo instruction is gone now.
4594 return BB;
4595 }
4596
4597 case X86::FP_TO_INT16_IN_MEM:
4598 case X86::FP_TO_INT32_IN_MEM:
4599 case X86::FP_TO_INT64_IN_MEM: {
4600 // Change the floating point control register to use "round towards zero"
4601 // mode when truncating to an integer value.
4602 MachineFunction *F = BB->getParent();
4603 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4604 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
4605
4606 // Load the old value of the high byte of the control word...
4607 unsigned OldCW =
4608 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4609 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
4610
4611 // Set the high part to be round to zero...
4612 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
4613
4614 // Reload the modified control word now...
4615 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
4616
4617 // Restore the memory image of control word to original value
4618 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
4619
4620 // Get the X86 opcode to use.
4621 unsigned Opc;
4622 switch (MI->getOpcode()) {
4623 default: assert(0 && "illegal opcode!");
4624 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4625 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4626 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4627 }
4628
4629 X86AddressMode AM;
4630 MachineOperand &Op = MI->getOperand(0);
4631 if (Op.isRegister()) {
4632 AM.BaseType = X86AddressMode::RegBase;
4633 AM.Base.Reg = Op.getReg();
4634 } else {
4635 AM.BaseType = X86AddressMode::FrameIndexBase;
4636 AM.Base.FrameIndex = Op.getFrameIndex();
4637 }
4638 Op = MI->getOperand(1);
4639 if (Op.isImmediate())
4640 AM.Scale = Op.getImmedValue();
4641 Op = MI->getOperand(2);
4642 if (Op.isImmediate())
4643 AM.IndexReg = Op.getImmedValue();
4644 Op = MI->getOperand(3);
4645 if (Op.isGlobalAddress()) {
4646 AM.GV = Op.getGlobal();
4647 } else {
4648 AM.Disp = Op.getImmedValue();
4649 }
4650 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
4651
4652 // Reload the original control word now.
4653 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
4654
4655 delete MI; // The pseudo instruction is gone now.
4656 return BB;
4657 }
4658 }
4659}
4660
4661//===----------------------------------------------------------------------===//
4662// X86 Optimization Hooks
4663//===----------------------------------------------------------------------===//
4664
Nate Begeman368e18d2006-02-16 21:11:51 +00004665void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4666 uint64_t Mask,
4667 uint64_t &KnownZero,
4668 uint64_t &KnownOne,
4669 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004670 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00004671 assert((Opc >= ISD::BUILTIN_OP_END ||
4672 Opc == ISD::INTRINSIC_WO_CHAIN ||
4673 Opc == ISD::INTRINSIC_W_CHAIN ||
4674 Opc == ISD::INTRINSIC_VOID) &&
4675 "Should use MaskedValueIsZero if you don't know whether Op"
4676 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004677
Evan Cheng865f0602006-04-05 06:11:20 +00004678 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004679 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00004680 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +00004681 case X86ISD::SETCC:
4682 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4683 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004684 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004685}
Chris Lattner259e97c2006-01-31 19:43:35 +00004686
Evan Cheng206ee9d2006-07-07 08:33:52 +00004687/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4688/// element of the result of the vector shuffle.
4689static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4690 MVT::ValueType VT = N->getValueType(0);
4691 SDOperand PermMask = N->getOperand(2);
4692 unsigned NumElems = PermMask.getNumOperands();
4693 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4694 i %= NumElems;
4695 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4696 return (i == 0)
4697 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4698 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4699 SDOperand Idx = PermMask.getOperand(i);
4700 if (Idx.getOpcode() == ISD::UNDEF)
4701 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4702 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4703 }
4704 return SDOperand();
4705}
4706
4707/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4708/// node is a GlobalAddress + an offset.
4709static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4710 if (N->getOpcode() == X86ISD::Wrapper) {
4711 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4712 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4713 return true;
4714 }
4715 } else if (N->getOpcode() == ISD::ADD) {
4716 SDOperand N1 = N->getOperand(0);
4717 SDOperand N2 = N->getOperand(1);
4718 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4719 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4720 if (V) {
4721 Offset += V->getSignExtended();
4722 return true;
4723 }
4724 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4725 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4726 if (V) {
4727 Offset += V->getSignExtended();
4728 return true;
4729 }
4730 }
4731 }
4732 return false;
4733}
4734
4735/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4736/// + Dist * Size.
4737static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4738 MachineFrameInfo *MFI) {
4739 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4740 return false;
4741
4742 SDOperand Loc = N->getOperand(1);
4743 SDOperand BaseLoc = Base->getOperand(1);
4744 if (Loc.getOpcode() == ISD::FrameIndex) {
4745 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4746 return false;
4747 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4748 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4749 int FS = MFI->getObjectSize(FI);
4750 int BFS = MFI->getObjectSize(BFI);
4751 if (FS != BFS || FS != Size) return false;
4752 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4753 } else {
4754 GlobalValue *GV1 = NULL;
4755 GlobalValue *GV2 = NULL;
4756 int64_t Offset1 = 0;
4757 int64_t Offset2 = 0;
4758 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4759 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4760 if (isGA1 && isGA2 && GV1 == GV2)
4761 return Offset1 == (Offset2 + Dist*Size);
4762 }
4763
4764 return false;
4765}
4766
Evan Cheng1e60c092006-07-10 21:37:44 +00004767static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4768 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004769 GlobalValue *GV;
4770 int64_t Offset;
4771 if (isGAPlusOffset(Base, GV, Offset))
4772 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4773 else {
4774 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4775 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00004776 if (BFI < 0)
4777 // Fixed objects do not specify alignment, however the offsets are known.
4778 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4779 (MFI->getObjectOffset(BFI) % 16) == 0);
4780 else
4781 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00004782 }
4783 return false;
4784}
4785
4786
4787/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4788/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4789/// if the load addresses are consecutive, non-overlapping, and in the right
4790/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00004791static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4792 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004793 MachineFunction &MF = DAG.getMachineFunction();
4794 MachineFrameInfo *MFI = MF.getFrameInfo();
4795 MVT::ValueType VT = N->getValueType(0);
4796 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4797 SDOperand PermMask = N->getOperand(2);
4798 int NumElems = (int)PermMask.getNumOperands();
4799 SDNode *Base = NULL;
4800 for (int i = 0; i < NumElems; ++i) {
4801 SDOperand Idx = PermMask.getOperand(i);
4802 if (Idx.getOpcode() == ISD::UNDEF) {
4803 if (!Base) return SDOperand();
4804 } else {
4805 SDOperand Arg =
4806 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4807 if (!Arg.Val || Arg.getOpcode() != ISD::LOAD)
4808 return SDOperand();
4809 if (!Base)
4810 Base = Arg.Val;
4811 else if (!isConsecutiveLoad(Arg.Val, Base,
4812 i, MVT::getSizeInBits(EVT)/8,MFI))
4813 return SDOperand();
4814 }
4815 }
4816
Evan Cheng1e60c092006-07-10 21:37:44 +00004817 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00004818 if (isAlign16)
4819 return DAG.getLoad(VT, Base->getOperand(0), Base->getOperand(1),
4820 Base->getOperand(2));
Evan Cheng311ace02006-08-11 07:35:45 +00004821 else {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004822 // Just use movups, it's shorter.
Evan Cheng64a752f2006-08-11 09:08:15 +00004823 std::vector<MVT::ValueType> Tys;
4824 Tys.push_back(MVT::v4f32);
4825 Tys.push_back(MVT::Other);
4826 SmallVector<SDOperand, 3> Ops;
4827 Ops.push_back(Base->getOperand(0));
4828 Ops.push_back(Base->getOperand(1));
4829 Ops.push_back(Base->getOperand(2));
Evan Cheng206ee9d2006-07-07 08:33:52 +00004830 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Cheng64a752f2006-08-11 09:08:15 +00004831 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
Evan Cheng311ace02006-08-11 07:35:45 +00004832 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00004833}
4834
4835SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4836 DAGCombinerInfo &DCI) const {
4837 TargetMachine &TM = getTargetMachine();
4838 SelectionDAG &DAG = DCI.DAG;
4839 switch (N->getOpcode()) {
4840 default: break;
4841 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00004842 return PerformShuffleCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00004843 }
4844
4845 return SDOperand();
4846}
4847
Evan Cheng60c07e12006-07-05 22:17:51 +00004848//===----------------------------------------------------------------------===//
4849// X86 Inline Assembly Support
4850//===----------------------------------------------------------------------===//
4851
Chris Lattnerf4dff842006-07-11 02:54:03 +00004852/// getConstraintType - Given a constraint letter, return the type of
4853/// constraint it is for this target.
4854X86TargetLowering::ConstraintType
4855X86TargetLowering::getConstraintType(char ConstraintLetter) const {
4856 switch (ConstraintLetter) {
Chris Lattner6d346572006-07-12 16:59:49 +00004857 case 'A':
4858 case 'r':
4859 case 'R':
4860 case 'l':
4861 case 'q':
4862 case 'Q':
4863 case 'x':
4864 case 'Y':
4865 return C_RegisterClass;
Chris Lattnerf4dff842006-07-11 02:54:03 +00004866 default: return TargetLowering::getConstraintType(ConstraintLetter);
4867 }
4868}
4869
Chris Lattner259e97c2006-01-31 19:43:35 +00004870std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00004871getRegClassForInlineAsmConstraint(const std::string &Constraint,
4872 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00004873 if (Constraint.size() == 1) {
4874 // FIXME: not handling fp-stack yet!
4875 // FIXME: not handling MMX registers yet ('y' constraint).
4876 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00004877 default: break; // Unknown constraint letter
4878 case 'A': // EAX/EDX
4879 if (VT == MVT::i32 || VT == MVT::i64)
4880 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4881 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00004882 case 'r': // GENERAL_REGS
4883 case 'R': // LEGACY_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00004884 if (VT == MVT::i32)
4885 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4886 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4887 else if (VT == MVT::i16)
4888 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4889 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4890 else if (VT == MVT::i8)
4891 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4892 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00004893 case 'l': // INDEX_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00004894 if (VT == MVT::i32)
4895 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4896 X86::ESI, X86::EDI, X86::EBP, 0);
4897 else if (VT == MVT::i16)
4898 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4899 X86::SI, X86::DI, X86::BP, 0);
4900 else if (VT == MVT::i8)
4901 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4902 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00004903 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4904 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00004905 if (VT == MVT::i32)
4906 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4907 else if (VT == MVT::i16)
4908 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4909 else if (VT == MVT::i8)
4910 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4911 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00004912 case 'x': // SSE_REGS if SSE1 allowed
4913 if (Subtarget->hasSSE1())
4914 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4915 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4916 0);
4917 return std::vector<unsigned>();
4918 case 'Y': // SSE_REGS if SSE2 allowed
4919 if (Subtarget->hasSSE2())
4920 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4921 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4922 0);
4923 return std::vector<unsigned>();
4924 }
4925 }
4926
Chris Lattner1efa40f2006-02-22 00:56:39 +00004927 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00004928}
Chris Lattnerf76d1802006-07-31 23:26:50 +00004929
4930std::pair<unsigned, const TargetRegisterClass*>
4931X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4932 MVT::ValueType VT) const {
4933 // Use the default implementation in TargetLowering to convert the register
4934 // constraint into a member of a register class.
4935 std::pair<unsigned, const TargetRegisterClass*> Res;
4936 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4937
4938 // Not found? Bail out.
4939 if (Res.second == 0) return Res;
4940
4941 // Otherwise, check to see if this is a register class of the wrong value
4942 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4943 // turn into {ax},{dx}.
4944 if (Res.second->hasType(VT))
4945 return Res; // Correct type already, nothing to do.
4946
4947 // All of the single-register GCC register classes map their values onto
4948 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4949 // really want an 8-bit or 32-bit register, map to the appropriate register
4950 // class and return the appropriate register.
4951 if (Res.second != X86::GR16RegisterClass)
4952 return Res;
4953
4954 if (VT == MVT::i8) {
4955 unsigned DestReg = 0;
4956 switch (Res.first) {
4957 default: break;
4958 case X86::AX: DestReg = X86::AL; break;
4959 case X86::DX: DestReg = X86::DL; break;
4960 case X86::CX: DestReg = X86::CL; break;
4961 case X86::BX: DestReg = X86::BL; break;
4962 }
4963 if (DestReg) {
4964 Res.first = DestReg;
4965 Res.second = Res.second = X86::GR8RegisterClass;
4966 }
4967 } else if (VT == MVT::i32) {
4968 unsigned DestReg = 0;
4969 switch (Res.first) {
4970 default: break;
4971 case X86::AX: DestReg = X86::EAX; break;
4972 case X86::DX: DestReg = X86::EDX; break;
4973 case X86::CX: DestReg = X86::ECX; break;
4974 case X86::BX: DestReg = X86::EBX; break;
4975 case X86::SI: DestReg = X86::ESI; break;
4976 case X86::DI: DestReg = X86::EDI; break;
4977 case X86::BP: DestReg = X86::EBP; break;
4978 case X86::SP: DestReg = X86::ESP; break;
4979 }
4980 if (DestReg) {
4981 Res.first = DestReg;
4982 Res.second = Res.second = X86::GR32RegisterClass;
4983 }
Evan Cheng25ab6902006-09-08 06:48:29 +00004984 } else if (VT == MVT::i64) {
4985 unsigned DestReg = 0;
4986 switch (Res.first) {
4987 default: break;
4988 case X86::AX: DestReg = X86::RAX; break;
4989 case X86::DX: DestReg = X86::RDX; break;
4990 case X86::CX: DestReg = X86::RCX; break;
4991 case X86::BX: DestReg = X86::RBX; break;
4992 case X86::SI: DestReg = X86::RSI; break;
4993 case X86::DI: DestReg = X86::RDI; break;
4994 case X86::BP: DestReg = X86::RBP; break;
4995 case X86::SP: DestReg = X86::RSP; break;
4996 }
4997 if (DestReg) {
4998 Res.first = DestReg;
4999 Res.second = Res.second = X86::GR64RegisterClass;
5000 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005001 }
5002
5003 return Res;
5004}
5005