blob: a9ebd0dacadad548d9ad3e934a967ac9ba2674cf [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);
Chris Lattner78c358d2006-01-15 09:00:21 +0000240 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000241
Chris Lattner76ac0682005-11-15 00:40:23 +0000242 if (X86ScalarSSE) {
243 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000244 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
245 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000246
Evan Cheng72d5c252006-01-31 22:28:30 +0000247 // Use ANDPD to simulate FABS.
248 setOperationAction(ISD::FABS , MVT::f64, Custom);
249 setOperationAction(ISD::FABS , MVT::f32, Custom);
250
251 // Use XORP to simulate FNEG.
252 setOperationAction(ISD::FNEG , MVT::f64, Custom);
253 setOperationAction(ISD::FNEG , MVT::f32, Custom);
254
Evan Cheng4363e882007-01-05 07:55:56 +0000255 // Use ANDPD and ORPD to simulate FCOPYSIGN.
256 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
257 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
258
Evan Chengd8fba3a2006-02-02 00:28:23 +0000259 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000260 setOperationAction(ISD::FSIN , MVT::f64, Expand);
261 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000262 setOperationAction(ISD::FREM , MVT::f64, Expand);
263 setOperationAction(ISD::FSIN , MVT::f32, Expand);
264 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000265 setOperationAction(ISD::FREM , MVT::f32, Expand);
266
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000267 // Expand FP immediates into loads from the stack, except for the special
268 // cases we handle.
269 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
270 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000271 addLegalFPImmediate(+0.0); // xorps / xorpd
272 } else {
273 // Set up the FP register classes.
274 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000275
Evan Cheng4363e882007-01-05 07:55:56 +0000276 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
277 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
278 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000279
Chris Lattner76ac0682005-11-15 00:40:23 +0000280 if (!UnsafeFPMath) {
281 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
282 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
283 }
284
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000285 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000286 addLegalFPImmediate(+0.0); // FLD0
287 addLegalFPImmediate(+1.0); // FLD1
288 addLegalFPImmediate(-0.0); // FLD0/FCHS
289 addLegalFPImmediate(-1.0); // FLD1/FCHS
290 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000291
Evan Cheng19264272006-03-01 01:11:20 +0000292 // First set operation action for all vector types to expand. Then we
293 // will selectively turn on ones that can be effectively codegen'd.
294 for (unsigned VT = (unsigned)MVT::Vector + 1;
295 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
296 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
297 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000298 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
299 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000300 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000301 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
302 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
303 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
304 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
305 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
306 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000307 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000308 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000309 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000310 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000311 }
312
Evan Chengbc047222006-03-22 19:22:18 +0000313 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000314 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
315 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
316 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendling98d21042007-03-26 07:53:08 +0000317 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng9e252e32006-02-22 02:26:30 +0000318
Evan Cheng19264272006-03-01 01:11:20 +0000319 // FIXME: add MMX packed arithmetics
Bill Wendling97905b42007-03-07 05:43:18 +0000320
Bill Wendling6092ce22007-03-08 22:09:11 +0000321 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
322 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
323 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
324
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000325 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
326 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
327 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
328
Bill Wendlinge3103412007-03-15 21:24:36 +0000329 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
330 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
331
Bill Wendling144b8bb2007-03-16 09:44:46 +0000332 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000333 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000334 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000335 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
336 setOperationAction(ISD::AND, MVT::v2i32, Promote);
337 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
338 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000339
340 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000341 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000342 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000343 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
344 setOperationAction(ISD::OR, MVT::v2i32, Promote);
345 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
346 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000347
348 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000349 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000350 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000351 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
352 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
353 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
354 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000355
Bill Wendling6092ce22007-03-08 22:09:11 +0000356 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000357 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling6092ce22007-03-08 22:09:11 +0000358 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000359 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
360 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
361 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
362 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000363
Bill Wendling6dff51a2007-03-27 20:22:40 +0000364 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
365 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
366 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
367 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlingd551a182007-03-22 18:42:45 +0000368
369 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
370 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
371 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendling6dff51a2007-03-27 20:22:40 +0000372 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendlingad2db4a2007-03-28 00:57:11 +0000373
374 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
375 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000376 }
377
Evan Chengbc047222006-03-22 19:22:18 +0000378 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000379 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
380
Evan Chengbf3df772006-10-27 18:49:08 +0000381 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
382 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
383 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
384 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000385 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
386 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
387 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000388 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000389 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000390 }
391
Evan Chengbc047222006-03-22 19:22:18 +0000392 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000393 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
394 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
395 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
396 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
397 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
398
Evan Cheng617a6a82006-04-10 07:23:14 +0000399 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
400 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
401 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000402 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000403 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
404 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
405 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000406 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000407 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengbf3df772006-10-27 18:49:08 +0000408 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
409 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
410 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
411 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Evan Cheng92232302006-04-12 21:21:57 +0000412
Evan Cheng617a6a82006-04-10 07:23:14 +0000413 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
414 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000415 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000416 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
417 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
418 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000419
Evan Cheng92232302006-04-12 21:21:57 +0000420 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
421 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
422 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
423 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
424 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
425 }
426 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
427 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
428 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
429 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
430 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
431 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
432
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000433 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng92232302006-04-12 21:21:57 +0000434 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
435 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
436 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
437 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
438 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
439 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
440 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000441 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
442 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000443 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
444 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000445 }
Evan Cheng92232302006-04-12 21:21:57 +0000446
447 // Custom lower v2i64 and v2f64 selects.
448 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000449 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000450 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000451 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000452 }
453
Evan Cheng78038292006-04-05 23:38:46 +0000454 // We want to custom lower some of our intrinsics.
455 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
456
Evan Cheng5987cfb2006-07-07 08:33:52 +0000457 // We have target-specific dag combine patterns for the following nodes:
458 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner9259b1e2006-10-04 06:57:07 +0000459 setTargetDAGCombine(ISD::SELECT);
Evan Cheng5987cfb2006-07-07 08:33:52 +0000460
Chris Lattner76ac0682005-11-15 00:40:23 +0000461 computeRegisterProperties();
462
Evan Cheng6a374562006-02-14 08:25:08 +0000463 // FIXME: These should be based on subtarget info. Plus, the values should
464 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000465 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
466 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
467 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000468 allowUnalignedMemoryAccesses = true; // x86 supports it!
469}
470
Chris Lattner3c763092007-02-25 08:29:00 +0000471
472//===----------------------------------------------------------------------===//
473// Return Value Calling Convention Implementation
474//===----------------------------------------------------------------------===//
475
Chris Lattnerba3d2732007-02-28 04:55:35 +0000476#include "X86GenCallingConv.inc"
Chris Lattnerc9eed392007-02-27 05:28:59 +0000477
Chris Lattner2fc0d702007-02-25 09:12:39 +0000478/// LowerRET - Lower an ISD::RET node.
479SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
480 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
481
Chris Lattnerc9eed392007-02-27 05:28:59 +0000482 SmallVector<CCValAssign, 16> RVLocs;
483 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
484 CCState CCInfo(CC, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000485 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000486
Chris Lattner2fc0d702007-02-25 09:12:39 +0000487
488 // If this is the first return lowered for this function, add the regs to the
489 // liveout set for the function.
490 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000491 for (unsigned i = 0; i != RVLocs.size(); ++i)
492 if (RVLocs[i].isRegLoc())
493 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2fc0d702007-02-25 09:12:39 +0000494 }
495
496 SDOperand Chain = Op.getOperand(0);
497 SDOperand Flag;
498
499 // Copy the result values into the output registers.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000500 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
501 RVLocs[0].getLocReg() != X86::ST0) {
502 for (unsigned i = 0; i != RVLocs.size(); ++i) {
503 CCValAssign &VA = RVLocs[i];
504 assert(VA.isRegLoc() && "Can only return in registers!");
505 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
506 Flag);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000507 Flag = Chain.getValue(1);
508 }
509 } else {
510 // We need to handle a destination of ST0 specially, because it isn't really
511 // a register.
512 SDOperand Value = Op.getOperand(1);
513
514 // If this is an FP return with ScalarSSE, we need to move the value from
515 // an XMM register onto the fp-stack.
516 if (X86ScalarSSE) {
517 SDOperand MemLoc;
518
519 // If this is a load into a scalarsse value, don't store the loaded value
520 // back to the stack, only to reload it: just replace the scalar-sse load.
521 if (ISD::isNON_EXTLoad(Value.Val) &&
522 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
523 Chain = Value.getOperand(0);
524 MemLoc = Value.getOperand(1);
525 } else {
526 // Spill the value to memory and reload it into top of stack.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000527 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2fc0d702007-02-25 09:12:39 +0000528 MachineFunction &MF = DAG.getMachineFunction();
529 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
530 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
531 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
532 }
533 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000534 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2fc0d702007-02-25 09:12:39 +0000535 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
536 Chain = Value.getValue(1);
537 }
538
539 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
540 SDOperand Ops[] = { Chain, Value };
541 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
542 Flag = Chain.getValue(1);
543 }
544
545 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
546 if (Flag.Val)
547 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
548 else
549 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
550}
551
552
Chris Lattner0cd99602007-02-25 08:59:22 +0000553/// LowerCallResult - Lower the result values of an ISD::CALL into the
554/// appropriate copies out of appropriate physical registers. This assumes that
555/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
556/// being lowered. The returns a SDNode with the same number of values as the
557/// ISD::CALL.
558SDNode *X86TargetLowering::
559LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
560 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattner152bfa12007-02-28 07:09:55 +0000561
562 // Assign locations to each value returned by this call.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000563 SmallVector<CCValAssign, 16> RVLocs;
564 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000565 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
566
Chris Lattner0cd99602007-02-25 08:59:22 +0000567
Chris Lattner152bfa12007-02-28 07:09:55 +0000568 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner0cd99602007-02-25 08:59:22 +0000569
570 // Copy all of the result registers out of their specified physreg.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000571 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
572 for (unsigned i = 0; i != RVLocs.size(); ++i) {
573 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
574 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner0cd99602007-02-25 08:59:22 +0000575 InFlag = Chain.getValue(2);
576 ResultVals.push_back(Chain.getValue(0));
577 }
578 } else {
579 // Copies from the FP stack are special, as ST0 isn't a valid register
580 // before the fp stackifier runs.
581
582 // Copy ST0 into an RFP register with FP_GET_RESULT.
583 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
584 SDOperand GROps[] = { Chain, InFlag };
585 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
586 Chain = RetVal.getValue(1);
587 InFlag = RetVal.getValue(2);
588
589 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
590 // an XMM register.
591 if (X86ScalarSSE) {
592 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
593 // shouldn't be necessary except that RFP cannot be live across
594 // multiple blocks. When stackifier is fixed, they can be uncoupled.
595 MachineFunction &MF = DAG.getMachineFunction();
596 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
597 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
598 SDOperand Ops[] = {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000599 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner0cd99602007-02-25 08:59:22 +0000600 };
601 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000602 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner0cd99602007-02-25 08:59:22 +0000603 Chain = RetVal.getValue(1);
604 }
605
Chris Lattnerc9eed392007-02-27 05:28:59 +0000606 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
Chris Lattner0cd99602007-02-25 08:59:22 +0000607 // FIXME: we would really like to remember that this FP_ROUND
608 // operation is okay to eliminate if we allow excess FP precision.
609 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
610 ResultVals.push_back(RetVal);
611 }
612
613 // Merge everything together with a MERGE_VALUES node.
614 ResultVals.push_back(Chain);
615 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
616 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner3c763092007-02-25 08:29:00 +0000617}
618
619
Chris Lattner76ac0682005-11-15 00:40:23 +0000620//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000621// C & StdCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000622//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000623// StdCall calling convention seems to be standard for many Windows' API
624// routines and around. It differs from C calling convention just a little:
625// callee should clean up the stack, not caller. Symbols should be also
626// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +0000627
Evan Cheng24eb3f42006-04-27 05:35:28 +0000628/// AddLiveIn - This helper function adds the specified physical register to the
629/// MachineFunction as a live in value. It also creates a corresponding virtual
630/// register for it.
631static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000632 const TargetRegisterClass *RC) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000633 assert(RC->contains(PReg) && "Not the correct regclass!");
634 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
635 MF.addLiveIn(PReg, VReg);
636 return VReg;
637}
638
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000639SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
640 bool isStdCall) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000641 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000642 MachineFunction &MF = DAG.getMachineFunction();
643 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000644 SDOperand Root = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000645 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000646
Chris Lattner227b6c52007-02-28 07:00:42 +0000647 // Assign locations to all of the incoming arguments.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000648 SmallVector<CCValAssign, 16> ArgLocs;
649 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
650 ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000651 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
652
Chris Lattnerb9db2252007-02-28 05:46:49 +0000653 SmallVector<SDOperand, 8> ArgValues;
654 unsigned LastVal = ~0U;
655 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
656 CCValAssign &VA = ArgLocs[i];
657 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
658 // places.
659 assert(VA.getValNo() != LastVal &&
660 "Don't support value assigned to multiple locs yet");
661 LastVal = VA.getValNo();
662
663 if (VA.isRegLoc()) {
664 MVT::ValueType RegVT = VA.getLocVT();
665 TargetRegisterClass *RC;
666 if (RegVT == MVT::i32)
667 RC = X86::GR32RegisterClass;
668 else {
669 assert(MVT::isVector(RegVT));
670 RC = X86::VR128RegisterClass;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000671 }
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000672
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000673 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
674 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerb9db2252007-02-28 05:46:49 +0000675
676 // If this is an 8 or 16-bit value, it is really passed promoted to 32
677 // bits. Insert an assert[sz]ext to capture this, then truncate to the
678 // right size.
679 if (VA.getLocInfo() == CCValAssign::SExt)
680 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
681 DAG.getValueType(VA.getValVT()));
682 else if (VA.getLocInfo() == CCValAssign::ZExt)
683 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
684 DAG.getValueType(VA.getValVT()));
685
686 if (VA.getLocInfo() != CCValAssign::Full)
687 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
688
689 ArgValues.push_back(ArgValue);
690 } else {
691 assert(VA.isMemLoc());
692
693 // Create the nodes corresponding to a load from this parameter slot.
694 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
695 VA.getLocMemOffset());
696 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
697 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000698 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000699 }
Chris Lattnerb9db2252007-02-28 05:46:49 +0000700
701 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000702
Evan Cheng17e734f2006-05-23 21:06:34 +0000703 ArgValues.push_back(Root);
704
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000705 // If the function takes variable number of arguments, make a frame index for
706 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000707 if (isVarArg)
Chris Lattnerb9db2252007-02-28 05:46:49 +0000708 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000709
710 if (isStdCall && !isVarArg) {
Chris Lattnerb9db2252007-02-28 05:46:49 +0000711 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000712 BytesCallerReserves = 0;
713 } else {
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000714 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000715
716 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000717 if (NumArgs &&
718 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000719 ISD::ParamFlags::StructReturn))
Chris Lattnerb9db2252007-02-28 05:46:49 +0000720 BytesToPopOnReturn = 4;
721
722 BytesCallerReserves = StackSize;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000723 }
724
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000725 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
726 ReturnAddrIndex = 0; // No return address slot generated yet.
Evan Cheng17e734f2006-05-23 21:06:34 +0000727
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000728 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000729
Evan Cheng17e734f2006-05-23 21:06:34 +0000730 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +0000731 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +0000732 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000733}
734
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000735SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +0000736 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +0000737 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000738 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000739 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
740 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +0000741 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000742
Chris Lattner227b6c52007-02-28 07:00:42 +0000743 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerbe799592007-02-28 05:31:48 +0000744 SmallVector<CCValAssign, 16> ArgLocs;
745 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000746 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000747
Chris Lattnerbe799592007-02-28 05:31:48 +0000748 // Get a count of how many bytes are to be pushed on the stack.
749 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +0000750
Evan Cheng2a330942006-05-25 00:59:30 +0000751 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000752
Chris Lattner35a08552007-02-25 07:10:00 +0000753 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
754 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng2a330942006-05-25 00:59:30 +0000755
Chris Lattnerbe799592007-02-28 05:31:48 +0000756 SDOperand StackPtr;
Chris Lattnerbe799592007-02-28 05:31:48 +0000757
758 // Walk the register/memloc assignments, inserting copies/loads.
759 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
760 CCValAssign &VA = ArgLocs[i];
761 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000762
Chris Lattnerbe799592007-02-28 05:31:48 +0000763 // Promote the value if needed.
764 switch (VA.getLocInfo()) {
765 default: assert(0 && "Unknown loc info!");
766 case CCValAssign::Full: break;
767 case CCValAssign::SExt:
768 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
769 break;
770 case CCValAssign::ZExt:
771 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
772 break;
773 case CCValAssign::AExt:
774 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
775 break;
Evan Cheng5ee96892006-05-25 18:56:34 +0000776 }
Chris Lattnerbe799592007-02-28 05:31:48 +0000777
778 if (VA.isRegLoc()) {
779 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
780 } else {
781 assert(VA.isMemLoc());
782 if (StackPtr.Val == 0)
783 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
784 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000785 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
786 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner76ac0682005-11-15 00:40:23 +0000787 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000788 }
789
Chris Lattner5958b172007-02-28 05:39:26 +0000790 // If the first argument is an sret pointer, remember it.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000791 bool isSRet = NumOps &&
792 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000793 ISD::ParamFlags::StructReturn);
Chris Lattner5958b172007-02-28 05:39:26 +0000794
Evan Cheng2a330942006-05-25 00:59:30 +0000795 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000796 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
797 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +0000798
Evan Cheng88decde2006-04-28 21:29:37 +0000799 // Build a sequence of copy-to-reg nodes chained together with token chain
800 // and flag operands which copy the outgoing args into registers.
801 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +0000802 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
803 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
804 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +0000805 InFlag = Chain.getValue(1);
806 }
807
Evan Cheng84a041e2007-02-21 21:18:14 +0000808 // ELF / PIC requires GOT in the EBX register before function calls via PLT
809 // GOT pointer.
Evan Cheng1281dc32007-01-22 21:34:25 +0000810 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
811 Subtarget->isPICStyleGOT()) {
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000812 Chain = DAG.getCopyToReg(Chain, X86::EBX,
813 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
814 InFlag);
815 InFlag = Chain.getValue(1);
816 }
817
Evan Cheng2a330942006-05-25 00:59:30 +0000818 // If the callee is a GlobalAddress node (quite common, every direct call is)
819 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +0000820 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +0000821 // We should use extra load for direct calls to dllimported functions in
822 // non-JIT mode.
823 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
824 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +0000825 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
826 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +0000827 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
828
Chris Lattnere56fef92007-02-25 06:40:16 +0000829 // Returns a chain & a flag for retval copy to use.
830 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +0000831 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000832 Ops.push_back(Chain);
833 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +0000834
835 // Add argument registers to the end of the list so that they are known live
836 // into the call.
837 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000838 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +0000839 RegsToPass[i].second.getValueType()));
Evan Cheng84a041e2007-02-21 21:18:14 +0000840
841 // Add an implicit use GOT pointer in EBX.
842 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
843 Subtarget->isPICStyleGOT())
844 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000845
Evan Cheng88decde2006-04-28 21:29:37 +0000846 if (InFlag.Val)
847 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000848
Evan Cheng2a330942006-05-25 00:59:30 +0000849 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000850 NodeTys, &Ops[0], Ops.size());
Evan Cheng88decde2006-04-28 21:29:37 +0000851 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000852
Chris Lattner8be5be82006-05-23 18:50:38 +0000853 // Create the CALLSEQ_END node.
854 unsigned NumBytesForCalleeToPush = 0;
855
Chris Lattner7802f3e2007-02-25 09:06:15 +0000856 if (CC == CallingConv::X86_StdCall) {
857 if (isVarArg)
Chris Lattner5958b172007-02-28 05:39:26 +0000858 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner7802f3e2007-02-25 09:06:15 +0000859 else
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000860 NumBytesForCalleeToPush = NumBytes;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000861 } else {
862 // If this is is a call to a struct-return function, the callee
863 // pops the hidden struct pointer, so we have to push it back.
864 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattner5958b172007-02-28 05:39:26 +0000865 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000866 }
867
Chris Lattnerd6b853ad2007-02-25 07:18:38 +0000868 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000869 Ops.clear();
870 Ops.push_back(Chain);
871 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000872 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000873 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000874 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner0cd99602007-02-25 08:59:22 +0000875 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000876
Chris Lattner0cd99602007-02-25 08:59:22 +0000877 // Handle result values, copying them out of physregs into vregs that we
878 // return.
Chris Lattner7802f3e2007-02-25 09:06:15 +0000879 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000880}
881
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000882
883//===----------------------------------------------------------------------===//
Chris Lattner3066bec2007-02-28 06:10:12 +0000884// FastCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000885//===----------------------------------------------------------------------===//
886//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000887// The X86 'fastcall' calling convention passes up to two integer arguments in
888// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
889// and requires that the callee pop its arguments off the stack (allowing proper
890// tail calls), and has the same return value conventions as C calling convs.
891//
892// This calling convention always arranges for the callee pop value to be 8n+4
893// bytes, which is needed for tail recursion elimination and stack alignment
894// reasons.
Evan Cheng17e734f2006-05-23 21:06:34 +0000895SDOperand
Chris Lattner3ed3be32007-02-28 06:05:16 +0000896X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000897 MachineFunction &MF = DAG.getMachineFunction();
898 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000899 SDOperand Root = Op.getOperand(0);
Chris Lattner76ac0682005-11-15 00:40:23 +0000900
Chris Lattner227b6c52007-02-28 07:00:42 +0000901 // Assign locations to all of the incoming arguments.
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000902 SmallVector<CCValAssign, 16> ArgLocs;
903 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
904 ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000905 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000906
907 SmallVector<SDOperand, 8> ArgValues;
908 unsigned LastVal = ~0U;
909 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
910 CCValAssign &VA = ArgLocs[i];
911 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
912 // places.
913 assert(VA.getValNo() != LastVal &&
914 "Don't support value assigned to multiple locs yet");
915 LastVal = VA.getValNo();
916
917 if (VA.isRegLoc()) {
918 MVT::ValueType RegVT = VA.getLocVT();
919 TargetRegisterClass *RC;
920 if (RegVT == MVT::i32)
921 RC = X86::GR32RegisterClass;
922 else {
923 assert(MVT::isVector(RegVT));
924 RC = X86::VR128RegisterClass;
925 }
926
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000927 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
928 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000929
930 // If this is an 8 or 16-bit value, it is really passed promoted to 32
931 // bits. Insert an assert[sz]ext to capture this, then truncate to the
932 // right size.
933 if (VA.getLocInfo() == CCValAssign::SExt)
934 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
935 DAG.getValueType(VA.getValVT()));
936 else if (VA.getLocInfo() == CCValAssign::ZExt)
937 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
938 DAG.getValueType(VA.getValVT()));
939
940 if (VA.getLocInfo() != CCValAssign::Full)
941 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
942
943 ArgValues.push_back(ArgValue);
944 } else {
945 assert(VA.isMemLoc());
946
947 // Create the nodes corresponding to a load from this parameter slot.
948 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
949 VA.getLocMemOffset());
950 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
951 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
952 }
953 }
954
Evan Cheng17e734f2006-05-23 21:06:34 +0000955 ArgValues.push_back(Root);
956
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000957 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000958
Anton Korobeynikov57af2a42007-03-02 21:50:27 +0000959 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000960 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
961 // arguments and the arguments after the retaddr has been pushed are aligned.
962 if ((StackSize & 7) == 0)
963 StackSize += 4;
964 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000965
966 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000967 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattner76ac0682005-11-15 00:40:23 +0000968 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000969 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +0000970 BytesCallerReserves = 0;
971
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000972 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
973
Evan Cheng17e734f2006-05-23 21:06:34 +0000974 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +0000975 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +0000976 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000977}
978
Chris Lattner104aa5d2006-09-26 03:57:53 +0000979SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +0000980 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +0000981 SDOperand Chain = Op.getOperand(0);
Evan Cheng2a330942006-05-25 00:59:30 +0000982 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
983 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +0000984
Chris Lattner227b6c52007-02-28 07:00:42 +0000985 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerd439e862007-02-28 06:26:33 +0000986 SmallVector<CCValAssign, 16> ArgLocs;
987 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000988 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerd439e862007-02-28 06:26:33 +0000989
990 // Get a count of how many bytes are to be pushed on the stack.
991 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +0000992
Anton Korobeynikov57af2a42007-03-02 21:50:27 +0000993 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000994 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
995 // arguments and the arguments after the retaddr has been pushed are aligned.
996 if ((NumBytes & 7) == 0)
997 NumBytes += 4;
998 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000999
Chris Lattner62c34842006-02-13 09:00:43 +00001000 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerd439e862007-02-28 06:26:33 +00001001
Chris Lattner35a08552007-02-25 07:10:00 +00001002 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1003 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerd439e862007-02-28 06:26:33 +00001004
1005 SDOperand StackPtr;
1006
1007 // Walk the register/memloc assignments, inserting copies/loads.
1008 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1009 CCValAssign &VA = ArgLocs[i];
1010 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1011
1012 // Promote the value if needed.
1013 switch (VA.getLocInfo()) {
1014 default: assert(0 && "Unknown loc info!");
1015 case CCValAssign::Full: break;
1016 case CCValAssign::SExt:
1017 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner3ed3be32007-02-28 06:05:16 +00001018 break;
Chris Lattnerd439e862007-02-28 06:26:33 +00001019 case CCValAssign::ZExt:
1020 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1021 break;
1022 case CCValAssign::AExt:
1023 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1024 break;
1025 }
1026
1027 if (VA.isRegLoc()) {
1028 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1029 } else {
1030 assert(VA.isMemLoc());
1031 if (StackPtr.Val == 0)
1032 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1033 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001034 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Chengab51cf22006-10-13 21:14:26 +00001035 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng2a330942006-05-25 00:59:30 +00001036 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001037 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001038
Evan Cheng2a330942006-05-25 00:59:30 +00001039 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001040 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1041 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +00001042
Nate Begeman7e5496d2006-02-17 00:03:04 +00001043 // Build a sequence of copy-to-reg nodes chained together with token chain
1044 // and flag operands which copy the outgoing args into registers.
1045 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001046 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1047 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1048 InFlag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001049 InFlag = Chain.getValue(1);
1050 }
1051
Evan Cheng2a330942006-05-25 00:59:30 +00001052 // If the callee is a GlobalAddress node (quite common, every direct call is)
1053 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001054 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00001055 // We should use extra load for direct calls to dllimported functions in
1056 // non-JIT mode.
1057 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1058 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001059 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1060 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +00001061 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1062
Evan Cheng84a041e2007-02-21 21:18:14 +00001063 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1064 // GOT pointer.
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001065 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1066 Subtarget->isPICStyleGOT()) {
1067 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1068 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1069 InFlag);
1070 InFlag = Chain.getValue(1);
1071 }
1072
Chris Lattnere56fef92007-02-25 06:40:16 +00001073 // Returns a chain & a flag for retval copy to use.
1074 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00001075 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001076 Ops.push_back(Chain);
1077 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +00001078
1079 // Add argument registers to the end of the list so that they are known live
1080 // into the call.
1081 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001082 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +00001083 RegsToPass[i].second.getValueType()));
1084
Evan Cheng84a041e2007-02-21 21:18:14 +00001085 // Add an implicit use GOT pointer in EBX.
1086 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1087 Subtarget->isPICStyleGOT())
1088 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1089
Nate Begeman7e5496d2006-02-17 00:03:04 +00001090 if (InFlag.Val)
1091 Ops.push_back(InFlag);
1092
1093 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001094 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001095 NodeTys, &Ops[0], Ops.size());
Nate Begeman7e5496d2006-02-17 00:03:04 +00001096 InFlag = Chain.getValue(1);
1097
Chris Lattnerd6b853ad2007-02-25 07:18:38 +00001098 // Returns a flag for retval copy to use.
1099 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001100 Ops.clear();
1101 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001102 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1103 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001104 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001105 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattnerba474f52007-02-25 09:10:05 +00001106 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001107
Chris Lattnerba474f52007-02-25 09:10:05 +00001108 // Handle result values, copying them out of physregs into vregs that we
1109 // return.
1110 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001111}
1112
Chris Lattner3066bec2007-02-28 06:10:12 +00001113
1114//===----------------------------------------------------------------------===//
1115// X86-64 C Calling Convention implementation
1116//===----------------------------------------------------------------------===//
1117
1118SDOperand
1119X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3066bec2007-02-28 06:10:12 +00001120 MachineFunction &MF = DAG.getMachineFunction();
1121 MachineFrameInfo *MFI = MF.getFrameInfo();
1122 SDOperand Root = Op.getOperand(0);
1123 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1124
1125 static const unsigned GPR64ArgRegs[] = {
1126 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1127 };
1128 static const unsigned XMMArgRegs[] = {
1129 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1130 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1131 };
1132
Chris Lattner227b6c52007-02-28 07:00:42 +00001133
1134 // Assign locations to all of the incoming arguments.
Chris Lattner3066bec2007-02-28 06:10:12 +00001135 SmallVector<CCValAssign, 16> ArgLocs;
1136 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1137 ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001138 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001139
1140 SmallVector<SDOperand, 8> ArgValues;
1141 unsigned LastVal = ~0U;
1142 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1143 CCValAssign &VA = ArgLocs[i];
1144 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1145 // places.
1146 assert(VA.getValNo() != LastVal &&
1147 "Don't support value assigned to multiple locs yet");
1148 LastVal = VA.getValNo();
1149
1150 if (VA.isRegLoc()) {
1151 MVT::ValueType RegVT = VA.getLocVT();
1152 TargetRegisterClass *RC;
1153 if (RegVT == MVT::i32)
1154 RC = X86::GR32RegisterClass;
1155 else if (RegVT == MVT::i64)
1156 RC = X86::GR64RegisterClass;
1157 else if (RegVT == MVT::f32)
1158 RC = X86::FR32RegisterClass;
1159 else if (RegVT == MVT::f64)
1160 RC = X86::FR64RegisterClass;
1161 else {
1162 assert(MVT::isVector(RegVT));
1163 RC = X86::VR128RegisterClass;
1164 }
Chris Lattner9c7e5e32007-03-02 05:12:29 +00001165
1166 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1167 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner3066bec2007-02-28 06:10:12 +00001168
1169 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1170 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1171 // right size.
1172 if (VA.getLocInfo() == CCValAssign::SExt)
1173 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1174 DAG.getValueType(VA.getValVT()));
1175 else if (VA.getLocInfo() == CCValAssign::ZExt)
1176 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1177 DAG.getValueType(VA.getValVT()));
1178
1179 if (VA.getLocInfo() != CCValAssign::Full)
1180 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1181
1182 ArgValues.push_back(ArgValue);
1183 } else {
1184 assert(VA.isMemLoc());
1185
1186 // Create the nodes corresponding to a load from this parameter slot.
1187 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1188 VA.getLocMemOffset());
1189 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1190 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1191 }
1192 }
1193
1194 unsigned StackSize = CCInfo.getNextStackOffset();
1195
1196 // If the function takes variable number of arguments, make a frame index for
1197 // the start of the first vararg value... for expansion of llvm.va_start.
1198 if (isVarArg) {
1199 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1200 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1201
1202 // For X86-64, if there are vararg parameters that are passed via
1203 // registers, then we must store them to their spots on the stack so they
1204 // may be loaded by deferencing the result of va_next.
1205 VarArgsGPOffset = NumIntRegs * 8;
1206 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1207 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1208 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1209
1210 // Store the integer parameter registers.
1211 SmallVector<SDOperand, 8> MemOps;
1212 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1213 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1214 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1215 for (; NumIntRegs != 6; ++NumIntRegs) {
1216 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1217 X86::GR64RegisterClass);
1218 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1219 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1220 MemOps.push_back(Store);
1221 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1222 DAG.getConstant(8, getPointerTy()));
1223 }
1224
1225 // Now store the XMM (fp + vector) parameter registers.
1226 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1227 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1228 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1229 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1230 X86::VR128RegisterClass);
1231 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1232 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1233 MemOps.push_back(Store);
1234 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1235 DAG.getConstant(16, getPointerTy()));
1236 }
1237 if (!MemOps.empty())
1238 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1239 &MemOps[0], MemOps.size());
1240 }
1241
1242 ArgValues.push_back(Root);
1243
1244 ReturnAddrIndex = 0; // No return address slot generated yet.
1245 BytesToPopOnReturn = 0; // Callee pops nothing.
1246 BytesCallerReserves = StackSize;
1247
1248 // Return the new list of results.
1249 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1250 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1251}
1252
1253SDOperand
1254X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1255 unsigned CC) {
1256 SDOperand Chain = Op.getOperand(0);
1257 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1258 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1259 SDOperand Callee = Op.getOperand(4);
Chris Lattner227b6c52007-02-28 07:00:42 +00001260
1261 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner3066bec2007-02-28 06:10:12 +00001262 SmallVector<CCValAssign, 16> ArgLocs;
1263 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001264 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001265
1266 // Get a count of how many bytes are to be pushed on the stack.
1267 unsigned NumBytes = CCInfo.getNextStackOffset();
1268 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1269
1270 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1271 SmallVector<SDOperand, 8> MemOpChains;
1272
1273 SDOperand StackPtr;
1274
1275 // Walk the register/memloc assignments, inserting copies/loads.
1276 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1277 CCValAssign &VA = ArgLocs[i];
1278 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1279
1280 // Promote the value if needed.
1281 switch (VA.getLocInfo()) {
1282 default: assert(0 && "Unknown loc info!");
1283 case CCValAssign::Full: break;
1284 case CCValAssign::SExt:
1285 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1286 break;
1287 case CCValAssign::ZExt:
1288 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1289 break;
1290 case CCValAssign::AExt:
1291 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1292 break;
1293 }
1294
1295 if (VA.isRegLoc()) {
1296 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1297 } else {
1298 assert(VA.isMemLoc());
1299 if (StackPtr.Val == 0)
1300 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1301 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1302 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1303 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1304 }
1305 }
1306
1307 if (!MemOpChains.empty())
1308 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1309 &MemOpChains[0], MemOpChains.size());
1310
1311 // Build a sequence of copy-to-reg nodes chained together with token chain
1312 // and flag operands which copy the outgoing args into registers.
1313 SDOperand InFlag;
1314 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1315 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1316 InFlag);
1317 InFlag = Chain.getValue(1);
1318 }
1319
1320 if (isVarArg) {
1321 // From AMD64 ABI document:
1322 // For calls that may call functions that use varargs or stdargs
1323 // (prototype-less calls or calls to functions containing ellipsis (...) in
1324 // the declaration) %al is used as hidden argument to specify the number
1325 // of SSE registers used. The contents of %al do not need to match exactly
1326 // the number of registers, but must be an ubound on the number of SSE
1327 // registers used and is in the range 0 - 8 inclusive.
1328
1329 // Count the number of XMM registers allocated.
1330 static const unsigned XMMArgRegs[] = {
1331 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1332 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1333 };
1334 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1335
1336 Chain = DAG.getCopyToReg(Chain, X86::AL,
1337 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1338 InFlag = Chain.getValue(1);
1339 }
1340
1341 // If the callee is a GlobalAddress node (quite common, every direct call is)
1342 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1343 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1344 // We should use extra load for direct calls to dllimported functions in
1345 // non-JIT mode.
Evan Chenga1779b92007-03-14 22:11:11 +00001346 if (getTargetMachine().getCodeModel() != CodeModel::Large
1347 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1348 getTargetMachine(), true))
Chris Lattner3066bec2007-02-28 06:10:12 +00001349 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1350 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chenga1779b92007-03-14 22:11:11 +00001351 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1352 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattner3066bec2007-02-28 06:10:12 +00001353
1354 // Returns a chain & a flag for retval copy to use.
1355 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1356 SmallVector<SDOperand, 8> Ops;
1357 Ops.push_back(Chain);
1358 Ops.push_back(Callee);
1359
1360 // Add argument registers to the end of the list so that they are known live
1361 // into the call.
1362 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1363 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1364 RegsToPass[i].second.getValueType()));
1365
1366 if (InFlag.Val)
1367 Ops.push_back(InFlag);
1368
1369 // FIXME: Do not generate X86ISD::TAILCALL for now.
1370 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1371 NodeTys, &Ops[0], Ops.size());
1372 InFlag = Chain.getValue(1);
1373
1374 // Returns a flag for retval copy to use.
1375 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1376 Ops.clear();
1377 Ops.push_back(Chain);
1378 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1379 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1380 Ops.push_back(InFlag);
1381 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1382 InFlag = Chain.getValue(1);
1383
1384 // Handle result values, copying them out of physregs into vregs that we
1385 // return.
1386 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1387}
1388
1389
1390//===----------------------------------------------------------------------===//
1391// Other Lowering Hooks
1392//===----------------------------------------------------------------------===//
1393
1394
Chris Lattner76ac0682005-11-15 00:40:23 +00001395SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1396 if (ReturnAddrIndex == 0) {
1397 // Set up a frame object for the return address.
1398 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001399 if (Subtarget->is64Bit())
1400 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1401 else
1402 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattner76ac0682005-11-15 00:40:23 +00001403 }
1404
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001405 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattner76ac0682005-11-15 00:40:23 +00001406}
1407
1408
1409
Evan Cheng45df7f82006-01-30 23:41:35 +00001410/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1411/// specific condition code. It returns a false if it cannot do a direct
Chris Lattner7a627672006-09-13 03:22:10 +00001412/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1413/// needed.
Evan Cheng78038292006-04-05 23:38:46 +00001414static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattner7a627672006-09-13 03:22:10 +00001415 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1416 SelectionDAG &DAG) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001417 X86CC = X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001418 if (!isFP) {
Chris Lattner971e3392006-09-13 17:04:54 +00001419 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1420 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1421 // X > -1 -> X == 0, jump !sign.
1422 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001423 X86CC = X86::COND_NS;
Chris Lattner971e3392006-09-13 17:04:54 +00001424 return true;
1425 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1426 // X < 0 -> X == 0, jump on sign.
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001427 X86CC = X86::COND_S;
Chris Lattner971e3392006-09-13 17:04:54 +00001428 return true;
1429 }
Chris Lattner7a627672006-09-13 03:22:10 +00001430 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001431
Evan Cheng172fce72006-01-06 00:43:03 +00001432 switch (SetCCOpcode) {
1433 default: break;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001434 case ISD::SETEQ: X86CC = X86::COND_E; break;
1435 case ISD::SETGT: X86CC = X86::COND_G; break;
1436 case ISD::SETGE: X86CC = X86::COND_GE; break;
1437 case ISD::SETLT: X86CC = X86::COND_L; break;
1438 case ISD::SETLE: X86CC = X86::COND_LE; break;
1439 case ISD::SETNE: X86CC = X86::COND_NE; break;
1440 case ISD::SETULT: X86CC = X86::COND_B; break;
1441 case ISD::SETUGT: X86CC = X86::COND_A; break;
1442 case ISD::SETULE: X86CC = X86::COND_BE; break;
1443 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001444 }
1445 } else {
1446 // On a floating point condition, the flags are set as follows:
1447 // ZF PF CF op
1448 // 0 | 0 | 0 | X > Y
1449 // 0 | 0 | 1 | X < Y
1450 // 1 | 0 | 0 | X == Y
1451 // 1 | 1 | 1 | unordered
Chris Lattner7a627672006-09-13 03:22:10 +00001452 bool Flip = false;
Evan Cheng172fce72006-01-06 00:43:03 +00001453 switch (SetCCOpcode) {
1454 default: break;
1455 case ISD::SETUEQ:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001456 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001457 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001458 case ISD::SETOGT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001459 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001460 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001461 case ISD::SETOGE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001462 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001463 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001464 case ISD::SETULT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001465 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001466 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001467 case ISD::SETULE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001468 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001469 case ISD::SETONE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001470 case ISD::SETNE: X86CC = X86::COND_NE; break;
1471 case ISD::SETUO: X86CC = X86::COND_P; break;
1472 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001473 }
Chris Lattner7a627672006-09-13 03:22:10 +00001474 if (Flip)
1475 std::swap(LHS, RHS);
Evan Cheng172fce72006-01-06 00:43:03 +00001476 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001477
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001478 return X86CC != X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001479}
1480
Evan Cheng339edad2006-01-11 00:33:36 +00001481/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1482/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001483/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001484static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001485 switch (X86CC) {
1486 default:
1487 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001488 case X86::COND_B:
1489 case X86::COND_BE:
1490 case X86::COND_E:
1491 case X86::COND_P:
1492 case X86::COND_A:
1493 case X86::COND_AE:
1494 case X86::COND_NE:
1495 case X86::COND_NP:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001496 return true;
1497 }
1498}
1499
Evan Chengc995b452006-04-06 23:23:56 +00001500/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001501/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001502static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1503 if (Op.getOpcode() == ISD::UNDEF)
1504 return true;
1505
1506 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001507 return (Val >= Low && Val < Hi);
1508}
1509
1510/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1511/// true if Op is undef or if its value equal to the specified value.
1512static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1513 if (Op.getOpcode() == ISD::UNDEF)
1514 return true;
1515 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001516}
1517
Evan Cheng68ad48b2006-03-22 18:59:22 +00001518/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1519/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1520bool X86::isPSHUFDMask(SDNode *N) {
1521 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1522
1523 if (N->getNumOperands() != 4)
1524 return false;
1525
1526 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001527 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001528 SDOperand Arg = N->getOperand(i);
1529 if (Arg.getOpcode() == ISD::UNDEF) continue;
1530 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1531 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001532 return false;
1533 }
1534
1535 return true;
1536}
1537
1538/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001539/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001540bool X86::isPSHUFHWMask(SDNode *N) {
1541 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1542
1543 if (N->getNumOperands() != 8)
1544 return false;
1545
1546 // Lower quadword copied in order.
1547 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001548 SDOperand Arg = N->getOperand(i);
1549 if (Arg.getOpcode() == ISD::UNDEF) continue;
1550 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1551 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001552 return false;
1553 }
1554
1555 // Upper quadword shuffled.
1556 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001557 SDOperand Arg = N->getOperand(i);
1558 if (Arg.getOpcode() == ISD::UNDEF) continue;
1559 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1560 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001561 if (Val < 4 || Val > 7)
1562 return false;
1563 }
1564
1565 return true;
1566}
1567
1568/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001569/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001570bool X86::isPSHUFLWMask(SDNode *N) {
1571 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1572
1573 if (N->getNumOperands() != 8)
1574 return false;
1575
1576 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001577 for (unsigned i = 4; i != 8; ++i)
1578 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001579 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001580
1581 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001582 for (unsigned i = 0; i != 4; ++i)
1583 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001584 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001585
1586 return true;
1587}
1588
Evan Chengd27fb3e2006-03-24 01:18:28 +00001589/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1590/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner35a08552007-02-25 07:10:00 +00001591static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001592 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001593
Evan Cheng60f0b892006-04-20 08:58:49 +00001594 unsigned Half = NumElems / 2;
1595 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001596 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng60f0b892006-04-20 08:58:49 +00001597 return false;
1598 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001599 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00001600 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001601
1602 return true;
1603}
1604
Evan Cheng60f0b892006-04-20 08:58:49 +00001605bool X86::isSHUFPMask(SDNode *N) {
1606 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001607 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001608}
1609
1610/// isCommutedSHUFP - Returns true if the shuffle mask is except
1611/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1612/// half elements to come from vector 1 (which would equal the dest.) and
1613/// the upper half to come from vector 2.
Chris Lattner35a08552007-02-25 07:10:00 +00001614static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1615 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001616
Chris Lattner35a08552007-02-25 07:10:00 +00001617 unsigned Half = NumOps / 2;
Evan Cheng60f0b892006-04-20 08:58:49 +00001618 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001619 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00001620 return false;
Chris Lattner35a08552007-02-25 07:10:00 +00001621 for (unsigned i = Half; i < NumOps; ++i)
1622 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng60f0b892006-04-20 08:58:49 +00001623 return false;
1624 return true;
1625}
1626
1627static bool isCommutedSHUFP(SDNode *N) {
1628 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001629 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001630}
1631
Evan Cheng2595a682006-03-24 02:58:06 +00001632/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1633/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1634bool X86::isMOVHLPSMask(SDNode *N) {
1635 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1636
Evan Cheng1a194a52006-03-28 06:50:32 +00001637 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001638 return false;
1639
Evan Cheng1a194a52006-03-28 06:50:32 +00001640 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001641 return isUndefOrEqual(N->getOperand(0), 6) &&
1642 isUndefOrEqual(N->getOperand(1), 7) &&
1643 isUndefOrEqual(N->getOperand(2), 2) &&
1644 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001645}
1646
Evan Cheng922e1912006-11-07 22:14:24 +00001647/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1648/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1649/// <2, 3, 2, 3>
1650bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1651 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1652
1653 if (N->getNumOperands() != 4)
1654 return false;
1655
1656 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1657 return isUndefOrEqual(N->getOperand(0), 2) &&
1658 isUndefOrEqual(N->getOperand(1), 3) &&
1659 isUndefOrEqual(N->getOperand(2), 2) &&
1660 isUndefOrEqual(N->getOperand(3), 3);
1661}
1662
Evan Chengc995b452006-04-06 23:23:56 +00001663/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1664/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1665bool X86::isMOVLPMask(SDNode *N) {
1666 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1667
1668 unsigned NumElems = N->getNumOperands();
1669 if (NumElems != 2 && NumElems != 4)
1670 return false;
1671
Evan Chengac847262006-04-07 21:53:05 +00001672 for (unsigned i = 0; i < NumElems/2; ++i)
1673 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1674 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001675
Evan Chengac847262006-04-07 21:53:05 +00001676 for (unsigned i = NumElems/2; i < NumElems; ++i)
1677 if (!isUndefOrEqual(N->getOperand(i), i))
1678 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001679
1680 return true;
1681}
1682
1683/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001684/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1685/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001686bool X86::isMOVHPMask(SDNode *N) {
1687 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1688
1689 unsigned NumElems = N->getNumOperands();
1690 if (NumElems != 2 && NumElems != 4)
1691 return false;
1692
Evan Chengac847262006-04-07 21:53:05 +00001693 for (unsigned i = 0; i < NumElems/2; ++i)
1694 if (!isUndefOrEqual(N->getOperand(i), i))
1695 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001696
1697 for (unsigned i = 0; i < NumElems/2; ++i) {
1698 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001699 if (!isUndefOrEqual(Arg, i + NumElems))
1700 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001701 }
1702
1703 return true;
1704}
1705
Evan Cheng5df75882006-03-28 00:39:58 +00001706/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1707/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner35a08552007-02-25 07:10:00 +00001708bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1709 bool V2IsSplat = false) {
1710 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng5df75882006-03-28 00:39:58 +00001711 return false;
1712
Chris Lattner35a08552007-02-25 07:10:00 +00001713 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1714 SDOperand BitI = Elts[i];
1715 SDOperand BitI1 = Elts[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001716 if (!isUndefOrEqual(BitI, j))
1717 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001718 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00001719 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001720 return false;
1721 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00001722 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001723 return false;
1724 }
Evan Cheng5df75882006-03-28 00:39:58 +00001725 }
1726
1727 return true;
1728}
1729
Evan Cheng60f0b892006-04-20 08:58:49 +00001730bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1731 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001732 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001733}
1734
Evan Cheng2bc32802006-03-28 02:43:26 +00001735/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1736/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner35a08552007-02-25 07:10:00 +00001737bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1738 bool V2IsSplat = false) {
1739 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng2bc32802006-03-28 02:43:26 +00001740 return false;
1741
Chris Lattner35a08552007-02-25 07:10:00 +00001742 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1743 SDOperand BitI = Elts[i];
1744 SDOperand BitI1 = Elts[i+1];
1745 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengac847262006-04-07 21:53:05 +00001746 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001747 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00001748 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001749 return false;
1750 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00001751 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001752 return false;
1753 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001754 }
1755
1756 return true;
1757}
1758
Evan Cheng60f0b892006-04-20 08:58:49 +00001759bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1760 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001761 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001762}
1763
Evan Chengf3b52c82006-04-05 07:20:06 +00001764/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1765/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1766/// <0, 0, 1, 1>
1767bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1768 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1769
1770 unsigned NumElems = N->getNumOperands();
1771 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1772 return false;
1773
1774 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1775 SDOperand BitI = N->getOperand(i);
1776 SDOperand BitI1 = N->getOperand(i+1);
1777
Evan Chengac847262006-04-07 21:53:05 +00001778 if (!isUndefOrEqual(BitI, j))
1779 return false;
1780 if (!isUndefOrEqual(BitI1, j))
1781 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001782 }
1783
1784 return true;
1785}
1786
Evan Chenge8b51802006-04-21 01:05:10 +00001787/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1788/// specifies a shuffle of elements that is suitable for input to MOVSS,
1789/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner35a08552007-02-25 07:10:00 +00001790static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1791 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001792 return false;
1793
Chris Lattner35a08552007-02-25 07:10:00 +00001794 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001795 return false;
1796
Chris Lattner35a08552007-02-25 07:10:00 +00001797 for (unsigned i = 1; i < NumElts; ++i) {
1798 if (!isUndefOrEqual(Elts[i], i))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001799 return false;
1800 }
1801
1802 return true;
1803}
Evan Chengf3b52c82006-04-05 07:20:06 +00001804
Evan Chenge8b51802006-04-21 01:05:10 +00001805bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001806 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001807 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001808}
1809
Evan Chenge8b51802006-04-21 01:05:10 +00001810/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1811/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001812/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner35a08552007-02-25 07:10:00 +00001813static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1814 bool V2IsSplat = false,
Evan Cheng89c5d042006-09-08 01:50:06 +00001815 bool V2IsUndef = false) {
Chris Lattner35a08552007-02-25 07:10:00 +00001816 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001817 return false;
1818
1819 if (!isUndefOrEqual(Ops[0], 0))
1820 return false;
1821
Chris Lattner35a08552007-02-25 07:10:00 +00001822 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001823 SDOperand Arg = Ops[i];
Chris Lattner35a08552007-02-25 07:10:00 +00001824 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1825 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1826 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng89c5d042006-09-08 01:50:06 +00001827 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001828 }
1829
1830 return true;
1831}
1832
Evan Cheng89c5d042006-09-08 01:50:06 +00001833static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1834 bool V2IsUndef = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001835 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001836 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1837 V2IsSplat, V2IsUndef);
Evan Cheng60f0b892006-04-20 08:58:49 +00001838}
1839
Evan Cheng5d247f82006-04-14 21:59:03 +00001840/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1841/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1842bool X86::isMOVSHDUPMask(SDNode *N) {
1843 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1844
1845 if (N->getNumOperands() != 4)
1846 return false;
1847
1848 // Expect 1, 1, 3, 3
1849 for (unsigned i = 0; i < 2; ++i) {
1850 SDOperand Arg = N->getOperand(i);
1851 if (Arg.getOpcode() == ISD::UNDEF) continue;
1852 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1853 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1854 if (Val != 1) return false;
1855 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001856
1857 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001858 for (unsigned i = 2; i < 4; ++i) {
1859 SDOperand Arg = N->getOperand(i);
1860 if (Arg.getOpcode() == ISD::UNDEF) continue;
1861 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1862 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1863 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001864 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001865 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001866
Evan Cheng6222cf22006-04-15 05:37:34 +00001867 // Don't use movshdup if it can be done with a shufps.
1868 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001869}
1870
1871/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1872/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1873bool X86::isMOVSLDUPMask(SDNode *N) {
1874 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1875
1876 if (N->getNumOperands() != 4)
1877 return false;
1878
1879 // Expect 0, 0, 2, 2
1880 for (unsigned i = 0; i < 2; ++i) {
1881 SDOperand Arg = N->getOperand(i);
1882 if (Arg.getOpcode() == ISD::UNDEF) continue;
1883 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1884 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1885 if (Val != 0) return false;
1886 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001887
1888 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001889 for (unsigned i = 2; i < 4; ++i) {
1890 SDOperand Arg = N->getOperand(i);
1891 if (Arg.getOpcode() == ISD::UNDEF) continue;
1892 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1893 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1894 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001895 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001896 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001897
Evan Cheng6222cf22006-04-15 05:37:34 +00001898 // Don't use movshdup if it can be done with a shufps.
1899 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001900}
1901
Evan Chengd097e672006-03-22 02:53:00 +00001902/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1903/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001904static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001905 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1906
Evan Chengd097e672006-03-22 02:53:00 +00001907 // This is a splat operation if each element of the permute is the same, and
1908 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001909 unsigned NumElems = N->getNumOperands();
1910 SDOperand ElementBase;
1911 unsigned i = 0;
1912 for (; i != NumElems; ++i) {
1913 SDOperand Elt = N->getOperand(i);
Reid Spencerde46e482006-11-02 20:25:50 +00001914 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001915 ElementBase = Elt;
1916 break;
1917 }
1918 }
1919
1920 if (!ElementBase.Val)
1921 return false;
1922
1923 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001924 SDOperand Arg = N->getOperand(i);
1925 if (Arg.getOpcode() == ISD::UNDEF) continue;
1926 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001927 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001928 }
1929
1930 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001931 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00001932}
1933
Evan Cheng5022b342006-04-17 20:43:08 +00001934/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1935/// a splat of a single element and it's a 2 or 4 element mask.
1936bool X86::isSplatMask(SDNode *N) {
1937 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1938
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001939 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00001940 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1941 return false;
1942 return ::isSplatMask(N);
1943}
1944
Evan Chenge056dd52006-10-27 21:08:32 +00001945/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
1946/// specifies a splat of zero element.
1947bool X86::isSplatLoMask(SDNode *N) {
1948 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1949
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001950 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chenge056dd52006-10-27 21:08:32 +00001951 if (!isUndefOrEqual(N->getOperand(i), 0))
1952 return false;
1953 return true;
1954}
1955
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001956/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1957/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1958/// instructions.
1959unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001960 unsigned NumOperands = N->getNumOperands();
1961 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1962 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001963 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001964 unsigned Val = 0;
1965 SDOperand Arg = N->getOperand(NumOperands-i-1);
1966 if (Arg.getOpcode() != ISD::UNDEF)
1967 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001968 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001969 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001970 if (i != NumOperands - 1)
1971 Mask <<= Shift;
1972 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001973
1974 return Mask;
1975}
1976
Evan Chengb7fedff2006-03-29 23:07:14 +00001977/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1978/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1979/// instructions.
1980unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1981 unsigned Mask = 0;
1982 // 8 nodes, but we only care about the last 4.
1983 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001984 unsigned Val = 0;
1985 SDOperand Arg = N->getOperand(i);
1986 if (Arg.getOpcode() != ISD::UNDEF)
1987 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001988 Mask |= (Val - 4);
1989 if (i != 4)
1990 Mask <<= 2;
1991 }
1992
1993 return Mask;
1994}
1995
1996/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1997/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1998/// instructions.
1999unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2000 unsigned Mask = 0;
2001 // 8 nodes, but we only care about the first 4.
2002 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002003 unsigned Val = 0;
2004 SDOperand Arg = N->getOperand(i);
2005 if (Arg.getOpcode() != ISD::UNDEF)
2006 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002007 Mask |= Val;
2008 if (i != 0)
2009 Mask <<= 2;
2010 }
2011
2012 return Mask;
2013}
2014
Evan Cheng59a63552006-04-05 01:47:37 +00002015/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2016/// specifies a 8 element shuffle that can be broken into a pair of
2017/// PSHUFHW and PSHUFLW.
2018static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2019 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2020
2021 if (N->getNumOperands() != 8)
2022 return false;
2023
2024 // Lower quadword shuffled.
2025 for (unsigned i = 0; i != 4; ++i) {
2026 SDOperand Arg = N->getOperand(i);
2027 if (Arg.getOpcode() == ISD::UNDEF) continue;
2028 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2029 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2030 if (Val > 4)
2031 return false;
2032 }
2033
2034 // Upper quadword shuffled.
2035 for (unsigned i = 4; i != 8; ++i) {
2036 SDOperand Arg = N->getOperand(i);
2037 if (Arg.getOpcode() == ISD::UNDEF) continue;
2038 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2039 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2040 if (Val < 4 || Val > 7)
2041 return false;
2042 }
2043
2044 return true;
2045}
2046
Evan Chengc995b452006-04-06 23:23:56 +00002047/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2048/// values in ther permute mask.
Evan Chengc415c5b2006-10-25 21:49:50 +00002049static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2050 SDOperand &V2, SDOperand &Mask,
2051 SelectionDAG &DAG) {
Evan Chengc995b452006-04-06 23:23:56 +00002052 MVT::ValueType VT = Op.getValueType();
2053 MVT::ValueType MaskVT = Mask.getValueType();
2054 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2055 unsigned NumElems = Mask.getNumOperands();
Chris Lattner35a08552007-02-25 07:10:00 +00002056 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc995b452006-04-06 23:23:56 +00002057
2058 for (unsigned i = 0; i != NumElems; ++i) {
2059 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002060 if (Arg.getOpcode() == ISD::UNDEF) {
2061 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2062 continue;
2063 }
Evan Chengc995b452006-04-06 23:23:56 +00002064 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2065 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2066 if (Val < NumElems)
2067 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2068 else
2069 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2070 }
2071
Evan Chengc415c5b2006-10-25 21:49:50 +00002072 std::swap(V1, V2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002073 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc415c5b2006-10-25 21:49:50 +00002074 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chengc995b452006-04-06 23:23:56 +00002075}
2076
Evan Cheng7855e4d2006-04-19 20:35:22 +00002077/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2078/// match movhlps. The lower half elements should come from upper half of
2079/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002080/// half of V2 (and in order).
Evan Cheng7855e4d2006-04-19 20:35:22 +00002081static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2082 unsigned NumElems = Mask->getNumOperands();
2083 if (NumElems != 4)
2084 return false;
2085 for (unsigned i = 0, e = 2; i != e; ++i)
2086 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2087 return false;
2088 for (unsigned i = 2; i != 4; ++i)
2089 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2090 return false;
2091 return true;
2092}
2093
Evan Chengc995b452006-04-06 23:23:56 +00002094/// isScalarLoadToVector - Returns true if the node is a scalar load that
2095/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002096static inline bool isScalarLoadToVector(SDNode *N) {
2097 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2098 N = N->getOperand(0).Val;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002099 return ISD::isNON_EXTLoad(N);
Evan Chengc995b452006-04-06 23:23:56 +00002100 }
2101 return false;
2102}
2103
Evan Cheng7855e4d2006-04-19 20:35:22 +00002104/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2105/// match movlp{s|d}. The lower half elements should come from lower half of
2106/// V1 (and in order), and the upper half elements should come from the upper
2107/// half of V2 (and in order). And since V1 will become the source of the
2108/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Chenge646abb2006-10-09 21:39:25 +00002109static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002110 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng7855e4d2006-04-19 20:35:22 +00002111 return false;
Evan Chenge646abb2006-10-09 21:39:25 +00002112 // Is V2 is a vector load, don't do this transformation. We will try to use
2113 // load folding shufps op.
2114 if (ISD::isNON_EXTLoad(V2))
2115 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002116
Evan Cheng7855e4d2006-04-19 20:35:22 +00002117 unsigned NumElems = Mask->getNumOperands();
2118 if (NumElems != 2 && NumElems != 4)
2119 return false;
2120 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2121 if (!isUndefOrEqual(Mask->getOperand(i), i))
2122 return false;
2123 for (unsigned i = NumElems/2; i != NumElems; ++i)
2124 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2125 return false;
2126 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002127}
2128
Evan Cheng60f0b892006-04-20 08:58:49 +00002129/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2130/// all the same.
2131static bool isSplatVector(SDNode *N) {
2132 if (N->getOpcode() != ISD::BUILD_VECTOR)
2133 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002134
Evan Cheng60f0b892006-04-20 08:58:49 +00002135 SDOperand SplatValue = N->getOperand(0);
2136 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2137 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002138 return false;
2139 return true;
2140}
2141
Evan Cheng89c5d042006-09-08 01:50:06 +00002142/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2143/// to an undef.
2144static bool isUndefShuffle(SDNode *N) {
2145 if (N->getOpcode() != ISD::BUILD_VECTOR)
2146 return false;
2147
2148 SDOperand V1 = N->getOperand(0);
2149 SDOperand V2 = N->getOperand(1);
2150 SDOperand Mask = N->getOperand(2);
2151 unsigned NumElems = Mask.getNumOperands();
2152 for (unsigned i = 0; i != NumElems; ++i) {
2153 SDOperand Arg = Mask.getOperand(i);
2154 if (Arg.getOpcode() != ISD::UNDEF) {
2155 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2156 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2157 return false;
2158 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2159 return false;
2160 }
2161 }
2162 return true;
2163}
2164
Evan Cheng60f0b892006-04-20 08:58:49 +00002165/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2166/// that point to V2 points to its first element.
2167static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2168 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2169
2170 bool Changed = false;
Chris Lattner35a08552007-02-25 07:10:00 +00002171 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002172 unsigned NumElems = Mask.getNumOperands();
2173 for (unsigned i = 0; i != NumElems; ++i) {
2174 SDOperand Arg = Mask.getOperand(i);
2175 if (Arg.getOpcode() != ISD::UNDEF) {
2176 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2177 if (Val > NumElems) {
2178 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2179 Changed = true;
2180 }
2181 }
2182 MaskVec.push_back(Arg);
2183 }
2184
2185 if (Changed)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002186 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2187 &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002188 return Mask;
2189}
2190
Evan Chenge8b51802006-04-21 01:05:10 +00002191/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2192/// operation of specified width.
2193static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002194 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2195 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2196
Chris Lattner35a08552007-02-25 07:10:00 +00002197 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002198 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2199 for (unsigned i = 1; i != NumElems; ++i)
2200 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002201 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002202}
2203
Evan Cheng5022b342006-04-17 20:43:08 +00002204/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2205/// of specified width.
2206static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2207 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2208 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002209 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5022b342006-04-17 20:43:08 +00002210 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2211 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2212 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2213 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002214 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng5022b342006-04-17 20:43:08 +00002215}
2216
Evan Cheng60f0b892006-04-20 08:58:49 +00002217/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2218/// of specified width.
2219static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2220 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2221 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2222 unsigned Half = NumElems/2;
Chris Lattner35a08552007-02-25 07:10:00 +00002223 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002224 for (unsigned i = 0; i != Half; ++i) {
2225 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2226 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2227 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002228 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002229}
2230
Evan Chenge8b51802006-04-21 01:05:10 +00002231/// getZeroVector - Returns a vector of specified type with all zero elements.
2232///
2233static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2234 assert(MVT::isVector(VT) && "Expected a vector type");
2235 unsigned NumElems = getVectorNumElements(VT);
2236 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2237 bool isFP = MVT::isFloatingPoint(EVT);
2238 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002239 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002240 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
Evan Chenge8b51802006-04-21 01:05:10 +00002241}
2242
Evan Cheng5022b342006-04-17 20:43:08 +00002243/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2244///
2245static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2246 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002247 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002248 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002249 unsigned NumElems = Mask.getNumOperands();
2250 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002251 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002252 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002253 NumElems >>= 1;
2254 }
2255 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2256
2257 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002258 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002259 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002260 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002261 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2262}
2263
Evan Chenge8b51802006-04-21 01:05:10 +00002264/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2265/// constant +0.0.
2266static inline bool isZeroNode(SDOperand Elt) {
2267 return ((isa<ConstantSDNode>(Elt) &&
2268 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2269 (isa<ConstantFPSDNode>(Elt) &&
2270 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2271}
2272
Evan Cheng14215c32006-04-21 23:03:30 +00002273/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2274/// vector and zero or undef vector.
2275static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002276 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002277 bool isZero, SelectionDAG &DAG) {
2278 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002279 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2280 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2281 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002282 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Chenge8b51802006-04-21 01:05:10 +00002283 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002284 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2285 &MaskVec[0], MaskVec.size());
Evan Cheng14215c32006-04-21 23:03:30 +00002286 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002287}
2288
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002289/// LowerBuildVectorv8i8 - Custom lower build_vector of v8i8.
2290///
2291static SDOperand LowerBuildVectorv8i8(SDOperand Op, unsigned NonZeros,
2292 unsigned NumNonZero, unsigned NumZero,
2293 SelectionDAG &DAG, TargetLowering &TLI) {
2294 if (NumNonZero > 8)
2295 return SDOperand();
2296
2297 SDOperand V(0, 0);
2298 bool First = true;
2299 for (unsigned i = 0; i < 8; ++i) {
2300 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2301 if (ThisIsNonZero && First) {
2302 if (NumZero)
2303 V = getZeroVector(MVT::v4i16, DAG);
2304 else
2305 V = DAG.getNode(ISD::UNDEF, MVT::v4i16);
2306 First = false;
2307 }
2308
2309 if ((i & 1) != 0) {
2310 SDOperand ThisElt(0, 0), LastElt(0, 0);
2311 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2312 if (LastIsNonZero) {
2313 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2314 }
2315 if (ThisIsNonZero) {
2316 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2317 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2318 ThisElt, DAG.getConstant(8, MVT::i8));
2319 if (LastIsNonZero)
2320 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2321 } else
2322 ThisElt = LastElt;
2323
2324 if (ThisElt.Val)
2325 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v4i16, V, ThisElt,
2326 DAG.getConstant(i/2, TLI.getPointerTy()));
2327 }
2328 }
2329
2330 return DAG.getNode(ISD::BIT_CONVERT, MVT::v8i8, V);
2331}
2332
2333/// LowerBuildVectorv4i16 - Custom lower build_vector of v4i16.
2334///
2335static SDOperand LowerBuildVectorv4i16(SDOperand Op, unsigned NonZeros,
2336 unsigned NumNonZero, unsigned NumZero,
2337 SelectionDAG &DAG, TargetLowering &TLI) {
2338 if (NumNonZero > 4)
2339 return SDOperand();
2340
2341 SDOperand V(0, 0);
2342 bool First = true;
2343 for (unsigned i = 0; i < 4; ++i) {
2344 bool isNonZero = (NonZeros & (1 << i)) != 0;
2345 if (isNonZero) {
2346 if (First) {
2347 if (NumZero)
2348 V = getZeroVector(MVT::v4i16, DAG);
2349 else
2350 V = DAG.getNode(ISD::UNDEF, MVT::v4i16);
2351 First = false;
2352 }
2353 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v4i16, V, Op.getOperand(i),
2354 DAG.getConstant(i, TLI.getPointerTy()));
2355 }
2356 }
2357
2358 return V;
2359}
2360
Evan Chengb0461082006-04-24 18:01:45 +00002361/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2362///
2363static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2364 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002365 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002366 if (NumNonZero > 8)
2367 return SDOperand();
2368
2369 SDOperand V(0, 0);
2370 bool First = true;
2371 for (unsigned i = 0; i < 16; ++i) {
2372 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2373 if (ThisIsNonZero && First) {
2374 if (NumZero)
2375 V = getZeroVector(MVT::v8i16, DAG);
2376 else
2377 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2378 First = false;
2379 }
2380
2381 if ((i & 1) != 0) {
2382 SDOperand ThisElt(0, 0), LastElt(0, 0);
2383 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2384 if (LastIsNonZero) {
2385 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2386 }
2387 if (ThisIsNonZero) {
2388 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2389 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2390 ThisElt, DAG.getConstant(8, MVT::i8));
2391 if (LastIsNonZero)
2392 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2393 } else
2394 ThisElt = LastElt;
2395
2396 if (ThisElt.Val)
2397 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002398 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00002399 }
2400 }
2401
2402 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2403}
2404
Bill Wendlingd551a182007-03-22 18:42:45 +00002405/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengb0461082006-04-24 18:01:45 +00002406///
2407static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2408 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002409 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002410 if (NumNonZero > 4)
2411 return SDOperand();
2412
2413 SDOperand V(0, 0);
2414 bool First = true;
2415 for (unsigned i = 0; i < 8; ++i) {
2416 bool isNonZero = (NonZeros & (1 << i)) != 0;
2417 if (isNonZero) {
2418 if (First) {
2419 if (NumZero)
2420 V = getZeroVector(MVT::v8i16, DAG);
2421 else
2422 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2423 First = false;
2424 }
2425 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002426 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00002427 }
2428 }
2429
2430 return V;
2431}
2432
Evan Chenga9467aa2006-04-25 20:13:52 +00002433SDOperand
2434X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2435 // All zero's are handled with pxor.
2436 if (ISD::isBuildVectorAllZeros(Op.Val))
2437 return Op;
2438
2439 // All one's are handled with pcmpeqd.
2440 if (ISD::isBuildVectorAllOnes(Op.Val))
2441 return Op;
2442
2443 MVT::ValueType VT = Op.getValueType();
2444 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2445 unsigned EVTBits = MVT::getSizeInBits(EVT);
2446
2447 unsigned NumElems = Op.getNumOperands();
2448 unsigned NumZero = 0;
2449 unsigned NumNonZero = 0;
2450 unsigned NonZeros = 0;
2451 std::set<SDOperand> Values;
2452 for (unsigned i = 0; i < NumElems; ++i) {
2453 SDOperand Elt = Op.getOperand(i);
2454 if (Elt.getOpcode() != ISD::UNDEF) {
2455 Values.insert(Elt);
2456 if (isZeroNode(Elt))
2457 NumZero++;
2458 else {
2459 NonZeros |= (1 << i);
2460 NumNonZero++;
2461 }
2462 }
2463 }
2464
2465 if (NumNonZero == 0)
2466 // Must be a mix of zero and undef. Return a zero vector.
2467 return getZeroVector(VT, DAG);
2468
2469 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2470 if (Values.size() == 1)
2471 return SDOperand();
2472
2473 // Special case for single non-zero element.
Evan Cheng798b3062006-10-25 20:48:19 +00002474 if (NumNonZero == 1) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002475 unsigned Idx = CountTrailingZeros_32(NonZeros);
2476 SDOperand Item = Op.getOperand(Idx);
2477 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2478 if (Idx == 0)
2479 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2480 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2481 NumZero > 0, DAG);
2482
2483 if (EVTBits == 32) {
2484 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2485 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2486 DAG);
2487 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2488 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002489 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002490 for (unsigned i = 0; i < NumElems; i++)
2491 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002492 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2493 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002494 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2495 DAG.getNode(ISD::UNDEF, VT), Mask);
2496 }
2497 }
2498
Evan Cheng8c5766e2006-10-04 18:33:38 +00002499 // Let legalizer expand 2-wide build_vector's.
Evan Chenga9467aa2006-04-25 20:13:52 +00002500 if (EVTBits == 64)
2501 return SDOperand();
2502
2503 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002504 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002505 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2506 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00002507 if (V.Val) return V;
2508 }
2509
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002510 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002511 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2512 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00002513 if (V.Val) return V;
2514 }
2515
2516 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner35a08552007-02-25 07:10:00 +00002517 SmallVector<SDOperand, 8> V;
2518 V.resize(NumElems);
Evan Chenga9467aa2006-04-25 20:13:52 +00002519 if (NumElems == 4 && NumZero > 0) {
2520 for (unsigned i = 0; i < 4; ++i) {
2521 bool isZero = !(NonZeros & (1 << i));
2522 if (isZero)
2523 V[i] = getZeroVector(VT, DAG);
2524 else
2525 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2526 }
2527
2528 for (unsigned i = 0; i < 2; ++i) {
2529 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2530 default: break;
2531 case 0:
2532 V[i] = V[i*2]; // Must be a zero vector.
2533 break;
2534 case 1:
2535 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2536 getMOVLMask(NumElems, DAG));
2537 break;
2538 case 2:
2539 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2540 getMOVLMask(NumElems, DAG));
2541 break;
2542 case 3:
2543 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2544 getUnpacklMask(NumElems, DAG));
2545 break;
2546 }
2547 }
2548
Evan Cheng9fee4422006-05-16 07:21:53 +00002549 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002550 // clears the upper bits.
Evan Chenga9467aa2006-04-25 20:13:52 +00002551 // FIXME: we can do the same for v4f32 case when we know both parts of
2552 // the lower half come from scalar_to_vector (loadf32). We should do
2553 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng798b3062006-10-25 20:48:19 +00002554 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Chenga9467aa2006-04-25 20:13:52 +00002555 return V[0];
2556 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2557 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002558 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002559 bool Reverse = (NonZeros & 0x3) == 2;
2560 for (unsigned i = 0; i < 2; ++i)
2561 if (Reverse)
2562 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2563 else
2564 MaskVec.push_back(DAG.getConstant(i, EVT));
2565 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2566 for (unsigned i = 0; i < 2; ++i)
2567 if (Reverse)
2568 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2569 else
2570 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnered728e82006-08-11 17:38:39 +00002571 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2572 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002573 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2574 }
2575
2576 if (Values.size() > 2) {
2577 // Expand into a number of unpckl*.
2578 // e.g. for v4f32
2579 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2580 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2581 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2582 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2583 for (unsigned i = 0; i < NumElems; ++i)
2584 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2585 NumElems >>= 1;
2586 while (NumElems != 0) {
2587 for (unsigned i = 0; i < NumElems; ++i)
2588 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2589 UnpckMask);
2590 NumElems >>= 1;
2591 }
2592 return V[0];
2593 }
2594
2595 return SDOperand();
2596}
2597
2598SDOperand
2599X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2600 SDOperand V1 = Op.getOperand(0);
2601 SDOperand V2 = Op.getOperand(1);
2602 SDOperand PermMask = Op.getOperand(2);
2603 MVT::ValueType VT = Op.getValueType();
2604 unsigned NumElems = PermMask.getNumOperands();
2605 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2606 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Cheng949bcc92006-10-16 06:36:00 +00002607 bool V1IsSplat = false;
2608 bool V2IsSplat = false;
Evan Chenga9467aa2006-04-25 20:13:52 +00002609
Evan Cheng89c5d042006-09-08 01:50:06 +00002610 if (isUndefShuffle(Op.Val))
2611 return DAG.getNode(ISD::UNDEF, VT);
2612
Evan Chenga9467aa2006-04-25 20:13:52 +00002613 if (isSplatMask(PermMask.Val)) {
2614 if (NumElems <= 4) return Op;
2615 // Promote it to a v4i32 splat.
Evan Cheng798b3062006-10-25 20:48:19 +00002616 return PromoteSplat(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00002617 }
2618
Evan Cheng798b3062006-10-25 20:48:19 +00002619 if (X86::isMOVLMask(PermMask.Val))
2620 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002621
Evan Cheng798b3062006-10-25 20:48:19 +00002622 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2623 X86::isMOVSLDUPMask(PermMask.Val) ||
2624 X86::isMOVHLPSMask(PermMask.Val) ||
2625 X86::isMOVHPMask(PermMask.Val) ||
2626 X86::isMOVLPMask(PermMask.Val))
2627 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002628
Evan Cheng798b3062006-10-25 20:48:19 +00002629 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2630 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Chengc415c5b2006-10-25 21:49:50 +00002631 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00002632
Evan Chengc415c5b2006-10-25 21:49:50 +00002633 bool Commuted = false;
Evan Cheng798b3062006-10-25 20:48:19 +00002634 V1IsSplat = isSplatVector(V1.Val);
2635 V2IsSplat = isSplatVector(V2.Val);
2636 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Chengc415c5b2006-10-25 21:49:50 +00002637 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00002638 std::swap(V1IsSplat, V2IsSplat);
2639 std::swap(V1IsUndef, V2IsUndef);
Evan Chengc415c5b2006-10-25 21:49:50 +00002640 Commuted = true;
Evan Cheng798b3062006-10-25 20:48:19 +00002641 }
2642
2643 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2644 if (V2IsUndef) return V1;
Evan Chengc415c5b2006-10-25 21:49:50 +00002645 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00002646 if (V2IsSplat) {
2647 // V2 is a splat, so the mask may be malformed. That is, it may point
2648 // to any V2 element. The instruction selectior won't like this. Get
2649 // a corrected mask and commute to form a proper MOVS{S|D}.
2650 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2651 if (NewMask.Val != PermMask.Val)
2652 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002653 }
Evan Cheng798b3062006-10-25 20:48:19 +00002654 return Op;
Evan Cheng949bcc92006-10-16 06:36:00 +00002655 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002656
Evan Cheng949bcc92006-10-16 06:36:00 +00002657 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2658 X86::isUNPCKLMask(PermMask.Val) ||
2659 X86::isUNPCKHMask(PermMask.Val))
2660 return Op;
Evan Cheng8c5766e2006-10-04 18:33:38 +00002661
Evan Cheng798b3062006-10-25 20:48:19 +00002662 if (V2IsSplat) {
2663 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002664 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng798b3062006-10-25 20:48:19 +00002665 // new vector_shuffle with the corrected mask.
2666 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2667 if (NewMask.Val != PermMask.Val) {
2668 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2669 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2670 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2671 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2672 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2673 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002674 }
2675 }
2676 }
2677
2678 // Normalize the node to match x86 shuffle ops if needed
Evan Chengc415c5b2006-10-25 21:49:50 +00002679 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2680 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2681
2682 if (Commuted) {
2683 // Commute is back and try unpck* again.
2684 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2685 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2686 X86::isUNPCKLMask(PermMask.Val) ||
2687 X86::isUNPCKHMask(PermMask.Val))
2688 return Op;
2689 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002690
2691 // If VT is integer, try PSHUF* first, then SHUFP*.
2692 if (MVT::isInteger(VT)) {
2693 if (X86::isPSHUFDMask(PermMask.Val) ||
2694 X86::isPSHUFHWMask(PermMask.Val) ||
2695 X86::isPSHUFLWMask(PermMask.Val)) {
2696 if (V2.getOpcode() != ISD::UNDEF)
2697 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2698 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2699 return Op;
2700 }
2701
2702 if (X86::isSHUFPMask(PermMask.Val))
2703 return Op;
2704
2705 // Handle v8i16 shuffle high / low shuffle node pair.
2706 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2707 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2708 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002709 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002710 for (unsigned i = 0; i != 4; ++i)
2711 MaskVec.push_back(PermMask.getOperand(i));
2712 for (unsigned i = 4; i != 8; ++i)
2713 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnered728e82006-08-11 17:38:39 +00002714 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2715 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002716 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2717 MaskVec.clear();
2718 for (unsigned i = 0; i != 4; ++i)
2719 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2720 for (unsigned i = 4; i != 8; ++i)
2721 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnered728e82006-08-11 17:38:39 +00002722 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002723 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2724 }
2725 } else {
2726 // Floating point cases in the other order.
2727 if (X86::isSHUFPMask(PermMask.Val))
2728 return Op;
2729 if (X86::isPSHUFDMask(PermMask.Val) ||
2730 X86::isPSHUFHWMask(PermMask.Val) ||
2731 X86::isPSHUFLWMask(PermMask.Val)) {
2732 if (V2.getOpcode() != ISD::UNDEF)
2733 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2734 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2735 return Op;
2736 }
2737 }
2738
2739 if (NumElems == 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002740 MVT::ValueType MaskVT = PermMask.getValueType();
2741 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002742 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng3cd43622006-04-28 07:03:38 +00002743 Locs.reserve(NumElems);
Chris Lattner35a08552007-02-25 07:10:00 +00002744 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2745 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng3cd43622006-04-28 07:03:38 +00002746 unsigned NumHi = 0;
2747 unsigned NumLo = 0;
2748 // If no more than two elements come from either vector. This can be
2749 // implemented with two shuffles. First shuffle gather the elements.
2750 // The second shuffle, which takes the first shuffle as both of its
2751 // vector operands, put the elements into the right order.
2752 for (unsigned i = 0; i != NumElems; ++i) {
2753 SDOperand Elt = PermMask.getOperand(i);
2754 if (Elt.getOpcode() == ISD::UNDEF) {
2755 Locs[i] = std::make_pair(-1, -1);
2756 } else {
2757 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2758 if (Val < NumElems) {
2759 Locs[i] = std::make_pair(0, NumLo);
2760 Mask1[NumLo] = Elt;
2761 NumLo++;
2762 } else {
2763 Locs[i] = std::make_pair(1, NumHi);
2764 if (2+NumHi < NumElems)
2765 Mask1[2+NumHi] = Elt;
2766 NumHi++;
2767 }
2768 }
2769 }
2770 if (NumLo <= 2 && NumHi <= 2) {
2771 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002772 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2773 &Mask1[0], Mask1.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00002774 for (unsigned i = 0; i != NumElems; ++i) {
2775 if (Locs[i].first == -1)
2776 continue;
2777 else {
2778 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2779 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2780 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2781 }
2782 }
2783
2784 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnered728e82006-08-11 17:38:39 +00002785 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2786 &Mask2[0], Mask2.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00002787 }
2788
2789 // Break it into (shuffle shuffle_hi, shuffle_lo).
2790 Locs.clear();
Chris Lattner35a08552007-02-25 07:10:00 +00002791 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2792 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2793 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Chenga9467aa2006-04-25 20:13:52 +00002794 unsigned MaskIdx = 0;
2795 unsigned LoIdx = 0;
2796 unsigned HiIdx = NumElems/2;
2797 for (unsigned i = 0; i != NumElems; ++i) {
2798 if (i == NumElems/2) {
2799 MaskPtr = &HiMask;
2800 MaskIdx = 1;
2801 LoIdx = 0;
2802 HiIdx = NumElems/2;
2803 }
2804 SDOperand Elt = PermMask.getOperand(i);
2805 if (Elt.getOpcode() == ISD::UNDEF) {
2806 Locs[i] = std::make_pair(-1, -1);
2807 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2808 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2809 (*MaskPtr)[LoIdx] = Elt;
2810 LoIdx++;
2811 } else {
2812 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2813 (*MaskPtr)[HiIdx] = Elt;
2814 HiIdx++;
2815 }
2816 }
2817
Chris Lattner3d826992006-05-16 06:45:34 +00002818 SDOperand LoShuffle =
2819 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002820 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2821 &LoMask[0], LoMask.size()));
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002822 SDOperand HiShuffle =
Chris Lattner3d826992006-05-16 06:45:34 +00002823 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002824 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2825 &HiMask[0], HiMask.size()));
Chris Lattner35a08552007-02-25 07:10:00 +00002826 SmallVector<SDOperand, 8> MaskOps;
Evan Chenga9467aa2006-04-25 20:13:52 +00002827 for (unsigned i = 0; i != NumElems; ++i) {
2828 if (Locs[i].first == -1) {
2829 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2830 } else {
2831 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2832 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2833 }
2834 }
2835 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnered728e82006-08-11 17:38:39 +00002836 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2837 &MaskOps[0], MaskOps.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002838 }
2839
2840 return SDOperand();
2841}
2842
2843SDOperand
2844X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2845 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2846 return SDOperand();
2847
2848 MVT::ValueType VT = Op.getValueType();
2849 // TODO: handle v16i8.
2850 if (MVT::getSizeInBits(VT) == 16) {
2851 // Transform it so it match pextrw which produces a 32-bit result.
2852 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2853 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2854 Op.getOperand(0), Op.getOperand(1));
2855 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2856 DAG.getValueType(VT));
2857 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2858 } else if (MVT::getSizeInBits(VT) == 32) {
2859 SDOperand Vec = Op.getOperand(0);
2860 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2861 if (Idx == 0)
2862 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002863 // SHUFPS the element to the lowest double word, then movss.
2864 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00002865 SmallVector<SDOperand, 8> IdxVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002866 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2867 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2868 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2869 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00002870 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2871 &IdxVec[0], IdxVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002872 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng922e1912006-11-07 22:14:24 +00002873 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002874 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00002875 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002876 } else if (MVT::getSizeInBits(VT) == 64) {
2877 SDOperand Vec = Op.getOperand(0);
2878 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2879 if (Idx == 0)
2880 return Op;
2881
2882 // UNPCKHPD the element to the lowest double word, then movsd.
2883 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2884 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2885 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00002886 SmallVector<SDOperand, 8> IdxVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002887 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2888 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00002889 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2890 &IdxVec[0], IdxVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002891 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2892 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2893 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00002894 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002895 }
2896
2897 return SDOperand();
2898}
2899
2900SDOperand
2901X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002902 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002903 // as its second argument.
2904 MVT::ValueType VT = Op.getValueType();
2905 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2906 SDOperand N0 = Op.getOperand(0);
2907 SDOperand N1 = Op.getOperand(1);
2908 SDOperand N2 = Op.getOperand(2);
2909 if (MVT::getSizeInBits(BaseVT) == 16) {
2910 if (N1.getValueType() != MVT::i32)
2911 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2912 if (N2.getValueType() != MVT::i32)
2913 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2914 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2915 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2916 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2917 if (Idx == 0) {
2918 // Use a movss.
2919 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2920 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2921 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002922 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002923 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2924 for (unsigned i = 1; i <= 3; ++i)
2925 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnered728e82006-08-11 17:38:39 +00002927 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2928 &MaskVec[0], MaskVec.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002929 } else {
2930 // Use two pinsrw instructions to insert a 32 bit value.
2931 Idx <<= 1;
2932 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002933 if (ISD::isNON_EXTLoad(N1.Val)) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002934 // Just load directly from f32mem to GR32.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002935 LoadSDNode *LD = cast<LoadSDNode>(N1);
2936 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2937 LD->getSrcValue(), LD->getSrcValueOffset());
Evan Chenga9467aa2006-04-25 20:13:52 +00002938 } else {
2939 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2940 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2941 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002942 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002943 }
2944 }
2945 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2946 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002947 DAG.getConstant(Idx, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002948 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2949 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002950 DAG.getConstant(Idx+1, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002951 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2952 }
2953 }
2954
2955 return SDOperand();
2956}
2957
2958SDOperand
2959X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2960 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2961 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2962}
2963
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002964// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Chenga9467aa2006-04-25 20:13:52 +00002965// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2966// one of the above mentioned nodes. It has to be wrapped because otherwise
2967// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2968// be used to form addressing mode. These wrapped nodes will be selected
2969// into MOV32ri.
2970SDOperand
2971X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2972 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng0b169222006-11-29 23:19:46 +00002973 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
2974 getPointerTy(),
2975 CP->getAlignment());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00002976 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002977 // With PIC, the address is actually $g + Offset.
2978 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2979 !Subtarget->isPICStyleRIPRel()) {
2980 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2981 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2982 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002983 }
2984
2985 return Result;
2986}
2987
2988SDOperand
2989X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2990 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng0b169222006-11-29 23:19:46 +00002991 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00002992 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002993 // With PIC, the address is actually $g + Offset.
2994 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2995 !Subtarget->isPICStyleRIPRel()) {
2996 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2997 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2998 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00002999 }
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00003000
3001 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3002 // load the value at address GV, not the value of GV itself. This means that
3003 // the GlobalAddress must be in the base or index register of the address, not
3004 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003005 // The same applies for external symbols during PIC codegen
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00003006 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3007 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003008
3009 return Result;
3010}
3011
3012SDOperand
3013X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3014 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Cheng0b169222006-11-29 23:19:46 +00003015 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003016 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003017 // With PIC, the address is actually $g + Offset.
3018 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3019 !Subtarget->isPICStyleRIPRel()) {
3020 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3021 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3022 Result);
3023 }
3024
3025 return Result;
3026}
3027
3028SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3029 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3030 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3031 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3032 // With PIC, the address is actually $g + Offset.
3033 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3034 !Subtarget->isPICStyleRIPRel()) {
3035 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3036 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3037 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003038 }
3039
3040 return Result;
3041}
3042
3043SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00003044 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3045 "Not an i64 shift!");
3046 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3047 SDOperand ShOpLo = Op.getOperand(0);
3048 SDOperand ShOpHi = Op.getOperand(1);
3049 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003050 SDOperand Tmp1 = isSRA ?
3051 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3052 DAG.getConstant(0, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00003053
3054 SDOperand Tmp2, Tmp3;
3055 if (Op.getOpcode() == ISD::SHL_PARTS) {
3056 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3057 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3058 } else {
3059 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00003060 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00003061 }
3062
Evan Cheng4259a0f2006-09-11 02:19:56 +00003063 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3064 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3065 DAG.getConstant(32, MVT::i8));
3066 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3067 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Cheng9c249c32006-01-09 18:33:28 +00003068
3069 SDOperand Hi, Lo;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003070 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00003071
Evan Cheng4259a0f2006-09-11 02:19:56 +00003072 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3073 SmallVector<SDOperand, 4> Ops;
Evan Cheng9c249c32006-01-09 18:33:28 +00003074 if (Op.getOpcode() == ISD::SHL_PARTS) {
3075 Ops.push_back(Tmp2);
3076 Ops.push_back(Tmp3);
3077 Ops.push_back(CC);
3078 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003079 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003080 InFlag = Hi.getValue(1);
3081
3082 Ops.clear();
3083 Ops.push_back(Tmp3);
3084 Ops.push_back(Tmp1);
3085 Ops.push_back(CC);
3086 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003087 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003088 } else {
3089 Ops.push_back(Tmp2);
3090 Ops.push_back(Tmp3);
3091 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00003092 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003093 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003094 InFlag = Lo.getValue(1);
3095
3096 Ops.clear();
3097 Ops.push_back(Tmp3);
3098 Ops.push_back(Tmp1);
3099 Ops.push_back(CC);
3100 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003101 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003102 }
3103
Evan Cheng4259a0f2006-09-11 02:19:56 +00003104 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00003105 Ops.clear();
3106 Ops.push_back(Lo);
3107 Ops.push_back(Hi);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003108 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003109}
Evan Cheng6305e502006-01-12 22:54:21 +00003110
Evan Chenga9467aa2006-04-25 20:13:52 +00003111SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3112 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3113 Op.getOperand(0).getValueType() >= MVT::i16 &&
3114 "Unknown SINT_TO_FP to lower!");
3115
3116 SDOperand Result;
3117 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3118 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3119 MachineFunction &MF = DAG.getMachineFunction();
3120 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3121 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chengdf9ac472006-10-05 23:01:46 +00003122 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Chengab51cf22006-10-13 21:14:26 +00003123 StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003124
3125 // Build the FILD
Chris Lattner35a08552007-02-25 07:10:00 +00003126 SDVTList Tys;
3127 if (X86ScalarSSE)
3128 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3129 else
3130 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3131 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003132 Ops.push_back(Chain);
3133 Ops.push_back(StackSlot);
3134 Ops.push_back(DAG.getValueType(SrcVT));
3135 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003136 Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003137
3138 if (X86ScalarSSE) {
3139 Chain = Result.getValue(1);
3140 SDOperand InFlag = Result.getValue(2);
3141
3142 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3143 // shouldn't be necessary except that RFP cannot be live across
3144 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003145 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003146 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003147 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner35a08552007-02-25 07:10:00 +00003148 Tys = DAG.getVTList(MVT::Other);
3149 SmallVector<SDOperand, 8> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003150 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003151 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003152 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003153 Ops.push_back(DAG.getValueType(Op.getValueType()));
3154 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003155 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003156 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattner76ac0682005-11-15 00:40:23 +00003157 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003158
Evan Chenga9467aa2006-04-25 20:13:52 +00003159 return Result;
3160}
3161
3162SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3163 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3164 "Unknown FP_TO_SINT to lower!");
3165 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3166 // stack slot.
3167 MachineFunction &MF = DAG.getMachineFunction();
3168 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3169 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3170 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3171
3172 unsigned Opc;
3173 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003174 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3175 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3176 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3177 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003178 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003179
Evan Chenga9467aa2006-04-25 20:13:52 +00003180 SDOperand Chain = DAG.getEntryNode();
3181 SDOperand Value = Op.getOperand(0);
3182 if (X86ScalarSSE) {
3183 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Chengab51cf22006-10-13 21:14:26 +00003184 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Chris Lattner35a08552007-02-25 07:10:00 +00003185 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3186 SDOperand Ops[] = {
3187 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3188 };
3189 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003190 Chain = Value.getValue(1);
3191 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3192 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3193 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003194
Evan Chenga9467aa2006-04-25 20:13:52 +00003195 // Build the FP_TO_INT*_IN_MEM
Chris Lattner35a08552007-02-25 07:10:00 +00003196 SDOperand Ops[] = { Chain, Value, StackSlot };
3197 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Cheng172fce72006-01-06 00:43:03 +00003198
Evan Chenga9467aa2006-04-25 20:13:52 +00003199 // Load the result.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003200 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003201}
3202
3203SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3204 MVT::ValueType VT = Op.getValueType();
3205 const Type *OpNTy = MVT::getTypeForValueType(VT);
3206 std::vector<Constant*> CV;
3207 if (VT == MVT::f64) {
3208 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3209 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3210 } else {
3211 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3212 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3213 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3214 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3215 }
3216 Constant *CS = ConstantStruct::get(CV);
3217 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner35a08552007-02-25 07:10:00 +00003218 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00003219 SmallVector<SDOperand, 3> Ops;
3220 Ops.push_back(DAG.getEntryNode());
3221 Ops.push_back(CPIdx);
3222 Ops.push_back(DAG.getSrcValue(NULL));
3223 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003224 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3225}
3226
3227SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3228 MVT::ValueType VT = Op.getValueType();
3229 const Type *OpNTy = MVT::getTypeForValueType(VT);
3230 std::vector<Constant*> CV;
3231 if (VT == MVT::f64) {
3232 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3233 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3234 } else {
3235 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3236 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3237 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3238 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3239 }
3240 Constant *CS = ConstantStruct::get(CV);
3241 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner35a08552007-02-25 07:10:00 +00003242 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00003243 SmallVector<SDOperand, 3> Ops;
3244 Ops.push_back(DAG.getEntryNode());
3245 Ops.push_back(CPIdx);
3246 Ops.push_back(DAG.getSrcValue(NULL));
3247 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003248 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3249}
3250
Evan Cheng4363e882007-01-05 07:55:56 +00003251SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng82241c82007-01-05 21:37:56 +00003252 SDOperand Op0 = Op.getOperand(0);
3253 SDOperand Op1 = Op.getOperand(1);
Evan Cheng4363e882007-01-05 07:55:56 +00003254 MVT::ValueType VT = Op.getValueType();
Evan Cheng82241c82007-01-05 21:37:56 +00003255 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng4363e882007-01-05 07:55:56 +00003256 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00003257
3258 // If second operand is smaller, extend it first.
3259 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3260 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3261 SrcVT = VT;
3262 }
3263
Evan Cheng4363e882007-01-05 07:55:56 +00003264 // First get the sign bit of second operand.
3265 std::vector<Constant*> CV;
3266 if (SrcVT == MVT::f64) {
3267 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3268 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3269 } else {
3270 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3271 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3272 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3273 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3274 }
3275 Constant *CS = ConstantStruct::get(CV);
3276 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnere56fef92007-02-25 06:40:16 +00003277 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
Evan Cheng4363e882007-01-05 07:55:56 +00003278 SmallVector<SDOperand, 3> Ops;
3279 Ops.push_back(DAG.getEntryNode());
3280 Ops.push_back(CPIdx);
3281 Ops.push_back(DAG.getSrcValue(NULL));
Evan Cheng82241c82007-01-05 21:37:56 +00003282 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3283 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng4363e882007-01-05 07:55:56 +00003284
3285 // Shift sign bit right or left if the two operands have different types.
3286 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3287 // Op0 is MVT::f32, Op1 is MVT::f64.
3288 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3289 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3290 DAG.getConstant(32, MVT::i32));
3291 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3292 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3293 DAG.getConstant(0, getPointerTy()));
Evan Cheng4363e882007-01-05 07:55:56 +00003294 }
3295
Evan Cheng82241c82007-01-05 21:37:56 +00003296 // Clear first operand sign bit.
3297 CV.clear();
3298 if (VT == MVT::f64) {
3299 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3300 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3301 } else {
3302 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3303 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3304 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3305 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3306 }
3307 CS = ConstantStruct::get(CV);
3308 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnere56fef92007-02-25 06:40:16 +00003309 Tys = DAG.getVTList(VT, MVT::Other);
Evan Cheng82241c82007-01-05 21:37:56 +00003310 Ops.clear();
3311 Ops.push_back(DAG.getEntryNode());
3312 Ops.push_back(CPIdx);
3313 Ops.push_back(DAG.getSrcValue(NULL));
3314 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3315 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3316
3317 // Or the value with the sign bit.
3318 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng4363e882007-01-05 07:55:56 +00003319}
3320
Evan Cheng4259a0f2006-09-11 02:19:56 +00003321SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3322 SDOperand Chain) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003323 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3324 SDOperand Cond;
Evan Cheng4259a0f2006-09-11 02:19:56 +00003325 SDOperand Op0 = Op.getOperand(0);
3326 SDOperand Op1 = Op.getOperand(1);
Evan Chenga9467aa2006-04-25 20:13:52 +00003327 SDOperand CC = Op.getOperand(2);
3328 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng694810c2006-10-12 19:12:56 +00003329 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3330 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003331 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Chenga9467aa2006-04-25 20:13:52 +00003332 unsigned X86CC;
Evan Chenga9467aa2006-04-25 20:13:52 +00003333
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003334 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattner7a627672006-09-13 03:22:10 +00003335 Op0, Op1, DAG)) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003336 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Cheng694810c2006-10-12 19:12:56 +00003337 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003338 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003339 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003340 }
3341
3342 assert(isFP && "Illegal integer SetCC!");
3343
3344 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Cheng694810c2006-10-12 19:12:56 +00003345 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003346
3347 switch (SetCCOpcode) {
3348 default: assert(false && "Illegal floating point SetCC!");
3349 case ISD::SETOEQ: { // !PF & ZF
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003350 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003351 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003352 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003353 Tmp1.getValue(1) };
Evan Cheng694810c2006-10-12 19:12:56 +00003354 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003355 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3356 }
3357 case ISD::SETUNE: { // PF | !ZF
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003358 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003359 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003360 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003361 Tmp1.getValue(1) };
Evan Cheng694810c2006-10-12 19:12:56 +00003362 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003363 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3364 }
Evan Chengc1583db2005-12-21 20:21:51 +00003365 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003366}
Evan Cheng45df7f82006-01-30 23:41:35 +00003367
Evan Chenga9467aa2006-04-25 20:13:52 +00003368SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003369 bool addTest = true;
3370 SDOperand Chain = DAG.getEntryNode();
3371 SDOperand Cond = Op.getOperand(0);
3372 SDOperand CC;
3373 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng944d1e92006-01-26 02:13:10 +00003374
Evan Cheng4259a0f2006-09-11 02:19:56 +00003375 if (Cond.getOpcode() == ISD::SETCC)
3376 Cond = LowerSETCC(Cond, DAG, Chain);
3377
3378 if (Cond.getOpcode() == X86ISD::SETCC) {
3379 CC = Cond.getOperand(0);
3380
Evan Chenga9467aa2006-04-25 20:13:52 +00003381 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng4259a0f2006-09-11 02:19:56 +00003382 // (since flag operand cannot be shared). Use it as the condition setting
3383 // operand in place of the X86ISD::SETCC.
3384 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Chenga9467aa2006-04-25 20:13:52 +00003385 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng4259a0f2006-09-11 02:19:56 +00003386 // pressure reason)?
3387 SDOperand Cmp = Cond.getOperand(1);
3388 unsigned Opc = Cmp.getOpcode();
3389 bool IllegalFPCMov = !X86ScalarSSE &&
3390 MVT::isFloatingPoint(Op.getValueType()) &&
3391 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3392 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3393 !IllegalFPCMov) {
3394 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3395 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3396 addTest = false;
3397 }
3398 }
Evan Cheng73a1ad92006-01-10 20:26:56 +00003399
Evan Chenga9467aa2006-04-25 20:13:52 +00003400 if (addTest) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003401 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003402 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3403 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng225a4d02005-12-17 01:21:05 +00003404 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003405
Evan Cheng4259a0f2006-09-11 02:19:56 +00003406 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3407 SmallVector<SDOperand, 4> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003408 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3409 // condition is true.
3410 Ops.push_back(Op.getOperand(2));
3411 Ops.push_back(Op.getOperand(1));
3412 Ops.push_back(CC);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003413 Ops.push_back(Cond.getValue(1));
3414 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003415}
Evan Cheng944d1e92006-01-26 02:13:10 +00003416
Evan Chenga9467aa2006-04-25 20:13:52 +00003417SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003418 bool addTest = true;
3419 SDOperand Chain = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003420 SDOperand Cond = Op.getOperand(1);
3421 SDOperand Dest = Op.getOperand(2);
3422 SDOperand CC;
Evan Cheng4259a0f2006-09-11 02:19:56 +00003423 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3424
Evan Chenga9467aa2006-04-25 20:13:52 +00003425 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng4259a0f2006-09-11 02:19:56 +00003426 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003427
3428 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003429 CC = Cond.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003430
Evan Cheng4259a0f2006-09-11 02:19:56 +00003431 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3432 // (since flag operand cannot be shared). Use it as the condition setting
3433 // operand in place of the X86ISD::SETCC.
3434 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3435 // to use a test instead of duplicating the X86ISD::CMP (for register
3436 // pressure reason)?
3437 SDOperand Cmp = Cond.getOperand(1);
3438 unsigned Opc = Cmp.getOpcode();
3439 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3440 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3441 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3442 addTest = false;
3443 }
3444 }
Evan Chengfb22e862006-01-13 01:03:02 +00003445
Evan Chenga9467aa2006-04-25 20:13:52 +00003446 if (addTest) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003447 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003448 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3449 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng6fc31042005-12-19 23:12:38 +00003450 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003451 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003452 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003453}
Evan Chengae986f12006-01-11 22:15:48 +00003454
Evan Cheng2a330942006-05-25 00:59:30 +00003455SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3456 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003457
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003458 if (Subtarget->is64Bit())
Chris Lattner7802f3e2007-02-25 09:06:15 +00003459 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng2a330942006-05-25 00:59:30 +00003460 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003461 switch (CallingConv) {
Chris Lattnerfc360392006-09-27 18:29:38 +00003462 default:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003463 assert(0 && "Unsupported calling convention");
Chris Lattnerfc360392006-09-27 18:29:38 +00003464 case CallingConv::Fast:
Chris Lattner3ed3be32007-02-28 06:05:16 +00003465 // TODO: Implement fastcc
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003466 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00003467 case CallingConv::C:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003468 case CallingConv::X86_StdCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00003469 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerfc360392006-09-27 18:29:38 +00003470 case CallingConv::X86_FastCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00003471 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003472 }
Evan Cheng2a330942006-05-25 00:59:30 +00003473}
3474
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003475SDOperand
3476X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chengdc614c12006-06-06 23:30:24 +00003477 MachineFunction &MF = DAG.getMachineFunction();
3478 const Function* Fn = MF.getFunction();
3479 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +00003480 Subtarget->isTargetCygMing() &&
Evan Cheng0e14a562006-06-09 06:24:42 +00003481 Fn->getName() == "main")
Evan Chengdc614c12006-06-06 23:30:24 +00003482 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3483
Evan Cheng17e734f2006-05-23 21:06:34 +00003484 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003485 if (Subtarget->is64Bit())
3486 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng17e734f2006-05-23 21:06:34 +00003487 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003488 switch(CC) {
Chris Lattnerfc360392006-09-27 18:29:38 +00003489 default:
3490 assert(0 && "Unsupported calling convention");
3491 case CallingConv::Fast:
Chris Lattner3ed3be32007-02-28 06:05:16 +00003492 // TODO: implement fastcc.
3493
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003494 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00003495 case CallingConv::C:
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003496 return LowerCCCArguments(Op, DAG);
Chris Lattnerfc360392006-09-27 18:29:38 +00003497 case CallingConv::X86_StdCall:
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003498 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00003499 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerfc360392006-09-27 18:29:38 +00003500 case CallingConv::X86_FastCall:
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003501 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner3ed3be32007-02-28 06:05:16 +00003502 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003503 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003504}
3505
Evan Chenga9467aa2006-04-25 20:13:52 +00003506SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3507 SDOperand InFlag(0, 0);
3508 SDOperand Chain = Op.getOperand(0);
3509 unsigned Align =
3510 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3511 if (Align == 0) Align = 1;
3512
3513 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3514 // If not DWORD aligned, call memset if size is less than the threshold.
3515 // It knows how to align to the right boundary first.
3516 if ((Align & 3) != 0 ||
3517 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3518 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003519 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00003520 TargetLowering::ArgListTy Args;
3521 TargetLowering::ArgListEntry Entry;
3522 Entry.Node = Op.getOperand(1);
3523 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00003524 Args.push_back(Entry);
Reid Spencere87b5e92007-01-03 17:24:59 +00003525 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencere63b6512006-12-31 05:55:36 +00003526 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3527 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00003528 Args.push_back(Entry);
3529 Entry.Node = Op.getOperand(3);
3530 Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00003531 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00003532 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00003533 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3534 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003535 }
Evan Chengd097e672006-03-22 02:53:00 +00003536
Evan Chenga9467aa2006-04-25 20:13:52 +00003537 MVT::ValueType AVT;
3538 SDOperand Count;
3539 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3540 unsigned BytesLeft = 0;
3541 bool TwoRepStos = false;
3542 if (ValC) {
3543 unsigned ValReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003544 uint64_t Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003545
Evan Chenga9467aa2006-04-25 20:13:52 +00003546 // If the value is a constant, then we can potentially use larger sets.
3547 switch (Align & 3) {
3548 case 2: // WORD aligned
3549 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00003550 ValReg = X86::AX;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003551 Val = (Val << 8) | Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00003552 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003553 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00003554 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003555 ValReg = X86::EAX;
Evan Chenga9467aa2006-04-25 20:13:52 +00003556 Val = (Val << 8) | Val;
3557 Val = (Val << 16) | Val;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003558 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3559 AVT = MVT::i64;
3560 ValReg = X86::RAX;
3561 Val = (Val << 32) | Val;
3562 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003563 break;
3564 default: // Byte aligned
3565 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00003566 ValReg = X86::AL;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003567 Count = Op.getOperand(3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003568 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003569 }
3570
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003571 if (AVT > MVT::i8) {
3572 if (I) {
3573 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3574 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3575 BytesLeft = I->getValue() % UBytes;
3576 } else {
3577 assert(AVT >= MVT::i32 &&
3578 "Do not use rep;stos if not at least DWORD aligned");
3579 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3580 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3581 TwoRepStos = true;
3582 }
3583 }
3584
Evan Chenga9467aa2006-04-25 20:13:52 +00003585 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3586 InFlag);
3587 InFlag = Chain.getValue(1);
3588 } else {
3589 AVT = MVT::i8;
3590 Count = Op.getOperand(3);
3591 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3592 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003593 }
Evan Chengb0461082006-04-24 18:01:45 +00003594
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003595 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3596 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003597 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003598 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3599 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003600 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003601
Chris Lattnere56fef92007-02-25 06:40:16 +00003602 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003603 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003604 Ops.push_back(Chain);
3605 Ops.push_back(DAG.getValueType(AVT));
3606 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003607 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengb0461082006-04-24 18:01:45 +00003608
Evan Chenga9467aa2006-04-25 20:13:52 +00003609 if (TwoRepStos) {
3610 InFlag = Chain.getValue(1);
3611 Count = Op.getOperand(3);
3612 MVT::ValueType CVT = Count.getValueType();
3613 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003614 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3615 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3616 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003617 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00003618 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003619 Ops.clear();
3620 Ops.push_back(Chain);
3621 Ops.push_back(DAG.getValueType(MVT::i8));
3622 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003623 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003624 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003625 // Issue stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00003626 SDOperand Value;
3627 unsigned Val = ValC->getValue() & 255;
3628 unsigned Offset = I->getValue() - BytesLeft;
3629 SDOperand DstAddr = Op.getOperand(1);
3630 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003631 if (BytesLeft >= 4) {
3632 Val = (Val << 8) | Val;
3633 Val = (Val << 16) | Val;
3634 Value = DAG.getConstant(Val, MVT::i32);
Evan Chengdf9ac472006-10-05 23:01:46 +00003635 Chain = DAG.getStore(Chain, Value,
3636 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3637 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003638 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003639 BytesLeft -= 4;
3640 Offset += 4;
3641 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003642 if (BytesLeft >= 2) {
3643 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Chengdf9ac472006-10-05 23:01:46 +00003644 Chain = DAG.getStore(Chain, Value,
3645 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3646 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003647 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003648 BytesLeft -= 2;
3649 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003650 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003651 if (BytesLeft == 1) {
3652 Value = DAG.getConstant(Val, MVT::i8);
Evan Chengdf9ac472006-10-05 23:01:46 +00003653 Chain = DAG.getStore(Chain, Value,
3654 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3655 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003656 NULL, 0);
Evan Cheng14215c32006-04-21 23:03:30 +00003657 }
Evan Cheng082c8782006-03-24 07:29:27 +00003658 }
Evan Chengebf10062006-04-03 20:53:28 +00003659
Evan Chenga9467aa2006-04-25 20:13:52 +00003660 return Chain;
3661}
Evan Chengebf10062006-04-03 20:53:28 +00003662
Evan Chenga9467aa2006-04-25 20:13:52 +00003663SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3664 SDOperand Chain = Op.getOperand(0);
3665 unsigned Align =
3666 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3667 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003668
Evan Chenga9467aa2006-04-25 20:13:52 +00003669 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3670 // If not DWORD aligned, call memcpy if size is less than the threshold.
3671 // It knows how to align to the right boundary first.
3672 if ((Align & 3) != 0 ||
3673 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3674 MVT::ValueType IntPtr = getPointerTy();
Reid Spencere63b6512006-12-31 05:55:36 +00003675 TargetLowering::ArgListTy Args;
3676 TargetLowering::ArgListEntry Entry;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00003677 Entry.Ty = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00003678 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3679 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3680 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00003681 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00003682 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00003683 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3684 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003685 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003686
3687 MVT::ValueType AVT;
3688 SDOperand Count;
3689 unsigned BytesLeft = 0;
3690 bool TwoRepMovs = false;
3691 switch (Align & 3) {
3692 case 2: // WORD aligned
3693 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00003694 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003695 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00003696 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003697 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3698 AVT = MVT::i64;
Evan Chenga9467aa2006-04-25 20:13:52 +00003699 break;
3700 default: // Byte aligned
3701 AVT = MVT::i8;
3702 Count = Op.getOperand(3);
3703 break;
3704 }
3705
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003706 if (AVT > MVT::i8) {
3707 if (I) {
3708 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3709 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3710 BytesLeft = I->getValue() % UBytes;
3711 } else {
3712 assert(AVT >= MVT::i32 &&
3713 "Do not use rep;movs if not at least DWORD aligned");
3714 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3715 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3716 TwoRepMovs = true;
3717 }
3718 }
3719
Evan Chenga9467aa2006-04-25 20:13:52 +00003720 SDOperand InFlag(0, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003721 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3722 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003723 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003724 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3725 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003726 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003727 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3728 Op.getOperand(2), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003729 InFlag = Chain.getValue(1);
3730
Chris Lattnere56fef92007-02-25 06:40:16 +00003731 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003732 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003733 Ops.push_back(Chain);
3734 Ops.push_back(DAG.getValueType(AVT));
3735 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003736 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003737
3738 if (TwoRepMovs) {
3739 InFlag = Chain.getValue(1);
3740 Count = Op.getOperand(3);
3741 MVT::ValueType CVT = Count.getValueType();
3742 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003743 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3744 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3745 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003746 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00003747 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003748 Ops.clear();
3749 Ops.push_back(Chain);
3750 Ops.push_back(DAG.getValueType(MVT::i8));
3751 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003752 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003753 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003754 // Issue loads and stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00003755 unsigned Offset = I->getValue() - BytesLeft;
3756 SDOperand DstAddr = Op.getOperand(1);
3757 MVT::ValueType DstVT = DstAddr.getValueType();
3758 SDOperand SrcAddr = Op.getOperand(2);
3759 MVT::ValueType SrcVT = SrcAddr.getValueType();
3760 SDOperand Value;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003761 if (BytesLeft >= 4) {
3762 Value = DAG.getLoad(MVT::i32, Chain,
3763 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3764 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003765 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003766 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003767 Chain = DAG.getStore(Chain, Value,
3768 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3769 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003770 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003771 BytesLeft -= 4;
3772 Offset += 4;
3773 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003774 if (BytesLeft >= 2) {
3775 Value = DAG.getLoad(MVT::i16, Chain,
3776 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3777 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003778 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003779 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003780 Chain = DAG.getStore(Chain, Value,
3781 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3782 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003783 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003784 BytesLeft -= 2;
3785 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003786 }
3787
Evan Chenga9467aa2006-04-25 20:13:52 +00003788 if (BytesLeft == 1) {
3789 Value = DAG.getLoad(MVT::i8, Chain,
3790 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3791 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003792 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003793 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003794 Chain = DAG.getStore(Chain, Value,
3795 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3796 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003797 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003798 }
Evan Chengcbffa462006-03-31 19:22:53 +00003799 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003800
3801 return Chain;
3802}
3803
3804SDOperand
3805X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere56fef92007-02-25 06:40:16 +00003806 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003807 SDOperand TheOp = Op.getOperand(0);
3808 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00003809 if (Subtarget->is64Bit()) {
3810 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3811 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3812 MVT::i64, Copy1.getValue(2));
3813 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3814 DAG.getConstant(32, MVT::i8));
Chris Lattner35a08552007-02-25 07:10:00 +00003815 SDOperand Ops[] = {
3816 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3817 };
Chris Lattnere56fef92007-02-25 06:40:16 +00003818
3819 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00003820 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00003821 }
Chris Lattner35a08552007-02-25 07:10:00 +00003822
3823 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3824 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3825 MVT::i32, Copy1.getValue(2));
3826 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3827 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3828 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003829}
3830
3831SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Chengab51cf22006-10-13 21:14:26 +00003832 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3833
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003834 if (!Subtarget->is64Bit()) {
3835 // vastart just stores the address of the VarArgsFrameIndex slot into the
3836 // memory location argument.
3837 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003838 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3839 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003840 }
3841
3842 // __va_list_tag:
3843 // gp_offset (0 - 6 * 8)
3844 // fp_offset (48 - 48 + 8 * 16)
3845 // overflow_arg_area (point to parameters coming in memory).
3846 // reg_save_area
Chris Lattner35a08552007-02-25 07:10:00 +00003847 SmallVector<SDOperand, 8> MemOps;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003848 SDOperand FIN = Op.getOperand(1);
3849 // Store gp_offset
Evan Chengdf9ac472006-10-05 23:01:46 +00003850 SDOperand Store = DAG.getStore(Op.getOperand(0),
3851 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00003852 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003853 MemOps.push_back(Store);
3854
3855 // Store fp_offset
3856 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3857 DAG.getConstant(4, getPointerTy()));
Evan Chengdf9ac472006-10-05 23:01:46 +00003858 Store = DAG.getStore(Op.getOperand(0),
3859 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00003860 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003861 MemOps.push_back(Store);
3862
3863 // Store ptr to overflow_arg_area
3864 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3865 DAG.getConstant(4, getPointerTy()));
3866 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003867 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3868 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003869 MemOps.push_back(Store);
3870
3871 // Store ptr to reg_save_area.
3872 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3873 DAG.getConstant(8, getPointerTy()));
3874 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003875 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3876 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003877 MemOps.push_back(Store);
3878 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003879}
3880
Evan Chengdeaea252007-03-02 23:16:35 +00003881SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
3882 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
3883 SDOperand Chain = Op.getOperand(0);
3884 SDOperand DstPtr = Op.getOperand(1);
3885 SDOperand SrcPtr = Op.getOperand(2);
3886 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
3887 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
3888
3889 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
3890 SrcSV->getValue(), SrcSV->getOffset());
3891 Chain = SrcPtr.getValue(1);
3892 for (unsigned i = 0; i < 3; ++i) {
3893 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
3894 SrcSV->getValue(), SrcSV->getOffset());
3895 Chain = Val.getValue(1);
3896 Chain = DAG.getStore(Chain, Val, DstPtr,
3897 DstSV->getValue(), DstSV->getOffset());
3898 if (i == 2)
3899 break;
3900 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
3901 DAG.getConstant(8, getPointerTy()));
3902 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
3903 DAG.getConstant(8, getPointerTy()));
3904 }
3905 return Chain;
3906}
3907
Evan Chenga9467aa2006-04-25 20:13:52 +00003908SDOperand
3909X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3910 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3911 switch (IntNo) {
3912 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003913 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003914 case Intrinsic::x86_sse_comieq_ss:
3915 case Intrinsic::x86_sse_comilt_ss:
3916 case Intrinsic::x86_sse_comile_ss:
3917 case Intrinsic::x86_sse_comigt_ss:
3918 case Intrinsic::x86_sse_comige_ss:
3919 case Intrinsic::x86_sse_comineq_ss:
3920 case Intrinsic::x86_sse_ucomieq_ss:
3921 case Intrinsic::x86_sse_ucomilt_ss:
3922 case Intrinsic::x86_sse_ucomile_ss:
3923 case Intrinsic::x86_sse_ucomigt_ss:
3924 case Intrinsic::x86_sse_ucomige_ss:
3925 case Intrinsic::x86_sse_ucomineq_ss:
3926 case Intrinsic::x86_sse2_comieq_sd:
3927 case Intrinsic::x86_sse2_comilt_sd:
3928 case Intrinsic::x86_sse2_comile_sd:
3929 case Intrinsic::x86_sse2_comigt_sd:
3930 case Intrinsic::x86_sse2_comige_sd:
3931 case Intrinsic::x86_sse2_comineq_sd:
3932 case Intrinsic::x86_sse2_ucomieq_sd:
3933 case Intrinsic::x86_sse2_ucomilt_sd:
3934 case Intrinsic::x86_sse2_ucomile_sd:
3935 case Intrinsic::x86_sse2_ucomigt_sd:
3936 case Intrinsic::x86_sse2_ucomige_sd:
3937 case Intrinsic::x86_sse2_ucomineq_sd: {
3938 unsigned Opc = 0;
3939 ISD::CondCode CC = ISD::SETCC_INVALID;
3940 switch (IntNo) {
3941 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003942 case Intrinsic::x86_sse_comieq_ss:
3943 case Intrinsic::x86_sse2_comieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003944 Opc = X86ISD::COMI;
3945 CC = ISD::SETEQ;
3946 break;
Evan Cheng78038292006-04-05 23:38:46 +00003947 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003948 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003949 Opc = X86ISD::COMI;
3950 CC = ISD::SETLT;
3951 break;
3952 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003953 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003954 Opc = X86ISD::COMI;
3955 CC = ISD::SETLE;
3956 break;
3957 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003958 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003959 Opc = X86ISD::COMI;
3960 CC = ISD::SETGT;
3961 break;
3962 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003963 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003964 Opc = X86ISD::COMI;
3965 CC = ISD::SETGE;
3966 break;
3967 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003968 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003969 Opc = X86ISD::COMI;
3970 CC = ISD::SETNE;
3971 break;
3972 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003973 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003974 Opc = X86ISD::UCOMI;
3975 CC = ISD::SETEQ;
3976 break;
3977 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003978 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003979 Opc = X86ISD::UCOMI;
3980 CC = ISD::SETLT;
3981 break;
3982 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003983 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003984 Opc = X86ISD::UCOMI;
3985 CC = ISD::SETLE;
3986 break;
3987 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003988 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003989 Opc = X86ISD::UCOMI;
3990 CC = ISD::SETGT;
3991 break;
3992 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003993 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003994 Opc = X86ISD::UCOMI;
3995 CC = ISD::SETGE;
3996 break;
3997 case Intrinsic::x86_sse_ucomineq_ss:
3998 case Intrinsic::x86_sse2_ucomineq_sd:
3999 Opc = X86ISD::UCOMI;
4000 CC = ISD::SETNE;
4001 break;
Evan Cheng78038292006-04-05 23:38:46 +00004002 }
Evan Cheng4259a0f2006-09-11 02:19:56 +00004003
Evan Chenga9467aa2006-04-25 20:13:52 +00004004 unsigned X86CC;
Chris Lattner7a627672006-09-13 03:22:10 +00004005 SDOperand LHS = Op.getOperand(1);
4006 SDOperand RHS = Op.getOperand(2);
4007 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004008
4009 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattner7a627672006-09-13 03:22:10 +00004010 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng4259a0f2006-09-11 02:19:56 +00004011 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4012 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4013 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4014 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Chenga9467aa2006-04-25 20:13:52 +00004015 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00004016 }
Evan Cheng5c59d492005-12-23 07:31:11 +00004017 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004018}
Evan Cheng6af02632005-12-20 06:22:03 +00004019
Nate Begemaneda59972007-01-29 22:58:52 +00004020SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4021 // Depths > 0 not supported yet!
4022 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4023 return SDOperand();
4024
4025 // Just load the return address
4026 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4027 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4028}
4029
4030SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4031 // Depths > 0 not supported yet!
4032 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4033 return SDOperand();
4034
4035 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4036 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4037 DAG.getConstant(4, getPointerTy()));
4038}
4039
Evan Chenga9467aa2006-04-25 20:13:52 +00004040/// LowerOperation - Provide custom lowering hooks for some operations.
4041///
4042SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4043 switch (Op.getOpcode()) {
4044 default: assert(0 && "Should not custom lower this!");
4045 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4046 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4047 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4048 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4049 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4050 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4051 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4052 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4053 case ISD::SHL_PARTS:
4054 case ISD::SRA_PARTS:
4055 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4056 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4057 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4058 case ISD::FABS: return LowerFABS(Op, DAG);
4059 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng4363e882007-01-05 07:55:56 +00004060 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004061 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Chenga9467aa2006-04-25 20:13:52 +00004062 case ISD::SELECT: return LowerSELECT(Op, DAG);
4063 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4064 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00004065 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004066 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00004067 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004068 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4069 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4070 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4071 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengdeaea252007-03-02 23:16:35 +00004072 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004073 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemaneda59972007-01-29 22:58:52 +00004074 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4075 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004076 }
Jim Laskey3796abe2007-02-21 22:54:50 +00004077 return SDOperand();
Evan Chenga9467aa2006-04-25 20:13:52 +00004078}
4079
Evan Cheng6af02632005-12-20 06:22:03 +00004080const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4081 switch (Opcode) {
4082 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00004083 case X86ISD::SHLD: return "X86ISD::SHLD";
4084 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00004085 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng4363e882007-01-05 07:55:56 +00004086 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng72d5c252006-01-31 22:28:30 +00004087 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng4363e882007-01-05 07:55:56 +00004088 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Cheng6305e502006-01-12 22:54:21 +00004089 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00004090 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00004091 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4092 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4093 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00004094 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00004095 case X86ISD::FST: return "X86ISD::FST";
4096 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00004097 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00004098 case X86ISD::CALL: return "X86ISD::CALL";
4099 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4100 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4101 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng78038292006-04-05 23:38:46 +00004102 case X86ISD::COMI: return "X86ISD::COMI";
4103 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00004104 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00004105 case X86ISD::CMOV: return "X86ISD::CMOV";
4106 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00004107 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00004108 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4109 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00004110 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5987cfb2006-07-07 08:33:52 +00004111 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
Evan Cheng5588de92006-02-18 00:15:05 +00004112 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00004113 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00004114 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00004115 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00004116 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng49683ba2006-11-10 21:43:37 +00004117 case X86ISD::FMAX: return "X86ISD::FMAX";
4118 case X86ISD::FMIN: return "X86ISD::FMIN";
Evan Cheng6af02632005-12-20 06:22:03 +00004119 }
4120}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004121
Evan Cheng3ab7ea72007-03-12 23:28:50 +00004122/// isLegalAddressImmediate - Return true if the integer value can be used
4123/// as the offset of the target addressing mode for load / store of the
4124/// given type.
4125bool X86TargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Cheng02612422006-07-05 22:17:51 +00004126 // X86 allows a sign-extended 32-bit immediate field.
4127 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4128}
4129
Evan Cheng3ab7ea72007-03-12 23:28:50 +00004130/// isLegalAddressImmediate - Return true if the GlobalValue can be used as
4131/// the offset of the target addressing mode.
Evan Cheng02612422006-07-05 22:17:51 +00004132bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Cheng7a9238c2006-11-29 23:48:14 +00004133 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
4134 // field unless we are in small code model.
4135 if (Subtarget->is64Bit() &&
4136 getTargetMachine().getCodeModel() != CodeModel::Small)
Evan Cheng02612422006-07-05 22:17:51 +00004137 return false;
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00004138
4139 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
Evan Cheng02612422006-07-05 22:17:51 +00004140}
4141
Evan Cheng3ab7ea72007-03-12 23:28:50 +00004142/// isLegalAddressScale - Return true if the integer value can be used as the
4143/// scale of the target addressing mode for load / store of the given type.
4144bool X86TargetLowering::isLegalAddressScale(int64_t S, const Type *Ty) const {
4145 switch (S) {
4146 default:
4147 return false;
4148 case 2: case 4: case 8:
4149 return true;
4150 // FIXME: These require both scale + index last and thus more expensive.
4151 // How to tell LSR to try for 2, 4, 8 first?
4152 case 3: case 5: case 9:
4153 return true;
4154 }
4155}
4156
Dale Johannesen0c6bb5e2007-03-21 21:51:52 +00004157/// isLegalAddressScaleAndImm - Return true if S works for IsLegalAddressScale
4158/// and V works for isLegalAddressImmediate _and_ both can be applied
4159/// simultaneously to the same instruction.
4160bool X86TargetLowering::isLegalAddressScaleAndImm(int64_t S, int64_t V,
4161 const Type* Ty) const {
4162 return isLegalAddressScale(S, Ty) && isLegalAddressImmediate(V, Ty);
4163}
4164
4165/// isLegalAddressScaleAndImm - Return true if S works for IsLegalAddressScale
4166/// and GV works for isLegalAddressImmediate _and_ both can be applied
4167/// simultaneously to the same instruction.
4168bool X86TargetLowering::isLegalAddressScaleAndImm(int64_t S, GlobalValue *GV,
4169 const Type* Ty) const {
4170 return isLegalAddressScale(S, Ty) && isLegalAddressImmediate(GV);
4171}
4172
Evan Cheng02612422006-07-05 22:17:51 +00004173/// isShuffleMaskLegal - Targets can use this to indicate that they only
4174/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4175/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4176/// are assumed to be legal.
4177bool
4178X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4179 // Only do shuffles on 128-bit vector types for now.
4180 if (MVT::getSizeInBits(VT) == 64) return false;
4181 return (Mask.Val->getNumOperands() <= 4 ||
4182 isSplatMask(Mask.Val) ||
4183 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4184 X86::isUNPCKLMask(Mask.Val) ||
4185 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4186 X86::isUNPCKHMask(Mask.Val));
4187}
4188
4189bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4190 MVT::ValueType EVT,
4191 SelectionDAG &DAG) const {
4192 unsigned NumElts = BVOps.size();
4193 // Only do shuffles on 128-bit vector types for now.
4194 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4195 if (NumElts == 2) return true;
4196 if (NumElts == 4) {
Chris Lattner35a08552007-02-25 07:10:00 +00004197 return (isMOVLMask(&BVOps[0], 4) ||
4198 isCommutedMOVL(&BVOps[0], 4, true) ||
4199 isSHUFPMask(&BVOps[0], 4) ||
4200 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng02612422006-07-05 22:17:51 +00004201 }
4202 return false;
4203}
4204
4205//===----------------------------------------------------------------------===//
4206// X86 Scheduler Hooks
4207//===----------------------------------------------------------------------===//
4208
4209MachineBasicBlock *
4210X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4211 MachineBasicBlock *BB) {
Evan Cheng20350c42006-11-27 23:37:22 +00004212 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng02612422006-07-05 22:17:51 +00004213 switch (MI->getOpcode()) {
4214 default: assert(false && "Unexpected instr type to insert");
4215 case X86::CMOV_FR32:
4216 case X86::CMOV_FR64:
4217 case X86::CMOV_V4F32:
4218 case X86::CMOV_V2F64:
4219 case X86::CMOV_V2I64: {
4220 // To "insert" a SELECT_CC instruction, we actually have to insert the
4221 // diamond control-flow pattern. The incoming instruction knows the
4222 // destination vreg to set, the condition code register to branch on, the
4223 // true/false values to select between, and a branch opcode to use.
4224 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4225 ilist<MachineBasicBlock>::iterator It = BB;
4226 ++It;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004227
Evan Cheng02612422006-07-05 22:17:51 +00004228 // thisMBB:
4229 // ...
4230 // TrueVal = ...
4231 // cmpTY ccX, r1, r2
4232 // bCC copy1MBB
4233 // fallthrough --> copy0MBB
4234 MachineBasicBlock *thisMBB = BB;
4235 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4236 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004237 unsigned Opc =
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004238 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Cheng20350c42006-11-27 23:37:22 +00004239 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng02612422006-07-05 22:17:51 +00004240 MachineFunction *F = BB->getParent();
4241 F->getBasicBlockList().insert(It, copy0MBB);
4242 F->getBasicBlockList().insert(It, sinkMBB);
4243 // Update machine-CFG edges by first adding all successors of the current
4244 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004245 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng02612422006-07-05 22:17:51 +00004246 e = BB->succ_end(); i != e; ++i)
4247 sinkMBB->addSuccessor(*i);
4248 // Next, remove all successors of the current block, and add the true
4249 // and fallthrough blocks as its successors.
4250 while(!BB->succ_empty())
4251 BB->removeSuccessor(BB->succ_begin());
4252 BB->addSuccessor(copy0MBB);
4253 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004254
Evan Cheng02612422006-07-05 22:17:51 +00004255 // copy0MBB:
4256 // %FalseValue = ...
4257 // # fallthrough to sinkMBB
4258 BB = copy0MBB;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004259
Evan Cheng02612422006-07-05 22:17:51 +00004260 // Update machine-CFG edges
4261 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004262
Evan Cheng02612422006-07-05 22:17:51 +00004263 // sinkMBB:
4264 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4265 // ...
4266 BB = sinkMBB;
Evan Cheng20350c42006-11-27 23:37:22 +00004267 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng02612422006-07-05 22:17:51 +00004268 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4269 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4270
4271 delete MI; // The pseudo instruction is gone now.
4272 return BB;
4273 }
4274
4275 case X86::FP_TO_INT16_IN_MEM:
4276 case X86::FP_TO_INT32_IN_MEM:
4277 case X86::FP_TO_INT64_IN_MEM: {
4278 // Change the floating point control register to use "round towards zero"
4279 // mode when truncating to an integer value.
4280 MachineFunction *F = BB->getParent();
4281 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Cheng20350c42006-11-27 23:37:22 +00004282 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004283
4284 // Load the old value of the high byte of the control word...
4285 unsigned OldCW =
4286 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng20350c42006-11-27 23:37:22 +00004287 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004288
4289 // Set the high part to be round to zero...
Evan Cheng20350c42006-11-27 23:37:22 +00004290 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4291 .addImm(0xC7F);
Evan Cheng02612422006-07-05 22:17:51 +00004292
4293 // Reload the modified control word now...
Evan Cheng20350c42006-11-27 23:37:22 +00004294 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004295
4296 // Restore the memory image of control word to original value
Evan Cheng20350c42006-11-27 23:37:22 +00004297 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4298 .addReg(OldCW);
Evan Cheng02612422006-07-05 22:17:51 +00004299
4300 // Get the X86 opcode to use.
4301 unsigned Opc;
4302 switch (MI->getOpcode()) {
4303 default: assert(0 && "illegal opcode!");
4304 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4305 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4306 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4307 }
4308
4309 X86AddressMode AM;
4310 MachineOperand &Op = MI->getOperand(0);
4311 if (Op.isRegister()) {
4312 AM.BaseType = X86AddressMode::RegBase;
4313 AM.Base.Reg = Op.getReg();
4314 } else {
4315 AM.BaseType = X86AddressMode::FrameIndexBase;
4316 AM.Base.FrameIndex = Op.getFrameIndex();
4317 }
4318 Op = MI->getOperand(1);
4319 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004320 AM.Scale = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004321 Op = MI->getOperand(2);
4322 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004323 AM.IndexReg = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004324 Op = MI->getOperand(3);
4325 if (Op.isGlobalAddress()) {
4326 AM.GV = Op.getGlobal();
4327 } else {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004328 AM.Disp = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004329 }
Evan Cheng20350c42006-11-27 23:37:22 +00004330 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4331 .addReg(MI->getOperand(4).getReg());
Evan Cheng02612422006-07-05 22:17:51 +00004332
4333 // Reload the original control word now.
Evan Cheng20350c42006-11-27 23:37:22 +00004334 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004335
4336 delete MI; // The pseudo instruction is gone now.
4337 return BB;
4338 }
4339 }
4340}
4341
4342//===----------------------------------------------------------------------===//
4343// X86 Optimization Hooks
4344//===----------------------------------------------------------------------===//
4345
Nate Begeman8a77efe2006-02-16 21:11:51 +00004346void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4347 uint64_t Mask,
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004348 uint64_t &KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +00004349 uint64_t &KnownOne,
4350 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004351 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00004352 assert((Opc >= ISD::BUILTIN_OP_END ||
4353 Opc == ISD::INTRINSIC_WO_CHAIN ||
4354 Opc == ISD::INTRINSIC_W_CHAIN ||
4355 Opc == ISD::INTRINSIC_VOID) &&
4356 "Should use MaskedValueIsZero if you don't know whether Op"
4357 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004358
Evan Cheng6d196db2006-04-05 06:11:20 +00004359 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004360 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00004361 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004362 case X86ISD::SETCC:
Nate Begeman8a77efe2006-02-16 21:11:51 +00004363 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4364 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004365 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004366}
Chris Lattnerc642aa52006-01-31 19:43:35 +00004367
Evan Cheng5987cfb2006-07-07 08:33:52 +00004368/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4369/// element of the result of the vector shuffle.
4370static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4371 MVT::ValueType VT = N->getValueType(0);
4372 SDOperand PermMask = N->getOperand(2);
4373 unsigned NumElems = PermMask.getNumOperands();
4374 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4375 i %= NumElems;
4376 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4377 return (i == 0)
4378 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4379 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4380 SDOperand Idx = PermMask.getOperand(i);
4381 if (Idx.getOpcode() == ISD::UNDEF)
4382 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4383 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4384 }
4385 return SDOperand();
4386}
4387
4388/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4389/// node is a GlobalAddress + an offset.
4390static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Chengae1cd752006-11-30 21:55:46 +00004391 unsigned Opc = N->getOpcode();
Evan Cheng62cdc3f2006-12-05 04:01:03 +00004392 if (Opc == X86ISD::Wrapper) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004393 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4394 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4395 return true;
4396 }
Evan Chengae1cd752006-11-30 21:55:46 +00004397 } else if (Opc == ISD::ADD) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004398 SDOperand N1 = N->getOperand(0);
4399 SDOperand N2 = N->getOperand(1);
4400 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4401 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4402 if (V) {
4403 Offset += V->getSignExtended();
4404 return true;
4405 }
4406 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4407 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4408 if (V) {
4409 Offset += V->getSignExtended();
4410 return true;
4411 }
4412 }
4413 }
4414 return false;
4415}
4416
4417/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4418/// + Dist * Size.
4419static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4420 MachineFrameInfo *MFI) {
4421 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4422 return false;
4423
4424 SDOperand Loc = N->getOperand(1);
4425 SDOperand BaseLoc = Base->getOperand(1);
4426 if (Loc.getOpcode() == ISD::FrameIndex) {
4427 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4428 return false;
4429 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4430 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4431 int FS = MFI->getObjectSize(FI);
4432 int BFS = MFI->getObjectSize(BFI);
4433 if (FS != BFS || FS != Size) return false;
4434 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4435 } else {
4436 GlobalValue *GV1 = NULL;
4437 GlobalValue *GV2 = NULL;
4438 int64_t Offset1 = 0;
4439 int64_t Offset2 = 0;
4440 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4441 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4442 if (isGA1 && isGA2 && GV1 == GV2)
4443 return Offset1 == (Offset2 + Dist*Size);
4444 }
4445
4446 return false;
4447}
4448
Evan Cheng79cf9a52006-07-10 21:37:44 +00004449static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4450 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004451 GlobalValue *GV;
4452 int64_t Offset;
4453 if (isGAPlusOffset(Base, GV, Offset))
4454 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4455 else {
4456 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4457 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng79cf9a52006-07-10 21:37:44 +00004458 if (BFI < 0)
4459 // Fixed objects do not specify alignment, however the offsets are known.
4460 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4461 (MFI->getObjectOffset(BFI) % 16) == 0);
4462 else
4463 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng5987cfb2006-07-07 08:33:52 +00004464 }
4465 return false;
4466}
4467
4468
4469/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4470/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4471/// if the load addresses are consecutive, non-overlapping, and in the right
4472/// order.
Evan Cheng79cf9a52006-07-10 21:37:44 +00004473static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4474 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004475 MachineFunction &MF = DAG.getMachineFunction();
4476 MachineFrameInfo *MFI = MF.getFrameInfo();
4477 MVT::ValueType VT = N->getValueType(0);
4478 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4479 SDOperand PermMask = N->getOperand(2);
4480 int NumElems = (int)PermMask.getNumOperands();
4481 SDNode *Base = NULL;
4482 for (int i = 0; i < NumElems; ++i) {
4483 SDOperand Idx = PermMask.getOperand(i);
4484 if (Idx.getOpcode() == ISD::UNDEF) {
4485 if (!Base) return SDOperand();
4486 } else {
4487 SDOperand Arg =
4488 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004489 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng5987cfb2006-07-07 08:33:52 +00004490 return SDOperand();
4491 if (!Base)
4492 Base = Arg.Val;
4493 else if (!isConsecutiveLoad(Arg.Val, Base,
4494 i, MVT::getSizeInBits(EVT)/8,MFI))
4495 return SDOperand();
4496 }
4497 }
4498
Evan Cheng79cf9a52006-07-10 21:37:44 +00004499 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004500 if (isAlign16) {
4501 LoadSDNode *LD = cast<LoadSDNode>(Base);
4502 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4503 LD->getSrcValueOffset());
4504 } else {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004505 // Just use movups, it's shorter.
Chris Lattnere56fef92007-02-25 06:40:16 +00004506 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00004507 SmallVector<SDOperand, 3> Ops;
4508 Ops.push_back(Base->getOperand(0));
4509 Ops.push_back(Base->getOperand(1));
4510 Ops.push_back(Base->getOperand(2));
Evan Cheng5987cfb2006-07-07 08:33:52 +00004511 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengbd1c5a82006-08-11 09:08:15 +00004512 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
Evan Cheng5c68bba2006-08-11 07:35:45 +00004513 }
Evan Cheng5987cfb2006-07-07 08:33:52 +00004514}
4515
Chris Lattner9259b1e2006-10-04 06:57:07 +00004516/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4517static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4518 const X86Subtarget *Subtarget) {
4519 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004520
Chris Lattner9259b1e2006-10-04 06:57:07 +00004521 // If we have SSE[12] support, try to form min/max nodes.
4522 if (Subtarget->hasSSE2() &&
4523 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4524 if (Cond.getOpcode() == ISD::SETCC) {
4525 // Get the LHS/RHS of the select.
4526 SDOperand LHS = N->getOperand(1);
4527 SDOperand RHS = N->getOperand(2);
4528 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004529
Evan Cheng49683ba2006-11-10 21:43:37 +00004530 unsigned Opcode = 0;
Chris Lattner9259b1e2006-10-04 06:57:07 +00004531 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004532 switch (CC) {
4533 default: break;
4534 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4535 case ISD::SETULE:
4536 case ISD::SETLE:
4537 if (!UnsafeFPMath) break;
4538 // FALL THROUGH.
4539 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4540 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00004541 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004542 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004543
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004544 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4545 case ISD::SETUGT:
4546 case ISD::SETGT:
4547 if (!UnsafeFPMath) break;
4548 // FALL THROUGH.
4549 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4550 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00004551 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004552 break;
4553 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00004554 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004555 switch (CC) {
4556 default: break;
4557 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4558 case ISD::SETUGT:
4559 case ISD::SETGT:
4560 if (!UnsafeFPMath) break;
4561 // FALL THROUGH.
4562 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4563 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00004564 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004565 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004566
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004567 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4568 case ISD::SETULE:
4569 case ISD::SETLE:
4570 if (!UnsafeFPMath) break;
4571 // FALL THROUGH.
4572 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4573 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00004574 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004575 break;
4576 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00004577 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004578
Evan Cheng49683ba2006-11-10 21:43:37 +00004579 if (Opcode)
4580 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner9259b1e2006-10-04 06:57:07 +00004581 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004582
Chris Lattner9259b1e2006-10-04 06:57:07 +00004583 }
4584
4585 return SDOperand();
4586}
4587
4588
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004589SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng5987cfb2006-07-07 08:33:52 +00004590 DAGCombinerInfo &DCI) const {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004591 SelectionDAG &DAG = DCI.DAG;
4592 switch (N->getOpcode()) {
4593 default: break;
4594 case ISD::VECTOR_SHUFFLE:
Evan Cheng79cf9a52006-07-10 21:37:44 +00004595 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner9259b1e2006-10-04 06:57:07 +00004596 case ISD::SELECT:
4597 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng5987cfb2006-07-07 08:33:52 +00004598 }
4599
4600 return SDOperand();
4601}
4602
Evan Cheng02612422006-07-05 22:17:51 +00004603//===----------------------------------------------------------------------===//
4604// X86 Inline Assembly Support
4605//===----------------------------------------------------------------------===//
4606
Chris Lattner298ef372006-07-11 02:54:03 +00004607/// getConstraintType - Given a constraint letter, return the type of
4608/// constraint it is for this target.
4609X86TargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00004610X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4611 if (Constraint.size() == 1) {
4612 switch (Constraint[0]) {
4613 case 'A':
4614 case 'r':
4615 case 'R':
4616 case 'l':
4617 case 'q':
4618 case 'Q':
4619 case 'x':
4620 case 'Y':
4621 return C_RegisterClass;
4622 default:
4623 break;
4624 }
Chris Lattner298ef372006-07-11 02:54:03 +00004625 }
Chris Lattnerd6855142007-03-25 02:14:49 +00004626 return TargetLowering::getConstraintType(Constraint);
Chris Lattner298ef372006-07-11 02:54:03 +00004627}
4628
Chris Lattner44daa502006-10-31 20:13:11 +00004629/// isOperandValidForConstraint - Return the specified operand (possibly
4630/// modified) if the specified SDOperand is valid for the specified target
4631/// constraint letter, otherwise return null.
4632SDOperand X86TargetLowering::
4633isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4634 switch (Constraint) {
4635 default: break;
Devang Patelb38c2ec2007-03-17 00:13:28 +00004636 case 'I':
Chris Lattner03a643a2007-03-25 01:57:35 +00004637 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4638 if (C->getValue() <= 31)
Devang Patelb38c2ec2007-03-17 00:13:28 +00004639 return Op;
Devang Patelb38c2ec2007-03-17 00:13:28 +00004640 }
Chris Lattner03a643a2007-03-25 01:57:35 +00004641 return SDOperand(0,0);
4642 case 'N':
4643 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4644 if (C->getValue() <= 255)
4645 return Op;
4646 }
4647 return SDOperand(0,0);
Chris Lattner44daa502006-10-31 20:13:11 +00004648 case 'i':
4649 // Literal immediates are always ok.
4650 if (isa<ConstantSDNode>(Op)) return Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004651
Chris Lattner44daa502006-10-31 20:13:11 +00004652 // If we are in non-pic codegen mode, we allow the address of a global to
4653 // be used with 'i'.
4654 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4655 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4656 return SDOperand(0, 0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004657
Chris Lattner44daa502006-10-31 20:13:11 +00004658 if (GA->getOpcode() != ISD::TargetGlobalAddress)
4659 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4660 GA->getOffset());
4661 return Op;
4662 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004663
Chris Lattner44daa502006-10-31 20:13:11 +00004664 // Otherwise, not valid for this mode.
4665 return SDOperand(0, 0);
4666 }
4667 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4668}
4669
4670
Chris Lattnerc642aa52006-01-31 19:43:35 +00004671std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00004672getRegClassForInlineAsmConstraint(const std::string &Constraint,
4673 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00004674 if (Constraint.size() == 1) {
4675 // FIXME: not handling fp-stack yet!
4676 // FIXME: not handling MMX registers yet ('y' constraint).
4677 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattner298ef372006-07-11 02:54:03 +00004678 default: break; // Unknown constraint letter
4679 case 'A': // EAX/EDX
4680 if (VT == MVT::i32 || VT == MVT::i64)
4681 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4682 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004683 case 'r': // GENERAL_REGS
4684 case 'R': // LEGACY_REGS
Chris Lattnerd139ddd2006-12-04 22:38:21 +00004685 if (VT == MVT::i64 && Subtarget->is64Bit())
4686 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
4687 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
4688 X86::R8, X86::R9, X86::R10, X86::R11,
4689 X86::R12, X86::R13, X86::R14, X86::R15, 0);
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004690 if (VT == MVT::i32)
4691 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4692 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4693 else if (VT == MVT::i16)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004694 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004695 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4696 else if (VT == MVT::i8)
Chris Lattnera16201c2006-12-05 17:29:40 +00004697 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004698 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004699 case 'l': // INDEX_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004700 if (VT == MVT::i32)
4701 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4702 X86::ESI, X86::EDI, X86::EBP, 0);
4703 else if (VT == MVT::i16)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004704 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004705 X86::SI, X86::DI, X86::BP, 0);
4706 else if (VT == MVT::i8)
4707 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4708 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004709 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4710 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004711 if (VT == MVT::i32)
4712 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4713 else if (VT == MVT::i16)
4714 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4715 else if (VT == MVT::i8)
4716 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4717 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004718 case 'x': // SSE_REGS if SSE1 allowed
4719 if (Subtarget->hasSSE1())
4720 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4721 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4722 0);
4723 return std::vector<unsigned>();
4724 case 'Y': // SSE_REGS if SSE2 allowed
4725 if (Subtarget->hasSSE2())
4726 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4727 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4728 0);
4729 return std::vector<unsigned>();
4730 }
4731 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004732
Chris Lattner7ad77df2006-02-22 00:56:39 +00004733 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00004734}
Chris Lattner524129d2006-07-31 23:26:50 +00004735
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004736std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner524129d2006-07-31 23:26:50 +00004737X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4738 MVT::ValueType VT) const {
4739 // Use the default implementation in TargetLowering to convert the register
4740 // constraint into a member of a register class.
4741 std::pair<unsigned, const TargetRegisterClass*> Res;
4742 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerf6a69662006-10-31 19:42:44 +00004743
4744 // Not found as a standard register?
4745 if (Res.second == 0) {
4746 // GCC calls "st(0)" just plain "st".
4747 if (StringsEqualNoCase("{st}", Constraint)) {
4748 Res.first = X86::ST0;
4749 Res.second = X86::RSTRegisterClass;
4750 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004751
Chris Lattnerf6a69662006-10-31 19:42:44 +00004752 return Res;
4753 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004754
Chris Lattner524129d2006-07-31 23:26:50 +00004755 // Otherwise, check to see if this is a register class of the wrong value
4756 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4757 // turn into {ax},{dx}.
4758 if (Res.second->hasType(VT))
4759 return Res; // Correct type already, nothing to do.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004760
Chris Lattner524129d2006-07-31 23:26:50 +00004761 // All of the single-register GCC register classes map their values onto
4762 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4763 // really want an 8-bit or 32-bit register, map to the appropriate register
4764 // class and return the appropriate register.
4765 if (Res.second != X86::GR16RegisterClass)
4766 return Res;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004767
Chris Lattner524129d2006-07-31 23:26:50 +00004768 if (VT == MVT::i8) {
4769 unsigned DestReg = 0;
4770 switch (Res.first) {
4771 default: break;
4772 case X86::AX: DestReg = X86::AL; break;
4773 case X86::DX: DestReg = X86::DL; break;
4774 case X86::CX: DestReg = X86::CL; break;
4775 case X86::BX: DestReg = X86::BL; break;
4776 }
4777 if (DestReg) {
4778 Res.first = DestReg;
4779 Res.second = Res.second = X86::GR8RegisterClass;
4780 }
4781 } else if (VT == MVT::i32) {
4782 unsigned DestReg = 0;
4783 switch (Res.first) {
4784 default: break;
4785 case X86::AX: DestReg = X86::EAX; break;
4786 case X86::DX: DestReg = X86::EDX; break;
4787 case X86::CX: DestReg = X86::ECX; break;
4788 case X86::BX: DestReg = X86::EBX; break;
4789 case X86::SI: DestReg = X86::ESI; break;
4790 case X86::DI: DestReg = X86::EDI; break;
4791 case X86::BP: DestReg = X86::EBP; break;
4792 case X86::SP: DestReg = X86::ESP; break;
4793 }
4794 if (DestReg) {
4795 Res.first = DestReg;
4796 Res.second = Res.second = X86::GR32RegisterClass;
4797 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004798 } else if (VT == MVT::i64) {
4799 unsigned DestReg = 0;
4800 switch (Res.first) {
4801 default: break;
4802 case X86::AX: DestReg = X86::RAX; break;
4803 case X86::DX: DestReg = X86::RDX; break;
4804 case X86::CX: DestReg = X86::RCX; break;
4805 case X86::BX: DestReg = X86::RBX; break;
4806 case X86::SI: DestReg = X86::RSI; break;
4807 case X86::DI: DestReg = X86::RDI; break;
4808 case X86::BP: DestReg = X86::RBP; break;
4809 case X86::SP: DestReg = X86::RSP; break;
4810 }
4811 if (DestReg) {
4812 Res.first = DestReg;
4813 Res.second = Res.second = X86::GR64RegisterClass;
4814 }
Chris Lattner524129d2006-07-31 23:26:50 +00004815 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004816
Chris Lattner524129d2006-07-31 23:26:50 +00004817 return Res;
4818}