blob: c6bdc2e356817383ffd83c46b2140d9f38ccdc96 [file] [log] [blame]
Chris Lattnera58f5592006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattner76ac0682005-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 Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chengdc614c12006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng88decde2006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000025#include "llvm/ADT/VectorExtras.h"
26#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdc3adc82007-02-27 04:43:02 +000027#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000031#include "llvm/CodeGen/SelectionDAG.h"
32#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattnerf6a69662006-10-31 19:42:44 +000035#include "llvm/ADT/StringExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000036using namespace llvm;
37
Chris Lattner76ac0682005-11-15 00:40:23 +000038X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000042 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Chengcde9e302006-01-27 08:10:46 +000043
Chris Lattner76ac0682005-11-15 00:40:23 +000044 // Set up the TargetLowering object.
45
46 // X86 is weird, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
49 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000050 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000051 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng11b0a5d2006-09-08 06:48:29 +000052 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng20931a72006-03-16 21:47:42 +000053
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000054 if (Subtarget->isTargetDarwin()) {
Evan Chengb09a56f2006-03-17 20:31:41 +000055 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000056 setUseUnderscoreSetJmp(false);
57 setUseUnderscoreLongJmp(false);
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +000058 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000059 // MS runtime is weird: it exports _setjmp, but longjmp!
60 setUseUnderscoreSetJmp(true);
61 setUseUnderscoreLongJmp(false);
62 } else {
63 setUseUnderscoreSetJmp(true);
64 setUseUnderscoreLongJmp(true);
65 }
66
Chris Lattner76ac0682005-11-15 00:40:23 +000067 // Set up the register classes.
Evan Cheng9fee4422006-05-16 07:21:53 +000068 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
69 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
70 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000071 if (Subtarget->is64Bit())
72 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +000073
Evan Cheng5d9fd972006-10-04 00:56:09 +000074 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
75
Chris Lattner76ac0682005-11-15 00:40:23 +000076 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
77 // operation.
78 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
79 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
80 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000081
Evan Cheng11b0a5d2006-09-08 06:48:29 +000082 if (Subtarget->is64Bit()) {
83 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000084 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000085 } else {
86 if (X86ScalarSSE)
87 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
88 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
89 else
90 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
91 }
Chris Lattner76ac0682005-11-15 00:40:23 +000092
93 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
94 // this operation.
95 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
96 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000097 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000098 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000099 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +0000100 else {
101 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
102 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
103 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000104
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000105 if (!Subtarget->is64Bit()) {
106 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
107 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
108 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
109 }
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000110
Evan Cheng08390f62006-01-30 22:13:22 +0000111 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
112 // this operation.
113 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
114 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
115
116 if (X86ScalarSSE) {
117 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
118 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000119 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000120 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000121 }
122
123 // Handle FP_TO_UINT by promoting the destination to a larger signed
124 // conversion.
125 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
126 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
127 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
128
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000129 if (Subtarget->is64Bit()) {
130 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000131 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000132 } else {
133 if (X86ScalarSSE && !Subtarget->hasSSE3())
134 // Expand FP_TO_UINT into a select.
135 // FIXME: We would like to use a Custom expander here eventually to do
136 // the optimal thing for SSE vs. the default expansion in the legalizer.
137 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
138 else
139 // With SSE3 we can use fisttpll to convert to a signed i64.
140 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
141 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000142
Chris Lattner55c17f92006-12-05 18:22:22 +0000143 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Chris Lattnerc20b7e82006-12-05 18:45:06 +0000144 if (!X86ScalarSSE) {
145 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
146 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
147 }
Chris Lattner30107e62005-12-23 05:15:23 +0000148
Evan Cheng0d41d192006-10-30 08:02:39 +0000149 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng593bea72006-02-17 07:01:52 +0000150 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000151 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
152 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000153 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000154 if (Subtarget->is64Bit())
155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
159 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000160 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000161
Chris Lattner76ac0682005-11-15 00:40:23 +0000162 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
163 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
164 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
165 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
166 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
167 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
168 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
169 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
170 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000171 if (Subtarget->is64Bit()) {
172 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
173 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
174 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
175 }
176
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000177 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000178 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000179
Chris Lattner76ac0682005-11-15 00:40:23 +0000180 // These should be promoted to a larger select which is supported.
181 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
182 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000183 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000184 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
185 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
186 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
187 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
188 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
189 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
190 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
191 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
192 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000193 if (Subtarget->is64Bit()) {
194 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
195 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
196 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000197 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000198 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000199 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000200 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000201 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000202 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000203 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000204 if (Subtarget->is64Bit()) {
205 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
206 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
207 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
208 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
209 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000210 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000211 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
212 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
213 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000214 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000215 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
216 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000217
Chris Lattner9c415362005-11-29 06:16:21 +0000218 // We don't have line number support yet.
219 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000220 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000221 // FIXME - use subtarget debug flags
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000222 if (!Subtarget->isTargetDarwin() &&
223 !Subtarget->isTargetELF() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +0000224 !Subtarget->isTargetCygMing())
Jim Laskeyf9e54452007-01-26 14:34:52 +0000225 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000226
Nate Begemane74795c2006-01-25 18:21:52 +0000227 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
228 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemane74795c2006-01-25 18:21:52 +0000229 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemane74795c2006-01-25 18:21:52 +0000230 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengdeaea252007-03-02 23:16:35 +0000231 if (Subtarget->is64Bit())
232 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
233 else
234 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
235
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000236 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000237 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000238 if (Subtarget->is64Bit())
239 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +0000240 if (Subtarget->isTargetCygMing())
241 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
242 else
243 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000244
Chris Lattner76ac0682005-11-15 00:40:23 +0000245 if (X86ScalarSSE) {
246 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000247 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
248 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000249
Evan Cheng72d5c252006-01-31 22:28:30 +0000250 // Use ANDPD to simulate FABS.
251 setOperationAction(ISD::FABS , MVT::f64, Custom);
252 setOperationAction(ISD::FABS , MVT::f32, Custom);
253
254 // Use XORP to simulate FNEG.
255 setOperationAction(ISD::FNEG , MVT::f64, Custom);
256 setOperationAction(ISD::FNEG , MVT::f32, Custom);
257
Evan Cheng4363e882007-01-05 07:55:56 +0000258 // Use ANDPD and ORPD to simulate FCOPYSIGN.
259 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
260 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
261
Evan Chengd8fba3a2006-02-02 00:28:23 +0000262 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000263 setOperationAction(ISD::FSIN , MVT::f64, Expand);
264 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000265 setOperationAction(ISD::FREM , MVT::f64, Expand);
266 setOperationAction(ISD::FSIN , MVT::f32, Expand);
267 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000268 setOperationAction(ISD::FREM , MVT::f32, Expand);
269
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000270 // Expand FP immediates into loads from the stack, except for the special
271 // cases we handle.
272 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
273 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000274 addLegalFPImmediate(+0.0); // xorps / xorpd
275 } else {
276 // Set up the FP register classes.
277 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000278
Evan Cheng4363e882007-01-05 07:55:56 +0000279 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
280 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
281 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000282
Chris Lattner76ac0682005-11-15 00:40:23 +0000283 if (!UnsafeFPMath) {
284 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
285 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
286 }
287
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000288 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000289 addLegalFPImmediate(+0.0); // FLD0
290 addLegalFPImmediate(+1.0); // FLD1
291 addLegalFPImmediate(-0.0); // FLD0/FCHS
292 addLegalFPImmediate(-1.0); // FLD1/FCHS
293 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000294
Evan Cheng19264272006-03-01 01:11:20 +0000295 // First set operation action for all vector types to expand. Then we
296 // will selectively turn on ones that can be effectively codegen'd.
297 for (unsigned VT = (unsigned)MVT::Vector + 1;
298 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
299 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
300 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000301 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
302 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000303 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000304 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
305 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
306 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
307 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
308 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
309 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000310 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000311 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000312 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000313 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000314 }
315
Evan Chengbc047222006-03-22 19:22:18 +0000316 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000317 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
318 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
319 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendling98d21042007-03-26 07:53:08 +0000320 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng9e252e32006-02-22 02:26:30 +0000321
Evan Cheng19264272006-03-01 01:11:20 +0000322 // FIXME: add MMX packed arithmetics
Bill Wendling97905b42007-03-07 05:43:18 +0000323
Bill Wendling6092ce22007-03-08 22:09:11 +0000324 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
325 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
326 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner2805bce2007-04-12 04:14:49 +0000327 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000328
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000329 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
330 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
331 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
332
Bill Wendlinge3103412007-03-15 21:24:36 +0000333 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
334 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
335
Bill Wendling144b8bb2007-03-16 09:44:46 +0000336 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000337 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000338 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000339 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
340 setOperationAction(ISD::AND, MVT::v2i32, Promote);
341 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
342 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000343
344 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000345 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000346 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000347 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
348 setOperationAction(ISD::OR, MVT::v2i32, Promote);
349 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
350 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000351
352 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000353 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000354 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000355 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
356 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
357 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
358 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000359
Bill Wendling6092ce22007-03-08 22:09:11 +0000360 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000361 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling6092ce22007-03-08 22:09:11 +0000362 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000363 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
364 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
365 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
366 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000367
Bill Wendling6dff51a2007-03-27 20:22:40 +0000368 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
369 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
370 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
371 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlingd551a182007-03-22 18:42:45 +0000372
373 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
374 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
375 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendling6dff51a2007-03-27 20:22:40 +0000376 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendlingad2db4a2007-03-28 00:57:11 +0000377
378 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
379 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000380 }
381
Evan Chengbc047222006-03-22 19:22:18 +0000382 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000383 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
384
Evan Chengbf3df772006-10-27 18:49:08 +0000385 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
386 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
387 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
388 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000389 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
390 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
391 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000392 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000393 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000394 }
395
Evan Chengbc047222006-03-22 19:22:18 +0000396 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000397 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
398 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
399 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
400 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
401 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
402
Evan Cheng617a6a82006-04-10 07:23:14 +0000403 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
404 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
405 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000406 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000407 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
408 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
409 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000410 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000411 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengbf3df772006-10-27 18:49:08 +0000412 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
413 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
414 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
415 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Evan Cheng92232302006-04-12 21:21:57 +0000416
Evan Cheng617a6a82006-04-10 07:23:14 +0000417 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
418 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000419 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000420 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
421 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
422 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000423
Evan Cheng92232302006-04-12 21:21:57 +0000424 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
425 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
426 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
427 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
428 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
429 }
430 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
431 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
432 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
433 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
434 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
435 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
436
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000437 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng92232302006-04-12 21:21:57 +0000438 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
439 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
440 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
441 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
442 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
443 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
444 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000445 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
446 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000447 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
448 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000449 }
Evan Cheng92232302006-04-12 21:21:57 +0000450
451 // Custom lower v2i64 and v2f64 selects.
452 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000453 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000454 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000455 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000456 }
457
Evan Cheng78038292006-04-05 23:38:46 +0000458 // We want to custom lower some of our intrinsics.
459 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
460
Evan Cheng5987cfb2006-07-07 08:33:52 +0000461 // We have target-specific dag combine patterns for the following nodes:
462 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner9259b1e2006-10-04 06:57:07 +0000463 setTargetDAGCombine(ISD::SELECT);
Evan Cheng5987cfb2006-07-07 08:33:52 +0000464
Chris Lattner76ac0682005-11-15 00:40:23 +0000465 computeRegisterProperties();
466
Evan Cheng6a374562006-02-14 08:25:08 +0000467 // FIXME: These should be based on subtarget info. Plus, the values should
468 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000469 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
470 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
471 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000472 allowUnalignedMemoryAccesses = true; // x86 supports it!
473}
474
Chris Lattner3c763092007-02-25 08:29:00 +0000475
476//===----------------------------------------------------------------------===//
477// Return Value Calling Convention Implementation
478//===----------------------------------------------------------------------===//
479
Chris Lattnerba3d2732007-02-28 04:55:35 +0000480#include "X86GenCallingConv.inc"
Chris Lattnerc9eed392007-02-27 05:28:59 +0000481
Chris Lattner2fc0d702007-02-25 09:12:39 +0000482/// LowerRET - Lower an ISD::RET node.
483SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
484 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
485
Chris Lattnerc9eed392007-02-27 05:28:59 +0000486 SmallVector<CCValAssign, 16> RVLocs;
487 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
488 CCState CCInfo(CC, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000489 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000490
Chris Lattner2fc0d702007-02-25 09:12:39 +0000491
492 // If this is the first return lowered for this function, add the regs to the
493 // liveout set for the function.
494 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000495 for (unsigned i = 0; i != RVLocs.size(); ++i)
496 if (RVLocs[i].isRegLoc())
497 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2fc0d702007-02-25 09:12:39 +0000498 }
499
500 SDOperand Chain = Op.getOperand(0);
501 SDOperand Flag;
502
503 // Copy the result values into the output registers.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000504 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
505 RVLocs[0].getLocReg() != X86::ST0) {
506 for (unsigned i = 0; i != RVLocs.size(); ++i) {
507 CCValAssign &VA = RVLocs[i];
508 assert(VA.isRegLoc() && "Can only return in registers!");
509 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
510 Flag);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000511 Flag = Chain.getValue(1);
512 }
513 } else {
514 // We need to handle a destination of ST0 specially, because it isn't really
515 // a register.
516 SDOperand Value = Op.getOperand(1);
517
518 // If this is an FP return with ScalarSSE, we need to move the value from
519 // an XMM register onto the fp-stack.
520 if (X86ScalarSSE) {
521 SDOperand MemLoc;
522
523 // If this is a load into a scalarsse value, don't store the loaded value
524 // back to the stack, only to reload it: just replace the scalar-sse load.
525 if (ISD::isNON_EXTLoad(Value.Val) &&
526 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
527 Chain = Value.getOperand(0);
528 MemLoc = Value.getOperand(1);
529 } else {
530 // Spill the value to memory and reload it into top of stack.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000531 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2fc0d702007-02-25 09:12:39 +0000532 MachineFunction &MF = DAG.getMachineFunction();
533 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
534 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
535 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
536 }
537 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000538 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2fc0d702007-02-25 09:12:39 +0000539 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
540 Chain = Value.getValue(1);
541 }
542
543 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
544 SDOperand Ops[] = { Chain, Value };
545 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
546 Flag = Chain.getValue(1);
547 }
548
549 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
550 if (Flag.Val)
551 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
552 else
553 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
554}
555
556
Chris Lattner0cd99602007-02-25 08:59:22 +0000557/// LowerCallResult - Lower the result values of an ISD::CALL into the
558/// appropriate copies out of appropriate physical registers. This assumes that
559/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
560/// being lowered. The returns a SDNode with the same number of values as the
561/// ISD::CALL.
562SDNode *X86TargetLowering::
563LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
564 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattner152bfa12007-02-28 07:09:55 +0000565
566 // Assign locations to each value returned by this call.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000567 SmallVector<CCValAssign, 16> RVLocs;
568 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000569 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
570
Chris Lattner0cd99602007-02-25 08:59:22 +0000571
Chris Lattner152bfa12007-02-28 07:09:55 +0000572 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner0cd99602007-02-25 08:59:22 +0000573
574 // Copy all of the result registers out of their specified physreg.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000575 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
576 for (unsigned i = 0; i != RVLocs.size(); ++i) {
577 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
578 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner0cd99602007-02-25 08:59:22 +0000579 InFlag = Chain.getValue(2);
580 ResultVals.push_back(Chain.getValue(0));
581 }
582 } else {
583 // Copies from the FP stack are special, as ST0 isn't a valid register
584 // before the fp stackifier runs.
585
586 // Copy ST0 into an RFP register with FP_GET_RESULT.
587 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
588 SDOperand GROps[] = { Chain, InFlag };
589 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
590 Chain = RetVal.getValue(1);
591 InFlag = RetVal.getValue(2);
592
593 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
594 // an XMM register.
595 if (X86ScalarSSE) {
596 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
597 // shouldn't be necessary except that RFP cannot be live across
598 // multiple blocks. When stackifier is fixed, they can be uncoupled.
599 MachineFunction &MF = DAG.getMachineFunction();
600 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
601 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
602 SDOperand Ops[] = {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000603 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner0cd99602007-02-25 08:59:22 +0000604 };
605 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000606 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner0cd99602007-02-25 08:59:22 +0000607 Chain = RetVal.getValue(1);
608 }
609
Chris Lattnerc9eed392007-02-27 05:28:59 +0000610 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
Chris Lattner0cd99602007-02-25 08:59:22 +0000611 // FIXME: we would really like to remember that this FP_ROUND
612 // operation is okay to eliminate if we allow excess FP precision.
613 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
614 ResultVals.push_back(RetVal);
615 }
616
617 // Merge everything together with a MERGE_VALUES node.
618 ResultVals.push_back(Chain);
619 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
620 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner3c763092007-02-25 08:29:00 +0000621}
622
623
Chris Lattner76ac0682005-11-15 00:40:23 +0000624//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000625// C & StdCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000626//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000627// StdCall calling convention seems to be standard for many Windows' API
628// routines and around. It differs from C calling convention just a little:
629// callee should clean up the stack, not caller. Symbols should be also
630// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +0000631
Evan Cheng24eb3f42006-04-27 05:35:28 +0000632/// AddLiveIn - This helper function adds the specified physical register to the
633/// MachineFunction as a live in value. It also creates a corresponding virtual
634/// register for it.
635static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000636 const TargetRegisterClass *RC) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000637 assert(RC->contains(PReg) && "Not the correct regclass!");
638 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
639 MF.addLiveIn(PReg, VReg);
640 return VReg;
641}
642
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000643SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
644 bool isStdCall) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000645 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000646 MachineFunction &MF = DAG.getMachineFunction();
647 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000648 SDOperand Root = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000649 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000650
Chris Lattner227b6c52007-02-28 07:00:42 +0000651 // Assign locations to all of the incoming arguments.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000652 SmallVector<CCValAssign, 16> ArgLocs;
653 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
654 ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000655 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
656
Chris Lattnerb9db2252007-02-28 05:46:49 +0000657 SmallVector<SDOperand, 8> ArgValues;
658 unsigned LastVal = ~0U;
659 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
660 CCValAssign &VA = ArgLocs[i];
661 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
662 // places.
663 assert(VA.getValNo() != LastVal &&
664 "Don't support value assigned to multiple locs yet");
665 LastVal = VA.getValNo();
666
667 if (VA.isRegLoc()) {
668 MVT::ValueType RegVT = VA.getLocVT();
669 TargetRegisterClass *RC;
670 if (RegVT == MVT::i32)
671 RC = X86::GR32RegisterClass;
672 else {
673 assert(MVT::isVector(RegVT));
674 RC = X86::VR128RegisterClass;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000675 }
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000676
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000677 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
678 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerb9db2252007-02-28 05:46:49 +0000679
680 // If this is an 8 or 16-bit value, it is really passed promoted to 32
681 // bits. Insert an assert[sz]ext to capture this, then truncate to the
682 // right size.
683 if (VA.getLocInfo() == CCValAssign::SExt)
684 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
685 DAG.getValueType(VA.getValVT()));
686 else if (VA.getLocInfo() == CCValAssign::ZExt)
687 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
688 DAG.getValueType(VA.getValVT()));
689
690 if (VA.getLocInfo() != CCValAssign::Full)
691 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
692
693 ArgValues.push_back(ArgValue);
694 } else {
695 assert(VA.isMemLoc());
696
697 // Create the nodes corresponding to a load from this parameter slot.
698 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
699 VA.getLocMemOffset());
700 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
701 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000702 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000703 }
Chris Lattnerb9db2252007-02-28 05:46:49 +0000704
705 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000706
Evan Cheng17e734f2006-05-23 21:06:34 +0000707 ArgValues.push_back(Root);
708
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000709 // If the function takes variable number of arguments, make a frame index for
710 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000711 if (isVarArg)
Chris Lattnerb9db2252007-02-28 05:46:49 +0000712 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000713
714 if (isStdCall && !isVarArg) {
Chris Lattnerb9db2252007-02-28 05:46:49 +0000715 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000716 BytesCallerReserves = 0;
717 } else {
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000718 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000719
720 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000721 if (NumArgs &&
722 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000723 ISD::ParamFlags::StructReturn))
Chris Lattnerb9db2252007-02-28 05:46:49 +0000724 BytesToPopOnReturn = 4;
725
726 BytesCallerReserves = StackSize;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000727 }
728
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000729 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
730 ReturnAddrIndex = 0; // No return address slot generated yet.
Evan Cheng17e734f2006-05-23 21:06:34 +0000731
Chris Lattnerff0598d2007-04-17 17:21:52 +0000732 MF.getInfo<X86MachineFunctionInfo>()
733 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000734
Evan Cheng17e734f2006-05-23 21:06:34 +0000735 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +0000736 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +0000737 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000738}
739
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000740SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +0000741 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +0000742 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000743 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000744 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
745 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +0000746 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000747
Chris Lattner227b6c52007-02-28 07:00:42 +0000748 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerbe799592007-02-28 05:31:48 +0000749 SmallVector<CCValAssign, 16> ArgLocs;
750 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000751 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000752
Chris Lattnerbe799592007-02-28 05:31:48 +0000753 // Get a count of how many bytes are to be pushed on the stack.
754 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +0000755
Evan Cheng2a330942006-05-25 00:59:30 +0000756 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000757
Chris Lattner35a08552007-02-25 07:10:00 +0000758 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
759 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng2a330942006-05-25 00:59:30 +0000760
Chris Lattnerbe799592007-02-28 05:31:48 +0000761 SDOperand StackPtr;
Chris Lattnerbe799592007-02-28 05:31:48 +0000762
763 // Walk the register/memloc assignments, inserting copies/loads.
764 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
765 CCValAssign &VA = ArgLocs[i];
766 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000767
Chris Lattnerbe799592007-02-28 05:31:48 +0000768 // Promote the value if needed.
769 switch (VA.getLocInfo()) {
770 default: assert(0 && "Unknown loc info!");
771 case CCValAssign::Full: break;
772 case CCValAssign::SExt:
773 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
774 break;
775 case CCValAssign::ZExt:
776 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
777 break;
778 case CCValAssign::AExt:
779 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
780 break;
Evan Cheng5ee96892006-05-25 18:56:34 +0000781 }
Chris Lattnerbe799592007-02-28 05:31:48 +0000782
783 if (VA.isRegLoc()) {
784 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
785 } else {
786 assert(VA.isMemLoc());
787 if (StackPtr.Val == 0)
788 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
789 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000790 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
791 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner76ac0682005-11-15 00:40:23 +0000792 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000793 }
794
Chris Lattner5958b172007-02-28 05:39:26 +0000795 // If the first argument is an sret pointer, remember it.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000796 bool isSRet = NumOps &&
797 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000798 ISD::ParamFlags::StructReturn);
Chris Lattner5958b172007-02-28 05:39:26 +0000799
Evan Cheng2a330942006-05-25 00:59:30 +0000800 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000801 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
802 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +0000803
Evan Cheng88decde2006-04-28 21:29:37 +0000804 // Build a sequence of copy-to-reg nodes chained together with token chain
805 // and flag operands which copy the outgoing args into registers.
806 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +0000807 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
808 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
809 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +0000810 InFlag = Chain.getValue(1);
811 }
812
Evan Cheng84a041e2007-02-21 21:18:14 +0000813 // ELF / PIC requires GOT in the EBX register before function calls via PLT
814 // GOT pointer.
Evan Cheng1281dc32007-01-22 21:34:25 +0000815 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
816 Subtarget->isPICStyleGOT()) {
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000817 Chain = DAG.getCopyToReg(Chain, X86::EBX,
818 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
819 InFlag);
820 InFlag = Chain.getValue(1);
821 }
822
Evan Cheng2a330942006-05-25 00:59:30 +0000823 // If the callee is a GlobalAddress node (quite common, every direct call is)
824 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +0000825 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +0000826 // We should use extra load for direct calls to dllimported functions in
827 // non-JIT mode.
828 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
829 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +0000830 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
831 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +0000832 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
833
Chris Lattnere56fef92007-02-25 06:40:16 +0000834 // Returns a chain & a flag for retval copy to use.
835 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +0000836 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000837 Ops.push_back(Chain);
838 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +0000839
840 // Add argument registers to the end of the list so that they are known live
841 // into the call.
842 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000843 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +0000844 RegsToPass[i].second.getValueType()));
Evan Cheng84a041e2007-02-21 21:18:14 +0000845
846 // Add an implicit use GOT pointer in EBX.
847 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
848 Subtarget->isPICStyleGOT())
849 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000850
Evan Cheng88decde2006-04-28 21:29:37 +0000851 if (InFlag.Val)
852 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000853
Evan Cheng2a330942006-05-25 00:59:30 +0000854 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000855 NodeTys, &Ops[0], Ops.size());
Evan Cheng88decde2006-04-28 21:29:37 +0000856 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000857
Chris Lattner8be5be82006-05-23 18:50:38 +0000858 // Create the CALLSEQ_END node.
859 unsigned NumBytesForCalleeToPush = 0;
860
Chris Lattner7802f3e2007-02-25 09:06:15 +0000861 if (CC == CallingConv::X86_StdCall) {
862 if (isVarArg)
Chris Lattner5958b172007-02-28 05:39:26 +0000863 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner7802f3e2007-02-25 09:06:15 +0000864 else
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000865 NumBytesForCalleeToPush = NumBytes;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000866 } else {
867 // If this is is a call to a struct-return function, the callee
868 // pops the hidden struct pointer, so we have to push it back.
869 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattner5958b172007-02-28 05:39:26 +0000870 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000871 }
872
Chris Lattnerd6b853ad2007-02-25 07:18:38 +0000873 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000874 Ops.clear();
875 Ops.push_back(Chain);
876 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000877 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000878 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000879 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner0cd99602007-02-25 08:59:22 +0000880 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000881
Chris Lattner0cd99602007-02-25 08:59:22 +0000882 // Handle result values, copying them out of physregs into vregs that we
883 // return.
Chris Lattner7802f3e2007-02-25 09:06:15 +0000884 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000885}
886
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000887
888//===----------------------------------------------------------------------===//
Chris Lattner3066bec2007-02-28 06:10:12 +0000889// FastCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000890//===----------------------------------------------------------------------===//
891//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000892// The X86 'fastcall' calling convention passes up to two integer arguments in
893// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
894// and requires that the callee pop its arguments off the stack (allowing proper
895// tail calls), and has the same return value conventions as C calling convs.
896//
897// This calling convention always arranges for the callee pop value to be 8n+4
898// bytes, which is needed for tail recursion elimination and stack alignment
899// reasons.
Evan Cheng17e734f2006-05-23 21:06:34 +0000900SDOperand
Chris Lattner3ed3be32007-02-28 06:05:16 +0000901X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000902 MachineFunction &MF = DAG.getMachineFunction();
903 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000904 SDOperand Root = Op.getOperand(0);
Chris Lattner76ac0682005-11-15 00:40:23 +0000905
Chris Lattner227b6c52007-02-28 07:00:42 +0000906 // Assign locations to all of the incoming arguments.
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000907 SmallVector<CCValAssign, 16> ArgLocs;
908 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
909 ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000910 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000911
912 SmallVector<SDOperand, 8> ArgValues;
913 unsigned LastVal = ~0U;
914 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
915 CCValAssign &VA = ArgLocs[i];
916 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
917 // places.
918 assert(VA.getValNo() != LastVal &&
919 "Don't support value assigned to multiple locs yet");
920 LastVal = VA.getValNo();
921
922 if (VA.isRegLoc()) {
923 MVT::ValueType RegVT = VA.getLocVT();
924 TargetRegisterClass *RC;
925 if (RegVT == MVT::i32)
926 RC = X86::GR32RegisterClass;
927 else {
928 assert(MVT::isVector(RegVT));
929 RC = X86::VR128RegisterClass;
930 }
931
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000932 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
933 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000934
935 // If this is an 8 or 16-bit value, it is really passed promoted to 32
936 // bits. Insert an assert[sz]ext to capture this, then truncate to the
937 // right size.
938 if (VA.getLocInfo() == CCValAssign::SExt)
939 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
940 DAG.getValueType(VA.getValVT()));
941 else if (VA.getLocInfo() == CCValAssign::ZExt)
942 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
943 DAG.getValueType(VA.getValVT()));
944
945 if (VA.getLocInfo() != CCValAssign::Full)
946 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
947
948 ArgValues.push_back(ArgValue);
949 } else {
950 assert(VA.isMemLoc());
951
952 // Create the nodes corresponding to a load from this parameter slot.
953 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
954 VA.getLocMemOffset());
955 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
956 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
957 }
958 }
959
Evan Cheng17e734f2006-05-23 21:06:34 +0000960 ArgValues.push_back(Root);
961
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000962 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000963
Anton Korobeynikov57af2a42007-03-02 21:50:27 +0000964 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000965 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
966 // arguments and the arguments after the retaddr has been pushed are aligned.
967 if ((StackSize & 7) == 0)
968 StackSize += 4;
969 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000970
971 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000972 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattner76ac0682005-11-15 00:40:23 +0000973 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000974 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +0000975 BytesCallerReserves = 0;
976
Chris Lattnerff0598d2007-04-17 17:21:52 +0000977 MF.getInfo<X86MachineFunctionInfo>()
978 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000979
Evan Cheng17e734f2006-05-23 21:06:34 +0000980 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +0000981 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +0000982 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000983}
984
Chris Lattner104aa5d2006-09-26 03:57:53 +0000985SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +0000986 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +0000987 SDOperand Chain = Op.getOperand(0);
Evan Cheng2a330942006-05-25 00:59:30 +0000988 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
989 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +0000990
Chris Lattner227b6c52007-02-28 07:00:42 +0000991 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerd439e862007-02-28 06:26:33 +0000992 SmallVector<CCValAssign, 16> ArgLocs;
993 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000994 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerd439e862007-02-28 06:26:33 +0000995
996 // Get a count of how many bytes are to be pushed on the stack.
997 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +0000998
Anton Korobeynikov57af2a42007-03-02 21:50:27 +0000999 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001000 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1001 // arguments and the arguments after the retaddr has been pushed are aligned.
1002 if ((NumBytes & 7) == 0)
1003 NumBytes += 4;
1004 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001005
Chris Lattner62c34842006-02-13 09:00:43 +00001006 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerd439e862007-02-28 06:26:33 +00001007
Chris Lattner35a08552007-02-25 07:10:00 +00001008 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1009 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerd439e862007-02-28 06:26:33 +00001010
1011 SDOperand StackPtr;
1012
1013 // Walk the register/memloc assignments, inserting copies/loads.
1014 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1015 CCValAssign &VA = ArgLocs[i];
1016 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1017
1018 // Promote the value if needed.
1019 switch (VA.getLocInfo()) {
1020 default: assert(0 && "Unknown loc info!");
1021 case CCValAssign::Full: break;
1022 case CCValAssign::SExt:
1023 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner3ed3be32007-02-28 06:05:16 +00001024 break;
Chris Lattnerd439e862007-02-28 06:26:33 +00001025 case CCValAssign::ZExt:
1026 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1027 break;
1028 case CCValAssign::AExt:
1029 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1030 break;
1031 }
1032
1033 if (VA.isRegLoc()) {
1034 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1035 } else {
1036 assert(VA.isMemLoc());
1037 if (StackPtr.Val == 0)
1038 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1039 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001040 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Chengab51cf22006-10-13 21:14:26 +00001041 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng2a330942006-05-25 00:59:30 +00001042 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001043 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001044
Evan Cheng2a330942006-05-25 00:59:30 +00001045 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001046 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1047 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +00001048
Nate Begeman7e5496d2006-02-17 00:03:04 +00001049 // Build a sequence of copy-to-reg nodes chained together with token chain
1050 // and flag operands which copy the outgoing args into registers.
1051 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001052 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1053 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1054 InFlag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001055 InFlag = Chain.getValue(1);
1056 }
1057
Evan Cheng2a330942006-05-25 00:59:30 +00001058 // If the callee is a GlobalAddress node (quite common, every direct call is)
1059 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001060 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00001061 // We should use extra load for direct calls to dllimported functions in
1062 // non-JIT mode.
1063 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1064 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001065 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1066 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +00001067 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1068
Evan Cheng84a041e2007-02-21 21:18:14 +00001069 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1070 // GOT pointer.
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001071 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1072 Subtarget->isPICStyleGOT()) {
1073 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1074 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1075 InFlag);
1076 InFlag = Chain.getValue(1);
1077 }
1078
Chris Lattnere56fef92007-02-25 06:40:16 +00001079 // Returns a chain & a flag for retval copy to use.
1080 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00001081 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001082 Ops.push_back(Chain);
1083 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +00001084
1085 // Add argument registers to the end of the list so that they are known live
1086 // into the call.
1087 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001088 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +00001089 RegsToPass[i].second.getValueType()));
1090
Evan Cheng84a041e2007-02-21 21:18:14 +00001091 // Add an implicit use GOT pointer in EBX.
1092 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1093 Subtarget->isPICStyleGOT())
1094 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1095
Nate Begeman7e5496d2006-02-17 00:03:04 +00001096 if (InFlag.Val)
1097 Ops.push_back(InFlag);
1098
1099 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001100 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001101 NodeTys, &Ops[0], Ops.size());
Nate Begeman7e5496d2006-02-17 00:03:04 +00001102 InFlag = Chain.getValue(1);
1103
Chris Lattnerd6b853ad2007-02-25 07:18:38 +00001104 // Returns a flag for retval copy to use.
1105 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001106 Ops.clear();
1107 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001108 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1109 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001110 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001111 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattnerba474f52007-02-25 09:10:05 +00001112 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001113
Chris Lattnerba474f52007-02-25 09:10:05 +00001114 // Handle result values, copying them out of physregs into vregs that we
1115 // return.
1116 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001117}
1118
Chris Lattner3066bec2007-02-28 06:10:12 +00001119
1120//===----------------------------------------------------------------------===//
1121// X86-64 C Calling Convention implementation
1122//===----------------------------------------------------------------------===//
1123
1124SDOperand
1125X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3066bec2007-02-28 06:10:12 +00001126 MachineFunction &MF = DAG.getMachineFunction();
1127 MachineFrameInfo *MFI = MF.getFrameInfo();
1128 SDOperand Root = Op.getOperand(0);
1129 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1130
1131 static const unsigned GPR64ArgRegs[] = {
1132 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1133 };
1134 static const unsigned XMMArgRegs[] = {
1135 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1136 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1137 };
1138
Chris Lattner227b6c52007-02-28 07:00:42 +00001139
1140 // Assign locations to all of the incoming arguments.
Chris Lattner3066bec2007-02-28 06:10:12 +00001141 SmallVector<CCValAssign, 16> ArgLocs;
1142 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1143 ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001144 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001145
1146 SmallVector<SDOperand, 8> ArgValues;
1147 unsigned LastVal = ~0U;
1148 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1149 CCValAssign &VA = ArgLocs[i];
1150 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1151 // places.
1152 assert(VA.getValNo() != LastVal &&
1153 "Don't support value assigned to multiple locs yet");
1154 LastVal = VA.getValNo();
1155
1156 if (VA.isRegLoc()) {
1157 MVT::ValueType RegVT = VA.getLocVT();
1158 TargetRegisterClass *RC;
1159 if (RegVT == MVT::i32)
1160 RC = X86::GR32RegisterClass;
1161 else if (RegVT == MVT::i64)
1162 RC = X86::GR64RegisterClass;
1163 else if (RegVT == MVT::f32)
1164 RC = X86::FR32RegisterClass;
1165 else if (RegVT == MVT::f64)
1166 RC = X86::FR64RegisterClass;
1167 else {
1168 assert(MVT::isVector(RegVT));
1169 RC = X86::VR128RegisterClass;
1170 }
Chris Lattner9c7e5e32007-03-02 05:12:29 +00001171
1172 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1173 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner3066bec2007-02-28 06:10:12 +00001174
1175 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1176 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1177 // right size.
1178 if (VA.getLocInfo() == CCValAssign::SExt)
1179 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1180 DAG.getValueType(VA.getValVT()));
1181 else if (VA.getLocInfo() == CCValAssign::ZExt)
1182 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1183 DAG.getValueType(VA.getValVT()));
1184
1185 if (VA.getLocInfo() != CCValAssign::Full)
1186 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1187
1188 ArgValues.push_back(ArgValue);
1189 } else {
1190 assert(VA.isMemLoc());
1191
1192 // Create the nodes corresponding to a load from this parameter slot.
1193 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1194 VA.getLocMemOffset());
1195 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1196 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1197 }
1198 }
1199
1200 unsigned StackSize = CCInfo.getNextStackOffset();
1201
1202 // If the function takes variable number of arguments, make a frame index for
1203 // the start of the first vararg value... for expansion of llvm.va_start.
1204 if (isVarArg) {
1205 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1206 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1207
1208 // For X86-64, if there are vararg parameters that are passed via
1209 // registers, then we must store them to their spots on the stack so they
1210 // may be loaded by deferencing the result of va_next.
1211 VarArgsGPOffset = NumIntRegs * 8;
1212 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1213 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1214 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1215
1216 // Store the integer parameter registers.
1217 SmallVector<SDOperand, 8> MemOps;
1218 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1219 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1220 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1221 for (; NumIntRegs != 6; ++NumIntRegs) {
1222 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1223 X86::GR64RegisterClass);
1224 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1225 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1226 MemOps.push_back(Store);
1227 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1228 DAG.getConstant(8, getPointerTy()));
1229 }
1230
1231 // Now store the XMM (fp + vector) parameter registers.
1232 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1233 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1234 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1235 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1236 X86::VR128RegisterClass);
1237 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1238 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1239 MemOps.push_back(Store);
1240 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1241 DAG.getConstant(16, getPointerTy()));
1242 }
1243 if (!MemOps.empty())
1244 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1245 &MemOps[0], MemOps.size());
1246 }
1247
1248 ArgValues.push_back(Root);
1249
1250 ReturnAddrIndex = 0; // No return address slot generated yet.
1251 BytesToPopOnReturn = 0; // Callee pops nothing.
1252 BytesCallerReserves = StackSize;
1253
1254 // Return the new list of results.
1255 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1256 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1257}
1258
1259SDOperand
1260X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1261 unsigned CC) {
1262 SDOperand Chain = Op.getOperand(0);
1263 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1264 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1265 SDOperand Callee = Op.getOperand(4);
Chris Lattner227b6c52007-02-28 07:00:42 +00001266
1267 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner3066bec2007-02-28 06:10:12 +00001268 SmallVector<CCValAssign, 16> ArgLocs;
1269 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001270 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001271
1272 // Get a count of how many bytes are to be pushed on the stack.
1273 unsigned NumBytes = CCInfo.getNextStackOffset();
1274 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1275
1276 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1277 SmallVector<SDOperand, 8> MemOpChains;
1278
1279 SDOperand StackPtr;
1280
1281 // Walk the register/memloc assignments, inserting copies/loads.
1282 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1283 CCValAssign &VA = ArgLocs[i];
1284 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1285
1286 // Promote the value if needed.
1287 switch (VA.getLocInfo()) {
1288 default: assert(0 && "Unknown loc info!");
1289 case CCValAssign::Full: break;
1290 case CCValAssign::SExt:
1291 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1292 break;
1293 case CCValAssign::ZExt:
1294 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1295 break;
1296 case CCValAssign::AExt:
1297 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1298 break;
1299 }
1300
1301 if (VA.isRegLoc()) {
1302 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1303 } else {
1304 assert(VA.isMemLoc());
1305 if (StackPtr.Val == 0)
1306 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1307 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1308 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1309 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1310 }
1311 }
1312
1313 if (!MemOpChains.empty())
1314 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1315 &MemOpChains[0], MemOpChains.size());
1316
1317 // Build a sequence of copy-to-reg nodes chained together with token chain
1318 // and flag operands which copy the outgoing args into registers.
1319 SDOperand InFlag;
1320 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1321 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1322 InFlag);
1323 InFlag = Chain.getValue(1);
1324 }
1325
1326 if (isVarArg) {
1327 // From AMD64 ABI document:
1328 // For calls that may call functions that use varargs or stdargs
1329 // (prototype-less calls or calls to functions containing ellipsis (...) in
1330 // the declaration) %al is used as hidden argument to specify the number
1331 // of SSE registers used. The contents of %al do not need to match exactly
1332 // the number of registers, but must be an ubound on the number of SSE
1333 // registers used and is in the range 0 - 8 inclusive.
1334
1335 // Count the number of XMM registers allocated.
1336 static const unsigned XMMArgRegs[] = {
1337 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1338 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1339 };
1340 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1341
1342 Chain = DAG.getCopyToReg(Chain, X86::AL,
1343 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1344 InFlag = Chain.getValue(1);
1345 }
1346
1347 // If the callee is a GlobalAddress node (quite common, every direct call is)
1348 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1349 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1350 // We should use extra load for direct calls to dllimported functions in
1351 // non-JIT mode.
Evan Chenga1779b92007-03-14 22:11:11 +00001352 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001353 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1354 getTargetMachine(), true))
Chris Lattner3066bec2007-02-28 06:10:12 +00001355 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1356 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chenga1779b92007-03-14 22:11:11 +00001357 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1358 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattner3066bec2007-02-28 06:10:12 +00001359
1360 // Returns a chain & a flag for retval copy to use.
1361 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1362 SmallVector<SDOperand, 8> Ops;
1363 Ops.push_back(Chain);
1364 Ops.push_back(Callee);
1365
1366 // Add argument registers to the end of the list so that they are known live
1367 // into the call.
1368 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1369 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1370 RegsToPass[i].second.getValueType()));
1371
1372 if (InFlag.Val)
1373 Ops.push_back(InFlag);
1374
1375 // FIXME: Do not generate X86ISD::TAILCALL for now.
1376 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1377 NodeTys, &Ops[0], Ops.size());
1378 InFlag = Chain.getValue(1);
1379
1380 // Returns a flag for retval copy to use.
1381 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1382 Ops.clear();
1383 Ops.push_back(Chain);
1384 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1385 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1386 Ops.push_back(InFlag);
1387 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1388 InFlag = Chain.getValue(1);
1389
1390 // Handle result values, copying them out of physregs into vregs that we
1391 // return.
1392 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1393}
1394
1395
1396//===----------------------------------------------------------------------===//
1397// Other Lowering Hooks
1398//===----------------------------------------------------------------------===//
1399
1400
Chris Lattner76ac0682005-11-15 00:40:23 +00001401SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1402 if (ReturnAddrIndex == 0) {
1403 // Set up a frame object for the return address.
1404 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001405 if (Subtarget->is64Bit())
1406 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1407 else
1408 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattner76ac0682005-11-15 00:40:23 +00001409 }
1410
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001411 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattner76ac0682005-11-15 00:40:23 +00001412}
1413
1414
1415
Evan Cheng45df7f82006-01-30 23:41:35 +00001416/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1417/// specific condition code. It returns a false if it cannot do a direct
Chris Lattner7a627672006-09-13 03:22:10 +00001418/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1419/// needed.
Evan Cheng78038292006-04-05 23:38:46 +00001420static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattner7a627672006-09-13 03:22:10 +00001421 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1422 SelectionDAG &DAG) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001423 X86CC = X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001424 if (!isFP) {
Chris Lattner971e3392006-09-13 17:04:54 +00001425 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1426 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1427 // X > -1 -> X == 0, jump !sign.
1428 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001429 X86CC = X86::COND_NS;
Chris Lattner971e3392006-09-13 17:04:54 +00001430 return true;
1431 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1432 // X < 0 -> X == 0, jump on sign.
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001433 X86CC = X86::COND_S;
Chris Lattner971e3392006-09-13 17:04:54 +00001434 return true;
1435 }
Chris Lattner7a627672006-09-13 03:22:10 +00001436 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001437
Evan Cheng172fce72006-01-06 00:43:03 +00001438 switch (SetCCOpcode) {
1439 default: break;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001440 case ISD::SETEQ: X86CC = X86::COND_E; break;
1441 case ISD::SETGT: X86CC = X86::COND_G; break;
1442 case ISD::SETGE: X86CC = X86::COND_GE; break;
1443 case ISD::SETLT: X86CC = X86::COND_L; break;
1444 case ISD::SETLE: X86CC = X86::COND_LE; break;
1445 case ISD::SETNE: X86CC = X86::COND_NE; break;
1446 case ISD::SETULT: X86CC = X86::COND_B; break;
1447 case ISD::SETUGT: X86CC = X86::COND_A; break;
1448 case ISD::SETULE: X86CC = X86::COND_BE; break;
1449 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001450 }
1451 } else {
1452 // On a floating point condition, the flags are set as follows:
1453 // ZF PF CF op
1454 // 0 | 0 | 0 | X > Y
1455 // 0 | 0 | 1 | X < Y
1456 // 1 | 0 | 0 | X == Y
1457 // 1 | 1 | 1 | unordered
Chris Lattner7a627672006-09-13 03:22:10 +00001458 bool Flip = false;
Evan Cheng172fce72006-01-06 00:43:03 +00001459 switch (SetCCOpcode) {
1460 default: break;
1461 case ISD::SETUEQ:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001462 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001463 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001464 case ISD::SETOGT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001465 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001466 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001467 case ISD::SETOGE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001468 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001469 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001470 case ISD::SETULT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001471 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001472 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001473 case ISD::SETULE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001474 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001475 case ISD::SETONE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001476 case ISD::SETNE: X86CC = X86::COND_NE; break;
1477 case ISD::SETUO: X86CC = X86::COND_P; break;
1478 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001479 }
Chris Lattner7a627672006-09-13 03:22:10 +00001480 if (Flip)
1481 std::swap(LHS, RHS);
Evan Cheng172fce72006-01-06 00:43:03 +00001482 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001483
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001484 return X86CC != X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001485}
1486
Evan Cheng339edad2006-01-11 00:33:36 +00001487/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1488/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001489/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001490static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001491 switch (X86CC) {
1492 default:
1493 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001494 case X86::COND_B:
1495 case X86::COND_BE:
1496 case X86::COND_E:
1497 case X86::COND_P:
1498 case X86::COND_A:
1499 case X86::COND_AE:
1500 case X86::COND_NE:
1501 case X86::COND_NP:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001502 return true;
1503 }
1504}
1505
Evan Chengc995b452006-04-06 23:23:56 +00001506/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001507/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001508static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1509 if (Op.getOpcode() == ISD::UNDEF)
1510 return true;
1511
1512 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001513 return (Val >= Low && Val < Hi);
1514}
1515
1516/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1517/// true if Op is undef or if its value equal to the specified value.
1518static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1519 if (Op.getOpcode() == ISD::UNDEF)
1520 return true;
1521 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001522}
1523
Evan Cheng68ad48b2006-03-22 18:59:22 +00001524/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1525/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1526bool X86::isPSHUFDMask(SDNode *N) {
1527 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1528
1529 if (N->getNumOperands() != 4)
1530 return false;
1531
1532 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001533 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001534 SDOperand Arg = N->getOperand(i);
1535 if (Arg.getOpcode() == ISD::UNDEF) continue;
1536 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1537 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001538 return false;
1539 }
1540
1541 return true;
1542}
1543
1544/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001545/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001546bool X86::isPSHUFHWMask(SDNode *N) {
1547 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1548
1549 if (N->getNumOperands() != 8)
1550 return false;
1551
1552 // Lower quadword copied in order.
1553 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001554 SDOperand Arg = N->getOperand(i);
1555 if (Arg.getOpcode() == ISD::UNDEF) continue;
1556 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1557 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001558 return false;
1559 }
1560
1561 // Upper quadword shuffled.
1562 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001563 SDOperand Arg = N->getOperand(i);
1564 if (Arg.getOpcode() == ISD::UNDEF) continue;
1565 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1566 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001567 if (Val < 4 || Val > 7)
1568 return false;
1569 }
1570
1571 return true;
1572}
1573
1574/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001575/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001576bool X86::isPSHUFLWMask(SDNode *N) {
1577 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1578
1579 if (N->getNumOperands() != 8)
1580 return false;
1581
1582 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001583 for (unsigned i = 4; i != 8; ++i)
1584 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001585 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001586
1587 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001588 for (unsigned i = 0; i != 4; ++i)
1589 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001590 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001591
1592 return true;
1593}
1594
Evan Chengd27fb3e2006-03-24 01:18:28 +00001595/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1596/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner35a08552007-02-25 07:10:00 +00001597static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001598 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001599
Evan Cheng60f0b892006-04-20 08:58:49 +00001600 unsigned Half = NumElems / 2;
1601 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001602 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng60f0b892006-04-20 08:58:49 +00001603 return false;
1604 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001605 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00001606 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001607
1608 return true;
1609}
1610
Evan Cheng60f0b892006-04-20 08:58:49 +00001611bool X86::isSHUFPMask(SDNode *N) {
1612 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001613 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001614}
1615
1616/// isCommutedSHUFP - Returns true if the shuffle mask is except
1617/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1618/// half elements to come from vector 1 (which would equal the dest.) and
1619/// the upper half to come from vector 2.
Chris Lattner35a08552007-02-25 07:10:00 +00001620static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1621 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001622
Chris Lattner35a08552007-02-25 07:10:00 +00001623 unsigned Half = NumOps / 2;
Evan Cheng60f0b892006-04-20 08:58:49 +00001624 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001625 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00001626 return false;
Chris Lattner35a08552007-02-25 07:10:00 +00001627 for (unsigned i = Half; i < NumOps; ++i)
1628 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng60f0b892006-04-20 08:58:49 +00001629 return false;
1630 return true;
1631}
1632
1633static bool isCommutedSHUFP(SDNode *N) {
1634 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001635 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001636}
1637
Evan Cheng2595a682006-03-24 02:58:06 +00001638/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1639/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1640bool X86::isMOVHLPSMask(SDNode *N) {
1641 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1642
Evan Cheng1a194a52006-03-28 06:50:32 +00001643 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001644 return false;
1645
Evan Cheng1a194a52006-03-28 06:50:32 +00001646 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001647 return isUndefOrEqual(N->getOperand(0), 6) &&
1648 isUndefOrEqual(N->getOperand(1), 7) &&
1649 isUndefOrEqual(N->getOperand(2), 2) &&
1650 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001651}
1652
Evan Cheng922e1912006-11-07 22:14:24 +00001653/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1654/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1655/// <2, 3, 2, 3>
1656bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1657 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1658
1659 if (N->getNumOperands() != 4)
1660 return false;
1661
1662 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1663 return isUndefOrEqual(N->getOperand(0), 2) &&
1664 isUndefOrEqual(N->getOperand(1), 3) &&
1665 isUndefOrEqual(N->getOperand(2), 2) &&
1666 isUndefOrEqual(N->getOperand(3), 3);
1667}
1668
Evan Chengc995b452006-04-06 23:23:56 +00001669/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1670/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1671bool X86::isMOVLPMask(SDNode *N) {
1672 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1673
1674 unsigned NumElems = N->getNumOperands();
1675 if (NumElems != 2 && NumElems != 4)
1676 return false;
1677
Evan Chengac847262006-04-07 21:53:05 +00001678 for (unsigned i = 0; i < NumElems/2; ++i)
1679 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1680 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001681
Evan Chengac847262006-04-07 21:53:05 +00001682 for (unsigned i = NumElems/2; i < NumElems; ++i)
1683 if (!isUndefOrEqual(N->getOperand(i), i))
1684 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001685
1686 return true;
1687}
1688
1689/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001690/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1691/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001692bool X86::isMOVHPMask(SDNode *N) {
1693 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1694
1695 unsigned NumElems = N->getNumOperands();
1696 if (NumElems != 2 && NumElems != 4)
1697 return false;
1698
Evan Chengac847262006-04-07 21:53:05 +00001699 for (unsigned i = 0; i < NumElems/2; ++i)
1700 if (!isUndefOrEqual(N->getOperand(i), i))
1701 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001702
1703 for (unsigned i = 0; i < NumElems/2; ++i) {
1704 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001705 if (!isUndefOrEqual(Arg, i + NumElems))
1706 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001707 }
1708
1709 return true;
1710}
1711
Evan Cheng5df75882006-03-28 00:39:58 +00001712/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1713/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner35a08552007-02-25 07:10:00 +00001714bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1715 bool V2IsSplat = false) {
1716 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng5df75882006-03-28 00:39:58 +00001717 return false;
1718
Chris Lattner35a08552007-02-25 07:10:00 +00001719 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1720 SDOperand BitI = Elts[i];
1721 SDOperand BitI1 = Elts[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001722 if (!isUndefOrEqual(BitI, j))
1723 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001724 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00001725 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001726 return false;
1727 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00001728 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001729 return false;
1730 }
Evan Cheng5df75882006-03-28 00:39:58 +00001731 }
1732
1733 return true;
1734}
1735
Evan Cheng60f0b892006-04-20 08:58:49 +00001736bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1737 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001738 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001739}
1740
Evan Cheng2bc32802006-03-28 02:43:26 +00001741/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1742/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner35a08552007-02-25 07:10:00 +00001743bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1744 bool V2IsSplat = false) {
1745 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng2bc32802006-03-28 02:43:26 +00001746 return false;
1747
Chris Lattner35a08552007-02-25 07:10:00 +00001748 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1749 SDOperand BitI = Elts[i];
1750 SDOperand BitI1 = Elts[i+1];
1751 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengac847262006-04-07 21:53:05 +00001752 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001753 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00001754 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001755 return false;
1756 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00001757 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001758 return false;
1759 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001760 }
1761
1762 return true;
1763}
1764
Evan Cheng60f0b892006-04-20 08:58:49 +00001765bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1766 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001767 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001768}
1769
Evan Chengf3b52c82006-04-05 07:20:06 +00001770/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1771/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1772/// <0, 0, 1, 1>
1773bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1774 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1775
1776 unsigned NumElems = N->getNumOperands();
1777 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1778 return false;
1779
1780 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1781 SDOperand BitI = N->getOperand(i);
1782 SDOperand BitI1 = N->getOperand(i+1);
1783
Evan Chengac847262006-04-07 21:53:05 +00001784 if (!isUndefOrEqual(BitI, j))
1785 return false;
1786 if (!isUndefOrEqual(BitI1, j))
1787 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001788 }
1789
1790 return true;
1791}
1792
Evan Chenge8b51802006-04-21 01:05:10 +00001793/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1794/// specifies a shuffle of elements that is suitable for input to MOVSS,
1795/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner35a08552007-02-25 07:10:00 +00001796static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1797 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001798 return false;
1799
Chris Lattner35a08552007-02-25 07:10:00 +00001800 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001801 return false;
1802
Chris Lattner35a08552007-02-25 07:10:00 +00001803 for (unsigned i = 1; i < NumElts; ++i) {
1804 if (!isUndefOrEqual(Elts[i], i))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001805 return false;
1806 }
1807
1808 return true;
1809}
Evan Chengf3b52c82006-04-05 07:20:06 +00001810
Evan Chenge8b51802006-04-21 01:05:10 +00001811bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001812 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001813 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001814}
1815
Evan Chenge8b51802006-04-21 01:05:10 +00001816/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1817/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001818/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner35a08552007-02-25 07:10:00 +00001819static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1820 bool V2IsSplat = false,
Evan Cheng89c5d042006-09-08 01:50:06 +00001821 bool V2IsUndef = false) {
Chris Lattner35a08552007-02-25 07:10:00 +00001822 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001823 return false;
1824
1825 if (!isUndefOrEqual(Ops[0], 0))
1826 return false;
1827
Chris Lattner35a08552007-02-25 07:10:00 +00001828 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001829 SDOperand Arg = Ops[i];
Chris Lattner35a08552007-02-25 07:10:00 +00001830 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1831 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1832 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng89c5d042006-09-08 01:50:06 +00001833 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001834 }
1835
1836 return true;
1837}
1838
Evan Cheng89c5d042006-09-08 01:50:06 +00001839static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1840 bool V2IsUndef = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001841 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001842 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1843 V2IsSplat, V2IsUndef);
Evan Cheng60f0b892006-04-20 08:58:49 +00001844}
1845
Evan Cheng5d247f82006-04-14 21:59:03 +00001846/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1847/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1848bool X86::isMOVSHDUPMask(SDNode *N) {
1849 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1850
1851 if (N->getNumOperands() != 4)
1852 return false;
1853
1854 // Expect 1, 1, 3, 3
1855 for (unsigned i = 0; i < 2; ++i) {
1856 SDOperand Arg = N->getOperand(i);
1857 if (Arg.getOpcode() == ISD::UNDEF) continue;
1858 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1859 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1860 if (Val != 1) return false;
1861 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001862
1863 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001864 for (unsigned i = 2; i < 4; ++i) {
1865 SDOperand Arg = N->getOperand(i);
1866 if (Arg.getOpcode() == ISD::UNDEF) continue;
1867 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1868 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1869 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001870 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001871 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001872
Evan Cheng6222cf22006-04-15 05:37:34 +00001873 // Don't use movshdup if it can be done with a shufps.
1874 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001875}
1876
1877/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1878/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1879bool X86::isMOVSLDUPMask(SDNode *N) {
1880 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1881
1882 if (N->getNumOperands() != 4)
1883 return false;
1884
1885 // Expect 0, 0, 2, 2
1886 for (unsigned i = 0; i < 2; ++i) {
1887 SDOperand Arg = N->getOperand(i);
1888 if (Arg.getOpcode() == ISD::UNDEF) continue;
1889 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1890 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1891 if (Val != 0) return false;
1892 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001893
1894 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001895 for (unsigned i = 2; i < 4; ++i) {
1896 SDOperand Arg = N->getOperand(i);
1897 if (Arg.getOpcode() == ISD::UNDEF) continue;
1898 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1899 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1900 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001901 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001902 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001903
Evan Cheng6222cf22006-04-15 05:37:34 +00001904 // Don't use movshdup if it can be done with a shufps.
1905 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001906}
1907
Evan Chengd097e672006-03-22 02:53:00 +00001908/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1909/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001910static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001911 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1912
Evan Chengd097e672006-03-22 02:53:00 +00001913 // This is a splat operation if each element of the permute is the same, and
1914 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001915 unsigned NumElems = N->getNumOperands();
1916 SDOperand ElementBase;
1917 unsigned i = 0;
1918 for (; i != NumElems; ++i) {
1919 SDOperand Elt = N->getOperand(i);
Reid Spencerde46e482006-11-02 20:25:50 +00001920 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001921 ElementBase = Elt;
1922 break;
1923 }
1924 }
1925
1926 if (!ElementBase.Val)
1927 return false;
1928
1929 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001930 SDOperand Arg = N->getOperand(i);
1931 if (Arg.getOpcode() == ISD::UNDEF) continue;
1932 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001933 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001934 }
1935
1936 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001937 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00001938}
1939
Evan Cheng5022b342006-04-17 20:43:08 +00001940/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1941/// a splat of a single element and it's a 2 or 4 element mask.
1942bool X86::isSplatMask(SDNode *N) {
1943 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1944
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001945 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00001946 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1947 return false;
1948 return ::isSplatMask(N);
1949}
1950
Evan Chenge056dd52006-10-27 21:08:32 +00001951/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
1952/// specifies a splat of zero element.
1953bool X86::isSplatLoMask(SDNode *N) {
1954 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1955
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001956 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chenge056dd52006-10-27 21:08:32 +00001957 if (!isUndefOrEqual(N->getOperand(i), 0))
1958 return false;
1959 return true;
1960}
1961
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001962/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1963/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1964/// instructions.
1965unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001966 unsigned NumOperands = N->getNumOperands();
1967 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1968 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001969 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001970 unsigned Val = 0;
1971 SDOperand Arg = N->getOperand(NumOperands-i-1);
1972 if (Arg.getOpcode() != ISD::UNDEF)
1973 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001974 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001975 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001976 if (i != NumOperands - 1)
1977 Mask <<= Shift;
1978 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001979
1980 return Mask;
1981}
1982
Evan Chengb7fedff2006-03-29 23:07:14 +00001983/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1984/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1985/// instructions.
1986unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1987 unsigned Mask = 0;
1988 // 8 nodes, but we only care about the last 4.
1989 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001990 unsigned Val = 0;
1991 SDOperand Arg = N->getOperand(i);
1992 if (Arg.getOpcode() != ISD::UNDEF)
1993 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001994 Mask |= (Val - 4);
1995 if (i != 4)
1996 Mask <<= 2;
1997 }
1998
1999 return Mask;
2000}
2001
2002/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2003/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2004/// instructions.
2005unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2006 unsigned Mask = 0;
2007 // 8 nodes, but we only care about the first 4.
2008 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002009 unsigned Val = 0;
2010 SDOperand Arg = N->getOperand(i);
2011 if (Arg.getOpcode() != ISD::UNDEF)
2012 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002013 Mask |= Val;
2014 if (i != 0)
2015 Mask <<= 2;
2016 }
2017
2018 return Mask;
2019}
2020
Evan Cheng59a63552006-04-05 01:47:37 +00002021/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2022/// specifies a 8 element shuffle that can be broken into a pair of
2023/// PSHUFHW and PSHUFLW.
2024static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2025 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2026
2027 if (N->getNumOperands() != 8)
2028 return false;
2029
2030 // Lower quadword shuffled.
2031 for (unsigned i = 0; i != 4; ++i) {
2032 SDOperand Arg = N->getOperand(i);
2033 if (Arg.getOpcode() == ISD::UNDEF) continue;
2034 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2035 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2036 if (Val > 4)
2037 return false;
2038 }
2039
2040 // Upper quadword shuffled.
2041 for (unsigned i = 4; i != 8; ++i) {
2042 SDOperand Arg = N->getOperand(i);
2043 if (Arg.getOpcode() == ISD::UNDEF) continue;
2044 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2045 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2046 if (Val < 4 || Val > 7)
2047 return false;
2048 }
2049
2050 return true;
2051}
2052
Evan Chengc995b452006-04-06 23:23:56 +00002053/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2054/// values in ther permute mask.
Evan Chengc415c5b2006-10-25 21:49:50 +00002055static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2056 SDOperand &V2, SDOperand &Mask,
2057 SelectionDAG &DAG) {
Evan Chengc995b452006-04-06 23:23:56 +00002058 MVT::ValueType VT = Op.getValueType();
2059 MVT::ValueType MaskVT = Mask.getValueType();
2060 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2061 unsigned NumElems = Mask.getNumOperands();
Chris Lattner35a08552007-02-25 07:10:00 +00002062 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc995b452006-04-06 23:23:56 +00002063
2064 for (unsigned i = 0; i != NumElems; ++i) {
2065 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002066 if (Arg.getOpcode() == ISD::UNDEF) {
2067 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2068 continue;
2069 }
Evan Chengc995b452006-04-06 23:23:56 +00002070 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2071 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2072 if (Val < NumElems)
2073 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2074 else
2075 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2076 }
2077
Evan Chengc415c5b2006-10-25 21:49:50 +00002078 std::swap(V1, V2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002079 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc415c5b2006-10-25 21:49:50 +00002080 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chengc995b452006-04-06 23:23:56 +00002081}
2082
Evan Cheng7855e4d2006-04-19 20:35:22 +00002083/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2084/// match movhlps. The lower half elements should come from upper half of
2085/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002086/// half of V2 (and in order).
Evan Cheng7855e4d2006-04-19 20:35:22 +00002087static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2088 unsigned NumElems = Mask->getNumOperands();
2089 if (NumElems != 4)
2090 return false;
2091 for (unsigned i = 0, e = 2; i != e; ++i)
2092 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2093 return false;
2094 for (unsigned i = 2; i != 4; ++i)
2095 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2096 return false;
2097 return true;
2098}
2099
Evan Chengc995b452006-04-06 23:23:56 +00002100/// isScalarLoadToVector - Returns true if the node is a scalar load that
2101/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002102static inline bool isScalarLoadToVector(SDNode *N) {
2103 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2104 N = N->getOperand(0).Val;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002105 return ISD::isNON_EXTLoad(N);
Evan Chengc995b452006-04-06 23:23:56 +00002106 }
2107 return false;
2108}
2109
Evan Cheng7855e4d2006-04-19 20:35:22 +00002110/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2111/// match movlp{s|d}. The lower half elements should come from lower half of
2112/// V1 (and in order), and the upper half elements should come from the upper
2113/// half of V2 (and in order). And since V1 will become the source of the
2114/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Chenge646abb2006-10-09 21:39:25 +00002115static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002116 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng7855e4d2006-04-19 20:35:22 +00002117 return false;
Evan Chenge646abb2006-10-09 21:39:25 +00002118 // Is V2 is a vector load, don't do this transformation. We will try to use
2119 // load folding shufps op.
2120 if (ISD::isNON_EXTLoad(V2))
2121 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002122
Evan Cheng7855e4d2006-04-19 20:35:22 +00002123 unsigned NumElems = Mask->getNumOperands();
2124 if (NumElems != 2 && NumElems != 4)
2125 return false;
2126 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2127 if (!isUndefOrEqual(Mask->getOperand(i), i))
2128 return false;
2129 for (unsigned i = NumElems/2; i != NumElems; ++i)
2130 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2131 return false;
2132 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002133}
2134
Evan Cheng60f0b892006-04-20 08:58:49 +00002135/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2136/// all the same.
2137static bool isSplatVector(SDNode *N) {
2138 if (N->getOpcode() != ISD::BUILD_VECTOR)
2139 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002140
Evan Cheng60f0b892006-04-20 08:58:49 +00002141 SDOperand SplatValue = N->getOperand(0);
2142 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2143 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002144 return false;
2145 return true;
2146}
2147
Evan Cheng89c5d042006-09-08 01:50:06 +00002148/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2149/// to an undef.
2150static bool isUndefShuffle(SDNode *N) {
2151 if (N->getOpcode() != ISD::BUILD_VECTOR)
2152 return false;
2153
2154 SDOperand V1 = N->getOperand(0);
2155 SDOperand V2 = N->getOperand(1);
2156 SDOperand Mask = N->getOperand(2);
2157 unsigned NumElems = Mask.getNumOperands();
2158 for (unsigned i = 0; i != NumElems; ++i) {
2159 SDOperand Arg = Mask.getOperand(i);
2160 if (Arg.getOpcode() != ISD::UNDEF) {
2161 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2162 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2163 return false;
2164 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2165 return false;
2166 }
2167 }
2168 return true;
2169}
2170
Evan Cheng60f0b892006-04-20 08:58:49 +00002171/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2172/// that point to V2 points to its first element.
2173static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2174 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2175
2176 bool Changed = false;
Chris Lattner35a08552007-02-25 07:10:00 +00002177 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002178 unsigned NumElems = Mask.getNumOperands();
2179 for (unsigned i = 0; i != NumElems; ++i) {
2180 SDOperand Arg = Mask.getOperand(i);
2181 if (Arg.getOpcode() != ISD::UNDEF) {
2182 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2183 if (Val > NumElems) {
2184 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2185 Changed = true;
2186 }
2187 }
2188 MaskVec.push_back(Arg);
2189 }
2190
2191 if (Changed)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002192 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2193 &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002194 return Mask;
2195}
2196
Evan Chenge8b51802006-04-21 01:05:10 +00002197/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2198/// operation of specified width.
2199static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002200 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2201 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2202
Chris Lattner35a08552007-02-25 07:10:00 +00002203 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002204 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2205 for (unsigned i = 1; i != NumElems; ++i)
2206 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002207 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002208}
2209
Evan Cheng5022b342006-04-17 20:43:08 +00002210/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2211/// of specified width.
2212static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2213 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2214 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002215 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5022b342006-04-17 20:43:08 +00002216 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2217 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2218 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2219 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002220 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng5022b342006-04-17 20:43:08 +00002221}
2222
Evan Cheng60f0b892006-04-20 08:58:49 +00002223/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2224/// of specified width.
2225static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2226 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2227 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2228 unsigned Half = NumElems/2;
Chris Lattner35a08552007-02-25 07:10:00 +00002229 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002230 for (unsigned i = 0; i != Half; ++i) {
2231 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2232 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2233 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002234 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002235}
2236
Evan Chenge8b51802006-04-21 01:05:10 +00002237/// getZeroVector - Returns a vector of specified type with all zero elements.
2238///
2239static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2240 assert(MVT::isVector(VT) && "Expected a vector type");
2241 unsigned NumElems = getVectorNumElements(VT);
2242 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2243 bool isFP = MVT::isFloatingPoint(EVT);
2244 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002245 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002246 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
Evan Chenge8b51802006-04-21 01:05:10 +00002247}
2248
Evan Cheng5022b342006-04-17 20:43:08 +00002249/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2250///
2251static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2252 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002253 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002254 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002255 unsigned NumElems = Mask.getNumOperands();
2256 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002257 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002258 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002259 NumElems >>= 1;
2260 }
2261 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2262
2263 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002264 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002265 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002266 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002267 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2268}
2269
Evan Chenge8b51802006-04-21 01:05:10 +00002270/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2271/// constant +0.0.
2272static inline bool isZeroNode(SDOperand Elt) {
2273 return ((isa<ConstantSDNode>(Elt) &&
2274 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2275 (isa<ConstantFPSDNode>(Elt) &&
2276 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2277}
2278
Evan Cheng14215c32006-04-21 23:03:30 +00002279/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2280/// vector and zero or undef vector.
2281static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002282 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002283 bool isZero, SelectionDAG &DAG) {
2284 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002285 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2286 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2287 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002288 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Chenge8b51802006-04-21 01:05:10 +00002289 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002290 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2291 &MaskVec[0], MaskVec.size());
Evan Cheng14215c32006-04-21 23:03:30 +00002292 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002293}
2294
Evan Chengb0461082006-04-24 18:01:45 +00002295/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2296///
2297static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2298 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002299 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002300 if (NumNonZero > 8)
2301 return SDOperand();
2302
2303 SDOperand V(0, 0);
2304 bool First = true;
2305 for (unsigned i = 0; i < 16; ++i) {
2306 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2307 if (ThisIsNonZero && First) {
2308 if (NumZero)
2309 V = getZeroVector(MVT::v8i16, DAG);
2310 else
2311 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2312 First = false;
2313 }
2314
2315 if ((i & 1) != 0) {
2316 SDOperand ThisElt(0, 0), LastElt(0, 0);
2317 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2318 if (LastIsNonZero) {
2319 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2320 }
2321 if (ThisIsNonZero) {
2322 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2323 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2324 ThisElt, DAG.getConstant(8, MVT::i8));
2325 if (LastIsNonZero)
2326 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2327 } else
2328 ThisElt = LastElt;
2329
2330 if (ThisElt.Val)
2331 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002332 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00002333 }
2334 }
2335
2336 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2337}
2338
Bill Wendlingd551a182007-03-22 18:42:45 +00002339/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengb0461082006-04-24 18:01:45 +00002340///
2341static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2342 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002343 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002344 if (NumNonZero > 4)
2345 return SDOperand();
2346
2347 SDOperand V(0, 0);
2348 bool First = true;
2349 for (unsigned i = 0; i < 8; ++i) {
2350 bool isNonZero = (NonZeros & (1 << i)) != 0;
2351 if (isNonZero) {
2352 if (First) {
2353 if (NumZero)
2354 V = getZeroVector(MVT::v8i16, DAG);
2355 else
2356 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2357 First = false;
2358 }
2359 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002360 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00002361 }
2362 }
2363
2364 return V;
2365}
2366
Evan Chenga9467aa2006-04-25 20:13:52 +00002367SDOperand
2368X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2369 // All zero's are handled with pxor.
2370 if (ISD::isBuildVectorAllZeros(Op.Val))
2371 return Op;
2372
2373 // All one's are handled with pcmpeqd.
2374 if (ISD::isBuildVectorAllOnes(Op.Val))
2375 return Op;
2376
2377 MVT::ValueType VT = Op.getValueType();
2378 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2379 unsigned EVTBits = MVT::getSizeInBits(EVT);
2380
2381 unsigned NumElems = Op.getNumOperands();
2382 unsigned NumZero = 0;
2383 unsigned NumNonZero = 0;
2384 unsigned NonZeros = 0;
2385 std::set<SDOperand> Values;
2386 for (unsigned i = 0; i < NumElems; ++i) {
2387 SDOperand Elt = Op.getOperand(i);
2388 if (Elt.getOpcode() != ISD::UNDEF) {
2389 Values.insert(Elt);
2390 if (isZeroNode(Elt))
2391 NumZero++;
2392 else {
2393 NonZeros |= (1 << i);
2394 NumNonZero++;
2395 }
2396 }
2397 }
2398
2399 if (NumNonZero == 0)
2400 // Must be a mix of zero and undef. Return a zero vector.
2401 return getZeroVector(VT, DAG);
2402
2403 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2404 if (Values.size() == 1)
2405 return SDOperand();
2406
2407 // Special case for single non-zero element.
Evan Cheng798b3062006-10-25 20:48:19 +00002408 if (NumNonZero == 1) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002409 unsigned Idx = CountTrailingZeros_32(NonZeros);
2410 SDOperand Item = Op.getOperand(Idx);
2411 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2412 if (Idx == 0)
2413 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2414 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2415 NumZero > 0, DAG);
2416
2417 if (EVTBits == 32) {
2418 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2419 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2420 DAG);
2421 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2422 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002423 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002424 for (unsigned i = 0; i < NumElems; i++)
2425 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002426 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2427 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002428 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2429 DAG.getNode(ISD::UNDEF, VT), Mask);
2430 }
2431 }
2432
Evan Cheng8c5766e2006-10-04 18:33:38 +00002433 // Let legalizer expand 2-wide build_vector's.
Evan Chenga9467aa2006-04-25 20:13:52 +00002434 if (EVTBits == 64)
2435 return SDOperand();
2436
2437 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002438 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002439 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2440 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00002441 if (V.Val) return V;
2442 }
2443
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002444 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002445 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2446 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00002447 if (V.Val) return V;
2448 }
2449
2450 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner35a08552007-02-25 07:10:00 +00002451 SmallVector<SDOperand, 8> V;
2452 V.resize(NumElems);
Evan Chenga9467aa2006-04-25 20:13:52 +00002453 if (NumElems == 4 && NumZero > 0) {
2454 for (unsigned i = 0; i < 4; ++i) {
2455 bool isZero = !(NonZeros & (1 << i));
2456 if (isZero)
2457 V[i] = getZeroVector(VT, DAG);
2458 else
2459 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2460 }
2461
2462 for (unsigned i = 0; i < 2; ++i) {
2463 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2464 default: break;
2465 case 0:
2466 V[i] = V[i*2]; // Must be a zero vector.
2467 break;
2468 case 1:
2469 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2470 getMOVLMask(NumElems, DAG));
2471 break;
2472 case 2:
2473 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2474 getMOVLMask(NumElems, DAG));
2475 break;
2476 case 3:
2477 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2478 getUnpacklMask(NumElems, DAG));
2479 break;
2480 }
2481 }
2482
Evan Cheng9fee4422006-05-16 07:21:53 +00002483 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002484 // clears the upper bits.
Evan Chenga9467aa2006-04-25 20:13:52 +00002485 // FIXME: we can do the same for v4f32 case when we know both parts of
2486 // the lower half come from scalar_to_vector (loadf32). We should do
2487 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng798b3062006-10-25 20:48:19 +00002488 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Chenga9467aa2006-04-25 20:13:52 +00002489 return V[0];
2490 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2491 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002492 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002493 bool Reverse = (NonZeros & 0x3) == 2;
2494 for (unsigned i = 0; i < 2; ++i)
2495 if (Reverse)
2496 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2497 else
2498 MaskVec.push_back(DAG.getConstant(i, EVT));
2499 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2500 for (unsigned i = 0; i < 2; ++i)
2501 if (Reverse)
2502 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2503 else
2504 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnered728e82006-08-11 17:38:39 +00002505 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2506 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002507 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2508 }
2509
2510 if (Values.size() > 2) {
2511 // Expand into a number of unpckl*.
2512 // e.g. for v4f32
2513 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2514 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2515 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2516 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2517 for (unsigned i = 0; i < NumElems; ++i)
2518 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2519 NumElems >>= 1;
2520 while (NumElems != 0) {
2521 for (unsigned i = 0; i < NumElems; ++i)
2522 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2523 UnpckMask);
2524 NumElems >>= 1;
2525 }
2526 return V[0];
2527 }
2528
2529 return SDOperand();
2530}
2531
2532SDOperand
2533X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2534 SDOperand V1 = Op.getOperand(0);
2535 SDOperand V2 = Op.getOperand(1);
2536 SDOperand PermMask = Op.getOperand(2);
2537 MVT::ValueType VT = Op.getValueType();
2538 unsigned NumElems = PermMask.getNumOperands();
2539 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2540 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Cheng949bcc92006-10-16 06:36:00 +00002541 bool V1IsSplat = false;
2542 bool V2IsSplat = false;
Evan Chenga9467aa2006-04-25 20:13:52 +00002543
Evan Cheng89c5d042006-09-08 01:50:06 +00002544 if (isUndefShuffle(Op.Val))
2545 return DAG.getNode(ISD::UNDEF, VT);
2546
Evan Chenga9467aa2006-04-25 20:13:52 +00002547 if (isSplatMask(PermMask.Val)) {
2548 if (NumElems <= 4) return Op;
2549 // Promote it to a v4i32 splat.
Evan Cheng798b3062006-10-25 20:48:19 +00002550 return PromoteSplat(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00002551 }
2552
Evan Cheng798b3062006-10-25 20:48:19 +00002553 if (X86::isMOVLMask(PermMask.Val))
2554 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002555
Evan Cheng798b3062006-10-25 20:48:19 +00002556 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2557 X86::isMOVSLDUPMask(PermMask.Val) ||
2558 X86::isMOVHLPSMask(PermMask.Val) ||
2559 X86::isMOVHPMask(PermMask.Val) ||
2560 X86::isMOVLPMask(PermMask.Val))
2561 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002562
Evan Cheng798b3062006-10-25 20:48:19 +00002563 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2564 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Chengc415c5b2006-10-25 21:49:50 +00002565 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00002566
Evan Chengc415c5b2006-10-25 21:49:50 +00002567 bool Commuted = false;
Evan Cheng798b3062006-10-25 20:48:19 +00002568 V1IsSplat = isSplatVector(V1.Val);
2569 V2IsSplat = isSplatVector(V2.Val);
2570 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Chengc415c5b2006-10-25 21:49:50 +00002571 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00002572 std::swap(V1IsSplat, V2IsSplat);
2573 std::swap(V1IsUndef, V2IsUndef);
Evan Chengc415c5b2006-10-25 21:49:50 +00002574 Commuted = true;
Evan Cheng798b3062006-10-25 20:48:19 +00002575 }
2576
2577 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2578 if (V2IsUndef) return V1;
Evan Chengc415c5b2006-10-25 21:49:50 +00002579 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00002580 if (V2IsSplat) {
2581 // V2 is a splat, so the mask may be malformed. That is, it may point
2582 // to any V2 element. The instruction selectior won't like this. Get
2583 // a corrected mask and commute to form a proper MOVS{S|D}.
2584 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2585 if (NewMask.Val != PermMask.Val)
2586 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002587 }
Evan Cheng798b3062006-10-25 20:48:19 +00002588 return Op;
Evan Cheng949bcc92006-10-16 06:36:00 +00002589 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002590
Evan Cheng949bcc92006-10-16 06:36:00 +00002591 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2592 X86::isUNPCKLMask(PermMask.Val) ||
2593 X86::isUNPCKHMask(PermMask.Val))
2594 return Op;
Evan Cheng8c5766e2006-10-04 18:33:38 +00002595
Evan Cheng798b3062006-10-25 20:48:19 +00002596 if (V2IsSplat) {
2597 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002598 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng798b3062006-10-25 20:48:19 +00002599 // new vector_shuffle with the corrected mask.
2600 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2601 if (NewMask.Val != PermMask.Val) {
2602 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2603 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2604 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2605 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2606 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2607 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002608 }
2609 }
2610 }
2611
2612 // Normalize the node to match x86 shuffle ops if needed
Evan Chengc415c5b2006-10-25 21:49:50 +00002613 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2614 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2615
2616 if (Commuted) {
2617 // Commute is back and try unpck* again.
2618 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2619 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2620 X86::isUNPCKLMask(PermMask.Val) ||
2621 X86::isUNPCKHMask(PermMask.Val))
2622 return Op;
2623 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002624
2625 // If VT is integer, try PSHUF* first, then SHUFP*.
2626 if (MVT::isInteger(VT)) {
2627 if (X86::isPSHUFDMask(PermMask.Val) ||
2628 X86::isPSHUFHWMask(PermMask.Val) ||
2629 X86::isPSHUFLWMask(PermMask.Val)) {
2630 if (V2.getOpcode() != ISD::UNDEF)
2631 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2632 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2633 return Op;
2634 }
2635
2636 if (X86::isSHUFPMask(PermMask.Val))
2637 return Op;
2638
2639 // Handle v8i16 shuffle high / low shuffle node pair.
2640 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2641 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2642 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002643 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002644 for (unsigned i = 0; i != 4; ++i)
2645 MaskVec.push_back(PermMask.getOperand(i));
2646 for (unsigned i = 4; i != 8; ++i)
2647 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnered728e82006-08-11 17:38:39 +00002648 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2649 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002650 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2651 MaskVec.clear();
2652 for (unsigned i = 0; i != 4; ++i)
2653 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2654 for (unsigned i = 4; i != 8; ++i)
2655 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnered728e82006-08-11 17:38:39 +00002656 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002657 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2658 }
2659 } else {
2660 // Floating point cases in the other order.
2661 if (X86::isSHUFPMask(PermMask.Val))
2662 return Op;
2663 if (X86::isPSHUFDMask(PermMask.Val) ||
2664 X86::isPSHUFHWMask(PermMask.Val) ||
2665 X86::isPSHUFLWMask(PermMask.Val)) {
2666 if (V2.getOpcode() != ISD::UNDEF)
2667 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2668 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2669 return Op;
2670 }
2671 }
2672
2673 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002674 MVT::ValueType MaskVT = PermMask.getValueType();
2675 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002676 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng3cd43622006-04-28 07:03:38 +00002677 Locs.reserve(NumElems);
Chris Lattner35a08552007-02-25 07:10:00 +00002678 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2679 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng3cd43622006-04-28 07:03:38 +00002680 unsigned NumHi = 0;
2681 unsigned NumLo = 0;
2682 // If no more than two elements come from either vector. This can be
2683 // implemented with two shuffles. First shuffle gather the elements.
2684 // The second shuffle, which takes the first shuffle as both of its
2685 // vector operands, put the elements into the right order.
2686 for (unsigned i = 0; i != NumElems; ++i) {
2687 SDOperand Elt = PermMask.getOperand(i);
2688 if (Elt.getOpcode() == ISD::UNDEF) {
2689 Locs[i] = std::make_pair(-1, -1);
2690 } else {
2691 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2692 if (Val < NumElems) {
2693 Locs[i] = std::make_pair(0, NumLo);
2694 Mask1[NumLo] = Elt;
2695 NumLo++;
2696 } else {
2697 Locs[i] = std::make_pair(1, NumHi);
2698 if (2+NumHi < NumElems)
2699 Mask1[2+NumHi] = Elt;
2700 NumHi++;
2701 }
2702 }
2703 }
2704 if (NumLo <= 2 && NumHi <= 2) {
2705 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002706 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2707 &Mask1[0], Mask1.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00002708 for (unsigned i = 0; i != NumElems; ++i) {
2709 if (Locs[i].first == -1)
2710 continue;
2711 else {
2712 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2713 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2714 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2715 }
2716 }
2717
2718 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnered728e82006-08-11 17:38:39 +00002719 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2720 &Mask2[0], Mask2.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00002721 }
2722
2723 // Break it into (shuffle shuffle_hi, shuffle_lo).
2724 Locs.clear();
Chris Lattner35a08552007-02-25 07:10:00 +00002725 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2726 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2727 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Chenga9467aa2006-04-25 20:13:52 +00002728 unsigned MaskIdx = 0;
2729 unsigned LoIdx = 0;
2730 unsigned HiIdx = NumElems/2;
2731 for (unsigned i = 0; i != NumElems; ++i) {
2732 if (i == NumElems/2) {
2733 MaskPtr = &HiMask;
2734 MaskIdx = 1;
2735 LoIdx = 0;
2736 HiIdx = NumElems/2;
2737 }
2738 SDOperand Elt = PermMask.getOperand(i);
2739 if (Elt.getOpcode() == ISD::UNDEF) {
2740 Locs[i] = std::make_pair(-1, -1);
2741 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2742 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2743 (*MaskPtr)[LoIdx] = Elt;
2744 LoIdx++;
2745 } else {
2746 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2747 (*MaskPtr)[HiIdx] = Elt;
2748 HiIdx++;
2749 }
2750 }
2751
Chris Lattner3d826992006-05-16 06:45:34 +00002752 SDOperand LoShuffle =
2753 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002754 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2755 &LoMask[0], LoMask.size()));
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002756 SDOperand HiShuffle =
Chris Lattner3d826992006-05-16 06:45:34 +00002757 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002758 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2759 &HiMask[0], HiMask.size()));
Chris Lattner35a08552007-02-25 07:10:00 +00002760 SmallVector<SDOperand, 8> MaskOps;
Evan Chenga9467aa2006-04-25 20:13:52 +00002761 for (unsigned i = 0; i != NumElems; ++i) {
2762 if (Locs[i].first == -1) {
2763 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2764 } else {
2765 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2766 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2767 }
2768 }
2769 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnered728e82006-08-11 17:38:39 +00002770 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2771 &MaskOps[0], MaskOps.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002772 }
2773
2774 return SDOperand();
2775}
2776
2777SDOperand
2778X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2779 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2780 return SDOperand();
2781
2782 MVT::ValueType VT = Op.getValueType();
2783 // TODO: handle v16i8.
2784 if (MVT::getSizeInBits(VT) == 16) {
2785 // Transform it so it match pextrw which produces a 32-bit result.
2786 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2787 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2788 Op.getOperand(0), Op.getOperand(1));
2789 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2790 DAG.getValueType(VT));
2791 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2792 } else if (MVT::getSizeInBits(VT) == 32) {
2793 SDOperand Vec = Op.getOperand(0);
2794 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2795 if (Idx == 0)
2796 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002797 // SHUFPS the element to the lowest double word, then movss.
2798 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00002799 SmallVector<SDOperand, 8> IdxVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002800 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2801 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2802 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2803 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00002804 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2805 &IdxVec[0], IdxVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002806 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng922e1912006-11-07 22:14:24 +00002807 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002808 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00002809 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002810 } else if (MVT::getSizeInBits(VT) == 64) {
2811 SDOperand Vec = Op.getOperand(0);
2812 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2813 if (Idx == 0)
2814 return Op;
2815
2816 // UNPCKHPD the element to the lowest double word, then movsd.
2817 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2818 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2819 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00002820 SmallVector<SDOperand, 8> IdxVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002821 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2822 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00002823 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2824 &IdxVec[0], IdxVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002825 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2826 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2827 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00002828 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002829 }
2830
2831 return SDOperand();
2832}
2833
2834SDOperand
2835X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002836 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002837 // as its second argument.
2838 MVT::ValueType VT = Op.getValueType();
2839 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2840 SDOperand N0 = Op.getOperand(0);
2841 SDOperand N1 = Op.getOperand(1);
2842 SDOperand N2 = Op.getOperand(2);
2843 if (MVT::getSizeInBits(BaseVT) == 16) {
2844 if (N1.getValueType() != MVT::i32)
2845 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2846 if (N2.getValueType() != MVT::i32)
2847 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2848 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2849 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2850 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2851 if (Idx == 0) {
2852 // Use a movss.
2853 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2854 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2855 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002856 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002857 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2858 for (unsigned i = 1; i <= 3; ++i)
2859 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2860 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnered728e82006-08-11 17:38:39 +00002861 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2862 &MaskVec[0], MaskVec.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002863 } else {
2864 // Use two pinsrw instructions to insert a 32 bit value.
2865 Idx <<= 1;
2866 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002867 if (ISD::isNON_EXTLoad(N1.Val)) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002868 // Just load directly from f32mem to GR32.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002869 LoadSDNode *LD = cast<LoadSDNode>(N1);
2870 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2871 LD->getSrcValue(), LD->getSrcValueOffset());
Evan Chenga9467aa2006-04-25 20:13:52 +00002872 } else {
2873 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2874 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2875 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002876 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002877 }
2878 }
2879 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2880 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002881 DAG.getConstant(Idx, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002882 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2883 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002884 DAG.getConstant(Idx+1, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002885 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2886 }
2887 }
2888
2889 return SDOperand();
2890}
2891
2892SDOperand
2893X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2894 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2895 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2896}
2897
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002898// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Chenga9467aa2006-04-25 20:13:52 +00002899// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2900// one of the above mentioned nodes. It has to be wrapped because otherwise
2901// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2902// be used to form addressing mode. These wrapped nodes will be selected
2903// into MOV32ri.
2904SDOperand
2905X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2906 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng0b169222006-11-29 23:19:46 +00002907 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
2908 getPointerTy(),
2909 CP->getAlignment());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00002910 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002911 // With PIC, the address is actually $g + Offset.
2912 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2913 !Subtarget->isPICStyleRIPRel()) {
2914 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2915 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2916 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002917 }
2918
2919 return Result;
2920}
2921
2922SDOperand
2923X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2924 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng0b169222006-11-29 23:19:46 +00002925 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00002926 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002927 // With PIC, the address is actually $g + Offset.
2928 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2929 !Subtarget->isPICStyleRIPRel()) {
2930 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2931 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2932 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002933 }
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00002934
2935 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
2936 // load the value at address GV, not the value of GV itself. This means that
2937 // the GlobalAddress must be in the base or index register of the address, not
2938 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002939 // The same applies for external symbols during PIC codegen
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00002940 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
2941 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00002942
2943 return Result;
2944}
2945
2946SDOperand
2947X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2948 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Cheng0b169222006-11-29 23:19:46 +00002949 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00002950 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002951 // With PIC, the address is actually $g + Offset.
2952 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2953 !Subtarget->isPICStyleRIPRel()) {
2954 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2955 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2956 Result);
2957 }
2958
2959 return Result;
2960}
2961
2962SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
2963 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
2964 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
2965 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2966 // With PIC, the address is actually $g + Offset.
2967 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2968 !Subtarget->isPICStyleRIPRel()) {
2969 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2970 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2971 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002972 }
2973
2974 return Result;
2975}
2976
2977SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00002978 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2979 "Not an i64 shift!");
2980 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2981 SDOperand ShOpLo = Op.getOperand(0);
2982 SDOperand ShOpHi = Op.getOperand(1);
2983 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00002984 SDOperand Tmp1 = isSRA ?
2985 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
2986 DAG.getConstant(0, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00002987
2988 SDOperand Tmp2, Tmp3;
2989 if (Op.getOpcode() == ISD::SHL_PARTS) {
2990 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2991 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2992 } else {
2993 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00002994 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00002995 }
2996
Evan Cheng4259a0f2006-09-11 02:19:56 +00002997 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
2998 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
2999 DAG.getConstant(32, MVT::i8));
3000 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3001 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Cheng9c249c32006-01-09 18:33:28 +00003002
3003 SDOperand Hi, Lo;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003004 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00003005
Evan Cheng4259a0f2006-09-11 02:19:56 +00003006 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3007 SmallVector<SDOperand, 4> Ops;
Evan Cheng9c249c32006-01-09 18:33:28 +00003008 if (Op.getOpcode() == ISD::SHL_PARTS) {
3009 Ops.push_back(Tmp2);
3010 Ops.push_back(Tmp3);
3011 Ops.push_back(CC);
3012 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003013 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003014 InFlag = Hi.getValue(1);
3015
3016 Ops.clear();
3017 Ops.push_back(Tmp3);
3018 Ops.push_back(Tmp1);
3019 Ops.push_back(CC);
3020 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003021 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003022 } else {
3023 Ops.push_back(Tmp2);
3024 Ops.push_back(Tmp3);
3025 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00003026 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003027 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003028 InFlag = Lo.getValue(1);
3029
3030 Ops.clear();
3031 Ops.push_back(Tmp3);
3032 Ops.push_back(Tmp1);
3033 Ops.push_back(CC);
3034 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003035 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003036 }
3037
Evan Cheng4259a0f2006-09-11 02:19:56 +00003038 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00003039 Ops.clear();
3040 Ops.push_back(Lo);
3041 Ops.push_back(Hi);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003042 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003043}
Evan Cheng6305e502006-01-12 22:54:21 +00003044
Evan Chenga9467aa2006-04-25 20:13:52 +00003045SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3046 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3047 Op.getOperand(0).getValueType() >= MVT::i16 &&
3048 "Unknown SINT_TO_FP to lower!");
3049
3050 SDOperand Result;
3051 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3052 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3053 MachineFunction &MF = DAG.getMachineFunction();
3054 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3055 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chengdf9ac472006-10-05 23:01:46 +00003056 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Chengab51cf22006-10-13 21:14:26 +00003057 StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003058
3059 // Build the FILD
Chris Lattner35a08552007-02-25 07:10:00 +00003060 SDVTList Tys;
3061 if (X86ScalarSSE)
3062 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3063 else
3064 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3065 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003066 Ops.push_back(Chain);
3067 Ops.push_back(StackSlot);
3068 Ops.push_back(DAG.getValueType(SrcVT));
3069 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003070 Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003071
3072 if (X86ScalarSSE) {
3073 Chain = Result.getValue(1);
3074 SDOperand InFlag = Result.getValue(2);
3075
3076 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3077 // shouldn't be necessary except that RFP cannot be live across
3078 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003079 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003080 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003081 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner35a08552007-02-25 07:10:00 +00003082 Tys = DAG.getVTList(MVT::Other);
3083 SmallVector<SDOperand, 8> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003084 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003085 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003086 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003087 Ops.push_back(DAG.getValueType(Op.getValueType()));
3088 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003089 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003090 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattner76ac0682005-11-15 00:40:23 +00003091 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003092
Evan Chenga9467aa2006-04-25 20:13:52 +00003093 return Result;
3094}
3095
3096SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3097 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3098 "Unknown FP_TO_SINT to lower!");
3099 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3100 // stack slot.
3101 MachineFunction &MF = DAG.getMachineFunction();
3102 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3103 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3104 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3105
3106 unsigned Opc;
3107 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003108 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3109 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3110 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3111 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003112 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003113
Evan Chenga9467aa2006-04-25 20:13:52 +00003114 SDOperand Chain = DAG.getEntryNode();
3115 SDOperand Value = Op.getOperand(0);
3116 if (X86ScalarSSE) {
3117 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Chengab51cf22006-10-13 21:14:26 +00003118 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Chris Lattner35a08552007-02-25 07:10:00 +00003119 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3120 SDOperand Ops[] = {
3121 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3122 };
3123 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003124 Chain = Value.getValue(1);
3125 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3126 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3127 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003128
Evan Chenga9467aa2006-04-25 20:13:52 +00003129 // Build the FP_TO_INT*_IN_MEM
Chris Lattner35a08552007-02-25 07:10:00 +00003130 SDOperand Ops[] = { Chain, Value, StackSlot };
3131 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Cheng172fce72006-01-06 00:43:03 +00003132
Evan Chenga9467aa2006-04-25 20:13:52 +00003133 // Load the result.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003134 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003135}
3136
3137SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3138 MVT::ValueType VT = Op.getValueType();
3139 const Type *OpNTy = MVT::getTypeForValueType(VT);
3140 std::vector<Constant*> CV;
3141 if (VT == MVT::f64) {
3142 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3143 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3144 } else {
3145 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3146 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3147 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3148 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3149 }
3150 Constant *CS = ConstantStruct::get(CV);
3151 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner35a08552007-02-25 07:10:00 +00003152 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00003153 SmallVector<SDOperand, 3> Ops;
3154 Ops.push_back(DAG.getEntryNode());
3155 Ops.push_back(CPIdx);
3156 Ops.push_back(DAG.getSrcValue(NULL));
3157 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003158 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3159}
3160
3161SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3162 MVT::ValueType VT = Op.getValueType();
3163 const Type *OpNTy = MVT::getTypeForValueType(VT);
3164 std::vector<Constant*> CV;
3165 if (VT == MVT::f64) {
3166 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3167 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3168 } else {
3169 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3170 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3171 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3172 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3173 }
3174 Constant *CS = ConstantStruct::get(CV);
3175 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner35a08552007-02-25 07:10:00 +00003176 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00003177 SmallVector<SDOperand, 3> Ops;
3178 Ops.push_back(DAG.getEntryNode());
3179 Ops.push_back(CPIdx);
3180 Ops.push_back(DAG.getSrcValue(NULL));
3181 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003182 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3183}
3184
Evan Cheng4363e882007-01-05 07:55:56 +00003185SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng82241c82007-01-05 21:37:56 +00003186 SDOperand Op0 = Op.getOperand(0);
3187 SDOperand Op1 = Op.getOperand(1);
Evan Cheng4363e882007-01-05 07:55:56 +00003188 MVT::ValueType VT = Op.getValueType();
Evan Cheng82241c82007-01-05 21:37:56 +00003189 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng4363e882007-01-05 07:55:56 +00003190 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00003191
3192 // If second operand is smaller, extend it first.
3193 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3194 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3195 SrcVT = VT;
3196 }
3197
Evan Cheng4363e882007-01-05 07:55:56 +00003198 // First get the sign bit of second operand.
3199 std::vector<Constant*> CV;
3200 if (SrcVT == MVT::f64) {
3201 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3202 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3203 } else {
3204 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3205 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3206 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3207 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3208 }
3209 Constant *CS = ConstantStruct::get(CV);
3210 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnere56fef92007-02-25 06:40:16 +00003211 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
Evan Cheng4363e882007-01-05 07:55:56 +00003212 SmallVector<SDOperand, 3> Ops;
3213 Ops.push_back(DAG.getEntryNode());
3214 Ops.push_back(CPIdx);
3215 Ops.push_back(DAG.getSrcValue(NULL));
Evan Cheng82241c82007-01-05 21:37:56 +00003216 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3217 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng4363e882007-01-05 07:55:56 +00003218
3219 // Shift sign bit right or left if the two operands have different types.
3220 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3221 // Op0 is MVT::f32, Op1 is MVT::f64.
3222 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3223 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3224 DAG.getConstant(32, MVT::i32));
3225 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3226 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3227 DAG.getConstant(0, getPointerTy()));
Evan Cheng4363e882007-01-05 07:55:56 +00003228 }
3229
Evan Cheng82241c82007-01-05 21:37:56 +00003230 // Clear first operand sign bit.
3231 CV.clear();
3232 if (VT == MVT::f64) {
3233 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3234 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3235 } else {
3236 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3237 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3238 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3239 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3240 }
3241 CS = ConstantStruct::get(CV);
3242 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnere56fef92007-02-25 06:40:16 +00003243 Tys = DAG.getVTList(VT, MVT::Other);
Evan Cheng82241c82007-01-05 21:37:56 +00003244 Ops.clear();
3245 Ops.push_back(DAG.getEntryNode());
3246 Ops.push_back(CPIdx);
3247 Ops.push_back(DAG.getSrcValue(NULL));
3248 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3249 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3250
3251 // Or the value with the sign bit.
3252 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng4363e882007-01-05 07:55:56 +00003253}
3254
Evan Cheng4259a0f2006-09-11 02:19:56 +00003255SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3256 SDOperand Chain) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003257 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3258 SDOperand Cond;
Evan Cheng4259a0f2006-09-11 02:19:56 +00003259 SDOperand Op0 = Op.getOperand(0);
3260 SDOperand Op1 = Op.getOperand(1);
Evan Chenga9467aa2006-04-25 20:13:52 +00003261 SDOperand CC = Op.getOperand(2);
3262 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng694810c2006-10-12 19:12:56 +00003263 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3264 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003265 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Chenga9467aa2006-04-25 20:13:52 +00003266 unsigned X86CC;
Evan Chenga9467aa2006-04-25 20:13:52 +00003267
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003268 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattner7a627672006-09-13 03:22:10 +00003269 Op0, Op1, DAG)) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003270 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Cheng694810c2006-10-12 19:12:56 +00003271 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003272 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003273 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003274 }
3275
3276 assert(isFP && "Illegal integer SetCC!");
3277
3278 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Cheng694810c2006-10-12 19:12:56 +00003279 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003280
3281 switch (SetCCOpcode) {
3282 default: assert(false && "Illegal floating point SetCC!");
3283 case ISD::SETOEQ: { // !PF & ZF
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003284 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003285 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003286 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003287 Tmp1.getValue(1) };
Evan Cheng694810c2006-10-12 19:12:56 +00003288 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003289 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3290 }
3291 case ISD::SETUNE: { // PF | !ZF
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003292 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003293 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003294 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003295 Tmp1.getValue(1) };
Evan Cheng694810c2006-10-12 19:12:56 +00003296 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003297 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3298 }
Evan Chengc1583db2005-12-21 20:21:51 +00003299 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003300}
Evan Cheng45df7f82006-01-30 23:41:35 +00003301
Evan Chenga9467aa2006-04-25 20:13:52 +00003302SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003303 bool addTest = true;
3304 SDOperand Chain = DAG.getEntryNode();
3305 SDOperand Cond = Op.getOperand(0);
3306 SDOperand CC;
3307 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng944d1e92006-01-26 02:13:10 +00003308
Evan Cheng4259a0f2006-09-11 02:19:56 +00003309 if (Cond.getOpcode() == ISD::SETCC)
3310 Cond = LowerSETCC(Cond, DAG, Chain);
3311
3312 if (Cond.getOpcode() == X86ISD::SETCC) {
3313 CC = Cond.getOperand(0);
3314
Evan Chenga9467aa2006-04-25 20:13:52 +00003315 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng4259a0f2006-09-11 02:19:56 +00003316 // (since flag operand cannot be shared). Use it as the condition setting
3317 // operand in place of the X86ISD::SETCC.
3318 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Chenga9467aa2006-04-25 20:13:52 +00003319 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng4259a0f2006-09-11 02:19:56 +00003320 // pressure reason)?
3321 SDOperand Cmp = Cond.getOperand(1);
3322 unsigned Opc = Cmp.getOpcode();
3323 bool IllegalFPCMov = !X86ScalarSSE &&
3324 MVT::isFloatingPoint(Op.getValueType()) &&
3325 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3326 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3327 !IllegalFPCMov) {
3328 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3329 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3330 addTest = false;
3331 }
3332 }
Evan Cheng73a1ad92006-01-10 20:26:56 +00003333
Evan Chenga9467aa2006-04-25 20:13:52 +00003334 if (addTest) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003335 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003336 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3337 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng225a4d02005-12-17 01:21:05 +00003338 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003339
Evan Cheng4259a0f2006-09-11 02:19:56 +00003340 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3341 SmallVector<SDOperand, 4> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003342 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3343 // condition is true.
3344 Ops.push_back(Op.getOperand(2));
3345 Ops.push_back(Op.getOperand(1));
3346 Ops.push_back(CC);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003347 Ops.push_back(Cond.getValue(1));
3348 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003349}
Evan Cheng944d1e92006-01-26 02:13:10 +00003350
Evan Chenga9467aa2006-04-25 20:13:52 +00003351SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003352 bool addTest = true;
3353 SDOperand Chain = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003354 SDOperand Cond = Op.getOperand(1);
3355 SDOperand Dest = Op.getOperand(2);
3356 SDOperand CC;
Evan Cheng4259a0f2006-09-11 02:19:56 +00003357 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3358
Evan Chenga9467aa2006-04-25 20:13:52 +00003359 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng4259a0f2006-09-11 02:19:56 +00003360 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003361
3362 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003363 CC = Cond.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003364
Evan Cheng4259a0f2006-09-11 02:19:56 +00003365 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3366 // (since flag operand cannot be shared). Use it as the condition setting
3367 // operand in place of the X86ISD::SETCC.
3368 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3369 // to use a test instead of duplicating the X86ISD::CMP (for register
3370 // pressure reason)?
3371 SDOperand Cmp = Cond.getOperand(1);
3372 unsigned Opc = Cmp.getOpcode();
3373 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3374 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3375 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3376 addTest = false;
3377 }
3378 }
Evan Chengfb22e862006-01-13 01:03:02 +00003379
Evan Chenga9467aa2006-04-25 20:13:52 +00003380 if (addTest) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003381 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003382 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3383 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng6fc31042005-12-19 23:12:38 +00003384 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003385 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003386 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003387}
Evan Chengae986f12006-01-11 22:15:48 +00003388
Evan Cheng2a330942006-05-25 00:59:30 +00003389SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3390 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003391
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003392 if (Subtarget->is64Bit())
Chris Lattner7802f3e2007-02-25 09:06:15 +00003393 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng2a330942006-05-25 00:59:30 +00003394 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003395 switch (CallingConv) {
Chris Lattnerfc360392006-09-27 18:29:38 +00003396 default:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003397 assert(0 && "Unsupported calling convention");
Chris Lattnerfc360392006-09-27 18:29:38 +00003398 case CallingConv::Fast:
Chris Lattner3ed3be32007-02-28 06:05:16 +00003399 // TODO: Implement fastcc
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003400 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00003401 case CallingConv::C:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003402 case CallingConv::X86_StdCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00003403 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerfc360392006-09-27 18:29:38 +00003404 case CallingConv::X86_FastCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00003405 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003406 }
Evan Cheng2a330942006-05-25 00:59:30 +00003407}
3408
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00003409SDOperand X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3410 SelectionDAG &DAG) {
3411 // Get the inputs.
3412 SDOperand Chain = Op.getOperand(0);
3413 SDOperand Size = Op.getOperand(1);
3414 // FIXME: Ensure alignment here
3415
3416 TargetLowering::ArgListTy Args;
3417 TargetLowering::ArgListEntry Entry;
3418 MVT::ValueType IntPtr = getPointerTy();
3419 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3420 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3421
3422 Entry.Node = Size;
3423 Entry.Ty = IntPtrTy;
3424 Entry.isInReg = true; // Should pass in EAX
3425 Args.push_back(Entry);
3426 std::pair<SDOperand, SDOperand> CallResult =
3427 LowerCallTo(Chain, IntPtrTy, false, false, CallingConv::C, false,
3428 DAG.getExternalSymbol("_alloca", IntPtr), Args, DAG);
3429
3430 SDOperand SP = DAG.getCopyFromReg(CallResult.second, X86StackPtr, SPTy);
3431
3432 std::vector<MVT::ValueType> Tys;
3433 Tys.push_back(SPTy);
3434 Tys.push_back(MVT::Other);
3435 SDOperand Ops[2] = { SP, CallResult.second };
3436 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3437}
3438
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003439SDOperand
3440X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chengdc614c12006-06-06 23:30:24 +00003441 MachineFunction &MF = DAG.getMachineFunction();
3442 const Function* Fn = MF.getFunction();
3443 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +00003444 Subtarget->isTargetCygMing() &&
Evan Cheng0e14a562006-06-09 06:24:42 +00003445 Fn->getName() == "main")
Chris Lattnerff0598d2007-04-17 17:21:52 +00003446 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chengdc614c12006-06-06 23:30:24 +00003447
Evan Cheng17e734f2006-05-23 21:06:34 +00003448 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003449 if (Subtarget->is64Bit())
3450 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng17e734f2006-05-23 21:06:34 +00003451 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003452 switch(CC) {
Chris Lattnerfc360392006-09-27 18:29:38 +00003453 default:
3454 assert(0 && "Unsupported calling convention");
3455 case CallingConv::Fast:
Chris Lattner3ed3be32007-02-28 06:05:16 +00003456 // TODO: implement fastcc.
3457
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003458 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00003459 case CallingConv::C:
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003460 return LowerCCCArguments(Op, DAG);
Chris Lattnerfc360392006-09-27 18:29:38 +00003461 case CallingConv::X86_StdCall:
Chris Lattnerff0598d2007-04-17 17:21:52 +00003462 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00003463 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerfc360392006-09-27 18:29:38 +00003464 case CallingConv::X86_FastCall:
Chris Lattnerff0598d2007-04-17 17:21:52 +00003465 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner3ed3be32007-02-28 06:05:16 +00003466 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003467 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003468}
3469
Evan Chenga9467aa2006-04-25 20:13:52 +00003470SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3471 SDOperand InFlag(0, 0);
3472 SDOperand Chain = Op.getOperand(0);
3473 unsigned Align =
3474 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3475 if (Align == 0) Align = 1;
3476
3477 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3478 // If not DWORD aligned, call memset if size is less than the threshold.
3479 // It knows how to align to the right boundary first.
3480 if ((Align & 3) != 0 ||
3481 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3482 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003483 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00003484 TargetLowering::ArgListTy Args;
3485 TargetLowering::ArgListEntry Entry;
3486 Entry.Node = Op.getOperand(1);
3487 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00003488 Args.push_back(Entry);
Reid Spencere87b5e92007-01-03 17:24:59 +00003489 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencere63b6512006-12-31 05:55:36 +00003490 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3491 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00003492 Args.push_back(Entry);
3493 Entry.Node = Op.getOperand(3);
3494 Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00003495 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00003496 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00003497 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3498 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003499 }
Evan Chengd097e672006-03-22 02:53:00 +00003500
Evan Chenga9467aa2006-04-25 20:13:52 +00003501 MVT::ValueType AVT;
3502 SDOperand Count;
3503 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3504 unsigned BytesLeft = 0;
3505 bool TwoRepStos = false;
3506 if (ValC) {
3507 unsigned ValReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003508 uint64_t Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003509
Evan Chenga9467aa2006-04-25 20:13:52 +00003510 // If the value is a constant, then we can potentially use larger sets.
3511 switch (Align & 3) {
3512 case 2: // WORD aligned
3513 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00003514 ValReg = X86::AX;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003515 Val = (Val << 8) | Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00003516 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003517 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00003518 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003519 ValReg = X86::EAX;
Evan Chenga9467aa2006-04-25 20:13:52 +00003520 Val = (Val << 8) | Val;
3521 Val = (Val << 16) | Val;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003522 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3523 AVT = MVT::i64;
3524 ValReg = X86::RAX;
3525 Val = (Val << 32) | Val;
3526 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003527 break;
3528 default: // Byte aligned
3529 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00003530 ValReg = X86::AL;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003531 Count = Op.getOperand(3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003532 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003533 }
3534
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003535 if (AVT > MVT::i8) {
3536 if (I) {
3537 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3538 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3539 BytesLeft = I->getValue() % UBytes;
3540 } else {
3541 assert(AVT >= MVT::i32 &&
3542 "Do not use rep;stos if not at least DWORD aligned");
3543 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3544 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3545 TwoRepStos = true;
3546 }
3547 }
3548
Evan Chenga9467aa2006-04-25 20:13:52 +00003549 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3550 InFlag);
3551 InFlag = Chain.getValue(1);
3552 } else {
3553 AVT = MVT::i8;
3554 Count = Op.getOperand(3);
3555 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3556 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003557 }
Evan Chengb0461082006-04-24 18:01:45 +00003558
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003559 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3560 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003561 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003562 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3563 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003564 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003565
Chris Lattnere56fef92007-02-25 06:40:16 +00003566 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003567 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003568 Ops.push_back(Chain);
3569 Ops.push_back(DAG.getValueType(AVT));
3570 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003571 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengb0461082006-04-24 18:01:45 +00003572
Evan Chenga9467aa2006-04-25 20:13:52 +00003573 if (TwoRepStos) {
3574 InFlag = Chain.getValue(1);
3575 Count = Op.getOperand(3);
3576 MVT::ValueType CVT = Count.getValueType();
3577 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003578 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3579 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3580 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003581 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00003582 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003583 Ops.clear();
3584 Ops.push_back(Chain);
3585 Ops.push_back(DAG.getValueType(MVT::i8));
3586 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003587 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003588 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003589 // Issue stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00003590 SDOperand Value;
3591 unsigned Val = ValC->getValue() & 255;
3592 unsigned Offset = I->getValue() - BytesLeft;
3593 SDOperand DstAddr = Op.getOperand(1);
3594 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003595 if (BytesLeft >= 4) {
3596 Val = (Val << 8) | Val;
3597 Val = (Val << 16) | Val;
3598 Value = DAG.getConstant(Val, MVT::i32);
Evan Chengdf9ac472006-10-05 23:01:46 +00003599 Chain = DAG.getStore(Chain, Value,
3600 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3601 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003602 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003603 BytesLeft -= 4;
3604 Offset += 4;
3605 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003606 if (BytesLeft >= 2) {
3607 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Chengdf9ac472006-10-05 23:01:46 +00003608 Chain = DAG.getStore(Chain, Value,
3609 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3610 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003611 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003612 BytesLeft -= 2;
3613 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003614 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003615 if (BytesLeft == 1) {
3616 Value = DAG.getConstant(Val, MVT::i8);
Evan Chengdf9ac472006-10-05 23:01:46 +00003617 Chain = DAG.getStore(Chain, Value,
3618 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3619 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003620 NULL, 0);
Evan Cheng14215c32006-04-21 23:03:30 +00003621 }
Evan Cheng082c8782006-03-24 07:29:27 +00003622 }
Evan Chengebf10062006-04-03 20:53:28 +00003623
Evan Chenga9467aa2006-04-25 20:13:52 +00003624 return Chain;
3625}
Evan Chengebf10062006-04-03 20:53:28 +00003626
Evan Chenga9467aa2006-04-25 20:13:52 +00003627SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3628 SDOperand Chain = Op.getOperand(0);
3629 unsigned Align =
3630 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3631 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003632
Evan Chenga9467aa2006-04-25 20:13:52 +00003633 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3634 // If not DWORD aligned, call memcpy if size is less than the threshold.
3635 // It knows how to align to the right boundary first.
3636 if ((Align & 3) != 0 ||
3637 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3638 MVT::ValueType IntPtr = getPointerTy();
Reid Spencere63b6512006-12-31 05:55:36 +00003639 TargetLowering::ArgListTy Args;
3640 TargetLowering::ArgListEntry Entry;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00003641 Entry.Ty = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00003642 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3643 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3644 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00003645 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00003646 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00003647 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3648 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003649 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003650
3651 MVT::ValueType AVT;
3652 SDOperand Count;
3653 unsigned BytesLeft = 0;
3654 bool TwoRepMovs = false;
3655 switch (Align & 3) {
3656 case 2: // WORD aligned
3657 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00003658 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003659 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00003660 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003661 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3662 AVT = MVT::i64;
Evan Chenga9467aa2006-04-25 20:13:52 +00003663 break;
3664 default: // Byte aligned
3665 AVT = MVT::i8;
3666 Count = Op.getOperand(3);
3667 break;
3668 }
3669
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003670 if (AVT > MVT::i8) {
3671 if (I) {
3672 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3673 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3674 BytesLeft = I->getValue() % UBytes;
3675 } else {
3676 assert(AVT >= MVT::i32 &&
3677 "Do not use rep;movs if not at least DWORD aligned");
3678 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3679 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3680 TwoRepMovs = true;
3681 }
3682 }
3683
Evan Chenga9467aa2006-04-25 20:13:52 +00003684 SDOperand InFlag(0, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003685 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3686 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003687 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003688 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3689 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003690 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003691 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3692 Op.getOperand(2), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003693 InFlag = Chain.getValue(1);
3694
Chris Lattnere56fef92007-02-25 06:40:16 +00003695 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003696 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003697 Ops.push_back(Chain);
3698 Ops.push_back(DAG.getValueType(AVT));
3699 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003700 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003701
3702 if (TwoRepMovs) {
3703 InFlag = Chain.getValue(1);
3704 Count = Op.getOperand(3);
3705 MVT::ValueType CVT = Count.getValueType();
3706 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003707 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3708 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3709 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003710 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00003711 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003712 Ops.clear();
3713 Ops.push_back(Chain);
3714 Ops.push_back(DAG.getValueType(MVT::i8));
3715 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003716 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003717 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003718 // Issue loads and stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00003719 unsigned Offset = I->getValue() - BytesLeft;
3720 SDOperand DstAddr = Op.getOperand(1);
3721 MVT::ValueType DstVT = DstAddr.getValueType();
3722 SDOperand SrcAddr = Op.getOperand(2);
3723 MVT::ValueType SrcVT = SrcAddr.getValueType();
3724 SDOperand Value;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003725 if (BytesLeft >= 4) {
3726 Value = DAG.getLoad(MVT::i32, Chain,
3727 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3728 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003729 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003730 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003731 Chain = DAG.getStore(Chain, Value,
3732 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3733 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003734 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003735 BytesLeft -= 4;
3736 Offset += 4;
3737 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003738 if (BytesLeft >= 2) {
3739 Value = DAG.getLoad(MVT::i16, Chain,
3740 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3741 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003742 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003743 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003744 Chain = DAG.getStore(Chain, Value,
3745 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3746 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003747 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003748 BytesLeft -= 2;
3749 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003750 }
3751
Evan Chenga9467aa2006-04-25 20:13:52 +00003752 if (BytesLeft == 1) {
3753 Value = DAG.getLoad(MVT::i8, Chain,
3754 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3755 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003756 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003757 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003758 Chain = DAG.getStore(Chain, Value,
3759 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3760 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003761 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003762 }
Evan Chengcbffa462006-03-31 19:22:53 +00003763 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003764
3765 return Chain;
3766}
3767
3768SDOperand
3769X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere56fef92007-02-25 06:40:16 +00003770 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003771 SDOperand TheOp = Op.getOperand(0);
3772 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00003773 if (Subtarget->is64Bit()) {
3774 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3775 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3776 MVT::i64, Copy1.getValue(2));
3777 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3778 DAG.getConstant(32, MVT::i8));
Chris Lattner35a08552007-02-25 07:10:00 +00003779 SDOperand Ops[] = {
3780 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3781 };
Chris Lattnere56fef92007-02-25 06:40:16 +00003782
3783 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00003784 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00003785 }
Chris Lattner35a08552007-02-25 07:10:00 +00003786
3787 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3788 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3789 MVT::i32, Copy1.getValue(2));
3790 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3791 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3792 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003793}
3794
3795SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Chengab51cf22006-10-13 21:14:26 +00003796 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3797
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003798 if (!Subtarget->is64Bit()) {
3799 // vastart just stores the address of the VarArgsFrameIndex slot into the
3800 // memory location argument.
3801 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003802 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3803 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003804 }
3805
3806 // __va_list_tag:
3807 // gp_offset (0 - 6 * 8)
3808 // fp_offset (48 - 48 + 8 * 16)
3809 // overflow_arg_area (point to parameters coming in memory).
3810 // reg_save_area
Chris Lattner35a08552007-02-25 07:10:00 +00003811 SmallVector<SDOperand, 8> MemOps;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003812 SDOperand FIN = Op.getOperand(1);
3813 // Store gp_offset
Evan Chengdf9ac472006-10-05 23:01:46 +00003814 SDOperand Store = DAG.getStore(Op.getOperand(0),
3815 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00003816 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003817 MemOps.push_back(Store);
3818
3819 // Store fp_offset
3820 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3821 DAG.getConstant(4, getPointerTy()));
Evan Chengdf9ac472006-10-05 23:01:46 +00003822 Store = DAG.getStore(Op.getOperand(0),
3823 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00003824 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003825 MemOps.push_back(Store);
3826
3827 // Store ptr to overflow_arg_area
3828 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3829 DAG.getConstant(4, getPointerTy()));
3830 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003831 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3832 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003833 MemOps.push_back(Store);
3834
3835 // Store ptr to reg_save_area.
3836 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3837 DAG.getConstant(8, getPointerTy()));
3838 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003839 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3840 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003841 MemOps.push_back(Store);
3842 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003843}
3844
Evan Chengdeaea252007-03-02 23:16:35 +00003845SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
3846 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
3847 SDOperand Chain = Op.getOperand(0);
3848 SDOperand DstPtr = Op.getOperand(1);
3849 SDOperand SrcPtr = Op.getOperand(2);
3850 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
3851 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
3852
3853 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
3854 SrcSV->getValue(), SrcSV->getOffset());
3855 Chain = SrcPtr.getValue(1);
3856 for (unsigned i = 0; i < 3; ++i) {
3857 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
3858 SrcSV->getValue(), SrcSV->getOffset());
3859 Chain = Val.getValue(1);
3860 Chain = DAG.getStore(Chain, Val, DstPtr,
3861 DstSV->getValue(), DstSV->getOffset());
3862 if (i == 2)
3863 break;
3864 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
3865 DAG.getConstant(8, getPointerTy()));
3866 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
3867 DAG.getConstant(8, getPointerTy()));
3868 }
3869 return Chain;
3870}
3871
Evan Chenga9467aa2006-04-25 20:13:52 +00003872SDOperand
3873X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3874 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3875 switch (IntNo) {
3876 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003877 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003878 case Intrinsic::x86_sse_comieq_ss:
3879 case Intrinsic::x86_sse_comilt_ss:
3880 case Intrinsic::x86_sse_comile_ss:
3881 case Intrinsic::x86_sse_comigt_ss:
3882 case Intrinsic::x86_sse_comige_ss:
3883 case Intrinsic::x86_sse_comineq_ss:
3884 case Intrinsic::x86_sse_ucomieq_ss:
3885 case Intrinsic::x86_sse_ucomilt_ss:
3886 case Intrinsic::x86_sse_ucomile_ss:
3887 case Intrinsic::x86_sse_ucomigt_ss:
3888 case Intrinsic::x86_sse_ucomige_ss:
3889 case Intrinsic::x86_sse_ucomineq_ss:
3890 case Intrinsic::x86_sse2_comieq_sd:
3891 case Intrinsic::x86_sse2_comilt_sd:
3892 case Intrinsic::x86_sse2_comile_sd:
3893 case Intrinsic::x86_sse2_comigt_sd:
3894 case Intrinsic::x86_sse2_comige_sd:
3895 case Intrinsic::x86_sse2_comineq_sd:
3896 case Intrinsic::x86_sse2_ucomieq_sd:
3897 case Intrinsic::x86_sse2_ucomilt_sd:
3898 case Intrinsic::x86_sse2_ucomile_sd:
3899 case Intrinsic::x86_sse2_ucomigt_sd:
3900 case Intrinsic::x86_sse2_ucomige_sd:
3901 case Intrinsic::x86_sse2_ucomineq_sd: {
3902 unsigned Opc = 0;
3903 ISD::CondCode CC = ISD::SETCC_INVALID;
3904 switch (IntNo) {
3905 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003906 case Intrinsic::x86_sse_comieq_ss:
3907 case Intrinsic::x86_sse2_comieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003908 Opc = X86ISD::COMI;
3909 CC = ISD::SETEQ;
3910 break;
Evan Cheng78038292006-04-05 23:38:46 +00003911 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003912 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003913 Opc = X86ISD::COMI;
3914 CC = ISD::SETLT;
3915 break;
3916 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003917 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003918 Opc = X86ISD::COMI;
3919 CC = ISD::SETLE;
3920 break;
3921 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003922 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003923 Opc = X86ISD::COMI;
3924 CC = ISD::SETGT;
3925 break;
3926 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003927 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003928 Opc = X86ISD::COMI;
3929 CC = ISD::SETGE;
3930 break;
3931 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003932 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003933 Opc = X86ISD::COMI;
3934 CC = ISD::SETNE;
3935 break;
3936 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003937 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003938 Opc = X86ISD::UCOMI;
3939 CC = ISD::SETEQ;
3940 break;
3941 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003942 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003943 Opc = X86ISD::UCOMI;
3944 CC = ISD::SETLT;
3945 break;
3946 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003947 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003948 Opc = X86ISD::UCOMI;
3949 CC = ISD::SETLE;
3950 break;
3951 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003952 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003953 Opc = X86ISD::UCOMI;
3954 CC = ISD::SETGT;
3955 break;
3956 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003957 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003958 Opc = X86ISD::UCOMI;
3959 CC = ISD::SETGE;
3960 break;
3961 case Intrinsic::x86_sse_ucomineq_ss:
3962 case Intrinsic::x86_sse2_ucomineq_sd:
3963 Opc = X86ISD::UCOMI;
3964 CC = ISD::SETNE;
3965 break;
Evan Cheng78038292006-04-05 23:38:46 +00003966 }
Evan Cheng4259a0f2006-09-11 02:19:56 +00003967
Evan Chenga9467aa2006-04-25 20:13:52 +00003968 unsigned X86CC;
Chris Lattner7a627672006-09-13 03:22:10 +00003969 SDOperand LHS = Op.getOperand(1);
3970 SDOperand RHS = Op.getOperand(2);
3971 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003972
3973 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattner7a627672006-09-13 03:22:10 +00003974 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng4259a0f2006-09-11 02:19:56 +00003975 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
3976 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3977 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3978 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Chenga9467aa2006-04-25 20:13:52 +00003979 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003980 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003981 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003982}
Evan Cheng6af02632005-12-20 06:22:03 +00003983
Nate Begemaneda59972007-01-29 22:58:52 +00003984SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
3985 // Depths > 0 not supported yet!
3986 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3987 return SDOperand();
3988
3989 // Just load the return address
3990 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3991 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
3992}
3993
3994SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
3995 // Depths > 0 not supported yet!
3996 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3997 return SDOperand();
3998
3999 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4000 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4001 DAG.getConstant(4, getPointerTy()));
4002}
4003
Evan Chenga9467aa2006-04-25 20:13:52 +00004004/// LowerOperation - Provide custom lowering hooks for some operations.
4005///
4006SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4007 switch (Op.getOpcode()) {
4008 default: assert(0 && "Should not custom lower this!");
4009 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4010 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4011 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4012 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4013 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4014 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4015 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4016 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4017 case ISD::SHL_PARTS:
4018 case ISD::SRA_PARTS:
4019 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4020 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4021 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4022 case ISD::FABS: return LowerFABS(Op, DAG);
4023 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng4363e882007-01-05 07:55:56 +00004024 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004025 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Chenga9467aa2006-04-25 20:13:52 +00004026 case ISD::SELECT: return LowerSELECT(Op, DAG);
4027 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4028 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00004029 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004030 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00004031 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004032 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4033 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4034 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4035 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengdeaea252007-03-02 23:16:35 +00004036 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004037 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemaneda59972007-01-29 22:58:52 +00004038 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4039 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004040 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004041 }
Jim Laskey3796abe2007-02-21 22:54:50 +00004042 return SDOperand();
Evan Chenga9467aa2006-04-25 20:13:52 +00004043}
4044
Evan Cheng6af02632005-12-20 06:22:03 +00004045const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4046 switch (Opcode) {
4047 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00004048 case X86ISD::SHLD: return "X86ISD::SHLD";
4049 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00004050 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng4363e882007-01-05 07:55:56 +00004051 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng72d5c252006-01-31 22:28:30 +00004052 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng4363e882007-01-05 07:55:56 +00004053 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Cheng6305e502006-01-12 22:54:21 +00004054 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00004055 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00004056 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4057 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4058 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00004059 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00004060 case X86ISD::FST: return "X86ISD::FST";
4061 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00004062 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00004063 case X86ISD::CALL: return "X86ISD::CALL";
4064 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4065 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4066 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng78038292006-04-05 23:38:46 +00004067 case X86ISD::COMI: return "X86ISD::COMI";
4068 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00004069 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00004070 case X86ISD::CMOV: return "X86ISD::CMOV";
4071 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00004072 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00004073 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4074 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00004075 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5987cfb2006-07-07 08:33:52 +00004076 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
Evan Cheng5588de92006-02-18 00:15:05 +00004077 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00004078 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00004079 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00004080 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00004081 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng49683ba2006-11-10 21:43:37 +00004082 case X86ISD::FMAX: return "X86ISD::FMAX";
4083 case X86ISD::FMIN: return "X86ISD::FMIN";
Evan Cheng6af02632005-12-20 06:22:03 +00004084 }
4085}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004086
Chris Lattner1eb94d92007-03-30 23:15:24 +00004087// isLegalAddressingMode - Return true if the addressing mode represented
4088// by AM is legal for this target, for a load/store of the specified type.
4089bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4090 const Type *Ty) const {
4091 // X86 supports extremely general addressing modes.
4092
4093 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4094 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4095 return false;
4096
4097 if (AM.BaseGV) {
4098 // X86-64 only supports addr of globals in small code model.
4099 if (Subtarget->is64Bit() &&
4100 getTargetMachine().getCodeModel() != CodeModel::Small)
4101 return false;
4102
4103 // We can only fold this if we don't need a load either.
4104 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4105 return false;
4106 }
4107
4108 switch (AM.Scale) {
4109 case 0:
4110 case 1:
4111 case 2:
4112 case 4:
4113 case 8:
4114 // These scales always work.
4115 break;
4116 case 3:
4117 case 5:
4118 case 9:
4119 // These scales are formed with basereg+scalereg. Only accept if there is
4120 // no basereg yet.
4121 if (AM.HasBaseReg)
4122 return false;
4123 break;
4124 default: // Other stuff never works.
4125 return false;
4126 }
4127
4128 return true;
4129}
4130
4131
Evan Cheng02612422006-07-05 22:17:51 +00004132/// isShuffleMaskLegal - Targets can use this to indicate that they only
4133/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4134/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4135/// are assumed to be legal.
4136bool
4137X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4138 // Only do shuffles on 128-bit vector types for now.
4139 if (MVT::getSizeInBits(VT) == 64) return false;
4140 return (Mask.Val->getNumOperands() <= 4 ||
4141 isSplatMask(Mask.Val) ||
4142 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4143 X86::isUNPCKLMask(Mask.Val) ||
4144 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4145 X86::isUNPCKHMask(Mask.Val));
4146}
4147
4148bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4149 MVT::ValueType EVT,
4150 SelectionDAG &DAG) const {
4151 unsigned NumElts = BVOps.size();
4152 // Only do shuffles on 128-bit vector types for now.
4153 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4154 if (NumElts == 2) return true;
4155 if (NumElts == 4) {
Chris Lattner35a08552007-02-25 07:10:00 +00004156 return (isMOVLMask(&BVOps[0], 4) ||
4157 isCommutedMOVL(&BVOps[0], 4, true) ||
4158 isSHUFPMask(&BVOps[0], 4) ||
4159 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng02612422006-07-05 22:17:51 +00004160 }
4161 return false;
4162}
4163
4164//===----------------------------------------------------------------------===//
4165// X86 Scheduler Hooks
4166//===----------------------------------------------------------------------===//
4167
4168MachineBasicBlock *
4169X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4170 MachineBasicBlock *BB) {
Evan Cheng20350c42006-11-27 23:37:22 +00004171 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng02612422006-07-05 22:17:51 +00004172 switch (MI->getOpcode()) {
4173 default: assert(false && "Unexpected instr type to insert");
4174 case X86::CMOV_FR32:
4175 case X86::CMOV_FR64:
4176 case X86::CMOV_V4F32:
4177 case X86::CMOV_V2F64:
4178 case X86::CMOV_V2I64: {
4179 // To "insert" a SELECT_CC instruction, we actually have to insert the
4180 // diamond control-flow pattern. The incoming instruction knows the
4181 // destination vreg to set, the condition code register to branch on, the
4182 // true/false values to select between, and a branch opcode to use.
4183 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4184 ilist<MachineBasicBlock>::iterator It = BB;
4185 ++It;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004186
Evan Cheng02612422006-07-05 22:17:51 +00004187 // thisMBB:
4188 // ...
4189 // TrueVal = ...
4190 // cmpTY ccX, r1, r2
4191 // bCC copy1MBB
4192 // fallthrough --> copy0MBB
4193 MachineBasicBlock *thisMBB = BB;
4194 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4195 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004196 unsigned Opc =
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004197 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Cheng20350c42006-11-27 23:37:22 +00004198 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng02612422006-07-05 22:17:51 +00004199 MachineFunction *F = BB->getParent();
4200 F->getBasicBlockList().insert(It, copy0MBB);
4201 F->getBasicBlockList().insert(It, sinkMBB);
4202 // Update machine-CFG edges by first adding all successors of the current
4203 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004204 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng02612422006-07-05 22:17:51 +00004205 e = BB->succ_end(); i != e; ++i)
4206 sinkMBB->addSuccessor(*i);
4207 // Next, remove all successors of the current block, and add the true
4208 // and fallthrough blocks as its successors.
4209 while(!BB->succ_empty())
4210 BB->removeSuccessor(BB->succ_begin());
4211 BB->addSuccessor(copy0MBB);
4212 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004213
Evan Cheng02612422006-07-05 22:17:51 +00004214 // copy0MBB:
4215 // %FalseValue = ...
4216 // # fallthrough to sinkMBB
4217 BB = copy0MBB;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004218
Evan Cheng02612422006-07-05 22:17:51 +00004219 // Update machine-CFG edges
4220 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004221
Evan Cheng02612422006-07-05 22:17:51 +00004222 // sinkMBB:
4223 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4224 // ...
4225 BB = sinkMBB;
Evan Cheng20350c42006-11-27 23:37:22 +00004226 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng02612422006-07-05 22:17:51 +00004227 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4228 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4229
4230 delete MI; // The pseudo instruction is gone now.
4231 return BB;
4232 }
4233
4234 case X86::FP_TO_INT16_IN_MEM:
4235 case X86::FP_TO_INT32_IN_MEM:
4236 case X86::FP_TO_INT64_IN_MEM: {
4237 // Change the floating point control register to use "round towards zero"
4238 // mode when truncating to an integer value.
4239 MachineFunction *F = BB->getParent();
4240 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Cheng20350c42006-11-27 23:37:22 +00004241 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004242
4243 // Load the old value of the high byte of the control word...
4244 unsigned OldCW =
4245 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng20350c42006-11-27 23:37:22 +00004246 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004247
4248 // Set the high part to be round to zero...
Evan Cheng20350c42006-11-27 23:37:22 +00004249 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4250 .addImm(0xC7F);
Evan Cheng02612422006-07-05 22:17:51 +00004251
4252 // Reload the modified control word now...
Evan Cheng20350c42006-11-27 23:37:22 +00004253 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004254
4255 // Restore the memory image of control word to original value
Evan Cheng20350c42006-11-27 23:37:22 +00004256 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4257 .addReg(OldCW);
Evan Cheng02612422006-07-05 22:17:51 +00004258
4259 // Get the X86 opcode to use.
4260 unsigned Opc;
4261 switch (MI->getOpcode()) {
4262 default: assert(0 && "illegal opcode!");
4263 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4264 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4265 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4266 }
4267
4268 X86AddressMode AM;
4269 MachineOperand &Op = MI->getOperand(0);
4270 if (Op.isRegister()) {
4271 AM.BaseType = X86AddressMode::RegBase;
4272 AM.Base.Reg = Op.getReg();
4273 } else {
4274 AM.BaseType = X86AddressMode::FrameIndexBase;
4275 AM.Base.FrameIndex = Op.getFrameIndex();
4276 }
4277 Op = MI->getOperand(1);
4278 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004279 AM.Scale = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004280 Op = MI->getOperand(2);
4281 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004282 AM.IndexReg = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004283 Op = MI->getOperand(3);
4284 if (Op.isGlobalAddress()) {
4285 AM.GV = Op.getGlobal();
4286 } else {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004287 AM.Disp = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004288 }
Evan Cheng20350c42006-11-27 23:37:22 +00004289 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4290 .addReg(MI->getOperand(4).getReg());
Evan Cheng02612422006-07-05 22:17:51 +00004291
4292 // Reload the original control word now.
Evan Cheng20350c42006-11-27 23:37:22 +00004293 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004294
4295 delete MI; // The pseudo instruction is gone now.
4296 return BB;
4297 }
4298 }
4299}
4300
4301//===----------------------------------------------------------------------===//
4302// X86 Optimization Hooks
4303//===----------------------------------------------------------------------===//
4304
Nate Begeman8a77efe2006-02-16 21:11:51 +00004305void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4306 uint64_t Mask,
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004307 uint64_t &KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +00004308 uint64_t &KnownOne,
4309 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004310 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00004311 assert((Opc >= ISD::BUILTIN_OP_END ||
4312 Opc == ISD::INTRINSIC_WO_CHAIN ||
4313 Opc == ISD::INTRINSIC_W_CHAIN ||
4314 Opc == ISD::INTRINSIC_VOID) &&
4315 "Should use MaskedValueIsZero if you don't know whether Op"
4316 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004317
Evan Cheng6d196db2006-04-05 06:11:20 +00004318 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004319 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00004320 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004321 case X86ISD::SETCC:
Nate Begeman8a77efe2006-02-16 21:11:51 +00004322 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4323 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004324 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004325}
Chris Lattnerc642aa52006-01-31 19:43:35 +00004326
Evan Cheng5987cfb2006-07-07 08:33:52 +00004327/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4328/// element of the result of the vector shuffle.
4329static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4330 MVT::ValueType VT = N->getValueType(0);
4331 SDOperand PermMask = N->getOperand(2);
4332 unsigned NumElems = PermMask.getNumOperands();
4333 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4334 i %= NumElems;
4335 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4336 return (i == 0)
4337 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4338 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4339 SDOperand Idx = PermMask.getOperand(i);
4340 if (Idx.getOpcode() == ISD::UNDEF)
4341 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4342 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4343 }
4344 return SDOperand();
4345}
4346
4347/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4348/// node is a GlobalAddress + an offset.
4349static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Chengae1cd752006-11-30 21:55:46 +00004350 unsigned Opc = N->getOpcode();
Evan Cheng62cdc3f2006-12-05 04:01:03 +00004351 if (Opc == X86ISD::Wrapper) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004352 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4353 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4354 return true;
4355 }
Evan Chengae1cd752006-11-30 21:55:46 +00004356 } else if (Opc == ISD::ADD) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004357 SDOperand N1 = N->getOperand(0);
4358 SDOperand N2 = N->getOperand(1);
4359 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4360 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4361 if (V) {
4362 Offset += V->getSignExtended();
4363 return true;
4364 }
4365 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4366 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4367 if (V) {
4368 Offset += V->getSignExtended();
4369 return true;
4370 }
4371 }
4372 }
4373 return false;
4374}
4375
4376/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4377/// + Dist * Size.
4378static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4379 MachineFrameInfo *MFI) {
4380 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4381 return false;
4382
4383 SDOperand Loc = N->getOperand(1);
4384 SDOperand BaseLoc = Base->getOperand(1);
4385 if (Loc.getOpcode() == ISD::FrameIndex) {
4386 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4387 return false;
4388 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4389 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4390 int FS = MFI->getObjectSize(FI);
4391 int BFS = MFI->getObjectSize(BFI);
4392 if (FS != BFS || FS != Size) return false;
4393 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4394 } else {
4395 GlobalValue *GV1 = NULL;
4396 GlobalValue *GV2 = NULL;
4397 int64_t Offset1 = 0;
4398 int64_t Offset2 = 0;
4399 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4400 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4401 if (isGA1 && isGA2 && GV1 == GV2)
4402 return Offset1 == (Offset2 + Dist*Size);
4403 }
4404
4405 return false;
4406}
4407
Evan Cheng79cf9a52006-07-10 21:37:44 +00004408static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4409 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004410 GlobalValue *GV;
4411 int64_t Offset;
4412 if (isGAPlusOffset(Base, GV, Offset))
4413 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4414 else {
4415 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4416 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng79cf9a52006-07-10 21:37:44 +00004417 if (BFI < 0)
4418 // Fixed objects do not specify alignment, however the offsets are known.
4419 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4420 (MFI->getObjectOffset(BFI) % 16) == 0);
4421 else
4422 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng5987cfb2006-07-07 08:33:52 +00004423 }
4424 return false;
4425}
4426
4427
4428/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4429/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4430/// if the load addresses are consecutive, non-overlapping, and in the right
4431/// order.
Evan Cheng79cf9a52006-07-10 21:37:44 +00004432static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4433 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004434 MachineFunction &MF = DAG.getMachineFunction();
4435 MachineFrameInfo *MFI = MF.getFrameInfo();
4436 MVT::ValueType VT = N->getValueType(0);
4437 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4438 SDOperand PermMask = N->getOperand(2);
4439 int NumElems = (int)PermMask.getNumOperands();
4440 SDNode *Base = NULL;
4441 for (int i = 0; i < NumElems; ++i) {
4442 SDOperand Idx = PermMask.getOperand(i);
4443 if (Idx.getOpcode() == ISD::UNDEF) {
4444 if (!Base) return SDOperand();
4445 } else {
4446 SDOperand Arg =
4447 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004448 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng5987cfb2006-07-07 08:33:52 +00004449 return SDOperand();
4450 if (!Base)
4451 Base = Arg.Val;
4452 else if (!isConsecutiveLoad(Arg.Val, Base,
4453 i, MVT::getSizeInBits(EVT)/8,MFI))
4454 return SDOperand();
4455 }
4456 }
4457
Evan Cheng79cf9a52006-07-10 21:37:44 +00004458 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004459 if (isAlign16) {
4460 LoadSDNode *LD = cast<LoadSDNode>(Base);
4461 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4462 LD->getSrcValueOffset());
4463 } else {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004464 // Just use movups, it's shorter.
Chris Lattnere56fef92007-02-25 06:40:16 +00004465 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00004466 SmallVector<SDOperand, 3> Ops;
4467 Ops.push_back(Base->getOperand(0));
4468 Ops.push_back(Base->getOperand(1));
4469 Ops.push_back(Base->getOperand(2));
Evan Cheng5987cfb2006-07-07 08:33:52 +00004470 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengbd1c5a82006-08-11 09:08:15 +00004471 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
Evan Cheng5c68bba2006-08-11 07:35:45 +00004472 }
Evan Cheng5987cfb2006-07-07 08:33:52 +00004473}
4474
Chris Lattner9259b1e2006-10-04 06:57:07 +00004475/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4476static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4477 const X86Subtarget *Subtarget) {
4478 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004479
Chris Lattner9259b1e2006-10-04 06:57:07 +00004480 // If we have SSE[12] support, try to form min/max nodes.
4481 if (Subtarget->hasSSE2() &&
4482 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4483 if (Cond.getOpcode() == ISD::SETCC) {
4484 // Get the LHS/RHS of the select.
4485 SDOperand LHS = N->getOperand(1);
4486 SDOperand RHS = N->getOperand(2);
4487 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004488
Evan Cheng49683ba2006-11-10 21:43:37 +00004489 unsigned Opcode = 0;
Chris Lattner9259b1e2006-10-04 06:57:07 +00004490 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004491 switch (CC) {
4492 default: break;
4493 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4494 case ISD::SETULE:
4495 case ISD::SETLE:
4496 if (!UnsafeFPMath) break;
4497 // FALL THROUGH.
4498 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4499 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00004500 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004501 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004502
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004503 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4504 case ISD::SETUGT:
4505 case ISD::SETGT:
4506 if (!UnsafeFPMath) break;
4507 // FALL THROUGH.
4508 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4509 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00004510 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004511 break;
4512 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00004513 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004514 switch (CC) {
4515 default: break;
4516 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4517 case ISD::SETUGT:
4518 case ISD::SETGT:
4519 if (!UnsafeFPMath) break;
4520 // FALL THROUGH.
4521 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4522 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00004523 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004524 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004525
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004526 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4527 case ISD::SETULE:
4528 case ISD::SETLE:
4529 if (!UnsafeFPMath) break;
4530 // FALL THROUGH.
4531 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4532 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00004533 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004534 break;
4535 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00004536 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004537
Evan Cheng49683ba2006-11-10 21:43:37 +00004538 if (Opcode)
4539 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner9259b1e2006-10-04 06:57:07 +00004540 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004541
Chris Lattner9259b1e2006-10-04 06:57:07 +00004542 }
4543
4544 return SDOperand();
4545}
4546
4547
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004548SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng5987cfb2006-07-07 08:33:52 +00004549 DAGCombinerInfo &DCI) const {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004550 SelectionDAG &DAG = DCI.DAG;
4551 switch (N->getOpcode()) {
4552 default: break;
4553 case ISD::VECTOR_SHUFFLE:
Evan Cheng79cf9a52006-07-10 21:37:44 +00004554 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner9259b1e2006-10-04 06:57:07 +00004555 case ISD::SELECT:
4556 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng5987cfb2006-07-07 08:33:52 +00004557 }
4558
4559 return SDOperand();
4560}
4561
Evan Cheng02612422006-07-05 22:17:51 +00004562//===----------------------------------------------------------------------===//
4563// X86 Inline Assembly Support
4564//===----------------------------------------------------------------------===//
4565
Chris Lattner298ef372006-07-11 02:54:03 +00004566/// getConstraintType - Given a constraint letter, return the type of
4567/// constraint it is for this target.
4568X86TargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00004569X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4570 if (Constraint.size() == 1) {
4571 switch (Constraint[0]) {
4572 case 'A':
4573 case 'r':
4574 case 'R':
4575 case 'l':
4576 case 'q':
4577 case 'Q':
4578 case 'x':
4579 case 'Y':
4580 return C_RegisterClass;
4581 default:
4582 break;
4583 }
Chris Lattner298ef372006-07-11 02:54:03 +00004584 }
Chris Lattnerd6855142007-03-25 02:14:49 +00004585 return TargetLowering::getConstraintType(Constraint);
Chris Lattner298ef372006-07-11 02:54:03 +00004586}
4587
Chris Lattner44daa502006-10-31 20:13:11 +00004588/// isOperandValidForConstraint - Return the specified operand (possibly
4589/// modified) if the specified SDOperand is valid for the specified target
4590/// constraint letter, otherwise return null.
4591SDOperand X86TargetLowering::
4592isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4593 switch (Constraint) {
4594 default: break;
Devang Patelb38c2ec2007-03-17 00:13:28 +00004595 case 'I':
Chris Lattner03a643a2007-03-25 01:57:35 +00004596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4597 if (C->getValue() <= 31)
Devang Patelb38c2ec2007-03-17 00:13:28 +00004598 return Op;
Devang Patelb38c2ec2007-03-17 00:13:28 +00004599 }
Chris Lattner03a643a2007-03-25 01:57:35 +00004600 return SDOperand(0,0);
4601 case 'N':
4602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4603 if (C->getValue() <= 255)
4604 return Op;
4605 }
4606 return SDOperand(0,0);
Chris Lattner44daa502006-10-31 20:13:11 +00004607 case 'i':
4608 // Literal immediates are always ok.
4609 if (isa<ConstantSDNode>(Op)) return Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004610
Chris Lattner44daa502006-10-31 20:13:11 +00004611 // If we are in non-pic codegen mode, we allow the address of a global to
4612 // be used with 'i'.
4613 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4614 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4615 return SDOperand(0, 0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004616
Chris Lattner44daa502006-10-31 20:13:11 +00004617 if (GA->getOpcode() != ISD::TargetGlobalAddress)
4618 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4619 GA->getOffset());
4620 return Op;
4621 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004622
Chris Lattner44daa502006-10-31 20:13:11 +00004623 // Otherwise, not valid for this mode.
4624 return SDOperand(0, 0);
4625 }
4626 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4627}
4628
Chris Lattnerc642aa52006-01-31 19:43:35 +00004629std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00004630getRegClassForInlineAsmConstraint(const std::string &Constraint,
4631 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00004632 if (Constraint.size() == 1) {
4633 // FIXME: not handling fp-stack yet!
Chris Lattnerc642aa52006-01-31 19:43:35 +00004634 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattner298ef372006-07-11 02:54:03 +00004635 default: break; // Unknown constraint letter
4636 case 'A': // EAX/EDX
4637 if (VT == MVT::i32 || VT == MVT::i64)
4638 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4639 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004640 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4641 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004642 if (VT == MVT::i32)
4643 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4644 else if (VT == MVT::i16)
4645 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4646 else if (VT == MVT::i8)
4647 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4648 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004649 }
4650 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004651
Chris Lattner7ad77df2006-02-22 00:56:39 +00004652 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00004653}
Chris Lattner524129d2006-07-31 23:26:50 +00004654
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004655std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner524129d2006-07-31 23:26:50 +00004656X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4657 MVT::ValueType VT) const {
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004658 // First, see if this is a constraint that directly corresponds to an LLVM
4659 // register class.
4660 if (Constraint.size() == 1) {
4661 // GCC Constraint Letters
4662 switch (Constraint[0]) {
4663 default: break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00004664 case 'r': // GENERAL_REGS
4665 case 'R': // LEGACY_REGS
4666 case 'l': // INDEX_REGS
4667 if (VT == MVT::i64 && Subtarget->is64Bit())
4668 return std::make_pair(0U, X86::GR64RegisterClass);
4669 if (VT == MVT::i32)
4670 return std::make_pair(0U, X86::GR32RegisterClass);
4671 else if (VT == MVT::i16)
4672 return std::make_pair(0U, X86::GR16RegisterClass);
4673 else if (VT == MVT::i8)
4674 return std::make_pair(0U, X86::GR8RegisterClass);
4675 break;
Chris Lattner2805bce2007-04-12 04:14:49 +00004676 case 'y': // MMX_REGS if MMX allowed.
4677 if (!Subtarget->hasMMX()) break;
4678 return std::make_pair(0U, X86::VR64RegisterClass);
4679 break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00004680 case 'Y': // SSE_REGS if SSE2 allowed
4681 if (!Subtarget->hasSSE2()) break;
4682 // FALL THROUGH.
4683 case 'x': // SSE_REGS if SSE1 allowed
4684 if (!Subtarget->hasSSE1()) break;
4685
4686 switch (VT) {
4687 default: break;
4688 // Scalar SSE types.
4689 case MVT::f32:
4690 case MVT::i32:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004691 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00004692 case MVT::f64:
4693 case MVT::i64:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004694 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00004695 // Vector types.
4696 case MVT::Vector:
4697 case MVT::v16i8:
4698 case MVT::v8i16:
4699 case MVT::v4i32:
4700 case MVT::v2i64:
4701 case MVT::v4f32:
4702 case MVT::v2f64:
4703 return std::make_pair(0U, X86::VR128RegisterClass);
4704 }
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004705 break;
4706 }
4707 }
4708
Chris Lattner524129d2006-07-31 23:26:50 +00004709 // Use the default implementation in TargetLowering to convert the register
4710 // constraint into a member of a register class.
4711 std::pair<unsigned, const TargetRegisterClass*> Res;
4712 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerf6a69662006-10-31 19:42:44 +00004713
4714 // Not found as a standard register?
4715 if (Res.second == 0) {
4716 // GCC calls "st(0)" just plain "st".
4717 if (StringsEqualNoCase("{st}", Constraint)) {
4718 Res.first = X86::ST0;
4719 Res.second = X86::RSTRegisterClass;
4720 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004721
Chris Lattnerf6a69662006-10-31 19:42:44 +00004722 return Res;
4723 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004724
Chris Lattner524129d2006-07-31 23:26:50 +00004725 // Otherwise, check to see if this is a register class of the wrong value
4726 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4727 // turn into {ax},{dx}.
4728 if (Res.second->hasType(VT))
4729 return Res; // Correct type already, nothing to do.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004730
Chris Lattner524129d2006-07-31 23:26:50 +00004731 // All of the single-register GCC register classes map their values onto
4732 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4733 // really want an 8-bit or 32-bit register, map to the appropriate register
4734 // class and return the appropriate register.
4735 if (Res.second != X86::GR16RegisterClass)
4736 return Res;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004737
Chris Lattner524129d2006-07-31 23:26:50 +00004738 if (VT == MVT::i8) {
4739 unsigned DestReg = 0;
4740 switch (Res.first) {
4741 default: break;
4742 case X86::AX: DestReg = X86::AL; break;
4743 case X86::DX: DestReg = X86::DL; break;
4744 case X86::CX: DestReg = X86::CL; break;
4745 case X86::BX: DestReg = X86::BL; break;
4746 }
4747 if (DestReg) {
4748 Res.first = DestReg;
4749 Res.second = Res.second = X86::GR8RegisterClass;
4750 }
4751 } else if (VT == MVT::i32) {
4752 unsigned DestReg = 0;
4753 switch (Res.first) {
4754 default: break;
4755 case X86::AX: DestReg = X86::EAX; break;
4756 case X86::DX: DestReg = X86::EDX; break;
4757 case X86::CX: DestReg = X86::ECX; break;
4758 case X86::BX: DestReg = X86::EBX; break;
4759 case X86::SI: DestReg = X86::ESI; break;
4760 case X86::DI: DestReg = X86::EDI; break;
4761 case X86::BP: DestReg = X86::EBP; break;
4762 case X86::SP: DestReg = X86::ESP; break;
4763 }
4764 if (DestReg) {
4765 Res.first = DestReg;
4766 Res.second = Res.second = X86::GR32RegisterClass;
4767 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004768 } else if (VT == MVT::i64) {
4769 unsigned DestReg = 0;
4770 switch (Res.first) {
4771 default: break;
4772 case X86::AX: DestReg = X86::RAX; break;
4773 case X86::DX: DestReg = X86::RDX; break;
4774 case X86::CX: DestReg = X86::RCX; break;
4775 case X86::BX: DestReg = X86::RBX; break;
4776 case X86::SI: DestReg = X86::RSI; break;
4777 case X86::DI: DestReg = X86::RDI; break;
4778 case X86::BP: DestReg = X86::RBP; break;
4779 case X86::SP: DestReg = X86::RSP; break;
4780 }
4781 if (DestReg) {
4782 Res.first = DestReg;
4783 Res.second = Res.second = X86::GR64RegisterClass;
4784 }
Chris Lattner524129d2006-07-31 23:26:50 +00004785 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004786
Chris Lattner524129d2006-07-31 23:26:50 +00004787 return Res;
4788}