blob: b5e91ce71449493715d5d7caf5233989439d379d [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner76ac0682005-11-15 00:40:23 +00007//
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"
Evan Chengefd142a2008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000036#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng4fbf4592007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Chris Lattnerf6a69662006-10-31 19:42:44 +000041#include "llvm/ADT/StringExtras.h"
Duncan Sandsce388532007-07-27 20:02:49 +000042#include "llvm/ParameterAttributes.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000043using namespace llvm;
44
Chris Lattner76ac0682005-11-15 00:40:23 +000045X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000047 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene36c4002007-09-23 14:52:20 +000048 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000050 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +000051
Chris Lattnera91f77e2008-01-24 08:07:48 +000052 bool Fast = false;
Evan Chengcde9e302006-01-27 08:10:46 +000053
Anton Korobeynikov383a3242007-07-14 14:06:15 +000054 RegInfo = TM.getRegisterInfo();
55
Chris Lattner76ac0682005-11-15 00:40:23 +000056 // Set up the TargetLowering object.
57
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
60 setSetCCResultType(MVT::i8);
61 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000062 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000063 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng11b0a5d2006-09-08 06:48:29 +000064 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng20931a72006-03-16 21:47:42 +000065
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000066 if (Subtarget->isTargetDarwin()) {
Evan Chengb09a56f2006-03-17 20:31:41 +000067 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000068 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +000070 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000071 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
Chris Lattner76ac0682005-11-15 00:40:23 +000079 // Set up the register classes.
Evan Cheng9fee4422006-05-16 07:21:53 +000080 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000083 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +000085
Duncan Sands95d46ef2008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Cheng5d9fd972006-10-04 00:56:09 +000087
Chris Lattner1ea55cf2008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Chris Lattner76ac0682005-11-15 00:40:23 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +0000101
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng0d5b69f2006-01-17 02:32:49 +0000104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000105 } else {
Dale Johannesene36c4002007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000117 // SSE has no i16 to fp conversion, only i32
Dale Johannesene36c4002007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Evan Cheng08390f62006-01-30 22:13:22 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Evan Cheng593bea72006-02-17 07:01:52 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000126
Dale Johannesen7d67e542007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000131
Evan Cheng08390f62006-01-30 22:13:22 +0000132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesene36c4002007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Evan Cheng08390f62006-01-30 22:13:22 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000141 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000155 } else {
Dale Johannesene36c4002007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000165
Chris Lattner55c17f92006-12-05 18:22:22 +0000166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene36c4002007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Chris Lattnerc20b7e82006-12-05 18:45:06 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
Chris Lattner30107e62005-12-23 05:15:23 +0000171
Dan Gohmanc5892432008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohmana1603612007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohmana1603612007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohmana1603612007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohmana1603612007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman31599682007-09-25 18:23:27 +0000206
Evan Cheng0d41d192006-10-30 08:02:39 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng593bea72006-02-17 07:01:52 +0000208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000211 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000212 if (Subtarget->is64Bit())
Christopher Lambb372aba2007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattner76ac0682005-11-15 00:40:23 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Dan Gohman9ba4d762008-01-31 00:41:03 +0000219 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov91460e42007-11-16 01:31:51 +0000220
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Chenge9fbc3f2007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000224 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Chenge9fbc3f2007-12-14 02:13:44 +0000225 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
226 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000227 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Chenge9fbc3f2007-12-14 02:13:44 +0000228 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
229 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit()) {
231 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Chenge9fbc3f2007-12-14 02:13:44 +0000232 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
233 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000234 }
235
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000236 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000237 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000238
Chris Lattner76ac0682005-11-15 00:40:23 +0000239 // These should be promoted to a larger select which is supported.
240 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
241 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000242 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000243 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
244 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000247 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000248 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000253 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
255 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
256 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
257 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000258 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000259 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000260 if (!Subtarget->is64Bit())
261 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
262
Nate Begeman7e5496d2006-02-17 00:03:04 +0000263 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000264 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000265 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000266 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000267 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000268 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
271 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
272 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
273 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
274 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000275 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000276 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000279 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000280 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
281 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000282
Andrew Lenharthfedcf472008-02-16 14:46:26 +0000283 if (!Subtarget->hasSSE2())
284 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
285
286
Evan Chengefd142a2008-02-02 04:07:54 +0000287 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Chris Lattner9c415362005-11-29 06:16:21 +0000288 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000289 // FIXME - use subtarget debug flags
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000290 if (!Subtarget->isTargetDarwin() &&
291 !Subtarget->isTargetELF() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +0000292 !Subtarget->isTargetCygMing())
Jim Laskeyf9e54452007-01-26 14:34:52 +0000293 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000294
Anton Korobeynikovf1dcf692007-05-02 19:53:33 +0000295 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
296 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
297 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
298 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
299 if (Subtarget->is64Bit()) {
300 // FIXME: Verify
301 setExceptionPointerRegister(X86::RAX);
302 setExceptionSelectorRegister(X86::RDX);
303 } else {
304 setExceptionPointerRegister(X86::EAX);
305 setExceptionSelectorRegister(X86::EDX);
306 }
Anton Korobeynikov50ab26e2007-09-03 00:36:06 +0000307 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovf1dcf692007-05-02 19:53:33 +0000308
Duncan Sands86e01192007-09-11 14:10:23 +0000309 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsce388532007-07-27 20:02:49 +0000310
Chris Lattner3c3fefd2008-01-15 21:58:22 +0000311 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +0000312
Nate Begemane74795c2006-01-25 18:21:52 +0000313 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
314 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemane74795c2006-01-25 18:21:52 +0000315 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemane74795c2006-01-25 18:21:52 +0000316 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengdeaea252007-03-02 23:16:35 +0000317 if (Subtarget->is64Bit())
318 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
319 else
320 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
321
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000322 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000323 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000324 if (Subtarget->is64Bit())
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +0000326 if (Subtarget->isTargetCygMing())
327 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
328 else
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000330
Dale Johannesene36c4002007-09-23 14:52:20 +0000331 if (X86ScalarSSEf64) {
332 // f32 and f64 use SSE.
Chris Lattner76ac0682005-11-15 00:40:23 +0000333 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000334 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
335 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000336
Evan Cheng72d5c252006-01-31 22:28:30 +0000337 // Use ANDPD to simulate FABS.
338 setOperationAction(ISD::FABS , MVT::f64, Custom);
339 setOperationAction(ISD::FABS , MVT::f32, Custom);
340
341 // Use XORP to simulate FNEG.
342 setOperationAction(ISD::FNEG , MVT::f64, Custom);
343 setOperationAction(ISD::FNEG , MVT::f32, Custom);
344
Evan Cheng4363e882007-01-05 07:55:56 +0000345 // Use ANDPD and ORPD to simulate FCOPYSIGN.
346 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
347 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
348
Evan Chengd8fba3a2006-02-02 00:28:23 +0000349 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000350 setOperationAction(ISD::FSIN , MVT::f64, Expand);
351 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000352 setOperationAction(ISD::FREM , MVT::f64, Expand);
353 setOperationAction(ISD::FSIN , MVT::f32, Expand);
354 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000355 setOperationAction(ISD::FREM , MVT::f32, Expand);
356
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000357 // Expand FP immediates into loads from the stack, except for the special
358 // cases we handle.
Dale Johannesene36c4002007-09-23 14:52:20 +0000359 addLegalFPImmediate(APFloat(+0.0)); // xorpd
360 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesenba1a98a2007-08-09 01:04:01 +0000361
Chris Lattnera91f77e2008-01-24 08:07:48 +0000362 // Floating truncations from f80 and extensions to f80 go through memory.
363 // If optimizing, we lie about this though and handle it in
364 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
365 if (Fast) {
366 setConvertAction(MVT::f32, MVT::f80, Expand);
367 setConvertAction(MVT::f64, MVT::f80, Expand);
368 setConvertAction(MVT::f80, MVT::f32, Expand);
369 setConvertAction(MVT::f80, MVT::f64, Expand);
370 }
Dale Johannesene36c4002007-09-23 14:52:20 +0000371 } else if (X86ScalarSSEf32) {
372 // Use SSE for f32, x87 for f64.
373 // Set up the FP register classes.
374 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
375 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
376
377 // Use ANDPS to simulate FABS.
378 setOperationAction(ISD::FABS , MVT::f32, Custom);
379
380 // Use XORP to simulate FNEG.
381 setOperationAction(ISD::FNEG , MVT::f32, Custom);
382
383 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
384
385 // Use ANDPS and ORPS to simulate FCOPYSIGN.
386 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
387 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
388
389 // We don't support sin/cos/fmod
390 setOperationAction(ISD::FSIN , MVT::f32, Expand);
391 setOperationAction(ISD::FCOS , MVT::f32, Expand);
392 setOperationAction(ISD::FREM , MVT::f32, Expand);
393
Nate Begeman53e1b3f2008-02-14 08:57:00 +0000394 // Special cases we handle for FP constants.
Dale Johannesene36c4002007-09-23 14:52:20 +0000395 addLegalFPImmediate(APFloat(+0.0f)); // xorps
396 addLegalFPImmediate(APFloat(+0.0)); // FLD0
397 addLegalFPImmediate(APFloat(+1.0)); // FLD1
398 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
399 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
400
Chris Lattnera91f77e2008-01-24 08:07:48 +0000401 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
402 // this though and handle it in InstructionSelectPreprocess so that
403 // dagcombine2 can hack on these.
404 if (Fast) {
405 setConvertAction(MVT::f32, MVT::f64, Expand);
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f80, MVT::f32, Expand);
408 setConvertAction(MVT::f64, MVT::f32, Expand);
409 // And x87->x87 truncations also.
410 setConvertAction(MVT::f80, MVT::f64, Expand);
411 }
Dale Johannesene36c4002007-09-23 14:52:20 +0000412
413 if (!UnsafeFPMath) {
414 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
415 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
416 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000417 } else {
Dale Johannesene36c4002007-09-23 14:52:20 +0000418 // f32 and f64 in x87.
Chris Lattner76ac0682005-11-15 00:40:23 +0000419 // Set up the FP register classes.
Dale Johannesena2b3c172007-07-03 00:53:03 +0000420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000422
Evan Cheng4363e882007-01-05 07:55:56 +0000423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesena2b3c172007-07-03 00:53:03 +0000424 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng4363e882007-01-05 07:55:56 +0000425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesenba1a98a2007-08-09 01:04:01 +0000427
Chris Lattnera91f77e2008-01-24 08:07:48 +0000428 // Floating truncations go through memory. If optimizing, we lie about
429 // this though and handle it in InstructionSelectPreprocess so that
430 // dagcombine2 can hack on these.
431 if (Fast) {
432 setConvertAction(MVT::f80, MVT::f32, Expand);
433 setConvertAction(MVT::f64, MVT::f32, Expand);
434 setConvertAction(MVT::f80, MVT::f64, Expand);
435 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000436
Chris Lattner76ac0682005-11-15 00:40:23 +0000437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Dale Johannesend246b2c2007-08-30 00:23:21 +0000441 addLegalFPImmediate(APFloat(+0.0)); // FLD0
442 addLegalFPImmediate(APFloat(+1.0)); // FLD1
443 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
444 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene36c4002007-09-23 14:52:20 +0000445 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
446 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
447 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
448 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattner76ac0682005-11-15 00:40:23 +0000449 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000450
Dale Johannesenb1888e72007-08-05 18:49:15 +0000451 // Long double always uses X87.
452 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000453 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerd05d2012008-01-27 06:19:31 +0000455 {
Chris Lattnerd05d2012008-01-27 06:19:31 +0000456 APFloat TmpFlt(+0.0);
457 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
458 addLegalFPImmediate(TmpFlt); // FLD0
459 TmpFlt.changeSign();
460 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
461 APFloat TmpFlt2(+1.0);
462 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
463 addLegalFPImmediate(TmpFlt2); // FLD1
464 TmpFlt2.changeSign();
465 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
466 }
467
Dale Johannesenb6d56402007-09-26 21:10:55 +0000468 if (!UnsafeFPMath) {
469 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
470 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
471 }
Dale Johannesenb1888e72007-08-05 18:49:15 +0000472
Dan Gohman482732a2007-10-11 23:21:31 +0000473 // Always use a library call for pow.
474 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
475 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
476 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
477
Evan Cheng19264272006-03-01 01:11:20 +0000478 // First set operation action for all vector types to expand. Then we
479 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmaneefa83e2007-05-18 18:44:07 +0000480 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
481 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Cheng19264272006-03-01 01:11:20 +0000482 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000484 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Cheng444d3ca2007-06-29 00:18:15 +0000485 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000486 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000487 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000488 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000494 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000495 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000496 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000497 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman57111e72007-07-10 00:05:58 +0000498 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohmana1603612007-10-08 18:33:35 +0000505 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman482732a2007-10-11 23:21:31 +0000509 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman8d978da2007-10-12 14:09:42 +0000510 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman7a7742c2007-12-12 22:21:26 +0000513 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000519 }
520
Evan Chengbc047222006-03-22 19:22:18 +0000521 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000522 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
523 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
524 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendling98d21042007-03-26 07:53:08 +0000525 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng9e252e32006-02-22 02:26:30 +0000526
Evan Cheng19264272006-03-01 01:11:20 +0000527 // FIXME: add MMX packed arithmetics
Bill Wendling97905b42007-03-07 05:43:18 +0000528
Bill Wendling6092ce22007-03-08 22:09:11 +0000529 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
530 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
531 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner2805bce2007-04-12 04:14:49 +0000532 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000533
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000534 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
535 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
536 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6aa304e2007-10-30 01:18:38 +0000537 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000538
Bill Wendlinge3103412007-03-15 21:24:36 +0000539 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
540 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
541
Bill Wendling144b8bb2007-03-16 09:44:46 +0000542 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000543 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000544 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000545 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
546 setOperationAction(ISD::AND, MVT::v2i32, Promote);
547 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
548 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000549
550 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000551 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000552 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000553 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
554 setOperationAction(ISD::OR, MVT::v2i32, Promote);
555 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
556 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000557
558 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000559 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000560 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000561 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
562 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
563 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
564 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000565
Bill Wendling6092ce22007-03-08 22:09:11 +0000566 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000567 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling6092ce22007-03-08 22:09:11 +0000568 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000569 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
570 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
571 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
572 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000573
Bill Wendling6dff51a2007-03-27 20:22:40 +0000574 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
575 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
576 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
577 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlingd551a182007-03-22 18:42:45 +0000578
579 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
580 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
581 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendling6dff51a2007-03-27 20:22:40 +0000582 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendlingad2db4a2007-03-28 00:57:11 +0000583
584 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
585 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling591eab82007-04-24 21:16:55 +0000586 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000587 }
588
Evan Chengbc047222006-03-22 19:22:18 +0000589 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000590 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
591
Evan Chengbf3df772006-10-27 18:49:08 +0000592 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
593 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
594 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
595 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman57111e72007-07-10 00:05:58 +0000596 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
597 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000598 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
599 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000601 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000602 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000603 }
604
Evan Chengbc047222006-03-22 19:22:18 +0000605 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000606 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
607 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
608 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
609 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
610 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
611
Evan Cheng617a6a82006-04-10 07:23:14 +0000612 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
613 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
614 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000615 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000616 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
617 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
618 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000619 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000620 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengbf3df772006-10-27 18:49:08 +0000621 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
622 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
623 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
624 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman57111e72007-07-10 00:05:58 +0000625 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
626 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000627
Evan Cheng617a6a82006-04-10 07:23:14 +0000628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000630 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000631 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000632 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000633
Evan Cheng92232302006-04-12 21:21:57 +0000634 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
635 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemana55a67a2007-12-11 01:41:33 +0000636 // Do not attempt to custom lower non-power-of-2 vectors
637 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
638 continue;
Evan Cheng92232302006-04-12 21:21:57 +0000639 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
641 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
642 }
643 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
644 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman8ef50212008-02-12 22:51:28 +0000647 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000648 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman8ef50212008-02-12 22:51:28 +0000649 if (Subtarget->is64Bit()) {
650 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesenb066c1f2007-10-31 00:32:36 +0000651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman8ef50212008-02-12 22:51:28 +0000652 }
Evan Cheng92232302006-04-12 21:21:57 +0000653
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000654 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng92232302006-04-12 21:21:57 +0000655 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
656 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
657 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
658 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
659 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
660 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
661 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000662 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
663 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000664 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000666 }
Evan Cheng92232302006-04-12 21:21:57 +0000667
Chris Lattner1ea55cf2008-01-17 19:59:44 +0000668 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnera91f77e2008-01-24 08:07:48 +0000669
Evan Cheng92232302006-04-12 21:21:57 +0000670 // Custom lower v2i64 and v2f64 selects.
671 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000672 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000673 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000674 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000675 }
Nate Begeman2d77e8e42008-02-11 04:19:36 +0000676
677 if (Subtarget->hasSSE41()) {
678 // FIXME: Do we need to handle scalar-to-vector here?
679 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
680
681 // i8 and i16 vectors are custom , because the source register and source
682 // source memory operand types are not the same width. f32 vectors are
683 // custom since the immediate controlling the insert encodes additional
684 // information.
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
686 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
687 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
689
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
692 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
694
695 if (Subtarget->is64Bit()) {
Nate Begeman8ef50212008-02-12 22:51:28 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman2d77e8e42008-02-11 04:19:36 +0000698 }
699 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000700
Evan Cheng78038292006-04-05 23:38:46 +0000701 // We want to custom lower some of our intrinsics.
702 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
703
Evan Cheng5987cfb2006-07-07 08:33:52 +0000704 // We have target-specific dag combine patterns for the following nodes:
705 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner9259b1e2006-10-04 06:57:07 +0000706 setTargetDAGCombine(ISD::SELECT);
Evan Cheng5987cfb2006-07-07 08:33:52 +0000707
Chris Lattner76ac0682005-11-15 00:40:23 +0000708 computeRegisterProperties();
709
Evan Cheng6a374562006-02-14 08:25:08 +0000710 // FIXME: These should be based on subtarget info. Plus, the values should
711 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000712 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
713 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
714 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000715 allowUnalignedMemoryAccesses = true; // x86 supports it!
716}
717
Evan Cheng35abd842008-01-23 23:17:41 +0000718/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
719/// the desired ByVal argument alignment.
720static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
721 if (MaxAlign == 16)
722 return;
723 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
724 if (VTy->getBitWidth() == 128)
725 MaxAlign = 16;
Evan Cheng35abd842008-01-23 23:17:41 +0000726 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
727 unsigned EltAlign = 0;
728 getMaxByValAlign(ATy->getElementType(), EltAlign);
729 if (EltAlign > MaxAlign)
730 MaxAlign = EltAlign;
731 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
732 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
733 unsigned EltAlign = 0;
734 getMaxByValAlign(STy->getElementType(i), EltAlign);
735 if (EltAlign > MaxAlign)
736 MaxAlign = EltAlign;
737 if (MaxAlign == 16)
738 break;
739 }
740 }
741 return;
742}
743
744/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
745/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen36c29672008-02-08 19:48:20 +0000746/// that contain SSE vectors are placed at 16-byte boundaries while the rest
747/// are at 4-byte boundaries.
Evan Cheng35abd842008-01-23 23:17:41 +0000748unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
749 if (Subtarget->is64Bit())
750 return getTargetData()->getABITypeAlignment(Ty);
751 unsigned Align = 4;
Dale Johannesen36c29672008-02-08 19:48:20 +0000752 if (Subtarget->hasSSE1())
753 getMaxByValAlign(Ty, Align);
Evan Cheng35abd842008-01-23 23:17:41 +0000754 return Align;
755}
Chris Lattner3c763092007-02-25 08:29:00 +0000756
Evan Cheng797d56f2007-11-09 01:32:10 +0000757/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
758/// jumptable.
759SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
760 SelectionDAG &DAG) const {
761 if (usesGlobalOffsetTable())
762 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
763 if (!Subtarget->isPICStyleRIPRel())
764 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
765 return Table;
766}
767
Chris Lattner3c763092007-02-25 08:29:00 +0000768//===----------------------------------------------------------------------===//
769// Return Value Calling Convention Implementation
770//===----------------------------------------------------------------------===//
771
Chris Lattnerba3d2732007-02-28 04:55:35 +0000772#include "X86GenCallingConv.inc"
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000773
774/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
775/// exists skip possible ISD:TokenFactor.
776static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerde5c74f2008-01-16 05:52:18 +0000777 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000778 return Chain;
Chris Lattnerde5c74f2008-01-16 05:52:18 +0000779 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000780 if (Chain.getNumOperands() &&
Chris Lattnerde5c74f2008-01-16 05:52:18 +0000781 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000782 return Chain.getOperand(0);
783 }
784 return Chain;
785}
Chris Lattnerde5c74f2008-01-16 05:52:18 +0000786
Chris Lattner2fc0d702007-02-25 09:12:39 +0000787/// LowerRET - Lower an ISD::RET node.
788SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
789 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
790
Chris Lattnerc9eed392007-02-27 05:28:59 +0000791 SmallVector<CCValAssign, 16> RVLocs;
792 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner944200b2007-06-19 00:13:10 +0000793 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
794 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000795 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000796
Chris Lattner2fc0d702007-02-25 09:12:39 +0000797 // If this is the first return lowered for this function, add the regs to the
798 // liveout set for the function.
Chris Lattnera10fff52007-12-31 04:13:23 +0000799 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000800 for (unsigned i = 0; i != RVLocs.size(); ++i)
801 if (RVLocs[i].isRegLoc())
Chris Lattnera10fff52007-12-31 04:13:23 +0000802 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2fc0d702007-02-25 09:12:39 +0000803 }
Chris Lattner2fc0d702007-02-25 09:12:39 +0000804 SDOperand Chain = Op.getOperand(0);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000805
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000806 // Handle tail call return.
807 Chain = GetPossiblePreceedingTailCall(Chain);
808 if (Chain.getOpcode() == X86ISD::TAILCALL) {
809 SDOperand TailCall = Chain;
810 SDOperand TargetAddress = TailCall.getOperand(1);
811 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerde5c74f2008-01-16 05:52:18 +0000812 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000813 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
814 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
815 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
816 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
817 "Expecting an global address, external symbol, or register");
Chris Lattnerde5c74f2008-01-16 05:52:18 +0000818 assert(StackAdjustment.getOpcode() == ISD::Constant &&
819 "Expecting a const value");
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000820
821 SmallVector<SDOperand,8> Operands;
822 Operands.push_back(Chain.getOperand(0));
823 Operands.push_back(TargetAddress);
824 Operands.push_back(StackAdjustment);
825 // Copy registers used by the call. Last operand is a flag so it is not
826 // copied.
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +0000827 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000828 Operands.push_back(Chain.getOperand(i));
829 }
Arnold Schwaighoferb3d58b92007-10-16 09:05:00 +0000830 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
831 Operands.size());
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000832 }
833
834 // Regular return.
835 SDOperand Flag;
836
Chris Lattner2fc0d702007-02-25 09:12:39 +0000837 // Copy the result values into the output registers.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000838 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
839 RVLocs[0].getLocReg() != X86::ST0) {
840 for (unsigned i = 0; i != RVLocs.size(); ++i) {
841 CCValAssign &VA = RVLocs[i];
842 assert(VA.isRegLoc() && "Can only return in registers!");
843 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
844 Flag);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000845 Flag = Chain.getValue(1);
846 }
847 } else {
848 // We need to handle a destination of ST0 specially, because it isn't really
849 // a register.
850 SDOperand Value = Op.getOperand(1);
851
Chris Lattnera91f77e2008-01-24 08:07:48 +0000852 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
853 // This will get legalized into a load/store if it can't get optimized away.
854 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
855 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000856
857 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
858 SDOperand Ops[] = { Chain, Value };
859 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
860 Flag = Chain.getValue(1);
861 }
862
863 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
864 if (Flag.Val)
865 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
866 else
867 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
868}
869
870
Chris Lattner0cd99602007-02-25 08:59:22 +0000871/// LowerCallResult - Lower the result values of an ISD::CALL into the
872/// appropriate copies out of appropriate physical registers. This assumes that
873/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
874/// being lowered. The returns a SDNode with the same number of values as the
875/// ISD::CALL.
876SDNode *X86TargetLowering::
877LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
878 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattner152bfa12007-02-28 07:09:55 +0000879
880 // Assign locations to each value returned by this call.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000881 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner944200b2007-06-19 00:13:10 +0000882 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
883 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000884 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
885
Chris Lattner152bfa12007-02-28 07:09:55 +0000886 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner0cd99602007-02-25 08:59:22 +0000887
888 // Copy all of the result registers out of their specified physreg.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000889 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
890 for (unsigned i = 0; i != RVLocs.size(); ++i) {
891 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
892 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner0cd99602007-02-25 08:59:22 +0000893 InFlag = Chain.getValue(2);
894 ResultVals.push_back(Chain.getValue(0));
895 }
896 } else {
897 // Copies from the FP stack are special, as ST0 isn't a valid register
898 // before the fp stackifier runs.
899
Chris Lattnera91f77e2008-01-24 08:07:48 +0000900 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
901 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
902 // the specified value type.
903 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
904 if (isScalarFPTypeInSSEReg(GetResultTy))
905 GetResultTy = MVT::f80;
906 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
907
Chris Lattner0cd99602007-02-25 08:59:22 +0000908 SDOperand GROps[] = { Chain, InFlag };
909 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
910 Chain = RetVal.getValue(1);
911 InFlag = RetVal.getValue(2);
Chris Lattner8013bd32007-12-29 06:41:28 +0000912
Chris Lattnera91f77e2008-01-24 08:07:48 +0000913 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
914 // there.
915 if (GetResultTy != RVLocs[0].getValVT())
916 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
917 // This truncation won't change the value.
918 DAG.getIntPtrConstant(1));
919
Chris Lattner0cd99602007-02-25 08:59:22 +0000920 ResultVals.push_back(RetVal);
921 }
922
923 // Merge everything together with a MERGE_VALUES node.
924 ResultVals.push_back(Chain);
925 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
926 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner3c763092007-02-25 08:29:00 +0000927}
928
Evan Cheng084a1cd2008-01-29 19:34:22 +0000929/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
930/// ISD::CALL where the results are known to be in two 64-bit registers,
931/// e.g. XMM0 and XMM1. This simplify store the two values back to the
932/// fixed stack slot allocated for StructRet.
933SDNode *X86TargetLowering::
934LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
935 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
936 MVT::ValueType VT, SelectionDAG &DAG) {
937 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
938 Chain = RetVal1.getValue(1);
939 InFlag = RetVal1.getValue(2);
940 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
941 Chain = RetVal2.getValue(1);
942 InFlag = RetVal2.getValue(2);
943 SDOperand FIN = TheCall->getOperand(5);
944 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
945 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
946 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
947 return Chain.Val;
948}
949
950/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
951/// where the results are known to be in ST0 and ST1.
952SDNode *X86TargetLowering::
953LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
954 SDNode *TheCall, SelectionDAG &DAG) {
955 SmallVector<SDOperand, 8> ResultVals;
956 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
957 SDVTList Tys = DAG.getVTList(VTs, 4);
958 SDOperand Ops[] = { Chain, InFlag };
959 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
960 Chain = RetVal.getValue(2);
961 SDOperand FIN = TheCall->getOperand(5);
962 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
963 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
964 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
965 return Chain.Val;
966}
Chris Lattner3c763092007-02-25 08:29:00 +0000967
Chris Lattner76ac0682005-11-15 00:40:23 +0000968//===----------------------------------------------------------------------===//
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000969// C & StdCall & Fast Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000970//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000971// StdCall calling convention seems to be standard for many Windows' API
972// routines and around. It differs from C calling convention just a little:
973// callee should clean up the stack, not caller. Symbols should be also
974// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000975// For info on fast calling convention see Fast Calling Convention (tail call)
976// implementation LowerX86_32FastCCCallTo.
Chris Lattner76ac0682005-11-15 00:40:23 +0000977
Evan Cheng24eb3f42006-04-27 05:35:28 +0000978/// AddLiveIn - This helper function adds the specified physical register to the
979/// MachineFunction as a live in value. It also creates a corresponding virtual
980/// register for it.
981static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000982 const TargetRegisterClass *RC) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000983 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattnera10fff52007-12-31 04:13:23 +0000984 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
985 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng24eb3f42006-04-27 05:35:28 +0000986 return VReg;
987}
988
Gordon Henriksen92319582008-01-05 16:56:59 +0000989// Determines whether a CALL node uses struct return semantics.
990static bool CallIsStructReturn(SDOperand Op) {
991 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
992 if (!NumOps)
993 return false;
994
995 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
996 return Flags->getValue() & ISD::ParamFlags::StructReturn;
997}
998
999// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1000static bool ArgsAreStructReturn(SDOperand Op) {
1001 unsigned NumArgs = Op.Val->getNumValues() - 1;
1002 if (!NumArgs)
1003 return false;
1004
1005 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1006 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1007}
1008
1009// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1010// its own arguments. Callee pop is necessary to support tail calls.
1011bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1012 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1013 if (IsVarArg)
1014 return false;
1015
1016 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1017 default:
1018 return false;
1019 case CallingConv::X86_StdCall:
1020 return !Subtarget->is64Bit();
1021 case CallingConv::X86_FastCall:
1022 return !Subtarget->is64Bit();
1023 case CallingConv::Fast:
1024 return PerformTailCallOpt;
1025 }
1026}
1027
1028// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1029CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1030 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1031
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00001032 if (Subtarget->is64Bit()) {
Gordon Henriksen92319582008-01-05 16:56:59 +00001033 if (CC == CallingConv::Fast && PerformTailCallOpt)
1034 return CC_X86_64_TailCall;
1035 else
1036 return CC_X86_64_C;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00001037 }
1038
Gordon Henriksen92319582008-01-05 16:56:59 +00001039 if (CC == CallingConv::X86_FastCall)
1040 return CC_X86_32_FastCall;
1041 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1042 return CC_X86_32_TailCall;
1043 else
1044 return CC_X86_32_C;
1045}
1046
1047// Selects the appropriate decoration to apply to a MachineFunction containing a
1048// given FORMAL_ARGUMENTS node.
1049NameDecorationStyle
1050X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1051 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1052 if (CC == CallingConv::X86_FastCall)
1053 return FastCall;
1054 else if (CC == CallingConv::X86_StdCall)
1055 return StdCall;
1056 return None;
1057}
1058
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001059
Arnold Schwaighofer06da9e22008-01-11 17:10:15 +00001060// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1061// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001062// the implementation of this call is very conservative and assumes all
1063// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofer06da9e22008-01-11 17:10:15 +00001064// registers would be overwritten by direct lowering.
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001065// Possible improvement:
1066// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1067// indicating inreg passed arguments which also need not be lowered to a safe
1068// stack slot.
Arnold Schwaighofer06da9e22008-01-11 17:10:15 +00001069static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001070 RegisterSDNode * OpReg = NULL;
1071 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1072 (Op.getOpcode()== ISD::CopyFromReg &&
1073 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001074 OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister))
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001075 return true;
1076 return false;
1077}
1078
Evan Cheng7411b512008-01-12 01:08:07 +00001079// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1080// by "Src" to address "Dst" with size and alignment information specified by
1081// the specific parameter attribute. The copy will be passed as a byval function
1082// parameter.
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001083static SDOperand
Evan Cheng7411b512008-01-12 01:08:07 +00001084CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1085 unsigned Flags, SelectionDAG &DAG) {
1086 unsigned Align = 1 <<
1087 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1088 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001089 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng7411b512008-01-12 01:08:07 +00001090 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1091 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001092 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng7411b512008-01-12 01:08:07 +00001093 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001094}
1095
Rafael Espindola272f7302007-09-14 15:48:13 +00001096SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1097 const CCValAssign &VA,
1098 MachineFrameInfo *MFI,
1099 SDOperand Root, unsigned i) {
1100 // Create the nodes corresponding to a load from this parameter slot.
Evan Chenga2655242008-01-10 02:24:25 +00001101 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1102 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1103
1104 // FIXME: For now, all byval parameter objects are marked mutable. This
1105 // can be changed with more analysis.
Rafael Espindola272f7302007-09-14 15:48:13 +00001106 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Chenga2655242008-01-10 02:24:25 +00001107 VA.getLocMemOffset(), !isByVal);
Rafael Espindola272f7302007-09-14 15:48:13 +00001108 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chenga2655242008-01-10 02:24:25 +00001109 if (isByVal)
Rafael Espindola272f7302007-09-14 15:48:13 +00001110 return FIN;
Dan Gohman2d489b52008-02-06 22:27:42 +00001111 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001112 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola272f7302007-09-14 15:48:13 +00001113}
1114
Gordon Henriksen92319582008-01-05 16:56:59 +00001115SDOperand
1116X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001117 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen92319582008-01-05 16:56:59 +00001118 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1119
1120 const Function* Fn = MF.getFunction();
1121 if (Fn->hasExternalLinkage() &&
1122 Subtarget->isTargetCygMing() &&
1123 Fn->getName() == "main")
1124 FuncInfo->setForceFramePointer(true);
1125
1126 // Decorate the function name.
1127 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1128
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001129 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +00001130 SDOperand Root = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001131 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001132 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen92319582008-01-05 16:56:59 +00001133 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001134
1135 assert(!(isVarArg && CC == CallingConv::Fast) &&
1136 "Var args not supported with calling convention fastcc");
1137
Chris Lattner227b6c52007-02-28 07:00:42 +00001138 // Assign locations to all of the incoming arguments.
Chris Lattnerb9db2252007-02-28 05:46:49 +00001139 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001140 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen92319582008-01-05 16:56:59 +00001141 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001142
Chris Lattnerb9db2252007-02-28 05:46:49 +00001143 SmallVector<SDOperand, 8> ArgValues;
1144 unsigned LastVal = ~0U;
1145 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1146 CCValAssign &VA = ArgLocs[i];
1147 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1148 // places.
1149 assert(VA.getValNo() != LastVal &&
1150 "Don't support value assigned to multiple locs yet");
1151 LastVal = VA.getValNo();
1152
1153 if (VA.isRegLoc()) {
1154 MVT::ValueType RegVT = VA.getLocVT();
1155 TargetRegisterClass *RC;
1156 if (RegVT == MVT::i32)
1157 RC = X86::GR32RegisterClass;
Gordon Henriksen92319582008-01-05 16:56:59 +00001158 else if (Is64Bit && RegVT == MVT::i64)
1159 RC = X86::GR64RegisterClass;
Dale Johannesend88f1d02008-02-05 20:46:33 +00001160 else if (RegVT == MVT::f32)
Gordon Henriksen92319582008-01-05 16:56:59 +00001161 RC = X86::FR32RegisterClass;
Dale Johannesend88f1d02008-02-05 20:46:33 +00001162 else if (RegVT == MVT::f64)
Gordon Henriksen92319582008-01-05 16:56:59 +00001163 RC = X86::FR64RegisterClass;
Chris Lattnerb9db2252007-02-28 05:46:49 +00001164 else {
1165 assert(MVT::isVector(RegVT));
Gordon Henriksen92319582008-01-05 16:56:59 +00001166 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1167 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1168 RegVT = MVT::i64;
1169 } else
1170 RC = X86::VR128RegisterClass;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001171 }
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001172
Chris Lattner9c7e5e32007-03-02 05:12:29 +00001173 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1174 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerb9db2252007-02-28 05:46:49 +00001175
1176 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1177 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1178 // right size.
1179 if (VA.getLocInfo() == CCValAssign::SExt)
1180 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1181 DAG.getValueType(VA.getValVT()));
1182 else if (VA.getLocInfo() == CCValAssign::ZExt)
1183 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1184 DAG.getValueType(VA.getValVT()));
1185
1186 if (VA.getLocInfo() != CCValAssign::Full)
1187 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1188
Gordon Henriksen92319582008-01-05 16:56:59 +00001189 // Handle MMX values passed in GPRs.
1190 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1191 MVT::getSizeInBits(RegVT) == 64)
1192 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1193
Chris Lattnerb9db2252007-02-28 05:46:49 +00001194 ArgValues.push_back(ArgValue);
1195 } else {
1196 assert(VA.isMemLoc());
Rafael Espindola272f7302007-09-14 15:48:13 +00001197 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001198 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001199 }
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001200
Chris Lattnerb9db2252007-02-28 05:46:49 +00001201 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001202 // align stack specially for tail calls
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001203 if (CC == CallingConv::Fast)
1204 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng17e734f2006-05-23 21:06:34 +00001205
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001206 // If the function takes variable number of arguments, make a frame index for
1207 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001208 if (isVarArg) {
Gordon Henriksen92319582008-01-05 16:56:59 +00001209 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1210 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1211 }
1212 if (Is64Bit) {
1213 static const unsigned GPR64ArgRegs[] = {
1214 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1215 };
1216 static const unsigned XMMArgRegs[] = {
1217 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1218 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1219 };
1220
1221 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1222 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1223
1224 // For X86-64, if there are vararg parameters that are passed via
1225 // registers, then we must store them to their spots on the stack so they
1226 // may be loaded by deferencing the result of va_next.
1227 VarArgsGPOffset = NumIntRegs * 8;
1228 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1229 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1230
1231 // Store the integer parameter registers.
1232 SmallVector<SDOperand, 8> MemOps;
1233 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1234 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner72733e52008-01-17 07:00:52 +00001235 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen92319582008-01-05 16:56:59 +00001236 for (; NumIntRegs != 6; ++NumIntRegs) {
1237 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1238 X86::GR64RegisterClass);
1239 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman2d489b52008-02-06 22:27:42 +00001240 SDOperand Store =
1241 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001242 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00001243 RegSaveFrameIndex);
Gordon Henriksen92319582008-01-05 16:56:59 +00001244 MemOps.push_back(Store);
1245 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner72733e52008-01-17 07:00:52 +00001246 DAG.getIntPtrConstant(8));
Gordon Henriksen92319582008-01-05 16:56:59 +00001247 }
1248
1249 // Now store the XMM (fp + vector) parameter registers.
1250 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner72733e52008-01-17 07:00:52 +00001251 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen92319582008-01-05 16:56:59 +00001252 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1253 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1254 X86::VR128RegisterClass);
1255 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman2d489b52008-02-06 22:27:42 +00001256 SDOperand Store =
1257 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001258 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00001259 RegSaveFrameIndex);
Gordon Henriksen92319582008-01-05 16:56:59 +00001260 MemOps.push_back(Store);
1261 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner72733e52008-01-17 07:00:52 +00001262 DAG.getIntPtrConstant(16));
Gordon Henriksen92319582008-01-05 16:56:59 +00001263 }
1264 if (!MemOps.empty())
1265 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1266 &MemOps[0], MemOps.size());
1267 }
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001268 }
Gordon Henriksen92319582008-01-05 16:56:59 +00001269
1270 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1271 // arguments and the arguments after the retaddr has been pushed are
1272 // aligned.
1273 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1274 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1275 (StackSize & 7) == 0)
1276 StackSize += 4;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001277
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001278 ArgValues.push_back(Root);
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001279
Gordon Henriksen92319582008-01-05 16:56:59 +00001280 // Some CCs need callee pop.
1281 if (IsCalleePop(Op)) {
1282 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001283 BytesCallerReserves = 0;
1284 } else {
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +00001285 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerb9db2252007-02-28 05:46:49 +00001286 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen92319582008-01-05 16:56:59 +00001287 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerb9db2252007-02-28 05:46:49 +00001288 BytesToPopOnReturn = 4;
Chris Lattnerb9db2252007-02-28 05:46:49 +00001289 BytesCallerReserves = StackSize;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001290 }
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001291
Gordon Henriksen92319582008-01-05 16:56:59 +00001292 if (!Is64Bit) {
1293 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1294 if (CC == CallingConv::X86_FastCall)
1295 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1296 }
Evan Cheng17e734f2006-05-23 21:06:34 +00001297
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00001298 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00001299
Evan Cheng17e734f2006-05-23 21:06:34 +00001300 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +00001301 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +00001302 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001303}
1304
Evan Cheng73d10172008-01-10 00:09:10 +00001305SDOperand
1306X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1307 const SDOperand &StackPtr,
1308 const CCValAssign &VA,
1309 SDOperand Chain,
1310 SDOperand Arg) {
Dan Gohman63a84522008-02-07 16:28:05 +00001311 unsigned LocMemOffset = VA.getLocMemOffset();
1312 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Cheng73d10172008-01-10 00:09:10 +00001313 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1314 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1315 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1316 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng7411b512008-01-12 01:08:07 +00001317 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Cheng73d10172008-01-10 00:09:10 +00001318 }
Dan Gohman63a84522008-02-07 16:28:05 +00001319 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001320 PseudoSourceValue::getStack(), LocMemOffset);
Evan Cheng73d10172008-01-10 00:09:10 +00001321}
1322
Evan Cheng084a1cd2008-01-29 19:34:22 +00001323/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1324/// struct return call to the specified function. X86-64 ABI specifies
1325/// some SRet calls are actually returned in registers. Since current
1326/// LLVM cannot represent multi-value calls, they are represent as
1327/// calls where the results are passed in a hidden struct provided by
1328/// the caller. This function examines the type of the struct to
1329/// determine the correct way to implement the call.
1330X86::X86_64SRet
1331X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1332 // FIXME: Disabled for now.
1333 return X86::InMemory;
1334
1335 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1336 const Type *RTy = PTy->getElementType();
1337 unsigned Size = getTargetData()->getABITypeSize(RTy);
1338 if (Size != 16 && Size != 32)
1339 return X86::InMemory;
1340
1341 if (Size == 32) {
1342 const StructType *STy = dyn_cast<StructType>(RTy);
1343 if (!STy) return X86::InMemory;
1344 if (STy->getNumElements() == 2 &&
1345 STy->getElementType(0) == Type::X86_FP80Ty &&
1346 STy->getElementType(1) == Type::X86_FP80Ty)
1347 return X86::InX87;
1348 }
1349
1350 bool AllFP = true;
1351 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1352 I != E; ++I) {
1353 const Type *STy = I->get();
1354 if (!STy->isFPOrFPVector()) {
1355 AllFP = false;
1356 break;
1357 }
1358 }
1359
1360 if (AllFP)
1361 return X86::InSSE;
1362 return X86::InGPR64;
1363}
1364
1365void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1366 CCAssignFn *Fn,
1367 CCState &CCInfo) {
1368 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1369 for (unsigned i = 1; i != NumOps; ++i) {
1370 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1371 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1372 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1373 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1374 cerr << "Call operand #" << i << " has unhandled type "
1375 << MVT::getValueTypeString(ArgVT) << "\n";
1376 abort();
1377 }
1378 }
1379}
1380
Gordon Henriksen92319582008-01-05 16:56:59 +00001381SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1382 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng2a330942006-05-25 00:59:30 +00001383 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen92319582008-01-05 16:56:59 +00001384 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001385 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen92319582008-01-05 16:56:59 +00001386 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1387 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng2a330942006-05-25 00:59:30 +00001388 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen92319582008-01-05 16:56:59 +00001389 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng084a1cd2008-01-29 19:34:22 +00001390 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001391
1392 assert(!(isVarArg && CC == CallingConv::Fast) &&
1393 "Var args not supported with calling convention fastcc");
1394
Chris Lattner227b6c52007-02-28 07:00:42 +00001395 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerbe799592007-02-28 05:31:48 +00001396 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001397 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng084a1cd2008-01-29 19:34:22 +00001398 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1399
1400 X86::X86_64SRet SRetMethod = X86::InMemory;
1401 if (Is64Bit && IsStructRet)
1402 // FIXME: We can't figure out type of the sret structure for indirect
1403 // calls. We need to copy more information from CallSite to the ISD::CALL
1404 // node.
1405 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1406 SRetMethod =
1407 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1408
1409 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1410 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1411 // a sret call.
1412 if (SRetMethod != X86::InMemory)
1413 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1414 else
1415 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001416
Chris Lattnerbe799592007-02-28 05:31:48 +00001417 // Get a count of how many bytes are to be pushed on the stack.
1418 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001419 if (CC == CallingConv::Fast)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001420 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattner76ac0682005-11-15 00:40:23 +00001421
Gordon Henriksen92319582008-01-05 16:56:59 +00001422 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1423 // arguments and the arguments after the retaddr has been pushed are aligned.
1424 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1425 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1426 (NumBytes & 7) == 0)
1427 NumBytes += 4;
1428
1429 int FPDiff = 0;
1430 if (IsTailCall) {
1431 // Lower arguments at fp - stackoffset + fpdiff.
1432 unsigned NumBytesCallerPushed =
1433 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1434 FPDiff = NumBytesCallerPushed - NumBytes;
1435
1436 // Set the delta of movement of the returnaddr stackslot.
1437 // But only set if delta is greater than previous delta.
1438 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1439 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1440 }
1441
Chris Lattner72733e52008-01-17 07:00:52 +00001442 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattner76ac0682005-11-15 00:40:23 +00001443
Gordon Henriksen92319582008-01-05 16:56:59 +00001444 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1445 if (IsTailCall) {
1446 // Adjust the Return address stack slot.
1447 if (FPDiff) {
1448 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1449 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1450 // Load the "old" Return address.
1451 RetAddrFrIdx =
1452 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1453 // Calculate the new stack slot for the return address.
1454 int SlotSize = Is64Bit ? 8 : 4;
1455 int NewReturnAddrFI =
1456 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1457 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1458 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1459 }
1460 }
1461
Chris Lattner35a08552007-02-25 07:10:00 +00001462 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1463 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng2a330942006-05-25 00:59:30 +00001464
Chris Lattnerbe799592007-02-28 05:31:48 +00001465 SDOperand StackPtr;
Chris Lattnerbe799592007-02-28 05:31:48 +00001466
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001467 // Walk the register/memloc assignments, inserting copies/loads. For tail
1468 // calls, lower arguments which could otherwise be possibly overwritten to the
1469 // stack slot where they would go on normal function calls.
Chris Lattnerbe799592007-02-28 05:31:48 +00001470 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1471 CCValAssign &VA = ArgLocs[i];
1472 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001473
Chris Lattnerbe799592007-02-28 05:31:48 +00001474 // Promote the value if needed.
1475 switch (VA.getLocInfo()) {
1476 default: assert(0 && "Unknown loc info!");
1477 case CCValAssign::Full: break;
1478 case CCValAssign::SExt:
1479 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1480 break;
1481 case CCValAssign::ZExt:
1482 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1483 break;
1484 case CCValAssign::AExt:
1485 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1486 break;
Evan Cheng5ee96892006-05-25 18:56:34 +00001487 }
Chris Lattnerbe799592007-02-28 05:31:48 +00001488
1489 if (VA.isRegLoc()) {
1490 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1491 } else {
Arnold Schwaighofer06da9e22008-01-11 17:10:15 +00001492 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001493 assert(VA.isMemLoc());
1494 if (StackPtr.Val == 0)
1495 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1496
1497 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1498 Arg));
1499 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001500 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001501 }
Chris Lattner5958b172007-02-28 05:39:26 +00001502
Evan Cheng2a330942006-05-25 00:59:30 +00001503 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001504 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1505 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +00001506
Evan Cheng88decde2006-04-28 21:29:37 +00001507 // Build a sequence of copy-to-reg nodes chained together with token chain
1508 // and flag operands which copy the outgoing args into registers.
1509 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001510 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1511 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1512 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +00001513 InFlag = Chain.getValue(1);
1514 }
Gordon Henriksen92319582008-01-05 16:56:59 +00001515
1516 if (IsTailCall)
1517 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1518
Evan Cheng84a041e2007-02-21 21:18:14 +00001519 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1520 // GOT pointer.
Gordon Henriksen92319582008-01-05 16:56:59 +00001521 // Does not work with tail call since ebx is not restored correctly by
1522 // tailcaller. TODO: at least for x86 - verify for x86-64
1523 if (!IsTailCall && !Is64Bit &&
1524 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Evan Cheng1281dc32007-01-22 21:34:25 +00001525 Subtarget->isPICStyleGOT()) {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001526 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1527 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1528 InFlag);
1529 InFlag = Chain.getValue(1);
1530 }
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001531
Gordon Henriksen92319582008-01-05 16:56:59 +00001532 if (Is64Bit && isVarArg) {
1533 // From AMD64 ABI document:
1534 // For calls that may call functions that use varargs or stdargs
1535 // (prototype-less calls or calls to functions containing ellipsis (...) in
1536 // the declaration) %al is used as hidden argument to specify the number
1537 // of SSE registers used. The contents of %al do not need to match exactly
1538 // the number of registers, but must be an ubound on the number of SSE
1539 // registers used and is in the range 0 - 8 inclusive.
1540
1541 // Count the number of XMM registers allocated.
1542 static const unsigned XMMArgRegs[] = {
1543 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1544 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1545 };
1546 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1547
1548 Chain = DAG.getCopyToReg(Chain, X86::AL,
1549 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1550 InFlag = Chain.getValue(1);
1551 }
1552
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001553 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen92319582008-01-05 16:56:59 +00001554 if (IsTailCall) {
1555 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen92319582008-01-05 16:56:59 +00001556 SDOperand FIN;
1557 int FI = 0;
1558 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1559 CCValAssign &VA = ArgLocs[i];
1560 if (!VA.isRegLoc()) {
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001561 assert(VA.isMemLoc());
1562 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen92319582008-01-05 16:56:59 +00001563 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1564 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen92319582008-01-05 16:56:59 +00001565 // Create frame index.
1566 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1567 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1568 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1569 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001570 SDOperand Source = Arg;
Evan Cheng7411b512008-01-12 01:08:07 +00001571 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001572 // Copy from stack slots to stack slot of a tail called function. This
1573 // needs to be done because if we would lower the arguments directly
1574 // to their real stack slot we might end up overwriting each other.
1575 // Get source stack slot.
Chris Lattner72733e52008-01-17 07:00:52 +00001576 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001577 if (StackPtr.Val == 0)
1578 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1579 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1580 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands51fe7bb2008-01-13 21:20:29 +00001581 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001582 }
1583
Gordon Henriksen92319582008-01-05 16:56:59 +00001584 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng7411b512008-01-12 01:08:07 +00001585 // Copy relative to framepointer.
1586 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1587 Flags, DAG));
Gordon Henriksen92319582008-01-05 16:56:59 +00001588 } else {
Evan Cheng7411b512008-01-12 01:08:07 +00001589 // Store relative to framepointer.
Dan Gohman2d489b52008-02-06 22:27:42 +00001590 MemOpChains2.push_back(
1591 DAG.getStore(Chain, Source, FIN,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001592 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer6cf72fb2008-01-11 16:49:42 +00001593 }
Gordon Henriksen92319582008-01-05 16:56:59 +00001594 }
1595 }
1596
1597 if (!MemOpChains2.empty())
1598 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferbf1816e2008-01-11 14:34:56 +00001599 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen92319582008-01-05 16:56:59 +00001600
1601 // Store the return address to the appropriate stack slot.
1602 if (FPDiff)
1603 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1604 }
1605
Evan Cheng2a330942006-05-25 00:59:30 +00001606 // If the callee is a GlobalAddress node (quite common, every direct call is)
1607 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001608 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00001609 // We should use extra load for direct calls to dllimported functions in
1610 // non-JIT mode.
Gordon Henriksen92319582008-01-05 16:56:59 +00001611 if ((IsTailCall || !Is64Bit ||
1612 getTargetMachine().getCodeModel() != CodeModel::Large)
1613 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1614 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001615 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001616 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen92319582008-01-05 16:56:59 +00001617 if (IsTailCall || !Is64Bit ||
1618 getTargetMachine().getCodeModel() != CodeModel::Large)
1619 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1620 } else if (IsTailCall) {
1621 assert(Callee.getOpcode() == ISD::LOAD &&
1622 "Function destination must be loaded into virtual register");
1623 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1624
1625 Chain = DAG.getCopyToReg(Chain,
1626 DAG.getRegister(Opc, getPointerTy()) ,
1627 Callee,InFlag);
1628 Callee = DAG.getRegister(Opc, getPointerTy());
1629 // Add register as live out.
1630 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001631 }
1632
Chris Lattnere56fef92007-02-25 06:40:16 +00001633 // Returns a chain & a flag for retval copy to use.
1634 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00001635 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen92319582008-01-05 16:56:59 +00001636
1637 if (IsTailCall) {
1638 Ops.push_back(Chain);
Chris Lattner72733e52008-01-17 07:00:52 +00001639 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1640 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen92319582008-01-05 16:56:59 +00001641 if (InFlag.Val)
1642 Ops.push_back(InFlag);
1643 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1644 InFlag = Chain.getValue(1);
1645
1646 // Returns a chain & a flag for retval copy to use.
1647 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1648 Ops.clear();
1649 }
1650
Nate Begeman7e5496d2006-02-17 00:03:04 +00001651 Ops.push_back(Chain);
1652 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +00001653
Gordon Henriksen92319582008-01-05 16:56:59 +00001654 if (IsTailCall)
1655 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Cheng84a041e2007-02-21 21:18:14 +00001656
1657 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen92319582008-01-05 16:56:59 +00001658 if (!IsTailCall && !Is64Bit &&
1659 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Evan Cheng84a041e2007-02-21 21:18:14 +00001660 Subtarget->isPICStyleGOT())
1661 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001662
Gordon Henriksen92319582008-01-05 16:56:59 +00001663 // Add argument registers to the end of the list so that they are known live
1664 // into the call.
Evan Cheng82421682008-01-07 23:08:23 +00001665 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1666 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1667 RegsToPass[i].second.getValueType()));
Gordon Henriksen92319582008-01-05 16:56:59 +00001668
Evan Cheng88decde2006-04-28 21:29:37 +00001669 if (InFlag.Val)
1670 Ops.push_back(InFlag);
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001671
Gordon Henriksen92319582008-01-05 16:56:59 +00001672 if (IsTailCall) {
1673 assert(InFlag.Val &&
1674 "Flag must be set. Depend on flag being set in LowerRET");
1675 Chain = DAG.getNode(X86ISD::TAILCALL,
1676 Op.Val->getVTList(), &Ops[0], Ops.size());
1677
1678 return SDOperand(Chain.Val, Op.ResNo);
1679 }
1680
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001681 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng88decde2006-04-28 21:29:37 +00001682 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +00001683
Chris Lattner8be5be82006-05-23 18:50:38 +00001684 // Create the CALLSEQ_END node.
Gordon Henriksen92319582008-01-05 16:56:59 +00001685 unsigned NumBytesForCalleeToPush;
1686 if (IsCalleePop(Op))
1687 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng084a1cd2008-01-29 19:34:22 +00001688 else if (!Is64Bit && IsStructRet)
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001689 // If this is is a call to a struct-return function, the callee
1690 // pops the hidden struct pointer, so we have to push it back.
1691 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001692 NumBytesForCalleeToPush = 4;
Gordon Henriksen92319582008-01-05 16:56:59 +00001693 else
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001694 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen92319582008-01-05 16:56:59 +00001695
Gordon Henriksenf066fc42008-01-03 16:47:34 +00001696 // Returns a flag for retval copy to use.
Bill Wendlingf359fed2007-11-13 00:44:25 +00001697 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner72733e52008-01-17 07:00:52 +00001698 DAG.getIntPtrConstant(NumBytes),
1699 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendlingf359fed2007-11-13 00:44:25 +00001700 InFlag);
Chris Lattner0cd99602007-02-25 08:59:22 +00001701 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001702
Chris Lattner0cd99602007-02-25 08:59:22 +00001703 // Handle result values, copying them out of physregs into vregs that we
1704 // return.
Evan Cheng084a1cd2008-01-29 19:34:22 +00001705 switch (SRetMethod) {
1706 default:
1707 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1708 case X86::InGPR64:
1709 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1710 X86::RAX, X86::RDX,
1711 MVT::i64, DAG), Op.ResNo);
1712 case X86::InSSE:
1713 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1714 X86::XMM0, X86::XMM1,
1715 MVT::f64, DAG), Op.ResNo);
1716 case X86::InX87:
1717 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1718 Op.ResNo);
1719 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001720}
1721
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001722
1723//===----------------------------------------------------------------------===//
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001724// Fast Calling Convention (tail call) implementation
1725//===----------------------------------------------------------------------===//
1726
1727// Like std call, callee cleans arguments, convention except that ECX is
1728// reserved for storing the tail called function address. Only 2 registers are
1729// free for argument passing (inreg). Tail call optimization is performed
1730// provided:
1731// * tailcallopt is enabled
1732// * caller/callee are fastcc
1733// * elf/pic is disabled OR
1734// * elf/pic enabled + callee is in module + callee has
1735// visibility protected or hidden
Arnold Schwaighofer1f0da1f2007-10-12 21:30:57 +00001736// To keep the stack aligned according to platform abi the function
1737// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1738// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001739// If a tail called function callee has more arguments than the caller the
1740// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer1f0da1f2007-10-12 21:30:57 +00001741// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001742// original REtADDR, but before the saved framepointer or the spilled registers
1743// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1744// stack layout:
1745// arg1
1746// arg2
1747// RETADDR
1748// [ new RETADDR
1749// move area ]
1750// (possible EBP)
1751// ESI
1752// EDI
1753// local1 ..
1754
1755/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1756/// for a 16 byte align requirement.
1757unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1758 SelectionDAG& DAG) {
1759 if (PerformTailCallOpt) {
1760 MachineFunction &MF = DAG.getMachineFunction();
1761 const TargetMachine &TM = MF.getTarget();
1762 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1763 unsigned StackAlignment = TFI.getStackAlignment();
1764 uint64_t AlignMask = StackAlignment - 1;
1765 int64_t Offset = StackSize;
1766 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1767 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1768 // Number smaller than 12 so just add the difference.
1769 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1770 } else {
1771 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1772 Offset = ((~AlignMask) & Offset) + StackAlignment +
1773 (StackAlignment-SlotSize);
1774 }
1775 StackSize = Offset;
1776 }
1777 return StackSize;
1778}
1779
1780/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge453ff42007-11-02 01:26:22 +00001781/// following the call is a return. A function is eligible if caller/callee
1782/// calling conventions match, currently only fastcc supports tail calls, and
1783/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001784bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1785 SDOperand Ret,
1786 SelectionDAG& DAG) const {
Evan Chenge453ff42007-11-02 01:26:22 +00001787 if (!PerformTailCallOpt)
1788 return false;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001789
1790 // Check whether CALL node immediatly preceeds the RET node and whether the
1791 // return uses the result of the node or is a void return.
Evan Chenge453ff42007-11-02 01:26:22 +00001792 unsigned NumOps = Ret.getNumOperands();
1793 if ((NumOps == 1 &&
1794 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1795 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng2b93a202007-11-02 17:45:40 +00001796 (NumOps > 1 &&
Evan Chenge453ff42007-11-02 01:26:22 +00001797 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1798 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001799 MachineFunction &MF = DAG.getMachineFunction();
1800 unsigned CallerCC = MF.getFunction()->getCallingConv();
1801 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1802 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1803 SDOperand Callee = Call.getOperand(4);
1804 // On elf/pic %ebx needs to be livein.
Evan Chenge453ff42007-11-02 01:26:22 +00001805 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1806 !Subtarget->isPICStyleGOT())
1807 return true;
1808
1809 // Can only do local tail calls with PIC.
Gordon Henriksen92319582008-01-05 16:56:59 +00001810 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1811 return G->getGlobal()->hasHiddenVisibility()
1812 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001813 }
1814 }
Evan Chenge453ff42007-11-02 01:26:22 +00001815
1816 return false;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00001817}
1818
Chris Lattner3066bec2007-02-28 06:10:12 +00001819//===----------------------------------------------------------------------===//
1820// Other Lowering Hooks
1821//===----------------------------------------------------------------------===//
1822
1823
Chris Lattner76ac0682005-11-15 00:40:23 +00001824SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00001825 MachineFunction &MF = DAG.getMachineFunction();
1826 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1827 int ReturnAddrIndex = FuncInfo->getRAIndex();
1828
Chris Lattner76ac0682005-11-15 00:40:23 +00001829 if (ReturnAddrIndex == 0) {
1830 // Set up a frame object for the return address.
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001831 if (Subtarget->is64Bit())
1832 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1833 else
1834 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikov597c8b72007-08-15 17:12:32 +00001835
1836 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattner76ac0682005-11-15 00:40:23 +00001837 }
1838
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001839 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattner76ac0682005-11-15 00:40:23 +00001840}
1841
1842
1843
Evan Cheng45df7f82006-01-30 23:41:35 +00001844/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1845/// specific condition code. It returns a false if it cannot do a direct
Chris Lattner7a627672006-09-13 03:22:10 +00001846/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1847/// needed.
Evan Cheng78038292006-04-05 23:38:46 +00001848static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattner7a627672006-09-13 03:22:10 +00001849 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1850 SelectionDAG &DAG) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001851 X86CC = X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001852 if (!isFP) {
Chris Lattner971e3392006-09-13 17:04:54 +00001853 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1854 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1855 // X > -1 -> X == 0, jump !sign.
1856 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001857 X86CC = X86::COND_NS;
Chris Lattner971e3392006-09-13 17:04:54 +00001858 return true;
1859 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1860 // X < 0 -> X == 0, jump on sign.
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001861 X86CC = X86::COND_S;
Chris Lattner971e3392006-09-13 17:04:54 +00001862 return true;
Dan Gohman863bdc32007-09-17 14:49:27 +00001863 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1864 // X < 1 -> X <= 0
1865 RHS = DAG.getConstant(0, RHS.getValueType());
1866 X86CC = X86::COND_LE;
1867 return true;
Chris Lattner971e3392006-09-13 17:04:54 +00001868 }
Chris Lattner7a627672006-09-13 03:22:10 +00001869 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001870
Evan Cheng172fce72006-01-06 00:43:03 +00001871 switch (SetCCOpcode) {
1872 default: break;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001873 case ISD::SETEQ: X86CC = X86::COND_E; break;
1874 case ISD::SETGT: X86CC = X86::COND_G; break;
1875 case ISD::SETGE: X86CC = X86::COND_GE; break;
1876 case ISD::SETLT: X86CC = X86::COND_L; break;
1877 case ISD::SETLE: X86CC = X86::COND_LE; break;
1878 case ISD::SETNE: X86CC = X86::COND_NE; break;
1879 case ISD::SETULT: X86CC = X86::COND_B; break;
1880 case ISD::SETUGT: X86CC = X86::COND_A; break;
1881 case ISD::SETULE: X86CC = X86::COND_BE; break;
1882 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001883 }
1884 } else {
1885 // On a floating point condition, the flags are set as follows:
1886 // ZF PF CF op
1887 // 0 | 0 | 0 | X > Y
1888 // 0 | 0 | 1 | X < Y
1889 // 1 | 0 | 0 | X == Y
1890 // 1 | 1 | 1 | unordered
Chris Lattner7a627672006-09-13 03:22:10 +00001891 bool Flip = false;
Evan Cheng172fce72006-01-06 00:43:03 +00001892 switch (SetCCOpcode) {
1893 default: break;
1894 case ISD::SETUEQ:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001895 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001896 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001897 case ISD::SETOGT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001898 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001899 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001900 case ISD::SETOGE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001901 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001902 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001903 case ISD::SETULT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001904 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001905 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001906 case ISD::SETULE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001907 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001908 case ISD::SETONE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001909 case ISD::SETNE: X86CC = X86::COND_NE; break;
1910 case ISD::SETUO: X86CC = X86::COND_P; break;
1911 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001912 }
Chris Lattner7a627672006-09-13 03:22:10 +00001913 if (Flip)
1914 std::swap(LHS, RHS);
Evan Cheng172fce72006-01-06 00:43:03 +00001915 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001916
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001917 return X86CC != X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001918}
1919
Evan Cheng339edad2006-01-11 00:33:36 +00001920/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1921/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001922/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001923static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001924 switch (X86CC) {
1925 default:
1926 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001927 case X86::COND_B:
1928 case X86::COND_BE:
1929 case X86::COND_E:
1930 case X86::COND_P:
1931 case X86::COND_A:
1932 case X86::COND_AE:
1933 case X86::COND_NE:
1934 case X86::COND_NP:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001935 return true;
1936 }
1937}
1938
Evan Chengc995b452006-04-06 23:23:56 +00001939/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001940/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001941static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1942 if (Op.getOpcode() == ISD::UNDEF)
1943 return true;
1944
1945 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001946 return (Val >= Low && Val < Hi);
1947}
1948
1949/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1950/// true if Op is undef or if its value equal to the specified value.
1951static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1952 if (Op.getOpcode() == ISD::UNDEF)
1953 return true;
1954 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001955}
1956
Evan Cheng68ad48b2006-03-22 18:59:22 +00001957/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1958/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1959bool X86::isPSHUFDMask(SDNode *N) {
1960 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1961
Dan Gohman8932bff2007-08-02 21:17:01 +00001962 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng68ad48b2006-03-22 18:59:22 +00001963 return false;
1964
1965 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001966 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001967 SDOperand Arg = N->getOperand(i);
1968 if (Arg.getOpcode() == ISD::UNDEF) continue;
1969 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman8932bff2007-08-02 21:17:01 +00001970 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Chengb7fedff2006-03-29 23:07:14 +00001971 return false;
1972 }
1973
1974 return true;
1975}
1976
1977/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001978/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001979bool X86::isPSHUFHWMask(SDNode *N) {
1980 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1981
1982 if (N->getNumOperands() != 8)
1983 return false;
1984
1985 // Lower quadword copied in order.
1986 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001987 SDOperand Arg = N->getOperand(i);
1988 if (Arg.getOpcode() == ISD::UNDEF) continue;
1989 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1990 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001991 return false;
1992 }
1993
1994 // Upper quadword shuffled.
1995 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001996 SDOperand Arg = N->getOperand(i);
1997 if (Arg.getOpcode() == ISD::UNDEF) continue;
1998 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1999 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002000 if (Val < 4 || Val > 7)
2001 return false;
2002 }
2003
2004 return true;
2005}
2006
2007/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00002008/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00002009bool X86::isPSHUFLWMask(SDNode *N) {
2010 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2011
2012 if (N->getNumOperands() != 8)
2013 return false;
2014
2015 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00002016 for (unsigned i = 4; i != 8; ++i)
2017 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00002018 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00002019
2020 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00002021 for (unsigned i = 0; i != 4; ++i)
2022 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00002023 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00002024
2025 return true;
2026}
2027
Evan Chengd27fb3e2006-03-24 01:18:28 +00002028/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2029/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner35a08552007-02-25 07:10:00 +00002030static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002031 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00002032
Evan Cheng60f0b892006-04-20 08:58:49 +00002033 unsigned Half = NumElems / 2;
2034 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00002035 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng60f0b892006-04-20 08:58:49 +00002036 return false;
2037 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00002038 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00002039 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00002040
2041 return true;
2042}
2043
Evan Cheng60f0b892006-04-20 08:58:49 +00002044bool X86::isSHUFPMask(SDNode *N) {
2045 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002046 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00002047}
2048
Evan Chengafa1cb62007-05-17 18:45:50 +00002049/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng60f0b892006-04-20 08:58:49 +00002050/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2051/// half elements to come from vector 1 (which would equal the dest.) and
2052/// the upper half to come from vector 2.
Chris Lattner35a08552007-02-25 07:10:00 +00002053static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2054 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002055
Chris Lattner35a08552007-02-25 07:10:00 +00002056 unsigned Half = NumOps / 2;
Evan Cheng60f0b892006-04-20 08:58:49 +00002057 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00002058 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00002059 return false;
Chris Lattner35a08552007-02-25 07:10:00 +00002060 for (unsigned i = Half; i < NumOps; ++i)
2061 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng60f0b892006-04-20 08:58:49 +00002062 return false;
2063 return true;
2064}
2065
2066static bool isCommutedSHUFP(SDNode *N) {
2067 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002068 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00002069}
2070
Evan Cheng2595a682006-03-24 02:58:06 +00002071/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2072/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2073bool X86::isMOVHLPSMask(SDNode *N) {
2074 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2075
Evan Cheng1a194a52006-03-28 06:50:32 +00002076 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00002077 return false;
2078
Evan Cheng1a194a52006-03-28 06:50:32 +00002079 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00002080 return isUndefOrEqual(N->getOperand(0), 6) &&
2081 isUndefOrEqual(N->getOperand(1), 7) &&
2082 isUndefOrEqual(N->getOperand(2), 2) &&
2083 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00002084}
2085
Evan Cheng922e1912006-11-07 22:14:24 +00002086/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2087/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2088/// <2, 3, 2, 3>
2089bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2090 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2091
2092 if (N->getNumOperands() != 4)
2093 return false;
2094
2095 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2096 return isUndefOrEqual(N->getOperand(0), 2) &&
2097 isUndefOrEqual(N->getOperand(1), 3) &&
2098 isUndefOrEqual(N->getOperand(2), 2) &&
2099 isUndefOrEqual(N->getOperand(3), 3);
2100}
2101
Evan Chengc995b452006-04-06 23:23:56 +00002102/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2103/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2104bool X86::isMOVLPMask(SDNode *N) {
2105 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2106
2107 unsigned NumElems = N->getNumOperands();
2108 if (NumElems != 2 && NumElems != 4)
2109 return false;
2110
Evan Chengac847262006-04-07 21:53:05 +00002111 for (unsigned i = 0; i < NumElems/2; ++i)
2112 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2113 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002114
Evan Chengac847262006-04-07 21:53:05 +00002115 for (unsigned i = NumElems/2; i < NumElems; ++i)
2116 if (!isUndefOrEqual(N->getOperand(i), i))
2117 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002118
2119 return true;
2120}
2121
2122/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00002123/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2124/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00002125bool X86::isMOVHPMask(SDNode *N) {
2126 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2127
2128 unsigned NumElems = N->getNumOperands();
2129 if (NumElems != 2 && NumElems != 4)
2130 return false;
2131
Evan Chengac847262006-04-07 21:53:05 +00002132 for (unsigned i = 0; i < NumElems/2; ++i)
2133 if (!isUndefOrEqual(N->getOperand(i), i))
2134 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002135
2136 for (unsigned i = 0; i < NumElems/2; ++i) {
2137 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00002138 if (!isUndefOrEqual(Arg, i + NumElems))
2139 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002140 }
2141
2142 return true;
2143}
2144
Evan Cheng5df75882006-03-28 00:39:58 +00002145/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2146/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner35a08552007-02-25 07:10:00 +00002147bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2148 bool V2IsSplat = false) {
2149 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng5df75882006-03-28 00:39:58 +00002150 return false;
2151
Chris Lattner35a08552007-02-25 07:10:00 +00002152 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2153 SDOperand BitI = Elts[i];
2154 SDOperand BitI1 = Elts[i+1];
Evan Chengac847262006-04-07 21:53:05 +00002155 if (!isUndefOrEqual(BitI, j))
2156 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002157 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00002158 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002159 return false;
2160 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00002161 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002162 return false;
2163 }
Evan Cheng5df75882006-03-28 00:39:58 +00002164 }
2165
2166 return true;
2167}
2168
Evan Cheng60f0b892006-04-20 08:58:49 +00002169bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2170 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002171 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00002172}
2173
Evan Cheng2bc32802006-03-28 02:43:26 +00002174/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2175/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner35a08552007-02-25 07:10:00 +00002176bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2177 bool V2IsSplat = false) {
2178 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng2bc32802006-03-28 02:43:26 +00002179 return false;
2180
Chris Lattner35a08552007-02-25 07:10:00 +00002181 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2182 SDOperand BitI = Elts[i];
2183 SDOperand BitI1 = Elts[i+1];
2184 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengac847262006-04-07 21:53:05 +00002185 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002186 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00002187 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002188 return false;
2189 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00002190 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00002191 return false;
2192 }
Evan Cheng2bc32802006-03-28 02:43:26 +00002193 }
2194
2195 return true;
2196}
2197
Evan Cheng60f0b892006-04-20 08:58:49 +00002198bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2199 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002200 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00002201}
2202
Evan Chengf3b52c82006-04-05 07:20:06 +00002203/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2204/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2205/// <0, 0, 1, 1>
2206bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2207 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2208
2209 unsigned NumElems = N->getNumOperands();
Bill Wendling591eab82007-04-24 21:16:55 +00002210 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengf3b52c82006-04-05 07:20:06 +00002211 return false;
2212
2213 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2214 SDOperand BitI = N->getOperand(i);
2215 SDOperand BitI1 = N->getOperand(i+1);
2216
Evan Chengac847262006-04-07 21:53:05 +00002217 if (!isUndefOrEqual(BitI, j))
2218 return false;
2219 if (!isUndefOrEqual(BitI1, j))
2220 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00002221 }
2222
2223 return true;
2224}
2225
Bill Wendling591eab82007-04-24 21:16:55 +00002226/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2227/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2228/// <2, 2, 3, 3>
2229bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2230 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2231
2232 unsigned NumElems = N->getNumOperands();
2233 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2234 return false;
2235
2236 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2237 SDOperand BitI = N->getOperand(i);
2238 SDOperand BitI1 = N->getOperand(i + 1);
2239
2240 if (!isUndefOrEqual(BitI, j))
2241 return false;
2242 if (!isUndefOrEqual(BitI1, j))
2243 return false;
2244 }
2245
2246 return true;
2247}
2248
Evan Chenge8b51802006-04-21 01:05:10 +00002249/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2250/// specifies a shuffle of elements that is suitable for input to MOVSS,
2251/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner35a08552007-02-25 07:10:00 +00002252static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Chengc829e5c2007-12-06 22:14:22 +00002253 if (NumElts != 2 && NumElts != 4)
Evan Cheng12ba3e22006-04-11 00:19:04 +00002254 return false;
2255
Chris Lattner35a08552007-02-25 07:10:00 +00002256 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Cheng12ba3e22006-04-11 00:19:04 +00002257 return false;
2258
Chris Lattner35a08552007-02-25 07:10:00 +00002259 for (unsigned i = 1; i < NumElts; ++i) {
2260 if (!isUndefOrEqual(Elts[i], i))
Evan Cheng12ba3e22006-04-11 00:19:04 +00002261 return false;
2262 }
2263
2264 return true;
2265}
Evan Chengf3b52c82006-04-05 07:20:06 +00002266
Evan Chenge8b51802006-04-21 01:05:10 +00002267bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002268 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002269 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00002270}
2271
Evan Chenge8b51802006-04-21 01:05:10 +00002272/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2273/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00002274/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner35a08552007-02-25 07:10:00 +00002275static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2276 bool V2IsSplat = false,
Evan Cheng89c5d042006-09-08 01:50:06 +00002277 bool V2IsUndef = false) {
Chris Lattner35a08552007-02-25 07:10:00 +00002278 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00002279 return false;
2280
2281 if (!isUndefOrEqual(Ops[0], 0))
2282 return false;
2283
Chris Lattner35a08552007-02-25 07:10:00 +00002284 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002285 SDOperand Arg = Ops[i];
Chris Lattner35a08552007-02-25 07:10:00 +00002286 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2287 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2288 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng89c5d042006-09-08 01:50:06 +00002289 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00002290 }
2291
2292 return true;
2293}
2294
Evan Cheng89c5d042006-09-08 01:50:06 +00002295static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2296 bool V2IsUndef = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002297 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00002298 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2299 V2IsSplat, V2IsUndef);
Evan Cheng60f0b892006-04-20 08:58:49 +00002300}
2301
Evan Cheng5d247f82006-04-14 21:59:03 +00002302/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2304bool X86::isMOVSHDUPMask(SDNode *N) {
2305 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2306
2307 if (N->getNumOperands() != 4)
2308 return false;
2309
2310 // Expect 1, 1, 3, 3
2311 for (unsigned i = 0; i < 2; ++i) {
2312 SDOperand Arg = N->getOperand(i);
2313 if (Arg.getOpcode() == ISD::UNDEF) continue;
2314 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2315 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2316 if (Val != 1) return false;
2317 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002318
2319 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002320 for (unsigned i = 2; i < 4; ++i) {
2321 SDOperand Arg = N->getOperand(i);
2322 if (Arg.getOpcode() == ISD::UNDEF) continue;
2323 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2324 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2325 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002326 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002327 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002328
Evan Cheng6222cf22006-04-15 05:37:34 +00002329 // Don't use movshdup if it can be done with a shufps.
2330 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002331}
2332
2333/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2334/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2335bool X86::isMOVSLDUPMask(SDNode *N) {
2336 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2337
2338 if (N->getNumOperands() != 4)
2339 return false;
2340
2341 // Expect 0, 0, 2, 2
2342 for (unsigned i = 0; i < 2; ++i) {
2343 SDOperand Arg = N->getOperand(i);
2344 if (Arg.getOpcode() == ISD::UNDEF) continue;
2345 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2346 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2347 if (Val != 0) return false;
2348 }
Evan Cheng6222cf22006-04-15 05:37:34 +00002349
2350 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00002351 for (unsigned i = 2; i < 4; ++i) {
2352 SDOperand Arg = N->getOperand(i);
2353 if (Arg.getOpcode() == ISD::UNDEF) continue;
2354 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2355 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2356 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00002357 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00002358 }
Evan Cheng65bb7202006-04-15 03:13:24 +00002359
Evan Cheng6222cf22006-04-15 05:37:34 +00002360 // Don't use movshdup if it can be done with a shufps.
2361 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00002362}
2363
Evan Chengcea02ff2007-06-19 00:02:56 +00002364/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2365/// specifies a identity operation on the LHS or RHS.
2366static bool isIdentityMask(SDNode *N, bool RHS = false) {
2367 unsigned NumElems = N->getNumOperands();
2368 for (unsigned i = 0; i < NumElems; ++i)
2369 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2370 return false;
2371 return true;
2372}
2373
Evan Chengd097e672006-03-22 02:53:00 +00002374/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2375/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00002376static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002377 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2378
Evan Chengd097e672006-03-22 02:53:00 +00002379 // This is a splat operation if each element of the permute is the same, and
2380 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002381 unsigned NumElems = N->getNumOperands();
2382 SDOperand ElementBase;
2383 unsigned i = 0;
2384 for (; i != NumElems; ++i) {
2385 SDOperand Elt = N->getOperand(i);
Reid Spencerde46e482006-11-02 20:25:50 +00002386 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002387 ElementBase = Elt;
2388 break;
2389 }
2390 }
2391
2392 if (!ElementBase.Val)
2393 return false;
2394
2395 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002396 SDOperand Arg = N->getOperand(i);
2397 if (Arg.getOpcode() == ISD::UNDEF) continue;
2398 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002399 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00002400 }
2401
2402 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002403 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00002404}
2405
Evan Cheng5022b342006-04-17 20:43:08 +00002406/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2407/// a splat of a single element and it's a 2 or 4 element mask.
2408bool X86::isSplatMask(SDNode *N) {
2409 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2410
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002411 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00002412 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2413 return false;
2414 return ::isSplatMask(N);
2415}
2416
Evan Chenge056dd52006-10-27 21:08:32 +00002417/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2418/// specifies a splat of zero element.
2419bool X86::isSplatLoMask(SDNode *N) {
2420 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2421
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002422 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chenge056dd52006-10-27 21:08:32 +00002423 if (!isUndefOrEqual(N->getOperand(i), 0))
2424 return false;
2425 return true;
2426}
2427
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002428/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2429/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2430/// instructions.
2431unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002432 unsigned NumOperands = N->getNumOperands();
2433 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2434 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00002435 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002436 unsigned Val = 0;
2437 SDOperand Arg = N->getOperand(NumOperands-i-1);
2438 if (Arg.getOpcode() != ISD::UNDEF)
2439 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00002440 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002441 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00002442 if (i != NumOperands - 1)
2443 Mask <<= Shift;
2444 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002445
2446 return Mask;
2447}
2448
Evan Chengb7fedff2006-03-29 23:07:14 +00002449/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2450/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2451/// instructions.
2452unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2453 unsigned Mask = 0;
2454 // 8 nodes, but we only care about the last 4.
2455 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002456 unsigned Val = 0;
2457 SDOperand Arg = N->getOperand(i);
2458 if (Arg.getOpcode() != ISD::UNDEF)
2459 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002460 Mask |= (Val - 4);
2461 if (i != 4)
2462 Mask <<= 2;
2463 }
2464
2465 return Mask;
2466}
2467
2468/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2469/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2470/// instructions.
2471unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2472 unsigned Mask = 0;
2473 // 8 nodes, but we only care about the first 4.
2474 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002475 unsigned Val = 0;
2476 SDOperand Arg = N->getOperand(i);
2477 if (Arg.getOpcode() != ISD::UNDEF)
2478 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002479 Mask |= Val;
2480 if (i != 0)
2481 Mask <<= 2;
2482 }
2483
2484 return Mask;
2485}
2486
Evan Cheng59a63552006-04-05 01:47:37 +00002487/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2488/// specifies a 8 element shuffle that can be broken into a pair of
2489/// PSHUFHW and PSHUFLW.
2490static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2491 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2492
2493 if (N->getNumOperands() != 8)
2494 return false;
2495
2496 // Lower quadword shuffled.
2497 for (unsigned i = 0; i != 4; ++i) {
2498 SDOperand Arg = N->getOperand(i);
2499 if (Arg.getOpcode() == ISD::UNDEF) continue;
2500 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2501 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng4fbf4592007-12-11 01:46:18 +00002502 if (Val >= 4)
Evan Cheng59a63552006-04-05 01:47:37 +00002503 return false;
2504 }
2505
2506 // Upper quadword shuffled.
2507 for (unsigned i = 4; i != 8; ++i) {
2508 SDOperand Arg = N->getOperand(i);
2509 if (Arg.getOpcode() == ISD::UNDEF) continue;
2510 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2511 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2512 if (Val < 4 || Val > 7)
2513 return false;
2514 }
2515
2516 return true;
2517}
2518
Chris Lattner5728bdd2007-11-25 00:24:49 +00002519/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Chengc995b452006-04-06 23:23:56 +00002520/// values in ther permute mask.
Evan Chengc415c5b2006-10-25 21:49:50 +00002521static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2522 SDOperand &V2, SDOperand &Mask,
2523 SelectionDAG &DAG) {
Evan Chengc995b452006-04-06 23:23:56 +00002524 MVT::ValueType VT = Op.getValueType();
2525 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002526 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Chengc995b452006-04-06 23:23:56 +00002527 unsigned NumElems = Mask.getNumOperands();
Chris Lattner35a08552007-02-25 07:10:00 +00002528 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc995b452006-04-06 23:23:56 +00002529
2530 for (unsigned i = 0; i != NumElems; ++i) {
2531 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002532 if (Arg.getOpcode() == ISD::UNDEF) {
2533 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2534 continue;
2535 }
Evan Chengc995b452006-04-06 23:23:56 +00002536 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2537 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2538 if (Val < NumElems)
2539 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2540 else
2541 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2542 }
2543
Evan Chengc415c5b2006-10-25 21:49:50 +00002544 std::swap(V1, V2);
Evan Chengbfd373a2007-12-07 08:07:39 +00002545 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Chengc415c5b2006-10-25 21:49:50 +00002546 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chengc995b452006-04-06 23:23:56 +00002547}
2548
Evan Chengb41d8382007-12-07 21:30:01 +00002549/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2550/// the two vector operands have swapped position.
Evan Chengbfd373a2007-12-07 08:07:39 +00002551static
2552SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2553 MVT::ValueType MaskVT = Mask.getValueType();
2554 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2555 unsigned NumElems = Mask.getNumOperands();
2556 SmallVector<SDOperand, 8> MaskVec;
2557 for (unsigned i = 0; i != NumElems; ++i) {
2558 SDOperand Arg = Mask.getOperand(i);
2559 if (Arg.getOpcode() == ISD::UNDEF) {
2560 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2561 continue;
2562 }
2563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2564 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2565 if (Val < NumElems)
2566 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2567 else
2568 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2569 }
2570 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2571}
2572
2573
Evan Cheng7855e4d2006-04-19 20:35:22 +00002574/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2575/// match movhlps. The lower half elements should come from upper half of
2576/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002577/// half of V2 (and in order).
Evan Cheng7855e4d2006-04-19 20:35:22 +00002578static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2579 unsigned NumElems = Mask->getNumOperands();
2580 if (NumElems != 4)
2581 return false;
2582 for (unsigned i = 0, e = 2; i != e; ++i)
2583 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2584 return false;
2585 for (unsigned i = 2; i != 4; ++i)
2586 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2587 return false;
2588 return true;
2589}
2590
Evan Chengc995b452006-04-06 23:23:56 +00002591/// isScalarLoadToVector - Returns true if the node is a scalar load that
2592/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002593static inline bool isScalarLoadToVector(SDNode *N) {
2594 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2595 N = N->getOperand(0).Val;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002596 return ISD::isNON_EXTLoad(N);
Evan Chengc995b452006-04-06 23:23:56 +00002597 }
2598 return false;
2599}
2600
Evan Cheng7855e4d2006-04-19 20:35:22 +00002601/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2602/// match movlp{s|d}. The lower half elements should come from lower half of
2603/// V1 (and in order), and the upper half elements should come from the upper
2604/// half of V2 (and in order). And since V1 will become the source of the
2605/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Chenge646abb2006-10-09 21:39:25 +00002606static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002607 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng7855e4d2006-04-19 20:35:22 +00002608 return false;
Evan Chenge646abb2006-10-09 21:39:25 +00002609 // Is V2 is a vector load, don't do this transformation. We will try to use
2610 // load folding shufps op.
2611 if (ISD::isNON_EXTLoad(V2))
2612 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002613
Evan Cheng7855e4d2006-04-19 20:35:22 +00002614 unsigned NumElems = Mask->getNumOperands();
2615 if (NumElems != 2 && NumElems != 4)
2616 return false;
2617 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2618 if (!isUndefOrEqual(Mask->getOperand(i), i))
2619 return false;
2620 for (unsigned i = NumElems/2; i != NumElems; ++i)
2621 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2622 return false;
2623 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002624}
2625
Evan Cheng60f0b892006-04-20 08:58:49 +00002626/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2627/// all the same.
2628static bool isSplatVector(SDNode *N) {
2629 if (N->getOpcode() != ISD::BUILD_VECTOR)
2630 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002631
Evan Cheng60f0b892006-04-20 08:58:49 +00002632 SDOperand SplatValue = N->getOperand(0);
2633 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2634 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002635 return false;
2636 return true;
2637}
2638
Evan Cheng89c5d042006-09-08 01:50:06 +00002639/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2640/// to an undef.
2641static bool isUndefShuffle(SDNode *N) {
Evan Chengafa1cb62007-05-17 18:45:50 +00002642 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng89c5d042006-09-08 01:50:06 +00002643 return false;
2644
2645 SDOperand V1 = N->getOperand(0);
2646 SDOperand V2 = N->getOperand(1);
2647 SDOperand Mask = N->getOperand(2);
2648 unsigned NumElems = Mask.getNumOperands();
2649 for (unsigned i = 0; i != NumElems; ++i) {
2650 SDOperand Arg = Mask.getOperand(i);
2651 if (Arg.getOpcode() != ISD::UNDEF) {
2652 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2653 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2654 return false;
2655 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2656 return false;
2657 }
2658 }
2659 return true;
2660}
2661
Evan Chengafa1cb62007-05-17 18:45:50 +00002662/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2663/// constant +0.0.
2664static inline bool isZeroNode(SDOperand Elt) {
2665 return ((isa<ConstantSDNode>(Elt) &&
2666 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2667 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesen3cf889f2007-08-31 04:03:46 +00002668 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Chengafa1cb62007-05-17 18:45:50 +00002669}
2670
2671/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2672/// to an zero vector.
2673static bool isZeroShuffle(SDNode *N) {
2674 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2675 return false;
2676
2677 SDOperand V1 = N->getOperand(0);
2678 SDOperand V2 = N->getOperand(1);
2679 SDOperand Mask = N->getOperand(2);
2680 unsigned NumElems = Mask.getNumOperands();
2681 for (unsigned i = 0; i != NumElems; ++i) {
2682 SDOperand Arg = Mask.getOperand(i);
Chris Lattner5728bdd2007-11-25 00:24:49 +00002683 if (Arg.getOpcode() == ISD::UNDEF)
2684 continue;
2685
2686 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2687 if (Idx < NumElems) {
2688 unsigned Opc = V1.Val->getOpcode();
2689 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2690 continue;
2691 if (Opc != ISD::BUILD_VECTOR ||
2692 !isZeroNode(V1.Val->getOperand(Idx)))
2693 return false;
2694 } else if (Idx >= NumElems) {
2695 unsigned Opc = V2.Val->getOpcode();
2696 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2697 continue;
2698 if (Opc != ISD::BUILD_VECTOR ||
2699 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2700 return false;
Evan Chengafa1cb62007-05-17 18:45:50 +00002701 }
2702 }
2703 return true;
2704}
2705
2706/// getZeroVector - Returns a vector of specified type with all zero elements.
2707///
2708static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2709 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner5728bdd2007-11-25 00:24:49 +00002710
2711 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2712 // type. This ensures they get CSE'd.
2713 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2714 SDOperand Vec;
2715 if (MVT::getSizeInBits(VT) == 64) // MMX
2716 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2717 else // SSE
2718 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2719 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Chengafa1cb62007-05-17 18:45:50 +00002720}
2721
Chris Lattner5728bdd2007-11-25 00:24:49 +00002722/// getOnesVector - Returns a vector of specified type with all bits set.
2723///
2724static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2725 assert(MVT::isVector(VT) && "Expected a vector type");
2726
2727 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2728 // type. This ensures they get CSE'd.
2729 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2730 SDOperand Vec;
2731 if (MVT::getSizeInBits(VT) == 64) // MMX
2732 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2733 else // SSE
2734 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2735 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2736}
2737
2738
Evan Cheng60f0b892006-04-20 08:58:49 +00002739/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2740/// that point to V2 points to its first element.
2741static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2742 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2743
2744 bool Changed = false;
Chris Lattner35a08552007-02-25 07:10:00 +00002745 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002746 unsigned NumElems = Mask.getNumOperands();
2747 for (unsigned i = 0; i != NumElems; ++i) {
2748 SDOperand Arg = Mask.getOperand(i);
2749 if (Arg.getOpcode() != ISD::UNDEF) {
2750 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2751 if (Val > NumElems) {
2752 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2753 Changed = true;
2754 }
2755 }
2756 MaskVec.push_back(Arg);
2757 }
2758
2759 if (Changed)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002760 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2761 &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002762 return Mask;
2763}
2764
Evan Chenge8b51802006-04-21 01:05:10 +00002765/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2766/// operation of specified width.
2767static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002768 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002769 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng60f0b892006-04-20 08:58:49 +00002770
Chris Lattner35a08552007-02-25 07:10:00 +00002771 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002772 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2773 for (unsigned i = 1; i != NumElems; ++i)
2774 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002775 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002776}
2777
Evan Cheng5022b342006-04-17 20:43:08 +00002778/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2779/// of specified width.
2780static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2781 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002782 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002783 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5022b342006-04-17 20:43:08 +00002784 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2785 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2786 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2787 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002788 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng5022b342006-04-17 20:43:08 +00002789}
2790
Evan Cheng60f0b892006-04-20 08:58:49 +00002791/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2792/// of specified width.
2793static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2794 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002795 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng60f0b892006-04-20 08:58:49 +00002796 unsigned Half = NumElems/2;
Chris Lattner35a08552007-02-25 07:10:00 +00002797 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002798 for (unsigned i = 0; i != Half; ++i) {
2799 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2800 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2801 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002802 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002803}
2804
Evan Cheng5022b342006-04-17 20:43:08 +00002805/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2806///
2807static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2808 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002809 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002810 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002811 unsigned NumElems = Mask.getNumOperands();
2812 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002813 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002814 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002815 NumElems >>= 1;
2816 }
2817 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2818
Chris Lattner5728bdd2007-11-25 00:24:49 +00002819 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002820 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002821 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002822 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2823}
2824
Evan Cheng14215c32006-04-21 23:03:30 +00002825/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner5728bdd2007-11-25 00:24:49 +00002826/// vector of zero or undef vector. This produces a shuffle where the low
2827/// element of V2 is swizzled into the zero/undef vector, landing at element
2828/// 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 +00002829static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002830 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002831 bool isZero, SelectionDAG &DAG) {
2832 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002833 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002834 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5728bdd2007-11-25 00:24:49 +00002835 SmallVector<SDOperand, 16> MaskVec;
2836 for (unsigned i = 0; i != NumElems; ++i)
2837 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2838 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2839 else
2840 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002841 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2842 &MaskVec[0], MaskVec.size());
Evan Cheng14215c32006-04-21 23:03:30 +00002843 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002844}
2845
Evan Chengb0461082006-04-24 18:01:45 +00002846/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2847///
2848static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2849 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002850 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002851 if (NumNonZero > 8)
2852 return SDOperand();
2853
2854 SDOperand V(0, 0);
2855 bool First = true;
2856 for (unsigned i = 0; i < 16; ++i) {
2857 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2858 if (ThisIsNonZero && First) {
2859 if (NumZero)
2860 V = getZeroVector(MVT::v8i16, DAG);
2861 else
2862 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2863 First = false;
2864 }
2865
2866 if ((i & 1) != 0) {
2867 SDOperand ThisElt(0, 0), LastElt(0, 0);
2868 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2869 if (LastIsNonZero) {
2870 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2871 }
2872 if (ThisIsNonZero) {
2873 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2874 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2875 ThisElt, DAG.getConstant(8, MVT::i8));
2876 if (LastIsNonZero)
2877 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2878 } else
2879 ThisElt = LastElt;
2880
2881 if (ThisElt.Val)
2882 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner72733e52008-01-17 07:00:52 +00002883 DAG.getIntPtrConstant(i/2));
Evan Chengb0461082006-04-24 18:01:45 +00002884 }
2885 }
2886
2887 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2888}
2889
Bill Wendlingd551a182007-03-22 18:42:45 +00002890/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengb0461082006-04-24 18:01:45 +00002891///
2892static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2893 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002894 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002895 if (NumNonZero > 4)
2896 return SDOperand();
2897
2898 SDOperand V(0, 0);
2899 bool First = true;
2900 for (unsigned i = 0; i < 8; ++i) {
2901 bool isNonZero = (NonZeros & (1 << i)) != 0;
2902 if (isNonZero) {
2903 if (First) {
2904 if (NumZero)
2905 V = getZeroVector(MVT::v8i16, DAG);
2906 else
2907 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2908 First = false;
2909 }
2910 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner72733e52008-01-17 07:00:52 +00002911 DAG.getIntPtrConstant(i));
Evan Chengb0461082006-04-24 18:01:45 +00002912 }
2913 }
2914
2915 return V;
2916}
2917
Evan Chenga9467aa2006-04-25 20:13:52 +00002918SDOperand
2919X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner5728bdd2007-11-25 00:24:49 +00002920 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2921 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2922 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2923 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2924 // eliminated on x86-32 hosts.
2925 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2926 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002927
Chris Lattner5728bdd2007-11-25 00:24:49 +00002928 if (ISD::isBuildVectorAllOnes(Op.Val))
2929 return getOnesVector(Op.getValueType(), DAG);
2930 return getZeroVector(Op.getValueType(), DAG);
2931 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002932
2933 MVT::ValueType VT = Op.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002934 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Chenga9467aa2006-04-25 20:13:52 +00002935 unsigned EVTBits = MVT::getSizeInBits(EVT);
2936
2937 unsigned NumElems = Op.getNumOperands();
2938 unsigned NumZero = 0;
2939 unsigned NumNonZero = 0;
2940 unsigned NonZeros = 0;
Evan Cheng2a9895672007-12-12 06:45:40 +00002941 bool HasNonImms = false;
Evan Cheng4fbf4592007-12-11 01:46:18 +00002942 SmallSet<SDOperand, 8> Values;
Evan Chenga9467aa2006-04-25 20:13:52 +00002943 for (unsigned i = 0; i < NumElems; ++i) {
2944 SDOperand Elt = Op.getOperand(i);
Evan Cheng2a9895672007-12-12 06:45:40 +00002945 if (Elt.getOpcode() == ISD::UNDEF)
2946 continue;
2947 Values.insert(Elt);
2948 if (Elt.getOpcode() != ISD::Constant &&
2949 Elt.getOpcode() != ISD::ConstantFP)
2950 HasNonImms = true;
2951 if (isZeroNode(Elt))
2952 NumZero++;
2953 else {
2954 NonZeros |= (1 << i);
2955 NumNonZero++;
Evan Chenga9467aa2006-04-25 20:13:52 +00002956 }
2957 }
2958
Dan Gohmana8665142007-06-25 16:23:39 +00002959 if (NumNonZero == 0) {
Chris Lattner5728bdd2007-11-25 00:24:49 +00002960 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2961 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002962 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002963
2964 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2965 if (Values.size() == 1)
2966 return SDOperand();
2967
2968 // Special case for single non-zero element.
Evan Cheng2a9895672007-12-12 06:45:40 +00002969 if (NumNonZero == 1 && NumElems <= 4) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002970 unsigned Idx = CountTrailingZeros_32(NonZeros);
2971 SDOperand Item = Op.getOperand(Idx);
2972 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2973 if (Idx == 0)
2974 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2975 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2976 NumZero > 0, DAG);
Evan Cheng2a9895672007-12-12 06:45:40 +00002977 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2978 return SDOperand();
Evan Chenga9467aa2006-04-25 20:13:52 +00002979
2980 if (EVTBits == 32) {
2981 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2982 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2983 DAG);
2984 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002985 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002986 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002987 for (unsigned i = 0; i < NumElems; i++)
2988 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002989 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2990 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002991 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2992 DAG.getNode(ISD::UNDEF, VT), Mask);
2993 }
2994 }
2995
Dan Gohmanf906c722007-07-24 22:55:08 +00002996 // A vector full of immediates; various special cases are already
2997 // handled, so this is best done with a single constant-pool load.
Evan Cheng2a9895672007-12-12 06:45:40 +00002998 if (!HasNonImms)
Dan Gohmanf906c722007-07-24 22:55:08 +00002999 return SDOperand();
3000
Bill Wendling591eab82007-04-24 21:16:55 +00003001 // Let legalizer expand 2-wide build_vectors.
Evan Chenga9467aa2006-04-25 20:13:52 +00003002 if (EVTBits == 64)
3003 return SDOperand();
3004
3005 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendlingad2db4a2007-03-28 00:57:11 +00003006 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003007 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3008 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00003009 if (V.Val) return V;
3010 }
3011
Bill Wendlingad2db4a2007-03-28 00:57:11 +00003012 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003013 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3014 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00003015 if (V.Val) return V;
3016 }
3017
3018 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner35a08552007-02-25 07:10:00 +00003019 SmallVector<SDOperand, 8> V;
3020 V.resize(NumElems);
Evan Chenga9467aa2006-04-25 20:13:52 +00003021 if (NumElems == 4 && NumZero > 0) {
3022 for (unsigned i = 0; i < 4; ++i) {
3023 bool isZero = !(NonZeros & (1 << i));
3024 if (isZero)
3025 V[i] = getZeroVector(VT, DAG);
3026 else
3027 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3028 }
3029
3030 for (unsigned i = 0; i < 2; ++i) {
3031 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3032 default: break;
3033 case 0:
3034 V[i] = V[i*2]; // Must be a zero vector.
3035 break;
3036 case 1:
3037 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3038 getMOVLMask(NumElems, DAG));
3039 break;
3040 case 2:
3041 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3042 getMOVLMask(NumElems, DAG));
3043 break;
3044 case 3:
3045 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3046 getUnpacklMask(NumElems, DAG));
3047 break;
3048 }
3049 }
3050
Evan Cheng9fee4422006-05-16 07:21:53 +00003051 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003052 // clears the upper bits.
Evan Chenga9467aa2006-04-25 20:13:52 +00003053 // FIXME: we can do the same for v4f32 case when we know both parts of
3054 // the lower half come from scalar_to_vector (loadf32). We should do
3055 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng798b3062006-10-25 20:48:19 +00003056 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Chenga9467aa2006-04-25 20:13:52 +00003057 return V[0];
3058 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00003059 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003060 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00003061 bool Reverse = (NonZeros & 0x3) == 2;
3062 for (unsigned i = 0; i < 2; ++i)
3063 if (Reverse)
3064 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3065 else
3066 MaskVec.push_back(DAG.getConstant(i, EVT));
3067 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3068 for (unsigned i = 0; i < 2; ++i)
3069 if (Reverse)
3070 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3071 else
3072 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnered728e82006-08-11 17:38:39 +00003073 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3074 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003075 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3076 }
3077
3078 if (Values.size() > 2) {
3079 // Expand into a number of unpckl*.
3080 // e.g. for v4f32
3081 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3082 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3083 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3084 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3085 for (unsigned i = 0; i < NumElems; ++i)
3086 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3087 NumElems >>= 1;
3088 while (NumElems != 0) {
3089 for (unsigned i = 0; i < NumElems; ++i)
3090 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3091 UnpckMask);
3092 NumElems >>= 1;
3093 }
3094 return V[0];
3095 }
3096
3097 return SDOperand();
3098}
3099
Evan Chengbfd373a2007-12-07 08:07:39 +00003100static
3101SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3102 SDOperand PermMask, SelectionDAG &DAG,
3103 TargetLowering &TLI) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003104 SDOperand NewV;
Evan Chengbfd373a2007-12-07 08:07:39 +00003105 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3106 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng4fbf4592007-12-11 01:46:18 +00003107 MVT::ValueType PtrVT = TLI.getPointerTy();
3108 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3109 PermMask.Val->op_end());
3110
3111 // First record which half of which vector the low elements come from.
3112 SmallVector<unsigned, 4> LowQuad(4);
3113 for (unsigned i = 0; i < 4; ++i) {
3114 SDOperand Elt = MaskElts[i];
3115 if (Elt.getOpcode() == ISD::UNDEF)
3116 continue;
3117 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3118 int QuadIdx = EltIdx / 4;
3119 ++LowQuad[QuadIdx];
3120 }
3121 int BestLowQuad = -1;
3122 unsigned MaxQuad = 1;
3123 for (unsigned i = 0; i < 4; ++i) {
3124 if (LowQuad[i] > MaxQuad) {
3125 BestLowQuad = i;
3126 MaxQuad = LowQuad[i];
3127 }
Evan Chengbfd373a2007-12-07 08:07:39 +00003128 }
3129
Evan Cheng4fbf4592007-12-11 01:46:18 +00003130 // Record which half of which vector the high elements come from.
3131 SmallVector<unsigned, 4> HighQuad(4);
3132 for (unsigned i = 4; i < 8; ++i) {
3133 SDOperand Elt = MaskElts[i];
3134 if (Elt.getOpcode() == ISD::UNDEF)
3135 continue;
3136 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3137 int QuadIdx = EltIdx / 4;
3138 ++HighQuad[QuadIdx];
3139 }
3140 int BestHighQuad = -1;
3141 MaxQuad = 1;
3142 for (unsigned i = 0; i < 4; ++i) {
3143 if (HighQuad[i] > MaxQuad) {
3144 BestHighQuad = i;
3145 MaxQuad = HighQuad[i];
3146 }
3147 }
3148
3149 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3150 if (BestLowQuad != -1 || BestHighQuad != -1) {
3151 // First sort the 4 chunks in order using shufpd.
3152 SmallVector<SDOperand, 8> MaskVec;
3153 if (BestLowQuad != -1)
3154 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3155 else
3156 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3157 if (BestHighQuad != -1)
3158 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3159 else
3160 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3161 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3162 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3163 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3164 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3165 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3166
3167 // Now sort high and low parts separately.
3168 BitVector InOrder(8);
3169 if (BestLowQuad != -1) {
3170 // Sort lower half in order using PSHUFLW.
3171 MaskVec.clear();
3172 bool AnyOutOrder = false;
3173 for (unsigned i = 0; i != 4; ++i) {
3174 SDOperand Elt = MaskElts[i];
3175 if (Elt.getOpcode() == ISD::UNDEF) {
3176 MaskVec.push_back(Elt);
3177 InOrder.set(i);
3178 } else {
3179 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3180 if (EltIdx != i)
3181 AnyOutOrder = true;
3182 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3183 // If this element is in the right place after this shuffle, then
3184 // remember it.
3185 if ((int)(EltIdx / 4) == BestLowQuad)
3186 InOrder.set(i);
3187 }
3188 }
3189 if (AnyOutOrder) {
3190 for (unsigned i = 4; i != 8; ++i)
3191 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3192 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3193 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3194 }
3195 }
3196
3197 if (BestHighQuad != -1) {
3198 // Sort high half in order using PSHUFHW if possible.
3199 MaskVec.clear();
3200 for (unsigned i = 0; i != 4; ++i)
3201 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3202 bool AnyOutOrder = false;
3203 for (unsigned i = 4; i != 8; ++i) {
3204 SDOperand Elt = MaskElts[i];
3205 if (Elt.getOpcode() == ISD::UNDEF) {
3206 MaskVec.push_back(Elt);
3207 InOrder.set(i);
3208 } else {
3209 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3210 if (EltIdx != i)
3211 AnyOutOrder = true;
3212 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3213 // If this element is in the right place after this shuffle, then
3214 // remember it.
3215 if ((int)(EltIdx / 4) == BestHighQuad)
3216 InOrder.set(i);
3217 }
3218 }
3219 if (AnyOutOrder) {
3220 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3221 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3222 }
3223 }
3224
3225 // The other elements are put in the right place using pextrw and pinsrw.
3226 for (unsigned i = 0; i != 8; ++i) {
3227 if (InOrder[i])
3228 continue;
3229 SDOperand Elt = MaskElts[i];
3230 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3231 if (EltIdx == i)
3232 continue;
3233 SDOperand ExtOp = (EltIdx < 8)
3234 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3235 DAG.getConstant(EltIdx, PtrVT))
3236 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3237 DAG.getConstant(EltIdx - 8, PtrVT));
3238 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3239 DAG.getConstant(i, PtrVT));
3240 }
3241 return NewV;
3242 }
3243
3244 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3245 ///as few as possible.
Evan Chengbfd373a2007-12-07 08:07:39 +00003246 // First, let's find out how many elements are already in the right order.
3247 unsigned V1InOrder = 0;
3248 unsigned V1FromV1 = 0;
3249 unsigned V2InOrder = 0;
3250 unsigned V2FromV2 = 0;
Evan Cheng4fbf4592007-12-11 01:46:18 +00003251 SmallVector<SDOperand, 8> V1Elts;
3252 SmallVector<SDOperand, 8> V2Elts;
Evan Chengbfd373a2007-12-07 08:07:39 +00003253 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003254 SDOperand Elt = MaskElts[i];
Evan Chengbfd373a2007-12-07 08:07:39 +00003255 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003256 V1Elts.push_back(Elt);
3257 V2Elts.push_back(Elt);
Evan Chengbfd373a2007-12-07 08:07:39 +00003258 ++V1InOrder;
3259 ++V2InOrder;
Evan Cheng4fbf4592007-12-11 01:46:18 +00003260 continue;
3261 }
3262 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3263 if (EltIdx == i) {
3264 V1Elts.push_back(Elt);
3265 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3266 ++V1InOrder;
3267 } else if (EltIdx == i+8) {
3268 V1Elts.push_back(Elt);
3269 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3270 ++V2InOrder;
3271 } else if (EltIdx < 8) {
3272 V1Elts.push_back(Elt);
3273 ++V1FromV1;
Evan Chengbfd373a2007-12-07 08:07:39 +00003274 } else {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003275 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3276 ++V2FromV2;
Evan Chengbfd373a2007-12-07 08:07:39 +00003277 }
3278 }
3279
3280 if (V2InOrder > V1InOrder) {
3281 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3282 std::swap(V1, V2);
3283 std::swap(V1Elts, V2Elts);
3284 std::swap(V1FromV1, V2FromV2);
3285 }
3286
Evan Cheng4fbf4592007-12-11 01:46:18 +00003287 if ((V1FromV1 + V1InOrder) != 8) {
3288 // Some elements are from V2.
3289 if (V1FromV1) {
3290 // If there are elements that are from V1 but out of place,
3291 // then first sort them in place
3292 SmallVector<SDOperand, 8> MaskVec;
3293 for (unsigned i = 0; i < 8; ++i) {
3294 SDOperand Elt = V1Elts[i];
3295 if (Elt.getOpcode() == ISD::UNDEF) {
3296 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3297 continue;
3298 }
3299 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3300 if (EltIdx >= 8)
3301 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3302 else
3303 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3304 }
3305 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3306 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengbfd373a2007-12-07 08:07:39 +00003307 }
Evan Cheng4fbf4592007-12-11 01:46:18 +00003308
3309 NewV = V1;
3310 for (unsigned i = 0; i < 8; ++i) {
3311 SDOperand Elt = V1Elts[i];
3312 if (Elt.getOpcode() == ISD::UNDEF)
3313 continue;
3314 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3315 if (EltIdx < 8)
3316 continue;
3317 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3318 DAG.getConstant(EltIdx - 8, PtrVT));
3319 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3320 DAG.getConstant(i, PtrVT));
3321 }
3322 return NewV;
3323 } else {
3324 // All elements are from V1.
3325 NewV = V1;
3326 for (unsigned i = 0; i < 8; ++i) {
3327 SDOperand Elt = V1Elts[i];
3328 if (Elt.getOpcode() == ISD::UNDEF)
3329 continue;
3330 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3331 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3332 DAG.getConstant(EltIdx, PtrVT));
3333 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3334 DAG.getConstant(i, PtrVT));
3335 }
3336 return NewV;
3337 }
3338}
3339
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003340/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3341/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3342/// done when every pair / quad of shuffle mask elements point to elements in
3343/// the right sequence. e.g.
Evan Cheng4fbf4592007-12-11 01:46:18 +00003344/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3345static
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003346SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3347 MVT::ValueType VT,
Evan Cheng4fbf4592007-12-11 01:46:18 +00003348 SDOperand PermMask, SelectionDAG &DAG,
3349 TargetLowering &TLI) {
3350 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003351 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3352 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3353 MVT::ValueType NewVT = MaskVT;
3354 switch (VT) {
3355 case MVT::v4f32: NewVT = MVT::v2f64; break;
3356 case MVT::v4i32: NewVT = MVT::v2i64; break;
3357 case MVT::v8i16: NewVT = MVT::v4i32; break;
3358 case MVT::v16i8: NewVT = MVT::v4i32; break;
3359 default: assert(false && "Unexpected!");
3360 }
3361
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00003362 if (NewWidth == 2) {
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003363 if (MVT::isInteger(VT))
3364 NewVT = MVT::v2i64;
3365 else
3366 NewVT = MVT::v2f64;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00003367 }
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003368 unsigned Scale = NumElems / NewWidth;
3369 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng4fbf4592007-12-11 01:46:18 +00003370 for (unsigned i = 0; i < NumElems; i += Scale) {
3371 unsigned StartIdx = ~0U;
3372 for (unsigned j = 0; j < Scale; ++j) {
3373 SDOperand Elt = PermMask.getOperand(i+j);
3374 if (Elt.getOpcode() == ISD::UNDEF)
3375 continue;
3376 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3377 if (StartIdx == ~0U)
3378 StartIdx = EltIdx - (EltIdx % Scale);
3379 if (EltIdx != StartIdx + j)
3380 return SDOperand();
3381 }
3382 if (StartIdx == ~0U)
3383 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3384 else
3385 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengbfd373a2007-12-07 08:07:39 +00003386 }
3387
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003388 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3389 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3390 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3391 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3392 &MaskVec[0], MaskVec.size()));
Evan Chengbfd373a2007-12-07 08:07:39 +00003393}
3394
Evan Chenga9467aa2006-04-25 20:13:52 +00003395SDOperand
3396X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3397 SDOperand V1 = Op.getOperand(0);
3398 SDOperand V2 = Op.getOperand(1);
3399 SDOperand PermMask = Op.getOperand(2);
3400 MVT::ValueType VT = Op.getValueType();
3401 unsigned NumElems = PermMask.getNumOperands();
3402 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3403 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Cheng949bcc92006-10-16 06:36:00 +00003404 bool V1IsSplat = false;
3405 bool V2IsSplat = false;
Evan Chenga9467aa2006-04-25 20:13:52 +00003406
Evan Cheng89c5d042006-09-08 01:50:06 +00003407 if (isUndefShuffle(Op.Val))
3408 return DAG.getNode(ISD::UNDEF, VT);
3409
Evan Chengafa1cb62007-05-17 18:45:50 +00003410 if (isZeroShuffle(Op.Val))
3411 return getZeroVector(VT, DAG);
3412
Evan Chengcea02ff2007-06-19 00:02:56 +00003413 if (isIdentityMask(PermMask.Val))
3414 return V1;
3415 else if (isIdentityMask(PermMask.Val, true))
3416 return V2;
3417
Evan Chenga9467aa2006-04-25 20:13:52 +00003418 if (isSplatMask(PermMask.Val)) {
3419 if (NumElems <= 4) return Op;
3420 // Promote it to a v4i32 splat.
Evan Cheng798b3062006-10-25 20:48:19 +00003421 return PromoteSplat(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003422 }
3423
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003424 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3425 // do it!
3426 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3427 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3428 if (NewOp.Val)
3429 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3430 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3431 // FIXME: Figure out a cleaner way to do this.
3432 // Try to make use of movq to zero out the top part.
3433 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3434 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3435 if (NewOp.Val) {
3436 SDOperand NewV1 = NewOp.getOperand(0);
3437 SDOperand NewV2 = NewOp.getOperand(1);
3438 SDOperand NewMask = NewOp.getOperand(2);
3439 if (isCommutedMOVL(NewMask.Val, true, false)) {
3440 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3441 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3442 NewV1, NewV2, getMOVLMask(2, DAG));
3443 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3444 }
3445 }
3446 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3447 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3448 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3449 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3450 }
3451 }
3452
Evan Cheng798b3062006-10-25 20:48:19 +00003453 if (X86::isMOVLMask(PermMask.Val))
3454 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003455
Evan Cheng798b3062006-10-25 20:48:19 +00003456 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3457 X86::isMOVSLDUPMask(PermMask.Val) ||
3458 X86::isMOVHLPSMask(PermMask.Val) ||
3459 X86::isMOVHPMask(PermMask.Val) ||
3460 X86::isMOVLPMask(PermMask.Val))
3461 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003462
Evan Cheng798b3062006-10-25 20:48:19 +00003463 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3464 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Chengc415c5b2006-10-25 21:49:50 +00003465 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003466
Evan Chengc415c5b2006-10-25 21:49:50 +00003467 bool Commuted = false;
Chris Lattner5728bdd2007-11-25 00:24:49 +00003468 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3469 // 1,1,1,1 -> v8i16 though.
Evan Cheng798b3062006-10-25 20:48:19 +00003470 V1IsSplat = isSplatVector(V1.Val);
3471 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner5728bdd2007-11-25 00:24:49 +00003472
3473 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng798b3062006-10-25 20:48:19 +00003474 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Chengc415c5b2006-10-25 21:49:50 +00003475 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00003476 std::swap(V1IsSplat, V2IsSplat);
3477 std::swap(V1IsUndef, V2IsUndef);
Evan Chengc415c5b2006-10-25 21:49:50 +00003478 Commuted = true;
Evan Cheng798b3062006-10-25 20:48:19 +00003479 }
3480
Evan Cheng23d2d4d2007-12-15 03:00:47 +00003481 // FIXME: Figure out a cleaner way to do this.
Evan Cheng798b3062006-10-25 20:48:19 +00003482 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3483 if (V2IsUndef) return V1;
Evan Chengc415c5b2006-10-25 21:49:50 +00003484 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00003485 if (V2IsSplat) {
3486 // V2 is a splat, so the mask may be malformed. That is, it may point
3487 // to any V2 element. The instruction selectior won't like this. Get
3488 // a corrected mask and commute to form a proper MOVS{S|D}.
3489 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3490 if (NewMask.Val != PermMask.Val)
3491 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00003492 }
Evan Cheng798b3062006-10-25 20:48:19 +00003493 return Op;
Evan Cheng949bcc92006-10-16 06:36:00 +00003494 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003495
Evan Cheng949bcc92006-10-16 06:36:00 +00003496 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling591eab82007-04-24 21:16:55 +00003497 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng949bcc92006-10-16 06:36:00 +00003498 X86::isUNPCKLMask(PermMask.Val) ||
3499 X86::isUNPCKHMask(PermMask.Val))
3500 return Op;
Evan Cheng8c5766e2006-10-04 18:33:38 +00003501
Evan Cheng798b3062006-10-25 20:48:19 +00003502 if (V2IsSplat) {
3503 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003504 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng798b3062006-10-25 20:48:19 +00003505 // new vector_shuffle with the corrected mask.
3506 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3507 if (NewMask.Val != PermMask.Val) {
3508 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3509 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3510 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3511 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3512 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3513 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00003514 }
3515 }
3516 }
3517
3518 // Normalize the node to match x86 shuffle ops if needed
Evan Chengc415c5b2006-10-25 21:49:50 +00003519 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3520 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3521
3522 if (Commuted) {
3523 // Commute is back and try unpck* again.
3524 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3525 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling591eab82007-04-24 21:16:55 +00003526 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengc415c5b2006-10-25 21:49:50 +00003527 X86::isUNPCKLMask(PermMask.Val) ||
3528 X86::isUNPCKHMask(PermMask.Val))
3529 return Op;
3530 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003531
3532 // If VT is integer, try PSHUF* first, then SHUFP*.
3533 if (MVT::isInteger(VT)) {
Dan Gohman8932bff2007-08-02 21:17:01 +00003534 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3535 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3536 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3537 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Chenga9467aa2006-04-25 20:13:52 +00003538 X86::isPSHUFHWMask(PermMask.Val) ||
3539 X86::isPSHUFLWMask(PermMask.Val)) {
3540 if (V2.getOpcode() != ISD::UNDEF)
3541 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3542 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3543 return Op;
3544 }
3545
Chris Lattnerdade6072007-05-17 17:13:13 +00003546 if (X86::isSHUFPMask(PermMask.Val) &&
3547 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Chenga9467aa2006-04-25 20:13:52 +00003548 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003549 } else {
3550 // Floating point cases in the other order.
3551 if (X86::isSHUFPMask(PermMask.Val))
3552 return Op;
3553 if (X86::isPSHUFDMask(PermMask.Val) ||
3554 X86::isPSHUFHWMask(PermMask.Val) ||
3555 X86::isPSHUFLWMask(PermMask.Val)) {
3556 if (V2.getOpcode() != ISD::UNDEF)
3557 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3558 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3559 return Op;
3560 }
3561 }
3562
Evan Cheng4fbf4592007-12-11 01:46:18 +00003563 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3564 if (VT == MVT::v8i16) {
3565 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3566 if (NewOp.Val)
3567 return NewOp;
3568 }
3569
3570 // Handle all 4 wide cases with a number of shuffles.
3571 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengbfd373a2007-12-07 08:07:39 +00003572 // Don't do this for MMX.
Evan Chenga9467aa2006-04-25 20:13:52 +00003573 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00003574 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00003575 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng3cd43622006-04-28 07:03:38 +00003576 Locs.reserve(NumElems);
Evan Cheng4fbf4592007-12-11 01:46:18 +00003577 SmallVector<SDOperand, 8> Mask1(NumElems,
3578 DAG.getNode(ISD::UNDEF, MaskEVT));
3579 SmallVector<SDOperand, 8> Mask2(NumElems,
3580 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng3cd43622006-04-28 07:03:38 +00003581 unsigned NumHi = 0;
3582 unsigned NumLo = 0;
3583 // If no more than two elements come from either vector. This can be
3584 // implemented with two shuffles. First shuffle gather the elements.
3585 // The second shuffle, which takes the first shuffle as both of its
3586 // vector operands, put the elements into the right order.
3587 for (unsigned i = 0; i != NumElems; ++i) {
3588 SDOperand Elt = PermMask.getOperand(i);
3589 if (Elt.getOpcode() == ISD::UNDEF) {
3590 Locs[i] = std::make_pair(-1, -1);
3591 } else {
3592 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3593 if (Val < NumElems) {
3594 Locs[i] = std::make_pair(0, NumLo);
3595 Mask1[NumLo] = Elt;
3596 NumLo++;
3597 } else {
3598 Locs[i] = std::make_pair(1, NumHi);
3599 if (2+NumHi < NumElems)
3600 Mask1[2+NumHi] = Elt;
3601 NumHi++;
3602 }
3603 }
3604 }
3605 if (NumLo <= 2 && NumHi <= 2) {
3606 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00003607 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3608 &Mask1[0], Mask1.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00003609 for (unsigned i = 0; i != NumElems; ++i) {
3610 if (Locs[i].first == -1)
3611 continue;
3612 else {
3613 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3614 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3615 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3616 }
3617 }
3618
3619 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnered728e82006-08-11 17:38:39 +00003620 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3621 &Mask2[0], Mask2.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00003622 }
3623
3624 // Break it into (shuffle shuffle_hi, shuffle_lo).
3625 Locs.clear();
Chris Lattner35a08552007-02-25 07:10:00 +00003626 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3627 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3628 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Chenga9467aa2006-04-25 20:13:52 +00003629 unsigned MaskIdx = 0;
3630 unsigned LoIdx = 0;
3631 unsigned HiIdx = NumElems/2;
3632 for (unsigned i = 0; i != NumElems; ++i) {
3633 if (i == NumElems/2) {
3634 MaskPtr = &HiMask;
3635 MaskIdx = 1;
3636 LoIdx = 0;
3637 HiIdx = NumElems/2;
3638 }
3639 SDOperand Elt = PermMask.getOperand(i);
3640 if (Elt.getOpcode() == ISD::UNDEF) {
3641 Locs[i] = std::make_pair(-1, -1);
3642 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3643 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3644 (*MaskPtr)[LoIdx] = Elt;
3645 LoIdx++;
3646 } else {
3647 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3648 (*MaskPtr)[HiIdx] = Elt;
3649 HiIdx++;
3650 }
3651 }
3652
Chris Lattner3d826992006-05-16 06:45:34 +00003653 SDOperand LoShuffle =
3654 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00003655 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3656 &LoMask[0], LoMask.size()));
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003657 SDOperand HiShuffle =
Chris Lattner3d826992006-05-16 06:45:34 +00003658 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00003659 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3660 &HiMask[0], HiMask.size()));
Chris Lattner35a08552007-02-25 07:10:00 +00003661 SmallVector<SDOperand, 8> MaskOps;
Evan Chenga9467aa2006-04-25 20:13:52 +00003662 for (unsigned i = 0; i != NumElems; ++i) {
3663 if (Locs[i].first == -1) {
3664 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3665 } else {
3666 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3667 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3668 }
3669 }
3670 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnered728e82006-08-11 17:38:39 +00003671 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3672 &MaskOps[0], MaskOps.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00003673 }
3674
3675 return SDOperand();
3676}
3677
3678SDOperand
Nate Begeman2d77e8e42008-02-11 04:19:36 +00003679X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3680 SelectionDAG &DAG) {
3681 MVT::ValueType VT = Op.getValueType();
3682 if (MVT::getSizeInBits(VT) == 8) {
3683 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3684 Op.getOperand(0), Op.getOperand(1));
3685 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3686 DAG.getValueType(VT));
3687 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3688 } else if (MVT::getSizeInBits(VT) == 16) {
3689 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3690 Op.getOperand(0), Op.getOperand(1));
3691 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3692 DAG.getValueType(VT));
3693 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3694 }
3695 return SDOperand();
3696}
3697
3698
3699SDOperand
Evan Chenga9467aa2006-04-25 20:13:52 +00003700X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3701 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3702 return SDOperand();
3703
Nate Begeman2d77e8e42008-02-11 04:19:36 +00003704 if (Subtarget->hasSSE41())
3705 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3706
Evan Chenga9467aa2006-04-25 20:13:52 +00003707 MVT::ValueType VT = Op.getValueType();
3708 // TODO: handle v16i8.
3709 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng4fbf4592007-12-11 01:46:18 +00003710 SDOperand Vec = Op.getOperand(0);
3711 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3712 if (Idx == 0)
3713 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3714 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3715 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3716 Op.getOperand(1)));
Evan Chenga9467aa2006-04-25 20:13:52 +00003717 // Transform it so it match pextrw which produces a 32-bit result.
3718 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3719 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3720 Op.getOperand(0), Op.getOperand(1));
3721 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3722 DAG.getValueType(VT));
3723 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3724 } else if (MVT::getSizeInBits(VT) == 32) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003725 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3726 if (Idx == 0)
3727 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00003728 // SHUFPS the element to the lowest double word, then movss.
3729 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00003730 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00003731 IdxVec.
3732 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3733 IdxVec.
3734 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3735 IdxVec.
3736 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3737 IdxVec.
3738 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00003739 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3740 &IdxVec[0], IdxVec.size());
Evan Cheng4fbf4592007-12-11 01:46:18 +00003741 SDOperand Vec = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003742 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng922e1912006-11-07 22:14:24 +00003743 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Chenga9467aa2006-04-25 20:13:52 +00003744 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner72733e52008-01-17 07:00:52 +00003745 DAG.getIntPtrConstant(0));
Evan Chenga9467aa2006-04-25 20:13:52 +00003746 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begeman2d77e8e42008-02-11 04:19:36 +00003747 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3748 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3749 // to match extract_elt for f64.
Evan Chenga9467aa2006-04-25 20:13:52 +00003750 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3751 if (Idx == 0)
3752 return Op;
3753
3754 // UNPCKHPD the element to the lowest double word, then movsd.
3755 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3756 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3757 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00003758 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman5c441312007-06-14 22:58:02 +00003759 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00003760 IdxVec.
3761 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00003762 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3763 &IdxVec[0], IdxVec.size());
Evan Cheng4fbf4592007-12-11 01:46:18 +00003764 SDOperand Vec = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003765 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3766 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3767 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner72733e52008-01-17 07:00:52 +00003768 DAG.getIntPtrConstant(0));
Evan Chenga9467aa2006-04-25 20:13:52 +00003769 }
3770
3771 return SDOperand();
3772}
3773
3774SDOperand
Nate Begeman2d77e8e42008-02-11 04:19:36 +00003775X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3776 MVT::ValueType VT = Op.getValueType();
3777 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3778
3779 SDOperand N0 = Op.getOperand(0);
3780 SDOperand N1 = Op.getOperand(1);
3781 SDOperand N2 = Op.getOperand(2);
3782
3783 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3784 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3785 : X86ISD::PINSRW;
3786 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3787 // argument.
3788 if (N1.getValueType() != MVT::i32)
3789 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3790 if (N2.getValueType() != MVT::i32)
3791 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3792 return DAG.getNode(Opc, VT, N0, N1, N2);
3793 } else if (EVT == MVT::f32) {
3794 // Bits [7:6] of the constant are the source select. This will always be
3795 // zero here. The DAG Combiner may combine an extract_elt index into these
3796 // bits. For example (insert (extract, 3), 2) could be matched by putting
3797 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3798 // Bits [5:4] of the constant are the destination select. This is the
3799 // value of the incoming immediate.
3800 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3801 // combine either bitwise AND or insert of float 0.0 to set these bits.
3802 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3803 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3804 }
3805 return SDOperand();
3806}
3807
3808SDOperand
Evan Chenga9467aa2006-04-25 20:13:52 +00003809X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003810 MVT::ValueType VT = Op.getValueType();
Evan Cheng0f427302007-12-12 07:55:34 +00003811 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begeman2d77e8e42008-02-11 04:19:36 +00003812
3813 if (Subtarget->hasSSE41())
3814 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3815
Evan Cheng0f427302007-12-12 07:55:34 +00003816 if (EVT == MVT::i8)
3817 return SDOperand();
3818
Evan Chenga9467aa2006-04-25 20:13:52 +00003819 SDOperand N0 = Op.getOperand(0);
3820 SDOperand N1 = Op.getOperand(1);
3821 SDOperand N2 = Op.getOperand(2);
Evan Cheng0f427302007-12-12 07:55:34 +00003822
3823 if (MVT::getSizeInBits(EVT) == 16) {
3824 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3825 // as its second argument.
Evan Chenga9467aa2006-04-25 20:13:52 +00003826 if (N1.getValueType() != MVT::i32)
3827 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3828 if (N2.getValueType() != MVT::i32)
Chris Lattner72733e52008-01-17 07:00:52 +00003829 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Chenga9467aa2006-04-25 20:13:52 +00003830 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Chenga9467aa2006-04-25 20:13:52 +00003831 }
Nate Begeman22950d22008-01-05 20:51:30 +00003832 return SDOperand();
Evan Chenga9467aa2006-04-25 20:13:52 +00003833}
3834
3835SDOperand
3836X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3837 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Cheng6200c222008-02-18 23:04:32 +00003838 MVT::ValueType VT = MVT::v2i32;
3839 switch (Op.getValueType()) {
3840 default: break;
3841 case MVT::v16i8:
3842 case MVT::v8i16:
3843 VT = MVT::v4i32;
3844 break;
3845 }
3846 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3847 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Chenga9467aa2006-04-25 20:13:52 +00003848}
3849
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003850// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Chenga9467aa2006-04-25 20:13:52 +00003851// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3852// one of the above mentioned nodes. It has to be wrapped because otherwise
3853// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3854// be used to form addressing mode. These wrapped nodes will be selected
3855// into MOV32ri.
3856SDOperand
3857X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3858 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng0b169222006-11-29 23:19:46 +00003859 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3860 getPointerTy(),
3861 CP->getAlignment());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003862 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003863 // With PIC, the address is actually $g + Offset.
3864 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3865 !Subtarget->isPICStyleRIPRel()) {
3866 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3867 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3868 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003869 }
3870
3871 return Result;
3872}
3873
3874SDOperand
3875X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3876 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng0b169222006-11-29 23:19:46 +00003877 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chengefd142a2008-02-02 04:07:54 +00003878 // If it's a debug information descriptor, don't mess with it.
3879 if (DAG.isVerifiedDebugInfoDesc(Op))
3880 return Result;
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003881 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003882 // With PIC, the address is actually $g + Offset.
3883 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3884 !Subtarget->isPICStyleRIPRel()) {
3885 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3886 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3887 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003888 }
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00003889
3890 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3891 // load the value at address GV, not the value of GV itself. This means that
3892 // the GlobalAddress must be in the base or index register of the address, not
3893 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003894 // The same applies for external symbols during PIC codegen
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00003895 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman2d489b52008-02-06 22:27:42 +00003896 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman16d4bc32008-02-07 18:41:25 +00003897 PseudoSourceValue::getGOT(), 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003898
3899 return Result;
3900}
3901
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00003902// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3903static SDOperand
3904LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3905 const MVT::ValueType PtrVT) {
3906 SDOperand InFlag;
3907 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3908 DAG.getNode(X86ISD::GlobalBaseReg,
3909 PtrVT), InFlag);
3910 InFlag = Chain.getValue(1);
3911
3912 // emit leal symbol@TLSGD(,%ebx,1), %eax
3913 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3914 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3915 GA->getValueType(0),
3916 GA->getOffset());
3917 SDOperand Ops[] = { Chain, TGA, InFlag };
3918 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3919 InFlag = Result.getValue(2);
3920 Chain = Result.getValue(1);
3921
3922 // call ___tls_get_addr. This function receives its argument in
3923 // the register EAX.
3924 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3925 InFlag = Chain.getValue(1);
3926
3927 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3928 SDOperand Ops1[] = { Chain,
3929 DAG.getTargetExternalSymbol("___tls_get_addr",
3930 PtrVT),
3931 DAG.getRegister(X86::EAX, PtrVT),
3932 DAG.getRegister(X86::EBX, PtrVT),
3933 InFlag };
3934 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3935 InFlag = Chain.getValue(1);
3936
3937 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3938}
3939
3940// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3941// "local exec" model.
3942static SDOperand
3943LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3944 const MVT::ValueType PtrVT) {
3945 // Get the Thread Pointer
3946 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3947 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3948 // exec)
3949 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3950 GA->getValueType(0),
3951 GA->getOffset());
3952 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancioefb80772007-04-22 22:50:52 +00003953
3954 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman2d489b52008-02-06 22:27:42 +00003955 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman16d4bc32008-02-07 18:41:25 +00003956 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancioefb80772007-04-22 22:50:52 +00003957
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00003958 // The address of the thread local variable is the add of the thread
3959 // pointer with the offset of the variable.
3960 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3961}
3962
3963SDOperand
3964X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3965 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00003966 // TODO: implement the "initial exec"model for pic executables
3967 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3968 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00003969 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3970 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3971 // otherwise use the "Local Exec"TLS Model
3972 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3973 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3974 else
3975 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3976}
3977
Evan Chenga9467aa2006-04-25 20:13:52 +00003978SDOperand
3979X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3980 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Cheng0b169222006-11-29 23:19:46 +00003981 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003982 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003983 // With PIC, the address is actually $g + Offset.
3984 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3985 !Subtarget->isPICStyleRIPRel()) {
3986 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3987 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3988 Result);
3989 }
3990
3991 return Result;
3992}
3993
3994SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3995 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3996 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3997 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3998 // With PIC, the address is actually $g + Offset.
3999 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4000 !Subtarget->isPICStyleRIPRel()) {
4001 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4002 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4003 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00004004 }
4005
4006 return Result;
4007}
4008
Chris Lattner693cbea2007-10-17 06:02:13 +00004009/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4010/// take a 2 x i32 value to shift plus a shift amount.
Evan Chenga9467aa2006-04-25 20:13:52 +00004011SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner693cbea2007-10-17 06:02:13 +00004012 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4013 "Not an i64 shift!");
4014 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4015 SDOperand ShOpLo = Op.getOperand(0);
4016 SDOperand ShOpHi = Op.getOperand(1);
4017 SDOperand ShAmt = Op.getOperand(2);
4018 SDOperand Tmp1 = isSRA ?
4019 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4020 DAG.getConstant(0, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00004021
Chris Lattner693cbea2007-10-17 06:02:13 +00004022 SDOperand Tmp2, Tmp3;
4023 if (Op.getOpcode() == ISD::SHL_PARTS) {
4024 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4025 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4026 } else {
4027 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4028 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4029 }
Evan Cheng9c249c32006-01-09 18:33:28 +00004030
Chris Lattner693cbea2007-10-17 06:02:13 +00004031 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4032 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4033 DAG.getConstant(32, MVT::i8));
4034 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4035 AndNode, DAG.getConstant(0, MVT::i8));
Evan Cheng9c249c32006-01-09 18:33:28 +00004036
Chris Lattner693cbea2007-10-17 06:02:13 +00004037 SDOperand Hi, Lo;
4038 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4039 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4040 SmallVector<SDOperand, 4> Ops;
4041 if (Op.getOpcode() == ISD::SHL_PARTS) {
4042 Ops.push_back(Tmp2);
4043 Ops.push_back(Tmp3);
4044 Ops.push_back(CC);
4045 Ops.push_back(Cond);
4046 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00004047
Evan Cheng9c249c32006-01-09 18:33:28 +00004048 Ops.clear();
Chris Lattner693cbea2007-10-17 06:02:13 +00004049 Ops.push_back(Tmp3);
4050 Ops.push_back(Tmp1);
4051 Ops.push_back(CC);
4052 Ops.push_back(Cond);
4053 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4054 } else {
4055 Ops.push_back(Tmp2);
4056 Ops.push_back(Tmp3);
4057 Ops.push_back(CC);
4058 Ops.push_back(Cond);
4059 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4060
4061 Ops.clear();
4062 Ops.push_back(Tmp3);
4063 Ops.push_back(Tmp1);
4064 Ops.push_back(CC);
4065 Ops.push_back(Cond);
4066 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4067 }
4068
4069 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4070 Ops.clear();
4071 Ops.push_back(Lo);
4072 Ops.push_back(Hi);
4073 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004074}
Evan Cheng6305e502006-01-12 22:54:21 +00004075
Evan Chenga9467aa2006-04-25 20:13:52 +00004076SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4077 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4078 Op.getOperand(0).getValueType() >= MVT::i16 &&
4079 "Unknown SINT_TO_FP to lower!");
4080
4081 SDOperand Result;
4082 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4083 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4084 MachineFunction &MF = DAG.getMachineFunction();
4085 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4086 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chengdf9ac472006-10-05 23:01:46 +00004087 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman2d489b52008-02-06 22:27:42 +00004088 StackSlot,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004089 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004090 SSFI);
Evan Chenga9467aa2006-04-25 20:13:52 +00004091
Dale Johannesen98d3a082007-09-14 22:26:36 +00004092 // These are really Legal; caller falls through into that case.
Chris Lattnere8bb9f22008-01-16 06:24:21 +00004093 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene36c4002007-09-23 14:52:20 +00004094 return Result;
Chris Lattner14e616e2008-01-16 06:19:45 +00004095 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen7d67e542007-09-19 23:55:34 +00004096 Subtarget->is64Bit())
4097 return Result;
Dale Johannesen98d3a082007-09-14 22:26:36 +00004098
Evan Chenga9467aa2006-04-25 20:13:52 +00004099 // Build the FILD
Chris Lattner35a08552007-02-25 07:10:00 +00004100 SDVTList Tys;
Chris Lattnere8bb9f22008-01-16 06:24:21 +00004101 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen98d3a082007-09-14 22:26:36 +00004102 if (useSSE)
Chris Lattner35a08552007-02-25 07:10:00 +00004103 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4104 else
Dale Johannesena2b3c172007-07-03 00:53:03 +00004105 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00004106 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00004107 Ops.push_back(Chain);
4108 Ops.push_back(StackSlot);
4109 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen98d3a082007-09-14 22:26:36 +00004110 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004111 Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004112
Dale Johannesen98d3a082007-09-14 22:26:36 +00004113 if (useSSE) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004114 Chain = Result.getValue(1);
4115 SDOperand InFlag = Result.getValue(2);
4116
4117 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4118 // shouldn't be necessary except that RFP cannot be live across
4119 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00004120 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00004121 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00004122 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner35a08552007-02-25 07:10:00 +00004123 Tys = DAG.getVTList(MVT::Other);
4124 SmallVector<SDOperand, 8> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00004125 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00004126 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00004127 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00004128 Ops.push_back(DAG.getValueType(Op.getValueType()));
4129 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004130 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman2d489b52008-02-06 22:27:42 +00004131 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004132 PseudoSourceValue::getFixedStack(), SSFI);
Chris Lattner76ac0682005-11-15 00:40:23 +00004133 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004134
Evan Chenga9467aa2006-04-25 20:13:52 +00004135 return Result;
4136}
4137
Chris Lattnerf81d5882007-11-24 07:07:01 +00004138std::pair<SDOperand,SDOperand> X86TargetLowering::
4139FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004140 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4141 "Unknown FP_TO_SINT to lower!");
Evan Chenga9467aa2006-04-25 20:13:52 +00004142
Dale Johannesen98d3a082007-09-14 22:26:36 +00004143 // These are really Legal.
Dale Johannesene36c4002007-09-23 14:52:20 +00004144 if (Op.getValueType() == MVT::i32 &&
Chris Lattnere8bb9f22008-01-16 06:24:21 +00004145 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerf81d5882007-11-24 07:07:01 +00004146 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen7d67e542007-09-19 23:55:34 +00004147 if (Subtarget->is64Bit() &&
4148 Op.getValueType() == MVT::i64 &&
4149 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerf81d5882007-11-24 07:07:01 +00004150 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen98d3a082007-09-14 22:26:36 +00004151
Evan Cheng7bcfd8f2007-10-15 20:11:21 +00004152 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4153 // stack slot.
4154 MachineFunction &MF = DAG.getMachineFunction();
4155 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4156 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4157 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga9467aa2006-04-25 20:13:52 +00004158 unsigned Opc;
4159 switch (Op.getValueType()) {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004160 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4161 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4162 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4163 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00004164 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004165
Evan Chenga9467aa2006-04-25 20:13:52 +00004166 SDOperand Chain = DAG.getEntryNode();
4167 SDOperand Value = Op.getOperand(0);
Chris Lattnere8bb9f22008-01-16 06:24:21 +00004168 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004169 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman2d489b52008-02-06 22:27:42 +00004170 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004171 PseudoSourceValue::getFixedStack(), SSFI);
Dale Johannesena2b3c172007-07-03 00:53:03 +00004172 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00004173 SDOperand Ops[] = {
4174 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4175 };
4176 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00004177 Chain = Value.getValue(1);
4178 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4179 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4180 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004181
Evan Chenga9467aa2006-04-25 20:13:52 +00004182 // Build the FP_TO_INT*_IN_MEM
Chris Lattner35a08552007-02-25 07:10:00 +00004183 SDOperand Ops[] = { Chain, Value, StackSlot };
4184 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Cheng172fce72006-01-06 00:43:03 +00004185
Chris Lattnerf81d5882007-11-24 07:07:01 +00004186 return std::make_pair(FIST, StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00004187}
4188
Chris Lattnerf81d5882007-11-24 07:07:01 +00004189SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004190 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4191 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4192 if (FIST.Val == 0) return SDOperand();
4193
4194 // Load the result.
4195 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4196}
4197
4198SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4199 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4200 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4201 if (FIST.Val == 0) return 0;
4202
4203 // Return an i64 load from the stack slot.
4204 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4205
4206 // Use a MERGE_VALUES node to drop the chain result value.
4207 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4208}
4209
Evan Chenga9467aa2006-04-25 20:13:52 +00004210SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4211 MVT::ValueType VT = Op.getValueType();
Dan Gohman57111e72007-07-10 00:05:58 +00004212 MVT::ValueType EltVT = VT;
4213 if (MVT::isVector(VT))
4214 EltVT = MVT::getVectorElementType(VT);
4215 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Chenga9467aa2006-04-25 20:13:52 +00004216 std::vector<Constant*> CV;
Dan Gohman57111e72007-07-10 00:05:58 +00004217 if (EltVT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004218 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman57111e72007-07-10 00:05:58 +00004219 CV.push_back(C);
4220 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004221 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004222 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman57111e72007-07-10 00:05:58 +00004223 CV.push_back(C);
4224 CV.push_back(C);
4225 CV.push_back(C);
4226 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004227 }
Dan Gohman47885522007-07-27 17:16:43 +00004228 Constant *C = ConstantVector::get(CV);
4229 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman2d489b52008-02-06 22:27:42 +00004230 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004231 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman47885522007-07-27 17:16:43 +00004232 false, 16);
Evan Chenga9467aa2006-04-25 20:13:52 +00004233 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4234}
4235
4236SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4237 MVT::ValueType VT = Op.getValueType();
Dan Gohman57111e72007-07-10 00:05:58 +00004238 MVT::ValueType EltVT = VT;
Evan Cheng64738532007-07-19 23:36:01 +00004239 unsigned EltNum = 1;
4240 if (MVT::isVector(VT)) {
Dan Gohman57111e72007-07-10 00:05:58 +00004241 EltVT = MVT::getVectorElementType(VT);
Evan Cheng64738532007-07-19 23:36:01 +00004242 EltNum = MVT::getVectorNumElements(VT);
4243 }
Dan Gohman57111e72007-07-10 00:05:58 +00004244 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Chenga9467aa2006-04-25 20:13:52 +00004245 std::vector<Constant*> CV;
Dan Gohman57111e72007-07-10 00:05:58 +00004246 if (EltVT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004247 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman57111e72007-07-10 00:05:58 +00004248 CV.push_back(C);
4249 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004250 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004251 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman57111e72007-07-10 00:05:58 +00004252 CV.push_back(C);
4253 CV.push_back(C);
4254 CV.push_back(C);
4255 CV.push_back(C);
Evan Chenga9467aa2006-04-25 20:13:52 +00004256 }
Dan Gohman47885522007-07-27 17:16:43 +00004257 Constant *C = ConstantVector::get(CV);
4258 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman2d489b52008-02-06 22:27:42 +00004259 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004260 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman47885522007-07-27 17:16:43 +00004261 false, 16);
Evan Cheng64738532007-07-19 23:36:01 +00004262 if (MVT::isVector(VT)) {
Evan Cheng64738532007-07-19 23:36:01 +00004263 return DAG.getNode(ISD::BIT_CONVERT, VT,
4264 DAG.getNode(ISD::XOR, MVT::v2i64,
4265 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4266 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4267 } else {
Evan Cheng64738532007-07-19 23:36:01 +00004268 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4269 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004270}
4271
Evan Cheng4363e882007-01-05 07:55:56 +00004272SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng82241c82007-01-05 21:37:56 +00004273 SDOperand Op0 = Op.getOperand(0);
4274 SDOperand Op1 = Op.getOperand(1);
Evan Cheng4363e882007-01-05 07:55:56 +00004275 MVT::ValueType VT = Op.getValueType();
Evan Cheng82241c82007-01-05 21:37:56 +00004276 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng4363e882007-01-05 07:55:56 +00004277 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00004278
4279 // If second operand is smaller, extend it first.
4280 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4281 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4282 SrcVT = VT;
Dale Johannesenbed9dc42007-09-06 18:13:44 +00004283 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00004284 }
Dale Johannesen8ee70112007-10-21 01:07:44 +00004285 // And if it is bigger, shrink it first.
4286 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner72733e52008-01-17 07:00:52 +00004287 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen8ee70112007-10-21 01:07:44 +00004288 SrcVT = VT;
4289 SrcTy = MVT::getTypeForValueType(SrcVT);
4290 }
4291
4292 // At this point the operands and the result should have the same
4293 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng82241c82007-01-05 21:37:56 +00004294
Evan Cheng4363e882007-01-05 07:55:56 +00004295 // First get the sign bit of second operand.
4296 std::vector<Constant*> CV;
4297 if (SrcVT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004298 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4299 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng4363e882007-01-05 07:55:56 +00004300 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004301 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4302 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4303 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4304 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng4363e882007-01-05 07:55:56 +00004305 }
Dan Gohman47885522007-07-27 17:16:43 +00004306 Constant *C = ConstantVector::get(CV);
4307 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman2d489b52008-02-06 22:27:42 +00004308 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004309 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman47885522007-07-27 17:16:43 +00004310 false, 16);
Evan Cheng82241c82007-01-05 21:37:56 +00004311 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng4363e882007-01-05 07:55:56 +00004312
4313 // Shift sign bit right or left if the two operands have different types.
4314 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4315 // Op0 is MVT::f32, Op1 is MVT::f64.
4316 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4317 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4318 DAG.getConstant(32, MVT::i32));
4319 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4320 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner72733e52008-01-17 07:00:52 +00004321 DAG.getIntPtrConstant(0));
Evan Cheng4363e882007-01-05 07:55:56 +00004322 }
4323
Evan Cheng82241c82007-01-05 21:37:56 +00004324 // Clear first operand sign bit.
4325 CV.clear();
4326 if (VT == MVT::f64) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004327 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4328 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng82241c82007-01-05 21:37:56 +00004329 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +00004330 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4331 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4332 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4333 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng82241c82007-01-05 21:37:56 +00004334 }
Dan Gohman47885522007-07-27 17:16:43 +00004335 C = ConstantVector::get(CV);
4336 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman2d489b52008-02-06 22:27:42 +00004337 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004338 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman47885522007-07-27 17:16:43 +00004339 false, 16);
Evan Cheng82241c82007-01-05 21:37:56 +00004340 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4341
4342 // Or the value with the sign bit.
4343 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng4363e882007-01-05 07:55:56 +00004344}
4345
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004346SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge95f3912007-09-25 01:57:46 +00004347 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng9b7f0e62007-09-26 00:45:55 +00004348 SDOperand Cond;
Evan Chenge95f3912007-09-25 01:57:46 +00004349 SDOperand Op0 = Op.getOperand(0);
4350 SDOperand Op1 = Op.getOperand(1);
4351 SDOperand CC = Op.getOperand(2);
4352 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4353 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4354 unsigned X86CC;
4355
Evan Chenge95f3912007-09-25 01:57:46 +00004356 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng9b7f0e62007-09-26 00:45:55 +00004357 Op0, Op1, DAG)) {
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004358 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4359 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004360 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng9b7f0e62007-09-26 00:45:55 +00004361 }
Evan Chenge95f3912007-09-25 01:57:46 +00004362
4363 assert(isFP && "Illegal integer SetCC!");
4364
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004365 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Chenge95f3912007-09-25 01:57:46 +00004366 switch (SetCCOpcode) {
4367 default: assert(false && "Illegal floating point SetCC!");
4368 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004369 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004370 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004371 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004372 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4373 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4374 }
4375 case ISD::SETUNE: { // PF | !ZF
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004376 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004377 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004378 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge95f3912007-09-25 01:57:46 +00004379 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4380 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4381 }
4382 }
4383}
4384
4385
Evan Chenga9467aa2006-04-25 20:13:52 +00004386SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00004387 bool addTest = true;
Evan Cheng4259a0f2006-09-11 02:19:56 +00004388 SDOperand Cond = Op.getOperand(0);
4389 SDOperand CC;
Evan Cheng944d1e92006-01-26 02:13:10 +00004390
Evan Cheng4259a0f2006-09-11 02:19:56 +00004391 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004392 Cond = LowerSETCC(Cond, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004393
Evan Chengf5ec10b2007-10-08 22:16:29 +00004394 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4395 // setting operand in place of the X86ISD::SETCC.
Evan Cheng4259a0f2006-09-11 02:19:56 +00004396 if (Cond.getOpcode() == X86ISD::SETCC) {
4397 CC = Cond.getOperand(0);
4398
Evan Cheng4259a0f2006-09-11 02:19:56 +00004399 SDOperand Cmp = Cond.getOperand(1);
4400 unsigned Opc = Cmp.getOpcode();
Evan Chengf5ec10b2007-10-08 22:16:29 +00004401 MVT::ValueType VT = Op.getValueType();
Chris Lattner14e616e2008-01-16 06:19:45 +00004402
Evan Chengf5ec10b2007-10-08 22:16:29 +00004403 bool IllegalFPCMov = false;
Chris Lattner14e616e2008-01-16 06:19:45 +00004404 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnere8bb9f22008-01-16 06:24:21 +00004405 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Chengf5ec10b2007-10-08 22:16:29 +00004406 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner14e616e2008-01-16 06:19:45 +00004407
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004408 if ((Opc == X86ISD::CMP ||
4409 Opc == X86ISD::COMI ||
4410 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Chengf5ec10b2007-10-08 22:16:29 +00004411 Cond = Cmp;
Evan Chenge95f3912007-09-25 01:57:46 +00004412 addTest = false;
4413 }
4414 }
4415
4416 if (addTest) {
4417 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chengf5ec10b2007-10-08 22:16:29 +00004418 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Chenge95f3912007-09-25 01:57:46 +00004419 }
4420
4421 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4422 MVT::Flag);
4423 SmallVector<SDOperand, 4> Ops;
4424 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4425 // condition is true.
4426 Ops.push_back(Op.getOperand(2));
4427 Ops.push_back(Op.getOperand(1));
4428 Ops.push_back(CC);
4429 Ops.push_back(Cond);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004430 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge95f3912007-09-25 01:57:46 +00004431}
4432
Evan Chenga9467aa2006-04-25 20:13:52 +00004433SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00004434 bool addTest = true;
4435 SDOperand Chain = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004436 SDOperand Cond = Op.getOperand(1);
4437 SDOperand Dest = Op.getOperand(2);
4438 SDOperand CC;
Evan Cheng4259a0f2006-09-11 02:19:56 +00004439
Evan Chenga9467aa2006-04-25 20:13:52 +00004440 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004441 Cond = LowerSETCC(Cond, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004442
Evan Chengf5ec10b2007-10-08 22:16:29 +00004443 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4444 // setting operand in place of the X86ISD::SETCC.
Evan Chenga9467aa2006-04-25 20:13:52 +00004445 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00004446 CC = Cond.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004447
Evan Cheng4259a0f2006-09-11 02:19:56 +00004448 SDOperand Cmp = Cond.getOperand(1);
4449 unsigned Opc = Cmp.getOpcode();
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004450 if (Opc == X86ISD::CMP ||
4451 Opc == X86ISD::COMI ||
4452 Opc == X86ISD::UCOMI) {
Evan Chengf5ec10b2007-10-08 22:16:29 +00004453 Cond = Cmp;
Evan Chenge95f3912007-09-25 01:57:46 +00004454 addTest = false;
4455 }
4456 }
4457
4458 if (addTest) {
4459 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004460 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Chenge95f3912007-09-25 01:57:46 +00004461 }
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004462 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Chenge95f3912007-09-25 01:57:46 +00004463 Chain, Op.getOperand(2), CC, Cond);
4464}
4465
Anton Korobeynikov9b91d982007-04-17 19:34:00 +00004466
4467// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4468// Calls to _alloca is needed to probe the stack when allocating more than 4k
4469// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4470// that the guard pages used by the OS virtual memory manager are allocated in
4471// correct sequence.
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004472SDOperand
4473X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4474 SelectionDAG &DAG) {
Anton Korobeynikov9b91d982007-04-17 19:34:00 +00004475 assert(Subtarget->isTargetCygMing() &&
4476 "This should be used only on Cygwin/Mingw targets");
4477
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004478 // Get the inputs.
4479 SDOperand Chain = Op.getOperand(0);
4480 SDOperand Size = Op.getOperand(1);
4481 // FIXME: Ensure alignment here
4482
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004483 SDOperand Flag;
4484
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004485 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner72733e52008-01-17 07:00:52 +00004486 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004487
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004488 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4489 Flag = Chain.getValue(1);
4490
4491 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4492 SDOperand Ops[] = { Chain,
4493 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4494 DAG.getRegister(X86::EAX, IntPtr),
4495 Flag };
4496 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4497 Flag = Chain.getValue(1);
4498
4499 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004500
4501 std::vector<MVT::ValueType> Tys;
4502 Tys.push_back(SPTy);
4503 Tys.push_back(MVT::Other);
Anton Korobeynikovde9c8252007-07-05 20:36:08 +00004504 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4505 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004506}
4507
Evan Chenga9467aa2006-04-25 20:13:52 +00004508SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4509 SDOperand InFlag(0, 0);
4510 SDOperand Chain = Op.getOperand(0);
4511 unsigned Align =
4512 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4513 if (Align == 0) Align = 1;
4514
4515 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindolaff332412007-08-27 10:18:20 +00004516 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab6024612007-08-27 17:48:26 +00004517 // The libc version is likely to be faster for these cases. It can use the
4518 // address value and run time information about the CPU.
Evan Chenga9467aa2006-04-25 20:13:52 +00004519 if ((Align & 3) != 0 ||
Rafael Espindola063f1772007-10-31 11:52:06 +00004520 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004521 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00004522 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00004523 TargetLowering::ArgListTy Args;
4524 TargetLowering::ArgListEntry Entry;
4525 Entry.Node = Op.getOperand(1);
4526 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00004527 Args.push_back(Entry);
Reid Spencere87b5e92007-01-03 17:24:59 +00004528 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencere63b6512006-12-31 05:55:36 +00004529 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4530 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00004531 Args.push_back(Entry);
4532 Entry.Node = Op.getOperand(3);
4533 Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00004534 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00004535 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4536 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004537 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00004538 }
Evan Chengd097e672006-03-22 02:53:00 +00004539
Evan Chenga9467aa2006-04-25 20:13:52 +00004540 MVT::ValueType AVT;
4541 SDOperand Count;
4542 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4543 unsigned BytesLeft = 0;
4544 bool TwoRepStos = false;
4545 if (ValC) {
4546 unsigned ValReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004547 uint64_t Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00004548
Evan Chenga9467aa2006-04-25 20:13:52 +00004549 // If the value is a constant, then we can potentially use larger sets.
4550 switch (Align & 3) {
4551 case 2: // WORD aligned
4552 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00004553 ValReg = X86::AX;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004554 Val = (Val << 8) | Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00004555 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004556 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00004557 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004558 ValReg = X86::EAX;
Evan Chenga9467aa2006-04-25 20:13:52 +00004559 Val = (Val << 8) | Val;
4560 Val = (Val << 16) | Val;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004561 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4562 AVT = MVT::i64;
4563 ValReg = X86::RAX;
4564 Val = (Val << 32) | Val;
4565 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004566 break;
4567 default: // Byte aligned
4568 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00004569 ValReg = X86::AL;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004570 Count = Op.getOperand(3);
Evan Chenga9467aa2006-04-25 20:13:52 +00004571 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00004572 }
4573
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004574 if (AVT > MVT::i8) {
4575 if (I) {
4576 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner72733e52008-01-17 07:00:52 +00004577 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004578 BytesLeft = I->getValue() % UBytes;
4579 } else {
4580 assert(AVT >= MVT::i32 &&
4581 "Do not use rep;stos if not at least DWORD aligned");
4582 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4583 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4584 TwoRepStos = true;
4585 }
4586 }
4587
Evan Chenga9467aa2006-04-25 20:13:52 +00004588 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4589 InFlag);
4590 InFlag = Chain.getValue(1);
4591 } else {
4592 AVT = MVT::i8;
4593 Count = Op.getOperand(3);
4594 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4595 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00004596 }
Evan Chengb0461082006-04-24 18:01:45 +00004597
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004598 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4599 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004600 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004601 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4602 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004603 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00004604
Chris Lattnere56fef92007-02-25 06:40:16 +00004605 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00004606 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00004607 Ops.push_back(Chain);
4608 Ops.push_back(DAG.getValueType(AVT));
4609 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00004610 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengb0461082006-04-24 18:01:45 +00004611
Evan Chenga9467aa2006-04-25 20:13:52 +00004612 if (TwoRepStos) {
4613 InFlag = Chain.getValue(1);
4614 Count = Op.getOperand(3);
4615 MVT::ValueType CVT = Count.getValueType();
4616 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004617 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4618 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4619 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004620 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00004621 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004622 Ops.clear();
4623 Ops.push_back(Chain);
4624 Ops.push_back(DAG.getValueType(MVT::i8));
4625 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00004626 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004627 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004628 // Issue stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00004629 SDOperand Value;
4630 unsigned Val = ValC->getValue() & 255;
4631 unsigned Offset = I->getValue() - BytesLeft;
4632 SDOperand DstAddr = Op.getOperand(1);
4633 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004634 if (BytesLeft >= 4) {
4635 Val = (Val << 8) | Val;
4636 Val = (Val << 16) | Val;
4637 Value = DAG.getConstant(Val, MVT::i32);
Evan Chengdf9ac472006-10-05 23:01:46 +00004638 Chain = DAG.getStore(Chain, Value,
4639 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4640 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004641 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004642 BytesLeft -= 4;
4643 Offset += 4;
4644 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004645 if (BytesLeft >= 2) {
4646 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Chengdf9ac472006-10-05 23:01:46 +00004647 Chain = DAG.getStore(Chain, Value,
4648 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4649 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004650 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004651 BytesLeft -= 2;
4652 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00004653 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004654 if (BytesLeft == 1) {
4655 Value = DAG.getConstant(Val, MVT::i8);
Evan Chengdf9ac472006-10-05 23:01:46 +00004656 Chain = DAG.getStore(Chain, Value,
4657 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4658 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004659 NULL, 0);
Evan Cheng14215c32006-04-21 23:03:30 +00004660 }
Evan Cheng082c8782006-03-24 07:29:27 +00004661 }
Evan Chengebf10062006-04-03 20:53:28 +00004662
Evan Chenga9467aa2006-04-25 20:13:52 +00004663 return Chain;
4664}
Evan Chengebf10062006-04-03 20:53:28 +00004665
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004666SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4667 SDOperand Dest,
4668 SDOperand Source,
4669 unsigned Size,
4670 unsigned Align,
4671 SelectionDAG &DAG) {
Evan Chenga9467aa2006-04-25 20:13:52 +00004672 MVT::ValueType AVT;
Evan Chenga9467aa2006-04-25 20:13:52 +00004673 unsigned BytesLeft = 0;
Evan Chenga9467aa2006-04-25 20:13:52 +00004674 switch (Align & 3) {
4675 case 2: // WORD aligned
4676 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00004677 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004678 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00004679 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004680 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4681 AVT = MVT::i64;
Evan Chenga9467aa2006-04-25 20:13:52 +00004682 break;
4683 default: // Byte aligned
4684 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00004685 break;
4686 }
4687
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004688 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner72733e52008-01-17 07:00:52 +00004689 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004690 BytesLeft = Size % UBytes;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004691
Evan Chenga9467aa2006-04-25 20:13:52 +00004692 SDOperand InFlag(0, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004693 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4694 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004695 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004696 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004697 Dest, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004698 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004699 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004700 Source, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00004701 InFlag = Chain.getValue(1);
4702
Chris Lattnere56fef92007-02-25 06:40:16 +00004703 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00004704 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00004705 Ops.push_back(Chain);
4706 Ops.push_back(DAG.getValueType(AVT));
4707 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00004708 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004709
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004710 if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004711 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004712 unsigned Offset = Size - BytesLeft;
4713 SDOperand DstAddr = Dest;
Evan Chenga9467aa2006-04-25 20:13:52 +00004714 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola6c04ac12007-09-28 12:53:01 +00004715 SDOperand SrcAddr = Source;
Evan Chenga9467aa2006-04-25 20:13:52 +00004716 MVT::ValueType SrcVT = SrcAddr.getValueType();
4717 SDOperand Value;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004718 if (BytesLeft >= 4) {
4719 Value = DAG.getLoad(MVT::i32, Chain,
4720 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4721 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004722 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004723 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00004724 Chain = DAG.getStore(Chain, Value,
4725 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4726 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004727 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004728 BytesLeft -= 4;
4729 Offset += 4;
4730 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004731 if (BytesLeft >= 2) {
4732 Value = DAG.getLoad(MVT::i16, Chain,
4733 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4734 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004735 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004736 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00004737 Chain = DAG.getStore(Chain, Value,
4738 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4739 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004740 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004741 BytesLeft -= 2;
4742 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00004743 }
4744
Evan Chenga9467aa2006-04-25 20:13:52 +00004745 if (BytesLeft == 1) {
4746 Value = DAG.getLoad(MVT::i8, Chain,
4747 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4748 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004749 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004750 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00004751 Chain = DAG.getStore(Chain, Value,
4752 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4753 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00004754 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00004755 }
Evan Chengcbffa462006-03-31 19:22:53 +00004756 }
Evan Chenga9467aa2006-04-25 20:13:52 +00004757
4758 return Chain;
4759}
4760
Chris Lattnerf81d5882007-11-24 07:07:01 +00004761/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4762SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnere56fef92007-02-25 06:40:16 +00004763 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004764 SDOperand TheChain = N->getOperand(0);
4765 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00004766 if (Subtarget->is64Bit()) {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004767 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4768 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4769 MVT::i64, rax.getValue(2));
4770 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng28a9e9b2006-11-29 08:28:13 +00004771 DAG.getConstant(32, MVT::i8));
Chris Lattner35a08552007-02-25 07:10:00 +00004772 SDOperand Ops[] = {
Chris Lattnerf81d5882007-11-24 07:07:01 +00004773 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner35a08552007-02-25 07:10:00 +00004774 };
Chris Lattnere56fef92007-02-25 06:40:16 +00004775
4776 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004777 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng28a9e9b2006-11-29 08:28:13 +00004778 }
Chris Lattner35a08552007-02-25 07:10:00 +00004779
Chris Lattnerf81d5882007-11-24 07:07:01 +00004780 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4781 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4782 MVT::i32, eax.getValue(2));
4783 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4784 SDOperand Ops[] = { eax, edx };
4785 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4786
4787 // Use a MERGE_VALUES to return the value and chain.
4788 Ops[1] = edx.getValue(1);
4789 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4790 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00004791}
4792
4793SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman2d489b52008-02-06 22:27:42 +00004794 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Chengab51cf22006-10-13 21:14:26 +00004795
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004796 if (!Subtarget->is64Bit()) {
4797 // vastart just stores the address of the VarArgsFrameIndex slot into the
4798 // memory location argument.
4799 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00004800 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004801 }
4802
4803 // __va_list_tag:
4804 // gp_offset (0 - 6 * 8)
4805 // fp_offset (48 - 48 + 8 * 16)
4806 // overflow_arg_area (point to parameters coming in memory).
4807 // reg_save_area
Chris Lattner35a08552007-02-25 07:10:00 +00004808 SmallVector<SDOperand, 8> MemOps;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004809 SDOperand FIN = Op.getOperand(1);
4810 // Store gp_offset
Evan Chengdf9ac472006-10-05 23:01:46 +00004811 SDOperand Store = DAG.getStore(Op.getOperand(0),
4812 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman2d489b52008-02-06 22:27:42 +00004813 FIN, SV, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004814 MemOps.push_back(Store);
4815
4816 // Store fp_offset
Chris Lattner72733e52008-01-17 07:00:52 +00004817 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Chengdf9ac472006-10-05 23:01:46 +00004818 Store = DAG.getStore(Op.getOperand(0),
4819 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman2d489b52008-02-06 22:27:42 +00004820 FIN, SV, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004821 MemOps.push_back(Store);
4822
4823 // Store ptr to overflow_arg_area
Chris Lattner72733e52008-01-17 07:00:52 +00004824 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004825 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00004826 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004827 MemOps.push_back(Store);
4828
4829 // Store ptr to reg_save_area.
Chris Lattner72733e52008-01-17 07:00:52 +00004830 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004831 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00004832 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004833 MemOps.push_back(Store);
4834 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004835}
4836
Evan Chengdeaea252007-03-02 23:16:35 +00004837SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4838 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4839 SDOperand Chain = Op.getOperand(0);
4840 SDOperand DstPtr = Op.getOperand(1);
4841 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman2d489b52008-02-06 22:27:42 +00004842 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4843 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengdeaea252007-03-02 23:16:35 +00004844
Dan Gohman2d489b52008-02-06 22:27:42 +00004845 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Evan Chengdeaea252007-03-02 23:16:35 +00004846 Chain = SrcPtr.getValue(1);
4847 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman2d489b52008-02-06 22:27:42 +00004848 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Evan Chengdeaea252007-03-02 23:16:35 +00004849 Chain = Val.getValue(1);
Dan Gohman2d489b52008-02-06 22:27:42 +00004850 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Evan Chengdeaea252007-03-02 23:16:35 +00004851 if (i == 2)
4852 break;
4853 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner72733e52008-01-17 07:00:52 +00004854 DAG.getIntPtrConstant(8));
Evan Chengdeaea252007-03-02 23:16:35 +00004855 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner72733e52008-01-17 07:00:52 +00004856 DAG.getIntPtrConstant(8));
Evan Chengdeaea252007-03-02 23:16:35 +00004857 }
4858 return Chain;
4859}
4860
Evan Chenga9467aa2006-04-25 20:13:52 +00004861SDOperand
4862X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4863 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4864 switch (IntNo) {
4865 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00004866 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00004867 case Intrinsic::x86_sse_comieq_ss:
4868 case Intrinsic::x86_sse_comilt_ss:
4869 case Intrinsic::x86_sse_comile_ss:
4870 case Intrinsic::x86_sse_comigt_ss:
4871 case Intrinsic::x86_sse_comige_ss:
4872 case Intrinsic::x86_sse_comineq_ss:
4873 case Intrinsic::x86_sse_ucomieq_ss:
4874 case Intrinsic::x86_sse_ucomilt_ss:
4875 case Intrinsic::x86_sse_ucomile_ss:
4876 case Intrinsic::x86_sse_ucomigt_ss:
4877 case Intrinsic::x86_sse_ucomige_ss:
4878 case Intrinsic::x86_sse_ucomineq_ss:
4879 case Intrinsic::x86_sse2_comieq_sd:
4880 case Intrinsic::x86_sse2_comilt_sd:
4881 case Intrinsic::x86_sse2_comile_sd:
4882 case Intrinsic::x86_sse2_comigt_sd:
4883 case Intrinsic::x86_sse2_comige_sd:
4884 case Intrinsic::x86_sse2_comineq_sd:
4885 case Intrinsic::x86_sse2_ucomieq_sd:
4886 case Intrinsic::x86_sse2_ucomilt_sd:
4887 case Intrinsic::x86_sse2_ucomile_sd:
4888 case Intrinsic::x86_sse2_ucomigt_sd:
4889 case Intrinsic::x86_sse2_ucomige_sd:
4890 case Intrinsic::x86_sse2_ucomineq_sd: {
4891 unsigned Opc = 0;
4892 ISD::CondCode CC = ISD::SETCC_INVALID;
4893 switch (IntNo) {
4894 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004895 case Intrinsic::x86_sse_comieq_ss:
4896 case Intrinsic::x86_sse2_comieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004897 Opc = X86ISD::COMI;
4898 CC = ISD::SETEQ;
4899 break;
Evan Cheng78038292006-04-05 23:38:46 +00004900 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004901 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004902 Opc = X86ISD::COMI;
4903 CC = ISD::SETLT;
4904 break;
4905 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004906 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004907 Opc = X86ISD::COMI;
4908 CC = ISD::SETLE;
4909 break;
4910 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004911 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004912 Opc = X86ISD::COMI;
4913 CC = ISD::SETGT;
4914 break;
4915 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004916 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004917 Opc = X86ISD::COMI;
4918 CC = ISD::SETGE;
4919 break;
4920 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004921 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004922 Opc = X86ISD::COMI;
4923 CC = ISD::SETNE;
4924 break;
4925 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004926 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004927 Opc = X86ISD::UCOMI;
4928 CC = ISD::SETEQ;
4929 break;
4930 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004931 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004932 Opc = X86ISD::UCOMI;
4933 CC = ISD::SETLT;
4934 break;
4935 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004936 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004937 Opc = X86ISD::UCOMI;
4938 CC = ISD::SETLE;
4939 break;
4940 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004941 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004942 Opc = X86ISD::UCOMI;
4943 CC = ISD::SETGT;
4944 break;
4945 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004946 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004947 Opc = X86ISD::UCOMI;
4948 CC = ISD::SETGE;
4949 break;
4950 case Intrinsic::x86_sse_ucomineq_ss:
4951 case Intrinsic::x86_sse2_ucomineq_sd:
4952 Opc = X86ISD::UCOMI;
4953 CC = ISD::SETNE;
4954 break;
Evan Cheng78038292006-04-05 23:38:46 +00004955 }
Evan Cheng4259a0f2006-09-11 02:19:56 +00004956
Evan Chenga9467aa2006-04-25 20:13:52 +00004957 unsigned X86CC;
Chris Lattner7a627672006-09-13 03:22:10 +00004958 SDOperand LHS = Op.getOperand(1);
4959 SDOperand RHS = Op.getOperand(2);
4960 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004961
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00004962 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4963 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4964 DAG.getConstant(X86CC, MVT::i8), Cond);
4965 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00004966 }
Evan Cheng5c59d492005-12-23 07:31:11 +00004967 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004968}
Evan Cheng6af02632005-12-20 06:22:03 +00004969
Nate Begemaneda59972007-01-29 22:58:52 +00004970SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4971 // Depths > 0 not supported yet!
4972 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4973 return SDOperand();
4974
4975 // Just load the return address
4976 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4977 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4978}
4979
4980SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4981 // Depths > 0 not supported yet!
4982 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4983 return SDOperand();
4984
4985 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4986 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner72733e52008-01-17 07:00:52 +00004987 DAG.getIntPtrConstant(4));
Nate Begemaneda59972007-01-29 22:58:52 +00004988}
4989
Anton Korobeynikov383a3242007-07-14 14:06:15 +00004990SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4991 SelectionDAG &DAG) {
4992 // Is not yet supported on x86-64
4993 if (Subtarget->is64Bit())
4994 return SDOperand();
4995
Chris Lattner72733e52008-01-17 07:00:52 +00004996 return DAG.getIntPtrConstant(8);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00004997}
4998
4999SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5000{
5001 assert(!Subtarget->is64Bit() &&
5002 "Lowering of eh_return builtin is not supported yet on x86-64");
5003
5004 MachineFunction &MF = DAG.getMachineFunction();
5005 SDOperand Chain = Op.getOperand(0);
5006 SDOperand Offset = Op.getOperand(1);
5007 SDOperand Handler = Op.getOperand(2);
5008
5009 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5010 getPointerTy());
5011
5012 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner72733e52008-01-17 07:00:52 +00005013 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005014 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5015 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5016 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattnera10fff52007-12-31 04:13:23 +00005017 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005018
5019 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5020 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5021}
5022
Duncan Sandsce388532007-07-27 20:02:49 +00005023SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5024 SelectionDAG &DAG) {
5025 SDOperand Root = Op.getOperand(0);
5026 SDOperand Trmp = Op.getOperand(1); // trampoline
5027 SDOperand FPtr = Op.getOperand(2); // nested function
5028 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5029
Dan Gohman2d489b52008-02-06 22:27:42 +00005030 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsce388532007-07-27 20:02:49 +00005031
Duncan Sands32b0ff62008-01-16 22:55:25 +00005032 const X86InstrInfo *TII =
5033 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5034
Duncan Sandsce388532007-07-27 20:02:49 +00005035 if (Subtarget->is64Bit()) {
Duncan Sands32b0ff62008-01-16 22:55:25 +00005036 SDOperand OutChains[6];
5037
5038 // Large code-model.
5039
5040 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5041 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5042
5043 const unsigned char N86R10 =
Dan Gohman7a55a942008-02-08 03:29:40 +00005044 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005045 const unsigned char N86R11 =
Dan Gohman7a55a942008-02-08 03:29:40 +00005046 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005047
5048 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5049
5050 // Load the pointer to the nested function into R11.
5051 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5052 SDOperand Addr = Trmp;
5053 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman2d489b52008-02-06 22:27:42 +00005054 TrmpAddr, 0);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005055
5056 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman2d489b52008-02-06 22:27:42 +00005057 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005058
5059 // Load the 'nest' parameter value into R10.
5060 // R10 is specified in X86CallingConv.td
5061 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5062 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5063 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman2d489b52008-02-06 22:27:42 +00005064 TrmpAddr, 10);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005065
5066 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman2d489b52008-02-06 22:27:42 +00005067 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005068
5069 // Jump to the nested function.
5070 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5071 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5072 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman2d489b52008-02-06 22:27:42 +00005073 TrmpAddr, 20);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005074
5075 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5076 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5077 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman2d489b52008-02-06 22:27:42 +00005078 TrmpAddr, 22);
Duncan Sands32b0ff62008-01-16 22:55:25 +00005079
5080 SDOperand Ops[] =
5081 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5082 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsce388532007-07-27 20:02:49 +00005083 } else {
Dan Gohmaned346f22008-01-31 01:01:48 +00005084 const Function *Func =
Duncan Sandsce388532007-07-27 20:02:49 +00005085 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5086 unsigned CC = Func->getCallingConv();
Duncan Sands77414272007-08-29 19:01:20 +00005087 unsigned NestReg;
Duncan Sandsce388532007-07-27 20:02:49 +00005088
5089 switch (CC) {
5090 default:
5091 assert(0 && "Unsupported calling convention");
5092 case CallingConv::C:
Duncan Sandsce388532007-07-27 20:02:49 +00005093 case CallingConv::X86_StdCall: {
5094 // Pass 'nest' parameter in ECX.
5095 // Must be kept in sync with X86CallingConv.td
Duncan Sands77414272007-08-29 19:01:20 +00005096 NestReg = X86::ECX;
Duncan Sandsce388532007-07-27 20:02:49 +00005097
5098 // Check that ECX wasn't needed by an 'inreg' parameter.
5099 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00005100 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsce388532007-07-27 20:02:49 +00005101
5102 if (Attrs && !Func->isVarArg()) {
5103 unsigned InRegCount = 0;
5104 unsigned Idx = 1;
5105
5106 for (FunctionType::param_iterator I = FTy->param_begin(),
5107 E = FTy->param_end(); I != E; ++I, ++Idx)
5108 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5109 // FIXME: should only count parameters that are lowered to integers.
5110 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5111
5112 if (InRegCount > 2) {
5113 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5114 abort();
5115 }
5116 }
5117 break;
5118 }
5119 case CallingConv::X86_FastCall:
5120 // Pass 'nest' parameter in EAX.
5121 // Must be kept in sync with X86CallingConv.td
Duncan Sands77414272007-08-29 19:01:20 +00005122 NestReg = X86::EAX;
Duncan Sandsce388532007-07-27 20:02:49 +00005123 break;
5124 }
5125
5126 SDOperand OutChains[4];
5127 SDOperand Addr, Disp;
5128
5129 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5130 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5131
Duncan Sands32b0ff62008-01-16 22:55:25 +00005132 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5133 const unsigned char N86Reg =
Dan Gohman7a55a942008-02-08 03:29:40 +00005134 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands77414272007-08-29 19:01:20 +00005135 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman2d489b52008-02-06 22:27:42 +00005136 Trmp, TrmpAddr, 0);
Duncan Sandsce388532007-07-27 20:02:49 +00005137
5138 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman2d489b52008-02-06 22:27:42 +00005139 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsce388532007-07-27 20:02:49 +00005140
Duncan Sands32b0ff62008-01-16 22:55:25 +00005141 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsce388532007-07-27 20:02:49 +00005142 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5143 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman2d489b52008-02-06 22:27:42 +00005144 TrmpAddr, 5, false, 1);
Duncan Sandsce388532007-07-27 20:02:49 +00005145
5146 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman2d489b52008-02-06 22:27:42 +00005147 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsce388532007-07-27 20:02:49 +00005148
Duncan Sands86e01192007-09-11 14:10:23 +00005149 SDOperand Ops[] =
5150 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5151 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsce388532007-07-27 20:02:49 +00005152 }
5153}
5154
Dan Gohman9ba4d762008-01-31 00:41:03 +00005155SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikov91460e42007-11-16 01:31:51 +00005156 /*
5157 The rounding mode is in bits 11:10 of FPSR, and has the following
5158 settings:
5159 00 Round to nearest
5160 01 Round to -inf
5161 10 Round to +inf
5162 11 Round to 0
5163
5164 FLT_ROUNDS, on the other hand, expects the following:
5165 -1 Undefined
5166 0 Round to 0
5167 1 Round to nearest
5168 2 Round to +inf
5169 3 Round to -inf
5170
5171 To perform the conversion, we do:
5172 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5173 */
5174
5175 MachineFunction &MF = DAG.getMachineFunction();
5176 const TargetMachine &TM = MF.getTarget();
5177 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5178 unsigned StackAlignment = TFI.getStackAlignment();
5179 MVT::ValueType VT = Op.getValueType();
5180
5181 // Save FP Control Word to stack slot
5182 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5183 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5184
5185 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5186 DAG.getEntryNode(), StackSlot);
5187
5188 // Load FP Control Word from stack slot
5189 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5190
5191 // Transform as necessary
5192 SDOperand CWD1 =
5193 DAG.getNode(ISD::SRL, MVT::i16,
5194 DAG.getNode(ISD::AND, MVT::i16,
5195 CWD, DAG.getConstant(0x800, MVT::i16)),
5196 DAG.getConstant(11, MVT::i8));
5197 SDOperand CWD2 =
5198 DAG.getNode(ISD::SRL, MVT::i16,
5199 DAG.getNode(ISD::AND, MVT::i16,
5200 CWD, DAG.getConstant(0x400, MVT::i16)),
5201 DAG.getConstant(9, MVT::i8));
5202
5203 SDOperand RetVal =
5204 DAG.getNode(ISD::AND, MVT::i16,
5205 DAG.getNode(ISD::ADD, MVT::i16,
5206 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5207 DAG.getConstant(1, MVT::i16)),
5208 DAG.getConstant(3, MVT::i16));
5209
5210
5211 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5212 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5213}
5214
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005215SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5216 MVT::ValueType VT = Op.getValueType();
5217 MVT::ValueType OpVT = VT;
5218 unsigned NumBits = MVT::getSizeInBits(VT);
5219
5220 Op = Op.getOperand(0);
5221 if (VT == MVT::i8) {
Evan Cheng0e640812007-12-14 08:30:15 +00005222 // Zero extend to i32 since there is not an i8 bsr.
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005223 OpVT = MVT::i32;
5224 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5225 }
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005226
Evan Cheng0e640812007-12-14 08:30:15 +00005227 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5228 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5229 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5230
5231 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5232 SmallVector<SDOperand, 4> Ops;
5233 Ops.push_back(Op);
5234 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5235 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5236 Ops.push_back(Op.getValue(1));
5237 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5238
5239 // Finally xor with NumBits-1.
5240 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5241
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005242 if (VT == MVT::i8)
5243 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5244 return Op;
5245}
5246
5247SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5248 MVT::ValueType VT = Op.getValueType();
5249 MVT::ValueType OpVT = VT;
Evan Cheng0e640812007-12-14 08:30:15 +00005250 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005251
5252 Op = Op.getOperand(0);
5253 if (VT == MVT::i8) {
5254 OpVT = MVT::i32;
5255 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5256 }
Evan Cheng0e640812007-12-14 08:30:15 +00005257
5258 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5259 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5260 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5261
5262 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5263 SmallVector<SDOperand, 4> Ops;
5264 Ops.push_back(Op);
5265 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5266 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5267 Ops.push_back(Op.getValue(1));
5268 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5269
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005270 if (VT == MVT::i8)
5271 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5272 return Op;
5273}
5274
Evan Chenga9467aa2006-04-25 20:13:52 +00005275/// LowerOperation - Provide custom lowering hooks for some operations.
5276///
5277SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5278 switch (Op.getOpcode()) {
5279 default: assert(0 && "Should not custom lower this!");
5280 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5281 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5282 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5283 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5284 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5285 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5286 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00005287 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005288 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5289 case ISD::SHL_PARTS:
5290 case ISD::SRA_PARTS:
5291 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5292 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5293 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5294 case ISD::FABS: return LowerFABS(Op, DAG);
5295 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng4363e882007-01-05 07:55:56 +00005296 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00005297 case ISD::SETCC: return LowerSETCC(Op, DAG);
5298 case ISD::SELECT: return LowerSELECT(Op, DAG);
5299 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005300 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00005301 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005302 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00005303 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005304 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5305 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005306 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengdeaea252007-03-02 23:16:35 +00005307 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00005308 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemaneda59972007-01-29 22:58:52 +00005309 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5310 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005311 case ISD::FRAME_TO_ARGS_OFFSET:
5312 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00005313 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005314 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsce388532007-07-27 20:02:49 +00005315 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005316 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005317 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5318 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerf81d5882007-11-24 07:07:01 +00005319
5320 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5321 case ISD::READCYCLECOUNTER:
5322 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00005323 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005324}
5325
5326/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5327SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5328 switch (N->getOpcode()) {
5329 default: assert(0 && "Should not custom lower this!");
5330 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5331 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5332 }
Evan Chenga9467aa2006-04-25 20:13:52 +00005333}
5334
Evan Cheng6af02632005-12-20 06:22:03 +00005335const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5336 switch (Opcode) {
5337 default: return NULL;
Evan Chenge9fbc3f2007-12-14 02:13:44 +00005338 case X86ISD::BSF: return "X86ISD::BSF";
5339 case X86ISD::BSR: return "X86ISD::BSR";
Evan Cheng9c249c32006-01-09 18:33:28 +00005340 case X86ISD::SHLD: return "X86ISD::SHLD";
5341 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00005342 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng4363e882007-01-05 07:55:56 +00005343 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng72d5c252006-01-31 22:28:30 +00005344 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng4363e882007-01-05 07:55:56 +00005345 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Cheng6305e502006-01-12 22:54:21 +00005346 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00005347 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00005348 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5349 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5350 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00005351 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00005352 case X86ISD::FST: return "X86ISD::FST";
5353 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng084a1cd2008-01-29 19:34:22 +00005354 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Evan Chenga74ce622005-12-21 02:39:21 +00005355 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00005356 case X86ISD::CALL: return "X86ISD::CALL";
5357 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5358 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5359 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng78038292006-04-05 23:38:46 +00005360 case X86ISD::COMI: return "X86ISD::COMI";
5361 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00005362 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00005363 case X86ISD::CMOV: return "X86ISD::CMOV";
5364 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00005365 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00005366 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5367 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng5588de92006-02-18 00:15:05 +00005368 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00005369 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman2d77e8e42008-02-11 04:19:36 +00005370 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengcbffa462006-03-31 19:22:53 +00005371 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman2d77e8e42008-02-11 04:19:36 +00005372 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5373 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng5fd7c692006-03-31 21:55:24 +00005374 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng49683ba2006-11-10 21:43:37 +00005375 case X86ISD::FMAX: return "X86ISD::FMAX";
5376 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman57111e72007-07-10 00:05:58 +00005377 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5378 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00005379 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5380 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov383a3242007-07-14 14:06:15 +00005381 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00005382 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov91460e42007-11-16 01:31:51 +00005383 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng6af02632005-12-20 06:22:03 +00005384 }
5385}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005386
Chris Lattner1eb94d92007-03-30 23:15:24 +00005387// isLegalAddressingMode - Return true if the addressing mode represented
5388// by AM is legal for this target, for a load/store of the specified type.
5389bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5390 const Type *Ty) const {
5391 // X86 supports extremely general addressing modes.
5392
5393 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5394 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5395 return false;
5396
5397 if (AM.BaseGV) {
Evan Chengd3d92892007-08-01 23:46:47 +00005398 // We can only fold this if we don't need an extra load.
Chris Lattner1eb94d92007-03-30 23:15:24 +00005399 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5400 return false;
Evan Chengd3d92892007-08-01 23:46:47 +00005401
5402 // X86-64 only supports addr of globals in small code model.
5403 if (Subtarget->is64Bit()) {
5404 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5405 return false;
5406 // If lower 4G is not available, then we must use rip-relative addressing.
5407 if (AM.BaseOffs || AM.Scale > 1)
5408 return false;
5409 }
Chris Lattner1eb94d92007-03-30 23:15:24 +00005410 }
5411
5412 switch (AM.Scale) {
5413 case 0:
5414 case 1:
5415 case 2:
5416 case 4:
5417 case 8:
5418 // These scales always work.
5419 break;
5420 case 3:
5421 case 5:
5422 case 9:
5423 // These scales are formed with basereg+scalereg. Only accept if there is
5424 // no basereg yet.
5425 if (AM.HasBaseReg)
5426 return false;
5427 break;
5428 default: // Other stuff never works.
5429 return false;
5430 }
5431
5432 return true;
5433}
5434
5435
Evan Cheng7f3d0242007-10-26 01:56:11 +00005436bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5437 if (!Ty1->isInteger() || !Ty2->isInteger())
5438 return false;
Evan Cheng7b3f7fe2007-10-29 07:57:50 +00005439 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5440 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5441 if (NumBits1 <= NumBits2)
5442 return false;
5443 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng7f3d0242007-10-26 01:56:11 +00005444}
5445
Evan Chenge106e2f2007-10-29 19:58:20 +00005446bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5447 MVT::ValueType VT2) const {
5448 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5449 return false;
5450 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5451 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5452 if (NumBits1 <= NumBits2)
5453 return false;
5454 return Subtarget->is64Bit() || NumBits1 < 64;
5455}
Evan Cheng7f3d0242007-10-26 01:56:11 +00005456
Evan Cheng02612422006-07-05 22:17:51 +00005457/// isShuffleMaskLegal - Targets can use this to indicate that they only
5458/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5459/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5460/// are assumed to be legal.
5461bool
5462X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5463 // Only do shuffles on 128-bit vector types for now.
5464 if (MVT::getSizeInBits(VT) == 64) return false;
5465 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengcea02ff2007-06-19 00:02:56 +00005466 isIdentityMask(Mask.Val) ||
5467 isIdentityMask(Mask.Val, true) ||
Evan Cheng02612422006-07-05 22:17:51 +00005468 isSplatMask(Mask.Val) ||
5469 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5470 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengcea02ff2007-06-19 00:02:56 +00005471 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng02612422006-07-05 22:17:51 +00005472 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Chengcea02ff2007-06-19 00:02:56 +00005473 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng02612422006-07-05 22:17:51 +00005474}
5475
5476bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5477 MVT::ValueType EVT,
5478 SelectionDAG &DAG) const {
5479 unsigned NumElts = BVOps.size();
5480 // Only do shuffles on 128-bit vector types for now.
5481 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5482 if (NumElts == 2) return true;
5483 if (NumElts == 4) {
Chris Lattner35a08552007-02-25 07:10:00 +00005484 return (isMOVLMask(&BVOps[0], 4) ||
5485 isCommutedMOVL(&BVOps[0], 4, true) ||
5486 isSHUFPMask(&BVOps[0], 4) ||
5487 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng02612422006-07-05 22:17:51 +00005488 }
5489 return false;
5490}
5491
5492//===----------------------------------------------------------------------===//
5493// X86 Scheduler Hooks
5494//===----------------------------------------------------------------------===//
5495
5496MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00005497X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5498 MachineBasicBlock *BB) {
Evan Cheng20350c42006-11-27 23:37:22 +00005499 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng02612422006-07-05 22:17:51 +00005500 switch (MI->getOpcode()) {
5501 default: assert(false && "Unexpected instr type to insert");
5502 case X86::CMOV_FR32:
5503 case X86::CMOV_FR64:
5504 case X86::CMOV_V4F32:
5505 case X86::CMOV_V2F64:
Evan Cheng5fb5a1f2007-09-29 00:00:36 +00005506 case X86::CMOV_V2I64: {
Evan Cheng02612422006-07-05 22:17:51 +00005507 // To "insert" a SELECT_CC instruction, we actually have to insert the
5508 // diamond control-flow pattern. The incoming instruction knows the
5509 // destination vreg to set, the condition code register to branch on, the
5510 // true/false values to select between, and a branch opcode to use.
5511 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5512 ilist<MachineBasicBlock>::iterator It = BB;
5513 ++It;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005514
Evan Cheng02612422006-07-05 22:17:51 +00005515 // thisMBB:
5516 // ...
5517 // TrueVal = ...
5518 // cmpTY ccX, r1, r2
5519 // bCC copy1MBB
5520 // fallthrough --> copy0MBB
5521 MachineBasicBlock *thisMBB = BB;
5522 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5523 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005524 unsigned Opc =
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005525 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Cheng20350c42006-11-27 23:37:22 +00005526 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng02612422006-07-05 22:17:51 +00005527 MachineFunction *F = BB->getParent();
5528 F->getBasicBlockList().insert(It, copy0MBB);
5529 F->getBasicBlockList().insert(It, sinkMBB);
5530 // Update machine-CFG edges by first adding all successors of the current
5531 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005532 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng02612422006-07-05 22:17:51 +00005533 e = BB->succ_end(); i != e; ++i)
5534 sinkMBB->addSuccessor(*i);
5535 // Next, remove all successors of the current block, and add the true
5536 // and fallthrough blocks as its successors.
5537 while(!BB->succ_empty())
5538 BB->removeSuccessor(BB->succ_begin());
5539 BB->addSuccessor(copy0MBB);
5540 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005541
Evan Cheng02612422006-07-05 22:17:51 +00005542 // copy0MBB:
5543 // %FalseValue = ...
5544 // # fallthrough to sinkMBB
5545 BB = copy0MBB;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005546
Evan Cheng02612422006-07-05 22:17:51 +00005547 // Update machine-CFG edges
5548 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005549
Evan Cheng02612422006-07-05 22:17:51 +00005550 // sinkMBB:
5551 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5552 // ...
5553 BB = sinkMBB;
Evan Cheng20350c42006-11-27 23:37:22 +00005554 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng02612422006-07-05 22:17:51 +00005555 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5556 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5557
5558 delete MI; // The pseudo instruction is gone now.
5559 return BB;
5560 }
5561
Dale Johannesena2b3c172007-07-03 00:53:03 +00005562 case X86::FP32_TO_INT16_IN_MEM:
5563 case X86::FP32_TO_INT32_IN_MEM:
5564 case X86::FP32_TO_INT64_IN_MEM:
5565 case X86::FP64_TO_INT16_IN_MEM:
5566 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +00005567 case X86::FP64_TO_INT64_IN_MEM:
5568 case X86::FP80_TO_INT16_IN_MEM:
5569 case X86::FP80_TO_INT32_IN_MEM:
5570 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng02612422006-07-05 22:17:51 +00005571 // Change the floating point control register to use "round towards zero"
5572 // mode when truncating to an integer value.
5573 MachineFunction *F = BB->getParent();
5574 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Cheng20350c42006-11-27 23:37:22 +00005575 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005576
5577 // Load the old value of the high byte of the control word...
5578 unsigned OldCW =
Chris Lattnera10fff52007-12-31 04:13:23 +00005579 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng20350c42006-11-27 23:37:22 +00005580 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005581
5582 // Set the high part to be round to zero...
Evan Cheng20350c42006-11-27 23:37:22 +00005583 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5584 .addImm(0xC7F);
Evan Cheng02612422006-07-05 22:17:51 +00005585
5586 // Reload the modified control word now...
Evan Cheng20350c42006-11-27 23:37:22 +00005587 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005588
5589 // Restore the memory image of control word to original value
Evan Cheng20350c42006-11-27 23:37:22 +00005590 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5591 .addReg(OldCW);
Evan Cheng02612422006-07-05 22:17:51 +00005592
5593 // Get the X86 opcode to use.
5594 unsigned Opc;
5595 switch (MI->getOpcode()) {
5596 default: assert(0 && "illegal opcode!");
Dale Johannesen3d7008c2007-07-04 21:07:47 +00005597 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5598 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5599 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5600 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5601 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5602 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +00005603 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5604 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5605 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng02612422006-07-05 22:17:51 +00005606 }
5607
5608 X86AddressMode AM;
5609 MachineOperand &Op = MI->getOperand(0);
5610 if (Op.isRegister()) {
5611 AM.BaseType = X86AddressMode::RegBase;
5612 AM.Base.Reg = Op.getReg();
5613 } else {
5614 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattnera5bb3702007-12-30 23:10:15 +00005615 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng02612422006-07-05 22:17:51 +00005616 }
5617 Op = MI->getOperand(1);
5618 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005619 AM.Scale = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00005620 Op = MI->getOperand(2);
5621 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005622 AM.IndexReg = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00005623 Op = MI->getOperand(3);
5624 if (Op.isGlobalAddress()) {
5625 AM.GV = Op.getGlobal();
5626 } else {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00005627 AM.Disp = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00005628 }
Evan Cheng20350c42006-11-27 23:37:22 +00005629 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5630 .addReg(MI->getOperand(4).getReg());
Evan Cheng02612422006-07-05 22:17:51 +00005631
5632 // Reload the original control word now.
Evan Cheng20350c42006-11-27 23:37:22 +00005633 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00005634
5635 delete MI; // The pseudo instruction is gone now.
5636 return BB;
5637 }
5638 }
5639}
5640
5641//===----------------------------------------------------------------------===//
5642// X86 Optimization Hooks
5643//===----------------------------------------------------------------------===//
5644
Nate Begeman8a77efe2006-02-16 21:11:51 +00005645void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmane1d9ee62008-02-13 22:28:48 +00005646 const APInt &Mask,
Dan Gohmanf990faf2008-02-13 00:35:47 +00005647 APInt &KnownZero,
5648 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00005649 const SelectionDAG &DAG,
Nate Begeman8a77efe2006-02-16 21:11:51 +00005650 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005651 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00005652 assert((Opc >= ISD::BUILTIN_OP_END ||
5653 Opc == ISD::INTRINSIC_WO_CHAIN ||
5654 Opc == ISD::INTRINSIC_W_CHAIN ||
5655 Opc == ISD::INTRINSIC_VOID) &&
5656 "Should use MaskedValueIsZero if you don't know whether Op"
5657 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005658
Dan Gohman9ca025f2008-02-13 23:07:24 +00005659 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005660 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00005661 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005662 case X86ISD::SETCC:
Dan Gohmanf990faf2008-02-13 00:35:47 +00005663 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5664 Mask.getBitWidth() - 1);
Nate Begeman8a77efe2006-02-16 21:11:51 +00005665 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005666 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00005667}
Chris Lattnerc642aa52006-01-31 19:43:35 +00005668
Evan Cheng5987cfb2006-07-07 08:33:52 +00005669/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5670/// element of the result of the vector shuffle.
5671static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5672 MVT::ValueType VT = N->getValueType(0);
5673 SDOperand PermMask = N->getOperand(2);
5674 unsigned NumElems = PermMask.getNumOperands();
5675 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5676 i %= NumElems;
5677 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5678 return (i == 0)
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +00005679 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng5987cfb2006-07-07 08:33:52 +00005680 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5681 SDOperand Idx = PermMask.getOperand(i);
5682 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman5c441312007-06-14 22:58:02 +00005683 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng5987cfb2006-07-07 08:33:52 +00005684 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5685 }
5686 return SDOperand();
5687}
5688
5689/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5690/// node is a GlobalAddress + an offset.
5691static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Chengae1cd752006-11-30 21:55:46 +00005692 unsigned Opc = N->getOpcode();
Evan Cheng62cdc3f2006-12-05 04:01:03 +00005693 if (Opc == X86ISD::Wrapper) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005694 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5695 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5696 return true;
5697 }
Evan Chengae1cd752006-11-30 21:55:46 +00005698 } else if (Opc == ISD::ADD) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005699 SDOperand N1 = N->getOperand(0);
5700 SDOperand N2 = N->getOperand(1);
5701 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5702 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5703 if (V) {
5704 Offset += V->getSignExtended();
5705 return true;
5706 }
5707 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5708 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5709 if (V) {
5710 Offset += V->getSignExtended();
5711 return true;
5712 }
5713 }
5714 }
5715 return false;
5716}
5717
5718/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5719/// + Dist * Size.
5720static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5721 MachineFrameInfo *MFI) {
5722 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5723 return false;
5724
5725 SDOperand Loc = N->getOperand(1);
5726 SDOperand BaseLoc = Base->getOperand(1);
5727 if (Loc.getOpcode() == ISD::FrameIndex) {
5728 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5729 return false;
Dan Gohmanb6a8ae22007-07-23 20:24:29 +00005730 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5731 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng5987cfb2006-07-07 08:33:52 +00005732 int FS = MFI->getObjectSize(FI);
5733 int BFS = MFI->getObjectSize(BFI);
5734 if (FS != BFS || FS != Size) return false;
5735 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5736 } else {
5737 GlobalValue *GV1 = NULL;
5738 GlobalValue *GV2 = NULL;
5739 int64_t Offset1 = 0;
5740 int64_t Offset2 = 0;
5741 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5742 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5743 if (isGA1 && isGA2 && GV1 == GV2)
5744 return Offset1 == (Offset2 + Dist*Size);
5745 }
5746
5747 return false;
5748}
5749
Evan Cheng79cf9a52006-07-10 21:37:44 +00005750static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5751 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005752 GlobalValue *GV;
Nick Lewyckyf5b99382008-02-02 08:29:58 +00005753 int64_t Offset = 0;
Evan Cheng5987cfb2006-07-07 08:33:52 +00005754 if (isGAPlusOffset(Base, GV, Offset))
5755 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner250789f2008-01-26 20:07:42 +00005756 // DAG combine handles the stack object case.
Evan Cheng5987cfb2006-07-07 08:33:52 +00005757 return false;
5758}
5759
5760
5761/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5762/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5763/// if the load addresses are consecutive, non-overlapping, and in the right
5764/// order.
Evan Cheng79cf9a52006-07-10 21:37:44 +00005765static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5766 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005767 MachineFunction &MF = DAG.getMachineFunction();
5768 MachineFrameInfo *MFI = MF.getFrameInfo();
5769 MVT::ValueType VT = N->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00005770 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng5987cfb2006-07-07 08:33:52 +00005771 SDOperand PermMask = N->getOperand(2);
5772 int NumElems = (int)PermMask.getNumOperands();
5773 SDNode *Base = NULL;
5774 for (int i = 0; i < NumElems; ++i) {
5775 SDOperand Idx = PermMask.getOperand(i);
5776 if (Idx.getOpcode() == ISD::UNDEF) {
5777 if (!Base) return SDOperand();
5778 } else {
5779 SDOperand Arg =
5780 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005781 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng5987cfb2006-07-07 08:33:52 +00005782 return SDOperand();
5783 if (!Base)
5784 Base = Arg.Val;
5785 else if (!isConsecutiveLoad(Arg.Val, Base,
5786 i, MVT::getSizeInBits(EVT)/8,MFI))
5787 return SDOperand();
5788 }
5789 }
5790
Evan Cheng79cf9a52006-07-10 21:37:44 +00005791 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman47885522007-07-27 17:16:43 +00005792 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005793 if (isAlign16) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005794 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman47885522007-07-27 17:16:43 +00005795 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chenge71fe34d2006-10-09 20:57:25 +00005796 } else {
Dan Gohman47885522007-07-27 17:16:43 +00005797 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5798 LD->getSrcValueOffset(), LD->isVolatile(),
5799 LD->getAlignment());
Evan Cheng5c68bba2006-08-11 07:35:45 +00005800 }
Evan Cheng5987cfb2006-07-07 08:33:52 +00005801}
5802
Chris Lattner9259b1e2006-10-04 06:57:07 +00005803/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5804static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5805 const X86Subtarget *Subtarget) {
5806 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005807
Chris Lattner9259b1e2006-10-04 06:57:07 +00005808 // If we have SSE[12] support, try to form min/max nodes.
5809 if (Subtarget->hasSSE2() &&
5810 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5811 if (Cond.getOpcode() == ISD::SETCC) {
5812 // Get the LHS/RHS of the select.
5813 SDOperand LHS = N->getOperand(1);
5814 SDOperand RHS = N->getOperand(2);
5815 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005816
Evan Cheng49683ba2006-11-10 21:43:37 +00005817 unsigned Opcode = 0;
Chris Lattner9259b1e2006-10-04 06:57:07 +00005818 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005819 switch (CC) {
5820 default: break;
5821 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5822 case ISD::SETULE:
5823 case ISD::SETLE:
5824 if (!UnsafeFPMath) break;
5825 // FALL THROUGH.
5826 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5827 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00005828 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005829 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005830
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005831 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5832 case ISD::SETUGT:
5833 case ISD::SETGT:
5834 if (!UnsafeFPMath) break;
5835 // FALL THROUGH.
5836 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5837 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00005838 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005839 break;
5840 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00005841 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005842 switch (CC) {
5843 default: break;
5844 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5845 case ISD::SETUGT:
5846 case ISD::SETGT:
5847 if (!UnsafeFPMath) break;
5848 // FALL THROUGH.
5849 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5850 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00005851 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005852 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005853
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005854 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5855 case ISD::SETULE:
5856 case ISD::SETLE:
5857 if (!UnsafeFPMath) break;
5858 // FALL THROUGH.
5859 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5860 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00005861 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00005862 break;
5863 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00005864 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005865
Evan Cheng49683ba2006-11-10 21:43:37 +00005866 if (Opcode)
5867 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner9259b1e2006-10-04 06:57:07 +00005868 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005869
Chris Lattner9259b1e2006-10-04 06:57:07 +00005870 }
5871
5872 return SDOperand();
5873}
5874
Chris Lattnerf4523c32008-01-25 06:14:17 +00005875/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
5876/// X86ISD::FXOR nodes.
Chris Lattner84ab7242008-01-25 05:46:26 +00005877static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf4523c32008-01-25 06:14:17 +00005878 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
5879 // F[X]OR(0.0, x) -> x
5880 // F[X]OR(x, 0.0) -> x
Chris Lattner84ab7242008-01-25 05:46:26 +00005881 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5882 if (C->getValueAPF().isPosZero())
5883 return N->getOperand(1);
5884 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5885 if (C->getValueAPF().isPosZero())
5886 return N->getOperand(0);
5887 return SDOperand();
5888}
5889
5890/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
5891static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
5892 // FAND(0.0, x) -> 0.0
5893 // FAND(x, 0.0) -> 0.0
5894 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5895 if (C->getValueAPF().isPosZero())
5896 return N->getOperand(0);
5897 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5898 if (C->getValueAPF().isPosZero())
5899 return N->getOperand(1);
5900 return SDOperand();
5901}
5902
Chris Lattner9259b1e2006-10-04 06:57:07 +00005903
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005904SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng5987cfb2006-07-07 08:33:52 +00005905 DAGCombinerInfo &DCI) const {
Evan Cheng5987cfb2006-07-07 08:33:52 +00005906 SelectionDAG &DAG = DCI.DAG;
5907 switch (N->getOpcode()) {
5908 default: break;
Chris Lattner84ab7242008-01-25 05:46:26 +00005909 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
5910 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerf4523c32008-01-25 06:14:17 +00005911 case X86ISD::FXOR:
Chris Lattner84ab7242008-01-25 05:46:26 +00005912 case X86ISD::FOR: return PerformFORCombine(N, DAG);
5913 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng5987cfb2006-07-07 08:33:52 +00005914 }
5915
5916 return SDOperand();
5917}
5918
Evan Cheng02612422006-07-05 22:17:51 +00005919//===----------------------------------------------------------------------===//
5920// X86 Inline Assembly Support
5921//===----------------------------------------------------------------------===//
5922
Chris Lattner298ef372006-07-11 02:54:03 +00005923/// getConstraintType - Given a constraint letter, return the type of
5924/// constraint it is for this target.
5925X86TargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00005926X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5927 if (Constraint.size() == 1) {
5928 switch (Constraint[0]) {
5929 case 'A':
5930 case 'r':
5931 case 'R':
5932 case 'l':
5933 case 'q':
5934 case 'Q':
5935 case 'x':
5936 case 'Y':
5937 return C_RegisterClass;
5938 default:
5939 break;
5940 }
Chris Lattner298ef372006-07-11 02:54:03 +00005941 }
Chris Lattnerd6855142007-03-25 02:14:49 +00005942 return TargetLowering::getConstraintType(Constraint);
Chris Lattner298ef372006-07-11 02:54:03 +00005943}
5944
Dale Johannesen2b3bc302008-01-29 02:21:21 +00005945/// LowerXConstraint - try to replace an X constraint, which matches anything,
5946/// with another that has more specific requirements based on the type of the
5947/// corresponding operand.
5948void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
5949 std::string& s) const {
5950 if (MVT::isFloatingPoint(ConstraintVT)) {
5951 if (Subtarget->hasSSE2())
5952 s = "Y";
5953 else if (Subtarget->hasSSE1())
5954 s = "x";
5955 else
5956 s = "f";
5957 } else
5958 return TargetLowering::lowerXConstraint(ConstraintVT, s);
5959}
5960
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00005961/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5962/// vector. If it is invalid, don't add anything to Ops.
5963void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5964 char Constraint,
5965 std::vector<SDOperand>&Ops,
5966 SelectionDAG &DAG) {
5967 SDOperand Result(0, 0);
5968
Chris Lattner44daa502006-10-31 20:13:11 +00005969 switch (Constraint) {
5970 default: break;
Devang Patelb38c2ec2007-03-17 00:13:28 +00005971 case 'I':
Chris Lattner03a643a2007-03-25 01:57:35 +00005972 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00005973 if (C->getValue() <= 31) {
5974 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5975 break;
5976 }
Devang Patelb38c2ec2007-03-17 00:13:28 +00005977 }
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00005978 return;
Chris Lattner03a643a2007-03-25 01:57:35 +00005979 case 'N':
5980 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00005981 if (C->getValue() <= 255) {
5982 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5983 break;
5984 }
Chris Lattner03a643a2007-03-25 01:57:35 +00005985 }
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00005986 return;
Chris Lattner83df45a2007-05-03 16:52:29 +00005987 case 'i': {
Chris Lattner44daa502006-10-31 20:13:11 +00005988 // Literal immediates are always ok.
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00005989 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5990 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5991 break;
5992 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005993
Chris Lattner83df45a2007-05-03 16:52:29 +00005994 // If we are in non-pic codegen mode, we allow the address of a global (with
5995 // an optional displacement) to be used with 'i'.
5996 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5997 int64_t Offset = 0;
5998
5999 // Match either (GA) or (GA+C)
6000 if (GA) {
6001 Offset = GA->getOffset();
6002 } else if (Op.getOpcode() == ISD::ADD) {
6003 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6004 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6005 if (C && GA) {
6006 Offset = GA->getOffset()+C->getValue();
6007 } else {
6008 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6009 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6010 if (C && GA)
6011 Offset = GA->getOffset()+C->getValue();
6012 else
6013 C = 0, GA = 0;
6014 }
6015 }
6016
6017 if (GA) {
6018 // If addressing this global requires a load (e.g. in PIC mode), we can't
6019 // match.
6020 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6021 false))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006022 return;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006023
Chris Lattner83df45a2007-05-03 16:52:29 +00006024 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6025 Offset);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006026 Result = Op;
6027 break;
Chris Lattner44daa502006-10-31 20:13:11 +00006028 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006029
Chris Lattner44daa502006-10-31 20:13:11 +00006030 // Otherwise, not valid for this mode.
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006031 return;
Chris Lattner44daa502006-10-31 20:13:11 +00006032 }
Chris Lattner83df45a2007-05-03 16:52:29 +00006033 }
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00006034
6035 if (Result.Val) {
6036 Ops.push_back(Result);
6037 return;
6038 }
6039 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner44daa502006-10-31 20:13:11 +00006040}
6041
Chris Lattnerc642aa52006-01-31 19:43:35 +00006042std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00006043getRegClassForInlineAsmConstraint(const std::string &Constraint,
6044 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00006045 if (Constraint.size() == 1) {
6046 // FIXME: not handling fp-stack yet!
Chris Lattnerc642aa52006-01-31 19:43:35 +00006047 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattner298ef372006-07-11 02:54:03 +00006048 default: break; // Unknown constraint letter
6049 case 'A': // EAX/EDX
6050 if (VT == MVT::i32 || VT == MVT::i64)
6051 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6052 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00006053 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6054 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00006055 if (VT == MVT::i32)
6056 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6057 else if (VT == MVT::i16)
6058 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6059 else if (VT == MVT::i8)
Evan Chengb2823da2007-08-13 23:27:11 +00006060 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner296160d2007-11-04 06:51:12 +00006061 else if (VT == MVT::i64)
6062 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6063 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00006064 }
6065 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006066
Chris Lattner7ad77df2006-02-22 00:56:39 +00006067 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00006068}
Chris Lattner524129d2006-07-31 23:26:50 +00006069
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006070std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner524129d2006-07-31 23:26:50 +00006071X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6072 MVT::ValueType VT) const {
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006073 // First, see if this is a constraint that directly corresponds to an LLVM
6074 // register class.
6075 if (Constraint.size() == 1) {
6076 // GCC Constraint Letters
6077 switch (Constraint[0]) {
6078 default: break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00006079 case 'r': // GENERAL_REGS
6080 case 'R': // LEGACY_REGS
6081 case 'l': // INDEX_REGS
6082 if (VT == MVT::i64 && Subtarget->is64Bit())
6083 return std::make_pair(0U, X86::GR64RegisterClass);
6084 if (VT == MVT::i32)
6085 return std::make_pair(0U, X86::GR32RegisterClass);
6086 else if (VT == MVT::i16)
6087 return std::make_pair(0U, X86::GR16RegisterClass);
6088 else if (VT == MVT::i8)
6089 return std::make_pair(0U, X86::GR8RegisterClass);
6090 break;
Chris Lattner2805bce2007-04-12 04:14:49 +00006091 case 'y': // MMX_REGS if MMX allowed.
6092 if (!Subtarget->hasMMX()) break;
6093 return std::make_pair(0U, X86::VR64RegisterClass);
6094 break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00006095 case 'Y': // SSE_REGS if SSE2 allowed
6096 if (!Subtarget->hasSSE2()) break;
6097 // FALL THROUGH.
6098 case 'x': // SSE_REGS if SSE1 allowed
6099 if (!Subtarget->hasSSE1()) break;
6100
6101 switch (VT) {
6102 default: break;
6103 // Scalar SSE types.
6104 case MVT::f32:
6105 case MVT::i32:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006106 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00006107 case MVT::f64:
6108 case MVT::i64:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006109 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00006110 // Vector types.
Chris Lattner7451e4d2007-04-09 05:49:22 +00006111 case MVT::v16i8:
6112 case MVT::v8i16:
6113 case MVT::v4i32:
6114 case MVT::v2i64:
6115 case MVT::v4f32:
6116 case MVT::v2f64:
6117 return std::make_pair(0U, X86::VR128RegisterClass);
6118 }
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00006119 break;
6120 }
6121 }
6122
Chris Lattner524129d2006-07-31 23:26:50 +00006123 // Use the default implementation in TargetLowering to convert the register
6124 // constraint into a member of a register class.
6125 std::pair<unsigned, const TargetRegisterClass*> Res;
6126 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerf6a69662006-10-31 19:42:44 +00006127
6128 // Not found as a standard register?
6129 if (Res.second == 0) {
6130 // GCC calls "st(0)" just plain "st".
6131 if (StringsEqualNoCase("{st}", Constraint)) {
6132 Res.first = X86::ST0;
Chris Lattner5b5484d2007-09-24 05:27:37 +00006133 Res.second = X86::RFP80RegisterClass;
Chris Lattnerf6a69662006-10-31 19:42:44 +00006134 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006135
Chris Lattnerf6a69662006-10-31 19:42:44 +00006136 return Res;
6137 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006138
Chris Lattner524129d2006-07-31 23:26:50 +00006139 // Otherwise, check to see if this is a register class of the wrong value
6140 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6141 // turn into {ax},{dx}.
6142 if (Res.second->hasType(VT))
6143 return Res; // Correct type already, nothing to do.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006144
Chris Lattner524129d2006-07-31 23:26:50 +00006145 // All of the single-register GCC register classes map their values onto
6146 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6147 // really want an 8-bit or 32-bit register, map to the appropriate register
6148 // class and return the appropriate register.
6149 if (Res.second != X86::GR16RegisterClass)
6150 return Res;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006151
Chris Lattner524129d2006-07-31 23:26:50 +00006152 if (VT == MVT::i8) {
6153 unsigned DestReg = 0;
6154 switch (Res.first) {
6155 default: break;
6156 case X86::AX: DestReg = X86::AL; break;
6157 case X86::DX: DestReg = X86::DL; break;
6158 case X86::CX: DestReg = X86::CL; break;
6159 case X86::BX: DestReg = X86::BL; break;
6160 }
6161 if (DestReg) {
6162 Res.first = DestReg;
6163 Res.second = Res.second = X86::GR8RegisterClass;
6164 }
6165 } else if (VT == MVT::i32) {
6166 unsigned DestReg = 0;
6167 switch (Res.first) {
6168 default: break;
6169 case X86::AX: DestReg = X86::EAX; break;
6170 case X86::DX: DestReg = X86::EDX; break;
6171 case X86::CX: DestReg = X86::ECX; break;
6172 case X86::BX: DestReg = X86::EBX; break;
6173 case X86::SI: DestReg = X86::ESI; break;
6174 case X86::DI: DestReg = X86::EDI; break;
6175 case X86::BP: DestReg = X86::EBP; break;
6176 case X86::SP: DestReg = X86::ESP; break;
6177 }
6178 if (DestReg) {
6179 Res.first = DestReg;
6180 Res.second = Res.second = X86::GR32RegisterClass;
6181 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00006182 } else if (VT == MVT::i64) {
6183 unsigned DestReg = 0;
6184 switch (Res.first) {
6185 default: break;
6186 case X86::AX: DestReg = X86::RAX; break;
6187 case X86::DX: DestReg = X86::RDX; break;
6188 case X86::CX: DestReg = X86::RCX; break;
6189 case X86::BX: DestReg = X86::RBX; break;
6190 case X86::SI: DestReg = X86::RSI; break;
6191 case X86::DI: DestReg = X86::RDI; break;
6192 case X86::BP: DestReg = X86::RBP; break;
6193 case X86::SP: DestReg = X86::RSP; break;
6194 }
6195 if (DestReg) {
6196 Res.first = DestReg;
6197 Res.second = Res.second = X86::GR64RegisterClass;
6198 }
Chris Lattner524129d2006-07-31 23:26:50 +00006199 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00006200
Chris Lattner524129d2006-07-31 23:26:50 +00006201 return Res;
6202}