blob: 505c302816513600b2820f7cb3dba05a7a4c5a1a [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000026#include "llvm/ADT/VectorExtras.h"
27#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/SelectionDAG.h"
33#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000034#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000038#include "llvm/ADT/StringExtras.h"
Duncan Sandsb116fac2007-07-27 20:02:49 +000039#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000040using namespace llvm;
41
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042X86TargetLowering::X86TargetLowering(TargetMachine &TM)
43 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000044 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000045 X86ScalarSSEf64 = Subtarget->hasSSE2();
46 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000047 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000048
Evan Cheng559806f2006-01-27 08:10:46 +000049
Anton Korobeynikov2365f512007-07-14 14:06:15 +000050 RegInfo = TM.getRegisterInfo();
51
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 // Set up the TargetLowering object.
53
54 // X86 is weird, it always uses i8 for shift amounts and setcc results.
55 setShiftAmountType(MVT::i8);
56 setSetCCResultType(MVT::i8);
57 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000058 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000059 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000060 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000061
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000062 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000063 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000064 setUseUnderscoreSetJmp(false);
65 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000066 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 // MS runtime is weird: it exports _setjmp, but longjmp!
68 setUseUnderscoreSetJmp(true);
69 setUseUnderscoreLongJmp(false);
70 } else {
71 setUseUnderscoreSetJmp(true);
72 setUseUnderscoreLongJmp(true);
73 }
74
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000075 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000076 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
77 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
78 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000079 if (Subtarget->is64Bit())
80 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000081
Evan Chengc5484282006-10-04 00:56:09 +000082 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
83
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000084 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
85 // operation.
86 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
87 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
88 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000089
Evan Cheng25ab6902006-09-08 06:48:29 +000090 if (Subtarget->is64Bit()) {
91 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000092 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000093 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000094 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +000095 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
96 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
97 else
98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
99 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000100
101 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
102 // this operation.
103 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
104 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000105 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000106 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000107 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000108 // f32 and f64 cases are Legal, f80 case is not
109 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
110 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000111 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
112 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
113 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000114
Dale Johannesen73328d12007-09-19 23:55:34 +0000115 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
116 // are Legal, f80 is custom lowered.
117 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
118 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000119
Evan Cheng02568ff2006-01-30 22:13:22 +0000120 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
121 // this operation.
122 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
123 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
124
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000125 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000126 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000127 // f32 and f64 cases are Legal, f80 case is not
128 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000129 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000130 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000131 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132 }
133
134 // Handle FP_TO_UINT by promoting the destination to a larger signed
135 // conversion.
136 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
137 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
138 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
139
Evan Cheng25ab6902006-09-08 06:48:29 +0000140 if (Subtarget->is64Bit()) {
141 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000142 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000143 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000144 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000145 // Expand FP_TO_UINT into a select.
146 // FIXME: We would like to use a Custom expander here eventually to do
147 // the optimal thing for SSE vs. the default expansion in the legalizer.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
149 else
150 // With SSE3 we can use fisttpll to convert to a signed i64.
151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
152 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000153
Chris Lattner399610a2006-12-05 18:22:22 +0000154 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000155 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000156 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
157 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
158 }
Chris Lattner21f66852005-12-23 05:15:23 +0000159
Dan Gohman525178c2007-10-08 18:33:35 +0000160 // Scalar integer multiply, multiply-high, divide, and remainder are
161 // lowered to use operations that produce two results, to match the
162 // available instructions. This exposes the two-result form to trivial
163 // CSE, which is able to combine x/y and x%y into a single instruction,
164 // for example. The single-result multiply instructions are introduced
165 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
166 // is not needed.
167 setOperationAction(ISD::MUL , MVT::i8 , Expand);
168 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
169 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
170 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
171 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
172 setOperationAction(ISD::SREM , MVT::i8 , Expand);
173 setOperationAction(ISD::UREM , MVT::i8 , Expand);
174 setOperationAction(ISD::MUL , MVT::i16 , Expand);
175 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
176 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
177 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
178 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
179 setOperationAction(ISD::SREM , MVT::i16 , Expand);
180 setOperationAction(ISD::UREM , MVT::i16 , Expand);
181 setOperationAction(ISD::MUL , MVT::i32 , Expand);
182 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
186 setOperationAction(ISD::SREM , MVT::i32 , Expand);
187 setOperationAction(ISD::UREM , MVT::i32 , Expand);
188 setOperationAction(ISD::MUL , MVT::i64 , Expand);
189 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
193 setOperationAction(ISD::SREM , MVT::i64 , Expand);
194 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000195
Evan Chengc35497f2006-10-30 08:02:39 +0000196 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000197 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000198 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
199 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000200 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000202 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
206 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000208 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
209
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000210 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
211 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
212 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
213 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
214 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
215 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
216 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
217 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
218 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 if (Subtarget->is64Bit()) {
220 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
221 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
222 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
223 }
224
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000225 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000226 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000227
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228 // These should be promoted to a larger select which is supported.
229 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
230 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000231 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000232 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
233 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
234 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
235 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000236 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000237 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
238 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
239 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
240 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
241 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000242 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000243 if (Subtarget->is64Bit()) {
244 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
245 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
246 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000247 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000248 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000249 if (!Subtarget->is64Bit())
250 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
251
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000252 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000253 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000254 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000255 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000256 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000257 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 if (Subtarget->is64Bit()) {
259 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
260 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
261 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
262 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
263 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000264 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000265 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
266 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
267 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000268 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000269 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
270 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000271
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000272 // Use the default ISD::LOCATION expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000273 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000274 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000275 if (!Subtarget->isTargetDarwin() &&
276 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000277 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000278 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000279
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000280 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
281 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
282 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
283 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
284 if (Subtarget->is64Bit()) {
285 // FIXME: Verify
286 setExceptionPointerRegister(X86::RAX);
287 setExceptionSelectorRegister(X86::RDX);
288 } else {
289 setExceptionPointerRegister(X86::EAX);
290 setExceptionSelectorRegister(X86::EDX);
291 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000292 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000293
Duncan Sandsf7331b32007-09-11 14:10:23 +0000294 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000295
Nate Begemanacc398c2006-01-25 18:21:52 +0000296 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
297 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000298 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000299 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000300 if (Subtarget->is64Bit())
301 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
302 else
303 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
304
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000305 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000306 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000307 if (Subtarget->is64Bit())
308 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000309 if (Subtarget->isTargetCygMing())
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
311 else
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000313
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000314 if (X86ScalarSSEf64) {
315 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000316 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000317 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
318 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000319
Evan Cheng223547a2006-01-31 22:28:30 +0000320 // Use ANDPD to simulate FABS.
321 setOperationAction(ISD::FABS , MVT::f64, Custom);
322 setOperationAction(ISD::FABS , MVT::f32, Custom);
323
324 // Use XORP to simulate FNEG.
325 setOperationAction(ISD::FNEG , MVT::f64, Custom);
326 setOperationAction(ISD::FNEG , MVT::f32, Custom);
327
Evan Cheng68c47cb2007-01-05 07:55:56 +0000328 // Use ANDPD and ORPD to simulate FCOPYSIGN.
329 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
330 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
331
Evan Chengd25e9e82006-02-02 00:28:23 +0000332 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000333 setOperationAction(ISD::FSIN , MVT::f64, Expand);
334 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000335 setOperationAction(ISD::FREM , MVT::f64, Expand);
336 setOperationAction(ISD::FSIN , MVT::f32, Expand);
337 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000338 setOperationAction(ISD::FREM , MVT::f32, Expand);
339
Chris Lattnera54aa942006-01-29 06:26:08 +0000340 // Expand FP immediates into loads from the stack, except for the special
341 // cases we handle.
342 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
343 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000344 addLegalFPImmediate(APFloat(+0.0)); // xorpd
345 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000346
347 // Conversions to long double (in X87) go through memory.
348 setConvertAction(MVT::f32, MVT::f80, Expand);
349 setConvertAction(MVT::f64, MVT::f80, Expand);
350
351 // Conversions from long double (in X87) go through memory.
352 setConvertAction(MVT::f80, MVT::f32, Expand);
353 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000354 } else if (X86ScalarSSEf32) {
355 // Use SSE for f32, x87 for f64.
356 // Set up the FP register classes.
357 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
358 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
359
360 // Use ANDPS to simulate FABS.
361 setOperationAction(ISD::FABS , MVT::f32, Custom);
362
363 // Use XORP to simulate FNEG.
364 setOperationAction(ISD::FNEG , MVT::f32, Custom);
365
366 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
367
368 // Use ANDPS and ORPS to simulate FCOPYSIGN.
369 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
370 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
371
372 // We don't support sin/cos/fmod
373 setOperationAction(ISD::FSIN , MVT::f32, Expand);
374 setOperationAction(ISD::FCOS , MVT::f32, Expand);
375 setOperationAction(ISD::FREM , MVT::f32, Expand);
376
377 // Expand FP immediates into loads from the stack, except for the special
378 // cases we handle.
379 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
380 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
381 addLegalFPImmediate(APFloat(+0.0f)); // xorps
382 addLegalFPImmediate(APFloat(+0.0)); // FLD0
383 addLegalFPImmediate(APFloat(+1.0)); // FLD1
384 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
385 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
386
387 // SSE->x87 conversions go through memory.
388 setConvertAction(MVT::f32, MVT::f64, Expand);
389 setConvertAction(MVT::f32, MVT::f80, Expand);
390
391 // x87->SSE truncations need to go through memory.
392 setConvertAction(MVT::f80, MVT::f32, Expand);
393 setConvertAction(MVT::f64, MVT::f32, Expand);
394 // And x87->x87 truncations also.
395 setConvertAction(MVT::f80, MVT::f64, Expand);
396
397 if (!UnsafeFPMath) {
398 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
399 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
400 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000401 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000402 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000404 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
405 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000406
Evan Cheng68c47cb2007-01-05 07:55:56 +0000407 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000408 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000409 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
410 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000411
412 // Floating truncations need to go through memory.
413 setConvertAction(MVT::f80, MVT::f32, Expand);
414 setConvertAction(MVT::f64, MVT::f32, Expand);
415 setConvertAction(MVT::f80, MVT::f64, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000416
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000417 if (!UnsafeFPMath) {
418 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
419 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
420 }
421
Chris Lattnera54aa942006-01-29 06:26:08 +0000422 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000423 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000424 addLegalFPImmediate(APFloat(+0.0)); // FLD0
425 addLegalFPImmediate(APFloat(+1.0)); // FLD1
426 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
427 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000428 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
429 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
430 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
431 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000433
Dale Johannesen59a58732007-08-05 18:49:15 +0000434 // Long double always uses X87.
435 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000436 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
437 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
438 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000439 if (!UnsafeFPMath) {
440 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
441 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
442 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000443
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000444 // Always use a library call for pow.
445 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
446 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
447 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
448
Evan Chengd30bf012006-03-01 01:11:20 +0000449 // First set operation action for all vector types to expand. Then we
450 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000451 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
452 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000453 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
454 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000455 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000456 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000457 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000458 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000459 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
460 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000465 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000466 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000467 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000468 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000469 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
470 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000476 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
478 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000480 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohmanf0d00892007-10-12 14:09:42 +0000481 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
482 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000484 }
485
Evan Chenga88973f2006-03-22 19:22:18 +0000486 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000487 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
488 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
489 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000490 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000491
Evan Chengd30bf012006-03-01 01:11:20 +0000492 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000493
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000494 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
495 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
496 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000497 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000498
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000499 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
500 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
501 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000502 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000503
Bill Wendling74027e92007-03-15 21:24:36 +0000504 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
505 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
506
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000507 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000508 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000509 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000510 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
511 setOperationAction(ISD::AND, MVT::v2i32, Promote);
512 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
513 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000514
515 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000516 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000517 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000518 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
519 setOperationAction(ISD::OR, MVT::v2i32, Promote);
520 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
521 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000522
523 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000524 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000525 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000526 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
527 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
528 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
529 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000530
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000531 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000532 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000533 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000534 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
535 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
536 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
537 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000538
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000539 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
540 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
541 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
542 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000543
544 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
545 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
546 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000547 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000548
549 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
550 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000551 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
552 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000553 }
554
Evan Chenga88973f2006-03-22 19:22:18 +0000555 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000556 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
557
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000558 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
559 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
560 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
561 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000562 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
563 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000564 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
565 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
566 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000567 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000568 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000569 }
570
Evan Chenga88973f2006-03-22 19:22:18 +0000571 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000572 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
573 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
574 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
575 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
576 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
577
Evan Chengf7c378e2006-04-10 07:23:14 +0000578 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
579 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
580 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000581 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000582 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
583 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
584 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000585 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000586 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000587 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
588 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
589 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
590 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000591 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
592 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000593
Evan Chengf7c378e2006-04-10 07:23:14 +0000594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
595 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000596 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000597 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
598 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
599 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000600
Evan Cheng2c3ae372006-04-12 21:21:57 +0000601 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
602 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
603 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
604 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
605 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
606 }
607 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
608 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
609 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
610 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
611 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000612 if (Subtarget->is64Bit())
613 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000614
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000615 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000616 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
617 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
618 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
619 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
620 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
621 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
622 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000623 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
624 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000625 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
626 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000627 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000628
629 // Custom lower v2i64 and v2f64 selects.
630 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000631 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000632 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000633 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000634 }
635
Evan Cheng6be2c582006-04-05 23:38:46 +0000636 // We want to custom lower some of our intrinsics.
637 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
638
Evan Cheng206ee9d2006-07-07 08:33:52 +0000639 // We have target-specific dag combine patterns for the following nodes:
640 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000641 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000642
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 computeRegisterProperties();
644
Evan Cheng87ed7162006-02-14 08:25:08 +0000645 // FIXME: These should be based on subtarget info. Plus, the values should
646 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000647 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
648 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
649 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000650 allowUnalignedMemoryAccesses = true; // x86 supports it!
651}
652
Chris Lattner2b02a442007-02-25 08:29:00 +0000653
Evan Chengcc415862007-11-09 01:32:10 +0000654/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
655/// jumptable.
656SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
657 SelectionDAG &DAG) const {
658 if (usesGlobalOffsetTable())
659 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
660 if (!Subtarget->isPICStyleRIPRel())
661 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
662 return Table;
663}
664
Chris Lattner2b02a442007-02-25 08:29:00 +0000665//===----------------------------------------------------------------------===//
666// Return Value Calling Convention Implementation
667//===----------------------------------------------------------------------===//
668
Chris Lattner59ed56b2007-02-28 04:55:35 +0000669#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000670
671/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
672/// exists skip possible ISD:TokenFactor.
673static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
674 if (Chain.getOpcode()==X86ISD::TAILCALL) {
675 return Chain;
676 } else if (Chain.getOpcode()==ISD::TokenFactor) {
677 if (Chain.getNumOperands() &&
678 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
679 return Chain.getOperand(0);
680 }
681 return Chain;
682}
Chris Lattner9774c912007-02-27 05:28:59 +0000683
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000684/// LowerRET - Lower an ISD::RET node.
685SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
686 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
687
Chris Lattner9774c912007-02-27 05:28:59 +0000688 SmallVector<CCValAssign, 16> RVLocs;
689 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000690 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
691 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000692 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000693
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000694 // If this is the first return lowered for this function, add the regs to the
695 // liveout set for the function.
696 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000697 for (unsigned i = 0; i != RVLocs.size(); ++i)
698 if (RVLocs[i].isRegLoc())
699 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000700 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000701 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000702
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000703 // Handle tail call return.
704 Chain = GetPossiblePreceedingTailCall(Chain);
705 if (Chain.getOpcode() == X86ISD::TAILCALL) {
706 SDOperand TailCall = Chain;
707 SDOperand TargetAddress = TailCall.getOperand(1);
708 SDOperand StackAdjustment = TailCall.getOperand(2);
709 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
710 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
711 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
712 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
713 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
714 "Expecting an global address, external symbol, or register");
715 assert( StackAdjustment.getOpcode() == ISD::Constant &&
716 "Expecting a const value");
717
718 SmallVector<SDOperand,8> Operands;
719 Operands.push_back(Chain.getOperand(0));
720 Operands.push_back(TargetAddress);
721 Operands.push_back(StackAdjustment);
722 // Copy registers used by the call. Last operand is a flag so it is not
723 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000724 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000725 Operands.push_back(Chain.getOperand(i));
726 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000727 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
728 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000729 }
730
731 // Regular return.
732 SDOperand Flag;
733
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000734 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000735 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
736 RVLocs[0].getLocReg() != X86::ST0) {
737 for (unsigned i = 0; i != RVLocs.size(); ++i) {
738 CCValAssign &VA = RVLocs[i];
739 assert(VA.isRegLoc() && "Can only return in registers!");
740 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
741 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000742 Flag = Chain.getValue(1);
743 }
744 } else {
745 // We need to handle a destination of ST0 specially, because it isn't really
746 // a register.
747 SDOperand Value = Op.getOperand(1);
748
749 // If this is an FP return with ScalarSSE, we need to move the value from
750 // an XMM register onto the fp-stack.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000751 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
752 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000753 SDOperand MemLoc;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000754
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000755 // If this is a load into a scalarsse value, don't store the loaded value
756 // back to the stack, only to reload it: just replace the scalar-sse load.
757 if (ISD::isNON_EXTLoad(Value.Val) &&
758 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
759 Chain = Value.getOperand(0);
760 MemLoc = Value.getOperand(1);
761 } else {
762 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000763 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000764 MachineFunction &MF = DAG.getMachineFunction();
765 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
766 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
767 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
768 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000769 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000770 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000771 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
772 Chain = Value.getValue(1);
773 }
774
775 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
776 SDOperand Ops[] = { Chain, Value };
777 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
778 Flag = Chain.getValue(1);
779 }
780
781 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
782 if (Flag.Val)
783 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
784 else
785 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
786}
787
788
Chris Lattner3085e152007-02-25 08:59:22 +0000789/// LowerCallResult - Lower the result values of an ISD::CALL into the
790/// appropriate copies out of appropriate physical registers. This assumes that
791/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
792/// being lowered. The returns a SDNode with the same number of values as the
793/// ISD::CALL.
794SDNode *X86TargetLowering::
795LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
796 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000797
798 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000799 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000800 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
801 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000802 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
803
Chris Lattner3085e152007-02-25 08:59:22 +0000804
Chris Lattnere32bbf62007-02-28 07:09:55 +0000805 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000806
807 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000808 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
809 for (unsigned i = 0; i != RVLocs.size(); ++i) {
810 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
811 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000812 InFlag = Chain.getValue(2);
813 ResultVals.push_back(Chain.getValue(0));
814 }
815 } else {
816 // Copies from the FP stack are special, as ST0 isn't a valid register
817 // before the fp stackifier runs.
818
819 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000820 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000821 SDOperand GROps[] = { Chain, InFlag };
822 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
823 Chain = RetVal.getValue(1);
824 InFlag = RetVal.getValue(2);
825
826 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
827 // an XMM register.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000828 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
829 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner3085e152007-02-25 08:59:22 +0000830 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
831 // shouldn't be necessary except that RFP cannot be live across
832 // multiple blocks. When stackifier is fixed, they can be uncoupled.
833 MachineFunction &MF = DAG.getMachineFunction();
834 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
835 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
836 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000837 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000838 };
839 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000840 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000841 Chain = RetVal.getValue(1);
842 }
Chris Lattner3085e152007-02-25 08:59:22 +0000843 ResultVals.push_back(RetVal);
844 }
845
846 // Merge everything together with a MERGE_VALUES node.
847 ResultVals.push_back(Chain);
848 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
849 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000850}
851
852
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000853//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000854// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000855//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000856// StdCall calling convention seems to be standard for many Windows' API
857// routines and around. It differs from C calling convention just a little:
858// callee should clean up the stack, not caller. Symbols should be also
859// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000860// For info on fast calling convention see Fast Calling Convention (tail call)
861// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000862
Evan Cheng85e38002006-04-27 05:35:28 +0000863/// AddLiveIn - This helper function adds the specified physical register to the
864/// MachineFunction as a live in value. It also creates a corresponding virtual
865/// register for it.
866static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000867 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000868 assert(RC->contains(PReg) && "Not the correct regclass!");
869 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
870 MF.addLiveIn(PReg, VReg);
871 return VReg;
872}
873
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000874// align stack arguments according to platform alignment needed for tail calls
875unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
876
Rafael Espindola7effac52007-09-14 15:48:13 +0000877SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
878 const CCValAssign &VA,
879 MachineFrameInfo *MFI,
880 SDOperand Root, unsigned i) {
881 // Create the nodes corresponding to a load from this parameter slot.
882 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
883 VA.getLocMemOffset());
884 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
885
886 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
887
888 if (Flags & ISD::ParamFlags::ByVal)
889 return FIN;
890 else
891 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
892}
893
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000894SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
895 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000896 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000897 MachineFunction &MF = DAG.getMachineFunction();
898 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000899 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000900 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000901 unsigned CC = MF.getFunction()->getCallingConv();
Chris Lattner638402b2007-02-28 07:00:42 +0000902 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000903 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000904 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +0000905 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000906 // Check for possible tail call calling convention.
907 if (CC == CallingConv::Fast && PerformTailCallOpt)
908 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
909 else
910 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
911
Chris Lattnerf39f7712007-02-28 05:46:49 +0000912 SmallVector<SDOperand, 8> ArgValues;
913 unsigned LastVal = ~0U;
914 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
915 CCValAssign &VA = ArgLocs[i];
916 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
917 // places.
918 assert(VA.getValNo() != LastVal &&
919 "Don't support value assigned to multiple locs yet");
920 LastVal = VA.getValNo();
921
922 if (VA.isRegLoc()) {
923 MVT::ValueType RegVT = VA.getLocVT();
924 TargetRegisterClass *RC;
925 if (RegVT == MVT::i32)
926 RC = X86::GR32RegisterClass;
927 else {
928 assert(MVT::isVector(RegVT));
929 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000930 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000931
Chris Lattner82932a52007-03-02 05:12:29 +0000932 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
933 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000934
935 // If this is an 8 or 16-bit value, it is really passed promoted to 32
936 // bits. Insert an assert[sz]ext to capture this, then truncate to the
937 // right size.
938 if (VA.getLocInfo() == CCValAssign::SExt)
939 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
940 DAG.getValueType(VA.getValVT()));
941 else if (VA.getLocInfo() == CCValAssign::ZExt)
942 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
943 DAG.getValueType(VA.getValVT()));
944
945 if (VA.getLocInfo() != CCValAssign::Full)
946 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
947
948 ArgValues.push_back(ArgValue);
949 } else {
950 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +0000951 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +0000952 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000953 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000954
955 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000956 // align stack specially for tail calls
957 if (CC==CallingConv::Fast)
958 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +0000959
Evan Cheng25caf632006-05-23 21:06:34 +0000960 ArgValues.push_back(Root);
961
Evan Cheng1bc78042006-04-26 01:20:17 +0000962 // If the function takes variable number of arguments, make a frame index for
963 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000964 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000965 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000966
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000967 // Tail call calling convention (CallingConv::Fast) does not support varargs.
968 assert( !(isVarArg && CC == CallingConv::Fast) &&
969 "CallingConv::Fast does not support varargs.");
970
971 if (isStdCall && !isVarArg &&
972 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000973 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000974 BytesCallerReserves = 0;
975 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000976 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000977
978 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000979 if (NumArgs &&
980 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000981 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000982 BytesToPopOnReturn = 4;
983
984 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000985 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000986
Evan Cheng25ab6902006-09-08 06:48:29 +0000987 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng25caf632006-05-23 21:06:34 +0000988
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000989 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
990 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000991
Evan Cheng25caf632006-05-23 21:06:34 +0000992 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000993 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000994 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000995}
996
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000997SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000998 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000999 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001000 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001001 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001002 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001003
Chris Lattner638402b2007-02-28 07:00:42 +00001004 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001005 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001006 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001007 if(CC==CallingConv::Fast && PerformTailCallOpt)
1008 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1009 else
1010 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001011
Chris Lattner423c5f42007-02-28 05:31:48 +00001012 // Get a count of how many bytes are to be pushed on the stack.
1013 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001014 if (CC==CallingConv::Fast)
1015 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001016
Evan Cheng32fe1032006-05-25 00:59:30 +00001017 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001018
Chris Lattner5a88b832007-02-25 07:10:00 +00001019 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1020 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001021
Chris Lattner423c5f42007-02-28 05:31:48 +00001022 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001023
1024 // Walk the register/memloc assignments, inserting copies/loads.
1025 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1026 CCValAssign &VA = ArgLocs[i];
1027 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001028
Chris Lattner423c5f42007-02-28 05:31:48 +00001029 // Promote the value if needed.
1030 switch (VA.getLocInfo()) {
1031 default: assert(0 && "Unknown loc info!");
1032 case CCValAssign::Full: break;
1033 case CCValAssign::SExt:
1034 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1035 break;
1036 case CCValAssign::ZExt:
1037 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1038 break;
1039 case CCValAssign::AExt:
1040 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1041 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001042 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001043
1044 if (VA.isRegLoc()) {
1045 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1046 } else {
1047 assert(VA.isMemLoc());
1048 if (StackPtr.Val == 0)
1049 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001050
1051 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1052 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001053 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001054 }
1055
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001056 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001057 bool isSRet = NumOps &&
1058 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001059 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001060
Evan Cheng32fe1032006-05-25 00:59:30 +00001061 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001062 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1063 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001064
Evan Cheng347d5f72006-04-28 21:29:37 +00001065 // Build a sequence of copy-to-reg nodes chained together with token chain
1066 // and flag operands which copy the outgoing args into registers.
1067 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001068 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1069 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1070 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001071 InFlag = Chain.getValue(1);
1072 }
1073
Evan Chengf4684712007-02-21 21:18:14 +00001074 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1075 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +00001076 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1077 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001078 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1079 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1080 InFlag);
1081 InFlag = Chain.getValue(1);
1082 }
1083
Evan Cheng32fe1032006-05-25 00:59:30 +00001084 // If the callee is a GlobalAddress node (quite common, every direct call is)
1085 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001086 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001087 // We should use extra load for direct calls to dllimported functions in
1088 // non-JIT mode.
1089 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1090 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001091 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1092 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001093 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1094
Chris Lattnerd96d0722007-02-25 06:40:16 +00001095 // Returns a chain & a flag for retval copy to use.
1096 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001097 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001098 Ops.push_back(Chain);
1099 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001100
1101 // Add argument registers to the end of the list so that they are known live
1102 // into the call.
1103 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001104 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001105 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +00001106
1107 // Add an implicit use GOT pointer in EBX.
1108 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1109 Subtarget->isPICStyleGOT())
1110 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001111
Evan Cheng347d5f72006-04-28 21:29:37 +00001112 if (InFlag.Val)
1113 Ops.push_back(InFlag);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001114
1115 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001116 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001117
Chris Lattner2d297092006-05-23 18:50:38 +00001118 // Create the CALLSEQ_END node.
1119 unsigned NumBytesForCalleeToPush = 0;
1120
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001121 if (CC == CallingConv::X86_StdCall ||
1122 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Chris Lattner09c75a42007-02-25 09:06:15 +00001123 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001124 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +00001125 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001126 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001127 assert(!(isVarArg && CC==CallingConv::Fast) &&
1128 "CallingConv::Fast does not support varargs.");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001129 } else {
1130 // If this is is a call to a struct-return function, the callee
1131 // pops the hidden struct pointer, so we have to push it back.
1132 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001133 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001134 }
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001135
1136 Chain = DAG.getCALLSEQ_END(Chain,
1137 DAG.getConstant(NumBytes, getPointerTy()),
1138 DAG.getConstant(NumBytesForCalleeToPush,
1139 getPointerTy()),
1140 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001141 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001142
Chris Lattner3085e152007-02-25 08:59:22 +00001143 // Handle result values, copying them out of physregs into vregs that we
1144 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +00001145 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001146}
1147
Evan Cheng25ab6902006-09-08 06:48:29 +00001148
1149//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001150// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001151//===----------------------------------------------------------------------===//
1152//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001153// The X86 'fastcall' calling convention passes up to two integer arguments in
1154// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1155// and requires that the callee pop its arguments off the stack (allowing proper
1156// tail calls), and has the same return value conventions as C calling convs.
1157//
1158// This calling convention always arranges for the callee pop value to be 8n+4
1159// bytes, which is needed for tail recursion elimination and stack alignment
1160// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +00001161SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +00001162X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001163 MachineFunction &MF = DAG.getMachineFunction();
1164 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001165 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +00001166 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001167
Chris Lattner638402b2007-02-28 07:00:42 +00001168 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001169 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001170 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1171 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001172 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001173
1174 SmallVector<SDOperand, 8> ArgValues;
1175 unsigned LastVal = ~0U;
1176 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1177 CCValAssign &VA = ArgLocs[i];
1178 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1179 // places.
1180 assert(VA.getValNo() != LastVal &&
1181 "Don't support value assigned to multiple locs yet");
1182 LastVal = VA.getValNo();
1183
1184 if (VA.isRegLoc()) {
1185 MVT::ValueType RegVT = VA.getLocVT();
1186 TargetRegisterClass *RC;
1187 if (RegVT == MVT::i32)
1188 RC = X86::GR32RegisterClass;
1189 else {
1190 assert(MVT::isVector(RegVT));
1191 RC = X86::VR128RegisterClass;
1192 }
1193
Chris Lattner82932a52007-03-02 05:12:29 +00001194 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1195 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001196
1197 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1198 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1199 // right size.
1200 if (VA.getLocInfo() == CCValAssign::SExt)
1201 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1202 DAG.getValueType(VA.getValVT()));
1203 else if (VA.getLocInfo() == CCValAssign::ZExt)
1204 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1205 DAG.getValueType(VA.getValVT()));
1206
1207 if (VA.getLocInfo() != CCValAssign::Full)
1208 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1209
1210 ArgValues.push_back(ArgValue);
1211 } else {
1212 assert(VA.isMemLoc());
Rafael Espindola1242d282007-09-21 14:55:38 +00001213 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfc664c12007-02-28 06:21:19 +00001214 }
1215 }
1216
Evan Cheng25caf632006-05-23 21:06:34 +00001217 ArgValues.push_back(Root);
1218
Chris Lattnerfc664c12007-02-28 06:21:19 +00001219 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001220
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001221 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001222 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001223 // arguments and the arguments after the retaddr has been pushed are
1224 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001225 if ((StackSize & 7) == 0)
1226 StackSize += 4;
1227 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001228
1229 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001230 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001231 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001232 BytesCallerReserves = 0;
1233
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001234 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1235 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001236
Evan Cheng25caf632006-05-23 21:06:34 +00001237 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001238 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001239 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001240}
1241
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001242SDOperand
1243X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1244 const SDOperand &StackPtr,
1245 const CCValAssign &VA,
1246 SDOperand Chain,
1247 SDOperand Arg) {
1248 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1249 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1250 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1251 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1252 if (Flags & ISD::ParamFlags::ByVal) {
1253 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1254 ISD::ParamFlags::ByValAlignOffs);
1255
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001256 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1257 ISD::ParamFlags::ByValSizeOffs;
1258
1259 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1260 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Chris Lattner27a6c732007-11-24 07:07:01 +00001261 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001262
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001263 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1264 AlwaysInline);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001265 } else {
1266 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1267 }
1268}
1269
Chris Lattnere87e1152006-09-26 03:57:53 +00001270SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001271 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001272 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001273 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001274 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001275 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001276
Chris Lattner638402b2007-02-28 07:00:42 +00001277 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001278 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001279 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001280 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001281
1282 // Get a count of how many bytes are to be pushed on the stack.
1283 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001284
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001285 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001286 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001287 // arguments and the arguments after the retaddr has been pushed are
1288 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001289 if ((NumBytes & 7) == 0)
1290 NumBytes += 4;
1291 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001292
Chris Lattner94dd2922006-02-13 09:00:43 +00001293 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001294
Chris Lattner5a88b832007-02-25 07:10:00 +00001295 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1296 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001297
1298 SDOperand StackPtr;
1299
1300 // Walk the register/memloc assignments, inserting copies/loads.
1301 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1302 CCValAssign &VA = ArgLocs[i];
1303 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1304
1305 // Promote the value if needed.
1306 switch (VA.getLocInfo()) {
1307 default: assert(0 && "Unknown loc info!");
1308 case CCValAssign::Full: break;
1309 case CCValAssign::SExt:
1310 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001311 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001312 case CCValAssign::ZExt:
1313 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1314 break;
1315 case CCValAssign::AExt:
1316 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1317 break;
1318 }
1319
1320 if (VA.isRegLoc()) {
1321 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1322 } else {
1323 assert(VA.isMemLoc());
1324 if (StackPtr.Val == 0)
1325 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001326
1327 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1328 Arg));
Evan Cheng32fe1032006-05-25 00:59:30 +00001329 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001330 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001331
Evan Cheng32fe1032006-05-25 00:59:30 +00001332 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001333 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1334 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001335
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001336 // Build a sequence of copy-to-reg nodes chained together with token chain
1337 // and flag operands which copy the outgoing args into registers.
1338 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001339 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1340 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1341 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001342 InFlag = Chain.getValue(1);
1343 }
1344
Evan Cheng32fe1032006-05-25 00:59:30 +00001345 // If the callee is a GlobalAddress node (quite common, every direct call is)
1346 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001347 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001348 // We should use extra load for direct calls to dllimported functions in
1349 // non-JIT mode.
1350 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1351 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001352 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1353 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001354 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1355
Evan Chengf4684712007-02-21 21:18:14 +00001356 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1357 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001358 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1359 Subtarget->isPICStyleGOT()) {
1360 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1361 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1362 InFlag);
1363 InFlag = Chain.getValue(1);
1364 }
1365
Chris Lattnerd96d0722007-02-25 06:40:16 +00001366 // Returns a chain & a flag for retval copy to use.
1367 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001368 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001369 Ops.push_back(Chain);
1370 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001371
1372 // Add argument registers to the end of the list so that they are known live
1373 // into the call.
1374 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001375 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001376 RegsToPass[i].second.getValueType()));
1377
Evan Chengf4684712007-02-21 21:18:14 +00001378 // Add an implicit use GOT pointer in EBX.
1379 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1380 Subtarget->isPICStyleGOT())
1381 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1382
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001383 if (InFlag.Val)
1384 Ops.push_back(InFlag);
1385
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001386 assert(isTailCall==false && "no tail call here");
1387 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001388 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001389 InFlag = Chain.getValue(1);
1390
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001391 // Returns a flag for retval copy to use.
1392 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001393 Ops.clear();
1394 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001395 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1396 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001397 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001398 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001399 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001400
Chris Lattner339b4392007-02-25 09:10:05 +00001401 // Handle result values, copying them out of physregs into vregs that we
1402 // return.
1403 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001404}
1405
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001406//===----------------------------------------------------------------------===//
1407// Fast Calling Convention (tail call) implementation
1408//===----------------------------------------------------------------------===//
1409
1410// Like std call, callee cleans arguments, convention except that ECX is
1411// reserved for storing the tail called function address. Only 2 registers are
1412// free for argument passing (inreg). Tail call optimization is performed
1413// provided:
1414// * tailcallopt is enabled
1415// * caller/callee are fastcc
1416// * elf/pic is disabled OR
1417// * elf/pic enabled + callee is in module + callee has
1418// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001419// To keep the stack aligned according to platform abi the function
1420// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1421// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001422// If a tail called function callee has more arguments than the caller the
1423// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001424// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001425// original REtADDR, but before the saved framepointer or the spilled registers
1426// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1427// stack layout:
1428// arg1
1429// arg2
1430// RETADDR
1431// [ new RETADDR
1432// move area ]
1433// (possible EBP)
1434// ESI
1435// EDI
1436// local1 ..
1437
1438/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1439/// for a 16 byte align requirement.
1440unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1441 SelectionDAG& DAG) {
1442 if (PerformTailCallOpt) {
1443 MachineFunction &MF = DAG.getMachineFunction();
1444 const TargetMachine &TM = MF.getTarget();
1445 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1446 unsigned StackAlignment = TFI.getStackAlignment();
1447 uint64_t AlignMask = StackAlignment - 1;
1448 int64_t Offset = StackSize;
1449 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1450 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1451 // Number smaller than 12 so just add the difference.
1452 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1453 } else {
1454 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1455 Offset = ((~AlignMask) & Offset) + StackAlignment +
1456 (StackAlignment-SlotSize);
1457 }
1458 StackSize = Offset;
1459 }
1460 return StackSize;
1461}
1462
1463/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001464/// following the call is a return. A function is eligible if caller/callee
1465/// calling conventions match, currently only fastcc supports tail calls, and
1466/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001467bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1468 SDOperand Ret,
1469 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001470 if (!PerformTailCallOpt)
1471 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001472
1473 // Check whether CALL node immediatly preceeds the RET node and whether the
1474 // return uses the result of the node or is a void return.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001475 unsigned NumOps = Ret.getNumOperands();
1476 if ((NumOps == 1 &&
1477 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1478 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Chenga9d641e2007-11-02 17:45:40 +00001479 (NumOps > 1 &&
Evan Cheng9df7dc52007-11-02 01:26:22 +00001480 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1481 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001482 MachineFunction &MF = DAG.getMachineFunction();
1483 unsigned CallerCC = MF.getFunction()->getCallingConv();
1484 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1485 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1486 SDOperand Callee = Call.getOperand(4);
1487 // On elf/pic %ebx needs to be livein.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001488 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1489 !Subtarget->isPICStyleGOT())
1490 return true;
1491
1492 // Can only do local tail calls with PIC.
1493 GlobalValue * GV = 0;
1494 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1495 if(G != 0 &&
1496 (GV = G->getGlobal()) &&
1497 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1498 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001499 }
1500 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001501
1502 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001503}
1504
1505SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1506 SelectionDAG &DAG,
1507 unsigned CC) {
1508 SDOperand Chain = Op.getOperand(0);
1509 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1510 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1511 SDOperand Callee = Op.getOperand(4);
1512 bool is64Bit = Subtarget->is64Bit();
1513
1514 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1515
1516 // Analyze operands of the call, assigning locations to each operand.
1517 SmallVector<CCValAssign, 16> ArgLocs;
1518 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1519 if (is64Bit)
1520 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1521 else
1522 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1523
1524
1525 // Lower arguments at fp - stackoffset + fpdiff.
1526 MachineFunction &MF = DAG.getMachineFunction();
1527
1528 unsigned NumBytesToBePushed =
1529 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1530
1531 unsigned NumBytesCallerPushed =
1532 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1533 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1534
1535 // Set the delta of movement of the returnaddr stackslot.
1536 // But only set if delta is greater than previous delta.
1537 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1538 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1539
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001540 Chain = DAG.
1541 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1542
1543 // Adjust the Return address stack slot.
1544 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001545 if (FPDiff) {
1546 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001547 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1548 // Load the "old" Return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001549 RetAddrFrIdx =
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001550 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1551 // Calculate the new stack slot for the return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001552 int SlotSize = is64Bit ? 8 : 4;
1553 int NewReturnAddrFI =
1554 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001555 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1556 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001557 }
1558
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001559 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1560 SmallVector<SDOperand, 8> MemOpChains;
1561 SmallVector<SDOperand, 8> MemOpChains2;
1562 SDOperand FramePtr, StackPtr;
1563 SDOperand PtrOff;
1564 SDOperand FIN;
1565 int FI = 0;
1566
1567 // Walk the register/memloc assignments, inserting copies/loads. Lower
1568 // arguments first to the stack slot where they would normally - in case of a
1569 // normal function call - be.
1570 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1571 CCValAssign &VA = ArgLocs[i];
1572 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1573
1574 // Promote the value if needed.
1575 switch (VA.getLocInfo()) {
1576 default: assert(0 && "Unknown loc info!");
1577 case CCValAssign::Full: break;
1578 case CCValAssign::SExt:
1579 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1580 break;
1581 case CCValAssign::ZExt:
1582 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1583 break;
1584 case CCValAssign::AExt:
1585 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1586 break;
1587 }
1588
1589 if (VA.isRegLoc()) {
1590 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1591 } else {
1592 assert(VA.isMemLoc());
1593 if (StackPtr.Val == 0)
1594 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1595
1596 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1597 Arg));
1598 }
1599 }
1600
1601 if (!MemOpChains.empty())
1602 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1603 &MemOpChains[0], MemOpChains.size());
1604
1605 // Build a sequence of copy-to-reg nodes chained together with token chain
1606 // and flag operands which copy the outgoing args into registers.
1607 SDOperand InFlag;
1608 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1609 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1610 InFlag);
1611 InFlag = Chain.getValue(1);
1612 }
1613 InFlag = SDOperand();
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001614
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001615 // Copy from stack slots to stack slot of a tail called function. This needs
1616 // to be done because if we would lower the arguments directly to their real
1617 // stack slot we might end up overwriting each other.
1618 // TODO: To make this more efficient (sometimes saving a store/load) we could
1619 // analyse the arguments and emit this store/load/store sequence only for
1620 // arguments which would be overwritten otherwise.
1621 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1622 CCValAssign &VA = ArgLocs[i];
1623 if (!VA.isRegLoc()) {
1624 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1625 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1626
1627 // Get source stack slot.
1628 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1629 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1630 // Create frame index.
1631 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1632 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1633 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1634 FIN = DAG.getFrameIndex(FI, MVT::i32);
1635 if (Flags & ISD::ParamFlags::ByVal) {
1636 // Copy relative to framepointer.
1637 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1638 ISD::ParamFlags::ByValAlignOffs);
1639
1640 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1641 ISD::ParamFlags::ByValSizeOffs;
1642
1643 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1644 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer38ada862007-11-10 10:48:01 +00001645 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1646
1647 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1648 AlignNode,AlwaysInline));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649 } else {
1650 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1651 // Store relative to framepointer.
1652 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1653 }
1654 }
1655 }
1656
1657 if (!MemOpChains2.empty())
1658 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1659 &MemOpChains2[0], MemOpChains.size());
1660
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001661 // Store the return address to the appropriate stack slot.
1662 if (FPDiff)
1663 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1664
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001665 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1666 // GOT pointer.
1667 // Does not work with tail call since ebx is not restored correctly by
1668 // tailcaller. TODO: at least for x86 - verify for x86-64
1669
1670 // If the callee is a GlobalAddress node (quite common, every direct call is)
1671 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1672 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1673 // We should use extra load for direct calls to dllimported functions in
1674 // non-JIT mode.
1675 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1676 getTargetMachine(), true))
1677 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1678 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1679 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1680 else {
1681 assert(Callee.getOpcode() == ISD::LOAD &&
1682 "Function destination must be loaded into virtual register");
1683 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1684
1685 Chain = DAG.getCopyToReg(Chain,
1686 DAG.getRegister(Opc, getPointerTy()) ,
1687 Callee,InFlag);
1688 Callee = DAG.getRegister(Opc, getPointerTy());
1689 // Add register as live out.
1690 DAG.getMachineFunction().addLiveOut(Opc);
1691 }
1692
1693 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1694 SmallVector<SDOperand, 8> Ops;
1695
1696 Ops.push_back(Chain);
1697 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1698 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1699 if (InFlag.Val)
1700 Ops.push_back(InFlag);
1701 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1702 InFlag = Chain.getValue(1);
1703
1704 // Returns a chain & a flag for retval copy to use.
1705 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1706 Ops.clear();
1707 Ops.push_back(Chain);
1708 Ops.push_back(Callee);
1709 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1710 // Add argument registers to the end of the list so that they are known live
1711 // into the call.
1712 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1713 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1714 RegsToPass[i].second.getValueType()));
1715 if (InFlag.Val)
1716 Ops.push_back(InFlag);
1717 assert(InFlag.Val &&
1718 "Flag must be set. Depend on flag being set in LowerRET");
1719 Chain = DAG.getNode(X86ISD::TAILCALL,
1720 Op.Val->getVTList(), &Ops[0], Ops.size());
1721
1722 return SDOperand(Chain.Val, Op.ResNo);
1723}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001724
1725//===----------------------------------------------------------------------===//
1726// X86-64 C Calling Convention implementation
1727//===----------------------------------------------------------------------===//
1728
1729SDOperand
1730X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001731 MachineFunction &MF = DAG.getMachineFunction();
1732 MachineFrameInfo *MFI = MF.getFrameInfo();
1733 SDOperand Root = Op.getOperand(0);
1734 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001735 unsigned CC= MF.getFunction()->getCallingConv();
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001736
1737 static const unsigned GPR64ArgRegs[] = {
1738 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1739 };
1740 static const unsigned XMMArgRegs[] = {
1741 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1742 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1743 };
1744
Chris Lattner638402b2007-02-28 07:00:42 +00001745
1746 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001747 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001748 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +00001749 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001750 if (CC == CallingConv::Fast && PerformTailCallOpt)
1751 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1752 else
1753 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001754
1755 SmallVector<SDOperand, 8> ArgValues;
1756 unsigned LastVal = ~0U;
1757 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1758 CCValAssign &VA = ArgLocs[i];
1759 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1760 // places.
1761 assert(VA.getValNo() != LastVal &&
1762 "Don't support value assigned to multiple locs yet");
1763 LastVal = VA.getValNo();
1764
1765 if (VA.isRegLoc()) {
1766 MVT::ValueType RegVT = VA.getLocVT();
1767 TargetRegisterClass *RC;
1768 if (RegVT == MVT::i32)
1769 RC = X86::GR32RegisterClass;
1770 else if (RegVT == MVT::i64)
1771 RC = X86::GR64RegisterClass;
1772 else if (RegVT == MVT::f32)
1773 RC = X86::FR32RegisterClass;
1774 else if (RegVT == MVT::f64)
1775 RC = X86::FR64RegisterClass;
1776 else {
1777 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001778 if (MVT::getSizeInBits(RegVT) == 64) {
1779 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1780 RegVT = MVT::i64;
1781 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001782 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001783 }
Chris Lattner82932a52007-03-02 05:12:29 +00001784
1785 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1786 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001787
1788 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1789 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1790 // right size.
1791 if (VA.getLocInfo() == CCValAssign::SExt)
1792 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1793 DAG.getValueType(VA.getValVT()));
1794 else if (VA.getLocInfo() == CCValAssign::ZExt)
1795 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1796 DAG.getValueType(VA.getValVT()));
1797
1798 if (VA.getLocInfo() != CCValAssign::Full)
1799 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1800
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001801 // Handle MMX values passed in GPRs.
1802 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1803 MVT::getSizeInBits(RegVT) == 64)
1804 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1805
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001806 ArgValues.push_back(ArgValue);
1807 } else {
1808 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001809 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001810 }
1811 }
1812
1813 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001814 if (CC==CallingConv::Fast)
1815 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001816
1817 // If the function takes variable number of arguments, make a frame index for
1818 // the start of the first vararg value... for expansion of llvm.va_start.
1819 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001820 assert(CC!=CallingConv::Fast
1821 && "Var arg not supported with calling convention fastcc");
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001822 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1823 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1824
1825 // For X86-64, if there are vararg parameters that are passed via
1826 // registers, then we must store them to their spots on the stack so they
1827 // may be loaded by deferencing the result of va_next.
1828 VarArgsGPOffset = NumIntRegs * 8;
1829 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1830 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1831 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1832
1833 // Store the integer parameter registers.
1834 SmallVector<SDOperand, 8> MemOps;
1835 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1836 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1837 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1838 for (; NumIntRegs != 6; ++NumIntRegs) {
1839 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1840 X86::GR64RegisterClass);
1841 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1842 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1843 MemOps.push_back(Store);
1844 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1845 DAG.getConstant(8, getPointerTy()));
1846 }
1847
1848 // Now store the XMM (fp + vector) parameter registers.
1849 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1850 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1851 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1852 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1853 X86::VR128RegisterClass);
1854 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1855 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1856 MemOps.push_back(Store);
1857 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1858 DAG.getConstant(16, getPointerTy()));
1859 }
1860 if (!MemOps.empty())
1861 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1862 &MemOps[0], MemOps.size());
1863 }
1864
1865 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001866 // Tail call convention (fastcc) needs callee pop.
Evan Cheng36446012007-10-14 10:09:39 +00001867 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001868 BytesToPopOnReturn = StackSize; // Callee pops everything.
1869 BytesCallerReserves = 0;
1870 } else {
1871 BytesToPopOnReturn = 0; // Callee pops nothing.
1872 BytesCallerReserves = StackSize;
1873 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001874 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1875 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1876
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001877 // Return the new list of results.
1878 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1879 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1880}
1881
1882SDOperand
1883X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1884 unsigned CC) {
1885 SDOperand Chain = Op.getOperand(0);
1886 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001887 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001888
1889 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001890 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001891 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng36446012007-10-14 10:09:39 +00001892 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001893 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1894 else
1895 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001896
1897 // Get a count of how many bytes are to be pushed on the stack.
1898 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001899 if (CC == CallingConv::Fast)
1900 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1901
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001902 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1903
1904 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1905 SmallVector<SDOperand, 8> MemOpChains;
1906
1907 SDOperand StackPtr;
1908
1909 // Walk the register/memloc assignments, inserting copies/loads.
1910 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1911 CCValAssign &VA = ArgLocs[i];
1912 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1913
1914 // Promote the value if needed.
1915 switch (VA.getLocInfo()) {
1916 default: assert(0 && "Unknown loc info!");
1917 case CCValAssign::Full: break;
1918 case CCValAssign::SExt:
1919 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1920 break;
1921 case CCValAssign::ZExt:
1922 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1923 break;
1924 case CCValAssign::AExt:
1925 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1926 break;
1927 }
1928
1929 if (VA.isRegLoc()) {
1930 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1931 } else {
1932 assert(VA.isMemLoc());
1933 if (StackPtr.Val == 0)
1934 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola21485be2007-08-20 15:18:24 +00001935
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001936 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1937 Arg));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001938 }
1939 }
1940
1941 if (!MemOpChains.empty())
1942 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1943 &MemOpChains[0], MemOpChains.size());
1944
1945 // Build a sequence of copy-to-reg nodes chained together with token chain
1946 // and flag operands which copy the outgoing args into registers.
1947 SDOperand InFlag;
1948 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1949 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1950 InFlag);
1951 InFlag = Chain.getValue(1);
1952 }
1953
1954 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001955 assert ( CallingConv::Fast != CC &&
1956 "Var args not supported with calling convention fastcc");
1957
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001958 // From AMD64 ABI document:
1959 // For calls that may call functions that use varargs or stdargs
1960 // (prototype-less calls or calls to functions containing ellipsis (...) in
1961 // the declaration) %al is used as hidden argument to specify the number
1962 // of SSE registers used. The contents of %al do not need to match exactly
1963 // the number of registers, but must be an ubound on the number of SSE
1964 // registers used and is in the range 0 - 8 inclusive.
1965
1966 // Count the number of XMM registers allocated.
1967 static const unsigned XMMArgRegs[] = {
1968 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1969 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1970 };
1971 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1972
1973 Chain = DAG.getCopyToReg(Chain, X86::AL,
1974 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1975 InFlag = Chain.getValue(1);
1976 }
1977
1978 // If the callee is a GlobalAddress node (quite common, every direct call is)
1979 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1980 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1981 // We should use extra load for direct calls to dllimported functions in
1982 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001983 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001984 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1985 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001986 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1987 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001988 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1989 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001990
1991 // Returns a chain & a flag for retval copy to use.
1992 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1993 SmallVector<SDOperand, 8> Ops;
1994 Ops.push_back(Chain);
1995 Ops.push_back(Callee);
1996
1997 // Add argument registers to the end of the list so that they are known live
1998 // into the call.
1999 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2000 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2001 RegsToPass[i].second.getValueType()));
2002
2003 if (InFlag.Val)
2004 Ops.push_back(InFlag);
2005
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002006 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002007 NodeTys, &Ops[0], Ops.size());
2008 InFlag = Chain.getValue(1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002009 int NumBytesForCalleeToPush = 0;
Evan Cheng36446012007-10-14 10:09:39 +00002010 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002011 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002012 } else {
2013 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2014 }
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002015 // Returns a flag for retval copy to use.
2016 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2017 Ops.clear();
2018 Ops.push_back(Chain);
2019 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002020 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002021 Ops.push_back(InFlag);
2022 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2023 InFlag = Chain.getValue(1);
2024
2025 // Handle result values, copying them out of physregs into vregs that we
2026 // return.
2027 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2028}
2029
2030
2031//===----------------------------------------------------------------------===//
2032// Other Lowering Hooks
2033//===----------------------------------------------------------------------===//
2034
2035
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002036SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002037 MachineFunction &MF = DAG.getMachineFunction();
2038 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2039 int ReturnAddrIndex = FuncInfo->getRAIndex();
2040
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002041 if (ReturnAddrIndex == 0) {
2042 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00002043 if (Subtarget->is64Bit())
2044 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2045 else
2046 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002047
2048 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002049 }
2050
Evan Cheng25ab6902006-09-08 06:48:29 +00002051 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002052}
2053
2054
2055
Evan Cheng6dfa9992006-01-30 23:41:35 +00002056/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2057/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00002058/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2059/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00002060static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00002061 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2062 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00002063 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002064 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002065 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2066 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2067 // X > -1 -> X == 0, jump !sign.
2068 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00002069 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002070 return true;
2071 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2072 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00002073 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002074 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00002075 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2076 // X < 1 -> X <= 0
2077 RHS = DAG.getConstant(0, RHS.getValueType());
2078 X86CC = X86::COND_LE;
2079 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002080 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002081 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002082
Evan Chengd9558e02006-01-06 00:43:03 +00002083 switch (SetCCOpcode) {
2084 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002085 case ISD::SETEQ: X86CC = X86::COND_E; break;
2086 case ISD::SETGT: X86CC = X86::COND_G; break;
2087 case ISD::SETGE: X86CC = X86::COND_GE; break;
2088 case ISD::SETLT: X86CC = X86::COND_L; break;
2089 case ISD::SETLE: X86CC = X86::COND_LE; break;
2090 case ISD::SETNE: X86CC = X86::COND_NE; break;
2091 case ISD::SETULT: X86CC = X86::COND_B; break;
2092 case ISD::SETUGT: X86CC = X86::COND_A; break;
2093 case ISD::SETULE: X86CC = X86::COND_BE; break;
2094 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002095 }
2096 } else {
2097 // On a floating point condition, the flags are set as follows:
2098 // ZF PF CF op
2099 // 0 | 0 | 0 | X > Y
2100 // 0 | 0 | 1 | X < Y
2101 // 1 | 0 | 0 | X == Y
2102 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00002103 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00002104 switch (SetCCOpcode) {
2105 default: break;
2106 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002107 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002108 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002109 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002110 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002111 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002112 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002113 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002114 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002115 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002116 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002117 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002118 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002119 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002120 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002121 case ISD::SETNE: X86CC = X86::COND_NE; break;
2122 case ISD::SETUO: X86CC = X86::COND_P; break;
2123 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002124 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002125 if (Flip)
2126 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00002127 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00002128
Chris Lattner7fbe9722006-10-20 17:42:20 +00002129 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002130}
2131
Evan Cheng4a460802006-01-11 00:33:36 +00002132/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2133/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002134/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002135static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002136 switch (X86CC) {
2137 default:
2138 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002139 case X86::COND_B:
2140 case X86::COND_BE:
2141 case X86::COND_E:
2142 case X86::COND_P:
2143 case X86::COND_A:
2144 case X86::COND_AE:
2145 case X86::COND_NE:
2146 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002147 return true;
2148 }
2149}
2150
Evan Cheng5ced1d82006-04-06 23:23:56 +00002151/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002152/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00002153static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2154 if (Op.getOpcode() == ISD::UNDEF)
2155 return true;
2156
2157 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002158 return (Val >= Low && Val < Hi);
2159}
2160
2161/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2162/// true if Op is undef or if its value equal to the specified value.
2163static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2164 if (Op.getOpcode() == ISD::UNDEF)
2165 return true;
2166 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002167}
2168
Evan Cheng0188ecb2006-03-22 18:59:22 +00002169/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2170/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2171bool X86::isPSHUFDMask(SDNode *N) {
2172 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2173
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002174 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002175 return false;
2176
2177 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002178 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002179 SDOperand Arg = N->getOperand(i);
2180 if (Arg.getOpcode() == ISD::UNDEF) continue;
2181 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002182 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002183 return false;
2184 }
2185
2186 return true;
2187}
2188
2189/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002190/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002191bool X86::isPSHUFHWMask(SDNode *N) {
2192 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2193
2194 if (N->getNumOperands() != 8)
2195 return false;
2196
2197 // Lower quadword copied in order.
2198 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002199 SDOperand Arg = N->getOperand(i);
2200 if (Arg.getOpcode() == ISD::UNDEF) continue;
2201 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2202 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002203 return false;
2204 }
2205
2206 // Upper quadword shuffled.
2207 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002208 SDOperand Arg = N->getOperand(i);
2209 if (Arg.getOpcode() == ISD::UNDEF) continue;
2210 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2211 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002212 if (Val < 4 || Val > 7)
2213 return false;
2214 }
2215
2216 return true;
2217}
2218
2219/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002220/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002221bool X86::isPSHUFLWMask(SDNode *N) {
2222 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2223
2224 if (N->getNumOperands() != 8)
2225 return false;
2226
2227 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002228 for (unsigned i = 4; i != 8; ++i)
2229 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002230 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002231
2232 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002233 for (unsigned i = 0; i != 4; ++i)
2234 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002235 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002236
2237 return true;
2238}
2239
Evan Cheng14aed5e2006-03-24 01:18:28 +00002240/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2241/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00002242static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002243 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002244
Evan Cheng39623da2006-04-20 08:58:49 +00002245 unsigned Half = NumElems / 2;
2246 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002247 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002248 return false;
2249 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002250 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002251 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002252
2253 return true;
2254}
2255
Evan Cheng39623da2006-04-20 08:58:49 +00002256bool X86::isSHUFPMask(SDNode *N) {
2257 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002258 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002259}
2260
Evan Cheng213d2cf2007-05-17 18:45:50 +00002261/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002262/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2263/// half elements to come from vector 1 (which would equal the dest.) and
2264/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002265static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2266 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002267
Chris Lattner5a88b832007-02-25 07:10:00 +00002268 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002269 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002270 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002271 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002272 for (unsigned i = Half; i < NumOps; ++i)
2273 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002274 return false;
2275 return true;
2276}
2277
2278static bool isCommutedSHUFP(SDNode *N) {
2279 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002280 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002281}
2282
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002283/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2284/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2285bool X86::isMOVHLPSMask(SDNode *N) {
2286 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2287
Evan Cheng2064a2b2006-03-28 06:50:32 +00002288 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002289 return false;
2290
Evan Cheng2064a2b2006-03-28 06:50:32 +00002291 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002292 return isUndefOrEqual(N->getOperand(0), 6) &&
2293 isUndefOrEqual(N->getOperand(1), 7) &&
2294 isUndefOrEqual(N->getOperand(2), 2) &&
2295 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002296}
2297
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002298/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2299/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2300/// <2, 3, 2, 3>
2301bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2302 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2303
2304 if (N->getNumOperands() != 4)
2305 return false;
2306
2307 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2308 return isUndefOrEqual(N->getOperand(0), 2) &&
2309 isUndefOrEqual(N->getOperand(1), 3) &&
2310 isUndefOrEqual(N->getOperand(2), 2) &&
2311 isUndefOrEqual(N->getOperand(3), 3);
2312}
2313
Evan Cheng5ced1d82006-04-06 23:23:56 +00002314/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2315/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2316bool X86::isMOVLPMask(SDNode *N) {
2317 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2318
2319 unsigned NumElems = N->getNumOperands();
2320 if (NumElems != 2 && NumElems != 4)
2321 return false;
2322
Evan Chengc5cdff22006-04-07 21:53:05 +00002323 for (unsigned i = 0; i < NumElems/2; ++i)
2324 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2325 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002326
Evan Chengc5cdff22006-04-07 21:53:05 +00002327 for (unsigned i = NumElems/2; i < NumElems; ++i)
2328 if (!isUndefOrEqual(N->getOperand(i), i))
2329 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002330
2331 return true;
2332}
2333
2334/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002335/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2336/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002337bool X86::isMOVHPMask(SDNode *N) {
2338 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2339
2340 unsigned NumElems = N->getNumOperands();
2341 if (NumElems != 2 && NumElems != 4)
2342 return false;
2343
Evan Chengc5cdff22006-04-07 21:53:05 +00002344 for (unsigned i = 0; i < NumElems/2; ++i)
2345 if (!isUndefOrEqual(N->getOperand(i), i))
2346 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002347
2348 for (unsigned i = 0; i < NumElems/2; ++i) {
2349 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002350 if (!isUndefOrEqual(Arg, i + NumElems))
2351 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002352 }
2353
2354 return true;
2355}
2356
Evan Cheng0038e592006-03-28 00:39:58 +00002357/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2358/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002359bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2360 bool V2IsSplat = false) {
2361 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002362 return false;
2363
Chris Lattner5a88b832007-02-25 07:10:00 +00002364 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2365 SDOperand BitI = Elts[i];
2366 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002367 if (!isUndefOrEqual(BitI, j))
2368 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002369 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002370 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002371 return false;
2372 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002373 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002374 return false;
2375 }
Evan Cheng0038e592006-03-28 00:39:58 +00002376 }
2377
2378 return true;
2379}
2380
Evan Cheng39623da2006-04-20 08:58:49 +00002381bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2382 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002383 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002384}
2385
Evan Cheng4fcb9222006-03-28 02:43:26 +00002386/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2387/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002388bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2389 bool V2IsSplat = false) {
2390 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002391 return false;
2392
Chris Lattner5a88b832007-02-25 07:10:00 +00002393 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2394 SDOperand BitI = Elts[i];
2395 SDOperand BitI1 = Elts[i+1];
2396 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002397 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002398 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002399 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002400 return false;
2401 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002402 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002403 return false;
2404 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002405 }
2406
2407 return true;
2408}
2409
Evan Cheng39623da2006-04-20 08:58:49 +00002410bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2411 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002412 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002413}
2414
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002415/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2416/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2417/// <0, 0, 1, 1>
2418bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2419 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2420
2421 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002422 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002423 return false;
2424
2425 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2426 SDOperand BitI = N->getOperand(i);
2427 SDOperand BitI1 = N->getOperand(i+1);
2428
Evan Chengc5cdff22006-04-07 21:53:05 +00002429 if (!isUndefOrEqual(BitI, j))
2430 return false;
2431 if (!isUndefOrEqual(BitI1, j))
2432 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002433 }
2434
2435 return true;
2436}
2437
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002438/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2439/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2440/// <2, 2, 3, 3>
2441bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2442 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2443
2444 unsigned NumElems = N->getNumOperands();
2445 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2446 return false;
2447
2448 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2449 SDOperand BitI = N->getOperand(i);
2450 SDOperand BitI1 = N->getOperand(i + 1);
2451
2452 if (!isUndefOrEqual(BitI, j))
2453 return false;
2454 if (!isUndefOrEqual(BitI1, j))
2455 return false;
2456 }
2457
2458 return true;
2459}
2460
Evan Cheng017dcc62006-04-21 01:05:10 +00002461/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2462/// specifies a shuffle of elements that is suitable for input to MOVSS,
2463/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002464static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2465 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002466 return false;
2467
Chris Lattner5a88b832007-02-25 07:10:00 +00002468 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002469 return false;
2470
Chris Lattner5a88b832007-02-25 07:10:00 +00002471 for (unsigned i = 1; i < NumElts; ++i) {
2472 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002473 return false;
2474 }
2475
2476 return true;
2477}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002478
Evan Cheng017dcc62006-04-21 01:05:10 +00002479bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002480 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002481 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002482}
2483
Evan Cheng017dcc62006-04-21 01:05:10 +00002484/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2485/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002486/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002487static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2488 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002489 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002490 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002491 return false;
2492
2493 if (!isUndefOrEqual(Ops[0], 0))
2494 return false;
2495
Chris Lattner5a88b832007-02-25 07:10:00 +00002496 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002497 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002498 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2499 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2500 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002501 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002502 }
2503
2504 return true;
2505}
2506
Evan Cheng8cf723d2006-09-08 01:50:06 +00002507static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2508 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002509 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002510 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2511 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002512}
2513
Evan Chengd9539472006-04-14 21:59:03 +00002514/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2515/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2516bool X86::isMOVSHDUPMask(SDNode *N) {
2517 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2518
2519 if (N->getNumOperands() != 4)
2520 return false;
2521
2522 // Expect 1, 1, 3, 3
2523 for (unsigned i = 0; i < 2; ++i) {
2524 SDOperand Arg = N->getOperand(i);
2525 if (Arg.getOpcode() == ISD::UNDEF) continue;
2526 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2527 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2528 if (Val != 1) return false;
2529 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002530
2531 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002532 for (unsigned i = 2; i < 4; ++i) {
2533 SDOperand Arg = N->getOperand(i);
2534 if (Arg.getOpcode() == ISD::UNDEF) continue;
2535 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2536 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2537 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002538 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002539 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002540
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002541 // Don't use movshdup if it can be done with a shufps.
2542 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002543}
2544
2545/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2546/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2547bool X86::isMOVSLDUPMask(SDNode *N) {
2548 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2549
2550 if (N->getNumOperands() != 4)
2551 return false;
2552
2553 // Expect 0, 0, 2, 2
2554 for (unsigned i = 0; i < 2; ++i) {
2555 SDOperand Arg = N->getOperand(i);
2556 if (Arg.getOpcode() == ISD::UNDEF) continue;
2557 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2558 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2559 if (Val != 0) return false;
2560 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002561
2562 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002563 for (unsigned i = 2; i < 4; ++i) {
2564 SDOperand Arg = N->getOperand(i);
2565 if (Arg.getOpcode() == ISD::UNDEF) continue;
2566 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2567 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2568 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002569 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002570 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002571
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002572 // Don't use movshdup if it can be done with a shufps.
2573 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002574}
2575
Evan Cheng49892af2007-06-19 00:02:56 +00002576/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2577/// specifies a identity operation on the LHS or RHS.
2578static bool isIdentityMask(SDNode *N, bool RHS = false) {
2579 unsigned NumElems = N->getNumOperands();
2580 for (unsigned i = 0; i < NumElems; ++i)
2581 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2582 return false;
2583 return true;
2584}
2585
Evan Chengb9df0ca2006-03-22 02:53:00 +00002586/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2587/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002588static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002589 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2590
Evan Chengb9df0ca2006-03-22 02:53:00 +00002591 // This is a splat operation if each element of the permute is the same, and
2592 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002593 unsigned NumElems = N->getNumOperands();
2594 SDOperand ElementBase;
2595 unsigned i = 0;
2596 for (; i != NumElems; ++i) {
2597 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002598 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002599 ElementBase = Elt;
2600 break;
2601 }
2602 }
2603
2604 if (!ElementBase.Val)
2605 return false;
2606
2607 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002608 SDOperand Arg = N->getOperand(i);
2609 if (Arg.getOpcode() == ISD::UNDEF) continue;
2610 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002611 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002612 }
2613
2614 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002615 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002616}
2617
Evan Chengc575ca22006-04-17 20:43:08 +00002618/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2619/// a splat of a single element and it's a 2 or 4 element mask.
2620bool X86::isSplatMask(SDNode *N) {
2621 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2622
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002623 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002624 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2625 return false;
2626 return ::isSplatMask(N);
2627}
2628
Evan Chengf686d9b2006-10-27 21:08:32 +00002629/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2630/// specifies a splat of zero element.
2631bool X86::isSplatLoMask(SDNode *N) {
2632 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2633
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002634 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002635 if (!isUndefOrEqual(N->getOperand(i), 0))
2636 return false;
2637 return true;
2638}
2639
Evan Cheng63d33002006-03-22 08:01:21 +00002640/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2641/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2642/// instructions.
2643unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002644 unsigned NumOperands = N->getNumOperands();
2645 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2646 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002647 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002648 unsigned Val = 0;
2649 SDOperand Arg = N->getOperand(NumOperands-i-1);
2650 if (Arg.getOpcode() != ISD::UNDEF)
2651 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002652 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002653 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002654 if (i != NumOperands - 1)
2655 Mask <<= Shift;
2656 }
Evan Cheng63d33002006-03-22 08:01:21 +00002657
2658 return Mask;
2659}
2660
Evan Cheng506d3df2006-03-29 23:07:14 +00002661/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2662/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2663/// instructions.
2664unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2665 unsigned Mask = 0;
2666 // 8 nodes, but we only care about the last 4.
2667 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002668 unsigned Val = 0;
2669 SDOperand Arg = N->getOperand(i);
2670 if (Arg.getOpcode() != ISD::UNDEF)
2671 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002672 Mask |= (Val - 4);
2673 if (i != 4)
2674 Mask <<= 2;
2675 }
2676
2677 return Mask;
2678}
2679
2680/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2681/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2682/// instructions.
2683unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2684 unsigned Mask = 0;
2685 // 8 nodes, but we only care about the first 4.
2686 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002687 unsigned Val = 0;
2688 SDOperand Arg = N->getOperand(i);
2689 if (Arg.getOpcode() != ISD::UNDEF)
2690 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002691 Mask |= Val;
2692 if (i != 0)
2693 Mask <<= 2;
2694 }
2695
2696 return Mask;
2697}
2698
Evan Chengc21a0532006-04-05 01:47:37 +00002699/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2700/// specifies a 8 element shuffle that can be broken into a pair of
2701/// PSHUFHW and PSHUFLW.
2702static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2703 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2704
2705 if (N->getNumOperands() != 8)
2706 return false;
2707
2708 // Lower quadword shuffled.
2709 for (unsigned i = 0; i != 4; ++i) {
2710 SDOperand Arg = N->getOperand(i);
2711 if (Arg.getOpcode() == ISD::UNDEF) continue;
2712 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2713 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2714 if (Val > 4)
2715 return false;
2716 }
2717
2718 // Upper quadword shuffled.
2719 for (unsigned i = 4; i != 8; ++i) {
2720 SDOperand Arg = N->getOperand(i);
2721 if (Arg.getOpcode() == ISD::UNDEF) continue;
2722 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2723 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2724 if (Val < 4 || Val > 7)
2725 return false;
2726 }
2727
2728 return true;
2729}
2730
Chris Lattner8a594482007-11-25 00:24:49 +00002731/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002732/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002733static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2734 SDOperand &V2, SDOperand &Mask,
2735 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002736 MVT::ValueType VT = Op.getValueType();
2737 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002738 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002739 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002740 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002741
2742 for (unsigned i = 0; i != NumElems; ++i) {
2743 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002744 if (Arg.getOpcode() == ISD::UNDEF) {
2745 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2746 continue;
2747 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002748 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2749 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2750 if (Val < NumElems)
2751 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2752 else
2753 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2754 }
2755
Evan Cheng9eca5e82006-10-25 21:49:50 +00002756 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002757 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002758 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002759}
2760
Evan Cheng533a0aa2006-04-19 20:35:22 +00002761/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2762/// match movhlps. The lower half elements should come from upper half of
2763/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002764/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002765static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2766 unsigned NumElems = Mask->getNumOperands();
2767 if (NumElems != 4)
2768 return false;
2769 for (unsigned i = 0, e = 2; i != e; ++i)
2770 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2771 return false;
2772 for (unsigned i = 2; i != 4; ++i)
2773 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2774 return false;
2775 return true;
2776}
2777
Evan Cheng5ced1d82006-04-06 23:23:56 +00002778/// isScalarLoadToVector - Returns true if the node is a scalar load that
2779/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002780static inline bool isScalarLoadToVector(SDNode *N) {
2781 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2782 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002783 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002784 }
2785 return false;
2786}
2787
Evan Cheng533a0aa2006-04-19 20:35:22 +00002788/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2789/// match movlp{s|d}. The lower half elements should come from lower half of
2790/// V1 (and in order), and the upper half elements should come from the upper
2791/// half of V2 (and in order). And since V1 will become the source of the
2792/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002793static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002794 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002795 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002796 // Is V2 is a vector load, don't do this transformation. We will try to use
2797 // load folding shufps op.
2798 if (ISD::isNON_EXTLoad(V2))
2799 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002800
Evan Cheng533a0aa2006-04-19 20:35:22 +00002801 unsigned NumElems = Mask->getNumOperands();
2802 if (NumElems != 2 && NumElems != 4)
2803 return false;
2804 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2805 if (!isUndefOrEqual(Mask->getOperand(i), i))
2806 return false;
2807 for (unsigned i = NumElems/2; i != NumElems; ++i)
2808 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2809 return false;
2810 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002811}
2812
Evan Cheng39623da2006-04-20 08:58:49 +00002813/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2814/// all the same.
2815static bool isSplatVector(SDNode *N) {
2816 if (N->getOpcode() != ISD::BUILD_VECTOR)
2817 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002818
Evan Cheng39623da2006-04-20 08:58:49 +00002819 SDOperand SplatValue = N->getOperand(0);
2820 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2821 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002822 return false;
2823 return true;
2824}
2825
Evan Cheng8cf723d2006-09-08 01:50:06 +00002826/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2827/// to an undef.
2828static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002829 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002830 return false;
2831
2832 SDOperand V1 = N->getOperand(0);
2833 SDOperand V2 = N->getOperand(1);
2834 SDOperand Mask = N->getOperand(2);
2835 unsigned NumElems = Mask.getNumOperands();
2836 for (unsigned i = 0; i != NumElems; ++i) {
2837 SDOperand Arg = Mask.getOperand(i);
2838 if (Arg.getOpcode() != ISD::UNDEF) {
2839 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2840 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2841 return false;
2842 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2843 return false;
2844 }
2845 }
2846 return true;
2847}
2848
Evan Cheng213d2cf2007-05-17 18:45:50 +00002849/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2850/// constant +0.0.
2851static inline bool isZeroNode(SDOperand Elt) {
2852 return ((isa<ConstantSDNode>(Elt) &&
2853 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2854 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002855 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002856}
2857
2858/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2859/// to an zero vector.
2860static bool isZeroShuffle(SDNode *N) {
2861 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2862 return false;
2863
2864 SDOperand V1 = N->getOperand(0);
2865 SDOperand V2 = N->getOperand(1);
2866 SDOperand Mask = N->getOperand(2);
2867 unsigned NumElems = Mask.getNumOperands();
2868 for (unsigned i = 0; i != NumElems; ++i) {
2869 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002870 if (Arg.getOpcode() == ISD::UNDEF)
2871 continue;
2872
2873 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2874 if (Idx < NumElems) {
2875 unsigned Opc = V1.Val->getOpcode();
2876 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2877 continue;
2878 if (Opc != ISD::BUILD_VECTOR ||
2879 !isZeroNode(V1.Val->getOperand(Idx)))
2880 return false;
2881 } else if (Idx >= NumElems) {
2882 unsigned Opc = V2.Val->getOpcode();
2883 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2884 continue;
2885 if (Opc != ISD::BUILD_VECTOR ||
2886 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2887 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002888 }
2889 }
2890 return true;
2891}
2892
2893/// getZeroVector - Returns a vector of specified type with all zero elements.
2894///
2895static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2896 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002897
2898 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2899 // type. This ensures they get CSE'd.
2900 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2901 SDOperand Vec;
2902 if (MVT::getSizeInBits(VT) == 64) // MMX
2903 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2904 else // SSE
2905 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2906 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002907}
2908
Chris Lattner8a594482007-11-25 00:24:49 +00002909/// getOnesVector - Returns a vector of specified type with all bits set.
2910///
2911static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2912 assert(MVT::isVector(VT) && "Expected a vector type");
2913
2914 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2915 // type. This ensures they get CSE'd.
2916 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2917 SDOperand Vec;
2918 if (MVT::getSizeInBits(VT) == 64) // MMX
2919 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2920 else // SSE
2921 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2922 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2923}
2924
2925
Evan Cheng39623da2006-04-20 08:58:49 +00002926/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2927/// that point to V2 points to its first element.
2928static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2929 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2930
2931 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002932 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002933 unsigned NumElems = Mask.getNumOperands();
2934 for (unsigned i = 0; i != NumElems; ++i) {
2935 SDOperand Arg = Mask.getOperand(i);
2936 if (Arg.getOpcode() != ISD::UNDEF) {
2937 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2938 if (Val > NumElems) {
2939 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2940 Changed = true;
2941 }
2942 }
2943 MaskVec.push_back(Arg);
2944 }
2945
2946 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002947 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2948 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002949 return Mask;
2950}
2951
Evan Cheng017dcc62006-04-21 01:05:10 +00002952/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2953/// operation of specified width.
2954static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002955 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002956 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002957
Chris Lattner5a88b832007-02-25 07:10:00 +00002958 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002959 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2960 for (unsigned i = 1; i != NumElems; ++i)
2961 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002962 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002963}
2964
Evan Chengc575ca22006-04-17 20:43:08 +00002965/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2966/// of specified width.
2967static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2968 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002969 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002970 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002971 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2972 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2973 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2974 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002975 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002976}
2977
Evan Cheng39623da2006-04-20 08:58:49 +00002978/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2979/// of specified width.
2980static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2981 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002982 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002983 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002984 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002985 for (unsigned i = 0; i != Half; ++i) {
2986 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2987 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2988 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002989 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002990}
2991
Evan Chengc575ca22006-04-17 20:43:08 +00002992/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2993///
2994static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2995 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002996 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002997 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002998 unsigned NumElems = Mask.getNumOperands();
2999 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00003000 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00003001 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00003002 NumElems >>= 1;
3003 }
3004 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3005
Chris Lattner8a594482007-11-25 00:24:49 +00003006 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00003007 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00003008 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00003009 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3010}
3011
Evan Chengba05f722006-04-21 23:03:30 +00003012/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003013/// vector of zero or undef vector. This produces a shuffle where the low
3014/// element of V2 is swizzled into the zero/undef vector, landing at element
3015/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Evan Chengba05f722006-04-21 23:03:30 +00003016static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00003017 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00003018 bool isZero, SelectionDAG &DAG) {
3019 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00003020 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003021 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner8a594482007-11-25 00:24:49 +00003022 SmallVector<SDOperand, 16> MaskVec;
3023 for (unsigned i = 0; i != NumElems; ++i)
3024 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3025 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3026 else
3027 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003028 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3029 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00003030 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003031}
3032
Evan Chengc78d3b42006-04-24 18:01:45 +00003033/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3034///
3035static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3036 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003037 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003038 if (NumNonZero > 8)
3039 return SDOperand();
3040
3041 SDOperand V(0, 0);
3042 bool First = true;
3043 for (unsigned i = 0; i < 16; ++i) {
3044 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3045 if (ThisIsNonZero && First) {
3046 if (NumZero)
3047 V = getZeroVector(MVT::v8i16, DAG);
3048 else
3049 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3050 First = false;
3051 }
3052
3053 if ((i & 1) != 0) {
3054 SDOperand ThisElt(0, 0), LastElt(0, 0);
3055 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3056 if (LastIsNonZero) {
3057 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3058 }
3059 if (ThisIsNonZero) {
3060 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3061 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3062 ThisElt, DAG.getConstant(8, MVT::i8));
3063 if (LastIsNonZero)
3064 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3065 } else
3066 ThisElt = LastElt;
3067
3068 if (ThisElt.Val)
3069 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00003070 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003071 }
3072 }
3073
3074 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3075}
3076
Bill Wendlinga348c562007-03-22 18:42:45 +00003077/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003078///
3079static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3080 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003081 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003082 if (NumNonZero > 4)
3083 return SDOperand();
3084
3085 SDOperand V(0, 0);
3086 bool First = true;
3087 for (unsigned i = 0; i < 8; ++i) {
3088 bool isNonZero = (NonZeros & (1 << i)) != 0;
3089 if (isNonZero) {
3090 if (First) {
3091 if (NumZero)
3092 V = getZeroVector(MVT::v8i16, DAG);
3093 else
3094 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3095 First = false;
3096 }
3097 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00003098 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003099 }
3100 }
3101
3102 return V;
3103}
3104
Evan Cheng0db9fe62006-04-25 20:13:52 +00003105SDOperand
3106X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003107 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3108 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3109 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3110 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3111 // eliminated on x86-32 hosts.
3112 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3113 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003114
Chris Lattner8a594482007-11-25 00:24:49 +00003115 if (ISD::isBuildVectorAllOnes(Op.Val))
3116 return getOnesVector(Op.getValueType(), DAG);
3117 return getZeroVector(Op.getValueType(), DAG);
3118 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003119
3120 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003121 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003122 unsigned EVTBits = MVT::getSizeInBits(EVT);
3123
3124 unsigned NumElems = Op.getNumOperands();
3125 unsigned NumZero = 0;
3126 unsigned NumNonZero = 0;
3127 unsigned NonZeros = 0;
Dan Gohmana3941172007-07-24 22:55:08 +00003128 unsigned NumNonZeroImms = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003129 std::set<SDOperand> Values;
3130 for (unsigned i = 0; i < NumElems; ++i) {
3131 SDOperand Elt = Op.getOperand(i);
3132 if (Elt.getOpcode() != ISD::UNDEF) {
3133 Values.insert(Elt);
3134 if (isZeroNode(Elt))
3135 NumZero++;
3136 else {
3137 NonZeros |= (1 << i);
3138 NumNonZero++;
Dan Gohmana3941172007-07-24 22:55:08 +00003139 if (Elt.getOpcode() == ISD::Constant ||
3140 Elt.getOpcode() == ISD::ConstantFP)
3141 NumNonZeroImms++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003142 }
3143 }
3144 }
3145
Dan Gohman7f321562007-06-25 16:23:39 +00003146 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003147 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3148 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003149 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003150
3151 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3152 if (Values.size() == 1)
3153 return SDOperand();
3154
3155 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003156 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003157 unsigned Idx = CountTrailingZeros_32(NonZeros);
3158 SDOperand Item = Op.getOperand(Idx);
3159 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3160 if (Idx == 0)
3161 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3162 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3163 NumZero > 0, DAG);
3164
3165 if (EVTBits == 32) {
3166 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3167 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3168 DAG);
3169 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003170 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003171 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003172 for (unsigned i = 0; i < NumElems; i++)
3173 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003174 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3175 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003176 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3177 DAG.getNode(ISD::UNDEF, VT), Mask);
3178 }
3179 }
3180
Dan Gohmana3941172007-07-24 22:55:08 +00003181 // A vector full of immediates; various special cases are already
3182 // handled, so this is best done with a single constant-pool load.
3183 if (NumNonZero == NumNonZeroImms)
3184 return SDOperand();
3185
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003186 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003187 if (EVTBits == 64)
3188 return SDOperand();
3189
3190 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003191 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003192 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3193 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003194 if (V.Val) return V;
3195 }
3196
Bill Wendling826f36f2007-03-28 00:57:11 +00003197 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003198 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3199 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003200 if (V.Val) return V;
3201 }
3202
3203 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003204 SmallVector<SDOperand, 8> V;
3205 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003206 if (NumElems == 4 && NumZero > 0) {
3207 for (unsigned i = 0; i < 4; ++i) {
3208 bool isZero = !(NonZeros & (1 << i));
3209 if (isZero)
3210 V[i] = getZeroVector(VT, DAG);
3211 else
3212 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3213 }
3214
3215 for (unsigned i = 0; i < 2; ++i) {
3216 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3217 default: break;
3218 case 0:
3219 V[i] = V[i*2]; // Must be a zero vector.
3220 break;
3221 case 1:
3222 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3223 getMOVLMask(NumElems, DAG));
3224 break;
3225 case 2:
3226 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3227 getMOVLMask(NumElems, DAG));
3228 break;
3229 case 3:
3230 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3231 getUnpacklMask(NumElems, DAG));
3232 break;
3233 }
3234 }
3235
Evan Cheng069287d2006-05-16 07:21:53 +00003236 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003237 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003238 // FIXME: we can do the same for v4f32 case when we know both parts of
3239 // the lower half come from scalar_to_vector (loadf32). We should do
3240 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003241 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003242 return V[0];
3243 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003244 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003245 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003246 bool Reverse = (NonZeros & 0x3) == 2;
3247 for (unsigned i = 0; i < 2; ++i)
3248 if (Reverse)
3249 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3250 else
3251 MaskVec.push_back(DAG.getConstant(i, EVT));
3252 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3253 for (unsigned i = 0; i < 2; ++i)
3254 if (Reverse)
3255 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3256 else
3257 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003258 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3259 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003260 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3261 }
3262
3263 if (Values.size() > 2) {
3264 // Expand into a number of unpckl*.
3265 // e.g. for v4f32
3266 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3267 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3268 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3269 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3270 for (unsigned i = 0; i < NumElems; ++i)
3271 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3272 NumElems >>= 1;
3273 while (NumElems != 0) {
3274 for (unsigned i = 0; i < NumElems; ++i)
3275 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3276 UnpckMask);
3277 NumElems >>= 1;
3278 }
3279 return V[0];
3280 }
3281
3282 return SDOperand();
3283}
3284
3285SDOperand
3286X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3287 SDOperand V1 = Op.getOperand(0);
3288 SDOperand V2 = Op.getOperand(1);
3289 SDOperand PermMask = Op.getOperand(2);
3290 MVT::ValueType VT = Op.getValueType();
3291 unsigned NumElems = PermMask.getNumOperands();
3292 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3293 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003294 bool V1IsSplat = false;
3295 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003296
Evan Cheng8cf723d2006-09-08 01:50:06 +00003297 if (isUndefShuffle(Op.Val))
3298 return DAG.getNode(ISD::UNDEF, VT);
3299
Evan Cheng213d2cf2007-05-17 18:45:50 +00003300 if (isZeroShuffle(Op.Val))
3301 return getZeroVector(VT, DAG);
3302
Evan Cheng49892af2007-06-19 00:02:56 +00003303 if (isIdentityMask(PermMask.Val))
3304 return V1;
3305 else if (isIdentityMask(PermMask.Val, true))
3306 return V2;
3307
Evan Cheng0db9fe62006-04-25 20:13:52 +00003308 if (isSplatMask(PermMask.Val)) {
3309 if (NumElems <= 4) return Op;
3310 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003311 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003312 }
3313
Evan Cheng9bbbb982006-10-25 20:48:19 +00003314 if (X86::isMOVLMask(PermMask.Val))
3315 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003316
Evan Cheng9bbbb982006-10-25 20:48:19 +00003317 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3318 X86::isMOVSLDUPMask(PermMask.Val) ||
3319 X86::isMOVHLPSMask(PermMask.Val) ||
3320 X86::isMOVHPMask(PermMask.Val) ||
3321 X86::isMOVLPMask(PermMask.Val))
3322 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003323
Evan Cheng9bbbb982006-10-25 20:48:19 +00003324 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3325 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003326 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003327
Evan Cheng9eca5e82006-10-25 21:49:50 +00003328 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003329 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3330 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003331 V1IsSplat = isSplatVector(V1.Val);
3332 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003333
3334 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003335 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003336 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003337 std::swap(V1IsSplat, V2IsSplat);
3338 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003339 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003340 }
3341
3342 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3343 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003344 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003345 if (V2IsSplat) {
3346 // V2 is a splat, so the mask may be malformed. That is, it may point
3347 // to any V2 element. The instruction selectior won't like this. Get
3348 // a corrected mask and commute to form a proper MOVS{S|D}.
3349 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3350 if (NewMask.Val != PermMask.Val)
3351 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003352 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003353 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003354 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003355
Evan Chengd9b8e402006-10-16 06:36:00 +00003356 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003357 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003358 X86::isUNPCKLMask(PermMask.Val) ||
3359 X86::isUNPCKHMask(PermMask.Val))
3360 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003361
Evan Cheng9bbbb982006-10-25 20:48:19 +00003362 if (V2IsSplat) {
3363 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003364 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003365 // new vector_shuffle with the corrected mask.
3366 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3367 if (NewMask.Val != PermMask.Val) {
3368 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3369 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3370 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3371 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3372 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3373 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003374 }
3375 }
3376 }
3377
3378 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003379 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3380 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3381
3382 if (Commuted) {
3383 // Commute is back and try unpck* again.
3384 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3385 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003386 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003387 X86::isUNPCKLMask(PermMask.Val) ||
3388 X86::isUNPCKHMask(PermMask.Val))
3389 return Op;
3390 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003391
3392 // If VT is integer, try PSHUF* first, then SHUFP*.
3393 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003394 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3395 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3396 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3397 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003398 X86::isPSHUFHWMask(PermMask.Val) ||
3399 X86::isPSHUFLWMask(PermMask.Val)) {
3400 if (V2.getOpcode() != ISD::UNDEF)
3401 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3402 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3403 return Op;
3404 }
3405
Chris Lattner07c70cd2007-05-17 17:13:13 +00003406 if (X86::isSHUFPMask(PermMask.Val) &&
3407 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003408 return Op;
3409
3410 // Handle v8i16 shuffle high / low shuffle node pair.
3411 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3412 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003413 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003414 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003415 for (unsigned i = 0; i != 4; ++i)
3416 MaskVec.push_back(PermMask.getOperand(i));
3417 for (unsigned i = 4; i != 8; ++i)
3418 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003419 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3420 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003421 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3422 MaskVec.clear();
3423 for (unsigned i = 0; i != 4; ++i)
3424 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3425 for (unsigned i = 4; i != 8; ++i)
3426 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00003427 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003428 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3429 }
3430 } else {
3431 // Floating point cases in the other order.
3432 if (X86::isSHUFPMask(PermMask.Val))
3433 return Op;
3434 if (X86::isPSHUFDMask(PermMask.Val) ||
3435 X86::isPSHUFHWMask(PermMask.Val) ||
3436 X86::isPSHUFLWMask(PermMask.Val)) {
3437 if (V2.getOpcode() != ISD::UNDEF)
3438 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3439 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3440 return Op;
3441 }
3442 }
3443
Chris Lattner07c70cd2007-05-17 17:13:13 +00003444 if (NumElems == 4 &&
3445 // Don't do this for MMX.
3446 MVT::getSizeInBits(VT) != 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003447 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003448 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003449 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003450 Locs.reserve(NumElems);
Chris Lattner5a88b832007-02-25 07:10:00 +00003451 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3452 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003453 unsigned NumHi = 0;
3454 unsigned NumLo = 0;
3455 // If no more than two elements come from either vector. This can be
3456 // implemented with two shuffles. First shuffle gather the elements.
3457 // The second shuffle, which takes the first shuffle as both of its
3458 // vector operands, put the elements into the right order.
3459 for (unsigned i = 0; i != NumElems; ++i) {
3460 SDOperand Elt = PermMask.getOperand(i);
3461 if (Elt.getOpcode() == ISD::UNDEF) {
3462 Locs[i] = std::make_pair(-1, -1);
3463 } else {
3464 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3465 if (Val < NumElems) {
3466 Locs[i] = std::make_pair(0, NumLo);
3467 Mask1[NumLo] = Elt;
3468 NumLo++;
3469 } else {
3470 Locs[i] = std::make_pair(1, NumHi);
3471 if (2+NumHi < NumElems)
3472 Mask1[2+NumHi] = Elt;
3473 NumHi++;
3474 }
3475 }
3476 }
3477 if (NumLo <= 2 && NumHi <= 2) {
3478 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003479 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3480 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003481 for (unsigned i = 0; i != NumElems; ++i) {
3482 if (Locs[i].first == -1)
3483 continue;
3484 else {
3485 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3486 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3487 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3488 }
3489 }
3490
3491 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003492 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3493 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003494 }
3495
3496 // Break it into (shuffle shuffle_hi, shuffle_lo).
3497 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003498 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3499 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3500 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003501 unsigned MaskIdx = 0;
3502 unsigned LoIdx = 0;
3503 unsigned HiIdx = NumElems/2;
3504 for (unsigned i = 0; i != NumElems; ++i) {
3505 if (i == NumElems/2) {
3506 MaskPtr = &HiMask;
3507 MaskIdx = 1;
3508 LoIdx = 0;
3509 HiIdx = NumElems/2;
3510 }
3511 SDOperand Elt = PermMask.getOperand(i);
3512 if (Elt.getOpcode() == ISD::UNDEF) {
3513 Locs[i] = std::make_pair(-1, -1);
3514 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3515 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3516 (*MaskPtr)[LoIdx] = Elt;
3517 LoIdx++;
3518 } else {
3519 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3520 (*MaskPtr)[HiIdx] = Elt;
3521 HiIdx++;
3522 }
3523 }
3524
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003525 SDOperand LoShuffle =
3526 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003527 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3528 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003529 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003530 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003531 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3532 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003533 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003534 for (unsigned i = 0; i != NumElems; ++i) {
3535 if (Locs[i].first == -1) {
3536 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3537 } else {
3538 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3539 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3540 }
3541 }
3542 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003543 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3544 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003545 }
3546
3547 return SDOperand();
3548}
3549
3550SDOperand
3551X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3552 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3553 return SDOperand();
3554
3555 MVT::ValueType VT = Op.getValueType();
3556 // TODO: handle v16i8.
3557 if (MVT::getSizeInBits(VT) == 16) {
3558 // Transform it so it match pextrw which produces a 32-bit result.
3559 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3560 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3561 Op.getOperand(0), Op.getOperand(1));
3562 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3563 DAG.getValueType(VT));
3564 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3565 } else if (MVT::getSizeInBits(VT) == 32) {
3566 SDOperand Vec = Op.getOperand(0);
3567 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3568 if (Idx == 0)
3569 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003570 // SHUFPS the element to the lowest double word, then movss.
3571 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003572 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003573 IdxVec.
3574 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3575 IdxVec.
3576 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3577 IdxVec.
3578 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3579 IdxVec.
3580 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003581 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3582 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003583 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003584 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003585 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003586 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003587 } else if (MVT::getSizeInBits(VT) == 64) {
3588 SDOperand Vec = Op.getOperand(0);
3589 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3590 if (Idx == 0)
3591 return Op;
3592
3593 // UNPCKHPD the element to the lowest double word, then movsd.
3594 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3595 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3596 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003597 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003598 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003599 IdxVec.
3600 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003601 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3602 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003603 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3604 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3605 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003606 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003607 }
3608
3609 return SDOperand();
3610}
3611
3612SDOperand
3613X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00003614 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00003615 // as its second argument.
3616 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003617 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003618 SDOperand N0 = Op.getOperand(0);
3619 SDOperand N1 = Op.getOperand(1);
3620 SDOperand N2 = Op.getOperand(2);
3621 if (MVT::getSizeInBits(BaseVT) == 16) {
3622 if (N1.getValueType() != MVT::i32)
3623 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3624 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00003625 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003626 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3627 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3628 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3629 if (Idx == 0) {
3630 // Use a movss.
3631 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3632 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman51eaa862007-06-14 22:58:02 +00003633 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003634 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003635 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3636 for (unsigned i = 1; i <= 3; ++i)
3637 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3638 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003639 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3640 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003641 } else {
3642 // Use two pinsrw instructions to insert a 32 bit value.
3643 Idx <<= 1;
3644 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng4ebcc8c2007-07-31 06:21:44 +00003645 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3646 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3647 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3648 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003649 }
3650 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3651 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003652 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003653 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3654 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003655 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003656 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3657 }
3658 }
3659
3660 return SDOperand();
3661}
3662
3663SDOperand
3664X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3665 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3666 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3667}
3668
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003669// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003670// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3671// one of the above mentioned nodes. It has to be wrapped because otherwise
3672// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3673// be used to form addressing mode. These wrapped nodes will be selected
3674// into MOV32ri.
3675SDOperand
3676X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3677 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003678 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3679 getPointerTy(),
3680 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003681 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003682 // With PIC, the address is actually $g + Offset.
3683 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3684 !Subtarget->isPICStyleRIPRel()) {
3685 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3686 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3687 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003688 }
3689
3690 return Result;
3691}
3692
3693SDOperand
3694X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3695 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003696 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003697 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003698 // With PIC, the address is actually $g + Offset.
3699 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3700 !Subtarget->isPICStyleRIPRel()) {
3701 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3702 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3703 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003704 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003705
3706 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3707 // load the value at address GV, not the value of GV itself. This means that
3708 // the GlobalAddress must be in the base or index register of the address, not
3709 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003710 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003711 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3712 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003713
3714 return Result;
3715}
3716
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003717// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3718static SDOperand
3719LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3720 const MVT::ValueType PtrVT) {
3721 SDOperand InFlag;
3722 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3723 DAG.getNode(X86ISD::GlobalBaseReg,
3724 PtrVT), InFlag);
3725 InFlag = Chain.getValue(1);
3726
3727 // emit leal symbol@TLSGD(,%ebx,1), %eax
3728 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3729 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3730 GA->getValueType(0),
3731 GA->getOffset());
3732 SDOperand Ops[] = { Chain, TGA, InFlag };
3733 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3734 InFlag = Result.getValue(2);
3735 Chain = Result.getValue(1);
3736
3737 // call ___tls_get_addr. This function receives its argument in
3738 // the register EAX.
3739 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3740 InFlag = Chain.getValue(1);
3741
3742 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3743 SDOperand Ops1[] = { Chain,
3744 DAG.getTargetExternalSymbol("___tls_get_addr",
3745 PtrVT),
3746 DAG.getRegister(X86::EAX, PtrVT),
3747 DAG.getRegister(X86::EBX, PtrVT),
3748 InFlag };
3749 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3750 InFlag = Chain.getValue(1);
3751
3752 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3753}
3754
3755// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3756// "local exec" model.
3757static SDOperand
3758LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3759 const MVT::ValueType PtrVT) {
3760 // Get the Thread Pointer
3761 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3762 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3763 // exec)
3764 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3765 GA->getValueType(0),
3766 GA->getOffset());
3767 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003768
3769 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3770 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3771
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003772 // The address of the thread local variable is the add of the thread
3773 // pointer with the offset of the variable.
3774 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3775}
3776
3777SDOperand
3778X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3779 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003780 // TODO: implement the "initial exec"model for pic executables
3781 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3782 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003783 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3784 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3785 // otherwise use the "Local Exec"TLS Model
3786 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3787 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3788 else
3789 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3790}
3791
Evan Cheng0db9fe62006-04-25 20:13:52 +00003792SDOperand
3793X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3794 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003795 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003796 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003797 // With PIC, the address is actually $g + Offset.
3798 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3799 !Subtarget->isPICStyleRIPRel()) {
3800 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3801 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3802 Result);
3803 }
3804
3805 return Result;
3806}
3807
3808SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3809 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3810 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3811 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3812 // With PIC, the address is actually $g + Offset.
3813 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3814 !Subtarget->isPICStyleRIPRel()) {
3815 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3816 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3817 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003818 }
3819
3820 return Result;
3821}
3822
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003823/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3824/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003825SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003826 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3827 "Not an i64 shift!");
3828 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3829 SDOperand ShOpLo = Op.getOperand(0);
3830 SDOperand ShOpHi = Op.getOperand(1);
3831 SDOperand ShAmt = Op.getOperand(2);
3832 SDOperand Tmp1 = isSRA ?
3833 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3834 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003835
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003836 SDOperand Tmp2, Tmp3;
3837 if (Op.getOpcode() == ISD::SHL_PARTS) {
3838 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3839 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3840 } else {
3841 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3842 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3843 }
Evan Chenge3413162006-01-09 18:33:28 +00003844
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003845 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3846 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3847 DAG.getConstant(32, MVT::i8));
3848 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3849 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00003850
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003851 SDOperand Hi, Lo;
3852 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3853 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3854 SmallVector<SDOperand, 4> Ops;
3855 if (Op.getOpcode() == ISD::SHL_PARTS) {
3856 Ops.push_back(Tmp2);
3857 Ops.push_back(Tmp3);
3858 Ops.push_back(CC);
3859 Ops.push_back(Cond);
3860 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003861
Evan Chenge3413162006-01-09 18:33:28 +00003862 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003863 Ops.push_back(Tmp3);
3864 Ops.push_back(Tmp1);
3865 Ops.push_back(CC);
3866 Ops.push_back(Cond);
3867 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3868 } else {
3869 Ops.push_back(Tmp2);
3870 Ops.push_back(Tmp3);
3871 Ops.push_back(CC);
3872 Ops.push_back(Cond);
3873 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3874
3875 Ops.clear();
3876 Ops.push_back(Tmp3);
3877 Ops.push_back(Tmp1);
3878 Ops.push_back(CC);
3879 Ops.push_back(Cond);
3880 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3881 }
3882
3883 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3884 Ops.clear();
3885 Ops.push_back(Lo);
3886 Ops.push_back(Hi);
3887 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003888}
Evan Chenga3195e82006-01-12 22:54:21 +00003889
Evan Cheng0db9fe62006-04-25 20:13:52 +00003890SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3891 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3892 Op.getOperand(0).getValueType() >= MVT::i16 &&
3893 "Unknown SINT_TO_FP to lower!");
3894
3895 SDOperand Result;
3896 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3897 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3898 MachineFunction &MF = DAG.getMachineFunction();
3899 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3900 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003901 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003902 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003903
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003904 // These are really Legal; caller falls through into that case.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003905 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3906 return Result;
3907 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003908 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003909 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3910 Subtarget->is64Bit())
3911 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003912
Evan Cheng0db9fe62006-04-25 20:13:52 +00003913 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003914 SDVTList Tys;
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003915 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3916 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003917 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00003918 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3919 else
Dale Johannesen849f2142007-07-03 00:53:03 +00003920 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003921 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003922 Ops.push_back(Chain);
3923 Ops.push_back(StackSlot);
3924 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003925 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003926 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003927
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003928 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003929 Chain = Result.getValue(1);
3930 SDOperand InFlag = Result.getValue(2);
3931
3932 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3933 // shouldn't be necessary except that RFP cannot be live across
3934 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003935 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003936 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003937 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00003938 Tys = DAG.getVTList(MVT::Other);
3939 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003940 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003941 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003942 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003943 Ops.push_back(DAG.getValueType(Op.getValueType()));
3944 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003945 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003946 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003947 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003948
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 return Result;
3950}
3951
Chris Lattner27a6c732007-11-24 07:07:01 +00003952std::pair<SDOperand,SDOperand> X86TargetLowering::
3953FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3955 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00003956
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003957 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003958 if (Op.getValueType() == MVT::i32 &&
3959 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
Chris Lattner27a6c732007-11-24 07:07:01 +00003960 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003961 if (Op.getValueType() == MVT::i32 &&
3962 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Chris Lattner27a6c732007-11-24 07:07:01 +00003963 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00003964 if (Subtarget->is64Bit() &&
3965 Op.getValueType() == MVT::i64 &&
3966 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00003967 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003968
Evan Cheng87c89352007-10-15 20:11:21 +00003969 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3970 // stack slot.
3971 MachineFunction &MF = DAG.getMachineFunction();
3972 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3973 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3974 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 unsigned Opc;
3976 switch (Op.getValueType()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00003977 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3978 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3979 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3980 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003981 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003982
Evan Cheng0db9fe62006-04-25 20:13:52 +00003983 SDOperand Chain = DAG.getEntryNode();
3984 SDOperand Value = Op.getOperand(0);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003985 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3986 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003987 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003988 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00003989 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003990 SDOperand Ops[] = {
3991 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3992 };
3993 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003994 Chain = Value.getValue(1);
3995 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3996 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3997 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003998
Evan Cheng0db9fe62006-04-25 20:13:52 +00003999 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004000 SDOperand Ops[] = { Chain, Value, StackSlot };
4001 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004002
Chris Lattner27a6c732007-11-24 07:07:01 +00004003 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004004}
4005
Chris Lattner27a6c732007-11-24 07:07:01 +00004006SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004007 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4008 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4009 if (FIST.Val == 0) return SDOperand();
4010
4011 // Load the result.
4012 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4013}
4014
4015SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4016 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4017 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4018 if (FIST.Val == 0) return 0;
4019
4020 // Return an i64 load from the stack slot.
4021 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4022
4023 // Use a MERGE_VALUES node to drop the chain result value.
4024 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4025}
4026
Evan Cheng0db9fe62006-04-25 20:13:52 +00004027SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4028 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004029 MVT::ValueType EltVT = VT;
4030 if (MVT::isVector(VT))
4031 EltVT = MVT::getVectorElementType(VT);
4032 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004033 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004034 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004035 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004036 CV.push_back(C);
4037 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004038 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004039 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004040 CV.push_back(C);
4041 CV.push_back(C);
4042 CV.push_back(C);
4043 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004044 }
Dan Gohmand3006222007-07-27 17:16:43 +00004045 Constant *C = ConstantVector::get(CV);
4046 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4047 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4048 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004049 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4050}
4051
4052SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4053 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004054 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004055 unsigned EltNum = 1;
4056 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004057 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004058 EltNum = MVT::getVectorNumElements(VT);
4059 }
Dan Gohman20382522007-07-10 00:05:58 +00004060 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004061 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004062 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004063 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004064 CV.push_back(C);
4065 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004066 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004067 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004068 CV.push_back(C);
4069 CV.push_back(C);
4070 CV.push_back(C);
4071 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004072 }
Dan Gohmand3006222007-07-27 17:16:43 +00004073 Constant *C = ConstantVector::get(CV);
4074 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4075 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4076 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004077 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004078 return DAG.getNode(ISD::BIT_CONVERT, VT,
4079 DAG.getNode(ISD::XOR, MVT::v2i64,
4080 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4081 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4082 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004083 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4084 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004085}
4086
Evan Cheng68c47cb2007-01-05 07:55:56 +00004087SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004088 SDOperand Op0 = Op.getOperand(0);
4089 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004090 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004091 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004092 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004093
4094 // If second operand is smaller, extend it first.
4095 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4096 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4097 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004098 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004099 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004100 // And if it is bigger, shrink it first.
4101 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4102 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4103 SrcVT = VT;
4104 SrcTy = MVT::getTypeForValueType(SrcVT);
4105 }
4106
4107 // At this point the operands and the result should have the same
4108 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004109
Evan Cheng68c47cb2007-01-05 07:55:56 +00004110 // First get the sign bit of second operand.
4111 std::vector<Constant*> CV;
4112 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004113 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4114 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004115 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004116 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4117 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4118 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4119 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004120 }
Dan Gohmand3006222007-07-27 17:16:43 +00004121 Constant *C = ConstantVector::get(CV);
4122 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4123 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4124 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004125 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004126
4127 // Shift sign bit right or left if the two operands have different types.
4128 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4129 // Op0 is MVT::f32, Op1 is MVT::f64.
4130 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4131 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4132 DAG.getConstant(32, MVT::i32));
4133 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4134 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4135 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004136 }
4137
Evan Cheng73d6cf12007-01-05 21:37:56 +00004138 // Clear first operand sign bit.
4139 CV.clear();
4140 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004141 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4142 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004143 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004144 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4145 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4146 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4147 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004148 }
Dan Gohmand3006222007-07-27 17:16:43 +00004149 C = ConstantVector::get(CV);
4150 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4151 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4152 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004153 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4154
4155 // Or the value with the sign bit.
4156 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004157}
4158
Evan Chenge5f62042007-09-29 00:00:36 +00004159SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004160 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004161 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004162 SDOperand Op0 = Op.getOperand(0);
4163 SDOperand Op1 = Op.getOperand(1);
4164 SDOperand CC = Op.getOperand(2);
4165 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4166 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4167 unsigned X86CC;
4168
Evan Cheng0488db92007-09-25 01:57:46 +00004169 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004170 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004171 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4172 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004173 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004174 }
Evan Cheng0488db92007-09-25 01:57:46 +00004175
4176 assert(isFP && "Illegal integer SetCC!");
4177
Evan Chenge5f62042007-09-29 00:00:36 +00004178 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004179 switch (SetCCOpcode) {
4180 default: assert(false && "Illegal floating point SetCC!");
4181 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004182 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004183 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004184 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004185 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4186 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4187 }
4188 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004189 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004190 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004191 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004192 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4193 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4194 }
4195 }
4196}
4197
4198
Evan Cheng0db9fe62006-04-25 20:13:52 +00004199SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004200 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004201 SDOperand Cond = Op.getOperand(0);
4202 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004203
Evan Cheng734503b2006-09-11 02:19:56 +00004204 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004205 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004206
Evan Cheng3f41d662007-10-08 22:16:29 +00004207 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4208 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004209 if (Cond.getOpcode() == X86ISD::SETCC) {
4210 CC = Cond.getOperand(0);
4211
Evan Cheng734503b2006-09-11 02:19:56 +00004212 SDOperand Cmp = Cond.getOperand(1);
4213 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004214 MVT::ValueType VT = Op.getValueType();
4215 bool IllegalFPCMov = false;
4216 if (VT == MVT::f32 && !X86ScalarSSEf32)
4217 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4218 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4219 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesenc274f542007-10-16 18:09:08 +00004220 else if (VT == MVT::f80)
4221 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chenge5f62042007-09-29 00:00:36 +00004222 if ((Opc == X86ISD::CMP ||
4223 Opc == X86ISD::COMI ||
4224 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004225 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004226 addTest = false;
4227 }
4228 }
4229
4230 if (addTest) {
4231 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004232 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004233 }
4234
4235 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4236 MVT::Flag);
4237 SmallVector<SDOperand, 4> Ops;
4238 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4239 // condition is true.
4240 Ops.push_back(Op.getOperand(2));
4241 Ops.push_back(Op.getOperand(1));
4242 Ops.push_back(CC);
4243 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004244 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004245}
4246
Evan Cheng0db9fe62006-04-25 20:13:52 +00004247SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004248 bool addTest = true;
4249 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004250 SDOperand Cond = Op.getOperand(1);
4251 SDOperand Dest = Op.getOperand(2);
4252 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004253
Evan Cheng0db9fe62006-04-25 20:13:52 +00004254 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004255 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004256
Evan Cheng3f41d662007-10-08 22:16:29 +00004257 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4258 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004259 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004260 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004261
Evan Cheng734503b2006-09-11 02:19:56 +00004262 SDOperand Cmp = Cond.getOperand(1);
4263 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004264 if (Opc == X86ISD::CMP ||
4265 Opc == X86ISD::COMI ||
4266 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004267 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004268 addTest = false;
4269 }
4270 }
4271
4272 if (addTest) {
4273 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004274 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004275 }
Evan Chenge5f62042007-09-29 00:00:36 +00004276 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004277 Chain, Op.getOperand(2), CC, Cond);
4278}
4279
Evan Cheng32fe1032006-05-25 00:59:30 +00004280SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004281 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4282 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004283
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004284 if (Subtarget->is64Bit())
4285 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4286 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4287 else
4288 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00004289 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004290 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004291 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004292 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00004293 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004294 if (isTailCall && PerformTailCallOpt)
4295 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4296 else
4297 return LowerCCCCallTo(Op,DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004298 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004299 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004300 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004301 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004302 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004303 }
Evan Cheng32fe1032006-05-25 00:59:30 +00004304}
4305
Anton Korobeynikove060b532007-04-17 19:34:00 +00004306
4307// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4308// Calls to _alloca is needed to probe the stack when allocating more than 4k
4309// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4310// that the guard pages used by the OS virtual memory manager are allocated in
4311// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004312SDOperand
4313X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4314 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004315 assert(Subtarget->isTargetCygMing() &&
4316 "This should be used only on Cygwin/Mingw targets");
4317
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004318 // Get the inputs.
4319 SDOperand Chain = Op.getOperand(0);
4320 SDOperand Size = Op.getOperand(1);
4321 // FIXME: Ensure alignment here
4322
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004323 SDOperand Flag;
4324
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004325 MVT::ValueType IntPtr = getPointerTy();
4326 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004327
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004328 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4329 Flag = Chain.getValue(1);
4330
4331 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4332 SDOperand Ops[] = { Chain,
4333 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4334 DAG.getRegister(X86::EAX, IntPtr),
4335 Flag };
4336 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4337 Flag = Chain.getValue(1);
4338
4339 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004340
4341 std::vector<MVT::ValueType> Tys;
4342 Tys.push_back(SPTy);
4343 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004344 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4345 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004346}
4347
Evan Cheng1bc78042006-04-26 01:20:17 +00004348SDOperand
4349X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00004350 MachineFunction &MF = DAG.getMachineFunction();
4351 const Function* Fn = MF.getFunction();
4352 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00004353 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00004354 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00004355 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00004356
Evan Cheng25caf632006-05-23 21:06:34 +00004357 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00004358 if (Subtarget->is64Bit())
4359 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00004360 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004361 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004362 default:
4363 assert(0 && "Unsupported calling convention");
4364 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004365 return LowerCCCArguments(Op,DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004366 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00004367 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004368 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004369 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004370 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004371 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004372 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004373 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00004374 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004375 }
Evan Cheng1bc78042006-04-26 01:20:17 +00004376}
4377
Evan Cheng0db9fe62006-04-25 20:13:52 +00004378SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4379 SDOperand InFlag(0, 0);
4380 SDOperand Chain = Op.getOperand(0);
4381 unsigned Align =
4382 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4383 if (Align == 0) Align = 1;
4384
4385 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004386 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004387 // The libc version is likely to be faster for these cases. It can use the
4388 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004389 if ((Align & 3) != 0 ||
Rafael Espindolafc05f402007-10-31 11:52:06 +00004390 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004391 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004392 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004393 TargetLowering::ArgListTy Args;
4394 TargetLowering::ArgListEntry Entry;
4395 Entry.Node = Op.getOperand(1);
4396 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004397 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004398 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004399 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4400 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004401 Args.push_back(Entry);
4402 Entry.Node = Op.getOperand(3);
4403 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004405 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004406 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4407 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004408 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004409
Evan Cheng0db9fe62006-04-25 20:13:52 +00004410 MVT::ValueType AVT;
4411 SDOperand Count;
4412 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4413 unsigned BytesLeft = 0;
4414 bool TwoRepStos = false;
4415 if (ValC) {
4416 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004417 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004418
Evan Cheng0db9fe62006-04-25 20:13:52 +00004419 // If the value is a constant, then we can potentially use larger sets.
4420 switch (Align & 3) {
4421 case 2: // WORD aligned
4422 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004423 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004424 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004425 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004426 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004427 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004428 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004429 Val = (Val << 8) | Val;
4430 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004431 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4432 AVT = MVT::i64;
4433 ValReg = X86::RAX;
4434 Val = (Val << 32) | Val;
4435 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436 break;
4437 default: // Byte aligned
4438 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004439 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004440 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004441 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004442 }
4443
Evan Cheng25ab6902006-09-08 06:48:29 +00004444 if (AVT > MVT::i8) {
4445 if (I) {
4446 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4447 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4448 BytesLeft = I->getValue() % UBytes;
4449 } else {
4450 assert(AVT >= MVT::i32 &&
4451 "Do not use rep;stos if not at least DWORD aligned");
4452 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4453 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4454 TwoRepStos = true;
4455 }
4456 }
4457
Evan Cheng0db9fe62006-04-25 20:13:52 +00004458 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4459 InFlag);
4460 InFlag = Chain.getValue(1);
4461 } else {
4462 AVT = MVT::i8;
4463 Count = Op.getOperand(3);
4464 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4465 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004466 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004467
Evan Cheng25ab6902006-09-08 06:48:29 +00004468 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4469 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004470 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004471 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4472 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004473 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004474
Chris Lattnerd96d0722007-02-25 06:40:16 +00004475 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004476 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477 Ops.push_back(Chain);
4478 Ops.push_back(DAG.getValueType(AVT));
4479 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004480 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004481
Evan Cheng0db9fe62006-04-25 20:13:52 +00004482 if (TwoRepStos) {
4483 InFlag = Chain.getValue(1);
4484 Count = Op.getOperand(3);
4485 MVT::ValueType CVT = Count.getValueType();
4486 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004487 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4488 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4489 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004490 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004491 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004492 Ops.clear();
4493 Ops.push_back(Chain);
4494 Ops.push_back(DAG.getValueType(MVT::i8));
4495 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004496 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004497 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004498 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004499 SDOperand Value;
4500 unsigned Val = ValC->getValue() & 255;
4501 unsigned Offset = I->getValue() - BytesLeft;
4502 SDOperand DstAddr = Op.getOperand(1);
4503 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004504 if (BytesLeft >= 4) {
4505 Val = (Val << 8) | Val;
4506 Val = (Val << 16) | Val;
4507 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004508 Chain = DAG.getStore(Chain, Value,
4509 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4510 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004511 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004512 BytesLeft -= 4;
4513 Offset += 4;
4514 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004515 if (BytesLeft >= 2) {
4516 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004517 Chain = DAG.getStore(Chain, Value,
4518 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4519 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004520 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004521 BytesLeft -= 2;
4522 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004523 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004524 if (BytesLeft == 1) {
4525 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004526 Chain = DAG.getStore(Chain, Value,
4527 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4528 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004529 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004530 }
Evan Cheng386031a2006-03-24 07:29:27 +00004531 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004532
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533 return Chain;
4534}
Evan Cheng11e15b32006-04-03 20:53:28 +00004535
Rafael Espindola068317b2007-09-28 12:53:01 +00004536SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4537 SDOperand Dest,
4538 SDOperand Source,
4539 unsigned Size,
4540 unsigned Align,
4541 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004542 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004543 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004544 switch (Align & 3) {
4545 case 2: // WORD aligned
4546 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004548 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004549 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004550 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4551 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 break;
4553 default: // Byte aligned
4554 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004555 break;
4556 }
4557
Rafael Espindola068317b2007-09-28 12:53:01 +00004558 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4559 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4560 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004561
Evan Cheng0db9fe62006-04-25 20:13:52 +00004562 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004563 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4564 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004565 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004566 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004567 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004568 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004569 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004570 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 InFlag = Chain.getValue(1);
4572
Chris Lattnerd96d0722007-02-25 06:40:16 +00004573 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004574 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575 Ops.push_back(Chain);
4576 Ops.push_back(DAG.getValueType(AVT));
4577 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004578 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579
Rafael Espindola068317b2007-09-28 12:53:01 +00004580 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004581 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004582 unsigned Offset = Size - BytesLeft;
4583 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004584 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004585 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586 MVT::ValueType SrcVT = SrcAddr.getValueType();
4587 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004588 if (BytesLeft >= 4) {
4589 Value = DAG.getLoad(MVT::i32, Chain,
4590 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4591 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004592 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004593 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004594 Chain = DAG.getStore(Chain, Value,
4595 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4596 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004597 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004598 BytesLeft -= 4;
4599 Offset += 4;
4600 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 if (BytesLeft >= 2) {
4602 Value = DAG.getLoad(MVT::i16, Chain,
4603 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4604 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004605 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004606 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004607 Chain = DAG.getStore(Chain, Value,
4608 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4609 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004610 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611 BytesLeft -= 2;
4612 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004613 }
4614
Evan Cheng0db9fe62006-04-25 20:13:52 +00004615 if (BytesLeft == 1) {
4616 Value = DAG.getLoad(MVT::i8, Chain,
4617 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4618 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004619 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004621 Chain = DAG.getStore(Chain, Value,
4622 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4623 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004624 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004625 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004626 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004627
4628 return Chain;
4629}
4630
Chris Lattner27a6c732007-11-24 07:07:01 +00004631/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4632SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00004633 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00004634 SDOperand TheChain = N->getOperand(0);
4635 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004636 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004637 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4638 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4639 MVT::i64, rax.getValue(2));
4640 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004641 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004642 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00004643 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00004644 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004645
4646 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner27a6c732007-11-24 07:07:01 +00004647 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004648 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004649
Chris Lattner27a6c732007-11-24 07:07:01 +00004650 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4651 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4652 MVT::i32, eax.getValue(2));
4653 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4654 SDOperand Ops[] = { eax, edx };
4655 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4656
4657 // Use a MERGE_VALUES to return the value and chain.
4658 Ops[1] = edx.getValue(1);
4659 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4660 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004661}
4662
4663SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004664 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4665
Evan Cheng25ab6902006-09-08 06:48:29 +00004666 if (!Subtarget->is64Bit()) {
4667 // vastart just stores the address of the VarArgsFrameIndex slot into the
4668 // memory location argument.
4669 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004670 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4671 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004672 }
4673
4674 // __va_list_tag:
4675 // gp_offset (0 - 6 * 8)
4676 // fp_offset (48 - 48 + 8 * 16)
4677 // overflow_arg_area (point to parameters coming in memory).
4678 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004679 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004680 SDOperand FIN = Op.getOperand(1);
4681 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004682 SDOperand Store = DAG.getStore(Op.getOperand(0),
4683 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004684 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004685 MemOps.push_back(Store);
4686
4687 // Store fp_offset
4688 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4689 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00004690 Store = DAG.getStore(Op.getOperand(0),
4691 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004692 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004693 MemOps.push_back(Store);
4694
4695 // Store ptr to overflow_arg_area
4696 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4697 DAG.getConstant(4, getPointerTy()));
4698 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004699 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4700 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004701 MemOps.push_back(Store);
4702
4703 // Store ptr to reg_save_area.
4704 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4705 DAG.getConstant(8, getPointerTy()));
4706 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004707 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4708 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004709 MemOps.push_back(Store);
4710 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004711}
4712
Evan Chengae642192007-03-02 23:16:35 +00004713SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4714 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4715 SDOperand Chain = Op.getOperand(0);
4716 SDOperand DstPtr = Op.getOperand(1);
4717 SDOperand SrcPtr = Op.getOperand(2);
4718 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4719 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4720
4721 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4722 SrcSV->getValue(), SrcSV->getOffset());
4723 Chain = SrcPtr.getValue(1);
4724 for (unsigned i = 0; i < 3; ++i) {
4725 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4726 SrcSV->getValue(), SrcSV->getOffset());
4727 Chain = Val.getValue(1);
4728 Chain = DAG.getStore(Chain, Val, DstPtr,
4729 DstSV->getValue(), DstSV->getOffset());
4730 if (i == 2)
4731 break;
4732 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4733 DAG.getConstant(8, getPointerTy()));
4734 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4735 DAG.getConstant(8, getPointerTy()));
4736 }
4737 return Chain;
4738}
4739
Evan Cheng0db9fe62006-04-25 20:13:52 +00004740SDOperand
4741X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4742 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4743 switch (IntNo) {
4744 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004745 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746 case Intrinsic::x86_sse_comieq_ss:
4747 case Intrinsic::x86_sse_comilt_ss:
4748 case Intrinsic::x86_sse_comile_ss:
4749 case Intrinsic::x86_sse_comigt_ss:
4750 case Intrinsic::x86_sse_comige_ss:
4751 case Intrinsic::x86_sse_comineq_ss:
4752 case Intrinsic::x86_sse_ucomieq_ss:
4753 case Intrinsic::x86_sse_ucomilt_ss:
4754 case Intrinsic::x86_sse_ucomile_ss:
4755 case Intrinsic::x86_sse_ucomigt_ss:
4756 case Intrinsic::x86_sse_ucomige_ss:
4757 case Intrinsic::x86_sse_ucomineq_ss:
4758 case Intrinsic::x86_sse2_comieq_sd:
4759 case Intrinsic::x86_sse2_comilt_sd:
4760 case Intrinsic::x86_sse2_comile_sd:
4761 case Intrinsic::x86_sse2_comigt_sd:
4762 case Intrinsic::x86_sse2_comige_sd:
4763 case Intrinsic::x86_sse2_comineq_sd:
4764 case Intrinsic::x86_sse2_ucomieq_sd:
4765 case Intrinsic::x86_sse2_ucomilt_sd:
4766 case Intrinsic::x86_sse2_ucomile_sd:
4767 case Intrinsic::x86_sse2_ucomigt_sd:
4768 case Intrinsic::x86_sse2_ucomige_sd:
4769 case Intrinsic::x86_sse2_ucomineq_sd: {
4770 unsigned Opc = 0;
4771 ISD::CondCode CC = ISD::SETCC_INVALID;
4772 switch (IntNo) {
4773 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004774 case Intrinsic::x86_sse_comieq_ss:
4775 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004776 Opc = X86ISD::COMI;
4777 CC = ISD::SETEQ;
4778 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004779 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004780 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004781 Opc = X86ISD::COMI;
4782 CC = ISD::SETLT;
4783 break;
4784 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004785 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 Opc = X86ISD::COMI;
4787 CC = ISD::SETLE;
4788 break;
4789 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004790 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004791 Opc = X86ISD::COMI;
4792 CC = ISD::SETGT;
4793 break;
4794 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004795 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004796 Opc = X86ISD::COMI;
4797 CC = ISD::SETGE;
4798 break;
4799 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004800 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801 Opc = X86ISD::COMI;
4802 CC = ISD::SETNE;
4803 break;
4804 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004805 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004806 Opc = X86ISD::UCOMI;
4807 CC = ISD::SETEQ;
4808 break;
4809 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004810 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811 Opc = X86ISD::UCOMI;
4812 CC = ISD::SETLT;
4813 break;
4814 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004815 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004816 Opc = X86ISD::UCOMI;
4817 CC = ISD::SETLE;
4818 break;
4819 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004820 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821 Opc = X86ISD::UCOMI;
4822 CC = ISD::SETGT;
4823 break;
4824 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004825 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826 Opc = X86ISD::UCOMI;
4827 CC = ISD::SETGE;
4828 break;
4829 case Intrinsic::x86_sse_ucomineq_ss:
4830 case Intrinsic::x86_sse2_ucomineq_sd:
4831 Opc = X86ISD::UCOMI;
4832 CC = ISD::SETNE;
4833 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004834 }
Evan Cheng734503b2006-09-11 02:19:56 +00004835
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004837 SDOperand LHS = Op.getOperand(1);
4838 SDOperand RHS = Op.getOperand(2);
4839 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004840
Evan Chenge5f62042007-09-29 00:00:36 +00004841 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4842 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4843 DAG.getConstant(X86CC, MVT::i8), Cond);
4844 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004845 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004846 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004847}
Evan Cheng72261582005-12-20 06:22:03 +00004848
Nate Begemanbcc5f362007-01-29 22:58:52 +00004849SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4850 // Depths > 0 not supported yet!
4851 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4852 return SDOperand();
4853
4854 // Just load the return address
4855 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4856 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4857}
4858
4859SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4860 // Depths > 0 not supported yet!
4861 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4862 return SDOperand();
4863
4864 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4865 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4866 DAG.getConstant(4, getPointerTy()));
4867}
4868
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004869SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4870 SelectionDAG &DAG) {
4871 // Is not yet supported on x86-64
4872 if (Subtarget->is64Bit())
4873 return SDOperand();
4874
4875 return DAG.getConstant(8, getPointerTy());
4876}
4877
4878SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4879{
4880 assert(!Subtarget->is64Bit() &&
4881 "Lowering of eh_return builtin is not supported yet on x86-64");
4882
4883 MachineFunction &MF = DAG.getMachineFunction();
4884 SDOperand Chain = Op.getOperand(0);
4885 SDOperand Offset = Op.getOperand(1);
4886 SDOperand Handler = Op.getOperand(2);
4887
4888 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4889 getPointerTy());
4890
4891 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4892 DAG.getConstant(-4UL, getPointerTy()));
4893 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4894 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4895 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4896 MF.addLiveOut(X86::ECX);
4897
4898 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4899 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4900}
4901
Duncan Sandsb116fac2007-07-27 20:02:49 +00004902SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4903 SelectionDAG &DAG) {
4904 SDOperand Root = Op.getOperand(0);
4905 SDOperand Trmp = Op.getOperand(1); // trampoline
4906 SDOperand FPtr = Op.getOperand(2); // nested function
4907 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4908
4909 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4910
4911 if (Subtarget->is64Bit()) {
4912 return SDOperand(); // not yet supported
4913 } else {
4914 Function *Func = (Function *)
4915 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4916 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00004917 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004918
4919 switch (CC) {
4920 default:
4921 assert(0 && "Unsupported calling convention");
4922 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00004923 case CallingConv::X86_StdCall: {
4924 // Pass 'nest' parameter in ECX.
4925 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004926 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004927
4928 // Check that ECX wasn't needed by an 'inreg' parameter.
4929 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsdc024672007-11-27 13:23:08 +00004930 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00004931
4932 if (Attrs && !Func->isVarArg()) {
4933 unsigned InRegCount = 0;
4934 unsigned Idx = 1;
4935
4936 for (FunctionType::param_iterator I = FTy->param_begin(),
4937 E = FTy->param_end(); I != E; ++I, ++Idx)
4938 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4939 // FIXME: should only count parameters that are lowered to integers.
4940 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4941
4942 if (InRegCount > 2) {
4943 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4944 abort();
4945 }
4946 }
4947 break;
4948 }
4949 case CallingConv::X86_FastCall:
4950 // Pass 'nest' parameter in EAX.
4951 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004952 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004953 break;
4954 }
4955
Duncan Sandsee465742007-08-29 19:01:20 +00004956 const X86InstrInfo *TII =
4957 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4958
Duncan Sandsb116fac2007-07-27 20:02:49 +00004959 SDOperand OutChains[4];
4960 SDOperand Addr, Disp;
4961
4962 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4963 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4964
Duncan Sandsee465742007-08-29 19:01:20 +00004965 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4966 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4967 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00004968 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4969
4970 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4971 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4972 TrmpSV->getOffset() + 1, false, 1);
4973
Duncan Sandsee465742007-08-29 19:01:20 +00004974 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004975 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4976 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4977 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4978
4979 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4980 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4981 TrmpSV->getOffset() + 6, false, 1);
4982
Duncan Sandsf7331b32007-09-11 14:10:23 +00004983 SDOperand Ops[] =
4984 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4985 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004986 }
4987}
4988
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00004989SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
4990 /*
4991 The rounding mode is in bits 11:10 of FPSR, and has the following
4992 settings:
4993 00 Round to nearest
4994 01 Round to -inf
4995 10 Round to +inf
4996 11 Round to 0
4997
4998 FLT_ROUNDS, on the other hand, expects the following:
4999 -1 Undefined
5000 0 Round to 0
5001 1 Round to nearest
5002 2 Round to +inf
5003 3 Round to -inf
5004
5005 To perform the conversion, we do:
5006 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5007 */
5008
5009 MachineFunction &MF = DAG.getMachineFunction();
5010 const TargetMachine &TM = MF.getTarget();
5011 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5012 unsigned StackAlignment = TFI.getStackAlignment();
5013 MVT::ValueType VT = Op.getValueType();
5014
5015 // Save FP Control Word to stack slot
5016 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5017 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5018
5019 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5020 DAG.getEntryNode(), StackSlot);
5021
5022 // Load FP Control Word from stack slot
5023 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5024
5025 // Transform as necessary
5026 SDOperand CWD1 =
5027 DAG.getNode(ISD::SRL, MVT::i16,
5028 DAG.getNode(ISD::AND, MVT::i16,
5029 CWD, DAG.getConstant(0x800, MVT::i16)),
5030 DAG.getConstant(11, MVT::i8));
5031 SDOperand CWD2 =
5032 DAG.getNode(ISD::SRL, MVT::i16,
5033 DAG.getNode(ISD::AND, MVT::i16,
5034 CWD, DAG.getConstant(0x400, MVT::i16)),
5035 DAG.getConstant(9, MVT::i8));
5036
5037 SDOperand RetVal =
5038 DAG.getNode(ISD::AND, MVT::i16,
5039 DAG.getNode(ISD::ADD, MVT::i16,
5040 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5041 DAG.getConstant(1, MVT::i16)),
5042 DAG.getConstant(3, MVT::i16));
5043
5044
5045 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5046 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5047}
5048
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049/// LowerOperation - Provide custom lowering hooks for some operations.
5050///
5051SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5052 switch (Op.getOpcode()) {
5053 default: assert(0 && "Should not custom lower this!");
5054 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5055 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5056 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5057 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5058 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5059 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5060 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005061 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005062 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5063 case ISD::SHL_PARTS:
5064 case ISD::SRA_PARTS:
5065 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5066 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5067 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5068 case ISD::FABS: return LowerFABS(Op, DAG);
5069 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005070 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005071 case ISD::SETCC: return LowerSETCC(Op, DAG);
5072 case ISD::SELECT: return LowerSELECT(Op, DAG);
5073 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005074 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005075 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005076 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005077 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005078 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5079 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005080 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005081 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005082 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005083 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5084 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005085 case ISD::FRAME_TO_ARGS_OFFSET:
5086 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005087 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005088 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005089 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005090 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005091
5092
5093 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5094 case ISD::READCYCLECOUNTER:
5095 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005096 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005097}
5098
5099/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5100SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5101 switch (N->getOpcode()) {
5102 default: assert(0 && "Should not custom lower this!");
5103 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5104 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5105 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005106}
5107
Evan Cheng72261582005-12-20 06:22:03 +00005108const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5109 switch (Opcode) {
5110 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00005111 case X86ISD::SHLD: return "X86ISD::SHLD";
5112 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005113 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005114 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005115 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005116 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005117 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005118 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005119 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5120 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5121 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005122 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005123 case X86ISD::FST: return "X86ISD::FST";
5124 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00005125 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005126 case X86ISD::CALL: return "X86ISD::CALL";
5127 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5128 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5129 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005130 case X86ISD::COMI: return "X86ISD::COMI";
5131 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005132 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005133 case X86ISD::CMOV: return "X86ISD::CMOV";
5134 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005135 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005136 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5137 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005138 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005139 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00005140 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00005141 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00005142 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005143 case X86ISD::FMAX: return "X86ISD::FMAX";
5144 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005145 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5146 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005147 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5148 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005149 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005150 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005151 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng72261582005-12-20 06:22:03 +00005152 }
5153}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005154
Chris Lattnerc9addb72007-03-30 23:15:24 +00005155// isLegalAddressingMode - Return true if the addressing mode represented
5156// by AM is legal for this target, for a load/store of the specified type.
5157bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5158 const Type *Ty) const {
5159 // X86 supports extremely general addressing modes.
5160
5161 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5162 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5163 return false;
5164
5165 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005166 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005167 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5168 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005169
5170 // X86-64 only supports addr of globals in small code model.
5171 if (Subtarget->is64Bit()) {
5172 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5173 return false;
5174 // If lower 4G is not available, then we must use rip-relative addressing.
5175 if (AM.BaseOffs || AM.Scale > 1)
5176 return false;
5177 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005178 }
5179
5180 switch (AM.Scale) {
5181 case 0:
5182 case 1:
5183 case 2:
5184 case 4:
5185 case 8:
5186 // These scales always work.
5187 break;
5188 case 3:
5189 case 5:
5190 case 9:
5191 // These scales are formed with basereg+scalereg. Only accept if there is
5192 // no basereg yet.
5193 if (AM.HasBaseReg)
5194 return false;
5195 break;
5196 default: // Other stuff never works.
5197 return false;
5198 }
5199
5200 return true;
5201}
5202
5203
Evan Cheng2bd122c2007-10-26 01:56:11 +00005204bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5205 if (!Ty1->isInteger() || !Ty2->isInteger())
5206 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005207 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5208 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5209 if (NumBits1 <= NumBits2)
5210 return false;
5211 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005212}
5213
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005214bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5215 MVT::ValueType VT2) const {
5216 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5217 return false;
5218 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5219 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5220 if (NumBits1 <= NumBits2)
5221 return false;
5222 return Subtarget->is64Bit() || NumBits1 < 64;
5223}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005224
Evan Cheng60c07e12006-07-05 22:17:51 +00005225/// isShuffleMaskLegal - Targets can use this to indicate that they only
5226/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5227/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5228/// are assumed to be legal.
5229bool
5230X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5231 // Only do shuffles on 128-bit vector types for now.
5232 if (MVT::getSizeInBits(VT) == 64) return false;
5233 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005234 isIdentityMask(Mask.Val) ||
5235 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005236 isSplatMask(Mask.Val) ||
5237 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5238 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005239 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005240 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005241 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005242}
5243
5244bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5245 MVT::ValueType EVT,
5246 SelectionDAG &DAG) const {
5247 unsigned NumElts = BVOps.size();
5248 // Only do shuffles on 128-bit vector types for now.
5249 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5250 if (NumElts == 2) return true;
5251 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005252 return (isMOVLMask(&BVOps[0], 4) ||
5253 isCommutedMOVL(&BVOps[0], 4, true) ||
5254 isSHUFPMask(&BVOps[0], 4) ||
5255 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005256 }
5257 return false;
5258}
5259
5260//===----------------------------------------------------------------------===//
5261// X86 Scheduler Hooks
5262//===----------------------------------------------------------------------===//
5263
5264MachineBasicBlock *
5265X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5266 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005267 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005268 switch (MI->getOpcode()) {
5269 default: assert(false && "Unexpected instr type to insert");
5270 case X86::CMOV_FR32:
5271 case X86::CMOV_FR64:
5272 case X86::CMOV_V4F32:
5273 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005274 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005275 // To "insert" a SELECT_CC instruction, we actually have to insert the
5276 // diamond control-flow pattern. The incoming instruction knows the
5277 // destination vreg to set, the condition code register to branch on, the
5278 // true/false values to select between, and a branch opcode to use.
5279 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5280 ilist<MachineBasicBlock>::iterator It = BB;
5281 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005282
Evan Cheng60c07e12006-07-05 22:17:51 +00005283 // thisMBB:
5284 // ...
5285 // TrueVal = ...
5286 // cmpTY ccX, r1, r2
5287 // bCC copy1MBB
5288 // fallthrough --> copy0MBB
5289 MachineBasicBlock *thisMBB = BB;
5290 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5291 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005292 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005293 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005294 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005295 MachineFunction *F = BB->getParent();
5296 F->getBasicBlockList().insert(It, copy0MBB);
5297 F->getBasicBlockList().insert(It, sinkMBB);
5298 // Update machine-CFG edges by first adding all successors of the current
5299 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005300 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005301 e = BB->succ_end(); i != e; ++i)
5302 sinkMBB->addSuccessor(*i);
5303 // Next, remove all successors of the current block, and add the true
5304 // and fallthrough blocks as its successors.
5305 while(!BB->succ_empty())
5306 BB->removeSuccessor(BB->succ_begin());
5307 BB->addSuccessor(copy0MBB);
5308 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005309
Evan Cheng60c07e12006-07-05 22:17:51 +00005310 // copy0MBB:
5311 // %FalseValue = ...
5312 // # fallthrough to sinkMBB
5313 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005314
Evan Cheng60c07e12006-07-05 22:17:51 +00005315 // Update machine-CFG edges
5316 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005317
Evan Cheng60c07e12006-07-05 22:17:51 +00005318 // sinkMBB:
5319 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5320 // ...
5321 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005322 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005323 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5324 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5325
5326 delete MI; // The pseudo instruction is gone now.
5327 return BB;
5328 }
5329
Dale Johannesen849f2142007-07-03 00:53:03 +00005330 case X86::FP32_TO_INT16_IN_MEM:
5331 case X86::FP32_TO_INT32_IN_MEM:
5332 case X86::FP32_TO_INT64_IN_MEM:
5333 case X86::FP64_TO_INT16_IN_MEM:
5334 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005335 case X86::FP64_TO_INT64_IN_MEM:
5336 case X86::FP80_TO_INT16_IN_MEM:
5337 case X86::FP80_TO_INT32_IN_MEM:
5338 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005339 // Change the floating point control register to use "round towards zero"
5340 // mode when truncating to an integer value.
5341 MachineFunction *F = BB->getParent();
5342 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005343 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005344
5345 // Load the old value of the high byte of the control word...
5346 unsigned OldCW =
5347 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005348 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005349
5350 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005351 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5352 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005353
5354 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005355 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005356
5357 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005358 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5359 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005360
5361 // Get the X86 opcode to use.
5362 unsigned Opc;
5363 switch (MI->getOpcode()) {
5364 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005365 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5366 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5367 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5368 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5369 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5370 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005371 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5372 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5373 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005374 }
5375
5376 X86AddressMode AM;
5377 MachineOperand &Op = MI->getOperand(0);
5378 if (Op.isRegister()) {
5379 AM.BaseType = X86AddressMode::RegBase;
5380 AM.Base.Reg = Op.getReg();
5381 } else {
5382 AM.BaseType = X86AddressMode::FrameIndexBase;
5383 AM.Base.FrameIndex = Op.getFrameIndex();
5384 }
5385 Op = MI->getOperand(1);
5386 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005387 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005388 Op = MI->getOperand(2);
5389 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005390 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005391 Op = MI->getOperand(3);
5392 if (Op.isGlobalAddress()) {
5393 AM.GV = Op.getGlobal();
5394 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005395 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005396 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005397 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5398 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005399
5400 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005401 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005402
5403 delete MI; // The pseudo instruction is gone now.
5404 return BB;
5405 }
5406 }
5407}
5408
5409//===----------------------------------------------------------------------===//
5410// X86 Optimization Hooks
5411//===----------------------------------------------------------------------===//
5412
Nate Begeman368e18d2006-02-16 21:11:51 +00005413void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5414 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005415 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005416 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005417 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005418 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005419 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005420 assert((Opc >= ISD::BUILTIN_OP_END ||
5421 Opc == ISD::INTRINSIC_WO_CHAIN ||
5422 Opc == ISD::INTRINSIC_W_CHAIN ||
5423 Opc == ISD::INTRINSIC_VOID) &&
5424 "Should use MaskedValueIsZero if you don't know whether Op"
5425 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005426
Evan Cheng865f0602006-04-05 06:11:20 +00005427 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005428 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005429 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005430 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00005431 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5432 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005433 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005434}
Chris Lattner259e97c2006-01-31 19:43:35 +00005435
Evan Cheng206ee9d2006-07-07 08:33:52 +00005436/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5437/// element of the result of the vector shuffle.
5438static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5439 MVT::ValueType VT = N->getValueType(0);
5440 SDOperand PermMask = N->getOperand(2);
5441 unsigned NumElems = PermMask.getNumOperands();
5442 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5443 i %= NumElems;
5444 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5445 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005446 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005447 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5448 SDOperand Idx = PermMask.getOperand(i);
5449 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005450 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005451 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5452 }
5453 return SDOperand();
5454}
5455
5456/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5457/// node is a GlobalAddress + an offset.
5458static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005459 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005460 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005461 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5462 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5463 return true;
5464 }
Evan Cheng0085a282006-11-30 21:55:46 +00005465 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005466 SDOperand N1 = N->getOperand(0);
5467 SDOperand N2 = N->getOperand(1);
5468 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5469 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5470 if (V) {
5471 Offset += V->getSignExtended();
5472 return true;
5473 }
5474 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5475 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5476 if (V) {
5477 Offset += V->getSignExtended();
5478 return true;
5479 }
5480 }
5481 }
5482 return false;
5483}
5484
5485/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5486/// + Dist * Size.
5487static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5488 MachineFrameInfo *MFI) {
5489 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5490 return false;
5491
5492 SDOperand Loc = N->getOperand(1);
5493 SDOperand BaseLoc = Base->getOperand(1);
5494 if (Loc.getOpcode() == ISD::FrameIndex) {
5495 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5496 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005497 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5498 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005499 int FS = MFI->getObjectSize(FI);
5500 int BFS = MFI->getObjectSize(BFI);
5501 if (FS != BFS || FS != Size) return false;
5502 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5503 } else {
5504 GlobalValue *GV1 = NULL;
5505 GlobalValue *GV2 = NULL;
5506 int64_t Offset1 = 0;
5507 int64_t Offset2 = 0;
5508 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5509 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5510 if (isGA1 && isGA2 && GV1 == GV2)
5511 return Offset1 == (Offset2 + Dist*Size);
5512 }
5513
5514 return false;
5515}
5516
Evan Cheng1e60c092006-07-10 21:37:44 +00005517static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5518 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005519 GlobalValue *GV;
5520 int64_t Offset;
5521 if (isGAPlusOffset(Base, GV, Offset))
5522 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5523 else {
5524 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00005525 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005526 if (BFI < 0)
5527 // Fixed objects do not specify alignment, however the offsets are known.
5528 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5529 (MFI->getObjectOffset(BFI) % 16) == 0);
5530 else
5531 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005532 }
5533 return false;
5534}
5535
5536
5537/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5538/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5539/// if the load addresses are consecutive, non-overlapping, and in the right
5540/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005541static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5542 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005543 MachineFunction &MF = DAG.getMachineFunction();
5544 MachineFrameInfo *MFI = MF.getFrameInfo();
5545 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005546 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005547 SDOperand PermMask = N->getOperand(2);
5548 int NumElems = (int)PermMask.getNumOperands();
5549 SDNode *Base = NULL;
5550 for (int i = 0; i < NumElems; ++i) {
5551 SDOperand Idx = PermMask.getOperand(i);
5552 if (Idx.getOpcode() == ISD::UNDEF) {
5553 if (!Base) return SDOperand();
5554 } else {
5555 SDOperand Arg =
5556 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005557 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005558 return SDOperand();
5559 if (!Base)
5560 Base = Arg.Val;
5561 else if (!isConsecutiveLoad(Arg.Val, Base,
5562 i, MVT::getSizeInBits(EVT)/8,MFI))
5563 return SDOperand();
5564 }
5565 }
5566
Evan Cheng1e60c092006-07-10 21:37:44 +00005567 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005568 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005569 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005570 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005571 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005572 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005573 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5574 LD->getSrcValueOffset(), LD->isVolatile(),
5575 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005576 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005577}
5578
Chris Lattner83e6c992006-10-04 06:57:07 +00005579/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5580static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5581 const X86Subtarget *Subtarget) {
5582 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005583
Chris Lattner83e6c992006-10-04 06:57:07 +00005584 // If we have SSE[12] support, try to form min/max nodes.
5585 if (Subtarget->hasSSE2() &&
5586 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5587 if (Cond.getOpcode() == ISD::SETCC) {
5588 // Get the LHS/RHS of the select.
5589 SDOperand LHS = N->getOperand(1);
5590 SDOperand RHS = N->getOperand(2);
5591 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005592
Evan Cheng8ca29322006-11-10 21:43:37 +00005593 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005594 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005595 switch (CC) {
5596 default: break;
5597 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5598 case ISD::SETULE:
5599 case ISD::SETLE:
5600 if (!UnsafeFPMath) break;
5601 // FALL THROUGH.
5602 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5603 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005604 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005605 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005606
Chris Lattner1907a7b2006-10-05 04:11:26 +00005607 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5608 case ISD::SETUGT:
5609 case ISD::SETGT:
5610 if (!UnsafeFPMath) break;
5611 // FALL THROUGH.
5612 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5613 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005614 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005615 break;
5616 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005617 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005618 switch (CC) {
5619 default: break;
5620 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5621 case ISD::SETUGT:
5622 case ISD::SETGT:
5623 if (!UnsafeFPMath) break;
5624 // FALL THROUGH.
5625 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5626 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005627 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005628 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005629
Chris Lattner1907a7b2006-10-05 04:11:26 +00005630 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5631 case ISD::SETULE:
5632 case ISD::SETLE:
5633 if (!UnsafeFPMath) break;
5634 // FALL THROUGH.
5635 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5636 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005637 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005638 break;
5639 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005640 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005641
Evan Cheng8ca29322006-11-10 21:43:37 +00005642 if (Opcode)
5643 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005644 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005645
Chris Lattner83e6c992006-10-04 06:57:07 +00005646 }
5647
5648 return SDOperand();
5649}
5650
5651
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005652SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005653 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005654 SelectionDAG &DAG = DCI.DAG;
5655 switch (N->getOpcode()) {
5656 default: break;
5657 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00005658 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00005659 case ISD::SELECT:
5660 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005661 }
5662
5663 return SDOperand();
5664}
5665
Evan Cheng60c07e12006-07-05 22:17:51 +00005666//===----------------------------------------------------------------------===//
5667// X86 Inline Assembly Support
5668//===----------------------------------------------------------------------===//
5669
Chris Lattnerf4dff842006-07-11 02:54:03 +00005670/// getConstraintType - Given a constraint letter, return the type of
5671/// constraint it is for this target.
5672X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005673X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5674 if (Constraint.size() == 1) {
5675 switch (Constraint[0]) {
5676 case 'A':
5677 case 'r':
5678 case 'R':
5679 case 'l':
5680 case 'q':
5681 case 'Q':
5682 case 'x':
5683 case 'Y':
5684 return C_RegisterClass;
5685 default:
5686 break;
5687 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00005688 }
Chris Lattner4234f572007-03-25 02:14:49 +00005689 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00005690}
5691
Chris Lattner48884cd2007-08-25 00:47:38 +00005692/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5693/// vector. If it is invalid, don't add anything to Ops.
5694void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5695 char Constraint,
5696 std::vector<SDOperand>&Ops,
5697 SelectionDAG &DAG) {
5698 SDOperand Result(0, 0);
5699
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005700 switch (Constraint) {
5701 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00005702 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00005703 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005704 if (C->getValue() <= 31) {
5705 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5706 break;
5707 }
Devang Patel84f7fd22007-03-17 00:13:28 +00005708 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005709 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00005710 case 'N':
5711 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005712 if (C->getValue() <= 255) {
5713 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5714 break;
5715 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00005716 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005717 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00005718 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005719 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00005720 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5721 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5722 break;
5723 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005724
Chris Lattnerdc43a882007-05-03 16:52:29 +00005725 // If we are in non-pic codegen mode, we allow the address of a global (with
5726 // an optional displacement) to be used with 'i'.
5727 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5728 int64_t Offset = 0;
5729
5730 // Match either (GA) or (GA+C)
5731 if (GA) {
5732 Offset = GA->getOffset();
5733 } else if (Op.getOpcode() == ISD::ADD) {
5734 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5735 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5736 if (C && GA) {
5737 Offset = GA->getOffset()+C->getValue();
5738 } else {
5739 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5740 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5741 if (C && GA)
5742 Offset = GA->getOffset()+C->getValue();
5743 else
5744 C = 0, GA = 0;
5745 }
5746 }
5747
5748 if (GA) {
5749 // If addressing this global requires a load (e.g. in PIC mode), we can't
5750 // match.
5751 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5752 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00005753 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005754
Chris Lattnerdc43a882007-05-03 16:52:29 +00005755 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5756 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00005757 Result = Op;
5758 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005759 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005760
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005761 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00005762 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005763 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00005764 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005765
5766 if (Result.Val) {
5767 Ops.push_back(Result);
5768 return;
5769 }
5770 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005771}
5772
Chris Lattner259e97c2006-01-31 19:43:35 +00005773std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005774getRegClassForInlineAsmConstraint(const std::string &Constraint,
5775 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005776 if (Constraint.size() == 1) {
5777 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00005778 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005779 default: break; // Unknown constraint letter
5780 case 'A': // EAX/EDX
5781 if (VT == MVT::i32 || VT == MVT::i64)
5782 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5783 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005784 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5785 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005786 if (VT == MVT::i32)
5787 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5788 else if (VT == MVT::i16)
5789 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5790 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00005791 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00005792 else if (VT == MVT::i64)
5793 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
5794 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005795 }
5796 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005797
Chris Lattner1efa40f2006-02-22 00:56:39 +00005798 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005799}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005800
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005801std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005802X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5803 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00005804 // First, see if this is a constraint that directly corresponds to an LLVM
5805 // register class.
5806 if (Constraint.size() == 1) {
5807 // GCC Constraint Letters
5808 switch (Constraint[0]) {
5809 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005810 case 'r': // GENERAL_REGS
5811 case 'R': // LEGACY_REGS
5812 case 'l': // INDEX_REGS
5813 if (VT == MVT::i64 && Subtarget->is64Bit())
5814 return std::make_pair(0U, X86::GR64RegisterClass);
5815 if (VT == MVT::i32)
5816 return std::make_pair(0U, X86::GR32RegisterClass);
5817 else if (VT == MVT::i16)
5818 return std::make_pair(0U, X86::GR16RegisterClass);
5819 else if (VT == MVT::i8)
5820 return std::make_pair(0U, X86::GR8RegisterClass);
5821 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00005822 case 'y': // MMX_REGS if MMX allowed.
5823 if (!Subtarget->hasMMX()) break;
5824 return std::make_pair(0U, X86::VR64RegisterClass);
5825 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005826 case 'Y': // SSE_REGS if SSE2 allowed
5827 if (!Subtarget->hasSSE2()) break;
5828 // FALL THROUGH.
5829 case 'x': // SSE_REGS if SSE1 allowed
5830 if (!Subtarget->hasSSE1()) break;
5831
5832 switch (VT) {
5833 default: break;
5834 // Scalar SSE types.
5835 case MVT::f32:
5836 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00005837 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005838 case MVT::f64:
5839 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00005840 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005841 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00005842 case MVT::v16i8:
5843 case MVT::v8i16:
5844 case MVT::v4i32:
5845 case MVT::v2i64:
5846 case MVT::v4f32:
5847 case MVT::v2f64:
5848 return std::make_pair(0U, X86::VR128RegisterClass);
5849 }
Chris Lattnerad043e82007-04-09 05:11:28 +00005850 break;
5851 }
5852 }
5853
Chris Lattnerf76d1802006-07-31 23:26:50 +00005854 // Use the default implementation in TargetLowering to convert the register
5855 // constraint into a member of a register class.
5856 std::pair<unsigned, const TargetRegisterClass*> Res;
5857 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00005858
5859 // Not found as a standard register?
5860 if (Res.second == 0) {
5861 // GCC calls "st(0)" just plain "st".
5862 if (StringsEqualNoCase("{st}", Constraint)) {
5863 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00005864 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00005865 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005866
Chris Lattner1a60aa72006-10-31 19:42:44 +00005867 return Res;
5868 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005869
Chris Lattnerf76d1802006-07-31 23:26:50 +00005870 // Otherwise, check to see if this is a register class of the wrong value
5871 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5872 // turn into {ax},{dx}.
5873 if (Res.second->hasType(VT))
5874 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005875
Chris Lattnerf76d1802006-07-31 23:26:50 +00005876 // All of the single-register GCC register classes map their values onto
5877 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5878 // really want an 8-bit or 32-bit register, map to the appropriate register
5879 // class and return the appropriate register.
5880 if (Res.second != X86::GR16RegisterClass)
5881 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005882
Chris Lattnerf76d1802006-07-31 23:26:50 +00005883 if (VT == MVT::i8) {
5884 unsigned DestReg = 0;
5885 switch (Res.first) {
5886 default: break;
5887 case X86::AX: DestReg = X86::AL; break;
5888 case X86::DX: DestReg = X86::DL; break;
5889 case X86::CX: DestReg = X86::CL; break;
5890 case X86::BX: DestReg = X86::BL; break;
5891 }
5892 if (DestReg) {
5893 Res.first = DestReg;
5894 Res.second = Res.second = X86::GR8RegisterClass;
5895 }
5896 } else if (VT == MVT::i32) {
5897 unsigned DestReg = 0;
5898 switch (Res.first) {
5899 default: break;
5900 case X86::AX: DestReg = X86::EAX; break;
5901 case X86::DX: DestReg = X86::EDX; break;
5902 case X86::CX: DestReg = X86::ECX; break;
5903 case X86::BX: DestReg = X86::EBX; break;
5904 case X86::SI: DestReg = X86::ESI; break;
5905 case X86::DI: DestReg = X86::EDI; break;
5906 case X86::BP: DestReg = X86::EBP; break;
5907 case X86::SP: DestReg = X86::ESP; break;
5908 }
5909 if (DestReg) {
5910 Res.first = DestReg;
5911 Res.second = Res.second = X86::GR32RegisterClass;
5912 }
Evan Cheng25ab6902006-09-08 06:48:29 +00005913 } else if (VT == MVT::i64) {
5914 unsigned DestReg = 0;
5915 switch (Res.first) {
5916 default: break;
5917 case X86::AX: DestReg = X86::RAX; break;
5918 case X86::DX: DestReg = X86::RDX; break;
5919 case X86::CX: DestReg = X86::RCX; break;
5920 case X86::BX: DestReg = X86::RBX; break;
5921 case X86::SI: DestReg = X86::RSI; break;
5922 case X86::DI: DestReg = X86::RDI; break;
5923 case X86::BP: DestReg = X86::RBP; break;
5924 case X86::SP: DestReg = X86::RSP; break;
5925 }
5926 if (DestReg) {
5927 Res.first = DestReg;
5928 Res.second = Res.second = X86::GR64RegisterClass;
5929 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005930 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005931
Chris Lattnerf76d1802006-07-31 23:26:50 +00005932 return Res;
5933}