blob: d51c1d97a3eddba5bd839cb4784f1a1a44a78eb3 [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);
Evan Cheng25ab6902006-09-08 06:48:29 +0000208
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
210 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
211 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
212 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
213 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
214 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
215 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
216 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
217 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000218 if (Subtarget->is64Bit()) {
219 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
220 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
221 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
222 }
223
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000224 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000225 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000226
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227 // These should be promoted to a larger select which is supported.
228 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
229 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000230 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000231 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
232 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
233 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
234 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000235 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000236 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
237 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
238 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
239 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
240 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000241 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000242 if (Subtarget->is64Bit()) {
243 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
244 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
245 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000246 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000247 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000248 if (!Subtarget->is64Bit())
249 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
250
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000251 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000252 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000253 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000254 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000255 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000256 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
258 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
259 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
260 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
261 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
262 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000263 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000264 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
265 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
266 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000267 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000268 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
269 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000270
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000271 // Use the default ISD::LOCATION expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000272 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000273 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000274 if (!Subtarget->isTargetDarwin() &&
275 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000276 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000277 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000278
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000279 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
280 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
281 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
282 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
283 if (Subtarget->is64Bit()) {
284 // FIXME: Verify
285 setExceptionPointerRegister(X86::RAX);
286 setExceptionSelectorRegister(X86::RDX);
287 } else {
288 setExceptionPointerRegister(X86::EAX);
289 setExceptionSelectorRegister(X86::EDX);
290 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000291 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000292
Duncan Sandsf7331b32007-09-11 14:10:23 +0000293 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000294
Nate Begemanacc398c2006-01-25 18:21:52 +0000295 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
296 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000297 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000298 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000299 if (Subtarget->is64Bit())
300 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
301 else
302 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
303
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000304 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000305 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000306 if (Subtarget->is64Bit())
307 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000308 if (Subtarget->isTargetCygMing())
309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
310 else
311 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000312
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000313 if (X86ScalarSSEf64) {
314 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000315 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000316 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
317 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000318
Evan Cheng223547a2006-01-31 22:28:30 +0000319 // Use ANDPD to simulate FABS.
320 setOperationAction(ISD::FABS , MVT::f64, Custom);
321 setOperationAction(ISD::FABS , MVT::f32, Custom);
322
323 // Use XORP to simulate FNEG.
324 setOperationAction(ISD::FNEG , MVT::f64, Custom);
325 setOperationAction(ISD::FNEG , MVT::f32, Custom);
326
Evan Cheng68c47cb2007-01-05 07:55:56 +0000327 // Use ANDPD and ORPD to simulate FCOPYSIGN.
328 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
329 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
330
Evan Chengd25e9e82006-02-02 00:28:23 +0000331 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000332 setOperationAction(ISD::FSIN , MVT::f64, Expand);
333 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000334 setOperationAction(ISD::FREM , MVT::f64, Expand);
335 setOperationAction(ISD::FSIN , MVT::f32, Expand);
336 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000337 setOperationAction(ISD::FREM , MVT::f32, Expand);
338
Chris Lattnera54aa942006-01-29 06:26:08 +0000339 // Expand FP immediates into loads from the stack, except for the special
340 // cases we handle.
341 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
342 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000343 addLegalFPImmediate(APFloat(+0.0)); // xorpd
344 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000345
346 // Conversions to long double (in X87) go through memory.
347 setConvertAction(MVT::f32, MVT::f80, Expand);
348 setConvertAction(MVT::f64, MVT::f80, Expand);
349
350 // Conversions from long double (in X87) go through memory.
351 setConvertAction(MVT::f80, MVT::f32, Expand);
352 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000353 } else if (X86ScalarSSEf32) {
354 // Use SSE for f32, x87 for f64.
355 // Set up the FP register classes.
356 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
357 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
358
359 // Use ANDPS to simulate FABS.
360 setOperationAction(ISD::FABS , MVT::f32, Custom);
361
362 // Use XORP to simulate FNEG.
363 setOperationAction(ISD::FNEG , MVT::f32, Custom);
364
365 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
366
367 // Use ANDPS and ORPS to simulate FCOPYSIGN.
368 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
369 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
370
371 // We don't support sin/cos/fmod
372 setOperationAction(ISD::FSIN , MVT::f32, Expand);
373 setOperationAction(ISD::FCOS , MVT::f32, Expand);
374 setOperationAction(ISD::FREM , MVT::f32, Expand);
375
376 // Expand FP immediates into loads from the stack, except for the special
377 // cases we handle.
378 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
379 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
380 addLegalFPImmediate(APFloat(+0.0f)); // xorps
381 addLegalFPImmediate(APFloat(+0.0)); // FLD0
382 addLegalFPImmediate(APFloat(+1.0)); // FLD1
383 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
384 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
385
386 // SSE->x87 conversions go through memory.
387 setConvertAction(MVT::f32, MVT::f64, Expand);
388 setConvertAction(MVT::f32, MVT::f80, Expand);
389
390 // x87->SSE truncations need to go through memory.
391 setConvertAction(MVT::f80, MVT::f32, Expand);
392 setConvertAction(MVT::f64, MVT::f32, Expand);
393 // And x87->x87 truncations also.
394 setConvertAction(MVT::f80, MVT::f64, Expand);
395
396 if (!UnsafeFPMath) {
397 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
398 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
399 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000400 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000401 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000402 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000403 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
404 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000405
Evan Cheng68c47cb2007-01-05 07:55:56 +0000406 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000407 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000408 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
409 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000410
411 // Floating truncations need to go through memory.
412 setConvertAction(MVT::f80, MVT::f32, Expand);
413 setConvertAction(MVT::f64, MVT::f32, Expand);
414 setConvertAction(MVT::f80, MVT::f64, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000415
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000416 if (!UnsafeFPMath) {
417 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
418 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
419 }
420
Chris Lattnera54aa942006-01-29 06:26:08 +0000421 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000422 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000423 addLegalFPImmediate(APFloat(+0.0)); // FLD0
424 addLegalFPImmediate(APFloat(+1.0)); // FLD1
425 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
426 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000427 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
428 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
429 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
430 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000431 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000432
Dale Johannesen59a58732007-08-05 18:49:15 +0000433 // Long double always uses X87.
434 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000435 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
437 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000438 if (!UnsafeFPMath) {
439 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
440 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
441 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000442
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000443 // Always use a library call for pow.
444 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
445 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
446 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
447
Evan Chengd30bf012006-03-01 01:11:20 +0000448 // First set operation action for all vector types to expand. Then we
449 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000450 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
451 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000452 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
453 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000454 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000455 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000456 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000457 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000458 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
459 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
460 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000464 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000465 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000466 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000467 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000468 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
469 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
470 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000475 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
478 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000479 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohmanf0d00892007-10-12 14:09:42 +0000480 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
481 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
482 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000483 }
484
Evan Chenga88973f2006-03-22 19:22:18 +0000485 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000486 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
487 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
488 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000489 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000490
Evan Chengd30bf012006-03-01 01:11:20 +0000491 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000492
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000493 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
494 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
495 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000496 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000497
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000498 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
499 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
500 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000501 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000502
Bill Wendling74027e92007-03-15 21:24:36 +0000503 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
504 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
505
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000506 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000507 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000508 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000509 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
510 setOperationAction(ISD::AND, MVT::v2i32, Promote);
511 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
512 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000513
514 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000515 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000516 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000517 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
518 setOperationAction(ISD::OR, MVT::v2i32, Promote);
519 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
520 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000521
522 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000523 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000524 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000525 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
526 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
527 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
528 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000529
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000530 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000531 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000532 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000533 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
534 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
535 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
536 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000537
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000538 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
539 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
540 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
541 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000542
543 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
544 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
545 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000546 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000547
548 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
549 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000550 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
551 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000552 }
553
Evan Chenga88973f2006-03-22 19:22:18 +0000554 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000555 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
556
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000557 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
558 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
559 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
560 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000561 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
562 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000563 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
564 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
565 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000566 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000567 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000568 }
569
Evan Chenga88973f2006-03-22 19:22:18 +0000570 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000571 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
572 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
573 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
574 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
575 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
576
Evan Chengf7c378e2006-04-10 07:23:14 +0000577 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
578 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
579 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000580 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000581 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
582 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
583 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000584 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000585 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000586 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
587 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
588 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
589 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000590 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
591 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000592
Evan Chengf7c378e2006-04-10 07:23:14 +0000593 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000595 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000596 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
597 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
598 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000599
Evan Cheng2c3ae372006-04-12 21:21:57 +0000600 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
601 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
602 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
603 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
604 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
605 }
606 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
607 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
608 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
609 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
610 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000611 if (Subtarget->is64Bit())
612 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000613
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000614 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000615 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
616 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
617 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
618 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
619 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
620 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
621 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000622 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
623 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000624 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
625 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000626 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000627
628 // Custom lower v2i64 and v2f64 selects.
629 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000630 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000631 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000632 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000633 }
634
Evan Cheng6be2c582006-04-05 23:38:46 +0000635 // We want to custom lower some of our intrinsics.
636 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
637
Evan Cheng206ee9d2006-07-07 08:33:52 +0000638 // We have target-specific dag combine patterns for the following nodes:
639 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000640 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000641
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000642 computeRegisterProperties();
643
Evan Cheng87ed7162006-02-14 08:25:08 +0000644 // FIXME: These should be based on subtarget info. Plus, the values should
645 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000646 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
647 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
648 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000649 allowUnalignedMemoryAccesses = true; // x86 supports it!
650}
651
Chris Lattner2b02a442007-02-25 08:29:00 +0000652
Evan Chengcc415862007-11-09 01:32:10 +0000653/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
654/// jumptable.
655SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
656 SelectionDAG &DAG) const {
657 if (usesGlobalOffsetTable())
658 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
659 if (!Subtarget->isPICStyleRIPRel())
660 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
661 return Table;
662}
663
Chris Lattner2b02a442007-02-25 08:29:00 +0000664//===----------------------------------------------------------------------===//
665// Return Value Calling Convention Implementation
666//===----------------------------------------------------------------------===//
667
Chris Lattner59ed56b2007-02-28 04:55:35 +0000668#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000669
670/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
671/// exists skip possible ISD:TokenFactor.
672static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
673 if (Chain.getOpcode()==X86ISD::TAILCALL) {
674 return Chain;
675 } else if (Chain.getOpcode()==ISD::TokenFactor) {
676 if (Chain.getNumOperands() &&
677 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
678 return Chain.getOperand(0);
679 }
680 return Chain;
681}
Chris Lattner9774c912007-02-27 05:28:59 +0000682
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000683/// LowerRET - Lower an ISD::RET node.
684SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
685 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
686
Chris Lattner9774c912007-02-27 05:28:59 +0000687 SmallVector<CCValAssign, 16> RVLocs;
688 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000689 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
690 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000691 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000692
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000693 // If this is the first return lowered for this function, add the regs to the
694 // liveout set for the function.
695 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000696 for (unsigned i = 0; i != RVLocs.size(); ++i)
697 if (RVLocs[i].isRegLoc())
698 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000699 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000700 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000701
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000702 // Handle tail call return.
703 Chain = GetPossiblePreceedingTailCall(Chain);
704 if (Chain.getOpcode() == X86ISD::TAILCALL) {
705 SDOperand TailCall = Chain;
706 SDOperand TargetAddress = TailCall.getOperand(1);
707 SDOperand StackAdjustment = TailCall.getOperand(2);
708 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
709 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
710 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
711 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
712 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
713 "Expecting an global address, external symbol, or register");
714 assert( StackAdjustment.getOpcode() == ISD::Constant &&
715 "Expecting a const value");
716
717 SmallVector<SDOperand,8> Operands;
718 Operands.push_back(Chain.getOperand(0));
719 Operands.push_back(TargetAddress);
720 Operands.push_back(StackAdjustment);
721 // Copy registers used by the call. Last operand is a flag so it is not
722 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000723 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000724 Operands.push_back(Chain.getOperand(i));
725 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000726 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
727 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000728 }
729
730 // Regular return.
731 SDOperand Flag;
732
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000733 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000734 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
735 RVLocs[0].getLocReg() != X86::ST0) {
736 for (unsigned i = 0; i != RVLocs.size(); ++i) {
737 CCValAssign &VA = RVLocs[i];
738 assert(VA.isRegLoc() && "Can only return in registers!");
739 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
740 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000741 Flag = Chain.getValue(1);
742 }
743 } else {
744 // We need to handle a destination of ST0 specially, because it isn't really
745 // a register.
746 SDOperand Value = Op.getOperand(1);
747
748 // If this is an FP return with ScalarSSE, we need to move the value from
749 // an XMM register onto the fp-stack.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000750 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
751 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000752 SDOperand MemLoc;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000753
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000754 // If this is a load into a scalarsse value, don't store the loaded value
755 // back to the stack, only to reload it: just replace the scalar-sse load.
756 if (ISD::isNON_EXTLoad(Value.Val) &&
757 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
758 Chain = Value.getOperand(0);
759 MemLoc = Value.getOperand(1);
760 } else {
761 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000762 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000763 MachineFunction &MF = DAG.getMachineFunction();
764 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
765 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
766 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
767 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000768 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000769 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000770 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
771 Chain = Value.getValue(1);
772 }
773
774 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
775 SDOperand Ops[] = { Chain, Value };
776 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
777 Flag = Chain.getValue(1);
778 }
779
780 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
781 if (Flag.Val)
782 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
783 else
784 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
785}
786
787
Chris Lattner3085e152007-02-25 08:59:22 +0000788/// LowerCallResult - Lower the result values of an ISD::CALL into the
789/// appropriate copies out of appropriate physical registers. This assumes that
790/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
791/// being lowered. The returns a SDNode with the same number of values as the
792/// ISD::CALL.
793SDNode *X86TargetLowering::
794LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
795 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000796
797 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000798 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000799 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
800 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000801 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
802
Chris Lattner3085e152007-02-25 08:59:22 +0000803
Chris Lattnere32bbf62007-02-28 07:09:55 +0000804 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000805
806 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000807 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
808 for (unsigned i = 0; i != RVLocs.size(); ++i) {
809 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
810 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000811 InFlag = Chain.getValue(2);
812 ResultVals.push_back(Chain.getValue(0));
813 }
814 } else {
815 // Copies from the FP stack are special, as ST0 isn't a valid register
816 // before the fp stackifier runs.
817
818 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000819 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000820 SDOperand GROps[] = { Chain, InFlag };
821 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
822 Chain = RetVal.getValue(1);
823 InFlag = RetVal.getValue(2);
824
825 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
826 // an XMM register.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000827 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
828 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner3085e152007-02-25 08:59:22 +0000829 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
830 // shouldn't be necessary except that RFP cannot be live across
831 // multiple blocks. When stackifier is fixed, they can be uncoupled.
832 MachineFunction &MF = DAG.getMachineFunction();
833 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
834 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
835 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000836 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000837 };
838 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000839 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000840 Chain = RetVal.getValue(1);
841 }
Chris Lattner3085e152007-02-25 08:59:22 +0000842 ResultVals.push_back(RetVal);
843 }
844
845 // Merge everything together with a MERGE_VALUES node.
846 ResultVals.push_back(Chain);
847 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
848 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000849}
850
851
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000852//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000853// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000854//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000855// StdCall calling convention seems to be standard for many Windows' API
856// routines and around. It differs from C calling convention just a little:
857// callee should clean up the stack, not caller. Symbols should be also
858// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000859// For info on fast calling convention see Fast Calling Convention (tail call)
860// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000861
Evan Cheng85e38002006-04-27 05:35:28 +0000862/// AddLiveIn - This helper function adds the specified physical register to the
863/// MachineFunction as a live in value. It also creates a corresponding virtual
864/// register for it.
865static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000866 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000867 assert(RC->contains(PReg) && "Not the correct regclass!");
868 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
869 MF.addLiveIn(PReg, VReg);
870 return VReg;
871}
872
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000873// align stack arguments according to platform alignment needed for tail calls
874unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
875
Rafael Espindola7effac52007-09-14 15:48:13 +0000876SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
877 const CCValAssign &VA,
878 MachineFrameInfo *MFI,
879 SDOperand Root, unsigned i) {
880 // Create the nodes corresponding to a load from this parameter slot.
881 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
882 VA.getLocMemOffset());
883 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
884
885 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
886
887 if (Flags & ISD::ParamFlags::ByVal)
888 return FIN;
889 else
890 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
891}
892
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000893SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
894 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000895 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000896 MachineFunction &MF = DAG.getMachineFunction();
897 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000898 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000899 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000900 unsigned CC = MF.getFunction()->getCallingConv();
Chris Lattner638402b2007-02-28 07:00:42 +0000901 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000902 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000903 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +0000904 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000905 // Check for possible tail call calling convention.
906 if (CC == CallingConv::Fast && PerformTailCallOpt)
907 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
908 else
909 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
910
Chris Lattnerf39f7712007-02-28 05:46:49 +0000911 SmallVector<SDOperand, 8> ArgValues;
912 unsigned LastVal = ~0U;
913 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
914 CCValAssign &VA = ArgLocs[i];
915 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
916 // places.
917 assert(VA.getValNo() != LastVal &&
918 "Don't support value assigned to multiple locs yet");
919 LastVal = VA.getValNo();
920
921 if (VA.isRegLoc()) {
922 MVT::ValueType RegVT = VA.getLocVT();
923 TargetRegisterClass *RC;
924 if (RegVT == MVT::i32)
925 RC = X86::GR32RegisterClass;
926 else {
927 assert(MVT::isVector(RegVT));
928 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000929 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000930
Chris Lattner82932a52007-03-02 05:12:29 +0000931 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
932 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000933
934 // If this is an 8 or 16-bit value, it is really passed promoted to 32
935 // bits. Insert an assert[sz]ext to capture this, then truncate to the
936 // right size.
937 if (VA.getLocInfo() == CCValAssign::SExt)
938 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
939 DAG.getValueType(VA.getValVT()));
940 else if (VA.getLocInfo() == CCValAssign::ZExt)
941 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
942 DAG.getValueType(VA.getValVT()));
943
944 if (VA.getLocInfo() != CCValAssign::Full)
945 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
946
947 ArgValues.push_back(ArgValue);
948 } else {
949 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +0000950 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +0000951 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000952 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000953
954 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000955 // align stack specially for tail calls
956 if (CC==CallingConv::Fast)
957 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +0000958
Evan Cheng25caf632006-05-23 21:06:34 +0000959 ArgValues.push_back(Root);
960
Evan Cheng1bc78042006-04-26 01:20:17 +0000961 // If the function takes variable number of arguments, make a frame index for
962 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000963 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000964 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000965
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000966 // Tail call calling convention (CallingConv::Fast) does not support varargs.
967 assert( !(isVarArg && CC == CallingConv::Fast) &&
968 "CallingConv::Fast does not support varargs.");
969
970 if (isStdCall && !isVarArg &&
971 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000972 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000973 BytesCallerReserves = 0;
974 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000975 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000976
977 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000978 if (NumArgs &&
979 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000980 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000981 BytesToPopOnReturn = 4;
982
983 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000984 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000985
Evan Cheng25ab6902006-09-08 06:48:29 +0000986 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng25caf632006-05-23 21:06:34 +0000987
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000988 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
989 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000990
Evan Cheng25caf632006-05-23 21:06:34 +0000991 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000992 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000993 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000994}
995
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000996SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000997 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000998 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000999 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001000 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001001 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001002
Chris Lattner638402b2007-02-28 07:00:42 +00001003 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001004 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001005 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001006 if(CC==CallingConv::Fast && PerformTailCallOpt)
1007 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1008 else
1009 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001010
Chris Lattner423c5f42007-02-28 05:31:48 +00001011 // Get a count of how many bytes are to be pushed on the stack.
1012 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001013 if (CC==CallingConv::Fast)
1014 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001015
Evan Cheng32fe1032006-05-25 00:59:30 +00001016 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001017
Chris Lattner5a88b832007-02-25 07:10:00 +00001018 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1019 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001020
Chris Lattner423c5f42007-02-28 05:31:48 +00001021 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001022
1023 // Walk the register/memloc assignments, inserting copies/loads.
1024 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1025 CCValAssign &VA = ArgLocs[i];
1026 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001027
Chris Lattner423c5f42007-02-28 05:31:48 +00001028 // Promote the value if needed.
1029 switch (VA.getLocInfo()) {
1030 default: assert(0 && "Unknown loc info!");
1031 case CCValAssign::Full: break;
1032 case CCValAssign::SExt:
1033 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1034 break;
1035 case CCValAssign::ZExt:
1036 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1037 break;
1038 case CCValAssign::AExt:
1039 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1040 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001041 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001042
1043 if (VA.isRegLoc()) {
1044 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1045 } else {
1046 assert(VA.isMemLoc());
1047 if (StackPtr.Val == 0)
1048 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001049
1050 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1051 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001052 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001053 }
1054
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001055 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001056 bool isSRet = NumOps &&
1057 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001058 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001059
Evan Cheng32fe1032006-05-25 00:59:30 +00001060 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001061 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1062 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001063
Evan Cheng347d5f72006-04-28 21:29:37 +00001064 // Build a sequence of copy-to-reg nodes chained together with token chain
1065 // and flag operands which copy the outgoing args into registers.
1066 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001067 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1068 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1069 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001070 InFlag = Chain.getValue(1);
1071 }
1072
Evan Chengf4684712007-02-21 21:18:14 +00001073 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1074 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +00001075 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1076 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001077 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1078 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1079 InFlag);
1080 InFlag = Chain.getValue(1);
1081 }
1082
Evan Cheng32fe1032006-05-25 00:59:30 +00001083 // If the callee is a GlobalAddress node (quite common, every direct call is)
1084 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001085 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001086 // We should use extra load for direct calls to dllimported functions in
1087 // non-JIT mode.
1088 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1089 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001090 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1091 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001092 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1093
Chris Lattnerd96d0722007-02-25 06:40:16 +00001094 // Returns a chain & a flag for retval copy to use.
1095 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001096 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001097 Ops.push_back(Chain);
1098 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001099
1100 // Add argument registers to the end of the list so that they are known live
1101 // into the call.
1102 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001103 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001104 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +00001105
1106 // Add an implicit use GOT pointer in EBX.
1107 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT())
1109 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001110
Evan Cheng347d5f72006-04-28 21:29:37 +00001111 if (InFlag.Val)
1112 Ops.push_back(InFlag);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001113
1114 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001115 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001116
Chris Lattner2d297092006-05-23 18:50:38 +00001117 // Create the CALLSEQ_END node.
1118 unsigned NumBytesForCalleeToPush = 0;
1119
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001120 if (CC == CallingConv::X86_StdCall ||
1121 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Chris Lattner09c75a42007-02-25 09:06:15 +00001122 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001123 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +00001124 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001125 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001126 assert(!(isVarArg && CC==CallingConv::Fast) &&
1127 "CallingConv::Fast does not support varargs.");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001128 } else {
1129 // If this is is a call to a struct-return function, the callee
1130 // pops the hidden struct pointer, so we have to push it back.
1131 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001132 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001133 }
1134
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001135 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001136 Ops.clear();
1137 Ops.push_back(Chain);
1138 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +00001139 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001140 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001141 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner3085e152007-02-25 08:59:22 +00001142 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001143
Chris Lattner3085e152007-02-25 08:59:22 +00001144 // Handle result values, copying them out of physregs into vregs that we
1145 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +00001146 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001147}
1148
Evan Cheng25ab6902006-09-08 06:48:29 +00001149
1150//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001151// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001152//===----------------------------------------------------------------------===//
1153//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001154// The X86 'fastcall' calling convention passes up to two integer arguments in
1155// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1156// and requires that the callee pop its arguments off the stack (allowing proper
1157// tail calls), and has the same return value conventions as C calling convs.
1158//
1159// This calling convention always arranges for the callee pop value to be 8n+4
1160// bytes, which is needed for tail recursion elimination and stack alignment
1161// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +00001162SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +00001163X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001164 MachineFunction &MF = DAG.getMachineFunction();
1165 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001166 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +00001167 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001168
Chris Lattner638402b2007-02-28 07:00:42 +00001169 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001170 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001171 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1172 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001173 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001174
1175 SmallVector<SDOperand, 8> ArgValues;
1176 unsigned LastVal = ~0U;
1177 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1178 CCValAssign &VA = ArgLocs[i];
1179 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1180 // places.
1181 assert(VA.getValNo() != LastVal &&
1182 "Don't support value assigned to multiple locs yet");
1183 LastVal = VA.getValNo();
1184
1185 if (VA.isRegLoc()) {
1186 MVT::ValueType RegVT = VA.getLocVT();
1187 TargetRegisterClass *RC;
1188 if (RegVT == MVT::i32)
1189 RC = X86::GR32RegisterClass;
1190 else {
1191 assert(MVT::isVector(RegVT));
1192 RC = X86::VR128RegisterClass;
1193 }
1194
Chris Lattner82932a52007-03-02 05:12:29 +00001195 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1196 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001197
1198 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1199 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1200 // right size.
1201 if (VA.getLocInfo() == CCValAssign::SExt)
1202 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1203 DAG.getValueType(VA.getValVT()));
1204 else if (VA.getLocInfo() == CCValAssign::ZExt)
1205 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1206 DAG.getValueType(VA.getValVT()));
1207
1208 if (VA.getLocInfo() != CCValAssign::Full)
1209 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1210
1211 ArgValues.push_back(ArgValue);
1212 } else {
1213 assert(VA.isMemLoc());
Rafael Espindola1242d282007-09-21 14:55:38 +00001214 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfc664c12007-02-28 06:21:19 +00001215 }
1216 }
1217
Evan Cheng25caf632006-05-23 21:06:34 +00001218 ArgValues.push_back(Root);
1219
Chris Lattnerfc664c12007-02-28 06:21:19 +00001220 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001221
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001222 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001223 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001224 // arguments and the arguments after the retaddr has been pushed are
1225 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001226 if ((StackSize & 7) == 0)
1227 StackSize += 4;
1228 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001229
1230 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001231 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001232 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001233 BytesCallerReserves = 0;
1234
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001235 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1236 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001237
Evan Cheng25caf632006-05-23 21:06:34 +00001238 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001239 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001240 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001241}
1242
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001243SDOperand
1244X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1245 const SDOperand &StackPtr,
1246 const CCValAssign &VA,
1247 SDOperand Chain,
1248 SDOperand Arg) {
1249 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1250 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1251 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1252 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1253 if (Flags & ISD::ParamFlags::ByVal) {
1254 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1255 ISD::ParamFlags::ByValAlignOffs);
1256
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001257 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1258 ISD::ParamFlags::ByValSizeOffs;
1259
1260 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1261 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001262 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001263
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001264 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1265 AlwaysInline);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001266 } else {
1267 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1268 }
1269}
1270
Chris Lattnere87e1152006-09-26 03:57:53 +00001271SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001272 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001273 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001274 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001275 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001276 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001277
Chris Lattner638402b2007-02-28 07:00:42 +00001278 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001279 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001280 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001281 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001282
1283 // Get a count of how many bytes are to be pushed on the stack.
1284 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001285
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001286 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001287 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001288 // arguments and the arguments after the retaddr has been pushed are
1289 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001290 if ((NumBytes & 7) == 0)
1291 NumBytes += 4;
1292 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001293
Chris Lattner94dd2922006-02-13 09:00:43 +00001294 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001295
Chris Lattner5a88b832007-02-25 07:10:00 +00001296 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1297 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001298
1299 SDOperand StackPtr;
1300
1301 // Walk the register/memloc assignments, inserting copies/loads.
1302 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1303 CCValAssign &VA = ArgLocs[i];
1304 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1305
1306 // Promote the value if needed.
1307 switch (VA.getLocInfo()) {
1308 default: assert(0 && "Unknown loc info!");
1309 case CCValAssign::Full: break;
1310 case CCValAssign::SExt:
1311 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001312 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001313 case CCValAssign::ZExt:
1314 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1315 break;
1316 case CCValAssign::AExt:
1317 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1318 break;
1319 }
1320
1321 if (VA.isRegLoc()) {
1322 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1323 } else {
1324 assert(VA.isMemLoc());
1325 if (StackPtr.Val == 0)
1326 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001327
1328 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1329 Arg));
Evan Cheng32fe1032006-05-25 00:59:30 +00001330 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001331 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001332
Evan Cheng32fe1032006-05-25 00:59:30 +00001333 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001334 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1335 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001336
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001337 // Build a sequence of copy-to-reg nodes chained together with token chain
1338 // and flag operands which copy the outgoing args into registers.
1339 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001340 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1341 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1342 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001343 InFlag = Chain.getValue(1);
1344 }
1345
Evan Cheng32fe1032006-05-25 00:59:30 +00001346 // If the callee is a GlobalAddress node (quite common, every direct call is)
1347 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001348 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001349 // We should use extra load for direct calls to dllimported functions in
1350 // non-JIT mode.
1351 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1352 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001353 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1354 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001355 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1356
Evan Chengf4684712007-02-21 21:18:14 +00001357 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1358 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001359 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1360 Subtarget->isPICStyleGOT()) {
1361 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1362 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1363 InFlag);
1364 InFlag = Chain.getValue(1);
1365 }
1366
Chris Lattnerd96d0722007-02-25 06:40:16 +00001367 // Returns a chain & a flag for retval copy to use.
1368 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001369 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001370 Ops.push_back(Chain);
1371 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001372
1373 // Add argument registers to the end of the list so that they are known live
1374 // into the call.
1375 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001376 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001377 RegsToPass[i].second.getValueType()));
1378
Evan Chengf4684712007-02-21 21:18:14 +00001379 // Add an implicit use GOT pointer in EBX.
1380 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1381 Subtarget->isPICStyleGOT())
1382 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1383
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001384 if (InFlag.Val)
1385 Ops.push_back(InFlag);
1386
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001387 assert(isTailCall==false && "no tail call here");
1388 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001389 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001390 InFlag = Chain.getValue(1);
1391
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001392 // Returns a flag for retval copy to use.
1393 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001394 Ops.clear();
1395 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001396 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1397 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001398 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001399 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001400 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001401
Chris Lattner339b4392007-02-25 09:10:05 +00001402 // Handle result values, copying them out of physregs into vregs that we
1403 // return.
1404 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001405}
1406
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001407//===----------------------------------------------------------------------===//
1408// Fast Calling Convention (tail call) implementation
1409//===----------------------------------------------------------------------===//
1410
1411// Like std call, callee cleans arguments, convention except that ECX is
1412// reserved for storing the tail called function address. Only 2 registers are
1413// free for argument passing (inreg). Tail call optimization is performed
1414// provided:
1415// * tailcallopt is enabled
1416// * caller/callee are fastcc
1417// * elf/pic is disabled OR
1418// * elf/pic enabled + callee is in module + callee has
1419// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001420// To keep the stack aligned according to platform abi the function
1421// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1422// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001423// If a tail called function callee has more arguments than the caller the
1424// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001425// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001426// original REtADDR, but before the saved framepointer or the spilled registers
1427// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1428// stack layout:
1429// arg1
1430// arg2
1431// RETADDR
1432// [ new RETADDR
1433// move area ]
1434// (possible EBP)
1435// ESI
1436// EDI
1437// local1 ..
1438
1439/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1440/// for a 16 byte align requirement.
1441unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1442 SelectionDAG& DAG) {
1443 if (PerformTailCallOpt) {
1444 MachineFunction &MF = DAG.getMachineFunction();
1445 const TargetMachine &TM = MF.getTarget();
1446 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1447 unsigned StackAlignment = TFI.getStackAlignment();
1448 uint64_t AlignMask = StackAlignment - 1;
1449 int64_t Offset = StackSize;
1450 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1451 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1452 // Number smaller than 12 so just add the difference.
1453 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1454 } else {
1455 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1456 Offset = ((~AlignMask) & Offset) + StackAlignment +
1457 (StackAlignment-SlotSize);
1458 }
1459 StackSize = Offset;
1460 }
1461 return StackSize;
1462}
1463
1464/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001465/// following the call is a return. A function is eligible if caller/callee
1466/// calling conventions match, currently only fastcc supports tail calls, and
1467/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001468bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1469 SDOperand Ret,
1470 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001471 if (!PerformTailCallOpt)
1472 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001473
1474 // Check whether CALL node immediatly preceeds the RET node and whether the
1475 // return uses the result of the node or is a void return.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001476 unsigned NumOps = Ret.getNumOperands();
1477 if ((NumOps == 1 &&
1478 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1479 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Chenga9d641e2007-11-02 17:45:40 +00001480 (NumOps > 1 &&
Evan Cheng9df7dc52007-11-02 01:26:22 +00001481 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1482 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001483 MachineFunction &MF = DAG.getMachineFunction();
1484 unsigned CallerCC = MF.getFunction()->getCallingConv();
1485 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1486 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1487 SDOperand Callee = Call.getOperand(4);
1488 // On elf/pic %ebx needs to be livein.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001489 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1490 !Subtarget->isPICStyleGOT())
1491 return true;
1492
1493 // Can only do local tail calls with PIC.
1494 GlobalValue * GV = 0;
1495 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1496 if(G != 0 &&
1497 (GV = G->getGlobal()) &&
1498 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1499 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001500 }
1501 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001502
1503 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001504}
1505
1506SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1507 SelectionDAG &DAG,
1508 unsigned CC) {
1509 SDOperand Chain = Op.getOperand(0);
1510 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1511 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1512 SDOperand Callee = Op.getOperand(4);
1513 bool is64Bit = Subtarget->is64Bit();
1514
1515 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1516
1517 // Analyze operands of the call, assigning locations to each operand.
1518 SmallVector<CCValAssign, 16> ArgLocs;
1519 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1520 if (is64Bit)
1521 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1522 else
1523 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1524
1525
1526 // Lower arguments at fp - stackoffset + fpdiff.
1527 MachineFunction &MF = DAG.getMachineFunction();
1528
1529 unsigned NumBytesToBePushed =
1530 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1531
1532 unsigned NumBytesCallerPushed =
1533 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1534 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1535
1536 // Set the delta of movement of the returnaddr stackslot.
1537 // But only set if delta is greater than previous delta.
1538 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1539 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1540
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001541 Chain = DAG.
1542 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1543
1544 // Adjust the Return address stack slot.
1545 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001546 if (FPDiff) {
1547 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001548 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1549 // Load the "old" Return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001550 RetAddrFrIdx =
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001551 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1552 // Calculate the new stack slot for the return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001553 int SlotSize = is64Bit ? 8 : 4;
1554 int NewReturnAddrFI =
1555 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001556 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1557 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001558 }
1559
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001560 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1561 SmallVector<SDOperand, 8> MemOpChains;
1562 SmallVector<SDOperand, 8> MemOpChains2;
1563 SDOperand FramePtr, StackPtr;
1564 SDOperand PtrOff;
1565 SDOperand FIN;
1566 int FI = 0;
1567
1568 // Walk the register/memloc assignments, inserting copies/loads. Lower
1569 // arguments first to the stack slot where they would normally - in case of a
1570 // normal function call - be.
1571 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1572 CCValAssign &VA = ArgLocs[i];
1573 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1574
1575 // Promote the value if needed.
1576 switch (VA.getLocInfo()) {
1577 default: assert(0 && "Unknown loc info!");
1578 case CCValAssign::Full: break;
1579 case CCValAssign::SExt:
1580 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1581 break;
1582 case CCValAssign::ZExt:
1583 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1584 break;
1585 case CCValAssign::AExt:
1586 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1587 break;
1588 }
1589
1590 if (VA.isRegLoc()) {
1591 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1592 } else {
1593 assert(VA.isMemLoc());
1594 if (StackPtr.Val == 0)
1595 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1596
1597 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1598 Arg));
1599 }
1600 }
1601
1602 if (!MemOpChains.empty())
1603 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1604 &MemOpChains[0], MemOpChains.size());
1605
1606 // Build a sequence of copy-to-reg nodes chained together with token chain
1607 // and flag operands which copy the outgoing args into registers.
1608 SDOperand InFlag;
1609 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1610 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1611 InFlag);
1612 InFlag = Chain.getValue(1);
1613 }
1614 InFlag = SDOperand();
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001615
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001616 // Copy from stack slots to stack slot of a tail called function. This needs
1617 // to be done because if we would lower the arguments directly to their real
1618 // stack slot we might end up overwriting each other.
1619 // TODO: To make this more efficient (sometimes saving a store/load) we could
1620 // analyse the arguments and emit this store/load/store sequence only for
1621 // arguments which would be overwritten otherwise.
1622 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1623 CCValAssign &VA = ArgLocs[i];
1624 if (!VA.isRegLoc()) {
1625 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1626 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1627
1628 // Get source stack slot.
1629 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1630 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1631 // Create frame index.
1632 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1633 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1634 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1635 FIN = DAG.getFrameIndex(FI, MVT::i32);
1636 if (Flags & ISD::ParamFlags::ByVal) {
1637 // Copy relative to framepointer.
1638 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1639 ISD::ParamFlags::ByValAlignOffs);
1640
1641 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1642 ISD::ParamFlags::ByValSizeOffs;
1643
1644 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1645 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer38ada862007-11-10 10:48:01 +00001646 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1647
1648 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1649 AlignNode,AlwaysInline));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001650 } else {
1651 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1652 // Store relative to framepointer.
1653 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1654 }
1655 }
1656 }
1657
1658 if (!MemOpChains2.empty())
1659 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1660 &MemOpChains2[0], MemOpChains.size());
1661
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001662 // Store the return address to the appropriate stack slot.
1663 if (FPDiff)
1664 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1665
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001666 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1667 // GOT pointer.
1668 // Does not work with tail call since ebx is not restored correctly by
1669 // tailcaller. TODO: at least for x86 - verify for x86-64
1670
1671 // If the callee is a GlobalAddress node (quite common, every direct call is)
1672 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1673 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1674 // We should use extra load for direct calls to dllimported functions in
1675 // non-JIT mode.
1676 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1677 getTargetMachine(), true))
1678 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1679 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1680 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1681 else {
1682 assert(Callee.getOpcode() == ISD::LOAD &&
1683 "Function destination must be loaded into virtual register");
1684 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1685
1686 Chain = DAG.getCopyToReg(Chain,
1687 DAG.getRegister(Opc, getPointerTy()) ,
1688 Callee,InFlag);
1689 Callee = DAG.getRegister(Opc, getPointerTy());
1690 // Add register as live out.
1691 DAG.getMachineFunction().addLiveOut(Opc);
1692 }
1693
1694 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1695 SmallVector<SDOperand, 8> Ops;
1696
1697 Ops.push_back(Chain);
1698 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1699 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1700 if (InFlag.Val)
1701 Ops.push_back(InFlag);
1702 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1703 InFlag = Chain.getValue(1);
1704
1705 // Returns a chain & a flag for retval copy to use.
1706 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1707 Ops.clear();
1708 Ops.push_back(Chain);
1709 Ops.push_back(Callee);
1710 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1711 // Add argument registers to the end of the list so that they are known live
1712 // into the call.
1713 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1714 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1715 RegsToPass[i].second.getValueType()));
1716 if (InFlag.Val)
1717 Ops.push_back(InFlag);
1718 assert(InFlag.Val &&
1719 "Flag must be set. Depend on flag being set in LowerRET");
1720 Chain = DAG.getNode(X86ISD::TAILCALL,
1721 Op.Val->getVTList(), &Ops[0], Ops.size());
1722
1723 return SDOperand(Chain.Val, Op.ResNo);
1724}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001725
1726//===----------------------------------------------------------------------===//
1727// X86-64 C Calling Convention implementation
1728//===----------------------------------------------------------------------===//
1729
1730SDOperand
1731X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001732 MachineFunction &MF = DAG.getMachineFunction();
1733 MachineFrameInfo *MFI = MF.getFrameInfo();
1734 SDOperand Root = Op.getOperand(0);
1735 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001736 unsigned CC= MF.getFunction()->getCallingConv();
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001737
1738 static const unsigned GPR64ArgRegs[] = {
1739 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1740 };
1741 static const unsigned XMMArgRegs[] = {
1742 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1743 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1744 };
1745
Chris Lattner638402b2007-02-28 07:00:42 +00001746
1747 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001748 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001749 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +00001750 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001751 if (CC == CallingConv::Fast && PerformTailCallOpt)
1752 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1753 else
1754 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001755
1756 SmallVector<SDOperand, 8> ArgValues;
1757 unsigned LastVal = ~0U;
1758 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1759 CCValAssign &VA = ArgLocs[i];
1760 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1761 // places.
1762 assert(VA.getValNo() != LastVal &&
1763 "Don't support value assigned to multiple locs yet");
1764 LastVal = VA.getValNo();
1765
1766 if (VA.isRegLoc()) {
1767 MVT::ValueType RegVT = VA.getLocVT();
1768 TargetRegisterClass *RC;
1769 if (RegVT == MVT::i32)
1770 RC = X86::GR32RegisterClass;
1771 else if (RegVT == MVT::i64)
1772 RC = X86::GR64RegisterClass;
1773 else if (RegVT == MVT::f32)
1774 RC = X86::FR32RegisterClass;
1775 else if (RegVT == MVT::f64)
1776 RC = X86::FR64RegisterClass;
1777 else {
1778 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001779 if (MVT::getSizeInBits(RegVT) == 64) {
1780 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1781 RegVT = MVT::i64;
1782 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001783 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001784 }
Chris Lattner82932a52007-03-02 05:12:29 +00001785
1786 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1787 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001788
1789 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1790 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1791 // right size.
1792 if (VA.getLocInfo() == CCValAssign::SExt)
1793 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1794 DAG.getValueType(VA.getValVT()));
1795 else if (VA.getLocInfo() == CCValAssign::ZExt)
1796 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1797 DAG.getValueType(VA.getValVT()));
1798
1799 if (VA.getLocInfo() != CCValAssign::Full)
1800 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1801
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001802 // Handle MMX values passed in GPRs.
1803 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1804 MVT::getSizeInBits(RegVT) == 64)
1805 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1806
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001807 ArgValues.push_back(ArgValue);
1808 } else {
1809 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001810 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001811 }
1812 }
1813
1814 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001815 if (CC==CallingConv::Fast)
1816 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001817
1818 // If the function takes variable number of arguments, make a frame index for
1819 // the start of the first vararg value... for expansion of llvm.va_start.
1820 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001821 assert(CC!=CallingConv::Fast
1822 && "Var arg not supported with calling convention fastcc");
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001823 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1824 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1825
1826 // For X86-64, if there are vararg parameters that are passed via
1827 // registers, then we must store them to their spots on the stack so they
1828 // may be loaded by deferencing the result of va_next.
1829 VarArgsGPOffset = NumIntRegs * 8;
1830 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1831 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1832 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1833
1834 // Store the integer parameter registers.
1835 SmallVector<SDOperand, 8> MemOps;
1836 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1837 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1838 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1839 for (; NumIntRegs != 6; ++NumIntRegs) {
1840 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1841 X86::GR64RegisterClass);
1842 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1843 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1844 MemOps.push_back(Store);
1845 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1846 DAG.getConstant(8, getPointerTy()));
1847 }
1848
1849 // Now store the XMM (fp + vector) parameter registers.
1850 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1851 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1852 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1853 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1854 X86::VR128RegisterClass);
1855 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1856 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1857 MemOps.push_back(Store);
1858 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1859 DAG.getConstant(16, getPointerTy()));
1860 }
1861 if (!MemOps.empty())
1862 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1863 &MemOps[0], MemOps.size());
1864 }
1865
1866 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001867 // Tail call convention (fastcc) needs callee pop.
Evan Cheng36446012007-10-14 10:09:39 +00001868 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001869 BytesToPopOnReturn = StackSize; // Callee pops everything.
1870 BytesCallerReserves = 0;
1871 } else {
1872 BytesToPopOnReturn = 0; // Callee pops nothing.
1873 BytesCallerReserves = StackSize;
1874 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001875 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1876 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1877
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001878 // Return the new list of results.
1879 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1880 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1881}
1882
1883SDOperand
1884X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1885 unsigned CC) {
1886 SDOperand Chain = Op.getOperand(0);
1887 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001888 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001889
1890 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001891 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001892 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng36446012007-10-14 10:09:39 +00001893 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001894 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1895 else
1896 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001897
1898 // Get a count of how many bytes are to be pushed on the stack.
1899 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001900 if (CC == CallingConv::Fast)
1901 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1902
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001903 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1904
1905 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1906 SmallVector<SDOperand, 8> MemOpChains;
1907
1908 SDOperand StackPtr;
1909
1910 // Walk the register/memloc assignments, inserting copies/loads.
1911 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1912 CCValAssign &VA = ArgLocs[i];
1913 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1914
1915 // Promote the value if needed.
1916 switch (VA.getLocInfo()) {
1917 default: assert(0 && "Unknown loc info!");
1918 case CCValAssign::Full: break;
1919 case CCValAssign::SExt:
1920 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1921 break;
1922 case CCValAssign::ZExt:
1923 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1924 break;
1925 case CCValAssign::AExt:
1926 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1927 break;
1928 }
1929
1930 if (VA.isRegLoc()) {
1931 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1932 } else {
1933 assert(VA.isMemLoc());
1934 if (StackPtr.Val == 0)
1935 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola21485be2007-08-20 15:18:24 +00001936
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001937 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1938 Arg));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001939 }
1940 }
1941
1942 if (!MemOpChains.empty())
1943 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1944 &MemOpChains[0], MemOpChains.size());
1945
1946 // Build a sequence of copy-to-reg nodes chained together with token chain
1947 // and flag operands which copy the outgoing args into registers.
1948 SDOperand InFlag;
1949 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1950 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1951 InFlag);
1952 InFlag = Chain.getValue(1);
1953 }
1954
1955 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001956 assert ( CallingConv::Fast != CC &&
1957 "Var args not supported with calling convention fastcc");
1958
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001959 // From AMD64 ABI document:
1960 // For calls that may call functions that use varargs or stdargs
1961 // (prototype-less calls or calls to functions containing ellipsis (...) in
1962 // the declaration) %al is used as hidden argument to specify the number
1963 // of SSE registers used. The contents of %al do not need to match exactly
1964 // the number of registers, but must be an ubound on the number of SSE
1965 // registers used and is in the range 0 - 8 inclusive.
1966
1967 // Count the number of XMM registers allocated.
1968 static const unsigned XMMArgRegs[] = {
1969 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1970 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1971 };
1972 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1973
1974 Chain = DAG.getCopyToReg(Chain, X86::AL,
1975 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1976 InFlag = Chain.getValue(1);
1977 }
1978
1979 // If the callee is a GlobalAddress node (quite common, every direct call is)
1980 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1981 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1982 // We should use extra load for direct calls to dllimported functions in
1983 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001984 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001985 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1986 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001987 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1988 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001989 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1990 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001991
1992 // Returns a chain & a flag for retval copy to use.
1993 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1994 SmallVector<SDOperand, 8> Ops;
1995 Ops.push_back(Chain);
1996 Ops.push_back(Callee);
1997
1998 // Add argument registers to the end of the list so that they are known live
1999 // into the call.
2000 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2001 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2002 RegsToPass[i].second.getValueType()));
2003
2004 if (InFlag.Val)
2005 Ops.push_back(InFlag);
2006
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002007 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002008 NodeTys, &Ops[0], Ops.size());
2009 InFlag = Chain.getValue(1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002010 int NumBytesForCalleeToPush = 0;
Evan Cheng36446012007-10-14 10:09:39 +00002011 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002012 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002013 } else {
2014 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2015 }
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002016 // Returns a flag for retval copy to use.
2017 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2018 Ops.clear();
2019 Ops.push_back(Chain);
2020 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002021 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002022 Ops.push_back(InFlag);
2023 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2024 InFlag = Chain.getValue(1);
2025
2026 // Handle result values, copying them out of physregs into vregs that we
2027 // return.
2028 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2029}
2030
2031
2032//===----------------------------------------------------------------------===//
2033// Other Lowering Hooks
2034//===----------------------------------------------------------------------===//
2035
2036
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002037SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002038 MachineFunction &MF = DAG.getMachineFunction();
2039 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2040 int ReturnAddrIndex = FuncInfo->getRAIndex();
2041
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002042 if (ReturnAddrIndex == 0) {
2043 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00002044 if (Subtarget->is64Bit())
2045 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2046 else
2047 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002048
2049 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002050 }
2051
Evan Cheng25ab6902006-09-08 06:48:29 +00002052 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002053}
2054
2055
2056
Evan Cheng6dfa9992006-01-30 23:41:35 +00002057/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2058/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00002059/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2060/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00002061static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00002062 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2063 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00002064 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002065 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002066 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2067 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2068 // X > -1 -> X == 0, jump !sign.
2069 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00002070 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002071 return true;
2072 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2073 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00002074 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002075 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00002076 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2077 // X < 1 -> X <= 0
2078 RHS = DAG.getConstant(0, RHS.getValueType());
2079 X86CC = X86::COND_LE;
2080 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002081 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002082 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002083
Evan Chengd9558e02006-01-06 00:43:03 +00002084 switch (SetCCOpcode) {
2085 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002086 case ISD::SETEQ: X86CC = X86::COND_E; break;
2087 case ISD::SETGT: X86CC = X86::COND_G; break;
2088 case ISD::SETGE: X86CC = X86::COND_GE; break;
2089 case ISD::SETLT: X86CC = X86::COND_L; break;
2090 case ISD::SETLE: X86CC = X86::COND_LE; break;
2091 case ISD::SETNE: X86CC = X86::COND_NE; break;
2092 case ISD::SETULT: X86CC = X86::COND_B; break;
2093 case ISD::SETUGT: X86CC = X86::COND_A; break;
2094 case ISD::SETULE: X86CC = X86::COND_BE; break;
2095 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002096 }
2097 } else {
2098 // On a floating point condition, the flags are set as follows:
2099 // ZF PF CF op
2100 // 0 | 0 | 0 | X > Y
2101 // 0 | 0 | 1 | X < Y
2102 // 1 | 0 | 0 | X == Y
2103 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00002104 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00002105 switch (SetCCOpcode) {
2106 default: break;
2107 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002108 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002109 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002110 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002111 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002112 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002113 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002114 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002115 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002116 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002117 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002118 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002119 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002120 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002121 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002122 case ISD::SETNE: X86CC = X86::COND_NE; break;
2123 case ISD::SETUO: X86CC = X86::COND_P; break;
2124 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002125 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002126 if (Flip)
2127 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00002128 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00002129
Chris Lattner7fbe9722006-10-20 17:42:20 +00002130 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002131}
2132
Evan Cheng4a460802006-01-11 00:33:36 +00002133/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2134/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002135/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002136static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002137 switch (X86CC) {
2138 default:
2139 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002140 case X86::COND_B:
2141 case X86::COND_BE:
2142 case X86::COND_E:
2143 case X86::COND_P:
2144 case X86::COND_A:
2145 case X86::COND_AE:
2146 case X86::COND_NE:
2147 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002148 return true;
2149 }
2150}
2151
Evan Cheng5ced1d82006-04-06 23:23:56 +00002152/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002153/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00002154static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2155 if (Op.getOpcode() == ISD::UNDEF)
2156 return true;
2157
2158 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002159 return (Val >= Low && Val < Hi);
2160}
2161
2162/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2163/// true if Op is undef or if its value equal to the specified value.
2164static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2165 if (Op.getOpcode() == ISD::UNDEF)
2166 return true;
2167 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002168}
2169
Evan Cheng0188ecb2006-03-22 18:59:22 +00002170/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2171/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2172bool X86::isPSHUFDMask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002175 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002176 return false;
2177
2178 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002179 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002180 SDOperand Arg = N->getOperand(i);
2181 if (Arg.getOpcode() == ISD::UNDEF) continue;
2182 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002183 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002184 return false;
2185 }
2186
2187 return true;
2188}
2189
2190/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002191/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002192bool X86::isPSHUFHWMask(SDNode *N) {
2193 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2194
2195 if (N->getNumOperands() != 8)
2196 return false;
2197
2198 // Lower quadword copied in order.
2199 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002200 SDOperand Arg = N->getOperand(i);
2201 if (Arg.getOpcode() == ISD::UNDEF) continue;
2202 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2203 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002204 return false;
2205 }
2206
2207 // Upper quadword shuffled.
2208 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002209 SDOperand Arg = N->getOperand(i);
2210 if (Arg.getOpcode() == ISD::UNDEF) continue;
2211 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2212 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002213 if (Val < 4 || Val > 7)
2214 return false;
2215 }
2216
2217 return true;
2218}
2219
2220/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002221/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002222bool X86::isPSHUFLWMask(SDNode *N) {
2223 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2224
2225 if (N->getNumOperands() != 8)
2226 return false;
2227
2228 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002229 for (unsigned i = 4; i != 8; ++i)
2230 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002231 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002232
2233 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002234 for (unsigned i = 0; i != 4; ++i)
2235 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002236 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002237
2238 return true;
2239}
2240
Evan Cheng14aed5e2006-03-24 01:18:28 +00002241/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2242/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00002243static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002244 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002245
Evan Cheng39623da2006-04-20 08:58:49 +00002246 unsigned Half = NumElems / 2;
2247 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002248 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002249 return false;
2250 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002251 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002252 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002253
2254 return true;
2255}
2256
Evan Cheng39623da2006-04-20 08:58:49 +00002257bool X86::isSHUFPMask(SDNode *N) {
2258 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002259 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002260}
2261
Evan Cheng213d2cf2007-05-17 18:45:50 +00002262/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002263/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2264/// half elements to come from vector 1 (which would equal the dest.) and
2265/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002266static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2267 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002268
Chris Lattner5a88b832007-02-25 07:10:00 +00002269 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002270 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002271 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002272 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002273 for (unsigned i = Half; i < NumOps; ++i)
2274 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002275 return false;
2276 return true;
2277}
2278
2279static bool isCommutedSHUFP(SDNode *N) {
2280 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002281 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002282}
2283
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002284/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2285/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2286bool X86::isMOVHLPSMask(SDNode *N) {
2287 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2288
Evan Cheng2064a2b2006-03-28 06:50:32 +00002289 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002290 return false;
2291
Evan Cheng2064a2b2006-03-28 06:50:32 +00002292 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002293 return isUndefOrEqual(N->getOperand(0), 6) &&
2294 isUndefOrEqual(N->getOperand(1), 7) &&
2295 isUndefOrEqual(N->getOperand(2), 2) &&
2296 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002297}
2298
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002299/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2300/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2301/// <2, 3, 2, 3>
2302bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2303 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2304
2305 if (N->getNumOperands() != 4)
2306 return false;
2307
2308 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2309 return isUndefOrEqual(N->getOperand(0), 2) &&
2310 isUndefOrEqual(N->getOperand(1), 3) &&
2311 isUndefOrEqual(N->getOperand(2), 2) &&
2312 isUndefOrEqual(N->getOperand(3), 3);
2313}
2314
Evan Cheng5ced1d82006-04-06 23:23:56 +00002315/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2316/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2317bool X86::isMOVLPMask(SDNode *N) {
2318 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2319
2320 unsigned NumElems = N->getNumOperands();
2321 if (NumElems != 2 && NumElems != 4)
2322 return false;
2323
Evan Chengc5cdff22006-04-07 21:53:05 +00002324 for (unsigned i = 0; i < NumElems/2; ++i)
2325 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2326 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002327
Evan Chengc5cdff22006-04-07 21:53:05 +00002328 for (unsigned i = NumElems/2; i < NumElems; ++i)
2329 if (!isUndefOrEqual(N->getOperand(i), i))
2330 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002331
2332 return true;
2333}
2334
2335/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002336/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2337/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002338bool X86::isMOVHPMask(SDNode *N) {
2339 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2340
2341 unsigned NumElems = N->getNumOperands();
2342 if (NumElems != 2 && NumElems != 4)
2343 return false;
2344
Evan Chengc5cdff22006-04-07 21:53:05 +00002345 for (unsigned i = 0; i < NumElems/2; ++i)
2346 if (!isUndefOrEqual(N->getOperand(i), i))
2347 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002348
2349 for (unsigned i = 0; i < NumElems/2; ++i) {
2350 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002351 if (!isUndefOrEqual(Arg, i + NumElems))
2352 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002353 }
2354
2355 return true;
2356}
2357
Evan Cheng0038e592006-03-28 00:39:58 +00002358/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2359/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002360bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2361 bool V2IsSplat = false) {
2362 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002363 return false;
2364
Chris Lattner5a88b832007-02-25 07:10:00 +00002365 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2366 SDOperand BitI = Elts[i];
2367 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002368 if (!isUndefOrEqual(BitI, j))
2369 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002370 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002371 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002372 return false;
2373 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002374 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002375 return false;
2376 }
Evan Cheng0038e592006-03-28 00:39:58 +00002377 }
2378
2379 return true;
2380}
2381
Evan Cheng39623da2006-04-20 08:58:49 +00002382bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2383 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002384 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002385}
2386
Evan Cheng4fcb9222006-03-28 02:43:26 +00002387/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2388/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002389bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2390 bool V2IsSplat = false) {
2391 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002392 return false;
2393
Chris Lattner5a88b832007-02-25 07:10:00 +00002394 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2395 SDOperand BitI = Elts[i];
2396 SDOperand BitI1 = Elts[i+1];
2397 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002398 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002399 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002400 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002401 return false;
2402 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002403 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002404 return false;
2405 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002406 }
2407
2408 return true;
2409}
2410
Evan Cheng39623da2006-04-20 08:58:49 +00002411bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2412 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002413 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002414}
2415
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002416/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2417/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2418/// <0, 0, 1, 1>
2419bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2420 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2421
2422 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002423 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002424 return false;
2425
2426 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2427 SDOperand BitI = N->getOperand(i);
2428 SDOperand BitI1 = N->getOperand(i+1);
2429
Evan Chengc5cdff22006-04-07 21:53:05 +00002430 if (!isUndefOrEqual(BitI, j))
2431 return false;
2432 if (!isUndefOrEqual(BitI1, j))
2433 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002434 }
2435
2436 return true;
2437}
2438
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002439/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2440/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2441/// <2, 2, 3, 3>
2442bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2443 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2444
2445 unsigned NumElems = N->getNumOperands();
2446 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2447 return false;
2448
2449 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2450 SDOperand BitI = N->getOperand(i);
2451 SDOperand BitI1 = N->getOperand(i + 1);
2452
2453 if (!isUndefOrEqual(BitI, j))
2454 return false;
2455 if (!isUndefOrEqual(BitI1, j))
2456 return false;
2457 }
2458
2459 return true;
2460}
2461
Evan Cheng017dcc62006-04-21 01:05:10 +00002462/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2463/// specifies a shuffle of elements that is suitable for input to MOVSS,
2464/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002465static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2466 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002467 return false;
2468
Chris Lattner5a88b832007-02-25 07:10:00 +00002469 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002470 return false;
2471
Chris Lattner5a88b832007-02-25 07:10:00 +00002472 for (unsigned i = 1; i < NumElts; ++i) {
2473 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002474 return false;
2475 }
2476
2477 return true;
2478}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002479
Evan Cheng017dcc62006-04-21 01:05:10 +00002480bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002481 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002482 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002483}
2484
Evan Cheng017dcc62006-04-21 01:05:10 +00002485/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2486/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002487/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002488static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2489 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002490 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002491 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002492 return false;
2493
2494 if (!isUndefOrEqual(Ops[0], 0))
2495 return false;
2496
Chris Lattner5a88b832007-02-25 07:10:00 +00002497 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002498 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002499 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2500 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2501 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002502 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002503 }
2504
2505 return true;
2506}
2507
Evan Cheng8cf723d2006-09-08 01:50:06 +00002508static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2509 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002510 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002511 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2512 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002513}
2514
Evan Chengd9539472006-04-14 21:59:03 +00002515/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2516/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2517bool X86::isMOVSHDUPMask(SDNode *N) {
2518 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2519
2520 if (N->getNumOperands() != 4)
2521 return false;
2522
2523 // Expect 1, 1, 3, 3
2524 for (unsigned i = 0; i < 2; ++i) {
2525 SDOperand Arg = N->getOperand(i);
2526 if (Arg.getOpcode() == ISD::UNDEF) continue;
2527 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2528 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2529 if (Val != 1) return false;
2530 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002531
2532 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002533 for (unsigned i = 2; i < 4; ++i) {
2534 SDOperand Arg = N->getOperand(i);
2535 if (Arg.getOpcode() == ISD::UNDEF) continue;
2536 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2537 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2538 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002539 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002540 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002541
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002542 // Don't use movshdup if it can be done with a shufps.
2543 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002544}
2545
2546/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2547/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2548bool X86::isMOVSLDUPMask(SDNode *N) {
2549 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2550
2551 if (N->getNumOperands() != 4)
2552 return false;
2553
2554 // Expect 0, 0, 2, 2
2555 for (unsigned i = 0; i < 2; ++i) {
2556 SDOperand Arg = N->getOperand(i);
2557 if (Arg.getOpcode() == ISD::UNDEF) continue;
2558 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2559 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2560 if (Val != 0) return false;
2561 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002562
2563 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002564 for (unsigned i = 2; i < 4; ++i) {
2565 SDOperand Arg = N->getOperand(i);
2566 if (Arg.getOpcode() == ISD::UNDEF) continue;
2567 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2568 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2569 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002570 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002571 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002572
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002573 // Don't use movshdup if it can be done with a shufps.
2574 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002575}
2576
Evan Cheng49892af2007-06-19 00:02:56 +00002577/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2578/// specifies a identity operation on the LHS or RHS.
2579static bool isIdentityMask(SDNode *N, bool RHS = false) {
2580 unsigned NumElems = N->getNumOperands();
2581 for (unsigned i = 0; i < NumElems; ++i)
2582 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2583 return false;
2584 return true;
2585}
2586
Evan Chengb9df0ca2006-03-22 02:53:00 +00002587/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2588/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002589static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002590 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2591
Evan Chengb9df0ca2006-03-22 02:53:00 +00002592 // This is a splat operation if each element of the permute is the same, and
2593 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002594 unsigned NumElems = N->getNumOperands();
2595 SDOperand ElementBase;
2596 unsigned i = 0;
2597 for (; i != NumElems; ++i) {
2598 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002599 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002600 ElementBase = Elt;
2601 break;
2602 }
2603 }
2604
2605 if (!ElementBase.Val)
2606 return false;
2607
2608 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002609 SDOperand Arg = N->getOperand(i);
2610 if (Arg.getOpcode() == ISD::UNDEF) continue;
2611 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002612 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002613 }
2614
2615 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002616 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002617}
2618
Evan Chengc575ca22006-04-17 20:43:08 +00002619/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2620/// a splat of a single element and it's a 2 or 4 element mask.
2621bool X86::isSplatMask(SDNode *N) {
2622 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2623
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002624 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002625 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2626 return false;
2627 return ::isSplatMask(N);
2628}
2629
Evan Chengf686d9b2006-10-27 21:08:32 +00002630/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2631/// specifies a splat of zero element.
2632bool X86::isSplatLoMask(SDNode *N) {
2633 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2634
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002635 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002636 if (!isUndefOrEqual(N->getOperand(i), 0))
2637 return false;
2638 return true;
2639}
2640
Evan Cheng63d33002006-03-22 08:01:21 +00002641/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2642/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2643/// instructions.
2644unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002645 unsigned NumOperands = N->getNumOperands();
2646 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2647 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002648 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002649 unsigned Val = 0;
2650 SDOperand Arg = N->getOperand(NumOperands-i-1);
2651 if (Arg.getOpcode() != ISD::UNDEF)
2652 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002653 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002654 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002655 if (i != NumOperands - 1)
2656 Mask <<= Shift;
2657 }
Evan Cheng63d33002006-03-22 08:01:21 +00002658
2659 return Mask;
2660}
2661
Evan Cheng506d3df2006-03-29 23:07:14 +00002662/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2663/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2664/// instructions.
2665unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2666 unsigned Mask = 0;
2667 // 8 nodes, but we only care about the last 4.
2668 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002669 unsigned Val = 0;
2670 SDOperand Arg = N->getOperand(i);
2671 if (Arg.getOpcode() != ISD::UNDEF)
2672 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002673 Mask |= (Val - 4);
2674 if (i != 4)
2675 Mask <<= 2;
2676 }
2677
2678 return Mask;
2679}
2680
2681/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2682/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2683/// instructions.
2684unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2685 unsigned Mask = 0;
2686 // 8 nodes, but we only care about the first 4.
2687 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002688 unsigned Val = 0;
2689 SDOperand Arg = N->getOperand(i);
2690 if (Arg.getOpcode() != ISD::UNDEF)
2691 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002692 Mask |= Val;
2693 if (i != 0)
2694 Mask <<= 2;
2695 }
2696
2697 return Mask;
2698}
2699
Evan Chengc21a0532006-04-05 01:47:37 +00002700/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2701/// specifies a 8 element shuffle that can be broken into a pair of
2702/// PSHUFHW and PSHUFLW.
2703static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2704 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2705
2706 if (N->getNumOperands() != 8)
2707 return false;
2708
2709 // Lower quadword shuffled.
2710 for (unsigned i = 0; i != 4; ++i) {
2711 SDOperand Arg = N->getOperand(i);
2712 if (Arg.getOpcode() == ISD::UNDEF) continue;
2713 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2714 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2715 if (Val > 4)
2716 return false;
2717 }
2718
2719 // Upper quadword shuffled.
2720 for (unsigned i = 4; i != 8; ++i) {
2721 SDOperand Arg = N->getOperand(i);
2722 if (Arg.getOpcode() == ISD::UNDEF) continue;
2723 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2724 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2725 if (Val < 4 || Val > 7)
2726 return false;
2727 }
2728
2729 return true;
2730}
2731
Evan Cheng5ced1d82006-04-06 23:23:56 +00002732/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2733/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002734static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2735 SDOperand &V2, SDOperand &Mask,
2736 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002737 MVT::ValueType VT = Op.getValueType();
2738 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002739 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002740 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002741 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002742
2743 for (unsigned i = 0; i != NumElems; ++i) {
2744 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002745 if (Arg.getOpcode() == ISD::UNDEF) {
2746 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2747 continue;
2748 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002749 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2750 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2751 if (Val < NumElems)
2752 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2753 else
2754 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2755 }
2756
Evan Cheng9eca5e82006-10-25 21:49:50 +00002757 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002758 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002759 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002760}
2761
Evan Cheng533a0aa2006-04-19 20:35:22 +00002762/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2763/// match movhlps. The lower half elements should come from upper half of
2764/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002765/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002766static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2767 unsigned NumElems = Mask->getNumOperands();
2768 if (NumElems != 4)
2769 return false;
2770 for (unsigned i = 0, e = 2; i != e; ++i)
2771 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2772 return false;
2773 for (unsigned i = 2; i != 4; ++i)
2774 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2775 return false;
2776 return true;
2777}
2778
Evan Cheng5ced1d82006-04-06 23:23:56 +00002779/// isScalarLoadToVector - Returns true if the node is a scalar load that
2780/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002781static inline bool isScalarLoadToVector(SDNode *N) {
2782 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2783 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002784 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002785 }
2786 return false;
2787}
2788
Evan Cheng533a0aa2006-04-19 20:35:22 +00002789/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2790/// match movlp{s|d}. The lower half elements should come from lower half of
2791/// V1 (and in order), and the upper half elements should come from the upper
2792/// half of V2 (and in order). And since V1 will become the source of the
2793/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002794static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002795 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002796 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002797 // Is V2 is a vector load, don't do this transformation. We will try to use
2798 // load folding shufps op.
2799 if (ISD::isNON_EXTLoad(V2))
2800 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002801
Evan Cheng533a0aa2006-04-19 20:35:22 +00002802 unsigned NumElems = Mask->getNumOperands();
2803 if (NumElems != 2 && NumElems != 4)
2804 return false;
2805 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2806 if (!isUndefOrEqual(Mask->getOperand(i), i))
2807 return false;
2808 for (unsigned i = NumElems/2; i != NumElems; ++i)
2809 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2810 return false;
2811 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002812}
2813
Evan Cheng39623da2006-04-20 08:58:49 +00002814/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2815/// all the same.
2816static bool isSplatVector(SDNode *N) {
2817 if (N->getOpcode() != ISD::BUILD_VECTOR)
2818 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002819
Evan Cheng39623da2006-04-20 08:58:49 +00002820 SDOperand SplatValue = N->getOperand(0);
2821 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2822 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002823 return false;
2824 return true;
2825}
2826
Evan Cheng8cf723d2006-09-08 01:50:06 +00002827/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2828/// to an undef.
2829static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002830 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002831 return false;
2832
2833 SDOperand V1 = N->getOperand(0);
2834 SDOperand V2 = N->getOperand(1);
2835 SDOperand Mask = N->getOperand(2);
2836 unsigned NumElems = Mask.getNumOperands();
2837 for (unsigned i = 0; i != NumElems; ++i) {
2838 SDOperand Arg = Mask.getOperand(i);
2839 if (Arg.getOpcode() != ISD::UNDEF) {
2840 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2841 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2842 return false;
2843 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2844 return false;
2845 }
2846 }
2847 return true;
2848}
2849
Evan Cheng213d2cf2007-05-17 18:45:50 +00002850/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2851/// constant +0.0.
2852static inline bool isZeroNode(SDOperand Elt) {
2853 return ((isa<ConstantSDNode>(Elt) &&
2854 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2855 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002856 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002857}
2858
2859/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2860/// to an zero vector.
2861static bool isZeroShuffle(SDNode *N) {
2862 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2863 return false;
2864
2865 SDOperand V1 = N->getOperand(0);
2866 SDOperand V2 = N->getOperand(1);
2867 SDOperand Mask = N->getOperand(2);
2868 unsigned NumElems = Mask.getNumOperands();
2869 for (unsigned i = 0; i != NumElems; ++i) {
2870 SDOperand Arg = Mask.getOperand(i);
2871 if (Arg.getOpcode() != ISD::UNDEF) {
2872 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2873 if (Idx < NumElems) {
2874 unsigned Opc = V1.Val->getOpcode();
2875 if (Opc == ISD::UNDEF)
2876 continue;
2877 if (Opc != ISD::BUILD_VECTOR ||
2878 !isZeroNode(V1.Val->getOperand(Idx)))
2879 return false;
2880 } else if (Idx >= NumElems) {
2881 unsigned Opc = V2.Val->getOpcode();
2882 if (Opc == ISD::UNDEF)
2883 continue;
2884 if (Opc != ISD::BUILD_VECTOR ||
2885 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2886 return false;
2887 }
2888 }
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");
Dan Gohman237898a2007-05-24 14:33:05 +00002897 unsigned NumElems = MVT::getVectorNumElements(VT);
Dan Gohman51eaa862007-06-14 22:58:02 +00002898 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002899 bool isFP = MVT::isFloatingPoint(EVT);
2900 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2901 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2902 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2903}
2904
Evan Cheng39623da2006-04-20 08:58:49 +00002905/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2906/// that point to V2 points to its first element.
2907static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2908 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2909
2910 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002911 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002912 unsigned NumElems = Mask.getNumOperands();
2913 for (unsigned i = 0; i != NumElems; ++i) {
2914 SDOperand Arg = Mask.getOperand(i);
2915 if (Arg.getOpcode() != ISD::UNDEF) {
2916 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2917 if (Val > NumElems) {
2918 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2919 Changed = true;
2920 }
2921 }
2922 MaskVec.push_back(Arg);
2923 }
2924
2925 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002926 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2927 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002928 return Mask;
2929}
2930
Evan Cheng017dcc62006-04-21 01:05:10 +00002931/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2932/// operation of specified width.
2933static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002934 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002935 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002936
Chris Lattner5a88b832007-02-25 07:10:00 +00002937 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002938 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2939 for (unsigned i = 1; i != NumElems; ++i)
2940 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002941 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002942}
2943
Evan Chengc575ca22006-04-17 20:43:08 +00002944/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2945/// of specified width.
2946static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2947 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002948 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002949 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002950 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2951 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2952 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2953 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002954 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002955}
2956
Evan Cheng39623da2006-04-20 08:58:49 +00002957/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2958/// of specified width.
2959static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2960 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002961 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002962 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002963 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002964 for (unsigned i = 0; i != Half; ++i) {
2965 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2966 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2967 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002968 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002969}
2970
Evan Chengc575ca22006-04-17 20:43:08 +00002971/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2972///
2973static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2974 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002975 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002976 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002977 unsigned NumElems = Mask.getNumOperands();
2978 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002979 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002980 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002981 NumElems >>= 1;
2982 }
2983 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2984
2985 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002986 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002987 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002988 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002989 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2990}
2991
Evan Chengba05f722006-04-21 23:03:30 +00002992/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Evan Cheng213d2cf2007-05-17 18:45:50 +00002993/// vector of zero or undef vector.
Evan Chengba05f722006-04-21 23:03:30 +00002994static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002995 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002996 bool isZero, SelectionDAG &DAG) {
2997 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002998 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002999 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Evan Cheng017dcc62006-04-21 01:05:10 +00003000 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003001 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Cheng017dcc62006-04-21 01:05:10 +00003002 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003003 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3004 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00003005 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003006}
3007
Evan Chengc78d3b42006-04-24 18:01:45 +00003008/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3009///
3010static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3011 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003012 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003013 if (NumNonZero > 8)
3014 return SDOperand();
3015
3016 SDOperand V(0, 0);
3017 bool First = true;
3018 for (unsigned i = 0; i < 16; ++i) {
3019 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3020 if (ThisIsNonZero && First) {
3021 if (NumZero)
3022 V = getZeroVector(MVT::v8i16, DAG);
3023 else
3024 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3025 First = false;
3026 }
3027
3028 if ((i & 1) != 0) {
3029 SDOperand ThisElt(0, 0), LastElt(0, 0);
3030 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3031 if (LastIsNonZero) {
3032 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3033 }
3034 if (ThisIsNonZero) {
3035 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3036 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3037 ThisElt, DAG.getConstant(8, MVT::i8));
3038 if (LastIsNonZero)
3039 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3040 } else
3041 ThisElt = LastElt;
3042
3043 if (ThisElt.Val)
3044 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00003045 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003046 }
3047 }
3048
3049 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3050}
3051
Bill Wendlinga348c562007-03-22 18:42:45 +00003052/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003053///
3054static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3055 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003056 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003057 if (NumNonZero > 4)
3058 return SDOperand();
3059
3060 SDOperand V(0, 0);
3061 bool First = true;
3062 for (unsigned i = 0; i < 8; ++i) {
3063 bool isNonZero = (NonZeros & (1 << i)) != 0;
3064 if (isNonZero) {
3065 if (First) {
3066 if (NumZero)
3067 V = getZeroVector(MVT::v8i16, DAG);
3068 else
3069 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3070 First = false;
3071 }
3072 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00003073 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003074 }
3075 }
3076
3077 return V;
3078}
3079
Evan Cheng0db9fe62006-04-25 20:13:52 +00003080SDOperand
3081X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3082 // All zero's are handled with pxor.
3083 if (ISD::isBuildVectorAllZeros(Op.Val))
3084 return Op;
3085
3086 // All one's are handled with pcmpeqd.
3087 if (ISD::isBuildVectorAllOnes(Op.Val))
3088 return Op;
3089
3090 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003091 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003092 unsigned EVTBits = MVT::getSizeInBits(EVT);
3093
3094 unsigned NumElems = Op.getNumOperands();
3095 unsigned NumZero = 0;
3096 unsigned NumNonZero = 0;
3097 unsigned NonZeros = 0;
Dan Gohmana3941172007-07-24 22:55:08 +00003098 unsigned NumNonZeroImms = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003099 std::set<SDOperand> Values;
3100 for (unsigned i = 0; i < NumElems; ++i) {
3101 SDOperand Elt = Op.getOperand(i);
3102 if (Elt.getOpcode() != ISD::UNDEF) {
3103 Values.insert(Elt);
3104 if (isZeroNode(Elt))
3105 NumZero++;
3106 else {
3107 NonZeros |= (1 << i);
3108 NumNonZero++;
Dan Gohmana3941172007-07-24 22:55:08 +00003109 if (Elt.getOpcode() == ISD::Constant ||
3110 Elt.getOpcode() == ISD::ConstantFP)
3111 NumNonZeroImms++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003112 }
3113 }
3114 }
3115
Dan Gohman7f321562007-06-25 16:23:39 +00003116 if (NumNonZero == 0) {
3117 if (NumZero == 0)
3118 // All undef vector. Return an UNDEF.
3119 return DAG.getNode(ISD::UNDEF, VT);
3120 else
3121 // A mix of zero and undef. Return a zero vector.
3122 return getZeroVector(VT, DAG);
3123 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003124
3125 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3126 if (Values.size() == 1)
3127 return SDOperand();
3128
3129 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003130 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003131 unsigned Idx = CountTrailingZeros_32(NonZeros);
3132 SDOperand Item = Op.getOperand(Idx);
3133 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3134 if (Idx == 0)
3135 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3136 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3137 NumZero > 0, DAG);
3138
3139 if (EVTBits == 32) {
3140 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3141 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3142 DAG);
3143 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003144 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003145 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003146 for (unsigned i = 0; i < NumElems; i++)
3147 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003148 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3149 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003150 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3151 DAG.getNode(ISD::UNDEF, VT), Mask);
3152 }
3153 }
3154
Dan Gohmana3941172007-07-24 22:55:08 +00003155 // A vector full of immediates; various special cases are already
3156 // handled, so this is best done with a single constant-pool load.
3157 if (NumNonZero == NumNonZeroImms)
3158 return SDOperand();
3159
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003160 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003161 if (EVTBits == 64)
3162 return SDOperand();
3163
3164 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003165 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003166 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3167 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003168 if (V.Val) return V;
3169 }
3170
Bill Wendling826f36f2007-03-28 00:57:11 +00003171 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003172 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3173 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003174 if (V.Val) return V;
3175 }
3176
3177 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003178 SmallVector<SDOperand, 8> V;
3179 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003180 if (NumElems == 4 && NumZero > 0) {
3181 for (unsigned i = 0; i < 4; ++i) {
3182 bool isZero = !(NonZeros & (1 << i));
3183 if (isZero)
3184 V[i] = getZeroVector(VT, DAG);
3185 else
3186 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3187 }
3188
3189 for (unsigned i = 0; i < 2; ++i) {
3190 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3191 default: break;
3192 case 0:
3193 V[i] = V[i*2]; // Must be a zero vector.
3194 break;
3195 case 1:
3196 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3197 getMOVLMask(NumElems, DAG));
3198 break;
3199 case 2:
3200 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3201 getMOVLMask(NumElems, DAG));
3202 break;
3203 case 3:
3204 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3205 getUnpacklMask(NumElems, DAG));
3206 break;
3207 }
3208 }
3209
Evan Cheng069287d2006-05-16 07:21:53 +00003210 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003211 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003212 // FIXME: we can do the same for v4f32 case when we know both parts of
3213 // the lower half come from scalar_to_vector (loadf32). We should do
3214 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003215 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003216 return V[0];
3217 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003218 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003219 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003220 bool Reverse = (NonZeros & 0x3) == 2;
3221 for (unsigned i = 0; i < 2; ++i)
3222 if (Reverse)
3223 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3224 else
3225 MaskVec.push_back(DAG.getConstant(i, EVT));
3226 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3227 for (unsigned i = 0; i < 2; ++i)
3228 if (Reverse)
3229 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3230 else
3231 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003232 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3233 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003234 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3235 }
3236
3237 if (Values.size() > 2) {
3238 // Expand into a number of unpckl*.
3239 // e.g. for v4f32
3240 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3241 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3242 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3243 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3244 for (unsigned i = 0; i < NumElems; ++i)
3245 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3246 NumElems >>= 1;
3247 while (NumElems != 0) {
3248 for (unsigned i = 0; i < NumElems; ++i)
3249 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3250 UnpckMask);
3251 NumElems >>= 1;
3252 }
3253 return V[0];
3254 }
3255
3256 return SDOperand();
3257}
3258
3259SDOperand
3260X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3261 SDOperand V1 = Op.getOperand(0);
3262 SDOperand V2 = Op.getOperand(1);
3263 SDOperand PermMask = Op.getOperand(2);
3264 MVT::ValueType VT = Op.getValueType();
3265 unsigned NumElems = PermMask.getNumOperands();
3266 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3267 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003268 bool V1IsSplat = false;
3269 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003270
Evan Cheng8cf723d2006-09-08 01:50:06 +00003271 if (isUndefShuffle(Op.Val))
3272 return DAG.getNode(ISD::UNDEF, VT);
3273
Evan Cheng213d2cf2007-05-17 18:45:50 +00003274 if (isZeroShuffle(Op.Val))
3275 return getZeroVector(VT, DAG);
3276
Evan Cheng49892af2007-06-19 00:02:56 +00003277 if (isIdentityMask(PermMask.Val))
3278 return V1;
3279 else if (isIdentityMask(PermMask.Val, true))
3280 return V2;
3281
Evan Cheng0db9fe62006-04-25 20:13:52 +00003282 if (isSplatMask(PermMask.Val)) {
3283 if (NumElems <= 4) return Op;
3284 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003285 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003286 }
3287
Evan Cheng9bbbb982006-10-25 20:48:19 +00003288 if (X86::isMOVLMask(PermMask.Val))
3289 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003290
Evan Cheng9bbbb982006-10-25 20:48:19 +00003291 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3292 X86::isMOVSLDUPMask(PermMask.Val) ||
3293 X86::isMOVHLPSMask(PermMask.Val) ||
3294 X86::isMOVHPMask(PermMask.Val) ||
3295 X86::isMOVLPMask(PermMask.Val))
3296 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003297
Evan Cheng9bbbb982006-10-25 20:48:19 +00003298 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3299 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003300 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003301
Evan Cheng9eca5e82006-10-25 21:49:50 +00003302 bool Commuted = false;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003303 V1IsSplat = isSplatVector(V1.Val);
3304 V2IsSplat = isSplatVector(V2.Val);
3305 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003306 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003307 std::swap(V1IsSplat, V2IsSplat);
3308 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003309 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003310 }
3311
3312 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3313 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003314 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003315 if (V2IsSplat) {
3316 // V2 is a splat, so the mask may be malformed. That is, it may point
3317 // to any V2 element. The instruction selectior won't like this. Get
3318 // a corrected mask and commute to form a proper MOVS{S|D}.
3319 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3320 if (NewMask.Val != PermMask.Val)
3321 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003322 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003323 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003324 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003325
Evan Chengd9b8e402006-10-16 06:36:00 +00003326 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003327 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003328 X86::isUNPCKLMask(PermMask.Val) ||
3329 X86::isUNPCKHMask(PermMask.Val))
3330 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003331
Evan Cheng9bbbb982006-10-25 20:48:19 +00003332 if (V2IsSplat) {
3333 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003334 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003335 // new vector_shuffle with the corrected mask.
3336 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3337 if (NewMask.Val != PermMask.Val) {
3338 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3339 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3340 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3341 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3342 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3343 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003344 }
3345 }
3346 }
3347
3348 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003349 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3350 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3351
3352 if (Commuted) {
3353 // Commute is back and try unpck* again.
3354 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3355 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003356 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003357 X86::isUNPCKLMask(PermMask.Val) ||
3358 X86::isUNPCKHMask(PermMask.Val))
3359 return Op;
3360 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003361
3362 // If VT is integer, try PSHUF* first, then SHUFP*.
3363 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003364 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3365 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3366 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3367 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003368 X86::isPSHUFHWMask(PermMask.Val) ||
3369 X86::isPSHUFLWMask(PermMask.Val)) {
3370 if (V2.getOpcode() != ISD::UNDEF)
3371 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3372 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3373 return Op;
3374 }
3375
Chris Lattner07c70cd2007-05-17 17:13:13 +00003376 if (X86::isSHUFPMask(PermMask.Val) &&
3377 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003378 return Op;
3379
3380 // Handle v8i16 shuffle high / low shuffle node pair.
3381 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3382 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003383 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003384 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003385 for (unsigned i = 0; i != 4; ++i)
3386 MaskVec.push_back(PermMask.getOperand(i));
3387 for (unsigned i = 4; i != 8; ++i)
3388 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003389 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3390 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003391 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3392 MaskVec.clear();
3393 for (unsigned i = 0; i != 4; ++i)
3394 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3395 for (unsigned i = 4; i != 8; ++i)
3396 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00003397 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003398 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3399 }
3400 } else {
3401 // Floating point cases in the other order.
3402 if (X86::isSHUFPMask(PermMask.Val))
3403 return Op;
3404 if (X86::isPSHUFDMask(PermMask.Val) ||
3405 X86::isPSHUFHWMask(PermMask.Val) ||
3406 X86::isPSHUFLWMask(PermMask.Val)) {
3407 if (V2.getOpcode() != ISD::UNDEF)
3408 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3409 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3410 return Op;
3411 }
3412 }
3413
Chris Lattner07c70cd2007-05-17 17:13:13 +00003414 if (NumElems == 4 &&
3415 // Don't do this for MMX.
3416 MVT::getSizeInBits(VT) != 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003417 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003418 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003419 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003420 Locs.reserve(NumElems);
Chris Lattner5a88b832007-02-25 07:10:00 +00003421 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3422 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003423 unsigned NumHi = 0;
3424 unsigned NumLo = 0;
3425 // If no more than two elements come from either vector. This can be
3426 // implemented with two shuffles. First shuffle gather the elements.
3427 // The second shuffle, which takes the first shuffle as both of its
3428 // vector operands, put the elements into the right order.
3429 for (unsigned i = 0; i != NumElems; ++i) {
3430 SDOperand Elt = PermMask.getOperand(i);
3431 if (Elt.getOpcode() == ISD::UNDEF) {
3432 Locs[i] = std::make_pair(-1, -1);
3433 } else {
3434 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3435 if (Val < NumElems) {
3436 Locs[i] = std::make_pair(0, NumLo);
3437 Mask1[NumLo] = Elt;
3438 NumLo++;
3439 } else {
3440 Locs[i] = std::make_pair(1, NumHi);
3441 if (2+NumHi < NumElems)
3442 Mask1[2+NumHi] = Elt;
3443 NumHi++;
3444 }
3445 }
3446 }
3447 if (NumLo <= 2 && NumHi <= 2) {
3448 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003449 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3450 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003451 for (unsigned i = 0; i != NumElems; ++i) {
3452 if (Locs[i].first == -1)
3453 continue;
3454 else {
3455 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3456 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3457 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3458 }
3459 }
3460
3461 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003462 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3463 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003464 }
3465
3466 // Break it into (shuffle shuffle_hi, shuffle_lo).
3467 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003468 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3469 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3470 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003471 unsigned MaskIdx = 0;
3472 unsigned LoIdx = 0;
3473 unsigned HiIdx = NumElems/2;
3474 for (unsigned i = 0; i != NumElems; ++i) {
3475 if (i == NumElems/2) {
3476 MaskPtr = &HiMask;
3477 MaskIdx = 1;
3478 LoIdx = 0;
3479 HiIdx = NumElems/2;
3480 }
3481 SDOperand Elt = PermMask.getOperand(i);
3482 if (Elt.getOpcode() == ISD::UNDEF) {
3483 Locs[i] = std::make_pair(-1, -1);
3484 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3485 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3486 (*MaskPtr)[LoIdx] = Elt;
3487 LoIdx++;
3488 } else {
3489 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3490 (*MaskPtr)[HiIdx] = Elt;
3491 HiIdx++;
3492 }
3493 }
3494
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003495 SDOperand LoShuffle =
3496 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003497 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3498 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003499 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003500 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003501 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3502 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003503 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003504 for (unsigned i = 0; i != NumElems; ++i) {
3505 if (Locs[i].first == -1) {
3506 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3507 } else {
3508 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3509 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3510 }
3511 }
3512 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003513 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3514 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003515 }
3516
3517 return SDOperand();
3518}
3519
3520SDOperand
3521X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3522 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3523 return SDOperand();
3524
3525 MVT::ValueType VT = Op.getValueType();
3526 // TODO: handle v16i8.
3527 if (MVT::getSizeInBits(VT) == 16) {
3528 // Transform it so it match pextrw which produces a 32-bit result.
3529 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3530 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3531 Op.getOperand(0), Op.getOperand(1));
3532 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3533 DAG.getValueType(VT));
3534 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3535 } else if (MVT::getSizeInBits(VT) == 32) {
3536 SDOperand Vec = Op.getOperand(0);
3537 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3538 if (Idx == 0)
3539 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003540 // SHUFPS the element to the lowest double word, then movss.
3541 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003542 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003543 IdxVec.
3544 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3545 IdxVec.
3546 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3547 IdxVec.
3548 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3549 IdxVec.
3550 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003551 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3552 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003553 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003554 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003555 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003556 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003557 } else if (MVT::getSizeInBits(VT) == 64) {
3558 SDOperand Vec = Op.getOperand(0);
3559 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3560 if (Idx == 0)
3561 return Op;
3562
3563 // UNPCKHPD the element to the lowest double word, then movsd.
3564 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3565 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3566 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003567 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003568 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003569 IdxVec.
3570 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003571 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3572 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003573 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3574 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3575 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003576 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003577 }
3578
3579 return SDOperand();
3580}
3581
3582SDOperand
3583X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00003584 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00003585 // as its second argument.
3586 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003587 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003588 SDOperand N0 = Op.getOperand(0);
3589 SDOperand N1 = Op.getOperand(1);
3590 SDOperand N2 = Op.getOperand(2);
3591 if (MVT::getSizeInBits(BaseVT) == 16) {
3592 if (N1.getValueType() != MVT::i32)
3593 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3594 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00003595 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003596 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3597 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3598 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3599 if (Idx == 0) {
3600 // Use a movss.
3601 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3602 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman51eaa862007-06-14 22:58:02 +00003603 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003604 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003605 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3606 for (unsigned i = 1; i <= 3; ++i)
3607 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3608 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003609 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3610 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003611 } else {
3612 // Use two pinsrw instructions to insert a 32 bit value.
3613 Idx <<= 1;
3614 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng4ebcc8c2007-07-31 06:21:44 +00003615 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3616 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3617 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3618 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003619 }
3620 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3621 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003622 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003623 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3624 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003625 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003626 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3627 }
3628 }
3629
3630 return SDOperand();
3631}
3632
3633SDOperand
3634X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3635 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3636 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3637}
3638
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003639// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003640// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3641// one of the above mentioned nodes. It has to be wrapped because otherwise
3642// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3643// be used to form addressing mode. These wrapped nodes will be selected
3644// into MOV32ri.
3645SDOperand
3646X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3647 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003648 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3649 getPointerTy(),
3650 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003651 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003652 // With PIC, the address is actually $g + Offset.
3653 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3654 !Subtarget->isPICStyleRIPRel()) {
3655 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3656 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3657 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003658 }
3659
3660 return Result;
3661}
3662
3663SDOperand
3664X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3665 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003666 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003667 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003668 // With PIC, the address is actually $g + Offset.
3669 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3670 !Subtarget->isPICStyleRIPRel()) {
3671 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3672 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3673 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003675
3676 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3677 // load the value at address GV, not the value of GV itself. This means that
3678 // the GlobalAddress must be in the base or index register of the address, not
3679 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003680 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003681 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3682 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003683
3684 return Result;
3685}
3686
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003687// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3688static SDOperand
3689LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3690 const MVT::ValueType PtrVT) {
3691 SDOperand InFlag;
3692 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3693 DAG.getNode(X86ISD::GlobalBaseReg,
3694 PtrVT), InFlag);
3695 InFlag = Chain.getValue(1);
3696
3697 // emit leal symbol@TLSGD(,%ebx,1), %eax
3698 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3699 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3700 GA->getValueType(0),
3701 GA->getOffset());
3702 SDOperand Ops[] = { Chain, TGA, InFlag };
3703 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3704 InFlag = Result.getValue(2);
3705 Chain = Result.getValue(1);
3706
3707 // call ___tls_get_addr. This function receives its argument in
3708 // the register EAX.
3709 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3710 InFlag = Chain.getValue(1);
3711
3712 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3713 SDOperand Ops1[] = { Chain,
3714 DAG.getTargetExternalSymbol("___tls_get_addr",
3715 PtrVT),
3716 DAG.getRegister(X86::EAX, PtrVT),
3717 DAG.getRegister(X86::EBX, PtrVT),
3718 InFlag };
3719 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3720 InFlag = Chain.getValue(1);
3721
3722 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3723}
3724
3725// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3726// "local exec" model.
3727static SDOperand
3728LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3729 const MVT::ValueType PtrVT) {
3730 // Get the Thread Pointer
3731 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3732 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3733 // exec)
3734 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3735 GA->getValueType(0),
3736 GA->getOffset());
3737 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003738
3739 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3740 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3741
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003742 // The address of the thread local variable is the add of the thread
3743 // pointer with the offset of the variable.
3744 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3745}
3746
3747SDOperand
3748X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3749 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003750 // TODO: implement the "initial exec"model for pic executables
3751 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3752 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003753 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3754 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3755 // otherwise use the "Local Exec"TLS Model
3756 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3757 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3758 else
3759 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3760}
3761
Evan Cheng0db9fe62006-04-25 20:13:52 +00003762SDOperand
3763X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3764 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003765 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003766 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003767 // With PIC, the address is actually $g + Offset.
3768 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3769 !Subtarget->isPICStyleRIPRel()) {
3770 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3771 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3772 Result);
3773 }
3774
3775 return Result;
3776}
3777
3778SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3779 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3780 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3781 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3782 // With PIC, the address is actually $g + Offset.
3783 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3784 !Subtarget->isPICStyleRIPRel()) {
3785 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3786 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3787 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003788 }
3789
3790 return Result;
3791}
3792
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003793/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3794/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003795SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003796 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3797 "Not an i64 shift!");
3798 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3799 SDOperand ShOpLo = Op.getOperand(0);
3800 SDOperand ShOpHi = Op.getOperand(1);
3801 SDOperand ShAmt = Op.getOperand(2);
3802 SDOperand Tmp1 = isSRA ?
3803 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3804 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003805
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003806 SDOperand Tmp2, Tmp3;
3807 if (Op.getOpcode() == ISD::SHL_PARTS) {
3808 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3809 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3810 } else {
3811 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3812 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3813 }
Evan Chenge3413162006-01-09 18:33:28 +00003814
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003815 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3816 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3817 DAG.getConstant(32, MVT::i8));
3818 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3819 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00003820
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003821 SDOperand Hi, Lo;
3822 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3823 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3824 SmallVector<SDOperand, 4> Ops;
3825 if (Op.getOpcode() == ISD::SHL_PARTS) {
3826 Ops.push_back(Tmp2);
3827 Ops.push_back(Tmp3);
3828 Ops.push_back(CC);
3829 Ops.push_back(Cond);
3830 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003831
Evan Chenge3413162006-01-09 18:33:28 +00003832 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003833 Ops.push_back(Tmp3);
3834 Ops.push_back(Tmp1);
3835 Ops.push_back(CC);
3836 Ops.push_back(Cond);
3837 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3838 } else {
3839 Ops.push_back(Tmp2);
3840 Ops.push_back(Tmp3);
3841 Ops.push_back(CC);
3842 Ops.push_back(Cond);
3843 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3844
3845 Ops.clear();
3846 Ops.push_back(Tmp3);
3847 Ops.push_back(Tmp1);
3848 Ops.push_back(CC);
3849 Ops.push_back(Cond);
3850 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3851 }
3852
3853 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3854 Ops.clear();
3855 Ops.push_back(Lo);
3856 Ops.push_back(Hi);
3857 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003858}
Evan Chenga3195e82006-01-12 22:54:21 +00003859
Evan Cheng0db9fe62006-04-25 20:13:52 +00003860SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3861 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3862 Op.getOperand(0).getValueType() >= MVT::i16 &&
3863 "Unknown SINT_TO_FP to lower!");
3864
3865 SDOperand Result;
3866 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3867 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3868 MachineFunction &MF = DAG.getMachineFunction();
3869 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3870 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003871 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003872 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003873
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003874 // These are really Legal; caller falls through into that case.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003875 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3876 return Result;
3877 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003878 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003879 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3880 Subtarget->is64Bit())
3881 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003882
Evan Cheng0db9fe62006-04-25 20:13:52 +00003883 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003884 SDVTList Tys;
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003885 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3886 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003887 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00003888 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3889 else
Dale Johannesen849f2142007-07-03 00:53:03 +00003890 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003891 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003892 Ops.push_back(Chain);
3893 Ops.push_back(StackSlot);
3894 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003895 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003896 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003897
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003898 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003899 Chain = Result.getValue(1);
3900 SDOperand InFlag = Result.getValue(2);
3901
3902 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3903 // shouldn't be necessary except that RFP cannot be live across
3904 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003905 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003906 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003907 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00003908 Tys = DAG.getVTList(MVT::Other);
3909 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003910 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003911 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003912 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003913 Ops.push_back(DAG.getValueType(Op.getValueType()));
3914 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003915 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003916 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003917 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003918
Evan Cheng0db9fe62006-04-25 20:13:52 +00003919 return Result;
3920}
3921
3922SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3923 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3924 "Unknown FP_TO_SINT to lower!");
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003925 SDOperand Result;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003926
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003927 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003928 if (Op.getValueType() == MVT::i32 &&
3929 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
3930 return Result;
3931 if (Op.getValueType() == MVT::i32 &&
3932 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003933 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003934 if (Subtarget->is64Bit() &&
3935 Op.getValueType() == MVT::i64 &&
3936 Op.getOperand(0).getValueType() != MVT::f80)
3937 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003938
Evan Cheng87c89352007-10-15 20:11:21 +00003939 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3940 // stack slot.
3941 MachineFunction &MF = DAG.getMachineFunction();
3942 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3943 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3944 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003945 unsigned Opc;
3946 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003947 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3948 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3949 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3950 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003951 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003952
Evan Cheng0db9fe62006-04-25 20:13:52 +00003953 SDOperand Chain = DAG.getEntryNode();
3954 SDOperand Value = Op.getOperand(0);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003955 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3956 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003957 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003958 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00003959 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003960 SDOperand Ops[] = {
3961 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3962 };
3963 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964 Chain = Value.getValue(1);
3965 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3966 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3967 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003968
Evan Cheng0db9fe62006-04-25 20:13:52 +00003969 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00003970 SDOperand Ops[] = { Chain, Value, StackSlot };
3971 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00003972
Chris Lattner7ef1a4b2007-10-17 06:17:29 +00003973 // Load the result. If this is an i64 load on an x86-32 host, expand the
3974 // load.
3975 if (Op.getValueType() != MVT::i64 || Subtarget->is64Bit())
3976 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3977
3978 SDOperand Lo = DAG.getLoad(MVT::i32, FIST, StackSlot, NULL, 0);
3979 StackSlot = DAG.getNode(ISD::ADD, StackSlot.getValueType(), StackSlot,
3980 DAG.getConstant(StackSlot.getValueType(), 4));
3981 SDOperand Hi = DAG.getLoad(MVT::i32, FIST, StackSlot, NULL, 0);
3982
3983
3984 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003985}
3986
3987SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3988 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003989 MVT::ValueType EltVT = VT;
3990 if (MVT::isVector(VT))
3991 EltVT = MVT::getVectorElementType(VT);
3992 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003993 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00003994 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003995 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00003996 CV.push_back(C);
3997 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003998 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003999 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004000 CV.push_back(C);
4001 CV.push_back(C);
4002 CV.push_back(C);
4003 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004004 }
Dan Gohmand3006222007-07-27 17:16:43 +00004005 Constant *C = ConstantVector::get(CV);
4006 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4007 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4008 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004009 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4010}
4011
4012SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4013 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004014 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004015 unsigned EltNum = 1;
4016 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004017 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004018 EltNum = MVT::getVectorNumElements(VT);
4019 }
Dan Gohman20382522007-07-10 00:05:58 +00004020 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004021 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004022 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004023 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004024 CV.push_back(C);
4025 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004026 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004027 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004028 CV.push_back(C);
4029 CV.push_back(C);
4030 CV.push_back(C);
4031 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004032 }
Dan Gohmand3006222007-07-27 17:16:43 +00004033 Constant *C = ConstantVector::get(CV);
4034 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4035 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4036 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004037 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004038 return DAG.getNode(ISD::BIT_CONVERT, VT,
4039 DAG.getNode(ISD::XOR, MVT::v2i64,
4040 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4041 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4042 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004043 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4044 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004045}
4046
Evan Cheng68c47cb2007-01-05 07:55:56 +00004047SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004048 SDOperand Op0 = Op.getOperand(0);
4049 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004050 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004051 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004052 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004053
4054 // If second operand is smaller, extend it first.
4055 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4056 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4057 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004058 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004059 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004060 // And if it is bigger, shrink it first.
4061 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4062 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4063 SrcVT = VT;
4064 SrcTy = MVT::getTypeForValueType(SrcVT);
4065 }
4066
4067 // At this point the operands and the result should have the same
4068 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004069
Evan Cheng68c47cb2007-01-05 07:55:56 +00004070 // First get the sign bit of second operand.
4071 std::vector<Constant*> CV;
4072 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004073 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4074 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004075 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004076 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4077 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4078 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4079 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004080 }
Dan Gohmand3006222007-07-27 17:16:43 +00004081 Constant *C = ConstantVector::get(CV);
4082 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4083 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4084 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004085 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004086
4087 // Shift sign bit right or left if the two operands have different types.
4088 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4089 // Op0 is MVT::f32, Op1 is MVT::f64.
4090 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4091 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4092 DAG.getConstant(32, MVT::i32));
4093 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4094 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4095 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004096 }
4097
Evan Cheng73d6cf12007-01-05 21:37:56 +00004098 // Clear first operand sign bit.
4099 CV.clear();
4100 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004101 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4102 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004103 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004104 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4105 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4106 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4107 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004108 }
Dan Gohmand3006222007-07-27 17:16:43 +00004109 C = ConstantVector::get(CV);
4110 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4111 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4112 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004113 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4114
4115 // Or the value with the sign bit.
4116 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004117}
4118
Evan Chenge5f62042007-09-29 00:00:36 +00004119SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004120 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004121 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004122 SDOperand Op0 = Op.getOperand(0);
4123 SDOperand Op1 = Op.getOperand(1);
4124 SDOperand CC = Op.getOperand(2);
4125 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4126 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4127 unsigned X86CC;
4128
Evan Cheng0488db92007-09-25 01:57:46 +00004129 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004130 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004131 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4132 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004133 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004134 }
Evan Cheng0488db92007-09-25 01:57:46 +00004135
4136 assert(isFP && "Illegal integer SetCC!");
4137
Evan Chenge5f62042007-09-29 00:00:36 +00004138 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004139 switch (SetCCOpcode) {
4140 default: assert(false && "Illegal floating point SetCC!");
4141 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004142 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004143 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004144 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004145 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4146 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4147 }
4148 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004149 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004150 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004151 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004152 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4153 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4154 }
4155 }
4156}
4157
4158
Evan Cheng0db9fe62006-04-25 20:13:52 +00004159SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004160 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004161 SDOperand Cond = Op.getOperand(0);
4162 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004163
Evan Cheng734503b2006-09-11 02:19:56 +00004164 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004165 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004166
Evan Cheng3f41d662007-10-08 22:16:29 +00004167 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4168 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004169 if (Cond.getOpcode() == X86ISD::SETCC) {
4170 CC = Cond.getOperand(0);
4171
Evan Cheng734503b2006-09-11 02:19:56 +00004172 SDOperand Cmp = Cond.getOperand(1);
4173 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004174 MVT::ValueType VT = Op.getValueType();
4175 bool IllegalFPCMov = false;
4176 if (VT == MVT::f32 && !X86ScalarSSEf32)
4177 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4178 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4179 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesenc274f542007-10-16 18:09:08 +00004180 else if (VT == MVT::f80)
4181 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chenge5f62042007-09-29 00:00:36 +00004182 if ((Opc == X86ISD::CMP ||
4183 Opc == X86ISD::COMI ||
4184 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004185 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004186 addTest = false;
4187 }
4188 }
4189
4190 if (addTest) {
4191 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004192 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004193 }
4194
4195 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4196 MVT::Flag);
4197 SmallVector<SDOperand, 4> Ops;
4198 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4199 // condition is true.
4200 Ops.push_back(Op.getOperand(2));
4201 Ops.push_back(Op.getOperand(1));
4202 Ops.push_back(CC);
4203 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004204 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004205}
4206
Evan Cheng0db9fe62006-04-25 20:13:52 +00004207SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004208 bool addTest = true;
4209 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004210 SDOperand Cond = Op.getOperand(1);
4211 SDOperand Dest = Op.getOperand(2);
4212 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004213
Evan Cheng0db9fe62006-04-25 20:13:52 +00004214 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004215 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004216
Evan Cheng3f41d662007-10-08 22:16:29 +00004217 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4218 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004219 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004220 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004221
Evan Cheng734503b2006-09-11 02:19:56 +00004222 SDOperand Cmp = Cond.getOperand(1);
4223 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004224 if (Opc == X86ISD::CMP ||
4225 Opc == X86ISD::COMI ||
4226 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004227 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004228 addTest = false;
4229 }
4230 }
4231
4232 if (addTest) {
4233 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004234 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004235 }
Evan Chenge5f62042007-09-29 00:00:36 +00004236 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004237 Chain, Op.getOperand(2), CC, Cond);
4238}
4239
Evan Cheng32fe1032006-05-25 00:59:30 +00004240SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004241 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4242 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004243
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004244 if (Subtarget->is64Bit())
4245 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4246 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4247 else
4248 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00004249 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004250 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004251 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004252 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00004253 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004254 if (isTailCall && PerformTailCallOpt)
4255 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4256 else
4257 return LowerCCCCallTo(Op,DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004258 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004259 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004260 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004261 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004262 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004263 }
Evan Cheng32fe1032006-05-25 00:59:30 +00004264}
4265
Anton Korobeynikove060b532007-04-17 19:34:00 +00004266
4267// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4268// Calls to _alloca is needed to probe the stack when allocating more than 4k
4269// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4270// that the guard pages used by the OS virtual memory manager are allocated in
4271// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004272SDOperand
4273X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4274 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004275 assert(Subtarget->isTargetCygMing() &&
4276 "This should be used only on Cygwin/Mingw targets");
4277
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004278 // Get the inputs.
4279 SDOperand Chain = Op.getOperand(0);
4280 SDOperand Size = Op.getOperand(1);
4281 // FIXME: Ensure alignment here
4282
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004283 SDOperand Flag;
4284
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004285 MVT::ValueType IntPtr = getPointerTy();
4286 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004287
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004288 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4289 Flag = Chain.getValue(1);
4290
4291 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4292 SDOperand Ops[] = { Chain,
4293 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4294 DAG.getRegister(X86::EAX, IntPtr),
4295 Flag };
4296 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4297 Flag = Chain.getValue(1);
4298
4299 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004300
4301 std::vector<MVT::ValueType> Tys;
4302 Tys.push_back(SPTy);
4303 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004304 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4305 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004306}
4307
Evan Cheng1bc78042006-04-26 01:20:17 +00004308SDOperand
4309X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00004310 MachineFunction &MF = DAG.getMachineFunction();
4311 const Function* Fn = MF.getFunction();
4312 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00004313 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00004314 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00004315 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00004316
Evan Cheng25caf632006-05-23 21:06:34 +00004317 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00004318 if (Subtarget->is64Bit())
4319 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00004320 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004321 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004322 default:
4323 assert(0 && "Unsupported calling convention");
4324 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004325 return LowerCCCArguments(Op,DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004326 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00004327 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004328 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004329 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004330 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004331 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004332 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004333 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00004334 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004335 }
Evan Cheng1bc78042006-04-26 01:20:17 +00004336}
4337
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4339 SDOperand InFlag(0, 0);
4340 SDOperand Chain = Op.getOperand(0);
4341 unsigned Align =
4342 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4343 if (Align == 0) Align = 1;
4344
4345 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004346 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004347 // The libc version is likely to be faster for these cases. It can use the
4348 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349 if ((Align & 3) != 0 ||
Rafael Espindolafc05f402007-10-31 11:52:06 +00004350 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004351 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004352 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004353 TargetLowering::ArgListTy Args;
4354 TargetLowering::ArgListEntry Entry;
4355 Entry.Node = Op.getOperand(1);
4356 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004357 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004358 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004359 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4360 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004361 Args.push_back(Entry);
4362 Entry.Node = Op.getOperand(3);
4363 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004364 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004365 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004366 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4367 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004368 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004369
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 MVT::ValueType AVT;
4371 SDOperand Count;
4372 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4373 unsigned BytesLeft = 0;
4374 bool TwoRepStos = false;
4375 if (ValC) {
4376 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004377 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004378
Evan Cheng0db9fe62006-04-25 20:13:52 +00004379 // If the value is a constant, then we can potentially use larger sets.
4380 switch (Align & 3) {
4381 case 2: // WORD aligned
4382 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004383 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004384 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004386 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004388 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004389 Val = (Val << 8) | Val;
4390 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004391 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4392 AVT = MVT::i64;
4393 ValReg = X86::RAX;
4394 Val = (Val << 32) | Val;
4395 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004396 break;
4397 default: // Byte aligned
4398 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004400 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004402 }
4403
Evan Cheng25ab6902006-09-08 06:48:29 +00004404 if (AVT > MVT::i8) {
4405 if (I) {
4406 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4407 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4408 BytesLeft = I->getValue() % UBytes;
4409 } else {
4410 assert(AVT >= MVT::i32 &&
4411 "Do not use rep;stos if not at least DWORD aligned");
4412 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4413 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4414 TwoRepStos = true;
4415 }
4416 }
4417
Evan Cheng0db9fe62006-04-25 20:13:52 +00004418 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4419 InFlag);
4420 InFlag = Chain.getValue(1);
4421 } else {
4422 AVT = MVT::i8;
4423 Count = Op.getOperand(3);
4424 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4425 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004426 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004427
Evan Cheng25ab6902006-09-08 06:48:29 +00004428 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4429 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004430 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004431 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4432 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004433 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004434
Chris Lattnerd96d0722007-02-25 06:40:16 +00004435 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004436 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004437 Ops.push_back(Chain);
4438 Ops.push_back(DAG.getValueType(AVT));
4439 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004440 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004441
Evan Cheng0db9fe62006-04-25 20:13:52 +00004442 if (TwoRepStos) {
4443 InFlag = Chain.getValue(1);
4444 Count = Op.getOperand(3);
4445 MVT::ValueType CVT = Count.getValueType();
4446 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004447 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4448 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4449 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004450 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004451 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004452 Ops.clear();
4453 Ops.push_back(Chain);
4454 Ops.push_back(DAG.getValueType(MVT::i8));
4455 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004456 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004457 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004458 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459 SDOperand Value;
4460 unsigned Val = ValC->getValue() & 255;
4461 unsigned Offset = I->getValue() - BytesLeft;
4462 SDOperand DstAddr = Op.getOperand(1);
4463 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004464 if (BytesLeft >= 4) {
4465 Val = (Val << 8) | Val;
4466 Val = (Val << 16) | Val;
4467 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004468 Chain = DAG.getStore(Chain, Value,
4469 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4470 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004471 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004472 BytesLeft -= 4;
4473 Offset += 4;
4474 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004475 if (BytesLeft >= 2) {
4476 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004477 Chain = DAG.getStore(Chain, Value,
4478 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4479 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004480 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481 BytesLeft -= 2;
4482 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004483 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484 if (BytesLeft == 1) {
4485 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004486 Chain = DAG.getStore(Chain, Value,
4487 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4488 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004489 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004490 }
Evan Cheng386031a2006-03-24 07:29:27 +00004491 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004492
Evan Cheng0db9fe62006-04-25 20:13:52 +00004493 return Chain;
4494}
Evan Cheng11e15b32006-04-03 20:53:28 +00004495
Rafael Espindola068317b2007-09-28 12:53:01 +00004496SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4497 SDOperand Dest,
4498 SDOperand Source,
4499 unsigned Size,
4500 unsigned Align,
4501 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004502 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004503 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004504 switch (Align & 3) {
4505 case 2: // WORD aligned
4506 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004508 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004509 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004510 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4511 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004512 break;
4513 default: // Byte aligned
4514 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004515 break;
4516 }
4517
Rafael Espindola068317b2007-09-28 12:53:01 +00004518 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4519 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4520 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004521
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004523 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4524 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004525 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004526 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004527 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004528 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004529 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004530 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004531 InFlag = Chain.getValue(1);
4532
Chris Lattnerd96d0722007-02-25 06:40:16 +00004533 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004534 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004535 Ops.push_back(Chain);
4536 Ops.push_back(DAG.getValueType(AVT));
4537 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004538 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004539
Rafael Espindola068317b2007-09-28 12:53:01 +00004540 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004541 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004542 unsigned Offset = Size - BytesLeft;
4543 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004544 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004545 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004546 MVT::ValueType SrcVT = SrcAddr.getValueType();
4547 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004548 if (BytesLeft >= 4) {
4549 Value = DAG.getLoad(MVT::i32, Chain,
4550 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4551 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004552 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004553 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004554 Chain = DAG.getStore(Chain, Value,
4555 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4556 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004557 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004558 BytesLeft -= 4;
4559 Offset += 4;
4560 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561 if (BytesLeft >= 2) {
4562 Value = DAG.getLoad(MVT::i16, Chain,
4563 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4564 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004565 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004567 Chain = DAG.getStore(Chain, Value,
4568 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4569 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004570 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 BytesLeft -= 2;
4572 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004573 }
4574
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575 if (BytesLeft == 1) {
4576 Value = DAG.getLoad(MVT::i8, Chain,
4577 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4578 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004579 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004580 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004581 Chain = DAG.getStore(Chain, Value,
4582 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4583 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004584 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004585 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004586 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004587
4588 return Chain;
4589}
4590
4591SDOperand
4592X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerd96d0722007-02-25 06:40:16 +00004593 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004594 SDOperand TheOp = Op.getOperand(0);
4595 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004596 if (Subtarget->is64Bit()) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004597 SDOperand Copy1 =
4598 DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004599 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4600 MVT::i64, Copy1.getValue(2));
4601 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4602 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004603 SDOperand Ops[] = {
4604 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4605 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004606
4607 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004608 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004609 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004610
4611 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4612 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4613 MVT::i32, Copy1.getValue(2));
4614 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4615 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4616 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617}
4618
4619SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004620 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4621
Evan Cheng25ab6902006-09-08 06:48:29 +00004622 if (!Subtarget->is64Bit()) {
4623 // vastart just stores the address of the VarArgsFrameIndex slot into the
4624 // memory location argument.
4625 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004626 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4627 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004628 }
4629
4630 // __va_list_tag:
4631 // gp_offset (0 - 6 * 8)
4632 // fp_offset (48 - 48 + 8 * 16)
4633 // overflow_arg_area (point to parameters coming in memory).
4634 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004635 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004636 SDOperand FIN = Op.getOperand(1);
4637 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004638 SDOperand Store = DAG.getStore(Op.getOperand(0),
4639 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004640 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004641 MemOps.push_back(Store);
4642
4643 // Store fp_offset
4644 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4645 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00004646 Store = DAG.getStore(Op.getOperand(0),
4647 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004648 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004649 MemOps.push_back(Store);
4650
4651 // Store ptr to overflow_arg_area
4652 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4653 DAG.getConstant(4, getPointerTy()));
4654 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004655 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4656 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004657 MemOps.push_back(Store);
4658
4659 // Store ptr to reg_save_area.
4660 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4661 DAG.getConstant(8, getPointerTy()));
4662 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004663 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4664 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004665 MemOps.push_back(Store);
4666 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667}
4668
Evan Chengae642192007-03-02 23:16:35 +00004669SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4670 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4671 SDOperand Chain = Op.getOperand(0);
4672 SDOperand DstPtr = Op.getOperand(1);
4673 SDOperand SrcPtr = Op.getOperand(2);
4674 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4675 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4676
4677 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4678 SrcSV->getValue(), SrcSV->getOffset());
4679 Chain = SrcPtr.getValue(1);
4680 for (unsigned i = 0; i < 3; ++i) {
4681 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4682 SrcSV->getValue(), SrcSV->getOffset());
4683 Chain = Val.getValue(1);
4684 Chain = DAG.getStore(Chain, Val, DstPtr,
4685 DstSV->getValue(), DstSV->getOffset());
4686 if (i == 2)
4687 break;
4688 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4689 DAG.getConstant(8, getPointerTy()));
4690 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4691 DAG.getConstant(8, getPointerTy()));
4692 }
4693 return Chain;
4694}
4695
Evan Cheng0db9fe62006-04-25 20:13:52 +00004696SDOperand
4697X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4698 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4699 switch (IntNo) {
4700 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004701 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004702 case Intrinsic::x86_sse_comieq_ss:
4703 case Intrinsic::x86_sse_comilt_ss:
4704 case Intrinsic::x86_sse_comile_ss:
4705 case Intrinsic::x86_sse_comigt_ss:
4706 case Intrinsic::x86_sse_comige_ss:
4707 case Intrinsic::x86_sse_comineq_ss:
4708 case Intrinsic::x86_sse_ucomieq_ss:
4709 case Intrinsic::x86_sse_ucomilt_ss:
4710 case Intrinsic::x86_sse_ucomile_ss:
4711 case Intrinsic::x86_sse_ucomigt_ss:
4712 case Intrinsic::x86_sse_ucomige_ss:
4713 case Intrinsic::x86_sse_ucomineq_ss:
4714 case Intrinsic::x86_sse2_comieq_sd:
4715 case Intrinsic::x86_sse2_comilt_sd:
4716 case Intrinsic::x86_sse2_comile_sd:
4717 case Intrinsic::x86_sse2_comigt_sd:
4718 case Intrinsic::x86_sse2_comige_sd:
4719 case Intrinsic::x86_sse2_comineq_sd:
4720 case Intrinsic::x86_sse2_ucomieq_sd:
4721 case Intrinsic::x86_sse2_ucomilt_sd:
4722 case Intrinsic::x86_sse2_ucomile_sd:
4723 case Intrinsic::x86_sse2_ucomigt_sd:
4724 case Intrinsic::x86_sse2_ucomige_sd:
4725 case Intrinsic::x86_sse2_ucomineq_sd: {
4726 unsigned Opc = 0;
4727 ISD::CondCode CC = ISD::SETCC_INVALID;
4728 switch (IntNo) {
4729 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004730 case Intrinsic::x86_sse_comieq_ss:
4731 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004732 Opc = X86ISD::COMI;
4733 CC = ISD::SETEQ;
4734 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004735 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004736 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004737 Opc = X86ISD::COMI;
4738 CC = ISD::SETLT;
4739 break;
4740 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004741 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004742 Opc = X86ISD::COMI;
4743 CC = ISD::SETLE;
4744 break;
4745 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004746 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004747 Opc = X86ISD::COMI;
4748 CC = ISD::SETGT;
4749 break;
4750 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004751 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004752 Opc = X86ISD::COMI;
4753 CC = ISD::SETGE;
4754 break;
4755 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004756 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004757 Opc = X86ISD::COMI;
4758 CC = ISD::SETNE;
4759 break;
4760 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004761 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004762 Opc = X86ISD::UCOMI;
4763 CC = ISD::SETEQ;
4764 break;
4765 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004766 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004767 Opc = X86ISD::UCOMI;
4768 CC = ISD::SETLT;
4769 break;
4770 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004771 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004772 Opc = X86ISD::UCOMI;
4773 CC = ISD::SETLE;
4774 break;
4775 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004776 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004777 Opc = X86ISD::UCOMI;
4778 CC = ISD::SETGT;
4779 break;
4780 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004781 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004782 Opc = X86ISD::UCOMI;
4783 CC = ISD::SETGE;
4784 break;
4785 case Intrinsic::x86_sse_ucomineq_ss:
4786 case Intrinsic::x86_sse2_ucomineq_sd:
4787 Opc = X86ISD::UCOMI;
4788 CC = ISD::SETNE;
4789 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004790 }
Evan Cheng734503b2006-09-11 02:19:56 +00004791
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004793 SDOperand LHS = Op.getOperand(1);
4794 SDOperand RHS = Op.getOperand(2);
4795 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004796
Evan Chenge5f62042007-09-29 00:00:36 +00004797 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4798 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4799 DAG.getConstant(X86CC, MVT::i8), Cond);
4800 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004801 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004802 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004803}
Evan Cheng72261582005-12-20 06:22:03 +00004804
Nate Begemanbcc5f362007-01-29 22:58:52 +00004805SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4806 // Depths > 0 not supported yet!
4807 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4808 return SDOperand();
4809
4810 // Just load the return address
4811 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4812 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4813}
4814
4815SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4816 // Depths > 0 not supported yet!
4817 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4818 return SDOperand();
4819
4820 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4821 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4822 DAG.getConstant(4, getPointerTy()));
4823}
4824
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004825SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4826 SelectionDAG &DAG) {
4827 // Is not yet supported on x86-64
4828 if (Subtarget->is64Bit())
4829 return SDOperand();
4830
4831 return DAG.getConstant(8, getPointerTy());
4832}
4833
4834SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4835{
4836 assert(!Subtarget->is64Bit() &&
4837 "Lowering of eh_return builtin is not supported yet on x86-64");
4838
4839 MachineFunction &MF = DAG.getMachineFunction();
4840 SDOperand Chain = Op.getOperand(0);
4841 SDOperand Offset = Op.getOperand(1);
4842 SDOperand Handler = Op.getOperand(2);
4843
4844 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4845 getPointerTy());
4846
4847 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4848 DAG.getConstant(-4UL, getPointerTy()));
4849 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4850 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4851 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4852 MF.addLiveOut(X86::ECX);
4853
4854 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4855 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4856}
4857
Duncan Sandsb116fac2007-07-27 20:02:49 +00004858SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4859 SelectionDAG &DAG) {
4860 SDOperand Root = Op.getOperand(0);
4861 SDOperand Trmp = Op.getOperand(1); // trampoline
4862 SDOperand FPtr = Op.getOperand(2); // nested function
4863 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4864
4865 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4866
4867 if (Subtarget->is64Bit()) {
4868 return SDOperand(); // not yet supported
4869 } else {
4870 Function *Func = (Function *)
4871 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4872 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00004873 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004874
4875 switch (CC) {
4876 default:
4877 assert(0 && "Unsupported calling convention");
4878 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00004879 case CallingConv::X86_StdCall: {
4880 // Pass 'nest' parameter in ECX.
4881 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004882 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004883
4884 // Check that ECX wasn't needed by an 'inreg' parameter.
4885 const FunctionType *FTy = Func->getFunctionType();
4886 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4887
4888 if (Attrs && !Func->isVarArg()) {
4889 unsigned InRegCount = 0;
4890 unsigned Idx = 1;
4891
4892 for (FunctionType::param_iterator I = FTy->param_begin(),
4893 E = FTy->param_end(); I != E; ++I, ++Idx)
4894 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4895 // FIXME: should only count parameters that are lowered to integers.
4896 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4897
4898 if (InRegCount > 2) {
4899 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4900 abort();
4901 }
4902 }
4903 break;
4904 }
4905 case CallingConv::X86_FastCall:
4906 // Pass 'nest' parameter in EAX.
4907 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004908 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004909 break;
4910 }
4911
Duncan Sandsee465742007-08-29 19:01:20 +00004912 const X86InstrInfo *TII =
4913 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4914
Duncan Sandsb116fac2007-07-27 20:02:49 +00004915 SDOperand OutChains[4];
4916 SDOperand Addr, Disp;
4917
4918 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4919 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4920
Duncan Sandsee465742007-08-29 19:01:20 +00004921 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4922 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4923 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00004924 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4925
4926 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4927 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4928 TrmpSV->getOffset() + 1, false, 1);
4929
Duncan Sandsee465742007-08-29 19:01:20 +00004930 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004931 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4932 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4933 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4934
4935 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4936 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4937 TrmpSV->getOffset() + 6, false, 1);
4938
Duncan Sandsf7331b32007-09-11 14:10:23 +00004939 SDOperand Ops[] =
4940 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4941 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004942 }
4943}
4944
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945/// LowerOperation - Provide custom lowering hooks for some operations.
4946///
4947SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4948 switch (Op.getOpcode()) {
4949 default: assert(0 && "Should not custom lower this!");
4950 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4951 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4952 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4953 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4954 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4955 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4956 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004957 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004958 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4959 case ISD::SHL_PARTS:
4960 case ISD::SRA_PARTS:
4961 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4962 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4963 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4964 case ISD::FABS: return LowerFABS(Op, DAG);
4965 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004966 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00004967 case ISD::SETCC: return LowerSETCC(Op, DAG);
4968 case ISD::SELECT: return LowerSELECT(Op, DAG);
4969 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004970 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00004971 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004972 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00004973 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004974 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4975 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4976 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4977 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00004978 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004979 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00004980 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4981 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004982 case ISD::FRAME_TO_ARGS_OFFSET:
4983 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004984 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004985 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004986 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004987 }
Jim Laskey62819f32007-02-21 22:54:50 +00004988 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004989}
4990
Evan Cheng72261582005-12-20 06:22:03 +00004991const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4992 switch (Opcode) {
4993 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00004994 case X86ISD::SHLD: return "X86ISD::SHLD";
4995 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00004996 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004997 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00004998 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004999 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005000 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005001 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005002 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5003 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5004 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005005 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005006 case X86ISD::FST: return "X86ISD::FST";
5007 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00005008 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005009 case X86ISD::CALL: return "X86ISD::CALL";
5010 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5011 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5012 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005013 case X86ISD::COMI: return "X86ISD::COMI";
5014 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005015 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005016 case X86ISD::CMOV: return "X86ISD::CMOV";
5017 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005018 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005019 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5020 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005021 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005022 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00005023 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00005024 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00005025 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005026 case X86ISD::FMAX: return "X86ISD::FMAX";
5027 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005028 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5029 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005030 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5031 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005032 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005033 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Evan Cheng72261582005-12-20 06:22:03 +00005034 }
5035}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005036
Chris Lattnerc9addb72007-03-30 23:15:24 +00005037// isLegalAddressingMode - Return true if the addressing mode represented
5038// by AM is legal for this target, for a load/store of the specified type.
5039bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5040 const Type *Ty) const {
5041 // X86 supports extremely general addressing modes.
5042
5043 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5044 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5045 return false;
5046
5047 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005048 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005049 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5050 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005051
5052 // X86-64 only supports addr of globals in small code model.
5053 if (Subtarget->is64Bit()) {
5054 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5055 return false;
5056 // If lower 4G is not available, then we must use rip-relative addressing.
5057 if (AM.BaseOffs || AM.Scale > 1)
5058 return false;
5059 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005060 }
5061
5062 switch (AM.Scale) {
5063 case 0:
5064 case 1:
5065 case 2:
5066 case 4:
5067 case 8:
5068 // These scales always work.
5069 break;
5070 case 3:
5071 case 5:
5072 case 9:
5073 // These scales are formed with basereg+scalereg. Only accept if there is
5074 // no basereg yet.
5075 if (AM.HasBaseReg)
5076 return false;
5077 break;
5078 default: // Other stuff never works.
5079 return false;
5080 }
5081
5082 return true;
5083}
5084
5085
Evan Cheng2bd122c2007-10-26 01:56:11 +00005086bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5087 if (!Ty1->isInteger() || !Ty2->isInteger())
5088 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005089 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5090 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5091 if (NumBits1 <= NumBits2)
5092 return false;
5093 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005094}
5095
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005096bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5097 MVT::ValueType VT2) const {
5098 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5099 return false;
5100 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5101 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5102 if (NumBits1 <= NumBits2)
5103 return false;
5104 return Subtarget->is64Bit() || NumBits1 < 64;
5105}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005106
Evan Cheng60c07e12006-07-05 22:17:51 +00005107/// isShuffleMaskLegal - Targets can use this to indicate that they only
5108/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5109/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5110/// are assumed to be legal.
5111bool
5112X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5113 // Only do shuffles on 128-bit vector types for now.
5114 if (MVT::getSizeInBits(VT) == 64) return false;
5115 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005116 isIdentityMask(Mask.Val) ||
5117 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005118 isSplatMask(Mask.Val) ||
5119 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5120 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005121 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005122 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005123 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005124}
5125
5126bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5127 MVT::ValueType EVT,
5128 SelectionDAG &DAG) const {
5129 unsigned NumElts = BVOps.size();
5130 // Only do shuffles on 128-bit vector types for now.
5131 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5132 if (NumElts == 2) return true;
5133 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005134 return (isMOVLMask(&BVOps[0], 4) ||
5135 isCommutedMOVL(&BVOps[0], 4, true) ||
5136 isSHUFPMask(&BVOps[0], 4) ||
5137 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005138 }
5139 return false;
5140}
5141
5142//===----------------------------------------------------------------------===//
5143// X86 Scheduler Hooks
5144//===----------------------------------------------------------------------===//
5145
5146MachineBasicBlock *
5147X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5148 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005149 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005150 switch (MI->getOpcode()) {
5151 default: assert(false && "Unexpected instr type to insert");
5152 case X86::CMOV_FR32:
5153 case X86::CMOV_FR64:
5154 case X86::CMOV_V4F32:
5155 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005156 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005157 // To "insert" a SELECT_CC instruction, we actually have to insert the
5158 // diamond control-flow pattern. The incoming instruction knows the
5159 // destination vreg to set, the condition code register to branch on, the
5160 // true/false values to select between, and a branch opcode to use.
5161 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5162 ilist<MachineBasicBlock>::iterator It = BB;
5163 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005164
Evan Cheng60c07e12006-07-05 22:17:51 +00005165 // thisMBB:
5166 // ...
5167 // TrueVal = ...
5168 // cmpTY ccX, r1, r2
5169 // bCC copy1MBB
5170 // fallthrough --> copy0MBB
5171 MachineBasicBlock *thisMBB = BB;
5172 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5173 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005174 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005175 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005176 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005177 MachineFunction *F = BB->getParent();
5178 F->getBasicBlockList().insert(It, copy0MBB);
5179 F->getBasicBlockList().insert(It, sinkMBB);
5180 // Update machine-CFG edges by first adding all successors of the current
5181 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005182 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005183 e = BB->succ_end(); i != e; ++i)
5184 sinkMBB->addSuccessor(*i);
5185 // Next, remove all successors of the current block, and add the true
5186 // and fallthrough blocks as its successors.
5187 while(!BB->succ_empty())
5188 BB->removeSuccessor(BB->succ_begin());
5189 BB->addSuccessor(copy0MBB);
5190 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005191
Evan Cheng60c07e12006-07-05 22:17:51 +00005192 // copy0MBB:
5193 // %FalseValue = ...
5194 // # fallthrough to sinkMBB
5195 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005196
Evan Cheng60c07e12006-07-05 22:17:51 +00005197 // Update machine-CFG edges
5198 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005199
Evan Cheng60c07e12006-07-05 22:17:51 +00005200 // sinkMBB:
5201 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5202 // ...
5203 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005204 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005205 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5206 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5207
5208 delete MI; // The pseudo instruction is gone now.
5209 return BB;
5210 }
5211
Dale Johannesen849f2142007-07-03 00:53:03 +00005212 case X86::FP32_TO_INT16_IN_MEM:
5213 case X86::FP32_TO_INT32_IN_MEM:
5214 case X86::FP32_TO_INT64_IN_MEM:
5215 case X86::FP64_TO_INT16_IN_MEM:
5216 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005217 case X86::FP64_TO_INT64_IN_MEM:
5218 case X86::FP80_TO_INT16_IN_MEM:
5219 case X86::FP80_TO_INT32_IN_MEM:
5220 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005221 // Change the floating point control register to use "round towards zero"
5222 // mode when truncating to an integer value.
5223 MachineFunction *F = BB->getParent();
5224 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005225 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005226
5227 // Load the old value of the high byte of the control word...
5228 unsigned OldCW =
5229 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005230 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005231
5232 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005233 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5234 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005235
5236 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005237 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005238
5239 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005240 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5241 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005242
5243 // Get the X86 opcode to use.
5244 unsigned Opc;
5245 switch (MI->getOpcode()) {
5246 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005247 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5248 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5249 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5250 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5251 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5252 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005253 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5254 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5255 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005256 }
5257
5258 X86AddressMode AM;
5259 MachineOperand &Op = MI->getOperand(0);
5260 if (Op.isRegister()) {
5261 AM.BaseType = X86AddressMode::RegBase;
5262 AM.Base.Reg = Op.getReg();
5263 } else {
5264 AM.BaseType = X86AddressMode::FrameIndexBase;
5265 AM.Base.FrameIndex = Op.getFrameIndex();
5266 }
5267 Op = MI->getOperand(1);
5268 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005269 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005270 Op = MI->getOperand(2);
5271 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005272 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005273 Op = MI->getOperand(3);
5274 if (Op.isGlobalAddress()) {
5275 AM.GV = Op.getGlobal();
5276 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005277 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005278 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005279 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5280 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005281
5282 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005283 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005284
5285 delete MI; // The pseudo instruction is gone now.
5286 return BB;
5287 }
5288 }
5289}
5290
5291//===----------------------------------------------------------------------===//
5292// X86 Optimization Hooks
5293//===----------------------------------------------------------------------===//
5294
Nate Begeman368e18d2006-02-16 21:11:51 +00005295void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5296 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005297 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005298 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005299 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005300 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005301 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005302 assert((Opc >= ISD::BUILTIN_OP_END ||
5303 Opc == ISD::INTRINSIC_WO_CHAIN ||
5304 Opc == ISD::INTRINSIC_W_CHAIN ||
5305 Opc == ISD::INTRINSIC_VOID) &&
5306 "Should use MaskedValueIsZero if you don't know whether Op"
5307 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005308
Evan Cheng865f0602006-04-05 06:11:20 +00005309 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005310 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005311 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005312 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00005313 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5314 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005315 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005316}
Chris Lattner259e97c2006-01-31 19:43:35 +00005317
Evan Cheng206ee9d2006-07-07 08:33:52 +00005318/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5319/// element of the result of the vector shuffle.
5320static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5321 MVT::ValueType VT = N->getValueType(0);
5322 SDOperand PermMask = N->getOperand(2);
5323 unsigned NumElems = PermMask.getNumOperands();
5324 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5325 i %= NumElems;
5326 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5327 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005328 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005329 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5330 SDOperand Idx = PermMask.getOperand(i);
5331 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005332 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005333 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5334 }
5335 return SDOperand();
5336}
5337
5338/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5339/// node is a GlobalAddress + an offset.
5340static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005341 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005342 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005343 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5344 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5345 return true;
5346 }
Evan Cheng0085a282006-11-30 21:55:46 +00005347 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005348 SDOperand N1 = N->getOperand(0);
5349 SDOperand N2 = N->getOperand(1);
5350 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5351 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5352 if (V) {
5353 Offset += V->getSignExtended();
5354 return true;
5355 }
5356 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5357 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5358 if (V) {
5359 Offset += V->getSignExtended();
5360 return true;
5361 }
5362 }
5363 }
5364 return false;
5365}
5366
5367/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5368/// + Dist * Size.
5369static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5370 MachineFrameInfo *MFI) {
5371 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5372 return false;
5373
5374 SDOperand Loc = N->getOperand(1);
5375 SDOperand BaseLoc = Base->getOperand(1);
5376 if (Loc.getOpcode() == ISD::FrameIndex) {
5377 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5378 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005379 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5380 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005381 int FS = MFI->getObjectSize(FI);
5382 int BFS = MFI->getObjectSize(BFI);
5383 if (FS != BFS || FS != Size) return false;
5384 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5385 } else {
5386 GlobalValue *GV1 = NULL;
5387 GlobalValue *GV2 = NULL;
5388 int64_t Offset1 = 0;
5389 int64_t Offset2 = 0;
5390 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5391 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5392 if (isGA1 && isGA2 && GV1 == GV2)
5393 return Offset1 == (Offset2 + Dist*Size);
5394 }
5395
5396 return false;
5397}
5398
Evan Cheng1e60c092006-07-10 21:37:44 +00005399static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5400 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005401 GlobalValue *GV;
5402 int64_t Offset;
5403 if (isGAPlusOffset(Base, GV, Offset))
5404 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5405 else {
5406 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00005407 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005408 if (BFI < 0)
5409 // Fixed objects do not specify alignment, however the offsets are known.
5410 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5411 (MFI->getObjectOffset(BFI) % 16) == 0);
5412 else
5413 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005414 }
5415 return false;
5416}
5417
5418
5419/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5420/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5421/// if the load addresses are consecutive, non-overlapping, and in the right
5422/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005423static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5424 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005425 MachineFunction &MF = DAG.getMachineFunction();
5426 MachineFrameInfo *MFI = MF.getFrameInfo();
5427 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005428 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005429 SDOperand PermMask = N->getOperand(2);
5430 int NumElems = (int)PermMask.getNumOperands();
5431 SDNode *Base = NULL;
5432 for (int i = 0; i < NumElems; ++i) {
5433 SDOperand Idx = PermMask.getOperand(i);
5434 if (Idx.getOpcode() == ISD::UNDEF) {
5435 if (!Base) return SDOperand();
5436 } else {
5437 SDOperand Arg =
5438 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005439 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005440 return SDOperand();
5441 if (!Base)
5442 Base = Arg.Val;
5443 else if (!isConsecutiveLoad(Arg.Val, Base,
5444 i, MVT::getSizeInBits(EVT)/8,MFI))
5445 return SDOperand();
5446 }
5447 }
5448
Evan Cheng1e60c092006-07-10 21:37:44 +00005449 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005450 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005451 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005452 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005453 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005454 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005455 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5456 LD->getSrcValueOffset(), LD->isVolatile(),
5457 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005458 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005459}
5460
Chris Lattner83e6c992006-10-04 06:57:07 +00005461/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5462static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5463 const X86Subtarget *Subtarget) {
5464 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005465
Chris Lattner83e6c992006-10-04 06:57:07 +00005466 // If we have SSE[12] support, try to form min/max nodes.
5467 if (Subtarget->hasSSE2() &&
5468 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5469 if (Cond.getOpcode() == ISD::SETCC) {
5470 // Get the LHS/RHS of the select.
5471 SDOperand LHS = N->getOperand(1);
5472 SDOperand RHS = N->getOperand(2);
5473 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005474
Evan Cheng8ca29322006-11-10 21:43:37 +00005475 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005476 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005477 switch (CC) {
5478 default: break;
5479 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5480 case ISD::SETULE:
5481 case ISD::SETLE:
5482 if (!UnsafeFPMath) break;
5483 // FALL THROUGH.
5484 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5485 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005486 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005487 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005488
Chris Lattner1907a7b2006-10-05 04:11:26 +00005489 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5490 case ISD::SETUGT:
5491 case ISD::SETGT:
5492 if (!UnsafeFPMath) break;
5493 // FALL THROUGH.
5494 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5495 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005496 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005497 break;
5498 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005499 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005500 switch (CC) {
5501 default: break;
5502 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5503 case ISD::SETUGT:
5504 case ISD::SETGT:
5505 if (!UnsafeFPMath) break;
5506 // FALL THROUGH.
5507 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5508 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005509 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005510 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005511
Chris Lattner1907a7b2006-10-05 04:11:26 +00005512 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5513 case ISD::SETULE:
5514 case ISD::SETLE:
5515 if (!UnsafeFPMath) break;
5516 // FALL THROUGH.
5517 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5518 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005519 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005520 break;
5521 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005522 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005523
Evan Cheng8ca29322006-11-10 21:43:37 +00005524 if (Opcode)
5525 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005526 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005527
Chris Lattner83e6c992006-10-04 06:57:07 +00005528 }
5529
5530 return SDOperand();
5531}
5532
5533
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005534SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005535 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005536 SelectionDAG &DAG = DCI.DAG;
5537 switch (N->getOpcode()) {
5538 default: break;
5539 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00005540 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00005541 case ISD::SELECT:
5542 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005543 }
5544
5545 return SDOperand();
5546}
5547
Evan Cheng60c07e12006-07-05 22:17:51 +00005548//===----------------------------------------------------------------------===//
5549// X86 Inline Assembly Support
5550//===----------------------------------------------------------------------===//
5551
Chris Lattnerf4dff842006-07-11 02:54:03 +00005552/// getConstraintType - Given a constraint letter, return the type of
5553/// constraint it is for this target.
5554X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005555X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5556 if (Constraint.size() == 1) {
5557 switch (Constraint[0]) {
5558 case 'A':
5559 case 'r':
5560 case 'R':
5561 case 'l':
5562 case 'q':
5563 case 'Q':
5564 case 'x':
5565 case 'Y':
5566 return C_RegisterClass;
5567 default:
5568 break;
5569 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00005570 }
Chris Lattner4234f572007-03-25 02:14:49 +00005571 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00005572}
5573
Chris Lattner48884cd2007-08-25 00:47:38 +00005574/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5575/// vector. If it is invalid, don't add anything to Ops.
5576void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5577 char Constraint,
5578 std::vector<SDOperand>&Ops,
5579 SelectionDAG &DAG) {
5580 SDOperand Result(0, 0);
5581
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005582 switch (Constraint) {
5583 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00005584 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00005585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005586 if (C->getValue() <= 31) {
5587 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5588 break;
5589 }
Devang Patel84f7fd22007-03-17 00:13:28 +00005590 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005591 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00005592 case 'N':
5593 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005594 if (C->getValue() <= 255) {
5595 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5596 break;
5597 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00005598 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005599 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00005600 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005601 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00005602 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5603 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5604 break;
5605 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005606
Chris Lattnerdc43a882007-05-03 16:52:29 +00005607 // If we are in non-pic codegen mode, we allow the address of a global (with
5608 // an optional displacement) to be used with 'i'.
5609 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5610 int64_t Offset = 0;
5611
5612 // Match either (GA) or (GA+C)
5613 if (GA) {
5614 Offset = GA->getOffset();
5615 } else if (Op.getOpcode() == ISD::ADD) {
5616 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5617 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5618 if (C && GA) {
5619 Offset = GA->getOffset()+C->getValue();
5620 } else {
5621 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5622 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5623 if (C && GA)
5624 Offset = GA->getOffset()+C->getValue();
5625 else
5626 C = 0, GA = 0;
5627 }
5628 }
5629
5630 if (GA) {
5631 // If addressing this global requires a load (e.g. in PIC mode), we can't
5632 // match.
5633 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5634 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00005635 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005636
Chris Lattnerdc43a882007-05-03 16:52:29 +00005637 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5638 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00005639 Result = Op;
5640 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005641 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005642
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005643 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00005644 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005645 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00005646 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005647
5648 if (Result.Val) {
5649 Ops.push_back(Result);
5650 return;
5651 }
5652 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005653}
5654
Chris Lattner259e97c2006-01-31 19:43:35 +00005655std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005656getRegClassForInlineAsmConstraint(const std::string &Constraint,
5657 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005658 if (Constraint.size() == 1) {
5659 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00005660 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005661 default: break; // Unknown constraint letter
5662 case 'A': // EAX/EDX
5663 if (VT == MVT::i32 || VT == MVT::i64)
5664 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5665 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005666 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5667 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005668 if (VT == MVT::i32)
5669 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5670 else if (VT == MVT::i16)
5671 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5672 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00005673 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00005674 else if (VT == MVT::i64)
5675 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
5676 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005677 }
5678 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005679
Chris Lattner1efa40f2006-02-22 00:56:39 +00005680 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005681}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005682
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005683std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005684X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5685 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00005686 // First, see if this is a constraint that directly corresponds to an LLVM
5687 // register class.
5688 if (Constraint.size() == 1) {
5689 // GCC Constraint Letters
5690 switch (Constraint[0]) {
5691 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005692 case 'r': // GENERAL_REGS
5693 case 'R': // LEGACY_REGS
5694 case 'l': // INDEX_REGS
5695 if (VT == MVT::i64 && Subtarget->is64Bit())
5696 return std::make_pair(0U, X86::GR64RegisterClass);
5697 if (VT == MVT::i32)
5698 return std::make_pair(0U, X86::GR32RegisterClass);
5699 else if (VT == MVT::i16)
5700 return std::make_pair(0U, X86::GR16RegisterClass);
5701 else if (VT == MVT::i8)
5702 return std::make_pair(0U, X86::GR8RegisterClass);
5703 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00005704 case 'y': // MMX_REGS if MMX allowed.
5705 if (!Subtarget->hasMMX()) break;
5706 return std::make_pair(0U, X86::VR64RegisterClass);
5707 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005708 case 'Y': // SSE_REGS if SSE2 allowed
5709 if (!Subtarget->hasSSE2()) break;
5710 // FALL THROUGH.
5711 case 'x': // SSE_REGS if SSE1 allowed
5712 if (!Subtarget->hasSSE1()) break;
5713
5714 switch (VT) {
5715 default: break;
5716 // Scalar SSE types.
5717 case MVT::f32:
5718 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00005719 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005720 case MVT::f64:
5721 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00005722 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005723 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00005724 case MVT::v16i8:
5725 case MVT::v8i16:
5726 case MVT::v4i32:
5727 case MVT::v2i64:
5728 case MVT::v4f32:
5729 case MVT::v2f64:
5730 return std::make_pair(0U, X86::VR128RegisterClass);
5731 }
Chris Lattnerad043e82007-04-09 05:11:28 +00005732 break;
5733 }
5734 }
5735
Chris Lattnerf76d1802006-07-31 23:26:50 +00005736 // Use the default implementation in TargetLowering to convert the register
5737 // constraint into a member of a register class.
5738 std::pair<unsigned, const TargetRegisterClass*> Res;
5739 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00005740
5741 // Not found as a standard register?
5742 if (Res.second == 0) {
5743 // GCC calls "st(0)" just plain "st".
5744 if (StringsEqualNoCase("{st}", Constraint)) {
5745 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00005746 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00005747 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005748
Chris Lattner1a60aa72006-10-31 19:42:44 +00005749 return Res;
5750 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005751
Chris Lattnerf76d1802006-07-31 23:26:50 +00005752 // Otherwise, check to see if this is a register class of the wrong value
5753 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5754 // turn into {ax},{dx}.
5755 if (Res.second->hasType(VT))
5756 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005757
Chris Lattnerf76d1802006-07-31 23:26:50 +00005758 // All of the single-register GCC register classes map their values onto
5759 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5760 // really want an 8-bit or 32-bit register, map to the appropriate register
5761 // class and return the appropriate register.
5762 if (Res.second != X86::GR16RegisterClass)
5763 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005764
Chris Lattnerf76d1802006-07-31 23:26:50 +00005765 if (VT == MVT::i8) {
5766 unsigned DestReg = 0;
5767 switch (Res.first) {
5768 default: break;
5769 case X86::AX: DestReg = X86::AL; break;
5770 case X86::DX: DestReg = X86::DL; break;
5771 case X86::CX: DestReg = X86::CL; break;
5772 case X86::BX: DestReg = X86::BL; break;
5773 }
5774 if (DestReg) {
5775 Res.first = DestReg;
5776 Res.second = Res.second = X86::GR8RegisterClass;
5777 }
5778 } else if (VT == MVT::i32) {
5779 unsigned DestReg = 0;
5780 switch (Res.first) {
5781 default: break;
5782 case X86::AX: DestReg = X86::EAX; break;
5783 case X86::DX: DestReg = X86::EDX; break;
5784 case X86::CX: DestReg = X86::ECX; break;
5785 case X86::BX: DestReg = X86::EBX; break;
5786 case X86::SI: DestReg = X86::ESI; break;
5787 case X86::DI: DestReg = X86::EDI; break;
5788 case X86::BP: DestReg = X86::EBP; break;
5789 case X86::SP: DestReg = X86::ESP; break;
5790 }
5791 if (DestReg) {
5792 Res.first = DestReg;
5793 Res.second = Res.second = X86::GR32RegisterClass;
5794 }
Evan Cheng25ab6902006-09-08 06:48:29 +00005795 } else if (VT == MVT::i64) {
5796 unsigned DestReg = 0;
5797 switch (Res.first) {
5798 default: break;
5799 case X86::AX: DestReg = X86::RAX; break;
5800 case X86::DX: DestReg = X86::RDX; break;
5801 case X86::CX: DestReg = X86::RCX; break;
5802 case X86::BX: DestReg = X86::RBX; break;
5803 case X86::SI: DestReg = X86::RSI; break;
5804 case X86::DI: DestReg = X86::RDI; break;
5805 case X86::BP: DestReg = X86::RBP; break;
5806 case X86::SP: DestReg = X86::RSP; break;
5807 }
5808 if (DestReg) {
5809 Res.first = DestReg;
5810 Res.second = Res.second = X86::GR64RegisterClass;
5811 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005812 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005813
Chris Lattnerf76d1802006-07-31 23:26:50 +00005814 return Res;
5815}