blob: 3ca9f56149d8b3a675e7cd3ab026ad0a783ad583 [file] [log] [blame]
Arnold Schwaighofere8d0bf22007-10-12 21:53:12 +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"
Lauro Ramos Venancio25188892007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng4fbf4592007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Chengaf598d22006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdc3adc82007-02-27 04:43:02 +000029#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000033#include "llvm/CodeGen/SelectionDAG.h"
34#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000035#include "llvm/Support/MathExtras.h"
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng4fbf4592007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattnerf6a69662006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Duncan Sandsce388532007-07-27 20:02:49 +000041#include "llvm/ParameterAttributes.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000042using namespace llvm;
43
Chris Lattner76ac0682005-11-15 00:40:23 +000044X86TargetLowering::X86TargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000046 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene36c4002007-09-23 14:52:20 +000047 X86ScalarSSEf64 = Subtarget->hasSSE2();
48 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000049 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +000050
Evan Chengcde9e302006-01-27 08:10:46 +000051
Anton Korobeynikov383a3242007-07-14 14:06:15 +000052 RegInfo = TM.getRegisterInfo();
53
Chris Lattner76ac0682005-11-15 00:40:23 +000054 // Set up the TargetLowering object.
55
56 // X86 is weird, it always uses i8 for shift amounts and setcc results.
57 setShiftAmountType(MVT::i8);
58 setSetCCResultType(MVT::i8);
59 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000060 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000061 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng11b0a5d2006-09-08 06:48:29 +000062 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng20931a72006-03-16 21:47:42 +000063
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000064 if (Subtarget->isTargetDarwin()) {
Evan Chengb09a56f2006-03-17 20:31:41 +000065 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000066 setUseUnderscoreSetJmp(false);
67 setUseUnderscoreLongJmp(false);
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +000068 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000069 // MS runtime is weird: it exports _setjmp, but longjmp!
70 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(false);
72 } else {
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
75 }
76
Chris Lattner76ac0682005-11-15 00:40:23 +000077 // Set up the register classes.
Evan Cheng9fee4422006-05-16 07:21:53 +000078 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
79 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
80 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000081 if (Subtarget->is64Bit())
82 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +000083
Evan Cheng5d9fd972006-10-04 00:56:09 +000084 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
85
Chris Lattner76ac0682005-11-15 00:40:23 +000086 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
87 // operation.
88 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000091
Evan Cheng11b0a5d2006-09-08 06:48:29 +000092 if (Subtarget->is64Bit()) {
93 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000094 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000095 } else {
Dale Johannesene36c4002007-09-23 14:52:20 +000096 if (X86ScalarSSEf64)
Evan Cheng11b0a5d2006-09-08 06:48:29 +000097 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
99 else
100 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
101 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000102
103 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
104 // this operation.
105 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
106 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000107 // SSE has no i16 to fp conversion, only i32
Dale Johannesene36c4002007-09-23 14:52:20 +0000108 if (X86ScalarSSEf32) {
Evan Cheng08390f62006-01-30 22:13:22 +0000109 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000110 // f32 and f64 cases are Legal, f80 case is not
111 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
112 } else {
Evan Cheng593bea72006-02-17 07:01:52 +0000113 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
115 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000116
Dale Johannesen7d67e542007-09-19 23:55:34 +0000117 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
118 // are Legal, f80 is custom lowered.
119 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
120 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000121
Evan Cheng08390f62006-01-30 22:13:22 +0000122 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
123 // this operation.
124 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
125 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
126
Dale Johannesene36c4002007-09-23 14:52:20 +0000127 if (X86ScalarSSEf32) {
Evan Cheng08390f62006-01-30 22:13:22 +0000128 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000129 // f32 and f64 cases are Legal, f80 case is not
130 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000131 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000132 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000133 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000134 }
135
136 // Handle FP_TO_UINT by promoting the destination to a larger signed
137 // conversion.
138 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
141
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000142 if (Subtarget->is64Bit()) {
143 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000145 } else {
Dale Johannesene36c4002007-09-23 14:52:20 +0000146 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000147 // Expand FP_TO_UINT into a select.
148 // FIXME: We would like to use a Custom expander here eventually to do
149 // the optimal thing for SSE vs. the default expansion in the legalizer.
150 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
151 else
152 // With SSE3 we can use fisttpll to convert to a signed i64.
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000155
Chris Lattner55c17f92006-12-05 18:22:22 +0000156 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene36c4002007-09-23 14:52:20 +0000157 if (!X86ScalarSSEf64) {
Chris Lattnerc20b7e82006-12-05 18:45:06 +0000158 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
159 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
160 }
Chris Lattner30107e62005-12-23 05:15:23 +0000161
Dan Gohmana1603612007-10-08 18:33:35 +0000162 // Scalar integer multiply, multiply-high, divide, and remainder are
163 // lowered to use operations that produce two results, to match the
164 // available instructions. This exposes the two-result form to trivial
165 // CSE, which is able to combine x/y and x%y into a single instruction,
166 // for example. The single-result multiply instructions are introduced
167 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
168 // is not needed.
169 setOperationAction(ISD::MUL , MVT::i8 , Expand);
170 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
171 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
172 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
173 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
174 setOperationAction(ISD::SREM , MVT::i8 , Expand);
175 setOperationAction(ISD::UREM , MVT::i8 , Expand);
176 setOperationAction(ISD::MUL , MVT::i16 , Expand);
177 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
178 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
179 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
180 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
181 setOperationAction(ISD::SREM , MVT::i16 , Expand);
182 setOperationAction(ISD::UREM , MVT::i16 , Expand);
183 setOperationAction(ISD::MUL , MVT::i32 , Expand);
184 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
185 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
186 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
187 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
188 setOperationAction(ISD::SREM , MVT::i32 , Expand);
189 setOperationAction(ISD::UREM , MVT::i32 , Expand);
190 setOperationAction(ISD::MUL , MVT::i64 , Expand);
191 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
192 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
193 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
194 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
195 setOperationAction(ISD::SREM , MVT::i64 , Expand);
196 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman31599682007-09-25 18:23:27 +0000197
Evan Cheng0d41d192006-10-30 08:02:39 +0000198 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng593bea72006-02-17 07:01:52 +0000199 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000200 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
201 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000202 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000203 if (Subtarget->is64Bit())
Christopher Lambb372aba2007-08-10 21:48:46 +0000204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattner76ac0682005-11-15 00:40:23 +0000207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
208 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000209 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikov91460e42007-11-16 01:31:51 +0000210 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
211
Chris Lattner76ac0682005-11-15 00:40:23 +0000212 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
213 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
214 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
215 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
216 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
217 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
218 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
219 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
220 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000221 if (Subtarget->is64Bit()) {
222 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
223 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
224 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
225 }
226
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000227 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000228 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000229
Chris Lattner76ac0682005-11-15 00:40:23 +0000230 // These should be promoted to a larger select which is supported.
231 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
232 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000233 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000234 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
235 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
236 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
237 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000238 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000239 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
240 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
241 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
242 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
243 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000244 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000245 if (Subtarget->is64Bit()) {
246 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
247 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
248 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000249 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000250 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000251 if (!Subtarget->is64Bit())
252 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
253
Nate Begeman7e5496d2006-02-17 00:03:04 +0000254 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000255 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000256 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000257 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000258 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000259 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000260 if (Subtarget->is64Bit()) {
261 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
262 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
263 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
264 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
265 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000266 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000267 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
268 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
269 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000270 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000271 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
272 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000273
Dan Gohman5e1a4282007-09-25 15:10:49 +0000274 // Use the default ISD::LOCATION expansion.
Chris Lattner9c415362005-11-29 06:16:21 +0000275 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000276 // FIXME - use subtarget debug flags
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000277 if (!Subtarget->isTargetDarwin() &&
278 !Subtarget->isTargetELF() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +0000279 !Subtarget->isTargetCygMing())
Jim Laskeyf9e54452007-01-26 14:34:52 +0000280 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000281
Anton Korobeynikovf1dcf692007-05-02 19:53:33 +0000282 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
283 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
284 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
285 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
286 if (Subtarget->is64Bit()) {
287 // FIXME: Verify
288 setExceptionPointerRegister(X86::RAX);
289 setExceptionSelectorRegister(X86::RDX);
290 } else {
291 setExceptionPointerRegister(X86::EAX);
292 setExceptionSelectorRegister(X86::EDX);
293 }
Anton Korobeynikov50ab26e2007-09-03 00:36:06 +0000294 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovf1dcf692007-05-02 19:53:33 +0000295
Duncan Sands86e01192007-09-11 14:10:23 +0000296 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsce388532007-07-27 20:02:49 +0000297
Nate Begemane74795c2006-01-25 18:21:52 +0000298 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
299 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemane74795c2006-01-25 18:21:52 +0000300 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemane74795c2006-01-25 18:21:52 +0000301 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengdeaea252007-03-02 23:16:35 +0000302 if (Subtarget->is64Bit())
303 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
304 else
305 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
306
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000307 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000308 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit())
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +0000311 if (Subtarget->isTargetCygMing())
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
313 else
314 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000315
Dale Johannesene36c4002007-09-23 14:52:20 +0000316 if (X86ScalarSSEf64) {
317 // f32 and f64 use SSE.
Chris Lattner76ac0682005-11-15 00:40:23 +0000318 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000319 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
320 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000321
Evan Cheng72d5c252006-01-31 22:28:30 +0000322 // Use ANDPD to simulate FABS.
323 setOperationAction(ISD::FABS , MVT::f64, Custom);
324 setOperationAction(ISD::FABS , MVT::f32, Custom);
325
326 // Use XORP to simulate FNEG.
327 setOperationAction(ISD::FNEG , MVT::f64, Custom);
328 setOperationAction(ISD::FNEG , MVT::f32, Custom);
329
Evan Cheng4363e882007-01-05 07:55:56 +0000330 // Use ANDPD and ORPD to simulate FCOPYSIGN.
331 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
332 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
333
Evan Chengd8fba3a2006-02-02 00:28:23 +0000334 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000335 setOperationAction(ISD::FSIN , MVT::f64, Expand);
336 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000337 setOperationAction(ISD::FREM , MVT::f64, Expand);
338 setOperationAction(ISD::FSIN , MVT::f32, Expand);
339 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000340 setOperationAction(ISD::FREM , MVT::f32, Expand);
341
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000342 // Expand FP immediates into loads from the stack, except for the special
343 // cases we handle.
344 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
345 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesene36c4002007-09-23 14:52:20 +0000346 addLegalFPImmediate(APFloat(+0.0)); // xorpd
347 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesenba1a98a2007-08-09 01:04:01 +0000348
349 // Conversions to long double (in X87) go through memory.
350 setConvertAction(MVT::f32, MVT::f80, Expand);
351 setConvertAction(MVT::f64, MVT::f80, Expand);
352
353 // Conversions from long double (in X87) go through memory.
354 setConvertAction(MVT::f80, MVT::f32, Expand);
355 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesene36c4002007-09-23 14:52:20 +0000356 } else if (X86ScalarSSEf32) {
357 // Use SSE for f32, x87 for f64.
358 // Set up the FP register classes.
359 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
360 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
361
362 // Use ANDPS to simulate FABS.
363 setOperationAction(ISD::FABS , MVT::f32, Custom);
364
365 // Use XORP to simulate FNEG.
366 setOperationAction(ISD::FNEG , MVT::f32, Custom);
367
368 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
369
370 // Use ANDPS and ORPS to simulate FCOPYSIGN.
371 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
372 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
373
374 // We don't support sin/cos/fmod
375 setOperationAction(ISD::FSIN , MVT::f32, Expand);
376 setOperationAction(ISD::FCOS , MVT::f32, Expand);
377 setOperationAction(ISD::FREM , MVT::f32, Expand);
378
379 // Expand FP immediates into loads from the stack, except for the special
380 // cases we handle.
381 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
382 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
383 addLegalFPImmediate(APFloat(+0.0f)); // xorps
384 addLegalFPImmediate(APFloat(+0.0)); // FLD0
385 addLegalFPImmediate(APFloat(+1.0)); // FLD1
386 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
387 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
388
389 // SSE->x87 conversions go through memory.
390 setConvertAction(MVT::f32, MVT::f64, Expand);
391 setConvertAction(MVT::f32, MVT::f80, Expand);
392
393 // x87->SSE truncations need to go through memory.
394 setConvertAction(MVT::f80, MVT::f32, Expand);
395 setConvertAction(MVT::f64, MVT::f32, Expand);
396 // And x87->x87 truncations also.
397 setConvertAction(MVT::f80, MVT::f64, Expand);
398
399 if (!UnsafeFPMath) {
400 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
401 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
402 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000403 } else {
Dale Johannesene36c4002007-09-23 14:52:20 +0000404 // f32 and f64 in x87.
Chris Lattner76ac0682005-11-15 00:40:23 +0000405 // Set up the FP register classes.
Dale Johannesena2b3c172007-07-03 00:53:03 +0000406 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
407 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000408
Evan Cheng4363e882007-01-05 07:55:56 +0000409 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesena2b3c172007-07-03 00:53:03 +0000410 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng4363e882007-01-05 07:55:56 +0000411 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
412 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesenba1a98a2007-08-09 01:04:01 +0000413
414 // Floating truncations need to go through memory.
415 setConvertAction(MVT::f80, MVT::f32, Expand);
416 setConvertAction(MVT::f64, MVT::f32, Expand);
417 setConvertAction(MVT::f80, MVT::f64, Expand);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000418
Chris Lattner76ac0682005-11-15 00:40:23 +0000419 if (!UnsafeFPMath) {
420 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
421 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
422 }
423
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000424 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesena2b3c172007-07-03 00:53:03 +0000425 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesend246b2c2007-08-30 00:23:21 +0000426 addLegalFPImmediate(APFloat(+0.0)); // FLD0
427 addLegalFPImmediate(APFloat(+1.0)); // FLD1
428 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
429 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene36c4002007-09-23 14:52:20 +0000430 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
431 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
432 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
433 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattner76ac0682005-11-15 00:40:23 +0000434 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000435
Dale Johannesenb1888e72007-08-05 18:49:15 +0000436 // Long double always uses X87.
437 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000438 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
440 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesenb6d56402007-09-26 21:10:55 +0000441 if (!UnsafeFPMath) {
442 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
443 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
444 }
Dale Johannesenb1888e72007-08-05 18:49:15 +0000445
Dan Gohman482732a2007-10-11 23:21:31 +0000446 // Always use a library call for pow.
447 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
448 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
449 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
450
Evan Cheng19264272006-03-01 01:11:20 +0000451 // First set operation action for all vector types to expand. Then we
452 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmaneefa83e2007-05-18 18:44:07 +0000453 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
454 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Cheng19264272006-03-01 01:11:20 +0000455 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
456 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000457 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Cheng444d3ca2007-06-29 00:18:15 +0000458 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000459 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000460 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000461 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000467 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000468 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000469 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000470 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman57111e72007-07-10 00:05:58 +0000471 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohmana1603612007-10-08 18:33:35 +0000478 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
481 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman482732a2007-10-11 23:21:31 +0000482 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman8d978da2007-10-12 14:09:42 +0000483 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000486 }
487
Evan Chengbc047222006-03-22 19:22:18 +0000488 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000489 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
490 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
491 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendling98d21042007-03-26 07:53:08 +0000492 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng9e252e32006-02-22 02:26:30 +0000493
Evan Cheng19264272006-03-01 01:11:20 +0000494 // FIXME: add MMX packed arithmetics
Bill Wendling97905b42007-03-07 05:43:18 +0000495
Bill Wendling6092ce22007-03-08 22:09:11 +0000496 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
497 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
498 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner2805bce2007-04-12 04:14:49 +0000499 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000500
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000501 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
502 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
503 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6aa304e2007-10-30 01:18:38 +0000504 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000505
Bill Wendlinge3103412007-03-15 21:24:36 +0000506 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
507 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
508
Bill Wendling144b8bb2007-03-16 09:44:46 +0000509 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000510 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000511 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000512 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
513 setOperationAction(ISD::AND, MVT::v2i32, Promote);
514 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
515 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000516
517 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000518 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000519 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000520 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
521 setOperationAction(ISD::OR, MVT::v2i32, Promote);
522 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
523 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000524
525 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000526 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000527 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000528 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
529 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
530 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
531 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000532
Bill Wendling6092ce22007-03-08 22:09:11 +0000533 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000534 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling6092ce22007-03-08 22:09:11 +0000535 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000536 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
537 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
538 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
539 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000540
Bill Wendling6dff51a2007-03-27 20:22:40 +0000541 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
542 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
543 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
544 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlingd551a182007-03-22 18:42:45 +0000545
546 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
547 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
548 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendling6dff51a2007-03-27 20:22:40 +0000549 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendlingad2db4a2007-03-28 00:57:11 +0000550
551 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
552 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling591eab82007-04-24 21:16:55 +0000553 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
554 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000555 }
556
Evan Chengbc047222006-03-22 19:22:18 +0000557 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000558 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
559
Evan Chengbf3df772006-10-27 18:49:08 +0000560 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
561 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
562 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
563 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman57111e72007-07-10 00:05:58 +0000564 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
565 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000566 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
567 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
568 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000569 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000570 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000571 }
572
Evan Chengbc047222006-03-22 19:22:18 +0000573 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000574 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
575 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
576 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
577 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
578 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
579
Evan Cheng617a6a82006-04-10 07:23:14 +0000580 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
581 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
582 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000583 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000584 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
585 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
586 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000587 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000588 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengbf3df772006-10-27 18:49:08 +0000589 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
590 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
591 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
592 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman57111e72007-07-10 00:05:58 +0000593 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
594 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000595
Evan Cheng617a6a82006-04-10 07:23:14 +0000596 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
597 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000598 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000599 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
600 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
601 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000602
Evan Cheng92232302006-04-12 21:21:57 +0000603 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
604 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemana55a67a2007-12-11 01:41:33 +0000605 // Do not attempt to custom lower non-power-of-2 vectors
606 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
607 continue;
Evan Cheng92232302006-04-12 21:21:57 +0000608 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
609 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
610 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
611 }
612 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
613 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
614 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
616 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesenb066c1f2007-10-31 00:32:36 +0000617 if (Subtarget->is64Bit())
618 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000619
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000620 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng92232302006-04-12 21:21:57 +0000621 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
622 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
623 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
624 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
625 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
626 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
627 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000628 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
629 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000630 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
631 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000632 }
Evan Cheng92232302006-04-12 21:21:57 +0000633
634 // Custom lower v2i64 and v2f64 selects.
635 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000636 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000637 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000638 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000639 }
640
Evan Cheng78038292006-04-05 23:38:46 +0000641 // We want to custom lower some of our intrinsics.
642 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
643
Evan Cheng5987cfb2006-07-07 08:33:52 +0000644 // We have target-specific dag combine patterns for the following nodes:
645 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner9259b1e2006-10-04 06:57:07 +0000646 setTargetDAGCombine(ISD::SELECT);
Evan Cheng5987cfb2006-07-07 08:33:52 +0000647
Chris Lattner76ac0682005-11-15 00:40:23 +0000648 computeRegisterProperties();
649
Evan Cheng6a374562006-02-14 08:25:08 +0000650 // FIXME: These should be based on subtarget info. Plus, the values should
651 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000652 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
653 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
654 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000655 allowUnalignedMemoryAccesses = true; // x86 supports it!
656}
657
Chris Lattner3c763092007-02-25 08:29:00 +0000658
Evan Cheng797d56f2007-11-09 01:32:10 +0000659/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
660/// jumptable.
661SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
662 SelectionDAG &DAG) const {
663 if (usesGlobalOffsetTable())
664 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
665 if (!Subtarget->isPICStyleRIPRel())
666 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
667 return Table;
668}
669
Chris Lattner3c763092007-02-25 08:29:00 +0000670//===----------------------------------------------------------------------===//
671// Return Value Calling Convention Implementation
672//===----------------------------------------------------------------------===//
673
Chris Lattnerba3d2732007-02-28 04:55:35 +0000674#include "X86GenCallingConv.inc"
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000675
676/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
677/// exists skip possible ISD:TokenFactor.
678static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
679 if (Chain.getOpcode()==X86ISD::TAILCALL) {
680 return Chain;
681 } else if (Chain.getOpcode()==ISD::TokenFactor) {
682 if (Chain.getNumOperands() &&
683 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
684 return Chain.getOperand(0);
685 }
686 return Chain;
687}
Chris Lattnerc9eed392007-02-27 05:28:59 +0000688
Chris Lattner2fc0d702007-02-25 09:12:39 +0000689/// LowerRET - Lower an ISD::RET node.
690SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
691 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
692
Chris Lattnerc9eed392007-02-27 05:28:59 +0000693 SmallVector<CCValAssign, 16> RVLocs;
694 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner944200b2007-06-19 00:13:10 +0000695 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
696 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000697 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000698
Chris Lattner2fc0d702007-02-25 09:12:39 +0000699 // If this is the first return lowered for this function, add the regs to the
700 // liveout set for the function.
701 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000702 for (unsigned i = 0; i != RVLocs.size(); ++i)
703 if (RVLocs[i].isRegLoc())
704 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2fc0d702007-02-25 09:12:39 +0000705 }
Chris Lattner2fc0d702007-02-25 09:12:39 +0000706 SDOperand Chain = Op.getOperand(0);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000707
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000708 // Handle tail call return.
709 Chain = GetPossiblePreceedingTailCall(Chain);
710 if (Chain.getOpcode() == X86ISD::TAILCALL) {
711 SDOperand TailCall = Chain;
712 SDOperand TargetAddress = TailCall.getOperand(1);
713 SDOperand StackAdjustment = TailCall.getOperand(2);
714 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
715 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
716 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
717 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
718 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
719 "Expecting an global address, external symbol, or register");
720 assert( StackAdjustment.getOpcode() == ISD::Constant &&
721 "Expecting a const value");
722
723 SmallVector<SDOperand,8> Operands;
724 Operands.push_back(Chain.getOperand(0));
725 Operands.push_back(TargetAddress);
726 Operands.push_back(StackAdjustment);
727 // Copy registers used by the call. Last operand is a flag so it is not
728 // copied.
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +0000729 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000730 Operands.push_back(Chain.getOperand(i));
731 }
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +0000732 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
733 Operands.size());
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000734 }
735
736 // Regular return.
737 SDOperand Flag;
738
Chris Lattner2fc0d702007-02-25 09:12:39 +0000739 // Copy the result values into the output registers.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000740 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
741 RVLocs[0].getLocReg() != X86::ST0) {
742 for (unsigned i = 0; i != RVLocs.size(); ++i) {
743 CCValAssign &VA = RVLocs[i];
744 assert(VA.isRegLoc() && "Can only return in registers!");
745 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
746 Flag);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000747 Flag = Chain.getValue(1);
748 }
749 } else {
750 // We need to handle a destination of ST0 specially, because it isn't really
751 // a register.
752 SDOperand Value = Op.getOperand(1);
753
754 // If this is an FP return with ScalarSSE, we need to move the value from
755 // an XMM register onto the fp-stack.
Dale Johannesene36c4002007-09-23 14:52:20 +0000756 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
757 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2fc0d702007-02-25 09:12:39 +0000758 SDOperand MemLoc;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000759
Chris Lattner2fc0d702007-02-25 09:12:39 +0000760 // If this is a load into a scalarsse value, don't store the loaded value
761 // back to the stack, only to reload it: just replace the scalar-sse load.
762 if (ISD::isNON_EXTLoad(Value.Val) &&
763 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
764 Chain = Value.getOperand(0);
765 MemLoc = Value.getOperand(1);
766 } else {
767 // Spill the value to memory and reload it into top of stack.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000768 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2fc0d702007-02-25 09:12:39 +0000769 MachineFunction &MF = DAG.getMachineFunction();
770 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
771 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
772 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
773 }
Dale Johannesena2b3c172007-07-03 00:53:03 +0000774 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000775 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2fc0d702007-02-25 09:12:39 +0000776 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
777 Chain = Value.getValue(1);
778 }
779
780 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
781 SDOperand Ops[] = { Chain, Value };
782 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
783 Flag = Chain.getValue(1);
784 }
785
786 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
787 if (Flag.Val)
788 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
789 else
790 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
791}
792
793
Chris Lattner0cd99602007-02-25 08:59:22 +0000794/// LowerCallResult - Lower the result values of an ISD::CALL into the
795/// appropriate copies out of appropriate physical registers. This assumes that
796/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
797/// being lowered. The returns a SDNode with the same number of values as the
798/// ISD::CALL.
799SDNode *X86TargetLowering::
800LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
801 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattner152bfa12007-02-28 07:09:55 +0000802
803 // Assign locations to each value returned by this call.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000804 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner944200b2007-06-19 00:13:10 +0000805 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
806 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000807 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
808
Chris Lattner0cd99602007-02-25 08:59:22 +0000809
Chris Lattner152bfa12007-02-28 07:09:55 +0000810 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner0cd99602007-02-25 08:59:22 +0000811
812 // Copy all of the result registers out of their specified physreg.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000813 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
814 for (unsigned i = 0; i != RVLocs.size(); ++i) {
815 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
816 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner0cd99602007-02-25 08:59:22 +0000817 InFlag = Chain.getValue(2);
818 ResultVals.push_back(Chain.getValue(0));
819 }
820 } else {
821 // Copies from the FP stack are special, as ST0 isn't a valid register
822 // before the fp stackifier runs.
823
824 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesena2b3c172007-07-03 00:53:03 +0000825 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner0cd99602007-02-25 08:59:22 +0000826 SDOperand GROps[] = { Chain, InFlag };
827 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
828 Chain = RetVal.getValue(1);
829 InFlag = RetVal.getValue(2);
830
831 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
832 // an XMM register.
Dale Johannesene36c4002007-09-23 14:52:20 +0000833 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
834 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner0cd99602007-02-25 08:59:22 +0000835 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
836 // shouldn't be necessary except that RFP cannot be live across
837 // multiple blocks. When stackifier is fixed, they can be uncoupled.
838 MachineFunction &MF = DAG.getMachineFunction();
839 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
840 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
841 SDOperand Ops[] = {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000842 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner0cd99602007-02-25 08:59:22 +0000843 };
844 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000845 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner0cd99602007-02-25 08:59:22 +0000846 Chain = RetVal.getValue(1);
847 }
Chris Lattner0cd99602007-02-25 08:59:22 +0000848 ResultVals.push_back(RetVal);
849 }
850
851 // Merge everything together with a MERGE_VALUES node.
852 ResultVals.push_back(Chain);
853 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
854 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner3c763092007-02-25 08:29:00 +0000855}
856
857
Chris Lattner76ac0682005-11-15 00:40:23 +0000858//===----------------------------------------------------------------------===//
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000859// C & StdCall & Fast Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000860//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000861// StdCall calling convention seems to be standard for many Windows' API
862// routines and around. It differs from C calling convention just a little:
863// callee should clean up the stack, not caller. Symbols should be also
864// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000865// For info on fast calling convention see Fast Calling Convention (tail call)
866// implementation LowerX86_32FastCCCallTo.
Chris Lattner76ac0682005-11-15 00:40:23 +0000867
Evan Cheng24eb3f42006-04-27 05:35:28 +0000868/// AddLiveIn - This helper function adds the specified physical register to the
869/// MachineFunction as a live in value. It also creates a corresponding virtual
870/// register for it.
871static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000872 const TargetRegisterClass *RC) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000873 assert(RC->contains(PReg) && "Not the correct regclass!");
874 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
875 MF.addLiveIn(PReg, VReg);
876 return VReg;
877}
878
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000879// align stack arguments according to platform alignment needed for tail calls
880unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
881
Rafael Espindola272f7302007-09-14 15:48:13 +0000882SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
883 const CCValAssign &VA,
884 MachineFrameInfo *MFI,
885 SDOperand Root, unsigned i) {
886 // Create the nodes corresponding to a load from this parameter slot.
887 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
888 VA.getLocMemOffset());
889 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
890
891 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
892
893 if (Flags & ISD::ParamFlags::ByVal)
894 return FIN;
895 else
896 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
897}
898
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000899SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
900 bool isStdCall) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000901 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000902 MachineFunction &MF = DAG.getMachineFunction();
903 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000904 SDOperand Root = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000905 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000906 unsigned CC = MF.getFunction()->getCallingConv();
Chris Lattner227b6c52007-02-28 07:00:42 +0000907 // Assign locations to all of the incoming arguments.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000908 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000909 CCState CCInfo(CC, isVarArg,
Chris Lattner944200b2007-06-19 00:13:10 +0000910 getTargetMachine(), ArgLocs);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000911 // Check for possible tail call calling convention.
912 if (CC == CallingConv::Fast && PerformTailCallOpt)
913 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
914 else
915 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
916
Chris Lattnerb9db2252007-02-28 05:46:49 +0000917 SmallVector<SDOperand, 8> ArgValues;
918 unsigned LastVal = ~0U;
919 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
920 CCValAssign &VA = ArgLocs[i];
921 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
922 // places.
923 assert(VA.getValNo() != LastVal &&
924 "Don't support value assigned to multiple locs yet");
925 LastVal = VA.getValNo();
926
927 if (VA.isRegLoc()) {
928 MVT::ValueType RegVT = VA.getLocVT();
929 TargetRegisterClass *RC;
930 if (RegVT == MVT::i32)
931 RC = X86::GR32RegisterClass;
932 else {
933 assert(MVT::isVector(RegVT));
934 RC = X86::VR128RegisterClass;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000935 }
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000936
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000937 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
938 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerb9db2252007-02-28 05:46:49 +0000939
940 // If this is an 8 or 16-bit value, it is really passed promoted to 32
941 // bits. Insert an assert[sz]ext to capture this, then truncate to the
942 // right size.
943 if (VA.getLocInfo() == CCValAssign::SExt)
944 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
945 DAG.getValueType(VA.getValVT()));
946 else if (VA.getLocInfo() == CCValAssign::ZExt)
947 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
948 DAG.getValueType(VA.getValVT()));
949
950 if (VA.getLocInfo() != CCValAssign::Full)
951 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
952
953 ArgValues.push_back(ArgValue);
954 } else {
955 assert(VA.isMemLoc());
Rafael Espindola272f7302007-09-14 15:48:13 +0000956 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000957 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000958 }
Chris Lattnerb9db2252007-02-28 05:46:49 +0000959
960 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000961 // align stack specially for tail calls
962 if (CC==CallingConv::Fast)
963 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000964
Evan Cheng17e734f2006-05-23 21:06:34 +0000965 ArgValues.push_back(Root);
966
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000967 // If the function takes variable number of arguments, make a frame index for
968 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000969 if (isVarArg)
Chris Lattnerb9db2252007-02-28 05:46:49 +0000970 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000971
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000972 // Tail call calling convention (CallingConv::Fast) does not support varargs.
973 assert( !(isVarArg && CC == CallingConv::Fast) &&
974 "CallingConv::Fast does not support varargs.");
975
976 if (isStdCall && !isVarArg &&
977 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Chris Lattnerb9db2252007-02-28 05:46:49 +0000978 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000979 BytesCallerReserves = 0;
980 } else {
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000981 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000982
983 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000984 if (NumArgs &&
985 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000986 ISD::ParamFlags::StructReturn))
Chris Lattnerb9db2252007-02-28 05:46:49 +0000987 BytesToPopOnReturn = 4;
988
989 BytesCallerReserves = StackSize;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000990 }
Anton Korobeynikov597c8b72007-08-15 17:12:32 +0000991
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000992 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng17e734f2006-05-23 21:06:34 +0000993
Anton Korobeynikov597c8b72007-08-15 17:12:32 +0000994 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
995 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000996
Evan Cheng17e734f2006-05-23 21:06:34 +0000997 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +0000998 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +0000999 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001000}
1001
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001002SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +00001003 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +00001004 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001005 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng2a330942006-05-25 00:59:30 +00001006 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +00001007 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001008
Chris Lattner227b6c52007-02-28 07:00:42 +00001009 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerbe799592007-02-28 05:31:48 +00001010 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001011 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001012 if(CC==CallingConv::Fast && PerformTailCallOpt)
1013 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1014 else
1015 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001016
Chris Lattnerbe799592007-02-28 05:31:48 +00001017 // Get a count of how many bytes are to be pushed on the stack.
1018 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001019 if (CC==CallingConv::Fast)
1020 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattner76ac0682005-11-15 00:40:23 +00001021
Evan Cheng2a330942006-05-25 00:59:30 +00001022 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +00001023
Chris Lattner35a08552007-02-25 07:10:00 +00001024 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1025 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng2a330942006-05-25 00:59:30 +00001026
Chris Lattnerbe799592007-02-28 05:31:48 +00001027 SDOperand StackPtr;
Chris Lattnerbe799592007-02-28 05:31:48 +00001028
1029 // Walk the register/memloc assignments, inserting copies/loads.
1030 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1031 CCValAssign &VA = ArgLocs[i];
1032 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001033
Chris Lattnerbe799592007-02-28 05:31:48 +00001034 // Promote the value if needed.
1035 switch (VA.getLocInfo()) {
1036 default: assert(0 && "Unknown loc info!");
1037 case CCValAssign::Full: break;
1038 case CCValAssign::SExt:
1039 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1040 break;
1041 case CCValAssign::ZExt:
1042 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1043 break;
1044 case CCValAssign::AExt:
1045 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1046 break;
Evan Cheng5ee96892006-05-25 18:56:34 +00001047 }
Chris Lattnerbe799592007-02-28 05:31:48 +00001048
1049 if (VA.isRegLoc()) {
1050 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1051 } else {
1052 assert(VA.isMemLoc());
1053 if (StackPtr.Val == 0)
1054 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola4730c042007-09-21 15:50:22 +00001055
1056 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1057 Arg));
Chris Lattner76ac0682005-11-15 00:40:23 +00001058 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001059 }
1060
Chris Lattner5958b172007-02-28 05:39:26 +00001061 // If the first argument is an sret pointer, remember it.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +00001062 bool isSRet = NumOps &&
1063 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00001064 ISD::ParamFlags::StructReturn);
Chris Lattner5958b172007-02-28 05:39:26 +00001065
Evan Cheng2a330942006-05-25 00:59:30 +00001066 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001067 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1068 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +00001069
Evan Cheng88decde2006-04-28 21:29:37 +00001070 // Build a sequence of copy-to-reg nodes chained together with token chain
1071 // and flag operands which copy the outgoing args into registers.
1072 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001073 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1074 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1075 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +00001076 InFlag = Chain.getValue(1);
1077 }
1078
Evan Cheng84a041e2007-02-21 21:18:14 +00001079 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1080 // GOT pointer.
Evan Cheng1281dc32007-01-22 21:34:25 +00001081 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1082 Subtarget->isPICStyleGOT()) {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001083 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1084 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1085 InFlag);
1086 InFlag = Chain.getValue(1);
1087 }
1088
Evan Cheng2a330942006-05-25 00:59:30 +00001089 // If the callee is a GlobalAddress node (quite common, every direct call is)
1090 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001091 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00001092 // We should use extra load for direct calls to dllimported functions in
1093 // non-JIT mode.
1094 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1095 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001096 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1097 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +00001098 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1099
Chris Lattnere56fef92007-02-25 06:40:16 +00001100 // Returns a chain & a flag for retval copy to use.
1101 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00001102 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001103 Ops.push_back(Chain);
1104 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +00001105
1106 // Add argument registers to the end of the list so that they are known live
1107 // into the call.
1108 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001109 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +00001110 RegsToPass[i].second.getValueType()));
Evan Cheng84a041e2007-02-21 21:18:14 +00001111
1112 // Add an implicit use GOT pointer in EBX.
1113 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1114 Subtarget->isPICStyleGOT())
1115 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001116
Evan Cheng88decde2006-04-28 21:29:37 +00001117 if (InFlag.Val)
1118 Ops.push_back(InFlag);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001119
1120 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng88decde2006-04-28 21:29:37 +00001121 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +00001122
Chris Lattner8be5be82006-05-23 18:50:38 +00001123 // Create the CALLSEQ_END node.
1124 unsigned NumBytesForCalleeToPush = 0;
1125
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001126 if (CC == CallingConv::X86_StdCall ||
1127 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Chris Lattner7802f3e2007-02-25 09:06:15 +00001128 if (isVarArg)
Chris Lattner5958b172007-02-28 05:39:26 +00001129 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner7802f3e2007-02-25 09:06:15 +00001130 else
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001131 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001132 assert(!(isVarArg && CC==CallingConv::Fast) &&
1133 "CallingConv::Fast does not support varargs.");
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001134 } else {
1135 // If this is is a call to a struct-return function, the callee
1136 // pops the hidden struct pointer, so we have to push it back.
1137 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattner5958b172007-02-28 05:39:26 +00001138 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001139 }
Bill Wendlingf359fed2007-11-13 00:44:25 +00001140
1141 Chain = DAG.getCALLSEQ_END(Chain,
1142 DAG.getConstant(NumBytes, getPointerTy()),
1143 DAG.getConstant(NumBytesForCalleeToPush,
1144 getPointerTy()),
1145 InFlag);
Chris Lattner0cd99602007-02-25 08:59:22 +00001146 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001147
Chris Lattner0cd99602007-02-25 08:59:22 +00001148 // Handle result values, copying them out of physregs into vregs that we
1149 // return.
Chris Lattner7802f3e2007-02-25 09:06:15 +00001150 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001151}
1152
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001153
1154//===----------------------------------------------------------------------===//
Chris Lattner3066bec2007-02-28 06:10:12 +00001155// FastCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +00001156//===----------------------------------------------------------------------===//
1157//
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001158// The X86 'fastcall' calling convention passes up to two integer arguments in
1159// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1160// and requires that the callee pop its arguments off the stack (allowing proper
1161// tail calls), and has the same return value conventions as C calling convs.
1162//
1163// This calling convention always arranges for the callee pop value to be 8n+4
1164// bytes, which is needed for tail recursion elimination and stack alignment
1165// reasons.
Evan Cheng17e734f2006-05-23 21:06:34 +00001166SDOperand
Chris Lattner3ed3be32007-02-28 06:05:16 +00001167X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001168 MachineFunction &MF = DAG.getMachineFunction();
1169 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +00001170 SDOperand Root = Op.getOperand(0);
Chris Lattner944200b2007-06-19 00:13:10 +00001171 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner76ac0682005-11-15 00:40:23 +00001172
Chris Lattner227b6c52007-02-28 07:00:42 +00001173 // Assign locations to all of the incoming arguments.
Chris Lattner66e1d1d2007-02-28 06:21:19 +00001174 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001175 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1176 getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001177 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattner66e1d1d2007-02-28 06:21:19 +00001178
1179 SmallVector<SDOperand, 8> ArgValues;
1180 unsigned LastVal = ~0U;
1181 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1182 CCValAssign &VA = ArgLocs[i];
1183 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1184 // places.
1185 assert(VA.getValNo() != LastVal &&
1186 "Don't support value assigned to multiple locs yet");
1187 LastVal = VA.getValNo();
1188
1189 if (VA.isRegLoc()) {
1190 MVT::ValueType RegVT = VA.getLocVT();
1191 TargetRegisterClass *RC;
1192 if (RegVT == MVT::i32)
1193 RC = X86::GR32RegisterClass;
1194 else {
1195 assert(MVT::isVector(RegVT));
1196 RC = X86::VR128RegisterClass;
1197 }
1198
Chris Lattner9c7e5e32007-03-02 05:12:29 +00001199 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1200 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner66e1d1d2007-02-28 06:21:19 +00001201
1202 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1203 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1204 // right size.
1205 if (VA.getLocInfo() == CCValAssign::SExt)
1206 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1207 DAG.getValueType(VA.getValVT()));
1208 else if (VA.getLocInfo() == CCValAssign::ZExt)
1209 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1210 DAG.getValueType(VA.getValVT()));
1211
1212 if (VA.getLocInfo() != CCValAssign::Full)
1213 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1214
1215 ArgValues.push_back(ArgValue);
1216 } else {
1217 assert(VA.isMemLoc());
Rafael Espindolaf065f0e2007-09-21 14:55:38 +00001218 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattner66e1d1d2007-02-28 06:21:19 +00001219 }
1220 }
1221
Evan Cheng17e734f2006-05-23 21:06:34 +00001222 ArgValues.push_back(Root);
1223
Chris Lattner66e1d1d2007-02-28 06:21:19 +00001224 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001225
Anton Korobeynikov57af2a42007-03-02 21:50:27 +00001226 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001227 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001228 // arguments and the arguments after the retaddr has been pushed are
1229 // aligned.
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001230 if ((StackSize & 7) == 0)
1231 StackSize += 4;
1232 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001233
1234 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001235 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattner66e1d1d2007-02-28 06:21:19 +00001236 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +00001237 BytesCallerReserves = 0;
1238
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00001239 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1240 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001241
Evan Cheng17e734f2006-05-23 21:06:34 +00001242 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +00001243 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +00001244 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001245}
1246
Rafael Espindolae636fc02007-08-31 15:06:30 +00001247SDOperand
1248X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1249 const SDOperand &StackPtr,
1250 const CCValAssign &VA,
1251 SDOperand Chain,
1252 SDOperand Arg) {
1253 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1254 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1255 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1256 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1257 if (Flags & ISD::ParamFlags::ByVal) {
1258 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1259 ISD::ParamFlags::ByValAlignOffs);
1260
Rafael Espindolae636fc02007-08-31 15:06:30 +00001261 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1262 ISD::ParamFlags::ByValSizeOffs;
1263
1264 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1265 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Chris Lattnerf81d5882007-11-24 07:07:01 +00001266 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Rafael Espindolae636fc02007-08-31 15:06:30 +00001267
Rafael Espindola846c19dd2007-10-19 10:41:11 +00001268 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1269 AlwaysInline);
Rafael Espindolae636fc02007-08-31 15:06:30 +00001270 } else {
1271 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1272 }
1273}
1274
Chris Lattner104aa5d2006-09-26 03:57:53 +00001275SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +00001276 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +00001277 SDOperand Chain = Op.getOperand(0);
Evan Cheng2a330942006-05-25 00:59:30 +00001278 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner944200b2007-06-19 00:13:10 +00001279 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng2a330942006-05-25 00:59:30 +00001280 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +00001281
Chris Lattner227b6c52007-02-28 07:00:42 +00001282 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerd439e862007-02-28 06:26:33 +00001283 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001284 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001285 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerd439e862007-02-28 06:26:33 +00001286
1287 // Get a count of how many bytes are to be pushed on the stack.
1288 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +00001289
Anton Korobeynikov57af2a42007-03-02 21:50:27 +00001290 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001291 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001292 // arguments and the arguments after the retaddr has been pushed are
1293 // aligned.
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001294 if ((NumBytes & 7) == 0)
1295 NumBytes += 4;
1296 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001297
Chris Lattner62c34842006-02-13 09:00:43 +00001298 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerd439e862007-02-28 06:26:33 +00001299
Chris Lattner35a08552007-02-25 07:10:00 +00001300 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1301 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerd439e862007-02-28 06:26:33 +00001302
1303 SDOperand StackPtr;
1304
1305 // Walk the register/memloc assignments, inserting copies/loads.
1306 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1307 CCValAssign &VA = ArgLocs[i];
1308 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1309
1310 // Promote the value if needed.
1311 switch (VA.getLocInfo()) {
1312 default: assert(0 && "Unknown loc info!");
1313 case CCValAssign::Full: break;
1314 case CCValAssign::SExt:
1315 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner3ed3be32007-02-28 06:05:16 +00001316 break;
Chris Lattnerd439e862007-02-28 06:26:33 +00001317 case CCValAssign::ZExt:
1318 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1319 break;
1320 case CCValAssign::AExt:
1321 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1322 break;
1323 }
1324
1325 if (VA.isRegLoc()) {
1326 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1327 } else {
1328 assert(VA.isMemLoc());
1329 if (StackPtr.Val == 0)
1330 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola4730c042007-09-21 15:50:22 +00001331
1332 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1333 Arg));
Evan Cheng2a330942006-05-25 00:59:30 +00001334 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001335 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001336
Evan Cheng2a330942006-05-25 00:59:30 +00001337 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001338 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1339 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +00001340
Nate Begeman7e5496d2006-02-17 00:03:04 +00001341 // Build a sequence of copy-to-reg nodes chained together with token chain
1342 // and flag operands which copy the outgoing args into registers.
1343 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001344 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1345 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1346 InFlag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001347 InFlag = Chain.getValue(1);
1348 }
1349
Evan Cheng2a330942006-05-25 00:59:30 +00001350 // If the callee is a GlobalAddress node (quite common, every direct call is)
1351 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001352 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00001353 // We should use extra load for direct calls to dllimported functions in
1354 // non-JIT mode.
1355 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1356 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001357 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1358 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +00001359 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1360
Evan Cheng84a041e2007-02-21 21:18:14 +00001361 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1362 // GOT pointer.
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001363 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1364 Subtarget->isPICStyleGOT()) {
1365 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1366 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1367 InFlag);
1368 InFlag = Chain.getValue(1);
1369 }
1370
Chris Lattnere56fef92007-02-25 06:40:16 +00001371 // Returns a chain & a flag for retval copy to use.
1372 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00001373 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001374 Ops.push_back(Chain);
1375 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +00001376
1377 // Add argument registers to the end of the list so that they are known live
1378 // into the call.
1379 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001380 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +00001381 RegsToPass[i].second.getValueType()));
1382
Evan Cheng84a041e2007-02-21 21:18:14 +00001383 // Add an implicit use GOT pointer in EBX.
1384 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1385 Subtarget->isPICStyleGOT())
1386 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1387
Nate Begeman7e5496d2006-02-17 00:03:04 +00001388 if (InFlag.Val)
1389 Ops.push_back(InFlag);
1390
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001391 assert(isTailCall==false && "no tail call here");
1392 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001393 NodeTys, &Ops[0], Ops.size());
Nate Begeman7e5496d2006-02-17 00:03:04 +00001394 InFlag = Chain.getValue(1);
1395
Chris Lattnerd6b853ad2007-02-25 07:18:38 +00001396 // Returns a flag for retval copy to use.
1397 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001398 Ops.clear();
1399 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001400 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1401 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001402 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001403 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattnerba474f52007-02-25 09:10:05 +00001404 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001405
Chris Lattnerba474f52007-02-25 09:10:05 +00001406 // Handle result values, copying them out of physregs into vregs that we
1407 // return.
1408 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001409}
1410
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001411//===----------------------------------------------------------------------===//
1412// Fast Calling Convention (tail call) implementation
1413//===----------------------------------------------------------------------===//
1414
1415// Like std call, callee cleans arguments, convention except that ECX is
1416// reserved for storing the tail called function address. Only 2 registers are
1417// free for argument passing (inreg). Tail call optimization is performed
1418// provided:
1419// * tailcallopt is enabled
1420// * caller/callee are fastcc
1421// * elf/pic is disabled OR
1422// * elf/pic enabled + callee is in module + callee has
1423// visibility protected or hidden
Arnold Schwaighofer1f0da1f2007-10-12 21:30:57 +00001424// To keep the stack aligned according to platform abi the function
1425// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1426// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001427// If a tail called function callee has more arguments than the caller the
1428// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer1f0da1f2007-10-12 21:30:57 +00001429// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001430// original REtADDR, but before the saved framepointer or the spilled registers
1431// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1432// stack layout:
1433// arg1
1434// arg2
1435// RETADDR
1436// [ new RETADDR
1437// move area ]
1438// (possible EBP)
1439// ESI
1440// EDI
1441// local1 ..
1442
1443/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1444/// for a 16 byte align requirement.
1445unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1446 SelectionDAG& DAG) {
1447 if (PerformTailCallOpt) {
1448 MachineFunction &MF = DAG.getMachineFunction();
1449 const TargetMachine &TM = MF.getTarget();
1450 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1451 unsigned StackAlignment = TFI.getStackAlignment();
1452 uint64_t AlignMask = StackAlignment - 1;
1453 int64_t Offset = StackSize;
1454 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1455 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1456 // Number smaller than 12 so just add the difference.
1457 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1458 } else {
1459 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1460 Offset = ((~AlignMask) & Offset) + StackAlignment +
1461 (StackAlignment-SlotSize);
1462 }
1463 StackSize = Offset;
1464 }
1465 return StackSize;
1466}
1467
1468/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge453ff42007-11-02 01:26:22 +00001469/// following the call is a return. A function is eligible if caller/callee
1470/// calling conventions match, currently only fastcc supports tail calls, and
1471/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001472bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1473 SDOperand Ret,
1474 SelectionDAG& DAG) const {
Evan Chenge453ff42007-11-02 01:26:22 +00001475 if (!PerformTailCallOpt)
1476 return false;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001477
1478 // Check whether CALL node immediatly preceeds the RET node and whether the
1479 // return uses the result of the node or is a void return.
Evan Chenge453ff42007-11-02 01:26:22 +00001480 unsigned NumOps = Ret.getNumOperands();
1481 if ((NumOps == 1 &&
1482 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1483 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng2b93a202007-11-02 17:45:40 +00001484 (NumOps > 1 &&
Evan Chenge453ff42007-11-02 01:26:22 +00001485 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1486 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001487 MachineFunction &MF = DAG.getMachineFunction();
1488 unsigned CallerCC = MF.getFunction()->getCallingConv();
1489 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1490 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1491 SDOperand Callee = Call.getOperand(4);
1492 // On elf/pic %ebx needs to be livein.
Evan Chenge453ff42007-11-02 01:26:22 +00001493 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1494 !Subtarget->isPICStyleGOT())
1495 return true;
1496
1497 // Can only do local tail calls with PIC.
1498 GlobalValue * GV = 0;
1499 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1500 if(G != 0 &&
1501 (GV = G->getGlobal()) &&
1502 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1503 return true;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001504 }
1505 }
Evan Chenge453ff42007-11-02 01:26:22 +00001506
1507 return false;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001508}
1509
1510SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1511 SelectionDAG &DAG,
1512 unsigned CC) {
1513 SDOperand Chain = Op.getOperand(0);
1514 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1515 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1516 SDOperand Callee = Op.getOperand(4);
1517 bool is64Bit = Subtarget->is64Bit();
1518
1519 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1520
1521 // Analyze operands of the call, assigning locations to each operand.
1522 SmallVector<CCValAssign, 16> ArgLocs;
1523 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1524 if (is64Bit)
1525 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1526 else
1527 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1528
1529
1530 // Lower arguments at fp - stackoffset + fpdiff.
1531 MachineFunction &MF = DAG.getMachineFunction();
1532
1533 unsigned NumBytesToBePushed =
1534 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1535
1536 unsigned NumBytesCallerPushed =
1537 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1538 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1539
1540 // Set the delta of movement of the returnaddr stackslot.
1541 // But only set if delta is greater than previous delta.
1542 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1543 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1544
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +00001545 Chain = DAG.
1546 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1547
1548 // Adjust the Return address stack slot.
1549 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001550 if (FPDiff) {
1551 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +00001552 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1553 // Load the "old" Return address.
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001554 RetAddrFrIdx =
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +00001555 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1556 // Calculate the new stack slot for the return address.
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001557 int SlotSize = is64Bit ? 8 : 4;
1558 int NewReturnAddrFI =
1559 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +00001560 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1561 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001562 }
1563
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001564 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1565 SmallVector<SDOperand, 8> MemOpChains;
1566 SmallVector<SDOperand, 8> MemOpChains2;
1567 SDOperand FramePtr, StackPtr;
1568 SDOperand PtrOff;
1569 SDOperand FIN;
1570 int FI = 0;
1571
1572 // Walk the register/memloc assignments, inserting copies/loads. Lower
1573 // arguments first to the stack slot where they would normally - in case of a
1574 // normal function call - be.
1575 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1576 CCValAssign &VA = ArgLocs[i];
1577 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1578
1579 // Promote the value if needed.
1580 switch (VA.getLocInfo()) {
1581 default: assert(0 && "Unknown loc info!");
1582 case CCValAssign::Full: break;
1583 case CCValAssign::SExt:
1584 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1585 break;
1586 case CCValAssign::ZExt:
1587 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1588 break;
1589 case CCValAssign::AExt:
1590 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1591 break;
1592 }
1593
1594 if (VA.isRegLoc()) {
1595 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1596 } else {
1597 assert(VA.isMemLoc());
1598 if (StackPtr.Val == 0)
1599 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1600
1601 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1602 Arg));
1603 }
1604 }
1605
1606 if (!MemOpChains.empty())
1607 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1608 &MemOpChains[0], MemOpChains.size());
1609
1610 // Build a sequence of copy-to-reg nodes chained together with token chain
1611 // and flag operands which copy the outgoing args into registers.
1612 SDOperand InFlag;
1613 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1614 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1615 InFlag);
1616 InFlag = Chain.getValue(1);
1617 }
1618 InFlag = SDOperand();
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +00001619
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001620 // Copy from stack slots to stack slot of a tail called function. This needs
1621 // to be done because if we would lower the arguments directly to their real
1622 // stack slot we might end up overwriting each other.
1623 // TODO: To make this more efficient (sometimes saving a store/load) we could
1624 // analyse the arguments and emit this store/load/store sequence only for
1625 // arguments which would be overwritten otherwise.
1626 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1627 CCValAssign &VA = ArgLocs[i];
1628 if (!VA.isRegLoc()) {
1629 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1630 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1631
1632 // Get source stack slot.
1633 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1634 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1635 // Create frame index.
1636 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1637 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1638 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1639 FIN = DAG.getFrameIndex(FI, MVT::i32);
1640 if (Flags & ISD::ParamFlags::ByVal) {
1641 // Copy relative to framepointer.
1642 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1643 ISD::ParamFlags::ByValAlignOffs);
1644
1645 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1646 ISD::ParamFlags::ByValSizeOffs;
1647
1648 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1649 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighoferd2c16ff2007-11-10 10:48:01 +00001650 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1651
1652 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1653 AlignNode,AlwaysInline));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001654 } else {
1655 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1656 // Store relative to framepointer.
1657 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1658 }
1659 }
1660 }
1661
1662 if (!MemOpChains2.empty())
1663 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1664 &MemOpChains2[0], MemOpChains.size());
1665
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +00001666 // Store the return address to the appropriate stack slot.
1667 if (FPDiff)
1668 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1669
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001670 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1671 // GOT pointer.
1672 // Does not work with tail call since ebx is not restored correctly by
1673 // tailcaller. TODO: at least for x86 - verify for x86-64
1674
1675 // If the callee is a GlobalAddress node (quite common, every direct call is)
1676 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1677 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1678 // We should use extra load for direct calls to dllimported functions in
1679 // non-JIT mode.
1680 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1681 getTargetMachine(), true))
1682 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1683 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1684 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1685 else {
1686 assert(Callee.getOpcode() == ISD::LOAD &&
1687 "Function destination must be loaded into virtual register");
1688 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1689
1690 Chain = DAG.getCopyToReg(Chain,
1691 DAG.getRegister(Opc, getPointerTy()) ,
1692 Callee,InFlag);
1693 Callee = DAG.getRegister(Opc, getPointerTy());
1694 // Add register as live out.
1695 DAG.getMachineFunction().addLiveOut(Opc);
1696 }
1697
1698 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1699 SmallVector<SDOperand, 8> Ops;
1700
1701 Ops.push_back(Chain);
1702 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1703 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1704 if (InFlag.Val)
1705 Ops.push_back(InFlag);
1706 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1707 InFlag = Chain.getValue(1);
1708
1709 // Returns a chain & a flag for retval copy to use.
1710 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1711 Ops.clear();
1712 Ops.push_back(Chain);
1713 Ops.push_back(Callee);
1714 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1715 // Add argument registers to the end of the list so that they are known live
1716 // into the call.
1717 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1718 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1719 RegsToPass[i].second.getValueType()));
1720 if (InFlag.Val)
1721 Ops.push_back(InFlag);
1722 assert(InFlag.Val &&
1723 "Flag must be set. Depend on flag being set in LowerRET");
1724 Chain = DAG.getNode(X86ISD::TAILCALL,
1725 Op.Val->getVTList(), &Ops[0], Ops.size());
1726
1727 return SDOperand(Chain.Val, Op.ResNo);
1728}
Chris Lattner3066bec2007-02-28 06:10:12 +00001729
1730//===----------------------------------------------------------------------===//
1731// X86-64 C Calling Convention implementation
1732//===----------------------------------------------------------------------===//
1733
1734SDOperand
1735X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3066bec2007-02-28 06:10:12 +00001736 MachineFunction &MF = DAG.getMachineFunction();
1737 MachineFrameInfo *MFI = MF.getFrameInfo();
1738 SDOperand Root = Op.getOperand(0);
1739 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001740 unsigned CC= MF.getFunction()->getCallingConv();
Chris Lattner3066bec2007-02-28 06:10:12 +00001741
1742 static const unsigned GPR64ArgRegs[] = {
1743 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1744 };
1745 static const unsigned XMMArgRegs[] = {
1746 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1747 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1748 };
1749
Chris Lattner227b6c52007-02-28 07:00:42 +00001750
1751 // Assign locations to all of the incoming arguments.
Chris Lattner3066bec2007-02-28 06:10:12 +00001752 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001753 CCState CCInfo(CC, isVarArg,
Chris Lattner944200b2007-06-19 00:13:10 +00001754 getTargetMachine(), ArgLocs);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001755 if (CC == CallingConv::Fast && PerformTailCallOpt)
1756 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1757 else
1758 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001759
1760 SmallVector<SDOperand, 8> ArgValues;
1761 unsigned LastVal = ~0U;
1762 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1763 CCValAssign &VA = ArgLocs[i];
1764 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1765 // places.
1766 assert(VA.getValNo() != LastVal &&
1767 "Don't support value assigned to multiple locs yet");
1768 LastVal = VA.getValNo();
1769
1770 if (VA.isRegLoc()) {
1771 MVT::ValueType RegVT = VA.getLocVT();
1772 TargetRegisterClass *RC;
1773 if (RegVT == MVT::i32)
1774 RC = X86::GR32RegisterClass;
1775 else if (RegVT == MVT::i64)
1776 RC = X86::GR64RegisterClass;
1777 else if (RegVT == MVT::f32)
1778 RC = X86::FR32RegisterClass;
1779 else if (RegVT == MVT::f64)
1780 RC = X86::FR64RegisterClass;
1781 else {
1782 assert(MVT::isVector(RegVT));
Chris Lattner75372ad2007-06-09 05:08:10 +00001783 if (MVT::getSizeInBits(RegVT) == 64) {
1784 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1785 RegVT = MVT::i64;
1786 } else
Chris Lattnera4a49e32007-06-09 05:01:50 +00001787 RC = X86::VR128RegisterClass;
Chris Lattner3066bec2007-02-28 06:10:12 +00001788 }
Chris Lattner9c7e5e32007-03-02 05:12:29 +00001789
1790 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1791 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner3066bec2007-02-28 06:10:12 +00001792
1793 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1794 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1795 // right size.
1796 if (VA.getLocInfo() == CCValAssign::SExt)
1797 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1798 DAG.getValueType(VA.getValVT()));
1799 else if (VA.getLocInfo() == CCValAssign::ZExt)
1800 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1801 DAG.getValueType(VA.getValVT()));
1802
1803 if (VA.getLocInfo() != CCValAssign::Full)
1804 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1805
Chris Lattner75372ad2007-06-09 05:08:10 +00001806 // Handle MMX values passed in GPRs.
1807 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1808 MVT::getSizeInBits(RegVT) == 64)
1809 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1810
Chris Lattner3066bec2007-02-28 06:10:12 +00001811 ArgValues.push_back(ArgValue);
1812 } else {
1813 assert(VA.isMemLoc());
Rafael Espindola272f7302007-09-14 15:48:13 +00001814 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattner3066bec2007-02-28 06:10:12 +00001815 }
1816 }
1817
1818 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001819 if (CC==CallingConv::Fast)
1820 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Chris Lattner3066bec2007-02-28 06:10:12 +00001821
1822 // If the function takes variable number of arguments, make a frame index for
1823 // the start of the first vararg value... for expansion of llvm.va_start.
1824 if (isVarArg) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001825 assert(CC!=CallingConv::Fast
1826 && "Var arg not supported with calling convention fastcc");
Chris Lattner3066bec2007-02-28 06:10:12 +00001827 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1828 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1829
1830 // For X86-64, if there are vararg parameters that are passed via
1831 // registers, then we must store them to their spots on the stack so they
1832 // may be loaded by deferencing the result of va_next.
1833 VarArgsGPOffset = NumIntRegs * 8;
1834 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1835 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1836 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1837
1838 // Store the integer parameter registers.
1839 SmallVector<SDOperand, 8> MemOps;
1840 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1841 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1842 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1843 for (; NumIntRegs != 6; ++NumIntRegs) {
1844 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1845 X86::GR64RegisterClass);
1846 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1847 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1848 MemOps.push_back(Store);
1849 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1850 DAG.getConstant(8, getPointerTy()));
1851 }
1852
1853 // Now store the XMM (fp + vector) parameter registers.
1854 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1855 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1856 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1857 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1858 X86::VR128RegisterClass);
1859 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1860 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1861 MemOps.push_back(Store);
1862 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1863 DAG.getConstant(16, getPointerTy()));
1864 }
1865 if (!MemOps.empty())
1866 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1867 &MemOps[0], MemOps.size());
1868 }
1869
1870 ArgValues.push_back(Root);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001871 // Tail call convention (fastcc) needs callee pop.
Evan Cheng4099f4f2007-10-14 10:09:39 +00001872 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001873 BytesToPopOnReturn = StackSize; // Callee pops everything.
1874 BytesCallerReserves = 0;
1875 } else {
1876 BytesToPopOnReturn = 0; // Callee pops nothing.
1877 BytesCallerReserves = StackSize;
1878 }
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00001879 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1880 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1881
Chris Lattner3066bec2007-02-28 06:10:12 +00001882 // Return the new list of results.
1883 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1884 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1885}
1886
1887SDOperand
1888X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1889 unsigned CC) {
1890 SDOperand Chain = Op.getOperand(0);
1891 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner3066bec2007-02-28 06:10:12 +00001892 SDOperand Callee = Op.getOperand(4);
Chris Lattner227b6c52007-02-28 07:00:42 +00001893
1894 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner3066bec2007-02-28 06:10:12 +00001895 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001896 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng4099f4f2007-10-14 10:09:39 +00001897 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001898 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1899 else
1900 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001901
1902 // Get a count of how many bytes are to be pushed on the stack.
1903 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001904 if (CC == CallingConv::Fast)
1905 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1906
Chris Lattner3066bec2007-02-28 06:10:12 +00001907 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1908
1909 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1910 SmallVector<SDOperand, 8> MemOpChains;
1911
1912 SDOperand StackPtr;
1913
1914 // Walk the register/memloc assignments, inserting copies/loads.
1915 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1916 CCValAssign &VA = ArgLocs[i];
1917 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1918
1919 // Promote the value if needed.
1920 switch (VA.getLocInfo()) {
1921 default: assert(0 && "Unknown loc info!");
1922 case CCValAssign::Full: break;
1923 case CCValAssign::SExt:
1924 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1925 break;
1926 case CCValAssign::ZExt:
1927 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1928 break;
1929 case CCValAssign::AExt:
1930 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1931 break;
1932 }
1933
1934 if (VA.isRegLoc()) {
1935 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1936 } else {
1937 assert(VA.isMemLoc());
1938 if (StackPtr.Val == 0)
1939 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola9c3d20d2007-08-20 15:18:24 +00001940
Rafael Espindolae636fc02007-08-31 15:06:30 +00001941 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1942 Arg));
Chris Lattner3066bec2007-02-28 06:10:12 +00001943 }
1944 }
1945
1946 if (!MemOpChains.empty())
1947 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1948 &MemOpChains[0], MemOpChains.size());
1949
1950 // Build a sequence of copy-to-reg nodes chained together with token chain
1951 // and flag operands which copy the outgoing args into registers.
1952 SDOperand InFlag;
1953 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1954 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1955 InFlag);
1956 InFlag = Chain.getValue(1);
1957 }
1958
1959 if (isVarArg) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001960 assert ( CallingConv::Fast != CC &&
1961 "Var args not supported with calling convention fastcc");
1962
Chris Lattner3066bec2007-02-28 06:10:12 +00001963 // From AMD64 ABI document:
1964 // For calls that may call functions that use varargs or stdargs
1965 // (prototype-less calls or calls to functions containing ellipsis (...) in
1966 // the declaration) %al is used as hidden argument to specify the number
1967 // of SSE registers used. The contents of %al do not need to match exactly
1968 // the number of registers, but must be an ubound on the number of SSE
1969 // registers used and is in the range 0 - 8 inclusive.
1970
1971 // Count the number of XMM registers allocated.
1972 static const unsigned XMMArgRegs[] = {
1973 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1974 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1975 };
1976 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1977
1978 Chain = DAG.getCopyToReg(Chain, X86::AL,
1979 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1980 InFlag = Chain.getValue(1);
1981 }
1982
1983 // If the callee is a GlobalAddress node (quite common, every direct call is)
1984 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1985 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1986 // We should use extra load for direct calls to dllimported functions in
1987 // non-JIT mode.
Evan Chenga1779b92007-03-14 22:11:11 +00001988 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001989 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1990 getTargetMachine(), true))
Chris Lattner3066bec2007-02-28 06:10:12 +00001991 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1992 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chenga1779b92007-03-14 22:11:11 +00001993 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1994 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattner3066bec2007-02-28 06:10:12 +00001995
1996 // Returns a chain & a flag for retval copy to use.
1997 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1998 SmallVector<SDOperand, 8> Ops;
1999 Ops.push_back(Chain);
2000 Ops.push_back(Callee);
2001
2002 // Add argument registers to the end of the list so that they are known live
2003 // into the call.
2004 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2005 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2006 RegsToPass[i].second.getValueType()));
2007
2008 if (InFlag.Val)
2009 Ops.push_back(InFlag);
2010
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00002011 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattner3066bec2007-02-28 06:10:12 +00002012 NodeTys, &Ops[0], Ops.size());
2013 InFlag = Chain.getValue(1);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00002014 int NumBytesForCalleeToPush = 0;
Evan Cheng4099f4f2007-10-14 10:09:39 +00002015 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00002016 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00002017 } else {
2018 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2019 }
Chris Lattner3066bec2007-02-28 06:10:12 +00002020 // Returns a flag for retval copy to use.
2021 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2022 Ops.clear();
2023 Ops.push_back(Chain);
2024 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00002025 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Chris Lattner3066bec2007-02-28 06:10:12 +00002026 Ops.push_back(InFlag);
2027 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2028 InFlag = Chain.getValue(1);
2029
2030 // Handle result values, copying them out of physregs into vregs that we
2031 // return.
2032 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2033}
2034
2035
2036//===----------------------------------------------------------------------===//
2037// Other Lowering Hooks
2038//===----------------------------------------------------------------------===//
2039
2040
Chris Lattner76ac0682005-11-15 00:40:23 +00002041SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00002042 MachineFunction &MF = DAG.getMachineFunction();
2043 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2044 int ReturnAddrIndex = FuncInfo->getRAIndex();
2045
Chris Lattner76ac0682005-11-15 00:40:23 +00002046 if (ReturnAddrIndex == 0) {
2047 // Set up a frame object for the return address.
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002048 if (Subtarget->is64Bit())
2049 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2050 else
2051 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00002052
2053 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattner76ac0682005-11-15 00:40:23 +00002054 }
2055
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002056 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattner76ac0682005-11-15 00:40:23 +00002057}
2058
2059
2060
Evan Cheng45df7f82006-01-30 23:41:35 +00002061/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2062/// specific condition code. It returns a false if it cannot do a direct
Chris Lattner7a627672006-09-13 03:22:10 +00002063/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2064/// needed.
Evan Cheng78038292006-04-05 23:38:46 +00002065static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattner7a627672006-09-13 03:22:10 +00002066 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2067 SelectionDAG &DAG) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002068 X86CC = X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00002069 if (!isFP) {
Chris Lattner971e3392006-09-13 17:04:54 +00002070 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2071 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2072 // X > -1 -> X == 0, jump !sign.
2073 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002074 X86CC = X86::COND_NS;
Chris Lattner971e3392006-09-13 17:04:54 +00002075 return true;
2076 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2077 // X < 0 -> X == 0, jump on sign.
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002078 X86CC = X86::COND_S;
Chris Lattner971e3392006-09-13 17:04:54 +00002079 return true;
Dan Gohman863bdc32007-09-17 14:49:27 +00002080 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2081 // X < 1 -> X <= 0
2082 RHS = DAG.getConstant(0, RHS.getValueType());
2083 X86CC = X86::COND_LE;
2084 return true;
Chris Lattner971e3392006-09-13 17:04:54 +00002085 }
Chris Lattner7a627672006-09-13 03:22:10 +00002086 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002087
Evan Cheng172fce72006-01-06 00:43:03 +00002088 switch (SetCCOpcode) {
2089 default: break;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002090 case ISD::SETEQ: X86CC = X86::COND_E; break;
2091 case ISD::SETGT: X86CC = X86::COND_G; break;
2092 case ISD::SETGE: X86CC = X86::COND_GE; break;
2093 case ISD::SETLT: X86CC = X86::COND_L; break;
2094 case ISD::SETLE: X86CC = X86::COND_LE; break;
2095 case ISD::SETNE: X86CC = X86::COND_NE; break;
2096 case ISD::SETULT: X86CC = X86::COND_B; break;
2097 case ISD::SETUGT: X86CC = X86::COND_A; break;
2098 case ISD::SETULE: X86CC = X86::COND_BE; break;
2099 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00002100 }
2101 } else {
2102 // On a floating point condition, the flags are set as follows:
2103 // ZF PF CF op
2104 // 0 | 0 | 0 | X > Y
2105 // 0 | 0 | 1 | X < Y
2106 // 1 | 0 | 0 | X == Y
2107 // 1 | 1 | 1 | unordered
Chris Lattner7a627672006-09-13 03:22:10 +00002108 bool Flip = false;
Evan Cheng172fce72006-01-06 00:43:03 +00002109 switch (SetCCOpcode) {
2110 default: break;
2111 case ISD::SETUEQ:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002112 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00002113 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00002114 case ISD::SETOGT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002115 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00002116 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00002117 case ISD::SETOGE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002118 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00002119 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00002120 case ISD::SETULT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002121 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00002122 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00002123 case ISD::SETULE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002124 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00002125 case ISD::SETONE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002126 case ISD::SETNE: X86CC = X86::COND_NE; break;
2127 case ISD::SETUO: X86CC = X86::COND_P; break;
2128 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Cheng172fce72006-01-06 00:43:03 +00002129 }
Chris Lattner7a627672006-09-13 03:22:10 +00002130 if (Flip)
2131 std::swap(LHS, RHS);
Evan Cheng172fce72006-01-06 00:43:03 +00002132 }
Evan Cheng45df7f82006-01-30 23:41:35 +00002133
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002134 return X86CC != X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00002135}
2136
Evan Cheng339edad2006-01-11 00:33:36 +00002137/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2138/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00002139/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00002140static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00002141 switch (X86CC) {
2142 default:
2143 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002144 case X86::COND_B:
2145 case X86::COND_BE:
2146 case X86::COND_E:
2147 case X86::COND_P:
2148 case X86::COND_A:
2149 case X86::COND_AE:
2150 case X86::COND_NE:
2151 case X86::COND_NP:
Evan Cheng73a1ad92006-01-10 20:26:56 +00002152 return true;
2153 }
2154}
2155
Evan Chengc995b452006-04-06 23:23:56 +00002156/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00002157/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00002158static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2159 if (Op.getOpcode() == ISD::UNDEF)
2160 return true;
2161
2162 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00002163 return (Val >= Low && Val < Hi);
2164}
2165
2166/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2167/// true if Op is undef or if its value equal to the specified value.
2168static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2169 if (Op.getOpcode() == ISD::UNDEF)
2170 return true;
2171 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00002172}
2173
Evan Cheng68ad48b2006-03-22 18:59:22 +00002174/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2175/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2176bool X86::isPSHUFDMask(SDNode *N) {
2177 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2178
Dan Gohman8932bff2007-08-02 21:17:01 +00002179 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng68ad48b2006-03-22 18:59:22 +00002180 return false;
2181
2182 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00002183 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002184 SDOperand Arg = N->getOperand(i);
2185 if (Arg.getOpcode() == ISD::UNDEF) continue;
2186 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman8932bff2007-08-02 21:17:01 +00002187 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Chengb7fedff2006-03-29 23:07:14 +00002188 return false;
2189 }
2190
2191 return true;
2192}
2193
2194/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00002195/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00002196bool X86::isPSHUFHWMask(SDNode *N) {
2197 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2198
2199 if (N->getNumOperands() != 8)
2200 return false;
2201
2202 // Lower quadword copied in order.
2203 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002204 SDOperand Arg = N->getOperand(i);
2205 if (Arg.getOpcode() == ISD::UNDEF) continue;
2206 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2207 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00002208 return false;
2209 }
2210
2211 // Upper quadword shuffled.
2212 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002213 SDOperand Arg = N->getOperand(i);
2214 if (Arg.getOpcode() == ISD::UNDEF) continue;
2215 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2216 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002217 if (Val < 4 || Val > 7)
2218 return false;
2219 }
2220
2221 return true;
2222}
2223
2224/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00002225/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00002226bool X86::isPSHUFLWMask(SDNode *N) {
2227 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2228
2229 if (N->getNumOperands() != 8)
2230 return false;
2231
2232 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00002233 for (unsigned i = 4; i != 8; ++i)
2234 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00002235 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00002236
2237 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00002238 for (unsigned i = 0; i != 4; ++i)
2239 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00002240 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00002241
2242 return true;
2243}
2244
Evan Chengd27fb3e2006-03-24 01:18:28 +00002245/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2246/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner35a08552007-02-25 07:10:00 +00002247static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002248 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00002249
Evan Cheng60f0b892006-04-20 08:58:49 +00002250 unsigned Half = NumElems / 2;
2251 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00002252 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng60f0b892006-04-20 08:58:49 +00002253 return false;
2254 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00002255 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00002256 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00002257
2258 return true;
2259}
2260
Evan Cheng60f0b892006-04-20 08:58:49 +00002261bool X86::isSHUFPMask(SDNode *N) {
2262 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002263 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00002264}
2265
Evan Chengafa1cb62007-05-17 18:45:50 +00002266/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng60f0b892006-04-20 08:58:49 +00002267/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2268/// half elements to come from vector 1 (which would equal the dest.) and
2269/// the upper half to come from vector 2.
Chris Lattner35a08552007-02-25 07:10:00 +00002270static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2271 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002272
Chris Lattner35a08552007-02-25 07:10:00 +00002273 unsigned Half = NumOps / 2;
Evan Cheng60f0b892006-04-20 08:58:49 +00002274 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00002275 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00002276 return false;
Chris Lattner35a08552007-02-25 07:10:00 +00002277 for (unsigned i = Half; i < NumOps; ++i)
2278 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng60f0b892006-04-20 08:58:49 +00002279 return false;
2280 return true;
2281}
2282
2283static bool isCommutedSHUFP(SDNode *N) {
2284 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002285 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00002286}
2287
Evan Cheng2595a682006-03-24 02:58:06 +00002288/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2289/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2290bool X86::isMOVHLPSMask(SDNode *N) {
2291 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2292
Evan Cheng1a194a52006-03-28 06:50:32 +00002293 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00002294 return false;
2295
Evan Cheng1a194a52006-03-28 06:50:32 +00002296 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00002297 return isUndefOrEqual(N->getOperand(0), 6) &&
2298 isUndefOrEqual(N->getOperand(1), 7) &&
2299 isUndefOrEqual(N->getOperand(2), 2) &&
2300 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00002301}
2302
Evan Cheng922e1912006-11-07 22:14:24 +00002303/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2304/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2305/// <2, 3, 2, 3>
2306bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2307 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2308
2309 if (N->getNumOperands() != 4)
2310 return false;
2311
2312 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2313 return isUndefOrEqual(N->getOperand(0), 2) &&
2314 isUndefOrEqual(N->getOperand(1), 3) &&
2315 isUndefOrEqual(N->getOperand(2), 2) &&
2316 isUndefOrEqual(N->getOperand(3), 3);
2317}
2318
Evan Chengc995b452006-04-06 23:23:56 +00002319/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2320/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2321bool X86::isMOVLPMask(SDNode *N) {
2322 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2323
2324 unsigned NumElems = N->getNumOperands();
2325 if (NumElems != 2 && NumElems != 4)
2326 return false;
2327
Evan Chengac847262006-04-07 21:53:05 +00002328 for (unsigned i = 0; i < NumElems/2; ++i)
2329 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2330 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002331
Evan Chengac847262006-04-07 21:53:05 +00002332 for (unsigned i = NumElems/2; i < NumElems; ++i)
2333 if (!isUndefOrEqual(N->getOperand(i), i))
2334 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002335
2336 return true;
2337}
2338
2339/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00002340/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2341/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00002342bool X86::isMOVHPMask(SDNode *N) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344
2345 unsigned NumElems = N->getNumOperands();
2346 if (NumElems != 2 && NumElems != 4)
2347 return false;
2348
Evan Chengac847262006-04-07 21:53:05 +00002349 for (unsigned i = 0; i < NumElems/2; ++i)
2350 if (!isUndefOrEqual(N->getOperand(i), i))
2351 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002352
2353 for (unsigned i = 0; i < NumElems/2; ++i) {
2354 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00002355 if (!isUndefOrEqual(Arg, i + NumElems))
2356 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002357 }
2358
2359 return true;
2360}
2361
Evan Cheng5df75882006-03-28 00:39:58 +00002362/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2363/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner35a08552007-02-25 07:10:00 +00002364bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2365 bool V2IsSplat = false) {
2366 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng5df75882006-03-28 00:39:58 +00002367 return false;
2368
Chris Lattner35a08552007-02-25 07:10:00 +00002369 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2370 SDOperand BitI = Elts[i];
2371 SDOperand BitI1 = Elts[i+1];
Evan Chengac847262006-04-07 21:53:05 +00002372 if (!isUndefOrEqual(BitI, j))
2373 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002374 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00002375 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002376 return false;
2377 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00002378 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002379 return false;
2380 }
Evan Cheng5df75882006-03-28 00:39:58 +00002381 }
2382
2383 return true;
2384}
2385
Evan Cheng60f0b892006-04-20 08:58:49 +00002386bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2387 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002388 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00002389}
2390
Evan Cheng2bc32802006-03-28 02:43:26 +00002391/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2392/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner35a08552007-02-25 07:10:00 +00002393bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2394 bool V2IsSplat = false) {
2395 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng2bc32802006-03-28 02:43:26 +00002396 return false;
2397
Chris Lattner35a08552007-02-25 07:10:00 +00002398 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2399 SDOperand BitI = Elts[i];
2400 SDOperand BitI1 = Elts[i+1];
2401 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengac847262006-04-07 21:53:05 +00002402 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002403 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00002404 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002405 return false;
2406 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00002407 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002408 return false;
2409 }
Evan Cheng2bc32802006-03-28 02:43:26 +00002410 }
2411
2412 return true;
2413}
2414
Evan Cheng60f0b892006-04-20 08:58:49 +00002415bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2416 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002417 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00002418}
2419
Evan Chengf3b52c82006-04-05 07:20:06 +00002420/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2421/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2422/// <0, 0, 1, 1>
2423bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2424 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2425
2426 unsigned NumElems = N->getNumOperands();
Bill Wendling591eab82007-04-24 21:16:55 +00002427 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengf3b52c82006-04-05 07:20:06 +00002428 return false;
2429
2430 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2431 SDOperand BitI = N->getOperand(i);
2432 SDOperand BitI1 = N->getOperand(i+1);
2433
Evan Chengac847262006-04-07 21:53:05 +00002434 if (!isUndefOrEqual(BitI, j))
2435 return false;
2436 if (!isUndefOrEqual(BitI1, j))
2437 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00002438 }
2439
2440 return true;
2441}
2442
Bill Wendling591eab82007-04-24 21:16:55 +00002443/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2444/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2445/// <2, 2, 3, 3>
2446bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2447 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2448
2449 unsigned NumElems = N->getNumOperands();
2450 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2451 return false;
2452
2453 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2454 SDOperand BitI = N->getOperand(i);
2455 SDOperand BitI1 = N->getOperand(i + 1);
2456
2457 if (!isUndefOrEqual(BitI, j))
2458 return false;
2459 if (!isUndefOrEqual(BitI1, j))
2460 return false;
2461 }
2462
2463 return true;
2464}
2465
Evan Chenge8b51802006-04-21 01:05:10 +00002466/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2467/// specifies a shuffle of elements that is suitable for input to MOVSS,
2468/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner35a08552007-02-25 07:10:00 +00002469static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Chengc829e5c2007-12-06 22:14:22 +00002470 if (NumElts != 2 && NumElts != 4)
Evan Cheng12ba3e22006-04-11 00:19:04 +00002471 return false;
2472
Chris Lattner35a08552007-02-25 07:10:00 +00002473 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Cheng12ba3e22006-04-11 00:19:04 +00002474 return false;
2475
Chris Lattner35a08552007-02-25 07:10:00 +00002476 for (unsigned i = 1; i < NumElts; ++i) {
2477 if (!isUndefOrEqual(Elts[i], i))
Evan Cheng12ba3e22006-04-11 00:19:04 +00002478 return false;
2479 }
2480
2481 return true;
2482}
Evan Chengf3b52c82006-04-05 07:20:06 +00002483
Evan Chenge8b51802006-04-21 01:05:10 +00002484bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002485 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002486 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00002487}
2488
Evan Chenge8b51802006-04-21 01:05:10 +00002489/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2490/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00002491/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner35a08552007-02-25 07:10:00 +00002492static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2493 bool V2IsSplat = false,
Evan Cheng89c5d042006-09-08 01:50:06 +00002494 bool V2IsUndef = false) {
Chris Lattner35a08552007-02-25 07:10:00 +00002495 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00002496 return false;
2497
2498 if (!isUndefOrEqual(Ops[0], 0))
2499 return false;
2500
Chris Lattner35a08552007-02-25 07:10:00 +00002501 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002502 SDOperand Arg = Ops[i];
Chris Lattner35a08552007-02-25 07:10:00 +00002503 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2504 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2505 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng89c5d042006-09-08 01:50:06 +00002506 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002507 }
2508
2509 return true;
2510}
2511
Evan Cheng89c5d042006-09-08 01:50:06 +00002512static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2513 bool V2IsUndef = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002514 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002515 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2516 V2IsSplat, V2IsUndef);
Evan Cheng60f0b892006-04-20 08:58:49 +00002517}
2518
Evan Cheng5d247f82006-04-14 21:59:03 +00002519/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2520/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2521bool X86::isMOVSHDUPMask(SDNode *N) {
2522 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2523
2524 if (N->getNumOperands() != 4)
2525 return false;
2526
2527 // Expect 1, 1, 3, 3
2528 for (unsigned i = 0; i < 2; ++i) {
2529 SDOperand Arg = N->getOperand(i);
2530 if (Arg.getOpcode() == ISD::UNDEF) continue;
2531 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2532 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2533 if (Val != 1) return false;
2534 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002535
2536 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002537 for (unsigned i = 2; i < 4; ++i) {
2538 SDOperand Arg = N->getOperand(i);
2539 if (Arg.getOpcode() == ISD::UNDEF) continue;
2540 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2541 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2542 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002543 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002544 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002545
Evan Cheng6222cf22006-04-15 05:37:34 +00002546 // Don't use movshdup if it can be done with a shufps.
2547 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002548}
2549
2550/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2551/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2552bool X86::isMOVSLDUPMask(SDNode *N) {
2553 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2554
2555 if (N->getNumOperands() != 4)
2556 return false;
2557
2558 // Expect 0, 0, 2, 2
2559 for (unsigned i = 0; i < 2; ++i) {
2560 SDOperand Arg = N->getOperand(i);
2561 if (Arg.getOpcode() == ISD::UNDEF) continue;
2562 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2563 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2564 if (Val != 0) return false;
2565 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002566
2567 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002568 for (unsigned i = 2; i < 4; ++i) {
2569 SDOperand Arg = N->getOperand(i);
2570 if (Arg.getOpcode() == ISD::UNDEF) continue;
2571 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2572 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2573 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002574 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002575 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002576
Evan Cheng6222cf22006-04-15 05:37:34 +00002577 // Don't use movshdup if it can be done with a shufps.
2578 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002579}
2580
Evan Chengcea02ff2007-06-19 00:02:56 +00002581/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2582/// specifies a identity operation on the LHS or RHS.
2583static bool isIdentityMask(SDNode *N, bool RHS = false) {
2584 unsigned NumElems = N->getNumOperands();
2585 for (unsigned i = 0; i < NumElems; ++i)
2586 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2587 return false;
2588 return true;
2589}
2590
Evan Chengd097e672006-03-22 02:53:00 +00002591/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2592/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00002593static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002594 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2595
Evan Chengd097e672006-03-22 02:53:00 +00002596 // This is a splat operation if each element of the permute is the same, and
2597 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002598 unsigned NumElems = N->getNumOperands();
2599 SDOperand ElementBase;
2600 unsigned i = 0;
2601 for (; i != NumElems; ++i) {
2602 SDOperand Elt = N->getOperand(i);
Reid Spencerde46e482006-11-02 20:25:50 +00002603 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002604 ElementBase = Elt;
2605 break;
2606 }
2607 }
2608
2609 if (!ElementBase.Val)
2610 return false;
2611
2612 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002613 SDOperand Arg = N->getOperand(i);
2614 if (Arg.getOpcode() == ISD::UNDEF) continue;
2615 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002616 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00002617 }
2618
2619 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002620 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00002621}
2622
Evan Cheng5022b342006-04-17 20:43:08 +00002623/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2624/// a splat of a single element and it's a 2 or 4 element mask.
2625bool X86::isSplatMask(SDNode *N) {
2626 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2627
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002628 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00002629 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2630 return false;
2631 return ::isSplatMask(N);
2632}
2633
Evan Chenge056dd52006-10-27 21:08:32 +00002634/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2635/// specifies a splat of zero element.
2636bool X86::isSplatLoMask(SDNode *N) {
2637 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2638
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002639 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chenge056dd52006-10-27 21:08:32 +00002640 if (!isUndefOrEqual(N->getOperand(i), 0))
2641 return false;
2642 return true;
2643}
2644
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002645/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2646/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2647/// instructions.
2648unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002649 unsigned NumOperands = N->getNumOperands();
2650 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2651 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00002652 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002653 unsigned Val = 0;
2654 SDOperand Arg = N->getOperand(NumOperands-i-1);
2655 if (Arg.getOpcode() != ISD::UNDEF)
2656 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00002657 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002658 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00002659 if (i != NumOperands - 1)
2660 Mask <<= Shift;
2661 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002662
2663 return Mask;
2664}
2665
Evan Chengb7fedff2006-03-29 23:07:14 +00002666/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2667/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2668/// instructions.
2669unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2670 unsigned Mask = 0;
2671 // 8 nodes, but we only care about the last 4.
2672 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002673 unsigned Val = 0;
2674 SDOperand Arg = N->getOperand(i);
2675 if (Arg.getOpcode() != ISD::UNDEF)
2676 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002677 Mask |= (Val - 4);
2678 if (i != 4)
2679 Mask <<= 2;
2680 }
2681
2682 return Mask;
2683}
2684
2685/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2686/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2687/// instructions.
2688unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2689 unsigned Mask = 0;
2690 // 8 nodes, but we only care about the first 4.
2691 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002692 unsigned Val = 0;
2693 SDOperand Arg = N->getOperand(i);
2694 if (Arg.getOpcode() != ISD::UNDEF)
2695 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002696 Mask |= Val;
2697 if (i != 0)
2698 Mask <<= 2;
2699 }
2700
2701 return Mask;
2702}
2703
Evan Cheng59a63552006-04-05 01:47:37 +00002704/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2705/// specifies a 8 element shuffle that can be broken into a pair of
2706/// PSHUFHW and PSHUFLW.
2707static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2708 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2709
2710 if (N->getNumOperands() != 8)
2711 return false;
2712
2713 // Lower quadword shuffled.
2714 for (unsigned i = 0; i != 4; ++i) {
2715 SDOperand Arg = N->getOperand(i);
2716 if (Arg.getOpcode() == ISD::UNDEF) continue;
2717 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2718 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng4fbf4592007-12-11 01:46:18 +00002719 if (Val >= 4)
Evan Cheng59a63552006-04-05 01:47:37 +00002720 return false;
2721 }
2722
2723 // Upper quadword shuffled.
2724 for (unsigned i = 4; i != 8; ++i) {
2725 SDOperand Arg = N->getOperand(i);
2726 if (Arg.getOpcode() == ISD::UNDEF) continue;
2727 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2728 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2729 if (Val < 4 || Val > 7)
2730 return false;
2731 }
2732
2733 return true;
2734}
2735
Chris Lattner5728bdd2007-11-25 00:24:49 +00002736/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Chengc995b452006-04-06 23:23:56 +00002737/// values in ther permute mask.
Evan Chengc415c5b2006-10-25 21:49:50 +00002738static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2739 SDOperand &V2, SDOperand &Mask,
2740 SelectionDAG &DAG) {
Evan Chengc995b452006-04-06 23:23:56 +00002741 MVT::ValueType VT = Op.getValueType();
2742 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002743 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Chengc995b452006-04-06 23:23:56 +00002744 unsigned NumElems = Mask.getNumOperands();
Chris Lattner35a08552007-02-25 07:10:00 +00002745 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc995b452006-04-06 23:23:56 +00002746
2747 for (unsigned i = 0; i != NumElems; ++i) {
2748 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002749 if (Arg.getOpcode() == ISD::UNDEF) {
2750 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2751 continue;
2752 }
Evan Chengc995b452006-04-06 23:23:56 +00002753 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2754 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2755 if (Val < NumElems)
2756 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2757 else
2758 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2759 }
2760
Evan Chengc415c5b2006-10-25 21:49:50 +00002761 std::swap(V1, V2);
Evan Chengbfd373a2007-12-07 08:07:39 +00002762 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Chengc415c5b2006-10-25 21:49:50 +00002763 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chengc995b452006-04-06 23:23:56 +00002764}
2765
Evan Chengb41d8382007-12-07 21:30:01 +00002766/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2767/// the two vector operands have swapped position.
Evan Chengbfd373a2007-12-07 08:07:39 +00002768static
2769SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2770 MVT::ValueType MaskVT = Mask.getValueType();
2771 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2772 unsigned NumElems = Mask.getNumOperands();
2773 SmallVector<SDOperand, 8> MaskVec;
2774 for (unsigned i = 0; i != NumElems; ++i) {
2775 SDOperand Arg = Mask.getOperand(i);
2776 if (Arg.getOpcode() == ISD::UNDEF) {
2777 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2778 continue;
2779 }
2780 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2781 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2782 if (Val < NumElems)
2783 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2784 else
2785 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2786 }
2787 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2788}
2789
2790
Evan Cheng7855e4d2006-04-19 20:35:22 +00002791/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2792/// match movhlps. The lower half elements should come from upper half of
2793/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002794/// half of V2 (and in order).
Evan Cheng7855e4d2006-04-19 20:35:22 +00002795static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2796 unsigned NumElems = Mask->getNumOperands();
2797 if (NumElems != 4)
2798 return false;
2799 for (unsigned i = 0, e = 2; i != e; ++i)
2800 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2801 return false;
2802 for (unsigned i = 2; i != 4; ++i)
2803 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2804 return false;
2805 return true;
2806}
2807
Evan Chengc995b452006-04-06 23:23:56 +00002808/// isScalarLoadToVector - Returns true if the node is a scalar load that
2809/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002810static inline bool isScalarLoadToVector(SDNode *N) {
2811 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2812 N = N->getOperand(0).Val;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002813 return ISD::isNON_EXTLoad(N);
Evan Chengc995b452006-04-06 23:23:56 +00002814 }
2815 return false;
2816}
2817
Evan Cheng7855e4d2006-04-19 20:35:22 +00002818/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2819/// match movlp{s|d}. The lower half elements should come from lower half of
2820/// V1 (and in order), and the upper half elements should come from the upper
2821/// half of V2 (and in order). And since V1 will become the source of the
2822/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Chenge646abb2006-10-09 21:39:25 +00002823static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002824 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng7855e4d2006-04-19 20:35:22 +00002825 return false;
Evan Chenge646abb2006-10-09 21:39:25 +00002826 // Is V2 is a vector load, don't do this transformation. We will try to use
2827 // load folding shufps op.
2828 if (ISD::isNON_EXTLoad(V2))
2829 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002830
Evan Cheng7855e4d2006-04-19 20:35:22 +00002831 unsigned NumElems = Mask->getNumOperands();
2832 if (NumElems != 2 && NumElems != 4)
2833 return false;
2834 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2835 if (!isUndefOrEqual(Mask->getOperand(i), i))
2836 return false;
2837 for (unsigned i = NumElems/2; i != NumElems; ++i)
2838 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2839 return false;
2840 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002841}
2842
Evan Cheng60f0b892006-04-20 08:58:49 +00002843/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2844/// all the same.
2845static bool isSplatVector(SDNode *N) {
2846 if (N->getOpcode() != ISD::BUILD_VECTOR)
2847 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002848
Evan Cheng60f0b892006-04-20 08:58:49 +00002849 SDOperand SplatValue = N->getOperand(0);
2850 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2851 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002852 return false;
2853 return true;
2854}
2855
Evan Cheng89c5d042006-09-08 01:50:06 +00002856/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2857/// to an undef.
2858static bool isUndefShuffle(SDNode *N) {
Evan Chengafa1cb62007-05-17 18:45:50 +00002859 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng89c5d042006-09-08 01:50:06 +00002860 return false;
2861
2862 SDOperand V1 = N->getOperand(0);
2863 SDOperand V2 = N->getOperand(1);
2864 SDOperand Mask = N->getOperand(2);
2865 unsigned NumElems = Mask.getNumOperands();
2866 for (unsigned i = 0; i != NumElems; ++i) {
2867 SDOperand Arg = Mask.getOperand(i);
2868 if (Arg.getOpcode() != ISD::UNDEF) {
2869 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2870 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2871 return false;
2872 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2873 return false;
2874 }
2875 }
2876 return true;
2877}
2878
Evan Chengafa1cb62007-05-17 18:45:50 +00002879/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2880/// constant +0.0.
2881static inline bool isZeroNode(SDOperand Elt) {
2882 return ((isa<ConstantSDNode>(Elt) &&
2883 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2884 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesen3cf889f2007-08-31 04:03:46 +00002885 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Chengafa1cb62007-05-17 18:45:50 +00002886}
2887
2888/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2889/// to an zero vector.
2890static bool isZeroShuffle(SDNode *N) {
2891 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2892 return false;
2893
2894 SDOperand V1 = N->getOperand(0);
2895 SDOperand V2 = N->getOperand(1);
2896 SDOperand Mask = N->getOperand(2);
2897 unsigned NumElems = Mask.getNumOperands();
2898 for (unsigned i = 0; i != NumElems; ++i) {
2899 SDOperand Arg = Mask.getOperand(i);
Chris Lattner5728bdd2007-11-25 00:24:49 +00002900 if (Arg.getOpcode() == ISD::UNDEF)
2901 continue;
2902
2903 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2904 if (Idx < NumElems) {
2905 unsigned Opc = V1.Val->getOpcode();
2906 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2907 continue;
2908 if (Opc != ISD::BUILD_VECTOR ||
2909 !isZeroNode(V1.Val->getOperand(Idx)))
2910 return false;
2911 } else if (Idx >= NumElems) {
2912 unsigned Opc = V2.Val->getOpcode();
2913 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2914 continue;
2915 if (Opc != ISD::BUILD_VECTOR ||
2916 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2917 return false;
Evan Chengafa1cb62007-05-17 18:45:50 +00002918 }
2919 }
2920 return true;
2921}
2922
2923/// getZeroVector - Returns a vector of specified type with all zero elements.
2924///
2925static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2926 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner5728bdd2007-11-25 00:24:49 +00002927
2928 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2929 // type. This ensures they get CSE'd.
2930 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2931 SDOperand Vec;
2932 if (MVT::getSizeInBits(VT) == 64) // MMX
2933 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2934 else // SSE
2935 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2936 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Chengafa1cb62007-05-17 18:45:50 +00002937}
2938
Chris Lattner5728bdd2007-11-25 00:24:49 +00002939/// getOnesVector - Returns a vector of specified type with all bits set.
2940///
2941static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2942 assert(MVT::isVector(VT) && "Expected a vector type");
2943
2944 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2945 // type. This ensures they get CSE'd.
2946 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2947 SDOperand Vec;
2948 if (MVT::getSizeInBits(VT) == 64) // MMX
2949 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2950 else // SSE
2951 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2952 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2953}
2954
2955
Evan Cheng60f0b892006-04-20 08:58:49 +00002956/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2957/// that point to V2 points to its first element.
2958static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2959 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2960
2961 bool Changed = false;
Chris Lattner35a08552007-02-25 07:10:00 +00002962 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002963 unsigned NumElems = Mask.getNumOperands();
2964 for (unsigned i = 0; i != NumElems; ++i) {
2965 SDOperand Arg = Mask.getOperand(i);
2966 if (Arg.getOpcode() != ISD::UNDEF) {
2967 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2968 if (Val > NumElems) {
2969 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2970 Changed = true;
2971 }
2972 }
2973 MaskVec.push_back(Arg);
2974 }
2975
2976 if (Changed)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002977 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2978 &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002979 return Mask;
2980}
2981
Evan Chenge8b51802006-04-21 01:05:10 +00002982/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2983/// operation of specified width.
2984static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002985 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002986 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng60f0b892006-04-20 08:58:49 +00002987
Chris Lattner35a08552007-02-25 07:10:00 +00002988 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002989 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2990 for (unsigned i = 1; i != NumElems; ++i)
2991 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002992 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002993}
2994
Evan Cheng5022b342006-04-17 20:43:08 +00002995/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2996/// of specified width.
2997static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2998 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002999 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003000 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5022b342006-04-17 20:43:08 +00003001 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3002 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3003 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3004 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003005 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng5022b342006-04-17 20:43:08 +00003006}
3007
Evan Cheng60f0b892006-04-20 08:58:49 +00003008/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3009/// of specified width.
3010static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3011 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00003012 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng60f0b892006-04-20 08:58:49 +00003013 unsigned Half = NumElems/2;
Chris Lattner35a08552007-02-25 07:10:00 +00003014 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00003015 for (unsigned i = 0; i != Half; ++i) {
3016 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3017 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3018 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003019 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00003020}
3021
Evan Cheng5022b342006-04-17 20:43:08 +00003022/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3023///
3024static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3025 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00003026 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00003027 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00003028 unsigned NumElems = Mask.getNumOperands();
3029 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00003030 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00003031 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00003032 NumElems >>= 1;
3033 }
3034 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3035
Chris Lattner5728bdd2007-11-25 00:24:49 +00003036 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00003037 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00003038 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00003039 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3040}
3041
Evan Cheng14215c32006-04-21 23:03:30 +00003042/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner5728bdd2007-11-25 00:24:49 +00003043/// vector of zero or undef vector. This produces a shuffle where the low
3044/// element of V2 is swizzled into the zero/undef vector, landing at element
3045/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Evan Cheng14215c32006-04-21 23:03:30 +00003046static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00003047 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00003048 bool isZero, SelectionDAG &DAG) {
3049 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00003050 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00003051 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5728bdd2007-11-25 00:24:49 +00003052 SmallVector<SDOperand, 16> MaskVec;
3053 for (unsigned i = 0; i != NumElems; ++i)
3054 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3055 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3056 else
3057 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003058 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3059 &MaskVec[0], MaskVec.size());
Evan Cheng14215c32006-04-21 23:03:30 +00003060 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00003061}
3062
Evan Chengb0461082006-04-24 18:01:45 +00003063/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3064///
3065static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3066 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003067 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00003068 if (NumNonZero > 8)
3069 return SDOperand();
3070
3071 SDOperand V(0, 0);
3072 bool First = true;
3073 for (unsigned i = 0; i < 16; ++i) {
3074 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3075 if (ThisIsNonZero && First) {
3076 if (NumZero)
3077 V = getZeroVector(MVT::v8i16, DAG);
3078 else
3079 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3080 First = false;
3081 }
3082
3083 if ((i & 1) != 0) {
3084 SDOperand ThisElt(0, 0), LastElt(0, 0);
3085 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3086 if (LastIsNonZero) {
3087 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3088 }
3089 if (ThisIsNonZero) {
3090 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3091 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3092 ThisElt, DAG.getConstant(8, MVT::i8));
3093 if (LastIsNonZero)
3094 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3095 } else
3096 ThisElt = LastElt;
3097
3098 if (ThisElt.Val)
3099 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003100 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00003101 }
3102 }
3103
3104 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3105}
3106
Bill Wendlingd551a182007-03-22 18:42:45 +00003107/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengb0461082006-04-24 18:01:45 +00003108///
3109static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3110 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003111 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00003112 if (NumNonZero > 4)
3113 return SDOperand();
3114
3115 SDOperand V(0, 0);
3116 bool First = true;
3117 for (unsigned i = 0; i < 8; ++i) {
3118 bool isNonZero = (NonZeros & (1 << i)) != 0;
3119 if (isNonZero) {
3120 if (First) {
3121 if (NumZero)
3122 V = getZeroVector(MVT::v8i16, DAG);
3123 else
3124 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3125 First = false;
3126 }
3127 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003128 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00003129 }
3130 }
3131
3132 return V;
3133}
3134
Evan Cheng4fbf4592007-12-11 01:46:18 +00003135/// is4WideVector - Returns true if the specific v8i16 or v16i8 vector is
3136/// actually just a 4 wide vector. e.g. <a, a, y, y, d, d, x, x>
Evan Chenga9467aa2006-04-25 20:13:52 +00003137SDOperand
3138X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner5728bdd2007-11-25 00:24:49 +00003139 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3140 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3141 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3142 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3143 // eliminated on x86-32 hosts.
3144 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3145 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003146
Chris Lattner5728bdd2007-11-25 00:24:49 +00003147 if (ISD::isBuildVectorAllOnes(Op.Val))
3148 return getOnesVector(Op.getValueType(), DAG);
3149 return getZeroVector(Op.getValueType(), DAG);
3150 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003151
3152 MVT::ValueType VT = Op.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00003153 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Chenga9467aa2006-04-25 20:13:52 +00003154 unsigned EVTBits = MVT::getSizeInBits(EVT);
3155
3156 unsigned NumElems = Op.getNumOperands();
3157 unsigned NumZero = 0;
3158 unsigned NumNonZero = 0;
3159 unsigned NonZeros = 0;
Dan Gohmanf906c722007-07-24 22:55:08 +00003160 unsigned NumNonZeroImms = 0;
Evan Cheng4fbf4592007-12-11 01:46:18 +00003161 SmallSet<SDOperand, 8> Values;
Evan Chenga9467aa2006-04-25 20:13:52 +00003162 for (unsigned i = 0; i < NumElems; ++i) {
3163 SDOperand Elt = Op.getOperand(i);
3164 if (Elt.getOpcode() != ISD::UNDEF) {
3165 Values.insert(Elt);
3166 if (isZeroNode(Elt))
3167 NumZero++;
3168 else {
3169 NonZeros |= (1 << i);
3170 NumNonZero++;
Dan Gohmanf906c722007-07-24 22:55:08 +00003171 if (Elt.getOpcode() == ISD::Constant ||
3172 Elt.getOpcode() == ISD::ConstantFP)
3173 NumNonZeroImms++;
Evan Chenga9467aa2006-04-25 20:13:52 +00003174 }
3175 }
3176 }
3177
Dan Gohmana8665142007-06-25 16:23:39 +00003178 if (NumNonZero == 0) {
Chris Lattner5728bdd2007-11-25 00:24:49 +00003179 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3180 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00003181 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003182
3183 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3184 if (Values.size() == 1)
3185 return SDOperand();
3186
3187 // Special case for single non-zero element.
Evan Cheng798b3062006-10-25 20:48:19 +00003188 if (NumNonZero == 1) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003189 unsigned Idx = CountTrailingZeros_32(NonZeros);
3190 SDOperand Item = Op.getOperand(Idx);
3191 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3192 if (Idx == 0)
3193 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3194 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3195 NumZero > 0, DAG);
3196
3197 if (EVTBits == 32) {
3198 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3199 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3200 DAG);
3201 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00003202 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003203 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00003204 for (unsigned i = 0; i < NumElems; i++)
3205 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003206 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3207 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003208 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3209 DAG.getNode(ISD::UNDEF, VT), Mask);
3210 }
3211 }
3212
Dan Gohmanf906c722007-07-24 22:55:08 +00003213 // A vector full of immediates; various special cases are already
3214 // handled, so this is best done with a single constant-pool load.
3215 if (NumNonZero == NumNonZeroImms)
3216 return SDOperand();
3217
Bill Wendling591eab82007-04-24 21:16:55 +00003218 // Let legalizer expand 2-wide build_vectors.
Evan Chenga9467aa2006-04-25 20:13:52 +00003219 if (EVTBits == 64)
3220 return SDOperand();
3221
3222 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendlingad2db4a2007-03-28 00:57:11 +00003223 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003224 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3225 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00003226 if (V.Val) return V;
3227 }
3228
Bill Wendlingad2db4a2007-03-28 00:57:11 +00003229 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003230 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3231 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00003232 if (V.Val) return V;
3233 }
3234
3235 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner35a08552007-02-25 07:10:00 +00003236 SmallVector<SDOperand, 8> V;
3237 V.resize(NumElems);
Evan Chenga9467aa2006-04-25 20:13:52 +00003238 if (NumElems == 4 && NumZero > 0) {
3239 for (unsigned i = 0; i < 4; ++i) {
3240 bool isZero = !(NonZeros & (1 << i));
3241 if (isZero)
3242 V[i] = getZeroVector(VT, DAG);
3243 else
3244 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3245 }
3246
3247 for (unsigned i = 0; i < 2; ++i) {
3248 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3249 default: break;
3250 case 0:
3251 V[i] = V[i*2]; // Must be a zero vector.
3252 break;
3253 case 1:
3254 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3255 getMOVLMask(NumElems, DAG));
3256 break;
3257 case 2:
3258 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3259 getMOVLMask(NumElems, DAG));
3260 break;
3261 case 3:
3262 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3263 getUnpacklMask(NumElems, DAG));
3264 break;
3265 }
3266 }
3267
Evan Cheng9fee4422006-05-16 07:21:53 +00003268 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003269 // clears the upper bits.
Evan Chenga9467aa2006-04-25 20:13:52 +00003270 // FIXME: we can do the same for v4f32 case when we know both parts of
3271 // the lower half come from scalar_to_vector (loadf32). We should do
3272 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng798b3062006-10-25 20:48:19 +00003273 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Chenga9467aa2006-04-25 20:13:52 +00003274 return V[0];
3275 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00003276 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003277 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00003278 bool Reverse = (NonZeros & 0x3) == 2;
3279 for (unsigned i = 0; i < 2; ++i)
3280 if (Reverse)
3281 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3282 else
3283 MaskVec.push_back(DAG.getConstant(i, EVT));
3284 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3285 for (unsigned i = 0; i < 2; ++i)
3286 if (Reverse)
3287 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3288 else
3289 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnered728e82006-08-11 17:38:39 +00003290 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3291 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003292 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3293 }
3294
3295 if (Values.size() > 2) {
3296 // Expand into a number of unpckl*.
3297 // e.g. for v4f32
3298 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3299 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3300 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3301 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3302 for (unsigned i = 0; i < NumElems; ++i)
3303 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3304 NumElems >>= 1;
3305 while (NumElems != 0) {
3306 for (unsigned i = 0; i < NumElems; ++i)
3307 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3308 UnpckMask);
3309 NumElems >>= 1;
3310 }
3311 return V[0];
3312 }
3313
3314 return SDOperand();
3315}
3316
Evan Chengbfd373a2007-12-07 08:07:39 +00003317static
3318SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3319 SDOperand PermMask, SelectionDAG &DAG,
3320 TargetLowering &TLI) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003321 SDOperand NewV;
Evan Chengbfd373a2007-12-07 08:07:39 +00003322 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3323 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng4fbf4592007-12-11 01:46:18 +00003324 MVT::ValueType PtrVT = TLI.getPointerTy();
3325 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3326 PermMask.Val->op_end());
3327
3328 // First record which half of which vector the low elements come from.
3329 SmallVector<unsigned, 4> LowQuad(4);
3330 for (unsigned i = 0; i < 4; ++i) {
3331 SDOperand Elt = MaskElts[i];
3332 if (Elt.getOpcode() == ISD::UNDEF)
3333 continue;
3334 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3335 int QuadIdx = EltIdx / 4;
3336 ++LowQuad[QuadIdx];
3337 }
3338 int BestLowQuad = -1;
3339 unsigned MaxQuad = 1;
3340 for (unsigned i = 0; i < 4; ++i) {
3341 if (LowQuad[i] > MaxQuad) {
3342 BestLowQuad = i;
3343 MaxQuad = LowQuad[i];
3344 }
Evan Chengbfd373a2007-12-07 08:07:39 +00003345 }
3346
Evan Cheng4fbf4592007-12-11 01:46:18 +00003347 // Record which half of which vector the high elements come from.
3348 SmallVector<unsigned, 4> HighQuad(4);
3349 for (unsigned i = 4; i < 8; ++i) {
3350 SDOperand Elt = MaskElts[i];
3351 if (Elt.getOpcode() == ISD::UNDEF)
3352 continue;
3353 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3354 int QuadIdx = EltIdx / 4;
3355 ++HighQuad[QuadIdx];
3356 }
3357 int BestHighQuad = -1;
3358 MaxQuad = 1;
3359 for (unsigned i = 0; i < 4; ++i) {
3360 if (HighQuad[i] > MaxQuad) {
3361 BestHighQuad = i;
3362 MaxQuad = HighQuad[i];
3363 }
3364 }
3365
3366 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3367 if (BestLowQuad != -1 || BestHighQuad != -1) {
3368 // First sort the 4 chunks in order using shufpd.
3369 SmallVector<SDOperand, 8> MaskVec;
3370 if (BestLowQuad != -1)
3371 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3372 else
3373 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3374 if (BestHighQuad != -1)
3375 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3376 else
3377 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3378 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3379 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3380 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3381 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3382 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3383
3384 // Now sort high and low parts separately.
3385 BitVector InOrder(8);
3386 if (BestLowQuad != -1) {
3387 // Sort lower half in order using PSHUFLW.
3388 MaskVec.clear();
3389 bool AnyOutOrder = false;
3390 for (unsigned i = 0; i != 4; ++i) {
3391 SDOperand Elt = MaskElts[i];
3392 if (Elt.getOpcode() == ISD::UNDEF) {
3393 MaskVec.push_back(Elt);
3394 InOrder.set(i);
3395 } else {
3396 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3397 if (EltIdx != i)
3398 AnyOutOrder = true;
3399 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3400 // If this element is in the right place after this shuffle, then
3401 // remember it.
3402 if ((int)(EltIdx / 4) == BestLowQuad)
3403 InOrder.set(i);
3404 }
3405 }
3406 if (AnyOutOrder) {
3407 for (unsigned i = 4; i != 8; ++i)
3408 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3409 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3410 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3411 }
3412 }
3413
3414 if (BestHighQuad != -1) {
3415 // Sort high half in order using PSHUFHW if possible.
3416 MaskVec.clear();
3417 for (unsigned i = 0; i != 4; ++i)
3418 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3419 bool AnyOutOrder = false;
3420 for (unsigned i = 4; i != 8; ++i) {
3421 SDOperand Elt = MaskElts[i];
3422 if (Elt.getOpcode() == ISD::UNDEF) {
3423 MaskVec.push_back(Elt);
3424 InOrder.set(i);
3425 } else {
3426 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3427 if (EltIdx != i)
3428 AnyOutOrder = true;
3429 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3430 // If this element is in the right place after this shuffle, then
3431 // remember it.
3432 if ((int)(EltIdx / 4) == BestHighQuad)
3433 InOrder.set(i);
3434 }
3435 }
3436 if (AnyOutOrder) {
3437 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3438 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3439 }
3440 }
3441
3442 // The other elements are put in the right place using pextrw and pinsrw.
3443 for (unsigned i = 0; i != 8; ++i) {
3444 if (InOrder[i])
3445 continue;
3446 SDOperand Elt = MaskElts[i];
3447 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3448 if (EltIdx == i)
3449 continue;
3450 SDOperand ExtOp = (EltIdx < 8)
3451 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3452 DAG.getConstant(EltIdx, PtrVT))
3453 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3454 DAG.getConstant(EltIdx - 8, PtrVT));
3455 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3456 DAG.getConstant(i, PtrVT));
3457 }
3458 return NewV;
3459 }
3460
3461 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3462 ///as few as possible.
Evan Chengbfd373a2007-12-07 08:07:39 +00003463 // First, let's find out how many elements are already in the right order.
3464 unsigned V1InOrder = 0;
3465 unsigned V1FromV1 = 0;
3466 unsigned V2InOrder = 0;
3467 unsigned V2FromV2 = 0;
Evan Cheng4fbf4592007-12-11 01:46:18 +00003468 SmallVector<SDOperand, 8> V1Elts;
3469 SmallVector<SDOperand, 8> V2Elts;
Evan Chengbfd373a2007-12-07 08:07:39 +00003470 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003471 SDOperand Elt = MaskElts[i];
Evan Chengbfd373a2007-12-07 08:07:39 +00003472 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003473 V1Elts.push_back(Elt);
3474 V2Elts.push_back(Elt);
Evan Chengbfd373a2007-12-07 08:07:39 +00003475 ++V1InOrder;
3476 ++V2InOrder;
Evan Cheng4fbf4592007-12-11 01:46:18 +00003477 continue;
3478 }
3479 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3480 if (EltIdx == i) {
3481 V1Elts.push_back(Elt);
3482 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3483 ++V1InOrder;
3484 } else if (EltIdx == i+8) {
3485 V1Elts.push_back(Elt);
3486 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3487 ++V2InOrder;
3488 } else if (EltIdx < 8) {
3489 V1Elts.push_back(Elt);
3490 ++V1FromV1;
Evan Chengbfd373a2007-12-07 08:07:39 +00003491 } else {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003492 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3493 ++V2FromV2;
Evan Chengbfd373a2007-12-07 08:07:39 +00003494 }
3495 }
3496
3497 if (V2InOrder > V1InOrder) {
3498 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3499 std::swap(V1, V2);
3500 std::swap(V1Elts, V2Elts);
3501 std::swap(V1FromV1, V2FromV2);
3502 }
3503
Evan Cheng4fbf4592007-12-11 01:46:18 +00003504 if ((V1FromV1 + V1InOrder) != 8) {
3505 // Some elements are from V2.
3506 if (V1FromV1) {
3507 // If there are elements that are from V1 but out of place,
3508 // then first sort them in place
3509 SmallVector<SDOperand, 8> MaskVec;
3510 for (unsigned i = 0; i < 8; ++i) {
3511 SDOperand Elt = V1Elts[i];
3512 if (Elt.getOpcode() == ISD::UNDEF) {
3513 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3514 continue;
3515 }
3516 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3517 if (EltIdx >= 8)
3518 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3519 else
3520 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3521 }
3522 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3523 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengbfd373a2007-12-07 08:07:39 +00003524 }
Evan Cheng4fbf4592007-12-11 01:46:18 +00003525
3526 NewV = V1;
3527 for (unsigned i = 0; i < 8; ++i) {
3528 SDOperand Elt = V1Elts[i];
3529 if (Elt.getOpcode() == ISD::UNDEF)
3530 continue;
3531 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3532 if (EltIdx < 8)
3533 continue;
3534 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3535 DAG.getConstant(EltIdx - 8, PtrVT));
3536 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3537 DAG.getConstant(i, PtrVT));
3538 }
3539 return NewV;
3540 } else {
3541 // All elements are from V1.
3542 NewV = V1;
3543 for (unsigned i = 0; i < 8; ++i) {
3544 SDOperand Elt = V1Elts[i];
3545 if (Elt.getOpcode() == ISD::UNDEF)
3546 continue;
3547 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3548 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3549 DAG.getConstant(EltIdx, PtrVT));
3550 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3551 DAG.getConstant(i, PtrVT));
3552 }
3553 return NewV;
3554 }
3555}
3556
3557/// RewriteAs4WideShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3558/// ones if possible. This can be done when every pair / quad of shuffle mask
3559/// elements point to elements in the right sequence. e.g.
3560/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3561static
3562SDOperand RewriteAs4WideShuffle(SDOperand V1, SDOperand V2,
3563 SDOperand PermMask, SelectionDAG &DAG,
3564 TargetLowering &TLI) {
3565 unsigned NumElems = PermMask.getNumOperands();
3566 unsigned Scale = NumElems / 4;
3567 SmallVector<SDOperand, 4> MaskVec;
3568 for (unsigned i = 0; i < NumElems; i += Scale) {
3569 unsigned StartIdx = ~0U;
3570 for (unsigned j = 0; j < Scale; ++j) {
3571 SDOperand Elt = PermMask.getOperand(i+j);
3572 if (Elt.getOpcode() == ISD::UNDEF)
3573 continue;
3574 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3575 if (StartIdx == ~0U)
3576 StartIdx = EltIdx - (EltIdx % Scale);
3577 if (EltIdx != StartIdx + j)
3578 return SDOperand();
3579 }
3580 if (StartIdx == ~0U)
3581 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3582 else
3583 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengbfd373a2007-12-07 08:07:39 +00003584 }
3585
Evan Cheng4fbf4592007-12-11 01:46:18 +00003586 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3587 V2 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V2);
3588 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, V2,
3589 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],4));
Evan Chengbfd373a2007-12-07 08:07:39 +00003590}
3591
Evan Chenga9467aa2006-04-25 20:13:52 +00003592SDOperand
3593X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3594 SDOperand V1 = Op.getOperand(0);
3595 SDOperand V2 = Op.getOperand(1);
3596 SDOperand PermMask = Op.getOperand(2);
3597 MVT::ValueType VT = Op.getValueType();
3598 unsigned NumElems = PermMask.getNumOperands();
3599 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3600 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Cheng949bcc92006-10-16 06:36:00 +00003601 bool V1IsSplat = false;
3602 bool V2IsSplat = false;
Evan Chenga9467aa2006-04-25 20:13:52 +00003603
Evan Cheng89c5d042006-09-08 01:50:06 +00003604 if (isUndefShuffle(Op.Val))
3605 return DAG.getNode(ISD::UNDEF, VT);
3606
Evan Chengafa1cb62007-05-17 18:45:50 +00003607 if (isZeroShuffle(Op.Val))
3608 return getZeroVector(VT, DAG);
3609
Evan Chengcea02ff2007-06-19 00:02:56 +00003610 if (isIdentityMask(PermMask.Val))
3611 return V1;
3612 else if (isIdentityMask(PermMask.Val, true))
3613 return V2;
3614
Evan Chenga9467aa2006-04-25 20:13:52 +00003615 if (isSplatMask(PermMask.Val)) {
3616 if (NumElems <= 4) return Op;
3617 // Promote it to a v4i32 splat.
Evan Cheng798b3062006-10-25 20:48:19 +00003618 return PromoteSplat(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003619 }
3620
Evan Cheng798b3062006-10-25 20:48:19 +00003621 if (X86::isMOVLMask(PermMask.Val))
3622 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003623
Evan Cheng798b3062006-10-25 20:48:19 +00003624 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3625 X86::isMOVSLDUPMask(PermMask.Val) ||
3626 X86::isMOVHLPSMask(PermMask.Val) ||
3627 X86::isMOVHPMask(PermMask.Val) ||
3628 X86::isMOVLPMask(PermMask.Val))
3629 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003630
Evan Cheng798b3062006-10-25 20:48:19 +00003631 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3632 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Chengc415c5b2006-10-25 21:49:50 +00003633 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003634
Evan Chengc415c5b2006-10-25 21:49:50 +00003635 bool Commuted = false;
Chris Lattner5728bdd2007-11-25 00:24:49 +00003636 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3637 // 1,1,1,1 -> v8i16 though.
Evan Cheng798b3062006-10-25 20:48:19 +00003638 V1IsSplat = isSplatVector(V1.Val);
3639 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner5728bdd2007-11-25 00:24:49 +00003640
3641 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng798b3062006-10-25 20:48:19 +00003642 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Chengc415c5b2006-10-25 21:49:50 +00003643 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00003644 std::swap(V1IsSplat, V2IsSplat);
3645 std::swap(V1IsUndef, V2IsUndef);
Evan Chengc415c5b2006-10-25 21:49:50 +00003646 Commuted = true;
Evan Cheng798b3062006-10-25 20:48:19 +00003647 }
3648
3649 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3650 if (V2IsUndef) return V1;
Evan Chengc415c5b2006-10-25 21:49:50 +00003651 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00003652 if (V2IsSplat) {
3653 // V2 is a splat, so the mask may be malformed. That is, it may point
3654 // to any V2 element. The instruction selectior won't like this. Get
3655 // a corrected mask and commute to form a proper MOVS{S|D}.
3656 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3657 if (NewMask.Val != PermMask.Val)
3658 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00003659 }
Evan Cheng798b3062006-10-25 20:48:19 +00003660 return Op;
Evan Cheng949bcc92006-10-16 06:36:00 +00003661 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003662
Evan Cheng949bcc92006-10-16 06:36:00 +00003663 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling591eab82007-04-24 21:16:55 +00003664 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng949bcc92006-10-16 06:36:00 +00003665 X86::isUNPCKLMask(PermMask.Val) ||
3666 X86::isUNPCKHMask(PermMask.Val))
3667 return Op;
Evan Cheng8c5766e2006-10-04 18:33:38 +00003668
Evan Cheng798b3062006-10-25 20:48:19 +00003669 if (V2IsSplat) {
3670 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003671 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng798b3062006-10-25 20:48:19 +00003672 // new vector_shuffle with the corrected mask.
3673 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3674 if (NewMask.Val != PermMask.Val) {
3675 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3676 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3677 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3678 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3679 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3680 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00003681 }
3682 }
3683 }
3684
3685 // Normalize the node to match x86 shuffle ops if needed
Evan Chengc415c5b2006-10-25 21:49:50 +00003686 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3687 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3688
3689 if (Commuted) {
3690 // Commute is back and try unpck* again.
3691 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3692 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling591eab82007-04-24 21:16:55 +00003693 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengc415c5b2006-10-25 21:49:50 +00003694 X86::isUNPCKLMask(PermMask.Val) ||
3695 X86::isUNPCKHMask(PermMask.Val))
3696 return Op;
3697 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003698
3699 // If VT is integer, try PSHUF* first, then SHUFP*.
3700 if (MVT::isInteger(VT)) {
Dan Gohman8932bff2007-08-02 21:17:01 +00003701 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3702 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3703 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3704 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Chenga9467aa2006-04-25 20:13:52 +00003705 X86::isPSHUFHWMask(PermMask.Val) ||
3706 X86::isPSHUFLWMask(PermMask.Val)) {
3707 if (V2.getOpcode() != ISD::UNDEF)
3708 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3709 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3710 return Op;
3711 }
3712
Chris Lattnerdade6072007-05-17 17:13:13 +00003713 if (X86::isSHUFPMask(PermMask.Val) &&
3714 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Chenga9467aa2006-04-25 20:13:52 +00003715 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003716 } else {
3717 // Floating point cases in the other order.
3718 if (X86::isSHUFPMask(PermMask.Val))
3719 return Op;
3720 if (X86::isPSHUFDMask(PermMask.Val) ||
3721 X86::isPSHUFHWMask(PermMask.Val) ||
3722 X86::isPSHUFLWMask(PermMask.Val)) {
3723 if (V2.getOpcode() != ISD::UNDEF)
3724 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3725 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3726 return Op;
3727 }
3728 }
3729
Evan Cheng4fbf4592007-12-11 01:46:18 +00003730 // If the shuffle can be rewritten as a 4 wide shuffle, then do it!
3731 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3732 SDOperand NewOp = RewriteAs4WideShuffle(V1, V2, PermMask, DAG, *this);
3733 if (NewOp.Val)
3734 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3735 }
Evan Chengbfd373a2007-12-07 08:07:39 +00003736
Evan Cheng4fbf4592007-12-11 01:46:18 +00003737 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3738 if (VT == MVT::v8i16) {
3739 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3740 if (NewOp.Val)
3741 return NewOp;
3742 }
3743
3744 // Handle all 4 wide cases with a number of shuffles.
3745 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengbfd373a2007-12-07 08:07:39 +00003746 // Don't do this for MMX.
Evan Chenga9467aa2006-04-25 20:13:52 +00003747 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00003748 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003749 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng3cd43622006-04-28 07:03:38 +00003750 Locs.reserve(NumElems);
Evan Cheng4fbf4592007-12-11 01:46:18 +00003751 SmallVector<SDOperand, 8> Mask1(NumElems,
3752 DAG.getNode(ISD::UNDEF, MaskEVT));
3753 SmallVector<SDOperand, 8> Mask2(NumElems,
3754 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng3cd43622006-04-28 07:03:38 +00003755 unsigned NumHi = 0;
3756 unsigned NumLo = 0;
3757 // If no more than two elements come from either vector. This can be
3758 // implemented with two shuffles. First shuffle gather the elements.
3759 // The second shuffle, which takes the first shuffle as both of its
3760 // vector operands, put the elements into the right order.
3761 for (unsigned i = 0; i != NumElems; ++i) {
3762 SDOperand Elt = PermMask.getOperand(i);
3763 if (Elt.getOpcode() == ISD::UNDEF) {
3764 Locs[i] = std::make_pair(-1, -1);
3765 } else {
3766 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3767 if (Val < NumElems) {
3768 Locs[i] = std::make_pair(0, NumLo);
3769 Mask1[NumLo] = Elt;
3770 NumLo++;
3771 } else {
3772 Locs[i] = std::make_pair(1, NumHi);
3773 if (2+NumHi < NumElems)
3774 Mask1[2+NumHi] = Elt;
3775 NumHi++;
3776 }
3777 }
3778 }
3779 if (NumLo <= 2 && NumHi <= 2) {
3780 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00003781 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3782 &Mask1[0], Mask1.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00003783 for (unsigned i = 0; i != NumElems; ++i) {
3784 if (Locs[i].first == -1)
3785 continue;
3786 else {
3787 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3788 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3789 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3790 }
3791 }
3792
3793 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnered728e82006-08-11 17:38:39 +00003794 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3795 &Mask2[0], Mask2.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00003796 }
3797
3798 // Break it into (shuffle shuffle_hi, shuffle_lo).
3799 Locs.clear();
Chris Lattner35a08552007-02-25 07:10:00 +00003800 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3801 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3802 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Chenga9467aa2006-04-25 20:13:52 +00003803 unsigned MaskIdx = 0;
3804 unsigned LoIdx = 0;
3805 unsigned HiIdx = NumElems/2;
3806 for (unsigned i = 0; i != NumElems; ++i) {
3807 if (i == NumElems/2) {
3808 MaskPtr = &HiMask;
3809 MaskIdx = 1;
3810 LoIdx = 0;
3811 HiIdx = NumElems/2;
3812 }
3813 SDOperand Elt = PermMask.getOperand(i);
3814 if (Elt.getOpcode() == ISD::UNDEF) {
3815 Locs[i] = std::make_pair(-1, -1);
3816 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3817 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3818 (*MaskPtr)[LoIdx] = Elt;
3819 LoIdx++;
3820 } else {
3821 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3822 (*MaskPtr)[HiIdx] = Elt;
3823 HiIdx++;
3824 }
3825 }
3826
Chris Lattner3d826992006-05-16 06:45:34 +00003827 SDOperand LoShuffle =
3828 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00003829 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3830 &LoMask[0], LoMask.size()));
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003831 SDOperand HiShuffle =
Chris Lattner3d826992006-05-16 06:45:34 +00003832 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00003833 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3834 &HiMask[0], HiMask.size()));
Chris Lattner35a08552007-02-25 07:10:00 +00003835 SmallVector<SDOperand, 8> MaskOps;
Evan Chenga9467aa2006-04-25 20:13:52 +00003836 for (unsigned i = 0; i != NumElems; ++i) {
3837 if (Locs[i].first == -1) {
3838 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3839 } else {
3840 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3841 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3842 }
3843 }
3844 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnered728e82006-08-11 17:38:39 +00003845 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3846 &MaskOps[0], MaskOps.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003847 }
3848
3849 return SDOperand();
3850}
3851
3852SDOperand
3853X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3854 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3855 return SDOperand();
3856
3857 MVT::ValueType VT = Op.getValueType();
3858 // TODO: handle v16i8.
3859 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003860 SDOperand Vec = Op.getOperand(0);
3861 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3862 if (Idx == 0)
3863 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3864 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3865 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3866 Op.getOperand(1)));
Evan Chenga9467aa2006-04-25 20:13:52 +00003867 // Transform it so it match pextrw which produces a 32-bit result.
3868 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3869 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3870 Op.getOperand(0), Op.getOperand(1));
3871 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3872 DAG.getValueType(VT));
3873 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3874 } else if (MVT::getSizeInBits(VT) == 32) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003875 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3876 if (Idx == 0)
3877 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003878 // SHUFPS the element to the lowest double word, then movss.
3879 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00003880 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00003881 IdxVec.
3882 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3883 IdxVec.
3884 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3885 IdxVec.
3886 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3887 IdxVec.
3888 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00003889 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3890 &IdxVec[0], IdxVec.size());
Evan Cheng4fbf4592007-12-11 01:46:18 +00003891 SDOperand Vec = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003892 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng922e1912006-11-07 22:14:24 +00003893 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Chenga9467aa2006-04-25 20:13:52 +00003894 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00003895 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003896 } else if (MVT::getSizeInBits(VT) == 64) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003897 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3898 if (Idx == 0)
3899 return Op;
3900
3901 // UNPCKHPD the element to the lowest double word, then movsd.
3902 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3903 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3904 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00003905 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman5c441312007-06-14 22:58:02 +00003906 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00003907 IdxVec.
3908 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00003909 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3910 &IdxVec[0], IdxVec.size());
Evan Cheng4fbf4592007-12-11 01:46:18 +00003911 SDOperand Vec = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003912 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3913 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3914 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00003915 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003916 }
3917
3918 return SDOperand();
3919}
3920
3921SDOperand
3922X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00003923 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00003924 // as its second argument.
3925 MVT::ValueType VT = Op.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00003926 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Chenga9467aa2006-04-25 20:13:52 +00003927 SDOperand N0 = Op.getOperand(0);
3928 SDOperand N1 = Op.getOperand(1);
3929 SDOperand N2 = Op.getOperand(2);
3930 if (MVT::getSizeInBits(BaseVT) == 16) {
3931 if (N1.getValueType() != MVT::i32)
3932 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3933 if (N2.getValueType() != MVT::i32)
Evan Cheng3bd318e2007-06-29 00:01:20 +00003934 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Chenga9467aa2006-04-25 20:13:52 +00003935 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3936 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3937 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3938 if (Idx == 0) {
3939 // Use a movss.
3940 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3941 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman5c441312007-06-14 22:58:02 +00003942 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003943 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00003944 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3945 for (unsigned i = 1; i <= 3; ++i)
3946 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3947 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnered728e82006-08-11 17:38:39 +00003948 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3949 &MaskVec[0], MaskVec.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003950 } else {
3951 // Use two pinsrw instructions to insert a 32 bit value.
3952 Idx <<= 1;
3953 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng242a8772007-07-31 06:21:44 +00003954 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3955 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3956 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3957 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003958 }
3959 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3960 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00003961 DAG.getConstant(Idx, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003962 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3963 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00003964 DAG.getConstant(Idx+1, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003965 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3966 }
3967 }
3968
3969 return SDOperand();
3970}
3971
3972SDOperand
3973X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3974 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3975 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3976}
3977
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003978// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Chenga9467aa2006-04-25 20:13:52 +00003979// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3980// one of the above mentioned nodes. It has to be wrapped because otherwise
3981// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3982// be used to form addressing mode. These wrapped nodes will be selected
3983// into MOV32ri.
3984SDOperand
3985X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3986 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng0b169222006-11-29 23:19:46 +00003987 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3988 getPointerTy(),
3989 CP->getAlignment());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003990 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003991 // With PIC, the address is actually $g + Offset.
3992 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3993 !Subtarget->isPICStyleRIPRel()) {
3994 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3995 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3996 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003997 }
3998
3999 return Result;
4000}
4001
4002SDOperand
4003X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4004 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng0b169222006-11-29 23:19:46 +00004005 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00004006 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00004007 // With PIC, the address is actually $g + Offset.
4008 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4009 !Subtarget->isPICStyleRIPRel()) {
4010 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4011 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4012 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00004013 }
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00004014
4015 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4016 // load the value at address GV, not the value of GV itself. This means that
4017 // the GlobalAddress must be in the base or index register of the address, not
4018 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikova0554d92007-01-12 19:20:47 +00004019 // The same applies for external symbols during PIC codegen
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00004020 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
4021 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004022
4023 return Result;
4024}
4025
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00004026// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4027static SDOperand
4028LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4029 const MVT::ValueType PtrVT) {
4030 SDOperand InFlag;
4031 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4032 DAG.getNode(X86ISD::GlobalBaseReg,
4033 PtrVT), InFlag);
4034 InFlag = Chain.getValue(1);
4035
4036 // emit leal symbol@TLSGD(,%ebx,1), %eax
4037 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4038 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4039 GA->getValueType(0),
4040 GA->getOffset());
4041 SDOperand Ops[] = { Chain, TGA, InFlag };
4042 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4043 InFlag = Result.getValue(2);
4044 Chain = Result.getValue(1);
4045
4046 // call ___tls_get_addr. This function receives its argument in
4047 // the register EAX.
4048 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4049 InFlag = Chain.getValue(1);
4050
4051 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4052 SDOperand Ops1[] = { Chain,
4053 DAG.getTargetExternalSymbol("___tls_get_addr",
4054 PtrVT),
4055 DAG.getRegister(X86::EAX, PtrVT),
4056 DAG.getRegister(X86::EBX, PtrVT),
4057 InFlag };
4058 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4059 InFlag = Chain.getValue(1);
4060
4061 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4062}
4063
4064// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4065// "local exec" model.
4066static SDOperand
4067LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4068 const MVT::ValueType PtrVT) {
4069 // Get the Thread Pointer
4070 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4071 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4072 // exec)
4073 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4074 GA->getValueType(0),
4075 GA->getOffset());
4076 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancioefb80772007-04-22 22:50:52 +00004077
4078 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4079 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
4080
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00004081 // The address of the thread local variable is the add of the thread
4082 // pointer with the offset of the variable.
4083 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4084}
4085
4086SDOperand
4087X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4088 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00004089 // TODO: implement the "initial exec"model for pic executables
4090 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4091 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00004092 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4093 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4094 // otherwise use the "Local Exec"TLS Model
4095 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4096 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4097 else
4098 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4099}
4100
Evan Chenga9467aa2006-04-25 20:13:52 +00004101SDOperand
4102X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4103 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Cheng0b169222006-11-29 23:19:46 +00004104 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00004105 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00004106 // With PIC, the address is actually $g + Offset.
4107 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4108 !Subtarget->isPICStyleRIPRel()) {
4109 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4110 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4111 Result);
4112 }
4113
4114 return Result;
4115}
4116
4117SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4118 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4119 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4120 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4121 // With PIC, the address is actually $g + Offset.
4122 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4123 !Subtarget->isPICStyleRIPRel()) {
4124 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4125 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4126 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00004127 }
4128
4129 return Result;
4130}
4131
Chris Lattner693cbea2007-10-17 06:02:13 +00004132/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4133/// take a 2 x i32 value to shift plus a shift amount.
Evan Chenga9467aa2006-04-25 20:13:52 +00004134SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner693cbea2007-10-17 06:02:13 +00004135 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4136 "Not an i64 shift!");
4137 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4138 SDOperand ShOpLo = Op.getOperand(0);
4139 SDOperand ShOpHi = Op.getOperand(1);
4140 SDOperand ShAmt = Op.getOperand(2);
4141 SDOperand Tmp1 = isSRA ?
4142 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4143 DAG.getConstant(0, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00004144
Chris Lattner693cbea2007-10-17 06:02:13 +00004145 SDOperand Tmp2, Tmp3;
4146 if (Op.getOpcode() == ISD::SHL_PARTS) {
4147 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4148 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4149 } else {
4150 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4151 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4152 }
Evan Cheng9c249c32006-01-09 18:33:28 +00004153
Chris Lattner693cbea2007-10-17 06:02:13 +00004154 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4155 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4156 DAG.getConstant(32, MVT::i8));
4157 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4158 AndNode, DAG.getConstant(0, MVT::i8));
Evan Cheng9c249c32006-01-09 18:33:28 +00004159
Chris Lattner693cbea2007-10-17 06:02:13 +00004160 SDOperand Hi, Lo;
4161 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4162 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4163 SmallVector<SDOperand, 4> Ops;
4164 if (Op.getOpcode() == ISD::SHL_PARTS) {
4165 Ops.push_back(Tmp2);
4166 Ops.push_back(Tmp3);
4167 Ops.push_back(CC);
4168 Ops.push_back(Cond);
4169 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00004170
Evan Cheng9c249c32006-01-09 18:33:28 +00004171 Ops.clear();
Chris Lattner693cbea2007-10-17 06:02:13 +00004172 Ops.push_back(Tmp3);
4173 Ops.push_back(Tmp1);
4174 Ops.push_back(CC);
4175 Ops.push_back(Cond);
4176 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4177 } else {
4178 Ops.push_back(Tmp2);
4179 Ops.push_back(Tmp3);
4180 Ops.push_back(CC);
4181 Ops.push_back(Cond);
4182 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4183
4184 Ops.clear();
4185 Ops.push_back(Tmp3);
4186 Ops.push_back(Tmp1);
4187 Ops.push_back(CC);
4188 Ops.push_back(Cond);
4189 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4190 }
4191
4192 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4193 Ops.clear();
4194 Ops.push_back(Lo);
4195 Ops.push_back(Hi);
4196 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004197}
Evan Cheng6305e502006-01-12 22:54:21 +00004198
Evan Chenga9467aa2006-04-25 20:13:52 +00004199SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4200 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4201 Op.getOperand(0).getValueType() >= MVT::i16 &&
4202 "Unknown SINT_TO_FP to lower!");
4203
4204 SDOperand Result;
4205 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4206 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4207 MachineFunction &MF = DAG.getMachineFunction();
4208 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4209 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chengdf9ac472006-10-05 23:01:46 +00004210 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Chengab51cf22006-10-13 21:14:26 +00004211 StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004212
Dale Johannesen98d3a082007-09-14 22:26:36 +00004213 // These are really Legal; caller falls through into that case.
Dale Johannesene36c4002007-09-23 14:52:20 +00004214 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
4215 return Result;
4216 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen98d3a082007-09-14 22:26:36 +00004217 return Result;
Dale Johannesen7d67e542007-09-19 23:55:34 +00004218 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
4219 Subtarget->is64Bit())
4220 return Result;
Dale Johannesen98d3a082007-09-14 22:26:36 +00004221
Evan Chenga9467aa2006-04-25 20:13:52 +00004222 // Build the FILD
Chris Lattner35a08552007-02-25 07:10:00 +00004223 SDVTList Tys;
Dale Johannesene36c4002007-09-23 14:52:20 +00004224 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
4225 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen98d3a082007-09-14 22:26:36 +00004226 if (useSSE)
Chris Lattner35a08552007-02-25 07:10:00 +00004227 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4228 else
Dale Johannesena2b3c172007-07-03 00:53:03 +00004229 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00004230 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00004231 Ops.push_back(Chain);
4232 Ops.push_back(StackSlot);
4233 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen98d3a082007-09-14 22:26:36 +00004234 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004235 Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004236
Dale Johannesen98d3a082007-09-14 22:26:36 +00004237 if (useSSE) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004238 Chain = Result.getValue(1);
4239 SDOperand InFlag = Result.getValue(2);
4240
4241 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4242 // shouldn't be necessary except that RFP cannot be live across
4243 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00004244 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00004245 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00004246 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner35a08552007-02-25 07:10:00 +00004247 Tys = DAG.getVTList(MVT::Other);
4248 SmallVector<SDOperand, 8> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00004249 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00004250 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00004251 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00004252 Ops.push_back(DAG.getValueType(Op.getValueType()));
4253 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004254 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004255 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattner76ac0682005-11-15 00:40:23 +00004256 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004257
Evan Chenga9467aa2006-04-25 20:13:52 +00004258 return Result;
4259}
4260
Chris Lattnerf81d5882007-11-24 07:07:01 +00004261std::pair<SDOperand,SDOperand> X86TargetLowering::
4262FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004263 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4264 "Unknown FP_TO_SINT to lower!");
Evan Chenga9467aa2006-04-25 20:13:52 +00004265
Dale Johannesen98d3a082007-09-14 22:26:36 +00004266 // These are really Legal.
Dale Johannesene36c4002007-09-23 14:52:20 +00004267 if (Op.getValueType() == MVT::i32 &&
4268 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
Chris Lattnerf81d5882007-11-24 07:07:01 +00004269 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesene36c4002007-09-23 14:52:20 +00004270 if (Op.getValueType() == MVT::i32 &&
4271 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Chris Lattnerf81d5882007-11-24 07:07:01 +00004272 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen7d67e542007-09-19 23:55:34 +00004273 if (Subtarget->is64Bit() &&
4274 Op.getValueType() == MVT::i64 &&
4275 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerf81d5882007-11-24 07:07:01 +00004276 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen98d3a082007-09-14 22:26:36 +00004277
Evan Cheng7bcfd8f2007-10-15 20:11:21 +00004278 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4279 // stack slot.
4280 MachineFunction &MF = DAG.getMachineFunction();
4281 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4282 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4283 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga9467aa2006-04-25 20:13:52 +00004284 unsigned Opc;
4285 switch (Op.getValueType()) {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004286 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4287 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4288 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4289 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00004290 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004291
Evan Chenga9467aa2006-04-25 20:13:52 +00004292 SDOperand Chain = DAG.getEntryNode();
4293 SDOperand Value = Op.getOperand(0);
Dale Johannesene36c4002007-09-23 14:52:20 +00004294 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
4295 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004296 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Chengab51cf22006-10-13 21:14:26 +00004297 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesena2b3c172007-07-03 00:53:03 +00004298 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00004299 SDOperand Ops[] = {
4300 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4301 };
4302 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00004303 Chain = Value.getValue(1);
4304 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4305 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4306 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004307
Evan Chenga9467aa2006-04-25 20:13:52 +00004308 // Build the FP_TO_INT*_IN_MEM
Chris Lattner35a08552007-02-25 07:10:00 +00004309 SDOperand Ops[] = { Chain, Value, StackSlot };
4310 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Cheng172fce72006-01-06 00:43:03 +00004311
Chris Lattnerf81d5882007-11-24 07:07:01 +00004312 return std::make_pair(FIST, StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00004313}
4314
Chris Lattnerf81d5882007-11-24 07:07:01 +00004315SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004316 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4317 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4318 if (FIST.Val == 0) return SDOperand();
4319
4320 // Load the result.
4321 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4322}
4323
4324SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4325 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4326 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4327 if (FIST.Val == 0) return 0;
4328
4329 // Return an i64 load from the stack slot.
4330 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4331
4332 // Use a MERGE_VALUES node to drop the chain result value.
4333 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4334}
4335
Evan Chenga9467aa2006-04-25 20:13:52 +00004336SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4337 MVT::ValueType VT = Op.getValueType();
Dan Gohman57111e72007-07-10 00:05:58 +00004338 MVT::ValueType EltVT = VT;
4339 if (MVT::isVector(VT))
4340 EltVT = MVT::getVectorElementType(VT);
4341 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Chenga9467aa2006-04-25 20:13:52 +00004342 std::vector<Constant*> CV;
Dan Gohman57111e72007-07-10 00:05:58 +00004343 if (EltVT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004344 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman57111e72007-07-10 00:05:58 +00004345 CV.push_back(C);
4346 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004347 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004348 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman57111e72007-07-10 00:05:58 +00004349 CV.push_back(C);
4350 CV.push_back(C);
4351 CV.push_back(C);
4352 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004353 }
Dan Gohman47885522007-07-27 17:16:43 +00004354 Constant *C = ConstantVector::get(CV);
4355 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4356 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4357 false, 16);
Evan Chenga9467aa2006-04-25 20:13:52 +00004358 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4359}
4360
4361SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4362 MVT::ValueType VT = Op.getValueType();
Dan Gohman57111e72007-07-10 00:05:58 +00004363 MVT::ValueType EltVT = VT;
Evan Cheng64738532007-07-19 23:36:01 +00004364 unsigned EltNum = 1;
4365 if (MVT::isVector(VT)) {
Dan Gohman57111e72007-07-10 00:05:58 +00004366 EltVT = MVT::getVectorElementType(VT);
Evan Cheng64738532007-07-19 23:36:01 +00004367 EltNum = MVT::getVectorNumElements(VT);
4368 }
Dan Gohman57111e72007-07-10 00:05:58 +00004369 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Chenga9467aa2006-04-25 20:13:52 +00004370 std::vector<Constant*> CV;
Dan Gohman57111e72007-07-10 00:05:58 +00004371 if (EltVT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004372 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman57111e72007-07-10 00:05:58 +00004373 CV.push_back(C);
4374 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004375 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004376 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman57111e72007-07-10 00:05:58 +00004377 CV.push_back(C);
4378 CV.push_back(C);
4379 CV.push_back(C);
4380 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004381 }
Dan Gohman47885522007-07-27 17:16:43 +00004382 Constant *C = ConstantVector::get(CV);
4383 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4384 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4385 false, 16);
Evan Cheng64738532007-07-19 23:36:01 +00004386 if (MVT::isVector(VT)) {
Evan Cheng64738532007-07-19 23:36:01 +00004387 return DAG.getNode(ISD::BIT_CONVERT, VT,
4388 DAG.getNode(ISD::XOR, MVT::v2i64,
4389 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4390 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4391 } else {
Evan Cheng64738532007-07-19 23:36:01 +00004392 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4393 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004394}
4395
Evan Cheng4363e882007-01-05 07:55:56 +00004396SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng82241c82007-01-05 21:37:56 +00004397 SDOperand Op0 = Op.getOperand(0);
4398 SDOperand Op1 = Op.getOperand(1);
Evan Cheng4363e882007-01-05 07:55:56 +00004399 MVT::ValueType VT = Op.getValueType();
Evan Cheng82241c82007-01-05 21:37:56 +00004400 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng4363e882007-01-05 07:55:56 +00004401 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00004402
4403 // If second operand is smaller, extend it first.
4404 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4405 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4406 SrcVT = VT;
Dale Johannesenbed9dc42007-09-06 18:13:44 +00004407 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00004408 }
Dale Johannesen8ee70112007-10-21 01:07:44 +00004409 // And if it is bigger, shrink it first.
4410 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4411 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4412 SrcVT = VT;
4413 SrcTy = MVT::getTypeForValueType(SrcVT);
4414 }
4415
4416 // At this point the operands and the result should have the same
4417 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng82241c82007-01-05 21:37:56 +00004418
Evan Cheng4363e882007-01-05 07:55:56 +00004419 // First get the sign bit of second operand.
4420 std::vector<Constant*> CV;
4421 if (SrcVT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004422 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4423 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng4363e882007-01-05 07:55:56 +00004424 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004425 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4426 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4427 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4428 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng4363e882007-01-05 07:55:56 +00004429 }
Dan Gohman47885522007-07-27 17:16:43 +00004430 Constant *C = ConstantVector::get(CV);
4431 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4432 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4433 false, 16);
Evan Cheng82241c82007-01-05 21:37:56 +00004434 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng4363e882007-01-05 07:55:56 +00004435
4436 // Shift sign bit right or left if the two operands have different types.
4437 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4438 // Op0 is MVT::f32, Op1 is MVT::f64.
4439 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4440 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4441 DAG.getConstant(32, MVT::i32));
4442 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4443 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4444 DAG.getConstant(0, getPointerTy()));
Evan Cheng4363e882007-01-05 07:55:56 +00004445 }
4446
Evan Cheng82241c82007-01-05 21:37:56 +00004447 // Clear first operand sign bit.
4448 CV.clear();
4449 if (VT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004450 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4451 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng82241c82007-01-05 21:37:56 +00004452 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004453 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4454 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4455 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4456 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng82241c82007-01-05 21:37:56 +00004457 }
Dan Gohman47885522007-07-27 17:16:43 +00004458 C = ConstantVector::get(CV);
4459 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4460 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4461 false, 16);
Evan Cheng82241c82007-01-05 21:37:56 +00004462 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4463
4464 // Or the value with the sign bit.
4465 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng4363e882007-01-05 07:55:56 +00004466}
4467
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004468SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge95f3912007-09-25 01:57:46 +00004469 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng9b7f0e62007-09-26 00:45:55 +00004470 SDOperand Cond;
Evan Chenge95f3912007-09-25 01:57:46 +00004471 SDOperand Op0 = Op.getOperand(0);
4472 SDOperand Op1 = Op.getOperand(1);
4473 SDOperand CC = Op.getOperand(2);
4474 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4475 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4476 unsigned X86CC;
4477
Evan Chenge95f3912007-09-25 01:57:46 +00004478 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng9b7f0e62007-09-26 00:45:55 +00004479 Op0, Op1, DAG)) {
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004480 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4481 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004482 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng9b7f0e62007-09-26 00:45:55 +00004483 }
Evan Chenge95f3912007-09-25 01:57:46 +00004484
4485 assert(isFP && "Illegal integer SetCC!");
4486
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004487 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Chenge95f3912007-09-25 01:57:46 +00004488 switch (SetCCOpcode) {
4489 default: assert(false && "Illegal floating point SetCC!");
4490 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004491 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004492 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004493 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004494 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4495 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4496 }
4497 case ISD::SETUNE: { // PF | !ZF
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004498 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004499 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004500 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004501 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4502 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4503 }
4504 }
4505}
4506
4507
Evan Chenga9467aa2006-04-25 20:13:52 +00004508SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00004509 bool addTest = true;
Evan Cheng4259a0f2006-09-11 02:19:56 +00004510 SDOperand Cond = Op.getOperand(0);
4511 SDOperand CC;
Evan Cheng944d1e92006-01-26 02:13:10 +00004512
Evan Cheng4259a0f2006-09-11 02:19:56 +00004513 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004514 Cond = LowerSETCC(Cond, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004515
Evan Chengf5ec10b2007-10-08 22:16:29 +00004516 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4517 // setting operand in place of the X86ISD::SETCC.
Evan Cheng4259a0f2006-09-11 02:19:56 +00004518 if (Cond.getOpcode() == X86ISD::SETCC) {
4519 CC = Cond.getOperand(0);
4520
Evan Cheng4259a0f2006-09-11 02:19:56 +00004521 SDOperand Cmp = Cond.getOperand(1);
4522 unsigned Opc = Cmp.getOpcode();
Evan Chengf5ec10b2007-10-08 22:16:29 +00004523 MVT::ValueType VT = Op.getValueType();
4524 bool IllegalFPCMov = false;
4525 if (VT == MVT::f32 && !X86ScalarSSEf32)
4526 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4527 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4528 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesene5530a32007-10-16 18:09:08 +00004529 else if (VT == MVT::f80)
4530 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004531 if ((Opc == X86ISD::CMP ||
4532 Opc == X86ISD::COMI ||
4533 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Chengf5ec10b2007-10-08 22:16:29 +00004534 Cond = Cmp;
Evan Chenge95f3912007-09-25 01:57:46 +00004535 addTest = false;
4536 }
4537 }
4538
4539 if (addTest) {
4540 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chengf5ec10b2007-10-08 22:16:29 +00004541 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Chenge95f3912007-09-25 01:57:46 +00004542 }
4543
4544 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4545 MVT::Flag);
4546 SmallVector<SDOperand, 4> Ops;
4547 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4548 // condition is true.
4549 Ops.push_back(Op.getOperand(2));
4550 Ops.push_back(Op.getOperand(1));
4551 Ops.push_back(CC);
4552 Ops.push_back(Cond);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004553 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge95f3912007-09-25 01:57:46 +00004554}
4555
Evan Chenga9467aa2006-04-25 20:13:52 +00004556SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00004557 bool addTest = true;
4558 SDOperand Chain = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004559 SDOperand Cond = Op.getOperand(1);
4560 SDOperand Dest = Op.getOperand(2);
4561 SDOperand CC;
Evan Cheng4259a0f2006-09-11 02:19:56 +00004562
Evan Chenga9467aa2006-04-25 20:13:52 +00004563 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004564 Cond = LowerSETCC(Cond, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004565
Evan Chengf5ec10b2007-10-08 22:16:29 +00004566 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4567 // setting operand in place of the X86ISD::SETCC.
Evan Chenga9467aa2006-04-25 20:13:52 +00004568 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00004569 CC = Cond.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004570
Evan Cheng4259a0f2006-09-11 02:19:56 +00004571 SDOperand Cmp = Cond.getOperand(1);
4572 unsigned Opc = Cmp.getOpcode();
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004573 if (Opc == X86ISD::CMP ||
4574 Opc == X86ISD::COMI ||
4575 Opc == X86ISD::UCOMI) {
Evan Chengf5ec10b2007-10-08 22:16:29 +00004576 Cond = Cmp;
Evan Chenge95f3912007-09-25 01:57:46 +00004577 addTest = false;
4578 }
4579 }
4580
4581 if (addTest) {
4582 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004583 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Chenge95f3912007-09-25 01:57:46 +00004584 }
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004585 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Chenge95f3912007-09-25 01:57:46 +00004586 Chain, Op.getOperand(2), CC, Cond);
4587}
4588
Evan Cheng2a330942006-05-25 00:59:30 +00004589SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00004590 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4591 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004592
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00004593 if (Subtarget->is64Bit())
4594 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4595 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4596 else
4597 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng2a330942006-05-25 00:59:30 +00004598 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00004599 switch (CallingConv) {
Chris Lattnerfc360392006-09-27 18:29:38 +00004600 default:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004601 assert(0 && "Unsupported calling convention");
Chris Lattnerfc360392006-09-27 18:29:38 +00004602 case CallingConv::Fast:
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00004603 if (isTailCall && PerformTailCallOpt)
4604 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4605 else
4606 return LowerCCCCallTo(Op,DAG, CallingConv);
Chris Lattnerfc360392006-09-27 18:29:38 +00004607 case CallingConv::C:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004608 case CallingConv::X86_StdCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00004609 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerfc360392006-09-27 18:29:38 +00004610 case CallingConv::X86_FastCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00004611 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00004612 }
Evan Cheng2a330942006-05-25 00:59:30 +00004613}
4614
Anton Korobeynikov9b91d982007-04-17 19:34:00 +00004615
4616// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4617// Calls to _alloca is needed to probe the stack when allocating more than 4k
4618// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4619// that the guard pages used by the OS virtual memory manager are allocated in
4620// correct sequence.
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004621SDOperand
4622X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4623 SelectionDAG &DAG) {
Anton Korobeynikov9b91d982007-04-17 19:34:00 +00004624 assert(Subtarget->isTargetCygMing() &&
4625 "This should be used only on Cygwin/Mingw targets");
4626
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004627 // Get the inputs.
4628 SDOperand Chain = Op.getOperand(0);
4629 SDOperand Size = Op.getOperand(1);
4630 // FIXME: Ensure alignment here
4631
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004632 SDOperand Flag;
4633
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004634 MVT::ValueType IntPtr = getPointerTy();
4635 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004636
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004637 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4638 Flag = Chain.getValue(1);
4639
4640 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4641 SDOperand Ops[] = { Chain,
4642 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4643 DAG.getRegister(X86::EAX, IntPtr),
4644 Flag };
4645 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4646 Flag = Chain.getValue(1);
4647
4648 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004649
4650 std::vector<MVT::ValueType> Tys;
4651 Tys.push_back(SPTy);
4652 Tys.push_back(MVT::Other);
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004653 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4654 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004655}
4656
Evan Chenge0bcfbe2006-04-26 01:20:17 +00004657SDOperand
4658X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chengdc614c12006-06-06 23:30:24 +00004659 MachineFunction &MF = DAG.getMachineFunction();
4660 const Function* Fn = MF.getFunction();
4661 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +00004662 Subtarget->isTargetCygMing() &&
Evan Cheng0e14a562006-06-09 06:24:42 +00004663 Fn->getName() == "main")
Chris Lattnerff0598d2007-04-17 17:21:52 +00004664 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chengdc614c12006-06-06 23:30:24 +00004665
Evan Cheng17e734f2006-05-23 21:06:34 +00004666 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004667 if (Subtarget->is64Bit())
4668 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng17e734f2006-05-23 21:06:34 +00004669 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00004670 switch(CC) {
Chris Lattnerfc360392006-09-27 18:29:38 +00004671 default:
4672 assert(0 && "Unsupported calling convention");
4673 case CallingConv::Fast:
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00004674 return LowerCCCArguments(Op,DAG, true);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00004675 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00004676 case CallingConv::C:
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00004677 return LowerCCCArguments(Op, DAG);
Chris Lattnerfc360392006-09-27 18:29:38 +00004678 case CallingConv::X86_StdCall:
Chris Lattnerff0598d2007-04-17 17:21:52 +00004679 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00004680 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerfc360392006-09-27 18:29:38 +00004681 case CallingConv::X86_FastCall:
Chris Lattnerff0598d2007-04-17 17:21:52 +00004682 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner3ed3be32007-02-28 06:05:16 +00004683 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00004684 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00004685}
4686
Evan Chenga9467aa2006-04-25 20:13:52 +00004687SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4688 SDOperand InFlag(0, 0);
4689 SDOperand Chain = Op.getOperand(0);
4690 unsigned Align =
4691 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4692 if (Align == 0) Align = 1;
4693
4694 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindolaff332412007-08-27 10:18:20 +00004695 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab6024612007-08-27 17:48:26 +00004696 // The libc version is likely to be faster for these cases. It can use the
4697 // address value and run time information about the CPU.
Evan Chenga9467aa2006-04-25 20:13:52 +00004698 if ((Align & 3) != 0 ||
Rafael Espindola063f1772007-10-31 11:52:06 +00004699 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004700 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00004701 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00004702 TargetLowering::ArgListTy Args;
4703 TargetLowering::ArgListEntry Entry;
4704 Entry.Node = Op.getOperand(1);
4705 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00004706 Args.push_back(Entry);
Reid Spencere87b5e92007-01-03 17:24:59 +00004707 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencere63b6512006-12-31 05:55:36 +00004708 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4709 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00004710 Args.push_back(Entry);
4711 Entry.Node = Op.getOperand(3);
4712 Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00004713 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00004714 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00004715 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4716 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00004717 }
Evan Chengd097e672006-03-22 02:53:00 +00004718
Evan Chenga9467aa2006-04-25 20:13:52 +00004719 MVT::ValueType AVT;
4720 SDOperand Count;
4721 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4722 unsigned BytesLeft = 0;
4723 bool TwoRepStos = false;
4724 if (ValC) {
4725 unsigned ValReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004726 uint64_t Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00004727
Evan Chenga9467aa2006-04-25 20:13:52 +00004728 // If the value is a constant, then we can potentially use larger sets.
4729 switch (Align & 3) {
4730 case 2: // WORD aligned
4731 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00004732 ValReg = X86::AX;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004733 Val = (Val << 8) | Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00004734 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004735 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00004736 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004737 ValReg = X86::EAX;
Evan Chenga9467aa2006-04-25 20:13:52 +00004738 Val = (Val << 8) | Val;
4739 Val = (Val << 16) | Val;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004740 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4741 AVT = MVT::i64;
4742 ValReg = X86::RAX;
4743 Val = (Val << 32) | Val;
4744 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004745 break;
4746 default: // Byte aligned
4747 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00004748 ValReg = X86::AL;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004749 Count = Op.getOperand(3);
Evan Chenga9467aa2006-04-25 20:13:52 +00004750 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00004751 }
4752
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004753 if (AVT > MVT::i8) {
4754 if (I) {
4755 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4756 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4757 BytesLeft = I->getValue() % UBytes;
4758 } else {
4759 assert(AVT >= MVT::i32 &&
4760 "Do not use rep;stos if not at least DWORD aligned");
4761 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4762 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4763 TwoRepStos = true;
4764 }
4765 }
4766
Evan Chenga9467aa2006-04-25 20:13:52 +00004767 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4768 InFlag);
4769 InFlag = Chain.getValue(1);
4770 } else {
4771 AVT = MVT::i8;
4772 Count = Op.getOperand(3);
4773 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4774 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00004775 }
Evan Chengb0461082006-04-24 18:01:45 +00004776
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004777 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4778 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004779 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004780 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4781 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004782 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00004783
Chris Lattnere56fef92007-02-25 06:40:16 +00004784 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00004785 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00004786 Ops.push_back(Chain);
4787 Ops.push_back(DAG.getValueType(AVT));
4788 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00004789 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengb0461082006-04-24 18:01:45 +00004790
Evan Chenga9467aa2006-04-25 20:13:52 +00004791 if (TwoRepStos) {
4792 InFlag = Chain.getValue(1);
4793 Count = Op.getOperand(3);
4794 MVT::ValueType CVT = Count.getValueType();
4795 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004796 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4797 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4798 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004799 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00004800 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004801 Ops.clear();
4802 Ops.push_back(Chain);
4803 Ops.push_back(DAG.getValueType(MVT::i8));
4804 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00004805 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004806 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004807 // Issue stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00004808 SDOperand Value;
4809 unsigned Val = ValC->getValue() & 255;
4810 unsigned Offset = I->getValue() - BytesLeft;
4811 SDOperand DstAddr = Op.getOperand(1);
4812 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004813 if (BytesLeft >= 4) {
4814 Val = (Val << 8) | Val;
4815 Val = (Val << 16) | Val;
4816 Value = DAG.getConstant(Val, MVT::i32);
Evan Chengdf9ac472006-10-05 23:01:46 +00004817 Chain = DAG.getStore(Chain, Value,
4818 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4819 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004820 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004821 BytesLeft -= 4;
4822 Offset += 4;
4823 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004824 if (BytesLeft >= 2) {
4825 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Chengdf9ac472006-10-05 23:01:46 +00004826 Chain = DAG.getStore(Chain, Value,
4827 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4828 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004829 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004830 BytesLeft -= 2;
4831 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00004832 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004833 if (BytesLeft == 1) {
4834 Value = DAG.getConstant(Val, MVT::i8);
Evan Chengdf9ac472006-10-05 23:01:46 +00004835 Chain = DAG.getStore(Chain, Value,
4836 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4837 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004838 NULL, 0);
Evan Cheng14215c32006-04-21 23:03:30 +00004839 }
Evan Cheng082c8782006-03-24 07:29:27 +00004840 }
Evan Chengebf10062006-04-03 20:53:28 +00004841
Evan Chenga9467aa2006-04-25 20:13:52 +00004842 return Chain;
4843}
Evan Chengebf10062006-04-03 20:53:28 +00004844
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004845SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4846 SDOperand Dest,
4847 SDOperand Source,
4848 unsigned Size,
4849 unsigned Align,
4850 SelectionDAG &DAG) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004851 MVT::ValueType AVT;
Evan Chenga9467aa2006-04-25 20:13:52 +00004852 unsigned BytesLeft = 0;
Evan Chenga9467aa2006-04-25 20:13:52 +00004853 switch (Align & 3) {
4854 case 2: // WORD aligned
4855 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00004856 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004857 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00004858 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004859 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4860 AVT = MVT::i64;
Evan Chenga9467aa2006-04-25 20:13:52 +00004861 break;
4862 default: // Byte aligned
4863 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00004864 break;
4865 }
4866
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004867 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4868 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4869 BytesLeft = Size % UBytes;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004870
Evan Chenga9467aa2006-04-25 20:13:52 +00004871 SDOperand InFlag(0, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004872 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4873 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004874 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004875 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004876 Dest, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004877 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004878 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004879 Source, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004880 InFlag = Chain.getValue(1);
4881
Chris Lattnere56fef92007-02-25 06:40:16 +00004882 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00004883 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00004884 Ops.push_back(Chain);
4885 Ops.push_back(DAG.getValueType(AVT));
4886 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00004887 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004888
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004889 if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004890 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004891 unsigned Offset = Size - BytesLeft;
4892 SDOperand DstAddr = Dest;
Evan Chenga9467aa2006-04-25 20:13:52 +00004893 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004894 SDOperand SrcAddr = Source;
Evan Chenga9467aa2006-04-25 20:13:52 +00004895 MVT::ValueType SrcVT = SrcAddr.getValueType();
4896 SDOperand Value;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004897 if (BytesLeft >= 4) {
4898 Value = DAG.getLoad(MVT::i32, Chain,
4899 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4900 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004901 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004902 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00004903 Chain = DAG.getStore(Chain, Value,
4904 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4905 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004906 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004907 BytesLeft -= 4;
4908 Offset += 4;
4909 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004910 if (BytesLeft >= 2) {
4911 Value = DAG.getLoad(MVT::i16, Chain,
4912 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4913 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004914 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004915 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00004916 Chain = DAG.getStore(Chain, Value,
4917 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4918 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004919 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004920 BytesLeft -= 2;
4921 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00004922 }
4923
Evan Chenga9467aa2006-04-25 20:13:52 +00004924 if (BytesLeft == 1) {
4925 Value = DAG.getLoad(MVT::i8, Chain,
4926 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4927 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004928 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004929 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00004930 Chain = DAG.getStore(Chain, Value,
4931 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4932 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004933 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004934 }
Evan Chengcbffa462006-03-31 19:22:53 +00004935 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004936
4937 return Chain;
4938}
4939
Chris Lattnerf81d5882007-11-24 07:07:01 +00004940/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4941SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnere56fef92007-02-25 06:40:16 +00004942 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004943 SDOperand TheChain = N->getOperand(0);
4944 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00004945 if (Subtarget->is64Bit()) {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004946 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4947 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4948 MVT::i64, rax.getValue(2));
4949 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng28a9e9b2006-11-29 08:28:13 +00004950 DAG.getConstant(32, MVT::i8));
Chris Lattner35a08552007-02-25 07:10:00 +00004951 SDOperand Ops[] = {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004952 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner35a08552007-02-25 07:10:00 +00004953 };
Chris Lattnere56fef92007-02-25 06:40:16 +00004954
4955 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004956 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng28a9e9b2006-11-29 08:28:13 +00004957 }
Chris Lattner35a08552007-02-25 07:10:00 +00004958
Chris Lattnerf81d5882007-11-24 07:07:01 +00004959 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4960 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4961 MVT::i32, eax.getValue(2));
4962 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4963 SDOperand Ops[] = { eax, edx };
4964 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4965
4966 // Use a MERGE_VALUES to return the value and chain.
4967 Ops[1] = edx.getValue(1);
4968 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4969 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00004970}
4971
4972SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Chengab51cf22006-10-13 21:14:26 +00004973 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4974
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004975 if (!Subtarget->is64Bit()) {
4976 // vastart just stores the address of the VarArgsFrameIndex slot into the
4977 // memory location argument.
4978 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00004979 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4980 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004981 }
4982
4983 // __va_list_tag:
4984 // gp_offset (0 - 6 * 8)
4985 // fp_offset (48 - 48 + 8 * 16)
4986 // overflow_arg_area (point to parameters coming in memory).
4987 // reg_save_area
Chris Lattner35a08552007-02-25 07:10:00 +00004988 SmallVector<SDOperand, 8> MemOps;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004989 SDOperand FIN = Op.getOperand(1);
4990 // Store gp_offset
Evan Chengdf9ac472006-10-05 23:01:46 +00004991 SDOperand Store = DAG.getStore(Op.getOperand(0),
4992 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00004993 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004994 MemOps.push_back(Store);
4995
4996 // Store fp_offset
4997 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4998 DAG.getConstant(4, getPointerTy()));
Evan Chengdf9ac472006-10-05 23:01:46 +00004999 Store = DAG.getStore(Op.getOperand(0),
5000 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00005001 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00005002 MemOps.push_back(Store);
5003
5004 // Store ptr to overflow_arg_area
5005 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5006 DAG.getConstant(4, getPointerTy()));
5007 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00005008 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
5009 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00005010 MemOps.push_back(Store);
5011
5012 // Store ptr to reg_save_area.
5013 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5014 DAG.getConstant(8, getPointerTy()));
5015 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00005016 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
5017 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00005018 MemOps.push_back(Store);
5019 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00005020}
5021
Evan Chengdeaea252007-03-02 23:16:35 +00005022SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5023 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5024 SDOperand Chain = Op.getOperand(0);
5025 SDOperand DstPtr = Op.getOperand(1);
5026 SDOperand SrcPtr = Op.getOperand(2);
5027 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
5028 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
5029
5030 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
5031 SrcSV->getValue(), SrcSV->getOffset());
5032 Chain = SrcPtr.getValue(1);
5033 for (unsigned i = 0; i < 3; ++i) {
5034 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
5035 SrcSV->getValue(), SrcSV->getOffset());
5036 Chain = Val.getValue(1);
5037 Chain = DAG.getStore(Chain, Val, DstPtr,
5038 DstSV->getValue(), DstSV->getOffset());
5039 if (i == 2)
5040 break;
5041 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
5042 DAG.getConstant(8, getPointerTy()));
5043 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
5044 DAG.getConstant(8, getPointerTy()));
5045 }
5046 return Chain;
5047}
5048
Evan Chenga9467aa2006-04-25 20:13:52 +00005049SDOperand
5050X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5051 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5052 switch (IntNo) {
5053 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00005054 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00005055 case Intrinsic::x86_sse_comieq_ss:
5056 case Intrinsic::x86_sse_comilt_ss:
5057 case Intrinsic::x86_sse_comile_ss:
5058 case Intrinsic::x86_sse_comigt_ss:
5059 case Intrinsic::x86_sse_comige_ss:
5060 case Intrinsic::x86_sse_comineq_ss:
5061 case Intrinsic::x86_sse_ucomieq_ss:
5062 case Intrinsic::x86_sse_ucomilt_ss:
5063 case Intrinsic::x86_sse_ucomile_ss:
5064 case Intrinsic::x86_sse_ucomigt_ss:
5065 case Intrinsic::x86_sse_ucomige_ss:
5066 case Intrinsic::x86_sse_ucomineq_ss:
5067 case Intrinsic::x86_sse2_comieq_sd:
5068 case Intrinsic::x86_sse2_comilt_sd:
5069 case Intrinsic::x86_sse2_comile_sd:
5070 case Intrinsic::x86_sse2_comigt_sd:
5071 case Intrinsic::x86_sse2_comige_sd:
5072 case Intrinsic::x86_sse2_comineq_sd:
5073 case Intrinsic::x86_sse2_ucomieq_sd:
5074 case Intrinsic::x86_sse2_ucomilt_sd:
5075 case Intrinsic::x86_sse2_ucomile_sd:
5076 case Intrinsic::x86_sse2_ucomigt_sd:
5077 case Intrinsic::x86_sse2_ucomige_sd:
5078 case Intrinsic::x86_sse2_ucomineq_sd: {
5079 unsigned Opc = 0;
5080 ISD::CondCode CC = ISD::SETCC_INVALID;
5081 switch (IntNo) {
5082 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005083 case Intrinsic::x86_sse_comieq_ss:
5084 case Intrinsic::x86_sse2_comieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005085 Opc = X86ISD::COMI;
5086 CC = ISD::SETEQ;
5087 break;
Evan Cheng78038292006-04-05 23:38:46 +00005088 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005089 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005090 Opc = X86ISD::COMI;
5091 CC = ISD::SETLT;
5092 break;
5093 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005094 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005095 Opc = X86ISD::COMI;
5096 CC = ISD::SETLE;
5097 break;
5098 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005099 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005100 Opc = X86ISD::COMI;
5101 CC = ISD::SETGT;
5102 break;
5103 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005104 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005105 Opc = X86ISD::COMI;
5106 CC = ISD::SETGE;
5107 break;
5108 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005109 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005110 Opc = X86ISD::COMI;
5111 CC = ISD::SETNE;
5112 break;
5113 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005114 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005115 Opc = X86ISD::UCOMI;
5116 CC = ISD::SETEQ;
5117 break;
5118 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005119 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005120 Opc = X86ISD::UCOMI;
5121 CC = ISD::SETLT;
5122 break;
5123 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005124 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005125 Opc = X86ISD::UCOMI;
5126 CC = ISD::SETLE;
5127 break;
5128 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005129 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005130 Opc = X86ISD::UCOMI;
5131 CC = ISD::SETGT;
5132 break;
5133 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00005134 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00005135 Opc = X86ISD::UCOMI;
5136 CC = ISD::SETGE;
5137 break;
5138 case Intrinsic::x86_sse_ucomineq_ss:
5139 case Intrinsic::x86_sse2_ucomineq_sd:
5140 Opc = X86ISD::UCOMI;
5141 CC = ISD::SETNE;
5142 break;
Evan Cheng78038292006-04-05 23:38:46 +00005143 }
Evan Cheng4259a0f2006-09-11 02:19:56 +00005144
Evan Chenga9467aa2006-04-25 20:13:52 +00005145 unsigned X86CC;
Chris Lattner7a627672006-09-13 03:22:10 +00005146 SDOperand LHS = Op.getOperand(1);
5147 SDOperand RHS = Op.getOperand(2);
5148 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00005149
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00005150 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5151 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5152 DAG.getConstant(X86CC, MVT::i8), Cond);
5153 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00005154 }
Evan Cheng5c59d492005-12-23 07:31:11 +00005155 }
Chris Lattner76ac0682005-11-15 00:40:23 +00005156}
Evan Cheng6af02632005-12-20 06:22:03 +00005157
Nate Begemaneda59972007-01-29 22:58:52 +00005158SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5159 // Depths > 0 not supported yet!
5160 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5161 return SDOperand();
5162
5163 // Just load the return address
5164 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5165 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5166}
5167
5168SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5169 // Depths > 0 not supported yet!
5170 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5171 return SDOperand();
5172
5173 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5174 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5175 DAG.getConstant(4, getPointerTy()));
5176}
5177
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005178SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5179 SelectionDAG &DAG) {
5180 // Is not yet supported on x86-64
5181 if (Subtarget->is64Bit())
5182 return SDOperand();
5183
5184 return DAG.getConstant(8, getPointerTy());
5185}
5186
5187SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5188{
5189 assert(!Subtarget->is64Bit() &&
5190 "Lowering of eh_return builtin is not supported yet on x86-64");
5191
5192 MachineFunction &MF = DAG.getMachineFunction();
5193 SDOperand Chain = Op.getOperand(0);
5194 SDOperand Offset = Op.getOperand(1);
5195 SDOperand Handler = Op.getOperand(2);
5196
5197 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5198 getPointerTy());
5199
5200 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5201 DAG.getConstant(-4UL, getPointerTy()));
5202 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5203 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5204 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5205 MF.addLiveOut(X86::ECX);
5206
5207 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5208 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5209}
5210
Duncan Sandsce388532007-07-27 20:02:49 +00005211SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5212 SelectionDAG &DAG) {
5213 SDOperand Root = Op.getOperand(0);
5214 SDOperand Trmp = Op.getOperand(1); // trampoline
5215 SDOperand FPtr = Op.getOperand(2); // nested function
5216 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5217
5218 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
5219
5220 if (Subtarget->is64Bit()) {
5221 return SDOperand(); // not yet supported
5222 } else {
5223 Function *Func = (Function *)
5224 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5225 unsigned CC = Func->getCallingConv();
Duncan Sands77414272007-08-29 19:01:20 +00005226 unsigned NestReg;
Duncan Sandsce388532007-07-27 20:02:49 +00005227
5228 switch (CC) {
5229 default:
5230 assert(0 && "Unsupported calling convention");
5231 case CallingConv::C:
Duncan Sandsce388532007-07-27 20:02:49 +00005232 case CallingConv::X86_StdCall: {
5233 // Pass 'nest' parameter in ECX.
5234 // Must be kept in sync with X86CallingConv.td
Duncan Sands77414272007-08-29 19:01:20 +00005235 NestReg = X86::ECX;
Duncan Sandsce388532007-07-27 20:02:49 +00005236
5237 // Check that ECX wasn't needed by an 'inreg' parameter.
5238 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00005239 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsce388532007-07-27 20:02:49 +00005240
5241 if (Attrs && !Func->isVarArg()) {
5242 unsigned InRegCount = 0;
5243 unsigned Idx = 1;
5244
5245 for (FunctionType::param_iterator I = FTy->param_begin(),
5246 E = FTy->param_end(); I != E; ++I, ++Idx)
5247 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5248 // FIXME: should only count parameters that are lowered to integers.
5249 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5250
5251 if (InRegCount > 2) {
5252 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5253 abort();
5254 }
5255 }
5256 break;
5257 }
5258 case CallingConv::X86_FastCall:
5259 // Pass 'nest' parameter in EAX.
5260 // Must be kept in sync with X86CallingConv.td
Duncan Sands77414272007-08-29 19:01:20 +00005261 NestReg = X86::EAX;
Duncan Sandsce388532007-07-27 20:02:49 +00005262 break;
5263 }
5264
Duncan Sands77414272007-08-29 19:01:20 +00005265 const X86InstrInfo *TII =
5266 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5267
Duncan Sandsce388532007-07-27 20:02:49 +00005268 SDOperand OutChains[4];
5269 SDOperand Addr, Disp;
5270
5271 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5272 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5273
Duncan Sands77414272007-08-29 19:01:20 +00005274 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5275 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
5276 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsce388532007-07-27 20:02:49 +00005277 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
5278
5279 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5280 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
5281 TrmpSV->getOffset() + 1, false, 1);
5282
Duncan Sands77414272007-08-29 19:01:20 +00005283 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsce388532007-07-27 20:02:49 +00005284 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5285 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5286 TrmpSV->getValue() + 5, TrmpSV->getOffset());
5287
5288 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5289 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
5290 TrmpSV->getOffset() + 6, false, 1);
5291
Duncan Sands86e01192007-09-11 14:10:23 +00005292 SDOperand Ops[] =
5293 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5294 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsce388532007-07-27 20:02:49 +00005295 }
5296}
5297
Anton Korobeynikov91460e42007-11-16 01:31:51 +00005298SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
5299 /*
5300 The rounding mode is in bits 11:10 of FPSR, and has the following
5301 settings:
5302 00 Round to nearest
5303 01 Round to -inf
5304 10 Round to +inf
5305 11 Round to 0
5306
5307 FLT_ROUNDS, on the other hand, expects the following:
5308 -1 Undefined
5309 0 Round to 0
5310 1 Round to nearest
5311 2 Round to +inf
5312 3 Round to -inf
5313
5314 To perform the conversion, we do:
5315 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5316 */
5317
5318 MachineFunction &MF = DAG.getMachineFunction();
5319 const TargetMachine &TM = MF.getTarget();
5320 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5321 unsigned StackAlignment = TFI.getStackAlignment();
5322 MVT::ValueType VT = Op.getValueType();
5323
5324 // Save FP Control Word to stack slot
5325 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5326 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5327
5328 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5329 DAG.getEntryNode(), StackSlot);
5330
5331 // Load FP Control Word from stack slot
5332 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5333
5334 // Transform as necessary
5335 SDOperand CWD1 =
5336 DAG.getNode(ISD::SRL, MVT::i16,
5337 DAG.getNode(ISD::AND, MVT::i16,
5338 CWD, DAG.getConstant(0x800, MVT::i16)),
5339 DAG.getConstant(11, MVT::i8));
5340 SDOperand CWD2 =
5341 DAG.getNode(ISD::SRL, MVT::i16,
5342 DAG.getNode(ISD::AND, MVT::i16,
5343 CWD, DAG.getConstant(0x400, MVT::i16)),
5344 DAG.getConstant(9, MVT::i8));
5345
5346 SDOperand RetVal =
5347 DAG.getNode(ISD::AND, MVT::i16,
5348 DAG.getNode(ISD::ADD, MVT::i16,
5349 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5350 DAG.getConstant(1, MVT::i16)),
5351 DAG.getConstant(3, MVT::i16));
5352
5353
5354 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5355 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5356}
5357
Evan Chenga9467aa2006-04-25 20:13:52 +00005358/// LowerOperation - Provide custom lowering hooks for some operations.
5359///
5360SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5361 switch (Op.getOpcode()) {
5362 default: assert(0 && "Should not custom lower this!");
5363 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5364 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5365 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5366 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5367 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5368 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5369 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00005370 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005371 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5372 case ISD::SHL_PARTS:
5373 case ISD::SRA_PARTS:
5374 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5375 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5376 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5377 case ISD::FABS: return LowerFABS(Op, DAG);
5378 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng4363e882007-01-05 07:55:56 +00005379 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00005380 case ISD::SETCC: return LowerSETCC(Op, DAG);
5381 case ISD::SELECT: return LowerSELECT(Op, DAG);
5382 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005383 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00005384 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005385 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00005386 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005387 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5388 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005389 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengdeaea252007-03-02 23:16:35 +00005390 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005391 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemaneda59972007-01-29 22:58:52 +00005392 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5393 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005394 case ISD::FRAME_TO_ARGS_OFFSET:
5395 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00005396 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005397 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsce388532007-07-27 20:02:49 +00005398 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikov91460e42007-11-16 01:31:51 +00005399 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Chris Lattnerf81d5882007-11-24 07:07:01 +00005400
5401
5402 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5403 case ISD::READCYCLECOUNTER:
5404 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00005405 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005406}
5407
5408/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5409SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5410 switch (N->getOpcode()) {
5411 default: assert(0 && "Should not custom lower this!");
5412 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5413 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5414 }
Evan Chenga9467aa2006-04-25 20:13:52 +00005415}
5416
Evan Cheng6af02632005-12-20 06:22:03 +00005417const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5418 switch (Opcode) {
5419 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00005420 case X86ISD::SHLD: return "X86ISD::SHLD";
5421 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00005422 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng4363e882007-01-05 07:55:56 +00005423 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng72d5c252006-01-31 22:28:30 +00005424 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng4363e882007-01-05 07:55:56 +00005425 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Cheng6305e502006-01-12 22:54:21 +00005426 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00005427 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00005428 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5429 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5430 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00005431 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00005432 case X86ISD::FST: return "X86ISD::FST";
5433 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00005434 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00005435 case X86ISD::CALL: return "X86ISD::CALL";
5436 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5437 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5438 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng78038292006-04-05 23:38:46 +00005439 case X86ISD::COMI: return "X86ISD::COMI";
5440 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00005441 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00005442 case X86ISD::CMOV: return "X86ISD::CMOV";
5443 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00005444 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00005445 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5446 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng5588de92006-02-18 00:15:05 +00005447 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00005448 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00005449 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00005450 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00005451 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng49683ba2006-11-10 21:43:37 +00005452 case X86ISD::FMAX: return "X86ISD::FMAX";
5453 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman57111e72007-07-10 00:05:58 +00005454 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5455 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00005456 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5457 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005458 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00005459 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov91460e42007-11-16 01:31:51 +00005460 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng6af02632005-12-20 06:22:03 +00005461 }
5462}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005463
Chris Lattner1eb94d92007-03-30 23:15:24 +00005464// isLegalAddressingMode - Return true if the addressing mode represented
5465// by AM is legal for this target, for a load/store of the specified type.
5466bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5467 const Type *Ty) const {
5468 // X86 supports extremely general addressing modes.
5469
5470 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5471 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5472 return false;
5473
5474 if (AM.BaseGV) {
Evan Chengd3d92892007-08-01 23:46:47 +00005475 // We can only fold this if we don't need an extra load.
Chris Lattner1eb94d92007-03-30 23:15:24 +00005476 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5477 return false;
Evan Chengd3d92892007-08-01 23:46:47 +00005478
5479 // X86-64 only supports addr of globals in small code model.
5480 if (Subtarget->is64Bit()) {
5481 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5482 return false;
5483 // If lower 4G is not available, then we must use rip-relative addressing.
5484 if (AM.BaseOffs || AM.Scale > 1)
5485 return false;
5486 }
Chris Lattner1eb94d92007-03-30 23:15:24 +00005487 }
5488
5489 switch (AM.Scale) {
5490 case 0:
5491 case 1:
5492 case 2:
5493 case 4:
5494 case 8:
5495 // These scales always work.
5496 break;
5497 case 3:
5498 case 5:
5499 case 9:
5500 // These scales are formed with basereg+scalereg. Only accept if there is
5501 // no basereg yet.
5502 if (AM.HasBaseReg)
5503 return false;
5504 break;
5505 default: // Other stuff never works.
5506 return false;
5507 }
5508
5509 return true;
5510}
5511
5512
Evan Cheng7f3d0242007-10-26 01:56:11 +00005513bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5514 if (!Ty1->isInteger() || !Ty2->isInteger())
5515 return false;
Evan Cheng7b3f7fe2007-10-29 07:57:50 +00005516 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5517 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5518 if (NumBits1 <= NumBits2)
5519 return false;
5520 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng7f3d0242007-10-26 01:56:11 +00005521}
5522
Evan Chenge106e2f2007-10-29 19:58:20 +00005523bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5524 MVT::ValueType VT2) const {
5525 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5526 return false;
5527 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5528 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5529 if (NumBits1 <= NumBits2)
5530 return false;
5531 return Subtarget->is64Bit() || NumBits1 < 64;
5532}
Evan Cheng7f3d0242007-10-26 01:56:11 +00005533
Evan Cheng02612422006-07-05 22:17:51 +00005534/// isShuffleMaskLegal - Targets can use this to indicate that they only
5535/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5536/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5537/// are assumed to be legal.
5538bool
5539X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5540 // Only do shuffles on 128-bit vector types for now.
5541 if (MVT::getSizeInBits(VT) == 64) return false;
5542 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengcea02ff2007-06-19 00:02:56 +00005543 isIdentityMask(Mask.Val) ||
5544 isIdentityMask(Mask.Val, true) ||
Evan Cheng02612422006-07-05 22:17:51 +00005545 isSplatMask(Mask.Val) ||
5546 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5547 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengcea02ff2007-06-19 00:02:56 +00005548 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng02612422006-07-05 22:17:51 +00005549 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Chengcea02ff2007-06-19 00:02:56 +00005550 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng02612422006-07-05 22:17:51 +00005551}
5552
5553bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5554 MVT::ValueType EVT,
5555 SelectionDAG &DAG) const {
5556 unsigned NumElts = BVOps.size();
5557 // Only do shuffles on 128-bit vector types for now.
5558 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5559 if (NumElts == 2) return true;
5560 if (NumElts == 4) {
Chris Lattner35a08552007-02-25 07:10:00 +00005561 return (isMOVLMask(&BVOps[0], 4) ||
5562 isCommutedMOVL(&BVOps[0], 4, true) ||
5563 isSHUFPMask(&BVOps[0], 4) ||
5564 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng02612422006-07-05 22:17:51 +00005565 }
5566 return false;
5567}
5568
5569//===----------------------------------------------------------------------===//
5570// X86 Scheduler Hooks
5571//===----------------------------------------------------------------------===//
5572
5573MachineBasicBlock *
5574X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5575 MachineBasicBlock *BB) {
Evan Cheng20350c42006-11-27 23:37:22 +00005576 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng02612422006-07-05 22:17:51 +00005577 switch (MI->getOpcode()) {
5578 default: assert(false && "Unexpected instr type to insert");
5579 case X86::CMOV_FR32:
5580 case X86::CMOV_FR64:
5581 case X86::CMOV_V4F32:
5582 case X86::CMOV_V2F64:
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00005583 case X86::CMOV_V2I64: {
Evan Cheng02612422006-07-05 22:17:51 +00005584 // To "insert" a SELECT_CC instruction, we actually have to insert the
5585 // diamond control-flow pattern. The incoming instruction knows the
5586 // destination vreg to set, the condition code register to branch on, the
5587 // true/false values to select between, and a branch opcode to use.
5588 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5589 ilist<MachineBasicBlock>::iterator It = BB;
5590 ++It;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005591
Evan Cheng02612422006-07-05 22:17:51 +00005592 // thisMBB:
5593 // ...
5594 // TrueVal = ...
5595 // cmpTY ccX, r1, r2
5596 // bCC copy1MBB
5597 // fallthrough --> copy0MBB
5598 MachineBasicBlock *thisMBB = BB;
5599 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5600 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005601 unsigned Opc =
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005602 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Cheng20350c42006-11-27 23:37:22 +00005603 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng02612422006-07-05 22:17:51 +00005604 MachineFunction *F = BB->getParent();
5605 F->getBasicBlockList().insert(It, copy0MBB);
5606 F->getBasicBlockList().insert(It, sinkMBB);
5607 // Update machine-CFG edges by first adding all successors of the current
5608 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005609 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng02612422006-07-05 22:17:51 +00005610 e = BB->succ_end(); i != e; ++i)
5611 sinkMBB->addSuccessor(*i);
5612 // Next, remove all successors of the current block, and add the true
5613 // and fallthrough blocks as its successors.
5614 while(!BB->succ_empty())
5615 BB->removeSuccessor(BB->succ_begin());
5616 BB->addSuccessor(copy0MBB);
5617 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005618
Evan Cheng02612422006-07-05 22:17:51 +00005619 // copy0MBB:
5620 // %FalseValue = ...
5621 // # fallthrough to sinkMBB
5622 BB = copy0MBB;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005623
Evan Cheng02612422006-07-05 22:17:51 +00005624 // Update machine-CFG edges
5625 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005626
Evan Cheng02612422006-07-05 22:17:51 +00005627 // sinkMBB:
5628 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5629 // ...
5630 BB = sinkMBB;
Evan Cheng20350c42006-11-27 23:37:22 +00005631 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng02612422006-07-05 22:17:51 +00005632 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5633 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5634
5635 delete MI; // The pseudo instruction is gone now.
5636 return BB;
5637 }
5638
Dale Johannesena2b3c172007-07-03 00:53:03 +00005639 case X86::FP32_TO_INT16_IN_MEM:
5640 case X86::FP32_TO_INT32_IN_MEM:
5641 case X86::FP32_TO_INT64_IN_MEM:
5642 case X86::FP64_TO_INT16_IN_MEM:
5643 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +00005644 case X86::FP64_TO_INT64_IN_MEM:
5645 case X86::FP80_TO_INT16_IN_MEM:
5646 case X86::FP80_TO_INT32_IN_MEM:
5647 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng02612422006-07-05 22:17:51 +00005648 // Change the floating point control register to use "round towards zero"
5649 // mode when truncating to an integer value.
5650 MachineFunction *F = BB->getParent();
5651 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Cheng20350c42006-11-27 23:37:22 +00005652 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005653
5654 // Load the old value of the high byte of the control word...
5655 unsigned OldCW =
5656 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng20350c42006-11-27 23:37:22 +00005657 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005658
5659 // Set the high part to be round to zero...
Evan Cheng20350c42006-11-27 23:37:22 +00005660 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5661 .addImm(0xC7F);
Evan Cheng02612422006-07-05 22:17:51 +00005662
5663 // Reload the modified control word now...
Evan Cheng20350c42006-11-27 23:37:22 +00005664 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005665
5666 // Restore the memory image of control word to original value
Evan Cheng20350c42006-11-27 23:37:22 +00005667 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5668 .addReg(OldCW);
Evan Cheng02612422006-07-05 22:17:51 +00005669
5670 // Get the X86 opcode to use.
5671 unsigned Opc;
5672 switch (MI->getOpcode()) {
5673 default: assert(0 && "illegal opcode!");
Dale Johannesen3d7008c2007-07-04 21:07:47 +00005674 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5675 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5676 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5677 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5678 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5679 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +00005680 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5681 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5682 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng02612422006-07-05 22:17:51 +00005683 }
5684
5685 X86AddressMode AM;
5686 MachineOperand &Op = MI->getOperand(0);
5687 if (Op.isRegister()) {
5688 AM.BaseType = X86AddressMode::RegBase;
5689 AM.Base.Reg = Op.getReg();
5690 } else {
5691 AM.BaseType = X86AddressMode::FrameIndexBase;
5692 AM.Base.FrameIndex = Op.getFrameIndex();
5693 }
5694 Op = MI->getOperand(1);
5695 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005696 AM.Scale = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00005697 Op = MI->getOperand(2);
5698 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005699 AM.IndexReg = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00005700 Op = MI->getOperand(3);
5701 if (Op.isGlobalAddress()) {
5702 AM.GV = Op.getGlobal();
5703 } else {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005704 AM.Disp = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00005705 }
Evan Cheng20350c42006-11-27 23:37:22 +00005706 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5707 .addReg(MI->getOperand(4).getReg());
Evan Cheng02612422006-07-05 22:17:51 +00005708
5709 // Reload the original control word now.
Evan Cheng20350c42006-11-27 23:37:22 +00005710 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005711
5712 delete MI; // The pseudo instruction is gone now.
5713 return BB;
5714 }
5715 }
5716}
5717
5718//===----------------------------------------------------------------------===//
5719// X86 Optimization Hooks
5720//===----------------------------------------------------------------------===//
5721
Nate Begeman8a77efe2006-02-16 21:11:51 +00005722void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5723 uint64_t Mask,
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005724 uint64_t &KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +00005725 uint64_t &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00005726 const SelectionDAG &DAG,
Nate Begeman8a77efe2006-02-16 21:11:51 +00005727 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005728 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00005729 assert((Opc >= ISD::BUILTIN_OP_END ||
5730 Opc == ISD::INTRINSIC_WO_CHAIN ||
5731 Opc == ISD::INTRINSIC_W_CHAIN ||
5732 Opc == ISD::INTRINSIC_VOID) &&
5733 "Should use MaskedValueIsZero if you don't know whether Op"
5734 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005735
Evan Cheng6d196db2006-04-05 06:11:20 +00005736 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005737 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00005738 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005739 case X86ISD::SETCC:
Nate Begeman8a77efe2006-02-16 21:11:51 +00005740 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5741 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005742 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005743}
Chris Lattnerc642aa52006-01-31 19:43:35 +00005744
Evan Cheng5987cfb2006-07-07 08:33:52 +00005745/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5746/// element of the result of the vector shuffle.
5747static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5748 MVT::ValueType VT = N->getValueType(0);
5749 SDOperand PermMask = N->getOperand(2);
5750 unsigned NumElems = PermMask.getNumOperands();
5751 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5752 i %= NumElems;
5753 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5754 return (i == 0)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00005755 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng5987cfb2006-07-07 08:33:52 +00005756 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5757 SDOperand Idx = PermMask.getOperand(i);
5758 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman5c441312007-06-14 22:58:02 +00005759 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng5987cfb2006-07-07 08:33:52 +00005760 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5761 }
5762 return SDOperand();
5763}
5764
5765/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5766/// node is a GlobalAddress + an offset.
5767static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Chengae1cd752006-11-30 21:55:46 +00005768 unsigned Opc = N->getOpcode();
Evan Cheng62cdc3f2006-12-05 04:01:03 +00005769 if (Opc == X86ISD::Wrapper) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005770 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5771 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5772 return true;
5773 }
Evan Chengae1cd752006-11-30 21:55:46 +00005774 } else if (Opc == ISD::ADD) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005775 SDOperand N1 = N->getOperand(0);
5776 SDOperand N2 = N->getOperand(1);
5777 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5778 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5779 if (V) {
5780 Offset += V->getSignExtended();
5781 return true;
5782 }
5783 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5784 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5785 if (V) {
5786 Offset += V->getSignExtended();
5787 return true;
5788 }
5789 }
5790 }
5791 return false;
5792}
5793
5794/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5795/// + Dist * Size.
5796static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5797 MachineFrameInfo *MFI) {
5798 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5799 return false;
5800
5801 SDOperand Loc = N->getOperand(1);
5802 SDOperand BaseLoc = Base->getOperand(1);
5803 if (Loc.getOpcode() == ISD::FrameIndex) {
5804 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5805 return false;
Dan Gohmanb6a8ae22007-07-23 20:24:29 +00005806 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5807 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng5987cfb2006-07-07 08:33:52 +00005808 int FS = MFI->getObjectSize(FI);
5809 int BFS = MFI->getObjectSize(BFI);
5810 if (FS != BFS || FS != Size) return false;
5811 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5812 } else {
5813 GlobalValue *GV1 = NULL;
5814 GlobalValue *GV2 = NULL;
5815 int64_t Offset1 = 0;
5816 int64_t Offset2 = 0;
5817 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5818 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5819 if (isGA1 && isGA2 && GV1 == GV2)
5820 return Offset1 == (Offset2 + Dist*Size);
5821 }
5822
5823 return false;
5824}
5825
Evan Cheng79cf9a52006-07-10 21:37:44 +00005826static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5827 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005828 GlobalValue *GV;
5829 int64_t Offset;
5830 if (isGAPlusOffset(Base, GV, Offset))
5831 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5832 else {
5833 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohmanb6a8ae22007-07-23 20:24:29 +00005834 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng79cf9a52006-07-10 21:37:44 +00005835 if (BFI < 0)
5836 // Fixed objects do not specify alignment, however the offsets are known.
5837 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5838 (MFI->getObjectOffset(BFI) % 16) == 0);
5839 else
5840 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng5987cfb2006-07-07 08:33:52 +00005841 }
5842 return false;
5843}
5844
5845
5846/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5847/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5848/// if the load addresses are consecutive, non-overlapping, and in the right
5849/// order.
Evan Cheng79cf9a52006-07-10 21:37:44 +00005850static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5851 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005852 MachineFunction &MF = DAG.getMachineFunction();
5853 MachineFrameInfo *MFI = MF.getFrameInfo();
5854 MVT::ValueType VT = N->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00005855 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng5987cfb2006-07-07 08:33:52 +00005856 SDOperand PermMask = N->getOperand(2);
5857 int NumElems = (int)PermMask.getNumOperands();
5858 SDNode *Base = NULL;
5859 for (int i = 0; i < NumElems; ++i) {
5860 SDOperand Idx = PermMask.getOperand(i);
5861 if (Idx.getOpcode() == ISD::UNDEF) {
5862 if (!Base) return SDOperand();
5863 } else {
5864 SDOperand Arg =
5865 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005866 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng5987cfb2006-07-07 08:33:52 +00005867 return SDOperand();
5868 if (!Base)
5869 Base = Arg.Val;
5870 else if (!isConsecutiveLoad(Arg.Val, Base,
5871 i, MVT::getSizeInBits(EVT)/8,MFI))
5872 return SDOperand();
5873 }
5874 }
5875
Evan Cheng79cf9a52006-07-10 21:37:44 +00005876 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman47885522007-07-27 17:16:43 +00005877 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005878 if (isAlign16) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005879 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman47885522007-07-27 17:16:43 +00005880 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chenge71fe34d2006-10-09 20:57:25 +00005881 } else {
Dan Gohman47885522007-07-27 17:16:43 +00005882 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5883 LD->getSrcValueOffset(), LD->isVolatile(),
5884 LD->getAlignment());
Evan Cheng5c68bba2006-08-11 07:35:45 +00005885 }
Evan Cheng5987cfb2006-07-07 08:33:52 +00005886}
5887
Chris Lattner9259b1e2006-10-04 06:57:07 +00005888/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5889static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5890 const X86Subtarget *Subtarget) {
5891 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005892
Chris Lattner9259b1e2006-10-04 06:57:07 +00005893 // If we have SSE[12] support, try to form min/max nodes.
5894 if (Subtarget->hasSSE2() &&
5895 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5896 if (Cond.getOpcode() == ISD::SETCC) {
5897 // Get the LHS/RHS of the select.
5898 SDOperand LHS = N->getOperand(1);
5899 SDOperand RHS = N->getOperand(2);
5900 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005901
Evan Cheng49683ba2006-11-10 21:43:37 +00005902 unsigned Opcode = 0;
Chris Lattner9259b1e2006-10-04 06:57:07 +00005903 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005904 switch (CC) {
5905 default: break;
5906 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5907 case ISD::SETULE:
5908 case ISD::SETLE:
5909 if (!UnsafeFPMath) break;
5910 // FALL THROUGH.
5911 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5912 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00005913 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005914 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005915
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005916 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5917 case ISD::SETUGT:
5918 case ISD::SETGT:
5919 if (!UnsafeFPMath) break;
5920 // FALL THROUGH.
5921 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5922 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00005923 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005924 break;
5925 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00005926 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005927 switch (CC) {
5928 default: break;
5929 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5930 case ISD::SETUGT:
5931 case ISD::SETGT:
5932 if (!UnsafeFPMath) break;
5933 // FALL THROUGH.
5934 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5935 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00005936 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005937 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005938
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005939 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5940 case ISD::SETULE:
5941 case ISD::SETLE:
5942 if (!UnsafeFPMath) break;
5943 // FALL THROUGH.
5944 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5945 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00005946 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005947 break;
5948 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00005949 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005950
Evan Cheng49683ba2006-11-10 21:43:37 +00005951 if (Opcode)
5952 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner9259b1e2006-10-04 06:57:07 +00005953 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005954
Chris Lattner9259b1e2006-10-04 06:57:07 +00005955 }
5956
5957 return SDOperand();
5958}
5959
5960
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005961SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng5987cfb2006-07-07 08:33:52 +00005962 DAGCombinerInfo &DCI) const {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005963 SelectionDAG &DAG = DCI.DAG;
5964 switch (N->getOpcode()) {
5965 default: break;
5966 case ISD::VECTOR_SHUFFLE:
Evan Cheng79cf9a52006-07-10 21:37:44 +00005967 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner9259b1e2006-10-04 06:57:07 +00005968 case ISD::SELECT:
5969 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng5987cfb2006-07-07 08:33:52 +00005970 }
5971
5972 return SDOperand();
5973}
5974
Evan Cheng02612422006-07-05 22:17:51 +00005975//===----------------------------------------------------------------------===//
5976// X86 Inline Assembly Support
5977//===----------------------------------------------------------------------===//
5978
Chris Lattner298ef372006-07-11 02:54:03 +00005979/// getConstraintType - Given a constraint letter, return the type of
5980/// constraint it is for this target.
5981X86TargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00005982X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5983 if (Constraint.size() == 1) {
5984 switch (Constraint[0]) {
5985 case 'A':
5986 case 'r':
5987 case 'R':
5988 case 'l':
5989 case 'q':
5990 case 'Q':
5991 case 'x':
5992 case 'Y':
5993 return C_RegisterClass;
5994 default:
5995 break;
5996 }
Chris Lattner298ef372006-07-11 02:54:03 +00005997 }
Chris Lattnerd6855142007-03-25 02:14:49 +00005998 return TargetLowering::getConstraintType(Constraint);
Chris Lattner298ef372006-07-11 02:54:03 +00005999}
6000
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006001/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6002/// vector. If it is invalid, don't add anything to Ops.
6003void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6004 char Constraint,
6005 std::vector<SDOperand>&Ops,
6006 SelectionDAG &DAG) {
6007 SDOperand Result(0, 0);
6008
Chris Lattner44daa502006-10-31 20:13:11 +00006009 switch (Constraint) {
6010 default: break;
Devang Patelb38c2ec2007-03-17 00:13:28 +00006011 case 'I':
Chris Lattner03a643a2007-03-25 01:57:35 +00006012 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006013 if (C->getValue() <= 31) {
6014 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6015 break;
6016 }
Devang Patelb38c2ec2007-03-17 00:13:28 +00006017 }
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006018 return;
Chris Lattner03a643a2007-03-25 01:57:35 +00006019 case 'N':
6020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006021 if (C->getValue() <= 255) {
6022 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6023 break;
6024 }
Chris Lattner03a643a2007-03-25 01:57:35 +00006025 }
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006026 return;
Chris Lattner83df45a2007-05-03 16:52:29 +00006027 case 'i': {
Chris Lattner44daa502006-10-31 20:13:11 +00006028 // Literal immediates are always ok.
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006029 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6030 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6031 break;
6032 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006033
Chris Lattner83df45a2007-05-03 16:52:29 +00006034 // If we are in non-pic codegen mode, we allow the address of a global (with
6035 // an optional displacement) to be used with 'i'.
6036 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6037 int64_t Offset = 0;
6038
6039 // Match either (GA) or (GA+C)
6040 if (GA) {
6041 Offset = GA->getOffset();
6042 } else if (Op.getOpcode() == ISD::ADD) {
6043 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6044 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6045 if (C && GA) {
6046 Offset = GA->getOffset()+C->getValue();
6047 } else {
6048 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6049 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6050 if (C && GA)
6051 Offset = GA->getOffset()+C->getValue();
6052 else
6053 C = 0, GA = 0;
6054 }
6055 }
6056
6057 if (GA) {
6058 // If addressing this global requires a load (e.g. in PIC mode), we can't
6059 // match.
6060 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6061 false))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006062 return;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006063
Chris Lattner83df45a2007-05-03 16:52:29 +00006064 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6065 Offset);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006066 Result = Op;
6067 break;
Chris Lattner44daa502006-10-31 20:13:11 +00006068 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006069
Chris Lattner44daa502006-10-31 20:13:11 +00006070 // Otherwise, not valid for this mode.
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006071 return;
Chris Lattner44daa502006-10-31 20:13:11 +00006072 }
Chris Lattner83df45a2007-05-03 16:52:29 +00006073 }
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006074
6075 if (Result.Val) {
6076 Ops.push_back(Result);
6077 return;
6078 }
6079 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner44daa502006-10-31 20:13:11 +00006080}
6081
Chris Lattnerc642aa52006-01-31 19:43:35 +00006082std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00006083getRegClassForInlineAsmConstraint(const std::string &Constraint,
6084 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00006085 if (Constraint.size() == 1) {
6086 // FIXME: not handling fp-stack yet!
Chris Lattnerc642aa52006-01-31 19:43:35 +00006087 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattner298ef372006-07-11 02:54:03 +00006088 default: break; // Unknown constraint letter
6089 case 'A': // EAX/EDX
6090 if (VT == MVT::i32 || VT == MVT::i64)
6091 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6092 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00006093 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6094 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00006095 if (VT == MVT::i32)
6096 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6097 else if (VT == MVT::i16)
6098 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6099 else if (VT == MVT::i8)
Evan Chengb2823da2007-08-13 23:27:11 +00006100 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner296160d2007-11-04 06:51:12 +00006101 else if (VT == MVT::i64)
6102 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6103 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00006104 }
6105 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006106
Chris Lattner7ad77df2006-02-22 00:56:39 +00006107 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00006108}
Chris Lattner524129d2006-07-31 23:26:50 +00006109
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006110std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner524129d2006-07-31 23:26:50 +00006111X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6112 MVT::ValueType VT) const {
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006113 // First, see if this is a constraint that directly corresponds to an LLVM
6114 // register class.
6115 if (Constraint.size() == 1) {
6116 // GCC Constraint Letters
6117 switch (Constraint[0]) {
6118 default: break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00006119 case 'r': // GENERAL_REGS
6120 case 'R': // LEGACY_REGS
6121 case 'l': // INDEX_REGS
6122 if (VT == MVT::i64 && Subtarget->is64Bit())
6123 return std::make_pair(0U, X86::GR64RegisterClass);
6124 if (VT == MVT::i32)
6125 return std::make_pair(0U, X86::GR32RegisterClass);
6126 else if (VT == MVT::i16)
6127 return std::make_pair(0U, X86::GR16RegisterClass);
6128 else if (VT == MVT::i8)
6129 return std::make_pair(0U, X86::GR8RegisterClass);
6130 break;
Chris Lattner2805bce2007-04-12 04:14:49 +00006131 case 'y': // MMX_REGS if MMX allowed.
6132 if (!Subtarget->hasMMX()) break;
6133 return std::make_pair(0U, X86::VR64RegisterClass);
6134 break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00006135 case 'Y': // SSE_REGS if SSE2 allowed
6136 if (!Subtarget->hasSSE2()) break;
6137 // FALL THROUGH.
6138 case 'x': // SSE_REGS if SSE1 allowed
6139 if (!Subtarget->hasSSE1()) break;
6140
6141 switch (VT) {
6142 default: break;
6143 // Scalar SSE types.
6144 case MVT::f32:
6145 case MVT::i32:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006146 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00006147 case MVT::f64:
6148 case MVT::i64:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006149 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00006150 // Vector types.
Chris Lattner7451e4d2007-04-09 05:49:22 +00006151 case MVT::v16i8:
6152 case MVT::v8i16:
6153 case MVT::v4i32:
6154 case MVT::v2i64:
6155 case MVT::v4f32:
6156 case MVT::v2f64:
6157 return std::make_pair(0U, X86::VR128RegisterClass);
6158 }
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006159 break;
6160 }
6161 }
6162
Chris Lattner524129d2006-07-31 23:26:50 +00006163 // Use the default implementation in TargetLowering to convert the register
6164 // constraint into a member of a register class.
6165 std::pair<unsigned, const TargetRegisterClass*> Res;
6166 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerf6a69662006-10-31 19:42:44 +00006167
6168 // Not found as a standard register?
6169 if (Res.second == 0) {
6170 // GCC calls "st(0)" just plain "st".
6171 if (StringsEqualNoCase("{st}", Constraint)) {
6172 Res.first = X86::ST0;
Chris Lattner5b5484d2007-09-24 05:27:37 +00006173 Res.second = X86::RFP80RegisterClass;
Chris Lattnerf6a69662006-10-31 19:42:44 +00006174 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006175
Chris Lattnerf6a69662006-10-31 19:42:44 +00006176 return Res;
6177 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006178
Chris Lattner524129d2006-07-31 23:26:50 +00006179 // Otherwise, check to see if this is a register class of the wrong value
6180 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6181 // turn into {ax},{dx}.
6182 if (Res.second->hasType(VT))
6183 return Res; // Correct type already, nothing to do.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006184
Chris Lattner524129d2006-07-31 23:26:50 +00006185 // All of the single-register GCC register classes map their values onto
6186 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6187 // really want an 8-bit or 32-bit register, map to the appropriate register
6188 // class and return the appropriate register.
6189 if (Res.second != X86::GR16RegisterClass)
6190 return Res;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006191
Chris Lattner524129d2006-07-31 23:26:50 +00006192 if (VT == MVT::i8) {
6193 unsigned DestReg = 0;
6194 switch (Res.first) {
6195 default: break;
6196 case X86::AX: DestReg = X86::AL; break;
6197 case X86::DX: DestReg = X86::DL; break;
6198 case X86::CX: DestReg = X86::CL; break;
6199 case X86::BX: DestReg = X86::BL; break;
6200 }
6201 if (DestReg) {
6202 Res.first = DestReg;
6203 Res.second = Res.second = X86::GR8RegisterClass;
6204 }
6205 } else if (VT == MVT::i32) {
6206 unsigned DestReg = 0;
6207 switch (Res.first) {
6208 default: break;
6209 case X86::AX: DestReg = X86::EAX; break;
6210 case X86::DX: DestReg = X86::EDX; break;
6211 case X86::CX: DestReg = X86::ECX; break;
6212 case X86::BX: DestReg = X86::EBX; break;
6213 case X86::SI: DestReg = X86::ESI; break;
6214 case X86::DI: DestReg = X86::EDI; break;
6215 case X86::BP: DestReg = X86::EBP; break;
6216 case X86::SP: DestReg = X86::ESP; break;
6217 }
6218 if (DestReg) {
6219 Res.first = DestReg;
6220 Res.second = Res.second = X86::GR32RegisterClass;
6221 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00006222 } else if (VT == MVT::i64) {
6223 unsigned DestReg = 0;
6224 switch (Res.first) {
6225 default: break;
6226 case X86::AX: DestReg = X86::RAX; break;
6227 case X86::DX: DestReg = X86::RDX; break;
6228 case X86::CX: DestReg = X86::RCX; break;
6229 case X86::BX: DestReg = X86::RBX; break;
6230 case X86::SI: DestReg = X86::RSI; break;
6231 case X86::DI: DestReg = X86::RDI; break;
6232 case X86::BP: DestReg = X86::RBP; break;
6233 case X86::SP: DestReg = X86::RSP; break;
6234 }
6235 if (DestReg) {
6236 Res.first = DestReg;
6237 Res.second = Res.second = X86::GR64RegisterClass;
6238 }
Chris Lattner524129d2006-07-31 23:26:50 +00006239 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006240
Chris Lattner524129d2006-07-31 23:26:50 +00006241 return Res;
6242}